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