ยปCore Development>Code coverage>Lib/idlelib/macosxSupport.py

Python code coverage for Lib/idlelib/macosxSupport.py

#countcontent
1n/a"""
2n/aA number of function that enhance IDLE on MacOSX when it used as a normal
3n/aGUI application (as opposed to an X11 application).
4n/a"""
5n/aimport sys
6n/aimport tkinter
7n/afrom os import path
8n/a
9n/a
10n/a_appbundle = None
11n/a
12n/adef runningAsOSXApp():
13n/a """
14n/a Returns True if Python is running from within an app on OSX.
15n/a If so, the various OS X customizations will be triggered later (menu
16n/a fixup, et al). (Originally, this test was supposed to condition
17n/a behavior on whether IDLE was running under Aqua Tk rather than
18n/a under X11 Tk but that does not work since a framework build
19n/a could be linked with X11. For several releases, this test actually
20n/a differentiates between whether IDLE is running from a framework or
21n/a not. As a future enhancement, it should be considered whether there
22n/a should be a difference based on framework and any needed X11 adaptions
23n/a should be made dependent on a new function that actually tests for X11.)
24n/a """
25n/a global _appbundle
26n/a if _appbundle is None:
27n/a _appbundle = sys.platform == 'darwin'
28n/a if _appbundle:
29n/a import sysconfig
30n/a _appbundle = bool(sysconfig.get_config_var('PYTHONFRAMEWORK'))
31n/a return _appbundle
32n/a
33n/a_carbonaquatk = None
34n/a
35n/adef isCarbonAquaTk(root):
36n/a """
37n/a Returns True if IDLE is using a Carbon Aqua Tk (instead of the
38n/a newer Cocoa Aqua Tk).
39n/a """
40n/a global _carbonaquatk
41n/a if _carbonaquatk is None:
42n/a _carbonaquatk = (runningAsOSXApp() and
43n/a 'aqua' in root.tk.call('tk', 'windowingsystem') and
44n/a 'AppKit' not in root.tk.call('winfo', 'server', '.'))
45n/a return _carbonaquatk
46n/a
47n/adef tkVersionWarning(root):
48n/a """
49n/a Returns a string warning message if the Tk version in use appears to
50n/a be one known to cause problems with IDLE.
51n/a 1. Apple Cocoa-based Tk 8.5.7 shipped with Mac OS X 10.6 is unusable.
52n/a 2. Apple Cocoa-based Tk 8.5.9 in OS X 10.7 and 10.8 is better but
53n/a can still crash unexpectedly.
54n/a """
55n/a
56n/a if (runningAsOSXApp() and
57n/a ('AppKit' in root.tk.call('winfo', 'server', '.')) ):
58n/a patchlevel = root.tk.call('info', 'patchlevel')
59n/a if patchlevel not in ('8.5.7', '8.5.9'):
60n/a return False
61n/a return (r"WARNING: The version of Tcl/Tk ({0}) in use may"
62n/a r" be unstable.\n"
63n/a r"Visit http://www.python.org/download/mac/tcltk/"
64n/a r" for current information.".format(patchlevel))
65n/a else:
66n/a return False
67n/a
68n/adef addOpenEventSupport(root, flist):
69n/a """
70n/a This ensures that the application will respond to open AppleEvents, which
71n/a makes is feasible to use IDLE as the default application for python files.
72n/a """
73n/a def doOpenFile(*args):
74n/a for fn in args:
75n/a flist.open(fn)
76n/a
77n/a # The command below is a hook in aquatk that is called whenever the app
78n/a # receives a file open event. The callback can have multiple arguments,
79n/a # one for every file that should be opened.
80n/a root.createcommand("::tk::mac::OpenDocument", doOpenFile)
81n/a
82n/adef hideTkConsole(root):
83n/a try:
84n/a root.tk.call('console', 'hide')
85n/a except tkinter.TclError:
86n/a # Some versions of the Tk framework don't have a console object
87n/a pass
88n/a
89n/adef overrideRootMenu(root, flist):
90n/a """
91n/a Replace the Tk root menu by something that's more appropriate for
92n/a IDLE.
93n/a """
94n/a # The menu that is attached to the Tk root (".") is also used by AquaTk for
95n/a # all windows that don't specify a menu of their own. The default menubar
96n/a # contains a number of menus, none of which are appropriate for IDLE. The
97n/a # Most annoying of those is an 'About Tck/Tk...' menu in the application
98n/a # menu.
99n/a #
100n/a # This function replaces the default menubar by a mostly empty one, it
101n/a # should only contain the correct application menu and the window menu.
102n/a #
103n/a # Due to a (mis-)feature of TkAqua the user will also see an empty Help
104n/a # menu.
105n/a from tkinter import Menu, Text, Text
106n/a from idlelib.EditorWindow import prepstr, get_accelerator
107n/a from idlelib import Bindings
108n/a from idlelib import WindowList
109n/a from idlelib.MultiCall import MultiCallCreator
110n/a
111n/a menubar = Menu(root)
112n/a root.configure(menu=menubar)
113n/a menudict = {}
114n/a
115n/a menudict['windows'] = menu = Menu(menubar, name='windows')
116n/a menubar.add_cascade(label='Window', menu=menu, underline=0)
117n/a
118n/a def postwindowsmenu(menu=menu):
119n/a end = menu.index('end')
120n/a if end is None:
121n/a end = -1
122n/a
123n/a if end > 0:
124n/a menu.delete(0, end)
125n/a WindowList.add_windows_to_menu(menu)
126n/a WindowList.register_callback(postwindowsmenu)
127n/a
128n/a def about_dialog(event=None):
129n/a from idlelib import aboutDialog
130n/a aboutDialog.AboutDialog(root, 'About IDLE')
131n/a
132n/a def config_dialog(event=None):
133n/a from idlelib import configDialog
134n/a
135n/a # Ensure that the root object has an instance_dict attribute,
136n/a # mirrors code in EditorWindow (although that sets the attribute
137n/a # on an EditorWindow instance that is then passed as the first
138n/a # argument to ConfigDialog)
139n/a root.instance_dict = flist.inversedict
140n/a root.instance_dict = flist.inversedict
141n/a configDialog.ConfigDialog(root, 'Settings')
142n/a
143n/a def help_dialog(event=None):
144n/a from idlelib import textView
145n/a fn = path.join(path.abspath(path.dirname(__file__)), 'help.txt')
146n/a textView.view_file(root, 'Help', fn)
147n/a
148n/a root.bind('<<about-idle>>', about_dialog)
149n/a root.bind('<<open-config-dialog>>', config_dialog)
150n/a root.createcommand('::tk::mac::ShowPreferences', config_dialog)
151n/a if flist:
152n/a root.bind('<<close-all-windows>>', flist.close_all_callback)
153n/a
154n/a # The binding above doesn't reliably work on all versions of Tk
155n/a # on MacOSX. Adding command definition below does seem to do the
156n/a # right thing for now.
157n/a root.createcommand('exit', flist.close_all_callback)
158n/a
159n/a if isCarbonAquaTk(root):
160n/a # for Carbon AquaTk, replace the default Tk apple menu
161n/a menudict['application'] = menu = Menu(menubar, name='apple')
162n/a menubar.add_cascade(label='IDLE', menu=menu)
163n/a Bindings.menudefs.insert(0,
164n/a ('application', [
165n/a ('About IDLE', '<<about-idle>>'),
166n/a None,
167n/a ]))
168n/a tkversion = root.tk.eval('info patchlevel')
169n/a if tuple(map(int, tkversion.split('.'))) < (8, 4, 14):
170n/a # for earlier AquaTk versions, supply a Preferences menu item
171n/a Bindings.menudefs[0][1].append(
172n/a ('_Preferences....', '<<open-config-dialog>>'),
173n/a )
174n/a else:
175n/a # assume Cocoa AquaTk
176n/a # replace default About dialog with About IDLE one
177n/a root.createcommand('tkAboutDialog', about_dialog)
178n/a # replace default "Help" item in Help menu
179n/a root.createcommand('::tk::mac::ShowHelp', help_dialog)
180n/a # remove redundant "IDLE Help" from menu
181n/a del Bindings.menudefs[-1][1][0]
182n/a
183n/adef setupApp(root, flist):
184n/a """
185n/a Perform setup for the OSX application bundle.
186n/a """
187n/a if not runningAsOSXApp(): return
188n/a
189n/a hideTkConsole(root)
190n/a overrideRootMenu(root, flist)
191n/a addOpenEventSupport(root, flist)