blob: 909856e359096cee18a384c01422f9389e27ff20 [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
Tim Peters231c3c82006-06-11 19:43:49 +00007
Ronald Oussoren92919a62009-12-24 13:30:58 +00008
9_appbundle = None
10
Tim Peters231c3c82006-06-11 19:43:49 +000011def runningAsOSXApp():
Ronald Oussorena97063a2009-03-04 21:35:05 +000012 """
13 Returns True if Python is running from within an app on OSX.
14 If so, assume that Python was built with Aqua Tcl/Tk rather than
Guilherme Polo175e0bf2009-08-05 23:48:26 +000015 X11 Tcl/Tk.
Ronald Oussorena97063a2009-03-04 21:35:05 +000016 """
Ronald Oussoren92919a62009-12-24 13:30:58 +000017 global _appbundle
18 if _appbundle is None:
19 _appbundle = (sys.platform == 'darwin' and '.app' in sys.executable)
20 return _appbundle
Tim Peters231c3c82006-06-11 19:43:49 +000021
22def addOpenEventSupport(root, flist):
23 """
24 This ensures that the application will respont to open AppleEvents, which
25 makes is feaseable to use IDLE as the default application for python files.
26 """
27 def doOpenFile(*args):
28 for fn in args:
29 flist.open(fn)
30
31 # The command below is a hook in aquatk that is called whenever the app
32 # receives a file open event. The callback can have multiple arguments,
33 # one for every file that should be opened.
34 root.createcommand("::tk::mac::OpenDocument", doOpenFile)
35
36def hideTkConsole(root):
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +000037 try:
38 root.tk.call('console', 'hide')
Georg Brandl6634bf22008-05-20 07:13:37 +000039 except Tkinter.TclError:
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +000040 # Some versions of the Tk framework don't have a console object
41 pass
Tim Peters231c3c82006-06-11 19:43:49 +000042
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000043def overrideRootMenu(root, flist):
44 """
45 Replace the Tk root menu by something that's more appropriate for
46 IDLE.
47 """
Tim Peters0bbfd832006-07-24 21:02:15 +000048 # The menu that is attached to the Tk root (".") is also used by AquaTk for
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000049 # all windows that don't specify a menu of their own. The default menubar
50 # contains a number of menus, none of which are appropriate for IDLE. The
51 # Most annoying of those is an 'About Tck/Tk...' menu in the application
52 # menu.
53 #
54 # This function replaces the default menubar by a mostly empty one, it
55 # should only contain the correct application menu and the window menu.
56 #
57 # Due to a (mis-)feature of TkAqua the user will also see an empty Help
58 # menu.
Georg Brandl6634bf22008-05-20 07:13:37 +000059 from Tkinter import Menu, Text, Text
Florent Xiclunad630c042010-04-02 07:24:52 +000060 from idlelib.EditorWindow import prepstr, get_accelerator
61 from idlelib import Bindings
62 from idlelib import WindowList
63 from idlelib.MultiCall import MultiCallCreator
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000064
65 menubar = Menu(root)
66 root.configure(menu=menubar)
67 menudict = {}
68
69 menudict['windows'] = menu = Menu(menubar, name='windows')
70 menubar.add_cascade(label='Window', menu=menu, underline=0)
71
72 def postwindowsmenu(menu=menu):
73 end = menu.index('end')
74 if end is None:
75 end = -1
76
77 if end > 0:
78 menu.delete(0, end)
79 WindowList.add_windows_to_menu(menu)
80 WindowList.register_callback(postwindowsmenu)
81
82 menudict['application'] = menu = Menu(menubar, name='apple')
83 menubar.add_cascade(label='IDLE', menu=menu)
84
85 def about_dialog(event=None):
Florent Xiclunad630c042010-04-02 07:24:52 +000086 from idlelib import aboutDialog
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000087 aboutDialog.AboutDialog(root, 'About IDLE')
88
89 def config_dialog(event=None):
Florent Xiclunad630c042010-04-02 07:24:52 +000090 from idlelib import configDialog
Ronald Oussoren55d88282009-05-26 18:44:48 +000091 root.instance_dict = flist.inversedict
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000092 configDialog.ConfigDialog(root, 'Settings')
93
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +000094
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000095 root.bind('<<about-idle>>', about_dialog)
96 root.bind('<<open-config-dialog>>', config_dialog)
97 if flist:
98 root.bind('<<close-all-windows>>', flist.close_all_callback)
99
Ronald Oussoren8ec374c2010-12-07 16:02:59 +0000100 # The binding above doesn't reliably work on all versions of Tk
101 # on MacOSX. Adding command definition below does seem to do the
102 # right thing for now.
103 root.createcommand('exit', flist.close_all_callback)
104
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +0000105
106 ###check if Tk version >= 8.4.14; if so, use hard-coded showprefs binding
107 tkversion = root.tk.eval('info patchlevel')
Ronald Oussoren8c954842009-01-02 12:59:32 +0000108 # Note: we cannot check if the string tkversion >= '8.4.14', because
109 # the string '8.4.7' is greater than the string '8.4.14'.
Ronald Oussoren55d88282009-05-26 18:44:48 +0000110 if tuple(map(int, tkversion.split('.'))) >= (8, 4, 14):
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +0000111 Bindings.menudefs[0] = ('application', [
112 ('About IDLE', '<<about-idle>>'),
113 None,
114 ])
115 root.createcommand('::tk::mac::ShowPreferences', config_dialog)
116 else:
117 for mname, entrylist in Bindings.menudefs:
118 menu = menudict.get(mname)
119 if not menu:
120 continue
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000121 else:
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +0000122 for entry in entrylist:
123 if not entry:
124 menu.add_separator()
125 else:
126 label, eventname = entry
127 underline, label = prepstr(label)
128 accelerator = get_accelerator(Bindings.default_keydefs,
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000129 eventname)
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +0000130 def command(text=root, eventname=eventname):
131 text.event_generate(eventname)
132 menu.add_command(label=label, underline=underline,
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000133 command=command, accelerator=accelerator)
134
Tim Peters231c3c82006-06-11 19:43:49 +0000135def setupApp(root, flist):
136 """
137 Perform setup for the OSX application bundle.
138 """
139 if not runningAsOSXApp(): return
140
141 hideTkConsole(root)
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000142 overrideRootMenu(root, flist)
Tim Peters231c3c82006-06-11 19:43:49 +0000143 addOpenEventSupport(root, flist)