]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/py/frame.py
Allow the frame (if there is one) to resize the toolbar after Realize
[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 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)
552
553 elif id == ID_EXECSTARTUPSCRIPT:
554 event.Check(self.execStartupScript)
555
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'))
560
561 elif id == ID_FIND:
562 event.Enable(hasattr(win, 'DoFindNext'))
563 elif id == ID_FINDNEXT:
564 event.Enable(hasattr(win, 'DoFindNext') and
565 self.findData.GetFindString() != '')
566
567 else:
568 event.Enable(False)
569 except AttributeError:
570 # This menu option is not supported in the current context.
571 event.Enable(False)
572
573
574 def OnActivate(self, event):
575 """
576 Event Handler for losing the focus of the Frame. Should close
577 Autocomplete listbox, if shown.
578 """
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():
585 win.AutoCompCancel()
586 event.Skip()
587
588
589
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))
594
595 size = wx.Size(config.ReadInt('Window/Width', -1),
596 config.ReadInt('Window/Height', -1))
597
598 self.SetSize(size)
599 self.Move(pos)
600
601
602 def SaveSettings(self, config):
603 """Called by derived classes to save Frame settings to a wx.Config object"""
604
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)
611
612 px, py = self.GetPosition()
613 config.WriteInt('Window/PosX', px)
614 config.WriteInt('Window/PosY', py)
615
616
617
618
619 class ShellFrameMixin:
620 """
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
623 functionality.
624 """
625 def __init__(self, config, dataDir):
626 self.config = config
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')
631
632 self.autoSaveSettings = False
633 self.saveHistory = False
634
635 # We need this one before we have a chance to load the settings...
636 self.execStartupScript = True
637 if self.config:
638 self.execStartupScript = self.config.ReadBool('Options/ExecStartupScript', True)
639
640
641 def OnHelp(self, event):
642 """Display a Help window."""
643 import wx.lib.dialogs
644 title = 'Help on key bindings'
645
646 text = wx.py.shell.HELP_TEXT
647
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)
652 dlg.ShowModal()
653 dlg.Destroy()
654
655
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)
661 self.LoadHistory()
662
663
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)
671 self.SaveHistory()
672
673
674
675 def SaveHistory(self):
676 if self.dataDir:
677 try:
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')
681 f = file(name, 'w')
682 if self.saveHistory:
683 hist = '\n'.join(self.shell.history)
684 f.write(hist)
685 f.close()
686 except:
687 d = wx.MessageDialog(self, "Error saving history file.",
688 "Error", wx.ICON_EXCLAMATION)
689 d.ShowModal()
690 d.Destroy()
691
692
693 def LoadHistory(self):
694 if self.dataDir:
695 name = os.path.join(self.dataDir, 'history')
696 if os.path.exists(name):
697 try:
698 f = file(name, 'U')
699 hist = f.read()
700 f.close()
701 self.shell.history = hist.split('\n')
702 except:
703 d = wx.MessageDialog(self, "Error loading history file.",
704 "Error", wx.ICON_EXCLAMATION)
705 d.ShowModal()
706 d.Destroy()
707
708
709 def bufferHasChanged(self):
710 # the shell buffers can always be saved
711 return True
712
713 def bufferSave(self):
714 import time
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",
720 wildcard="*.py",
721 flags = wx.SAVE | wx.OVERWRITE_PROMPT)
722 if not fileName:
723 return
724
725 text = self.shell.GetText()
726
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)
733 ## d.Destroy()
734
735 try:
736 f = open(fileName, "w")
737 f.write(text)
738 f.close()
739 except:
740 d = wx.MessageDialog(self, u'Error saving session',u'Error',
741 wx.OK | wx.ICON_ERROR)
742 d.ShowModal()
743 d.Destroy()
744
745
746 def EditStartupScript(self):
747 if os.path.exists(self.startupScript):
748 text = file(self.startupScript, 'U').read()
749 else:
750 text = ''
751
752 dlg = EditStartupScriptDialog(self, self.startupScript, text)
753 if dlg.ShowModal() == wx.ID_OK:
754 text = dlg.GetText()
755 try:
756 f = file(self.startupScript, 'w')
757 f.write(text)
758 f.close()
759 except:
760 d = wx.MessageDialog(self, "Error saving startup file.",
761 "Error", wx.ICON_EXCLAMATION)
762 d.ShowModal()
763 d.Destroy()
764
765
766
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)
772
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)
778
779 ok = wx.Button(self, wx.ID_OK)
780 cancel = wx.Button(self, wx.ID_CANCEL)
781
782 mainSizer = wx.BoxSizer(wx.VERTICAL)
783
784 pthSizer = wx.BoxSizer(wx.HORIZONTAL)
785 pthSizer.Add(pst, flag=wx.ALIGN_CENTER_VERTICAL)
786 pthSizer.Add((5,5))
787 pthSizer.Add(ptx, 1)
788 mainSizer.Add(pthSizer, 0, wx.EXPAND|wx.ALL, 10)
789
790 mainSizer.Add(self.editor, 1, wx.EXPAND|wx.LEFT|wx.RIGHT, 10)
791
792 btnSizer = wx.BoxSizer(wx.HORIZONTAL)
793 btnSizer.Add((5,5), 1)
794 btnSizer.Add(ok)
795 btnSizer.Add((5,5), 1)
796 btnSizer.Add(cancel)
797 btnSizer.Add((5,5), 1)
798 mainSizer.Add(btnSizer, 0, wx.EXPAND|wx.ALL, 10)
799
800 self.SetSizer(mainSizer)
801 self.Layout()
802
803
804 def GetText(self):
805 return self.editor.GetText()