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