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