| 1 | from wxPython.wx import * |
| 2 | |
| 3 | havePopupWindow = 1 |
| 4 | try: |
| 5 | wxPopupWindow |
| 6 | except NameError: |
| 7 | havePopupWindow = 0 |
| 8 | wxPopupWindow = wxPopupTransientWindow = wxWindow |
| 9 | |
| 10 | #--------------------------------------------------------------------------- |
| 11 | |
| 12 | class TestPopup(wxPopupWindow): |
| 13 | """Adds a bit of text and mouse movement to the wxPopupWindow""" |
| 14 | def __init__(self, parent, style): |
| 15 | wxPopupWindow.__init__(self, parent, style) |
| 16 | self.SetBackgroundColour("CADET BLUE") |
| 17 | st = wxStaticText(self, -1, |
| 18 | "This is a special kind of top level\n" |
| 19 | "window that can be used for\n" |
| 20 | "popup menus, combobox popups\n" |
| 21 | "and such.\n\n" |
| 22 | "Try positioning the demo near\n" |
| 23 | "the bottom of the screen and \n" |
| 24 | "hit the button again.\n\n" |
| 25 | "In this demo this window can\n" |
| 26 | "be dragged with the left button\n" |
| 27 | "and closed with the right." |
| 28 | , |
| 29 | pos=(10,10)) |
| 30 | sz = st.GetBestSize() |
| 31 | self.SetSize( (sz.width+20, sz.height+20) ) |
| 32 | |
| 33 | EVT_LEFT_DOWN(self, self.OnMouseLeftDown) |
| 34 | EVT_MOTION(self, self.OnMouseMotion) |
| 35 | EVT_LEFT_UP(self, self.OnMouseLeftUp) |
| 36 | EVT_RIGHT_UP(self, self.OnRightUp) |
| 37 | EVT_LEFT_DOWN(st, self.OnMouseLeftDown) |
| 38 | EVT_MOTION(st, self.OnMouseMotion) |
| 39 | EVT_LEFT_UP(st, self.OnMouseLeftUp) |
| 40 | EVT_RIGHT_UP(st, self.OnRightUp) |
| 41 | |
| 42 | def OnMouseLeftDown(self, evt): |
| 43 | self.ldPos = evt.GetEventObject().ClientToScreen(evt.GetPosition()) |
| 44 | self.wPos = self.GetPosition() |
| 45 | self.CaptureMouse() |
| 46 | |
| 47 | def OnMouseMotion(self, evt): |
| 48 | if evt.Dragging() and evt.LeftIsDown(): |
| 49 | dPos = evt.GetEventObject().ClientToScreen(evt.GetPosition()) |
| 50 | nPos = (self.wPos.x + (dPos.x - self.ldPos.x), |
| 51 | self.wPos.y + (dPos.y - self.ldPos.y)) |
| 52 | self.Move(nPos) |
| 53 | |
| 54 | def OnMouseLeftUp(self, evt): |
| 55 | self.ReleaseMouse() |
| 56 | |
| 57 | def OnRightUp(self, evt): |
| 58 | self.Show(false) |
| 59 | self.Destroy() |
| 60 | |
| 61 | |
| 62 | class TestTransientPopup(wxPopupTransientWindow): |
| 63 | """Adds a bit of text and mouse movement to the wxPopupWindow""" |
| 64 | def __init__(self, parent, style, log): |
| 65 | wxPopupTransientWindow.__init__(self, parent, style) |
| 66 | self.log = log |
| 67 | panel = wxPanel(self, -1) |
| 68 | panel.SetBackgroundColour("#FFB6C1") |
| 69 | st = wxStaticText(panel, -1, |
| 70 | "wxPopupTransientWindow is a\n" |
| 71 | "wxPopupWindow which disappears\n" |
| 72 | "automatically when the user\n" |
| 73 | "clicks the mouse outside it or if it\n" |
| 74 | "loses focus in any other way." |
| 75 | , |
| 76 | pos=(10,10)) |
| 77 | sz = st.GetBestSize() |
| 78 | panel.SetSize( (sz.width+20, sz.height+20) ) |
| 79 | self.SetSize(panel.GetSize()) |
| 80 | |
| 81 | def ProcessLeftDown(self, evt): |
| 82 | self.log.write("ProcessLeftDown\n") |
| 83 | return false |
| 84 | |
| 85 | def OnDismiss(self): |
| 86 | self.log.write("OnDismiss\n") |
| 87 | |
| 88 | |
| 89 | |
| 90 | class TestPanel(wxPanel): |
| 91 | def __init__(self, parent, log): |
| 92 | wxPanel.__init__(self, parent, -1) |
| 93 | self.log = log |
| 94 | |
| 95 | b = wxButton(self, -1, "Show wxPopupWindow", (25, 50)) |
| 96 | EVT_BUTTON(self, b.GetId(), self.OnShowPopup) |
| 97 | |
| 98 | b = wxButton(self, -1, "Show wxPopupTransientWindow", (25, 95)) |
| 99 | EVT_BUTTON(self, b.GetId(), self.OnShowPopupTransient) |
| 100 | |
| 101 | if 0: |
| 102 | b = wxButton(self, -1, "Show wxPopupWindow with listbox", (25, 140)) |
| 103 | EVT_BUTTON(self, b.GetId(), self.OnShowPopupListbox) |
| 104 | |
| 105 | |
| 106 | def OnShowPopup(self, evt): |
| 107 | win = TestPopup(self, wxSIMPLE_BORDER) |
| 108 | |
| 109 | # Show the popup right below or above the button |
| 110 | # depending on available screen space... |
| 111 | btn = evt.GetEventObject() |
| 112 | pos = btn.ClientToScreen( (0,0) ) |
| 113 | sz = btn.GetSize() |
| 114 | win.Position(pos, (0, sz.height)) |
| 115 | |
| 116 | win.Show(true) |
| 117 | |
| 118 | |
| 119 | def OnShowPopupTransient(self, evt): |
| 120 | win = TestTransientPopup(self, wxSIMPLE_BORDER, self.log) |
| 121 | |
| 122 | # Show the popup right below or above the button |
| 123 | # depending on available screen space... |
| 124 | btn = evt.GetEventObject() |
| 125 | pos = btn.ClientToScreen( (0,0) ) |
| 126 | sz = btn.GetSize() |
| 127 | win.Position(pos, (0, sz.height)) |
| 128 | |
| 129 | win.Popup() |
| 130 | |
| 131 | |
| 132 | def OnShowPopupListbox(self, evt): |
| 133 | win = TestPopupWithListbox(self, wxNO_BORDER, self.log) |
| 134 | |
| 135 | # Show the popup right below or above the button |
| 136 | # depending on available screen space... |
| 137 | btn = evt.GetEventObject() |
| 138 | pos = btn.ClientToScreen( (0,0) ) |
| 139 | sz = btn.GetSize() |
| 140 | win.Position(pos, (0, sz.height)) |
| 141 | |
| 142 | win.Show(true) |
| 143 | |
| 144 | class TestPopupWithListbox(wxPopupWindow): |
| 145 | def __init__(self, parent, style, log): |
| 146 | wxPopupWindow.__init__(self, parent, style) |
| 147 | import keyword |
| 148 | self.lb = wxListBox(self, -1, choices = keyword.kwlist) |
| 149 | #sz = self.lb.GetBestSize() |
| 150 | self.SetSize((150, 75)) #sz) |
| 151 | self.lb.SetSize(self.GetClientSize()) |
| 152 | self.lb.SetFocus() |
| 153 | EVT_LEFT_DOWN(self.lb, self.OnLeft) |
| 154 | EVT_LISTBOX(self, -1, self.OnListBox) |
| 155 | |
| 156 | def OnLeft(self, evt): |
| 157 | print "OnLeft", evt.GetEventObject() |
| 158 | evt.Skip() |
| 159 | def OnListBox(self, evt): |
| 160 | print "OnListBox", evt.GetEventObject() |
| 161 | evt.Skip() |
| 162 | |
| 163 | |
| 164 | |
| 165 | #--------------------------------------------------------------------------- |
| 166 | |
| 167 | def runTest(frame, nb, log): |
| 168 | if havePopupWindow: |
| 169 | win = TestPanel(nb, log) |
| 170 | return win |
| 171 | else: |
| 172 | dlg = wxMessageDialog(frame, 'wxPopupWindow is not available on this platform.', |
| 173 | 'Sorry', wxOK | wxICON_INFORMATION) |
| 174 | dlg.ShowModal() |
| 175 | dlg.Destroy() |
| 176 | |
| 177 | #--------------------------------------------------------------------------- |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | overview = """\ |
| 183 | """ |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | if __name__ == '__main__': |
| 189 | import sys,os |
| 190 | import run |
| 191 | run.main(['', os.path.basename(sys.argv[0])]) |
| 192 | |