| 1 | # |
| 2 | # 11/30/2003 - Jeff Grimmett (grimmtooth@softhome.net) |
| 3 | # |
| 4 | # o Some issues with the listbox example; I tried correcting |
| 5 | # it but it's still not working the way it should. Commented |
| 6 | # out for now, as I found it. |
| 7 | # |
| 8 | |
| 9 | import wx |
| 10 | |
| 11 | havePopupWindow = 1 |
| 12 | if wx.Platform == '__WXMAC__': |
| 13 | havePopupWindow = 0 |
| 14 | wx.PopupWindow = wx.PopupTransientWindow = wx.Window |
| 15 | |
| 16 | #--------------------------------------------------------------------------- |
| 17 | |
| 18 | class TestPopup(wx.PopupWindow): |
| 19 | """Adds a bit of text and mouse movement to the wx.PopupWindow""" |
| 20 | def __init__(self, parent, style): |
| 21 | wx.PopupWindow.__init__(self, parent, style) |
| 22 | self.SetBackgroundColour("CADET BLUE") |
| 23 | |
| 24 | st = wx.StaticText(self, -1, |
| 25 | "This is a special kind of top level\n" |
| 26 | "window that can be used for\n" |
| 27 | "popup menus, combobox popups\n" |
| 28 | "and such.\n\n" |
| 29 | "Try positioning the demo near\n" |
| 30 | "the bottom of the screen and \n" |
| 31 | "hit the button again.\n\n" |
| 32 | "In this demo this window can\n" |
| 33 | "be dragged with the left button\n" |
| 34 | "and closed with the right." |
| 35 | , |
| 36 | pos=(10,10)) |
| 37 | |
| 38 | sz = st.GetBestSize() |
| 39 | self.SetSize( (sz.width+20, sz.height+20) ) |
| 40 | |
| 41 | self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown) |
| 42 | self.Bind(wx.EVT_MOTION, self.OnMouseMotion) |
| 43 | self.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp) |
| 44 | self.Bind(wx.EVT_RIGHT_UP, self.OnRightUp) |
| 45 | |
| 46 | st.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown) |
| 47 | st.Bind(wx.EVT_MOTION, self.OnMouseMotion) |
| 48 | st.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp) |
| 49 | st.Bind(wx.EVT_RIGHT_UP, self.OnRightUp) |
| 50 | |
| 51 | wx.CallAfter(self.Refresh) |
| 52 | |
| 53 | def OnMouseLeftDown(self, evt): |
| 54 | self.Refresh() |
| 55 | self.ldPos = evt.GetEventObject().ClientToScreen(evt.GetPosition()) |
| 56 | self.wPos = self.ClientToScreen((0,0)) |
| 57 | self.CaptureMouse() |
| 58 | |
| 59 | def OnMouseMotion(self, evt): |
| 60 | if evt.Dragging() and evt.LeftIsDown(): |
| 61 | dPos = evt.GetEventObject().ClientToScreen(evt.GetPosition()) |
| 62 | nPos = (self.wPos.x + (dPos.x - self.ldPos.x), |
| 63 | self.wPos.y + (dPos.y - self.ldPos.y)) |
| 64 | self.Move(nPos) |
| 65 | |
| 66 | def OnMouseLeftUp(self, evt): |
| 67 | self.ReleaseMouse() |
| 68 | |
| 69 | def OnRightUp(self, evt): |
| 70 | self.Show(False) |
| 71 | self.Destroy() |
| 72 | |
| 73 | |
| 74 | class TestTransientPopup(wx.PopupTransientWindow): |
| 75 | """Adds a bit of text and mouse movement to the wx.PopupWindow""" |
| 76 | def __init__(self, parent, style, log): |
| 77 | wx.PopupTransientWindow.__init__(self, parent, style) |
| 78 | self.log = log |
| 79 | self.SetBackgroundColour("#FFB6C1") |
| 80 | st = wx.StaticText(self, -1, |
| 81 | "wx.PopupTransientWindow is a\n" |
| 82 | "wx.PopupWindow which disappears\n" |
| 83 | "automatically when the user\n" |
| 84 | "clicks the mouse outside it or if it\n" |
| 85 | "(or its first child) loses focus in \n" |
| 86 | "any other way." |
| 87 | , |
| 88 | pos=(10,10)) |
| 89 | sz = st.GetBestSize() |
| 90 | self.SetSize( (sz.width+20, sz.height+20) ) |
| 91 | |
| 92 | def ProcessLeftDown(self, evt): |
| 93 | self.log.write("ProcessLeftDown\n") |
| 94 | return False |
| 95 | |
| 96 | def OnDismiss(self): |
| 97 | self.log.write("OnDismiss\n") |
| 98 | |
| 99 | |
| 100 | |
| 101 | class TestPanel(wx.Panel): |
| 102 | def __init__(self, parent, log): |
| 103 | wx.Panel.__init__(self, parent, -1) |
| 104 | self.log = log |
| 105 | |
| 106 | b = wx.Button(self, -1, "Show wx.PopupWindow", (25, 50)) |
| 107 | self.Bind(wx.EVT_BUTTON, self.OnShowPopup, b) |
| 108 | |
| 109 | b = wx.Button(self, -1, "Show wx.PopupTransientWindow", (25, 95)) |
| 110 | self.Bind(wx.EVT_BUTTON, self.OnShowPopupTransient, b) |
| 111 | |
| 112 | # This isn't working so well, not sure why. Commented out for |
| 113 | # now. |
| 114 | |
| 115 | # b = wx.Button(self, -1, "Show wx.PopupWindow with listbox", (25, 140)) |
| 116 | # self.Bind(wx.EVT_BUTTON, self.OnShowPopupListbox, b) |
| 117 | |
| 118 | |
| 119 | def OnShowPopup(self, evt): |
| 120 | win = TestPopup(self, wx.SIMPLE_BORDER) |
| 121 | #win = TestPopupWithListbox(self, wx.SIMPLE_BORDER, self.log) |
| 122 | |
| 123 | # Show the popup right below or above the button |
| 124 | # depending on available screen space... |
| 125 | btn = evt.GetEventObject() |
| 126 | pos = btn.ClientToScreen( (0,0) ) |
| 127 | sz = btn.GetSize() |
| 128 | win.Position(pos, (0, sz[1])) |
| 129 | |
| 130 | win.Show(True) |
| 131 | |
| 132 | |
| 133 | def OnShowPopupTransient(self, evt): |
| 134 | win = TestTransientPopup(self, |
| 135 | wx.SIMPLE_BORDER, |
| 136 | self.log) |
| 137 | |
| 138 | # Show the popup right below or above the button |
| 139 | # depending on available screen space... |
| 140 | btn = evt.GetEventObject() |
| 141 | pos = btn.ClientToScreen( (0,0) ) |
| 142 | sz = btn.GetSize() |
| 143 | win.Position(pos, (0, sz[1])) |
| 144 | |
| 145 | win.Popup() |
| 146 | |
| 147 | |
| 148 | def OnShowPopupListbox(self, evt): |
| 149 | win = TestPopupWithListbox(self, wx.NO_BORDER, self.log) |
| 150 | |
| 151 | # Show the popup right below or above the button |
| 152 | # depending on available screen space... |
| 153 | btn = evt.GetEventObject() |
| 154 | pos = btn.ClientToScreen( (0,0) ) |
| 155 | sz = btn.GetSize() |
| 156 | win.Position(pos, (0, sz[1])) |
| 157 | |
| 158 | win.Show(True) |
| 159 | |
| 160 | |
| 161 | |
| 162 | # This class is currently not implemented in the demo. It does not |
| 163 | # behave the way it should, so for the time being it's only here |
| 164 | # for show. If you figure out how to make it work, please send |
| 165 | # a corrected file to Robin! |
| 166 | class TestPopupWithListbox(wx.PopupWindow): |
| 167 | def __init__(self, parent, style, log): |
| 168 | wx.PopupWindow.__init__(self, parent, style) |
| 169 | self.log = log |
| 170 | import keyword |
| 171 | self.lb = wx.ListBox(self, -1, choices = keyword.kwlist) |
| 172 | #sz = self.lb.GetBestSize() |
| 173 | self.SetSize((150, 75)) #sz) |
| 174 | self.lb.SetSize(self.GetClientSize()) |
| 175 | self.lb.SetFocus() |
| 176 | self.Bind(wx.EVT_LISTBOX, self.OnListBox) |
| 177 | self.Bind(wx.EVT_LISTBOX_DCLICK, self.OnListBoxDClick) |
| 178 | |
| 179 | def OnListBox(self, evt): |
| 180 | obj = evt.GetEventObject() |
| 181 | self.log.write("OnListBox: %s\n" % obj) |
| 182 | self.log.write('Selected: %s\n' % obj.GetString(evt.GetInt())) |
| 183 | evt.Skip() |
| 184 | |
| 185 | def OnListBoxDClick(self, evt): |
| 186 | self.Hide() |
| 187 | self.Destroy() |
| 188 | |
| 189 | #--------------------------------------------------------------------------- |
| 190 | |
| 191 | def runTest(frame, nb, log): |
| 192 | if havePopupWindow: |
| 193 | win = TestPanel(nb, log) |
| 194 | return win |
| 195 | else: |
| 196 | from Main import MessagePanel |
| 197 | win = MessagePanel(nb, 'wx.PopupWindow is not available on this platform.', |
| 198 | 'Sorry', wx.ICON_WARNING) |
| 199 | return win |
| 200 | |
| 201 | #--------------------------------------------------------------------------- |
| 202 | |
| 203 | |
| 204 | overview = """\ |
| 205 | """ |
| 206 | |
| 207 | |
| 208 | if __name__ == '__main__': |
| 209 | import sys,os |
| 210 | import run |
| 211 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |
| 212 | |