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