]> git.saurik.com Git - wxWidgets.git/blob - src/msw/treectrl.cpp
Added wxWindowBase::CentreOnParent to allow top level windows to be
[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 #ifndef WX_PRECOMP
34 #include "wx/settings.h"
35 #endif
36
37 // Mingw32 is a bit mental even though this is done in winundef
38 #ifdef GetFirstChild
39 #undef GetFirstChild
40 #endif
41 #ifdef GetNextSibling
42 #undef GetNextSibling
43 #endif
44
45 #if defined(__WIN95__)
46
47 #include "wx/log.h"
48 #include "wx/dynarray.h"
49 #include "wx/imaglist.h"
50 #include "wx/msw/treectrl.h"
51
52 #ifdef __GNUWIN32__
53 #include "wx/msw/gnuwin32/extra.h"
54 #endif
55
56 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
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 struct wxTreeViewItem : public TV_ITEM
71 {
72 wxTreeViewItem(const wxTreeItemId& item,
73 UINT mask_, UINT stateMask_ = 0)
74 {
75 mask = mask_;
76 stateMask = stateMask_;
77 hItem = (HTREEITEM) (WXHTREEITEM) item;
78 }
79 };
80
81 // ----------------------------------------------------------------------------
82 // macros
83 // ----------------------------------------------------------------------------
84
85 #if !USE_SHARED_LIBRARY
86 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl)
87 #endif
88
89 // hide the ugly cast (of course, the macro is _quite_ ugly too...)
90 #define wxhWnd ((HWND)m_hWnd)
91
92 // ----------------------------------------------------------------------------
93 // variables
94 // ----------------------------------------------------------------------------
95
96 // handy table for sending events
97 static const wxEventType g_events[2][2] =
98 {
99 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
100 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
101 };
102
103 // ============================================================================
104 // implementation
105 // ============================================================================
106
107 // ----------------------------------------------------------------------------
108 // construction and destruction
109 // ----------------------------------------------------------------------------
110
111 void wxTreeCtrl::Init()
112 {
113 m_imageListNormal = NULL;
114 m_imageListState = NULL;
115 m_textCtrl = NULL;
116 }
117
118 bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
119 const wxPoint& pos, const wxSize& size,
120 long style, const wxValidator& validator,
121 const wxString& name)
122 {
123 Init();
124
125 wxSystemSettings settings;
126
127 SetName(name);
128 SetValidator(validator);
129
130 m_windowStyle = style;
131
132 SetParent(parent);
133
134 m_windowId = (id == -1) ? NewControlId() : id;
135
136 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | TVS_HASLINES | TVS_SHOWSELALWAYS ;
137
138
139 bool want3D;
140 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
141
142 // Even with extended styles, need to combine with WS_BORDER
143 // for them to look right.
144 if ( want3D || wxStyleHasBorder(m_windowStyle) )
145 {
146 wstyle |= WS_BORDER;
147 }
148
149 if ( m_windowStyle & wxTR_HAS_BUTTONS )
150 wstyle |= TVS_HASBUTTONS;
151
152 if ( m_windowStyle & wxTR_EDIT_LABELS )
153 wstyle |= TVS_EDITLABELS;
154
155 if ( m_windowStyle & wxTR_LINES_AT_ROOT )
156 wstyle |= TVS_LINESATROOT;
157
158 // Create the tree control.
159 m_hWnd = (WXHWND)::CreateWindowEx
160 (
161 exStyle,
162 WC_TREEVIEW,
163 "",
164 wstyle,
165 pos.x, pos.y, size.x, size.y,
166 (HWND)parent->GetHWND(),
167 (HMENU)m_windowId,
168 wxGetInstance(),
169 NULL
170 );
171
172 wxCHECK_MSG( m_hWnd, FALSE, "Failed to create tree ctrl" );
173
174 if ( parent )
175 parent->AddChild(this);
176
177 SubclassWin(m_hWnd);
178
179 return TRUE;
180 }
181
182 wxTreeCtrl::~wxTreeCtrl()
183 {
184 DeleteTextCtrl();
185
186 // delete user data to prevent memory leaks
187 DeleteAllItems();
188 }
189
190 // ----------------------------------------------------------------------------
191 // accessors
192 // ----------------------------------------------------------------------------
193
194 // simple wrappers which add error checking in debug mode
195
196 bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const
197 {
198 if ( !TreeView_GetItem(wxhWnd, tvItem) )
199 {
200 wxLogLastError("TreeView_GetItem");
201
202 return FALSE;
203 }
204
205 return TRUE;
206 }
207
208 void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem)
209 {
210 if ( TreeView_SetItem(wxhWnd, tvItem) == -1 )
211 {
212 wxLogLastError("TreeView_SetItem");
213 }
214 }
215
216 size_t wxTreeCtrl::GetCount() const
217 {
218 return (size_t)TreeView_GetCount(wxhWnd);
219 }
220
221 unsigned int wxTreeCtrl::GetIndent() const
222 {
223 return TreeView_GetIndent(wxhWnd);
224 }
225
226 void wxTreeCtrl::SetIndent(unsigned int indent)
227 {
228 TreeView_SetIndent(wxhWnd, indent);
229 }
230
231 wxImageList *wxTreeCtrl::GetImageList() const
232 {
233 return m_imageListNormal;
234 }
235
236 wxImageList *wxTreeCtrl::GetStateImageList() const
237 {
238 return m_imageListNormal;
239 }
240
241 void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
242 {
243 // no error return
244 TreeView_SetImageList(wxhWnd,
245 imageList ? imageList->GetHIMAGELIST() : 0,
246 which);
247 }
248
249 void wxTreeCtrl::SetImageList(wxImageList *imageList)
250 {
251 SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL);
252 }
253
254 void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
255 {
256 SetAnyImageList(m_imageListState = imageList, TVSIL_STATE);
257 }
258
259 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
260 {
261 long cookie;
262
263 size_t result = 0;
264
265 wxArrayLong children;
266 wxTreeItemId child = GetFirstChild(item, cookie);
267 while ( child.IsOk() )
268 {
269 if ( recursively )
270 {
271 // recursive call
272 result += GetChildrenCount(child, TRUE);
273 }
274
275 // add the child to the result in any case
276 result++;
277
278 child = GetNextChild(item, cookie);
279 }
280
281 return result;
282 }
283
284 // ----------------------------------------------------------------------------
285 // Item access
286 // ----------------------------------------------------------------------------
287
288 wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
289 {
290 char buf[512]; // the size is arbitrary...
291
292 wxTreeViewItem tvItem(item, TVIF_TEXT);
293 tvItem.pszText = buf;
294 tvItem.cchTextMax = WXSIZEOF(buf);
295 if ( !DoGetItem(&tvItem) )
296 {
297 // don't return some garbage which was on stack, but an empty string
298 buf[0] = '\0';
299 }
300
301 return wxString(buf);
302 }
303
304 void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
305 {
306 wxTreeViewItem tvItem(item, TVIF_TEXT);
307 tvItem.pszText = (char *)text.c_str(); // conversion is ok
308 DoSetItem(&tvItem);
309 }
310
311 int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const
312 {
313 wxTreeViewItem tvItem(item, TVIF_IMAGE);
314 DoGetItem(&tvItem);
315
316 return tvItem.iImage;
317 }
318
319 void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image)
320 {
321 wxTreeViewItem tvItem(item, TVIF_IMAGE);
322 tvItem.iImage = image;
323 DoSetItem(&tvItem);
324 }
325
326 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const
327 {
328 wxTreeViewItem tvItem(item, TVIF_SELECTEDIMAGE);
329 DoGetItem(&tvItem);
330
331 return tvItem.iSelectedImage;
332 }
333
334 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image)
335 {
336 wxTreeViewItem tvItem(item, TVIF_SELECTEDIMAGE);
337 tvItem.iSelectedImage = image;
338 DoSetItem(&tvItem);
339 }
340
341 wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
342 {
343 wxTreeViewItem tvItem(item, TVIF_PARAM);
344 if ( !DoGetItem(&tvItem) )
345 {
346 return NULL;
347 }
348
349 return (wxTreeItemData *)tvItem.lParam;
350 }
351
352 void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
353 {
354 wxTreeViewItem tvItem(item, TVIF_PARAM);
355 tvItem.lParam = (LPARAM)data;
356 DoSetItem(&tvItem);
357 }
358
359 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
360 {
361 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
362 tvItem.cChildren = (int)has;
363 DoSetItem(&tvItem);
364 }
365
366 void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
367 {
368 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
369 tvItem.state = bold ? TVIS_BOLD : 0;
370 DoSetItem(&tvItem);
371 }
372
373 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
374 {
375 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
376 tvItem.state = highlight ? TVIS_DROPHILITED : 0;
377 DoSetItem(&tvItem);
378 }
379
380 // ----------------------------------------------------------------------------
381 // Item status
382 // ----------------------------------------------------------------------------
383
384 bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
385 {
386 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
387 RECT rect;
388 return SendMessage(wxhWnd, TVM_GETITEMRECT, FALSE, (LPARAM)&rect) != 0;
389
390 }
391
392 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
393 {
394 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
395 DoGetItem(&tvItem);
396
397 return tvItem.cChildren != 0;
398 }
399
400 bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
401 {
402 // probably not a good idea to put it here
403 //wxASSERT( ItemHasChildren(item) );
404
405 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
406 DoGetItem(&tvItem);
407
408 return (tvItem.state & TVIS_EXPANDED) != 0;
409 }
410
411 bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
412 {
413 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
414 DoGetItem(&tvItem);
415
416 return (tvItem.state & TVIS_SELECTED) != 0;
417 }
418
419 bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
420 {
421 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
422 DoGetItem(&tvItem);
423
424 return (tvItem.state & TVIS_BOLD) != 0;
425 }
426
427 // ----------------------------------------------------------------------------
428 // navigation
429 // ----------------------------------------------------------------------------
430
431 wxTreeItemId wxTreeCtrl::GetRootItem() const
432 {
433 return wxTreeItemId((WXHTREEITEM) TreeView_GetRoot(wxhWnd));
434 }
435
436 wxTreeItemId wxTreeCtrl::GetSelection() const
437 {
438 return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(wxhWnd));
439 }
440
441 wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
442 {
443 return wxTreeItemId((WXHTREEITEM) TreeView_GetParent(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));
444 }
445
446 wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
447 long& _cookie) const
448 {
449 // remember the last child returned in 'cookie'
450 _cookie = (long)TreeView_GetChild(wxhWnd, (HTREEITEM) (WXHTREEITEM)item);
451
452 return wxTreeItemId((WXHTREEITEM)_cookie);
453 }
454
455 wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
456 long& _cookie) const
457 {
458 wxTreeItemId l = wxTreeItemId((WXHTREEITEM)TreeView_GetNextSibling(wxhWnd,
459 (HTREEITEM)(WXHTREEITEM)_cookie));
460 _cookie = (long)l;
461
462 return l;
463 }
464
465 wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
466 {
467 // can this be done more efficiently?
468 long cookie;
469
470 wxTreeItemId childLast,
471 child = GetFirstChild(item, cookie);
472 while ( child.IsOk() )
473 {
474 childLast = child;
475 child = GetNextChild(item, cookie);
476 }
477
478 return childLast;
479 }
480
481 wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
482 {
483 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));
484 }
485
486 wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
487 {
488 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));
489 }
490
491 wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
492 {
493 return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(wxhWnd));
494 }
495
496 wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
497 {
498 wxASSERT_MSG( IsVisible(item), "The item you call GetNextVisible() "
499 "for must be visible itself!");
500
501 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));
502 }
503
504 wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
505 {
506 wxASSERT_MSG( IsVisible(item), "The item you call GetPrevVisible() "
507 "for must be visible itself!");
508
509 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));
510 }
511
512 // ----------------------------------------------------------------------------
513 // Usual operations
514 // ----------------------------------------------------------------------------
515
516 wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
517 wxTreeItemId hInsertAfter,
518 const wxString& text,
519 int image, int selectedImage,
520 wxTreeItemData *data)
521 {
522 TV_INSERTSTRUCT tvIns;
523 tvIns.hParent = (HTREEITEM) (WXHTREEITEM)parent;
524 tvIns.hInsertAfter = (HTREEITEM) (WXHTREEITEM) hInsertAfter;
525
526 // This is how we insert the item as the first child: supply a NULL hInsertAfter
527 if (tvIns.hInsertAfter == (HTREEITEM) 0)
528 {
529 tvIns.hInsertAfter = TVI_FIRST;
530 }
531
532 UINT mask = 0;
533 if ( !text.IsEmpty() )
534 {
535 mask |= TVIF_TEXT;
536 tvIns.item.pszText = (char *)text.c_str(); // cast is ok
537 }
538
539 if ( image != -1 )
540 {
541 mask |= TVIF_IMAGE;
542 tvIns.item.iImage = image;
543
544 if ( selectedImage == -1 )
545 {
546 // take the same image for selected icon if not specified
547 selectedImage = image;
548 }
549 }
550
551 if ( selectedImage != -1 )
552 {
553 mask |= TVIF_SELECTEDIMAGE;
554 tvIns.item.iSelectedImage = selectedImage;
555 }
556
557 if ( data != NULL )
558 {
559 mask |= TVIF_PARAM;
560 tvIns.item.lParam = (LPARAM)data;
561 }
562
563 tvIns.item.mask = mask;
564
565 HTREEITEM id = (HTREEITEM) TreeView_InsertItem(wxhWnd, &tvIns);
566 if ( id == 0 )
567 {
568 wxLogLastError("TreeView_InsertItem");
569 }
570
571 if ( data != NULL )
572 {
573 // associate the application tree item with Win32 tree item handle
574 data->SetId((WXHTREEITEM)id);
575 }
576
577 return wxTreeItemId((WXHTREEITEM)id);
578 }
579
580 // for compatibility only
581 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
582 const wxString& text,
583 int image, int selImage,
584 long insertAfter)
585 {
586 return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text,
587 image, selImage, NULL);
588 }
589
590 wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
591 int image, int selectedImage,
592 wxTreeItemData *data)
593 {
594 return DoInsertItem(wxTreeItemId((WXHTREEITEM) 0), (WXHTREEITEM) 0,
595 text, image, selectedImage, data);
596 }
597
598 wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
599 const wxString& text,
600 int image, int selectedImage,
601 wxTreeItemData *data)
602 {
603 return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST,
604 text, image, selectedImage, data);
605 }
606
607 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
608 const wxTreeItemId& idPrevious,
609 const wxString& text,
610 int image, int selectedImage,
611 wxTreeItemData *data)
612 {
613 return DoInsertItem(parent, idPrevious, text, image, selectedImage, data);
614 }
615
616 wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
617 const wxString& text,
618 int image, int selectedImage,
619 wxTreeItemData *data)
620 {
621 return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST,
622 text, image, selectedImage, data);
623 }
624
625 void wxTreeCtrl::Delete(const wxTreeItemId& item)
626 {
627 if ( !TreeView_DeleteItem(wxhWnd, (HTREEITEM)(WXHTREEITEM)item) )
628 {
629 wxLogLastError("TreeView_DeleteItem");
630 }
631 }
632
633 // delete all children (but don't delete the item itself)
634 void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
635 {
636 long cookie;
637
638 wxArrayLong children;
639 wxTreeItemId child = GetFirstChild(item, cookie);
640 while ( child.IsOk() )
641 {
642 children.Add((long)(WXHTREEITEM)child);
643
644 child = GetNextChild(item, cookie);
645 }
646
647 size_t nCount = children.Count();
648 for ( size_t n = 0; n < nCount; n++ )
649 {
650 if ( !TreeView_DeleteItem(wxhWnd, (HTREEITEM)children[n]) )
651 {
652 wxLogLastError("TreeView_DeleteItem");
653 }
654 }
655 }
656
657 void wxTreeCtrl::DeleteAllItems()
658 {
659 if ( !TreeView_DeleteAllItems(wxhWnd) )
660 {
661 wxLogLastError("TreeView_DeleteAllItems");
662 }
663 }
664
665 void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
666 {
667 wxASSERT_MSG( flag == TVE_COLLAPSE ||
668 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
669 flag == TVE_EXPAND ||
670 flag == TVE_TOGGLE,
671 "Unknown flag in wxTreeCtrl::DoExpand" );
672
673 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
674 // emulate them
675 if ( TreeView_Expand(wxhWnd, (HTREEITEM) (WXHTREEITEM) item, flag) != 0 )
676 {
677 wxTreeEvent event(wxEVT_NULL, m_windowId);
678 event.m_item = item;
679
680 bool isExpanded = IsExpanded(item);
681
682 event.SetEventObject(this);
683
684 // @@@ return values of {EXPAND|COLLAPS}ING event handler is discarded
685 event.SetEventType(g_events[isExpanded][TRUE]);
686 GetEventHandler()->ProcessEvent(event);
687
688 event.SetEventType(g_events[isExpanded][FALSE]);
689 GetEventHandler()->ProcessEvent(event);
690 }
691 else
692 {
693 // I wonder if it really ever happens...
694 wxLogDebug("TreeView_Expand: change didn't took place.");
695 }
696 }
697
698 void wxTreeCtrl::Expand(const wxTreeItemId& item)
699 {
700 DoExpand(item, TVE_EXPAND);
701 }
702
703 void wxTreeCtrl::Collapse(const wxTreeItemId& item)
704 {
705 DoExpand(item, TVE_COLLAPSE);
706 }
707
708 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
709 {
710 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
711 }
712
713 void wxTreeCtrl::Toggle(const wxTreeItemId& item)
714 {
715 DoExpand(item, TVE_TOGGLE);
716 }
717
718 void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
719 {
720 DoExpand(item, action);
721 }
722
723 void wxTreeCtrl::Unselect()
724 {
725 SelectItem(wxTreeItemId((WXHTREEITEM) 0));
726 }
727
728 void wxTreeCtrl::SelectItem(const wxTreeItemId& item)
729 {
730 if ( !TreeView_SelectItem(wxhWnd, (HTREEITEM) (WXHTREEITEM) item) )
731 {
732 wxLogLastError("TreeView_SelectItem");
733 }
734 }
735
736 void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
737 {
738 // no error return
739 TreeView_EnsureVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item);
740 }
741
742 void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
743 {
744 if ( !TreeView_SelectSetFirstVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item) )
745 {
746 wxLogLastError("TreeView_SelectSetFirstVisible");
747 }
748 }
749
750 wxTextCtrl* wxTreeCtrl::GetEditControl() const
751 {
752 return m_textCtrl;
753 }
754
755 void wxTreeCtrl::DeleteTextCtrl()
756 {
757 if ( m_textCtrl )
758 {
759 m_textCtrl->UnsubclassWin();
760 m_textCtrl->SetHWND(0);
761 delete m_textCtrl;
762 m_textCtrl = NULL;
763 }
764 }
765
766 wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
767 wxClassInfo* textControlClass)
768 {
769 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
770
771 HWND hWnd = (HWND) TreeView_EditLabel(wxhWnd, (HTREEITEM) (WXHTREEITEM) item);
772
773 wxCHECK_MSG( hWnd, NULL, "Can't edit tree ctrl label" );
774
775 DeleteTextCtrl();
776
777 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
778 m_textCtrl->SetHWND((WXHWND)hWnd);
779 m_textCtrl->SubclassWin((WXHWND)hWnd);
780
781 return m_textCtrl;
782 }
783
784 // End label editing, optionally cancelling the edit
785 void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges)
786 {
787 TreeView_EndEditLabelNow(wxhWnd, discardChanges);
788
789 DeleteTextCtrl();
790 }
791
792 wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
793 {
794 TV_HITTESTINFO hitTestInfo;
795 hitTestInfo.pt.x = (int)point.x;
796 hitTestInfo.pt.y = (int)point.y;
797
798 TreeView_HitTest(wxhWnd, &hitTestInfo);
799
800 flags = 0;
801
802 // avoid repetition
803 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
804 flags |= wxTREE_HITTEST_##flag
805
806 TRANSLATE_FLAG(ABOVE);
807 TRANSLATE_FLAG(BELOW);
808 TRANSLATE_FLAG(NOWHERE);
809 TRANSLATE_FLAG(ONITEMBUTTON);
810 TRANSLATE_FLAG(ONITEMICON);
811 TRANSLATE_FLAG(ONITEMINDENT);
812 TRANSLATE_FLAG(ONITEMLABEL);
813 TRANSLATE_FLAG(ONITEMRIGHT);
814 TRANSLATE_FLAG(ONITEMSTATEICON);
815 TRANSLATE_FLAG(TOLEFT);
816 TRANSLATE_FLAG(TORIGHT);
817
818 #undef TRANSLATE_FLAG
819
820 return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem);
821 }
822
823 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
824 wxRect& rect,
825 bool textOnly) const
826 {
827 RECT rc;
828 if ( TreeView_GetItemRect(wxhWnd, (HTREEITEM)(WXHTREEITEM)item,
829 &rc, textOnly) )
830 {
831 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
832
833 return TRUE;
834 }
835 else
836 {
837 // couldn't retrieve rect: for example, item isn't visible
838 return FALSE;
839 }
840 }
841
842 // ----------------------------------------------------------------------------
843 // sorting stuff
844 // ----------------------------------------------------------------------------
845
846 static int CALLBACK TreeView_CompareCallback(wxTreeItemData *pItem1,
847 wxTreeItemData *pItem2,
848 wxTreeCtrl *tree)
849 {
850 wxCHECK_MSG( pItem1 && pItem2, 0,
851 _T("sorting tree without data doesn't make sense") );
852
853 return tree->OnCompareItems(pItem1->GetId(), pItem2->GetId());
854 }
855
856 int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
857 const wxTreeItemId& item2)
858 {
859 return strcmp(GetItemText(item1), GetItemText(item2));
860 }
861
862 void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
863 {
864 // rely on the fact that TreeView_SortChildren does the same thing as our
865 // default behaviour, i.e. sorts items alphabetically and so call it
866 // directly if we're not in derived class (much more efficient!)
867 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
868 {
869 TreeView_SortChildren(wxhWnd, (HTREEITEM)(WXHTREEITEM)item, 0);
870 }
871 else
872 {
873 TV_SORTCB tvSort;
874 tvSort.hParent = (HTREEITEM)(WXHTREEITEM)item;
875 tvSort.lpfnCompare = (PFNTVCOMPARE)TreeView_CompareCallback;
876 tvSort.lParam = (LPARAM)this;
877 TreeView_SortChildrenCB(wxhWnd, &tvSort, 0 /* reserved */);
878 }
879 }
880
881 // ----------------------------------------------------------------------------
882 // implementation
883 // ----------------------------------------------------------------------------
884
885 bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id)
886 {
887 if ( cmd == EN_UPDATE )
888 {
889 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
890 event.SetEventObject( this );
891 ProcessCommand(event);
892 }
893 else if ( cmd == EN_KILLFOCUS )
894 {
895 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
896 event.SetEventObject( this );
897 ProcessCommand(event);
898 }
899 else
900 {
901 // nothing done
902 return FALSE;
903 }
904
905 // command processed
906 return TRUE;
907 }
908
909 // process WM_NOTIFY Windows message
910 bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
911 {
912 wxTreeEvent event(wxEVT_NULL, m_windowId);
913 wxEventType eventType = wxEVT_NULL;
914 NMHDR *hdr = (NMHDR *)lParam;
915
916 switch ( hdr->code )
917 {
918 case TVN_BEGINDRAG:
919 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
920 // fall through
921
922 case TVN_BEGINRDRAG:
923 {
924 if ( eventType == wxEVT_NULL )
925 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
926 //else: left drag, already set above
927
928 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
929
930 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
931 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
932 break;
933 }
934
935 case TVN_BEGINLABELEDIT:
936 {
937 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
938 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
939
940 event.m_item = (WXHTREEITEM) info->item.hItem;
941 break;
942 }
943
944 case TVN_DELETEITEM:
945 {
946 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
947 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
948
949 event.m_item = (WXHTREEITEM) tv->itemOld.hItem;
950 break;
951 }
952
953 case TVN_ENDLABELEDIT:
954 {
955 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
956 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
957
958 event.m_item = (WXHTREEITEM) info->item.hItem;
959 break;
960 }
961
962 case TVN_GETDISPINFO:
963 eventType = wxEVT_COMMAND_TREE_GET_INFO;
964 // fall through
965
966 case TVN_SETDISPINFO:
967 {
968 if ( eventType == wxEVT_NULL )
969 eventType = wxEVT_COMMAND_TREE_SET_INFO;
970 //else: get, already set above
971
972 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
973
974 event.m_item = (WXHTREEITEM) info->item.hItem;
975 break;
976 }
977
978 case TVN_ITEMEXPANDING:
979 event.m_code = FALSE;
980 // fall through
981
982 case TVN_ITEMEXPANDED:
983 {
984 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
985
986 bool expand = FALSE;
987 switch ( tv->action )
988 {
989 case TVE_EXPAND:
990 expand = TRUE;
991 break;
992
993 case TVE_COLLAPSE:
994 expand = FALSE;
995 break;
996
997 default:
998 wxLogDebug("unexpected code %d in TVN_ITEMEXPAND "
999 "message", tv->action);
1000 }
1001
1002 bool ing = (hdr->code == TVN_ITEMEXPANDING);
1003 eventType = g_events[expand][ing];
1004
1005 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
1006 break;
1007 }
1008
1009 case TVN_KEYDOWN:
1010 {
1011 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
1012 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
1013
1014 event.m_code = wxCharCodeMSWToWX(info->wVKey);
1015
1016 // a separate event for this case
1017 if ( info->wVKey == VK_SPACE || info->wVKey == VK_RETURN )
1018 {
1019 wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
1020 m_windowId);
1021 event2.SetEventObject(this);
1022
1023 GetEventHandler()->ProcessEvent(event2);
1024 }
1025 break;
1026 }
1027
1028 case TVN_SELCHANGED:
1029 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
1030 // fall through
1031
1032 case TVN_SELCHANGING:
1033 {
1034 if ( eventType == wxEVT_NULL )
1035 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
1036 //else: already set above
1037
1038 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
1039
1040 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
1041 event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem;
1042 break;
1043 }
1044
1045 default:
1046 return wxControl::MSWOnNotify(idCtrl, lParam, result);
1047 }
1048
1049 event.SetEventObject(this);
1050 event.SetEventType(eventType);
1051
1052 bool processed = GetEventHandler()->ProcessEvent(event);
1053
1054 // post processing
1055 if ( hdr->code == TVN_DELETEITEM )
1056 {
1057 // NB: we might process this message using wxWindows event tables, but
1058 // due to overhead of wxWin event system we prefer to do it here
1059 // (otherwise deleting a tree with many items is just too slow)
1060 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
1061 wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
1062 delete data; // may be NULL, ok
1063 processed = TRUE; // Make sure we don't get called twice
1064 }
1065
1066 *result = !event.IsAllowed();
1067
1068 return processed;
1069 }
1070
1071 // ----------------------------------------------------------------------------
1072 // Tree event
1073 // ----------------------------------------------------------------------------
1074
1075 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
1076
1077 wxTreeEvent::wxTreeEvent(wxEventType commandType, int id)
1078 : wxNotifyEvent(commandType, id)
1079 {
1080 m_code = 0;
1081 m_itemOld = 0;
1082 }
1083
1084 #endif // __WIN95__
1085