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