Fix a bug in the ``compiler`` package that caused invalid code to be
[python/dscho.git] / Lib / idlelib / macosxSupport.py
blobad61fff46f4f3de8ed10f82503227b32f2a3af35
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
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])
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)
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)
25 def hideTkConsole(root):
26 root.tk.call('console', 'hide')
28 def overrideRootMenu(root, flist):
29 """
30 Replace the Tk root menu by something that's more appropriate for
31 IDLE.
32 """
33 # The menu that is attached to the Tk root (".") is also used by AquaTk for
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.
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.
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
50 menubar = Menu(root)
51 root.configure(menu=menubar)
52 menudict = {}
54 menudict['windows'] = menu = Menu(menubar, name='windows')
55 menubar.add_cascade(label='Window', menu=menu, underline=0)
57 def postwindowsmenu(menu=menu):
58 end = menu.index('end')
59 if end is None:
60 end = -1
62 if end > 0:
63 menu.delete(0, end)
64 WindowList.add_windows_to_menu(menu)
65 WindowList.register_callback(postwindowsmenu)
67 menudict['application'] = menu = Menu(menubar, name='apple')
68 menubar.add_cascade(label='IDLE', menu=menu)
70 def about_dialog(event=None):
71 import aboutDialog
72 aboutDialog.AboutDialog(root, 'About IDLE')
74 def config_dialog(event=None):
75 import configDialog
76 configDialog.ConfigDialog(root, 'Settings')
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)
83 for mname, entrylist in Bindings.menudefs:
84 menu = menudict.get(mname)
85 if not menu:
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)
93 accelerator = get_accelerator(Bindings.default_keydefs,
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)
104 def setupApp(root, flist):
106 Perform setup for the OSX application bundle.
108 if not runningAsOSXApp(): return
110 hideTkConsole(root)
111 overrideRootMenu(root, flist)
112 addOpenEventSupport(root, flist)