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