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