2006-06-11 16:43:49 -03:00
|
|
|
"""
|
|
|
|
A number of function that enhance IDLE on MacOSX when it used as a normal
|
|
|
|
GUI application (as opposed to an X11 application).
|
|
|
|
"""
|
|
|
|
import sys
|
2008-05-20 04:13:37 -03:00
|
|
|
import Tkinter
|
2006-06-11 16:43:49 -03:00
|
|
|
|
2009-12-24 09:30:58 -04:00
|
|
|
|
|
|
|
_appbundle = None
|
|
|
|
|
2006-06-11 16:43:49 -03:00
|
|
|
def runningAsOSXApp():
|
2009-03-04 17:35:05 -04:00
|
|
|
"""
|
|
|
|
Returns True if Python is running from within an app on OSX.
|
|
|
|
If so, assume that Python was built with Aqua Tcl/Tk rather than
|
2009-08-05 20:48:26 -03:00
|
|
|
X11 Tcl/Tk.
|
2009-03-04 17:35:05 -04:00
|
|
|
"""
|
2009-12-24 09:30:58 -04:00
|
|
|
global _appbundle
|
|
|
|
if _appbundle is None:
|
|
|
|
_appbundle = (sys.platform == 'darwin' and '.app' in sys.executable)
|
|
|
|
return _appbundle
|
2006-06-11 16:43:49 -03:00
|
|
|
|
|
|
|
def addOpenEventSupport(root, flist):
|
|
|
|
"""
|
|
|
|
This ensures that the application will respont to open AppleEvents, which
|
|
|
|
makes is feaseable to use IDLE as the default application for python files.
|
|
|
|
"""
|
|
|
|
def doOpenFile(*args):
|
|
|
|
for fn in args:
|
|
|
|
flist.open(fn)
|
|
|
|
|
|
|
|
# The command below is a hook in aquatk that is called whenever the app
|
|
|
|
# receives a file open event. The callback can have multiple arguments,
|
|
|
|
# one for every file that should be opened.
|
|
|
|
root.createcommand("::tk::mac::OpenDocument", doOpenFile)
|
|
|
|
|
|
|
|
def hideTkConsole(root):
|
2007-07-09 03:02:21 -03:00
|
|
|
try:
|
|
|
|
root.tk.call('console', 'hide')
|
2008-05-20 04:13:37 -03:00
|
|
|
except Tkinter.TclError:
|
2007-07-09 03:02:21 -03:00
|
|
|
# Some versions of the Tk framework don't have a console object
|
|
|
|
pass
|
2006-06-11 16:43:49 -03:00
|
|
|
|
2006-07-23 06:46:11 -03:00
|
|
|
def overrideRootMenu(root, flist):
|
|
|
|
"""
|
|
|
|
Replace the Tk root menu by something that's more appropriate for
|
|
|
|
IDLE.
|
|
|
|
"""
|
2006-07-24 18:02:15 -03:00
|
|
|
# The menu that is attached to the Tk root (".") is also used by AquaTk for
|
2006-07-23 06:46:11 -03:00
|
|
|
# all windows that don't specify a menu of their own. The default menubar
|
|
|
|
# contains a number of menus, none of which are appropriate for IDLE. The
|
|
|
|
# Most annoying of those is an 'About Tck/Tk...' menu in the application
|
|
|
|
# menu.
|
|
|
|
#
|
|
|
|
# This function replaces the default menubar by a mostly empty one, it
|
|
|
|
# should only contain the correct application menu and the window menu.
|
|
|
|
#
|
|
|
|
# Due to a (mis-)feature of TkAqua the user will also see an empty Help
|
|
|
|
# menu.
|
2008-05-20 04:13:37 -03:00
|
|
|
from Tkinter import Menu, Text, Text
|
2010-04-02 04:24:52 -03:00
|
|
|
from idlelib.EditorWindow import prepstr, get_accelerator
|
|
|
|
from idlelib import Bindings
|
|
|
|
from idlelib import WindowList
|
|
|
|
from idlelib.MultiCall import MultiCallCreator
|
2006-07-23 06:46:11 -03:00
|
|
|
|
|
|
|
menubar = Menu(root)
|
|
|
|
root.configure(menu=menubar)
|
|
|
|
menudict = {}
|
|
|
|
|
|
|
|
menudict['windows'] = menu = Menu(menubar, name='windows')
|
|
|
|
menubar.add_cascade(label='Window', menu=menu, underline=0)
|
|
|
|
|
|
|
|
def postwindowsmenu(menu=menu):
|
|
|
|
end = menu.index('end')
|
|
|
|
if end is None:
|
|
|
|
end = -1
|
|
|
|
|
|
|
|
if end > 0:
|
|
|
|
menu.delete(0, end)
|
|
|
|
WindowList.add_windows_to_menu(menu)
|
|
|
|
WindowList.register_callback(postwindowsmenu)
|
|
|
|
|
|
|
|
menudict['application'] = menu = Menu(menubar, name='apple')
|
|
|
|
menubar.add_cascade(label='IDLE', menu=menu)
|
|
|
|
|
|
|
|
def about_dialog(event=None):
|
2010-04-02 04:24:52 -03:00
|
|
|
from idlelib import aboutDialog
|
2006-07-23 06:46:11 -03:00
|
|
|
aboutDialog.AboutDialog(root, 'About IDLE')
|
|
|
|
|
|
|
|
def config_dialog(event=None):
|
2010-04-02 04:24:52 -03:00
|
|
|
from idlelib import configDialog
|
2009-05-26 15:44:48 -03:00
|
|
|
root.instance_dict = flist.inversedict
|
2006-07-23 06:46:11 -03:00
|
|
|
configDialog.ConfigDialog(root, 'Settings')
|
|
|
|
|
2007-07-09 03:02:21 -03:00
|
|
|
|
2006-07-23 06:46:11 -03:00
|
|
|
root.bind('<<about-idle>>', about_dialog)
|
|
|
|
root.bind('<<open-config-dialog>>', config_dialog)
|
|
|
|
if flist:
|
|
|
|
root.bind('<<close-all-windows>>', flist.close_all_callback)
|
|
|
|
|
2007-07-09 03:02:21 -03:00
|
|
|
|
|
|
|
###check if Tk version >= 8.4.14; if so, use hard-coded showprefs binding
|
|
|
|
tkversion = root.tk.eval('info patchlevel')
|
2009-01-02 08:59:32 -04:00
|
|
|
# Note: we cannot check if the string tkversion >= '8.4.14', because
|
|
|
|
# the string '8.4.7' is greater than the string '8.4.14'.
|
2009-05-26 15:44:48 -03:00
|
|
|
if tuple(map(int, tkversion.split('.'))) >= (8, 4, 14):
|
2007-07-09 03:02:21 -03:00
|
|
|
Bindings.menudefs[0] = ('application', [
|
|
|
|
('About IDLE', '<<about-idle>>'),
|
|
|
|
None,
|
|
|
|
])
|
|
|
|
root.createcommand('::tk::mac::ShowPreferences', config_dialog)
|
|
|
|
else:
|
|
|
|
for mname, entrylist in Bindings.menudefs:
|
|
|
|
menu = menudict.get(mname)
|
|
|
|
if not menu:
|
|
|
|
continue
|
2006-07-23 06:46:11 -03:00
|
|
|
else:
|
2007-07-09 03:02:21 -03:00
|
|
|
for entry in entrylist:
|
|
|
|
if not entry:
|
|
|
|
menu.add_separator()
|
|
|
|
else:
|
|
|
|
label, eventname = entry
|
|
|
|
underline, label = prepstr(label)
|
|
|
|
accelerator = get_accelerator(Bindings.default_keydefs,
|
2006-07-23 06:46:11 -03:00
|
|
|
eventname)
|
2007-07-09 03:02:21 -03:00
|
|
|
def command(text=root, eventname=eventname):
|
|
|
|
text.event_generate(eventname)
|
|
|
|
menu.add_command(label=label, underline=underline,
|
2006-07-23 06:46:11 -03:00
|
|
|
command=command, accelerator=accelerator)
|
|
|
|
|
2006-06-11 16:43:49 -03:00
|
|
|
def setupApp(root, flist):
|
|
|
|
"""
|
|
|
|
Perform setup for the OSX application bundle.
|
|
|
|
"""
|
|
|
|
if not runningAsOSXApp(): return
|
|
|
|
|
|
|
|
hideTkConsole(root)
|
2006-07-23 06:46:11 -03:00
|
|
|
overrideRootMenu(root, flist)
|
2006-06-11 16:43:49 -03:00
|
|
|
addOpenEventSupport(root, flist)
|