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