]> git.saurik.com Git - wxWidgets.git/blame - wxPython/wx/py/frame.py
Allow clearing the history, and saving the history on demand
[wxWidgets.git] / wxPython / wx / py / frame.py
CommitLineData
d14a1e28 1"""Base frame with menu."""
1fded56b 2
d14a1e28
RD
3__author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
4__cvsid__ = "$Id$"
5__revision__ = "$Revision$"[11:-2]
1fded56b 6
d14a1e28 7import wx
02b800ce 8import os
d14a1e28 9from version import VERSION
02b800ce 10import editwindow
e773f79b 11import dispatcher
d14a1e28
RD
12
13ID_NEW = wx.ID_NEW
14ID_OPEN = wx.ID_OPEN
15ID_REVERT = wx.ID_REVERT
16ID_CLOSE = wx.ID_CLOSE
17ID_SAVE = wx.ID_SAVE
18ID_SAVEAS = wx.ID_SAVEAS
19ID_PRINT = wx.ID_PRINT
20ID_EXIT = wx.ID_EXIT
21ID_UNDO = wx.ID_UNDO
22ID_REDO = wx.ID_REDO
23ID_CUT = wx.ID_CUT
24ID_COPY = wx.ID_COPY
25ID_PASTE = wx.ID_PASTE
26ID_CLEAR = wx.ID_CLEAR
27ID_SELECTALL = wx.ID_SELECTALL
02b800ce 28ID_EMPTYBUFFER = wx.NewId()
d14a1e28 29ID_ABOUT = wx.ID_ABOUT
02b800ce 30ID_HELP = wx.NewId()
d14a1e28
RD
31ID_AUTOCOMP = wx.NewId()
32ID_AUTOCOMP_SHOW = wx.NewId()
33ID_AUTOCOMP_MAGIC = wx.NewId()
34ID_AUTOCOMP_SINGLE = wx.NewId()
35ID_AUTOCOMP_DOUBLE = wx.NewId()
36ID_CALLTIPS = wx.NewId()
37ID_CALLTIPS_SHOW = wx.NewId()
02b800ce 38ID_CALLTIPS_INSERT = wx.NewId()
d14a1e28
RD
39ID_COPY_PLUS = wx.NewId()
40ID_NAMESPACE = wx.NewId()
41ID_PASTE_PLUS = wx.NewId()
42ID_WRAP = wx.NewId()
02b800ce 43ID_TOGGLE_MAXIMIZE = wx.NewId()
9513c5b6 44ID_USEAA = wx.NewId()
02b800ce
RD
45ID_SHOW_LINENUMBERS = wx.NewId()
46ID_AUTO_SAVESETTINGS = wx.NewId()
47ID_SAVEHISTORY = wx.NewId()
e773f79b
RD
48ID_SAVEHISTORYNOW = wx.NewId()
49ID_CLEARHISTORY = wx.NewId()
02b800ce
RD
50ID_SAVESETTINGS = wx.NewId()
51ID_DELSETTINGSFILE = wx.NewId()
52ID_EDITSTARTUPSCRIPT = wx.NewId()
53ID_EXECSTARTUPSCRIPT = wx.NewId()
54ID_STARTUP = wx.NewId()
55ID_SETTINGS = wx.NewId()
56ID_FIND = wx.ID_FIND
57ID_FINDNEXT = wx.NewId()
58
d14a1e28
RD
59
60
61class 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()
02b800ce
RD
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
d14a1e28
RD
90
91 def OnClose(self, event):
92 """Event handler for closing."""
93 self.Destroy()
94
02b800ce 95
d14a1e28 96 def __createMenus(self):
02b800ce 97 # File Menu
d14a1e28
RD
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')
02b800ce 111 m.Append(ID_SAVEAS, 'Save &As \tCtrl+Shift+S',
d14a1e28
RD
112 'Save file with new name')
113 m.AppendSeparator()
114 m.Append(ID_PRINT, '&Print... \tCtrl+P',
115 'Print file')
116 m.AppendSeparator()
02b800ce 117 m.Append(ID_NAMESPACE, '&Update Namespace \tCtrl+Shift+N',
d14a1e28
RD
118 'Update namespace for autocompletion and calltips')
119 m.AppendSeparator()
02b800ce 120 m.Append(ID_EXIT, 'E&xit\tCtrl+Q', 'Exit Program')
d14a1e28 121
02b800ce 122 # Edit
d14a1e28
RD
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')
02b800ce 133 m.Append(ID_COPY_PLUS, 'Cop&y Plus \tCtrl+Shift+C',
d14a1e28
RD
134 'Copy the selection - retaining prompts')
135 m.Append(ID_PASTE, '&Paste \tCtrl+V', 'Paste from clipboard')
02b800ce 136 m.Append(ID_PASTE_PLUS, 'Past&e Plus \tCtrl+Shift+V',
d14a1e28
RD
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')
02b800ce 143 m.AppendSeparator()
e773f79b 144 m.Append(ID_EMPTYBUFFER, 'E&mpty Buffer...',
02b800ce 145 'Delete all the contents of the edit buffer')
e773f79b 146 m.Append(ID_FIND, '&Find Text... \tCtrl+F',
02b800ce
RD
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')
d14a1e28 157
02b800ce 158 # Options
d14a1e28 159 m = self.autocompMenu = wx.Menu()
02b800ce 160 m.Append(ID_AUTOCOMP_SHOW, 'Show &Auto Completion\tCtrl+Shift+A',
9513c5b6 161 'Show auto completion list', wx.ITEM_CHECK)
02b800ce 162 m.Append(ID_AUTOCOMP_MAGIC, 'Include &Magic Attributes\tCtrl+Shift+M',
d14a1e28 163 'Include attributes visible to __getattr__ and __setattr__',
9513c5b6 164 wx.ITEM_CHECK)
02b800ce 165 m.Append(ID_AUTOCOMP_SINGLE, 'Include Single &Underscores\tCtrl+Shift+U',
9513c5b6 166 'Include attibutes prefixed by a single underscore', wx.ITEM_CHECK)
02b800ce 167 m.Append(ID_AUTOCOMP_DOUBLE, 'Include &Double Underscores\tCtrl+Shift+D',
9513c5b6 168 'Include attibutes prefixed by a double underscore', wx.ITEM_CHECK)
d14a1e28 169 m = self.calltipsMenu = wx.Menu()
02b800ce 170 m.Append(ID_CALLTIPS_SHOW, 'Show Call &Tips\tCtrl+Shift+T',
9513c5b6 171 'Show call tips with argument signature and docstring', wx.ITEM_CHECK)
02b800ce
RD
172 m.Append(ID_CALLTIPS_INSERT, '&Insert Call Tips\tCtrl+Shift+I',
173 '&Insert Call Tips', wx.ITEM_CHECK)
d14a1e28
RD
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')
02b800ce 180
9513c5b6 181 if wx.Platform == "__WXMAC__":
486afba9 182 m.Append(ID_USEAA, '&Use AntiAliasing',
9513c5b6 183 'Use anti-aliased fonts', wx.ITEM_CHECK)
02b800ce
RD
184
185 m.AppendSeparator()
e773f79b
RD
186
187 self.historyMenu = wx.Menu()
188 self.historyMenu.Append(ID_SAVEHISTORY, '&Autosave History',
486afba9 189 'Automatically save history on close', wx.ITEM_CHECK)
e773f79b
RD
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
02b800ce 196 self.startupMenu = wx.Menu()
486afba9
RD
197 self.startupMenu.Append(ID_EXECSTARTUPSCRIPT,
198 'E&xecute Startup Script',
199 'Execute Startup Script', wx.ITEM_CHECK)
200 self.startupMenu.Append(ID_EDITSTARTUPSCRIPT,
e773f79b 201 '&Edit Startup Script...',
486afba9 202 'Edit Startup Script')
02b800ce
RD
203 m.AppendMenu(ID_STARTUP, '&Startup', self.startupMenu, 'Startup Options')
204
205 self.settingsMenu = wx.Menu()
486afba9
RD
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')
02b800ce 215 m.AppendMenu(ID_SETTINGS, '&Settings', self.settingsMenu, 'Settings Options')
d14a1e28
RD
216
217 m = self.helpMenu = wx.Menu()
02b800ce 218 m.Append(ID_HELP, '&Help\tF1', 'Help!')
d14a1e28 219 m.AppendSeparator()
486afba9 220 m.Append(ID_ABOUT, '&About...', 'About this program')
d14a1e28
RD
221
222 b = self.menuBar = wx.MenuBar()
223 b.Append(self.fileMenu, '&File')
224 b.Append(self.editMenu, '&Edit')
02b800ce 225 b.Append(self.viewMenu, '&View')
d14a1e28
RD
226 b.Append(self.optionsMenu, '&Options')
227 b.Append(self.helpMenu, '&Help')
228 self.SetMenuBar(b)
229
02b800ce
RD
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)
e773f79b
RD
263 self.Bind(wx.EVT_MENU, self.OnSaveHistoryNow, id=ID_SAVEHISTORYNOW)
264 self.Bind(wx.EVT_MENU, self.OnClearHistory, id=ID_CLEARHISTORY)
02b800ce
RD
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)
e773f79b
RD
304 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_SAVEHISTORYNOW)
305 self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateMenu, id=ID_CLEARHISTORY)
02b800ce
RD
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())
d14a1e28
RD
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):
02b800ce 354 win = wx.Window.FindFocus()
d14a1e28
RD
355 win.Undo()
356
357 def OnRedo(self, event):
02b800ce 358 win = wx.Window.FindFocus()
d14a1e28
RD
359 win.Redo()
360
361 def OnCut(self, event):
02b800ce 362 win = wx.Window.FindFocus()
d14a1e28
RD
363 win.Cut()
364
365 def OnCopy(self, event):
02b800ce 366 win = wx.Window.FindFocus()
d14a1e28
RD
367 win.Copy()
368
369 def OnCopyPlus(self, event):
02b800ce 370 win = wx.Window.FindFocus()
d14a1e28
RD
371 win.CopyWithPrompts()
372
373 def OnPaste(self, event):
02b800ce 374 win = wx.Window.FindFocus()
d14a1e28
RD
375 win.Paste()
376
377 def OnPastePlus(self, event):
02b800ce 378 win = wx.Window.FindFocus()
d14a1e28
RD
379 win.PasteAndRun()
380
381 def OnClear(self, event):
02b800ce 382 win = wx.Window.FindFocus()
d14a1e28 383 win.Clear()
02b800ce
RD
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()
d14a1e28
RD
397
398 def OnSelectAll(self, event):
02b800ce 399 win = wx.Window.FindFocus()
d14a1e28
RD
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
02b800ce
RD
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
d14a1e28 420 def OnAutoCompleteShow(self, event):
02b800ce 421 win = wx.Window.FindFocus()
d14a1e28
RD
422 win.autoComplete = event.IsChecked()
423
424 def OnAutoCompleteMagic(self, event):
02b800ce 425 win = wx.Window.FindFocus()
d14a1e28
RD
426 win.autoCompleteIncludeMagic = event.IsChecked()
427
428 def OnAutoCompleteSingle(self, event):
02b800ce 429 win = wx.Window.FindFocus()
d14a1e28
RD
430 win.autoCompleteIncludeSingle = event.IsChecked()
431
432 def OnAutoCompleteDouble(self, event):
02b800ce 433 win = wx.Window.FindFocus()
d14a1e28
RD
434 win.autoCompleteIncludeDouble = event.IsChecked()
435
436 def OnCallTipsShow(self, event):
02b800ce 437 win = wx.Window.FindFocus()
d14a1e28
RD
438 win.autoCallTip = event.IsChecked()
439
02b800ce
RD
440 def OnCallTipsInsert(self, event):
441 win = wx.Window.FindFocus()
442 win.callTipInsert = event.IsChecked()
443
d14a1e28 444 def OnWrap(self, event):
02b800ce 445 win = wx.Window.FindFocus()
d14a1e28 446 win.SetWrapMode(event.IsChecked())
02b800ce 447 wx.FutureCall(1, self.shell.EnsureCaretVisible)
d14a1e28 448
9513c5b6 449 def OnUseAA(self, event):
02b800ce 450 win = wx.Window.FindFocus()
9513c5b6 451 win.SetUseAntiAliasing(event.IsChecked())
02b800ce
RD
452
453 def OnSaveHistory(self, event):
e773f79b
RD
454 self.autoSaveHistory = event.IsChecked()
455
456 def OnSaveHistoryNow(self, event):
457 self.SaveHistory()
458
459 def OnClearHistory(self, event):
460 self.shell.clearHistory()
02b800ce
RD
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()
9513c5b6 496
02b800ce 497 def OnFindNext(self, event):
e773f79b
RD
498 if not self.findData.GetFindString():
499 self.OnFindText(event)
500 return
02b800ce
RD
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
507 def OnFindClose(self, event):
508 self.findDlg.Destroy()
509 self.findDlg = None
510
511
9513c5b6 512
d14a1e28
RD
513 def OnUpdateMenu(self, event):
514 """Update menu items based on current status and context."""
02b800ce 515 win = wx.Window.FindFocus()
d14a1e28
RD
516 id = event.GetId()
517 event.Enable(True)
518 try:
519 if id == ID_NEW:
520 event.Enable(hasattr(self, 'bufferNew'))
521 elif id == ID_OPEN:
522 event.Enable(hasattr(self, 'bufferOpen'))
523 elif id == ID_REVERT:
524 event.Enable(hasattr(self, 'bufferRevert')
525 and self.hasBuffer())
526 elif id == ID_CLOSE:
527 event.Enable(hasattr(self, 'bufferClose')
528 and self.hasBuffer())
529 elif id == ID_SAVE:
530 event.Enable(hasattr(self, 'bufferSave')
531 and self.bufferHasChanged())
532 elif id == ID_SAVEAS:
533 event.Enable(hasattr(self, 'bufferSaveAs')
534 and self.hasBuffer())
535 elif id == ID_NAMESPACE:
536 event.Enable(hasattr(self, 'updateNamespace')
537 and self.hasBuffer())
538 elif id == ID_PRINT:
539 event.Enable(hasattr(self, 'bufferPrint')
540 and self.hasBuffer())
541 elif id == ID_UNDO:
542 event.Enable(win.CanUndo())
543 elif id == ID_REDO:
544 event.Enable(win.CanRedo())
545 elif id == ID_CUT:
546 event.Enable(win.CanCut())
547 elif id == ID_COPY:
548 event.Enable(win.CanCopy())
549 elif id == ID_COPY_PLUS:
550 event.Enable(win.CanCopy() and hasattr(win, 'CopyWithPrompts'))
551 elif id == ID_PASTE:
552 event.Enable(win.CanPaste())
553 elif id == ID_PASTE_PLUS:
554 event.Enable(win.CanPaste() and hasattr(win, 'PasteAndRun'))
555 elif id == ID_CLEAR:
556 event.Enable(win.CanCut())
557 elif id == ID_SELECTALL:
558 event.Enable(hasattr(win, 'SelectAll'))
02b800ce
RD
559 elif id == ID_EMPTYBUFFER:
560 event.Enable(hasattr(win, 'ClearAll') and not win.GetReadOnly())
d14a1e28
RD
561 elif id == ID_AUTOCOMP_SHOW:
562 event.Check(win.autoComplete)
563 elif id == ID_AUTOCOMP_MAGIC:
564 event.Check(win.autoCompleteIncludeMagic)
565 elif id == ID_AUTOCOMP_SINGLE:
566 event.Check(win.autoCompleteIncludeSingle)
567 elif id == ID_AUTOCOMP_DOUBLE:
568 event.Check(win.autoCompleteIncludeDouble)
569 elif id == ID_CALLTIPS_SHOW:
570 event.Check(win.autoCallTip)
02b800ce
RD
571 elif id == ID_CALLTIPS_INSERT:
572 event.Check(win.callTipInsert)
d14a1e28
RD
573 elif id == ID_WRAP:
574 event.Check(win.GetWrapMode())
9513c5b6
RD
575 elif id == ID_USEAA:
576 event.Check(win.GetUseAntiAliasing())
02b800ce
RD
577
578 elif id == ID_SHOW_LINENUMBERS:
579 event.Check(win.lineNumbers)
580 elif id == ID_AUTO_SAVESETTINGS:
581 event.Check(self.autoSaveSettings)
095315e2 582 event.Enable(self.config is not None)
02b800ce
RD
583 elif id == ID_SAVESETTINGS:
584 event.Enable(self.config is not None and
585 hasattr(self, 'DoSaveSettings'))
586 elif id == ID_DELSETTINGSFILE:
587 event.Enable(self.config is not None)
588
589 elif id == ID_EXECSTARTUPSCRIPT:
590 event.Check(self.execStartupScript)
095315e2 591 event.Enable(self.config is not None)
02b800ce
RD
592
593 elif id == ID_SAVEHISTORY:
e773f79b 594 event.Check(self.autoSaveHistory)
095315e2 595 event.Enable(self.dataDir is not None)
e773f79b
RD
596 elif id == ID_SAVEHISTORYNOW:
597 event.Enable(self.dataDir is not None and
598 hasattr(self, 'SaveHistory'))
599 elif id == ID_CLEARHISTORY:
600 event.Enable(self.dataDir is not None)
601
02b800ce
RD
602 elif id == ID_EDITSTARTUPSCRIPT:
603 event.Enable(hasattr(self, 'EditStartupScript'))
095315e2
RD
604 event.Enable(self.dataDir is not None)
605
02b800ce
RD
606 elif id == ID_FIND:
607 event.Enable(hasattr(win, 'DoFindNext'))
608 elif id == ID_FINDNEXT:
e773f79b
RD
609 event.Enable(hasattr(win, 'DoFindNext'))
610
d14a1e28
RD
611 else:
612 event.Enable(False)
613 except AttributeError:
614 # This menu option is not supported in the current context.
615 event.Enable(False)
02b800ce
RD
616
617
618 def OnActivate(self, event):
619 """
620 Event Handler for losing the focus of the Frame. Should close
621 Autocomplete listbox, if shown.
622 """
623 if not event.GetActive():
624 # If autocomplete active, cancel it. Otherwise, the
625 # autocomplete list will stay visible on top of the
626 # z-order after switching to another application
627 win = wx.Window.FindFocus()
628 if hasattr(win, 'AutoCompActive') and win.AutoCompActive():
629 win.AutoCompCancel()
630 event.Skip()
631
632
633
634 def LoadSettings(self, config):
635 """Called be derived classes to load settings specific to the Frame"""
636 pos = wx.Point(config.ReadInt('Window/PosX', -1),
637 config.ReadInt('Window/PosY', -1))
638
639 size = wx.Size(config.ReadInt('Window/Width', -1),
640 config.ReadInt('Window/Height', -1))
641
642 self.SetSize(size)
643 self.Move(pos)
644
645
646 def SaveSettings(self, config):
647 """Called by derived classes to save Frame settings to a wx.Config object"""
648
649 # TODO: track position/size so we can save it even if the
650 # frame is maximized or iconized.
651 if not self.iconized and not self.IsMaximized():
652 w, h = self.GetSize()
653 config.WriteInt('Window/Width', w)
654 config.WriteInt('Window/Height', h)
655
656 px, py = self.GetPosition()
657 config.WriteInt('Window/PosX', px)
658 config.WriteInt('Window/PosY', py)
659
660
661
662
663class ShellFrameMixin:
664 """
665 A mix-in class for frames that will have a Shell or a Crust window
666 and that want to add history, startupScript and other common
667 functionality.
668 """
669 def __init__(self, config, dataDir):
670 self.config = config
671 self.dataDir = dataDir
672 self.startupScript = os.environ.get('PYTHONSTARTUP')
673 if not self.startupScript and self.dataDir:
674 self.startupScript = os.path.join(self.dataDir, 'startup')
675
676 self.autoSaveSettings = False
e773f79b 677 self.autoSaveHistory = False
02b800ce
RD
678
679 # We need this one before we have a chance to load the settings...
680 self.execStartupScript = True
681 if self.config:
682 self.execStartupScript = self.config.ReadBool('Options/ExecStartupScript', True)
683
684
685 def OnHelp(self, event):
686 """Display a Help window."""
687 import wx.lib.dialogs
688 title = 'Help on key bindings'
689
690 text = wx.py.shell.HELP_TEXT
691
692 dlg = wx.lib.dialogs.ScrolledMessageDialog(self, text, title, size = ((700, 540)))
693 fnt = wx.Font(10, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
694 dlg.GetChildren()[0].SetFont(fnt)
695 dlg.GetChildren()[0].SetInsertionPoint(0)
696 dlg.ShowModal()
697 dlg.Destroy()
698
699
700 def LoadSettings(self):
701 if self.config is not None:
702 self.autoSaveSettings = self.config.ReadBool('Options/AutoSaveSettings', False)
703 self.execStartupScript = self.config.ReadBool('Options/ExecStartupScript', True)
e773f79b 704 self.autoSaveHistory = self.config.ReadBool('Options/AutoSaveHistory', False)
02b800ce
RD
705 self.LoadHistory()
706
707
708 def SaveSettings(self):
709 if self.config is not None:
710 # always save this one
711 self.config.WriteBool('Options/AutoSaveSettings', self.autoSaveSettings)
712 if self.autoSaveSettings:
e773f79b 713 self.config.WriteBool('Options/AutoSaveHistory', self.autoSaveHistory)
02b800ce 714 self.config.WriteBool('Options/ExecStartupScript', self.execStartupScript)
e773f79b 715 if self.autoSaveHistory:
02b800ce
RD
716 self.SaveHistory()
717
718
719
720 def SaveHistory(self):
721 if self.dataDir:
722 try:
02b800ce
RD
723 name = os.path.join(self.dataDir, 'history')
724 f = file(name, 'w')
e773f79b
RD
725 hist = '\x00\n'.join(self.shell.history)
726 f.write(hist)
02b800ce
RD
727 f.close()
728 except:
729 d = wx.MessageDialog(self, "Error saving history file.",
730 "Error", wx.ICON_EXCLAMATION)
731 d.ShowModal()
732 d.Destroy()
e773f79b 733 raise
02b800ce
RD
734
735 def LoadHistory(self):
736 if self.dataDir:
737 name = os.path.join(self.dataDir, 'history')
738 if os.path.exists(name):
739 try:
740 f = file(name, 'U')
741 hist = f.read()
742 f.close()
e773f79b
RD
743 self.shell.history = hist.split('\x00\n')
744 dispatcher.send(signal="Shell.loadHistory", history=self.shell.history)
02b800ce
RD
745 except:
746 d = wx.MessageDialog(self, "Error loading history file.",
747 "Error", wx.ICON_EXCLAMATION)
748 d.ShowModal()
749 d.Destroy()
750
751
752 def bufferHasChanged(self):
753 # the shell buffers can always be saved
754 return True
755
756 def bufferSave(self):
757 import time
758 appname = wx.GetApp().GetAppName()
759 default = appname + '-' + time.strftime("%Y%m%d-%H%M.py")
760 fileName = wx.FileSelector("Save File As", "Saving",
761 default_filename=default,
762 default_extension="py",
763 wildcard="*.py",
764 flags = wx.SAVE | wx.OVERWRITE_PROMPT)
765 if not fileName:
766 return
767
768 text = self.shell.GetText()
769
770## This isn't working currently...
771## 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)
772## yes_no = d.ShowModal()
773## if yes_no == wx.ID_YES:
774## m = re.findall('^[>\.]{3,3} (.*)\r', text, re.MULTILINE | re.LOCALE)
775## text = '\n'.join(m)
776## d.Destroy()
777
778 try:
779 f = open(fileName, "w")
780 f.write(text)
781 f.close()
782 except:
783 d = wx.MessageDialog(self, u'Error saving session',u'Error',
784 wx.OK | wx.ICON_ERROR)
785 d.ShowModal()
786 d.Destroy()
787
788
789 def EditStartupScript(self):
790 if os.path.exists(self.startupScript):
791 text = file(self.startupScript, 'U').read()
792 else:
793 text = ''
794
795 dlg = EditStartupScriptDialog(self, self.startupScript, text)
796 if dlg.ShowModal() == wx.ID_OK:
797 text = dlg.GetText()
798 try:
799 f = file(self.startupScript, 'w')
800 f.write(text)
801 f.close()
802 except:
803 d = wx.MessageDialog(self, "Error saving startup file.",
804 "Error", wx.ICON_EXCLAMATION)
805 d.ShowModal()
806 d.Destroy()
807
808
809
810class EditStartupScriptDialog(wx.Dialog):
811 def __init__(self, parent, fileName, text):
812 wx.Dialog.__init__(self, parent, size=(425,350),
813 title="Edit Startup Script",
814 style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
815
816 pst = wx.StaticText(self, -1, "Path:")
817 ptx = wx.TextCtrl(self, -1, fileName, style=wx.TE_READONLY)
818 self.editor = editwindow.EditWindow(self)
819 self.editor.SetText(text)
820 wx.CallAfter(self.editor.SetFocus)
821
822 ok = wx.Button(self, wx.ID_OK)
823 cancel = wx.Button(self, wx.ID_CANCEL)
824
825 mainSizer = wx.BoxSizer(wx.VERTICAL)
826
827 pthSizer = wx.BoxSizer(wx.HORIZONTAL)
828 pthSizer.Add(pst, flag=wx.ALIGN_CENTER_VERTICAL)
829 pthSizer.Add((5,5))
830 pthSizer.Add(ptx, 1)
831 mainSizer.Add(pthSizer, 0, wx.EXPAND|wx.ALL, 10)
832
833 mainSizer.Add(self.editor, 1, wx.EXPAND|wx.LEFT|wx.RIGHT, 10)
834
835 btnSizer = wx.BoxSizer(wx.HORIZONTAL)
836 btnSizer.Add((5,5), 1)
837 btnSizer.Add(ok)
838 btnSizer.Add((5,5), 1)
839 btnSizer.Add(cancel)
840 btnSizer.Add((5,5), 1)
841 mainSizer.Add(btnSizer, 0, wx.EXPAND|wx.ALL, 10)
842
843 self.SetSizer(mainSizer)
844 self.Layout()
845
846
847 def GetText(self):
848 return self.editor.GetText()