blob: 4438bd2a67268c20a6329505e80898e8e725c707 [file] [log] [blame]
Tim Peters231c3c82006-06-11 19:43:49 +00001"""
2A number of function that enhance IDLE on MacOSX when it used as a normal
3GUI application (as opposed to an X11 application).
4"""
5import sys
Georg Brandl6634bf22008-05-20 07:13:37 +00006import Tkinter
Ned Deily4a705502011-01-18 04:33:22 +00007from os import path
Tim Peters231c3c82006-06-11 19:43:49 +00008
Ronald Oussoren92919a62009-12-24 13:30:58 +00009
10_appbundle = None
11
Tim Peters231c3c82006-06-11 19:43:49 +000012def runningAsOSXApp():
Ronald Oussorena97063a2009-03-04 21:35:05 +000013 """
14 Returns True if Python is running from within an app on OSX.
15 If so, assume that Python was built with Aqua Tcl/Tk rather than
Guilherme Polo175e0bf2009-08-05 23:48:26 +000016 X11 Tcl/Tk.
Ronald Oussorena97063a2009-03-04 21:35:05 +000017 """
Ronald Oussoren92919a62009-12-24 13:30:58 +000018 global _appbundle
19 if _appbundle is None:
20 _appbundle = (sys.platform == 'darwin' and '.app' in sys.executable)
21 return _appbundle
Tim Peters231c3c82006-06-11 19:43:49 +000022
Ned Deily4a705502011-01-18 04:33:22 +000023_carbonaquatk = None
24
25def isCarbonAquaTk(root):
26 """
27 Returns True if IDLE is using a Carbon Aqua Tk (instead of the
28 newer Cocoa Aqua Tk).
29 """
30 global _carbonaquatk
31 if _carbonaquatk is None:
32 _carbonaquatk = (runningAsOSXApp() and
33 'aqua' in root.tk.call('tk', 'windowingsystem') and
34 'AppKit' not in root.tk.call('winfo', 'server', '.'))
35 return _carbonaquatk
36
Tim Peters231c3c82006-06-11 19:43:49 +000037def addOpenEventSupport(root, flist):
38 """
39 This ensures that the application will respont to open AppleEvents, which
40 makes is feaseable to use IDLE as the default application for python files.
41 """
42 def doOpenFile(*args):
43 for fn in args:
44 flist.open(fn)
45
46 # The command below is a hook in aquatk that is called whenever the app
47 # receives a file open event. The callback can have multiple arguments,
48 # one for every file that should be opened.
49 root.createcommand("::tk::mac::OpenDocument", doOpenFile)
50
51def hideTkConsole(root):
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +000052 try:
53 root.tk.call('console', 'hide')
Georg Brandl6634bf22008-05-20 07:13:37 +000054 except Tkinter.TclError:
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +000055 # Some versions of the Tk framework don't have a console object
56 pass
Tim Peters231c3c82006-06-11 19:43:49 +000057
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000058def overrideRootMenu(root, flist):
59 """
60 Replace the Tk root menu by something that's more appropriate for
61 IDLE.
62 """
Tim Peters0bbfd832006-07-24 21:02:15 +000063 # The menu that is attached to the Tk root (".") is also used by AquaTk for
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000064 # all windows that don't specify a menu of their own. The default menubar
65 # contains a number of menus, none of which are appropriate for IDLE. The
66 # Most annoying of those is an 'About Tck/Tk...' menu in the application
67 # menu.
68 #
69 # This function replaces the default menubar by a mostly empty one, it
70 # should only contain the correct application menu and the window menu.
71 #
72 # Due to a (mis-)feature of TkAqua the user will also see an empty Help
73 # menu.
Georg Brandl6634bf22008-05-20 07:13:37 +000074 from Tkinter import Menu, Text, Text
Florent Xiclunad630c042010-04-02 07:24:52 +000075 from idlelib.EditorWindow import prepstr, get_accelerator
76 from idlelib import Bindings
77 from idlelib import WindowList
78 from idlelib.MultiCall import MultiCallCreator
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000079
80 menubar = Menu(root)
81 root.configure(menu=menubar)
82 menudict = {}
83
84 menudict['windows'] = menu = Menu(menubar, name='windows')
85 menubar.add_cascade(label='Window', menu=menu, underline=0)
86
87 def postwindowsmenu(menu=menu):
88 end = menu.index('end')
89 if end is None:
90 end = -1
91
92 if end > 0:
93 menu.delete(0, end)
94 WindowList.add_windows_to_menu(menu)
95 WindowList.register_callback(postwindowsmenu)
96
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000097 def about_dialog(event=None):
Florent Xiclunad630c042010-04-02 07:24:52 +000098 from idlelib import aboutDialog
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000099 aboutDialog.AboutDialog(root, 'About IDLE')
100
101 def config_dialog(event=None):
Florent Xiclunad630c042010-04-02 07:24:52 +0000102 from idlelib import configDialog
Ronald Oussoren55d88282009-05-26 18:44:48 +0000103 root.instance_dict = flist.inversedict
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000104 configDialog.ConfigDialog(root, 'Settings')
105
Ned Deily4a705502011-01-18 04:33:22 +0000106 def help_dialog(event=None):
107 from idlelib import textView
108 fn = path.join(path.abspath(path.dirname(__file__)), 'help.txt')
109 textView.view_file(root, 'Help', fn)
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +0000110
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000111 root.bind('<<about-idle>>', about_dialog)
112 root.bind('<<open-config-dialog>>', config_dialog)
Ned Deily4a705502011-01-18 04:33:22 +0000113 root.createcommand('::tk::mac::ShowPreferences', config_dialog)
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000114 if flist:
115 root.bind('<<close-all-windows>>', flist.close_all_callback)
116
Ronald Oussoren8ec374c2010-12-07 16:02:59 +0000117 # The binding above doesn't reliably work on all versions of Tk
118 # on MacOSX. Adding command definition below does seem to do the
119 # right thing for now.
120 root.createcommand('exit', flist.close_all_callback)
121
Ned Deily4a705502011-01-18 04:33:22 +0000122 if isCarbonAquaTk(root):
123 # for Carbon AquaTk, replace the default Tk apple menu
124 menudict['application'] = menu = Menu(menubar, name='apple')
125 menubar.add_cascade(label='IDLE', menu=menu)
126 Bindings.menudefs.insert(0,
127 ('application', [
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +0000128 ('About IDLE', '<<about-idle>>'),
Ned Deily4a705502011-01-18 04:33:22 +0000129 None,
130 ]))
131 tkversion = root.tk.eval('info patchlevel')
132 if tuple(map(int, tkversion.split('.'))) < (8, 4, 14):
133 # for earlier AquaTk versions, supply a Preferences menu item
134 Bindings.menudefs[0][1].append(
135 ('_Preferences....', '<<open-config-dialog>>'),
136 )
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +0000137 else:
Ned Deily4a705502011-01-18 04:33:22 +0000138 # assume Cocoa AquaTk
139 # replace default About dialog with About IDLE one
140 root.createcommand('tkAboutDialog', about_dialog)
141 # replace default "Help" item in Help menu
142 root.createcommand('::tk::mac::ShowHelp', help_dialog)
143 # remove redundant "IDLE Help" from menu
144 del Bindings.menudefs[-1][1][0]
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000145
Tim Peters231c3c82006-06-11 19:43:49 +0000146def setupApp(root, flist):
147 """
148 Perform setup for the OSX application bundle.
149 """
150 if not runningAsOSXApp(): return
151
152 hideTkConsole(root)
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000153 overrideRootMenu(root, flist)
Tim Peters231c3c82006-06-11 19:43:49 +0000154 addOpenEventSupport(root, flist)