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