1 | n/a | """ |
---|
2 | n/a | A number of function that enhance IDLE on MacOSX when it used as a normal |
---|
3 | n/a | GUI application (as opposed to an X11 application). |
---|
4 | n/a | """ |
---|
5 | n/a | import sys |
---|
6 | n/a | import tkinter |
---|
7 | n/a | from os import path |
---|
8 | n/a | |
---|
9 | n/a | |
---|
10 | n/a | _appbundle = None |
---|
11 | n/a | |
---|
12 | n/a | def runningAsOSXApp(): |
---|
13 | n/a | """ |
---|
14 | n/a | Returns True if Python is running from within an app on OSX. |
---|
15 | n/a | If so, the various OS X customizations will be triggered later (menu |
---|
16 | n/a | fixup, et al). (Originally, this test was supposed to condition |
---|
17 | n/a | behavior on whether IDLE was running under Aqua Tk rather than |
---|
18 | n/a | under X11 Tk but that does not work since a framework build |
---|
19 | n/a | could be linked with X11. For several releases, this test actually |
---|
20 | n/a | differentiates between whether IDLE is running from a framework or |
---|
21 | n/a | not. As a future enhancement, it should be considered whether there |
---|
22 | n/a | should be a difference based on framework and any needed X11 adaptions |
---|
23 | n/a | should be made dependent on a new function that actually tests for X11.) |
---|
24 | n/a | """ |
---|
25 | n/a | global _appbundle |
---|
26 | n/a | if _appbundle is None: |
---|
27 | n/a | _appbundle = sys.platform == 'darwin' |
---|
28 | n/a | if _appbundle: |
---|
29 | n/a | import sysconfig |
---|
30 | n/a | _appbundle = bool(sysconfig.get_config_var('PYTHONFRAMEWORK')) |
---|
31 | n/a | return _appbundle |
---|
32 | n/a | |
---|
33 | n/a | _carbonaquatk = None |
---|
34 | n/a | |
---|
35 | n/a | def isCarbonAquaTk(root): |
---|
36 | n/a | """ |
---|
37 | n/a | Returns True if IDLE is using a Carbon Aqua Tk (instead of the |
---|
38 | n/a | newer Cocoa Aqua Tk). |
---|
39 | n/a | """ |
---|
40 | n/a | global _carbonaquatk |
---|
41 | n/a | if _carbonaquatk is None: |
---|
42 | n/a | _carbonaquatk = (runningAsOSXApp() and |
---|
43 | n/a | 'aqua' in root.tk.call('tk', 'windowingsystem') and |
---|
44 | n/a | 'AppKit' not in root.tk.call('winfo', 'server', '.')) |
---|
45 | n/a | return _carbonaquatk |
---|
46 | n/a | |
---|
47 | n/a | def tkVersionWarning(root): |
---|
48 | n/a | """ |
---|
49 | n/a | Returns a string warning message if the Tk version in use appears to |
---|
50 | n/a | be one known to cause problems with IDLE. |
---|
51 | n/a | 1. Apple Cocoa-based Tk 8.5.7 shipped with Mac OS X 10.6 is unusable. |
---|
52 | n/a | 2. Apple Cocoa-based Tk 8.5.9 in OS X 10.7 and 10.8 is better but |
---|
53 | n/a | can still crash unexpectedly. |
---|
54 | n/a | """ |
---|
55 | n/a | |
---|
56 | n/a | if (runningAsOSXApp() and |
---|
57 | n/a | ('AppKit' in root.tk.call('winfo', 'server', '.')) ): |
---|
58 | n/a | patchlevel = root.tk.call('info', 'patchlevel') |
---|
59 | n/a | if patchlevel not in ('8.5.7', '8.5.9'): |
---|
60 | n/a | return False |
---|
61 | n/a | return (r"WARNING: The version of Tcl/Tk ({0}) in use may" |
---|
62 | n/a | r" be unstable.\n" |
---|
63 | n/a | r"Visit http://www.python.org/download/mac/tcltk/" |
---|
64 | n/a | r" for current information.".format(patchlevel)) |
---|
65 | n/a | else: |
---|
66 | n/a | return False |
---|
67 | n/a | |
---|
68 | n/a | def addOpenEventSupport(root, flist): |
---|
69 | n/a | """ |
---|
70 | n/a | This ensures that the application will respond to open AppleEvents, which |
---|
71 | n/a | makes is feasible to use IDLE as the default application for python files. |
---|
72 | n/a | """ |
---|
73 | n/a | def doOpenFile(*args): |
---|
74 | n/a | for fn in args: |
---|
75 | n/a | flist.open(fn) |
---|
76 | n/a | |
---|
77 | n/a | # The command below is a hook in aquatk that is called whenever the app |
---|
78 | n/a | # receives a file open event. The callback can have multiple arguments, |
---|
79 | n/a | # one for every file that should be opened. |
---|
80 | n/a | root.createcommand("::tk::mac::OpenDocument", doOpenFile) |
---|
81 | n/a | |
---|
82 | n/a | def hideTkConsole(root): |
---|
83 | n/a | try: |
---|
84 | n/a | root.tk.call('console', 'hide') |
---|
85 | n/a | except tkinter.TclError: |
---|
86 | n/a | # Some versions of the Tk framework don't have a console object |
---|
87 | n/a | pass |
---|
88 | n/a | |
---|
89 | n/a | def overrideRootMenu(root, flist): |
---|
90 | n/a | """ |
---|
91 | n/a | Replace the Tk root menu by something that's more appropriate for |
---|
92 | n/a | IDLE. |
---|
93 | n/a | """ |
---|
94 | n/a | # The menu that is attached to the Tk root (".") is also used by AquaTk for |
---|
95 | n/a | # all windows that don't specify a menu of their own. The default menubar |
---|
96 | n/a | # contains a number of menus, none of which are appropriate for IDLE. The |
---|
97 | n/a | # Most annoying of those is an 'About Tck/Tk...' menu in the application |
---|
98 | n/a | # menu. |
---|
99 | n/a | # |
---|
100 | n/a | # This function replaces the default menubar by a mostly empty one, it |
---|
101 | n/a | # should only contain the correct application menu and the window menu. |
---|
102 | n/a | # |
---|
103 | n/a | # Due to a (mis-)feature of TkAqua the user will also see an empty Help |
---|
104 | n/a | # menu. |
---|
105 | n/a | from tkinter import Menu, Text, Text |
---|
106 | n/a | from idlelib.EditorWindow import prepstr, get_accelerator |
---|
107 | n/a | from idlelib import Bindings |
---|
108 | n/a | from idlelib import WindowList |
---|
109 | n/a | from idlelib.MultiCall import MultiCallCreator |
---|
110 | n/a | |
---|
111 | n/a | menubar = Menu(root) |
---|
112 | n/a | root.configure(menu=menubar) |
---|
113 | n/a | menudict = {} |
---|
114 | n/a | |
---|
115 | n/a | menudict['windows'] = menu = Menu(menubar, name='windows') |
---|
116 | n/a | menubar.add_cascade(label='Window', menu=menu, underline=0) |
---|
117 | n/a | |
---|
118 | n/a | def postwindowsmenu(menu=menu): |
---|
119 | n/a | end = menu.index('end') |
---|
120 | n/a | if end is None: |
---|
121 | n/a | end = -1 |
---|
122 | n/a | |
---|
123 | n/a | if end > 0: |
---|
124 | n/a | menu.delete(0, end) |
---|
125 | n/a | WindowList.add_windows_to_menu(menu) |
---|
126 | n/a | WindowList.register_callback(postwindowsmenu) |
---|
127 | n/a | |
---|
128 | n/a | def about_dialog(event=None): |
---|
129 | n/a | from idlelib import aboutDialog |
---|
130 | n/a | aboutDialog.AboutDialog(root, 'About IDLE') |
---|
131 | n/a | |
---|
132 | n/a | def config_dialog(event=None): |
---|
133 | n/a | from idlelib import configDialog |
---|
134 | n/a | |
---|
135 | n/a | # Ensure that the root object has an instance_dict attribute, |
---|
136 | n/a | # mirrors code in EditorWindow (although that sets the attribute |
---|
137 | n/a | # on an EditorWindow instance that is then passed as the first |
---|
138 | n/a | # argument to ConfigDialog) |
---|
139 | n/a | root.instance_dict = flist.inversedict |
---|
140 | n/a | root.instance_dict = flist.inversedict |
---|
141 | n/a | configDialog.ConfigDialog(root, 'Settings') |
---|
142 | n/a | |
---|
143 | n/a | def help_dialog(event=None): |
---|
144 | n/a | from idlelib import textView |
---|
145 | n/a | fn = path.join(path.abspath(path.dirname(__file__)), 'help.txt') |
---|
146 | n/a | textView.view_file(root, 'Help', fn) |
---|
147 | n/a | |
---|
148 | n/a | root.bind('<<about-idle>>', about_dialog) |
---|
149 | n/a | root.bind('<<open-config-dialog>>', config_dialog) |
---|
150 | n/a | root.createcommand('::tk::mac::ShowPreferences', config_dialog) |
---|
151 | n/a | if flist: |
---|
152 | n/a | root.bind('<<close-all-windows>>', flist.close_all_callback) |
---|
153 | n/a | |
---|
154 | n/a | # The binding above doesn't reliably work on all versions of Tk |
---|
155 | n/a | # on MacOSX. Adding command definition below does seem to do the |
---|
156 | n/a | # right thing for now. |
---|
157 | n/a | root.createcommand('exit', flist.close_all_callback) |
---|
158 | n/a | |
---|
159 | n/a | if isCarbonAquaTk(root): |
---|
160 | n/a | # for Carbon AquaTk, replace the default Tk apple menu |
---|
161 | n/a | menudict['application'] = menu = Menu(menubar, name='apple') |
---|
162 | n/a | menubar.add_cascade(label='IDLE', menu=menu) |
---|
163 | n/a | Bindings.menudefs.insert(0, |
---|
164 | n/a | ('application', [ |
---|
165 | n/a | ('About IDLE', '<<about-idle>>'), |
---|
166 | n/a | None, |
---|
167 | n/a | ])) |
---|
168 | n/a | tkversion = root.tk.eval('info patchlevel') |
---|
169 | n/a | if tuple(map(int, tkversion.split('.'))) < (8, 4, 14): |
---|
170 | n/a | # for earlier AquaTk versions, supply a Preferences menu item |
---|
171 | n/a | Bindings.menudefs[0][1].append( |
---|
172 | n/a | ('_Preferences....', '<<open-config-dialog>>'), |
---|
173 | n/a | ) |
---|
174 | n/a | else: |
---|
175 | n/a | # assume Cocoa AquaTk |
---|
176 | n/a | # replace default About dialog with About IDLE one |
---|
177 | n/a | root.createcommand('tkAboutDialog', about_dialog) |
---|
178 | n/a | # replace default "Help" item in Help menu |
---|
179 | n/a | root.createcommand('::tk::mac::ShowHelp', help_dialog) |
---|
180 | n/a | # remove redundant "IDLE Help" from menu |
---|
181 | n/a | del Bindings.menudefs[-1][1][0] |
---|
182 | n/a | |
---|
183 | n/a | def setupApp(root, flist): |
---|
184 | n/a | """ |
---|
185 | n/a | Perform setup for the OSX application bundle. |
---|
186 | n/a | """ |
---|
187 | n/a | if not runningAsOSXApp(): return |
---|
188 | n/a | |
---|
189 | n/a | hideTkConsole(root) |
---|
190 | n/a | overrideRootMenu(root, flist) |
---|
191 | n/a | addOpenEventSupport(root, flist) |
---|