]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/PopupWindow.py
use Bind
[wxWidgets.git] / wxPython / demo / PopupWindow.py
CommitLineData
8fa876ca
RD
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
95bfd958 6# out for now, as I found it.
8fa876ca
RD
7#
8
9import wx
0122b7e3 10
eb0f373c
RD
11havePopupWindow = 1
12try:
8fa876ca 13 wx.PopupWindow
eb0f373c
RD
14except NameError:
15 havePopupWindow = 0
8fa876ca 16 wx.PopupWindow = wx.PopupTransientWindow = wx.Window
eb0f373c 17
0122b7e3
RD
18#---------------------------------------------------------------------------
19
8fa876ca 20class TestPopup(wx.PopupWindow):
95bfd958 21 """Adds a bit of text and mouse movement to the wx.PopupWindow"""
0122b7e3 22 def __init__(self, parent, style):
8fa876ca 23 wx.PopupWindow.__init__(self, parent, style)
c0c84fa0 24 self.SetBackgroundColour("CADET BLUE")
8fa876ca
RD
25
26 st = wx.StaticText(self, -1,
c0c84fa0
RD
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))
8fa876ca 39
c0c84fa0
RD
40 sz = st.GetBestSize()
41 self.SetSize( (sz.width+20, sz.height+20) )
0122b7e3 42
8fa876ca
RD
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)
0122b7e3
RD
52
53 def OnMouseLeftDown(self, evt):
c0c84fa0 54 self.ldPos = evt.GetEventObject().ClientToScreen(evt.GetPosition())
122070e2 55 self.wPos = self.ClientToScreen((0,0))
0122b7e3
RD
56 self.CaptureMouse()
57
58 def OnMouseMotion(self, evt):
c0c84fa0
RD
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)
0122b7e3
RD
64
65 def OnMouseLeftUp(self, evt):
66 self.ReleaseMouse()
c0c84fa0 67
c0c84fa0 68 def OnRightUp(self, evt):
1e4a197e 69 self.Show(False)
c0c84fa0
RD
70 self.Destroy()
71
72
8fa876ca 73class TestTransientPopup(wx.PopupTransientWindow):
95bfd958 74 """Adds a bit of text and mouse movement to the wx.PopupWindow"""
a57d56d6 75 def __init__(self, parent, style, log):
8fa876ca 76 wx.PopupTransientWindow.__init__(self, parent, style)
a57d56d6 77 self.log = log
8fa876ca 78 panel = wx.Panel(self, -1)
a57d56d6 79 panel.SetBackgroundColour("#FFB6C1")
8fa876ca 80 st = wx.StaticText(panel, -1,
95bfd958
RD
81 "wx.PopupTransientWindow is a\n"
82 "wx.PopupWindow which disappears\n"
c0c84fa0
RD
83 "automatically when the user\n"
84 "clicks the mouse outside it or if it\n"
19ab38e7
RD
85 "(or its first child) loses focus in \n"
86 "any other way."
c0c84fa0
RD
87 ,
88 pos=(10,10))
89 sz = st.GetBestSize()
a57d56d6
RD
90 panel.SetSize( (sz.width+20, sz.height+20) )
91 self.SetSize(panel.GetSize())
0122b7e3 92
a57d56d6
RD
93 def ProcessLeftDown(self, evt):
94 self.log.write("ProcessLeftDown\n")
1e4a197e 95 return False
a57d56d6
RD
96
97 def OnDismiss(self):
98 self.log.write("OnDismiss\n")
99
0122b7e3 100
2d3a90ce 101
8fa876ca 102class TestPanel(wx.Panel):
0122b7e3 103 def __init__(self, parent, log):
8fa876ca 104 wx.Panel.__init__(self, parent, -1)
0122b7e3
RD
105 self.log = log
106
95bfd958 107 b = wx.Button(self, -1, "Show wx.PopupWindow", (25, 50))
8fa876ca 108 self.Bind(wx.EVT_BUTTON, self.OnShowPopup, b)
0122b7e3 109
95bfd958 110 b = wx.Button(self, -1, "Show wx.PopupTransientWindow", (25, 95))
8fa876ca 111 self.Bind(wx.EVT_BUTTON, self.OnShowPopupTransient, b)
c0c84fa0 112
8fa876ca
RD
113 # This isn't working so well, not sure why. Commented out for
114 # now.
115
95bfd958 116# b = wx.Button(self, -1, "Show wx.PopupWindow with listbox", (25, 140))
8fa876ca 117# self.Bind(wx.EVT_BUTTON, self.OnShowPopupListbox, b)
2d3a90ce 118
0122b7e3
RD
119
120 def OnShowPopup(self, evt):
8fa876ca 121 win = TestPopup(self, wx.SIMPLE_BORDER)
c0c84fa0
RD
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()
8fa876ca 128 win.Position(pos, (0, sz[1]))
c0c84fa0 129
1e4a197e 130 win.Show(True)
0122b7e3
RD
131
132
c0c84fa0 133 def OnShowPopupTransient(self, evt):
8fa876ca 134 win = TestTransientPopup(self, wx.SIMPLE_BORDER, self.log)
c0c84fa0 135
a57d56d6
RD
136 # Show the popup right below or above the button
137 # depending on available screen space...
c0c84fa0
RD
138 btn = evt.GetEventObject()
139 pos = btn.ClientToScreen( (0,0) )
140 sz = btn.GetSize()
8fa876ca 141 win.Position(pos, (0, sz[1]))
c0c84fa0 142
a57d56d6 143 win.Popup()
c0c84fa0
RD
144
145
25e92d2d 146 def OnShowPopupListbox(self, evt):
8fa876ca 147 win = TestPopupWithListbox(self, wx.NO_BORDER, self.log)
25e92d2d
RD
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()
8fa876ca 154 win.Position(pos, (0, sz[1]))
25e92d2d 155
1e4a197e 156 win.Show(True)
25e92d2d 157
95bfd958
RD
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!
8fa876ca 162class TestPopupWithListbox(wx.PopupWindow):
25e92d2d 163 def __init__(self, parent, style, log):
8fa876ca
RD
164 wx.PopupWindow.__init__(self, parent, style)
165
25e92d2d 166 import keyword
8fa876ca
RD
167
168 self.lb = wx.ListBox(self, -1, choices = keyword.kwlist)
25e92d2d
RD
169 #sz = self.lb.GetBestSize()
170 self.SetSize((150, 75)) #sz)
171 self.lb.SetSize(self.GetClientSize())
172 self.lb.SetFocus()
8fa876ca
RD
173 self.Bind(wx.EVT_LISTBOX, self.OnListBox)
174 self.lb.Bind(wx.EVT_LEFT_DOWN, self.OnLeft)
25e92d2d
RD
175
176 def OnLeft(self, evt):
8fa876ca
RD
177 obj = evt.GetEventObject()
178 print "OnLeft", obj
179 print 'Selected: %s' % obj.GetStringSelection()
180 obj.Show(False)
25e92d2d 181 evt.Skip()
8fa876ca 182
25e92d2d 183 def OnListBox(self, evt):
8fa876ca
RD
184 obj = evt.GetEventObject()
185 print "OnListBox", obj
186 print 'Selected: %s' % obj.GetString()
25e92d2d 187 evt.Skip()
2d3a90ce 188
2d3a90ce 189
0122b7e3
RD
190
191#---------------------------------------------------------------------------
192
193def runTest(frame, nb, log):
eb0f373c
RD
194 if havePopupWindow:
195 win = TestPanel(nb, log)
196 return win
197 else:
8fa876ca 198 dlg = wx.MessageDialog(
95bfd958 199 frame, 'wx.PopupWindow is not available on this platform.',
8fa876ca
RD
200 'Sorry', wx.OK | wx.ICON_INFORMATION
201 )
202
eb0f373c
RD
203 dlg.ShowModal()
204 dlg.Destroy()
0122b7e3
RD
205
206#---------------------------------------------------------------------------
207
208
0122b7e3
RD
209overview = """\
210"""
29c1c7f7
RD
211
212
29c1c7f7
RD
213if __name__ == '__main__':
214 import sys,os
215 import run
216 run.main(['', os.path.basename(sys.argv[0])])
217