]> git.saurik.com Git - wxWidgets.git/blob - src/msw/treectrl.cpp
1. added wxTreeCtrl::SetItemBold and IsBold, updated the sample to show them
[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 // ----------------------------------------------------------------------------
258 // Item access
259 // ----------------------------------------------------------------------------
260
261 wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
262 {
263 char buf[512]; // the size is arbitrary...
264
265 wxTreeViewItem tvItem(item, TVIF_TEXT);
266 tvItem.pszText = buf;
267 tvItem.cchTextMax = WXSIZEOF(buf);
268 if ( !DoGetItem(&tvItem) )
269 {
270 // don't return some garbage which was on stack, but an empty string
271 buf[0] = '\0';
272 }
273
274 return wxString(buf);
275 }
276
277 void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
278 {
279 wxTreeViewItem tvItem(item, TVIF_TEXT);
280 tvItem.pszText = (char *)text.c_str(); // conversion is ok
281 DoSetItem(&tvItem);
282 }
283
284 int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const
285 {
286 wxTreeViewItem tvItem(item, TVIF_IMAGE);
287 DoGetItem(&tvItem);
288
289 return tvItem.iImage;
290 }
291
292 void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image)
293 {
294 wxTreeViewItem tvItem(item, TVIF_IMAGE);
295 tvItem.iImage = image;
296 DoSetItem(&tvItem);
297 }
298
299 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const
300 {
301 wxTreeViewItem tvItem(item, TVIF_SELECTEDIMAGE);
302 DoGetItem(&tvItem);
303
304 return tvItem.iSelectedImage;
305 }
306
307 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image)
308 {
309 wxTreeViewItem tvItem(item, TVIF_SELECTEDIMAGE);
310 tvItem.iSelectedImage = image;
311 DoSetItem(&tvItem);
312 }
313
314 wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
315 {
316 wxTreeViewItem tvItem(item, TVIF_PARAM);
317 if ( !DoGetItem(&tvItem) )
318 {
319 return NULL;
320 }
321
322 return (wxTreeItemData *)tvItem.lParam;
323 }
324
325 void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
326 {
327 wxTreeViewItem tvItem(item, TVIF_PARAM);
328 tvItem.lParam = (LPARAM)data;
329 DoSetItem(&tvItem);
330 }
331
332 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
333 {
334 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
335 tvItem.cChildren = (int)has;
336 DoSetItem(&tvItem);
337 }
338
339 void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
340 {
341 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
342 tvItem.state = bold ? TVIS_BOLD : 0;
343 DoSetItem(&tvItem);
344 }
345
346 // ----------------------------------------------------------------------------
347 // Item status
348 // ----------------------------------------------------------------------------
349
350 bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
351 {
352 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
353 RECT rect;
354 return SendMessage(wxhWnd, TVM_GETITEMRECT, FALSE, (LPARAM)&rect) != 0;
355
356 }
357
358 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
359 {
360 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
361 DoGetItem(&tvItem);
362
363 return tvItem.cChildren != 0;
364 }
365
366 bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
367 {
368 // probably not a good idea to put it here
369 //wxASSERT( ItemHasChildren(item) );
370
371 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
372 DoGetItem(&tvItem);
373
374 return (tvItem.state & TVIS_EXPANDED) != 0;
375 }
376
377 bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
378 {
379 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
380 DoGetItem(&tvItem);
381
382 return (tvItem.state & TVIS_SELECTED) != 0;
383 }
384
385 bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
386 {
387 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
388 DoGetItem(&tvItem);
389
390 return (tvItem.state & TVIS_BOLD) != 0;
391 }
392
393 // ----------------------------------------------------------------------------
394 // navigation
395 // ----------------------------------------------------------------------------
396
397 wxTreeItemId wxTreeCtrl::GetRootItem() const
398 {
399 return wxTreeItemId((WXHTREEITEM) TreeView_GetRoot(wxhWnd));
400 }
401
402 wxTreeItemId wxTreeCtrl::GetSelection() const
403 {
404 return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(wxhWnd));
405 }
406
407 wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
408 {
409 return wxTreeItemId((WXHTREEITEM) TreeView_GetParent(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));
410 }
411
412 wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
413 long& _cookie) const
414 {
415 // remember the last child returned in 'cookie'
416 _cookie = (long)TreeView_GetChild(wxhWnd, (HTREEITEM) (WXHTREEITEM)item);
417
418 return wxTreeItemId((WXHTREEITEM)_cookie);
419 }
420
421 wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
422 long& _cookie) const
423 {
424 wxTreeItemId l=wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(wxhWnd,
425 (HTREEITEM) (WXHTREEITEM)_cookie));
426 _cookie=(long)l;
427 return l;
428 }
429
430 wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
431 {
432 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));
433 }
434
435 wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
436 {
437 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));
438 }
439
440 wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
441 {
442 return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(wxhWnd));
443 }
444
445 wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
446 {
447 wxASSERT_MSG( IsVisible(item), "The item you call GetNextVisible() "
448 "for must be visible itself!");
449
450 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));
451 }
452
453 wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
454 {
455 wxASSERT_MSG( IsVisible(item), "The item you call GetPrevVisible() "
456 "for must be visible itself!");
457
458 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item));
459 }
460
461 // ----------------------------------------------------------------------------
462 // Usual operations
463 // ----------------------------------------------------------------------------
464
465 wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
466 wxTreeItemId hInsertAfter,
467 const wxString& text,
468 int image, int selectedImage,
469 wxTreeItemData *data)
470 {
471 TV_INSERTSTRUCT tvIns;
472 tvIns.hParent = (HTREEITEM) (WXHTREEITEM)parent;
473 tvIns.hInsertAfter = (HTREEITEM) (WXHTREEITEM) hInsertAfter;
474 UINT mask = 0;
475 if ( !text.IsEmpty() )
476 {
477 mask |= TVIF_TEXT;
478 tvIns.item.pszText = (char *)text.c_str(); // cast is ok
479 }
480
481 if ( image != -1 )
482 {
483 mask |= TVIF_IMAGE;
484 tvIns.item.iImage = image;
485
486 if ( selectedImage == -1 )
487 {
488 // take the same image for selected icon if not specified
489 selectedImage = image;
490 }
491 }
492
493 if ( selectedImage != -1 )
494 {
495 mask |= TVIF_SELECTEDIMAGE;
496 tvIns.item.iSelectedImage = selectedImage;
497 }
498
499 if ( data != NULL )
500 {
501 mask |= TVIF_PARAM;
502 tvIns.item.lParam = (LPARAM)data;
503 }
504
505 tvIns.item.mask = mask;
506
507 HTREEITEM id = (HTREEITEM) TreeView_InsertItem(wxhWnd, &tvIns);
508 if ( id == 0 )
509 {
510 wxLogLastError("TreeView_InsertItem");
511 }
512
513 if ( data != NULL )
514 {
515 // associate the application tree item with Win32 tree item handle
516 data->SetId((WXHTREEITEM)id);
517 }
518
519 return wxTreeItemId((WXHTREEITEM)id);
520 }
521
522 // for compatibility only
523 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
524 const wxString& text,
525 int image, int selImage,
526 long insertAfter)
527 {
528 return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text,
529 image, selImage, NULL);
530 }
531
532 wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
533 int image, int selectedImage,
534 wxTreeItemData *data)
535 {
536 return DoInsertItem(wxTreeItemId((WXHTREEITEM) 0), (WXHTREEITEM) 0,
537 text, image, selectedImage, data);
538 }
539
540 wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
541 const wxString& text,
542 int image, int selectedImage,
543 wxTreeItemData *data)
544 {
545 return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST,
546 text, image, selectedImage, data);
547 }
548
549 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
550 const wxTreeItemId& idPrevious,
551 const wxString& text,
552 int image, int selectedImage,
553 wxTreeItemData *data)
554 {
555 return DoInsertItem(parent, idPrevious, text, image, selectedImage, data);
556 }
557
558 wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
559 const wxString& text,
560 int image, int selectedImage,
561 wxTreeItemData *data)
562 {
563 return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST,
564 text, image, selectedImage, data);
565 }
566
567 void wxTreeCtrl::Delete(const wxTreeItemId& item)
568 {
569 if ( !TreeView_DeleteItem(wxhWnd, (HTREEITEM)(WXHTREEITEM)item) )
570 {
571 wxLogLastError("TreeView_DeleteItem");
572 }
573 }
574
575 void wxTreeCtrl::DeleteAllItems()
576 {
577 if ( !TreeView_DeleteAllItems(wxhWnd) )
578 {
579 wxLogLastError("TreeView_DeleteAllItems");
580 }
581 }
582
583 void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
584 {
585 wxASSERT_MSG( flag == TVE_COLLAPSE || flag == TVE_COLLAPSERESET ||
586 flag == TVE_EXPAND || flag == TVE_TOGGLE,
587 "Unknown flag in wxTreeCtrl::DoExpand" );
588
589 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
590 // emulate them
591 if ( TreeView_Expand(wxhWnd, (HTREEITEM) (WXHTREEITEM) item, flag) != 0 )
592 {
593 wxTreeEvent event(wxEVT_NULL, m_windowId);
594 event.m_item = item;
595
596 bool isExpanded = IsExpanded(item);
597
598 event.SetEventObject(this);
599
600 // @@@ return values of {EXPAND|COLLAPS}ING event handler is discarded
601 event.SetEventType(g_events[isExpanded][TRUE]);
602 GetEventHandler()->ProcessEvent(event);
603
604 event.SetEventType(g_events[isExpanded][FALSE]);
605 GetEventHandler()->ProcessEvent(event);
606 }
607 else
608 {
609 // I wonder if it really ever happens...
610 wxLogDebug("TreeView_Expand: change didn't took place.");
611 }
612 }
613
614 void wxTreeCtrl::Expand(const wxTreeItemId& item)
615 {
616 DoExpand(item, TVE_EXPAND);
617 }
618
619 void wxTreeCtrl::Collapse(const wxTreeItemId& item)
620 {
621 DoExpand(item, TVE_COLLAPSE);
622 }
623
624 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
625 {
626 DoExpand(item, TVE_COLLAPSERESET);
627 }
628
629 void wxTreeCtrl::Toggle(const wxTreeItemId& item)
630 {
631 DoExpand(item, TVE_TOGGLE);
632 }
633
634 void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
635 {
636 DoExpand(item, action);
637 }
638
639 void wxTreeCtrl::Unselect()
640 {
641 SelectItem(wxTreeItemId((WXHTREEITEM) 0));
642 }
643
644 void wxTreeCtrl::SelectItem(const wxTreeItemId& item)
645 {
646 if ( !TreeView_SelectItem(wxhWnd, (HTREEITEM) (WXHTREEITEM) item) )
647 {
648 wxLogLastError("TreeView_SelectItem");
649 }
650 }
651
652 void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
653 {
654 // no error return
655 TreeView_EnsureVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item);
656 }
657
658 void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
659 {
660 if ( !TreeView_SelectSetFirstVisible(wxhWnd, (HTREEITEM) (WXHTREEITEM) item) )
661 {
662 wxLogLastError("TreeView_SelectSetFirstVisible");
663 }
664 }
665
666 wxTextCtrl* wxTreeCtrl::GetEditControl() const
667 {
668 return m_textCtrl;
669 }
670
671 void wxTreeCtrl::DeleteTextCtrl()
672 {
673 if ( m_textCtrl )
674 {
675 m_textCtrl->UnsubclassWin();
676 m_textCtrl->SetHWND(0);
677 delete m_textCtrl;
678 m_textCtrl = NULL;
679 }
680 }
681
682 wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
683 wxClassInfo* textControlClass)
684 {
685 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
686
687 HWND hWnd = (HWND) TreeView_EditLabel(wxhWnd, (HTREEITEM) (WXHTREEITEM) item);
688
689 wxCHECK_MSG( hWnd, NULL, "Can't edit tree ctrl label" );
690
691 DeleteTextCtrl();
692
693 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
694 m_textCtrl->SetHWND((WXHWND)hWnd);
695 m_textCtrl->SubclassWin((WXHWND)hWnd);
696
697 return m_textCtrl;
698 }
699
700 // End label editing, optionally cancelling the edit
701 void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges)
702 {
703 TreeView_EndEditLabelNow(wxhWnd, discardChanges);
704
705 DeleteTextCtrl();
706 }
707
708 wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
709 {
710 TV_HITTESTINFO hitTestInfo;
711 hitTestInfo.pt.x = (int)point.x;
712 hitTestInfo.pt.y = (int)point.y;
713
714 TreeView_HitTest(wxhWnd, &hitTestInfo);
715
716 flags = 0;
717
718 // avoid repetition
719 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
720 flags |= wxTREE_HITTEST_##flag
721
722 TRANSLATE_FLAG(ABOVE);
723 TRANSLATE_FLAG(BELOW);
724 TRANSLATE_FLAG(NOWHERE);
725 TRANSLATE_FLAG(ONITEMBUTTON);
726 TRANSLATE_FLAG(ONITEMICON);
727 TRANSLATE_FLAG(ONITEMINDENT);
728 TRANSLATE_FLAG(ONITEMLABEL);
729 TRANSLATE_FLAG(ONITEMRIGHT);
730 TRANSLATE_FLAG(ONITEMSTATEICON);
731 TRANSLATE_FLAG(TOLEFT);
732 TRANSLATE_FLAG(TORIGHT);
733
734 #undef TRANSLATE_FLAG
735
736 return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem);
737 }
738
739 void wxTreeCtrl::SortChildren(const wxTreeItemId& item,
740 wxTreeItemCmpFunc *cmpFunction)
741 {
742 if ( cmpFunction == NULL )
743 {
744 TreeView_SortChildren(wxhWnd, (HTREEITEM) (WXHTREEITEM) item, 0);
745 }
746 else
747 {
748 // TODO: use TreeView_SortChildrenCB
749 wxFAIL_MSG("wxTreeCtrl::SortChildren not implemented");
750 }
751 }
752
753 // TODO
754 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
755 {
756 return 0;
757 }
758
759 // ----------------------------------------------------------------------------
760 // implementation
761 // ----------------------------------------------------------------------------
762
763 bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id)
764 {
765 if ( cmd == EN_UPDATE )
766 {
767 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
768 event.SetEventObject( this );
769 ProcessCommand(event);
770 }
771 else if ( cmd == EN_KILLFOCUS )
772 {
773 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
774 event.SetEventObject( this );
775 ProcessCommand(event);
776 }
777 else
778 {
779 // nothing done
780 return FALSE;
781 }
782
783 // command processed
784 return TRUE;
785 }
786
787 // process WM_NOTIFY Windows message
788 bool wxTreeCtrl::MSWNotify(WXWPARAM wParam, WXLPARAM lParam, WXLPARAM *result)
789 {
790 wxTreeEvent event(wxEVT_NULL, m_windowId);
791 wxEventType eventType = wxEVT_NULL;
792 NMHDR *hdr = (NMHDR *)lParam;
793
794 switch ( hdr->code )
795 {
796 case TVN_BEGINDRAG:
797 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
798 // fall through
799
800 case TVN_BEGINRDRAG:
801 {
802 if ( eventType == wxEVT_NULL )
803 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
804 //else: left drag, already set above
805
806 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
807
808 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
809 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
810 break;
811 }
812
813 case TVN_BEGINLABELEDIT:
814 {
815 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
816 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
817
818 event.m_item = (WXHTREEITEM) info->item.hItem;
819 break;
820 }
821
822 case TVN_DELETEITEM:
823 {
824 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
825 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
826
827 event.m_item = (WXHTREEITEM) tv->itemOld.hItem;
828 break;
829 }
830
831 case TVN_ENDLABELEDIT:
832 {
833 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
834 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
835
836 event.m_item = (WXHTREEITEM) info->item.hItem;
837 break;
838 }
839
840 case TVN_GETDISPINFO:
841 eventType = wxEVT_COMMAND_TREE_GET_INFO;
842 // fall through
843
844 case TVN_SETDISPINFO:
845 {
846 if ( eventType == wxEVT_NULL )
847 eventType = wxEVT_COMMAND_TREE_SET_INFO;
848 //else: get, already set above
849
850 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
851
852 event.m_item = (WXHTREEITEM) info->item.hItem;
853 break;
854 }
855
856 case TVN_ITEMEXPANDING:
857 event.m_code = FALSE;
858 // fall through
859
860 case TVN_ITEMEXPANDED:
861 {
862 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
863
864 bool expand = FALSE;
865 switch ( tv->action )
866 {
867 case TVE_EXPAND:
868 expand = TRUE;
869 break;
870
871 case TVE_COLLAPSE:
872 expand = FALSE;
873 break;
874
875 default:
876 wxLogDebug("unexpected code %d in TVN_ITEMEXPAND "
877 "message", tv->action);
878 }
879
880 bool ing = (hdr->code == TVN_ITEMEXPANDING);
881 eventType = g_events[expand][ing];
882
883 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
884 break;
885 }
886
887 case TVN_KEYDOWN:
888 {
889 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
890 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
891
892 event.m_code = wxCharCodeMSWToWX(info->wVKey);
893 break;
894 }
895
896 case TVN_SELCHANGED:
897 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
898 // fall through
899
900 case TVN_SELCHANGING:
901 {
902 if ( eventType == wxEVT_NULL )
903 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
904 //else: already set above
905
906 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
907
908 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
909 event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem;
910 break;
911 }
912
913 default:
914 return wxControl::MSWNotify(wParam, lParam, result);
915 }
916
917 event.SetEventObject(this);
918 event.SetEventType(eventType);
919
920 bool processed = GetEventHandler()->ProcessEvent(event);
921
922 // post processing
923 if ( hdr->code == TVN_DELETEITEM )
924 {
925 // NB: we might process this message using wxWindows event tables, but
926 // due to overhead of wxWin event system we prefer to do it here
927 // (otherwise deleting a tree with many items is just too slow)
928 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
929 wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
930 delete data; // may be NULL, ok
931 }
932
933 *result = !event.IsAllowed();
934
935 return processed;
936 }
937
938 // ----------------------------------------------------------------------------
939 // Tree event
940 // ----------------------------------------------------------------------------
941
942 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxCommandEvent)
943
944 wxTreeEvent::wxTreeEvent(wxEventType commandType, int id)
945 : wxNotifyEvent(commandType, id)
946 {
947 m_code = 0;
948 m_itemOld = 0;
949 }
950
951 #endif // __WIN95__
952