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