blob: f5e30407152f41bf4b50760f8b156ec5b112f177 [file] [log] [blame]
Thomas Wouters0e3f5912006-08-11 14:57:12 +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 Brandl14fc4272008-05-17 18:39:55 +00006import tkinter
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007
Ronald Oussoren6f6c5622009-12-24 14:03:19 +00008
9_appbundle = None
10
Thomas Wouters0e3f5912006-08-11 14:57:12 +000011def runningAsOSXApp():
Ronald Oussoren827822e2009-02-12 15:01:44 +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
Benjamin Peterson8719ad52009-09-11 22:24:02 +000015 X11 Tcl/Tk.
Ronald Oussoren827822e2009-02-12 15:01:44 +000016 """
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000017 global _appbundle
18 if _appbundle is None:
19 _appbundle = (sys.platform == 'darwin' and '.app' in sys.executable)
20 return _appbundle
Thomas Wouters0e3f5912006-08-11 14:57:12 +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):
Guido van Rossumb5a755e2007-07-18 18:15:48 +000037 try:
38 root.tk.call('console', 'hide')
Georg Brandl14fc4272008-05-17 18:39:55 +000039 except tkinter.TclError:
Guido van Rossumb5a755e2007-07-18 18:15:48 +000040 # Some versions of the Tk framework don't have a console object
41 pass
Thomas Wouters0e3f5912006-08-11 14:57:12 +000042
43def overrideRootMenu(root, flist):
44 """
45 Replace the Tk root menu by something that's more appropriate for
46 IDLE.
47 """
48 # The menu that is attached to the Tk root (".") is also used by AquaTk for
49 # 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 Brandl14fc4272008-05-17 18:39:55 +000059 from tkinter import Menu, Text, Text
Kurt B. Kaiser2d7f6a02007-08-22 23:01:33 +000060 from idlelib.EditorWindow import prepstr, get_accelerator
61 from idlelib import Bindings
62 from idlelib import WindowList
63 from idlelib.MultiCall import MultiCallCreator
Thomas Wouters0e3f5912006-08-11 14:57:12 +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):
Kurt B. Kaiser2d7f6a02007-08-22 23:01:33 +000086 from idlelib import aboutDialog
Thomas Wouters0e3f5912006-08-11 14:57:12 +000087 aboutDialog.AboutDialog(root, 'About IDLE')
88
89 def config_dialog(event=None):
Kurt B. Kaiser2d7f6a02007-08-22 23:01:33 +000090 from idlelib import configDialog
Ronald Oussoren220a9fb2009-05-26 18:41:00 +000091
92 # Ensure that the root object has an instance_dict attribute,
93 # mirrors code in EditorWindow (although that sets the attribute
94 # on an EditorWindow instance that is then passed as the first
95 # argument to ConfigDialog)
96 root.instance_dict = flist.inversedict
Benjamin Petersonfa0d7032009-06-01 22:42:33 +000097 root.instance_dict = flist.inversedict
Thomas Wouters0e3f5912006-08-11 14:57:12 +000098 configDialog.ConfigDialog(root, 'Settings')
99
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000100
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000101 root.bind('<<about-idle>>', about_dialog)
102 root.bind('<<open-config-dialog>>', config_dialog)
103 if flist:
104 root.bind('<<close-all-windows>>', flist.close_all_callback)
105
Ronald Oussoren10e05e12010-12-07 15:28:10 +0000106 # The binding above doesn't reliably work on all versions of Tk
107 # on MacOSX. Adding command definition below does seem to do the
108 # right thing for now.
109 root.createcommand('exit', flist.close_all_callback)
110
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000111
112 ###check if Tk version >= 8.4.14; if so, use hard-coded showprefs binding
113 tkversion = root.tk.eval('info patchlevel')
Ronald Oussorene9f8bf02009-01-02 13:10:34 +0000114 # Note: we cannot check if the string tkversion >= '8.4.14', because
115 # the string '8.4.7' is greater than the string '8.4.14'.
116 if tuple(map(int, tkversion.split('.'))) >= (8, 4, 14):
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000117 Bindings.menudefs[0] = ('application', [
118 ('About IDLE', '<<about-idle>>'),
119 None,
120 ])
121 root.createcommand('::tk::mac::ShowPreferences', config_dialog)
122 else:
123 for mname, entrylist in Bindings.menudefs:
124 menu = menudict.get(mname)
125 if not menu:
126 continue
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000127 else:
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000128 for entry in entrylist:
129 if not entry:
130 menu.add_separator()
131 else:
132 label, eventname = entry
133 underline, label = prepstr(label)
134 accelerator = get_accelerator(Bindings.default_keydefs,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000135 eventname)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000136 def command(text=root, eventname=eventname):
137 text.event_generate(eventname)
138 menu.add_command(label=label, underline=underline,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000139 command=command, accelerator=accelerator)
140
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000141def setupApp(root, flist):
142 """
143 Perform setup for the OSX application bundle.
144 """
145 if not runningAsOSXApp(): return
146
147 hideTkConsole(root)
148 overrideRootMenu(root, flist)
149 addOpenEventSupport(root, flist)