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