Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1 | """ |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 2 | A number of functions that enhance IDLE on Mac OSX. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 3 | """ |
Terry Jan Reedy | 2518fa8 | 2016-06-12 15:49:20 -0400 | [diff] [blame] | 4 | from sys import platform # Used in _init_tk_type, changed by test. |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 5 | import warnings |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 6 | |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame^] | 7 | import tkinter |
| 8 | |
Terry Jan Reedy | 2518fa8 | 2016-06-12 15:49:20 -0400 | [diff] [blame] | 9 | |
| 10 | ## Define functions that query the Mac graphics type. |
| 11 | ## _tk_type and its initializer are private to this section. |
| 12 | |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 13 | _tk_type = None |
| 14 | |
Terry Jan Reedy | 2518fa8 | 2016-06-12 15:49:20 -0400 | [diff] [blame] | 15 | def _init_tk_type(): |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 16 | """ |
| 17 | Initializes OS X Tk variant values for |
| 18 | isAquaTk(), isCarbonTk(), isCocoaTk(), and isXQuartz(). |
| 19 | """ |
| 20 | global _tk_type |
Terry Jan Reedy | 2518fa8 | 2016-06-12 15:49:20 -0400 | [diff] [blame] | 21 | if platform == 'darwin': |
| 22 | root = tkinter.Tk() |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 23 | ws = root.tk.call('tk', 'windowingsystem') |
| 24 | if 'x11' in ws: |
| 25 | _tk_type = "xquartz" |
| 26 | elif 'aqua' not in ws: |
| 27 | _tk_type = "other" |
| 28 | elif 'AppKit' in root.tk.call('winfo', 'server', '.'): |
| 29 | _tk_type = "cocoa" |
| 30 | else: |
| 31 | _tk_type = "carbon" |
Terry Jan Reedy | 2518fa8 | 2016-06-12 15:49:20 -0400 | [diff] [blame] | 32 | root.destroy() |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 33 | else: |
| 34 | _tk_type = "other" |
| 35 | |
| 36 | def isAquaTk(): |
| 37 | """ |
| 38 | Returns True if IDLE is using a native OS X Tk (Cocoa or Carbon). |
| 39 | """ |
Terry Jan Reedy | fb51e65 | 2016-06-08 18:09:22 -0400 | [diff] [blame] | 40 | if not _tk_type: |
| 41 | _init_tk_type() |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 42 | return _tk_type == "cocoa" or _tk_type == "carbon" |
| 43 | |
| 44 | def isCarbonTk(): |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 45 | """ |
| 46 | Returns True if IDLE is using a Carbon Aqua Tk (instead of the |
| 47 | newer Cocoa Aqua Tk). |
| 48 | """ |
Terry Jan Reedy | fb51e65 | 2016-06-08 18:09:22 -0400 | [diff] [blame] | 49 | if not _tk_type: |
| 50 | _init_tk_type() |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 51 | return _tk_type == "carbon" |
| 52 | |
| 53 | def isCocoaTk(): |
| 54 | """ |
| 55 | Returns True if IDLE is using a Cocoa Aqua Tk. |
| 56 | """ |
Terry Jan Reedy | fb51e65 | 2016-06-08 18:09:22 -0400 | [diff] [blame] | 57 | if not _tk_type: |
| 58 | _init_tk_type() |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 59 | return _tk_type == "cocoa" |
| 60 | |
| 61 | def isXQuartz(): |
| 62 | """ |
| 63 | Returns True if IDLE is using an OS X X11 Tk. |
| 64 | """ |
Terry Jan Reedy | fb51e65 | 2016-06-08 18:09:22 -0400 | [diff] [blame] | 65 | if not _tk_type: |
| 66 | _init_tk_type() |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 67 | return _tk_type == "xquartz" |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 68 | |
Terry Jan Reedy | 2518fa8 | 2016-06-12 15:49:20 -0400 | [diff] [blame] | 69 | |
Ned Deily | 4ce92b2 | 2011-01-15 04:37:12 +0000 | [diff] [blame] | 70 | def tkVersionWarning(root): |
| 71 | """ |
| 72 | Returns a string warning message if the Tk version in use appears to |
Ned Deily | c556c64 | 2012-07-30 03:31:21 -0700 | [diff] [blame] | 73 | be one known to cause problems with IDLE. |
| 74 | 1. Apple Cocoa-based Tk 8.5.7 shipped with Mac OS X 10.6 is unusable. |
| 75 | 2. Apple Cocoa-based Tk 8.5.9 in OS X 10.7 and 10.8 is better but |
| 76 | can still crash unexpectedly. |
Ned Deily | 4ce92b2 | 2011-01-15 04:37:12 +0000 | [diff] [blame] | 77 | """ |
| 78 | |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 79 | if isCocoaTk(): |
Ned Deily | c556c64 | 2012-07-30 03:31:21 -0700 | [diff] [blame] | 80 | patchlevel = root.tk.call('info', 'patchlevel') |
| 81 | if patchlevel not in ('8.5.7', '8.5.9'): |
| 82 | return False |
| 83 | return (r"WARNING: The version of Tcl/Tk ({0}) in use may" |
Ned Deily | 4ce92b2 | 2011-01-15 04:37:12 +0000 | [diff] [blame] | 84 | r" be unstable.\n" |
| 85 | r"Visit http://www.python.org/download/mac/tcltk/" |
Ned Deily | c556c64 | 2012-07-30 03:31:21 -0700 | [diff] [blame] | 86 | r" for current information.".format(patchlevel)) |
Ned Deily | 4ce92b2 | 2011-01-15 04:37:12 +0000 | [diff] [blame] | 87 | else: |
| 88 | return False |
| 89 | |
Terry Jan Reedy | 2518fa8 | 2016-06-12 15:49:20 -0400 | [diff] [blame] | 90 | |
| 91 | ## Fix the menu and related functions. |
| 92 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 93 | def addOpenEventSupport(root, flist): |
| 94 | """ |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 95 | This ensures that the application will respond to open AppleEvents, which |
| 96 | makes is feasible to use IDLE as the default application for python files. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 97 | """ |
| 98 | def doOpenFile(*args): |
| 99 | for fn in args: |
| 100 | flist.open(fn) |
| 101 | |
| 102 | # The command below is a hook in aquatk that is called whenever the app |
| 103 | # receives a file open event. The callback can have multiple arguments, |
| 104 | # one for every file that should be opened. |
| 105 | root.createcommand("::tk::mac::OpenDocument", doOpenFile) |
| 106 | |
| 107 | def hideTkConsole(root): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 108 | try: |
| 109 | root.tk.call('console', 'hide') |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 110 | except tkinter.TclError: |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 111 | # Some versions of the Tk framework don't have a console object |
| 112 | pass |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 113 | |
| 114 | def overrideRootMenu(root, flist): |
| 115 | """ |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 116 | Replace the Tk root menu by something that is more appropriate for |
| 117 | IDLE with an Aqua Tk. |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 118 | """ |
| 119 | # The menu that is attached to the Tk root (".") is also used by AquaTk for |
| 120 | # all windows that don't specify a menu of their own. The default menubar |
| 121 | # contains a number of menus, none of which are appropriate for IDLE. The |
| 122 | # Most annoying of those is an 'About Tck/Tk...' menu in the application |
| 123 | # menu. |
| 124 | # |
| 125 | # This function replaces the default menubar by a mostly empty one, it |
| 126 | # should only contain the correct application menu and the window menu. |
| 127 | # |
| 128 | # Due to a (mis-)feature of TkAqua the user will also see an empty Help |
| 129 | # menu. |
Terry Jan Reedy | 038c16b | 2015-05-15 23:03:17 -0400 | [diff] [blame] | 130 | from tkinter import Menu |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 131 | from idlelib import mainmenu |
| 132 | from idlelib import windows |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 133 | |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 134 | closeItem = mainmenu.menudefs[0][1][-2] |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 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. |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 139 | del mainmenu.menudefs[0][1][-3:] |
| 140 | mainmenu.menudefs[0][1].insert(6, closeItem) |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 141 | |
| 142 | # Remove the 'About' entry from the help menu, it is in the application |
| 143 | # menu |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 144 | del mainmenu.menudefs[-1][1][0:2] |
Terry Jan Reedy | a9421fb | 2014-10-22 20:15:18 -0400 | [diff] [blame] | 145 | # Remove the 'Configure Idle' entry from the options menu, it is in the |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 146 | # application menu as 'Preferences' |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 147 | del mainmenu.menudefs[-2][1][0] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 148 | menubar = Menu(root) |
| 149 | root.configure(menu=menubar) |
| 150 | menudict = {} |
| 151 | |
Terry Jan Reedy | 30f1f67 | 2015-07-30 16:44:22 -0400 | [diff] [blame] | 152 | menudict['windows'] = menu = Menu(menubar, name='windows', tearoff=0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 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) |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 162 | windows.add_windows_to_menu(menu) |
Ned Deily | 622b2f6 | 2016-06-03 17:50:44 -0700 | [diff] [blame] | 163 | windows.register_callback(postwindowsmenu) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 164 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 165 | def about_dialog(event=None): |
Terry Jan Reedy | b50c637 | 2015-09-20 22:55:39 -0400 | [diff] [blame] | 166 | "Handle Help 'About IDLE' event." |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 167 | # Synchronize with editor.EditorWindow.about_dialog. |
| 168 | from idlelib import help_about |
| 169 | help_about.AboutDialog(root, 'About IDLE') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 170 | |
| 171 | def config_dialog(event=None): |
Terry Jan Reedy | b50c637 | 2015-09-20 22:55:39 -0400 | [diff] [blame] | 172 | "Handle Options 'Configure IDLE' event." |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 173 | # Synchronize with editor.EditorWindow.config_dialog. |
| 174 | from idlelib import configdialog |
Ronald Oussoren | 220a9fb | 2009-05-26 18:41:00 +0000 | [diff] [blame] | 175 | |
| 176 | # Ensure that the root object has an instance_dict attribute, |
| 177 | # mirrors code in EditorWindow (although that sets the attribute |
| 178 | # on an EditorWindow instance that is then passed as the first |
| 179 | # argument to ConfigDialog) |
| 180 | root.instance_dict = flist.inversedict |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 181 | configdialog.ConfigDialog(root, 'Settings') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 182 | |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 183 | def help_dialog(event=None): |
Terry Jan Reedy | b50c637 | 2015-09-20 22:55:39 -0400 | [diff] [blame] | 184 | "Handle Help 'IDLE Help' event." |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 185 | # Synchronize with editor.EditorWindow.help_dialog. |
Terry Jan Reedy | 5d46ab1 | 2015-09-20 19:57:13 -0400 | [diff] [blame] | 186 | from idlelib import help |
| 187 | help.show_idlehelp(root) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 188 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 189 | root.bind('<<about-idle>>', about_dialog) |
| 190 | root.bind('<<open-config-dialog>>', config_dialog) |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 191 | root.createcommand('::tk::mac::ShowPreferences', config_dialog) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 192 | if flist: |
| 193 | root.bind('<<close-all-windows>>', flist.close_all_callback) |
| 194 | |
Ronald Oussoren | 10e05e1 | 2010-12-07 15:28:10 +0000 | [diff] [blame] | 195 | # The binding above doesn't reliably work on all versions of Tk |
| 196 | # on MacOSX. Adding command definition below does seem to do the |
| 197 | # right thing for now. |
| 198 | root.createcommand('exit', flist.close_all_callback) |
| 199 | |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 200 | if isCarbonTk(): |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 201 | # for Carbon AquaTk, replace the default Tk apple menu |
Terry Jan Reedy | 30f1f67 | 2015-07-30 16:44:22 -0400 | [diff] [blame] | 202 | menudict['application'] = menu = Menu(menubar, name='apple', |
| 203 | tearoff=0) |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 204 | menubar.add_cascade(label='IDLE', menu=menu) |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 205 | mainmenu.menudefs.insert(0, |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 206 | ('application', [ |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 207 | ('About IDLE', '<<about-idle>>'), |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 208 | None, |
| 209 | ])) |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 210 | if isCocoaTk(): |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 211 | # replace default About dialog with About IDLE one |
| 212 | root.createcommand('tkAboutDialog', about_dialog) |
| 213 | # replace default "Help" item in Help menu |
| 214 | root.createcommand('::tk::mac::ShowHelp', help_dialog) |
| 215 | # remove redundant "IDLE Help" from menu |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 216 | del mainmenu.menudefs[-1][1][0] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 217 | |
Terry Jan Reedy | 24f3a18 | 2016-06-08 14:37:05 -0400 | [diff] [blame] | 218 | def fixb2context(root): |
| 219 | '''Removed bad AquaTk Button-2 (right) and Paste bindings. |
| 220 | |
| 221 | They prevent context menu access and seem to be gone in AquaTk8.6. |
| 222 | See issue #24801. |
| 223 | ''' |
| 224 | root.unbind_class('Text', '<B2>') |
| 225 | root.unbind_class('Text', '<B2-Motion>') |
| 226 | root.unbind_class('Text', '<<PasteSelection>>') |
| 227 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 228 | def setupApp(root, flist): |
| 229 | """ |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 230 | Perform initial OS X customizations if needed. |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 231 | Called from pyshell.main() after initial calls to Tk() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 232 | |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 233 | There are currently three major versions of Tk in use on OS X: |
| 234 | 1. Aqua Cocoa Tk (native default since OS X 10.6) |
| 235 | 2. Aqua Carbon Tk (original native, 32-bit only, deprecated) |
| 236 | 3. X11 (supported by some third-party distributors, deprecated) |
| 237 | There are various differences among the three that affect IDLE |
| 238 | behavior, primarily with menus, mouse key events, and accelerators. |
| 239 | Some one-time customizations are performed here. |
| 240 | Others are dynamically tested throughout idlelib by calls to the |
| 241 | isAquaTk(), isCarbonTk(), isCocoaTk(), isXQuartz() functions which |
| 242 | are initialized here as well. |
| 243 | """ |
Ned Deily | b760167 | 2014-03-27 20:49:14 -0700 | [diff] [blame] | 244 | if isAquaTk(): |
| 245 | hideTkConsole(root) |
| 246 | overrideRootMenu(root, flist) |
| 247 | addOpenEventSupport(root, flist) |
Ned Deily | 139fb7c | 2016-06-11 02:57:56 -0400 | [diff] [blame] | 248 | fixb2context(root) |
Terry Jan Reedy | 2518fa8 | 2016-06-12 15:49:20 -0400 | [diff] [blame] | 249 | |
| 250 | |
| 251 | if __name__ == '__main__': |
| 252 | from unittest import main |
| 253 | main('idlelib.idle_test.test_macosx', verbosity=2) |