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