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