Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1 | """ |
| 2 | A number of function that enhance IDLE on MacOSX when it used as a normal |
| 3 | GUI application (as opposed to an X11 application). |
| 4 | """ |
| 5 | import sys |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 6 | import tkinter |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 7 | from os import path |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 8 | |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 9 | |
| 10 | _appbundle = None |
| 11 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 12 | def runningAsOSXApp(): |
Ronald Oussoren | 827822e | 2009-02-12 15:01:44 +0000 | [diff] [blame] | 13 | """ |
| 14 | Returns True if Python is running from within an app on OSX. |
| 15 | If so, assume that Python was built with Aqua Tcl/Tk rather than |
Benjamin Peterson | 8719ad5 | 2009-09-11 22:24:02 +0000 | [diff] [blame] | 16 | X11 Tcl/Tk. |
Ronald Oussoren | 827822e | 2009-02-12 15:01:44 +0000 | [diff] [blame] | 17 | """ |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 18 | global _appbundle |
| 19 | if _appbundle is None: |
| 20 | _appbundle = (sys.platform == 'darwin' and '.app' in sys.executable) |
| 21 | return _appbundle |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 22 | |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 23 | _carbonaquatk = None |
| 24 | |
| 25 | def isCarbonAquaTk(root): |
| 26 | """ |
| 27 | Returns True if IDLE is using a Carbon Aqua Tk (instead of the |
| 28 | newer Cocoa Aqua Tk). |
| 29 | """ |
| 30 | global _carbonaquatk |
| 31 | if _carbonaquatk is None: |
| 32 | _carbonaquatk = (runningAsOSXApp() and |
| 33 | 'aqua' in root.tk.call('tk', 'windowingsystem') and |
| 34 | 'AppKit' not in root.tk.call('winfo', 'server', '.')) |
| 35 | return _carbonaquatk |
| 36 | |
Ned Deily | 4ce92b2 | 2011-01-15 04:37:12 +0000 | [diff] [blame] | 37 | def tkVersionWarning(root): |
| 38 | """ |
| 39 | Returns a string warning message if the Tk version in use appears to |
| 40 | be one known to cause problems with IDLE. The Apple Cocoa-based Tk 8.5 |
| 41 | that was shipped with Mac OS X 10.6. |
| 42 | """ |
| 43 | |
| 44 | if (runningAsOSXApp() and |
| 45 | ('AppKit' in root.tk.call('winfo', 'server', '.')) and |
| 46 | (root.tk.call('info', 'patchlevel') == '8.5.7') ): |
| 47 | return (r"WARNING: The version of Tcl/Tk (8.5.7) in use may" |
| 48 | r" be unstable.\n" |
| 49 | r"Visit http://www.python.org/download/mac/tcltk/" |
| 50 | r" for current information.") |
| 51 | else: |
| 52 | return False |
| 53 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 54 | def addOpenEventSupport(root, flist): |
| 55 | """ |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 56 | This ensures that the application will respond to open AppleEvents, which |
| 57 | 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] | 58 | """ |
| 59 | def doOpenFile(*args): |
| 60 | for fn in args: |
| 61 | flist.open(fn) |
| 62 | |
| 63 | # The command below is a hook in aquatk that is called whenever the app |
| 64 | # receives a file open event. The callback can have multiple arguments, |
| 65 | # one for every file that should be opened. |
| 66 | root.createcommand("::tk::mac::OpenDocument", doOpenFile) |
| 67 | |
| 68 | def hideTkConsole(root): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 69 | try: |
| 70 | root.tk.call('console', 'hide') |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 71 | except tkinter.TclError: |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 72 | # Some versions of the Tk framework don't have a console object |
| 73 | pass |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 74 | |
| 75 | def overrideRootMenu(root, flist): |
| 76 | """ |
| 77 | Replace the Tk root menu by something that's more appropriate for |
| 78 | IDLE. |
| 79 | """ |
| 80 | # The menu that is attached to the Tk root (".") is also used by AquaTk for |
| 81 | # all windows that don't specify a menu of their own. The default menubar |
| 82 | # contains a number of menus, none of which are appropriate for IDLE. The |
| 83 | # Most annoying of those is an 'About Tck/Tk...' menu in the application |
| 84 | # menu. |
| 85 | # |
| 86 | # This function replaces the default menubar by a mostly empty one, it |
| 87 | # should only contain the correct application menu and the window menu. |
| 88 | # |
| 89 | # Due to a (mis-)feature of TkAqua the user will also see an empty Help |
| 90 | # menu. |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 91 | from tkinter import Menu, Text, Text |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 92 | from idlelib.EditorWindow import prepstr, get_accelerator |
| 93 | from idlelib import Bindings |
| 94 | from idlelib import WindowList |
| 95 | from idlelib.MultiCall import MultiCallCreator |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 96 | |
| 97 | menubar = Menu(root) |
| 98 | root.configure(menu=menubar) |
| 99 | menudict = {} |
| 100 | |
| 101 | menudict['windows'] = menu = Menu(menubar, name='windows') |
| 102 | menubar.add_cascade(label='Window', menu=menu, underline=0) |
| 103 | |
| 104 | def postwindowsmenu(menu=menu): |
| 105 | end = menu.index('end') |
| 106 | if end is None: |
| 107 | end = -1 |
| 108 | |
| 109 | if end > 0: |
| 110 | menu.delete(0, end) |
| 111 | WindowList.add_windows_to_menu(menu) |
| 112 | WindowList.register_callback(postwindowsmenu) |
| 113 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 114 | def about_dialog(event=None): |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 115 | from idlelib import aboutDialog |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 116 | aboutDialog.AboutDialog(root, 'About IDLE') |
| 117 | |
| 118 | def config_dialog(event=None): |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 119 | from idlelib import configDialog |
Ronald Oussoren | 220a9fb | 2009-05-26 18:41:00 +0000 | [diff] [blame] | 120 | |
| 121 | # Ensure that the root object has an instance_dict attribute, |
| 122 | # mirrors code in EditorWindow (although that sets the attribute |
| 123 | # on an EditorWindow instance that is then passed as the first |
| 124 | # argument to ConfigDialog) |
| 125 | root.instance_dict = flist.inversedict |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 126 | root.instance_dict = flist.inversedict |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 127 | configDialog.ConfigDialog(root, 'Settings') |
| 128 | |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 129 | def help_dialog(event=None): |
| 130 | from idlelib import textView |
| 131 | fn = path.join(path.abspath(path.dirname(__file__)), 'help.txt') |
| 132 | textView.view_file(root, 'Help', fn) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 133 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 134 | root.bind('<<about-idle>>', about_dialog) |
| 135 | root.bind('<<open-config-dialog>>', config_dialog) |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 136 | root.createcommand('::tk::mac::ShowPreferences', config_dialog) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 137 | if flist: |
| 138 | root.bind('<<close-all-windows>>', flist.close_all_callback) |
| 139 | |
Ronald Oussoren | 10e05e1 | 2010-12-07 15:28:10 +0000 | [diff] [blame] | 140 | # The binding above doesn't reliably work on all versions of Tk |
| 141 | # on MacOSX. Adding command definition below does seem to do the |
| 142 | # right thing for now. |
| 143 | root.createcommand('exit', flist.close_all_callback) |
| 144 | |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 145 | if isCarbonAquaTk(root): |
| 146 | # for Carbon AquaTk, replace the default Tk apple menu |
| 147 | menudict['application'] = menu = Menu(menubar, name='apple') |
| 148 | menubar.add_cascade(label='IDLE', menu=menu) |
| 149 | Bindings.menudefs.insert(0, |
| 150 | ('application', [ |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 151 | ('About IDLE', '<<about-idle>>'), |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 152 | None, |
| 153 | ])) |
| 154 | tkversion = root.tk.eval('info patchlevel') |
| 155 | if tuple(map(int, tkversion.split('.'))) < (8, 4, 14): |
| 156 | # for earlier AquaTk versions, supply a Preferences menu item |
| 157 | Bindings.menudefs[0][1].append( |
| 158 | ('_Preferences....', '<<open-config-dialog>>'), |
| 159 | ) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 160 | else: |
Georg Brandl | aedd289 | 2010-12-19 10:10:32 +0000 | [diff] [blame] | 161 | # assume Cocoa AquaTk |
| 162 | # replace default About dialog with About IDLE one |
| 163 | root.createcommand('tkAboutDialog', about_dialog) |
| 164 | # replace default "Help" item in Help menu |
| 165 | root.createcommand('::tk::mac::ShowHelp', help_dialog) |
| 166 | # remove redundant "IDLE Help" from menu |
| 167 | del Bindings.menudefs[-1][1][0] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 168 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 169 | def setupApp(root, flist): |
| 170 | """ |
| 171 | Perform setup for the OSX application bundle. |
| 172 | """ |
| 173 | if not runningAsOSXApp(): return |
| 174 | |
| 175 | hideTkConsole(root) |
| 176 | overrideRootMenu(root, flist) |
| 177 | addOpenEventSupport(root, flist) |