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
|
2011-01-18 00:33:22 -04:00
|
|
|
from os import path
|
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
|
|
|
|
2011-01-18 00:33:22 -04:00
|
|
|
_carbonaquatk = None
|
|
|
|
|
|
|
|
def isCarbonAquaTk(root):
|
|
|
|
"""
|
|
|
|
Returns True if IDLE is using a Carbon Aqua Tk (instead of the
|
|
|
|
newer Cocoa Aqua Tk).
|
|
|
|
"""
|
|
|
|
global _carbonaquatk
|
|
|
|
if _carbonaquatk is None:
|
|
|
|
_carbonaquatk = (runningAsOSXApp() and
|
|
|
|
'aqua' in root.tk.call('tk', 'windowingsystem') and
|
|
|
|
'AppKit' not in root.tk.call('winfo', 'server', '.'))
|
|
|
|
return _carbonaquatk
|
|
|
|
|
2011-01-29 20:18:47 -04:00
|
|
|
def tkVersionWarning(root):
|
|
|
|
"""
|
|
|
|
Returns a string warning message if the Tk version in use appears to
|
|
|
|
be one known to cause problems with IDLE. The Apple Cocoa-based Tk 8.5
|
|
|
|
that was shipped with Mac OS X 10.6.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if (runningAsOSXApp() and
|
|
|
|
('AppKit' in root.tk.call('winfo', 'server', '.')) and
|
|
|
|
(root.tk.call('info', 'patchlevel') == '8.5.7') ):
|
|
|
|
return (r"WARNING: The version of Tcl/Tk (8.5.7) in use may"
|
|
|
|
r" be unstable.\n"
|
|
|
|
r"Visit http://www.python.org/download/mac/tcltk/"
|
|
|
|
r" for current information.")
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
2011-01-18 00:33:22 -04:00
|
|
|
def help_dialog(event=None):
|
|
|
|
from idlelib import textView
|
|
|
|
fn = path.join(path.abspath(path.dirname(__file__)), 'help.txt')
|
|
|
|
textView.view_file(root, 'Help', fn)
|
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)
|
2011-01-18 00:33:22 -04:00
|
|
|
root.createcommand('::tk::mac::ShowPreferences', config_dialog)
|
2006-07-23 06:46:11 -03:00
|
|
|
if flist:
|
|
|
|
root.bind('<<close-all-windows>>', flist.close_all_callback)
|
|
|
|
|
2010-12-07 12:02:59 -04:00
|
|
|
# The binding above doesn't reliably work on all versions of Tk
|
|
|
|
# on MacOSX. Adding command definition below does seem to do the
|
|
|
|
# right thing for now.
|
|
|
|
root.createcommand('exit', flist.close_all_callback)
|
|
|
|
|
2011-01-18 00:33:22 -04:00
|
|
|
if isCarbonAquaTk(root):
|
|
|
|
# for Carbon AquaTk, replace the default Tk apple menu
|
|
|
|
menudict['application'] = menu = Menu(menubar, name='apple')
|
|
|
|
menubar.add_cascade(label='IDLE', menu=menu)
|
|
|
|
Bindings.menudefs.insert(0,
|
|
|
|
('application', [
|
2007-07-09 03:02:21 -03:00
|
|
|
('About IDLE', '<<about-idle>>'),
|
2011-01-18 00:33:22 -04:00
|
|
|
None,
|
|
|
|
]))
|
|
|
|
tkversion = root.tk.eval('info patchlevel')
|
|
|
|
if tuple(map(int, tkversion.split('.'))) < (8, 4, 14):
|
|
|
|
# for earlier AquaTk versions, supply a Preferences menu item
|
|
|
|
Bindings.menudefs[0][1].append(
|
|
|
|
('_Preferences....', '<<open-config-dialog>>'),
|
|
|
|
)
|
2007-07-09 03:02:21 -03:00
|
|
|
else:
|
2011-01-18 00:33:22 -04:00
|
|
|
# assume Cocoa AquaTk
|
|
|
|
# replace default About dialog with About IDLE one
|
|
|
|
root.createcommand('tkAboutDialog', about_dialog)
|
|
|
|
# replace default "Help" item in Help menu
|
|
|
|
root.createcommand('::tk::mac::ShowHelp', help_dialog)
|
|
|
|
# remove redundant "IDLE Help" from menu
|
|
|
|
del Bindings.menudefs[-1][1][0]
|
2006-07-23 06:46:11 -03:00
|
|
|
|
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)
|