dabo Commit
Revision 6134
Date: 2010-10-23 09:26:01 -0700 (Sat, 23 Oct 2010)
Author: Ed
Trac: http://trac.dabodev.com/changeset/6134

Changed:
U   trunk/dabo/dApp.py
U   trunk/dabo/ui/uiwx/dBaseMenuBar.py
U   trunk/dabo/ui/uiwx/dMenu.py
U   trunk/dabo/ui/uiwx/uiApp.py

Log:
Fixed the menu handling issues that prevented the MRU functionality from 
working. Also added support for using MRU with submenus.


Diff:
Modified: trunk/dabo/dApp.py
===================================================================
--- trunk/dabo/dApp.py  2010-10-23 12:26:05 UTC (rev 6133)
+++ trunk/dabo/dApp.py  2010-10-23 16:26:01 UTC (rev 6134)
@@ -28,6 +28,7 @@
 from dabo import dUserSettingProvider
 from dabo.lib.RemoteConnector import RemoteConnector
 from dabo.lib.utils import ustr
+from dabo.lib.utils import cleanMenuCaption
 
 try:
        import simplejson
@@ -466,8 +467,9 @@
                base = "MRU.%s" % self.getAppInfo("appName")
                self.deleteAllUserSettings(base)
                for cap in self._persistentMRUs.keys():
-                       mruList = self.uiApp.getMRUListForMenu(cap)
-                       setName = ".".join((base, cap))
+                       cleanCap = cleanMenuCaption(cap)
+                       mruList = self.uiApp.getMRUListForMenu(cleanCap)
+                       setName = ".".join((base, cleanCap))
                        self.setUserSetting(setName, mruList)
 
 
@@ -475,11 +477,12 @@
                """Retrieve any saved MRU lists."""
                base = "MRU.%s" % self.getAppInfo("appName")
                for cap, fcn in self._persistentMRUs.items():
-                       itms = self.getUserSetting(".".join((base, cap)))
+                       cleanCap = cleanMenuCaption(cap)
+                       itms = self.getUserSetting(".".join((base, cleanCap)))
                        if itms:
                                # Should be a list of items. Add 'em in reverse 
order
                                for itm in itms:
-                                       self.uiApp.addToMRU(cap, itm, fcn)
+                                       self.uiApp.addToMRU(cleanCap, itm, fcn)
 
 
        def getAppInfo(self, item):

Modified: trunk/dabo/ui/uiwx/dBaseMenuBar.py
===================================================================
--- trunk/dabo/ui/uiwx/dBaseMenuBar.py  2010-10-23 12:26:05 UTC (rev 6133)
+++ trunk/dabo/ui/uiwx/dBaseMenuBar.py  2010-10-23 16:26:01 UTC (rev 6134)
@@ -22,7 +22,8 @@
 class FileMenu(dMenu):
 
        def __init__(self, *args, **kwargs):
-               kwargs["MRU"] = True
+               if "MRU" not in kwargs:
+                       kwargs["MRU"] = True
                self.super(*args, **kwargs)
 
 

Modified: trunk/dabo/ui/uiwx/dMenu.py
===================================================================
--- trunk/dabo/ui/uiwx/dMenu.py 2010-10-23 12:26:05 UTC (rev 6133)
+++ trunk/dabo/ui/uiwx/dMenu.py 2010-10-23 16:26:01 UTC (rev 6134)
@@ -56,10 +56,30 @@
                self.bindEvent(dEvents.MenuOpen, self.__onMenuHighlight)
                self.bindEvent(dEvents.MenuHighlight, self.__onMenuHighlight)
                if self._useMRU:
+                       self._setMRUBindings()
+
+
+       def _setMRUBindings(self):
+               """If the menu is not top-level (i.e., directly opened from the 
MenuBar),
+               the MenuOpen event will not be raised, so trigger on the 
MenuHighlight
+               event instead.
+               """
+               if isinstance(self.Parent, dabo.ui.dMenuBar):
                        self.bindEvent(dEvents.MenuOpen, self.__onMenuOpenMRU)
+               else:
+                       self.bindEvent(dEvents.MenuHighlight, 
self.__onMenuOpenMRU)
 
 
+       def _clearMRUBindings(self):
+               """See the _setMRUBindings method for an explanation. This uses
+               the same logic to unbind MRU events.
+               """
+               if isinstance(self.Parent, dabo.ui.dMenuBar):
+                       self.unbindEvent(dEvents.MenuOpen, self.__onMenuOpenMRU)
+               else:
+                       self.unbindEvent(dEvents.MenuHighlight, 
self.__onMenuOpenMRU)
 
+
        def __onMenuHighlight(self, evt):
                """Note that this code is here in a dabo binding instead of in 
the wx binding
                because of the way we've worked around wx limitations: dMenu as 
a top-level
@@ -517,6 +537,20 @@
                        self._properties["MenuID"] = val
 
 
+       def _getMRU(self):
+               return self._useMRU
+
+       def _setMRU(self, val):
+               if self._constructed():
+                       self._useMRU = val
+                       if val:
+                               self._setMRUBindings()
+                       else:
+                               self._clearMRUBindings()
+               else:
+                       self._properties["MRU"] = val
+
+
        def _getParent(self):
                try:
                        v = self._parent
@@ -529,24 +563,27 @@
 
 
        Caption = property(_getCaption, _setCaption, None,
-                       _("Specifies the text of the menu."))
+                       _("Specifies the text of the menu.  (str)"))
 
        Enabled = property(_getEnabled, _setEnabled, None,
-                       _("Specifies whether the menu can be interacted with."))
+                       _("Specifies whether the menu can be interacted with. 
Default=True  (bool)"))
 
        Form = property(_getForm, None, None,
-                       _("Specifies the form that contains the menu."))
+                       _("Specifies the form that contains the menu.  
(dForm)"))
 
        HelpText = property(_getHelpText, _setHelpText, None,
-                       _("Specifies the help text associated with this menu. 
(str)"))
+                       _("Specifies the help text associated with this menu.  
(str)"))
 
        MenuID = property(_getMenuID, _setMenuID, None,
                        _("""Identifying value for this menu. NOTE: there is no 
checking for
                        duplicate values; it is the responsibility to ensure 
that MenuID values
                        are unique.  (varies)"""))
 
+       MRU = property(_getMRU, _setMRU, None,
+                       _("Determines if this menu uses Most Recently Used 
behavior. Default=False  (bool)"))
+
        Parent = property(_getParent, _setParent, None,
-                       _("Specifies the parent menu or menubar."))
+                       _("Specifies the parent menu or menubar.  (varies)"))
 
 
        DynamicCaption = makeDynamicProperty(Caption)

Modified: trunk/dabo/ui/uiwx/uiApp.py
===================================================================
--- trunk/dabo/ui/uiwx/uiApp.py 2010-10-23 12:26:05 UTC (rev 6133)
+++ trunk/dabo/ui/uiwx/uiApp.py 2010-10-23 16:26:01 UTC (rev 6134)
@@ -10,6 +10,7 @@
 import dabo.lib.utils as utils
 from dabo.dObject import dObject
 from dabo.dLocalize import _, n_
+from dabo.lib.utils import cleanMenuCaption
 import dabo.dConstants as kons
 
 
@@ -1048,23 +1049,24 @@
                                return ret
 
 
-       def addToMRU(self, menu, prompt, bindfunc=None):
+       def addToMRU(self, menuOrCaption, prompt, bindfunc=None):
                """Adds the specified menu to the top of the list of
                MRU prompts for that menu.
                """
-               if isinstance(menu, basestring):
+               if isinstance(menuOrCaption, basestring):
                        # They passed the menu caption directly
-                       cap = menu
+                       cap = menuOrCaption
                else:
-                       cap = menu.Caption
-               mn = self._mruMenuPrompts.get(cap, [])
+                       cap = menuOrCaption.Caption
+               cleanCap = cleanMenuCaption(cap)
+               mn = self._mruMenuPrompts.get(cleanCap, [])
                if prompt in mn:
                        mn.remove(prompt)
                mn.insert(0, prompt)
-               self._mruMenuPrompts[cap] = mn[:self._mruMaxItems]
-               mf = self._mruMenuFuncs.get(cap, {})
+               self._mruMenuPrompts[cleanCap] = mn[:self._mruMaxItems]
+               mf = self._mruMenuFuncs.get(cleanCap, {})
                mf[prompt] = bindfunc
-               self._mruMenuFuncs[cap] = mf
+               self._mruMenuFuncs[cleanCap] = mf
 
 
        def onMenuOpenMRU(self, menu):
@@ -1072,10 +1074,12 @@
                correct order.
                """
                cap = menu.Caption
-               mnPrm = self._mruMenuPrompts.get(cap, [])
+               cleanCap = cleanMenuCaption(cap)
+               topLevel = isinstance(menu.Parent, dabo.ui.dMenuBar)
+               mnPrm = self._mruMenuPrompts.get(cleanCap, [])
                if not mnPrm:
                        return
-               if menu._mruSeparator is None:
+               if topLevel and (menu._mruSeparator is None):
                        menu._mruSeparator = menu.appendSeparator()
                tmplt = "&%s %s"
                promptList = [tmplt % (pos+1, txt)
@@ -1102,13 +1106,12 @@
                                if itm not in kids:
                                        continue
                                try:
-                                       pos = kids.index(itm)
-                                       menu.remove(pos, True)
-                               except (IndexError, ValueError):
+                                       menu.remove(itm)
+                               except (IndexError, ValueError), e:
                                        pass
                        # Add them all back
                        lnks = {}
-                       fncs = self._mruMenuFuncs.get(cap, {})
+                       fncs = self._mruMenuFuncs.get(cleanCap, {})
                        for pos, txt in enumerate(mnPrm):
                                itm = menu.append(tmplt % (pos+1, txt), 
OnHit=fncs.get(txt, None))
                                lnks[itm.GetId()] = itm



_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message: 
http://leafe.com/archives/byMID/[email protected]

Reply via email to