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