]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/py/frame.py
b74d6521234918e4c43c13e154aa30b106d714f8
[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 import os
9 from version import VERSION
10 import editwindow
11
12 ID_NEW = wx.ID_NEW
13 ID_OPEN = wx.ID_OPEN
14 ID_REVERT = wx.ID_REVERT
15 ID_CLOSE = wx.ID_CLOSE
16 ID_SAVE = wx.ID_SAVE
17 ID_SAVEAS = wx.ID_SAVEAS
18 ID_PRINT = wx.ID_PRINT
19 ID_EXIT = wx.ID_EXIT
20 ID_UNDO = wx.ID_UNDO
21 ID_REDO = wx.ID_REDO
22 ID_CUT = wx.ID_CUT
23 ID_COPY = wx.ID_COPY
24 ID_PASTE = wx.ID_PASTE
25 ID_CLEAR = wx.ID_CLEAR
26 ID_SELECTALL = wx.ID_SELECTALL
27 ID_EMPTYBUFFER = wx.NewId()
28 ID_ABOUT = wx.ID_ABOUT
29 ID_HELP = wx.NewId()
30 ID_AUTOCOMP = wx.NewId()
31 ID_AUTOCOMP_SHOW = wx.NewId()
32 ID_AUTOCOMP_MAGIC = wx.NewId()
33 ID_AUTOCOMP_SINGLE = wx.NewId()
34 ID_AUTOCOMP_DOUBLE = wx.NewId()
35 ID_CALLTIPS = wx.NewId()
36 ID_CALLTIPS_SHOW = wx.NewId()
37 ID_CALLTIPS_INSERT = wx.NewId()
38 ID_COPY_PLUS = wx.NewId()
39 ID_NAMESPACE = wx.NewId()
40 ID_PASTE_PLUS = wx.NewId()
41 ID_WRAP = wx.NewId()
42 ID_TOGGLE_MAXIMIZE = wx.NewId()
43 ID_USEAA = wx.NewId()
44 ID_SHOW_LINENUMBERS = wx.NewId()
45 ID_AUTO_SAVESETTINGS = wx.NewId()
46 ID_SAVEHISTORY = wx.NewId()
47 ID_SAVESETTINGS = wx.NewId()
48 ID_DELSETTINGSFILE = wx.NewId()
49 ID_EDITSTARTUPSCRIPT = wx.NewId()
50 ID_EXECSTARTUPSCRIPT = wx.NewId()
51 ID_STARTUP = wx.NewId()
52 ID_SETTINGS = wx.NewId()
53 ID_FIND = wx.ID_FIND
54 ID_FINDNEXT = wx.NewId()
55
56
57
58 class Frame(wx.Frame):
59 """Frame with standard menu items."""
60
61 revision = __revision__
62
63 def __init__(self, parent=None, id=-1, title='Editor',
64 pos=wx.DefaultPosition, size=wx.DefaultSize,
65 style=wx.DEFAULT_FRAME_STYLE):
66 """Create a Frame instance."""
67 wx.Frame.__init__(self, parent, id, title, pos, size, style)
68 self.CreateStatusBar()
69 self.SetStatusText('Frame')
70 import images
71 self.SetIcon(images.getPyIcon())
72 self.__createMenus()
73
74 self.iconized = False
75 self.findDlg = None
76 self.findData = wx.FindReplaceData()
77 self.findData.SetFlags(wx.FR_DOWN)
78
79 self.Bind(wx.EVT_CLOSE, self.OnClose)
80 self.Bind(wx.EVT_ICONIZE, self.OnIconize)
81
82
83 def OnIconize(self, event):
84 """Event handler for Iconize."""
85 self.iconized = event.Iconized()
86
87
88 def OnClose(self, event):
89 """Event handler for closing."""
90 self.Destroy()
91
92
93 def __createMenus(self):
94 # File Menu
95 m = self.fileMenu = wx.Menu()
96 m.Append(ID_NEW, '&New \tCtrl+N',
97 'New file')
98 m.Append(ID_OPEN, '&Open... \tCtrl+O',
99 'Open file')
100 m.AppendSeparator()
101 m.Append(ID_REVERT, '&Revert \tCtrl+R',
102 'Revert to last saved version')
103 m.Append(ID_CLOSE, '&Close \tCtrl+W',
104 'Close file')
105 m.AppendSeparator()
106 m.Append(ID_SAVE, '&Save... \tCtrl+S',
107 'Save file')
108 m.Append(ID_SAVEAS, 'Save &As \tCtrl+Shift+S',
109 'Save file with new name')
110 m.AppendSeparator()
111 m.Append(ID_PRINT, '&Print... \tCtrl+P',
112 'Print file')
113 m.AppendSeparator()
114 m.Append(ID_NAMESPACE, '&Update Namespace \tCtrl+Shift+N',
115 'Update namespace for autocompletion and calltips')
116 m.AppendSeparator()
117 m.Append(ID_EXIT, 'E&xit\tCtrl+Q', 'Exit Program')
118
119 # Edit
120 m = self.editMenu = wx.Menu()
121 m.Append(ID_UNDO, '&Undo \tCtrl+Z',
122 'Undo the last action')
123 m.Append(ID_REDO, '&Redo \tCtrl+Y',
124 'Redo the last undone action')
125 m.AppendSeparator()
126 m.Append(ID_CUT, 'Cu&t \tCtrl+X',
127 'Cut the selection')
128 m.Append(ID_COPY, '&Copy \tCtrl+C',
129 'Copy the selection')
130 m.Append(ID_COPY_PLUS, 'Cop&y Plus \tCtrl+Shift+C',
131 'Copy the selection - retaining prompts')
132 m.Append(ID_PASTE, '&Paste \tCtrl+V', 'Paste from clipboard')
133 m.Append(ID_PASTE_PLUS, 'Past&e Plus \tCtrl+Shift+V',
134 'Paste and run commands')
135 m.AppendSeparator()
136 m.Append(ID_CLEAR, 'Cle&ar',
137 'Delete the selection')
138 m.Append(ID_SELECTALL, 'Select A&ll \tCtrl+A',
139 'Select all text')
140 m.AppendSeparator()
141 m.Append(ID_EMPTYBUFFER, 'E&mpty Buffer',
142 'Delete all the contents of the edit buffer')
143 m.Append(ID_FIND, '&Find Text \tCtrl+F',
144 'Search for text in the edit buffer')
145 m.Append(ID_FINDNEXT, 'Find &Next \tF3',
146 'Find next/previous instance of the search text')
147
148 # View
149 m = self.viewMenu = wx.Menu()
150 m.Append(ID_WRAP, '&Wrap Lines\tCtrl+Shift+W',
151 'Wrap lines at right edge', wx.ITEM_CHECK)
152 m.Append(ID_SHOW_LINENUMBERS, '&Show Line Numbers\tCtrl+Shift+L', 'Show Line Numbers', wx.ITEM_CHECK)
153 m.Append(ID_TOGGLE_MAXIMIZE, '&Toggle Maximize\tF11', 'Maximize/Restore Application')
154
155 # Options
156 m = self.autocompMenu = wx.Menu()
157 m.Append(ID_AUTOCOMP_SHOW, 'Show &Auto Completion\tCtrl+Shift+A',
158 'Show auto completion list', wx.ITEM_CHECK)
159 m.Append(ID_AUTOCOMP_MAGIC, 'Include &Magic Attributes\tCtrl+Shift+M',
160 'Include attributes visible to __getattr__ and __setattr__',
161 wx.ITEM_CHECK)
162 m.Append(ID_AUTOCOMP_SINGLE, 'Include Single &Underscores\tCtrl+Shift+U',
163 'Include attibutes prefixed by a single underscore', wx.ITEM_CHECK)
164 m.Append(ID_AUTOCOMP_DOUBLE, 'Include &Double Underscores\tCtrl+Shift+D',
165 'Include attibutes prefixed by a double underscore', wx.ITEM_CHECK)
166 m = self.calltipsMenu = wx.Menu()
167 m.Append(ID_CALLTIPS_SHOW, 'Show Call &Tips\tCtrl+Shift+T',
168 'Show call tips with argument signature and docstring', wx.ITEM_CHECK)
169 m.Append(ID_CALLTIPS_INSERT, '&Insert Call Tips\tCtrl+Shift+I',
170 '&Insert Call Tips', wx.ITEM_CHECK)
171
172 m = self.optionsMenu = wx.Menu()
173 m.AppendMenu(ID_AUTOCOMP, '&Auto Completion', self.autocompMenu,
174 'Auto Completion Options')
175 m.AppendMenu(ID_CALLTIPS, '&Call Tips', self.calltipsMenu,
176 'Call Tip Options')
177
178 if wx.Platform == "__WXMAC__":
179 m.Append(ID_USEAA, '&Use AntiAliasing\tCtrl+Shift+A',
180 'Use anti-aliased fonts', wx.ITEM_CHECK)
181
182 m.AppendSeparator()
183 m.Append(ID_SAVEHISTORY, '&Save History\tAlt+Ctrl+A', 'Automatically save history on close', wx.ITEM_CHECK)
184 self.startupMenu = wx.Menu()
185 self.startupMenu.Append(ID_EXECSTARTUPSCRIPT, 'E&xecute Startup Script\tAlt+Ctrl+X', 'Execute Startup Script', wx.ITEM_CHECK)
186 self.startupMenu.Append(ID_EDITSTARTUPSCRIPT, '&Edit Startup Script\tAlt+Ctrl+E', 'Edit Startup Script')
187 m.AppendMenu(ID_STARTUP, '&Startup', self.startupMenu, 'Startup Options')
188
189 self.settingsMenu = wx.Menu()
190 self.settingsMenu.Append(ID_AUTO_SAVESETTINGS, '&Auto Save Settings\tAlt+Ctrl+A', 'Automatically save settings on close', wx.ITEM_CHECK)
191 self.settingsMenu.Append(ID_SAVESETTINGS, '&Save Settings\tAlt+Ctrl+S', 'Save settings now')
192 self.settingsMenu.Append(ID_DELSETTINGSFILE, '&Revert to default\tAlt+Ctrl+R', 'Revert to the default settings')
193 m.AppendMenu(ID_SETTINGS, '&Settings', self.settingsMenu, 'Settings Options')
194
195 m = self.helpMenu = wx.Menu()
196 m.Append(ID_HELP, '&Help\tF1', 'Help!')
197 m.AppendSeparator()
198 m.Append(ID_ABOUT, '&About...\tAlt+A', 'About this program')
199
200 b = self.menuBar = wx.MenuBar()
201 b.Append(self.fileMenu, '&File')
202 b.Append(self.editMenu, '&Edit')
203 b.Append(self.viewMenu, '&View')
204 b.Append(self.optionsMenu, '&Options')
205 b.Append(self.helpMenu, '&Help')
206 self.SetMenuBar(b)
207
208 self.Bind(wx.EVT_MENU, self.OnFileNew, id=ID_NEW)
209 self.Bind(wx.EVT_MENU, self.OnFileOpen, id=ID_OPEN)
210 self.Bind(wx.EVT_MENU, self.OnFileRevert, id=ID_REVERT)
211 self.Bind(wx.EVT_MENU, self.OnFileClose, id=ID_CLOSE)
212 self.Bind(wx.EVT_MENU, self.OnFileSave, id=ID_SAVE)
213 self.Bind(wx.EVT_MENU, self.OnFileSaveAs, id=ID_SAVEAS)
214 self.Bind(wx.EVT_MENU, self.OnFileUpdateNamespace, id=ID_NAMESPACE)
215 self.Bind(wx.EVT_MENU, self.OnFilePrint, id=ID_PRINT)
216 self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT)
217 self.Bind(wx.EVT_MENU, self.OnUndo, id=ID_UNDO)
218 self.Bind(wx.EVT_MENU, self.OnRedo, id=ID_REDO)
219 self.Bind(wx.EVT_MENU, self.OnCut, id=ID_CUT)
220 self.Bind(wx.EVT_MENU, self.OnCopy, id=ID_COPY)
221 self.Bind(wx.EVT_MENU, self.OnCopyPlus, id=ID_COPY_PLUS)
222 self.Bind(wx.EVT_MENU, self.OnPaste, id=ID_PASTE)
223 self.Bind(wx.EVT_MENU, self.OnPastePlus, id=ID_PASTE_PLUS)
224 self.Bind(wx.EVT_MENU, self.OnClear, id=ID_CLEAR)
225 self.Bind(wx.EVT_MENU, self.OnSelectAll, id=ID_SELECTALL)
226 self.Bind(wx.EVT_MENU, self.OnEmptyBuffer, id=ID_EMPTYBUFFER)
227 self.Bind(wx.EVT_MENU, self.OnAbout, id=ID_ABOUT)
228 self.Bind(wx.EVT_MENU, self.OnHelp, id=ID_HELP)
229 self.Bind(wx.EVT_MENU, self.OnAutoCompleteShow, id=ID_AUTOCOMP_SHOW)
230 self.Bind(wx.EVT_MENU, self.OnAutoCompleteMagic, id=ID_AUTOCOMP_MAGIC)
231 self.Bind(wx.EVT_MENU, self.OnAutoCompleteSingle, id=ID_AUTOCOMP_SINGLE)
232 self.Bind(wx.EVT_MENU, self.OnAutoCompleteDouble, id=ID_AUTOCOMP_DOUBLE)
233 self.Bind(wx.EVT_MENU, self.OnCallTipsShow, id=ID_CALLTIPS_SHOW)
234 self.Bind(wx.EVT_MENU, self.OnCallTipsInsert, id=ID_CALLTIPS_INSERT)
235 self.Bind(wx.EVT_MENU, self.OnWrap, id=ID_WRAP)
236 self.Bind(wx.EVT_MENU, self.OnUseAA, id=ID_USEAA)
237 self.Bind(wx.EVT_MENU, self.OnToggleMaximize, id=ID_TOGGLE_MAXIMIZE)
238 self.Bind(wx.EVT_MENU, self.OnShowLineNumbers, id=ID_SHOW_LINENUMBERS)
239 self.Bind(wx.EVT_MENU, self.OnAutoSaveSettings, id=ID_AUTO_SAVESETTINGS)
240 self.Bind(wx.EVT_MENU, self.OnSaveHistory, id=ID_SAVEHISTORY)
241 self.Bind(wx.EVT_MENU, self.OnSaveSettings, id=ID_SAVESETTINGS)
242 self.Bind(wx.EVT_MENU, self.OnDelSettingsFile, id=ID_DELSETTINGSFILE)
243 self.Bind(wx.EVT_MENU, self.OnEditStartupScript, id=ID_EDITSTARTUPSCRIPT)
244 self.Bind(wx.EVT_MENU, self.OnExecStartupScript, id=ID_EXECSTARTUPSCRIPT)
245 self.Bind(wx.EVT_MENU, self.OnFindText, id=ID_FIND)
246 self.Bind(wx.EVT_MENU, self.OnFindNext, id=ID_FINDNEXT)
247
248 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_NEW)
249 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_OPEN)
250 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_REVERT)
251 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_CLOSE)
252 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_SAVE)
253 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_SAVEAS)
254 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_NAMESPACE)
255 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_PRINT)
256 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_UNDO)
257 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_REDO)
258 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_CUT)
259 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_COPY)
260 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_COPY_PLUS)
261 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_PASTE)
262 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_PASTE_PLUS)
263 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_CLEAR)
264 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_SELECTALL)
265 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_EMPTYBUFFER)
266 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_AUTOCOMP_SHOW)
267 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_AUTOCOMP_MAGIC)
268 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_AUTOCOMP_SINGLE)
269 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_AUTOCOMP_DOUBLE)
270 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_CALLTIPS_SHOW)
271 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_CALLTIPS_INSERT)
272 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_WRAP)
273 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_USEAA)
274 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_SHOW_LINENUMBERS)
275 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_AUTO_SAVESETTINGS)
276 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_SAVESETTINGS)
277 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_DELSETTINGSFILE)
278 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_EXECSTARTUPSCRIPT)
279 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_SAVEHISTORY)
280 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_EDITSTARTUPSCRIPT)
281 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_FIND)
282 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_FINDNEXT)
283
284 self.Bind(wx.EVT_ACTIVATE, self.OnActivate)
285 self.Bind(wx.EVT_FIND, self.OnFindNext)
286 self.Bind(wx.EVT_FIND_NEXT, self.OnFindNext)
287 self.Bind(wx.EVT_FIND_CLOSE, self.OnFindClose)
288
289
290
291 def OnShowLineNumbers(self, event):
292 win = wx.Window.FindFocus()
293 if hasattr(win, 'lineNumbers'):
294 win.lineNumbers = event.IsChecked()
295 win.setDisplayLineNumbers(win.lineNumbers)
296
297 def OnToggleMaximize(self, event):
298 self.Maximize(not self.IsMaximized())
299
300 def OnFileNew(self, event):
301 self.bufferNew()
302
303 def OnFileOpen(self, event):
304 self.bufferOpen()
305
306 def OnFileRevert(self, event):
307 self.bufferRevert()
308
309 def OnFileClose(self, event):
310 self.bufferClose()
311
312 def OnFileSave(self, event):
313 self.bufferSave()
314
315 def OnFileSaveAs(self, event):
316 self.bufferSaveAs()
317
318 def OnFileUpdateNamespace(self, event):
319 self.updateNamespace()
320
321 def OnFilePrint(self, event):
322 self.bufferPrint()
323
324 def OnExit(self, event):
325 self.Close(False)
326
327 def OnUndo(self, event):
328 win = wx.Window.FindFocus()
329 win.Undo()
330
331 def OnRedo(self, event):
332 win = wx.Window.FindFocus()
333 win.Redo()
334
335 def OnCut(self, event):
336 win = wx.Window.FindFocus()
337 win.Cut()
338
339 def OnCopy(self, event):
340 win = wx.Window.FindFocus()
341 win.Copy()
342
343 def OnCopyPlus(self, event):
344 win = wx.Window.FindFocus()
345 win.CopyWithPrompts()
346
347 def OnPaste(self, event):
348 win = wx.Window.FindFocus()
349 win.Paste()
350
351 def OnPastePlus(self, event):
352 win = wx.Window.FindFocus()
353 win.PasteAndRun()
354
355 def OnClear(self, event):
356 win = wx.Window.FindFocus()
357 win.Clear()
358
359 def OnEmptyBuffer(self, event):
360 win = wx.Window.FindFocus()
361 d = wx.MessageDialog(self,
362 "Are you sure you want to clear the edit buffer,\n"
363 "deleting all the text?",
364 "Empty Buffer", wx.OK | wx.CANCEL | wx.ICON_QUESTION)
365 answer = d.ShowModal()
366 d.Destroy()
367 if (answer == wx.ID_OK):
368 win.ClearAll()
369 if hasattr(win,'prompt'):
370 win.prompt()
371
372 def OnSelectAll(self, event):
373 win = wx.Window.FindFocus()
374 win.SelectAll()
375
376 def OnAbout(self, event):
377 """Display an About window."""
378 title = 'About'
379 text = 'Your message here.'
380 dialog = wx.MessageDialog(self, text, title,
381 wx.OK | wx.ICON_INFORMATION)
382 dialog.ShowModal()
383 dialog.Destroy()
384
385 def OnHelp(self, event):
386 """Display a Help window."""
387 title = 'Help'
388 text = "Type 'shell.help()' in the shell window."
389 dialog = wx.MessageDialog(self, text, title,
390 wx.OK | wx.ICON_INFORMATION)
391 dialog.ShowModal()
392 dialog.Destroy()
393
394 def OnAutoCompleteShow(self, event):
395 win = wx.Window.FindFocus()
396 win.autoComplete = event.IsChecked()
397
398 def OnAutoCompleteMagic(self, event):
399 win = wx.Window.FindFocus()
400 win.autoCompleteIncludeMagic = event.IsChecked()
401
402 def OnAutoCompleteSingle(self, event):
403 win = wx.Window.FindFocus()
404 win.autoCompleteIncludeSingle = event.IsChecked()
405
406 def OnAutoCompleteDouble(self, event):
407 win = wx.Window.FindFocus()
408 win.autoCompleteIncludeDouble = event.IsChecked()
409
410 def OnCallTipsShow(self, event):
411 win = wx.Window.FindFocus()
412 win.autoCallTip = event.IsChecked()
413
414 def OnCallTipsInsert(self, event):
415 win = wx.Window.FindFocus()
416 win.callTipInsert = event.IsChecked()
417
418 def OnWrap(self, event):
419 win = wx.Window.FindFocus()
420 win.SetWrapMode(event.IsChecked())
421 wx.FutureCall(1, self.shell.EnsureCaretVisible)
422
423 def OnUseAA(self, event):
424 win = wx.Window.FindFocus()
425 win.SetUseAntiAliasing(event.IsChecked())
426
427 def OnSaveHistory(self, event):
428 self.saveHistory = event.IsChecked()
429
430 def OnAutoSaveSettings(self, event):
431 self.autoSaveSettings = event.IsChecked()
432
433 def OnSaveSettings(self, event):
434 self.DoSaveSettings()
435
436 def OnDelSettingsFile(self, event):
437 if self.config is not None:
438 d = wx.MessageDialog(
439 self, "Do you want to revert to the default settings?\n" +
440 "A restart is needed for the change to take effect",
441 "Warning", wx.OK | wx.CANCEL | wx.ICON_QUESTION)
442 answer = d.ShowModal()
443 d.Destroy()
444 if (answer == wx.ID_OK):
445 self.config.DeleteAll()
446 self.LoadSettings()
447
448
449 def OnEditStartupScript(self, event):
450 if hasattr(self, 'EditStartupScript'):
451 self.EditStartupScript()
452
453 def OnExecStartupScript(self, event):
454 self.execStartupScript = event.IsChecked()
455
456
457 def OnFindText(self, event):
458 if self.findDlg is not None:
459 return
460 win = wx.Window.FindFocus()
461 self.findDlg = wx.FindReplaceDialog(win, self.findData, "Find",
462 wx.FR_NOWHOLEWORD)
463 self.findDlg.Show()
464
465 def OnFindNext(self, event):
466 if isinstance(event, wx.FindDialogEvent):
467 win = self.findDlg.GetParent()
468 else:
469 win = wx.Window.FindFocus()
470 win.DoFindNext(self.findData, self.findDlg)
471
472 def OnFindClose(self, event):
473 self.findDlg.Destroy()
474 self.findDlg = None
475
476
477
478 def OnUpdateMenu(self, event):
479 """Update menu items based on current status and context."""
480 win = wx.Window.FindFocus()
481 id = event.GetId()
482 event.Enable(True)
483 try:
484 if id == ID_NEW:
485 event.Enable(hasattr(self, 'bufferNew'))
486 elif id == ID_OPEN:
487 event.Enable(hasattr(self, 'bufferOpen'))
488 elif id == ID_REVERT:
489 event.Enable(hasattr(self, 'bufferRevert')
490 and self.hasBuffer())
491 elif id == ID_CLOSE:
492 event.Enable(hasattr(self, 'bufferClose')
493 and self.hasBuffer())
494 elif id == ID_SAVE:
495 event.Enable(hasattr(self, 'bufferSave')
496 and self.bufferHasChanged())
497 elif id == ID_SAVEAS:
498 event.Enable(hasattr(self, 'bufferSaveAs')
499 and self.hasBuffer())
500 elif id == ID_NAMESPACE:
501 event.Enable(hasattr(self, 'updateNamespace')
502 and self.hasBuffer())
503 elif id == ID_PRINT:
504 event.Enable(hasattr(self, 'bufferPrint')
505 and self.hasBuffer())
506 elif id == ID_UNDO:
507 event.Enable(win.CanUndo())
508 elif id == ID_REDO:
509 event.Enable(win.CanRedo())
510 elif id == ID_CUT:
511 event.Enable(win.CanCut())
512 elif id == ID_COPY:
513 event.Enable(win.CanCopy())
514 elif id == ID_COPY_PLUS:
515 event.Enable(win.CanCopy() and hasattr(win, 'CopyWithPrompts'))
516 elif id == ID_PASTE:
517 event.Enable(win.CanPaste())
518 elif id == ID_PASTE_PLUS:
519 event.Enable(win.CanPaste() and hasattr(win, 'PasteAndRun'))
520 elif id == ID_CLEAR:
521 event.Enable(win.CanCut())
522 elif id == ID_SELECTALL:
523 event.Enable(hasattr(win, 'SelectAll'))
524 elif id == ID_EMPTYBUFFER:
525 event.Enable(hasattr(win, 'ClearAll') and not win.GetReadOnly())
526 elif id == ID_AUTOCOMP_SHOW:
527 event.Check(win.autoComplete)
528 elif id == ID_AUTOCOMP_MAGIC:
529 event.Check(win.autoCompleteIncludeMagic)
530 elif id == ID_AUTOCOMP_SINGLE:
531 event.Check(win.autoCompleteIncludeSingle)
532 elif id == ID_AUTOCOMP_DOUBLE:
533 event.Check(win.autoCompleteIncludeDouble)
534 elif id == ID_CALLTIPS_SHOW:
535 event.Check(win.autoCallTip)
536 elif id == ID_CALLTIPS_INSERT:
537 event.Check(win.callTipInsert)
538 elif id == ID_WRAP:
539 event.Check(win.GetWrapMode())
540 elif id == ID_USEAA:
541 event.Check(win.GetUseAntiAliasing())
542
543 elif id == ID_SHOW_LINENUMBERS:
544 event.Check(win.lineNumbers)
545 elif id == ID_AUTO_SAVESETTINGS:
546 event.Check(self.autoSaveSettings)
547 event.Enable(self.config is not None)
548 elif id == ID_SAVESETTINGS:
549 event.Enable(self.config is not None and
550 hasattr(self, 'DoSaveSettings'))
551 elif id == ID_DELSETTINGSFILE:
552 event.Enable(self.config is not None)
553
554 elif id == ID_EXECSTARTUPSCRIPT:
555 event.Check(self.execStartupScript)
556 event.Enable(self.config is not None)
557
558 elif id == ID_SAVEHISTORY:
559 event.Check(self.saveHistory)
560 event.Enable(self.dataDir is not None)
561 elif id == ID_EDITSTARTUPSCRIPT:
562 event.Enable(hasattr(self, 'EditStartupScript'))
563 event.Enable(self.dataDir is not None)
564
565 elif id == ID_FIND:
566 event.Enable(hasattr(win, 'DoFindNext'))
567 elif id == ID_FINDNEXT:
568 event.Enable(hasattr(win, 'DoFindNext') and
569 self.findData.GetFindString() != '')
570
571 else:
572 event.Enable(False)
573 except AttributeError:
574 # This menu option is not supported in the current context.
575 event.Enable(False)
576
577
578 def OnActivate(self, event):
579 """
580 Event Handler for losing the focus of the Frame. Should close
581 Autocomplete listbox, if shown.
582 """
583 if not event.GetActive():
584 # If autocomplete active, cancel it. Otherwise, the
585 # autocomplete list will stay visible on top of the
586 # z-order after switching to another application
587 win = wx.Window.FindFocus()
588 if hasattr(win, 'AutoCompActive') and win.AutoCompActive():
589 win.AutoCompCancel()
590 event.Skip()
591
592
593
594 def LoadSettings(self, config):
595 """Called be derived classes to load settings specific to the Frame"""
596 pos = wx.Point(config.ReadInt('Window/PosX', -1),
597 config.ReadInt('Window/PosY', -1))
598
599 size = wx.Size(config.ReadInt('Window/Width', -1),
600 config.ReadInt('Window/Height', -1))
601
602 self.SetSize(size)
603 self.Move(pos)
604
605
606 def SaveSettings(self, config):
607 """Called by derived classes to save Frame settings to a wx.Config object"""
608
609 # TODO: track position/size so we can save it even if the
610 # frame is maximized or iconized.
611 if not self.iconized and not self.IsMaximized():
612 w, h = self.GetSize()
613 config.WriteInt('Window/Width', w)
614 config.WriteInt('Window/Height', h)
615
616 px, py = self.GetPosition()
617 config.WriteInt('Window/PosX', px)
618 config.WriteInt('Window/PosY', py)
619
620
621
622
623 class ShellFrameMixin:
624 """
625 A mix-in class for frames that will have a Shell or a Crust window
626 and that want to add history, startupScript and other common
627 functionality.
628 """
629 def __init__(self, config, dataDir):
630 self.config = config
631 self.dataDir = dataDir
632 self.startupScript = os.environ.get('PYTHONSTARTUP')
633 if not self.startupScript and self.dataDir:
634 self.startupScript = os.path.join(self.dataDir, 'startup')
635
636 self.autoSaveSettings = False
637 self.saveHistory = False
638
639 # We need this one before we have a chance to load the settings...
640 self.execStartupScript = True
641 if self.config:
642 self.execStartupScript = self.config.ReadBool('Options/ExecStartupScript', True)
643
644
645 def OnHelp(self, event):
646 """Display a Help window."""
647 import wx.lib.dialogs
648 title = 'Help on key bindings'
649
650 text = wx.py.shell.HELP_TEXT
651
652 dlg = wx.lib.dialogs.ScrolledMessageDialog(self, text, title, size = ((700, 540)))
653 fnt = wx.Font(10, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
654 dlg.GetChildren()[0].SetFont(fnt)
655 dlg.GetChildren()[0].SetInsertionPoint(0)
656 dlg.ShowModal()
657 dlg.Destroy()
658
659
660 def LoadSettings(self):
661 if self.config is not None:
662 self.autoSaveSettings = self.config.ReadBool('Options/AutoSaveSettings', False)
663 self.execStartupScript = self.config.ReadBool('Options/ExecStartupScript', True)
664 self.saveHistory = self.config.ReadBool('Options/SaveHistory', False)
665 self.LoadHistory()
666
667
668 def SaveSettings(self):
669 if self.config is not None:
670 # always save this one
671 self.config.WriteBool('Options/AutoSaveSettings', self.autoSaveSettings)
672 if self.autoSaveSettings:
673 self.config.WriteBool('Options/SaveHistory', self.saveHistory)
674 self.config.WriteBool('Options/ExecStartupScript', self.execStartupScript)
675 self.SaveHistory()
676
677
678
679 def SaveHistory(self):
680 if self.dataDir:
681 try:
682 # always open the file so that when we are not
683 # saving the history, the old file is emptied.
684 name = os.path.join(self.dataDir, 'history')
685 f = file(name, 'w')
686 if self.saveHistory:
687 hist = '\n'.join(self.shell.history)
688 f.write(hist)
689 f.close()
690 except:
691 d = wx.MessageDialog(self, "Error saving history file.",
692 "Error", wx.ICON_EXCLAMATION)
693 d.ShowModal()
694 d.Destroy()
695
696
697 def LoadHistory(self):
698 if self.dataDir:
699 name = os.path.join(self.dataDir, 'history')
700 if os.path.exists(name):
701 try:
702 f = file(name, 'U')
703 hist = f.read()
704 f.close()
705 self.shell.history = hist.split('\n')
706 except:
707 d = wx.MessageDialog(self, "Error loading history file.",
708 "Error", wx.ICON_EXCLAMATION)
709 d.ShowModal()
710 d.Destroy()
711
712
713 def bufferHasChanged(self):
714 # the shell buffers can always be saved
715 return True
716
717 def bufferSave(self):
718 import time
719 appname = wx.GetApp().GetAppName()
720 default = appname + '-' + time.strftime("%Y%m%d-%H%M.py")
721 fileName = wx.FileSelector("Save File As", "Saving",
722 default_filename=default,
723 default_extension="py",
724 wildcard="*.py",
725 flags = wx.SAVE | wx.OVERWRITE_PROMPT)
726 if not fileName:
727 return
728
729 text = self.shell.GetText()
730
731 ## This isn't working currently...
732 ## d = wx.MessageDialog(self,u'Save source code only?\nAnswering yes will only save lines starting with >>> and ...',u'Question', wx.YES_NO | wx.ICON_QUESTION)
733 ## yes_no = d.ShowModal()
734 ## if yes_no == wx.ID_YES:
735 ## m = re.findall('^[>\.]{3,3} (.*)\r', text, re.MULTILINE | re.LOCALE)
736 ## text = '\n'.join(m)
737 ## d.Destroy()
738
739 try:
740 f = open(fileName, "w")
741 f.write(text)
742 f.close()
743 except:
744 d = wx.MessageDialog(self, u'Error saving session',u'Error',
745 wx.OK | wx.ICON_ERROR)
746 d.ShowModal()
747 d.Destroy()
748
749
750 def EditStartupScript(self):
751 if os.path.exists(self.startupScript):
752 text = file(self.startupScript, 'U').read()
753 else:
754 text = ''
755
756 dlg = EditStartupScriptDialog(self, self.startupScript, text)
757 if dlg.ShowModal() == wx.ID_OK:
758 text = dlg.GetText()
759 try:
760 f = file(self.startupScript, 'w')
761 f.write(text)
762 f.close()
763 except:
764 d = wx.MessageDialog(self, "Error saving startup file.",
765 "Error", wx.ICON_EXCLAMATION)
766 d.ShowModal()
767 d.Destroy()
768
769
770
771 class EditStartupScriptDialog(wx.Dialog):
772 def __init__(self, parent, fileName, text):
773 wx.Dialog.__init__(self, parent, size=(425,350),
774 title="Edit Startup Script",
775 style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
776
777 pst = wx.StaticText(self, -1, "Path:")
778 ptx = wx.TextCtrl(self, -1, fileName, style=wx.TE_READONLY)
779 self.editor = editwindow.EditWindow(self)
780 self.editor.SetText(text)
781 wx.CallAfter(self.editor.SetFocus)
782
783 ok = wx.Button(self, wx.ID_OK)
784 cancel = wx.Button(self, wx.ID_CANCEL)
785
786 mainSizer = wx.BoxSizer(wx.VERTICAL)
787
788 pthSizer = wx.BoxSizer(wx.HORIZONTAL)
789 pthSizer.Add(pst, flag=wx.ALIGN_CENTER_VERTICAL)
790 pthSizer.Add((5,5))
791 pthSizer.Add(ptx, 1)
792 mainSizer.Add(pthSizer, 0, wx.EXPAND|wx.ALL, 10)
793
794 mainSizer.Add(self.editor, 1, wx.EXPAND|wx.LEFT|wx.RIGHT, 10)
795
796 btnSizer = wx.BoxSizer(wx.HORIZONTAL)
797 btnSizer.Add((5,5), 1)
798 btnSizer.Add(ok)
799 btnSizer.Add((5,5), 1)
800 btnSizer.Add(cancel)
801 btnSizer.Add((5,5), 1)
802 mainSizer.Add(btnSizer, 0, wx.EXPAND|wx.ALL, 10)
803
804 self.SetSizer(mainSizer)
805 self.Layout()
806
807
808 def GetText(self):
809 return self.editor.GetText()