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