]> git.saurik.com Git - wxWidgets.git/commitdiff
A new listctrl mixin class
authorRobin Dunn <robin@alldunn.com>
Tue, 16 Apr 2002 21:32:55 +0000 (21:32 +0000)
committerRobin Dunn <robin@alldunn.com>
Tue, 16 Apr 2002 21:32:55 +0000 (21:32 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@15175 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

wxPython/wxPython/lib/mixins/listctrl.py

index 1e1ce1baf3cf16b43b03b55b63c2dc5811411935..1c70c12ce7d53b9d4810a4964beb9dae9fc525c8 100644 (file)
@@ -267,3 +267,82 @@ class wxListCtrlAutoWidthMixin:
 
 
 #----------------------------------------------------------------------------
+
+SEL_FOC = wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED
+def selectBeforePopup(event):
+    """Ensures the item the mouse is pointing at is selected before a popup.
+
+    Works with both single-select and multi-select lists."""
+    ctrl = event.GetEventObject()
+    if isinstance(ctrl, wxListCtrl):
+        n, flags = ctrl.HitTest(event.GetPosition())
+        if n >= 0:
+            if not ctrl.GetItemState(n, wxLIST_STATE_SELECTED):
+                for i in range(ctrl.GetItemCount()):
+                    ctrl.SetItemState(i, 0, SEL_FOC)
+                #for i in getListCtrlSelection(ctrl, SEL_FOC):
+                #    ctrl.SetItemState(i, 0, SEL_FOC)
+                ctrl.SetItemState(n, SEL_FOC, SEL_FOC)
+
+def getListCtrlSelection(listctrl, state=wxLIST_STATE_SELECTED):
+    """ Returns list of item indexes of given state (selected by defaults) """
+    res = []
+    idx = -1
+    while 1:
+        idx = listctrl.GetNextItem(idx, wxLIST_NEXT_ALL, state)
+        if idx == -1:
+            break
+        res.append(idx)
+    return res
+
+class ListCtrlSelectionManagerMix:
+    """Mixin that defines a platform independent selection policy
+
+    As selection single and multi-select list return the item index or a
+    list of item indexes respectively.
+    """
+    wxEVT_DOPOPUPMENU = wxNewId()
+    _menu = None
+
+    def __init__(self):
+        EVT_RIGHT_DOWN(self, self.OnLCSMRightDown)
+        self.Connect(-1, -1, self.wxEVT_DOPOPUPMENU, self.OnLCSMDoPopup)
+
+    def getPopupMenu(self):
+        """ Override to implement dynamic menus (create) """
+        return self._menu
+
+    def setPopupMenu(self, menu):
+        """ Must be set for default behaviour """
+        self._menu = menu
+
+    def afterPopupMenu(self, menu):
+        """ Override to implement dynamic menus (destroy) """
+        pass
+
+    def getSelection(self):
+        res = getListCtrlSelection(self)
+        if self.GetWindowStyleFlag() & wxLC_SINGLE_SEL:
+            if res:
+                return res[0]
+            else:
+                return -1
+        else:
+            return res
+
+    def OnLCSMRightDown(self, event):
+        selectBeforePopup(event)
+        event.Skip()
+        menu = self.getPopupMenu()
+        if menu:
+            evt = wxPyEvent()
+            evt.SetEventType(self.wxEVT_DOPOPUPMENU)
+            evt.menu = menu
+            evt.pos = event.GetPosition()
+            wxPostEvent(self, evt)
+
+    def OnLCSMDoPopup(self, event):
+        self.PopupMenu(event.menu, event.pos)
+        self.afterPopupMenu(event.menu)
+
+#----------------------------------------------------------------------