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