blob: da519f7a0fccf87b655123960e5b5dff16c27b2f [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
8def runningAsOSXApp():
Ronald Oussoren827822e2009-02-12 15:01:44 +00009 """
10 Returns True if Python is running from within an app on OSX.
11 If so, assume that Python was built with Aqua Tcl/Tk rather than
Benjamin Petersona8332062009-09-11 22:36:27 +000012 X11 Tcl/Tk.
Ronald Oussoren827822e2009-02-12 15:01:44 +000013 """
14 return (sys.platform == 'darwin' and '.app' in sys.executable)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000015
16def addOpenEventSupport(root, flist):
17 """
Ezio Melotti13925002011-03-16 11:05:33 +020018 This ensures that the application will respond to open AppleEvents, which
19 makes is feasible to use IDLE as the default application for python files.
Thomas Wouters0e3f5912006-08-11 14:57:12 +000020 """
21 def doOpenFile(*args):
22 for fn in args:
23 flist.open(fn)
24
25 # The command below is a hook in aquatk that is called whenever the app
26 # receives a file open event. The callback can have multiple arguments,
27 # one for every file that should be opened.
28 root.createcommand("::tk::mac::OpenDocument", doOpenFile)
29
30def hideTkConsole(root):
Guido van Rossumb5a755e2007-07-18 18:15:48 +000031 try:
32 root.tk.call('console', 'hide')
Georg Brandl14fc4272008-05-17 18:39:55 +000033 except tkinter.TclError:
Guido van Rossumb5a755e2007-07-18 18:15:48 +000034 # Some versions of the Tk framework don't have a console object
35 pass
Thomas Wouters0e3f5912006-08-11 14:57:12 +000036
37def overrideRootMenu(root, flist):
38 """
39 Replace the Tk root menu by something that's more appropriate for
40 IDLE.
41 """
42 # The menu that is attached to the Tk root (".") is also used by AquaTk for
43 # all windows that don't specify a menu of their own. The default menubar
44 # contains a number of menus, none of which are appropriate for IDLE. The
45 # Most annoying of those is an 'About Tck/Tk...' menu in the application
46 # menu.
47 #
48 # This function replaces the default menubar by a mostly empty one, it
49 # should only contain the correct application menu and the window menu.
50 #
51 # Due to a (mis-)feature of TkAqua the user will also see an empty Help
52 # menu.
Georg Brandl14fc4272008-05-17 18:39:55 +000053 from tkinter import Menu, Text, Text
Kurt B. Kaiser2d7f6a02007-08-22 23:01:33 +000054 from idlelib.EditorWindow import prepstr, get_accelerator
55 from idlelib import Bindings
56 from idlelib import WindowList
57 from idlelib.MultiCall import MultiCallCreator
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058
59 menubar = Menu(root)
60 root.configure(menu=menubar)
61 menudict = {}
62
63 menudict['windows'] = menu = Menu(menubar, name='windows')
64 menubar.add_cascade(label='Window', menu=menu, underline=0)
65
66 def postwindowsmenu(menu=menu):
67 end = menu.index('end')
68 if end is None:
69 end = -1
70
71 if end > 0:
72 menu.delete(0, end)
73 WindowList.add_windows_to_menu(menu)
74 WindowList.register_callback(postwindowsmenu)
75
76 menudict['application'] = menu = Menu(menubar, name='apple')
77 menubar.add_cascade(label='IDLE', menu=menu)
78
79 def about_dialog(event=None):
Kurt B. Kaiser2d7f6a02007-08-22 23:01:33 +000080 from idlelib import aboutDialog
Thomas Wouters0e3f5912006-08-11 14:57:12 +000081 aboutDialog.AboutDialog(root, 'About IDLE')
82
83 def config_dialog(event=None):
Kurt B. Kaiser2d7f6a02007-08-22 23:01:33 +000084 from idlelib import configDialog
Ronald Oussoren220a9fb2009-05-26 18:41:00 +000085
86 # Ensure that the root object has an instance_dict attribute,
87 # mirrors code in EditorWindow (although that sets the attribute
88 # on an EditorWindow instance that is then passed as the first
89 # argument to ConfigDialog)
90 root.instance_dict = flist.inversedict
Benjamin Petersonfa0d7032009-06-01 22:42:33 +000091 root.instance_dict = flist.inversedict
Thomas Wouters0e3f5912006-08-11 14:57:12 +000092 configDialog.ConfigDialog(root, 'Settings')
93
Guido van Rossumb5a755e2007-07-18 18:15:48 +000094
Thomas Wouters0e3f5912006-08-11 14:57:12 +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 Oussorena85b6712010-12-07 15:31:07 +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
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000105
106 ###check if Tk version >= 8.4.14; if so, use hard-coded showprefs binding
107 tkversion = root.tk.eval('info patchlevel')
Ronald Oussorene9f8bf02009-01-02 13:10:34 +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'.
110 if tuple(map(int, tkversion.split('.'))) >= (8, 4, 14):
Guido van Rossumb5a755e2007-07-18 18:15:48 +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
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000121 else:
Guido van Rossumb5a755e2007-07-18 18:15:48 +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,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000129 eventname)
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000130 def command(text=root, eventname=eventname):
131 text.event_generate(eventname)
132 menu.add_command(label=label, underline=underline,
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000133 command=command, accelerator=accelerator)
134
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000135def setupApp(root, flist):
136 """
137 Perform setup for the OSX application bundle.
138 """
139 if not runningAsOSXApp(): return
140
141 hideTkConsole(root)
142 overrideRootMenu(root, flist)
143 addOpenEventSupport(root, flist)