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