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