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