get rid of wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE code, it's never used and just makes...
[wxWidgets.git] / src / msw / treectrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/treectrl.cpp
3 // Purpose: wxTreeCtrl
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
6 // Created: 1997
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_TREECTRL
28
29 #include "wx/treectrl.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/msw/missing.h"
34 #include "wx/dynarray.h"
35 #include "wx/log.h"
36 #include "wx/app.h"
37 #include "wx/settings.h"
38 #endif
39
40 #include "wx/msw/private.h"
41
42 // Set this to 1 to be _absolutely_ sure that repainting will work for all
43 // comctl32.dll versions
44 #define wxUSE_COMCTL32_SAFELY 0
45
46 #include "wx/imaglist.h"
47 #include "wx/msw/dragimag.h"
48
49 // macros to hide the cast ugliness
50 // --------------------------------
51
52 // get HTREEITEM from wxTreeItemId
53 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
54
55 // ----------------------------------------------------------------------------
56 // private functions
57 // ----------------------------------------------------------------------------
58
59 // wrapper for TreeView_HitTest
60 static HTREEITEM GetItemFromPoint(HWND hwndTV, int x, int y)
61 {
62 TV_HITTESTINFO tvht;
63 tvht.pt.x = x;
64 tvht.pt.y = y;
65
66 return (HTREEITEM)TreeView_HitTest(hwndTV, &tvht);
67 }
68
69 // ----------------------------------------------------------------------------
70 // private classes
71 // ----------------------------------------------------------------------------
72
73 // a convenient wrapper around TV_ITEM struct which adds a ctor
74 #ifdef __VISUALC__
75 #pragma warning( disable : 4097 ) // inheriting from typedef
76 #endif
77
78 struct wxTreeViewItem : public TV_ITEM
79 {
80 wxTreeViewItem(const wxTreeItemId& item, // the item handle
81 UINT mask_, // fields which are valid
82 UINT stateMask_ = 0) // for TVIF_STATE only
83 {
84 wxZeroMemory(*this);
85
86 // hItem member is always valid
87 mask = mask_ | TVIF_HANDLE;
88 stateMask = stateMask_;
89 hItem = HITEM(item);
90 }
91 };
92
93 // ----------------------------------------------------------------------------
94 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
95 //
96 // We need this for a couple of reasons:
97 //
98 // 1) This class is needed for support of different images: the Win32 common
99 // control natively supports only 2 images (the normal one and another for the
100 // selected state). We wish to provide support for 2 more of them for folder
101 // items (i.e. those which have children): for expanded state and for expanded
102 // selected state. For this we use this structure to store the additional items
103 // images.
104 //
105 // 2) This class is also needed to hold the HITEM so that we can sort
106 // it correctly in the MSW sort callback.
107 //
108 // In addition it makes other workarounds such as this easier and helps
109 // simplify the code.
110 // ----------------------------------------------------------------------------
111
112 class wxTreeItemParam
113 {
114 public:
115 wxTreeItemParam()
116 {
117 m_data = NULL;
118
119 for ( size_t n = 0; n < WXSIZEOF(m_images); n++ )
120 {
121 m_images[n] = -1;
122 }
123 }
124
125 // dtor deletes the associated data as well
126 virtual ~wxTreeItemParam() { delete m_data; }
127
128 // accessors
129 // get the real data associated with the item
130 wxTreeItemData *GetData() const { return m_data; }
131 // change it
132 void SetData(wxTreeItemData *data) { m_data = data; }
133
134 // do we have such image?
135 bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; }
136 // get image, falling back to the other images if this one is not
137 // specified
138 int GetImage(wxTreeItemIcon which) const
139 {
140 int image = m_images[which];
141 if ( image == -1 )
142 {
143 switch ( which )
144 {
145 case wxTreeItemIcon_SelectedExpanded:
146 image = GetImage(wxTreeItemIcon_Expanded);
147 if ( image != -1 )
148 break;
149 //else: fall through
150
151 case wxTreeItemIcon_Selected:
152 case wxTreeItemIcon_Expanded:
153 image = GetImage(wxTreeItemIcon_Normal);
154 break;
155
156 case wxTreeItemIcon_Normal:
157 // no fallback
158 break;
159
160 default:
161 wxFAIL_MSG( _T("unsupported wxTreeItemIcon value") );
162 }
163 }
164
165 return image;
166 }
167 // change the given image
168 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
169
170 // get item
171 const wxTreeItemId& GetItem() const { return m_item; }
172 // set item
173 void SetItem(const wxTreeItemId& item) { m_item = item; }
174
175 protected:
176 // all the images associated with the item
177 int m_images[wxTreeItemIcon_Max];
178
179 // item for sort callbacks
180 wxTreeItemId m_item;
181
182 // the real client data
183 wxTreeItemData *m_data;
184
185 DECLARE_NO_COPY_CLASS(wxTreeItemParam)
186 };
187
188 // wxVirutalNode is used in place of a single root when 'hidden' root is
189 // specified.
190 class wxVirtualNode : public wxTreeViewItem
191 {
192 public:
193 wxVirtualNode(wxTreeItemParam *param)
194 : wxTreeViewItem(TVI_ROOT, 0)
195 {
196 m_param = param;
197 }
198
199 ~wxVirtualNode()
200 {
201 delete m_param;
202 }
203
204 wxTreeItemParam *GetParam() const { return m_param; }
205 void SetParam(wxTreeItemParam *param) { delete m_param; m_param = param; }
206
207 private:
208 wxTreeItemParam *m_param;
209
210 DECLARE_NO_COPY_CLASS(wxVirtualNode)
211 };
212
213 #ifdef __VISUALC__
214 #pragma warning( default : 4097 )
215 #endif
216
217 // a macro to get the virtual root, returns NULL if none
218 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
219
220 // returns true if the item is the virtual root
221 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
222
223 // a class which encapsulates the tree traversal logic: it vists all (unless
224 // OnVisit() returns false) items under the given one
225 class wxTreeTraversal
226 {
227 public:
228 wxTreeTraversal(const wxTreeCtrl *tree)
229 {
230 m_tree = tree;
231 }
232
233 // give it a virtual dtor: not really needed as the class is never used
234 // polymorphically and not even allocated on heap at all, but this is safer
235 // (in case it ever is) and silences the compiler warnings for now
236 virtual ~wxTreeTraversal() { }
237
238 // do traverse the tree: visit all items (recursively by default) under the
239 // given one; return true if all items were traversed or false if the
240 // traversal was aborted because OnVisit returned false
241 bool DoTraverse(const wxTreeItemId& root, bool recursively = true);
242
243 // override this function to do whatever is needed for each item, return
244 // false to stop traversing
245 virtual bool OnVisit(const wxTreeItemId& item) = 0;
246
247 protected:
248 const wxTreeCtrl *GetTree() const { return m_tree; }
249
250 private:
251 bool Traverse(const wxTreeItemId& root, bool recursively);
252
253 const wxTreeCtrl *m_tree;
254
255 DECLARE_NO_COPY_CLASS(wxTreeTraversal)
256 };
257
258 // internal class for getting the selected items
259 class TraverseSelections : public wxTreeTraversal
260 {
261 public:
262 TraverseSelections(const wxTreeCtrl *tree,
263 wxArrayTreeItemIds& selections)
264 : wxTreeTraversal(tree), m_selections(selections)
265 {
266 m_selections.Empty();
267
268 if (tree->GetCount() > 0)
269 DoTraverse(tree->GetRootItem());
270 }
271
272 virtual bool OnVisit(const wxTreeItemId& item)
273 {
274 const wxTreeCtrl * const tree = GetTree();
275
276 // can't visit a virtual node.
277 if ( (tree->GetRootItem() == item) && tree->HasFlag(wxTR_HIDE_ROOT) )
278 {
279 return true;
280 }
281
282 if ( ::IsItemSelected(GetHwndOf(tree), HITEM(item)) )
283 {
284 m_selections.Add(item);
285 }
286
287 return true;
288 }
289
290 size_t GetCount() const { return m_selections.GetCount(); }
291
292 private:
293 wxArrayTreeItemIds& m_selections;
294
295 DECLARE_NO_COPY_CLASS(TraverseSelections)
296 };
297
298 // internal class for counting tree items
299 class TraverseCounter : public wxTreeTraversal
300 {
301 public:
302 TraverseCounter(const wxTreeCtrl *tree,
303 const wxTreeItemId& root,
304 bool recursively)
305 : wxTreeTraversal(tree)
306 {
307 m_count = 0;
308
309 DoTraverse(root, recursively);
310 }
311
312 virtual bool OnVisit(const wxTreeItemId& WXUNUSED(item))
313 {
314 m_count++;
315
316 return true;
317 }
318
319 size_t GetCount() const { return m_count; }
320
321 private:
322 size_t m_count;
323
324 DECLARE_NO_COPY_CLASS(TraverseCounter)
325 };
326
327 // ----------------------------------------------------------------------------
328 // wxWin macros
329 // ----------------------------------------------------------------------------
330
331 #if wxUSE_EXTENDED_RTTI
332 WX_DEFINE_FLAGS( wxTreeCtrlStyle )
333
334 wxBEGIN_FLAGS( wxTreeCtrlStyle )
335 // new style border flags, we put them first to
336 // use them for streaming out
337 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
338 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
339 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
340 wxFLAGS_MEMBER(wxBORDER_RAISED)
341 wxFLAGS_MEMBER(wxBORDER_STATIC)
342 wxFLAGS_MEMBER(wxBORDER_NONE)
343
344 // old style border flags
345 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
346 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
347 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
348 wxFLAGS_MEMBER(wxRAISED_BORDER)
349 wxFLAGS_MEMBER(wxSTATIC_BORDER)
350 wxFLAGS_MEMBER(wxBORDER)
351
352 // standard window styles
353 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
354 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
355 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
356 wxFLAGS_MEMBER(wxWANTS_CHARS)
357 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
358 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
359 wxFLAGS_MEMBER(wxVSCROLL)
360 wxFLAGS_MEMBER(wxHSCROLL)
361
362 wxFLAGS_MEMBER(wxTR_EDIT_LABELS)
363 wxFLAGS_MEMBER(wxTR_NO_BUTTONS)
364 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS)
365 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS)
366 wxFLAGS_MEMBER(wxTR_NO_LINES)
367 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT)
368 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT)
369 wxFLAGS_MEMBER(wxTR_HIDE_ROOT)
370 wxFLAGS_MEMBER(wxTR_ROW_LINES)
371 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT)
372 wxFLAGS_MEMBER(wxTR_SINGLE)
373 wxFLAGS_MEMBER(wxTR_MULTIPLE)
374 wxFLAGS_MEMBER(wxTR_EXTENDED)
375 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE)
376
377 wxEND_FLAGS( wxTreeCtrlStyle )
378
379 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl, wxControl,"wx/treectrl.h")
380
381 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl)
382 wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent )
383 wxEVENT_RANGE_PROPERTY( TreeEvent , wxEVT_COMMAND_TREE_BEGIN_DRAG , wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK , wxTreeEvent )
384 wxPROPERTY_FLAGS( WindowStyle , wxTreeCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
385 wxEND_PROPERTIES_TABLE()
386
387 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl)
388 wxEND_HANDLERS_TABLE()
389
390 wxCONSTRUCTOR_5( wxTreeCtrl , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
391 #else
392 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl)
393 #endif
394
395 // ----------------------------------------------------------------------------
396 // constants
397 // ----------------------------------------------------------------------------
398
399 // indices in gs_expandEvents table below
400 enum
401 {
402 IDX_COLLAPSE,
403 IDX_EXPAND,
404 IDX_WHAT_MAX
405 };
406
407 enum
408 {
409 IDX_DONE,
410 IDX_DOING,
411 IDX_HOW_MAX
412 };
413
414 // handy table for sending events - it has to be initialized during run-time
415 // now so can't be const any more
416 static /* const */ wxEventType gs_expandEvents[IDX_WHAT_MAX][IDX_HOW_MAX];
417
418 /*
419 but logically it's a const table with the following entries:
420 =
421 {
422 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
423 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
424 };
425 */
426
427 // ============================================================================
428 // implementation
429 // ============================================================================
430
431 // ----------------------------------------------------------------------------
432 // tree traversal
433 // ----------------------------------------------------------------------------
434
435 bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively)
436 {
437 if ( !OnVisit(root) )
438 return false;
439
440 return Traverse(root, recursively);
441 }
442
443 bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively)
444 {
445 wxTreeItemIdValue cookie;
446 wxTreeItemId child = m_tree->GetFirstChild(root, cookie);
447 while ( child.IsOk() )
448 {
449 // depth first traversal
450 if ( recursively && !Traverse(child, true) )
451 return false;
452
453 if ( !OnVisit(child) )
454 return false;
455
456 child = m_tree->GetNextChild(root, cookie);
457 }
458
459 return true;
460 }
461
462 // ----------------------------------------------------------------------------
463 // construction and destruction
464 // ----------------------------------------------------------------------------
465
466 void wxTreeCtrl::Init()
467 {
468 m_textCtrl = NULL;
469 m_hasAnyAttr = false;
470 m_dragImage = NULL;
471 m_pVirtualRoot = NULL;
472
473 // initialize the global array of events now as it can't be done statically
474 // with the wxEVT_XXX values being allocated during run-time only
475 gs_expandEvents[IDX_COLLAPSE][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED;
476 gs_expandEvents[IDX_COLLAPSE][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING;
477 gs_expandEvents[IDX_EXPAND][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_EXPANDED;
478 gs_expandEvents[IDX_EXPAND][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_EXPANDING;
479 }
480
481 bool wxTreeCtrl::Create(wxWindow *parent,
482 wxWindowID id,
483 const wxPoint& pos,
484 const wxSize& size,
485 long style,
486 const wxValidator& validator,
487 const wxString& name)
488 {
489 Init();
490
491 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
492 style |= wxBORDER_SUNKEN;
493
494 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
495 return false;
496
497 DWORD exStyle = 0;
498 DWORD wstyle = MSWGetStyle(m_windowStyle, & exStyle);
499 wstyle |= WS_TABSTOP | TVS_SHOWSELALWAYS;
500
501 if ((m_windowStyle & wxTR_NO_LINES) == 0)
502 wstyle |= TVS_HASLINES;
503 if ( m_windowStyle & wxTR_HAS_BUTTONS )
504 wstyle |= TVS_HASBUTTONS;
505
506 if ( m_windowStyle & wxTR_EDIT_LABELS )
507 wstyle |= TVS_EDITLABELS;
508
509 if ( m_windowStyle & wxTR_LINES_AT_ROOT )
510 wstyle |= TVS_LINESATROOT;
511
512 if ( m_windowStyle & wxTR_FULL_ROW_HIGHLIGHT )
513 {
514 if ( wxApp::GetComCtl32Version() >= 471 )
515 wstyle |= TVS_FULLROWSELECT;
516 }
517
518 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
519 // Need so that TVN_GETINFOTIP messages will be sent
520 wstyle |= TVS_INFOTIP;
521 #endif
522
523 // Create the tree control.
524 if ( !MSWCreateControl(WC_TREEVIEW, wstyle, pos, size) )
525 return false;
526
527 #if wxUSE_COMCTL32_SAFELY
528 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
529 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
530 #elif 1
531 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
532 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
533 #else
534 // This works around a bug in the Windows tree control whereby for some versions
535 // of comctrl32, setting any colour actually draws the background in black.
536 // This will initialise the background to the system colour.
537 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
538 // Assume the user has an updated comctl32.dll.
539 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0,-1);
540 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
541 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
542 #endif
543
544
545 // VZ: this is some experimental code which may be used to get the
546 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
547 // AFAIK, the standard DLL does about the same thing anyhow.
548 #if 0
549 if ( m_windowStyle & wxTR_MULTIPLE )
550 {
551 wxBitmap bmp;
552
553 // create the DC compatible with the current screen
554 HDC hdcMem = CreateCompatibleDC(NULL);
555
556 // create a mono bitmap of the standard size
557 int x = ::GetSystemMetrics(SM_CXMENUCHECK);
558 int y = ::GetSystemMetrics(SM_CYMENUCHECK);
559 wxImageList imagelistCheckboxes(x, y, false, 2);
560 HBITMAP hbmpCheck = CreateBitmap(x, y, // bitmap size
561 1, // # of color planes
562 1, // # bits needed for one pixel
563 0); // array containing colour data
564 SelectObject(hdcMem, hbmpCheck);
565
566 // then draw a check mark into it
567 RECT rect = { 0, 0, x, y };
568 if ( !::DrawFrameControl(hdcMem, &rect,
569 DFC_BUTTON,
570 DFCS_BUTTONCHECK | DFCS_CHECKED) )
571 {
572 wxLogLastError(wxT("DrawFrameControl(check)"));
573 }
574
575 bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
576 imagelistCheckboxes.Add(bmp);
577
578 if ( !::DrawFrameControl(hdcMem, &rect,
579 DFC_BUTTON,
580 DFCS_BUTTONCHECK) )
581 {
582 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
583 }
584
585 bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
586 imagelistCheckboxes.Add(bmp);
587
588 // clean up
589 ::DeleteDC(hdcMem);
590
591 // set the imagelist
592 SetStateImageList(&imagelistCheckboxes);
593 }
594 #endif // 0
595
596 wxSetCCUnicodeFormat(GetHwnd());
597
598 return true;
599 }
600
601 wxTreeCtrl::~wxTreeCtrl()
602 {
603 // delete any attributes
604 if ( m_hasAnyAttr )
605 {
606 WX_CLEAR_HASH_MAP(wxMapTreeAttr, m_attrs);
607
608 // prevent TVN_DELETEITEM handler from deleting the attributes again!
609 m_hasAnyAttr = false;
610 }
611
612 DeleteTextCtrl();
613
614 // delete user data to prevent memory leaks
615 // also deletes hidden root node storage.
616 DeleteAllItems();
617 }
618
619 // ----------------------------------------------------------------------------
620 // accessors
621 // ----------------------------------------------------------------------------
622
623 /* static */ wxVisualAttributes
624 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
625 {
626 wxVisualAttributes attrs = GetCompositeControlsDefaultAttributes(variant);
627
628 // common controls have their own default font
629 attrs.font = wxGetCCDefaultFont();
630
631 return attrs;
632 }
633
634
635 // simple wrappers which add error checking in debug mode
636
637 bool wxTreeCtrl::DoGetItem(wxTreeViewItem *tvItem) const
638 {
639 wxCHECK_MSG( tvItem->hItem != TVI_ROOT, false,
640 _T("can't retrieve virtual root item") );
641
642 if ( !TreeView_GetItem(GetHwnd(), tvItem) )
643 {
644 wxLogLastError(wxT("TreeView_GetItem"));
645
646 return false;
647 }
648
649 return true;
650 }
651
652 void wxTreeCtrl::DoSetItem(wxTreeViewItem *tvItem)
653 {
654 if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 )
655 {
656 wxLogLastError(wxT("TreeView_SetItem"));
657 }
658 }
659
660 unsigned int wxTreeCtrl::GetCount() const
661 {
662 return (unsigned int)TreeView_GetCount(GetHwnd());
663 }
664
665 unsigned int wxTreeCtrl::GetIndent() const
666 {
667 return TreeView_GetIndent(GetHwnd());
668 }
669
670 void wxTreeCtrl::SetIndent(unsigned int indent)
671 {
672 TreeView_SetIndent(GetHwnd(), indent);
673 }
674
675 void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
676 {
677 // no error return
678 (void) TreeView_SetImageList(GetHwnd(),
679 imageList ? imageList->GetHIMAGELIST() : 0,
680 which);
681 }
682
683 void wxTreeCtrl::SetImageList(wxImageList *imageList)
684 {
685 if (m_ownsImageListNormal)
686 delete m_imageListNormal;
687
688 SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL);
689 m_ownsImageListNormal = false;
690 }
691
692 void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
693 {
694 if (m_ownsImageListState) delete m_imageListState;
695 SetAnyImageList(m_imageListState = imageList, TVSIL_STATE);
696 m_ownsImageListState = false;
697 }
698
699 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
700 bool recursively) const
701 {
702 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
703
704 TraverseCounter counter(this, item, recursively);
705 return counter.GetCount() - 1;
706 }
707
708 // ----------------------------------------------------------------------------
709 // control colours
710 // ----------------------------------------------------------------------------
711
712 bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour)
713 {
714 #if !wxUSE_COMCTL32_SAFELY
715 if ( !wxWindowBase::SetBackgroundColour(colour) )
716 return false;
717
718 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel());
719 #endif
720
721 return true;
722 }
723
724 bool wxTreeCtrl::SetForegroundColour(const wxColour &colour)
725 {
726 #if !wxUSE_COMCTL32_SAFELY
727 if ( !wxWindowBase::SetForegroundColour(colour) )
728 return false;
729
730 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel());
731 #endif
732
733 return true;
734 }
735
736 // ----------------------------------------------------------------------------
737 // Item access
738 // ----------------------------------------------------------------------------
739
740 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId& item) const
741 {
742 return HITEM(item) == TVI_ROOT && HasFlag(wxTR_HIDE_ROOT);
743 }
744
745 wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
746 {
747 wxCHECK_MSG( item.IsOk(), wxEmptyString, wxT("invalid tree item") );
748
749 wxChar buf[512]; // the size is arbitrary...
750
751 wxTreeViewItem tvItem(item, TVIF_TEXT);
752 tvItem.pszText = buf;
753 tvItem.cchTextMax = WXSIZEOF(buf);
754 if ( !DoGetItem(&tvItem) )
755 {
756 // don't return some garbage which was on stack, but an empty string
757 buf[0] = wxT('\0');
758 }
759
760 return wxString(buf);
761 }
762
763 void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
764 {
765 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
766
767 if ( IS_VIRTUAL_ROOT(item) )
768 return;
769
770 wxTreeViewItem tvItem(item, TVIF_TEXT);
771 tvItem.pszText = (wxChar *)text.c_str(); // conversion is ok
772 DoSetItem(&tvItem);
773
774 // when setting the text of the item being edited, the text control should
775 // be updated to reflect the new text as well, otherwise calling
776 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
777 //
778 // don't use GetEditControl() here because m_textCtrl is not set yet
779 HWND hwndEdit = TreeView_GetEditControl(GetHwnd());
780 if ( hwndEdit )
781 {
782 if ( item == m_idEdited )
783 {
784 ::SetWindowText(hwndEdit, text);
785 }
786 }
787 }
788
789 int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
790 wxTreeItemIcon which) const
791 {
792 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
793
794 if ( IsHiddenRoot(item) )
795 {
796 // no images for hidden root item
797 return -1;
798 }
799
800 wxTreeItemParam *param = GetItemParam(item);
801
802 return param && param->HasImage(which) ? param->GetImage(which) : -1;
803 }
804
805 void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
806 wxTreeItemIcon which)
807 {
808 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
809 wxCHECK_RET( which >= 0 &&
810 which < wxTreeItemIcon_Max,
811 wxT("invalid image index"));
812
813
814 if ( IsHiddenRoot(item) )
815 {
816 // no images for hidden root item
817 return;
818 }
819
820 wxTreeItemParam *data = GetItemParam(item);
821 if ( !data )
822 return;
823
824 data->SetImage(image, which);
825
826 RefreshItem(item);
827 }
828
829 wxTreeItemParam *wxTreeCtrl::GetItemParam(const wxTreeItemId& item) const
830 {
831 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
832
833 wxTreeViewItem tvItem(item, TVIF_PARAM);
834
835 // hidden root may still have data.
836 if ( IS_VIRTUAL_ROOT(item) )
837 {
838 return GET_VIRTUAL_ROOT()->GetParam();
839 }
840
841 // visible node.
842 if ( !DoGetItem(&tvItem) )
843 {
844 return NULL;
845 }
846
847 return (wxTreeItemParam *)tvItem.lParam;
848 }
849
850 wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
851 {
852 wxTreeItemParam *data = GetItemParam(item);
853
854 return data ? data->GetData() : NULL;
855 }
856
857 void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
858 {
859 // first, associate this piece of data with this item
860 if ( data )
861 {
862 data->SetId(item);
863 }
864
865 wxTreeItemParam *param = GetItemParam(item);
866
867 wxCHECK_RET( param, wxT("failed to change tree items data") );
868
869 param->SetData(data);
870 }
871
872 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
873 {
874 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
875
876 if ( IS_VIRTUAL_ROOT(item) )
877 return;
878
879 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
880 tvItem.cChildren = (int)has;
881 DoSetItem(&tvItem);
882 }
883
884 void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
885 {
886 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
887
888 if ( IS_VIRTUAL_ROOT(item) )
889 return;
890
891 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
892 tvItem.state = bold ? TVIS_BOLD : 0;
893 DoSetItem(&tvItem);
894 }
895
896 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
897 {
898 if ( IS_VIRTUAL_ROOT(item) )
899 return;
900
901 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
902 tvItem.state = highlight ? TVIS_DROPHILITED : 0;
903 DoSetItem(&tvItem);
904 }
905
906 void wxTreeCtrl::RefreshItem(const wxTreeItemId& item)
907 {
908 if ( IS_VIRTUAL_ROOT(item) )
909 return;
910
911 wxRect rect;
912 if ( GetBoundingRect(item, rect) )
913 {
914 RefreshRect(rect);
915 }
916 }
917
918 wxColour wxTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
919 {
920 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
921
922 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
923 return it == m_attrs.end() ? wxNullColour : it->second->GetTextColour();
924 }
925
926 wxColour wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const
927 {
928 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
929
930 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
931 return it == m_attrs.end() ? wxNullColour : it->second->GetBackgroundColour();
932 }
933
934 wxFont wxTreeCtrl::GetItemFont(const wxTreeItemId& item) const
935 {
936 wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") );
937
938 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
939 return it == m_attrs.end() ? wxNullFont : it->second->GetFont();
940 }
941
942 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
943 const wxColour& col)
944 {
945 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
946
947 wxTreeItemAttr *attr;
948 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
949 if ( it == m_attrs.end() )
950 {
951 m_hasAnyAttr = true;
952
953 m_attrs[item.m_pItem] =
954 attr = new wxTreeItemAttr;
955 }
956 else
957 {
958 attr = it->second;
959 }
960
961 attr->SetTextColour(col);
962
963 RefreshItem(item);
964 }
965
966 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
967 const wxColour& col)
968 {
969 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
970
971 wxTreeItemAttr *attr;
972 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
973 if ( it == m_attrs.end() )
974 {
975 m_hasAnyAttr = true;
976
977 m_attrs[item.m_pItem] =
978 attr = new wxTreeItemAttr;
979 }
980 else // already in the hash
981 {
982 attr = it->second;
983 }
984
985 attr->SetBackgroundColour(col);
986
987 RefreshItem(item);
988 }
989
990 void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
991 {
992 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
993
994 wxTreeItemAttr *attr;
995 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
996 if ( it == m_attrs.end() )
997 {
998 m_hasAnyAttr = true;
999
1000 m_attrs[item.m_pItem] =
1001 attr = new wxTreeItemAttr;
1002 }
1003 else // already in the hash
1004 {
1005 attr = it->second;
1006 }
1007
1008 attr->SetFont(font);
1009
1010 RefreshItem(item);
1011 }
1012
1013 // ----------------------------------------------------------------------------
1014 // Item status
1015 // ----------------------------------------------------------------------------
1016
1017 bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
1018 {
1019 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1020
1021 if ( item == wxTreeItemId(TVI_ROOT) )
1022 {
1023 // virtual (hidden) root is never visible
1024 return false;
1025 }
1026
1027 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1028 RECT rect;
1029
1030 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1031 // the HTREEITEM with TVM_GETITEMRECT
1032 *(HTREEITEM *)&rect = HITEM(item);
1033
1034 // true means to get rect for just the text, not the whole line
1035 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT, true, (LPARAM)&rect) )
1036 {
1037 // if TVM_GETITEMRECT returned false, then the item is definitely not
1038 // visible (because its parent is not expanded)
1039 return false;
1040 }
1041
1042 // however if it returned true, the item might still be outside the
1043 // currently visible part of the tree, test for it (notice that partly
1044 // visible means visible here)
1045 return rect.bottom > 0 && rect.top < GetClientSize().y;
1046 }
1047
1048 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
1049 {
1050 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1051
1052 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1053 DoGetItem(&tvItem);
1054
1055 return tvItem.cChildren != 0;
1056 }
1057
1058 bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
1059 {
1060 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1061
1062 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
1063 DoGetItem(&tvItem);
1064
1065 return (tvItem.state & TVIS_EXPANDED) != 0;
1066 }
1067
1068 bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
1069 {
1070 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1071
1072 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
1073 DoGetItem(&tvItem);
1074
1075 return (tvItem.state & TVIS_SELECTED) != 0;
1076 }
1077
1078 bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
1079 {
1080 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1081
1082 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1083 DoGetItem(&tvItem);
1084
1085 return (tvItem.state & TVIS_BOLD) != 0;
1086 }
1087
1088 // ----------------------------------------------------------------------------
1089 // navigation
1090 // ----------------------------------------------------------------------------
1091
1092 wxTreeItemId wxTreeCtrl::GetRootItem() const
1093 {
1094 // Root may be real (visible) or virtual (hidden).
1095 if ( GET_VIRTUAL_ROOT() )
1096 return TVI_ROOT;
1097
1098 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1099 }
1100
1101 wxTreeItemId wxTreeCtrl::GetSelection() const
1102 {
1103 wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), wxTreeItemId(),
1104 wxT("this only works with single selection controls") );
1105
1106 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1107 }
1108
1109 wxTreeItemId wxTreeCtrl::GetItemParent(const wxTreeItemId& item) const
1110 {
1111 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1112
1113 HTREEITEM hItem;
1114
1115 if ( IS_VIRTUAL_ROOT(item) )
1116 {
1117 // no parent for the virtual root
1118 hItem = 0;
1119 }
1120 else // normal item
1121 {
1122 hItem = TreeView_GetParent(GetHwnd(), HITEM(item));
1123 if ( !hItem && HasFlag(wxTR_HIDE_ROOT) )
1124 {
1125 // the top level items should have the virtual root as their parent
1126 hItem = TVI_ROOT;
1127 }
1128 }
1129
1130 return wxTreeItemId(hItem);
1131 }
1132
1133 wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1134 wxTreeItemIdValue& cookie) const
1135 {
1136 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1137
1138 // remember the last child returned in 'cookie'
1139 cookie = TreeView_GetChild(GetHwnd(), HITEM(item));
1140
1141 return wxTreeItemId(cookie);
1142 }
1143
1144 wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
1145 wxTreeItemIdValue& cookie) const
1146 {
1147 wxTreeItemId fromCookie(cookie);
1148
1149 HTREEITEM hitem = HITEM(fromCookie);
1150
1151 hitem = TreeView_GetNextSibling(GetHwnd(), hitem);
1152
1153 wxTreeItemId item(hitem);
1154
1155 cookie = item.m_pItem;
1156
1157 return item;
1158 }
1159
1160 #if WXWIN_COMPATIBILITY_2_4
1161
1162 wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1163 long& cookie) const
1164 {
1165 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1166
1167 cookie = (long)TreeView_GetChild(GetHwnd(), HITEM(item));
1168
1169 return wxTreeItemId((void *)cookie);
1170 }
1171
1172 wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
1173 long& cookie) const
1174 {
1175 wxTreeItemId fromCookie((void *)cookie);
1176
1177 HTREEITEM hitem = HITEM(fromCookie);
1178
1179 hitem = TreeView_GetNextSibling(GetHwnd(), hitem);
1180
1181 wxTreeItemId item(hitem);
1182
1183 cookie = (long)item.m_pItem;
1184
1185 return item;
1186 }
1187
1188 #endif // WXWIN_COMPATIBILITY_2_4
1189
1190 wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
1191 {
1192 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1193
1194 // can this be done more efficiently?
1195 wxTreeItemIdValue cookie;
1196
1197 wxTreeItemId childLast,
1198 child = GetFirstChild(item, cookie);
1199 while ( child.IsOk() )
1200 {
1201 childLast = child;
1202 child = GetNextChild(item, cookie);
1203 }
1204
1205 return childLast;
1206 }
1207
1208 wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
1209 {
1210 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1211 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item)));
1212 }
1213
1214 wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
1215 {
1216 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1217 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item)));
1218 }
1219
1220 wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
1221 {
1222 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1223 }
1224
1225 wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
1226 {
1227 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1228 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() for must be visible itself!"));
1229
1230 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item)));
1231 }
1232
1233 wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1234 {
1235 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1236 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1237
1238 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item)));
1239 }
1240
1241 // ----------------------------------------------------------------------------
1242 // multiple selections emulation
1243 // ----------------------------------------------------------------------------
1244
1245 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const
1246 {
1247 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1248
1249 // receive the desired information.
1250 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1251 DoGetItem(&tvItem);
1252
1253 // state image indices are 1 based
1254 return ((tvItem.state >> 12) - 1) == 1;
1255 }
1256
1257 void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check)
1258 {
1259 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1260
1261 // receive the desired information.
1262 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1263
1264 DoGetItem(&tvItem);
1265
1266 // state images are one-based
1267 tvItem.state = (check ? 2 : 1) << 12;
1268
1269 DoSetItem(&tvItem);
1270 }
1271
1272 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
1273 {
1274 TraverseSelections selector(this, selections);
1275
1276 return selector.GetCount();
1277 }
1278
1279 // ----------------------------------------------------------------------------
1280 // Usual operations
1281 // ----------------------------------------------------------------------------
1282
1283 wxTreeItemId wxTreeCtrl::DoInsertAfter(const wxTreeItemId& parent,
1284 const wxTreeItemId& hInsertAfter,
1285 const wxString& text,
1286 int image, int selectedImage,
1287 wxTreeItemData *data)
1288 {
1289 wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()),
1290 wxTreeItemId(),
1291 _T("can't have more than one root in the tree") );
1292
1293 TV_INSERTSTRUCT tvIns;
1294 tvIns.hParent = HITEM(parent);
1295 tvIns.hInsertAfter = HITEM(hInsertAfter);
1296
1297 // this is how we insert the item as the first child: supply a NULL
1298 // hInsertAfter
1299 if ( !tvIns.hInsertAfter )
1300 {
1301 tvIns.hInsertAfter = TVI_FIRST;
1302 }
1303
1304 UINT mask = 0;
1305 if ( !text.empty() )
1306 {
1307 mask |= TVIF_TEXT;
1308 tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok
1309 }
1310 else
1311 {
1312 tvIns.item.pszText = NULL;
1313 tvIns.item.cchTextMax = 0;
1314 }
1315
1316 // create the param which will store the other item parameters
1317 wxTreeItemParam *param = new wxTreeItemParam;
1318
1319 // we return the images on demand as they depend on whether the item is
1320 // expanded or collapsed too in our case
1321 mask |= TVIF_IMAGE | TVIF_SELECTEDIMAGE;
1322 tvIns.item.iImage = I_IMAGECALLBACK;
1323 tvIns.item.iSelectedImage = I_IMAGECALLBACK;
1324
1325 param->SetImage(image, wxTreeItemIcon_Normal);
1326 param->SetImage(selectedImage, wxTreeItemIcon_Selected);
1327
1328 mask |= TVIF_PARAM;
1329 tvIns.item.lParam = (LPARAM)param;
1330 tvIns.item.mask = mask;
1331
1332 HTREEITEM id = TreeView_InsertItem(GetHwnd(), &tvIns);
1333 if ( id == 0 )
1334 {
1335 wxLogLastError(wxT("TreeView_InsertItem"));
1336 }
1337
1338 // associate the application tree item with Win32 tree item handle
1339 param->SetItem(id);
1340
1341 // setup wxTreeItemData
1342 if ( data != NULL )
1343 {
1344 param->SetData(data);
1345 data->SetId(id);
1346 }
1347
1348 return wxTreeItemId(id);
1349 }
1350
1351 // for compatibility only
1352 #if WXWIN_COMPATIBILITY_2_4
1353
1354 void wxTreeCtrl::SetImageList(wxImageList *imageList, int)
1355 {
1356 SetImageList(imageList);
1357 }
1358
1359 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const
1360 {
1361 return GetItemImage(item, wxTreeItemIcon_Selected);
1362 }
1363
1364 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image)
1365 {
1366 SetItemImage(item, image, wxTreeItemIcon_Selected);
1367 }
1368
1369 #endif // WXWIN_COMPATIBILITY_2_4
1370
1371 wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
1372 int image, int selectedImage,
1373 wxTreeItemData *data)
1374 {
1375
1376 if ( HasFlag(wxTR_HIDE_ROOT) )
1377 {
1378 // create a virtual root item, the parent for all the others
1379 wxTreeItemParam *param = new wxTreeItemParam;
1380 param->SetData(data);
1381
1382 m_pVirtualRoot = new wxVirtualNode(param);
1383
1384 return TVI_ROOT;
1385 }
1386
1387 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1388 text, image, selectedImage, data);
1389 }
1390
1391 wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
1392 size_t index,
1393 const wxString& text,
1394 int image, int selectedImage,
1395 wxTreeItemData *data)
1396 {
1397 wxTreeItemId idPrev;
1398 if ( index == (size_t)-1 )
1399 {
1400 // special value: append to the end
1401 idPrev = TVI_LAST;
1402 }
1403 else // find the item from index
1404 {
1405 wxTreeItemIdValue cookie;
1406 wxTreeItemId idCur = GetFirstChild(parent, cookie);
1407 while ( index != 0 && idCur.IsOk() )
1408 {
1409 index--;
1410
1411 idPrev = idCur;
1412 idCur = GetNextChild(parent, cookie);
1413 }
1414
1415 // assert, not check: if the index is invalid, we will append the item
1416 // to the end
1417 wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") );
1418 }
1419
1420 return DoInsertAfter(parent, idPrev, text, image, selectedImage, data);
1421 }
1422
1423 void wxTreeCtrl::Delete(const wxTreeItemId& item)
1424 {
1425 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) )
1426 {
1427 wxLogLastError(wxT("TreeView_DeleteItem"));
1428 }
1429 }
1430
1431 // delete all children (but don't delete the item itself)
1432 void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
1433 {
1434 wxTreeItemIdValue cookie;
1435
1436 wxArrayTreeItemIds children;
1437 wxTreeItemId child = GetFirstChild(item, cookie);
1438 while ( child.IsOk() )
1439 {
1440 children.Add(child);
1441
1442 child = GetNextChild(item, cookie);
1443 }
1444
1445 size_t nCount = children.Count();
1446 for ( size_t n = 0; n < nCount; n++ )
1447 {
1448 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children[n])) )
1449 {
1450 wxLogLastError(wxT("TreeView_DeleteItem"));
1451 }
1452 }
1453 }
1454
1455 void wxTreeCtrl::DeleteAllItems()
1456 {
1457 // delete the "virtual" root item.
1458 if ( GET_VIRTUAL_ROOT() )
1459 {
1460 delete GET_VIRTUAL_ROOT();
1461 m_pVirtualRoot = NULL;
1462 }
1463
1464 // and all the real items
1465
1466 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1467 {
1468 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1469 }
1470 }
1471
1472 void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
1473 {
1474 wxASSERT_MSG( flag == TVE_COLLAPSE ||
1475 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
1476 flag == TVE_EXPAND ||
1477 flag == TVE_TOGGLE,
1478 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1479
1480 // A hidden root can be neither expanded nor collapsed.
1481 wxCHECK_RET( !IsHiddenRoot(item),
1482 wxT("Can't expand/collapse hidden root node!") );
1483
1484 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1485 // emulate them. This behaviour has changed slightly with comctl32.dll
1486 // v 4.70 - now it does send them but only the first time. To maintain
1487 // compatible behaviour and also in order to not have surprises with the
1488 // future versions, don't rely on this and still do everything ourselves.
1489 // To avoid that the messages be sent twice when the item is expanded for
1490 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1491
1492 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE);
1493 tvItem.state = 0;
1494 DoSetItem(&tvItem);
1495
1496 if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) != 0 )
1497 {
1498 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1499 // itself
1500 wxTreeEvent event(gs_expandEvents[IsExpanded(item) ? IDX_EXPAND
1501 : IDX_COLLAPSE]
1502 [IDX_DONE],
1503 this, item);
1504 (void)GetEventHandler()->ProcessEvent(event);
1505 }
1506 //else: change didn't took place, so do nothing at all
1507 }
1508
1509 void wxTreeCtrl::Expand(const wxTreeItemId& item)
1510 {
1511 DoExpand(item, TVE_EXPAND);
1512 }
1513
1514 void wxTreeCtrl::Collapse(const wxTreeItemId& item)
1515 {
1516 DoExpand(item, TVE_COLLAPSE);
1517 }
1518
1519 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1520 {
1521 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
1522 }
1523
1524 void wxTreeCtrl::Toggle(const wxTreeItemId& item)
1525 {
1526 DoExpand(item, TVE_TOGGLE);
1527 }
1528
1529 #if WXWIN_COMPATIBILITY_2_4
1530
1531 void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
1532 {
1533 DoExpand(item, action);
1534 }
1535
1536 #endif
1537
1538 void wxTreeCtrl::Unselect()
1539 {
1540 wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE),
1541 wxT("doesn't make sense, may be you want UnselectAll()?") );
1542
1543 // just remove the selection
1544 SelectItem(wxTreeItemId());
1545 }
1546
1547 void wxTreeCtrl::UnselectAll()
1548 {
1549 if ( m_windowStyle & wxTR_MULTIPLE )
1550 {
1551 wxArrayTreeItemIds selections;
1552 size_t count = GetSelections(selections);
1553 for ( size_t n = 0; n < count; n++ )
1554 {
1555 ::UnselectItem(GetHwnd(), HITEM(selections[n]));
1556 }
1557
1558 m_htSelStart.Unset();
1559 }
1560 else // single selection
1561 {
1562 // just remove the selection
1563 Unselect();
1564 }
1565 }
1566
1567 void wxTreeCtrl::SelectItem(const wxTreeItemId& item, bool select)
1568 {
1569 if ( m_windowStyle & wxTR_MULTIPLE )
1570 {
1571 ::SelectItem(GetHwnd(), HITEM(item), select);
1572 }
1573 else // single selection
1574 {
1575 wxASSERT_MSG( select,
1576 _T("SelectItem(false) works only for multiselect") );
1577
1578 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1579 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1580 // send them ourselves
1581
1582 wxTreeEvent event(wxEVT_COMMAND_TREE_SEL_CHANGING, this, item);
1583 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
1584 {
1585 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item)) )
1586 {
1587 wxLogLastError(wxT("TreeView_SelectItem"));
1588 }
1589 else // ok
1590 {
1591 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1592 (void)GetEventHandler()->ProcessEvent(event);
1593 }
1594 }
1595 //else: program vetoed the change
1596 }
1597 }
1598
1599 void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1600 {
1601 wxCHECK_RET( !IsHiddenRoot(item), _T("can't show hidden root item") );
1602
1603 // no error return
1604 TreeView_EnsureVisible(GetHwnd(), HITEM(item));
1605 }
1606
1607 void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
1608 {
1609 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) )
1610 {
1611 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1612 }
1613 }
1614
1615 wxTextCtrl *wxTreeCtrl::GetEditControl() const
1616 {
1617 return m_textCtrl;
1618 }
1619
1620 void wxTreeCtrl::DeleteTextCtrl()
1621 {
1622 if ( m_textCtrl )
1623 {
1624 // the HWND corresponding to this control is deleted by the tree
1625 // control itself and we don't know when exactly this happens, so check
1626 // if the window still exists before calling UnsubclassWin()
1627 if ( !::IsWindow(GetHwndOf(m_textCtrl)) )
1628 {
1629 m_textCtrl->SetHWND(0);
1630 }
1631
1632 m_textCtrl->UnsubclassWin();
1633 m_textCtrl->SetHWND(0);
1634 delete m_textCtrl;
1635 m_textCtrl = NULL;
1636
1637 m_idEdited.Unset();
1638 }
1639 }
1640
1641 wxTextCtrl *wxTreeCtrl::EditLabel(const wxTreeItemId& item,
1642 wxClassInfo *textControlClass)
1643 {
1644 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
1645
1646 DeleteTextCtrl();
1647
1648 m_idEdited = item;
1649 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
1650 HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item));
1651
1652 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1653 // returned false
1654 if ( !hWnd )
1655 {
1656 delete m_textCtrl;
1657 m_textCtrl = NULL;
1658 return NULL;
1659 }
1660
1661 // textctrl is subclassed in MSWOnNotify
1662 return m_textCtrl;
1663 }
1664
1665 // End label editing, optionally cancelling the edit
1666 void wxTreeCtrl::DoEndEditLabel(bool discardChanges)
1667 {
1668 TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
1669
1670 DeleteTextCtrl();
1671 }
1672
1673 wxTreeItemId wxTreeCtrl::DoTreeHitTest(const wxPoint& point, int& flags) const
1674 {
1675 TV_HITTESTINFO hitTestInfo;
1676 hitTestInfo.pt.x = (int)point.x;
1677 hitTestInfo.pt.y = (int)point.y;
1678
1679 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo);
1680
1681 flags = 0;
1682
1683 // avoid repetition
1684 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1685 flags |= wxTREE_HITTEST_##flag
1686
1687 TRANSLATE_FLAG(ABOVE);
1688 TRANSLATE_FLAG(BELOW);
1689 TRANSLATE_FLAG(NOWHERE);
1690 TRANSLATE_FLAG(ONITEMBUTTON);
1691 TRANSLATE_FLAG(ONITEMICON);
1692 TRANSLATE_FLAG(ONITEMINDENT);
1693 TRANSLATE_FLAG(ONITEMLABEL);
1694 TRANSLATE_FLAG(ONITEMRIGHT);
1695 TRANSLATE_FLAG(ONITEMSTATEICON);
1696 TRANSLATE_FLAG(TOLEFT);
1697 TRANSLATE_FLAG(TORIGHT);
1698
1699 #undef TRANSLATE_FLAG
1700
1701 return wxTreeItemId(hitTestInfo.hItem);
1702 }
1703
1704 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
1705 wxRect& rect,
1706 bool textOnly) const
1707 {
1708 RECT rc;
1709
1710 // Virtual root items have no bounding rectangle
1711 if ( IS_VIRTUAL_ROOT(item) )
1712 {
1713 return false;
1714 }
1715
1716 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item),
1717 &rc, textOnly) )
1718 {
1719 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
1720
1721 return true;
1722 }
1723 else
1724 {
1725 // couldn't retrieve rect: for example, item isn't visible
1726 return false;
1727 }
1728 }
1729
1730 // ----------------------------------------------------------------------------
1731 // sorting stuff
1732 // ----------------------------------------------------------------------------
1733
1734 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1735 // functions such as IsDataIndirect()
1736 class wxTreeSortHelper
1737 {
1738 public:
1739 static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree);
1740
1741 private:
1742 static wxTreeItemId GetIdFromData(LPARAM lParam)
1743 {
1744 return ((wxTreeItemParam*)lParam)->GetItem();
1745 }
1746 };
1747
1748 int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1,
1749 LPARAM pItem2,
1750 LPARAM htree)
1751 {
1752 wxCHECK_MSG( pItem1 && pItem2, 0,
1753 wxT("sorting tree without data doesn't make sense") );
1754
1755 wxTreeCtrl *tree = (wxTreeCtrl *)htree;
1756
1757 return tree->OnCompareItems(GetIdFromData(pItem1),
1758 GetIdFromData(pItem2));
1759 }
1760
1761 void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
1762 {
1763 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1764
1765 // rely on the fact that TreeView_SortChildren does the same thing as our
1766 // default behaviour, i.e. sorts items alphabetically and so call it
1767 // directly if we're not in derived class (much more efficient!)
1768 // RN: Note that if you find you're code doesn't sort as expected this
1769 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
1770 // combo for your derived wxTreeCtrl if will sort without
1771 // OnCompareItems
1772 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
1773 {
1774 TreeView_SortChildren(GetHwnd(), HITEM(item), 0);
1775 }
1776 else
1777 {
1778 TV_SORTCB tvSort;
1779 tvSort.hParent = HITEM(item);
1780 tvSort.lpfnCompare = wxTreeSortHelper::Compare;
1781 tvSort.lParam = (LPARAM)this;
1782 TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
1783 }
1784 }
1785
1786 // ----------------------------------------------------------------------------
1787 // implementation
1788 // ----------------------------------------------------------------------------
1789
1790 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG* msg)
1791 {
1792 if ( msg->message == WM_KEYDOWN )
1793 {
1794 if ( msg->wParam == VK_RETURN )
1795 {
1796 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
1797 return false;
1798 }
1799 }
1800
1801 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg);
1802 }
1803
1804 bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1805 {
1806 if ( cmd == EN_UPDATE )
1807 {
1808 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1809 event.SetEventObject( this );
1810 ProcessCommand(event);
1811 }
1812 else if ( cmd == EN_KILLFOCUS )
1813 {
1814 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1815 event.SetEventObject( this );
1816 ProcessCommand(event);
1817 }
1818 else
1819 {
1820 // nothing done
1821 return false;
1822 }
1823
1824 // command processed
1825 return true;
1826 }
1827
1828 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1829 // only do it during dragging, minimize wxWin overhead (this is important for
1830 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1831 // instead of passing by wxWin events
1832 WXLRESULT wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
1833 {
1834 bool processed = false;
1835 WXLRESULT rc = 0;
1836 bool isMultiple = HasFlag(wxTR_MULTIPLE);
1837
1838 // This message is sent after a right-click, or when the "menu" key is pressed
1839 if ( nMsg == WM_CONTEXTMENU )
1840 {
1841 int x = GET_X_LPARAM(lParam),
1842 y = GET_Y_LPARAM(lParam);
1843 // Convert the screen point to a client point
1844 wxPoint MenuPoint = ScreenToClient(wxPoint(x, y));
1845
1846 // can't use GetSelection() here as it would assert in multiselect mode
1847 wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_MENU, this,
1848 wxTreeItemId(TreeView_GetSelection(GetHwnd())));
1849
1850 // Get the bounding rectangle for the item, including the non-text areas
1851 wxRect ItemRect;
1852 GetBoundingRect(event.m_item, ItemRect, false);
1853 // If the point is inside the bounding rectangle, use it as the click position.
1854 // This should be the case for WM_CONTEXTMENU as the result of a right-click
1855 if (ItemRect.Contains(MenuPoint))
1856 {
1857 event.m_pointDrag = MenuPoint;
1858 }
1859 // Use the Explorer standard of putting the menu at the left edge of the text,
1860 // in the vertical middle of the text. Should be the case for the "menu" key
1861 else
1862 {
1863 // Use the bounding rectangle of only the text part
1864 GetBoundingRect(event.m_item, ItemRect, true);
1865 event.m_pointDrag = wxPoint(ItemRect.GetX(), ItemRect.GetY() + ItemRect.GetHeight() / 2);
1866 }
1867
1868 if ( GetEventHandler()->ProcessEvent(event) )
1869 processed = true;
1870 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
1871 }
1872 else if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
1873 {
1874 // we only process mouse messages here and these parameters have the
1875 // same meaning for all of them
1876 int x = GET_X_LPARAM(lParam),
1877 y = GET_Y_LPARAM(lParam);
1878 HTREEITEM htItem = GetItemFromPoint(GetHwnd(), x, y);
1879
1880 TV_HITTESTINFO tvht;
1881 tvht.pt.x = x;
1882 tvht.pt.y = y;
1883
1884 (void) TreeView_HitTest(GetHwnd(), &tvht);
1885
1886 switch ( nMsg )
1887 {
1888 case WM_RBUTTONDOWN:
1889 // if the item we are about to right click on is not already
1890 // selected or if we click outside of any item, remove the
1891 // entire previous selection
1892 if ( !htItem || !::IsItemSelected(GetHwnd(), htItem) )
1893 {
1894 UnselectAll();
1895 }
1896
1897 // select item and set the focus to the
1898 // newly selected item
1899 ::SelectItem(GetHwnd(), htItem);
1900 ::SetFocus(GetHwnd(), htItem);
1901 break;
1902
1903 case WM_LBUTTONDOWN:
1904 if ( htItem && isMultiple && (tvht.flags & TVHT_ONITEM) != 0 )
1905 {
1906 m_htClickedItem = (WXHTREEITEM) htItem;
1907 m_ptClick = wxPoint(x, y);
1908
1909 if ( wParam & MK_CONTROL )
1910 {
1911 SetFocus();
1912
1913 // toggle selected state
1914 ::ToggleItemSelection(GetHwnd(), htItem);
1915
1916 ::SetFocus(GetHwnd(), htItem);
1917
1918 // reset on any click without Shift
1919 m_htSelStart.Unset();
1920
1921 processed = true;
1922 }
1923 else if ( wParam & MK_SHIFT )
1924 {
1925 // this selects all items between the starting one and
1926 // the current
1927
1928 if ( !m_htSelStart )
1929 {
1930 // take the focused item
1931 m_htSelStart = TreeView_GetSelection(GetHwnd());
1932 }
1933
1934 if ( m_htSelStart )
1935 SelectRange(GetHwnd(), HITEM(m_htSelStart), htItem,
1936 !(wParam & MK_CONTROL));
1937 else
1938 ::SelectItem(GetHwnd(), htItem);
1939
1940 ::SetFocus(GetHwnd(), htItem);
1941
1942 processed = true;
1943 }
1944 else // normal click
1945 {
1946 // avoid doing anything if we click on the only
1947 // currently selected item
1948
1949 SetFocus();
1950
1951 wxArrayTreeItemIds selections;
1952 size_t count = GetSelections(selections);
1953 if ( count == 0 ||
1954 count > 1 ||
1955 HITEM(selections[0]) != htItem )
1956 {
1957 // clear the previously selected items, if the
1958 // user clicked outside of the present selection.
1959 // otherwise, perform the deselection on mouse-up.
1960 // this allows multiple drag and drop to work.
1961
1962 if (!IsItemSelected(GetHwnd(), htItem))
1963 {
1964 UnselectAll();
1965
1966 // prevent the click from starting in-place editing
1967 // which should only happen if we click on the
1968 // already selected item (and nothing else is
1969 // selected)
1970
1971 TreeView_SelectItem(GetHwnd(), 0);
1972 ::SelectItem(GetHwnd(), htItem);
1973 }
1974 ::SetFocus(GetHwnd(), htItem);
1975 processed = true;
1976 }
1977
1978 // reset on any click without Shift
1979 m_htSelStart.Unset();
1980 }
1981 }
1982 break;
1983
1984 case WM_MOUSEMOVE:
1985 #ifndef __WXWINCE__
1986 if ( m_htClickedItem )
1987 {
1988 int cx = abs(m_ptClick.x - x);
1989 int cy = abs(m_ptClick.y - y);
1990
1991 if ( cx > GetSystemMetrics( SM_CXDRAG ) || cy > GetSystemMetrics( SM_CYDRAG ) )
1992 {
1993 HWND pWnd = ::GetParent( GetHwnd() );
1994 if ( pWnd )
1995 {
1996 NM_TREEVIEW tv;
1997
1998 tv.hdr.hwndFrom = GetHwnd();
1999 tv.hdr.idFrom = ::GetWindowLong( GetHwnd(), GWL_ID );
2000 tv.hdr.code = TVN_BEGINDRAG;
2001
2002 tv.itemNew.hItem = HITEM(m_htClickedItem);
2003
2004 TVITEM tviAux;
2005 ZeroMemory(&tviAux, sizeof(tviAux));
2006 tviAux.hItem = HITEM(m_htClickedItem);
2007 tviAux.mask = TVIF_STATE | TVIF_PARAM;
2008 tviAux.stateMask = 0xffffffff;
2009 TreeView_GetItem( GetHwnd(), &tviAux );
2010
2011 tv.itemNew.state = tviAux.state;
2012 tv.itemNew.lParam = tviAux.lParam;
2013
2014 tv.ptDrag.x = x;
2015 tv.ptDrag.y = y;
2016
2017 ::SendMessage( pWnd, WM_NOTIFY, tv.hdr.idFrom, (LPARAM)&tv );
2018 }
2019 m_htClickedItem.Unset();
2020 }
2021 }
2022 #endif // __WXWINCE__
2023
2024 if ( m_dragImage )
2025 {
2026 m_dragImage->Move(wxPoint(x, y));
2027 if ( htItem )
2028 {
2029 // highlight the item as target (hiding drag image is
2030 // necessary - otherwise the display will be corrupted)
2031 m_dragImage->Hide();
2032 TreeView_SelectDropTarget(GetHwnd(), htItem);
2033 m_dragImage->Show();
2034 }
2035 }
2036 break;
2037
2038 case WM_LBUTTONUP:
2039
2040 // facilitates multiple drag-and-drop
2041 if (htItem && isMultiple)
2042 {
2043 wxArrayTreeItemIds selections;
2044 size_t count = GetSelections(selections);
2045
2046 if (count > 1 &&
2047 !(wParam & MK_CONTROL) &&
2048 !(wParam & MK_SHIFT))
2049 {
2050 UnselectAll();
2051 TreeView_SelectItem(GetHwnd(), htItem);
2052 ::SelectItem(GetHwnd(), htItem);
2053 ::SetFocus(GetHwnd(), htItem);
2054 }
2055 m_htClickedItem.Unset();
2056 }
2057
2058 // fall through
2059
2060 case WM_RBUTTONUP:
2061 if ( m_dragImage )
2062 {
2063 m_dragImage->EndDrag();
2064 delete m_dragImage;
2065 m_dragImage = NULL;
2066
2067 // generate the drag end event
2068 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, this, htItem);
2069 (void)GetEventHandler()->ProcessEvent(event);
2070
2071 // if we don't do it, the tree seems to think that 2 items
2072 // are selected simultaneously which is quite weird
2073 TreeView_SelectDropTarget(GetHwnd(), 0);
2074 }
2075 break;
2076 }
2077 }
2078 else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) && isMultiple )
2079 {
2080 // the tree control greys out the selected item when it loses focus and
2081 // paints it as selected again when it regains it, but it won't do it
2082 // for the other items itself - help it
2083 wxArrayTreeItemIds selections;
2084 size_t count = GetSelections(selections);
2085 RECT rect;
2086 for ( size_t n = 0; n < count; n++ )
2087 {
2088 // TreeView_GetItemRect() will return false if item is not visible,
2089 // which may happen perfectly well
2090 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections[n]),
2091 &rect, TRUE) )
2092 {
2093 ::InvalidateRect(GetHwnd(), &rect, FALSE);
2094 }
2095 }
2096 }
2097 else if ( nMsg == WM_KEYDOWN && isMultiple )
2098 {
2099 bool bCtrl = wxIsCtrlDown(),
2100 bShift = wxIsShiftDown();
2101
2102 HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
2103 switch ( wParam )
2104 {
2105 case VK_SPACE:
2106 if ( bCtrl )
2107 {
2108 ::ToggleItemSelection(GetHwnd(), htSel);
2109 }
2110 else
2111 {
2112 UnselectAll();
2113
2114 ::SelectItem(GetHwnd(), htSel);
2115 }
2116
2117 processed = true;
2118 break;
2119
2120 case VK_UP:
2121 case VK_DOWN:
2122 if ( !bCtrl && !bShift )
2123 {
2124 // no modifiers, just clear selection and then let the default
2125 // processing to take place
2126 UnselectAll();
2127 }
2128 else if ( htSel )
2129 {
2130 (void)wxControl::MSWWindowProc(nMsg, wParam, lParam);
2131
2132 HTREEITEM htNext = (HTREEITEM)
2133 TreeView_GetNextItem
2134 (
2135 GetHwnd(),
2136 htSel,
2137 wParam == VK_UP ? TVGN_PREVIOUSVISIBLE
2138 : TVGN_NEXTVISIBLE
2139 );
2140
2141 if ( !htNext )
2142 {
2143 // at the top/bottom
2144 htNext = htSel;
2145 }
2146
2147 if ( bShift )
2148 {
2149 if ( !m_htSelStart )
2150 m_htSelStart = htSel;
2151
2152 SelectRange(GetHwnd(), HITEM(m_htSelStart), htNext);
2153 }
2154 else // bCtrl
2155 {
2156 // without changing selection
2157 ::SetFocus(GetHwnd(), htNext);
2158 }
2159
2160 processed = true;
2161 }
2162 break;
2163
2164 case VK_HOME:
2165 case VK_END:
2166 case VK_PRIOR:
2167 case VK_NEXT:
2168 // TODO: handle Shift/Ctrl with these keys
2169 if ( !bCtrl && !bShift )
2170 {
2171 UnselectAll();
2172
2173 m_htSelStart.Unset();
2174 }
2175 }
2176 }
2177 else if ( nMsg == WM_COMMAND )
2178 {
2179 // if we receive a EN_KILLFOCUS command from the in-place edit control
2180 // used for label editing, make sure to end editing
2181 WORD id, cmd;
2182 WXHWND hwnd;
2183 UnpackCommand(wParam, lParam, &id, &hwnd, &cmd);
2184
2185 if ( cmd == EN_KILLFOCUS )
2186 {
2187 if ( m_textCtrl && m_textCtrl->GetHandle() == hwnd )
2188 {
2189 DoEndEditLabel();
2190
2191 processed = true;
2192 }
2193 }
2194 }
2195
2196 if ( !processed )
2197 rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
2198
2199 return rc;
2200 }
2201
2202 WXLRESULT
2203 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
2204 {
2205 // default WM_RBUTTONDOWN handler enters modal loop inside DefWindowProc()
2206 // waiting for WM_RBUTTONUP and then sends the resulting WM_CONTEXTMENU to
2207 // the parent window, not us, which completely breaks everything so simply
2208 // don't let it see this message at all
2209 if ( nMsg == WM_RBUTTONDOWN )
2210 return 0;
2211
2212 // but because of the above we don't get NM_RCLICK which is normally
2213 // generated by tree window proc when the modal loop mentioned above ends
2214 // because the mouse is released -- synthesize it ourselves instead
2215 if ( nMsg == WM_RBUTTONUP )
2216 {
2217 NMHDR hdr;
2218 hdr.hwndFrom = GetHwnd();
2219 hdr.idFrom = GetId();
2220 hdr.code = NM_RCLICK;
2221
2222 WXLPARAM rc;
2223 MSWOnNotify(GetId(), (LPARAM)&hdr, &rc);
2224
2225 // continue as usual
2226 }
2227
2228 if ( nMsg == WM_CHAR )
2229 {
2230 // also don't let the control process Space and Return keys because it
2231 // doesn't do anything useful with them anyhow but always beeps
2232 // annoyingly when it receives them and there is no way to turn it off
2233 // simply if you just process TREEITEM_ACTIVATED event to which Space
2234 // and Enter presses are mapped in your code
2235 if ( wParam == VK_SPACE || wParam == VK_RETURN )
2236 return 0;
2237 }
2238
2239 return wxControl::MSWDefWindowProc(nMsg, wParam, lParam);
2240 }
2241
2242 // process WM_NOTIFY Windows message
2243 bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
2244 {
2245 wxTreeEvent event(wxEVT_NULL, this);
2246 wxEventType eventType = wxEVT_NULL;
2247 NMHDR *hdr = (NMHDR *)lParam;
2248
2249 switch ( hdr->code )
2250 {
2251 case TVN_BEGINDRAG:
2252 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
2253 // fall through
2254
2255 case TVN_BEGINRDRAG:
2256 {
2257 if ( eventType == wxEVT_NULL )
2258 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
2259 //else: left drag, already set above
2260
2261 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2262
2263 event.m_item = tv->itemNew.hItem;
2264 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
2265
2266 // don't allow dragging by default: the user code must
2267 // explicitly say that it wants to allow it to avoid breaking
2268 // the old apps
2269 event.Veto();
2270 }
2271 break;
2272
2273 case TVN_BEGINLABELEDIT:
2274 {
2275 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
2276 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2277
2278 // although the user event handler may still veto it, it is
2279 // important to set it now so that calls to SetItemText() from
2280 // the event handler would change the text controls contents
2281 m_idEdited =
2282 event.m_item = info->item.hItem;
2283 event.m_label = info->item.pszText;
2284 event.m_editCancelled = false;
2285 }
2286 break;
2287
2288 case TVN_DELETEITEM:
2289 {
2290 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
2291 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2292
2293 event.m_item = tv->itemOld.hItem;
2294
2295 if ( m_hasAnyAttr )
2296 {
2297 wxMapTreeAttr::iterator it = m_attrs.find(tv->itemOld.hItem);
2298 if ( it != m_attrs.end() )
2299 {
2300 delete it->second;
2301 m_attrs.erase(it);
2302 }
2303 }
2304 }
2305 break;
2306
2307 case TVN_ENDLABELEDIT:
2308 {
2309 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
2310 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2311
2312 event.m_item = info->item.hItem;
2313 event.m_label = info->item.pszText;
2314 event.m_editCancelled = info->item.pszText == NULL;
2315 break;
2316 }
2317
2318 #ifndef __WXWINCE__
2319 // These *must* not be removed or TVN_GETINFOTIP will
2320 // not be processed each time the mouse is moved
2321 // and the tooltip will only ever update once.
2322 case TTN_NEEDTEXTA:
2323 case TTN_NEEDTEXTW:
2324 {
2325 *result = 0;
2326
2327 break;
2328 }
2329
2330 #ifdef TVN_GETINFOTIP
2331 case TVN_GETINFOTIP:
2332 {
2333 eventType = wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP;
2334 NMTVGETINFOTIP *info = (NMTVGETINFOTIP*)lParam;
2335
2336 // Which item are we trying to get a tooltip for?
2337 event.m_item = info->hItem;
2338
2339 break;
2340 }
2341 #endif
2342 #endif
2343
2344 case TVN_GETDISPINFO:
2345 eventType = wxEVT_COMMAND_TREE_GET_INFO;
2346 // fall through
2347
2348 case TVN_SETDISPINFO:
2349 {
2350 if ( eventType == wxEVT_NULL )
2351 eventType = wxEVT_COMMAND_TREE_SET_INFO;
2352 //else: get, already set above
2353
2354 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2355
2356 event.m_item = info->item.hItem;
2357 break;
2358 }
2359
2360 case TVN_ITEMEXPANDING:
2361 case TVN_ITEMEXPANDED:
2362 {
2363 NM_TREEVIEW *tv = (NM_TREEVIEW*)lParam;
2364
2365 int what;
2366 switch ( tv->action )
2367 {
2368 default:
2369 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action);
2370 // fall through
2371
2372 case TVE_EXPAND:
2373 what = IDX_EXPAND;
2374 break;
2375
2376 case TVE_COLLAPSE:
2377 what = IDX_COLLAPSE;
2378 break;
2379 }
2380
2381 int how = hdr->code == TVN_ITEMEXPANDING ? IDX_DOING
2382 : IDX_DONE;
2383
2384 eventType = gs_expandEvents[what][how];
2385
2386 event.m_item = tv->itemNew.hItem;
2387 }
2388 break;
2389
2390 case TVN_KEYDOWN:
2391 {
2392 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
2393 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
2394
2395 // fabricate the lParam and wParam parameters sufficiently
2396 // similar to the ones from a "real" WM_KEYDOWN so that
2397 // CreateKeyEvent() works correctly
2398 const bool isAltDown = ::GetKeyState(VK_MENU) < 0;
2399 WXLPARAM lParam = (isAltDown ? KF_ALTDOWN : 0) << 16;
2400
2401 WXWPARAM wParam = info->wVKey;
2402
2403 int keyCode = wxCharCodeMSWToWX(wParam);
2404 if ( !keyCode )
2405 {
2406 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2407 // simple ASCII key
2408 keyCode = wParam;
2409 }
2410
2411 event.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN,
2412 keyCode,
2413 lParam,
2414 wParam);
2415
2416 // a separate event for Space/Return
2417 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown &&
2418 ((info->wVKey == VK_SPACE) || (info->wVKey == VK_RETURN)) )
2419 {
2420 wxTreeItemId item;
2421 if ( !HasFlag(wxTR_MULTIPLE) )
2422 item = GetSelection();
2423
2424 wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
2425 this, item);
2426 (void)GetEventHandler()->ProcessEvent(event2);
2427 }
2428 }
2429 break;
2430
2431 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2432 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2433 // we have to handle both messages:
2434 case TVN_SELCHANGEDA:
2435 case TVN_SELCHANGEDW:
2436 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
2437 // fall through
2438
2439 case TVN_SELCHANGINGA:
2440 case TVN_SELCHANGINGW:
2441 {
2442 if ( eventType == wxEVT_NULL )
2443 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
2444 //else: already set above
2445
2446 if (hdr->code == TVN_SELCHANGINGW ||
2447 hdr->code == TVN_SELCHANGEDW)
2448 {
2449 NM_TREEVIEWW *tv = (NM_TREEVIEWW *)lParam;
2450 event.m_item = tv->itemNew.hItem;
2451 event.m_itemOld = tv->itemOld.hItem;
2452 }
2453 else
2454 {
2455 NM_TREEVIEWA *tv = (NM_TREEVIEWA *)lParam;
2456 event.m_item = tv->itemNew.hItem;
2457 event.m_itemOld = tv->itemOld.hItem;
2458 }
2459 }
2460 break;
2461
2462 // instead of explicitly checking for _WIN32_IE, check if the
2463 // required symbols are available in the headers
2464 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2465 case NM_CUSTOMDRAW:
2466 {
2467 LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam;
2468 NMCUSTOMDRAW& nmcd = lptvcd->nmcd;
2469 switch ( nmcd.dwDrawStage )
2470 {
2471 case CDDS_PREPAINT:
2472 // if we've got any items with non standard attributes,
2473 // notify us before painting each item
2474 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
2475 : CDRF_DODEFAULT;
2476 break;
2477
2478 case CDDS_ITEMPREPAINT:
2479 {
2480 wxMapTreeAttr::iterator
2481 it = m_attrs.find((void *)nmcd.dwItemSpec);
2482
2483 if ( it == m_attrs.end() )
2484 {
2485 // nothing to do for this item
2486 *result = CDRF_DODEFAULT;
2487 break;
2488 }
2489
2490 wxTreeItemAttr * const attr = it->second;
2491
2492 wxTreeViewItem tvItem((void *)nmcd.dwItemSpec,
2493 TVIF_STATE, TVIS_DROPHILITED);
2494 DoGetItem(&tvItem);
2495 const UINT tvItemState = tvItem.state;
2496
2497 // selection colours should override ours,
2498 // otherwise it is too confusing to the user
2499 if ( !(nmcd.uItemState & CDIS_SELECTED) &&
2500 !(tvItemState & TVIS_DROPHILITED) )
2501 {
2502 wxColour colBack;
2503 if ( attr->HasBackgroundColour() )
2504 {
2505 colBack = attr->GetBackgroundColour();
2506 lptvcd->clrTextBk = wxColourToRGB(colBack);
2507 }
2508 }
2509
2510 // but we still want to keep the special foreground
2511 // colour when we don't have focus (we can't keep
2512 // it when we do, it would usually be unreadable on
2513 // the almost inverted bg colour...)
2514 if ( ( !(nmcd.uItemState & CDIS_SELECTED) ||
2515 FindFocus() != this ) &&
2516 !(tvItemState & TVIS_DROPHILITED) )
2517 {
2518 wxColour colText;
2519 if ( attr->HasTextColour() )
2520 {
2521 colText = attr->GetTextColour();
2522 lptvcd->clrText = wxColourToRGB(colText);
2523 }
2524 }
2525
2526 if ( attr->HasFont() )
2527 {
2528 HFONT hFont = GetHfontOf(attr->GetFont());
2529
2530 ::SelectObject(nmcd.hdc, hFont);
2531
2532 *result = CDRF_NEWFONT;
2533 }
2534 else // no specific font
2535 {
2536 *result = CDRF_DODEFAULT;
2537 }
2538 }
2539 break;
2540
2541 default:
2542 *result = CDRF_DODEFAULT;
2543 }
2544 }
2545
2546 // we always process it
2547 return true;
2548 #endif // have owner drawn support in headers
2549
2550 case NM_CLICK:
2551 {
2552 DWORD pos = GetMessagePos();
2553 POINT point;
2554 point.x = LOWORD(pos);
2555 point.y = HIWORD(pos);
2556 ::MapWindowPoints(HWND_DESKTOP, GetHwnd(), &point, 1);
2557 int flags = 0;
2558 wxTreeItemId item = HitTest(wxPoint(point.x, point.y), flags);
2559 if (flags & wxTREE_HITTEST_ONITEMSTATEICON)
2560 {
2561 event.m_item = item;
2562 eventType = wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK;
2563 }
2564 break;
2565 }
2566
2567 case NM_DBLCLK:
2568 case NM_RCLICK:
2569 {
2570 TV_HITTESTINFO tvhti;
2571 ::GetCursorPos(&tvhti.pt);
2572 ::ScreenToClient(GetHwnd(), &tvhti.pt);
2573 if ( TreeView_HitTest(GetHwnd(), &tvhti) )
2574 {
2575 if ( tvhti.flags & TVHT_ONITEM )
2576 {
2577 event.m_item = tvhti.hItem;
2578 eventType = (int)hdr->code == NM_DBLCLK
2579 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2580 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
2581
2582 event.m_pointDrag.x = tvhti.pt.x;
2583 event.m_pointDrag.y = tvhti.pt.y;
2584 }
2585
2586 break;
2587 }
2588 }
2589 // fall through
2590
2591 default:
2592 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2593 }
2594
2595 event.SetEventType(eventType);
2596
2597 if ( event.m_item.IsOk() )
2598 event.SetClientObject(GetItemData(event.m_item));
2599
2600 bool processed = GetEventHandler()->ProcessEvent(event);
2601
2602 // post processing
2603 switch ( hdr->code )
2604 {
2605 case NM_DBLCLK:
2606 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2607 // the return code of this event handler as the return value for
2608 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2609 // expanded status would never work
2610 *result = false;
2611 break;
2612
2613 case TVN_BEGINDRAG:
2614 case TVN_BEGINRDRAG:
2615 if ( event.IsAllowed() )
2616 {
2617 // normally this is impossible because the m_dragImage is
2618 // deleted once the drag operation is over
2619 wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") );
2620
2621 m_dragImage = new wxDragImage(*this, event.m_item);
2622 m_dragImage->BeginDrag(wxPoint(0,0), this);
2623 m_dragImage->Show();
2624 }
2625 break;
2626
2627 case TVN_DELETEITEM:
2628 {
2629 // NB: we might process this message using wxWidgets event
2630 // tables, but due to overhead of wxWin event system we
2631 // prefer to do it here ourself (otherwise deleting a tree
2632 // with many items is just too slow)
2633 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2634
2635 wxTreeItemParam *param =
2636 (wxTreeItemParam *)tv->itemOld.lParam;
2637 delete param;
2638
2639 processed = true; // Make sure we don't get called twice
2640 }
2641 break;
2642
2643 case TVN_BEGINLABELEDIT:
2644 // return true to cancel label editing
2645 *result = !event.IsAllowed();
2646
2647 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2648 if ( event.IsAllowed() )
2649 {
2650 HWND hText = TreeView_GetEditControl(GetHwnd());
2651 if ( hText )
2652 {
2653 // MBN: if m_textCtrl already has an HWND, it is a stale
2654 // pointer from a previous edit (because the user
2655 // didn't modify the label before dismissing the control,
2656 // and TVN_ENDLABELEDIT was not sent), so delete it
2657 if ( m_textCtrl && m_textCtrl->GetHWND() )
2658 DeleteTextCtrl();
2659 if ( !m_textCtrl )
2660 m_textCtrl = new wxTextCtrl();
2661 m_textCtrl->SetParent(this);
2662 m_textCtrl->SetHWND((WXHWND)hText);
2663 m_textCtrl->SubclassWin((WXHWND)hText);
2664
2665 // set wxTE_PROCESS_ENTER style for the text control to
2666 // force it to process the Enter presses itself, otherwise
2667 // they could be stolen from it by the dialog
2668 // navigation code
2669 m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle()
2670 | wxTE_PROCESS_ENTER);
2671 }
2672 }
2673 else // we had set m_idEdited before
2674 {
2675 m_idEdited.Unset();
2676 }
2677 break;
2678
2679 case TVN_ENDLABELEDIT:
2680 // return true to set the label to the new string: note that we
2681 // also must pretend that we did process the message or it is going
2682 // to be passed to DefWindowProc() which will happily return false
2683 // cancelling the label change
2684 *result = event.IsAllowed();
2685 processed = true;
2686
2687 // ensure that we don't have the text ctrl which is going to be
2688 // deleted any more
2689 DeleteTextCtrl();
2690 break;
2691
2692 #ifndef __WXWINCE__
2693 #ifdef TVN_GETINFOTIP
2694 case TVN_GETINFOTIP:
2695 {
2696 // If the user permitted a tooltip change, change it
2697 if (event.IsAllowed())
2698 {
2699 SetToolTip(event.m_label);
2700 }
2701 }
2702 break;
2703 #endif
2704 #endif
2705
2706 case TVN_SELCHANGING:
2707 case TVN_ITEMEXPANDING:
2708 // return true to prevent the action from happening
2709 *result = !event.IsAllowed();
2710 break;
2711
2712 case TVN_ITEMEXPANDED:
2713 // the item is not refreshed properly after expansion when it has
2714 // an image depending on the expanded/collapsed state - bug in
2715 // comctl32.dll or our code?
2716 {
2717 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2718 wxTreeItemId id(tv->itemNew.hItem);
2719
2720 int image = GetItemImage(id, wxTreeItemIcon_Expanded);
2721 if ( image != -1 )
2722 {
2723 RefreshItem(id);
2724 }
2725 }
2726 break;
2727
2728 case TVN_GETDISPINFO:
2729 // NB: so far the user can't set the image himself anyhow, so do it
2730 // anyway - but this may change later
2731 //if ( /* !processed && */ )
2732 {
2733 wxTreeItemId item = event.m_item;
2734 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2735
2736 const wxTreeItemParam * const param = GetItemParam(item);
2737 if ( !param )
2738 break;
2739
2740 if ( info->item.mask & TVIF_IMAGE )
2741 {
2742 info->item.iImage =
2743 param->GetImage
2744 (
2745 IsExpanded(item) ? wxTreeItemIcon_Expanded
2746 : wxTreeItemIcon_Normal
2747 );
2748 }
2749 if ( info->item.mask & TVIF_SELECTEDIMAGE )
2750 {
2751 info->item.iSelectedImage =
2752 param->GetImage
2753 (
2754 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
2755 : wxTreeItemIcon_Selected
2756 );
2757 }
2758 }
2759 break;
2760
2761 //default:
2762 // for the other messages the return value is ignored and there is
2763 // nothing special to do
2764 }
2765 return processed;
2766 }
2767
2768 // ----------------------------------------------------------------------------
2769 // State control.
2770 // ----------------------------------------------------------------------------
2771
2772 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2773 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2774
2775 void wxTreeCtrl::SetState(const wxTreeItemId& node, int state)
2776 {
2777 TV_ITEM tvi;
2778 tvi.hItem = (HTREEITEM)node.m_pItem;
2779 tvi.mask = TVIF_STATE;
2780 tvi.stateMask = TVIS_STATEIMAGEMASK;
2781
2782 // Select the specified state, or -1 == cycle to the next one.
2783 if ( state == -1 )
2784 {
2785 TreeView_GetItem(GetHwnd(), &tvi);
2786
2787 state = STATEIMAGEMASKTOINDEX(tvi.state) + 1;
2788 if ( state == m_imageListState->GetImageCount() )
2789 state = 1;
2790 }
2791
2792 wxCHECK_RET( state < m_imageListState->GetImageCount(),
2793 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2794
2795 tvi.state = INDEXTOSTATEIMAGEMASK(state);
2796
2797 TreeView_SetItem(GetHwnd(), &tvi);
2798 }
2799
2800 int wxTreeCtrl::GetState(const wxTreeItemId& node)
2801 {
2802 TV_ITEM tvi;
2803 tvi.hItem = (HTREEITEM)node.m_pItem;
2804 tvi.mask = TVIF_STATE;
2805 tvi.stateMask = TVIS_STATEIMAGEMASK;
2806 TreeView_GetItem(GetHwnd(), &tvi);
2807
2808 return STATEIMAGEMASKTOINDEX(tvi.state);
2809 }
2810
2811 #endif // wxUSE_TREECTRL