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