1 """Base frame with menu."""
3 __author__
= "Patrick K. O'Brien <pobrien@orbtech.com>"
5 __revision__
= "$Revision$"[11:-2]
9 from version
import VERSION
14 ID_REVERT
= wx
.ID_REVERT
15 ID_CLOSE
= wx
.ID_CLOSE
17 ID_SAVEAS
= wx
.ID_SAVEAS
18 ID_PRINT
= wx
.ID_PRINT
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
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()
42 ID_TOGGLE_MAXIMIZE
= 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()
54 ID_FINDNEXT
= wx
.NewId()
58 class Frame(wx
.Frame
):
59 """Frame with standard menu items."""
61 revision
= __revision__
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')
71 self
.SetIcon(images
.getPyIcon())
76 self
.findData
= wx
.FindReplaceData()
77 self
.findData
.SetFlags(wx
.FR_DOWN
)
79 self
.Bind(wx
.EVT_CLOSE
, self
.OnClose
)
80 self
.Bind(wx
.EVT_ICONIZE
, self
.OnIconize
)
83 def OnIconize(self
, event
):
84 """Event handler for Iconize."""
85 self
.iconized
= event
.Iconized()
88 def OnClose(self
, event
):
89 """Event handler for closing."""
93 def __createMenus(self
):
95 m
= self
.fileMenu
= wx
.Menu()
96 m
.Append(ID_NEW
, '&New \tCtrl+N',
98 m
.Append(ID_OPEN
, '&Open... \tCtrl+O',
101 m
.Append(ID_REVERT
, '&Revert \tCtrl+R',
102 'Revert to last saved version')
103 m
.Append(ID_CLOSE
, '&Close \tCtrl+W',
106 m
.Append(ID_SAVE
, '&Save... \tCtrl+S',
108 m
.Append(ID_SAVEAS
, 'Save &As \tCtrl+Shift+S',
109 'Save file with new name')
111 m
.Append(ID_PRINT
, '&Print... \tCtrl+P',
114 m
.Append(ID_NAMESPACE
, '&Update Namespace \tCtrl+Shift+N',
115 'Update namespace for autocompletion and calltips')
117 m
.Append(ID_EXIT
, 'E&xit\tCtrl+Q', 'Exit Program')
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')
126 m
.Append(ID_CUT
, 'Cu&t \tCtrl+X',
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')
136 m
.Append(ID_CLEAR
, 'Cle&ar',
137 'Delete the selection')
138 m
.Append(ID_SELECTALL
, 'Select A&ll \tCtrl+A',
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')
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')
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__',
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
)
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
,
178 if wx
.Platform
== "__WXMAC__":
179 m
.Append(ID_USEAA
, '&Use AntiAliasing\tCtrl+Shift+A',
180 'Use anti-aliased fonts', wx
.ITEM_CHECK
)
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')
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')
195 m
= self
.helpMenu
= wx
.Menu()
196 m
.Append(ID_HELP
, '&Help\tF1', 'Help!')
198 m
.Append(ID_ABOUT
, '&About...\tAlt+A', 'About this program')
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')
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
)
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
)
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
)
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
)
297 def OnToggleMaximize(self
, event
):
298 self
.Maximize(not self
.IsMaximized())
300 def OnFileNew(self
, event
):
303 def OnFileOpen(self
, event
):
306 def OnFileRevert(self
, event
):
309 def OnFileClose(self
, event
):
312 def OnFileSave(self
, event
):
315 def OnFileSaveAs(self
, event
):
318 def OnFileUpdateNamespace(self
, event
):
319 self
.updateNamespace()
321 def OnFilePrint(self
, event
):
324 def OnExit(self
, event
):
327 def OnUndo(self
, event
):
328 win
= wx
.Window
.FindFocus()
331 def OnRedo(self
, event
):
332 win
= wx
.Window
.FindFocus()
335 def OnCut(self
, event
):
336 win
= wx
.Window
.FindFocus()
339 def OnCopy(self
, event
):
340 win
= wx
.Window
.FindFocus()
343 def OnCopyPlus(self
, event
):
344 win
= wx
.Window
.FindFocus()
345 win
.CopyWithPrompts()
347 def OnPaste(self
, event
):
348 win
= wx
.Window
.FindFocus()
351 def OnPastePlus(self
, event
):
352 win
= wx
.Window
.FindFocus()
355 def OnClear(self
, event
):
356 win
= wx
.Window
.FindFocus()
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()
367 if (answer
== wx
.ID_OK
):
369 if hasattr(win
,'prompt'):
372 def OnSelectAll(self
, event
):
373 win
= wx
.Window
.FindFocus()
376 def OnAbout(self
, event
):
377 """Display an About window."""
379 text
= 'Your message here.'
380 dialog
= wx
.MessageDialog(self
, text
, title
,
381 wx
.OK | wx
.ICON_INFORMATION
)
385 def OnHelp(self
, event
):
386 """Display a Help window."""
388 text
= "Type 'shell.help()' in the shell window."
389 dialog
= wx
.MessageDialog(self
, text
, title
,
390 wx
.OK | wx
.ICON_INFORMATION
)
394 def OnAutoCompleteShow(self
, event
):
395 win
= wx
.Window
.FindFocus()
396 win
.autoComplete
= event
.IsChecked()
398 def OnAutoCompleteMagic(self
, event
):
399 win
= wx
.Window
.FindFocus()
400 win
.autoCompleteIncludeMagic
= event
.IsChecked()
402 def OnAutoCompleteSingle(self
, event
):
403 win
= wx
.Window
.FindFocus()
404 win
.autoCompleteIncludeSingle
= event
.IsChecked()
406 def OnAutoCompleteDouble(self
, event
):
407 win
= wx
.Window
.FindFocus()
408 win
.autoCompleteIncludeDouble
= event
.IsChecked()
410 def OnCallTipsShow(self
, event
):
411 win
= wx
.Window
.FindFocus()
412 win
.autoCallTip
= event
.IsChecked()
414 def OnCallTipsInsert(self
, event
):
415 win
= wx
.Window
.FindFocus()
416 win
.callTipInsert
= event
.IsChecked()
418 def OnWrap(self
, event
):
419 win
= wx
.Window
.FindFocus()
420 win
.SetWrapMode(event
.IsChecked())
421 wx
.FutureCall(1, self
.shell
.EnsureCaretVisible
)
423 def OnUseAA(self
, event
):
424 win
= wx
.Window
.FindFocus()
425 win
.SetUseAntiAliasing(event
.IsChecked())
427 def OnSaveHistory(self
, event
):
428 self
.saveHistory
= event
.IsChecked()
430 def OnAutoSaveSettings(self
, event
):
431 self
.autoSaveSettings
= event
.IsChecked()
433 def OnSaveSettings(self
, event
):
434 self
.DoSaveSettings()
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()
444 if (answer
== wx
.ID_OK
):
445 self
.config
.DeleteAll()
449 def OnEditStartupScript(self
, event
):
450 if hasattr(self
, 'EditStartupScript'):
451 self
.EditStartupScript()
453 def OnExecStartupScript(self
, event
):
454 self
.execStartupScript
= event
.IsChecked()
457 def OnFindText(self
, event
):
458 if self
.findDlg
is not None:
460 win
= wx
.Window
.FindFocus()
461 self
.findDlg
= wx
.FindReplaceDialog(win
, self
.findData
, "Find",
465 def OnFindNext(self
, event
):
466 if isinstance(event
, wx
.FindDialogEvent
):
467 win
= self
.findDlg
.GetParent()
469 win
= wx
.Window
.FindFocus()
470 win
.DoFindNext(self
.findData
, self
.findDlg
)
472 def OnFindClose(self
, event
):
473 self
.findDlg
.Destroy()
478 def OnUpdateMenu(self
, event
):
479 """Update menu items based on current status and context."""
480 win
= wx
.Window
.FindFocus()
485 event
.Enable(hasattr(self
, 'bufferNew'))
487 event
.Enable(hasattr(self
, 'bufferOpen'))
488 elif id == ID_REVERT
:
489 event
.Enable(hasattr(self
, 'bufferRevert')
490 and self
.hasBuffer())
492 event
.Enable(hasattr(self
, 'bufferClose')
493 and self
.hasBuffer())
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())
504 event
.Enable(hasattr(self
, 'bufferPrint')
505 and self
.hasBuffer())
507 event
.Enable(win
.CanUndo())
509 event
.Enable(win
.CanRedo())
511 event
.Enable(win
.CanCut())
513 event
.Enable(win
.CanCopy())
514 elif id == ID_COPY_PLUS
:
515 event
.Enable(win
.CanCopy() and hasattr(win
, 'CopyWithPrompts'))
517 event
.Enable(win
.CanPaste())
518 elif id == ID_PASTE_PLUS
:
519 event
.Enable(win
.CanPaste() and hasattr(win
, 'PasteAndRun'))
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
)
539 event
.Check(win
.GetWrapMode())
541 event
.Check(win
.GetUseAntiAliasing())
543 elif id == ID_SHOW_LINENUMBERS
:
544 event
.Check(win
.lineNumbers
)
545 elif id == ID_AUTO_SAVESETTINGS
:
546 event
.Check(self
.autoSaveSettings
)
547 elif id == ID_SAVESETTINGS
:
548 event
.Enable(self
.config
is not None and
549 hasattr(self
, 'DoSaveSettings'))
550 elif id == ID_DELSETTINGSFILE
:
551 event
.Enable(self
.config
is not None)
553 elif id == ID_EXECSTARTUPSCRIPT
:
554 event
.Check(self
.execStartupScript
)
556 elif id == ID_SAVEHISTORY
:
557 event
.Check(self
.saveHistory
and self
.dataDir
is not None)
558 elif id == ID_EDITSTARTUPSCRIPT
:
559 event
.Enable(hasattr(self
, 'EditStartupScript'))
562 event
.Enable(hasattr(win
, 'DoFindNext'))
563 elif id == ID_FINDNEXT
:
564 event
.Enable(hasattr(win
, 'DoFindNext') and
565 self
.findData
.GetFindString() != '')
569 except AttributeError:
570 # This menu option is not supported in the current context.
574 def OnActivate(self
, event
):
576 Event Handler for losing the focus of the Frame. Should close
577 Autocomplete listbox, if shown.
579 if not event
.GetActive():
580 # If autocomplete active, cancel it. Otherwise, the
581 # autocomplete list will stay visible on top of the
582 # z-order after switching to another application
583 win
= wx
.Window
.FindFocus()
584 if hasattr(win
, 'AutoCompActive') and win
.AutoCompActive():
590 def LoadSettings(self
, config
):
591 """Called be derived classes to load settings specific to the Frame"""
592 pos
= wx
.Point(config
.ReadInt('Window/PosX', -1),
593 config
.ReadInt('Window/PosY', -1))
595 size
= wx
.Size(config
.ReadInt('Window/Width', -1),
596 config
.ReadInt('Window/Height', -1))
602 def SaveSettings(self
, config
):
603 """Called by derived classes to save Frame settings to a wx.Config object"""
605 # TODO: track position/size so we can save it even if the
606 # frame is maximized or iconized.
607 if not self
.iconized
and not self
.IsMaximized():
608 w
, h
= self
.GetSize()
609 config
.WriteInt('Window/Width', w
)
610 config
.WriteInt('Window/Height', h
)
612 px
, py
= self
.GetPosition()
613 config
.WriteInt('Window/PosX', px
)
614 config
.WriteInt('Window/PosY', py
)
619 class ShellFrameMixin
:
621 A mix-in class for frames that will have a Shell or a Crust window
622 and that want to add history, startupScript and other common
625 def __init__(self
, config
, dataDir
):
627 self
.dataDir
= dataDir
628 self
.startupScript
= os
.environ
.get('PYTHONSTARTUP')
629 if not self
.startupScript
and self
.dataDir
:
630 self
.startupScript
= os
.path
.join(self
.dataDir
, 'startup')
632 self
.autoSaveSettings
= False
633 self
.saveHistory
= False
635 # We need this one before we have a chance to load the settings...
636 self
.execStartupScript
= True
638 self
.execStartupScript
= self
.config
.ReadBool('Options/ExecStartupScript', True)
641 def OnHelp(self
, event
):
642 """Display a Help window."""
643 import wx
.lib
.dialogs
644 title
= 'Help on key bindings'
646 text
= wx
.py
.shell
.HELP_TEXT
648 dlg
= wx
.lib
.dialogs
.ScrolledMessageDialog(self
, text
, title
, size
= ((700, 540)))
649 fnt
= wx
.Font(10, wx
.TELETYPE
, wx
.NORMAL
, wx
.NORMAL
)
650 dlg
.GetChildren()[0].SetFont(fnt
)
651 dlg
.GetChildren()[0].SetInsertionPoint(0)
656 def LoadSettings(self
):
657 if self
.config
is not None:
658 self
.autoSaveSettings
= self
.config
.ReadBool('Options/AutoSaveSettings', False)
659 self
.execStartupScript
= self
.config
.ReadBool('Options/ExecStartupScript', True)
660 self
.saveHistory
= self
.config
.ReadBool('Options/SaveHistory', False)
664 def SaveSettings(self
):
665 if self
.config
is not None:
666 # always save this one
667 self
.config
.WriteBool('Options/AutoSaveSettings', self
.autoSaveSettings
)
668 if self
.autoSaveSettings
:
669 self
.config
.WriteBool('Options/SaveHistory', self
.saveHistory
)
670 self
.config
.WriteBool('Options/ExecStartupScript', self
.execStartupScript
)
675 def SaveHistory(self
):
678 # always open the file so that when we are not
679 # saving the history, the old file is emptied.
680 name
= os
.path
.join(self
.dataDir
, 'history')
683 hist
= '\n'.join(self
.shell
.history
)
687 d
= wx
.MessageDialog(self
, "Error saving history file.",
688 "Error", wx
.ICON_EXCLAMATION
)
693 def LoadHistory(self
):
695 name
= os
.path
.join(self
.dataDir
, 'history')
696 if os
.path
.exists(name
):
701 self
.shell
.history
= hist
.split('\n')
703 d
= wx
.MessageDialog(self
, "Error loading history file.",
704 "Error", wx
.ICON_EXCLAMATION
)
709 def bufferHasChanged(self
):
710 # the shell buffers can always be saved
713 def bufferSave(self
):
715 appname
= wx
.GetApp().GetAppName()
716 default
= appname
+ '-' + time
.strftime("%Y%m%d-%H%M.py")
717 fileName
= wx
.FileSelector("Save File As", "Saving",
718 default_filename
=default
,
719 default_extension
="py",
721 flags
= wx
.SAVE | wx
.OVERWRITE_PROMPT
)
725 text
= self
.shell
.GetText()
727 ## This isn't working currently...
728 ## 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)
729 ## yes_no = d.ShowModal()
730 ## if yes_no == wx.ID_YES:
731 ## m = re.findall('^[>\.]{3,3} (.*)\r', text, re.MULTILINE | re.LOCALE)
732 ## text = '\n'.join(m)
736 f
= open(fileName
, "w")
740 d
= wx
.MessageDialog(self
, u
'Error saving session',u
'Error',
741 wx
.OK | wx
.ICON_ERROR
)
746 def EditStartupScript(self
):
747 if os
.path
.exists(self
.startupScript
):
748 text
= file(self
.startupScript
, 'U').read()
752 dlg
= EditStartupScriptDialog(self
, self
.startupScript
, text
)
753 if dlg
.ShowModal() == wx
.ID_OK
:
756 f
= file(self
.startupScript
, 'w')
760 d
= wx
.MessageDialog(self
, "Error saving startup file.",
761 "Error", wx
.ICON_EXCLAMATION
)
767 class EditStartupScriptDialog(wx
.Dialog
):
768 def __init__(self
, parent
, fileName
, text
):
769 wx
.Dialog
.__init
__(self
, parent
, size
=(425,350),
770 title
="Edit Startup Script",
771 style
=wx
.DEFAULT_DIALOG_STYLE|wx
.RESIZE_BORDER
)
773 pst
= wx
.StaticText(self
, -1, "Path:")
774 ptx
= wx
.TextCtrl(self
, -1, fileName
, style
=wx
.TE_READONLY
)
775 self
.editor
= editwindow
.EditWindow(self
)
776 self
.editor
.SetText(text
)
777 wx
.CallAfter(self
.editor
.SetFocus
)
779 ok
= wx
.Button(self
, wx
.ID_OK
)
780 cancel
= wx
.Button(self
, wx
.ID_CANCEL
)
782 mainSizer
= wx
.BoxSizer(wx
.VERTICAL
)
784 pthSizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
785 pthSizer
.Add(pst
, flag
=wx
.ALIGN_CENTER_VERTICAL
)
788 mainSizer
.Add(pthSizer
, 0, wx
.EXPAND|wx
.ALL
, 10)
790 mainSizer
.Add(self
.editor
, 1, wx
.EXPAND|wx
.LEFT|wx
.RIGHT
, 10)
792 btnSizer
= wx
.BoxSizer(wx
.HORIZONTAL
)
793 btnSizer
.Add((5,5), 1)
795 btnSizer
.Add((5,5), 1)
797 btnSizer
.Add((5,5), 1)
798 mainSizer
.Add(btnSizer
, 0, wx
.EXPAND|wx
.ALL
, 10)
800 self
.SetSizer(mainSizer
)
805 return self
.editor
.GetText()