macosx.py 9.4 KB

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