]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/treectrl.cpp
wxDateTime progress: DST compuation, weekday computation, day-in-year and week
[wxWidgets.git] / src / msw / treectrl.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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#ifdef __GNUG__
20 #pragma implementation "treectrl.h"
21#endif
22
23// For compilers that support precompilation, includes "wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif
29
30#include "wx/window.h"
31#include "wx/msw/private.h"
32
33// Mingw32 is a bit mental even though this is done in winundef
34#ifdef GetFirstChild
35 #undef GetFirstChild
36#endif
37
38#ifdef GetNextSibling
39 #undef GetNextSibling
40#endif
41
42#if defined(__WIN95__)
43
44#include "wx/log.h"
45#include "wx/dynarray.h"
46#include "wx/imaglist.h"
47#include "wx/treectrl.h"
48#include "wx/settings.h"
49
50#ifdef __GNUWIN32__
51#ifndef wxUSE_NORLANDER_HEADERS
52#include "wx/msw/gnuwin32/extra.h"
53#endif
54#endif
55
56#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
57 #include <commctrl.h>
58#endif
59
60// Bug in headers, sometimes
61#ifndef TVIS_FOCUSED
62 #define TVIS_FOCUSED 0x0001
63#endif
64
65// ----------------------------------------------------------------------------
66// private classes
67// ----------------------------------------------------------------------------
68
69// a convenient wrapper around TV_ITEM struct which adds a ctor
70#ifdef __VISUALC__
71#pragma warning( disable : 4097 )
72#endif
73
74struct wxTreeViewItem : public TV_ITEM
75{
76 wxTreeViewItem(const wxTreeItemId& item, // the item handle
77 UINT mask_, // fields which are valid
78 UINT stateMask_ = 0) // for TVIF_STATE only
79 {
80 // hItem member is always valid
81 mask = mask_ | TVIF_HANDLE;
82 stateMask = stateMask_;
83 hItem = (HTREEITEM) (WXHTREEITEM) item;
84 }
85};
86
87#ifdef __VISUALC__
88#pragma warning( default : 4097 )
89#endif
90
91// a class which encapsulates the tree traversal logic: it vists all (unless
92// OnVisit() returns FALSE) items under the given one
93class wxTreeTraversal
94{
95public:
96 wxTreeTraversal(const wxTreeCtrl *tree)
97 {
98 m_tree = tree;
99 }
100
101 // do traverse the tree: visit all items (recursively by default) under the
102 // given one; return TRUE if all items were traversed or FALSE if the
103 // traversal was aborted because OnVisit returned FALSE
104 bool DoTraverse(const wxTreeItemId& root, bool recursively = TRUE);
105
106 // override this function to do whatever is needed for each item, return
107 // FALSE to stop traversing
108 virtual bool OnVisit(const wxTreeItemId& item) = 0;
109
110protected:
111 const wxTreeCtrl *GetTree() const { return m_tree; }
112
113private:
114 bool Traverse(const wxTreeItemId& root, bool recursively);
115
116 const wxTreeCtrl *m_tree;
117};
118
119// internal class for getting the selected items
120class TraverseSelections : public wxTreeTraversal
121{
122public:
123 TraverseSelections(const wxTreeCtrl *tree,
124 wxArrayTreeItemIds& selections)
125 : wxTreeTraversal(tree), m_selections(selections)
126 {
127 m_selections.Empty();
128
129 DoTraverse(tree->GetRootItem());
130 }
131
132 virtual bool OnVisit(const wxTreeItemId& item)
133 {
134 if ( GetTree()->IsItemChecked(item) )
135 {
136 m_selections.Add(item);
137 }
138
139 return TRUE;
140 }
141
142private:
143 wxArrayTreeItemIds& m_selections;
144};
145
146// internal class for counting tree items
147class TraverseCounter : public wxTreeTraversal
148{
149public:
150 TraverseCounter(const wxTreeCtrl *tree,
151 const wxTreeItemId& root,
152 bool recursively)
153 : wxTreeTraversal(tree)
154 {
155 m_count = 0;
156
157 DoTraverse(root, recursively);
158 }
159
160 virtual bool OnVisit(const wxTreeItemId& item)
161 {
162 m_count++;
163
164 return TRUE;
165 }
166
167 size_t GetCount() const { return m_count; }
168
169private:
170 size_t m_count;
171};
172
173// ----------------------------------------------------------------------------
174// This class is needed for support of different images: the Win32 common
175// control natively supports only 2 images (the normal one and another for the
176// selected state). We wish to provide support for 2 more of them for folder
177// items (i.e. those which have children): for expanded state and for expanded
178// selected state. For this we use this structure to store the additional items
179// images.
180//
181// There is only one problem with this: when we retrieve the item's data, we
182// don't know whether we get a pointer to wxTreeItemData or
183// wxTreeItemIndirectData. So we have to maintain a list of all items which
184// have indirect data inside the listctrl itself.
185// ----------------------------------------------------------------------------
186
187class wxTreeItemIndirectData
188{
189public:
190 // ctor associates this data with the item and the real item data becomes
191 // available through our GetData() method
192 wxTreeItemIndirectData(wxTreeCtrl *tree, const wxTreeItemId& item)
193 {
194 for ( size_t n = 0; n < WXSIZEOF(m_images); n++ )
195 {
196 m_images[n] = -1;
197 }
198
199 // save the old data
200 m_data = tree->GetItemData(item);
201
202 // and set ourselves as the new one
203 tree->SetIndirectItemData(item, this);
204 }
205
206 // dtor deletes the associated data as well
207 ~wxTreeItemIndirectData() { delete m_data; }
208
209 // accessors
210 // get the real data associated with the item
211 wxTreeItemData *GetData() const { return m_data; }
212 // change it
213 void SetData(wxTreeItemData *data) { m_data = data; }
214
215 // do we have such image?
216 bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; }
217 // get image
218 int GetImage(wxTreeItemIcon which) const { return m_images[which]; }
219 // change it
220 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
221
222private:
223 // all the images associated with the item
224 int m_images[wxTreeItemIcon_Max];
225
226 wxTreeItemData *m_data;
227};
228
229// ----------------------------------------------------------------------------
230// macros
231// ----------------------------------------------------------------------------
232
233 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl)
234
235// ----------------------------------------------------------------------------
236// variables
237// ----------------------------------------------------------------------------
238
239// handy table for sending events
240static const wxEventType g_events[2][2] =
241{
242 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
243 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
244};
245
246// ============================================================================
247// implementation
248// ============================================================================
249
250// ----------------------------------------------------------------------------
251// tree traversal
252// ----------------------------------------------------------------------------
253
254bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively)
255{
256 if ( !OnVisit(root) )
257 return FALSE;
258
259 return Traverse(root, recursively);
260}
261
262bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively)
263{
264 long cookie;
265 wxTreeItemId child = m_tree->GetFirstChild(root, cookie);
266 while ( child.IsOk() )
267 {
268 // depth first traversal
269 if ( recursively && !Traverse(child, TRUE) )
270 return FALSE;
271
272 if ( !OnVisit(child) )
273 return FALSE;
274
275 child = m_tree->GetNextChild(root, cookie);
276 }
277
278 return TRUE;
279}
280
281// ----------------------------------------------------------------------------
282// construction and destruction
283// ----------------------------------------------------------------------------
284
285void wxTreeCtrl::Init()
286{
287 m_imageListNormal = NULL;
288 m_imageListState = NULL;
289 m_textCtrl = NULL;
290 m_hasAnyAttr = FALSE;
291}
292
293bool wxTreeCtrl::Create(wxWindow *parent,
294 wxWindowID id,
295 const wxPoint& pos,
296 const wxSize& size,
297 long style,
298 const wxValidator& validator,
299 const wxString& name)
300{
301 Init();
302
303 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
304 return FALSE;
305
306 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP |
307 TVS_HASLINES | TVS_SHOWSELALWAYS;
308
309 if ( m_windowStyle & wxTR_HAS_BUTTONS )
310 wstyle |= TVS_HASBUTTONS;
311
312 if ( m_windowStyle & wxTR_EDIT_LABELS )
313 wstyle |= TVS_EDITLABELS;
314
315 if ( m_windowStyle & wxTR_LINES_AT_ROOT )
316 wstyle |= TVS_LINESATROOT;
317
318#if !defined( __GNUWIN32__ ) && !defined( __BORLANDC__ ) && !defined( __WATCOMC__ ) && !defined(wxUSE_NORLANDER_HEADERS)
319 // we emulate the multiple selection tree controls by using checkboxes: set
320 // up the image list we need for this if we do have multiple selections
321#if !defined(__VISUALC__) || (__VISUALC__ > 1010)
322 if ( m_windowStyle & wxTR_MULTIPLE )
323 wstyle |= TVS_CHECKBOXES;
324#endif
325#endif
326
327 // Create the tree control.
328 if ( !MSWCreateControl(WC_TREEVIEW, wstyle) )
329 return FALSE;
330
331 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
332 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
333
334 // VZ: this is some experimental code which may be used to get the
335 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
336 // AFAIK, the standard DLL does about the same thing anyhow.
337#if 0
338 if ( m_windowStyle & wxTR_MULTIPLE )
339 {
340 wxBitmap bmp;
341
342 // create the DC compatible with the current screen
343 HDC hdcMem = CreateCompatibleDC(NULL);
344
345 // create a mono bitmap of the standard size
346 int x = GetSystemMetrics(SM_CXMENUCHECK);
347 int y = GetSystemMetrics(SM_CYMENUCHECK);
348 wxImageList imagelistCheckboxes(x, y, FALSE, 2);
349 HBITMAP hbmpCheck = CreateBitmap(x, y, // bitmap size
350 1, // # of color planes
351 1, // # bits needed for one pixel
352 0); // array containing colour data
353 SelectObject(hdcMem, hbmpCheck);
354
355 // then draw a check mark into it
356 RECT rect = { 0, 0, x, y };
357 if ( !::DrawFrameControl(hdcMem, &rect,
358 DFC_BUTTON,
359 DFCS_BUTTONCHECK | DFCS_CHECKED) )
360 {
361 wxLogLastError(wxT("DrawFrameControl(check)"));
362 }
363
364 bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
365 imagelistCheckboxes.Add(bmp);
366
367 if ( !::DrawFrameControl(hdcMem, &rect,
368 DFC_BUTTON,
369 DFCS_BUTTONCHECK) )
370 {
371 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
372 }
373
374 bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
375 imagelistCheckboxes.Add(bmp);
376
377 // clean up
378 ::DeleteDC(hdcMem);
379
380 // set the imagelist
381 SetStateImageList(&imagelistCheckboxes);
382 }
383#endif // 0
384
385 SetSize(pos.x, pos.y, size.x, size.y);
386
387 return TRUE;
388}
389
390wxTreeCtrl::~wxTreeCtrl()
391{
392 // delete any attributes
393 if ( m_hasAnyAttr )
394 {
395 for ( wxNode *node = m_attrs.Next(); node; node = m_attrs.Next() )
396 {
397 delete (wxTreeItemAttr *)node->Data();
398 }
399
400 // prevent TVN_DELETEITEM handler from deleting the attributes again!
401 m_hasAnyAttr = FALSE;
402 }
403
404 DeleteTextCtrl();
405
406 // delete user data to prevent memory leaks
407 DeleteAllItems();
408}
409
410// ----------------------------------------------------------------------------
411// accessors
412// ----------------------------------------------------------------------------
413
414// simple wrappers which add error checking in debug mode
415
416bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const
417{
418 if ( !TreeView_GetItem(GetHwnd(), tvItem) )
419 {
420 wxLogLastError("TreeView_GetItem");
421
422 return FALSE;
423 }
424
425 return TRUE;
426}
427
428void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem)
429{
430 if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 )
431 {
432 wxLogLastError("TreeView_SetItem");
433 }
434}
435
436size_t wxTreeCtrl::GetCount() const
437{
438 return (size_t)TreeView_GetCount(GetHwnd());
439}
440
441unsigned int wxTreeCtrl::GetIndent() const
442{
443 return TreeView_GetIndent(GetHwnd());
444}
445
446void wxTreeCtrl::SetIndent(unsigned int indent)
447{
448 TreeView_SetIndent(GetHwnd(), indent);
449}
450
451wxImageList *wxTreeCtrl::GetImageList() const
452{
453 return m_imageListNormal;
454}
455
456wxImageList *wxTreeCtrl::GetStateImageList() const
457{
458 return m_imageListNormal;
459}
460
461void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
462{
463 // no error return
464 TreeView_SetImageList(GetHwnd(),
465 imageList ? imageList->GetHIMAGELIST() : 0,
466 which);
467}
468
469void wxTreeCtrl::SetImageList(wxImageList *imageList)
470{
471 SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL);
472}
473
474void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
475{
476 SetAnyImageList(m_imageListState = imageList, TVSIL_STATE);
477}
478
479size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
480 bool recursively) const
481{
482 TraverseCounter counter(this, item, recursively);
483
484 return counter.GetCount() - 1;
485}
486
487// ----------------------------------------------------------------------------
488// Item access
489// ----------------------------------------------------------------------------
490
491wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
492{
493 wxChar buf[512]; // the size is arbitrary...
494
495 wxTreeViewItem tvItem(item, TVIF_TEXT);
496 tvItem.pszText = buf;
497 tvItem.cchTextMax = WXSIZEOF(buf);
498 if ( !DoGetItem(&tvItem) )
499 {
500 // don't return some garbage which was on stack, but an empty string
501 buf[0] = wxT('\0');
502 }
503
504 return wxString(buf);
505}
506
507void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
508{
509 wxTreeViewItem tvItem(item, TVIF_TEXT);
510 tvItem.pszText = (wxChar *)text.c_str(); // conversion is ok
511 DoSetItem(&tvItem);
512}
513
514int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId& item,
515 wxTreeItemIcon which) const
516{
517 wxTreeViewItem tvItem(item, TVIF_PARAM);
518 if ( !DoGetItem(&tvItem) )
519 {
520 return -1;
521 }
522
523 return ((wxTreeItemIndirectData *)tvItem.lParam)->GetImage(which);
524}
525
526void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId& item,
527 int image,
528 wxTreeItemIcon which) const
529{
530 wxTreeViewItem tvItem(item, TVIF_PARAM);
531 if ( !DoGetItem(&tvItem) )
532 {
533 return;
534 }
535
536 wxTreeItemIndirectData *data = ((wxTreeItemIndirectData *)tvItem.lParam);
537
538 data->SetImage(image, which);
539
540 // make sure that we have selected images as well
541 if ( which == wxTreeItemIcon_Normal &&
542 !data->HasImage(wxTreeItemIcon_Selected) )
543 {
544 data->SetImage(image, wxTreeItemIcon_Selected);
545 }
546
547 if ( which == wxTreeItemIcon_Expanded &&
548 !data->HasImage(wxTreeItemIcon_SelectedExpanded) )
549 {
550 data->SetImage(image, wxTreeItemIcon_SelectedExpanded);
551 }
552}
553
554void wxTreeCtrl::DoSetItemImages(const wxTreeItemId& item,
555 int image,
556 int imageSel)
557{
558 wxTreeViewItem tvItem(item, TVIF_IMAGE | TVIF_SELECTEDIMAGE);
559 tvItem.iSelectedImage = imageSel;
560 tvItem.iImage = image;
561 DoSetItem(&tvItem);
562}
563
564int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
565 wxTreeItemIcon which) const
566{
567 if ( HasIndirectData(item) )
568 {
569 return DoGetItemImageFromData(item, which);
570 }
571
572 UINT mask;
573 switch ( which )
574 {
575 default:
576 wxFAIL_MSG( wxT("unknown tree item image type") );
577
578 case wxTreeItemIcon_Normal:
579 mask = TVIF_IMAGE;
580 break;
581
582 case wxTreeItemIcon_Selected:
583 mask = TVIF_SELECTEDIMAGE;
584 break;
585
586 case wxTreeItemIcon_Expanded:
587 case wxTreeItemIcon_SelectedExpanded:
588 return -1;
589 }
590
591 wxTreeViewItem tvItem(item, mask);
592 DoGetItem(&tvItem);
593
594 return mask == TVIF_IMAGE ? tvItem.iImage : tvItem.iSelectedImage;
595}
596
597void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
598 wxTreeItemIcon which)
599{
600 int imageNormal, imageSel;
601 switch ( which )
602 {
603 default:
604 wxFAIL_MSG( wxT("unknown tree item image type") );
605
606 case wxTreeItemIcon_Normal:
607 imageNormal = image;
608 imageSel = GetItemSelectedImage(item);
609 break;
610
611 case wxTreeItemIcon_Selected:
612 imageNormal = GetItemImage(item);
613 imageSel = image;
614 break;
615
616 case wxTreeItemIcon_Expanded:
617 case wxTreeItemIcon_SelectedExpanded:
618 if ( !HasIndirectData(item) )
619 {
620 // we need to get the old images first, because after we create
621 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
622 // get the images
623 imageNormal = GetItemImage(item);
624 imageSel = GetItemSelectedImage(item);
625
626 // if it doesn't have it yet, add it
627 wxTreeItemIndirectData *data = new
628 wxTreeItemIndirectData(this, item);
629
630 // copy the data to the new location
631 data->SetImage(imageNormal, wxTreeItemIcon_Normal);
632 data->SetImage(imageSel, wxTreeItemIcon_Selected);
633 }
634
635 DoSetItemImageFromData(item, image, which);
636
637 // reset the normal/selected images because we won't use them any
638 // more - now they're stored inside the indirect data
639 imageNormal =
640 imageSel = I_IMAGECALLBACK;
641 break;
642 }
643
644 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
645 // change both normal and selected image - otherwise the change simply
646 // doesn't take place!
647 DoSetItemImages(item, imageNormal, imageSel);
648}
649
650wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
651{
652 wxTreeViewItem tvItem(item, TVIF_PARAM);
653 if ( !DoGetItem(&tvItem) )
654 {
655 return NULL;
656 }
657
658 if ( HasIndirectData(item) )
659 {
660 return ((wxTreeItemIndirectData *)tvItem.lParam)->GetData();
661 }
662 else
663 {
664 return (wxTreeItemData *)tvItem.lParam;
665 }
666}
667
668void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
669{
670 wxTreeViewItem tvItem(item, TVIF_PARAM);
671
672 if ( HasIndirectData(item) )
673 {
674 if ( DoGetItem(&tvItem) )
675 {
676 ((wxTreeItemIndirectData *)tvItem.lParam)->SetData(data);
677 }
678 else
679 {
680 wxFAIL_MSG( wxT("failed to change tree items data") );
681 }
682 }
683 else
684 {
685 tvItem.lParam = (LPARAM)data;
686 DoSetItem(&tvItem);
687 }
688}
689
690void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId& item,
691 wxTreeItemIndirectData *data)
692{
693 // this should never happen because it's unnecessary and will probably lead
694 // to crash too because the code elsewhere supposes that the pointer the
695 // wxTreeItemIndirectData has is a real wxItemData and not
696 // wxTreeItemIndirectData as well
697 wxASSERT_MSG( !HasIndirectData(item), wxT("setting indirect data twice?") );
698
699 SetItemData(item, (wxTreeItemData *)data);
700
701 m_itemsWithIndirectData.Add(item);
702}
703
704bool wxTreeCtrl::HasIndirectData(const wxTreeItemId& item) const
705{
706 return m_itemsWithIndirectData.Index(item) != wxNOT_FOUND;
707}
708
709void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
710{
711 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
712 tvItem.cChildren = (int)has;
713 DoSetItem(&tvItem);
714}
715
716void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
717{
718 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
719 tvItem.state = bold ? TVIS_BOLD : 0;
720 DoSetItem(&tvItem);
721}
722
723void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
724{
725 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
726 tvItem.state = highlight ? TVIS_DROPHILITED : 0;
727 DoSetItem(&tvItem);
728}
729
730void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
731 const wxColour& col)
732{
733 m_hasAnyAttr = TRUE;
734
735 long id = (long)(WXHTREEITEM)item;
736 wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id);
737 if ( !attr )
738 {
739 attr = new wxTreeItemAttr;
740 m_attrs.Put(id, (wxObject *)attr);
741 }
742
743 attr->SetTextColour(col);
744}
745
746void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
747 const wxColour& col)
748{
749 m_hasAnyAttr = TRUE;
750
751 long id = (long)(WXHTREEITEM)item;
752 wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id);
753 if ( !attr )
754 {
755 attr = new wxTreeItemAttr;
756 m_attrs.Put(id, (wxObject *)attr);
757 }
758
759 attr->SetBackgroundColour(col);
760}
761
762void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
763{
764 m_hasAnyAttr = TRUE;
765
766 long id = (long)(WXHTREEITEM)item;
767 wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id);
768 if ( !attr )
769 {
770 attr = new wxTreeItemAttr;
771 m_attrs.Put(id, (wxObject *)attr);
772 }
773
774 attr->SetFont(font);
775}
776
777// ----------------------------------------------------------------------------
778// Item status
779// ----------------------------------------------------------------------------
780
781bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
782{
783 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
784 RECT rect;
785
786 // this ugliness comes directly from MSDN - it *is* the correct way to pass
787 // the HTREEITEM with TVM_GETITEMRECT
788 *(WXHTREEITEM *)&rect = (WXHTREEITEM)item;
789
790 // FALSE means get item rect for the whole item, not only text
791 return SendMessage(GetHwnd(), TVM_GETITEMRECT, FALSE, (LPARAM)&rect) != 0;
792
793}
794
795bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
796{
797 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
798 DoGetItem(&tvItem);
799
800 return tvItem.cChildren != 0;
801}
802
803bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
804{
805 // probably not a good idea to put it here
806 //wxASSERT( ItemHasChildren(item) );
807
808 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
809 DoGetItem(&tvItem);
810
811 return (tvItem.state & TVIS_EXPANDED) != 0;
812}
813
814bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
815{
816 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
817 DoGetItem(&tvItem);
818
819 return (tvItem.state & TVIS_SELECTED) != 0;
820}
821
822bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
823{
824 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
825 DoGetItem(&tvItem);
826
827 return (tvItem.state & TVIS_BOLD) != 0;
828}
829
830// ----------------------------------------------------------------------------
831// navigation
832// ----------------------------------------------------------------------------
833
834wxTreeItemId wxTreeCtrl::GetRootItem() const
835{
836 return wxTreeItemId((WXHTREEITEM) TreeView_GetRoot(GetHwnd()));
837}
838
839wxTreeItemId wxTreeCtrl::GetSelection() const
840{
841 wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), (WXHTREEITEM)0,
842 wxT("this only works with single selection controls") );
843
844 return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(GetHwnd()));
845}
846
847wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
848{
849 return wxTreeItemId((WXHTREEITEM) TreeView_GetParent(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
850}
851
852wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
853 long& _cookie) const
854{
855 // remember the last child returned in 'cookie'
856 _cookie = (long)TreeView_GetChild(GetHwnd(), (HTREEITEM) (WXHTREEITEM)item);
857
858 return wxTreeItemId((WXHTREEITEM)_cookie);
859}
860
861wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
862 long& _cookie) const
863{
864 wxTreeItemId l = wxTreeItemId((WXHTREEITEM)TreeView_GetNextSibling(GetHwnd(),
865 (HTREEITEM)(WXHTREEITEM)_cookie));
866 _cookie = (long)l;
867
868 return l;
869}
870
871wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
872{
873 // can this be done more efficiently?
874 long cookie;
875
876 wxTreeItemId childLast,
877 child = GetFirstChild(item, cookie);
878 while ( child.IsOk() )
879 {
880 childLast = child;
881 child = GetNextChild(item, cookie);
882 }
883
884 return childLast;
885}
886
887wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
888{
889 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
890}
891
892wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
893{
894 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
895}
896
897wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
898{
899 return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(GetHwnd()));
900}
901
902wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
903{
904 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() "
905 "for must be visible itself!"));
906
907 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
908}
909
910wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
911{
912 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() "
913 "for must be visible itself!"));
914
915 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
916}
917
918// ----------------------------------------------------------------------------
919// multiple selections emulation
920// ----------------------------------------------------------------------------
921
922bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const
923{
924 // receive the desired information.
925 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
926 DoGetItem(&tvItem);
927
928 // state image indices are 1 based
929 return ((tvItem.state >> 12) - 1) == 1;
930}
931
932void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check)
933{
934 // receive the desired information.
935 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
936
937 // state images are one-based
938 tvItem.state = (check ? 2 : 1) << 12;
939
940 DoSetItem(&tvItem);
941}
942
943size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
944{
945 TraverseSelections selector(this, selections);
946
947 return selections.GetCount();
948}
949
950// ----------------------------------------------------------------------------
951// Usual operations
952// ----------------------------------------------------------------------------
953
954wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
955 wxTreeItemId hInsertAfter,
956 const wxString& text,
957 int image, int selectedImage,
958 wxTreeItemData *data)
959{
960 TV_INSERTSTRUCT tvIns;
961 tvIns.hParent = (HTREEITEM) (WXHTREEITEM)parent;
962 tvIns.hInsertAfter = (HTREEITEM) (WXHTREEITEM) hInsertAfter;
963
964 // this is how we insert the item as the first child: supply a NULL
965 // hInsertAfter
966 if ( !tvIns.hInsertAfter )
967 {
968 tvIns.hInsertAfter = TVI_FIRST;
969 }
970
971 UINT mask = 0;
972 if ( !text.IsEmpty() )
973 {
974 mask |= TVIF_TEXT;
975 tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok
976 }
977
978 if ( image != -1 )
979 {
980 mask |= TVIF_IMAGE;
981 tvIns.item.iImage = image;
982
983 if ( selectedImage == -1 )
984 {
985 // take the same image for selected icon if not specified
986 selectedImage = image;
987 }
988 }
989
990 if ( selectedImage != -1 )
991 {
992 mask |= TVIF_SELECTEDIMAGE;
993 tvIns.item.iSelectedImage = selectedImage;
994 }
995
996 if ( data != NULL )
997 {
998 mask |= TVIF_PARAM;
999 tvIns.item.lParam = (LPARAM)data;
1000 }
1001
1002 tvIns.item.mask = mask;
1003
1004 HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns);
1005 if ( id == 0 )
1006 {
1007 wxLogLastError("TreeView_InsertItem");
1008 }
1009
1010 if ( data != NULL )
1011 {
1012 // associate the application tree item with Win32 tree item handle
1013 data->SetId((WXHTREEITEM)id);
1014 }
1015
1016 return wxTreeItemId((WXHTREEITEM)id);
1017}
1018
1019// for compatibility only
1020wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1021 const wxString& text,
1022 int image, int selImage,
1023 long insertAfter)
1024{
1025 return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text,
1026 image, selImage, NULL);
1027}
1028
1029wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
1030 int image, int selectedImage,
1031 wxTreeItemData *data)
1032{
1033 return DoInsertItem(wxTreeItemId((WXHTREEITEM) 0), (WXHTREEITEM) 0,
1034 text, image, selectedImage, data);
1035}
1036
1037wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
1038 const wxString& text,
1039 int image, int selectedImage,
1040 wxTreeItemData *data)
1041{
1042 return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST,
1043 text, image, selectedImage, data);
1044}
1045
1046wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1047 const wxTreeItemId& idPrevious,
1048 const wxString& text,
1049 int image, int selectedImage,
1050 wxTreeItemData *data)
1051{
1052 return DoInsertItem(parent, idPrevious, text, image, selectedImage, data);
1053}
1054
1055wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
1056 const wxString& text,
1057 int image, int selectedImage,
1058 wxTreeItemData *data)
1059{
1060 return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST,
1061 text, image, selectedImage, data);
1062}
1063
1064void wxTreeCtrl::Delete(const wxTreeItemId& item)
1065{
1066 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item) )
1067 {
1068 wxLogLastError("TreeView_DeleteItem");
1069 }
1070}
1071
1072// delete all children (but don't delete the item itself)
1073void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
1074{
1075 long cookie;
1076
1077 wxArrayLong children;
1078 wxTreeItemId child = GetFirstChild(item, cookie);
1079 while ( child.IsOk() )
1080 {
1081 children.Add((long)(WXHTREEITEM)child);
1082
1083 child = GetNextChild(item, cookie);
1084 }
1085
1086 size_t nCount = children.Count();
1087 for ( size_t n = 0; n < nCount; n++ )
1088 {
1089 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)children[n]) )
1090 {
1091 wxLogLastError("TreeView_DeleteItem");
1092 }
1093 }
1094}
1095
1096void wxTreeCtrl::DeleteAllItems()
1097{
1098 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1099 {
1100 wxLogLastError("TreeView_DeleteAllItems");
1101 }
1102}
1103
1104void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
1105{
1106 wxASSERT_MSG( flag == TVE_COLLAPSE ||
1107 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
1108 flag == TVE_EXPAND ||
1109 flag == TVE_TOGGLE,
1110 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1111
1112 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1113 // emulate them. This behaviour has changed slightly with comctl32.dll
1114 // v 4.70 - now it does send them but only the first time. To maintain
1115 // compatible behaviour and also in order to not have surprises with the
1116 // future versions, don't rely on this and still do everything ourselves.
1117 // To avoid that the messages be sent twice when the item is expanded for
1118 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1119
1120 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE);
1121 tvItem.state = 0;
1122 DoSetItem(&tvItem);
1123
1124 if ( TreeView_Expand(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item, flag) != 0 )
1125 {
1126 wxTreeEvent event(wxEVT_NULL, m_windowId);
1127 event.m_item = item;
1128
1129 bool isExpanded = IsExpanded(item);
1130
1131 event.SetEventObject(this);
1132
1133 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1134 event.SetEventType(g_events[isExpanded][TRUE]);
1135 GetEventHandler()->ProcessEvent(event);
1136
1137 event.SetEventType(g_events[isExpanded][FALSE]);
1138 GetEventHandler()->ProcessEvent(event);
1139 }
1140 //else: change didn't took place, so do nothing at all
1141}
1142
1143void wxTreeCtrl::Expand(const wxTreeItemId& item)
1144{
1145 DoExpand(item, TVE_EXPAND);
1146}
1147
1148void wxTreeCtrl::Collapse(const wxTreeItemId& item)
1149{
1150 DoExpand(item, TVE_COLLAPSE);
1151}
1152
1153void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1154{
1155 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
1156}
1157
1158void wxTreeCtrl::Toggle(const wxTreeItemId& item)
1159{
1160 DoExpand(item, TVE_TOGGLE);
1161}
1162
1163void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
1164{
1165 DoExpand(item, action);
1166}
1167
1168void wxTreeCtrl::Unselect()
1169{
1170 wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE), wxT("doesn't make sense") );
1171
1172 // just remove the selection
1173 SelectItem(wxTreeItemId((WXHTREEITEM) 0));
1174}
1175
1176void wxTreeCtrl::UnselectAll()
1177{
1178 if ( m_windowStyle & wxTR_MULTIPLE )
1179 {
1180 wxArrayTreeItemIds selections;
1181 size_t count = GetSelections(selections);
1182 for ( size_t n = 0; n < count; n++ )
1183 {
1184 SetItemCheck(selections[n], FALSE);
1185 }
1186 }
1187 else
1188 {
1189 // just remove the selection
1190 Unselect();
1191 }
1192}
1193
1194void wxTreeCtrl::SelectItem(const wxTreeItemId& item)
1195{
1196 if ( m_windowStyle & wxTR_MULTIPLE )
1197 {
1198 // selecting the item means checking it
1199 SetItemCheck(item);
1200 }
1201 else
1202 {
1203 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1204 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1205 // send them ourselves
1206
1207 wxTreeEvent event(wxEVT_NULL, m_windowId);
1208 event.m_item = item;
1209 event.SetEventObject(this);
1210
1211 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING);
1212 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
1213 {
1214 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item) )
1215 {
1216 wxLogLastError("TreeView_SelectItem");
1217 }
1218 else
1219 {
1220 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1221 (void)GetEventHandler()->ProcessEvent(event);
1222 }
1223 }
1224 //else: program vetoed the change
1225 }
1226}
1227
1228void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1229{
1230 // no error return
1231 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item);
1232}
1233
1234void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
1235{
1236 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item) )
1237 {
1238 wxLogLastError("TreeView_SelectSetFirstVisible");
1239 }
1240}
1241
1242wxTextCtrl* wxTreeCtrl::GetEditControl() const
1243{
1244 return m_textCtrl;
1245}
1246
1247void wxTreeCtrl::DeleteTextCtrl()
1248{
1249 if ( m_textCtrl )
1250 {
1251 m_textCtrl->UnsubclassWin();
1252 m_textCtrl->SetHWND(0);
1253 delete m_textCtrl;
1254 m_textCtrl = NULL;
1255 }
1256}
1257
1258wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
1259 wxClassInfo* textControlClass)
1260{
1261 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
1262
1263 HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item);
1264
1265 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1266 // returned FALSE
1267 if ( !hWnd )
1268 {
1269 return NULL;
1270 }
1271
1272 DeleteTextCtrl();
1273
1274 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
1275 m_textCtrl->SetHWND((WXHWND)hWnd);
1276 m_textCtrl->SubclassWin((WXHWND)hWnd);
1277
1278 return m_textCtrl;
1279}
1280
1281// End label editing, optionally cancelling the edit
1282void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges)
1283{
1284 TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
1285
1286 DeleteTextCtrl();
1287}
1288
1289wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
1290{
1291 TV_HITTESTINFO hitTestInfo;
1292 hitTestInfo.pt.x = (int)point.x;
1293 hitTestInfo.pt.y = (int)point.y;
1294
1295 TreeView_HitTest(GetHwnd(), &hitTestInfo);
1296
1297 flags = 0;
1298
1299 // avoid repetition
1300 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1301 flags |= wxTREE_HITTEST_##flag
1302
1303 TRANSLATE_FLAG(ABOVE);
1304 TRANSLATE_FLAG(BELOW);
1305 TRANSLATE_FLAG(NOWHERE);
1306 TRANSLATE_FLAG(ONITEMBUTTON);
1307 TRANSLATE_FLAG(ONITEMICON);
1308 TRANSLATE_FLAG(ONITEMINDENT);
1309 TRANSLATE_FLAG(ONITEMLABEL);
1310 TRANSLATE_FLAG(ONITEMRIGHT);
1311 TRANSLATE_FLAG(ONITEMSTATEICON);
1312 TRANSLATE_FLAG(TOLEFT);
1313 TRANSLATE_FLAG(TORIGHT);
1314
1315 #undef TRANSLATE_FLAG
1316
1317 return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem);
1318}
1319
1320bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
1321 wxRect& rect,
1322 bool textOnly) const
1323{
1324 RECT rc;
1325 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item,
1326 &rc, textOnly) )
1327 {
1328 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
1329
1330 return TRUE;
1331 }
1332 else
1333 {
1334 // couldn't retrieve rect: for example, item isn't visible
1335 return FALSE;
1336 }
1337}
1338
1339// ----------------------------------------------------------------------------
1340// sorting stuff
1341// ----------------------------------------------------------------------------
1342
1343static int CALLBACK TreeView_CompareCallback(wxTreeItemData *pItem1,
1344 wxTreeItemData *pItem2,
1345 wxTreeCtrl *tree)
1346{
1347 wxCHECK_MSG( pItem1 && pItem2, 0,
1348 wxT("sorting tree without data doesn't make sense") );
1349
1350 return tree->OnCompareItems(pItem1->GetId(), pItem2->GetId());
1351}
1352
1353int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1354 const wxTreeItemId& item2)
1355{
1356 return wxStrcmp(GetItemText(item1), GetItemText(item2));
1357}
1358
1359void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
1360{
1361 // rely on the fact that TreeView_SortChildren does the same thing as our
1362 // default behaviour, i.e. sorts items alphabetically and so call it
1363 // directly if we're not in derived class (much more efficient!)
1364 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
1365 {
1366 TreeView_SortChildren(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item, 0);
1367 }
1368 else
1369 {
1370 TV_SORTCB tvSort;
1371 tvSort.hParent = (HTREEITEM)(WXHTREEITEM)item;
1372 tvSort.lpfnCompare = (PFNTVCOMPARE)TreeView_CompareCallback;
1373 tvSort.lParam = (LPARAM)this;
1374 TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
1375 }
1376}
1377
1378// ----------------------------------------------------------------------------
1379// implementation
1380// ----------------------------------------------------------------------------
1381
1382bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1383{
1384 if ( cmd == EN_UPDATE )
1385 {
1386 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1387 event.SetEventObject( this );
1388 ProcessCommand(event);
1389 }
1390 else if ( cmd == EN_KILLFOCUS )
1391 {
1392 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1393 event.SetEventObject( this );
1394 ProcessCommand(event);
1395 }
1396 else
1397 {
1398 // nothing done
1399 return FALSE;
1400 }
1401
1402 // command processed
1403 return TRUE;
1404}
1405
1406// process WM_NOTIFY Windows message
1407bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1408{
1409 wxTreeEvent event(wxEVT_NULL, m_windowId);
1410 wxEventType eventType = wxEVT_NULL;
1411 NMHDR *hdr = (NMHDR *)lParam;
1412
1413 switch ( hdr->code )
1414 {
1415 case NM_RCLICK:
1416 {
1417 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
1418 return TRUE;
1419
1420 TV_HITTESTINFO tvhti;
1421 ::GetCursorPos(&(tvhti.pt));
1422 ::ScreenToClient(GetHwnd(),&(tvhti.pt));
1423 if ( TreeView_HitTest(GetHwnd(),&tvhti) )
1424 {
1425 if( tvhti.flags & TVHT_ONITEM )
1426 {
1427 event.m_item = (WXHTREEITEM) tvhti.hItem;
1428 eventType = wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
1429 }
1430 }
1431 }
1432 break;
1433
1434 case TVN_BEGINDRAG:
1435 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
1436 // fall through
1437
1438 case TVN_BEGINRDRAG:
1439 {
1440 if ( eventType == wxEVT_NULL )
1441 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
1442 //else: left drag, already set above
1443
1444 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
1445
1446 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
1447 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
1448 }
1449 break;
1450
1451 case TVN_BEGINLABELEDIT:
1452 {
1453 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
1454 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1455
1456 event.m_item = (WXHTREEITEM) info->item.hItem;
1457 event.m_label = info->item.pszText;
1458 }
1459 break;
1460
1461 case TVN_DELETEITEM:
1462 {
1463 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
1464 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
1465
1466 event.m_item = (WXHTREEITEM)tv->itemOld.hItem;
1467
1468 if ( m_hasAnyAttr )
1469 {
1470 delete (wxTreeItemAttr *)m_attrs.
1471 Delete((long)tv->itemOld.hItem);
1472 }
1473 }
1474 break;
1475
1476 case TVN_ENDLABELEDIT:
1477 {
1478 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
1479 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1480
1481 event.m_item = (WXHTREEITEM)info->item.hItem;
1482 event.m_label = info->item.pszText;
1483 if (info->item.pszText == NULL)
1484 return FALSE;
1485 break;
1486 }
1487
1488 case TVN_GETDISPINFO:
1489 eventType = wxEVT_COMMAND_TREE_GET_INFO;
1490 // fall through
1491
1492 case TVN_SETDISPINFO:
1493 {
1494 if ( eventType == wxEVT_NULL )
1495 eventType = wxEVT_COMMAND_TREE_SET_INFO;
1496 //else: get, already set above
1497
1498 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1499
1500 event.m_item = (WXHTREEITEM) info->item.hItem;
1501 break;
1502 }
1503
1504 case TVN_ITEMEXPANDING:
1505 event.m_code = FALSE;
1506 // fall through
1507
1508 case TVN_ITEMEXPANDED:
1509 {
1510 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
1511
1512 bool expand = FALSE;
1513 switch ( tv->action )
1514 {
1515 case TVE_EXPAND:
1516 expand = TRUE;
1517 break;
1518
1519 case TVE_COLLAPSE:
1520 expand = FALSE;
1521 break;
1522
1523 default:
1524 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
1525 "message"), tv->action);
1526 }
1527
1528 bool ing = ((int)hdr->code == TVN_ITEMEXPANDING);
1529 eventType = g_events[expand][ing];
1530
1531 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
1532 }
1533 break;
1534
1535 case TVN_KEYDOWN:
1536 {
1537 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
1538 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
1539
1540 event.m_code = wxCharCodeMSWToWX(info->wVKey);
1541
1542 // a separate event for this case
1543 if ( info->wVKey == VK_SPACE || info->wVKey == VK_RETURN )
1544 {
1545 wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
1546 m_windowId);
1547 event2.SetEventObject(this);
1548
1549 GetEventHandler()->ProcessEvent(event2);
1550 }
1551 }
1552 break;
1553
1554 case TVN_SELCHANGED:
1555 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
1556 // fall through
1557
1558 case TVN_SELCHANGING:
1559 {
1560 if ( eventType == wxEVT_NULL )
1561 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
1562 //else: already set above
1563
1564 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
1565
1566 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
1567 event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem;
1568 }
1569 break;
1570
1571#if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1572 case NM_CUSTOMDRAW:
1573 {
1574 LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam;
1575 NMCUSTOMDRAW& nmcd = lptvcd->nmcd;
1576 switch( nmcd.dwDrawStage )
1577 {
1578 case CDDS_PREPAINT:
1579 // if we've got any items with non standard attributes,
1580 // notify us before painting each item
1581 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
1582 : CDRF_DODEFAULT;
1583 return TRUE;
1584
1585 case CDDS_ITEMPREPAINT:
1586 {
1587 wxTreeItemAttr *attr =
1588 (wxTreeItemAttr *)m_attrs.Get(nmcd.dwItemSpec);
1589
1590 if ( !attr )
1591 {
1592 // nothing to do for this item
1593 return CDRF_DODEFAULT;
1594 }
1595
1596 HFONT hFont;
1597 wxColour colText, colBack;
1598 if ( attr->HasFont() )
1599 {
1600 wxFont font = attr->GetFont();
1601 hFont = (HFONT)font.GetResourceHandle();
1602 }
1603 else
1604 {
1605 hFont = 0;
1606 }
1607
1608 if ( attr->HasTextColour() )
1609 {
1610 colText = attr->GetTextColour();
1611 }
1612 else
1613 {
1614 colText = GetForegroundColour();
1615 }
1616
1617 // selection colours should override ours
1618 if ( nmcd.uItemState & CDIS_SELECTED )
1619 {
1620 DWORD clrBk = ::GetSysColor(COLOR_HIGHLIGHT);
1621 lptvcd->clrTextBk = clrBk;
1622
1623 // try to make the text visible
1624 lptvcd->clrText = wxColourToRGB(colText);
1625 lptvcd->clrText |= ~clrBk;
1626 lptvcd->clrText &= 0x00ffffff;
1627 }
1628 else
1629 {
1630 if ( attr->HasBackgroundColour() )
1631 {
1632 colBack = attr->GetBackgroundColour();
1633 }
1634 else
1635 {
1636 colBack = GetBackgroundColour();
1637 }
1638
1639 lptvcd->clrText = wxColourToRGB(colText);
1640 lptvcd->clrTextBk = wxColourToRGB(colBack);
1641 }
1642
1643 // note that if we wanted to set colours for
1644 // individual columns (subitems), we would have
1645 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1646 if ( hFont )
1647 {
1648 ::SelectObject(nmcd.hdc, hFont);
1649
1650 *result = CDRF_NEWFONT;
1651 }
1652 else
1653 {
1654 *result = CDRF_DODEFAULT;
1655 }
1656
1657 return TRUE;
1658 }
1659
1660 default:
1661 *result = CDRF_DODEFAULT;
1662 return TRUE;
1663 }
1664 }
1665 break;
1666#endif // _WIN32_IE >= 0x300
1667
1668 default:
1669 return wxControl::MSWOnNotify(idCtrl, lParam, result);
1670 }
1671
1672 event.SetEventObject(this);
1673 event.SetEventType(eventType);
1674
1675 bool processed = GetEventHandler()->ProcessEvent(event);
1676
1677 // post processing
1678 switch ( hdr->code )
1679 {
1680 case TVN_DELETEITEM:
1681 {
1682 // NB: we might process this message using wxWindows event
1683 // tables, but due to overhead of wxWin event system we
1684 // prefer to do it here ourself (otherwise deleting a tree
1685 // with many items is just too slow)
1686 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
1687
1688 wxTreeItemId item = event.m_item;
1689 if ( HasIndirectData(item) )
1690 {
1691 wxTreeItemIndirectData *data = (wxTreeItemIndirectData *)
1692 tv->itemOld.lParam;
1693 delete data; // can't be NULL here
1694
1695 m_itemsWithIndirectData.Remove(item);
1696 }
1697 else
1698 {
1699 wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
1700 delete data; // may be NULL, ok
1701 }
1702
1703 processed = TRUE; // Make sure we don't get called twice
1704 }
1705 break;
1706
1707 case TVN_BEGINLABELEDIT:
1708 // return TRUE to cancel label editing
1709 *result = !event.IsAllowed();
1710 break;
1711
1712 case TVN_ENDLABELEDIT:
1713 // return TRUE to set the label to the new string
1714 *result = event.IsAllowed();
1715
1716 // ensure that we don't have the text ctrl which is going to be
1717 // deleted any more
1718 DeleteTextCtrl();
1719 break;
1720
1721 case TVN_SELCHANGING:
1722 case TVN_ITEMEXPANDING:
1723 // return TRUE to prevent the action from happening
1724 *result = !event.IsAllowed();
1725 break;
1726
1727 case TVN_GETDISPINFO:
1728 // NB: so far the user can't set the image himself anyhow, so do it
1729 // anyway - but this may change later
1730 if ( /* !processed && */ 1 )
1731 {
1732 wxTreeItemId item = event.m_item;
1733 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1734 if ( info->item.mask & TVIF_IMAGE )
1735 {
1736 info->item.iImage =
1737 DoGetItemImageFromData
1738 (
1739 item,
1740 IsExpanded(item) ? wxTreeItemIcon_Expanded
1741 : wxTreeItemIcon_Normal
1742 );
1743 }
1744 if ( info->item.mask & TVIF_SELECTEDIMAGE )
1745 {
1746 info->item.iSelectedImage =
1747 DoGetItemImageFromData
1748 (
1749 item,
1750 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
1751 : wxTreeItemIcon_Selected
1752 );
1753 }
1754 }
1755 break;
1756
1757 //default:
1758 // for the other messages the return value is ignored and there is
1759 // nothing special to do
1760 }
1761
1762 return processed;
1763}
1764
1765// ----------------------------------------------------------------------------
1766// Tree event
1767// ----------------------------------------------------------------------------
1768
1769IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
1770
1771wxTreeEvent::wxTreeEvent(wxEventType commandType, int id)
1772 : wxNotifyEvent(commandType, id)
1773{
1774 m_code = 0;
1775 m_itemOld = 0;
1776}
1777
1778#endif // __WIN95__
1779