]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/PopupWindow.py
new DC API fix
[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 try:
13 wx.PopupWindow
14 except NameError:
15 havePopupWindow = 0
16 wx.PopupWindow = wx.PopupTransientWindow = wx.Window
17
18 #---------------------------------------------------------------------------
19
20 class TestPopup(wx.PopupWindow):
21 """Adds a bit of text and mouse movement to the wx.PopupWindow"""
22 def __init__(self, parent, style):
23 wx.PopupWindow.__init__(self, parent, style)
24 self.SetBackgroundColour("CADET BLUE")
25
26 st = wx.StaticText(self, -1,
27 "This is a special kind of top level\n"
28 "window that can be used for\n"
29 "popup menus, combobox popups\n"
30 "and such.\n\n"
31 "Try positioning the demo near\n"
32 "the bottom of the screen and \n"
33 "hit the button again.\n\n"
34 "In this demo this window can\n"
35 "be dragged with the left button\n"
36 "and closed with the right."
37 ,
38 pos=(10,10))
39
40 sz = st.GetBestSize()
41 self.SetSize( (sz.width+20, sz.height+20) )
42
43 self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown)
44 self.Bind(wx.EVT_MOTION, self.OnMouseMotion)
45 self.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp)
46 self.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)
47
48 st.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown)
49 st.Bind(wx.EVT_MOTION, self.OnMouseMotion)
50 st.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp)
51 st.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)
52
53 def OnMouseLeftDown(self, evt):
54 self.ldPos = evt.GetEventObject().ClientToScreen(evt.GetPosition())
55 self.wPos = self.ClientToScreen((0,0))
56 self.CaptureMouse()
57
58 def OnMouseMotion(self, evt):
59 if evt.Dragging() and evt.LeftIsDown():
60 dPos = evt.GetEventObject().ClientToScreen(evt.GetPosition())
61 nPos = (self.wPos.x + (dPos.x - self.ldPos.x),
62 self.wPos.y + (dPos.y - self.ldPos.y))
63 self.Move(nPos)
64
65 def OnMouseLeftUp(self, evt):
66 self.ReleaseMouse()
67
68 def OnRightUp(self, evt):
69 self.Show(False)
70 self.Destroy()
71
72
73 class TestTransientPopup(wx.PopupTransientWindow):
74 """Adds a bit of text and mouse movement to the wx.PopupWindow"""
75 def __init__(self, parent, style, log):
76 wx.PopupTransientWindow.__init__(self, parent, style)
77 self.log = log
78 panel = wx.Panel(self, -1)
79 panel.SetBackgroundColour("#FFB6C1")
80 st = wx.StaticText(panel, -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 panel.SetSize( (sz.width+20, sz.height+20) )
91 self.SetSize(panel.GetSize())
92
93 def ProcessLeftDown(self, evt):
94 self.log.write("ProcessLeftDown\n")
95 return False
96
97 def OnDismiss(self):
98 self.log.write("OnDismiss\n")
99
100
101
102 class TestPanel(wx.Panel):
103 def __init__(self, parent, log):
104 wx.Panel.__init__(self, parent, -1)
105 self.log = log
106
107 b = wx.Button(self, -1, "Show wx.PopupWindow", (25, 50))
108 self.Bind(wx.EVT_BUTTON, self.OnShowPopup, b)
109
110 b = wx.Button(self, -1, "Show wx.PopupTransientWindow", (25, 95))
111 self.Bind(wx.EVT_BUTTON, self.OnShowPopupTransient, b)
112
113 # This isn't working so well, not sure why. Commented out for
114 # now.
115
116 # b = wx.Button(self, -1, "Show wx.PopupWindow with listbox", (25, 140))
117 # self.Bind(wx.EVT_BUTTON, self.OnShowPopupListbox, b)
118
119
120 def OnShowPopup(self, evt):
121 win = TestPopup(self, wx.SIMPLE_BORDER)
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, wx.SIMPLE_BORDER, self.log)
135
136 # Show the popup right below or above the button
137 # depending on available screen space...
138 btn = evt.GetEventObject()
139 pos = btn.ClientToScreen( (0,0) )
140 sz = btn.GetSize()
141 win.Position(pos, (0, sz[1]))
142
143 win.Popup()
144
145
146 def OnShowPopupListbox(self, evt):
147 win = TestPopupWithListbox(self, wx.NO_BORDER, self.log)
148
149 # Show the popup right below or above the button
150 # depending on available screen space...
151 btn = evt.GetEventObject()
152 pos = btn.ClientToScreen( (0,0) )
153 sz = btn.GetSize()
154 win.Position(pos, (0, sz[1]))
155
156 win.Show(True)
157
158 # This class is currently not implemented in the demo. It does not
159 # behave the way it should, so for the time being it's only here
160 # for show. If you figure out how to make it work, please send
161 # a corrected file to Robin!
162 class TestPopupWithListbox(wx.PopupWindow):
163 def __init__(self, parent, style, log):
164 wx.PopupWindow.__init__(self, parent, style)
165
166 import keyword
167
168 self.lb = wx.ListBox(self, -1, choices = keyword.kwlist)
169 #sz = self.lb.GetBestSize()
170 self.SetSize((150, 75)) #sz)
171 self.lb.SetSize(self.GetClientSize())
172 self.lb.SetFocus()
173 self.Bind(wx.EVT_LISTBOX, self.OnListBox)
174 self.lb.Bind(wx.EVT_LEFT_DOWN, self.OnLeft)
175
176 def OnLeft(self, evt):
177 obj = evt.GetEventObject()
178 print "OnLeft", obj
179 print 'Selected: %s' % obj.GetStringSelection()
180 obj.Show(False)
181 evt.Skip()
182
183 def OnListBox(self, evt):
184 obj = evt.GetEventObject()
185 print "OnListBox", obj
186 print 'Selected: %s' % obj.GetString()
187 evt.Skip()
188
189
190
191 #---------------------------------------------------------------------------
192
193 def runTest(frame, nb, log):
194 if havePopupWindow:
195 win = TestPanel(nb, log)
196 return win
197 else:
198 dlg = wx.MessageDialog(
199 frame, 'wx.PopupWindow is not available on this platform.',
200 'Sorry', wx.OK | wx.ICON_INFORMATION
201 )
202
203 dlg.ShowModal()
204 dlg.Destroy()
205
206 #---------------------------------------------------------------------------
207
208
209 overview = """\
210 """
211
212
213 if __name__ == '__main__':
214 import sys,os
215 import run
216 run.main(['', os.path.basename(sys.argv[0])])
217