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