]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/treectrl.cpp
Compilaton fixes.
[wxWidgets.git] / src / msw / treectrl.cpp
... / ...
CommitLineData
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
100static 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
112static 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
128static 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
142static inline void UnselectItem(HWND hwndTV, HTREEITEM htItem)
143{
144 SelectItem(hwndTV, htItem, false);
145}
146
147static 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
154static 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!)
220static 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
284struct 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.
301class wxVirtualNode : public wxTreeViewItem
302{
303public:
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
318private:
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
336class wxTreeTraversal
337{
338public:
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
353protected:
354 const wxTreeCtrl *GetTree() const { return m_tree; }
355
356private:
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
365class TraverseSelections : public wxTreeTraversal
366{
367public:
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
399private:
400 wxArrayTreeItemIds& m_selections;
401};
402
403// internal class for counting tree items
404class TraverseCounter : public wxTreeTraversal
405{
406public:
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
426private:
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
445class wxTreeItemIndirectData : public wxTreeItemData
446{
447public:
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
483private:
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
497IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl)
498
499// ----------------------------------------------------------------------------
500// constants
501// ----------------------------------------------------------------------------
502
503// indices in gs_expandEvents table below
504enum
505{
506 IDX_COLLAPSE,
507 IDX_EXPAND,
508 IDX_WHAT_MAX
509};
510
511enum
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
520static /* 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
539bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively)
540{
541 if ( !OnVisit(root) )
542 return false;
543
544 return Traverse(root, recursively);
545}
546
547bool 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
570void 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
588bool 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
717wxTreeCtrl::~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
744bool 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
759void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem)
760{
761 if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 )
762 {
763 wxLogLastError(wxT("TreeView_SetItem"));
764 }
765}
766
767size_t wxTreeCtrl::GetCount() const
768{
769 return (size_t)TreeView_GetCount(GetHwnd());
770}
771
772unsigned int wxTreeCtrl::GetIndent() const
773{
774 return TreeView_GetIndent(GetHwnd());
775}
776
777void wxTreeCtrl::SetIndent(unsigned int indent)
778{
779 TreeView_SetIndent(GetHwnd(), indent);
780}
781
782wxImageList *wxTreeCtrl::GetImageList() const
783{
784 return m_imageListNormal;
785}
786
787wxImageList *wxTreeCtrl::GetStateImageList() const
788{
789 return m_imageListState;
790}
791
792void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
793{
794 // no error return
795 TreeView_SetImageList(GetHwnd(),
796 imageList ? imageList->GetHIMAGELIST() : 0,
797 which);
798}
799
800void 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
809void 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
816void wxTreeCtrl::AssignImageList(wxImageList *imageList)
817{
818 SetImageList(imageList);
819 m_ownsImageListNormal = true;
820}
821
822void wxTreeCtrl::AssignStateImageList(wxImageList *imageList)
823{
824 SetStateImageList(imageList);
825 m_ownsImageListState = true;
826}
827
828size_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
840bool 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
852bool 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
868wxString 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
884void 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
908int 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
920void 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
948void 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
958int 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
997void 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 =
1019 GetItemImage(item, wxTreeItemIcon_Selected);
1020
1021 // always set the normal image
1022 imageNormal = image;
1023
1024 // if the selected and normal images were the same, they should
1025 // be the same after the update, otherwise leave the selected
1026 // image as it was
1027 imageSel = imageNormalOld == imageSelOld ? image : imageSelOld;
1028 }
1029 break;
1030
1031 case wxTreeItemIcon_Selected:
1032 imageNormal = GetItemImage(item);
1033 imageSel = image;
1034 break;
1035
1036 case wxTreeItemIcon_Expanded:
1037 case wxTreeItemIcon_SelectedExpanded:
1038 if ( !HasIndirectData(item) )
1039 {
1040 // we need to get the old images first, because after we create
1041 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1042 // get the images
1043 imageNormal = GetItemImage(item);
1044 imageSel = GetItemImage(item, wxTreeItemIcon_Selected);
1045
1046 // if it doesn't have it yet, add it
1047 wxTreeItemIndirectData *data = new
1048 wxTreeItemIndirectData(this, item);
1049
1050 // copy the data to the new location
1051 data->SetImage(imageNormal, wxTreeItemIcon_Normal);
1052 data->SetImage(imageSel, wxTreeItemIcon_Selected);
1053 }
1054
1055 DoSetItemImageFromData(item, image, which);
1056
1057 // reset the normal/selected images because we won't use them any
1058 // more - now they're stored inside the indirect data
1059 imageNormal =
1060 imageSel = I_IMAGECALLBACK;
1061 break;
1062 }
1063
1064 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1065 // change both normal and selected image - otherwise the change simply
1066 // doesn't take place!
1067 DoSetItemImages(item, imageNormal, imageSel);
1068}
1069
1070wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
1071{
1072 wxTreeViewItem tvItem(item, TVIF_PARAM);
1073
1074 // Hidden root may have data.
1075 if ( IS_VIRTUAL_ROOT(item) )
1076 {
1077 return GET_VIRTUAL_ROOT()->GetData();
1078 }
1079
1080 // Visible node.
1081 if ( !DoGetItem(&tvItem) )
1082 {
1083 return NULL;
1084 }
1085
1086 wxTreeItemData *data = (wxTreeItemData *)tvItem.lParam;
1087 if ( IsDataIndirect(data) )
1088 {
1089 data = ((wxTreeItemIndirectData *)data)->GetData();
1090 }
1091
1092 return data;
1093}
1094
1095void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
1096{
1097 if ( IS_VIRTUAL_ROOT(item) )
1098 {
1099 GET_VIRTUAL_ROOT()->SetData(data);
1100 }
1101
1102 // first, associate this piece of data with this item
1103 if ( data )
1104 {
1105 data->SetId(item);
1106 }
1107
1108 wxTreeViewItem tvItem(item, TVIF_PARAM);
1109
1110 if ( HasIndirectData(item) )
1111 {
1112 if ( DoGetItem(&tvItem) )
1113 {
1114 ((wxTreeItemIndirectData *)tvItem.lParam)->SetData(data);
1115 }
1116 else
1117 {
1118 wxFAIL_MSG( wxT("failed to change tree items data") );
1119 }
1120 }
1121 else
1122 {
1123 tvItem.lParam = (LPARAM)data;
1124 DoSetItem(&tvItem);
1125 }
1126}
1127
1128void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId& item,
1129 wxTreeItemIndirectData *data)
1130{
1131 // this should never happen because it's unnecessary and will probably lead
1132 // to crash too because the code elsewhere supposes that the pointer the
1133 // wxTreeItemIndirectData has is a real wxItemData and not
1134 // wxTreeItemIndirectData as well
1135 wxASSERT_MSG( !HasIndirectData(item), wxT("setting indirect data twice?") );
1136
1137 SetItemData(item, data);
1138}
1139
1140bool wxTreeCtrl::HasIndirectData(const wxTreeItemId& item) const
1141{
1142 // query the item itself
1143 wxTreeViewItem tvItem(item, TVIF_PARAM);
1144 if ( !DoGetItem(&tvItem) )
1145 {
1146 return false;
1147 }
1148
1149 wxTreeItemData *data = (wxTreeItemData *)tvItem.lParam;
1150
1151 return data && IsDataIndirect(data);
1152}
1153
1154void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
1155{
1156 if ( IS_VIRTUAL_ROOT(item) )
1157 return;
1158
1159 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1160 tvItem.cChildren = (int)has;
1161 DoSetItem(&tvItem);
1162}
1163
1164void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
1165{
1166 if ( IS_VIRTUAL_ROOT(item) )
1167 return;
1168
1169 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1170 tvItem.state = bold ? TVIS_BOLD : 0;
1171 DoSetItem(&tvItem);
1172}
1173
1174void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
1175{
1176 if ( IS_VIRTUAL_ROOT(item) )
1177 return;
1178
1179 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
1180 tvItem.state = highlight ? TVIS_DROPHILITED : 0;
1181 DoSetItem(&tvItem);
1182}
1183
1184void wxTreeCtrl::RefreshItem(const wxTreeItemId& item)
1185{
1186 if ( IS_VIRTUAL_ROOT(item) )
1187 return;
1188
1189 wxRect rect;
1190 if ( GetBoundingRect(item, rect) )
1191 {
1192 RefreshRect(rect);
1193 }
1194}
1195
1196wxColour wxTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
1197{
1198 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
1199
1200 return it == m_attrs.end() ? wxNullColour : it->second->GetTextColour();
1201}
1202
1203wxColour wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const
1204{
1205 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
1206
1207 return it == m_attrs.end() ? wxNullColour : it->second->GetBackgroundColour();
1208}
1209
1210wxFont wxTreeCtrl::GetItemFont(const wxTreeItemId& item) const
1211{
1212 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
1213
1214 return it == m_attrs.end() ? wxNullFont : it->second->GetFont();
1215}
1216
1217void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
1218 const wxColour& col)
1219{
1220 wxTreeItemAttr *attr;
1221 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
1222 if ( it == m_attrs.end() )
1223 {
1224 m_hasAnyAttr = true;
1225
1226 m_attrs[item.m_pItem] =
1227 attr = new wxTreeItemAttr;
1228 }
1229 else
1230 {
1231 attr = it->second;
1232 }
1233
1234 attr->SetTextColour(col);
1235
1236 RefreshItem(item);
1237}
1238
1239void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
1240 const wxColour& col)
1241{
1242 wxTreeItemAttr *attr;
1243 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
1244 if ( it == m_attrs.end() )
1245 {
1246 m_hasAnyAttr = true;
1247
1248 m_attrs[item.m_pItem] =
1249 attr = new wxTreeItemAttr;
1250 }
1251 else // already in the hash
1252 {
1253 attr = it->second;
1254 }
1255
1256 attr->SetBackgroundColour(col);
1257
1258 RefreshItem(item);
1259}
1260
1261void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
1262{
1263 wxTreeItemAttr *attr;
1264 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
1265 if ( it == m_attrs.end() )
1266 {
1267 m_hasAnyAttr = true;
1268
1269 m_attrs[item.m_pItem] =
1270 attr = new wxTreeItemAttr;
1271 }
1272 else // already in the hash
1273 {
1274 attr = it->second;
1275 }
1276
1277 attr->SetFont(font);
1278
1279 RefreshItem(item);
1280}
1281
1282// ----------------------------------------------------------------------------
1283// Item status
1284// ----------------------------------------------------------------------------
1285
1286bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
1287{
1288 if ( item == wxTreeItemId(TVI_ROOT) )
1289 {
1290 // virtual (hidden) root is never visible
1291 return false;
1292 }
1293
1294 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1295 RECT rect;
1296
1297 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1298 // the HTREEITEM with TVM_GETITEMRECT
1299 *(HTREEITEM *)&rect = HITEM(item);
1300
1301 // false means get item rect for the whole item, not only text
1302 return SendMessage(GetHwnd(), TVM_GETITEMRECT, false, (LPARAM)&rect) != 0;
1303}
1304
1305bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
1306{
1307 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1308 DoGetItem(&tvItem);
1309
1310 return tvItem.cChildren != 0;
1311}
1312
1313bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
1314{
1315 // probably not a good idea to put it here
1316 //wxASSERT( ItemHasChildren(item) );
1317
1318 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
1319 DoGetItem(&tvItem);
1320
1321 return (tvItem.state & TVIS_EXPANDED) != 0;
1322}
1323
1324bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
1325{
1326 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
1327 DoGetItem(&tvItem);
1328
1329 return (tvItem.state & TVIS_SELECTED) != 0;
1330}
1331
1332bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
1333{
1334 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1335 DoGetItem(&tvItem);
1336
1337 return (tvItem.state & TVIS_BOLD) != 0;
1338}
1339
1340// ----------------------------------------------------------------------------
1341// navigation
1342// ----------------------------------------------------------------------------
1343
1344wxTreeItemId wxTreeCtrl::GetRootItem() const
1345{
1346 // Root may be real (visible) or virtual (hidden).
1347 if ( GET_VIRTUAL_ROOT() )
1348 return TVI_ROOT;
1349
1350 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1351}
1352
1353wxTreeItemId wxTreeCtrl::GetSelection() const
1354{
1355 wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), wxTreeItemId(),
1356 wxT("this only works with single selection controls") );
1357
1358 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1359}
1360
1361wxTreeItemId wxTreeCtrl::GetItemParent(const wxTreeItemId& item) const
1362{
1363 HTREEITEM hItem;
1364
1365 if ( IS_VIRTUAL_ROOT(item) )
1366 {
1367 // no parent for the virtual root
1368 hItem = 0;
1369 }
1370 else // normal item
1371 {
1372 hItem = TreeView_GetParent(GetHwnd(), HITEM(item));
1373 if ( !hItem && HasFlag(wxTR_HIDE_ROOT) )
1374 {
1375 // the top level items should have the virtual root as their parent
1376 hItem = TVI_ROOT;
1377 }
1378 }
1379
1380 return wxTreeItemId(hItem);
1381}
1382
1383wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1384 wxTreeItemIdValue& cookie) const
1385{
1386 // remember the last child returned in 'cookie'
1387 cookie = TreeView_GetChild(GetHwnd(), HITEM(item));
1388
1389 return wxTreeItemId(cookie);
1390}
1391
1392wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
1393 wxTreeItemIdValue& cookie) const
1394{
1395 wxTreeItemId item(TreeView_GetNextSibling(GetHwnd(),
1396 HITEM(wxTreeItemId(cookie))));
1397 cookie = item.m_pItem;
1398
1399 return item;
1400}
1401
1402#if WXWIN_COMPATIBILITY_2_4
1403
1404wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1405 long& cookie) const
1406{
1407 cookie = (long)TreeView_GetChild(GetHwnd(), HITEM(item));
1408
1409 return wxTreeItemId((void *)cookie);
1410}
1411
1412wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
1413 long& cookie) const
1414{
1415 wxTreeItemId item(TreeView_GetNextSibling
1416 (
1417 GetHwnd(),
1418 HITEM(wxTreeItemId((void *)cookie)
1419 )));
1420 cookie = (long)item.m_pItem;
1421
1422 return item;
1423}
1424
1425#endif // WXWIN_COMPATIBILITY_2_4
1426
1427wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
1428{
1429 // can this be done more efficiently?
1430 wxTreeItemIdValue cookie;
1431
1432 wxTreeItemId childLast,
1433 child = GetFirstChild(item, cookie);
1434 while ( child.IsOk() )
1435 {
1436 childLast = child;
1437 child = GetNextChild(item, cookie);
1438 }
1439
1440 return childLast;
1441}
1442
1443wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
1444{
1445 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item)));
1446}
1447
1448wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
1449{
1450 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item)));
1451}
1452
1453wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
1454{
1455 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1456}
1457
1458wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
1459{
1460 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() for must be visible itself!"));
1461
1462 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item)));
1463}
1464
1465wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1466{
1467 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1468
1469 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item)));
1470}
1471
1472// ----------------------------------------------------------------------------
1473// multiple selections emulation
1474// ----------------------------------------------------------------------------
1475
1476bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const
1477{
1478 // receive the desired information.
1479 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1480 DoGetItem(&tvItem);
1481
1482 // state image indices are 1 based
1483 return ((tvItem.state >> 12) - 1) == 1;
1484}
1485
1486void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check)
1487{
1488 // receive the desired information.
1489 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1490
1491 DoGetItem(&tvItem);
1492
1493 // state images are one-based
1494 tvItem.state = (check ? 2 : 1) << 12;
1495
1496 DoSetItem(&tvItem);
1497}
1498
1499size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
1500{
1501 TraverseSelections selector(this, selections);
1502
1503 return selector.GetCount();
1504}
1505
1506// ----------------------------------------------------------------------------
1507// Usual operations
1508// ----------------------------------------------------------------------------
1509
1510wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
1511 wxTreeItemId hInsertAfter,
1512 const wxString& text,
1513 int image, int selectedImage,
1514 wxTreeItemData *data)
1515{
1516 wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()),
1517 wxTreeItemId(),
1518 _T("can't have more than one root in the tree") );
1519
1520 TV_INSERTSTRUCT tvIns;
1521 tvIns.hParent = HITEM(parent);
1522 tvIns.hInsertAfter = HITEM(hInsertAfter);
1523
1524 // this is how we insert the item as the first child: supply a NULL
1525 // hInsertAfter
1526 if ( !tvIns.hInsertAfter )
1527 {
1528 tvIns.hInsertAfter = TVI_FIRST;
1529 }
1530
1531 UINT mask = 0;
1532 if ( !text.IsEmpty() )
1533 {
1534 mask |= TVIF_TEXT;
1535 tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok
1536 }
1537 else
1538 {
1539 tvIns.item.pszText = NULL;
1540 tvIns.item.cchTextMax = 0;
1541 }
1542
1543 if ( image != -1 )
1544 {
1545 mask |= TVIF_IMAGE;
1546 tvIns.item.iImage = image;
1547
1548 if ( selectedImage == -1 )
1549 {
1550 // take the same image for selected icon if not specified
1551 selectedImage = image;
1552 }
1553 }
1554
1555 if ( selectedImage != -1 )
1556 {
1557 mask |= TVIF_SELECTEDIMAGE;
1558 tvIns.item.iSelectedImage = selectedImage;
1559 }
1560
1561 if ( data != NULL )
1562 {
1563 mask |= TVIF_PARAM;
1564 tvIns.item.lParam = (LPARAM)data;
1565 }
1566
1567 tvIns.item.mask = mask;
1568
1569 HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns);
1570 if ( id == 0 )
1571 {
1572 wxLogLastError(wxT("TreeView_InsertItem"));
1573 }
1574
1575 if ( data != NULL )
1576 {
1577 // associate the application tree item with Win32 tree item handle
1578 data->SetId(id);
1579 }
1580
1581 return wxTreeItemId(id);
1582}
1583
1584// for compatibility only
1585#if WXWIN_COMPATIBILITY_2_4
1586
1587wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1588 const wxString& text,
1589 int image, int selImage,
1590 long insertAfter)
1591{
1592 return DoInsertItem(parent, wxTreeItemId((void *)insertAfter), text,
1593 image, selImage, NULL);
1594}
1595
1596#endif // WXWIN_COMPATIBILITY_2_4
1597
1598wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
1599 int image, int selectedImage,
1600 wxTreeItemData *data)
1601{
1602
1603 if ( m_windowStyle & wxTR_HIDE_ROOT )
1604 {
1605 // create a virtual root item, the parent for all the others
1606 m_pVirtualRoot = new wxVirtualNode(data);
1607
1608 return TVI_ROOT;
1609 }
1610
1611 return DoInsertItem(wxTreeItemId(), wxTreeItemId(),
1612 text, image, selectedImage, data);
1613}
1614
1615wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
1616 const wxString& text,
1617 int image, int selectedImage,
1618 wxTreeItemData *data)
1619{
1620 return DoInsertItem(parent, TVI_FIRST,
1621 text, image, selectedImage, data);
1622}
1623
1624wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1625 const wxTreeItemId& idPrevious,
1626 const wxString& text,
1627 int image, int selectedImage,
1628 wxTreeItemData *data)
1629{
1630 return DoInsertItem(parent, idPrevious, text, image, selectedImage, data);
1631}
1632
1633wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1634 size_t index,
1635 const wxString& text,
1636 int image, int selectedImage,
1637 wxTreeItemData *data)
1638{
1639 // find the item from index
1640 wxTreeItemIdValue cookie;
1641 wxTreeItemId idPrev, idCur = GetFirstChild(parent, cookie);
1642 while ( index != 0 && idCur.IsOk() )
1643 {
1644 index--;
1645
1646 idPrev = idCur;
1647 idCur = GetNextChild(parent, cookie);
1648 }
1649
1650 // assert, not check: if the index is invalid, we will append the item
1651 // to the end
1652 wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") );
1653
1654 return DoInsertItem(parent, idPrev, text, image, selectedImage, data);
1655}
1656
1657wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
1658 const wxString& text,
1659 int image, int selectedImage,
1660 wxTreeItemData *data)
1661{
1662 return DoInsertItem(parent, TVI_LAST,
1663 text, image, selectedImage, data);
1664}
1665
1666void wxTreeCtrl::Delete(const wxTreeItemId& item)
1667{
1668 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) )
1669 {
1670 wxLogLastError(wxT("TreeView_DeleteItem"));
1671 }
1672}
1673
1674// delete all children (but don't delete the item itself)
1675void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
1676{
1677 wxTreeItemIdValue cookie;
1678
1679 wxArrayTreeItemIds children;
1680 wxTreeItemId child = GetFirstChild(item, cookie);
1681 while ( child.IsOk() )
1682 {
1683 children.Add(child);
1684
1685 child = GetNextChild(item, cookie);
1686 }
1687
1688 size_t nCount = children.Count();
1689 for ( size_t n = 0; n < nCount; n++ )
1690 {
1691 if ( !TreeView_DeleteItem(GetHwnd(), HITEM_PTR(children[n])) )
1692 {
1693 wxLogLastError(wxT("TreeView_DeleteItem"));
1694 }
1695 }
1696}
1697
1698void wxTreeCtrl::DeleteAllItems()
1699{
1700 // delete the "virtual" root item.
1701 if ( GET_VIRTUAL_ROOT() )
1702 {
1703 delete GET_VIRTUAL_ROOT();
1704 m_pVirtualRoot = NULL;
1705 }
1706
1707 // and all the real items
1708
1709 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1710 {
1711 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1712 }
1713}
1714
1715void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
1716{
1717 wxASSERT_MSG( flag == TVE_COLLAPSE ||
1718 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
1719 flag == TVE_EXPAND ||
1720 flag == TVE_TOGGLE,
1721 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1722
1723 // A hidden root can be neither expanded nor collapsed.
1724 wxCHECK_RET( !(m_windowStyle & wxTR_HIDE_ROOT) || (HITEM(item) != TVI_ROOT),
1725 wxT("Can't expand/collapse hidden root node!") )
1726
1727 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1728 // emulate them. This behaviour has changed slightly with comctl32.dll
1729 // v 4.70 - now it does send them but only the first time. To maintain
1730 // compatible behaviour and also in order to not have surprises with the
1731 // future versions, don't rely on this and still do everything ourselves.
1732 // To avoid that the messages be sent twice when the item is expanded for
1733 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1734
1735 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE);
1736 tvItem.state = 0;
1737 DoSetItem(&tvItem);
1738
1739 if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) != 0 )
1740 {
1741 wxTreeEvent event(wxEVT_NULL, m_windowId);
1742 event.m_item = item;
1743 event.SetEventObject(this);
1744
1745 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1746 // itself
1747 event.SetEventType(gs_expandEvents[IsExpanded(item) ? IDX_EXPAND
1748 : IDX_COLLAPSE]
1749 [IDX_DONE]);
1750
1751 (void)GetEventHandler()->ProcessEvent(event);
1752 }
1753 //else: change didn't took place, so do nothing at all
1754}
1755
1756void wxTreeCtrl::Expand(const wxTreeItemId& item)
1757{
1758 DoExpand(item, TVE_EXPAND);
1759}
1760
1761void wxTreeCtrl::Collapse(const wxTreeItemId& item)
1762{
1763 DoExpand(item, TVE_COLLAPSE);
1764}
1765
1766void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1767{
1768 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
1769}
1770
1771void wxTreeCtrl::Toggle(const wxTreeItemId& item)
1772{
1773 DoExpand(item, TVE_TOGGLE);
1774}
1775
1776#if WXWIN_COMPATIBILITY_2_4
1777void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
1778{
1779 DoExpand(item, action);
1780}
1781#endif
1782
1783void wxTreeCtrl::Unselect()
1784{
1785 wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE),
1786 wxT("doesn't make sense, may be you want UnselectAll()?") );
1787
1788 // just remove the selection
1789 SelectItem(wxTreeItemId());
1790}
1791
1792void wxTreeCtrl::UnselectAll()
1793{
1794 if ( m_windowStyle & wxTR_MULTIPLE )
1795 {
1796 wxArrayTreeItemIds selections;
1797 size_t count = GetSelections(selections);
1798 for ( size_t n = 0; n < count; n++ )
1799 {
1800#if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1801 SetItemCheck(HITEM_PTR(selections[n]), false);
1802#else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1803 ::UnselectItem(GetHwnd(), HITEM_PTR(selections[n]));
1804#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1805 }
1806 }
1807 else
1808 {
1809 // just remove the selection
1810 Unselect();
1811 }
1812}
1813
1814void wxTreeCtrl::SelectItem(const wxTreeItemId& item)
1815{
1816 if ( m_windowStyle & wxTR_MULTIPLE )
1817 {
1818#if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1819 // selecting the item means checking it
1820 SetItemCheck(item);
1821#else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1822 ::SelectItem(GetHwnd(), HITEM(item));
1823#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1824 }
1825 else
1826 {
1827 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1828 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1829 // send them ourselves
1830
1831 wxTreeEvent event(wxEVT_NULL, m_windowId);
1832 event.m_item = item;
1833 event.SetEventObject(this);
1834
1835 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING);
1836 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
1837 {
1838 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item)) )
1839 {
1840 wxLogLastError(wxT("TreeView_SelectItem"));
1841 }
1842 else
1843 {
1844 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1845 (void)GetEventHandler()->ProcessEvent(event);
1846 }
1847 }
1848 //else: program vetoed the change
1849 }
1850}
1851
1852void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1853{
1854 // no error return
1855 TreeView_EnsureVisible(GetHwnd(), HITEM(item));
1856}
1857
1858void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
1859{
1860 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) )
1861 {
1862 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1863 }
1864}
1865
1866wxTextCtrl *wxTreeCtrl::GetEditControl() const
1867{
1868 return m_textCtrl;
1869}
1870
1871void wxTreeCtrl::DeleteTextCtrl()
1872{
1873 if ( m_textCtrl )
1874 {
1875 // the HWND corresponding to this control is deleted by the tree
1876 // control itself and we don't know when exactly this happens, so check
1877 // if the window still exists before calling UnsubclassWin()
1878 if ( !::IsWindow(GetHwndOf(m_textCtrl)) )
1879 {
1880 m_textCtrl->SetHWND(0);
1881 }
1882
1883 m_textCtrl->UnsubclassWin();
1884 m_textCtrl->SetHWND(0);
1885 delete m_textCtrl;
1886 m_textCtrl = NULL;
1887 }
1888}
1889
1890wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
1891 wxClassInfo* textControlClass)
1892{
1893 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
1894
1895 DeleteTextCtrl();
1896
1897 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
1898 HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item));
1899
1900 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1901 // returned false
1902 if ( !hWnd )
1903 {
1904 delete m_textCtrl;
1905 m_textCtrl = NULL;
1906 return NULL;
1907 }
1908
1909 // textctrl is subclassed in MSWOnNotify
1910 return m_textCtrl;
1911}
1912
1913// End label editing, optionally cancelling the edit
1914void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool discardChanges)
1915{
1916 TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
1917
1918 DeleteTextCtrl();
1919}
1920
1921wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
1922{
1923 TV_HITTESTINFO hitTestInfo;
1924 hitTestInfo.pt.x = (int)point.x;
1925 hitTestInfo.pt.y = (int)point.y;
1926
1927 TreeView_HitTest(GetHwnd(), &hitTestInfo);
1928
1929 flags = 0;
1930
1931 // avoid repetition
1932 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1933 flags |= wxTREE_HITTEST_##flag
1934
1935 TRANSLATE_FLAG(ABOVE);
1936 TRANSLATE_FLAG(BELOW);
1937 TRANSLATE_FLAG(NOWHERE);
1938 TRANSLATE_FLAG(ONITEMBUTTON);
1939 TRANSLATE_FLAG(ONITEMICON);
1940 TRANSLATE_FLAG(ONITEMINDENT);
1941 TRANSLATE_FLAG(ONITEMLABEL);
1942 TRANSLATE_FLAG(ONITEMRIGHT);
1943 TRANSLATE_FLAG(ONITEMSTATEICON);
1944 TRANSLATE_FLAG(TOLEFT);
1945 TRANSLATE_FLAG(TORIGHT);
1946
1947 #undef TRANSLATE_FLAG
1948
1949 return wxTreeItemId(hitTestInfo.hItem);
1950}
1951
1952bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
1953 wxRect& rect,
1954 bool textOnly) const
1955{
1956 RECT rc;
1957
1958 // Virtual root items have no bounding rectangle
1959 if ( IS_VIRTUAL_ROOT(item) )
1960 {
1961 return false;
1962 }
1963
1964 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item),
1965 &rc, textOnly) )
1966 {
1967 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
1968
1969 return true;
1970 }
1971 else
1972 {
1973 // couldn't retrieve rect: for example, item isn't visible
1974 return false;
1975 }
1976}
1977
1978// ----------------------------------------------------------------------------
1979// sorting stuff
1980// ----------------------------------------------------------------------------
1981
1982// this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1983// functions such as IsDataIndirect()
1984class wxTreeSortHelper
1985{
1986public:
1987 static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree);
1988
1989private:
1990 static wxTreeItemId GetIdFromData(wxTreeCtrl *tree, LPARAM item)
1991 {
1992 wxTreeItemData *data = (wxTreeItemData *)item;
1993 if ( tree->IsDataIndirect(data) )
1994 {
1995 data = ((wxTreeItemIndirectData *)data)->GetData();
1996 }
1997
1998 return data->GetId();
1999 }
2000};
2001
2002int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1,
2003 LPARAM pItem2,
2004 LPARAM htree)
2005{
2006 wxCHECK_MSG( pItem1 && pItem2, 0,
2007 wxT("sorting tree without data doesn't make sense") );
2008
2009 wxTreeCtrl *tree = (wxTreeCtrl *)htree;
2010
2011 return tree->OnCompareItems(GetIdFromData(tree, pItem1),
2012 GetIdFromData(tree, pItem2));
2013}
2014
2015int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
2016 const wxTreeItemId& item2)
2017{
2018 return wxStrcmp(GetItemText(item1), GetItemText(item2));
2019}
2020
2021void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
2022{
2023 // rely on the fact that TreeView_SortChildren does the same thing as our
2024 // default behaviour, i.e. sorts items alphabetically and so call it
2025 // directly if we're not in derived class (much more efficient!)
2026 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
2027 {
2028 TreeView_SortChildren(GetHwnd(), HITEM(item), 0);
2029 }
2030 else
2031 {
2032 TV_SORTCB tvSort;
2033 tvSort.hParent = HITEM(item);
2034 tvSort.lpfnCompare = wxTreeSortHelper::Compare;
2035 tvSort.lParam = (LPARAM)this;
2036 TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
2037 }
2038}
2039
2040// ----------------------------------------------------------------------------
2041// implementation
2042// ----------------------------------------------------------------------------
2043
2044bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id)
2045{
2046 if ( cmd == EN_UPDATE )
2047 {
2048 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
2049 event.SetEventObject( this );
2050 ProcessCommand(event);
2051 }
2052 else if ( cmd == EN_KILLFOCUS )
2053 {
2054 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
2055 event.SetEventObject( this );
2056 ProcessCommand(event);
2057 }
2058 else
2059 {
2060 // nothing done
2061 return false;
2062 }
2063
2064 // command processed
2065 return true;
2066}
2067
2068// we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2069// only do it during dragging, minimize wxWin overhead (this is important for
2070// WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2071// instead of passing by wxWin events
2072long wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
2073{
2074 bool processed = false;
2075 long rc = 0;
2076 bool isMultiple = (GetWindowStyle() & wxTR_MULTIPLE) != 0;
2077
2078 if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
2079 {
2080 // we only process mouse messages here and these parameters have the
2081 // same meaning for all of them
2082 int x = GET_X_LPARAM(lParam),
2083 y = GET_Y_LPARAM(lParam);
2084 HTREEITEM htItem = GetItemFromPoint(GetHwnd(), x, y);
2085
2086 switch ( nMsg )
2087 {
2088 case WM_RBUTTONDOWN:
2089 // if the item we are about to right click on
2090 // is not already select, remove the entire
2091 // previous selection
2092 if (!::IsItemSelected(GetHwnd(), htItem))
2093 {
2094 UnselectAll();
2095 }
2096
2097 // select item and set the focus to the
2098 // newly selected item
2099 ::SelectItem(GetHwnd(), htItem);
2100 ::SetFocus(GetHwnd(), htItem);
2101 break;
2102
2103#if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2104 case WM_LBUTTONDOWN:
2105 if ( htItem && isMultiple )
2106 {
2107 if ( wParam & MK_CONTROL )
2108 {
2109 SetFocus();
2110
2111 // toggle selected state
2112 ToggleItemSelection(GetHwnd(), htItem);
2113
2114 ::SetFocus(GetHwnd(), htItem);
2115
2116 // reset on any click without Shift
2117 m_htSelStart.Unset();
2118
2119 processed = true;
2120 }
2121 else if ( wParam & MK_SHIFT )
2122 {
2123 // this selects all items between the starting one and
2124 // the current
2125
2126 if ( !m_htSelStart )
2127 {
2128 // take the focused item
2129 m_htSelStart = TreeView_GetSelection(GetHwnd());
2130 }
2131
2132 SelectRange(GetHwnd(), HITEM(m_htSelStart), htItem,
2133 !(wParam & MK_CONTROL));
2134
2135 ::SetFocus(GetHwnd(), htItem);
2136
2137 processed = true;
2138 }
2139 else // normal click
2140 {
2141 // avoid doing anything if we click on the only
2142 // currently selected item
2143
2144 wxArrayTreeItemIds selections;
2145 size_t count = GetSelections(selections);
2146 if ( count == 0 ||
2147 count > 1 ||
2148 HITEM_PTR(selections[0]) != htItem )
2149 {
2150 // clear the previously selected items, if the
2151 // user clicked outside of the present selection.
2152 // otherwise, perform the deselection on mouse-up.
2153 // this allows multiple drag and drop to work.
2154
2155 if (IsItemSelected(GetHwnd(), htItem))
2156 {
2157 ::SetFocus(GetHwnd(), htItem);
2158 }
2159 else
2160 {
2161 UnselectAll();
2162
2163 // prevent the click from starting in-place editing
2164 // which should only happen if we click on the
2165 // already selected item (and nothing else is
2166 // selected)
2167
2168 TreeView_SelectItem(GetHwnd(), 0);
2169 ::SelectItem(GetHwnd(), htItem);
2170 }
2171 }
2172
2173 // reset on any click without Shift
2174 m_htSelStart.Unset();
2175 }
2176 }
2177 break;
2178#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2179
2180 case WM_MOUSEMOVE:
2181 if ( m_dragImage )
2182 {
2183 m_dragImage->Move(wxPoint(x, y));
2184 if ( htItem )
2185 {
2186 // highlight the item as target (hiding drag image is
2187 // necessary - otherwise the display will be corrupted)
2188 m_dragImage->Hide();
2189 TreeView_SelectDropTarget(GetHwnd(), htItem);
2190 m_dragImage->Show();
2191 }
2192 }
2193 break;
2194
2195 case WM_LBUTTONUP:
2196
2197 // facilitates multiple drag-and-drop
2198 if (htItem && isMultiple)
2199 {
2200 wxArrayTreeItemIds selections;
2201 size_t count = GetSelections(selections);
2202
2203 if (count > 1 &&
2204 !(wParam & MK_CONTROL) &&
2205 !(wParam & MK_SHIFT))
2206 {
2207 UnselectAll();
2208 TreeView_SelectItem(GetHwnd(), htItem);
2209 }
2210 }
2211
2212 // fall through
2213
2214 case WM_RBUTTONUP:
2215 if ( m_dragImage )
2216 {
2217 m_dragImage->EndDrag();
2218 delete m_dragImage;
2219 m_dragImage = NULL;
2220
2221 // generate the drag end event
2222 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, m_windowId);
2223
2224 event.m_item = htItem;
2225 event.m_pointDrag = wxPoint(x, y);
2226 event.SetEventObject(this);
2227
2228 (void)GetEventHandler()->ProcessEvent(event);
2229
2230 // if we don't do it, the tree seems to think that 2 items
2231 // are selected simultaneously which is quite weird
2232 TreeView_SelectDropTarget(GetHwnd(), 0);
2233 }
2234 break;
2235 }
2236 }
2237#if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2238 else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) && isMultiple )
2239 {
2240 // the tree control greys out the selected item when it loses focus and
2241 // paints it as selected again when it regains it, but it won't do it
2242 // for the other items itself - help it
2243 wxArrayTreeItemIds selections;
2244 size_t count = GetSelections(selections);
2245 RECT rect;
2246 for ( size_t n = 0; n < count; n++ )
2247 {
2248 // TreeView_GetItemRect() will return false if item is not visible,
2249 // which may happen perfectly well
2250 if ( TreeView_GetItemRect(GetHwnd(), HITEM_PTR(selections[n]),
2251 &rect, true) )
2252 {
2253 ::InvalidateRect(GetHwnd(), &rect, false);
2254 }
2255 }
2256 }
2257 else if ( nMsg == WM_KEYDOWN && isMultiple )
2258 {
2259 bool bCtrl = wxIsCtrlDown(),
2260 bShift = wxIsShiftDown();
2261
2262 // we handle.arrows and space, but not page up/down and home/end: the
2263 // latter should be easy, but not the former
2264
2265 HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
2266 if ( !m_htSelStart )
2267 {
2268 m_htSelStart = htSel;
2269 }
2270
2271 if ( wParam == VK_SPACE )
2272 {
2273 if ( bCtrl )
2274 {
2275 ToggleItemSelection(GetHwnd(), htSel);
2276 }
2277 else
2278 {
2279 UnselectAll();
2280
2281 ::SelectItem(GetHwnd(), htSel);
2282 }
2283
2284 processed = true;
2285 }
2286 else if ( wParam == VK_UP || wParam == VK_DOWN )
2287 {
2288 if ( !bCtrl && !bShift )
2289 {
2290 // no modifiers, just clear selection and then let the default
2291 // processing to take place
2292 UnselectAll();
2293 }
2294 else if ( htSel )
2295 {
2296 (void)wxControl::MSWWindowProc(nMsg, wParam, lParam);
2297
2298 HTREEITEM htNext = (HTREEITEM)(wParam == VK_UP
2299 ? TreeView_GetPrevVisible(GetHwnd(), htSel)
2300 : TreeView_GetNextVisible(GetHwnd(), htSel));
2301
2302 if ( !htNext )
2303 {
2304 // at the top/bottom
2305 htNext = htSel;
2306 }
2307
2308 if ( bShift )
2309 {
2310 SelectRange(GetHwnd(), HITEM(m_htSelStart), htNext);
2311 }
2312 else // bCtrl
2313 {
2314 // without changing selection
2315 ::SetFocus(GetHwnd(), htNext);
2316 }
2317
2318 processed = true;
2319 }
2320 }
2321 }
2322#endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2323 else if ( nMsg == WM_CHAR )
2324 {
2325 // don't let the control process Space and Return keys because it
2326 // doesn't do anything useful with them anyhow but always beeps
2327 // annoyingly when it receives them and there is no way to turn it off
2328 // simply if you just process TREEITEM_ACTIVATED event to which Space
2329 // and Enter presses are mapped in your code
2330 if ( wParam == VK_SPACE || wParam == VK_RETURN )
2331 {
2332 processed = true;
2333 }
2334 }
2335
2336 if ( !processed )
2337 rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
2338
2339 return rc;
2340}
2341
2342// process WM_NOTIFY Windows message
2343bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
2344{
2345 wxTreeEvent event(wxEVT_NULL, m_windowId);
2346 wxEventType eventType = wxEVT_NULL;
2347 NMHDR *hdr = (NMHDR *)lParam;
2348
2349 switch ( hdr->code )
2350 {
2351 case TVN_BEGINDRAG:
2352 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
2353 // fall through
2354
2355 case TVN_BEGINRDRAG:
2356 {
2357 if ( eventType == wxEVT_NULL )
2358 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
2359 //else: left drag, already set above
2360
2361 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2362
2363 event.m_item = tv->itemNew.hItem;
2364 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
2365
2366 // don't allow dragging by default: the user code must
2367 // explicitly say that it wants to allow it to avoid breaking
2368 // the old apps
2369 event.Veto();
2370 }
2371 break;
2372
2373 case TVN_BEGINLABELEDIT:
2374 {
2375 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
2376 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2377
2378 event.m_item = info->item.hItem;
2379 event.m_label = info->item.pszText;
2380 event.m_editCancelled = false;
2381 }
2382 break;
2383
2384 case TVN_DELETEITEM:
2385 {
2386 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
2387 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2388
2389 event.m_item = tv->itemOld.hItem;
2390
2391 if ( m_hasAnyAttr )
2392 {
2393 wxMapTreeAttr::iterator it = m_attrs.find(tv->itemOld.hItem);
2394 if ( it != m_attrs.end() )
2395 {
2396 delete it->second;
2397 m_attrs.erase(it);
2398 }
2399 }
2400 }
2401 break;
2402
2403 case TVN_ENDLABELEDIT:
2404 {
2405 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
2406 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2407
2408 event.m_item = info->item.hItem;
2409 event.m_label = info->item.pszText;
2410 if (info->item.pszText == NULL)
2411 {
2412 event.m_editCancelled = true;
2413 }
2414 else
2415 {
2416 event.m_editCancelled = false;
2417 }
2418 break;
2419 }
2420
2421 case TVN_GETDISPINFO:
2422 eventType = wxEVT_COMMAND_TREE_GET_INFO;
2423 // fall through
2424
2425 case TVN_SETDISPINFO:
2426 {
2427 if ( eventType == wxEVT_NULL )
2428 eventType = wxEVT_COMMAND_TREE_SET_INFO;
2429 //else: get, already set above
2430
2431 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2432
2433 event.m_item = info->item.hItem;
2434 break;
2435 }
2436
2437 case TVN_ITEMEXPANDING:
2438 case TVN_ITEMEXPANDED:
2439 {
2440 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
2441
2442 int what;
2443 switch ( tv->action )
2444 {
2445 default:
2446 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action);
2447 // fall through
2448
2449 case TVE_EXPAND:
2450 what = IDX_EXPAND;
2451 break;
2452
2453 case TVE_COLLAPSE:
2454 what = IDX_COLLAPSE;
2455 break;
2456 }
2457
2458 int how = hdr->code == TVN_ITEMEXPANDING ? IDX_DOING
2459 : IDX_DONE;
2460
2461 eventType = gs_expandEvents[what][how];
2462
2463 event.m_item = tv->itemNew.hItem;
2464 }
2465 break;
2466
2467 case TVN_KEYDOWN:
2468 {
2469 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
2470 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
2471
2472 // fabricate the lParam and wParam parameters sufficiently
2473 // similar to the ones from a "real" WM_KEYDOWN so that
2474 // CreateKeyEvent() works correctly
2475 WXLPARAM lParam =
2476 (::GetKeyState(VK_MENU) < 0 ? KF_ALTDOWN : 0) << 16;
2477
2478 WXWPARAM wParam = info->wVKey;
2479
2480 int keyCode = wxCharCodeMSWToWX(info->wVKey);
2481 if ( !keyCode )
2482 {
2483 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2484 // simple ASCII key
2485 keyCode = wParam;
2486 }
2487
2488 event.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN,
2489 keyCode,
2490 lParam,
2491 wParam);
2492
2493 // a separate event for Space/Return
2494 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2495 ((info->wVKey == VK_SPACE) || (info->wVKey == VK_RETURN)) )
2496 {
2497 wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
2498 m_windowId);
2499 event2.SetEventObject(this);
2500 if ( !(GetWindowStyle() & wxTR_MULTIPLE) )
2501 {
2502 event2.m_item = GetSelection();
2503 }
2504 //else: don't know how to get it
2505
2506 (void)GetEventHandler()->ProcessEvent(event2);
2507 }
2508 }
2509 break;
2510
2511 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2512 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2513 // we have to handle both messages:
2514 case TVN_SELCHANGEDA:
2515 case TVN_SELCHANGEDW:
2516 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
2517 // fall through
2518
2519 case TVN_SELCHANGINGA:
2520 case TVN_SELCHANGINGW:
2521 {
2522 if ( eventType == wxEVT_NULL )
2523 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
2524 //else: already set above
2525
2526 if (hdr->code == TVN_SELCHANGINGW ||
2527 hdr->code == TVN_SELCHANGEDW)
2528 {
2529 NM_TREEVIEWW* tv = (NM_TREEVIEWW *)lParam;
2530 event.m_item = tv->itemNew.hItem;
2531 event.m_itemOld = tv->itemOld.hItem;
2532 }
2533 else
2534 {
2535 NM_TREEVIEWA* tv = (NM_TREEVIEWA *)lParam;
2536 event.m_item = tv->itemNew.hItem;
2537 event.m_itemOld = tv->itemOld.hItem;
2538 }
2539 }
2540 break;
2541
2542#if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
2543 case NM_CUSTOMDRAW:
2544 {
2545 LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam;
2546 NMCUSTOMDRAW& nmcd = lptvcd->nmcd;
2547 switch ( nmcd.dwDrawStage )
2548 {
2549 case CDDS_PREPAINT:
2550 // if we've got any items with non standard attributes,
2551 // notify us before painting each item
2552 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
2553 : CDRF_DODEFAULT;
2554 break;
2555
2556 case CDDS_ITEMPREPAINT:
2557 {
2558 wxMapTreeAttr::iterator
2559 it = m_attrs.find((void *)nmcd.dwItemSpec);
2560
2561 if ( it == m_attrs.end() )
2562 {
2563 // nothing to do for this item
2564 *result = CDRF_DODEFAULT;
2565 break;
2566 }
2567
2568 wxTreeItemAttr * const attr = it->second;
2569
2570 HFONT hFont;
2571 if ( attr->HasFont() )
2572 {
2573 hFont = GetHfontOf(attr->GetFont());
2574 }
2575 else
2576 {
2577 hFont = 0;
2578 }
2579
2580 wxColour colText;
2581 if ( attr->HasTextColour() )
2582 {
2583 colText = attr->GetTextColour();
2584 }
2585 else
2586 {
2587 colText = GetForegroundColour();
2588 }
2589
2590 // selection colours should override ours
2591 if ( nmcd.uItemState & CDIS_SELECTED )
2592 {
2593 lptvcd->clrTextBk =
2594 ::GetSysColor(COLOR_HIGHLIGHT);
2595 lptvcd->clrText =
2596 ::GetSysColor(COLOR_HIGHLIGHTTEXT);
2597 }
2598 else // !selected
2599 {
2600 wxColour colBack;
2601 if ( attr->HasBackgroundColour() )
2602 {
2603 colBack = attr->GetBackgroundColour();
2604 }
2605 else
2606 {
2607 colBack = GetBackgroundColour();
2608 }
2609
2610 lptvcd->clrText = wxColourToRGB(colText);
2611 lptvcd->clrTextBk = wxColourToRGB(colBack);
2612 }
2613
2614 // note that if we wanted to set colours for
2615 // individual columns (subitems), we would have
2616 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2617 if ( hFont )
2618 {
2619 ::SelectObject(nmcd.hdc, hFont);
2620
2621 *result = CDRF_NEWFONT;
2622 }
2623 else
2624 {
2625 *result = CDRF_DODEFAULT;
2626 }
2627 }
2628 break;
2629
2630 default:
2631 *result = CDRF_DODEFAULT;
2632 }
2633 }
2634
2635 // we always process it
2636 return true;
2637#endif // _WIN32_IE >= 0x300
2638
2639 case NM_CLICK:
2640 {
2641 DWORD pos = GetMessagePos();
2642 POINT point;
2643 point.x = LOWORD(pos);
2644 point.y = HIWORD(pos);
2645 ::MapWindowPoints(HWND_DESKTOP, GetHwnd(), &point, 1);
2646 int flags = 0;
2647 wxTreeItemId item = HitTest(wxPoint(point.x, point.y), flags);
2648 if (flags & wxTREE_HITTEST_ONITEMSTATEICON)
2649 {
2650 event.m_item = item;
2651 eventType = wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK;
2652 }
2653 break;
2654 }
2655
2656 case NM_DBLCLK:
2657 case NM_RCLICK:
2658 {
2659 TV_HITTESTINFO tvhti;
2660 ::GetCursorPos(&tvhti.pt);
2661 ::ScreenToClient(GetHwnd(), &tvhti.pt);
2662 if ( TreeView_HitTest(GetHwnd(), &tvhti) )
2663 {
2664 if ( tvhti.flags & TVHT_ONITEM )
2665 {
2666 event.m_item = tvhti.hItem;
2667 eventType = (int)hdr->code == NM_DBLCLK
2668 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2669 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
2670
2671 event.m_pointDrag.x = tvhti.pt.x;
2672 event.m_pointDrag.y = tvhti.pt.y;
2673 }
2674
2675 break;
2676 }
2677 }
2678 // fall through
2679
2680 default:
2681 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2682 }
2683
2684 event.SetEventObject(this);
2685 event.SetEventType(eventType);
2686
2687 bool processed = GetEventHandler()->ProcessEvent(event);
2688
2689 // post processing
2690 switch ( hdr->code )
2691 {
2692 case NM_DBLCLK:
2693 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2694 // the return code of this event handler as the return value for
2695 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2696 // expanded status would never work
2697 *result = false;
2698 break;
2699
2700 case TVN_BEGINDRAG:
2701 case TVN_BEGINRDRAG:
2702 if ( event.IsAllowed() )
2703 {
2704 // normally this is impossible because the m_dragImage is
2705 // deleted once the drag operation is over
2706 wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") );
2707
2708 m_dragImage = new wxDragImage(*this, event.m_item);
2709 m_dragImage->BeginDrag(wxPoint(0, 0), this);
2710 m_dragImage->Show();
2711 }
2712 break;
2713
2714 case TVN_DELETEITEM:
2715 {
2716 // NB: we might process this message using wxWindows event
2717 // tables, but due to overhead of wxWin event system we
2718 // prefer to do it here ourself (otherwise deleting a tree
2719 // with many items is just too slow)
2720 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
2721
2722 wxTreeItemId item = event.m_item;
2723 if ( HasIndirectData(item) )
2724 {
2725 wxTreeItemIndirectData *data = (wxTreeItemIndirectData *)
2726 tv->itemOld.lParam;
2727 delete data; // can't be NULL here
2728 }
2729 else
2730 {
2731 wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
2732 delete data; // may be NULL, ok
2733 }
2734
2735 processed = true; // Make sure we don't get called twice
2736 }
2737 break;
2738
2739 case TVN_BEGINLABELEDIT:
2740 // return true to cancel label editing
2741 *result = !event.IsAllowed();
2742 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2743 if(event.IsAllowed())
2744 {
2745 HWND hText = TreeView_GetEditControl(GetHwnd());
2746 if(hText != NULL)
2747 {
2748 // MBN: if m_textCtrl already has an HWND, it is a stale
2749 // pointer from a previous edit (because the user
2750 // didn't modify the label before dismissing the control,
2751 // and TVN_ENDLABELEDIT was not sent), so delete it
2752 if(m_textCtrl && m_textCtrl->GetHWND() != 0)
2753 DeleteTextCtrl();
2754 if(!m_textCtrl)
2755 m_textCtrl = new wxTextCtrl();
2756 m_textCtrl->SetParent(this);
2757 m_textCtrl->SetHWND((WXHWND)hText);
2758 m_textCtrl->SubclassWin((WXHWND)hText);
2759
2760 // set wxTE_PROCESS_ENTER style for the text control to
2761 // force it to process the Enter presses itself, otherwise
2762 // they could be stolen from it by the dialog
2763 // navigation code
2764 m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle()
2765 | wxTE_PROCESS_ENTER);
2766 }
2767 }
2768 break;
2769
2770 case TVN_ENDLABELEDIT:
2771 // return true to set the label to the new string: note that we
2772 // also must pretend that we did process the message or it is going
2773 // to be passed to DefWindowProc() which will happily return false
2774 // cancelling the label change
2775 *result = event.IsAllowed();
2776 processed = true;
2777
2778 // ensure that we don't have the text ctrl which is going to be
2779 // deleted any more
2780 DeleteTextCtrl();
2781 break;
2782
2783 case TVN_SELCHANGING:
2784 case TVN_ITEMEXPANDING:
2785 // return true to prevent the action from happening
2786 *result = !event.IsAllowed();
2787 break;
2788
2789 case TVN_ITEMEXPANDED:
2790 // the item is not refreshed properly after expansion when it has
2791 // an image depending on the expanded/collapsed state - bug in
2792 // comctl32.dll or our code?
2793 {
2794 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
2795 wxTreeItemId id(tv->itemNew.hItem);
2796
2797 int image = GetItemImage(id, wxTreeItemIcon_Expanded);
2798 if ( image != -1 )
2799 {
2800 RefreshItem(id);
2801 }
2802 }
2803 break;
2804
2805 case TVN_GETDISPINFO:
2806 // NB: so far the user can't set the image himself anyhow, so do it
2807 // anyway - but this may change later
2808 //if ( /* !processed && */ 1 )
2809 {
2810 wxTreeItemId item = event.m_item;
2811 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2812 if ( info->item.mask & TVIF_IMAGE )
2813 {
2814 info->item.iImage =
2815 DoGetItemImageFromData
2816 (
2817 item,
2818 IsExpanded(item) ? wxTreeItemIcon_Expanded
2819 : wxTreeItemIcon_Normal
2820 );
2821 }
2822 if ( info->item.mask & TVIF_SELECTEDIMAGE )
2823 {
2824 info->item.iSelectedImage =
2825 DoGetItemImageFromData
2826 (
2827 item,
2828 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
2829 : wxTreeItemIcon_Selected
2830 );
2831 }
2832 }
2833 break;
2834
2835 //default:
2836 // for the other messages the return value is ignored and there is
2837 // nothing special to do
2838 }
2839 return processed;
2840}
2841
2842// ----------------------------------------------------------------------------
2843// State control.
2844// ----------------------------------------------------------------------------
2845
2846// why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2847#define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2848
2849void wxTreeCtrl::SetState(const wxTreeItemId& node, int state)
2850{
2851 TV_ITEM tvi;
2852 tvi.hItem = (HTREEITEM)node.m_pItem;
2853 tvi.mask = TVIF_STATE;
2854 tvi.stateMask = TVIS_STATEIMAGEMASK;
2855
2856 // Select the specified state, or -1 == cycle to the next one.
2857 if ( state == -1 )
2858 {
2859 TreeView_GetItem(GetHwnd(), &tvi);
2860
2861 state = STATEIMAGEMASKTOINDEX(tvi.state) + 1;
2862 if ( state == m_imageListState->GetImageCount() )
2863 state = 1;
2864 }
2865
2866 wxCHECK_RET( state < m_imageListState->GetImageCount(),
2867 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2868
2869 tvi.state = INDEXTOSTATEIMAGEMASK(state);
2870
2871 TreeView_SetItem(GetHwnd(), &tvi);
2872}
2873
2874int wxTreeCtrl::GetState(const wxTreeItemId& node)
2875{
2876 TV_ITEM tvi;
2877 tvi.hItem = (HTREEITEM)node.m_pItem;
2878 tvi.mask = TVIF_STATE;
2879 tvi.stateMask = TVIS_STATEIMAGEMASK;
2880 TreeView_GetItem(GetHwnd(), &tvi);
2881
2882 return STATEIMAGEMASKTOINDEX(tvi.state);
2883}
2884
2885#endif // wxUSE_TREECTRL
2886