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