]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/PopupMenu.py
replaced annoying wxLogDebug()s with wxLogTrace()
[wxWidgets.git] / wxPython / demo / PopupMenu.py
1
2 import wx
3
4 import images
5
6 #----------------------------------------------------------------------
7
8 text = """\
9
10 Right-click on any bare area of this panel (or Ctrl-click on the Mac)
11 to show a popup menu. Then look at the code for this sample. Notice
12 how the PopupMenu method is similar to the ShowModal method of a
13 wx.Dialog in that it doesn't return until the popup menu has been
14 dismissed. The event handlers for the popup menu items can either be
15 attached to the menu itself, or to the window that invokes PopupMenu.
16 """
17
18 #----------------------------------------------------------------------
19
20 class TestPanel(wx.Panel):
21 def __init__(self, parent, log):
22 self.log = log
23 wx.Panel.__init__(self, parent, -1)
24 box = wx.BoxSizer(wx.VERTICAL)
25
26 # Make and layout the controls
27 fs = self.GetFont().GetPointSize()
28 bf = wx.Font(fs+4, wx.SWISS, wx.NORMAL, wx.BOLD)
29 nf = wx.Font(fs+2, wx.SWISS, wx.NORMAL, wx.NORMAL)
30
31 t = wx.StaticText(self, -1, "PopupMenu")
32 t.SetFont(bf)
33 box.Add(t, 0, wx.CENTER|wx.ALL, 5)
34 self.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
35
36
37 box.Add(wx.StaticLine(self, -1), 0, wx.EXPAND)
38 box.Add((10,20))
39
40 t = wx.StaticText(self, -1, text)
41 t.SetFont(nf)
42 box.Add(t, 0, wx.CENTER|wx.ALL, 5)
43
44 self.SetSizer(box)
45
46 self.Bind(wx.EVT_RIGHT_UP, self.OnRightClick)
47
48
49 def OnRightClick(self, event):
50 self.log.WriteText("OnRightClick\n")
51
52 # only do this part the first time so the events are only bound once
53 #
54 # Yet another anternate way to do IDs. Some prefer them up top to
55 # avoid clutter, some prefer them close to the object of interest
56 # for clarity.
57 if not hasattr(self, "popupID1"):
58 self.popupID1 = wx.NewId()
59 self.popupID2 = wx.NewId()
60 self.popupID3 = wx.NewId()
61 self.popupID4 = wx.NewId()
62 self.popupID5 = wx.NewId()
63 self.popupID6 = wx.NewId()
64 self.popupID7 = wx.NewId()
65 self.popupID8 = wx.NewId()
66 self.popupID9 = wx.NewId()
67
68 self.Bind(wx.EVT_MENU, self.OnPopupOne, id=self.popupID1)
69 self.Bind(wx.EVT_MENU, self.OnPopupTwo, id=self.popupID2)
70 self.Bind(wx.EVT_MENU, self.OnPopupThree, id=self.popupID3)
71 self.Bind(wx.EVT_MENU, self.OnPopupFour, id=self.popupID4)
72 self.Bind(wx.EVT_MENU, self.OnPopupFive, id=self.popupID5)
73 self.Bind(wx.EVT_MENU, self.OnPopupSix, id=self.popupID6)
74 self.Bind(wx.EVT_MENU, self.OnPopupSeven, id=self.popupID7)
75 self.Bind(wx.EVT_MENU, self.OnPopupEight, id=self.popupID8)
76 self.Bind(wx.EVT_MENU, self.OnPopupNine, id=self.popupID9)
77
78 # make a menu
79 menu = wx.Menu()
80 # Show how to put an icon in the menu
81 item = wx.MenuItem(menu, self.popupID1,"One")
82 bmp = images.getSmilesBitmap()
83 item.SetBitmap(bmp)
84 menu.AppendItem(item)
85 # add some other items
86 menu.Append(self.popupID2, "Two")
87 menu.Append(self.popupID3, "Three")
88 menu.Append(self.popupID4, "Four")
89 menu.Append(self.popupID5, "Five")
90 menu.Append(self.popupID6, "Six")
91 # make a submenu
92 sm = wx.Menu()
93 sm.Append(self.popupID8, "sub item 1")
94 sm.Append(self.popupID9, "sub item 1")
95 menu.AppendMenu(self.popupID7, "Test Submenu", sm)
96
97
98 # Popup the menu. If an item is selected then its handler
99 # will be called before PopupMenu returns.
100 self.PopupMenu(menu, event.GetPosition())
101 menu.Destroy()
102
103
104 def OnPopupOne(self, event):
105 self.log.WriteText("Popup one\n")
106
107 def OnPopupTwo(self, event):
108 self.log.WriteText("Popup two\n")
109
110 def OnPopupThree(self, event):
111 self.log.WriteText("Popup three\n")
112
113 def OnPopupFour(self, event):
114 self.log.WriteText("Popup four\n")
115
116 def OnPopupFive(self, event):
117 self.log.WriteText("Popup five\n")
118
119 def OnPopupSix(self, event):
120 self.log.WriteText("Popup six\n")
121
122 def OnPopupSeven(self, event):
123 self.log.WriteText("Popup seven\n")
124
125 def OnPopupEight(self, event):
126 self.log.WriteText("Popup eight\n")
127
128 def OnPopupNine(self, event):
129 self.log.WriteText("Popup nine\n")
130
131
132
133
134
135 #----------------------------------------------------------------------
136
137 def runTest(frame, nb, log):
138 win = TestPanel(nb, log)
139 return win
140
141 #----------------------------------------------------------------------
142
143
144
145 overview = """<html><body>
146 <h2><center>PopupMenu</center></h2>
147 """ + text + """
148 </body></html>
149 """
150
151
152
153 if __name__ == '__main__':
154 import sys,os
155 import run
156 run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
157