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