macosxSupport.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. """
  2. A number of functions that enhance IDLE on Mac OSX.
  3. """
  4. import sys
  5. import Tkinter
  6. from os import path
  7. import warnings
  8. def runningAsOSXApp():
  9. warnings.warn("runningAsOSXApp() is deprecated, use isAquaTk()",
  10. DeprecationWarning, stacklevel=2)
  11. return isAquaTk()
  12. def isCarbonAquaTk(root):
  13. warnings.warn("isCarbonAquaTk(root) is deprecated, use isCarbonTk()",
  14. DeprecationWarning, stacklevel=2)
  15. return isCarbonTk()
  16. _tk_type = None
  17. def _initializeTkVariantTests(root):
  18. """
  19. Initializes OS X Tk variant values for
  20. isAquaTk(), isCarbonTk(), isCocoaTk(), and isXQuartz().
  21. """
  22. global _tk_type
  23. if sys.platform == 'darwin':
  24. ws = root.tk.call('tk', 'windowingsystem')
  25. if 'x11' in ws:
  26. _tk_type = "xquartz"
  27. elif 'aqua' not in ws:
  28. _tk_type = "other"
  29. elif 'AppKit' in root.tk.call('winfo', 'server', '.'):
  30. _tk_type = "cocoa"
  31. else:
  32. _tk_type = "carbon"
  33. else:
  34. _tk_type = "other"
  35. def isAquaTk():
  36. """
  37. Returns True if IDLE is using a native OS X Tk (Cocoa or Carbon).
  38. """
  39. assert _tk_type is not None
  40. return _tk_type == "cocoa" or _tk_type == "carbon"
  41. def isCarbonTk():
  42. """
  43. Returns True if IDLE is using a Carbon Aqua Tk (instead of the
  44. newer Cocoa Aqua Tk).
  45. """
  46. assert _tk_type is not None
  47. return _tk_type == "carbon"
  48. def isCocoaTk():
  49. """
  50. Returns True if IDLE is using a Cocoa Aqua Tk.
  51. """
  52. assert _tk_type is not None
  53. return _tk_type == "cocoa"
  54. def isXQuartz():
  55. """
  56. Returns True if IDLE is using an OS X X11 Tk.
  57. """
  58. assert _tk_type is not None
  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 (r"WARNING: The version of Tcl/Tk ({0}) in use may"
  73. r" be unstable.\n"
  74. r"Visit http://www.python.org/download/mac/tcltk/"
  75. r" for current information.".format(patchlevel))
  76. else:
  77. return False
  78. def addOpenEventSupport(root, flist):
  79. """
  80. This ensures that the application will respond to open AppleEvents, which
  81. makes is feasible to use IDLE as the default application for python files.
  82. """
  83. def doOpenFile(*args):
  84. for fn in args:
  85. flist.open(fn)
  86. # The command below is a hook in aquatk that is called whenever the app
  87. # receives a file open event. The callback can have multiple arguments,
  88. # one for every file that should be opened.
  89. root.createcommand("::tk::mac::OpenDocument", doOpenFile)
  90. def hideTkConsole(root):
  91. try:
  92. root.tk.call('console', 'hide')
  93. except Tkinter.TclError:
  94. # Some versions of the Tk framework don't have a console object
  95. pass
  96. def overrideRootMenu(root, flist):
  97. """
  98. Replace the Tk root menu by something that is more appropriate for
  99. IDLE with an Aqua Tk.
  100. """
  101. # The menu that is attached to the Tk root (".") is also used by AquaTk for
  102. # all windows that don't specify a menu of their own. The default menubar
  103. # contains a number of menus, none of which are appropriate for IDLE. The
  104. # Most annoying of those is an 'About Tck/Tk...' menu in the application
  105. # menu.
  106. #
  107. # This function replaces the default menubar by a mostly empty one, it
  108. # should only contain the correct application menu and the window menu.
  109. #
  110. # Due to a (mis-)feature of TkAqua the user will also see an empty Help
  111. # menu.
  112. from Tkinter import Menu
  113. from idlelib import Bindings
  114. from idlelib import WindowList
  115. closeItem = Bindings.menudefs[0][1][-2]
  116. # Remove the last 3 items of the file menu: a separator, close window and
  117. # quit. Close window will be reinserted just above the save item, where
  118. # it should be according to the HIG. Quit is in the application menu.
  119. del Bindings.menudefs[0][1][-3:]
  120. Bindings.menudefs[0][1].insert(6, closeItem)
  121. # Remove the 'About' entry from the help menu, it is in the application
  122. # menu
  123. del Bindings.menudefs[-1][1][0:2]
  124. # Remove the 'Configure Idle' entry from the options menu, it is in the
  125. # application menu as 'Preferences'
  126. del Bindings.menudefs[-2][1][0]
  127. menubar = Menu(root)
  128. root.configure(menu=menubar)
  129. menudict = {}
  130. menudict['windows'] = menu = Menu(menubar, name='windows', tearoff=0)
  131. menubar.add_cascade(label='Window', menu=menu, underline=0)
  132. def postwindowsmenu(menu=menu):
  133. end = menu.index('end')
  134. if end is None:
  135. end = -1
  136. if end > 0:
  137. menu.delete(0, end)
  138. WindowList.add_windows_to_menu(menu)
  139. WindowList.register_callback(postwindowsmenu)
  140. def about_dialog(event=None):
  141. "Handle Help 'About IDLE' event."
  142. # Synchronize with EditorWindow.EditorWindow.about_dialog.
  143. from idlelib import aboutDialog
  144. aboutDialog.AboutDialog(root, 'About IDLE')
  145. def config_dialog(event=None):
  146. "Handle Options 'Configure IDLE' event."
  147. # Synchronize with EditorWindow.EditorWindow.config_dialog.
  148. from idlelib import configDialog
  149. root.instance_dict = flist.inversedict
  150. configDialog.ConfigDialog(root, 'Settings')
  151. def help_dialog(event=None):
  152. "Handle Help 'IDLE Help' event."
  153. # Synchronize with EditorWindow.EditorWindow.help_dialog.
  154. from idlelib import help
  155. help.show_idlehelp(root)
  156. root.bind('<<about-idle>>', about_dialog)
  157. root.bind('<<open-config-dialog>>', config_dialog)
  158. root.createcommand('::tk::mac::ShowPreferences', config_dialog)
  159. if flist:
  160. root.bind('<<close-all-windows>>', flist.close_all_callback)
  161. # The binding above doesn't reliably work on all versions of Tk
  162. # on MacOSX. Adding command definition below does seem to do the
  163. # right thing for now.
  164. root.createcommand('exit', flist.close_all_callback)
  165. if isCarbonTk():
  166. # for Carbon AquaTk, replace the default Tk apple menu
  167. menudict['application'] = menu = Menu(menubar, name='apple',
  168. tearoff=0)
  169. menubar.add_cascade(label='IDLE', menu=menu)
  170. Bindings.menudefs.insert(0,
  171. ('application', [
  172. ('About IDLE', '<<about-idle>>'),
  173. None,
  174. ]))
  175. tkversion = root.tk.eval('info patchlevel')
  176. if tuple(map(int, tkversion.split('.'))) < (8, 4, 14):
  177. # for earlier AquaTk versions, supply a Preferences menu item
  178. Bindings.menudefs[0][1].append(
  179. ('_Preferences....', '<<open-config-dialog>>'),
  180. )
  181. if isCocoaTk():
  182. # replace default About dialog with About IDLE one
  183. root.createcommand('tkAboutDialog', about_dialog)
  184. # replace default "Help" item in Help menu
  185. root.createcommand('::tk::mac::ShowHelp', help_dialog)
  186. # remove redundant "IDLE Help" from menu
  187. del Bindings.menudefs[-1][1][0]
  188. def setupApp(root, flist):
  189. """
  190. Perform initial OS X customizations if needed.
  191. Called from PyShell.main() after initial calls to Tk()
  192. There are currently three major versions of Tk in use on OS X:
  193. 1. Aqua Cocoa Tk (native default since OS X 10.6)
  194. 2. Aqua Carbon Tk (original native, 32-bit only, deprecated)
  195. 3. X11 (supported by some third-party distributors, deprecated)
  196. There are various differences among the three that affect IDLE
  197. behavior, primarily with menus, mouse key events, and accelerators.
  198. Some one-time customizations are performed here.
  199. Others are dynamically tested throughout idlelib by calls to the
  200. isAquaTk(), isCarbonTk(), isCocoaTk(), isXQuartz() functions which
  201. are initialized here as well.
  202. """
  203. _initializeTkVariantTests(root)
  204. if isAquaTk():
  205. hideTkConsole(root)
  206. overrideRootMenu(root, flist)
  207. addOpenEventSupport(root, flist)