Tim Peters | 231c3c8 | 2006-06-11 19:43:49 +0000 | [diff] [blame] | 1 | """ |
Ned Deily | 57847df | 2014-03-27 20:47:04 -0700 | [diff] [blame] | 2 | A number of functions that enhance IDLE on Mac OSX. |
Tim Peters | 231c3c8 | 2006-06-11 19:43:49 +0000 | [diff] [blame] | 3 | """ |
| 4 | import sys |
Georg Brandl | 6634bf2 | 2008-05-20 07:13:37 +0000 | [diff] [blame] | 5 | import Tkinter |
Ned Deily | 4a70550 | 2011-01-18 04:33:22 +0000 | [diff] [blame] | 6 | from os import path |
Tim Peters | 231c3c8 | 2006-06-11 19:43:49 +0000 | [diff] [blame] | 7 | |
Ronald Oussoren | 92919a6 | 2009-12-24 13:30:58 +0000 | [diff] [blame] | 8 | |
Ned Deily | 57847df | 2014-03-27 20:47:04 -0700 | [diff] [blame] | 9 | import warnings |
Ronald Oussoren | 92919a6 | 2009-12-24 13:30:58 +0000 | [diff] [blame] | 10 | |
Tim Peters | 231c3c8 | 2006-06-11 19:43:49 +0000 | [diff] [blame] | 11 | def runningAsOSXApp(): |
Ned Deily | 57847df | 2014-03-27 20:47:04 -0700 | [diff] [blame] | 12 | warnings.warn("runningAsOSXApp() is deprecated, use isAquaTk()", |
| 13 | DeprecationWarning, stacklevel=2) |
| 14 | return isAquaTk() |
Ned Deily | 4a70550 | 2011-01-18 04:33:22 +0000 | [diff] [blame] | 15 | |
| 16 | def isCarbonAquaTk(root): |
Ned Deily | 57847df | 2014-03-27 20:47:04 -0700 | [diff] [blame] | 17 | warnings.warn("isCarbonAquaTk(root) is deprecated, use isCarbonTk()", |
| 18 | DeprecationWarning, stacklevel=2) |
| 19 | return isCarbonTk() |
| 20 | |
| 21 | _tk_type = None |
| 22 | |
| 23 | def _initializeTkVariantTests(root): |
| 24 | """ |
| 25 | Initializes OS X Tk variant values for |
| 26 | isAquaTk(), isCarbonTk(), isCocoaTk(), and isXQuartz(). |
| 27 | """ |
| 28 | global _tk_type |
| 29 | if sys.platform == 'darwin': |
| 30 | ws = root.tk.call('tk', 'windowingsystem') |
| 31 | if 'x11' in ws: |
| 32 | _tk_type = "xquartz" |
| 33 | elif 'aqua' not in ws: |
| 34 | _tk_type = "other" |
| 35 | elif 'AppKit' in root.tk.call('winfo', 'server', '.'): |
| 36 | _tk_type = "cocoa" |
| 37 | else: |
| 38 | _tk_type = "carbon" |
| 39 | else: |
| 40 | _tk_type = "other" |
| 41 | |
| 42 | def isAquaTk(): |
| 43 | """ |
| 44 | Returns True if IDLE is using a native OS X Tk (Cocoa or Carbon). |
| 45 | """ |
| 46 | assert _tk_type is not None |
| 47 | return _tk_type == "cocoa" or _tk_type == "carbon" |
| 48 | |
| 49 | def isCarbonTk(): |
Ned Deily | 4a70550 | 2011-01-18 04:33:22 +0000 | [diff] [blame] | 50 | """ |
| 51 | Returns True if IDLE is using a Carbon Aqua Tk (instead of the |
| 52 | newer Cocoa Aqua Tk). |
| 53 | """ |
Ned Deily | 57847df | 2014-03-27 20:47:04 -0700 | [diff] [blame] | 54 | assert _tk_type is not None |
| 55 | return _tk_type == "carbon" |
| 56 | |
| 57 | def isCocoaTk(): |
| 58 | """ |
| 59 | Returns True if IDLE is using a Cocoa Aqua Tk. |
| 60 | """ |
| 61 | assert _tk_type is not None |
| 62 | return _tk_type == "cocoa" |
| 63 | |
| 64 | def isXQuartz(): |
| 65 | """ |
| 66 | Returns True if IDLE is using an OS X X11 Tk. |
| 67 | """ |
| 68 | assert _tk_type is not None |
| 69 | return _tk_type == "xquartz" |
Ned Deily | 4a70550 | 2011-01-18 04:33:22 +0000 | [diff] [blame] | 70 | |
Ned Deily | 2a6f4b3 | 2011-01-30 00:18:47 +0000 | [diff] [blame] | 71 | def tkVersionWarning(root): |
| 72 | """ |
| 73 | Returns a string warning message if the Tk version in use appears to |
Ned Deily | 38df514 | 2012-07-30 03:28:22 -0700 | [diff] [blame] | 74 | be one known to cause problems with IDLE. |
| 75 | 1. Apple Cocoa-based Tk 8.5.7 shipped with Mac OS X 10.6 is unusable. |
| 76 | 2. Apple Cocoa-based Tk 8.5.9 in OS X 10.7 and 10.8 is better but |
| 77 | can still crash unexpectedly. |
Ned Deily | 2a6f4b3 | 2011-01-30 00:18:47 +0000 | [diff] [blame] | 78 | """ |
| 79 | |
Ned Deily | 57847df | 2014-03-27 20:47:04 -0700 | [diff] [blame] | 80 | if isCocoaTk(): |
Ned Deily | 38df514 | 2012-07-30 03:28:22 -0700 | [diff] [blame] | 81 | patchlevel = root.tk.call('info', 'patchlevel') |
| 82 | if patchlevel not in ('8.5.7', '8.5.9'): |
| 83 | return False |
| 84 | return (r"WARNING: The version of Tcl/Tk ({0}) in use may" |
Ned Deily | 2a6f4b3 | 2011-01-30 00:18:47 +0000 | [diff] [blame] | 85 | r" be unstable.\n" |
| 86 | r"Visit http://www.python.org/download/mac/tcltk/" |
Ned Deily | 38df514 | 2012-07-30 03:28:22 -0700 | [diff] [blame] | 87 | r" for current information.".format(patchlevel)) |
Ned Deily | 2a6f4b3 | 2011-01-30 00:18:47 +0000 | [diff] [blame] | 88 | else: |
| 89 | return False |
| 90 | |
Tim Peters | 231c3c8 | 2006-06-11 19:43:49 +0000 | [diff] [blame] | 91 | def addOpenEventSupport(root, flist): |
| 92 | """ |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 93 | This ensures that the application will respond to open AppleEvents, which |
| 94 | makes is feasible to use IDLE as the default application for python files. |
Tim Peters | 231c3c8 | 2006-06-11 19:43:49 +0000 | [diff] [blame] | 95 | """ |
| 96 | def doOpenFile(*args): |
| 97 | for fn in args: |
| 98 | flist.open(fn) |
| 99 | |
| 100 | # The command below is a hook in aquatk that is called whenever the app |
| 101 | # receives a file open event. The callback can have multiple arguments, |
| 102 | # one for every file that should be opened. |
| 103 | root.createcommand("::tk::mac::OpenDocument", doOpenFile) |
| 104 | |
| 105 | def hideTkConsole(root): |
Ronald Oussoren | 9b0bcc1 | 2007-07-09 06:02:21 +0000 | [diff] [blame] | 106 | try: |
| 107 | root.tk.call('console', 'hide') |
Georg Brandl | 6634bf2 | 2008-05-20 07:13:37 +0000 | [diff] [blame] | 108 | except Tkinter.TclError: |
Ronald Oussoren | 9b0bcc1 | 2007-07-09 06:02:21 +0000 | [diff] [blame] | 109 | # Some versions of the Tk framework don't have a console object |
| 110 | pass |
Tim Peters | 231c3c8 | 2006-06-11 19:43:49 +0000 | [diff] [blame] | 111 | |
Ronald Oussoren | 8133f9d | 2006-07-23 09:46:11 +0000 | [diff] [blame] | 112 | def overrideRootMenu(root, flist): |
| 113 | """ |
Ned Deily | 57847df | 2014-03-27 20:47:04 -0700 | [diff] [blame] | 114 | Replace the Tk root menu by something that is more appropriate for |
| 115 | IDLE with an Aqua Tk. |
Ronald Oussoren | 8133f9d | 2006-07-23 09:46:11 +0000 | [diff] [blame] | 116 | """ |
Tim Peters | 0bbfd83 | 2006-07-24 21:02:15 +0000 | [diff] [blame] | 117 | # The menu that is attached to the Tk root (".") is also used by AquaTk for |
Ronald Oussoren | 8133f9d | 2006-07-23 09:46:11 +0000 | [diff] [blame] | 118 | # all windows that don't specify a menu of their own. The default menubar |
| 119 | # contains a number of menus, none of which are appropriate for IDLE. The |
| 120 | # Most annoying of those is an 'About Tck/Tk...' menu in the application |
| 121 | # menu. |
| 122 | # |
| 123 | # This function replaces the default menubar by a mostly empty one, it |
| 124 | # should only contain the correct application menu and the window menu. |
| 125 | # |
| 126 | # Due to a (mis-)feature of TkAqua the user will also see an empty Help |
| 127 | # menu. |
Georg Brandl | 6634bf2 | 2008-05-20 07:13:37 +0000 | [diff] [blame] | 128 | from Tkinter import Menu, Text, Text |
Florent Xicluna | d630c04 | 2010-04-02 07:24:52 +0000 | [diff] [blame] | 129 | from idlelib.EditorWindow import prepstr, get_accelerator |
| 130 | from idlelib import Bindings |
| 131 | from idlelib import WindowList |
| 132 | from idlelib.MultiCall import MultiCallCreator |
Ronald Oussoren | 8133f9d | 2006-07-23 09:46:11 +0000 | [diff] [blame] | 133 | |
Ned Deily | 57847df | 2014-03-27 20:47:04 -0700 | [diff] [blame] | 134 | closeItem = Bindings.menudefs[0][1][-2] |
| 135 | |
| 136 | # Remove the last 3 items of the file menu: a separator, close window and |
| 137 | # quit. Close window will be reinserted just above the save item, where |
| 138 | # it should be according to the HIG. Quit is in the application menu. |
| 139 | del Bindings.menudefs[0][1][-3:] |
| 140 | Bindings.menudefs[0][1].insert(6, closeItem) |
| 141 | |
| 142 | # Remove the 'About' entry from the help menu, it is in the application |
| 143 | # menu |
| 144 | del Bindings.menudefs[-1][1][0:2] |
Terry Jan Reedy | 7a16207 | 2014-10-22 20:15:12 -0400 | [diff] [blame] | 145 | # Remove the 'Configure Idle' entry from the options menu, it is in the |
Ned Deily | 57847df | 2014-03-27 20:47:04 -0700 | [diff] [blame] | 146 | # application menu as 'Preferences' |
Terry Jan Reedy | 7a16207 | 2014-10-22 20:15:12 -0400 | [diff] [blame] | 147 | del Bindings.menudefs[-2][1][0] |
Ronald Oussoren | 8133f9d | 2006-07-23 09:46:11 +0000 | [diff] [blame] | 148 | menubar = Menu(root) |
| 149 | root.configure(menu=menubar) |
| 150 | menudict = {} |
| 151 | |
| 152 | menudict['windows'] = menu = Menu(menubar, name='windows') |
| 153 | menubar.add_cascade(label='Window', menu=menu, underline=0) |
| 154 | |
| 155 | def postwindowsmenu(menu=menu): |
| 156 | end = menu.index('end') |
| 157 | if end is None: |
| 158 | end = -1 |
| 159 | |
| 160 | if end > 0: |
| 161 | menu.delete(0, end) |
| 162 | WindowList.add_windows_to_menu(menu) |
| 163 | WindowList.register_callback(postwindowsmenu) |
| 164 | |
Ronald Oussoren | 8133f9d | 2006-07-23 09:46:11 +0000 | [diff] [blame] | 165 | def about_dialog(event=None): |
Florent Xicluna | d630c04 | 2010-04-02 07:24:52 +0000 | [diff] [blame] | 166 | from idlelib import aboutDialog |
Ronald Oussoren | 8133f9d | 2006-07-23 09:46:11 +0000 | [diff] [blame] | 167 | aboutDialog.AboutDialog(root, 'About IDLE') |
| 168 | |
| 169 | def config_dialog(event=None): |
Florent Xicluna | d630c04 | 2010-04-02 07:24:52 +0000 | [diff] [blame] | 170 | from idlelib import configDialog |
Ronald Oussoren | 55d8828 | 2009-05-26 18:44:48 +0000 | [diff] [blame] | 171 | root.instance_dict = flist.inversedict |
Ronald Oussoren | 8133f9d | 2006-07-23 09:46:11 +0000 | [diff] [blame] | 172 | configDialog.ConfigDialog(root, 'Settings') |
| 173 | |
Ned Deily | 4a70550 | 2011-01-18 04:33:22 +0000 | [diff] [blame] | 174 | def help_dialog(event=None): |
| 175 | from idlelib import textView |
| 176 | fn = path.join(path.abspath(path.dirname(__file__)), 'help.txt') |
| 177 | textView.view_file(root, 'Help', fn) |
Ronald Oussoren | 9b0bcc1 | 2007-07-09 06:02:21 +0000 | [diff] [blame] | 178 | |
Ronald Oussoren | 8133f9d | 2006-07-23 09:46:11 +0000 | [diff] [blame] | 179 | root.bind('<<about-idle>>', about_dialog) |
| 180 | root.bind('<<open-config-dialog>>', config_dialog) |
Ned Deily | 4a70550 | 2011-01-18 04:33:22 +0000 | [diff] [blame] | 181 | root.createcommand('::tk::mac::ShowPreferences', config_dialog) |
Ronald Oussoren | 8133f9d | 2006-07-23 09:46:11 +0000 | [diff] [blame] | 182 | if flist: |
| 183 | root.bind('<<close-all-windows>>', flist.close_all_callback) |
| 184 | |
Ronald Oussoren | 8ec374c | 2010-12-07 16:02:59 +0000 | [diff] [blame] | 185 | # The binding above doesn't reliably work on all versions of Tk |
| 186 | # on MacOSX. Adding command definition below does seem to do the |
| 187 | # right thing for now. |
| 188 | root.createcommand('exit', flist.close_all_callback) |
| 189 | |
Ned Deily | 57847df | 2014-03-27 20:47:04 -0700 | [diff] [blame] | 190 | if isCarbonTk(): |
Ned Deily | 4a70550 | 2011-01-18 04:33:22 +0000 | [diff] [blame] | 191 | # for Carbon AquaTk, replace the default Tk apple menu |
| 192 | menudict['application'] = menu = Menu(menubar, name='apple') |
| 193 | menubar.add_cascade(label='IDLE', menu=menu) |
| 194 | Bindings.menudefs.insert(0, |
| 195 | ('application', [ |
Ronald Oussoren | 9b0bcc1 | 2007-07-09 06:02:21 +0000 | [diff] [blame] | 196 | ('About IDLE', '<<about-idle>>'), |
Ned Deily | 4a70550 | 2011-01-18 04:33:22 +0000 | [diff] [blame] | 197 | None, |
| 198 | ])) |
| 199 | tkversion = root.tk.eval('info patchlevel') |
| 200 | if tuple(map(int, tkversion.split('.'))) < (8, 4, 14): |
| 201 | # for earlier AquaTk versions, supply a Preferences menu item |
| 202 | Bindings.menudefs[0][1].append( |
| 203 | ('_Preferences....', '<<open-config-dialog>>'), |
| 204 | ) |
Ned Deily | 57847df | 2014-03-27 20:47:04 -0700 | [diff] [blame] | 205 | if isCocoaTk(): |
Ned Deily | 4a70550 | 2011-01-18 04:33:22 +0000 | [diff] [blame] | 206 | # replace default About dialog with About IDLE one |
| 207 | root.createcommand('tkAboutDialog', about_dialog) |
| 208 | # replace default "Help" item in Help menu |
| 209 | root.createcommand('::tk::mac::ShowHelp', help_dialog) |
| 210 | # remove redundant "IDLE Help" from menu |
| 211 | del Bindings.menudefs[-1][1][0] |
Ronald Oussoren | 8133f9d | 2006-07-23 09:46:11 +0000 | [diff] [blame] | 212 | |
Tim Peters | 231c3c8 | 2006-06-11 19:43:49 +0000 | [diff] [blame] | 213 | def setupApp(root, flist): |
| 214 | """ |
Ned Deily | 57847df | 2014-03-27 20:47:04 -0700 | [diff] [blame] | 215 | Perform initial OS X customizations if needed. |
| 216 | Called from PyShell.main() after initial calls to Tk() |
Tim Peters | 231c3c8 | 2006-06-11 19:43:49 +0000 | [diff] [blame] | 217 | |
Ned Deily | 57847df | 2014-03-27 20:47:04 -0700 | [diff] [blame] | 218 | There are currently three major versions of Tk in use on OS X: |
| 219 | 1. Aqua Cocoa Tk (native default since OS X 10.6) |
| 220 | 2. Aqua Carbon Tk (original native, 32-bit only, deprecated) |
| 221 | 3. X11 (supported by some third-party distributors, deprecated) |
| 222 | There are various differences among the three that affect IDLE |
| 223 | behavior, primarily with menus, mouse key events, and accelerators. |
| 224 | Some one-time customizations are performed here. |
| 225 | Others are dynamically tested throughout idlelib by calls to the |
| 226 | isAquaTk(), isCarbonTk(), isCocoaTk(), isXQuartz() functions which |
| 227 | are initialized here as well. |
| 228 | """ |
| 229 | _initializeTkVariantTests(root) |
| 230 | if isAquaTk(): |
| 231 | hideTkConsole(root) |
| 232 | overrideRootMenu(root, flist) |
| 233 | addOpenEventSupport(root, flist) |