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