]>
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.
12 if wx
.Platform
== '__WXMAC__':
14 wx
.PopupWindow
= wx
.PopupTransientWindow
= wx
.Window
16 #---------------------------------------------------------------------------
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")
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"
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."
39 self
.SetSize( (sz
.width
+20, sz
.height
+20) )
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
)
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
)
51 wx
.CallAfter(self
.Refresh
)
53 def OnMouseLeftDown(self
, evt
):
55 self
.ldPos
= evt
.GetEventObject().ClientToScreen(evt
.GetPosition())
56 self
.wPos
= self
.ClientToScreen((0,0))
59 def OnMouseMotion(self
, evt
):
60 if evt
.Dragging() and evt
.LeftIsDown():
61 dPos
= evt
.GetEventObject().ClientToScreen(evt
.GetPosition())
62 nPos
= (self
.wPos
.x
+ (dPos
.x
- self
.ldPos
.x
),
63 self
.wPos
.y
+ (dPos
.y
- self
.ldPos
.y
))
66 def OnMouseLeftUp(self
, evt
):
69 def OnRightUp(self
, evt
):
74 class TestTransientPopup(wx
.PopupTransientWindow
):
75 """Adds a bit of text and mouse movement to the wx.PopupWindow"""
76 def __init__(self
, parent
, style
, log
):
77 wx
.PopupTransientWindow
.__init
__(self
, parent
, style
)
79 panel
= wx
.Panel(self
, -1)
80 panel
.SetBackgroundColour("#FFB6C1")
81 st
= wx
.StaticText(panel
, -1,
82 "wx.PopupTransientWindow is a\n"
83 "wx.PopupWindow which disappears\n"
84 "automatically when the user\n"
85 "clicks the mouse outside it or if it\n"
86 "(or its first child) loses focus in \n"
91 panel
.SetSize( (sz
.width
+20, sz
.height
+20) )
92 self
.SetSize(panel
.GetSize())
94 def ProcessLeftDown(self
, evt
):
95 self
.log
.write("ProcessLeftDown\n")
99 self
.log
.write("OnDismiss\n")
103 class TestPanel(wx
.Panel
):
104 def __init__(self
, parent
, log
):
105 wx
.Panel
.__init
__(self
, parent
, -1)
108 b
= wx
.Button(self
, -1, "Show wx.PopupWindow", (25, 50))
109 self
.Bind(wx
.EVT_BUTTON
, self
.OnShowPopup
, b
)
111 b
= wx
.Button(self
, -1, "Show wx.PopupTransientWindow", (25, 95))
112 self
.Bind(wx
.EVT_BUTTON
, self
.OnShowPopupTransient
, b
)
114 # This isn't working so well, not sure why. Commented out for
117 # b = wx.Button(self, -1, "Show wx.PopupWindow with listbox", (25, 140))
118 # self.Bind(wx.EVT_BUTTON, self.OnShowPopupListbox, b)
121 def OnShowPopup(self
, evt
):
122 win
= TestPopup(self
, wx
.SIMPLE_BORDER
)
124 # Show the popup right below or above the button
125 # depending on available screen space...
126 btn
= evt
.GetEventObject()
127 pos
= btn
.ClientToScreen( (0,0) )
129 win
.Position(pos
, (0, sz
[1]))
134 def OnShowPopupTransient(self
, evt
):
135 win
= TestTransientPopup(self
, wx
.SIMPLE_BORDER
, self
.log
)
137 # Show the popup right below or above the button
138 # depending on available screen space...
139 btn
= evt
.GetEventObject()
140 pos
= btn
.ClientToScreen( (0,0) )
142 win
.Position(pos
, (0, sz
[1]))
147 def OnShowPopupListbox(self
, evt
):
148 win
= TestPopupWithListbox(self
, wx
.NO_BORDER
, self
.log
)
150 # Show the popup right below or above the button
151 # depending on available screen space...
152 btn
= evt
.GetEventObject()
153 pos
= btn
.ClientToScreen( (0,0) )
155 win
.Position(pos
, (0, sz
[1]))
159 # This class is currently not implemented in the demo. It does not
160 # behave the way it should, so for the time being it's only here
161 # for show. If you figure out how to make it work, please send
162 # a corrected file to Robin!
163 class TestPopupWithListbox(wx
.PopupWindow
):
164 def __init__(self
, parent
, style
, log
):
165 wx
.PopupWindow
.__init
__(self
, parent
, style
)
169 self
.lb
= wx
.ListBox(self
, -1, choices
= keyword
.kwlist
)
170 #sz = self.lb.GetBestSize()
171 self
.SetSize((150, 75)) #sz)
172 self
.lb
.SetSize(self
.GetClientSize())
174 self
.Bind(wx
.EVT_LISTBOX
, self
.OnListBox
)
175 self
.lb
.Bind(wx
.EVT_LEFT_DOWN
, self
.OnLeft
)
177 def OnLeft(self
, evt
):
178 obj
= evt
.GetEventObject()
180 print 'Selected: %s' % obj
.GetStringSelection()
184 def OnListBox(self
, evt
):
185 obj
= evt
.GetEventObject()
186 print "OnListBox", obj
187 print 'Selected: %s' % obj
.GetString()
192 #---------------------------------------------------------------------------
194 def runTest(frame
, nb
, log
):
196 win
= TestPanel(nb
, log
)
199 from Main
import MessagePanel
200 win
= MessagePanel(nb
, 'wx.PopupWindow is not available on this platform.',
201 'Sorry', wx
.ICON_WARNING
)
204 #---------------------------------------------------------------------------
211 if __name__
== '__main__':
214 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])