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