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