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