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