]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxPopupWindow.py
1 # 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
5 # 11/30/2003 - Jeff Grimmett (grimmtooth@softhome.net)
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
21 wx
.PopupWindow
= wx
.PopupTransientWindow
= wx
.Window
23 #---------------------------------------------------------------------------
25 class TestPopup(wx
.PopupWindow
):
26 """Adds a bit of text and mouse movement to the wxPopupWindow"""
27 def __init__(self
, parent
, style
):
28 wx
.PopupWindow
.__init
__(self
, parent
, style
)
29 self
.SetBackgroundColour("CADET BLUE")
31 st
= wx
.StaticText(self
, -1,
32 "This is a special kind of top level\n"
33 "window that can be used for\n"
34 "popup menus, combobox popups\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."
46 self
.SetSize( (sz
.width
+20, sz
.height
+20) )
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
)
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
)
58 def OnMouseLeftDown(self
, evt
):
59 self
.ldPos
= evt
.GetEventObject().ClientToScreen(evt
.GetPosition())
60 self
.wPos
= self
.GetPosition()
63 def OnMouseMotion(self
, evt
):
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
))
70 def OnMouseLeftUp(self
, evt
):
73 def OnRightUp(self
, evt
):
78 class TestTransientPopup(wx
.PopupTransientWindow
):
79 """Adds a bit of text and mouse movement to the wxPopupWindow"""
80 def __init__(self
, parent
, style
, log
):
81 wx
.PopupTransientWindow
.__init
__(self
, parent
, style
)
83 panel
= wx
.Panel(self
, -1)
84 panel
.SetBackgroundColour("#FFB6C1")
85 st
= wx
.StaticText(panel
, -1,
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"
90 "loses focus in any other way."
94 panel
.SetSize( (sz
.width
+20, sz
.height
+20) )
95 self
.SetSize(panel
.GetSize())
97 def ProcessLeftDown(self
, evt
):
98 self
.log
.write("ProcessLeftDown\n")
102 self
.log
.write("OnDismiss\n")
106 class TestPanel(wx
.Panel
):
107 def __init__(self
, parent
, log
):
108 wx
.Panel
.__init
__(self
, parent
, -1)
111 b
= wx
.Button(self
, -1, "Show wxPopupWindow", (25, 50))
112 self
.Bind(wx
.EVT_BUTTON
, self
.OnShowPopup
, b
)
114 b
= wx
.Button(self
, -1, "Show wxPopupTransientWindow", (25, 95))
115 self
.Bind(wx
.EVT_BUTTON
, self
.OnShowPopupTransient
, b
)
117 # This isn't working so well, not sure why. Commented out for
120 # b = wx.Button(self, -1, "Show wxPopupWindow with listbox", (25, 140))
121 # self.Bind(wx.EVT_BUTTON, self.OnShowPopupListbox, b)
124 def OnShowPopup(self
, evt
):
125 win
= TestPopup(self
, wx
.SIMPLE_BORDER
)
127 # Show the popup right below or above the button
128 # depending on available screen space...
129 btn
= evt
.GetEventObject()
130 pos
= btn
.ClientToScreen( (0,0) )
132 win
.Position(pos
, (0, sz
[1]))
137 def OnShowPopupTransient(self
, evt
):
138 win
= TestTransientPopup(self
, wx
.SIMPLE_BORDER
, self
.log
)
140 # Show the popup right below or above the button
141 # depending on available screen space...
142 btn
= evt
.GetEventObject()
143 pos
= btn
.ClientToScreen( (0,0) )
145 win
.Position(pos
, (0, sz
[1]))
150 def OnShowPopupListbox(self
, evt
):
151 win
= TestPopupWithListbox(self
, wx
.NO_BORDER
, self
.log
)
153 # Show the popup right below or above the button
154 # depending on available screen space...
155 btn
= evt
.GetEventObject()
156 pos
= btn
.ClientToScreen( (0,0) )
158 win
.Position(pos
, (0, sz
[1]))
162 class TestPopupWithListbox(wx
.PopupWindow
):
163 def __init__(self
, parent
, style
, log
):
164 wx
.PopupWindow
.__init
__(self
, parent
, style
)
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())
173 self
.Bind(wx
.EVT_LISTBOX
, self
.OnListBox
)
174 self
.lb
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnLeft
)
176 def OnLeft(self
, evt
):
177 obj
= evt
.GetEventObject()
179 print 'Selected: %s' % obj
.GetStringSelection()
183 def OnListBox(self
, evt
):
184 obj
= evt
.GetEventObject()
185 print "OnListBox", obj
186 print 'Selected: %s' % obj
.GetString()
191 #---------------------------------------------------------------------------
193 def runTest(frame
, nb
, log
):
195 win
= TestPanel(nb
, log
)
198 dlg
= wx
.MessageDialog(
199 frame
, 'wxPopupWindow is not available on this platform.',
200 'Sorry', wx
.OK | wx
.ICON_INFORMATION
206 #---------------------------------------------------------------------------
213 if __name__
== '__main__':
216 run
.main(['', os
.path
.basename(sys
.argv
[0])])