]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/PopupWindow.py
2 # 11/30/2003 - Jeff Grimmett (grimmtooth@softhome.net)
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.
16 wx
.PopupWindow
= wx
.PopupTransientWindow
= wx
.Window
18 #---------------------------------------------------------------------------
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")
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"
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."
41 self
.SetSize( (sz
.width
+20, sz
.height
+20) )
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
)
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
)
53 def OnMouseLeftDown(self
, evt
):
54 self
.ldPos
= evt
.GetEventObject().ClientToScreen(evt
.GetPosition())
55 self
.wPos
= self
.ClientToScreen((0,0))
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
))
65 def OnMouseLeftUp(self
, evt
):
68 def OnRightUp(self
, evt
):
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
)
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"
90 panel
.SetSize( (sz
.width
+20, sz
.height
+20) )
91 self
.SetSize(panel
.GetSize())
93 def ProcessLeftDown(self
, evt
):
94 self
.log
.write("ProcessLeftDown\n")
98 self
.log
.write("OnDismiss\n")
102 class TestPanel(wx
.Panel
):
103 def __init__(self
, parent
, log
):
104 wx
.Panel
.__init
__(self
, parent
, -1)
107 b
= wx
.Button(self
, -1, "Show wx.PopupWindow", (25, 50))
108 self
.Bind(wx
.EVT_BUTTON
, self
.OnShowPopup
, b
)
110 b
= wx
.Button(self
, -1, "Show wx.PopupTransientWindow", (25, 95))
111 self
.Bind(wx
.EVT_BUTTON
, self
.OnShowPopupTransient
, b
)
113 # This isn't working so well, not sure why. Commented out for
116 # b = wx.Button(self, -1, "Show wx.PopupWindow with listbox", (25, 140))
117 # self.Bind(wx.EVT_BUTTON, self.OnShowPopupListbox, b)
120 def OnShowPopup(self
, evt
):
121 win
= TestPopup(self
, wx
.SIMPLE_BORDER
)
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) )
128 win
.Position(pos
, (0, sz
[1]))
133 def OnShowPopupTransient(self
, evt
):
134 win
= TestTransientPopup(self
, wx
.SIMPLE_BORDER
, self
.log
)
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) )
141 win
.Position(pos
, (0, sz
[1]))
146 def OnShowPopupListbox(self
, evt
):
147 win
= TestPopupWithListbox(self
, wx
.NO_BORDER
, self
.log
)
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) )
154 win
.Position(pos
, (0, sz
[1]))
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
)
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
, 'wx.PopupWindow 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])])