Replace the localized min/max calls with normal if/else
[python.git] / Lib / idlelib / macosxSupport.py
blob98b35b6a65e024e0f6b4c063213de87504801d48
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
8 def runningAsOSXApp():
9 """
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
12 X11 Tck/Tk.
13 """
14 return (sys.platform == 'darwin' and '.app' in sys.executable)
16 def 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)
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)
30 def hideTkConsole(root):
31 try:
32 root.tk.call('console', 'hide')
33 except Tkinter.TclError:
34 # Some versions of the Tk framework don't have a console object
35 pass
37 def overrideRootMenu(root, flist):
38 """
39 Replace the Tk root menu by something that's more appropriate for
40 IDLE.
41 """
42 # The menu that is attached to the Tk root (".") is also used by AquaTk for
43 # 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.
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.
51 # Due to a (mis-)feature of TkAqua the user will also see an empty Help
52 # menu.
53 from Tkinter import Menu, Text, Text
54 from EditorWindow import prepstr, get_accelerator
55 import Bindings
56 import WindowList
57 from MultiCall import MultiCallCreator
59 menubar = Menu(root)
60 root.configure(menu=menubar)
61 menudict = {}
63 menudict['windows'] = menu = Menu(menubar, name='windows')
64 menubar.add_cascade(label='Window', menu=menu, underline=0)
66 def postwindowsmenu(menu=menu):
67 end = menu.index('end')
68 if end is None:
69 end = -1
71 if end > 0:
72 menu.delete(0, end)
73 WindowList.add_windows_to_menu(menu)
74 WindowList.register_callback(postwindowsmenu)
76 menudict['application'] = menu = Menu(menubar, name='apple')
77 menubar.add_cascade(label='IDLE', menu=menu)
79 def about_dialog(event=None):
80 import aboutDialog
81 aboutDialog.AboutDialog(root, 'About IDLE')
83 def config_dialog(event=None):
84 import configDialog
85 configDialog.ConfigDialog(root, 'Settings')
88 root.bind('<<about-idle>>', about_dialog)
89 root.bind('<<open-config-dialog>>', config_dialog)
90 if flist:
91 root.bind('<<close-all-windows>>', flist.close_all_callback)
94 ###check if Tk version >= 8.4.14; if so, use hard-coded showprefs binding
95 tkversion = root.tk.eval('info patchlevel')
96 # Note: we cannot check if the string tkversion >= '8.4.14', because
97 # the string '8.4.7' is greater than the string '8.4.14'.
98 if map(int, tkversion.split('.')) >= (8, 4, 14):
99 Bindings.menudefs[0] = ('application', [
100 ('About IDLE', '<<about-idle>>'),
101 None,
103 root.createcommand('::tk::mac::ShowPreferences', config_dialog)
104 else:
105 for mname, entrylist in Bindings.menudefs:
106 menu = menudict.get(mname)
107 if not menu:
108 continue
109 else:
110 for entry in entrylist:
111 if not entry:
112 menu.add_separator()
113 else:
114 label, eventname = entry
115 underline, label = prepstr(label)
116 accelerator = get_accelerator(Bindings.default_keydefs,
117 eventname)
118 def command(text=root, eventname=eventname):
119 text.event_generate(eventname)
120 menu.add_command(label=label, underline=underline,
121 command=command, accelerator=accelerator)
123 def setupApp(root, flist):
125 Perform setup for the OSX application bundle.
127 if not runningAsOSXApp(): return
129 hideTkConsole(root)
130 overrideRootMenu(root, flist)
131 addOpenEventSupport(root, flist)