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