]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/PopupWindow.py
It's now wx._core
[wxWidgets.git] / wxPython / demo / PopupWindow.py
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 def OnMouseLeftDown(self, evt):
52 self.ldPos = evt.GetEventObject().ClientToScreen(evt.GetPosition())
53 self.wPos = self.ClientToScreen((0,0))
54 self.CaptureMouse()
55
56 def OnMouseMotion(self, evt):
57 if evt.Dragging() and evt.LeftIsDown():
58 dPos = evt.GetEventObject().ClientToScreen(evt.GetPosition())
59 nPos = (self.wPos.x + (dPos.x - self.ldPos.x),
60 self.wPos.y + (dPos.y - self.ldPos.y))
61 self.Move(nPos)
62
63 def OnMouseLeftUp(self, evt):
64 self.ReleaseMouse()
65
66 def OnRightUp(self, evt):
67 self.Show(False)
68 self.Destroy()
69
70
71 class TestTransientPopup(wx.PopupTransientWindow):
72 """Adds a bit of text and mouse movement to the wx.PopupWindow"""
73 def __init__(self, parent, style, log):
74 wx.PopupTransientWindow.__init__(self, parent, style)
75 self.log = log
76 panel = wx.Panel(self, -1)
77 panel.SetBackgroundColour("#FFB6C1")
78 st = wx.StaticText(panel, -1,
79 "wx.PopupTransientWindow is a\n"
80 "wx.PopupWindow which disappears\n"
81 "automatically when the user\n"
82 "clicks the mouse outside it or if it\n"
83 "(or its first child) loses focus in \n"
84 "any other way."
85 ,
86 pos=(10,10))
87 sz = st.GetBestSize()
88 panel.SetSize( (sz.width+20, sz.height+20) )
89 self.SetSize(panel.GetSize())
90
91 def ProcessLeftDown(self, evt):
92 self.log.write("ProcessLeftDown\n")
93 return False
94
95 def OnDismiss(self):
96 self.log.write("OnDismiss\n")
97
98
99
100 class TestPanel(wx.Panel):
101 def __init__(self, parent, log):
102 wx.Panel.__init__(self, parent, -1)
103 self.log = log
104
105 b = wx.Button(self, -1, "Show wx.PopupWindow", (25, 50))
106 self.Bind(wx.EVT_BUTTON, self.OnShowPopup, b)
107
108 b = wx.Button(self, -1, "Show wx.PopupTransientWindow", (25, 95))
109 self.Bind(wx.EVT_BUTTON, self.OnShowPopupTransient, b)
110
111 # This isn't working so well, not sure why. Commented out for
112 # now.
113
114 # b = wx.Button(self, -1, "Show wx.PopupWindow with listbox", (25, 140))
115 # self.Bind(wx.EVT_BUTTON, self.OnShowPopupListbox, b)
116
117
118 def OnShowPopup(self, evt):
119 win = TestPopup(self, wx.SIMPLE_BORDER)
120
121 # Show the popup right below or above the button
122 # depending on available screen space...
123 btn = evt.GetEventObject()
124 pos = btn.ClientToScreen( (0,0) )
125 sz = btn.GetSize()
126 win.Position(pos, (0, sz[1]))
127
128 win.Show(True)
129
130
131 def OnShowPopupTransient(self, evt):
132 win = TestTransientPopup(self, wx.SIMPLE_BORDER, self.log)
133
134 # Show the popup right below or above the button
135 # depending on available screen space...
136 btn = evt.GetEventObject()
137 pos = btn.ClientToScreen( (0,0) )
138 sz = btn.GetSize()
139 win.Position(pos, (0, sz[1]))
140
141 win.Popup()
142
143
144 def OnShowPopupListbox(self, evt):
145 win = TestPopupWithListbox(self, wx.NO_BORDER, self.log)
146
147 # Show the popup right below or above the button
148 # depending on available screen space...
149 btn = evt.GetEventObject()
150 pos = btn.ClientToScreen( (0,0) )
151 sz = btn.GetSize()
152 win.Position(pos, (0, sz[1]))
153
154 win.Show(True)
155
156 # This class is currently not implemented in the demo. It does not
157 # behave the way it should, so for the time being it's only here
158 # for show. If you figure out how to make it work, please send
159 # a corrected file to Robin!
160 class TestPopupWithListbox(wx.PopupWindow):
161 def __init__(self, parent, style, log):
162 wx.PopupWindow.__init__(self, parent, style)
163
164 import keyword
165
166 self.lb = wx.ListBox(self, -1, choices = keyword.kwlist)
167 #sz = self.lb.GetBestSize()
168 self.SetSize((150, 75)) #sz)
169 self.lb.SetSize(self.GetClientSize())
170 self.lb.SetFocus()
171 self.Bind(wx.EVT_LISTBOX, self.OnListBox)
172 self.lb.Bind(wx.EVT_LEFT_DOWN, self.OnLeft)
173
174 def OnLeft(self, evt):
175 obj = evt.GetEventObject()
176 print "OnLeft", obj
177 print 'Selected: %s' % obj.GetStringSelection()
178 obj.Show(False)
179 evt.Skip()
180
181 def OnListBox(self, evt):
182 obj = evt.GetEventObject()
183 print "OnListBox", obj
184 print 'Selected: %s' % obj.GetString()
185 evt.Skip()
186
187
188
189 #---------------------------------------------------------------------------
190
191 def runTest(frame, nb, log):
192 if havePopupWindow:
193 win = TestPanel(nb, log)
194 return win
195 else:
196 dlg = wx.MessageDialog(
197 frame, 'wx.PopupWindow is not available on this platform.',
198 'Sorry', wx.OK | wx.ICON_INFORMATION
199 )
200 dlg.ShowModal()
201 dlg.Destroy()
202
203 #---------------------------------------------------------------------------
204
205
206 overview = """\
207 """
208
209
210 if __name__ == '__main__':
211 import sys,os
212 import run
213 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
214