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