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