]> git.saurik.com Git - wxWidgets.git/blame - src/msw/treectrl.cpp
use wxUniv's setup
[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
VZ
893 DoSetItem(&tvItem);
894}
2bda0e17 895
74b31181
VZ
896int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId& item,
897 wxTreeItemIcon which) const
898{
899 wxTreeViewItem tvItem(item, TVIF_PARAM);
900 if ( !DoGetItem(&tvItem) )
901 {
902 return -1;
903 }
904
905 return ((wxTreeItemIndirectData *)tvItem.lParam)->GetImage(which);
906}
907
908void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId& item,
909 int image,
910 wxTreeItemIcon which) const
911{
912 wxTreeViewItem tvItem(item, TVIF_PARAM);
913 if ( !DoGetItem(&tvItem) )
914 {
915 return;
916 }
917
918 wxTreeItemIndirectData *data = ((wxTreeItemIndirectData *)tvItem.lParam);
919
920 data->SetImage(image, which);
921
922 // make sure that we have selected images as well
923 if ( which == wxTreeItemIcon_Normal &&
924 !data->HasImage(wxTreeItemIcon_Selected) )
925 {
926 data->SetImage(image, wxTreeItemIcon_Selected);
927 }
928
929 if ( which == wxTreeItemIcon_Expanded &&
930 !data->HasImage(wxTreeItemIcon_SelectedExpanded) )
931 {
932 data->SetImage(image, wxTreeItemIcon_SelectedExpanded);
933 }
934}
935
9dfbf520
VZ
936void wxTreeCtrl::DoSetItemImages(const wxTreeItemId& item,
937 int image,
938 int imageSel)
939{
940 wxTreeViewItem tvItem(item, TVIF_IMAGE | TVIF_SELECTEDIMAGE);
941 tvItem.iSelectedImage = imageSel;
942 tvItem.iImage = image;
943 DoSetItem(&tvItem);
944}
945
74b31181
VZ
946int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
947 wxTreeItemIcon which) const
08b7c251 948{
a9c1265f
VZ
949 if ( (HITEM(item) == TVI_ROOT) && (m_windowStyle & wxTR_HIDE_ROOT) )
950 {
951 // TODO: Maybe a hidden root can still provide images?
952 return -1;
953 }
954
74b31181
VZ
955 if ( HasIndirectData(item) )
956 {
957 return DoGetItemImageFromData(item, which);
958 }
2bda0e17 959
74b31181
VZ
960 UINT mask;
961 switch ( which )
962 {
963 default:
223d09f6 964 wxFAIL_MSG( wxT("unknown tree item image type") );
2bda0e17 965
74b31181
VZ
966 case wxTreeItemIcon_Normal:
967 mask = TVIF_IMAGE;
968 break;
2bda0e17 969
74b31181
VZ
970 case wxTreeItemIcon_Selected:
971 mask = TVIF_SELECTEDIMAGE;
972 break;
973
974 case wxTreeItemIcon_Expanded:
975 case wxTreeItemIcon_SelectedExpanded:
976 return -1;
977 }
978
979 wxTreeViewItem tvItem(item, mask);
08b7c251 980 DoGetItem(&tvItem);
2bda0e17 981
74b31181 982 return mask == TVIF_IMAGE ? tvItem.iImage : tvItem.iSelectedImage;
2bda0e17
KB
983}
984
74b31181
VZ
985void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
986 wxTreeItemIcon which)
2bda0e17 987{
a9c1265f
VZ
988 if ( IS_VIRTUAL_ROOT(item) )
989 {
990 // TODO: Maybe a hidden root can still store images?
991 return;
992 }
993
74b31181
VZ
994 int imageNormal, imageSel;
995 switch ( which )
996 {
997 default:
223d09f6 998 wxFAIL_MSG( wxT("unknown tree item image type") );
74b31181
VZ
999
1000 case wxTreeItemIcon_Normal:
1001 imageNormal = image;
1002 imageSel = GetItemSelectedImage(item);
1003 break;
1004
1005 case wxTreeItemIcon_Selected:
1006 imageNormal = GetItemImage(item);
1007 imageSel = image;
1008 break;
1009
1010 case wxTreeItemIcon_Expanded:
1011 case wxTreeItemIcon_SelectedExpanded:
1012 if ( !HasIndirectData(item) )
1013 {
1014 // we need to get the old images first, because after we create
1015 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1016 // get the images
1017 imageNormal = GetItemImage(item);
1018 imageSel = GetItemSelectedImage(item);
1019
1020 // if it doesn't have it yet, add it
1021 wxTreeItemIndirectData *data = new
1022 wxTreeItemIndirectData(this, item);
1023
1024 // copy the data to the new location
1025 data->SetImage(imageNormal, wxTreeItemIcon_Normal);
1026 data->SetImage(imageSel, wxTreeItemIcon_Selected);
1027 }
1028
1029 DoSetItemImageFromData(item, image, which);
1030
1031 // reset the normal/selected images because we won't use them any
1032 // more - now they're stored inside the indirect data
1033 imageNormal =
1034 imageSel = I_IMAGECALLBACK;
1035 break;
1036 }
1037
9dfbf520
VZ
1038 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1039 // change both normal and selected image - otherwise the change simply
1040 // doesn't take place!
74b31181 1041 DoSetItemImages(item, imageNormal, imageSel);
2bda0e17
KB
1042}
1043
08b7c251 1044wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
2bda0e17 1045{
08b7c251 1046 wxTreeViewItem tvItem(item, TVIF_PARAM);
a9c1265f
VZ
1047
1048 // Hidden root may have data.
1049 if ( IS_VIRTUAL_ROOT(item) )
1050 {
1051 return GET_VIRTUAL_ROOT()->GetData();
1052 }
1053
1054 // Visible node.
08b7c251
VZ
1055 if ( !DoGetItem(&tvItem) )
1056 {
1057 return NULL;
1058 }
2bda0e17 1059
502a2b18
VZ
1060 wxTreeItemData *data = (wxTreeItemData *)tvItem.lParam;
1061 if ( IsDataIndirect(data) )
74b31181 1062 {
502a2b18 1063 data = ((wxTreeItemIndirectData *)data)->GetData();
74b31181 1064 }
502a2b18
VZ
1065
1066 return data;
2bda0e17
KB
1067}
1068
08b7c251 1069void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
2bda0e17 1070{
a9c1265f
VZ
1071 if ( IS_VIRTUAL_ROOT(item) )
1072 {
1073 GET_VIRTUAL_ROOT()->SetData(data);
1074 }
1075
188781db
VZ
1076 // first, associate this piece of data with this item
1077 if ( data )
1078 {
1079 data->SetId(item);
1080 }
1081
08b7c251 1082 wxTreeViewItem tvItem(item, TVIF_PARAM);
74b31181
VZ
1083
1084 if ( HasIndirectData(item) )
1085 {
1086 if ( DoGetItem(&tvItem) )
1087 {
1088 ((wxTreeItemIndirectData *)tvItem.lParam)->SetData(data);
1089 }
1090 else
1091 {
223d09f6 1092 wxFAIL_MSG( wxT("failed to change tree items data") );
74b31181
VZ
1093 }
1094 }
1095 else
1096 {
1097 tvItem.lParam = (LPARAM)data;
1098 DoSetItem(&tvItem);
1099 }
1100}
1101
1102void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId& item,
1103 wxTreeItemIndirectData *data)
1104{
1105 // this should never happen because it's unnecessary and will probably lead
1106 // to crash too because the code elsewhere supposes that the pointer the
1107 // wxTreeItemIndirectData has is a real wxItemData and not
1108 // wxTreeItemIndirectData as well
223d09f6 1109 wxASSERT_MSG( !HasIndirectData(item), wxT("setting indirect data twice?") );
74b31181 1110
502a2b18 1111 SetItemData(item, data);
74b31181
VZ
1112}
1113
1114bool wxTreeCtrl::HasIndirectData(const wxTreeItemId& item) const
1115{
502a2b18
VZ
1116 // query the item itself
1117 wxTreeViewItem tvItem(item, TVIF_PARAM);
1118 if ( !DoGetItem(&tvItem) )
1119 {
1120 return FALSE;
1121 }
1122
1123 wxTreeItemData *data = (wxTreeItemData *)tvItem.lParam;
1124
1125 return data && IsDataIndirect(data);
08b7c251 1126}
2bda0e17 1127
3a5a2f56
VZ
1128void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
1129{
a9c1265f
VZ
1130 if ( IS_VIRTUAL_ROOT(item) )
1131 return;
1132
3a5a2f56
VZ
1133 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1134 tvItem.cChildren = (int)has;
1135 DoSetItem(&tvItem);
1136}
1137
add28c55
VZ
1138void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
1139{
a9c1265f
VZ
1140 if ( IS_VIRTUAL_ROOT(item) )
1141 return;
1142
add28c55
VZ
1143 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1144 tvItem.state = bold ? TVIS_BOLD : 0;
1145 DoSetItem(&tvItem);
1146}
1147
58a8ab88
JS
1148void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
1149{
a9c1265f
VZ
1150 if ( IS_VIRTUAL_ROOT(item) )
1151 return;
1152
58a8ab88
JS
1153 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
1154 tvItem.state = highlight ? TVIS_DROPHILITED : 0;
1155 DoSetItem(&tvItem);
1156}
1157
d00407b2
VZ
1158void wxTreeCtrl::RefreshItem(const wxTreeItemId& item)
1159{
a9c1265f
VZ
1160 if ( IS_VIRTUAL_ROOT(item) )
1161 return;
1162
d00407b2
VZ
1163 wxRect rect;
1164 if ( GetBoundingRect(item, rect) )
1165 {
1166 RefreshRect(rect);
1167 }
1168}
1169
696e1ea0
VZ
1170void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
1171 const wxColour& col)
1172{
1173 m_hasAnyAttr = TRUE;
1174
1175 long id = (long)(WXHTREEITEM)item;
1176 wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id);
1177 if ( !attr )
1178 {
1179 attr = new wxTreeItemAttr;
1180 m_attrs.Put(id, (wxObject *)attr);
1181 }
1182
1183 attr->SetTextColour(col);
d00407b2
VZ
1184
1185 RefreshItem(item);
696e1ea0
VZ
1186}
1187
1188void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
1189 const wxColour& col)
1190{
1191 m_hasAnyAttr = TRUE;
1192
1193 long id = (long)(WXHTREEITEM)item;
1194 wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id);
1195 if ( !attr )
1196 {
1197 attr = new wxTreeItemAttr;
1198 m_attrs.Put(id, (wxObject *)attr);
1199 }
1200
1201 attr->SetBackgroundColour(col);
d00407b2
VZ
1202
1203 RefreshItem(item);
696e1ea0
VZ
1204}
1205
1206void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
1207{
1208 m_hasAnyAttr = TRUE;
1209
1210 long id = (long)(WXHTREEITEM)item;
1211 wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id);
1212 if ( !attr )
1213 {
1214 attr = new wxTreeItemAttr;
1215 m_attrs.Put(id, (wxObject *)attr);
1216 }
1217
1218 attr->SetFont(font);
d00407b2
VZ
1219
1220 RefreshItem(item);
696e1ea0
VZ
1221}
1222
08b7c251
VZ
1223// ----------------------------------------------------------------------------
1224// Item status
1225// ----------------------------------------------------------------------------
2bda0e17 1226
08b7c251
VZ
1227bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
1228{
add28c55 1229 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
08b7c251 1230 RECT rect;
955be36c
VZ
1231
1232 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1233 // the HTREEITEM with TVM_GETITEMRECT
1234 *(WXHTREEITEM *)&rect = (WXHTREEITEM)item;
1235
1236 // FALSE means get item rect for the whole item, not only text
1c74a900 1237 return SendMessage(GetHwnd(), TVM_GETITEMRECT, FALSE, (LPARAM)&rect) != 0;
2bda0e17
KB
1238}
1239
08b7c251 1240bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
2bda0e17 1241{
08b7c251
VZ
1242 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1243 DoGetItem(&tvItem);
2bda0e17 1244
08b7c251 1245 return tvItem.cChildren != 0;
2bda0e17
KB
1246}
1247
08b7c251 1248bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
2bda0e17 1249{
08b7c251
VZ
1250 // probably not a good idea to put it here
1251 //wxASSERT( ItemHasChildren(item) );
2bda0e17 1252
08b7c251
VZ
1253 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
1254 DoGetItem(&tvItem);
2bda0e17 1255
08b7c251 1256 return (tvItem.state & TVIS_EXPANDED) != 0;
2bda0e17
KB
1257}
1258
08b7c251 1259bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
2bda0e17 1260{
08b7c251
VZ
1261 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
1262 DoGetItem(&tvItem);
2bda0e17 1263
08b7c251 1264 return (tvItem.state & TVIS_SELECTED) != 0;
2bda0e17
KB
1265}
1266
add28c55
VZ
1267bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
1268{
1269 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1270 DoGetItem(&tvItem);
1271
1272 return (tvItem.state & TVIS_BOLD) != 0;
1273}
1274
08b7c251
VZ
1275// ----------------------------------------------------------------------------
1276// navigation
1277// ----------------------------------------------------------------------------
2bda0e17 1278
08b7c251
VZ
1279wxTreeItemId wxTreeCtrl::GetRootItem() const
1280{
a9c1265f
VZ
1281 // Root may be real (visible) or virtual (hidden).
1282 if ( GET_VIRTUAL_ROOT() )
1283 return TVI_ROOT;
1284
d220ae32 1285 return wxTreeItemId((WXHTREEITEM) TreeView_GetRoot(GetHwnd()));
08b7c251 1286}
2bda0e17 1287
08b7c251
VZ
1288wxTreeItemId wxTreeCtrl::GetSelection() const
1289{
ad90972f 1290 wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), (long)(WXHTREEITEM)0,
223d09f6 1291 wxT("this only works with single selection controls") );
9dfbf520 1292
d220ae32 1293 return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(GetHwnd()));
2bda0e17
KB
1294}
1295
08b7c251 1296wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
2bda0e17 1297{
c9b142c9
VZ
1298 HTREEITEM hItem;
1299
1300 if ( IS_VIRTUAL_ROOT(item) )
1301 {
1302 // no parent for the virtual root
1303 hItem = 0;
1304 }
1305 else // normal item
a9c1265f 1306 {
c9b142c9
VZ
1307 hItem = TreeView_GetParent(GetHwnd(), HITEM(item));
1308 if ( !hItem && HasFlag(wxTR_HIDE_ROOT) )
1309 {
1310 // the top level items should have the virtual root as their parent
1311 hItem = TVI_ROOT;
1312 }
a9c1265f
VZ
1313 }
1314
1315 return wxTreeItemId((WXHTREEITEM)hItem);
08b7c251 1316}
2bda0e17 1317
08b7c251 1318wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
06e38c8e 1319 long& _cookie) const
08b7c251
VZ
1320{
1321 // remember the last child returned in 'cookie'
3f7bc32b 1322 _cookie = (long)TreeView_GetChild(GetHwnd(), HITEM(item));
2bda0e17 1323
06e38c8e 1324 return wxTreeItemId((WXHTREEITEM)_cookie);
2bda0e17
KB
1325}
1326
08b7c251 1327wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
06e38c8e 1328 long& _cookie) const
2bda0e17 1329{
d220ae32 1330 wxTreeItemId l = wxTreeItemId((WXHTREEITEM)TreeView_GetNextSibling(GetHwnd(),
3f7bc32b 1331 HITEM(_cookie)));
23fd5130
VZ
1332 _cookie = (long)l;
1333
2e5dddb0 1334 return l;
08b7c251 1335}
2bda0e17 1336
978f38c2
VZ
1337wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
1338{
1339 // can this be done more efficiently?
1340 long cookie;
1341
1342 wxTreeItemId childLast,
2165ad93 1343 child = GetFirstChild(item, cookie);
978f38c2
VZ
1344 while ( child.IsOk() )
1345 {
1346 childLast = child;
2165ad93 1347 child = GetNextChild(item, cookie);
978f38c2
VZ
1348 }
1349
1350 return childLast;
1351}
1352
08b7c251
VZ
1353wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
1354{
3f7bc32b 1355 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(GetHwnd(), HITEM(item)));
2bda0e17
KB
1356}
1357
08b7c251 1358wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
2bda0e17 1359{
3f7bc32b 1360 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(GetHwnd(), HITEM(item)));
2bda0e17
KB
1361}
1362
08b7c251 1363wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
2bda0e17 1364{
d220ae32 1365 return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(GetHwnd()));
2bda0e17
KB
1366}
1367
08b7c251 1368wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
2bda0e17 1369{
f6bcfd97 1370 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() for must be visible itself!"));
02ce7b72 1371
3f7bc32b 1372 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(GetHwnd(), HITEM(item)));
08b7c251 1373}
02ce7b72 1374
08b7c251
VZ
1375wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1376{
f6bcfd97 1377 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() for must be visible itself!"));
02ce7b72 1378
3f7bc32b 1379 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(GetHwnd(), HITEM(item)));
08b7c251 1380}
02ce7b72 1381
9dfbf520
VZ
1382// ----------------------------------------------------------------------------
1383// multiple selections emulation
1384// ----------------------------------------------------------------------------
1385
1386bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const
1387{
1388 // receive the desired information.
1389 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1390 DoGetItem(&tvItem);
1391
1392 // state image indices are 1 based
1393 return ((tvItem.state >> 12) - 1) == 1;
1394}
1395
1396void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check)
1397{
1398 // receive the desired information.
1399 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1400
a9c1265f
VZ
1401 DoGetItem(&tvItem);
1402
9dfbf520
VZ
1403 // state images are one-based
1404 tvItem.state = (check ? 2 : 1) << 12;
1405
1406 DoSetItem(&tvItem);
1407}
1408
33961d59
RR
1409size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
1410{
1411 TraverseSelections selector(this, selections);
9dfbf520 1412
e47c4d48 1413 return selector.GetCount();
9dfbf520
VZ
1414}
1415
08b7c251
VZ
1416// ----------------------------------------------------------------------------
1417// Usual operations
1418// ----------------------------------------------------------------------------
02ce7b72 1419
08b7c251
VZ
1420wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
1421 wxTreeItemId hInsertAfter,
1422 const wxString& text,
1423 int image, int selectedImage,
1424 wxTreeItemData *data)
1425{
0e935169
VZ
1426 wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()),
1427 wxTreeItemId(),
1428 _T("can't have more than one root in the tree") );
1429
08b7c251 1430 TV_INSERTSTRUCT tvIns;
3f7bc32b
VZ
1431 tvIns.hParent = HITEM(parent);
1432 tvIns.hInsertAfter = HITEM(hInsertAfter);
58a8ab88 1433
74b31181
VZ
1434 // this is how we insert the item as the first child: supply a NULL
1435 // hInsertAfter
1436 if ( !tvIns.hInsertAfter )
58a8ab88
JS
1437 {
1438 tvIns.hInsertAfter = TVI_FIRST;
1439 }
1440
08b7c251
VZ
1441 UINT mask = 0;
1442 if ( !text.IsEmpty() )
1443 {
1444 mask |= TVIF_TEXT;
837e5743 1445 tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok
08b7c251 1446 }
f6bcfd97
BP
1447 else
1448 {
1449 tvIns.item.pszText = NULL;
1450 tvIns.item.cchTextMax = 0;
1451 }
02ce7b72 1452
08b7c251
VZ
1453 if ( image != -1 )
1454 {
1455 mask |= TVIF_IMAGE;
1456 tvIns.item.iImage = image;
3a5a2f56 1457
6b037754 1458 if ( selectedImage == -1 )
3a5a2f56
VZ
1459 {
1460 // take the same image for selected icon if not specified
1461 selectedImage = image;
1462 }
08b7c251 1463 }
02ce7b72 1464
08b7c251
VZ
1465 if ( selectedImage != -1 )
1466 {
1467 mask |= TVIF_SELECTEDIMAGE;
1468 tvIns.item.iSelectedImage = selectedImage;
1469 }
02ce7b72 1470
08b7c251
VZ
1471 if ( data != NULL )
1472 {
1473 mask |= TVIF_PARAM;
1474 tvIns.item.lParam = (LPARAM)data;
1475 }
02ce7b72 1476
08b7c251 1477 tvIns.item.mask = mask;
02ce7b72 1478
d220ae32 1479 HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns);
08b7c251
VZ
1480 if ( id == 0 )
1481 {
f6bcfd97 1482 wxLogLastError(wxT("TreeView_InsertItem"));
08b7c251 1483 }
02ce7b72 1484
fd3f686c
VZ
1485 if ( data != NULL )
1486 {
1487 // associate the application tree item with Win32 tree item handle
1488 data->SetId((WXHTREEITEM)id);
1489 }
1490
06e38c8e 1491 return wxTreeItemId((WXHTREEITEM)id);
2bda0e17
KB
1492}
1493
08b7c251
VZ
1494// for compatibility only
1495wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1496 const wxString& text,
1497 int image, int selImage,
1498 long insertAfter)
2bda0e17 1499{
06e38c8e 1500 return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text,
08b7c251 1501 image, selImage, NULL);
2bda0e17
KB
1502}
1503
08b7c251
VZ
1504wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
1505 int image, int selectedImage,
1506 wxTreeItemData *data)
2bda0e17 1507{
a9c1265f
VZ
1508
1509 if ( m_windowStyle & wxTR_HIDE_ROOT )
1510 {
1511 // create a virtual root item, the parent for all the others
1512 m_pVirtualRoot = new wxVirtualNode(data);
1513
1514 return TVI_ROOT;
1515 }
1516
22fb32d5 1517 return DoInsertItem(wxTreeItemId((long)(WXHTREEITEM) 0), (long)(WXHTREEITEM) 0,
08b7c251 1518 text, image, selectedImage, data);
2bda0e17
KB
1519}
1520
08b7c251
VZ
1521wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
1522 const wxString& text,
1523 int image, int selectedImage,
1524 wxTreeItemData *data)
2bda0e17 1525{
06e38c8e 1526 return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST,
08b7c251 1527 text, image, selectedImage, data);
2bda0e17
KB
1528}
1529
08b7c251
VZ
1530wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1531 const wxTreeItemId& idPrevious,
1532 const wxString& text,
1533 int image, int selectedImage,
1534 wxTreeItemData *data)
2bda0e17 1535{
08b7c251 1536 return DoInsertItem(parent, idPrevious, text, image, selectedImage, data);
2bda0e17
KB
1537}
1538
2ef31e80
VZ
1539wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1540 size_t index,
1541 const wxString& text,
1542 int image, int selectedImage,
1543 wxTreeItemData *data)
1544{
1545 // find the item from index
1546 long cookie;
1547 wxTreeItemId idPrev, idCur = GetFirstChild(parent, cookie);
1548 while ( index != 0 && idCur.IsOk() )
1549 {
1550 index--;
1551
1552 idPrev = idCur;
1553 idCur = GetNextChild(parent, cookie);
1554 }
1555
1556 // assert, not check: if the index is invalid, we will append the item
1557 // to the end
1558 wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") );
1559
1560 return DoInsertItem(parent, idPrev, text, image, selectedImage, data);
1561}
1562
08b7c251
VZ
1563wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
1564 const wxString& text,
1565 int image, int selectedImage,
1566 wxTreeItemData *data)
2bda0e17 1567{
06e38c8e 1568 return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST,
08b7c251 1569 text, image, selectedImage, data);
2bda0e17
KB
1570}
1571
08b7c251 1572void wxTreeCtrl::Delete(const wxTreeItemId& item)
2bda0e17 1573{
3f7bc32b 1574 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) )
bbcdf8bc 1575 {
f6bcfd97 1576 wxLogLastError(wxT("TreeView_DeleteItem"));
bbcdf8bc 1577 }
bbcdf8bc
JS
1578}
1579
23fd5130
VZ
1580// delete all children (but don't delete the item itself)
1581void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
1582{
1583 long cookie;
1584
1585 wxArrayLong children;
1586 wxTreeItemId child = GetFirstChild(item, cookie);
1587 while ( child.IsOk() )
1588 {
1589 children.Add((long)(WXHTREEITEM)child);
1590
1591 child = GetNextChild(item, cookie);
1592 }
1593
1594 size_t nCount = children.Count();
1595 for ( size_t n = 0; n < nCount; n++ )
1596 {
d220ae32 1597 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)children[n]) )
23fd5130 1598 {
f6bcfd97 1599 wxLogLastError(wxT("TreeView_DeleteItem"));
23fd5130
VZ
1600 }
1601 }
1602}
1603
08b7c251 1604void wxTreeCtrl::DeleteAllItems()
bbcdf8bc 1605{
a9c1265f
VZ
1606 // delete stored root item.
1607 delete GET_VIRTUAL_ROOT();
1608
d220ae32 1609 if ( !TreeView_DeleteAllItems(GetHwnd()) )
bbcdf8bc 1610 {
f6bcfd97 1611 wxLogLastError(wxT("TreeView_DeleteAllItems"));
bbcdf8bc 1612 }
2bda0e17
KB
1613}
1614
08b7c251 1615void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
2bda0e17 1616{
dd3646fd
VZ
1617 wxASSERT_MSG( flag == TVE_COLLAPSE ||
1618 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
1619 flag == TVE_EXPAND ||
1620 flag == TVE_TOGGLE,
223d09f6 1621 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
08b7c251 1622
a9c1265f
VZ
1623 // A hidden root can be neither expanded nor collapsed.
1624 if ( (HITEM(item) == TVI_ROOT) && (m_windowStyle & wxTR_HIDE_ROOT) )
1625 {
1626 // No action will be taken.
1627 return;
1628 }
1629
08b7c251 1630 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
d220ae32
VZ
1631 // emulate them. This behaviour has changed slightly with comctl32.dll
1632 // v 4.70 - now it does send them but only the first time. To maintain
1633 // compatible behaviour and also in order to not have surprises with the
1634 // future versions, don't rely on this and still do everything ourselves.
1635 // To avoid that the messages be sent twice when the item is expanded for
1636 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1637
1638 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE);
1639 tvItem.state = 0;
1640 DoSetItem(&tvItem);
1641
3f7bc32b 1642 if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) != 0 )
08b7c251
VZ
1643 {
1644 wxTreeEvent event(wxEVT_NULL, m_windowId);
1645 event.m_item = item;
08b7c251 1646 event.SetEventObject(this);
2bda0e17 1647
deb1de30
VZ
1648 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1649 // itself
1650 event.SetEventType(gs_expandEvents[IsExpanded(item) ? IDX_EXPAND
1651 : IDX_COLLAPSE]
1652 [IDX_DONE]);
2bda0e17 1653
deb1de30 1654 (void)GetEventHandler()->ProcessEvent(event);
08b7c251 1655 }
d220ae32 1656 //else: change didn't took place, so do nothing at all
2bda0e17
KB
1657}
1658
08b7c251 1659void wxTreeCtrl::Expand(const wxTreeItemId& item)
2bda0e17 1660{
08b7c251 1661 DoExpand(item, TVE_EXPAND);
2bda0e17 1662}
2bda0e17 1663
08b7c251 1664void wxTreeCtrl::Collapse(const wxTreeItemId& item)
2bda0e17 1665{
08b7c251 1666 DoExpand(item, TVE_COLLAPSE);
2bda0e17
KB
1667}
1668
08b7c251 1669void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
2bda0e17 1670{
dd3646fd 1671 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
2bda0e17
KB
1672}
1673
08b7c251 1674void wxTreeCtrl::Toggle(const wxTreeItemId& item)
2bda0e17 1675{
08b7c251 1676 DoExpand(item, TVE_TOGGLE);
2bda0e17
KB
1677}
1678
42c5812d
UU
1679void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
1680{
9dfbf520 1681 DoExpand(item, action);
42c5812d
UU
1682}
1683
08b7c251 1684void wxTreeCtrl::Unselect()
2bda0e17 1685{
3f7bc32b
VZ
1686 wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE),
1687 wxT("doesn't make sense, may be you want UnselectAll()?") );
9dfbf520
VZ
1688
1689 // just remove the selection
ad90972f 1690 SelectItem(wxTreeItemId((long) (WXHTREEITEM) 0));
08b7c251 1691}
02ce7b72 1692
9dfbf520 1693void wxTreeCtrl::UnselectAll()
08b7c251 1694{
9dfbf520 1695 if ( m_windowStyle & wxTR_MULTIPLE )
2bda0e17 1696 {
9dfbf520
VZ
1697 wxArrayTreeItemIds selections;
1698 size_t count = GetSelections(selections);
1699 for ( size_t n = 0; n < count; n++ )
d220ae32 1700 {
3f7bc32b 1701#if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
9dfbf520 1702 SetItemCheck(selections[n], FALSE);
3f7bc32b
VZ
1703#else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1704 ::UnselectItem(GetHwnd(), HITEM(selections[n]));
1705#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
d220ae32 1706 }
9dfbf520
VZ
1707 }
1708 else
1709 {
1710 // just remove the selection
1711 Unselect();
1712 }
1713}
1714
1715void wxTreeCtrl::SelectItem(const wxTreeItemId& item)
1716{
1717 if ( m_windowStyle & wxTR_MULTIPLE )
1718 {
3f7bc32b 1719#if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
9dfbf520
VZ
1720 // selecting the item means checking it
1721 SetItemCheck(item);
3f7bc32b
VZ
1722#else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1723 ::SelectItem(GetHwnd(), HITEM(item));
1724#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
9dfbf520
VZ
1725 }
1726 else
1727 {
1728 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1729 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1730 // send them ourselves
1731
1732 wxTreeEvent event(wxEVT_NULL, m_windowId);
1733 event.m_item = item;
1734 event.SetEventObject(this);
1735
1736 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING);
1737 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
d220ae32 1738 {
3f7bc32b 1739 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item)) )
9dfbf520 1740 {
f6bcfd97 1741 wxLogLastError(wxT("TreeView_SelectItem"));
9dfbf520
VZ
1742 }
1743 else
1744 {
1745 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1746 (void)GetEventHandler()->ProcessEvent(event);
1747 }
d220ae32 1748 }
9dfbf520 1749 //else: program vetoed the change
2bda0e17 1750 }
08b7c251 1751}
2bda0e17 1752
08b7c251
VZ
1753void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1754{
1755 // no error return
3f7bc32b 1756 TreeView_EnsureVisible(GetHwnd(), HITEM(item));
08b7c251
VZ
1757}
1758
1759void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
1760{
3f7bc32b 1761 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) )
2bda0e17 1762 {
f6bcfd97 1763 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
2bda0e17 1764 }
08b7c251
VZ
1765}
1766
1767wxTextCtrl* wxTreeCtrl::GetEditControl() const
1768{
b94ae1ea
VZ
1769 // normally, we could try to do something like this to return something
1770 // even when the editing was started by the user and not by calling
1771 // EditLabel() - but as nobody has asked for this so far and there might be
1772 // problems in the code below, I leave it disabled for now (VZ)
1773#if 0
1774 if ( !m_textCtrl )
1775 {
1776 HWND hwndText = TreeView_GetEditControl(GetHwnd());
1777 if ( hwndText )
1778 {
1779 m_textCtrl = new wxTextCtrl(this, -1);
1780 m_textCtrl->Hide();
1781 m_textCtrl->SetHWND((WXHWND)hwndText);
1782 }
1783 //else: not editing label right now
1784 }
1785#endif // 0
1786
08b7c251
VZ
1787 return m_textCtrl;
1788}
1789
1790void wxTreeCtrl::DeleteTextCtrl()
1791{
1792 if ( m_textCtrl )
2bda0e17 1793 {
b6a3d6ad
VZ
1794 // the HWND corresponding to this control is deleted by the tree
1795 // control itself and we don't know when exactly this happens, so check
1796 // if the window still exists before calling UnsubclassWin()
1797 if ( !::IsWindow(GetHwndOf(m_textCtrl)) )
1798 {
1799 m_textCtrl->SetHWND(0);
1800 }
1801
08b7c251
VZ
1802 m_textCtrl->UnsubclassWin();
1803 m_textCtrl->SetHWND(0);
1804 delete m_textCtrl;
1805 m_textCtrl = NULL;
2bda0e17 1806 }
08b7c251 1807}
2bda0e17 1808
08b7c251
VZ
1809wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
1810 wxClassInfo* textControlClass)
1811{
1812 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
1813
b94ae1ea
VZ
1814 DeleteTextCtrl();
1815
3f7bc32b 1816 HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item));
2bda0e17 1817
5ea47806
VZ
1818 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1819 // returned FALSE
1820 if ( !hWnd )
1821 {
1822 return NULL;
1823 }
2bda0e17 1824
08b7c251 1825 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
c8f14476 1826 m_textCtrl->SetParent(this);
08b7c251
VZ
1827 m_textCtrl->SetHWND((WXHWND)hWnd);
1828 m_textCtrl->SubclassWin((WXHWND)hWnd);
2bda0e17 1829
08b7c251 1830 return m_textCtrl;
2bda0e17
KB
1831}
1832
08b7c251 1833// End label editing, optionally cancelling the edit
33ac7e6f 1834void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool discardChanges)
2bda0e17 1835{
d220ae32 1836 TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
08b7c251
VZ
1837
1838 DeleteTextCtrl();
2bda0e17
KB
1839}
1840
08b7c251 1841wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
2bda0e17 1842{
08b7c251
VZ
1843 TV_HITTESTINFO hitTestInfo;
1844 hitTestInfo.pt.x = (int)point.x;
1845 hitTestInfo.pt.y = (int)point.y;
2bda0e17 1846
d220ae32 1847 TreeView_HitTest(GetHwnd(), &hitTestInfo);
2bda0e17 1848
08b7c251
VZ
1849 flags = 0;
1850
1851 // avoid repetition
1852 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1853 flags |= wxTREE_HITTEST_##flag
1854
1855 TRANSLATE_FLAG(ABOVE);
1856 TRANSLATE_FLAG(BELOW);
1857 TRANSLATE_FLAG(NOWHERE);
1858 TRANSLATE_FLAG(ONITEMBUTTON);
1859 TRANSLATE_FLAG(ONITEMICON);
1860 TRANSLATE_FLAG(ONITEMINDENT);
1861 TRANSLATE_FLAG(ONITEMLABEL);
1862 TRANSLATE_FLAG(ONITEMRIGHT);
1863 TRANSLATE_FLAG(ONITEMSTATEICON);
1864 TRANSLATE_FLAG(TOLEFT);
1865 TRANSLATE_FLAG(TORIGHT);
2bda0e17 1866
08b7c251
VZ
1867 #undef TRANSLATE_FLAG
1868
06e38c8e 1869 return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem);
08b7c251
VZ
1870}
1871
f7c832a7
VZ
1872bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
1873 wxRect& rect,
1874 bool textOnly) const
1875{
1876 RECT rc;
3f7bc32b 1877 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item),
f7c832a7
VZ
1878 &rc, textOnly) )
1879 {
1880 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
1881
1882 return TRUE;
1883 }
1884 else
1885 {
1886 // couldn't retrieve rect: for example, item isn't visible
1887 return FALSE;
1888 }
1889}
1890
23fd5130
VZ
1891// ----------------------------------------------------------------------------
1892// sorting stuff
1893// ----------------------------------------------------------------------------
f7c832a7 1894
502a2b18
VZ
1895// this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1896// functions such as IsDataIndirect()
1897class wxTreeSortHelper
1898{
1899public:
1900 static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree);
1901
1902private:
1903 static wxTreeItemId GetIdFromData(wxTreeCtrl *tree, LPARAM item)
1904 {
1905 wxTreeItemData *data = (wxTreeItemData *)item;
1906 if ( tree->IsDataIndirect(data) )
1907 {
1908 data = ((wxTreeItemIndirectData *)data)->GetData();
1909 }
1910
1911 return data->GetId();
1912 }
1913};
1914
1915int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1,
1916 LPARAM pItem2,
1917 LPARAM htree)
23fd5130 1918{
096c9f9b 1919 wxCHECK_MSG( pItem1 && pItem2, 0,
223d09f6 1920 wxT("sorting tree without data doesn't make sense") );
096c9f9b 1921
502a2b18
VZ
1922 wxTreeCtrl *tree = (wxTreeCtrl *)htree;
1923
1924 return tree->OnCompareItems(GetIdFromData(tree, pItem1),
1925 GetIdFromData(tree, pItem2));
23fd5130
VZ
1926}
1927
95aabccc
VZ
1928int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1929 const wxTreeItemId& item2)
08b7c251 1930{
837e5743 1931 return wxStrcmp(GetItemText(item1), GetItemText(item2));
95aabccc
VZ
1932}
1933
1934void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
1935{
1936 // rely on the fact that TreeView_SortChildren does the same thing as our
23fd5130
VZ
1937 // default behaviour, i.e. sorts items alphabetically and so call it
1938 // directly if we're not in derived class (much more efficient!)
1939 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
2bda0e17 1940 {
3f7bc32b 1941 TreeView_SortChildren(GetHwnd(), HITEM(item), 0);
2bda0e17 1942 }
08b7c251 1943 else
2bda0e17 1944 {
62448488 1945 TV_SORTCB tvSort;
3f7bc32b 1946 tvSort.hParent = HITEM(item);
502a2b18 1947 tvSort.lpfnCompare = wxTreeSortHelper::Compare;
23fd5130 1948 tvSort.lParam = (LPARAM)this;
d220ae32 1949 TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
2bda0e17 1950 }
08b7c251 1951}
2bda0e17 1952
08b7c251
VZ
1953// ----------------------------------------------------------------------------
1954// implementation
1955// ----------------------------------------------------------------------------
2bda0e17 1956
08b7c251
VZ
1957bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1958{
1959 if ( cmd == EN_UPDATE )
2bda0e17 1960 {
08b7c251
VZ
1961 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1962 event.SetEventObject( this );
1963 ProcessCommand(event);
2bda0e17 1964 }
08b7c251 1965 else if ( cmd == EN_KILLFOCUS )
2bda0e17 1966 {
08b7c251
VZ
1967 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1968 event.SetEventObject( this );
1969 ProcessCommand(event);
2bda0e17 1970 }
08b7c251 1971 else
2bda0e17 1972 {
08b7c251
VZ
1973 // nothing done
1974 return FALSE;
2bda0e17 1975 }
08b7c251
VZ
1976
1977 // command processed
1978 return TRUE;
1979}
1980
23f681ec
VZ
1981// we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1982// only do it during dragging, minimize wxWin overhead (this is important for
1983// WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1984// instead of passing by wxWin events
1985long wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
1986{
3f7bc32b
VZ
1987 bool processed = FALSE;
1988 long rc = 0;
3f7bc32b
VZ
1989 bool isMultiple = (GetWindowStyle() & wxTR_MULTIPLE) != 0;
1990
1991 if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
23f681ec 1992 {
3f7bc32b
VZ
1993 // we only process mouse messages here and these parameters have the same
1994 // meaning for all of them
1995 int x = GET_X_LPARAM(lParam),
1996 y = GET_Y_LPARAM(lParam);
1997 HTREEITEM htItem = GetItemFromPoint(GetHwnd(), x, y);
1998
23f681ec
VZ
1999 switch ( nMsg )
2000 {
3f7bc32b
VZ
2001#if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2002 case WM_LBUTTONDOWN:
2003 if ( htItem && isMultiple )
23f681ec 2004 {
3f7bc32b
VZ
2005 if ( wParam & MK_CONTROL )
2006 {
2007 SetFocus();
23f681ec 2008
3f7bc32b
VZ
2009 // toggle selected state
2010 ToggleItemSelection(GetHwnd(), htItem);
2011
2012 ::SetFocus(GetHwnd(), htItem);
2013
2014 // reset on any click without Shift
2015 m_htSelStart = 0;
2016
2017 processed = TRUE;
2018 }
2019 else if ( wParam & MK_SHIFT )
2020 {
2021 // this selects all items between the starting one and
2022 // the current
2023
2024 if ( !m_htSelStart )
2025 {
2026 // take the focused item
2027 m_htSelStart = (WXHTREEITEM)
2028 TreeView_GetSelection(GetHwnd());
2029 }
2030
2031 SelectRange(GetHwnd(), HITEM(m_htSelStart), htItem,
2032 !(wParam & MK_CONTROL));
2033
2034 ::SetFocus(GetHwnd(), htItem);
23f681ec 2035
3f7bc32b
VZ
2036 processed = TRUE;
2037 }
2038 else // normal click
2039 {
2040 // clear the selection and then let the default handler
2041 // do the job
2042 UnselectAll();
2043
2044 // prevent the click from starting in-place editing
2045 // when there was no selection in the control
2046 TreeView_SelectItem(GetHwnd(), 0);
2047
2048 // reset on any click without Shift
2049 m_htSelStart = 0;
2050 }
2051 }
2052 break;
2053#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2054
2055 case WM_MOUSEMOVE:
2056 if ( m_dragImage )
2057 {
afff720b 2058 m_dragImage->Move(wxPoint(x, y));
3f7bc32b 2059 if ( htItem )
23f681ec
VZ
2060 {
2061 // highlight the item as target (hiding drag image is
2062 // necessary - otherwise the display will be corrupted)
68be9f09 2063 m_dragImage->Hide();
3f7bc32b 2064 TreeView_SelectDropTarget(GetHwnd(), htItem);
68be9f09 2065 m_dragImage->Show();
23f681ec
VZ
2066 }
2067 }
2068 break;
2069
2070 case WM_LBUTTONUP:
2071 case WM_RBUTTONUP:
3f7bc32b 2072 if ( m_dragImage )
23f681ec 2073 {
68be9f09 2074 m_dragImage->EndDrag();
23f681ec
VZ
2075 delete m_dragImage;
2076 m_dragImage = NULL;
2077
2078 // generate the drag end event
2079 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, m_windowId);
2080
3f7bc32b 2081 event.m_item = (WXHTREEITEM)htItem;
23f681ec
VZ
2082 event.m_pointDrag = wxPoint(x, y);
2083 event.SetEventObject(this);
2084
2085 (void)GetEventHandler()->ProcessEvent(event);
225fe9d6
VZ
2086
2087 // if we don't do it, the tree seems to think that 2 items
2088 // are selected simultaneously which is quite weird
2089 TreeView_SelectDropTarget(GetHwnd(), 0);
23f681ec
VZ
2090 }
2091 break;
2092 }
2093 }
3f7bc32b
VZ
2094#if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2095 else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) && isMultiple )
2096 {
2097 // the tree control greys out the selected item when it loses focus and
2098 // paints it as selected again when it regains it, but it won't do it
2099 // for the other items itself - help it
2100 wxArrayTreeItemIds selections;
2101 size_t count = GetSelections(selections);
2102 RECT rect;
2103 for ( size_t n = 0; n < count; n++ )
2104 {
2105 // TreeView_GetItemRect() will return FALSE if item is not visible,
2106 // which may happen perfectly well
2107 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections[n]),
2108 &rect, TRUE) )
2109 {
2110 ::InvalidateRect(GetHwnd(), &rect, FALSE);
2111 }
2112 }
2113 }
2114 else if ( nMsg == WM_KEYDOWN && isMultiple )
2115 {
2116 bool bCtrl = wxIsCtrlDown(),
2117 bShift = wxIsShiftDown();
2118
2119 // we handle.arrows and space, but not page up/down and home/end: the
2120 // latter should be easy, but not the former
2121
2c8e4738 2122 HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
3f7bc32b
VZ
2123 if ( !m_htSelStart )
2124 {
2125 m_htSelStart = (WXHTREEITEM)htSel;
2126 }
2127
2128 if ( wParam == VK_SPACE )
2129 {
2130 if ( bCtrl )
2131 {
2132 ToggleItemSelection(GetHwnd(), htSel);
2133 }
2134 else
2135 {
2136 UnselectAll();
2137
2138 ::SelectItem(GetHwnd(), htSel);
2139 }
23f681ec 2140
3f7bc32b
VZ
2141 processed = TRUE;
2142 }
2143 else if ( wParam == VK_UP || wParam == VK_DOWN )
2144 {
2145 if ( !bCtrl && !bShift )
2146 {
2147 // no modifiers, just clear selection and then let the default
2148 // processing to take place
2149 UnselectAll();
2150 }
2151 else if ( htSel )
2152 {
2153 (void)wxControl::MSWWindowProc(nMsg, wParam, lParam);
2154
2c8e4738
VZ
2155 HTREEITEM htNext = (HTREEITEM)(wParam == VK_UP
2156 ? TreeView_GetPrevVisible(GetHwnd(), htSel)
2157 : TreeView_GetNextVisible(GetHwnd(), htSel));
3f7bc32b
VZ
2158
2159 if ( !htNext )
2160 {
2161 // at the top/bottom
2162 htNext = htSel;
2163 }
2164
2165 if ( bShift )
2166 {
2167 SelectRange(GetHwnd(), HITEM(m_htSelStart), htNext);
2168 }
2169 else // bCtrl
2170 {
2171 // without changing selection
2172 ::SetFocus(GetHwnd(), htNext);
2173 }
2174
2175 processed = TRUE;
2176 }
2177 }
2178 }
2179#endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
3f7bc32b
VZ
2180 if ( !processed )
2181 rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
2182
2183 return rc;
23f681ec
VZ
2184}
2185
08b7c251 2186// process WM_NOTIFY Windows message
a23fd0e1 2187bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
08b7c251
VZ
2188{
2189 wxTreeEvent event(wxEVT_NULL, m_windowId);
2190 wxEventType eventType = wxEVT_NULL;
2191 NMHDR *hdr = (NMHDR *)lParam;
2192
2193 switch ( hdr->code )
2bda0e17 2194 {
08b7c251
VZ
2195 case TVN_BEGINDRAG:
2196 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
2197 // fall through
2198
2199 case TVN_BEGINRDRAG:
2200 {
2201 if ( eventType == wxEVT_NULL )
2202 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
2203 //else: left drag, already set above
2204
2205 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2206
06e38c8e 2207 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
08b7c251 2208 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
23f681ec
VZ
2209
2210 // don't allow dragging by default: the user code must
2211 // explicitly say that it wants to allow it to avoid breaking
2212 // the old apps
2213 event.Veto();
08b7c251 2214 }
696e1ea0 2215 break;
08b7c251
VZ
2216
2217 case TVN_BEGINLABELEDIT:
2218 {
2219 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
2220 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2221
06e38c8e 2222 event.m_item = (WXHTREEITEM) info->item.hItem;
5ea47806 2223 event.m_label = info->item.pszText;
08b7c251 2224 }
696e1ea0 2225 break;
08b7c251
VZ
2226
2227 case TVN_DELETEITEM:
2228 {
2229 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
2230 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2231
696e1ea0
VZ
2232 event.m_item = (WXHTREEITEM)tv->itemOld.hItem;
2233
2234 if ( m_hasAnyAttr )
2235 {
2236 delete (wxTreeItemAttr *)m_attrs.
2237 Delete((long)tv->itemOld.hItem);
2238 }
08b7c251 2239 }
696e1ea0 2240 break;
08b7c251
VZ
2241
2242 case TVN_ENDLABELEDIT:
2243 {
2244 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
2245 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2246
5ea47806
VZ
2247 event.m_item = (WXHTREEITEM)info->item.hItem;
2248 event.m_label = info->item.pszText;
1ee4ead5
VZ
2249 if (info->item.pszText == NULL)
2250 return FALSE;
08b7c251
VZ
2251 break;
2252 }
2253
2254 case TVN_GETDISPINFO:
2255 eventType = wxEVT_COMMAND_TREE_GET_INFO;
2256 // fall through
2257
2258 case TVN_SETDISPINFO:
2259 {
2260 if ( eventType == wxEVT_NULL )
2261 eventType = wxEVT_COMMAND_TREE_SET_INFO;
2262 //else: get, already set above
2263
2264 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2265
06e38c8e 2266 event.m_item = (WXHTREEITEM) info->item.hItem;
08b7c251
VZ
2267 break;
2268 }
2269
2270 case TVN_ITEMEXPANDING:
08b7c251
VZ
2271 case TVN_ITEMEXPANDED:
2272 {
2273 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
2274
deb1de30 2275 int what;
08b7c251
VZ
2276 switch ( tv->action )
2277 {
deb1de30
VZ
2278 default:
2279 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action);
2280 // fall through
2281
08b7c251 2282 case TVE_EXPAND:
deb1de30 2283 what = IDX_EXPAND;
08b7c251
VZ
2284 break;
2285
2286 case TVE_COLLAPSE:
deb1de30 2287 what = IDX_COLLAPSE;
08b7c251 2288 break;
08b7c251
VZ
2289 }
2290
deb1de30
VZ
2291 int how = (int)hdr->code == TVN_ITEMEXPANDING ? IDX_DOING
2292 : IDX_DONE;
2293
2294 eventType = gs_expandEvents[what][how];
08b7c251 2295
06e38c8e 2296 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
08b7c251 2297 }
696e1ea0 2298 break;
08b7c251
VZ
2299
2300 case TVN_KEYDOWN:
2301 {
2302 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
2303 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
2304
b09bda68
VZ
2305 // we pass 0 as last CreateKeyEvent() parameter because we
2306 // don't have access to the real key press flags here - but as
2307 // it is only used to determin wxKeyEvent::m_altDown flag it's
2308 // not too bad
8191741f
VZ
2309 event.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN,
2310 wxCharCodeMSWToWX(info->wVKey),
2311 0);
23fd5130 2312
3f7bc32b
VZ
2313 // a separate event for Space/Return
2314 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2315 ((info->wVKey == VK_SPACE) || (info->wVKey == VK_RETURN)) )
23fd5130
VZ
2316 {
2317 wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
2318 m_windowId);
2319 event2.SetEventObject(this);
3f7bc32b
VZ
2320 if ( !(GetWindowStyle() & wxTR_MULTIPLE) )
2321 {
2322 event2.m_item = GetSelection();
2323 }
2324 //else: don't know how to get it
23fd5130 2325
3f7bc32b 2326 (void)GetEventHandler()->ProcessEvent(event2);
23fd5130 2327 }
08b7c251 2328 }
696e1ea0 2329 break;
08b7c251
VZ
2330
2331 case TVN_SELCHANGED:
2332 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
2333 // fall through
2334
2335 case TVN_SELCHANGING:
2336 {
2337 if ( eventType == wxEVT_NULL )
2338 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
2339 //else: already set above
2340
2341 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
2342
06e38c8e
JS
2343 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
2344 event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem;
08b7c251 2345 }
696e1ea0
VZ
2346 break;
2347
bf9b6266 2348#if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
696e1ea0
VZ
2349 case NM_CUSTOMDRAW:
2350 {
2351 LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam;
2352 NMCUSTOMDRAW& nmcd = lptvcd->nmcd;
d7926e0b 2353 switch ( nmcd.dwDrawStage )
696e1ea0
VZ
2354 {
2355 case CDDS_PREPAINT:
2356 // if we've got any items with non standard attributes,
2357 // notify us before painting each item
2358 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
2359 : CDRF_DODEFAULT;
d7926e0b 2360 break;
696e1ea0
VZ
2361
2362 case CDDS_ITEMPREPAINT:
2363 {
2364 wxTreeItemAttr *attr =
2365 (wxTreeItemAttr *)m_attrs.Get(nmcd.dwItemSpec);
2366
2367 if ( !attr )
2368 {
2369 // nothing to do for this item
d7926e0b
VZ
2370 *result = CDRF_DODEFAULT;
2371 break;
696e1ea0
VZ
2372 }
2373
2374 HFONT hFont;
2375 wxColour colText, colBack;
2376 if ( attr->HasFont() )
2377 {
2378 wxFont font = attr->GetFont();
2379 hFont = (HFONT)font.GetResourceHandle();
2380 }
2381 else
2382 {
2383 hFont = 0;
2384 }
2385
2386 if ( attr->HasTextColour() )
2387 {
2388 colText = attr->GetTextColour();
2389 }
2390 else
2391 {
2392 colText = GetForegroundColour();
2393 }
2394
2395 // selection colours should override ours
2396 if ( nmcd.uItemState & CDIS_SELECTED )
2397 {
2398 DWORD clrBk = ::GetSysColor(COLOR_HIGHLIGHT);
2399 lptvcd->clrTextBk = clrBk;
2400
2401 // try to make the text visible
2402 lptvcd->clrText = wxColourToRGB(colText);
2403 lptvcd->clrText |= ~clrBk;
2404 lptvcd->clrText &= 0x00ffffff;
2405 }
2406 else
2407 {
2408 if ( attr->HasBackgroundColour() )
2409 {
2410 colBack = attr->GetBackgroundColour();
2411 }
2412 else
2413 {
2414 colBack = GetBackgroundColour();
2415 }
2416
2417 lptvcd->clrText = wxColourToRGB(colText);
2418 lptvcd->clrTextBk = wxColourToRGB(colBack);
2419 }
2420
2421 // note that if we wanted to set colours for
2422 // individual columns (subitems), we would have
2423 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2424 if ( hFont )
2425 {
2426 ::SelectObject(nmcd.hdc, hFont);
2427
2428 *result = CDRF_NEWFONT;
2429 }
2430 else
2431 {
2432 *result = CDRF_DODEFAULT;
2433 }
696e1ea0 2434 }
d7926e0b 2435 break;
696e1ea0
VZ
2436
2437 default:
2438 *result = CDRF_DODEFAULT;
696e1ea0
VZ
2439 }
2440 }
d7926e0b
VZ
2441
2442 // we always process it
2443 return TRUE;
6ecfe2ac 2444#endif // _WIN32_IE >= 0x300
08b7c251 2445
f6bcfd97
BP
2446 case NM_DBLCLK:
2447 case NM_RCLICK:
2448 {
2449 TV_HITTESTINFO tvhti;
2450 ::GetCursorPos(&tvhti.pt);
2451 ::ScreenToClient(GetHwnd(), &tvhti.pt);
2452 if ( TreeView_HitTest(GetHwnd(), &tvhti) )
2453 {
2454 if ( tvhti.flags & TVHT_ONITEM )
2455 {
2456 event.m_item = (WXHTREEITEM) tvhti.hItem;
2457 eventType = (int)hdr->code == NM_DBLCLK
2458 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2459 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
8591268f
VZ
2460
2461 event.m_pointDrag.x = tvhti.pt.x;
2462 event.m_pointDrag.y = tvhti.pt.y;
f6bcfd97
BP
2463 }
2464
2465 break;
2466 }
2467 }
2468 // fall through
2469
08b7c251 2470 default:
a23fd0e1 2471 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2bda0e17 2472 }
08b7c251
VZ
2473
2474 event.SetEventObject(this);
2475 event.SetEventType(eventType);
2476
fd3f686c 2477 bool processed = GetEventHandler()->ProcessEvent(event);
08b7c251
VZ
2478
2479 // post processing
5ea47806 2480 switch ( hdr->code )
2bda0e17 2481 {
f6bcfd97
BP
2482 case NM_DBLCLK:
2483 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2484 // the return code of this event handler as the return value for
2485 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2486 // expanded status would never work
2487 *result = FALSE;
2488 break;
2489
23f681ec
VZ
2490 case TVN_BEGINDRAG:
2491 case TVN_BEGINRDRAG:
2492 if ( event.IsAllowed() )
2493 {
2494 // normally this is impossible because the m_dragImage is
2495 // deleted once the drag operation is over
2496 wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") );
2497
2498 m_dragImage = new wxDragImage(*this, event.m_item);
2499 m_dragImage->BeginDrag(wxPoint(0, 0), this);
68be9f09 2500 m_dragImage->Show();
23f681ec
VZ
2501 }
2502 break;
2503
5ea47806
VZ
2504 case TVN_DELETEITEM:
2505 {
2506 // NB: we might process this message using wxWindows event
2507 // tables, but due to overhead of wxWin event system we
2508 // prefer to do it here ourself (otherwise deleting a tree
2509 // with many items is just too slow)
2510 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
74b31181
VZ
2511
2512 wxTreeItemId item = event.m_item;
2513 if ( HasIndirectData(item) )
2514 {
2515 wxTreeItemIndirectData *data = (wxTreeItemIndirectData *)
2516 tv->itemOld.lParam;
2517 delete data; // can't be NULL here
74b31181
VZ
2518 }
2519 else
2520 {
2521 wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
2522 delete data; // may be NULL, ok
2523 }
08b7c251 2524
5ea47806
VZ
2525 processed = TRUE; // Make sure we don't get called twice
2526 }
2527 break;
2528
2529 case TVN_BEGINLABELEDIT:
2530 // return TRUE to cancel label editing
2531 *result = !event.IsAllowed();
2532 break;
2533
2534 case TVN_ENDLABELEDIT:
188781db
VZ
2535 // return TRUE to set the label to the new string: note that we
2536 // also must pretend that we did process the message or it is going
2537 // to be passed to DefWindowProc() which will happily return FALSE
2538 // cancelling the label change
5ea47806 2539 *result = event.IsAllowed();
188781db 2540 processed = TRUE;
5ea47806
VZ
2541
2542 // ensure that we don't have the text ctrl which is going to be
2543 // deleted any more
2544 DeleteTextCtrl();
2545 break;
2546
2547 case TVN_SELCHANGING:
2548 case TVN_ITEMEXPANDING:
2549 // return TRUE to prevent the action from happening
2550 *result = !event.IsAllowed();
2551 break;
2552
deb1de30
VZ
2553 case TVN_ITEMEXPANDED:
2554 // the item is not refreshed properly after expansion when it has
2555 // an image depending on the expanded/collapsed state - bug in
2556 // comctl32.dll or our code?
2557 {
2558 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
2559 if ( tv->action == TVE_EXPAND )
2560 {
2561 wxTreeItemId id = (WXHTREEITEM)tv->itemNew.hItem;
2562
2563 int image = GetItemImage(id, wxTreeItemIcon_Expanded);
2564 if ( image != -1 )
2565 {
2566 RefreshItem(id);
2567 }
2568 }
2569 }
2570 break;
2571
74b31181
VZ
2572 case TVN_GETDISPINFO:
2573 // NB: so far the user can't set the image himself anyhow, so do it
2574 // anyway - but this may change later
33ac7e6f 2575// if ( /* !processed && */ 1 )
74b31181
VZ
2576 {
2577 wxTreeItemId item = event.m_item;
2578 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2579 if ( info->item.mask & TVIF_IMAGE )
2580 {
2581 info->item.iImage =
2582 DoGetItemImageFromData
2583 (
2584 item,
2585 IsExpanded(item) ? wxTreeItemIcon_Expanded
2586 : wxTreeItemIcon_Normal
2587 );
2588 }
2589 if ( info->item.mask & TVIF_SELECTEDIMAGE )
2590 {
2591 info->item.iSelectedImage =
2592 DoGetItemImageFromData
2593 (
2594 item,
2595 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
2596 : wxTreeItemIcon_Selected
2597 );
2598 }
deb1de30 2599 }
74b31181
VZ
2600 break;
2601
5ea47806
VZ
2602 //default:
2603 // for the other messages the return value is ignored and there is
2604 // nothing special to do
2605 }
fd3f686c 2606 return processed;
2bda0e17
KB
2607}
2608
08b7c251 2609#endif // __WIN95__
2bda0e17 2610
1e6feb95 2611#endif // wxUSE_TREECTRL