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