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

Python code coverage for Lib/idlelib/macosx.py

#countcontent
1n/a"""
2n/aA number of functions that enhance IDLE on Mac OSX.
3n/a"""
4n/afrom sys import platform # Used in _init_tk_type, changed by test.
5n/a
6n/aimport tkinter
7n/a
8n/a
9n/a## Define functions that query the Mac graphics type.
10n/a## _tk_type and its initializer are private to this section.
11n/a
12n/a_tk_type = None
13n/a
14n/adef _init_tk_type():
15n/a """
16n/a Initializes OS X Tk variant values for
17n/a isAquaTk(), isCarbonTk(), isCocoaTk(), and isXQuartz().
18n/a """
19n/a global _tk_type
20n/a if platform == 'darwin':
21n/a root = tkinter.Tk()
22n/a ws = root.tk.call('tk', 'windowingsystem')
23n/a if 'x11' in ws:
24n/a _tk_type = "xquartz"
25n/a elif 'aqua' not in ws:
26n/a _tk_type = "other"
27n/a elif 'AppKit' in root.tk.call('winfo', 'server', '.'):
28n/a _tk_type = "cocoa"
29n/a else:
30n/a _tk_type = "carbon"
31n/a root.destroy()
32n/a else:
33n/a _tk_type = "other"
34n/a
35n/adef isAquaTk():
36n/a """
37n/a Returns True if IDLE is using a native OS X Tk (Cocoa or Carbon).
38n/a """
39n/a if not _tk_type:
40n/a _init_tk_type()
41n/a return _tk_type == "cocoa" or _tk_type == "carbon"
42n/a
43n/adef isCarbonTk():
44n/a """
45n/a Returns True if IDLE is using a Carbon Aqua Tk (instead of the
46n/a newer Cocoa Aqua Tk).
47n/a """
48n/a if not _tk_type:
49n/a _init_tk_type()
50n/a return _tk_type == "carbon"
51n/a
52n/adef isCocoaTk():
53n/a """
54n/a Returns True if IDLE is using a Cocoa Aqua Tk.
55n/a """
56n/a if not _tk_type:
57n/a _init_tk_type()
58n/a return _tk_type == "cocoa"
59n/a
60n/adef isXQuartz():
61n/a """
62n/a Returns True if IDLE is using an OS X X11 Tk.
63n/a """
64n/a if not _tk_type:
65n/a _init_tk_type()
66n/a return _tk_type == "xquartz"
67n/a
68n/a
69n/adef tkVersionWarning(root):
70n/a """
71n/a Returns a string warning message if the Tk version in use appears to
72n/a be one known to cause problems with IDLE.
73n/a 1. Apple Cocoa-based Tk 8.5.7 shipped with Mac OS X 10.6 is unusable.
74n/a 2. Apple Cocoa-based Tk 8.5.9 in OS X 10.7 and 10.8 is better but
75n/a can still crash unexpectedly.
76n/a """
77n/a
78n/a if isCocoaTk():
79n/a patchlevel = root.tk.call('info', 'patchlevel')
80n/a if patchlevel not in ('8.5.7', '8.5.9'):
81n/a return False
82n/a return (r"WARNING: The version of Tcl/Tk ({0}) in use may"
83n/a r" be unstable.\n"
84n/a r"Visit http://www.python.org/download/mac/tcltk/"
85n/a r" for current information.".format(patchlevel))
86n/a else:
87n/a return False
88n/a
89n/a
90n/a## Fix the menu and related functions.
91n/a
92n/adef addOpenEventSupport(root, flist):
93n/a """
94n/a This ensures that the application will respond to open AppleEvents, which
95n/a makes is feasible to use IDLE as the default application for python files.
96n/a """
97n/a def doOpenFile(*args):
98n/a for fn in args:
99n/a flist.open(fn)
100n/a
101n/a # The command below is a hook in aquatk that is called whenever the app
102n/a # receives a file open event. The callback can have multiple arguments,
103n/a # one for every file that should be opened.
104n/a root.createcommand("::tk::mac::OpenDocument", doOpenFile)
105n/a
106n/adef hideTkConsole(root):
107n/a try:
108n/a root.tk.call('console', 'hide')
109n/a except tkinter.TclError:
110n/a # Some versions of the Tk framework don't have a console object
111n/a pass
112n/a
113n/adef overrideRootMenu(root, flist):
114n/a """
115n/a Replace the Tk root menu by something that is more appropriate for
116n/a IDLE with an Aqua Tk.
117n/a """
118n/a # The menu that is attached to the Tk root (".") is also used by AquaTk for
119n/a # all windows that don't specify a menu of their own. The default menubar
120n/a # contains a number of menus, none of which are appropriate for IDLE. The
121n/a # Most annoying of those is an 'About Tck/Tk...' menu in the application
122n/a # menu.
123n/a #
124n/a # This function replaces the default menubar by a mostly empty one, it
125n/a # should only contain the correct application menu and the window menu.
126n/a #
127n/a # Due to a (mis-)feature of TkAqua the user will also see an empty Help
128n/a # menu.
129n/a from tkinter import Menu
130n/a from idlelib import mainmenu
131n/a from idlelib import windows
132n/a
133n/a closeItem = mainmenu.menudefs[0][1][-2]
134n/a
135n/a # Remove the last 3 items of the file menu: a separator, close window and
136n/a # quit. Close window will be reinserted just above the save item, where
137n/a # it should be according to the HIG. Quit is in the application menu.
138n/a del mainmenu.menudefs[0][1][-3:]
139n/a mainmenu.menudefs[0][1].insert(6, closeItem)
140n/a
141n/a # Remove the 'About' entry from the help menu, it is in the application
142n/a # menu
143n/a del mainmenu.menudefs[-1][1][0:2]
144n/a # Remove the 'Configure Idle' entry from the options menu, it is in the
145n/a # application menu as 'Preferences'
146n/a del mainmenu.menudefs[-2][1][0]
147n/a menubar = Menu(root)
148n/a root.configure(menu=menubar)
149n/a menudict = {}
150n/a
151n/a menudict['windows'] = menu = Menu(menubar, name='windows', tearoff=0)
152n/a menubar.add_cascade(label='Window', menu=menu, underline=0)
153n/a
154n/a def postwindowsmenu(menu=menu):
155n/a end = menu.index('end')
156n/a if end is None:
157n/a end = -1
158n/a
159n/a if end > 0:
160n/a menu.delete(0, end)
161n/a windows.add_windows_to_menu(menu)
162n/a windows.register_callback(postwindowsmenu)
163n/a
164n/a def about_dialog(event=None):
165n/a "Handle Help 'About IDLE' event."
166n/a # Synchronize with editor.EditorWindow.about_dialog.
167n/a from idlelib import help_about
168n/a help_about.AboutDialog(root, 'About IDLE')
169n/a
170n/a def config_dialog(event=None):
171n/a "Handle Options 'Configure IDLE' event."
172n/a # Synchronize with editor.EditorWindow.config_dialog.
173n/a from idlelib import configdialog
174n/a
175n/a # Ensure that the root object has an instance_dict attribute,
176n/a # mirrors code in EditorWindow (although that sets the attribute
177n/a # on an EditorWindow instance that is then passed as the first
178n/a # argument to ConfigDialog)
179n/a root.instance_dict = flist.inversedict
180n/a configdialog.ConfigDialog(root, 'Settings')
181n/a
182n/a def help_dialog(event=None):
183n/a "Handle Help 'IDLE Help' event."
184n/a # Synchronize with editor.EditorWindow.help_dialog.
185n/a from idlelib import help
186n/a help.show_idlehelp(root)
187n/a
188n/a root.bind('<<about-idle>>', about_dialog)
189n/a root.bind('<<open-config-dialog>>', config_dialog)
190n/a root.createcommand('::tk::mac::ShowPreferences', config_dialog)
191n/a if flist:
192n/a root.bind('<<close-all-windows>>', flist.close_all_callback)
193n/a
194n/a # The binding above doesn't reliably work on all versions of Tk
195n/a # on MacOSX. Adding command definition below does seem to do the
196n/a # right thing for now.
197n/a root.createcommand('exit', flist.close_all_callback)
198n/a
199n/a if isCarbonTk():
200n/a # for Carbon AquaTk, replace the default Tk apple menu
201n/a menudict['application'] = menu = Menu(menubar, name='apple',
202n/a tearoff=0)
203n/a menubar.add_cascade(label='IDLE', menu=menu)
204n/a mainmenu.menudefs.insert(0,
205n/a ('application', [
206n/a ('About IDLE', '<<about-idle>>'),
207n/a None,
208n/a ]))
209n/a if isCocoaTk():
210n/a # replace default About dialog with About IDLE one
211n/a root.createcommand('tkAboutDialog', about_dialog)
212n/a # replace default "Help" item in Help menu
213n/a root.createcommand('::tk::mac::ShowHelp', help_dialog)
214n/a # remove redundant "IDLE Help" from menu
215n/a del mainmenu.menudefs[-1][1][0]
216n/a
217n/adef fixb2context(root):
218n/a '''Removed bad AquaTk Button-2 (right) and Paste bindings.
219n/a
220n/a They prevent context menu access and seem to be gone in AquaTk8.6.
221n/a See issue #24801.
222n/a '''
223n/a root.unbind_class('Text', '<B2>')
224n/a root.unbind_class('Text', '<B2-Motion>')
225n/a root.unbind_class('Text', '<<PasteSelection>>')
226n/a
227n/adef setupApp(root, flist):
228n/a """
229n/a Perform initial OS X customizations if needed.
230n/a Called from pyshell.main() after initial calls to Tk()
231n/a
232n/a There are currently three major versions of Tk in use on OS X:
233n/a 1. Aqua Cocoa Tk (native default since OS X 10.6)
234n/a 2. Aqua Carbon Tk (original native, 32-bit only, deprecated)
235n/a 3. X11 (supported by some third-party distributors, deprecated)
236n/a There are various differences among the three that affect IDLE
237n/a behavior, primarily with menus, mouse key events, and accelerators.
238n/a Some one-time customizations are performed here.
239n/a Others are dynamically tested throughout idlelib by calls to the
240n/a isAquaTk(), isCarbonTk(), isCocoaTk(), isXQuartz() functions which
241n/a are initialized here as well.
242n/a """
243n/a if isAquaTk():
244n/a hideTkConsole(root)
245n/a overrideRootMenu(root, flist)
246n/a addOpenEventSupport(root, flist)
247n/a fixb2context(root)
248n/a
249n/a
250n/aif __name__ == '__main__':
251n/a from unittest import main
252n/a main('idlelib.idle_test.test_macosx', verbosity=2)