]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/treectrl.cpp
Sizing fixes according to patch #1523304.
[wxWidgets.git] / src / msw / treectrl.cpp
index 01ab371bf02d5fe557cf8d9b021a8502830c342f..ef013be6f74d82d57b40e2d62af0e14d04b86d26 100644 (file)
@@ -32,6 +32,7 @@
     #include "wx/dynarray.h"
     #include "wx/log.h"
     #include "wx/app.h"
+    #include "wx/settings.h"
 #endif
 
 #include "wx/msw/private.h"
@@ -46,7 +47,6 @@
 #define wxUSE_COMCTL32_SAFELY 0
 
 #include "wx/imaglist.h"
-#include "wx/settings.h"
 #include "wx/msw/dragimag.h"
 
 // macros to hide the cast ugliness
@@ -313,9 +313,38 @@ public:
 
         // do we have such image?
     bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; }
-        // get image
-    int GetImage(wxTreeItemIcon which) const { return m_images[which]; }
-        // change it
+        // get image, falling back to the other images if this one is not
+        // specified
+    int GetImage(wxTreeItemIcon which) const
+    {
+        int image = m_images[which];
+        if ( image == -1 )
+        {
+            switch ( which )
+            {
+                case wxTreeItemIcon_SelectedExpanded:
+                    image = GetImage(wxTreeItemIcon_Expanded);
+                    if ( image != -1 )
+                        break;
+                    //else: fall through
+
+                case wxTreeItemIcon_Selected:
+                case wxTreeItemIcon_Expanded:
+                    image = GetImage(wxTreeItemIcon_Normal);
+                    break;
+
+                case wxTreeItemIcon_Normal:
+                    // no fallback
+                    break;
+
+                default:
+                    wxFAIL_MSG( _T("unsupported wxTreeItemIcon value") );
+            }
+        }
+
+        return image;
+    }
+        // change the given image
     void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
 
         // get item
@@ -961,7 +990,7 @@ int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
 
     wxTreeItemParam *param = GetItemParam(item);
 
-    return param ? param->GetImage(which) : -1;
+    return param && param->HasImage(which) ? param->GetImage(which) : -1;
 }
 
 void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
@@ -985,18 +1014,7 @@ void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
 
     data->SetImage(image, which);
 
-    // make sure that we have selected images as well
-    if ( which == wxTreeItemIcon_Normal &&
-         !data->HasImage(wxTreeItemIcon_Selected) )
-    {
-        data->SetImage(image, wxTreeItemIcon_Selected);
-    }
-
-    if ( which == wxTreeItemIcon_Expanded &&
-         !data->HasImage(wxTreeItemIcon_SelectedExpanded) )
-    {
-        data->SetImage(image, wxTreeItemIcon_SelectedExpanded);
-    }
+    RefreshItem(item);
 }
 
 wxTreeItemParam *wxTreeCtrl::GetItemParam(const wxTreeItemId& item) const
@@ -1486,27 +1504,23 @@ wxTreeItemId wxTreeCtrl::DoInsertAfter(const wxTreeItemId& parent,
         tvIns.item.cchTextMax = 0;
     }
 
-    // we use the wxTreeItemParam of the LPARAM to return the image
+    // create the param which will store the other item parameters
+    wxTreeItemParam *param = new wxTreeItemParam;
+
+    // we return the images on demand as they depend on whether the item is
+    // expanded or collapsed too in our case
     mask |= TVIF_IMAGE | TVIF_SELECTEDIMAGE;
     tvIns.item.iImage = I_IMAGECALLBACK;
     tvIns.item.iSelectedImage = I_IMAGECALLBACK;
 
-    // create the param and setup the initial image numbers
-    wxTreeItemParam *param = new wxTreeItemParam;
-
     param->SetImage(image, wxTreeItemIcon_Normal);
-
-    // take the same image for selected icon if not specified
-    if ( selectedImage == -1 )
-        param->SetImage(image, wxTreeItemIcon_Selected);
-    else
-        param->SetImage(selectedImage, wxTreeItemIcon_Selected);
+    param->SetImage(selectedImage, wxTreeItemIcon_Selected);
 
     mask |= TVIF_PARAM;
     tvIns.item.lParam = (LPARAM)param;
     tvIns.item.mask = mask;
 
-    HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns);
+    HTREEITEM id = TreeView_InsertItem(GetHwnd(), &tvIns);
     if ( id == 0 )
     {
         wxLogLastError(wxT("TreeView_InsertItem"));
@@ -1672,16 +1686,12 @@ void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
 
     if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) != 0 )
     {
-        wxTreeEvent event(wxEVT_NULL, m_windowId);
-        event.m_item = item;
-        event.SetEventObject(this);
-
         // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
         // itself
-        event.SetEventType(gs_expandEvents[IsExpanded(item) ? IDX_EXPAND
-                                                            : IDX_COLLAPSE]
-                                          [IDX_DONE]);
-
+        wxTreeEvent event(gs_expandEvents[IsExpanded(item) ? IDX_EXPAND
+                                                           : IDX_COLLAPSE]
+                                         [IDX_DONE],
+                           this, item);
         (void)GetEventHandler()->ProcessEvent(event);
     }
     //else: change didn't took place, so do nothing at all
@@ -1769,11 +1779,7 @@ void wxTreeCtrl::SelectItem(const wxTreeItemId& item, bool select)
         // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
         // send them ourselves
 
-        wxTreeEvent event(wxEVT_NULL, m_windowId);
-        event.m_item = item;
-        event.SetEventObject(this);
-
-        event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING);
+        wxTreeEvent event(wxEVT_COMMAND_TREE_SEL_CHANGING, this, item);
         if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
         {
             if ( !TreeView_SelectItem(GetHwnd(), HITEM(item)) )
@@ -1862,7 +1868,7 @@ void wxTreeCtrl::DoEndEditLabel(bool discardChanges)
     DeleteTextCtrl();
 }
 
-wxTreeItemId wxTreeCtrl::DoTreeHitTest(const wxPoint& point, int& flags)
+wxTreeItemId wxTreeCtrl::DoTreeHitTest(const wxPoint& point, int& flags) const
 {
     TV_HITTESTINFO hitTestInfo;
     hitTestInfo.pt.x = (int)point.x;
@@ -2021,11 +2027,9 @@ WXLRESULT wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lPara
         // Convert the screen point to a client point
         wxPoint MenuPoint = ScreenToClient(wxPoint(x, y));
 
-        wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_MENU, GetId() );
-
         // can't use GetSelection() here as it would assert in multiselect mode
-        event.m_item = wxTreeItemId(TreeView_GetSelection(GetHwnd()));
-        event.SetEventObject( this );
+        wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_MENU, this,
+                          wxTreeItemId(TreeView_GetSelection(GetHwnd())));
 
         // Get the bounding rectangle for the item, including the non-text areas
         wxRect ItemRect;
@@ -2247,12 +2251,7 @@ WXLRESULT wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lPara
                     m_dragImage = NULL;
 
                     // generate the drag end event
-                    wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, m_windowId);
-
-                    event.m_item = htItem;
-                    event.m_pointDrag = wxPoint(x, y);
-                    event.SetEventObject(this);
-
+                    wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, this, htItem);
                     (void)GetEventHandler()->ProcessEvent(event);
 
                     // if we don't do it, the tree seems to think that 2 items
@@ -2431,7 +2430,7 @@ wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
 // process WM_NOTIFY Windows message
 bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
 {
-    wxTreeEvent event(wxEVT_NULL, m_windowId);
+    wxTreeEvent event(wxEVT_NULL, this);
     wxEventType eventType = wxEVT_NULL;
     NMHDR *hdr = (NMHDR *)lParam;
 
@@ -2606,16 +2605,13 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
                 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown &&
                      ((info->wVKey == VK_SPACE) || (info->wVKey == VK_RETURN)) )
                 {
-                    wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
-                                       m_windowId);
-                    event2.SetEventObject(this);
-                    if ( !(GetWindowStyle() & wxTR_MULTIPLE) )
-                    {
-                        event2.m_item = GetSelection();
-                    }
-                    //else: don't know how to get it
+                   wxTreeItemId item;
+                   if ( !HasFlag(wxTR_MULTIPLE) )
+                       item = GetSelection();
 
-                    (void)GetEventHandler()->ProcessEvent(event2);
+                   wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
+                                        this, item);
+                   (void)GetEventHandler()->ProcessEvent(event2);
                 }
             }
             break;
@@ -2681,9 +2677,15 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
 
                             wxTreeItemAttr * const attr = it->second;
 
+                            wxTreeViewItem tvItem((void *)nmcd.dwItemSpec,
+                                                  TVIF_STATE, TVIS_DROPHILITED);
+                            DoGetItem(&tvItem);
+                            const UINT tvItemState = tvItem.state;
+
                             // selection colours should override ours,
-                            // otherwise it is too confusing ot the user
-                            if ( !(nmcd.uItemState & CDIS_SELECTED) )
+                            // otherwise it is too confusing to the user
+                            if ( !(nmcd.uItemState & CDIS_SELECTED) &&
+                                 !(tvItemState & TVIS_DROPHILITED) )
                             {
                                 wxColour colBack;
                                 if ( attr->HasBackgroundColour() )
@@ -2697,8 +2699,9 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
                             // colour when we don't have focus (we can't keep
                             // it when we do, it would usually be unreadable on
                             // the almost inverted bg colour...)
-                            if ( !(nmcd.uItemState & CDIS_SELECTED) ||
-                                    FindFocus() != this )
+                            if ( ( !(nmcd.uItemState & CDIS_SELECTED) ||
+                                    FindFocus() != this ) &&
+                                 !(tvItemState & TVIS_DROPHILITED) )
                             {
                                 wxColour colText;
                                 if ( attr->HasTextColour() )
@@ -2777,9 +2780,11 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
             return wxControl::MSWOnNotify(idCtrl, lParam, result);
     }
 
-    event.SetEventObject(this);
     event.SetEventType(eventType);
 
+    if ( event.m_item.IsOk() )
+        event.SetClientObject(GetItemData(event.m_item));
+
     bool processed = GetEventHandler()->ProcessEvent(event);
 
     // post processing
@@ -2815,8 +2820,6 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
                 //     with many items is just too slow)
                 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
 
-                wxTreeItemId item = event.m_item;
-
                 wxTreeItemParam *param =
                         (wxTreeItemParam *)tv->itemOld.lParam;
                 delete param;
@@ -2918,7 +2921,7 @@ bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
                 wxTreeItemId item = event.m_item;
                 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
 
-                wxTreeItemParam *param = GetItemParam(item);
+                const wxTreeItemParam * const param = GetItemParam(item);
                 if ( !param )
                     break;