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