]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/listctrl.cpp
better focus handling (blind fix)
[wxWidgets.git] / src / msw / listctrl.cpp
index 107623555b3fec9692fdacf03d8cab8db682bead..1196685fe8506f99ad660b9d43176a5734a05e96 100644 (file)
     #define LVS_OWNERDATA 0x1000
 #endif
 
+// mingw32/cygwin don't have declarations for comctl32.dll 4.70+ stuff
+#ifndef NM_CACHEHINT
+    typedef struct tagNMLVCACHEHINT
+    {
+        NMHDR   hdr;
+        int     iFrom;
+        int     iTo;
+    } NMLVCACHEHINT;
+
+    #define NM_CACHEHINT NMLVCACHEHINT
+#endif
+
+#ifndef LVN_ODCACHEHINT
+    #define LVN_ODCACHEHINT (-113)
+#endif
+
 // ----------------------------------------------------------------------------
 // private functions
 // ----------------------------------------------------------------------------
 
-static void wxConvertToMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& tvItem);
-static void wxConvertFromMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& tvItem, HWND getFullInfo = 0);
+// convert our state and mask flags to LV_ITEM constants
+static void wxConvertToMSWFlags(long state, long mask, LV_ITEM& lvItem);
+
+// convert wxListItem to LV_ITEM
+static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
+                                   const wxListItem& info, LV_ITEM& lvItem);
+
+// convert LV_ITEM to wxListItem
+static void wxConvertFromMSWListItem(HWND hwndListCtrl,
+                                     wxListItem& info,
+                                     /* const */ LV_ITEM& lvItem);
 
 // ----------------------------------------------------------------------------
 // events
@@ -95,8 +120,10 @@ DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK)
 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK)
 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK)
 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT)
 
 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl)
+IMPLEMENT_DYNAMIC_CLASS(wxListView, wxListCtrl)
 IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
 
 BEGIN_EVENT_TABLE(wxListCtrl, wxControl)
@@ -454,20 +481,19 @@ long wxListCtrl::ConvertToMSWStyle(long& oldStyle, long style) const
         wstyle |= LVS_SORTDESCENDING;
     }
 
+#if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
     if ( style & wxLC_VIRTUAL )
     {
         int ver = wxTheApp->GetComCtl32Version();
         if ( ver < 470 )
         {
-            wxLogWarning(_("Please install a newer version of comctl32.dll\n"
-                           "(at least version 4.70 is required but you have "
-                           "%d.%02d)\n"
-                           "or this program won't operate correctly."),
+            wxLogWarning(_("Please install a newer version of comctl32.dll\n(at least version 4.70 is required but you have %d.%02d)\nor this program won't operate correctly."),
                         ver / 100, ver % 100);
         }
 
         wstyle |= LVS_OWNERDATA;
     }
+#endif
 
     return wstyle;
 }
@@ -660,7 +686,8 @@ bool wxListCtrl::GetItem(wxListItem& info) const
     }
     else
     {
-        wxConvertFromMSWListItem(this, info, lvItem);
+        // give NULL as hwnd as we already have everything we need
+        wxConvertFromMSWListItem(NULL, info, lvItem);
     }
 
     if (lvItem.pszText)
@@ -745,14 +772,22 @@ int wxListCtrl::GetItemState(long item, long stateMask) const
 // Sets the item state
 bool wxListCtrl::SetItemState(long item, long state, long stateMask)
 {
-    wxListItem info;
+    // NB: don't use SetItem() here as it doesn't work with the virtual list
+    //     controls
+    LV_ITEM lvItem;
+    wxZeroMemory(lvItem);
 
-    info.m_mask = wxLIST_MASK_STATE;
-    info.m_state = state;
-    info.m_stateMask = stateMask;
-    info.m_itemId = item;
+    wxConvertToMSWFlags(state, stateMask, lvItem);
 
-    return SetItem(info);
+    if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE,
+                        (WPARAM)item, (LPARAM)&lvItem) )
+    {
+        wxLogLastError(_T("ListView_SetItemState"));
+
+        return FALSE;
+    }
+
+    return TRUE;
 }
 
 // Sets the item image
@@ -1066,7 +1101,7 @@ wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
 {
     wxASSERT( (textControlClass->IsKindOf(CLASSINFO(wxTextCtrl))) );
 
-    // VS: ListView_EditLabel requires that the list has focus.  
+    // VS: ListView_EditLabel requires that the list has focus.
     SetFocus();
     HWND hWnd = (HWND) ListView_EditLabel(GetHwnd(), item);
 
@@ -1341,11 +1376,74 @@ bool wxListCtrl::ScrollList(int dx, int dy)
 // or zero if the two items are equivalent.
 
 // data is arbitrary data to be passed to the sort function.
+
+// FIXME: this is horrible and MT-unsafe and everything else but I don't have
+//        time for anything better right now (VZ)
+static long gs_sortData = 0;
+static wxListCtrl *gs_sortCtrl = NULL;
+static wxListCtrlCompare gs_sortFunction = NULL;
+
+int wxCMPFUNC_CONV wxListCtrlCompareFn(const void *arg1, const void *arg2)
+{
+    int n1 = *(const int *)arg1,
+        n2 = *(const int *)arg2;
+
+    return gs_sortFunction(gs_sortCtrl->GetItemData(n1),
+                           gs_sortCtrl->GetItemData(n2),
+                           gs_sortData);
+}
+
 bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
 {
-    return (ListView_SortItems(GetHwnd(), (PFNLVCOMPARE) fn, data) != 0);
+    // sort the attributes too
+    if ( m_hasAnyAttr )
+    {
+        int n,
+            count = GetItemCount();
+        int *aItems = new int[count];
+        for ( n = 0; n < count; n++ )
+        {
+            aItems[n] = n;
+        }
+
+        gs_sortData = data;
+        gs_sortCtrl = this;
+        gs_sortFunction = fn;
+
+        qsort(aItems, count, sizeof(int), wxListCtrlCompareFn);
+
+        gs_sortData = 0;
+        gs_sortCtrl = NULL;
+        gs_sortFunction = NULL;
+
+        wxHashTable attrsNew(wxKEY_INTEGER, 1000);
+        for ( n = 0; n < count; n++ )
+        {
+            wxObject *attr = m_attrs.Delete(aItems[n]);
+            if ( attr )
+            {
+                attrsNew.Put(n, attr);
+            }
+        }
+
+        m_attrs.Destroy();
+        m_attrs = attrsNew;
+
+        delete [] aItems;
+    }
+
+    if ( !ListView_SortItems(GetHwnd(), (PFNLVCOMPARE)fn, data) )
+    {
+        wxLogDebug(_T("ListView_SortItems() failed"));
+
+        return FALSE;
+    }
+
+    return TRUE;
 }
 
+
+
 // ----------------------------------------------------------------------------
 // message processing
 // ----------------------------------------------------------------------------
@@ -1407,7 +1505,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
             {
                 eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT;
                 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
-                wxConvertFromMSWListItem(this, event.m_item, info->item, GetHwnd());
+                wxConvertFromMSWListItem(GetHwnd(), event.m_item, info->item);
                 event.m_itemIndex = event.m_item.m_itemId;
             }
             break;
@@ -1440,7 +1538,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
             {
                 eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT;
                 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
-                wxConvertFromMSWListItem(this, event.m_item, info->item);
+                wxConvertFromMSWListItem(GetHwnd(), event.m_item, info->item);
                 if ( info->item.pszText == NULL || info->item.iItem == -1 )
                     return FALSE;
 
@@ -1452,7 +1550,7 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
             {
                 eventType = wxEVT_COMMAND_LIST_SET_INFO;
                 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
-                wxConvertFromMSWListItem(this, event.m_item, info->item, GetHwnd());
+                wxConvertFromMSWListItem(GetHwnd(), event.m_item, info->item);
             }
             break;
 
@@ -1590,13 +1688,38 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
             break;
 #endif // 0
 
-#if defined(_WIN32_IE) && _WIN32_IE >= 0x300
+#if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \
+    && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
         case NM_CUSTOMDRAW:
             *result = OnCustomDraw(lParam);
 
             return TRUE;
 #endif // _WIN32_IE >= 0x300
 
+        case LVN_ODCACHEHINT:
+            {
+                const NM_CACHEHINT *cacheHint = (NM_CACHEHINT *)lParam;
+
+                eventType = wxEVT_COMMAND_LIST_CACHE_HINT;
+
+                // we get some really stupid cache hints like ones for items in
+                // range 0..0 for an empty control or, after deleting an item,
+                // for items in invalid range - filter this garbage out
+                if ( cacheHint->iFrom < cacheHint->iTo )
+                {
+                    event.m_oldItemIndex = cacheHint->iFrom;
+
+                    long iMax = GetItemCount();
+                    event.m_itemIndex = cacheHint->iTo < iMax ? cacheHint->iTo
+                                                              : iMax - 1;
+                }
+                else
+                {
+                    return FALSE;
+                }
+            }
+            break;
+
         case LVN_GETDISPINFO:
             if ( IsVirtual() )
             {
@@ -1652,10 +1775,10 @@ bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
             return TRUE;
 
         case LVN_ENDLABELEDIT:
-            {
-                *result = event.IsAllowed();
-                return TRUE;
-            }
+            // logic here is inversed compared to all the other messages
+            *result = event.IsAllowed();
+
+            return TRUE;
     }
 
     *result = !event.IsAllowed();
@@ -1781,24 +1904,28 @@ void wxListCtrl::OnPaint(wxPaintEvent& event)
 
     int itemCount = GetItemCount();
     int i;
-    for (i = 0; i < itemCount; i++)
+    if (drawHRules)
     {
-        if (GetItemRect(i, itemRect))
+        long top = GetTopItem();
+        for (i = top; i < top + GetCountPerPage() + 1; i++)
         {
-            cy = itemRect.GetTop();
-            if (i != 0) // Don't draw the first one
-            {
-                dc.DrawLine(0, cy, clientSize.x, cy);
-            }
-            // Draw last line
-            if (i == (GetItemCount() - 1))
+            if (GetItemRect(i, itemRect))
             {
-                cy = itemRect.GetBottom();
-                dc.DrawLine(0, cy, clientSize.x, cy);
+                cy = itemRect.GetTop();
+                if (i != 0) // Don't draw the first one
+                {
+                    dc.DrawLine(0, cy, clientSize.x, cy);
+                }
+                // Draw last line
+                if (i == itemCount - 1)
+                {
+                    cy = itemRect.GetBottom();
+                    dc.DrawLine(0, cy, clientSize.x, cy);
+                }
             }
         }
     }
-    i = (GetItemCount() - 1);
+    i = itemCount - 1;
     if (drawVRules && (i > -1))
     {
         wxRect firstItemRect;
@@ -1822,7 +1949,7 @@ void wxListCtrl::OnPaint(wxPaintEvent& event)
 // virtual list controls
 // ----------------------------------------------------------------------------
 
-wxString wxListCtrl::OnGetItemText(long item, long col) const
+wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
 {
     // this is a pure virtual function, in fact - which is not really pure
     // because the controls which are not virtual don't need to implement it
@@ -1831,7 +1958,7 @@ wxString wxListCtrl::OnGetItemText(long item, long col) const
     return wxEmptyString;
 }
 
-int wxListCtrl::OnGetItemImage(long item) const
+int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const
 {
     // same as above
     wxFAIL_MSG( _T("not supposed to be called") );
@@ -1839,7 +1966,7 @@ int wxListCtrl::OnGetItemImage(long item) const
     return -1;
 }
 
-wxListItemAttr *wxListCtrl::OnGetItemAttr(long item) const
+wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
 {
     wxASSERT_MSG( item >= 0 && item < GetItemCount(),
                   _T("invalid item index in OnGetItemAttr()") );
@@ -1868,10 +1995,14 @@ void wxListCtrl::RefreshItem(long item)
 
 void wxListCtrl::RefreshItems(long itemFrom, long itemTo)
 {
-    for ( long item = itemFrom; item <= itemTo; item++ )
-    {
-        RefreshItem(item);
-    }
+    wxRect rect1, rect2;
+    GetItemRect(itemFrom, rect1);
+    GetItemRect(itemTo, rect2);
+
+    wxRect rect = rect1;
+    rect.height = rect2.GetBottom() - rect1.GetTop();
+
+    RefreshRect(rect);
 }
 
 // ----------------------------------------------------------------------------
@@ -1918,7 +2049,9 @@ void wxListItem::ClearAttributes()
     m_attr = NULL;
 }
 
-static void wxConvertFromMSWListItem(const wxListCtrl *WXUNUSED(ctrl), wxListItem& info, LV_ITEM& lvItem, HWND getFullInfo)
+static void wxConvertFromMSWListItem(HWND hwndListCtrl,
+                                     wxListItem& info,
+                                     LV_ITEM& lvItem)
 {
     info.m_data = lvItem.lParam;
     info.m_mask = 0;
@@ -1929,7 +2062,7 @@ static void wxConvertFromMSWListItem(const wxListCtrl *WXUNUSED(ctrl), wxListIte
     long oldMask = lvItem.mask;
 
     bool needText = FALSE;
-    if (getFullInfo != 0)
+    if (hwndListCtrl != 0)
     {
         if ( lvItem.mask & LVIF_TEXT )
             needText = FALSE;
@@ -1941,9 +2074,8 @@ static void wxConvertFromMSWListItem(const wxListCtrl *WXUNUSED(ctrl), wxListIte
             lvItem.pszText = new wxChar[513];
             lvItem.cchTextMax = 512;
         }
-        //    lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
         lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
-        ::SendMessage(getFullInfo, LVM_GETITEM, 0, (LPARAM)& lvItem);
+        ::SendMessage(hwndListCtrl, LVM_GETITEM, 0, (LPARAM)& lvItem);
     }
 
     if ( lvItem.mask & LVIF_STATE )
@@ -2000,7 +2132,37 @@ static void wxConvertFromMSWListItem(const wxListCtrl *WXUNUSED(ctrl), wxListIte
     lvItem.mask = oldMask;
 }
 
-static void wxConvertToMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& lvItem)
+static void wxConvertToMSWFlags(long state, long stateMask, LV_ITEM& lvItem)
+{
+    if (stateMask & wxLIST_STATE_CUT)
+    {
+        lvItem.stateMask |= LVIS_CUT;
+        if (state & wxLIST_STATE_CUT)
+            lvItem.state |= LVIS_CUT;
+    }
+    if (stateMask & wxLIST_STATE_DROPHILITED)
+    {
+        lvItem.stateMask |= LVIS_DROPHILITED;
+        if (state & wxLIST_STATE_DROPHILITED)
+            lvItem.state |= LVIS_DROPHILITED;
+    }
+    if (stateMask & wxLIST_STATE_FOCUSED)
+    {
+        lvItem.stateMask |= LVIS_FOCUSED;
+        if (state & wxLIST_STATE_FOCUSED)
+            lvItem.state |= LVIS_FOCUSED;
+    }
+    if (stateMask & wxLIST_STATE_SELECTED)
+    {
+        lvItem.stateMask |= LVIS_SELECTED;
+        if (state & wxLIST_STATE_SELECTED)
+            lvItem.state |= LVIS_SELECTED;
+    }
+}
+
+static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
+                                   const wxListItem& info,
+                                   LV_ITEM& lvItem)
 {
     lvItem.iItem = (int) info.m_itemId;
 
@@ -2014,30 +2176,8 @@ static void wxConvertToMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_
     if (info.m_mask & wxLIST_MASK_STATE)
     {
         lvItem.mask |= LVIF_STATE;
-        if (info.m_stateMask & wxLIST_STATE_CUT)
-        {
-            lvItem.stateMask |= LVIS_CUT;
-            if (info.m_state & wxLIST_STATE_CUT)
-                lvItem.state |= LVIS_CUT;
-        }
-        if (info.m_stateMask & wxLIST_STATE_DROPHILITED)
-        {
-            lvItem.stateMask |= LVIS_DROPHILITED;
-            if (info.m_state & wxLIST_STATE_DROPHILITED)
-                lvItem.state |= LVIS_DROPHILITED;
-        }
-        if (info.m_stateMask & wxLIST_STATE_FOCUSED)
-        {
-            lvItem.stateMask |= LVIS_FOCUSED;
-            if (info.m_state & wxLIST_STATE_FOCUSED)
-                lvItem.state |= LVIS_FOCUSED;
-        }
-        if (info.m_stateMask & wxLIST_STATE_SELECTED)
-        {
-            lvItem.stateMask |= LVIS_SELECTED;
-            if (info.m_state & wxLIST_STATE_SELECTED)
-                lvItem.state |= LVIS_SELECTED;
-        }
+
+        wxConvertToMSWFlags(info.m_state, info.m_stateMask, lvItem);
     }
 
     if (info.m_mask & wxLIST_MASK_TEXT)