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