[Python-checkins] r46859 - python/trunk/Lib/idlelib/Bindings.py python/trunk/Lib/idlelib/EditorWindow.py python/trunk/Lib/idlelib/PyShell.py python/trunk/Lib/idlelib/ZoomHeight.py python/trunk/Lib/idlelib/buildapp.py python/trunk/Lib/idlelib/configHandler.py python/trunk/Lib/idlelib/keybindingDialog.py python/trunk/Lib/idlelib/macosxSupport.py

ronald.oussoren python-checkins at python.org
Sun Jun 11 16:33:37 CEST 2006


Author: ronald.oussoren
Date: Sun Jun 11 16:33:36 2006
New Revision: 46859

Added:
   python/trunk/Lib/idlelib/macosxSupport.py
Removed:
   python/trunk/Lib/idlelib/buildapp.py
Modified:
   python/trunk/Lib/idlelib/Bindings.py
   python/trunk/Lib/idlelib/EditorWindow.py
   python/trunk/Lib/idlelib/PyShell.py
   python/trunk/Lib/idlelib/ZoomHeight.py
   python/trunk/Lib/idlelib/configHandler.py
   python/trunk/Lib/idlelib/keybindingDialog.py
Log:
This patch improves the L&F of IDLE on OSX. The changes are conditionalized on
being in an IDLE.app bundle on darwin. This does a slight reorganisation of the
menus and adds support for file-open events.


Modified: python/trunk/Lib/idlelib/Bindings.py
==============================================================================
--- python/trunk/Lib/idlelib/Bindings.py	(original)
+++ python/trunk/Lib/idlelib/Bindings.py	Sun Jun 11 16:33:36 2006
@@ -80,6 +80,32 @@
    ]),
 ]
 
+import sys
+if sys.platform == 'darwin' and '.app' in sys.executable:
+    # Running as a proper MacOS application bundle. This block restructures
+    # the menus a little to make them conform better to the HIG.
+
+    quitItem = menudefs[0][1][-1]
+    closeItem = menudefs[0][1][-2]
+
+    # Remove the last 3 items of the file menu: a separator, close window and
+    # quit. Close window will be reinserted just above the save item, where
+    # it should be according to the HIG. Quit is in the application menu.
+    del menudefs[0][1][-3:]
+    menudefs[0][1].insert(6, closeItem)
+
+    # Remove the 'About' entry from the help menu, it is in the application 
+    # menu
+    del menudefs[-1][1][0:2]
+
+    menudefs.insert(0,
+            ('application', [
+                ('About IDLE', '<<about-idle>>'),
+                None,
+                ('_Preferences....', '<<open-config-dialog>>'),
+            ]))
+
+
 default_keydefs = idleConf.GetCurrentKeySet()
 
 del sys

Modified: python/trunk/Lib/idlelib/EditorWindow.py
==============================================================================
--- python/trunk/Lib/idlelib/EditorWindow.py	(original)
+++ python/trunk/Lib/idlelib/EditorWindow.py	Sun Jun 11 16:33:36 2006
@@ -17,6 +17,7 @@
 import PyParse
 from configHandler import idleConf
 import aboutDialog, textView, configDialog
+import macosxSupport
 
 # The default tab setting for a Text widget, in average-width characters.
 TK_TABWIDTH_DEFAULT = 8
@@ -66,9 +67,18 @@
                                        'Python%d%d.chm' % sys.version_info[:2])
                 if os.path.isfile(chmfile):
                     dochome = chmfile
+
+            elif macosxSupport.runningAsOSXApp():
+                # documentation is stored inside the python framework
+                dochome = os.path.join(sys.prefix,
+                        'Resources/English.lproj/Documentation/index.html')
+
             dochome = os.path.normpath(dochome)
             if os.path.isfile(dochome):
                 EditorWindow.help_url = dochome
+                if sys.platform == 'darwin':
+                    # Safari requires real file:-URLs
+                    EditorWindow.help_url = 'file://' + EditorWindow.help_url
             else:
                 EditorWindow.help_url = "http://www.python.org/doc/current"
         currentTheme=idleConf.CurrentTheme()
@@ -278,6 +288,10 @@
 
     def set_status_bar(self):
         self.status_bar = self.MultiStatusBar(self.top)
+        if macosxSupport.runningAsOSXApp():
+            # Insert some padding to avoid obscuring some of the statusbar
+            # by the resize widget.
+            self.status_bar.set_label('_padding1', '    ', side=RIGHT)
         self.status_bar.set_label('column', 'Col: ?', side=RIGHT)
         self.status_bar.set_label('line', 'Ln: ?', side=RIGHT)
         self.status_bar.pack(side=BOTTOM, fill=X)
@@ -301,6 +315,11 @@
         ("help", "_Help"),
     ]
 
+    if macosxSupport.runningAsOSXApp():
+        del menu_specs[-3]
+        menu_specs[-2] = ("windows", "_Window")
+
+
     def createmenubar(self):
         mbar = self.menubar
         self.menudict = menudict = {}
@@ -308,6 +327,12 @@
             underline, label = prepstr(label)
             menudict[name] = menu = Menu(mbar, name=name)
             mbar.add_cascade(label=label, menu=menu, underline=underline)
+
+        if sys.platform == 'darwin' and '.framework' in sys.executable:
+            # Insert the application menu
+            menudict['application'] = menu = Menu(mbar, name='apple')
+            mbar.add_cascade(label='IDLE', menu=menu)
+
         self.fill_menus()
         self.base_helpmenu_length = self.menudict['help'].index(END)
         self.reset_help_menu_entries()

Modified: python/trunk/Lib/idlelib/PyShell.py
==============================================================================
--- python/trunk/Lib/idlelib/PyShell.py	(original)
+++ python/trunk/Lib/idlelib/PyShell.py	Sun Jun 11 16:33:36 2006
@@ -11,6 +11,7 @@
 import threading
 import traceback
 import types
+import macosxSupport
 
 import linecache
 from code import InteractiveInterpreter
@@ -777,6 +778,11 @@
         ("help", "_Help"),
     ]
 
+    if macosxSupport.runningAsOSXApp():
+        del menu_specs[-3]
+        menu_specs[-2] = ("windows", "_Window")
+
+
     # New classes
     from IdleHistory import History
 
@@ -1371,9 +1377,12 @@
     enable_shell = enable_shell or not edit_start
     # start editor and/or shell windows:
     root = Tk(className="Idle")
+
     fixwordbreaks(root)
     root.withdraw()
     flist = PyShellFileList(root)
+    macosxSupport.setupApp(root, flist)
+
     if enable_edit:
         if not (cmd or script):
             for filename in args:
@@ -1381,8 +1390,17 @@
             if not args:
                 flist.new()
     if enable_shell:
-        if not flist.open_shell():
+        shell = flist.open_shell()
+        if not shell:
             return # couldn't open shell
+
+        if macosxSupport.runningAsOSXApp() and flist.dict:
+            # On OSX: when the user has double-clicked on a file that causes
+            # IDLE to be launched the shell window will open just in front of 
+            # the file she wants to see. Lower the interpreter window when 
+            # there are open files.
+            shell.top.lower()
+
     shell = flist.pyshell
     # handle remaining options:
     if debug:
@@ -1403,6 +1421,7 @@
         elif script:
             shell.interp.prepend_syspath(script)
             shell.interp.execfile(script)
+
     root.mainloop()
     root.destroy()
 

Modified: python/trunk/Lib/idlelib/ZoomHeight.py
==============================================================================
--- python/trunk/Lib/idlelib/ZoomHeight.py	(original)
+++ python/trunk/Lib/idlelib/ZoomHeight.py	Sun Jun 11 16:33:36 2006
@@ -2,6 +2,7 @@
 
 import re
 import sys
+import macosxSupport
 
 class ZoomHeight:
 
@@ -29,6 +30,14 @@
     if sys.platform == 'win32':
         newy = 0
         newheight = newheight - 72
+
+    elif macosxSupport.runningAsOSXApp():
+        # The '88' below is a magic number that avoids placing the bottom
+        # of the window below the panel on my machine. I don't know how
+        # to calculate the correct value for this with tkinter.
+        newy = 22
+        newheight = newheight - newy - 88
+
     else:
         #newy = 24
         newy = 0

Deleted: /python/trunk/Lib/idlelib/buildapp.py
==============================================================================
--- /python/trunk/Lib/idlelib/buildapp.py	Sun Jun 11 16:33:36 2006
+++ (empty file)
@@ -1,17 +0,0 @@
-#
-# After running python setup.py install, run this program from the command
-# line like so:
-#
-# % python2.3 buildapp.py build
-#
-# A double-clickable IDLE application will be created in the build/ directory.
-#
-
-from bundlebuilder import buildapp
-
-buildapp(
-        name="IDLE",
-        mainprogram="idle.py",
-        argv_emulation=1,
-        iconfile="Icons/idle.icns",
-)

Modified: python/trunk/Lib/idlelib/configHandler.py
==============================================================================
--- python/trunk/Lib/idlelib/configHandler.py	(original)
+++ python/trunk/Lib/idlelib/configHandler.py	Sun Jun 11 16:33:36 2006
@@ -20,6 +20,7 @@
 import os
 import sys
 import string
+import macosxSupport
 from ConfigParser import ConfigParser, NoOptionError, NoSectionError
 
 class InvalidConfigType(Exception): pass
@@ -495,7 +496,18 @@
         return binding
 
     def GetCurrentKeySet(self):
-        return self.GetKeySet(self.CurrentKeys())
+        result = self.GetKeySet(self.CurrentKeys())
+
+        if macosxSupport.runningAsOSXApp():
+            # We're using AquaTk, replace all keybingings that use the
+            # Alt key by ones that use the Option key because the former 
+            # don't work reliably.
+            for k, v in result.items():
+                v2 = [ x.replace('<Alt-', '<Option-') for x in v ]
+                if v != v2:
+                    result[k] = v2
+
+        return result
 
     def GetKeySet(self,keySetName):
         """

Modified: python/trunk/Lib/idlelib/keybindingDialog.py
==============================================================================
--- python/trunk/Lib/idlelib/keybindingDialog.py	(original)
+++ python/trunk/Lib/idlelib/keybindingDialog.py	Sun Jun 11 16:33:36 2006
@@ -133,7 +133,7 @@
         config-keys.def must use the same ordering.
         """
         import sys
-        if sys.platform == 'darwin' and sys.executable.count('.app'):
+        if sys.platform == 'darwin' and sys.argv[0].count('.app'):
             self.modifiers = ['Shift', 'Control', 'Option', 'Command']
         else:
             self.modifiers = ['Control', 'Alt', 'Shift']

Added: python/trunk/Lib/idlelib/macosxSupport.py
==============================================================================
--- (empty file)
+++ python/trunk/Lib/idlelib/macosxSupport.py	Sun Jun 11 16:33:36 2006
@@ -0,0 +1,36 @@
+"""
+A number of function that enhance IDLE on MacOSX when it used as a normal
+GUI application (as opposed to an X11 application).
+"""
+import sys
+
+def runningAsOSXApp():
+    """ Returns True iff running from the IDLE.app bundle on OSX """
+    return (sys.platform == 'darwin' and 'IDLE.app' in sys.argv[0])
+
+def addOpenEventSupport(root, flist):
+    """
+    This ensures that the application will respont to open AppleEvents, which
+    makes is feaseable to use IDLE as the default application for python files.
+    """
+    def doOpenFile(*args):
+        for fn in args:
+            flist.open(fn)
+           
+    # The command below is a hook in aquatk that is called whenever the app
+    # receives a file open event. The callback can have multiple arguments,
+    # one for every file that should be opened.
+    root.createcommand("::tk::mac::OpenDocument", doOpenFile)
+
+def hideTkConsole(root):
+    root.tk.call('console', 'hide')
+
+
+def setupApp(root, flist):
+    """
+    Perform setup for the OSX application bundle.
+    """
+    if not runningAsOSXApp(): return
+
+    hideTkConsole(root)
+    addOpenEventSupport(root, flist)


More information about the Python-checkins mailing list