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