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