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 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 7 | |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 8 | |
| 9 | _appbundle = None |
| 10 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11 | def runningAsOSXApp(): |
Ronald Oussoren | 827822e | 2009-02-12 15:01:44 +0000 | [diff] [blame] | 12 | """ |
| 13 | Returns True if Python is running from within an app on OSX. |
| 14 | 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] | 15 | X11 Tcl/Tk. |
Ronald Oussoren | 827822e | 2009-02-12 15:01:44 +0000 | [diff] [blame] | 16 | """ |
Ronald Oussoren | 6f6c562 | 2009-12-24 14:03:19 +0000 | [diff] [blame] | 17 | global _appbundle |
| 18 | if _appbundle is None: |
| 19 | _appbundle = (sys.platform == 'darwin' and '.app' in sys.executable) |
| 20 | return _appbundle |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 21 | |
| 22 | def addOpenEventSupport(root, flist): |
| 23 | """ |
| 24 | This ensures that the application will respont to open AppleEvents, which |
| 25 | makes is feaseable to use IDLE as the default application for python files. |
| 26 | """ |
| 27 | def doOpenFile(*args): |
| 28 | for fn in args: |
| 29 | flist.open(fn) |
| 30 | |
| 31 | # The command below is a hook in aquatk that is called whenever the app |
| 32 | # receives a file open event. The callback can have multiple arguments, |
| 33 | # one for every file that should be opened. |
| 34 | root.createcommand("::tk::mac::OpenDocument", doOpenFile) |
| 35 | |
| 36 | def hideTkConsole(root): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 37 | try: |
| 38 | root.tk.call('console', 'hide') |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 39 | except tkinter.TclError: |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 40 | # Some versions of the Tk framework don't have a console object |
| 41 | pass |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 42 | |
| 43 | def overrideRootMenu(root, flist): |
| 44 | """ |
| 45 | Replace the Tk root menu by something that's more appropriate for |
| 46 | IDLE. |
| 47 | """ |
| 48 | # The menu that is attached to the Tk root (".") is also used by AquaTk for |
| 49 | # all windows that don't specify a menu of their own. The default menubar |
| 50 | # contains a number of menus, none of which are appropriate for IDLE. The |
| 51 | # Most annoying of those is an 'About Tck/Tk...' menu in the application |
| 52 | # menu. |
| 53 | # |
| 54 | # This function replaces the default menubar by a mostly empty one, it |
| 55 | # should only contain the correct application menu and the window menu. |
| 56 | # |
| 57 | # Due to a (mis-)feature of TkAqua the user will also see an empty Help |
| 58 | # menu. |
Georg Brandl | 14fc427 | 2008-05-17 18:39:55 +0000 | [diff] [blame] | 59 | from tkinter import Menu, Text, Text |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 60 | from idlelib.EditorWindow import prepstr, get_accelerator |
| 61 | from idlelib import Bindings |
| 62 | from idlelib import WindowList |
| 63 | from idlelib.MultiCall import MultiCallCreator |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | |
| 65 | menubar = Menu(root) |
| 66 | root.configure(menu=menubar) |
| 67 | menudict = {} |
| 68 | |
| 69 | menudict['windows'] = menu = Menu(menubar, name='windows') |
| 70 | menubar.add_cascade(label='Window', menu=menu, underline=0) |
| 71 | |
| 72 | def postwindowsmenu(menu=menu): |
| 73 | end = menu.index('end') |
| 74 | if end is None: |
| 75 | end = -1 |
| 76 | |
| 77 | if end > 0: |
| 78 | menu.delete(0, end) |
| 79 | WindowList.add_windows_to_menu(menu) |
| 80 | WindowList.register_callback(postwindowsmenu) |
| 81 | |
| 82 | menudict['application'] = menu = Menu(menubar, name='apple') |
| 83 | menubar.add_cascade(label='IDLE', menu=menu) |
| 84 | |
| 85 | def about_dialog(event=None): |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 86 | from idlelib import aboutDialog |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 87 | aboutDialog.AboutDialog(root, 'About IDLE') |
| 88 | |
| 89 | def config_dialog(event=None): |
Kurt B. Kaiser | 2d7f6a0 | 2007-08-22 23:01:33 +0000 | [diff] [blame] | 90 | from idlelib import configDialog |
Ronald Oussoren | 220a9fb | 2009-05-26 18:41:00 +0000 | [diff] [blame] | 91 | |
| 92 | # Ensure that the root object has an instance_dict attribute, |
| 93 | # mirrors code in EditorWindow (although that sets the attribute |
| 94 | # on an EditorWindow instance that is then passed as the first |
| 95 | # argument to ConfigDialog) |
| 96 | root.instance_dict = flist.inversedict |
Benjamin Peterson | fa0d703 | 2009-06-01 22:42:33 +0000 | [diff] [blame] | 97 | root.instance_dict = flist.inversedict |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 98 | configDialog.ConfigDialog(root, 'Settings') |
| 99 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 100 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 101 | root.bind('<<about-idle>>', about_dialog) |
| 102 | root.bind('<<open-config-dialog>>', config_dialog) |
| 103 | if flist: |
| 104 | root.bind('<<close-all-windows>>', flist.close_all_callback) |
| 105 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 106 | |
| 107 | ###check if Tk version >= 8.4.14; if so, use hard-coded showprefs binding |
| 108 | tkversion = root.tk.eval('info patchlevel') |
Ronald Oussoren | e9f8bf0 | 2009-01-02 13:10:34 +0000 | [diff] [blame] | 109 | # Note: we cannot check if the string tkversion >= '8.4.14', because |
| 110 | # the string '8.4.7' is greater than the string '8.4.14'. |
| 111 | if tuple(map(int, tkversion.split('.'))) >= (8, 4, 14): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 112 | Bindings.menudefs[0] = ('application', [ |
| 113 | ('About IDLE', '<<about-idle>>'), |
| 114 | None, |
| 115 | ]) |
| 116 | root.createcommand('::tk::mac::ShowPreferences', config_dialog) |
| 117 | else: |
| 118 | for mname, entrylist in Bindings.menudefs: |
| 119 | menu = menudict.get(mname) |
| 120 | if not menu: |
| 121 | continue |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 122 | else: |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 123 | for entry in entrylist: |
| 124 | if not entry: |
| 125 | menu.add_separator() |
| 126 | else: |
| 127 | label, eventname = entry |
| 128 | underline, label = prepstr(label) |
| 129 | accelerator = get_accelerator(Bindings.default_keydefs, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 130 | eventname) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 131 | def command(text=root, eventname=eventname): |
| 132 | text.event_generate(eventname) |
| 133 | menu.add_command(label=label, underline=underline, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 134 | command=command, accelerator=accelerator) |
| 135 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 136 | def setupApp(root, flist): |
| 137 | """ |
| 138 | Perform setup for the OSX application bundle. |
| 139 | """ |
| 140 | if not runningAsOSXApp(): return |
| 141 | |
| 142 | hideTkConsole(root) |
| 143 | overrideRootMenu(root, flist) |
| 144 | addOpenEventSupport(root, flist) |