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