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