blob: 8f86ca69280a7f9757501fe4b12885c4d8c5fb42 [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():
Ronald Oussorena97063a2009-03-04 21:35:05 +00009 """
10 Returns True if Python is running from within an app on OSX.
11 If so, assume that Python was built with Aqua Tcl/Tk rather than
Guilherme Polo175e0bf2009-08-05 23:48:26 +000012 X11 Tcl/Tk.
Ronald Oussorena97063a2009-03-04 21:35:05 +000013 """
14 return (sys.platform == 'darwin' and '.app' in sys.executable)
Tim Peters231c3c82006-06-11 19:43:49 +000015
16def addOpenEventSupport(root, flist):
17 """
18 This ensures that the application will respont to open AppleEvents, which
19 makes is feaseable to use IDLE as the default application for python files.
20 """
21 def doOpenFile(*args):
22 for fn in args:
23 flist.open(fn)
24
25 # The command below is a hook in aquatk that is called whenever the app
26 # receives a file open event. The callback can have multiple arguments,
27 # one for every file that should be opened.
28 root.createcommand("::tk::mac::OpenDocument", doOpenFile)
29
30def hideTkConsole(root):
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +000031 try:
32 root.tk.call('console', 'hide')
Georg Brandl6634bf22008-05-20 07:13:37 +000033 except Tkinter.TclError:
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +000034 # Some versions of the Tk framework don't have a console object
35 pass
Tim Peters231c3c82006-06-11 19:43:49 +000036
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000037def overrideRootMenu(root, flist):
38 """
39 Replace the Tk root menu by something that's more appropriate for
40 IDLE.
41 """
Tim Peters0bbfd832006-07-24 21:02:15 +000042 # The menu that is attached to the Tk root (".") is also used by AquaTk for
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000043 # all windows that don't specify a menu of their own. The default menubar
44 # contains a number of menus, none of which are appropriate for IDLE. The
45 # Most annoying of those is an 'About Tck/Tk...' menu in the application
46 # menu.
47 #
48 # This function replaces the default menubar by a mostly empty one, it
49 # should only contain the correct application menu and the window menu.
50 #
51 # Due to a (mis-)feature of TkAqua the user will also see an empty Help
52 # menu.
Georg Brandl6634bf22008-05-20 07:13:37 +000053 from Tkinter import Menu, Text, Text
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000054 from EditorWindow import prepstr, get_accelerator
55 import Bindings
56 import WindowList
57 from MultiCall import MultiCallCreator
58
59 menubar = Menu(root)
60 root.configure(menu=menubar)
61 menudict = {}
62
63 menudict['windows'] = menu = Menu(menubar, name='windows')
64 menubar.add_cascade(label='Window', menu=menu, underline=0)
65
66 def postwindowsmenu(menu=menu):
67 end = menu.index('end')
68 if end is None:
69 end = -1
70
71 if end > 0:
72 menu.delete(0, end)
73 WindowList.add_windows_to_menu(menu)
74 WindowList.register_callback(postwindowsmenu)
75
76 menudict['application'] = menu = Menu(menubar, name='apple')
77 menubar.add_cascade(label='IDLE', menu=menu)
78
79 def about_dialog(event=None):
80 import aboutDialog
81 aboutDialog.AboutDialog(root, 'About IDLE')
82
83 def config_dialog(event=None):
84 import configDialog
Ronald Oussoren55d88282009-05-26 18:44:48 +000085 root.instance_dict = flist.inversedict
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000086 configDialog.ConfigDialog(root, 'Settings')
87
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +000088
Ronald Oussoren8133f9d2006-07-23 09:46:11 +000089 root.bind('<<about-idle>>', about_dialog)
90 root.bind('<<open-config-dialog>>', config_dialog)
91 if flist:
92 root.bind('<<close-all-windows>>', flist.close_all_callback)
93
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +000094
95 ###check if Tk version >= 8.4.14; if so, use hard-coded showprefs binding
96 tkversion = root.tk.eval('info patchlevel')
Ronald Oussoren8c954842009-01-02 12:59:32 +000097 # Note: we cannot check if the string tkversion >= '8.4.14', because
98 # the string '8.4.7' is greater than the string '8.4.14'.
Ronald Oussoren55d88282009-05-26 18:44:48 +000099 if tuple(map(int, tkversion.split('.'))) >= (8, 4, 14):
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +0000100 Bindings.menudefs[0] = ('application', [
101 ('About IDLE', '<<about-idle>>'),
102 None,
103 ])
104 root.createcommand('::tk::mac::ShowPreferences', config_dialog)
105 else:
106 for mname, entrylist in Bindings.menudefs:
107 menu = menudict.get(mname)
108 if not menu:
109 continue
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000110 else:
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +0000111 for entry in entrylist:
112 if not entry:
113 menu.add_separator()
114 else:
115 label, eventname = entry
116 underline, label = prepstr(label)
117 accelerator = get_accelerator(Bindings.default_keydefs,
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000118 eventname)
Ronald Oussoren9b0bcc12007-07-09 06:02:21 +0000119 def command(text=root, eventname=eventname):
120 text.event_generate(eventname)
121 menu.add_command(label=label, underline=underline,
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000122 command=command, accelerator=accelerator)
123
Tim Peters231c3c82006-06-11 19:43:49 +0000124def setupApp(root, flist):
125 """
126 Perform setup for the OSX application bundle.
127 """
128 if not runningAsOSXApp(): return
129
130 hideTkConsole(root)
Ronald Oussoren8133f9d2006-07-23 09:46:11 +0000131 overrideRootMenu(root, flist)
Tim Peters231c3c82006-06-11 19:43:49 +0000132 addOpenEventSupport(root, flist)