]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/py/frame.py
Applied patch [ 827011 ] Event-based processing of item tooltips in wxTreeCtrl
[wxWidgets.git] / wxPython / wx / py / frame.py
1 """Base frame with menu."""
2
3 __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
4 __cvsid__ = "$Id$"
5 __revision__ = "$Revision$"[11:-2]
6
7 import wx
8 from version import VERSION
9
10 try:
11 True
12 except NameError:
13 True = 1==1
14 False = 1==0
15
16 ID_NEW = wx.ID_NEW
17 ID_OPEN = wx.ID_OPEN
18 ID_REVERT = wx.ID_REVERT
19 ID_CLOSE = wx.ID_CLOSE
20 ID_SAVE = wx.ID_SAVE
21 ID_SAVEAS = wx.ID_SAVEAS
22 ID_PRINT = wx.ID_PRINT
23 ID_EXIT = wx.ID_EXIT
24 ID_UNDO = wx.ID_UNDO
25 ID_REDO = wx.ID_REDO
26 ID_CUT = wx.ID_CUT
27 ID_COPY = wx.ID_COPY
28 ID_PASTE = wx.ID_PASTE
29 ID_CLEAR = wx.ID_CLEAR
30 ID_SELECTALL = wx.ID_SELECTALL
31 ID_ABOUT = wx.ID_ABOUT
32 ID_AUTOCOMP = wx.NewId()
33 ID_AUTOCOMP_SHOW = wx.NewId()
34 ID_AUTOCOMP_MAGIC = wx.NewId()
35 ID_AUTOCOMP_SINGLE = wx.NewId()
36 ID_AUTOCOMP_DOUBLE = wx.NewId()
37 ID_CALLTIPS = wx.NewId()
38 ID_CALLTIPS_SHOW = wx.NewId()
39 ID_COPY_PLUS = wx.NewId()
40 ID_NAMESPACE = wx.NewId()
41 ID_PASTE_PLUS = wx.NewId()
42 ID_WRAP = wx.NewId()
43
44
45 class Frame(wx.Frame):
46 """Frame with standard menu items."""
47
48 revision = __revision__
49
50 def __init__(self, parent=None, id=-1, title='Editor',
51 pos=wx.DefaultPosition, size=wx.DefaultSize,
52 style=wx.DEFAULT_FRAME_STYLE):
53 """Create a Frame instance."""
54 wx.Frame.__init__(self, parent, id, title, pos, size, style)
55 self.CreateStatusBar()
56 self.SetStatusText('Frame')
57 import images
58 self.SetIcon(images.getPyIcon())
59 self.__createMenus()
60 wx.EVT_CLOSE(self, self.OnClose)
61
62 def OnClose(self, event):
63 """Event handler for closing."""
64 self.Destroy()
65
66 def __createMenus(self):
67 m = self.fileMenu = wx.Menu()
68 m.Append(ID_NEW, '&New \tCtrl+N',
69 'New file')
70 m.Append(ID_OPEN, '&Open... \tCtrl+O',
71 'Open file')
72 m.AppendSeparator()
73 m.Append(ID_REVERT, '&Revert \tCtrl+R',
74 'Revert to last saved version')
75 m.Append(ID_CLOSE, '&Close \tCtrl+W',
76 'Close file')
77 m.AppendSeparator()
78 m.Append(ID_SAVE, '&Save... \tCtrl+S',
79 'Save file')
80 m.Append(ID_SAVEAS, 'Save &As \tShift+Ctrl+S',
81 'Save file with new name')
82 m.AppendSeparator()
83 m.Append(ID_PRINT, '&Print... \tCtrl+P',
84 'Print file')
85 m.AppendSeparator()
86 m.Append(ID_NAMESPACE, '&Update Namespace \tShift+Ctrl+N',
87 'Update namespace for autocompletion and calltips')
88 m.AppendSeparator()
89 m.Append(ID_EXIT, 'E&xit', 'Exit Program')
90
91 m = self.editMenu = wx.Menu()
92 m.Append(ID_UNDO, '&Undo \tCtrl+Z',
93 'Undo the last action')
94 m.Append(ID_REDO, '&Redo \tCtrl+Y',
95 'Redo the last undone action')
96 m.AppendSeparator()
97 m.Append(ID_CUT, 'Cu&t \tCtrl+X',
98 'Cut the selection')
99 m.Append(ID_COPY, '&Copy \tCtrl+C',
100 'Copy the selection')
101 m.Append(ID_COPY_PLUS, 'Cop&y Plus \tShift+Ctrl+C',
102 'Copy the selection - retaining prompts')
103 m.Append(ID_PASTE, '&Paste \tCtrl+V', 'Paste from clipboard')
104 m.Append(ID_PASTE_PLUS, 'Past&e Plus \tShift+Ctrl+V',
105 'Paste and run commands')
106 m.AppendSeparator()
107 m.Append(ID_CLEAR, 'Cle&ar',
108 'Delete the selection')
109 m.Append(ID_SELECTALL, 'Select A&ll \tCtrl+A',
110 'Select all text')
111
112 m = self.autocompMenu = wx.Menu()
113 m.Append(ID_AUTOCOMP_SHOW, 'Show Auto Completion',
114 'Show auto completion list', 1)
115 m.Append(ID_AUTOCOMP_MAGIC, 'Include Magic Attributes',
116 'Include attributes visible to __getattr__ and __setattr__',
117 1)
118 m.Append(ID_AUTOCOMP_SINGLE, 'Include Single Underscores',
119 'Include attibutes prefixed by a single underscore', 1)
120 m.Append(ID_AUTOCOMP_DOUBLE, 'Include Double Underscores',
121 'Include attibutes prefixed by a double underscore', 1)
122
123 m = self.calltipsMenu = wx.Menu()
124 m.Append(ID_CALLTIPS_SHOW, 'Show Call Tips',
125 'Show call tips with argument signature and docstring', 1)
126
127 m = self.optionsMenu = wx.Menu()
128 m.AppendMenu(ID_AUTOCOMP, '&Auto Completion', self.autocompMenu,
129 'Auto Completion Options')
130 m.AppendMenu(ID_CALLTIPS, '&Call Tips', self.calltipsMenu,
131 'Call Tip Options')
132 m.Append(ID_WRAP, '&Wrap Lines',
133 'Wrap lines at right edge', 1)
134
135 m = self.helpMenu = wx.Menu()
136 m.AppendSeparator()
137 m.Append(ID_ABOUT, '&About...', 'About this program')
138
139 b = self.menuBar = wx.MenuBar()
140 b.Append(self.fileMenu, '&File')
141 b.Append(self.editMenu, '&Edit')
142 b.Append(self.optionsMenu, '&Options')
143 b.Append(self.helpMenu, '&Help')
144 self.SetMenuBar(b)
145
146 wx.EVT_MENU(self, ID_NEW, self.OnFileNew)
147 wx.EVT_MENU(self, ID_OPEN, self.OnFileOpen)
148 wx.EVT_MENU(self, ID_REVERT, self.OnFileRevert)
149 wx.EVT_MENU(self, ID_CLOSE, self.OnFileClose)
150 wx.EVT_MENU(self, ID_SAVE, self.OnFileSave)
151 wx.EVT_MENU(self, ID_SAVEAS, self.OnFileSaveAs)
152 wx.EVT_MENU(self, ID_NAMESPACE, self.OnFileUpdateNamespace)
153 wx.EVT_MENU(self, ID_PRINT, self.OnFilePrint)
154 wx.EVT_MENU(self, ID_EXIT, self.OnExit)
155 wx.EVT_MENU(self, ID_UNDO, self.OnUndo)
156 wx.EVT_MENU(self, ID_REDO, self.OnRedo)
157 wx.EVT_MENU(self, ID_CUT, self.OnCut)
158 wx.EVT_MENU(self, ID_COPY, self.OnCopy)
159 wx.EVT_MENU(self, ID_COPY_PLUS, self.OnCopyPlus)
160 wx.EVT_MENU(self, ID_PASTE, self.OnPaste)
161 wx.EVT_MENU(self, ID_PASTE_PLUS, self.OnPastePlus)
162 wx.EVT_MENU(self, ID_CLEAR, self.OnClear)
163 wx.EVT_MENU(self, ID_SELECTALL, self.OnSelectAll)
164 wx.EVT_MENU(self, ID_ABOUT, self.OnAbout)
165 wx.EVT_MENU(self, ID_AUTOCOMP_SHOW, self.OnAutoCompleteShow)
166 wx.EVT_MENU(self, ID_AUTOCOMP_MAGIC, self.OnAutoCompleteMagic)
167 wx.EVT_MENU(self, ID_AUTOCOMP_SINGLE, self.OnAutoCompleteSingle)
168 wx.EVT_MENU(self, ID_AUTOCOMP_DOUBLE, self.OnAutoCompleteDouble)
169 wx.EVT_MENU(self, ID_CALLTIPS_SHOW, self.OnCallTipsShow)
170 wx.EVT_MENU(self, ID_WRAP, self.OnWrap)
171
172 wx.EVT_UPDATE_UI(self, ID_NEW, self.OnUpdateMenu)
173 wx.EVT_UPDATE_UI(self, ID_OPEN, self.OnUpdateMenu)
174 wx.EVT_UPDATE_UI(self, ID_REVERT, self.OnUpdateMenu)
175 wx.EVT_UPDATE_UI(self, ID_CLOSE, self.OnUpdateMenu)
176 wx.EVT_UPDATE_UI(self, ID_SAVE, self.OnUpdateMenu)
177 wx.EVT_UPDATE_UI(self, ID_SAVEAS, self.OnUpdateMenu)
178 wx.EVT_UPDATE_UI(self, ID_NAMESPACE, self.OnUpdateMenu)
179 wx.EVT_UPDATE_UI(self, ID_PRINT, self.OnUpdateMenu)
180 wx.EVT_UPDATE_UI(self, ID_UNDO, self.OnUpdateMenu)
181 wx.EVT_UPDATE_UI(self, ID_REDO, self.OnUpdateMenu)
182 wx.EVT_UPDATE_UI(self, ID_CUT, self.OnUpdateMenu)
183 wx.EVT_UPDATE_UI(self, ID_COPY, self.OnUpdateMenu)
184 wx.EVT_UPDATE_UI(self, ID_COPY_PLUS, self.OnUpdateMenu)
185 wx.EVT_UPDATE_UI(self, ID_PASTE, self.OnUpdateMenu)
186 wx.EVT_UPDATE_UI(self, ID_PASTE_PLUS, self.OnUpdateMenu)
187 wx.EVT_UPDATE_UI(self, ID_CLEAR, self.OnUpdateMenu)
188 wx.EVT_UPDATE_UI(self, ID_SELECTALL, self.OnUpdateMenu)
189 wx.EVT_UPDATE_UI(self, ID_AUTOCOMP_SHOW, self.OnUpdateMenu)
190 wx.EVT_UPDATE_UI(self, ID_AUTOCOMP_MAGIC, self.OnUpdateMenu)
191 wx.EVT_UPDATE_UI(self, ID_AUTOCOMP_SINGLE, self.OnUpdateMenu)
192 wx.EVT_UPDATE_UI(self, ID_AUTOCOMP_DOUBLE, self.OnUpdateMenu)
193 wx.EVT_UPDATE_UI(self, ID_CALLTIPS_SHOW, self.OnUpdateMenu)
194 wx.EVT_UPDATE_UI(self, ID_WRAP, self.OnUpdateMenu)
195
196 def OnFileNew(self, event):
197 self.bufferNew()
198
199 def OnFileOpen(self, event):
200 self.bufferOpen()
201
202 def OnFileRevert(self, event):
203 self.bufferRevert()
204
205 def OnFileClose(self, event):
206 self.bufferClose()
207
208 def OnFileSave(self, event):
209 self.bufferSave()
210
211 def OnFileSaveAs(self, event):
212 self.bufferSaveAs()
213
214 def OnFileUpdateNamespace(self, event):
215 self.updateNamespace()
216
217 def OnFilePrint(self, event):
218 self.bufferPrint()
219
220 def OnExit(self, event):
221 self.Close(False)
222
223 def OnUndo(self, event):
224 win = wx.Window_FindFocus()
225 win.Undo()
226
227 def OnRedo(self, event):
228 win = wx.Window_FindFocus()
229 win.Redo()
230
231 def OnCut(self, event):
232 win = wx.Window_FindFocus()
233 win.Cut()
234
235 def OnCopy(self, event):
236 win = wx.Window_FindFocus()
237 win.Copy()
238
239 def OnCopyPlus(self, event):
240 win = wx.Window_FindFocus()
241 win.CopyWithPrompts()
242
243 def OnPaste(self, event):
244 win = wx.Window_FindFocus()
245 win.Paste()
246
247 def OnPastePlus(self, event):
248 win = wx.Window_FindFocus()
249 win.PasteAndRun()
250
251 def OnClear(self, event):
252 win = wx.Window_FindFocus()
253 win.Clear()
254
255 def OnSelectAll(self, event):
256 win = wx.Window_FindFocus()
257 win.SelectAll()
258
259 def OnAbout(self, event):
260 """Display an About window."""
261 title = 'About'
262 text = 'Your message here.'
263 dialog = wx.MessageDialog(self, text, title,
264 wx.OK | wx.ICON_INFORMATION)
265 dialog.ShowModal()
266 dialog.Destroy()
267
268 def OnAutoCompleteShow(self, event):
269 win = wx.Window_FindFocus()
270 win.autoComplete = event.IsChecked()
271
272 def OnAutoCompleteMagic(self, event):
273 win = wx.Window_FindFocus()
274 win.autoCompleteIncludeMagic = event.IsChecked()
275
276 def OnAutoCompleteSingle(self, event):
277 win = wx.Window_FindFocus()
278 win.autoCompleteIncludeSingle = event.IsChecked()
279
280 def OnAutoCompleteDouble(self, event):
281 win = wx.Window_FindFocus()
282 win.autoCompleteIncludeDouble = event.IsChecked()
283
284 def OnCallTipsShow(self, event):
285 win = wx.Window_FindFocus()
286 win.autoCallTip = event.IsChecked()
287
288 def OnWrap(self, event):
289 win = wx.Window_FindFocus()
290 win.SetWrapMode(event.IsChecked())
291
292 def OnUpdateMenu(self, event):
293 """Update menu items based on current status and context."""
294 win = wx.Window_FindFocus()
295 id = event.GetId()
296 event.Enable(True)
297 try:
298 if id == ID_NEW:
299 event.Enable(hasattr(self, 'bufferNew'))
300 elif id == ID_OPEN:
301 event.Enable(hasattr(self, 'bufferOpen'))
302 elif id == ID_REVERT:
303 event.Enable(hasattr(self, 'bufferRevert')
304 and self.hasBuffer())
305 elif id == ID_CLOSE:
306 event.Enable(hasattr(self, 'bufferClose')
307 and self.hasBuffer())
308 elif id == ID_SAVE:
309 event.Enable(hasattr(self, 'bufferSave')
310 and self.bufferHasChanged())
311 elif id == ID_SAVEAS:
312 event.Enable(hasattr(self, 'bufferSaveAs')
313 and self.hasBuffer())
314 elif id == ID_NAMESPACE:
315 event.Enable(hasattr(self, 'updateNamespace')
316 and self.hasBuffer())
317 elif id == ID_PRINT:
318 event.Enable(hasattr(self, 'bufferPrint')
319 and self.hasBuffer())
320 elif id == ID_UNDO:
321 event.Enable(win.CanUndo())
322 elif id == ID_REDO:
323 event.Enable(win.CanRedo())
324 elif id == ID_CUT:
325 event.Enable(win.CanCut())
326 elif id == ID_COPY:
327 event.Enable(win.CanCopy())
328 elif id == ID_COPY_PLUS:
329 event.Enable(win.CanCopy() and hasattr(win, 'CopyWithPrompts'))
330 elif id == ID_PASTE:
331 event.Enable(win.CanPaste())
332 elif id == ID_PASTE_PLUS:
333 event.Enable(win.CanPaste() and hasattr(win, 'PasteAndRun'))
334 elif id == ID_CLEAR:
335 event.Enable(win.CanCut())
336 elif id == ID_SELECTALL:
337 event.Enable(hasattr(win, 'SelectAll'))
338 elif id == ID_AUTOCOMP_SHOW:
339 event.Check(win.autoComplete)
340 elif id == ID_AUTOCOMP_MAGIC:
341 event.Check(win.autoCompleteIncludeMagic)
342 elif id == ID_AUTOCOMP_SINGLE:
343 event.Check(win.autoCompleteIncludeSingle)
344 elif id == ID_AUTOCOMP_DOUBLE:
345 event.Check(win.autoCompleteIncludeDouble)
346 elif id == ID_CALLTIPS_SHOW:
347 event.Check(win.autoCallTip)
348 elif id == ID_WRAP:
349 event.Check(win.GetWrapMode())
350 else:
351 event.Enable(False)
352 except AttributeError:
353 # This menu option is not supported in the current context.
354 event.Enable(False)