blob: 279b1cc18ef19215212ce3267bbd9393dda6fe7b [file] [log] [blame]
Tim Peters231c3c82006-06-11 19:43:49 +00001"""
2A number of function that enhance IDLE on MacOSX when it used as a normal
3GUI application (as opposed to an X11 application).
4"""
5import sys
Georg Brandl6634bf22008-05-20 07:13:37 +00006import Tkinter
Tim Peters231c3c82006-06-11 19:43:49 +00007
8def 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
12def 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
26def hideTkConsole(root):
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +000027 try:
28 root.tk.call('console', 'hide')
Georg Brandl6634bf22008-05-20 07:13:37 +000029 except Tkinter.TclError:
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +000030 # Some versions of the Tk framework don't have a console object
31 pass
Tim Peters231c3c82006-06-11 19:43:49 +000032
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000033def overrideRootMenu(root, flist):
34 """
35 Replace the Tk root menu by something that's more appropriate for
36 IDLE.
37 """
Tim Peters0bbfd832006-07-24 21:02:15 +000038 # The menu that is attached to the Tk root (".") is also used by AquaTk for
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000039 # 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.
Georg Brandl6634bf22008-05-20 07:13:37 +000049 from Tkinter import Menu, Text, Text
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000050 from EditorWindow import prepstr, get_accelerator
51 import Bindings
52 import WindowList
53 from MultiCall import MultiCallCreator
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):
76 import aboutDialog
77 aboutDialog.AboutDialog(root, 'About IDLE')
78
79 def config_dialog(event=None):
80 import configDialog
81 configDialog.ConfigDialog(root, 'Settings')
82
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +000083
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000084 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
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +000089
90 ###check if Tk version >= 8.4.14; if so, use hard-coded showprefs binding
91 tkversion = root.tk.eval('info patchlevel')
Ronald Oussoren8c954842009-01-02 12:59:32 +000092 # Note: we cannot check if the string tkversion >= '8.4.14', because
93 # the string '8.4.7' is greater than the string '8.4.14'.
94 if map(int, tkversion.split('.')) >= (8, 4, 14):
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +000095 Bindings.menudefs[0] = ('application', [
96 ('About IDLE', '<<about-idle>>'),
97 None,
98 ])
99 root.createcommand('::tk::mac::ShowPreferences', config_dialog)
100 else:
101 for mname, entrylist in Bindings.menudefs:
102 menu = menudict.get(mname)
103 if not menu:
104 continue
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000105 else:
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +0000106 for entry in entrylist:
107 if not entry:
108 menu.add_separator()
109 else:
110 label, eventname = entry
111 underline, label = prepstr(label)
112 accelerator = get_accelerator(Bindings.default_keydefs,
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000113 eventname)
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +0000114 def command(text=root, eventname=eventname):
115 text.event_generate(eventname)
116 menu.add_command(label=label, underline=underline,
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000117 command=command, accelerator=accelerator)
118
Tim Peters231c3c82006-06-11 19:43:49 +0000119def setupApp(root, flist):
120 """
121 Perform setup for the OSX application bundle.
122 """
123 if not runningAsOSXApp(): return
124
125 hideTkConsole(root)
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000126 overrideRootMenu(root, flist)
Tim Peters231c3c82006-06-11 19:43:49 +0000127 addOpenEventSupport(root, flist)