10 import Menu
; MenuToolbox
= Menu
; del Menu
13 class Application(FrameWork
.Application
):
15 def __init__(self
, signature
='Pyth'):
17 W
.setapplication(self
, signature
)
18 FrameWork
.Application
.__init
__(self
)
21 self
.debugger_quitting
= 1
22 self
.DebuggerQuit
= 'DebuggerQuitDummyException'
24 # map certain F key codes to equivalent command-letter combos (JJS)
25 self
.fkeymaps
= {122:"z", 120:"x", 99:"c", 118:"v"}
27 def mainloop(self
, mask
=FrameWork
.everyEvent
, wait
=0):
30 saveyield
= MacOS
.EnableAppswitch(-1)
32 while not self
.quitting
:
34 self
.do1event(mask
, wait
)
35 except W
.AlertError
, detail
:
36 MacOS
.EnableAppswitch(-1)
38 except self
.DebuggerQuit
:
39 MacOS
.EnableAppswitch(-1)
41 MacOS
.EnableAppswitch(-1)
43 PyEdit
.tracebackwindow
.traceback()
45 MacOS
.EnableAppswitch(1)
47 def debugger_mainloop(self
, mask
=FrameWork
.everyEvent
, wait
=0):
49 self
.debugger_quitting
= 0
50 saveyield
= MacOS
.EnableAppswitch(-1)
52 while not self
.quitting
and not self
.debugger_quitting
:
54 self
.do1event(mask
, wait
)
55 except W
.AlertError
, detail
:
59 PyEdit
.tracebackwindow
.traceback()
61 MacOS
.EnableAppswitch(saveyield
)
63 def breathe(self
, wait
=1):
65 ok
, event
= Evt
.WaitNextEvent(FrameWork
.updateMask |
66 FrameWork
.mDownMask | FrameWork
.osMask |
70 (what
, message
, when
, where
, modifiers
) = event
71 #print FrameWork.eventname[what]
72 if FrameWork
.eventname
[what
] == 'mouseDown':
73 partcode
, wid
= Win
.FindWindow(where
)
74 if FrameWork
.partname
[partcode
] <> 'inDesk':
80 def refreshwindows(self
, wait
=1):
83 ok
, event
= Evt
.WaitNextEvent(FrameWork
.updateMask
, wait
)
88 def addidlefunc(self
, func
):
89 self
._idlefuncs
.append(func
)
91 def removeidlefunc(self
, func
):
92 self
._idlefuncs
.remove(func
)
94 def idle(self
, event
):
95 if not self
._suspended
:
96 if not self
.do_frontWindowMethod("idle", event
):
99 for func
in self
._idlefuncs
:
104 sys
.stderr
.write("exception in idle function %s; killed:\n" % `func`
)
105 traceback
.print_exc()
106 self
._idlefuncs
.remove(func
)
109 def do_frontWindowMethod(self
, attr
, *args
):
110 wid
= Win
.FrontWindow()
111 if wid
and self
._windows
.has_key(wid
):
112 window
= self
._windows
[wid
]
113 if hasattr(window
, attr
):
114 handler
= getattr(window
, attr
)
118 def appendwindow(self
, wid
, window
):
119 self
._windows
[wid
] = window
120 self
.makeopenwindowsmenu()
122 def removewindow(self
, wid
):
123 del self
._windows
[wid
]
124 self
.makeopenwindowsmenu()
126 def makeopenwindowsmenu(self
):
127 # dummy; could be the full version from PythonIDEMain.py
128 self
._openwindows
= {}
129 self
._openwindowscheckmark
= 0
130 if not hasattr(self
, "_menustocheck"):
131 self
._menustocheck
= []
133 def do_key(self
, event
):
134 (what
, message
, when
, where
, modifiers
) = event
135 ch
= chr(message
& FrameWork
.charCodeMask
)
136 rest
= message
& ~FrameWork
.charCodeMask
137 keycode
= (message
& FrameWork
.keyCodeMask
) >> 8
138 if keycode
in self
.fkeymaps
.keys(): # JJS
139 ch
= self
.fkeymaps
[keycode
]
140 modifiers
= modifiers | FrameWork
.cmdKey
141 wid
= Win
.FrontWindow()
142 if modifiers
& FrameWork
.cmdKey
and not modifiers
& FrameWork
.shiftKey
:
143 if wid
and self
._windows
.has_key(wid
):
144 self
.checkmenus(self
._windows
[wid
])
146 self
.checkmenus(None)
147 event
= (what
, ord(ch
) | rest
, when
, where
, modifiers
)
148 result
= MenuToolbox
.MenuKey(ord(ch
))
149 id = (result
>>16) & 0xffff # Hi word
150 item
= result
& 0xffff # Lo word
152 self
.do_rawmenu(id, item
, None, event
)
153 return # here! we had a menukey!
155 # print "XXX Command-" +`ch`
156 # See whether the front window wants it
157 if wid
and self
._windows
.has_key(wid
):
158 window
= self
._windows
[wid
]
160 do_char
= window
.do_char
161 except AttributeError:
162 do_char
= self
.do_char
164 # else it wasn't for us, sigh...
166 def do_inMenuBar(self
, partcode
, window
, event
):
168 (what
, message
, when
, where
, modifiers
) = event
169 self
.checkopenwindowsmenu()
170 wid
= Win
.FrontWindow()
171 if wid
and self
._windows
.has_key(wid
):
172 self
.checkmenus(self
._windows
[wid
])
174 self
.checkmenus(None)
175 result
= MenuToolbox
.MenuSelect(where
)
176 id = (result
>>16) & 0xffff # Hi word
177 item
= result
& 0xffff # Lo word
178 self
.do_rawmenu(id, item
, window
, event
)
180 def do_updateEvt(self
, event
):
181 (what
, message
, when
, where
, modifiers
) = event
182 wid
= Win
.WhichWindow(message
)
183 if wid
and self
._windows
.has_key(wid
):
184 window
= self
._windows
[wid
]
185 window
.do_rawupdate(wid
, event
)
190 sys
.stderr
.write("XXX killed unknown (crashed?) Python window.\n")
192 MacOS
.HandleEvent(event
)
194 def suspendresume(self
, onoff
):
197 def do_suspendresume(self
, event
):
198 self
._suspended
= not event
[1] & 1
199 FrameWork
.Application
.do_suspendresume(self
, event
)
201 def checkopenwindowsmenu(self
):
202 if self
._openwindowscheckmark
:
203 self
.openwindowsmenu
.menu
.CheckItem(self
._openwindowscheckmark
, 0)
204 window
= Win
.FrontWindow()
206 for item
, wid
in self
._openwindows
.items():
208 #self.pythonwindowsmenuitem.check(1)
209 self
.openwindowsmenu
.menu
.CheckItem(item
, 1)
210 self
._openwindowscheckmark
= item
213 self
._openwindowscheckmark
= 0
214 #if self._openwindows:
215 # self.pythonwindowsmenuitem.enable(1)
217 # self.pythonwindowsmenuitem.enable(0)
219 def checkmenus(self
, window
):
220 for item
in self
._menustocheck
:
221 callback
= item
.menu
.items
[item
.item
-1][2]
222 if type(callback
) <> StringType
:
224 elif hasattr(window
, "domenu_" + callback
):
225 if hasattr(window
, "can_" + callback
):
226 canhandler
= getattr(window
, "can_" + callback
)
236 def enablemenubar(self
, onoff
):
237 for m
in self
.menubar
.menus
.values():
240 elif m
.menu
.GetMenuItemText(3) <> 'Cut': # ew...
241 m
.menu
.DisableItem(0)
242 MenuToolbox
.DrawMenuBar()
244 def makemenubar(self
):
245 self
.menubar
= MenuBar(self
)
246 FrameWork
.AppleMenu(self
.menubar
, self
.getabouttext(), self
.do_about
)
249 def scriptswalk(self
, top
, menu
, done
=None):
252 if done
.has_key(top
):
255 import os
, macfs
, string
257 names
= os
.listdir(top
)
259 FrameWork
.MenuItem(menu
, '(Scripts Folder not found)', None, None)
261 savedir
= os
.getcwd()
267 fss
, isdir
, isalias
= macfs
.ResolveAliasFile(name
)
269 # maybe a broken alias
271 path
= fss
.as_pathname()
272 if done
.has_key(path
):
274 name
= string
.strip(name
)
275 if name
[-3:] == '---':
278 submenu
= FrameWork
.SubMenu(menu
, name
)
279 self
.scriptswalk(path
, submenu
, done
)
281 creator
, type = fss
.GetCreatorType()
283 if name
[-3:] == '.py':
285 item
= FrameWork
.MenuItem(menu
, name
, None, self
.domenu_script
)
286 self
._scripts
[(menu
.id, item
.item
)] = path
290 def domenu_script(self
, id, item
, window
, event
):
291 (what
, message
, when
, where
, modifiers
) = event
292 path
= self
._scripts
[(id, item
)]
294 if not os
.path
.exists(path
):
295 self
.makescriptsmenu()
297 raise W
.AlertError
, "File not found."
298 if ord(Evt
.GetKeys()[7]) & 4:
299 self
.openscript(path
)
305 #os.chdir(os.path.dirname(path) + ':')
307 # xxx if there is a script window for this file,
308 # exec in that window's namespace.
309 # xxx what to do when it's not saved???
311 MacOS
.EnableAppswitch(0)
312 execfile(path
, {'__name__': '__main__', '__file__': path
})
313 except W
.AlertError
, detail
:
314 MacOS
.EnableAppswitch(-1)
315 raise W
.AlertError
, detail
316 except KeyboardInterrupt:
317 MacOS
.EnableAppswitch(-1)
319 MacOS
.EnableAppswitch(-1)
321 PyEdit
.tracebackwindow
.traceback(1)
323 MacOS
.EnableAppswitch(-1)
326 def openscript(self
, filename
, lineno
=None, charoffset
=0, modname
=""):
328 editor
= self
.getscript(filename
)
331 elif os
.path
.exists(filename
):
332 editor
= PyEdit
.Editor(filename
)
333 elif filename
[-3:] == '.py' or filename
[-4:] == '.pyc':
336 if filename
[-1] == 'c':
337 modname
= os
.path
.basename(filename
)[:-4]
339 modname
= os
.path
.basename(filename
)[:-3]
341 # XXX This does not work correctly with packages!
342 # XXX The docs say we should do it manually, pack, then sub, then sub2 etc.
343 # XXX It says we should use imp.load_module(), but that *reloads* a package,
344 # XXX and that's the last thing we want here.
345 f
, filename
, (suff
, mode
, dummy
) = imp
.find_module(modname
)
347 raise W
.AlertError
, "Can¹t find file for ³%s²" % modname
350 raise W
.AlertError
, "Can¹t find file for ³%s²" % modname
353 self
.openscript(filename
, lineno
, charoffset
)
356 raise W
.AlertError
, "Can¹t find file for ³%s²" % modname
358 raise W
.AlertError
, "Can¹t find file Œ%s¹" % filename
359 if lineno
is not None:
360 editor
.selectline(lineno
, charoffset
)
363 def getscript(self
, filename
):
364 if filename
[:1] == '<' and filename
[-1:] == '>':
365 filename
= filename
[1:-1]
367 lowpath
= string
.lower(filename
)
368 for wid
, window
in self
._windows
.items():
369 if hasattr(window
, "path") and type(window
.path
) == StringType
and \
370 lowpath
== string
.lower(window
.path
):
372 elif hasattr(window
, "path") and filename
== wid
.GetWTitle():
377 return MacPrefs
.GetPrefs(self
.preffilepath
)
379 def do_editorprefs(self
, *args
):
381 PyEdit
.EditorDefaultSettings()
383 def do_setwindowfont(self
, *args
):
384 import FontSettings
, W
385 prefs
= self
.getprefs()
386 settings
= FontSettings
.FontDialog(prefs
.defaultfont
)
388 prefs
.defaultfont
, tabsettings
= settings
389 raise W
.AlertError
, "Note that changes will only affect new windows!"
393 class MenuBar(FrameWork
.MenuBar
):
395 possibleIDs
= range(10, 256)
398 id = self
.possibleIDs
[0]
399 del self
.possibleIDs
[0]
402 def __init__(self
, parent
= None):
403 self
.bar
= MenuToolbox
.GetMenuBar()
404 MenuToolbox
.ClearMenuBar()
408 def dispatch(self
, id, item
, window
, event
):
409 if self
.menus
.has_key(id):
410 self
.menus
[id].dispatch(id, item
, window
, event
)
412 def delmenu(self
, id):
413 MenuToolbox
.DeleteMenu(id)
414 if id in self
.possibleIDs
:
415 print "XXX duplicate menu ID!", id
416 self
.possibleIDs
.append(id)
419 class Menu(FrameWork
.Menu
):
421 def dispatch(self
, id, item
, window
, event
):
422 title
, shortcut
, callback
, kind
= self
.items
[item
-1]
423 if type(callback
) == StringType
:
424 callback
= self
._getmenuhandler
(callback
)
427 W
.CallbackCall(callback
, 0, id, item
, window
, event
)
429 def _getmenuhandler(self
, callback
):
431 wid
= Win
.FrontWindow()
432 if wid
and self
.bar
.parent
._windows
.has_key(wid
):
433 window
= self
.bar
.parent
._windows
[wid
]
434 if hasattr(window
, "domenu_" + callback
):
435 menuhandler
= getattr(window
, "domenu_" + callback
)
436 elif hasattr(self
.bar
.parent
, "domenu_" + callback
):
437 menuhandler
= getattr(self
.bar
.parent
, "domenu_" + callback
)
438 elif hasattr(self
.bar
.parent
, "domenu_" + callback
):
439 menuhandler
= getattr(self
.bar
.parent
, "domenu_" + callback
)