]> git.saurik.com Git - wxWidgets.git/commitdiff
added support for column images under MSW
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 2 Sep 2001 23:47:42 +0000 (23:47 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 2 Sep 2001 23:47:42 +0000 (23:47 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@11546 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

samples/listctrl/listtest.cpp
samples/listctrl/listtest.h
src/msw/listctrl.cpp
src/msw/window.cpp

index 862ac6d52d13a62ed64ac1afa7aefe83852ed416..bcf0dddac556ead5e21036a8aa4facc6792aa914 100644 (file)
@@ -366,9 +366,23 @@ void MyFrame::InitWithReportItems()
 {
     m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
 
+    // under MSW for SetColumnWidth() to work we need to create the items with
+    // images initially
+#if 1
+    wxListItem itemCol;
+    itemCol.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE;
+    itemCol.m_text = "Column 1";
+    itemCol.m_image = -1;
+    m_listCtrl->InsertColumn(0, itemCol);
+    itemCol.m_text = "Column 2";
+    m_listCtrl->InsertColumn(1, itemCol);
+    itemCol.m_text = "Column 3";
+    m_listCtrl->InsertColumn(2, itemCol);
+#else
     m_listCtrl->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
     m_listCtrl->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
     m_listCtrl->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
+#endif
 
     // to speed up inserting we hide the control temporarily
     m_listCtrl->Hide();
@@ -578,13 +592,27 @@ void MyListCtrl::OnCacheHint(wxListEvent& event)
                   event.GetCacheFrom(), event.GetCacheTo() );
 }
 
+void MyListCtrl::SetColumnImage(int col, int image)
+{
+    wxListItem item;
+    item.SetMask(wxLIST_MASK_IMAGE);
+    item.SetImage(image);
+    SetColumn(col, item);
+}
+
 void MyListCtrl::OnColClick(wxListEvent& event)
 {
-    wxLogMessage( wxT("OnColumnClick at %d."), event.GetColumn() );
+    int col = event.GetColumn();
+    SetColumnImage(col, 0);
+
+    wxLogMessage( wxT("OnColumnClick at %d."), col );
 }
 
 void MyListCtrl::OnColRightClick(wxListEvent& event)
 {
+    int col = event.GetColumn();
+    SetColumnImage(col, -1);
+
     wxLogMessage( wxT("OnColumnRightClick at %d."), event.GetColumn() );
 }
 
index d91ce1b9f4c9a72a9000e03b6ba4c4364e75b773..b3464b74db6c8ef8710b68381317dc352e0dbbfc 100644 (file)
@@ -54,6 +54,8 @@ public:
     void OnChar(wxKeyEvent& event);
 
 private:
+    void SetColumnImage(int col, int image);
+
     void LogEvent(const wxListEvent& event, const wxChar *eventName);
 
     virtual wxString OnGetItemText(long item, long column) const;
index 0bb04aa4a000b3ac22f0f2925ba10c705e3bfd9b..fbf22fa5dd406586ab530de8c9de0f3ce47e049f 100644 (file)
@@ -117,6 +117,10 @@ static void wxConvertFromMSWListItem(HWND hwndListCtrl,
                                      wxListItem& info,
                                      /* const */ LV_ITEM& lvItem);
 
+// convert our wxListItem to LV_COLUMN
+static void wxConvertToMSWListCol(int col, const wxListItem& item,
+                                  LV_COLUMN& lvCol);
+
 // ----------------------------------------------------------------------------
 // events
 // ----------------------------------------------------------------------------
@@ -553,9 +557,7 @@ bool wxListCtrl::SetBackgroundColour(const wxColour& col)
 bool wxListCtrl::GetColumn(int col, wxListItem& item) const
 {
     LV_COLUMN lvCol;
-    lvCol.mask = 0;
-    lvCol.fmt = 0;
-    lvCol.pszText = NULL;
+    wxZeroMemory(lvCol);
 
     if ( item.m_mask & wxLIST_MASK_TEXT )
     {
@@ -564,7 +566,7 @@ bool wxListCtrl::GetColumn(int col, wxListItem& item) const
         lvCol.cchTextMax = 512;
     }
 
-    bool success = (ListView_GetColumn(GetHwnd(), col, & lvCol) != 0);
+    bool success = ListView_GetColumn(GetHwnd(), col, & lvCol) != 0;
 
     //  item.m_subItem = lvCol.iSubItem;
     item.m_width = lvCol.cx;
@@ -592,41 +594,9 @@ bool wxListCtrl::GetColumn(int col, wxListItem& item) const
 bool wxListCtrl::SetColumn(int col, wxListItem& item)
 {
     LV_COLUMN lvCol;
-    lvCol.mask = 0;
-    lvCol.fmt = 0;
-    lvCol.pszText = NULL;
+    wxConvertToMSWListCol(col, item, lvCol);
 
-    if ( item.m_mask & wxLIST_MASK_TEXT )
-    {
-        lvCol.mask |= LVCF_TEXT;
-        lvCol.pszText = WXSTRINGCAST item.m_text;
-        lvCol.cchTextMax = 0; // Ignored
-    }
-    if ( item.m_mask & wxLIST_MASK_FORMAT )
-    {
-        lvCol.mask |= LVCF_FMT;
-
-        if ( item.m_format == wxLIST_FORMAT_LEFT )
-            lvCol.fmt = LVCFMT_LEFT;
-        if ( item.m_format == wxLIST_FORMAT_RIGHT )
-            lvCol.fmt = LVCFMT_RIGHT;
-        if ( item.m_format == wxLIST_FORMAT_CENTRE )
-            lvCol.fmt = LVCFMT_CENTER;
-    }
-
-    if ( item.m_mask & wxLIST_MASK_WIDTH )
-    {
-        lvCol.mask |= LVCF_WIDTH;
-        lvCol.cx = item.m_width;
-
-        if ( lvCol.cx == wxLIST_AUTOSIZE)
-            lvCol.cx = LVSCW_AUTOSIZE;
-        else if ( lvCol.cx == wxLIST_AUTOSIZE_USEHEADER)
-            lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
-    }
-    lvCol.mask |= LVCF_SUBITEM;
-    lvCol.iSubItem = col;
-    return (ListView_SetColumn(GetHwnd(), col, & lvCol) != 0);
+    return ListView_SetColumn(GetHwnd(), col, &lvCol) != 0;
 }
 
 // Gets the column width
@@ -648,7 +618,7 @@ bool wxListCtrl::SetColumnWidth(int col, int width)
     else if ( width2 == wxLIST_AUTOSIZE_USEHEADER)
         width2 = LVSCW_AUTOSIZE_USEHEADER;
 
-    return (ListView_SetColumnWidth(GetHwnd(), col2, width2) != 0);
+    return ListView_SetColumnWidth(GetHwnd(), col2, width2) != 0;
 }
 
 // Gets the number of items that can fit vertically in the
@@ -1302,49 +1272,33 @@ long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
 long wxListCtrl::InsertColumn(long col, wxListItem& item)
 {
     LV_COLUMN lvCol;
-    lvCol.mask = 0;
-    lvCol.fmt = 0;
-    lvCol.pszText = NULL;
+    wxConvertToMSWListCol(col, item, lvCol);
 
-    if ( item.m_mask & wxLIST_MASK_TEXT )
-    {
-        lvCol.mask |= LVCF_TEXT;
-        lvCol.pszText = WXSTRINGCAST item.m_text;
-        lvCol.cchTextMax = 0; // Ignored
-    }
-    if ( item.m_mask & wxLIST_MASK_FORMAT )
-    {
-        lvCol.mask |= LVCF_FMT;
-
-        if ( item.m_format == wxLIST_FORMAT_LEFT )
-            lvCol.fmt = LVCFMT_LEFT;
-        if ( item.m_format == wxLIST_FORMAT_RIGHT )
-            lvCol.fmt = LVCFMT_RIGHT;
-        if ( item.m_format == wxLIST_FORMAT_CENTRE )
-            lvCol.fmt = LVCFMT_CENTER;
-    }
-
-    lvCol.mask |= LVCF_WIDTH;
-    if ( item.m_mask & wxLIST_MASK_WIDTH )
-    {
-        if ( item.m_width == wxLIST_AUTOSIZE)
-            lvCol.cx = LVSCW_AUTOSIZE;
-        else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER)
-            lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
-        else
-            lvCol.cx = item.m_width;
-    }
-    else
+    if ( !(lvCol.mask & LVCF_WIDTH) )
     {
         // always give some width to the new column: this one is compatible
-        // with wxGTK
+        // with the generic version
+        lvCol.mask |= LVCF_WIDTH;
         lvCol.cx = 80;
     }
 
-    lvCol.mask |= LVCF_SUBITEM;
-    lvCol.iSubItem = col;
+    // when we insert a column which can contain an image, we must specify this
+    // flag right now as doing it later in SetColumn() has no effect
+    //
+    // we use LVCFMT_BITMAP_ON_RIGHT by default because without it there is no
+    // way to dynamically set/clear the bitmap as the column without a bitmap
+    // on the left looks ugly (there is a hole)
+    //
+    // unfortunately with my version of comctl32.dll (5.80), the left column
+    // image is always on the left and it seems that it's a "feature" - I
+    // didn't find any way to work around it in any case
+    if ( lvCol.mask & LVCF_IMAGE )
+    {
+        lvCol.mask |= LVCF_FMT;
+        lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT;
+    }
 
-    bool success = ListView_InsertColumn(GetHwnd(), col, & lvCol) != -1;
+    bool success = ListView_InsertColumn(GetHwnd(), col, &lvCol) != -1;
     if ( success )
     {
         m_colCount++;
@@ -2288,6 +2242,51 @@ static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
         lvItem.mask |= LVIF_PARAM;
 }
 
+static void wxConvertToMSWListCol(int col, const wxListItem& item,
+                                  LV_COLUMN& lvCol)
+{
+    wxZeroMemory(lvCol);
+
+    if ( item.m_mask & wxLIST_MASK_TEXT )
+    {
+        lvCol.mask |= LVCF_TEXT;
+        lvCol.pszText = (wxChar *)item.m_text.c_str(); // cast is safe
+    }
+
+    if ( item.m_mask & wxLIST_MASK_FORMAT )
+    {
+        lvCol.mask |= LVCF_FMT;
+
+        if ( item.m_format == wxLIST_FORMAT_LEFT )
+            lvCol.fmt = LVCFMT_LEFT;
+        else if ( item.m_format == wxLIST_FORMAT_RIGHT )
+            lvCol.fmt = LVCFMT_RIGHT;
+        else if ( item.m_format == wxLIST_FORMAT_CENTRE )
+            lvCol.fmt = LVCFMT_CENTER;
+    }
+
+    if ( item.m_mask & wxLIST_MASK_WIDTH )
+    {
+        lvCol.mask |= LVCF_WIDTH;
+        if ( item.m_width == wxLIST_AUTOSIZE)
+            lvCol.cx = LVSCW_AUTOSIZE;
+        else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER)
+            lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
+        else
+            lvCol.cx = item.m_width;
+    }
+
+    if ( item.m_mask & wxLIST_MASK_IMAGE )
+    {
+        if ( wxTheApp->GetComCtl32Version() >= 470 )
+        {
+            lvCol.mask |= LVCF_IMAGE;
+            lvCol.iImage = item.m_image;
+        }
+        //else: it doesn't support item images anyhow
+    }
+}
+
 // ----------------------------------------------------------------------------
 // List event
 // ----------------------------------------------------------------------------
index e9acd3dc698f923ccd1c3f9ade57f4cfb618cb57..37fa15429cc76a64e213f8c195a65c9a47086f63 100644 (file)
@@ -3303,9 +3303,9 @@ bool wxWindowMSW::MSWOnDrawItem(int id, WXDRAWITEMSTRUCT *itemStruct)
 
 #if wxUSE_MENUS_NATIVE
     // is it a menu item?
-    if ( id == 0 )
+    DRAWITEMSTRUCT *pDrawStruct = (DRAWITEMSTRUCT *)itemStruct;
+    if ( id == 0 && pDrawStruct->CtlType == ODT_MENU )
     {
-        DRAWITEMSTRUCT *pDrawStruct = (DRAWITEMSTRUCT *)itemStruct;
         wxMenuItem *pMenuItem = (wxMenuItem *)(pDrawStruct->itemData);
 
         wxCHECK( pMenuItem->IsKindOf(CLASSINFO(wxMenuItem)), FALSE );
@@ -3344,9 +3344,9 @@ bool wxWindowMSW::MSWOnMeasureItem(int id, WXMEASUREITEMSTRUCT *itemStruct)
 {
 #if wxUSE_OWNER_DRAWN
     // is it a menu item?
-    if ( id == 0 )
+    MEASUREITEMSTRUCT *pMeasureStruct = (MEASUREITEMSTRUCT *)itemStruct;
+    if ( id == 0 && pMeasureStruct->CtlType == ODT_MENU )
     {
-        MEASUREITEMSTRUCT *pMeasureStruct = (MEASUREITEMSTRUCT *)itemStruct;
         wxMenuItem *pMenuItem = (wxMenuItem *)(pMeasureStruct->itemData);
 
         wxCHECK( pMenuItem->IsKindOf(CLASSINFO(wxMenuItem)), FALSE );