Credit Nir Aides for r77288
[python.git] / Lib / idlelib / macosxSupport.py
blob03a145870b780a41acd3dc6ca85e2952da6e5c58
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 import Tkinter
9 _appbundle = None
11 def runningAsOSXApp():
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
15 X11 Tcl/Tk.
16 """
17 global _appbundle
18 if _appbundle is None:
19 _appbundle = (sys.platform == 'darwin' and '.app' in sys.executable)
20 return _appbundle
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)
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)
36 def hideTkConsole(root):
37 try:
38 root.tk.call('console', 'hide')
39 except Tkinter.TclError:
40 # Some versions of the Tk framework don't have a console object
41 pass
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.
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.
57 # Due to a (mis-)feature of TkAqua the user will also see an empty Help
58 # menu.
59 from Tkinter import Menu, Text, Text
60 from EditorWindow import prepstr, get_accelerator
61 import Bindings
62 import WindowList
63 from MultiCall import MultiCallCreator
65 menubar = Menu(root)
66 root.configure(menu=menubar)
67 menudict = {}
69 menudict['windows'] = menu = Menu(menubar, name='windows')
70 menubar.add_cascade(label='Window', menu=menu, underline=0)
72 def postwindowsmenu(menu=menu):
73 end = menu.index('end')
74 if end is None:
75 end = -1
77 if end > 0:
78 menu.delete(0, end)
79 WindowList.add_windows_to_menu(menu)
80 WindowList.register_callback(postwindowsmenu)
82 menudict['application'] = menu = Menu(menubar, name='apple')
83 menubar.add_cascade(label='IDLE', menu=menu)
85 def about_dialog(event=None):
86 import aboutDialog
87 aboutDialog.AboutDialog(root, 'About IDLE')
89 def config_dialog(event=None):
90 import configDialog
91 root.instance_dict = flist.inversedict
92 configDialog.ConfigDialog(root, 'Settings')
95 root.bind('<<about-idle>>', about_dialog)
96 root.bind('<<open-config-dialog>>', config_dialog)
97 if flist:
98 root.bind('<<close-all-windows>>', flist.close_all_callback)
101 ###check if Tk version >= 8.4.14; if so, use hard-coded showprefs binding
102 tkversion = root.tk.eval('info patchlevel')
103 # Note: we cannot check if the string tkversion >= '8.4.14', because
104 # the string '8.4.7' is greater than the string '8.4.14'.
105 if tuple(map(int, tkversion.split('.'))) >= (8, 4, 14):
106 Bindings.menudefs[0] = ('application', [
107 ('About IDLE', '<<about-idle>>'),
108 None,
110 root.createcommand('::tk::mac::ShowPreferences', config_dialog)
111 else:
112 for mname, entrylist in Bindings.menudefs:
113 menu = menudict.get(mname)
114 if not menu:
115 continue
116 else:
117 for entry in entrylist:
118 if not entry:
119 menu.add_separator()
120 else:
121 label, eventname = entry
122 underline, label = prepstr(label)
123 accelerator = get_accelerator(Bindings.default_keydefs,
124 eventname)
125 def command(text=root, eventname=eventname):
126 text.event_generate(eventname)
127 menu.add_command(label=label, underline=underline,
128 command=command, accelerator=accelerator)
130 def setupApp(root, flist):
132 Perform setup for the OSX application bundle.
134 if not runningAsOSXApp(): return
136 hideTkConsole(root)
137 overrideRootMenu(root, flist)
138 addOpenEventSupport(root, flist)