]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/py/frame.py
Respect the REUSEADDR flag to allow rebinding; bind to local port prior to connecting...
[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',
180 'Use anti-aliased fonts', wx.ITEM_CHECK)
181
182 m.AppendSeparator()
183 m.Append(ID_SAVEHISTORY, '&Save History',
184 'Automatically save history on close', wx.ITEM_CHECK)
185 self.startupMenu = wx.Menu()
186 self.startupMenu.Append(ID_EXECSTARTUPSCRIPT,
187 'E&xecute Startup Script',
188 'Execute Startup Script', wx.ITEM_CHECK)
189 self.startupMenu.Append(ID_EDITSTARTUPSCRIPT,
190 '&Edit Startup Script',
191 'Edit Startup Script')
192 m.AppendMenu(ID_STARTUP, '&Startup', self.startupMenu, 'Startup Options')
193
194 self.settingsMenu = wx.Menu()
195 self.settingsMenu.Append(ID_AUTO_SAVESETTINGS,
196 '&Auto Save Settings',
197 'Automatically save settings on close', wx.ITEM_CHECK)
198 self.settingsMenu.Append(ID_SAVESETTINGS,
199 '&Save Settings',
200 'Save settings now')
201 self.settingsMenu.Append(ID_DELSETTINGSFILE,
202 '&Revert to default',
203 'Revert to the default settings')
204 m.AppendMenu(ID_SETTINGS, '&Settings', self.settingsMenu, 'Settings Options')
205
206 m = self.helpMenu = wx.Menu()
207 m.Append(ID_HELP, '&Help\tF1', 'Help!')
208 m.AppendSeparator()
209 m.Append(ID_ABOUT, '&About...', 'About this program')
210
211 b = self.menuBar = wx.MenuBar()
212 b.Append(self.fileMenu, '&File')
213 b.Append(self.editMenu, '&Edit')
214 b.Append(self.viewMenu, '&View')
215 b.Append(self.optionsMenu, '&Options')
216 b.Append(self.helpMenu, '&Help')
217 self.SetMenuBar(b)
218
219 self.Bind(wx.EVT_MENU, self.OnFileNew, id=ID_NEW)
220 self.Bind(wx.EVT_MENU, self.OnFileOpen, id=ID_OPEN)
221 self.Bind(wx.EVT_MENU, self.OnFileRevert, id=ID_REVERT)
222 self.Bind(wx.EVT_MENU, self.OnFileClose, id=ID_CLOSE)
223 self.Bind(wx.EVT_MENU, self.OnFileSave, id=ID_SAVE)
224 self.Bind(wx.EVT_MENU, self.OnFileSaveAs, id=ID_SAVEAS)
225 self.Bind(wx.EVT_MENU, self.OnFileUpdateNamespace, id=ID_NAMESPACE)
226 self.Bind(wx.EVT_MENU, self.OnFilePrint, id=ID_PRINT)
227 self.Bind(wx.EVT_MENU, self.OnExit, id=ID_EXIT)
228 self.Bind(wx.EVT_MENU, self.OnUndo, id=ID_UNDO)
229 self.Bind(wx.EVT_MENU, self.OnRedo, id=ID_REDO)
230 self.Bind(wx.EVT_MENU, self.OnCut, id=ID_CUT)
231 self.Bind(wx.EVT_MENU, self.OnCopy, id=ID_COPY)
232 self.Bind(wx.EVT_MENU, self.OnCopyPlus, id=ID_COPY_PLUS)
233 self.Bind(wx.EVT_MENU, self.OnPaste, id=ID_PASTE)
234 self.Bind(wx.EVT_MENU, self.OnPastePlus, id=ID_PASTE_PLUS)
235 self.Bind(wx.EVT_MENU, self.OnClear, id=ID_CLEAR)
236 self.Bind(wx.EVT_MENU, self.OnSelectAll, id=ID_SELECTALL)
237 self.Bind(wx.EVT_MENU, self.OnEmptyBuffer, id=ID_EMPTYBUFFER)
238 self.Bind(wx.EVT_MENU, self.OnAbout, id=ID_ABOUT)
239 self.Bind(wx.EVT_MENU, self.OnHelp, id=ID_HELP)
240 self.Bind(wx.EVT_MENU, self.OnAutoCompleteShow, id=ID_AUTOCOMP_SHOW)
241 self.Bind(wx.EVT_MENU, self.OnAutoCompleteMagic, id=ID_AUTOCOMP_MAGIC)
242 self.Bind(wx.EVT_MENU, self.OnAutoCompleteSingle, id=ID_AUTOCOMP_SINGLE)
243 self.Bind(wx.EVT_MENU, self.OnAutoCompleteDouble, id=ID_AUTOCOMP_DOUBLE)
244 self.Bind(wx.EVT_MENU, self.OnCallTipsShow, id=ID_CALLTIPS_SHOW)
245 self.Bind(wx.EVT_MENU, self.OnCallTipsInsert, id=ID_CALLTIPS_INSERT)
246 self.Bind(wx.EVT_MENU, self.OnWrap, id=ID_WRAP)
247 self.Bind(wx.EVT_MENU, self.OnUseAA, id=ID_USEAA)
248 self.Bind(wx.EVT_MENU, self.OnToggleMaximize, id=ID_TOGGLE_MAXIMIZE)
249 self.Bind(wx.EVT_MENU, self.OnShowLineNumbers, id=ID_SHOW_LINENUMBERS)
250 self.Bind(wx.EVT_MENU, self.OnAutoSaveSettings, id=ID_AUTO_SAVESETTINGS)
251 self.Bind(wx.EVT_MENU, self.OnSaveHistory, id=ID_SAVEHISTORY)
252 self.Bind(wx.EVT_MENU, self.OnSaveSettings, id=ID_SAVESETTINGS)
253 self.Bind(wx.EVT_MENU, self.OnDelSettingsFile, id=ID_DELSETTINGSFILE)
254 self.Bind(wx.EVT_MENU, self.OnEditStartupScript, id=ID_EDITSTARTUPSCRIPT)
255 self.Bind(wx.EVT_MENU, self.OnExecStartupScript, id=ID_EXECSTARTUPSCRIPT)
256 self.Bind(wx.EVT_MENU, self.OnFindText, id=ID_FIND)
257 self.Bind(wx.EVT_MENU, self.OnFindNext, id=ID_FINDNEXT)
258
259 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_NEW)
260 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_OPEN)
261 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_REVERT)
262 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_CLOSE)
263 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_SAVE)
264 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_SAVEAS)
265 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_NAMESPACE)
266 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_PRINT)
267 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_UNDO)
268 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_REDO)
269 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_CUT)
270 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_COPY)
271 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_COPY_PLUS)
272 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_PASTE)
273 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_PASTE_PLUS)
274 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_CLEAR)
275 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_SELECTALL)
276 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_EMPTYBUFFER)
277 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_AUTOCOMP_SHOW)
278 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_AUTOCOMP_MAGIC)
279 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_AUTOCOMP_SINGLE)
280 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_AUTOCOMP_DOUBLE)
281 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_CALLTIPS_SHOW)
282 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_CALLTIPS_INSERT)
283 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_WRAP)
284 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_USEAA)
285 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_SHOW_LINENUMBERS)
286 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_AUTO_SAVESETTINGS)
287 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_SAVESETTINGS)
288 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_DELSETTINGSFILE)
289 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_EXECSTARTUPSCRIPT)
290 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_SAVEHISTORY)
291 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_EDITSTARTUPSCRIPT)
292 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_FIND)
293 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_FINDNEXT)
294
295 self.Bind(wx.EVT_ACTIVATE, self.OnActivate)
296 self.Bind(wx.EVT_FIND, self.OnFindNext)
297 self.Bind(wx.EVT_FIND_NEXT, self.OnFindNext)
298 self.Bind(wx.EVT_FIND_CLOSE, self.OnFindClose)
299
300
301
302 def OnShowLineNumbers(self, event):
303 win = wx.Window.FindFocus()
304 if hasattr(win, 'lineNumbers'):
305 win.lineNumbers = event.IsChecked()
306 win.setDisplayLineNumbers(win.lineNumbers)
307
308 def OnToggleMaximize(self, event):
309 self.Maximize(not self.IsMaximized())
310
311 def OnFileNew(self, event):
312 self.bufferNew()
313
314 def OnFileOpen(self, event):
315 self.bufferOpen()
316
317 def OnFileRevert(self, event):
318 self.bufferRevert()
319
320 def OnFileClose(self, event):
321 self.bufferClose()
322
323 def OnFileSave(self, event):
324 self.bufferSave()
325
326 def OnFileSaveAs(self, event):
327 self.bufferSaveAs()
328
329 def OnFileUpdateNamespace(self, event):
330 self.updateNamespace()
331
332 def OnFilePrint(self, event):
333 self.bufferPrint()
334
335 def OnExit(self, event):
336 self.Close(False)
337
338 def OnUndo(self, event):
339 win = wx.Window.FindFocus()
340 win.Undo()
341
342 def OnRedo(self, event):
343 win = wx.Window.FindFocus()
344 win.Redo()
345
346 def OnCut(self, event):
347 win = wx.Window.FindFocus()
348 win.Cut()
349
350 def OnCopy(self, event):
351 win = wx.Window.FindFocus()
352 win.Copy()
353
354 def OnCopyPlus(self, event):
355 win = wx.Window.FindFocus()
356 win.CopyWithPrompts()
357
358 def OnPaste(self, event):
359 win = wx.Window.FindFocus()
360 win.Paste()
361
362 def OnPastePlus(self, event):
363 win = wx.Window.FindFocus()
364 win.PasteAndRun()
365
366 def OnClear(self, event):
367 win = wx.Window.FindFocus()
368 win.Clear()
369
370 def OnEmptyBuffer(self, event):
371 win = wx.Window.FindFocus()
372 d = wx.MessageDialog(self,
373 "Are you sure you want to clear the edit buffer,\n"
374 "deleting all the text?",
375 "Empty Buffer", wx.OK | wx.CANCEL | wx.ICON_QUESTION)
376 answer = d.ShowModal()
377 d.Destroy()
378 if (answer == wx.ID_OK):
379 win.ClearAll()
380 if hasattr(win,'prompt'):
381 win.prompt()
382
383 def OnSelectAll(self, event):
384 win = wx.Window.FindFocus()
385 win.SelectAll()
386
387 def OnAbout(self, event):
388 """Display an About window."""
389 title = 'About'
390 text = 'Your message here.'
391 dialog = wx.MessageDialog(self, text, title,
392 wx.OK | wx.ICON_INFORMATION)
393 dialog.ShowModal()
394 dialog.Destroy()
395
396 def OnHelp(self, event):
397 """Display a Help window."""
398 title = 'Help'
399 text = "Type 'shell.help()' in the shell window."
400 dialog = wx.MessageDialog(self, text, title,
401 wx.OK | wx.ICON_INFORMATION)
402 dialog.ShowModal()
403 dialog.Destroy()
404
405 def OnAutoCompleteShow(self, event):
406 win = wx.Window.FindFocus()
407 win.autoComplete = event.IsChecked()
408
409 def OnAutoCompleteMagic(self, event):
410 win = wx.Window.FindFocus()
411 win.autoCompleteIncludeMagic = event.IsChecked()
412
413 def OnAutoCompleteSingle(self, event):
414 win = wx.Window.FindFocus()
415 win.autoCompleteIncludeSingle = event.IsChecked()
416
417 def OnAutoCompleteDouble(self, event):
418 win = wx.Window.FindFocus()
419 win.autoCompleteIncludeDouble = event.IsChecked()
420
421 def OnCallTipsShow(self, event):
422 win = wx.Window.FindFocus()
423 win.autoCallTip = event.IsChecked()
424
425 def OnCallTipsInsert(self, event):
426 win = wx.Window.FindFocus()
427 win.callTipInsert = event.IsChecked()
428
429 def OnWrap(self, event):
430 win = wx.Window.FindFocus()
431 win.SetWrapMode(event.IsChecked())
432 wx.FutureCall(1, self.shell.EnsureCaretVisible)
433
434 def OnUseAA(self, event):
435 win = wx.Window.FindFocus()
436 win.SetUseAntiAliasing(event.IsChecked())
437
438 def OnSaveHistory(self, event):
439 self.saveHistory = event.IsChecked()
440
441 def OnAutoSaveSettings(self, event):
442 self.autoSaveSettings = event.IsChecked()
443
444 def OnSaveSettings(self, event):
445 self.DoSaveSettings()
446
447 def OnDelSettingsFile(self, event):
448 if self.config is not None:
449 d = wx.MessageDialog(
450 self, "Do you want to revert to the default settings?\n" +
451 "A restart is needed for the change to take effect",
452 "Warning", wx.OK | wx.CANCEL | wx.ICON_QUESTION)
453 answer = d.ShowModal()
454 d.Destroy()
455 if (answer == wx.ID_OK):
456 self.config.DeleteAll()
457 self.LoadSettings()
458
459
460 def OnEditStartupScript(self, event):
461 if hasattr(self, 'EditStartupScript'):
462 self.EditStartupScript()
463
464 def OnExecStartupScript(self, event):
465 self.execStartupScript = event.IsChecked()
466
467
468 def OnFindText(self, event):
469 if self.findDlg is not None:
470 return
471 win = wx.Window.FindFocus()
472 self.findDlg = wx.FindReplaceDialog(win, self.findData, "Find",
473 wx.FR_NOWHOLEWORD)
474 self.findDlg.Show()
475
476 def OnFindNext(self, event):
477 if isinstance(event, wx.FindDialogEvent):
478 win = self.findDlg.GetParent()
479 else:
480 win = wx.Window.FindFocus()
481 win.DoFindNext(self.findData, self.findDlg)
482
483 def OnFindClose(self, event):
484 self.findDlg.Destroy()
485 self.findDlg = None
486
487
488
489 def OnUpdateMenu(self, event):
490 """Update menu items based on current status and context."""
491 win = wx.Window.FindFocus()
492 id = event.GetId()
493 event.Enable(True)
494 try:
495 if id == ID_NEW:
496 event.Enable(hasattr(self, 'bufferNew'))
497 elif id == ID_OPEN:
498 event.Enable(hasattr(self, 'bufferOpen'))
499 elif id == ID_REVERT:
500 event.Enable(hasattr(self, 'bufferRevert')
501 and self.hasBuffer())
502 elif id == ID_CLOSE:
503 event.Enable(hasattr(self, 'bufferClose')
504 and self.hasBuffer())
505 elif id == ID_SAVE:
506 event.Enable(hasattr(self, 'bufferSave')
507 and self.bufferHasChanged())
508 elif id == ID_SAVEAS:
509 event.Enable(hasattr(self, 'bufferSaveAs')
510 and self.hasBuffer())
511 elif id == ID_NAMESPACE:
512 event.Enable(hasattr(self, 'updateNamespace')
513 and self.hasBuffer())
514 elif id == ID_PRINT:
515 event.Enable(hasattr(self, 'bufferPrint')
516 and self.hasBuffer())
517 elif id == ID_UNDO:
518 event.Enable(win.CanUndo())
519 elif id == ID_REDO:
520 event.Enable(win.CanRedo())
521 elif id == ID_CUT:
522 event.Enable(win.CanCut())
523 elif id == ID_COPY:
524 event.Enable(win.CanCopy())
525 elif id == ID_COPY_PLUS:
526 event.Enable(win.CanCopy() and hasattr(win, 'CopyWithPrompts'))
527 elif id == ID_PASTE:
528 event.Enable(win.CanPaste())
529 elif id == ID_PASTE_PLUS:
530 event.Enable(win.CanPaste() and hasattr(win, 'PasteAndRun'))
531 elif id == ID_CLEAR:
532 event.Enable(win.CanCut())
533 elif id == ID_SELECTALL:
534 event.Enable(hasattr(win, 'SelectAll'))
535 elif id == ID_EMPTYBUFFER:
536 event.Enable(hasattr(win, 'ClearAll') and not win.GetReadOnly())
537 elif id == ID_AUTOCOMP_SHOW:
538 event.Check(win.autoComplete)
539 elif id == ID_AUTOCOMP_MAGIC:
540 event.Check(win.autoCompleteIncludeMagic)
541 elif id == ID_AUTOCOMP_SINGLE:
542 event.Check(win.autoCompleteIncludeSingle)
543 elif id == ID_AUTOCOMP_DOUBLE:
544 event.Check(win.autoCompleteIncludeDouble)
545 elif id == ID_CALLTIPS_SHOW:
546 event.Check(win.autoCallTip)
547 elif id == ID_CALLTIPS_INSERT:
548 event.Check(win.callTipInsert)
549 elif id == ID_WRAP:
550 event.Check(win.GetWrapMode())
551 elif id == ID_USEAA:
552 event.Check(win.GetUseAntiAliasing())
553
554 elif id == ID_SHOW_LINENUMBERS:
555 event.Check(win.lineNumbers)
556 elif id == ID_AUTO_SAVESETTINGS:
557 event.Check(self.autoSaveSettings)
558 event.Enable(self.config is not None)
559 elif id == ID_SAVESETTINGS:
560 event.Enable(self.config is not None and
561 hasattr(self, 'DoSaveSettings'))
562 elif id == ID_DELSETTINGSFILE:
563 event.Enable(self.config is not None)
564
565 elif id == ID_EXECSTARTUPSCRIPT:
566 event.Check(self.execStartupScript)
567 event.Enable(self.config is not None)
568
569 elif id == ID_SAVEHISTORY:
570 event.Check(self.saveHistory)
571 event.Enable(self.dataDir is not None)
572 elif id == ID_EDITSTARTUPSCRIPT:
573 event.Enable(hasattr(self, 'EditStartupScript'))
574 event.Enable(self.dataDir is not None)
575
576 elif id == ID_FIND:
577 event.Enable(hasattr(win, 'DoFindNext'))
578 elif id == ID_FINDNEXT:
579 event.Enable(hasattr(win, 'DoFindNext') and
580 self.findData.GetFindString() != '')
581
582 else:
583 event.Enable(False)
584 except AttributeError:
585 # This menu option is not supported in the current context.
586 event.Enable(False)
587
588
589 def OnActivate(self, event):
590 """
591 Event Handler for losing the focus of the Frame. Should close
592 Autocomplete listbox, if shown.
593 """
594 if not event.GetActive():
595 # If autocomplete active, cancel it. Otherwise, the
596 # autocomplete list will stay visible on top of the
597 # z-order after switching to another application
598 win = wx.Window.FindFocus()
599 if hasattr(win, 'AutoCompActive') and win.AutoCompActive():
600 win.AutoCompCancel()
601 event.Skip()
602
603
604
605 def LoadSettings(self, config):
606 """Called be derived classes to load settings specific to the Frame"""
607 pos = wx.Point(config.ReadInt('Window/PosX', -1),
608 config.ReadInt('Window/PosY', -1))
609
610 size = wx.Size(config.ReadInt('Window/Width', -1),
611 config.ReadInt('Window/Height', -1))
612
613 self.SetSize(size)
614 self.Move(pos)
615
616
617 def SaveSettings(self, config):
618 """Called by derived classes to save Frame settings to a wx.Config object"""
619
620 # TODO: track position/size so we can save it even if the
621 # frame is maximized or iconized.
622 if not self.iconized and not self.IsMaximized():
623 w, h = self.GetSize()
624 config.WriteInt('Window/Width', w)
625 config.WriteInt('Window/Height', h)
626
627 px, py = self.GetPosition()
628 config.WriteInt('Window/PosX', px)
629 config.WriteInt('Window/PosY', py)
630
631
632
633
634 class ShellFrameMixin:
635 """
636 A mix-in class for frames that will have a Shell or a Crust window
637 and that want to add history, startupScript and other common
638 functionality.
639 """
640 def __init__(self, config, dataDir):
641 self.config = config
642 self.dataDir = dataDir
643 self.startupScript = os.environ.get('PYTHONSTARTUP')
644 if not self.startupScript and self.dataDir:
645 self.startupScript = os.path.join(self.dataDir, 'startup')
646
647 self.autoSaveSettings = False
648 self.saveHistory = False
649
650 # We need this one before we have a chance to load the settings...
651 self.execStartupScript = True
652 if self.config:
653 self.execStartupScript = self.config.ReadBool('Options/ExecStartupScript', True)
654
655
656 def OnHelp(self, event):
657 """Display a Help window."""
658 import wx.lib.dialogs
659 title = 'Help on key bindings'
660
661 text = wx.py.shell.HELP_TEXT
662
663 dlg = wx.lib.dialogs.ScrolledMessageDialog(self, text, title, size = ((700, 540)))
664 fnt = wx.Font(10, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
665 dlg.GetChildren()[0].SetFont(fnt)
666 dlg.GetChildren()[0].SetInsertionPoint(0)
667 dlg.ShowModal()
668 dlg.Destroy()
669
670
671 def LoadSettings(self):
672 if self.config is not None:
673 self.autoSaveSettings = self.config.ReadBool('Options/AutoSaveSettings', False)
674 self.execStartupScript = self.config.ReadBool('Options/ExecStartupScript', True)
675 self.saveHistory = self.config.ReadBool('Options/SaveHistory', False)
676 self.LoadHistory()
677
678
679 def SaveSettings(self):
680 if self.config is not None:
681 # always save this one
682 self.config.WriteBool('Options/AutoSaveSettings', self.autoSaveSettings)
683 if self.autoSaveSettings:
684 self.config.WriteBool('Options/SaveHistory', self.saveHistory)
685 self.config.WriteBool('Options/ExecStartupScript', self.execStartupScript)
686 self.SaveHistory()
687
688
689
690 def SaveHistory(self):
691 if self.dataDir:
692 try:
693 # always open the file so that when we are not
694 # saving the history, the old file is emptied.
695 name = os.path.join(self.dataDir, 'history')
696 f = file(name, 'w')
697 if self.saveHistory:
698 hist = '\n'.join(self.shell.history)
699 f.write(hist)
700 f.close()
701 except:
702 d = wx.MessageDialog(self, "Error saving history file.",
703 "Error", wx.ICON_EXCLAMATION)
704 d.ShowModal()
705 d.Destroy()
706
707
708 def LoadHistory(self):
709 if self.dataDir:
710 name = os.path.join(self.dataDir, 'history')
711 if os.path.exists(name):
712 try:
713 f = file(name, 'U')
714 hist = f.read()
715 f.close()
716 self.shell.history = hist.split('\n')
717 except:
718 d = wx.MessageDialog(self, "Error loading history file.",
719 "Error", wx.ICON_EXCLAMATION)
720 d.ShowModal()
721 d.Destroy()
722
723
724 def bufferHasChanged(self):
725 # the shell buffers can always be saved
726 return True
727
728 def bufferSave(self):
729 import time
730 appname = wx.GetApp().GetAppName()
731 default = appname + '-' + time.strftime("%Y%m%d-%H%M.py")
732 fileName = wx.FileSelector("Save File As", "Saving",
733 default_filename=default,
734 default_extension="py",
735 wildcard="*.py",
736 flags = wx.SAVE | wx.OVERWRITE_PROMPT)
737 if not fileName:
738 return
739
740 text = self.shell.GetText()
741
742 ## This isn't working currently...
743 ## 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)
744 ## yes_no = d.ShowModal()
745 ## if yes_no == wx.ID_YES:
746 ## m = re.findall('^[>\.]{3,3} (.*)\r', text, re.MULTILINE | re.LOCALE)
747 ## text = '\n'.join(m)
748 ## d.Destroy()
749
750 try:
751 f = open(fileName, "w")
752 f.write(text)
753 f.close()
754 except:
755 d = wx.MessageDialog(self, u'Error saving session',u'Error',
756 wx.OK | wx.ICON_ERROR)
757 d.ShowModal()
758 d.Destroy()
759
760
761 def EditStartupScript(self):
762 if os.path.exists(self.startupScript):
763 text = file(self.startupScript, 'U').read()
764 else:
765 text = ''
766
767 dlg = EditStartupScriptDialog(self, self.startupScript, text)
768 if dlg.ShowModal() == wx.ID_OK:
769 text = dlg.GetText()
770 try:
771 f = file(self.startupScript, 'w')
772 f.write(text)
773 f.close()
774 except:
775 d = wx.MessageDialog(self, "Error saving startup file.",
776 "Error", wx.ICON_EXCLAMATION)
777 d.ShowModal()
778 d.Destroy()
779
780
781
782 class EditStartupScriptDialog(wx.Dialog):
783 def __init__(self, parent, fileName, text):
784 wx.Dialog.__init__(self, parent, size=(425,350),
785 title="Edit Startup Script",
786 style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
787
788 pst = wx.StaticText(self, -1, "Path:")
789 ptx = wx.TextCtrl(self, -1, fileName, style=wx.TE_READONLY)
790 self.editor = editwindow.EditWindow(self)
791 self.editor.SetText(text)
792 wx.CallAfter(self.editor.SetFocus)
793
794 ok = wx.Button(self, wx.ID_OK)
795 cancel = wx.Button(self, wx.ID_CANCEL)
796
797 mainSizer = wx.BoxSizer(wx.VERTICAL)
798
799 pthSizer = wx.BoxSizer(wx.HORIZONTAL)
800 pthSizer.Add(pst, flag=wx.ALIGN_CENTER_VERTICAL)
801 pthSizer.Add((5,5))
802 pthSizer.Add(ptx, 1)
803 mainSizer.Add(pthSizer, 0, wx.EXPAND|wx.ALL, 10)
804
805 mainSizer.Add(self.editor, 1, wx.EXPAND|wx.LEFT|wx.RIGHT, 10)
806
807 btnSizer = wx.BoxSizer(wx.HORIZONTAL)
808 btnSizer.Add((5,5), 1)
809 btnSizer.Add(ok)
810 btnSizer.Add((5,5), 1)
811 btnSizer.Add(cancel)
812 btnSizer.Add((5,5), 1)
813 mainSizer.Add(btnSizer, 0, wx.EXPAND|wx.ALL, 10)
814
815 self.SetSizer(mainSizer)
816 self.Layout()
817
818
819 def GetText(self):
820 return self.editor.GetText()