]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/headerctrl.cpp
use a slightly less ugly way to conditionally suppress unused parameter warnings
[wxWidgets.git] / src / msw / headerctrl.cpp
index e60b1bbc1bd9d50f427471d66b6566f9787460d6..d6ad5ccddaad6e63e8d7e36efe1fbf91b81c7be7 100644 (file)
@@ -151,16 +151,22 @@ void wxHeaderCtrl::DoSetCount(unsigned int count)
     // and add the new ones
     for ( n = 0; n < count; n++ )
     {
-        DoSetOrInsertItem(Insert, n);
+        DoInsertItem(n);
     }
 }
 
 void wxHeaderCtrl::DoUpdate(unsigned int idx)
 {
-    DoSetOrInsertItem(Set, idx);
+    // the native control does provide Header_SetItem() but it's inconvenient
+    // to use it because it sends HDN_ITEMCHANGING messages and we'd have to
+    // arrange not to block setting the width from there and the logic would be
+    // more complicated as we'd have to reset the old values as well as setting
+    // the new ones -- so instead just recreate the column
+    Header_DeleteItem(GetHwnd(), idx);
+    DoInsertItem(idx);
 }
 
-void wxHeaderCtrl::DoSetOrInsertItem(Operation oper, unsigned int idx)
+void wxHeaderCtrl::DoInsertItem(unsigned int idx)
 {
     const wxHeaderColumnBase& col = GetColumn(idx);
 
@@ -168,41 +174,44 @@ void wxHeaderCtrl::DoSetOrInsertItem(Operation oper, unsigned int idx)
 
     // notice that we need to store the string we use the pointer to until we
     // pass it to the control
-    wxWxCharBuffer buf;
-    if ( !col.GetTitle().empty() )
-    {
-        hdi.mask |= HDI_TEXT;
-
-        buf = col.GetTitle().wx_str();
-        hdi.pszText = buf.data();
-        hdi.cchTextMax = wxStrlen(buf);
-    }
+    hdi.mask |= HDI_TEXT;
+    wxWxCharBuffer buf = col.GetTitle().wx_str();
+    hdi.pszText = buf.data();
+    hdi.cchTextMax = wxStrlen(buf);
 
     const wxBitmap bmp = col.GetBitmap();
     if ( bmp.IsOk() )
     {
-        const int bmpWidth = bmp.GetWidth(),
-                  bmpHeight = bmp.GetHeight();
+        hdi.mask |= HDI_IMAGE;
 
-        if ( !m_imageList )
+        if ( bmp.IsOk() )
         {
-            m_imageList = new wxImageList(bmpWidth, bmpHeight);
-            Header_SetImageList(GetHwnd(), GetHimagelistOf(m_imageList));
+            const int bmpWidth = bmp.GetWidth(),
+                      bmpHeight = bmp.GetHeight();
+
+            if ( !m_imageList )
+            {
+                m_imageList = new wxImageList(bmpWidth, bmpHeight);
+                Header_SetImageList(GetHwnd(), GetHimagelistOf(m_imageList));
+            }
+            else // already have an image list
+            {
+                // check that all bitmaps we use have the same size
+                int imageWidth,
+                    imageHeight;
+                m_imageList->GetSize(0, imageWidth, imageHeight);
+
+                wxASSERT_MSG( imageWidth == bmpWidth && imageHeight == bmpHeight,
+                              "all column bitmaps must have the same size" );
+            }
+
+            m_imageList->Add(bmp);
+            hdi.iImage = m_imageList->GetImageCount() - 1;
         }
-        else // already have an image list
+        else // no bitmap but we still need to update the item
         {
-            // check that all bitmaps we use have the same size
-            int imageWidth,
-                imageHeight;
-            m_imageList->GetSize(0, imageWidth, imageHeight);
-
-            wxASSERT_MSG( imageWidth == bmpWidth && imageHeight == bmpHeight,
-                          "all column bitmaps must have the same size" );
+            hdi.iImage = I_IMAGENONE;
         }
-
-        m_imageList->Add(bmp);
-        hdi.mask |= HDI_IMAGE;
-        hdi.iImage = m_imageList->GetImageCount() - 1;
     }
 
     if ( col.GetAlignment() != wxALIGN_NOT )
@@ -240,19 +249,9 @@ void wxHeaderCtrl::DoSetOrInsertItem(Operation oper, unsigned int idx)
         hdi.cxy = col.IsHidden() ? 0 : col.GetWidth();
     }
 
-    const LRESULT rc = ::SendMessage(GetHwnd(),
-                                     oper == Set ? HDM_SETITEM : HDM_INSERTITEM,
-                                     idx,
-                                     (LPARAM)&hdi);
-    if ( oper == Set )
-    {
-        if ( !rc )
-            wxLogLastError(_T("Header_SetItem()"));
-    }
-    else // Insert
+    if ( ::SendMessage(GetHwnd(), HDM_INSERTITEM, idx, (LPARAM)&hdi) == -1 )
     {
-        if ( rc == -1 )
-            wxLogLastError(_T("Header_InsertItem()"));
+        wxLogLastError(_T("Header_InsertItem()"));
     }
 }
 
@@ -295,7 +294,9 @@ bool wxHeaderCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
     wxEventType evtType = wxEVT_NULL;
     int idx = nmhdr->iItem;
     int width = 0;
-    switch ( const UINT code = nmhdr->hdr.code )
+    bool cancelled = false;
+    const UINT code = nmhdr->hdr.code;
+    switch ( code )
     {
         // click events
         // ------------
@@ -331,21 +332,54 @@ bool wxHeaderCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
         // ASCII and Unicode versions of this message
         case HDN_BEGINTRACKA:
         case HDN_BEGINTRACKW:
-            evtType = wxEVT_COMMAND_HEADER_BEGIN_DRAG;
+            // non-resizeable columns can't be resized no matter what, don't
+            // even generate any events for them
+            if ( !GetColumn(idx).IsResizeable() )
+            {
+                *result = TRUE;
+
+                return true;
+            }
+
+            evtType = wxEVT_COMMAND_HEADER_BEGIN_RESIZE;
             // fall through
 
         case HDN_TRACKA:
         case HDN_TRACKW:
             if ( evtType == wxEVT_NULL )
-                evtType = wxEVT_COMMAND_HEADER_DRAGGING;
+                evtType = wxEVT_COMMAND_HEADER_RESIZING;
             // fall through
 
         case HDN_ENDTRACKA:
         case HDN_ENDTRACKW:
+            width = nmhdr->pitem->cxy;
+
             if ( evtType == wxEVT_NULL )
-                evtType = wxEVT_COMMAND_HEADER_END_DRAG;
+            {
+                evtType = wxEVT_COMMAND_HEADER_END_RESIZE;
 
-            width = nmhdr->pitem->cxy;
+                // don't generate events with invalid width
+                const int minWidth = GetColumn(idx).GetMinWidth();
+                if ( width < minWidth )
+                    width = minWidth;
+            }
+            break;
+
+        case HDN_ITEMCHANGING:
+            if ( nmhdr->pitem && (nmhdr->pitem->mask & HDI_WIDTH) )
+            {
+                // prevent the column from being shrunk beneath its min width
+                if ( nmhdr->pitem->cxy < GetColumn(idx).GetMinWidth() )
+                {
+                    *result = TRUE;
+
+                    return true;
+                }
+            }
+            break;
+
+        case NM_RELEASEDCAPTURE:
+            cancelled = true;
             break;
     }
 
@@ -357,6 +391,8 @@ bool wxHeaderCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
         event.SetEventObject(this);
         event.SetColumn(idx);
         event.SetWidth(width);
+        if ( cancelled )
+            event.SetCancelled();
 
         if ( GetEventHandler()->ProcessEvent(event) )
         {