]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/PopupWindow.py
I keep running into situations where the old small default window size
[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 11havePopupWindow = 1
08b89fa8 12if wx.Platform == '__WXMAC__':
eb0f373c 13 havePopupWindow = 0
8fa876ca 14 wx.PopupWindow = wx.PopupTransientWindow = wx.Window
eb0f373c 15
0122b7e3
RD
16#---------------------------------------------------------------------------
17
8fa876ca 18class TestPopup(wx.PopupWindow):
95bfd958 19 """Adds a bit of text and mouse movement to the wx.PopupWindow"""
0122b7e3 20 def __init__(self, parent, style):
8fa876ca 21 wx.PopupWindow.__init__(self, parent, style)
c0c84fa0 22 self.SetBackgroundColour("CADET BLUE")
8fa876ca
RD
23
24 st = wx.StaticText(self, -1,
c0c84fa0
RD
25 "This is a special kind of top level\n"
26 "window that can be used for\n"
27 "popup menus, combobox popups\n"
28 "and such.\n\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."
35 ,
36 pos=(10,10))
8fa876ca 37
c0c84fa0
RD
38 sz = st.GetBestSize()
39 self.SetSize( (sz.width+20, sz.height+20) )
0122b7e3 40
8fa876ca
RD
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)
45
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)
0122b7e3
RD
50
51 def OnMouseLeftDown(self, evt):
c0c84fa0 52 self.ldPos = evt.GetEventObject().ClientToScreen(evt.GetPosition())
122070e2 53 self.wPos = self.ClientToScreen((0,0))
0122b7e3
RD
54 self.CaptureMouse()
55
56 def OnMouseMotion(self, evt):
c0c84fa0
RD
57 if evt.Dragging() and evt.LeftIsDown():
58 dPos = evt.GetEventObject().ClientToScreen(evt.GetPosition())
59 nPos = (self.wPos.x + (dPos.x - self.ldPos.x),
60 self.wPos.y + (dPos.y - self.ldPos.y))
61 self.Move(nPos)
0122b7e3
RD
62
63 def OnMouseLeftUp(self, evt):
64 self.ReleaseMouse()
c0c84fa0 65
c0c84fa0 66 def OnRightUp(self, evt):
1e4a197e 67 self.Show(False)
c0c84fa0
RD
68 self.Destroy()
69
70
8fa876ca 71class TestTransientPopup(wx.PopupTransientWindow):
95bfd958 72 """Adds a bit of text and mouse movement to the wx.PopupWindow"""
a57d56d6 73 def __init__(self, parent, style, log):
8fa876ca 74 wx.PopupTransientWindow.__init__(self, parent, style)
a57d56d6 75 self.log = log
8fa876ca 76 panel = wx.Panel(self, -1)
a57d56d6 77 panel.SetBackgroundColour("#FFB6C1")
8fa876ca 78 st = wx.StaticText(panel, -1,
95bfd958
RD
79 "wx.PopupTransientWindow is a\n"
80 "wx.PopupWindow which disappears\n"
c0c84fa0
RD
81 "automatically when the user\n"
82 "clicks the mouse outside it or if it\n"
19ab38e7
RD
83 "(or its first child) loses focus in \n"
84 "any other way."
c0c84fa0
RD
85 ,
86 pos=(10,10))
87 sz = st.GetBestSize()
a57d56d6
RD
88 panel.SetSize( (sz.width+20, sz.height+20) )
89 self.SetSize(panel.GetSize())
0122b7e3 90
a57d56d6
RD
91 def ProcessLeftDown(self, evt):
92 self.log.write("ProcessLeftDown\n")
1e4a197e 93 return False
a57d56d6
RD
94
95 def OnDismiss(self):
96 self.log.write("OnDismiss\n")
97
0122b7e3 98
2d3a90ce 99
8fa876ca 100class TestPanel(wx.Panel):
0122b7e3 101 def __init__(self, parent, log):
8fa876ca 102 wx.Panel.__init__(self, parent, -1)
0122b7e3
RD
103 self.log = log
104
95bfd958 105 b = wx.Button(self, -1, "Show wx.PopupWindow", (25, 50))
8fa876ca 106 self.Bind(wx.EVT_BUTTON, self.OnShowPopup, b)
0122b7e3 107
95bfd958 108 b = wx.Button(self, -1, "Show wx.PopupTransientWindow", (25, 95))
8fa876ca 109 self.Bind(wx.EVT_BUTTON, self.OnShowPopupTransient, b)
c0c84fa0 110
8fa876ca
RD
111 # This isn't working so well, not sure why. Commented out for
112 # now.
113
95bfd958 114# b = wx.Button(self, -1, "Show wx.PopupWindow with listbox", (25, 140))
8fa876ca 115# self.Bind(wx.EVT_BUTTON, self.OnShowPopupListbox, b)
2d3a90ce 116
0122b7e3
RD
117
118 def OnShowPopup(self, evt):
8fa876ca 119 win = TestPopup(self, wx.SIMPLE_BORDER)
c0c84fa0
RD
120
121 # Show the popup right below or above the button
122 # depending on available screen space...
123 btn = evt.GetEventObject()
124 pos = btn.ClientToScreen( (0,0) )
125 sz = btn.GetSize()
8fa876ca 126 win.Position(pos, (0, sz[1]))
c0c84fa0 127
1e4a197e 128 win.Show(True)
0122b7e3
RD
129
130
c0c84fa0 131 def OnShowPopupTransient(self, evt):
8fa876ca 132 win = TestTransientPopup(self, wx.SIMPLE_BORDER, self.log)
c0c84fa0 133
a57d56d6
RD
134 # Show the popup right below or above the button
135 # depending on available screen space...
c0c84fa0
RD
136 btn = evt.GetEventObject()
137 pos = btn.ClientToScreen( (0,0) )
138 sz = btn.GetSize()
8fa876ca 139 win.Position(pos, (0, sz[1]))
c0c84fa0 140
a57d56d6 141 win.Popup()
c0c84fa0
RD
142
143
25e92d2d 144 def OnShowPopupListbox(self, evt):
8fa876ca 145 win = TestPopupWithListbox(self, wx.NO_BORDER, self.log)
25e92d2d
RD
146
147 # Show the popup right below or above the button
148 # depending on available screen space...
149 btn = evt.GetEventObject()
150 pos = btn.ClientToScreen( (0,0) )
151 sz = btn.GetSize()
8fa876ca 152 win.Position(pos, (0, sz[1]))
25e92d2d 153
1e4a197e 154 win.Show(True)
25e92d2d 155
95bfd958
RD
156# This class is currently not implemented in the demo. It does not
157# behave the way it should, so for the time being it's only here
158# for show. If you figure out how to make it work, please send
159# a corrected file to Robin!
8fa876ca 160class TestPopupWithListbox(wx.PopupWindow):
25e92d2d 161 def __init__(self, parent, style, log):
8fa876ca
RD
162 wx.PopupWindow.__init__(self, parent, style)
163
25e92d2d 164 import keyword
8fa876ca
RD
165
166 self.lb = wx.ListBox(self, -1, choices = keyword.kwlist)
25e92d2d
RD
167 #sz = self.lb.GetBestSize()
168 self.SetSize((150, 75)) #sz)
169 self.lb.SetSize(self.GetClientSize())
170 self.lb.SetFocus()
8fa876ca
RD
171 self.Bind(wx.EVT_LISTBOX, self.OnListBox)
172 self.lb.Bind(wx.EVT_LEFT_DOWN, self.OnLeft)
25e92d2d
RD
173
174 def OnLeft(self, evt):
8fa876ca
RD
175 obj = evt.GetEventObject()
176 print "OnLeft", obj
177 print 'Selected: %s' % obj.GetStringSelection()
178 obj.Show(False)
25e92d2d 179 evt.Skip()
8fa876ca 180
25e92d2d 181 def OnListBox(self, evt):
8fa876ca
RD
182 obj = evt.GetEventObject()
183 print "OnListBox", obj
184 print 'Selected: %s' % obj.GetString()
25e92d2d 185 evt.Skip()
2d3a90ce 186
2d3a90ce 187
0122b7e3
RD
188
189#---------------------------------------------------------------------------
190
191def runTest(frame, nb, log):
eb0f373c
RD
192 if havePopupWindow:
193 win = TestPanel(nb, log)
194 return win
195 else:
8fa876ca 196 dlg = wx.MessageDialog(
95bfd958 197 frame, 'wx.PopupWindow is not available on this platform.',
8fa876ca
RD
198 'Sorry', wx.OK | wx.ICON_INFORMATION
199 )
eb0f373c
RD
200 dlg.ShowModal()
201 dlg.Destroy()
0122b7e3
RD
202
203#---------------------------------------------------------------------------
204
205
0122b7e3
RD
206overview = """\
207"""
29c1c7f7
RD
208
209
29c1c7f7
RD
210if __name__ == '__main__':
211 import sys,os
212 import run
8eca4fef 213 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
29c1c7f7 214