Hello,
I am rather unsure about the coordinate systems used in a wxWindow. From my
impressions gained (over quite a while) from my reading of the
documentation, it appears that each window has an origin at the top left, in
the usual sense... [incidentally, this is when using a wxPoint, which I
don't expect monks any difference--coordinates are coordinates, however
they're specified, after all...]
I want to centre a dialog on the screen. This dialog, however, may not be a
top-level window [it will nominally be the child of my main frame].
According to the documentation, the Centre method of a wxDialog will do just
that. Indeed, it worked a couple of releases ago... but it seems not to do
so now. Can anyone verify this?
Since the CentreOnScreen method is missing, I had a bash at writing my own.
A word of explanation first.
So, if we want to centre some window on the screen--whether a top-level
window or not--we should, it seems to me, traverse the parent-child chain
from the given window upwards until we reach the root window (represented by
None), adding each window's position (-vector) until we get the offset of
the given window's origin from the top-left corner of the screen, then issue
a Move command to the given window, using our new-found knowledge of this
offset, along with the size of the given window, to construct an appropriate
vector, which [and I capitalize the following because this appears to be the
source of my problem/the area which I misunderstood...] SHOULD BE A VECTOR
FROM THE ORIGIN OF THE PARENT WINDOW TO THE TOP-LEFT CORNER WHERE WE WANT
THE GIVEN WINDOW TO APPEAR (i.e., centred on the screen).
That's a bit of a mouthful. A bit of code will hopefully make things a
little clearer.
def CentreOnScreen(self,i):
ds, s = wxDisplaySize (), self.GetSizeTuple ()
w = self.GetParent()
p_rel_screen = [0,0]
while w is not None:
p_rel_screen = map(operator.add,
p_rel_screen,
w.GetPositionTuple())
## if __debug__:
## import Errors
## Errors.InformationalMessagesWindow(" position now is %s...
"
% (p_rel_screen,))
w = w.GetParent()
x,y = self.GetPositionTuple()
if i | wxBOTH or i | wxHORIZONTAL:
x = (ds[0] - s[0]) / 2 - p_rel_screen[0]
if i | wxBOTH or i | wxVERTICAL:
y = (ds[1] - s[1]) / 2 - p_rel_screen[1]
## if __debug__:
## import Errors
## Errors.InformationalMessagesWindow(" final position is %s.\n"
## % ((x, y),))
self.Move(wxPoint(x,y))
I couldn't see what's wrong with this, and the interesting thing is that the
default Centre method of my dialog appears to do the same (wrong) thing and
place the dialog with it's upper-left corner off the screen.
Have I misunderstood something about the placement of the origins of
coordinate systems of windows? Is this latter? Am I doing something
incredibly stupid in the above code? Help...!?
Thanks,
Chris Fama