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