]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/treectrl.cpp
added default ctor for wxDirDialog
[wxWidgets.git] / src / msw / treectrl.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: treectrl.cpp
3// Purpose: wxTreeCtrl
4// Author: Julian Smart
5// Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
6// Created: 1997
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19#ifdef __GNUG__
20 #pragma implementation "treectrl.h"
21#endif
22
23// For compilers that support precompilation, includes "wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif
29
30#include "wx/window.h"
31#include "wx/msw/private.h"
32
33// Mingw32 is a bit mental even though this is done in winundef
34#ifdef GetFirstChild
35 #undef GetFirstChild
36#endif
37
38#ifdef GetNextSibling
39 #undef GetNextSibling
40#endif
41
42#if defined(__WIN95__)
43
44#include "wx/log.h"
45#include "wx/dynarray.h"
46#include "wx/imaglist.h"
47#include "wx/treectrl.h"
48#include "wx/settings.h"
49
50#ifdef __GNUWIN32__
51#ifndef wxUSE_NORLANDER_HEADERS
52#include "wx/msw/gnuwin32/extra.h"
53#endif
54#endif
55
56#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
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#ifdef __VISUALC__
71#pragma warning( disable : 4097 )
72#endif
73
74struct wxTreeViewItem : public TV_ITEM
75{
76 wxTreeViewItem(const wxTreeItemId& item, // the item handle
77 UINT mask_, // fields which are valid
78 UINT stateMask_ = 0) // for TVIF_STATE only
79 {
80 // hItem member is always valid
81 mask = mask_ | TVIF_HANDLE;
82 stateMask = stateMask_;
83 hItem = (HTREEITEM) (WXHTREEITEM) item;
84 }
85};
86
87#ifdef __VISUALC__
88#pragma warning( default : 4097 )
89#endif
90
91// a class which encapsulates the tree traversal logic: it vists all (unless
92// OnVisit() returns FALSE) items under the given one
93class wxTreeTraversal
94{
95public:
96 wxTreeTraversal(const wxTreeCtrl *tree)
97 {
98 m_tree = tree;
99 }
100
101 // do traverse the tree: visit all items (recursively by default) under the
102 // given one; return TRUE if all items were traversed or FALSE if the
103 // traversal was aborted because OnVisit returned FALSE
104 bool DoTraverse(const wxTreeItemId& root, bool recursively = TRUE);
105
106 // override this function to do whatever is needed for each item, return
107 // FALSE to stop traversing
108 virtual bool OnVisit(const wxTreeItemId& item) = 0;
109
110protected:
111 const wxTreeCtrl *GetTree() const { return m_tree; }
112
113private:
114 bool Traverse(const wxTreeItemId& root, bool recursively);
115
116 const wxTreeCtrl *m_tree;
117};
118
119// internal class for getting the selected items
120class TraverseSelections : public wxTreeTraversal
121{
122public:
123 TraverseSelections(const wxTreeCtrl *tree,
124 wxArrayTreeItemIds& selections)
125 : wxTreeTraversal(tree), m_selections(selections)
126 {
127 m_selections.Empty();
128
129 DoTraverse(tree->GetRootItem());
130 }
131
132 virtual bool OnVisit(const wxTreeItemId& item)
133 {
134 if ( GetTree()->IsItemChecked(item) )
135 {
136 m_selections.Add(item);
137 }
138
139 return TRUE;
140 }
141
142private:
143 wxArrayTreeItemIds& m_selections;
144};
145
146// internal class for counting tree items
147class TraverseCounter : public wxTreeTraversal
148{
149public:
150 TraverseCounter(const wxTreeCtrl *tree,
151 const wxTreeItemId& root,
152 bool recursively)
153 : wxTreeTraversal(tree)
154 {
155 m_count = 0;
156
157 DoTraverse(root, recursively);
158 }
159
160 virtual bool OnVisit(const wxTreeItemId& item)
161 {
162 m_count++;
163
164 return TRUE;
165 }
166
167 size_t GetCount() const { return m_count; }
168
169private:
170 size_t m_count;
171};
172
173// ----------------------------------------------------------------------------
174// This class is needed for support of different images: the Win32 common
175// control natively supports only 2 images (the normal one and another for the
176// selected state). We wish to provide support for 2 more of them for folder
177// items (i.e. those which have children): for expanded state and for expanded
178// selected state. For this we use this structure to store the additional items
179// images.
180//
181// There is only one problem with this: when we retrieve the item's data, we
182// don't know whether we get a pointer to wxTreeItemData or
183// wxTreeItemIndirectData. So we have to maintain a list of all items which
184// have indirect data inside the listctrl itself.
185// ----------------------------------------------------------------------------
186
187class wxTreeItemIndirectData
188{
189public:
190 // ctor associates this data with the item and the real item data becomes
191 // available through our GetData() method
192 wxTreeItemIndirectData(wxTreeCtrl *tree, const wxTreeItemId& item)
193 {
194 for ( size_t n = 0; n < WXSIZEOF(m_images); n++ )
195 {
196 m_images[n] = -1;
197 }
198
199 // save the old data
200 m_data = tree->GetItemData(item);
201
202 // and set ourselves as the new one
203 tree->SetIndirectItemData(item, this);
204 }
205
206 // dtor deletes the associated data as well
207 ~wxTreeItemIndirectData() { delete m_data; }
208
209 // accessors
210 // get the real data associated with the item
211 wxTreeItemData *GetData() const { return m_data; }
212 // change it
213 void SetData(wxTreeItemData *data) { m_data = data; }
214
215 // do we have such image?
216 bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; }
217 // get image
218 int GetImage(wxTreeItemIcon which) const { return m_images[which]; }
219 // change it
220 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
221
222private:
223 // all the images associated with the item
224 int m_images[wxTreeItemIcon_Max];
225
226 wxTreeItemData *m_data;
227};
228
229// ----------------------------------------------------------------------------
230// macros
231// ----------------------------------------------------------------------------
232
233#if !USE_SHARED_LIBRARY
234 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl)
235#endif
236
237// ----------------------------------------------------------------------------
238// variables
239// ----------------------------------------------------------------------------
240
241// handy table for sending events
242static const wxEventType g_events[2][2] =
243{
244 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
245 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
246};
247
248// ============================================================================
249// implementation
250// ============================================================================
251
252// ----------------------------------------------------------------------------
253// tree traversal
254// ----------------------------------------------------------------------------
255
256bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively)
257{
258 if ( !OnVisit(root) )
259 return FALSE;
260
261 return Traverse(root, recursively);
262}
263
264bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively)
265{
266 long cookie;
267 wxTreeItemId child = m_tree->GetFirstChild(root, cookie);
268 while ( child.IsOk() )
269 {
270 // depth first traversal
271 if ( recursively && !Traverse(child, TRUE) )
272 return FALSE;
273
274 if ( !OnVisit(child) )
275 return FALSE;
276
277 child = m_tree->GetNextChild(root, cookie);
278 }
279
280 return TRUE;
281}
282
283// ----------------------------------------------------------------------------
284// construction and destruction
285// ----------------------------------------------------------------------------
286
287void wxTreeCtrl::Init()
288{
289 m_imageListNormal = NULL;
290 m_imageListState = NULL;
291 m_textCtrl = NULL;
292 m_hasAnyAttr = FALSE;
293}
294
295bool wxTreeCtrl::Create(wxWindow *parent,
296 wxWindowID id,
297 const wxPoint& pos,
298 const wxSize& size,
299 long style,
300 const wxValidator& validator,
301 const wxString& name)
302{
303 Init();
304
305 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
306 return FALSE;
307
308 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP |
309 TVS_HASLINES | TVS_SHOWSELALWAYS;
310
311 if ( m_windowStyle & wxTR_HAS_BUTTONS )
312 wstyle |= TVS_HASBUTTONS;
313
314 if ( m_windowStyle & wxTR_EDIT_LABELS )
315 wstyle |= TVS_EDITLABELS;
316
317 if ( m_windowStyle & wxTR_LINES_AT_ROOT )
318 wstyle |= TVS_LINESATROOT;
319
320#if !defined( __GNUWIN32__ ) && !defined( __BORLANDC__ ) && !defined( __WATCOMC__ ) && !defined(wxUSE_NORLANDER_HEADERS)
321 // we emulate the multiple selection tree controls by using checkboxes: set
322 // up the image list we need for this if we do have multiple selections
323#if !defined(__VISUALC__) || (__VISUALC__ > 1010)
324 if ( m_windowStyle & wxTR_MULTIPLE )
325 wstyle |= TVS_CHECKBOXES;
326#endif
327#endif
328
329 // Create the tree control.
330 if ( !MSWCreateControl(WC_TREEVIEW, wstyle) )
331 return FALSE;
332
333 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
334 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
335
336 // VZ: this is some experimental code which may be used to get the
337 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
338 // AFAIK, the standard DLL does about the same thing anyhow.
339#if 0
340 if ( m_windowStyle & wxTR_MULTIPLE )
341 {
342 wxBitmap bmp;
343
344 // create the DC compatible with the current screen
345 HDC hdcMem = CreateCompatibleDC(NULL);
346
347 // create a mono bitmap of the standard size
348 int x = GetSystemMetrics(SM_CXMENUCHECK);
349 int y = GetSystemMetrics(SM_CYMENUCHECK);
350 wxImageList imagelistCheckboxes(x, y, FALSE, 2);
351 HBITMAP hbmpCheck = CreateBitmap(x, y, // bitmap size
352 1, // # of color planes
353 1, // # bits needed for one pixel
354 0); // array containing colour data
355 SelectObject(hdcMem, hbmpCheck);
356
357 // then draw a check mark into it
358 RECT rect = { 0, 0, x, y };
359 if ( !::DrawFrameControl(hdcMem, &rect,
360 DFC_BUTTON,
361 DFCS_BUTTONCHECK | DFCS_CHECKED) )
362 {
363 wxLogLastError(wxT("DrawFrameControl(check)"));
364 }
365
366 bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
367 imagelistCheckboxes.Add(bmp);
368
369 if ( !::DrawFrameControl(hdcMem, &rect,
370 DFC_BUTTON,
371 DFCS_BUTTONCHECK) )
372 {
373 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
374 }
375
376 bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
377 imagelistCheckboxes.Add(bmp);
378
379 // clean up
380 ::DeleteDC(hdcMem);
381
382 // set the imagelist
383 SetStateImageList(&imagelistCheckboxes);
384 }
385#endif // 0
386
387 SetSize(pos.x, pos.y, size.x, size.y);
388
389 return TRUE;
390}
391
392wxTreeCtrl::~wxTreeCtrl()
393{
394 // delete any attributes
395 if ( m_hasAnyAttr )
396 {
397 for ( wxNode *node = m_attrs.Next(); node; node = m_attrs.Next() )
398 {
399 delete (wxTreeItemAttr *)node->Data();
400 }
401
402 // prevent TVN_DELETEITEM handler from deleting the attributes again!
403 m_hasAnyAttr = FALSE;
404 }
405
406 DeleteTextCtrl();
407
408 // delete user data to prevent memory leaks
409 DeleteAllItems();
410}
411
412// ----------------------------------------------------------------------------
413// accessors
414// ----------------------------------------------------------------------------
415
416// simple wrappers which add error checking in debug mode
417
418bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const
419{
420 if ( !TreeView_GetItem(GetHwnd(), tvItem) )
421 {
422 wxLogLastError("TreeView_GetItem");
423
424 return FALSE;
425 }
426
427 return TRUE;
428}
429
430void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem)
431{
432 if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 )
433 {
434 wxLogLastError("TreeView_SetItem");
435 }
436}
437
438size_t wxTreeCtrl::GetCount() const
439{
440 return (size_t)TreeView_GetCount(GetHwnd());
441}
442
443unsigned int wxTreeCtrl::GetIndent() const
444{
445 return TreeView_GetIndent(GetHwnd());
446}
447
448void wxTreeCtrl::SetIndent(unsigned int indent)
449{
450 TreeView_SetIndent(GetHwnd(), indent);
451}
452
453wxImageList *wxTreeCtrl::GetImageList() const
454{
455 return m_imageListNormal;
456}
457
458wxImageList *wxTreeCtrl::GetStateImageList() const
459{
460 return m_imageListNormal;
461}
462
463void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
464{
465 // no error return
466 TreeView_SetImageList(GetHwnd(),
467 imageList ? imageList->GetHIMAGELIST() : 0,
468 which);
469}
470
471void wxTreeCtrl::SetImageList(wxImageList *imageList)
472{
473 SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL);
474}
475
476void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
477{
478 SetAnyImageList(m_imageListState = imageList, TVSIL_STATE);
479}
480
481size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
482 bool recursively) const
483{
484 TraverseCounter counter(this, item, recursively);
485
486 return counter.GetCount() - 1;
487}
488
489// ----------------------------------------------------------------------------
490// Item access
491// ----------------------------------------------------------------------------
492
493wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
494{
495 wxChar buf[512]; // the size is arbitrary...
496
497 wxTreeViewItem tvItem(item, TVIF_TEXT);
498 tvItem.pszText = buf;
499 tvItem.cchTextMax = WXSIZEOF(buf);
500 if ( !DoGetItem(&tvItem) )
501 {
502 // don't return some garbage which was on stack, but an empty string
503 buf[0] = wxT('\0');
504 }
505
506 return wxString(buf);
507}
508
509void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
510{
511 wxTreeViewItem tvItem(item, TVIF_TEXT);
512 tvItem.pszText = (wxChar *)text.c_str(); // conversion is ok
513 DoSetItem(&tvItem);
514}
515
516int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId& item,
517 wxTreeItemIcon which) const
518{
519 wxTreeViewItem tvItem(item, TVIF_PARAM);
520 if ( !DoGetItem(&tvItem) )
521 {
522 return -1;
523 }
524
525 return ((wxTreeItemIndirectData *)tvItem.lParam)->GetImage(which);
526}
527
528void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId& item,
529 int image,
530 wxTreeItemIcon which) const
531{
532 wxTreeViewItem tvItem(item, TVIF_PARAM);
533 if ( !DoGetItem(&tvItem) )
534 {
535 return;
536 }
537
538 wxTreeItemIndirectData *data = ((wxTreeItemIndirectData *)tvItem.lParam);
539
540 data->SetImage(image, which);
541
542 // make sure that we have selected images as well
543 if ( which == wxTreeItemIcon_Normal &&
544 !data->HasImage(wxTreeItemIcon_Selected) )
545 {
546 data->SetImage(image, wxTreeItemIcon_Selected);
547 }
548
549 if ( which == wxTreeItemIcon_Expanded &&
550 !data->HasImage(wxTreeItemIcon_SelectedExpanded) )
551 {
552 data->SetImage(image, wxTreeItemIcon_SelectedExpanded);
553 }
554}
555
556void wxTreeCtrl::DoSetItemImages(const wxTreeItemId& item,
557 int image,
558 int imageSel)
559{
560 wxTreeViewItem tvItem(item, TVIF_IMAGE | TVIF_SELECTEDIMAGE);
561 tvItem.iSelectedImage = imageSel;
562 tvItem.iImage = image;
563 DoSetItem(&tvItem);
564}
565
566int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
567 wxTreeItemIcon which) const
568{
569 if ( HasIndirectData(item) )
570 {
571 return DoGetItemImageFromData(item, which);
572 }
573
574 UINT mask;
575 switch ( which )
576 {
577 default:
578 wxFAIL_MSG( wxT("unknown tree item image type") );
579
580 case wxTreeItemIcon_Normal:
581 mask = TVIF_IMAGE;
582 break;
583
584 case wxTreeItemIcon_Selected:
585 mask = TVIF_SELECTEDIMAGE;
586 break;
587
588 case wxTreeItemIcon_Expanded:
589 case wxTreeItemIcon_SelectedExpanded:
590 return -1;
591 }
592
593 wxTreeViewItem tvItem(item, mask);
594 DoGetItem(&tvItem);
595
596 return mask == TVIF_IMAGE ? tvItem.iImage : tvItem.iSelectedImage;
597}
598
599void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
600 wxTreeItemIcon which)
601{
602 int imageNormal, imageSel;
603 switch ( which )
604 {
605 default:
606 wxFAIL_MSG( wxT("unknown tree item image type") );
607
608 case wxTreeItemIcon_Normal:
609 imageNormal = image;
610 imageSel = GetItemSelectedImage(item);
611 break;
612
613 case wxTreeItemIcon_Selected:
614 imageNormal = GetItemImage(item);
615 imageSel = image;
616 break;
617
618 case wxTreeItemIcon_Expanded:
619 case wxTreeItemIcon_SelectedExpanded:
620 if ( !HasIndirectData(item) )
621 {
622 // we need to get the old images first, because after we create
623 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
624 // get the images
625 imageNormal = GetItemImage(item);
626 imageSel = GetItemSelectedImage(item);
627
628 // if it doesn't have it yet, add it
629 wxTreeItemIndirectData *data = new
630 wxTreeItemIndirectData(this, item);
631
632 // copy the data to the new location
633 data->SetImage(imageNormal, wxTreeItemIcon_Normal);
634 data->SetImage(imageSel, wxTreeItemIcon_Selected);
635 }
636
637 DoSetItemImageFromData(item, image, which);
638
639 // reset the normal/selected images because we won't use them any
640 // more - now they're stored inside the indirect data
641 imageNormal =
642 imageSel = I_IMAGECALLBACK;
643 break;
644 }
645
646 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
647 // change both normal and selected image - otherwise the change simply
648 // doesn't take place!
649 DoSetItemImages(item, imageNormal, imageSel);
650}
651
652wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
653{
654 wxTreeViewItem tvItem(item, TVIF_PARAM);
655 if ( !DoGetItem(&tvItem) )
656 {
657 return NULL;
658 }
659
660 if ( HasIndirectData(item) )
661 {
662 return ((wxTreeItemIndirectData *)tvItem.lParam)->GetData();
663 }
664 else
665 {
666 return (wxTreeItemData *)tvItem.lParam;
667 }
668}
669
670void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
671{
672 wxTreeViewItem tvItem(item, TVIF_PARAM);
673
674 if ( HasIndirectData(item) )
675 {
676 if ( DoGetItem(&tvItem) )
677 {
678 ((wxTreeItemIndirectData *)tvItem.lParam)->SetData(data);
679 }
680 else
681 {
682 wxFAIL_MSG( wxT("failed to change tree items data") );
683 }
684 }
685 else
686 {
687 tvItem.lParam = (LPARAM)data;
688 DoSetItem(&tvItem);
689 }
690}
691
692void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId& item,
693 wxTreeItemIndirectData *data)
694{
695 // this should never happen because it's unnecessary and will probably lead
696 // to crash too because the code elsewhere supposes that the pointer the
697 // wxTreeItemIndirectData has is a real wxItemData and not
698 // wxTreeItemIndirectData as well
699 wxASSERT_MSG( !HasIndirectData(item), wxT("setting indirect data twice?") );
700
701 SetItemData(item, (wxTreeItemData *)data);
702
703 m_itemsWithIndirectData.Add(item);
704}
705
706bool wxTreeCtrl::HasIndirectData(const wxTreeItemId& item) const
707{
708 return m_itemsWithIndirectData.Index(item) != wxNOT_FOUND;
709}
710
711void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
712{
713 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
714 tvItem.cChildren = (int)has;
715 DoSetItem(&tvItem);
716}
717
718void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
719{
720 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
721 tvItem.state = bold ? TVIS_BOLD : 0;
722 DoSetItem(&tvItem);
723}
724
725void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
726{
727 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
728 tvItem.state = highlight ? TVIS_DROPHILITED : 0;
729 DoSetItem(&tvItem);
730}
731
732void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
733 const wxColour& col)
734{
735 m_hasAnyAttr = TRUE;
736
737 long id = (long)(WXHTREEITEM)item;
738 wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id);
739 if ( !attr )
740 {
741 attr = new wxTreeItemAttr;
742 m_attrs.Put(id, (wxObject *)attr);
743 }
744
745 attr->SetTextColour(col);
746}
747
748void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
749 const wxColour& col)
750{
751 m_hasAnyAttr = TRUE;
752
753 long id = (long)(WXHTREEITEM)item;
754 wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id);
755 if ( !attr )
756 {
757 attr = new wxTreeItemAttr;
758 m_attrs.Put(id, (wxObject *)attr);
759 }
760
761 attr->SetBackgroundColour(col);
762}
763
764void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
765{
766 m_hasAnyAttr = TRUE;
767
768 long id = (long)(WXHTREEITEM)item;
769 wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id);
770 if ( !attr )
771 {
772 attr = new wxTreeItemAttr;
773 m_attrs.Put(id, (wxObject *)attr);
774 }
775
776 attr->SetFont(font);
777}
778
779// ----------------------------------------------------------------------------
780// Item status
781// ----------------------------------------------------------------------------
782
783bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
784{
785 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
786 RECT rect;
787
788 // this ugliness comes directly from MSDN - it *is* the correct way to pass
789 // the HTREEITEM with TVM_GETITEMRECT
790 *(WXHTREEITEM *)&rect = (WXHTREEITEM)item;
791
792 // FALSE means get item rect for the whole item, not only text
793 return SendMessage(GetHwnd(), TVM_GETITEMRECT, FALSE, (LPARAM)&rect) != 0;
794
795}
796
797bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
798{
799 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
800 DoGetItem(&tvItem);
801
802 return tvItem.cChildren != 0;
803}
804
805bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
806{
807 // probably not a good idea to put it here
808 //wxASSERT( ItemHasChildren(item) );
809
810 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
811 DoGetItem(&tvItem);
812
813 return (tvItem.state & TVIS_EXPANDED) != 0;
814}
815
816bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
817{
818 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
819 DoGetItem(&tvItem);
820
821 return (tvItem.state & TVIS_SELECTED) != 0;
822}
823
824bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
825{
826 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
827 DoGetItem(&tvItem);
828
829 return (tvItem.state & TVIS_BOLD) != 0;
830}
831
832// ----------------------------------------------------------------------------
833// navigation
834// ----------------------------------------------------------------------------
835
836wxTreeItemId wxTreeCtrl::GetRootItem() const
837{
838 return wxTreeItemId((WXHTREEITEM) TreeView_GetRoot(GetHwnd()));
839}
840
841wxTreeItemId wxTreeCtrl::GetSelection() const
842{
843 wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), (WXHTREEITEM)0,
844 wxT("this only works with single selection controls") );
845
846 return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(GetHwnd()));
847}
848
849wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
850{
851 return wxTreeItemId((WXHTREEITEM) TreeView_GetParent(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
852}
853
854wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
855 long& _cookie) const
856{
857 // remember the last child returned in 'cookie'
858 _cookie = (long)TreeView_GetChild(GetHwnd(), (HTREEITEM) (WXHTREEITEM)item);
859
860 return wxTreeItemId((WXHTREEITEM)_cookie);
861}
862
863wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
864 long& _cookie) const
865{
866 wxTreeItemId l = wxTreeItemId((WXHTREEITEM)TreeView_GetNextSibling(GetHwnd(),
867 (HTREEITEM)(WXHTREEITEM)_cookie));
868 _cookie = (long)l;
869
870 return l;
871}
872
873wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
874{
875 // can this be done more efficiently?
876 long cookie;
877
878 wxTreeItemId childLast,
879 child = GetFirstChild(item, cookie);
880 while ( child.IsOk() )
881 {
882 childLast = child;
883 child = GetNextChild(item, cookie);
884 }
885
886 return childLast;
887}
888
889wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
890{
891 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
892}
893
894wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
895{
896 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
897}
898
899wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
900{
901 return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(GetHwnd()));
902}
903
904wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
905{
906 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() "
907 "for must be visible itself!"));
908
909 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
910}
911
912wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
913{
914 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() "
915 "for must be visible itself!"));
916
917 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
918}
919
920// ----------------------------------------------------------------------------
921// multiple selections emulation
922// ----------------------------------------------------------------------------
923
924bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const
925{
926 // receive the desired information.
927 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
928 DoGetItem(&tvItem);
929
930 // state image indices are 1 based
931 return ((tvItem.state >> 12) - 1) == 1;
932}
933
934void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check)
935{
936 // receive the desired information.
937 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
938
939 // state images are one-based
940 tvItem.state = (check ? 2 : 1) << 12;
941
942 DoSetItem(&tvItem);
943}
944
945size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
946{
947 TraverseSelections selector(this, selections);
948
949 return selections.GetCount();
950}
951
952// ----------------------------------------------------------------------------
953// Usual operations
954// ----------------------------------------------------------------------------
955
956wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
957 wxTreeItemId hInsertAfter,
958 const wxString& text,
959 int image, int selectedImage,
960 wxTreeItemData *data)
961{
962 TV_INSERTSTRUCT tvIns;
963 tvIns.hParent = (HTREEITEM) (WXHTREEITEM)parent;
964 tvIns.hInsertAfter = (HTREEITEM) (WXHTREEITEM) hInsertAfter;
965
966 // this is how we insert the item as the first child: supply a NULL
967 // hInsertAfter
968 if ( !tvIns.hInsertAfter )
969 {
970 tvIns.hInsertAfter = TVI_FIRST;
971 }
972
973 UINT mask = 0;
974 if ( !text.IsEmpty() )
975 {
976 mask |= TVIF_TEXT;
977 tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok
978 }
979
980 if ( image != -1 )
981 {
982 mask |= TVIF_IMAGE;
983 tvIns.item.iImage = image;
984
985 if ( selectedImage == -1 )
986 {
987 // take the same image for selected icon if not specified
988 selectedImage = image;
989 }
990 }
991
992 if ( selectedImage != -1 )
993 {
994 mask |= TVIF_SELECTEDIMAGE;
995 tvIns.item.iSelectedImage = selectedImage;
996 }
997
998 if ( data != NULL )
999 {
1000 mask |= TVIF_PARAM;
1001 tvIns.item.lParam = (LPARAM)data;
1002 }
1003
1004 tvIns.item.mask = mask;
1005
1006 HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns);
1007 if ( id == 0 )
1008 {
1009 wxLogLastError("TreeView_InsertItem");
1010 }
1011
1012 if ( data != NULL )
1013 {
1014 // associate the application tree item with Win32 tree item handle
1015 data->SetId((WXHTREEITEM)id);
1016 }
1017
1018 return wxTreeItemId((WXHTREEITEM)id);
1019}
1020
1021// for compatibility only
1022wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1023 const wxString& text,
1024 int image, int selImage,
1025 long insertAfter)
1026{
1027 return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text,
1028 image, selImage, NULL);
1029}
1030
1031wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
1032 int image, int selectedImage,
1033 wxTreeItemData *data)
1034{
1035 return DoInsertItem(wxTreeItemId((WXHTREEITEM) 0), (WXHTREEITEM) 0,
1036 text, image, selectedImage, data);
1037}
1038
1039wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
1040 const wxString& text,
1041 int image, int selectedImage,
1042 wxTreeItemData *data)
1043{
1044 return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST,
1045 text, image, selectedImage, data);
1046}
1047
1048wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1049 const wxTreeItemId& idPrevious,
1050 const wxString& text,
1051 int image, int selectedImage,
1052 wxTreeItemData *data)
1053{
1054 return DoInsertItem(parent, idPrevious, text, image, selectedImage, data);
1055}
1056
1057wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
1058 const wxString& text,
1059 int image, int selectedImage,
1060 wxTreeItemData *data)
1061{
1062 return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST,
1063 text, image, selectedImage, data);
1064}
1065
1066void wxTreeCtrl::Delete(const wxTreeItemId& item)
1067{
1068 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item) )
1069 {
1070 wxLogLastError("TreeView_DeleteItem");
1071 }
1072}
1073
1074// delete all children (but don't delete the item itself)
1075void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
1076{
1077 long cookie;
1078
1079 wxArrayLong children;
1080 wxTreeItemId child = GetFirstChild(item, cookie);
1081 while ( child.IsOk() )
1082 {
1083 children.Add((long)(WXHTREEITEM)child);
1084
1085 child = GetNextChild(item, cookie);
1086 }
1087
1088 size_t nCount = children.Count();
1089 for ( size_t n = 0; n < nCount; n++ )
1090 {
1091 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)children[n]) )
1092 {
1093 wxLogLastError("TreeView_DeleteItem");
1094 }
1095 }
1096}
1097
1098void wxTreeCtrl::DeleteAllItems()
1099{
1100 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1101 {
1102 wxLogLastError("TreeView_DeleteAllItems");
1103 }
1104}
1105
1106void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
1107{
1108 wxASSERT_MSG( flag == TVE_COLLAPSE ||
1109 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
1110 flag == TVE_EXPAND ||
1111 flag == TVE_TOGGLE,
1112 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1113
1114 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1115 // emulate them. This behaviour has changed slightly with comctl32.dll
1116 // v 4.70 - now it does send them but only the first time. To maintain
1117 // compatible behaviour and also in order to not have surprises with the
1118 // future versions, don't rely on this and still do everything ourselves.
1119 // To avoid that the messages be sent twice when the item is expanded for
1120 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1121
1122 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE);
1123 tvItem.state = 0;
1124 DoSetItem(&tvItem);
1125
1126 if ( TreeView_Expand(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item, flag) != 0 )
1127 {
1128 wxTreeEvent event(wxEVT_NULL, m_windowId);
1129 event.m_item = item;
1130
1131 bool isExpanded = IsExpanded(item);
1132
1133 event.SetEventObject(this);
1134
1135 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1136 event.SetEventType(g_events[isExpanded][TRUE]);
1137 GetEventHandler()->ProcessEvent(event);
1138
1139 event.SetEventType(g_events[isExpanded][FALSE]);
1140 GetEventHandler()->ProcessEvent(event);
1141 }
1142 //else: change didn't took place, so do nothing at all
1143}
1144
1145void wxTreeCtrl::Expand(const wxTreeItemId& item)
1146{
1147 DoExpand(item, TVE_EXPAND);
1148}
1149
1150void wxTreeCtrl::Collapse(const wxTreeItemId& item)
1151{
1152 DoExpand(item, TVE_COLLAPSE);
1153}
1154
1155void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1156{
1157 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
1158}
1159
1160void wxTreeCtrl::Toggle(const wxTreeItemId& item)
1161{
1162 DoExpand(item, TVE_TOGGLE);
1163}
1164
1165void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
1166{
1167 DoExpand(item, action);
1168}
1169
1170void wxTreeCtrl::Unselect()
1171{
1172 wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE), wxT("doesn't make sense") );
1173
1174 // just remove the selection
1175 SelectItem(wxTreeItemId((WXHTREEITEM) 0));
1176}
1177
1178void wxTreeCtrl::UnselectAll()
1179{
1180 if ( m_windowStyle & wxTR_MULTIPLE )
1181 {
1182 wxArrayTreeItemIds selections;
1183 size_t count = GetSelections(selections);
1184 for ( size_t n = 0; n < count; n++ )
1185 {
1186 SetItemCheck(selections[n], FALSE);
1187 }
1188 }
1189 else
1190 {
1191 // just remove the selection
1192 Unselect();
1193 }
1194}
1195
1196void wxTreeCtrl::SelectItem(const wxTreeItemId& item)
1197{
1198 if ( m_windowStyle & wxTR_MULTIPLE )
1199 {
1200 // selecting the item means checking it
1201 SetItemCheck(item);
1202 }
1203 else
1204 {
1205 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1206 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1207 // send them ourselves
1208
1209 wxTreeEvent event(wxEVT_NULL, m_windowId);
1210 event.m_item = item;
1211 event.SetEventObject(this);
1212
1213 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING);
1214 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
1215 {
1216 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item) )
1217 {
1218 wxLogLastError("TreeView_SelectItem");
1219 }
1220 else
1221 {
1222 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1223 (void)GetEventHandler()->ProcessEvent(event);
1224 }
1225 }
1226 //else: program vetoed the change
1227 }
1228}
1229
1230void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1231{
1232 // no error return
1233 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item);
1234}
1235
1236void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
1237{
1238 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item) )
1239 {
1240 wxLogLastError("TreeView_SelectSetFirstVisible");
1241 }
1242}
1243
1244wxTextCtrl* wxTreeCtrl::GetEditControl() const
1245{
1246 return m_textCtrl;
1247}
1248
1249void wxTreeCtrl::DeleteTextCtrl()
1250{
1251 if ( m_textCtrl )
1252 {
1253 m_textCtrl->UnsubclassWin();
1254 m_textCtrl->SetHWND(0);
1255 delete m_textCtrl;
1256 m_textCtrl = NULL;
1257 }
1258}
1259
1260wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
1261 wxClassInfo* textControlClass)
1262{
1263 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
1264
1265 HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item);
1266
1267 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1268 // returned FALSE
1269 if ( !hWnd )
1270 {
1271 return NULL;
1272 }
1273
1274 DeleteTextCtrl();
1275
1276 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
1277 m_textCtrl->SetHWND((WXHWND)hWnd);
1278 m_textCtrl->SubclassWin((WXHWND)hWnd);
1279
1280 return m_textCtrl;
1281}
1282
1283// End label editing, optionally cancelling the edit
1284void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges)
1285{
1286 TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
1287
1288 DeleteTextCtrl();
1289}
1290
1291wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
1292{
1293 TV_HITTESTINFO hitTestInfo;
1294 hitTestInfo.pt.x = (int)point.x;
1295 hitTestInfo.pt.y = (int)point.y;
1296
1297 TreeView_HitTest(GetHwnd(), &hitTestInfo);
1298
1299 flags = 0;
1300
1301 // avoid repetition
1302 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1303 flags |= wxTREE_HITTEST_##flag
1304
1305 TRANSLATE_FLAG(ABOVE);
1306 TRANSLATE_FLAG(BELOW);
1307 TRANSLATE_FLAG(NOWHERE);
1308 TRANSLATE_FLAG(ONITEMBUTTON);
1309 TRANSLATE_FLAG(ONITEMICON);
1310 TRANSLATE_FLAG(ONITEMINDENT);
1311 TRANSLATE_FLAG(ONITEMLABEL);
1312 TRANSLATE_FLAG(ONITEMRIGHT);
1313 TRANSLATE_FLAG(ONITEMSTATEICON);
1314 TRANSLATE_FLAG(TOLEFT);
1315 TRANSLATE_FLAG(TORIGHT);
1316
1317 #undef TRANSLATE_FLAG
1318
1319 return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem);
1320}
1321
1322bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
1323 wxRect& rect,
1324 bool textOnly) const
1325{
1326 RECT rc;
1327 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item,
1328 &rc, textOnly) )
1329 {
1330 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
1331
1332 return TRUE;
1333 }
1334 else
1335 {
1336 // couldn't retrieve rect: for example, item isn't visible
1337 return FALSE;
1338 }
1339}
1340
1341// ----------------------------------------------------------------------------
1342// sorting stuff
1343// ----------------------------------------------------------------------------
1344
1345static int CALLBACK TreeView_CompareCallback(wxTreeItemData *pItem1,
1346 wxTreeItemData *pItem2,
1347 wxTreeCtrl *tree)
1348{
1349 wxCHECK_MSG( pItem1 && pItem2, 0,
1350 wxT("sorting tree without data doesn't make sense") );
1351
1352 return tree->OnCompareItems(pItem1->GetId(), pItem2->GetId());
1353}
1354
1355int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1356 const wxTreeItemId& item2)
1357{
1358 return wxStrcmp(GetItemText(item1), GetItemText(item2));
1359}
1360
1361void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
1362{
1363 // rely on the fact that TreeView_SortChildren does the same thing as our
1364 // default behaviour, i.e. sorts items alphabetically and so call it
1365 // directly if we're not in derived class (much more efficient!)
1366 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
1367 {
1368 TreeView_SortChildren(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item, 0);
1369 }
1370 else
1371 {
1372 TV_SORTCB tvSort;
1373 tvSort.hParent = (HTREEITEM)(WXHTREEITEM)item;
1374 tvSort.lpfnCompare = (PFNTVCOMPARE)TreeView_CompareCallback;
1375 tvSort.lParam = (LPARAM)this;
1376 TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
1377 }
1378}
1379
1380// ----------------------------------------------------------------------------
1381// implementation
1382// ----------------------------------------------------------------------------
1383
1384bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1385{
1386 if ( cmd == EN_UPDATE )
1387 {
1388 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1389 event.SetEventObject( this );
1390 ProcessCommand(event);
1391 }
1392 else if ( cmd == EN_KILLFOCUS )
1393 {
1394 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1395 event.SetEventObject( this );
1396 ProcessCommand(event);
1397 }
1398 else
1399 {
1400 // nothing done
1401 return FALSE;
1402 }
1403
1404 // command processed
1405 return TRUE;
1406}
1407
1408// process WM_NOTIFY Windows message
1409bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1410{
1411 wxTreeEvent event(wxEVT_NULL, m_windowId);
1412 wxEventType eventType = wxEVT_NULL;
1413 NMHDR *hdr = (NMHDR *)lParam;
1414
1415 switch ( hdr->code )
1416 {
1417 case NM_RCLICK:
1418 {
1419 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
1420 return TRUE;
1421
1422 TV_HITTESTINFO tvhti;
1423 ::GetCursorPos(&(tvhti.pt));
1424 ::ScreenToClient(GetHwnd(),&(tvhti.pt));
1425 if ( TreeView_HitTest(GetHwnd(),&tvhti) )
1426 {
1427 if( tvhti.flags & TVHT_ONITEM )
1428 {
1429 event.m_item = (WXHTREEITEM) tvhti.hItem;
1430 eventType = wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
1431 }
1432 }
1433 }
1434 break;
1435
1436 case TVN_BEGINDRAG:
1437 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
1438 // fall through
1439
1440 case TVN_BEGINRDRAG:
1441 {
1442 if ( eventType == wxEVT_NULL )
1443 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
1444 //else: left drag, already set above
1445
1446 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
1447
1448 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
1449 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
1450 }
1451 break;
1452
1453 case TVN_BEGINLABELEDIT:
1454 {
1455 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
1456 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1457
1458 event.m_item = (WXHTREEITEM) info->item.hItem;
1459 event.m_label = info->item.pszText;
1460 }
1461 break;
1462
1463 case TVN_DELETEITEM:
1464 {
1465 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
1466 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
1467
1468 event.m_item = (WXHTREEITEM)tv->itemOld.hItem;
1469
1470 if ( m_hasAnyAttr )
1471 {
1472 delete (wxTreeItemAttr *)m_attrs.
1473 Delete((long)tv->itemOld.hItem);
1474 }
1475 }
1476 break;
1477
1478 case TVN_ENDLABELEDIT:
1479 {
1480 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
1481 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1482
1483 event.m_item = (WXHTREEITEM)info->item.hItem;
1484 event.m_label = info->item.pszText;
1485 if (info->item.pszText == NULL)
1486 return FALSE;
1487 break;
1488 }
1489
1490 case TVN_GETDISPINFO:
1491 eventType = wxEVT_COMMAND_TREE_GET_INFO;
1492 // fall through
1493
1494 case TVN_SETDISPINFO:
1495 {
1496 if ( eventType == wxEVT_NULL )
1497 eventType = wxEVT_COMMAND_TREE_SET_INFO;
1498 //else: get, already set above
1499
1500 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1501
1502 event.m_item = (WXHTREEITEM) info->item.hItem;
1503 break;
1504 }
1505
1506 case TVN_ITEMEXPANDING:
1507 event.m_code = FALSE;
1508 // fall through
1509
1510 case TVN_ITEMEXPANDED:
1511 {
1512 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
1513
1514 bool expand = FALSE;
1515 switch ( tv->action )
1516 {
1517 case TVE_EXPAND:
1518 expand = TRUE;
1519 break;
1520
1521 case TVE_COLLAPSE:
1522 expand = FALSE;
1523 break;
1524
1525 default:
1526 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
1527 "message"), tv->action);
1528 }
1529
1530 bool ing = ((int)hdr->code == TVN_ITEMEXPANDING);
1531 eventType = g_events[expand][ing];
1532
1533 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
1534 }
1535 break;
1536
1537 case TVN_KEYDOWN:
1538 {
1539 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
1540 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
1541
1542 event.m_code = wxCharCodeMSWToWX(info->wVKey);
1543
1544 // a separate event for this case
1545 if ( info->wVKey == VK_SPACE || info->wVKey == VK_RETURN )
1546 {
1547 wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
1548 m_windowId);
1549 event2.SetEventObject(this);
1550
1551 GetEventHandler()->ProcessEvent(event2);
1552 }
1553 }
1554 break;
1555
1556 case TVN_SELCHANGED:
1557 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
1558 // fall through
1559
1560 case TVN_SELCHANGING:
1561 {
1562 if ( eventType == wxEVT_NULL )
1563 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
1564 //else: already set above
1565
1566 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
1567
1568 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
1569 event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem;
1570 }
1571 break;
1572
1573#ifdef NM_CUSTOMDRAW
1574 case NM_CUSTOMDRAW:
1575 {
1576 LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam;
1577 NMCUSTOMDRAW& nmcd = lptvcd->nmcd;
1578 switch( nmcd.dwDrawStage )
1579 {
1580 case CDDS_PREPAINT:
1581 // if we've got any items with non standard attributes,
1582 // notify us before painting each item
1583 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
1584 : CDRF_DODEFAULT;
1585 return TRUE;
1586
1587 case CDDS_ITEMPREPAINT:
1588 {
1589 wxTreeItemAttr *attr =
1590 (wxTreeItemAttr *)m_attrs.Get(nmcd.dwItemSpec);
1591
1592 if ( !attr )
1593 {
1594 // nothing to do for this item
1595 return CDRF_DODEFAULT;
1596 }
1597
1598 HFONT hFont;
1599 wxColour colText, colBack;
1600 if ( attr->HasFont() )
1601 {
1602 wxFont font = attr->GetFont();
1603 hFont = (HFONT)font.GetResourceHandle();
1604 }
1605 else
1606 {
1607 hFont = 0;
1608 }
1609
1610 if ( attr->HasTextColour() )
1611 {
1612 colText = attr->GetTextColour();
1613 }
1614 else
1615 {
1616 colText = GetForegroundColour();
1617 }
1618
1619 // selection colours should override ours
1620 if ( nmcd.uItemState & CDIS_SELECTED )
1621 {
1622 DWORD clrBk = ::GetSysColor(COLOR_HIGHLIGHT);
1623 lptvcd->clrTextBk = clrBk;
1624
1625 // try to make the text visible
1626 lptvcd->clrText = wxColourToRGB(colText);
1627 lptvcd->clrText |= ~clrBk;
1628 lptvcd->clrText &= 0x00ffffff;
1629 }
1630 else
1631 {
1632 if ( attr->HasBackgroundColour() )
1633 {
1634 colBack = attr->GetBackgroundColour();
1635 }
1636 else
1637 {
1638 colBack = GetBackgroundColour();
1639 }
1640
1641 lptvcd->clrText = wxColourToRGB(colText);
1642 lptvcd->clrTextBk = wxColourToRGB(colBack);
1643 }
1644
1645 // note that if we wanted to set colours for
1646 // individual columns (subitems), we would have
1647 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1648 if ( hFont )
1649 {
1650 ::SelectObject(nmcd.hdc, hFont);
1651
1652 *result = CDRF_NEWFONT;
1653 }
1654 else
1655 {
1656 *result = CDRF_DODEFAULT;
1657 }
1658
1659 return TRUE;
1660 }
1661
1662 default:
1663 *result = CDRF_DODEFAULT;
1664 return TRUE;
1665 }
1666 }
1667 break;
1668#endif // NM_CUSTOMDRAW
1669
1670 default:
1671 return wxControl::MSWOnNotify(idCtrl, lParam, result);
1672 }
1673
1674 event.SetEventObject(this);
1675 event.SetEventType(eventType);
1676
1677 bool processed = GetEventHandler()->ProcessEvent(event);
1678
1679 // post processing
1680 switch ( hdr->code )
1681 {
1682 case TVN_DELETEITEM:
1683 {
1684 // NB: we might process this message using wxWindows event
1685 // tables, but due to overhead of wxWin event system we
1686 // prefer to do it here ourself (otherwise deleting a tree
1687 // with many items is just too slow)
1688 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
1689
1690 wxTreeItemId item = event.m_item;
1691 if ( HasIndirectData(item) )
1692 {
1693 wxTreeItemIndirectData *data = (wxTreeItemIndirectData *)
1694 tv->itemOld.lParam;
1695 delete data; // can't be NULL here
1696
1697 m_itemsWithIndirectData.Remove(item);
1698 }
1699 else
1700 {
1701 wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
1702 delete data; // may be NULL, ok
1703 }
1704
1705 processed = TRUE; // Make sure we don't get called twice
1706 }
1707 break;
1708
1709 case TVN_BEGINLABELEDIT:
1710 // return TRUE to cancel label editing
1711 *result = !event.IsAllowed();
1712 break;
1713
1714 case TVN_ENDLABELEDIT:
1715 // return TRUE to set the label to the new string
1716 *result = event.IsAllowed();
1717
1718 // ensure that we don't have the text ctrl which is going to be
1719 // deleted any more
1720 DeleteTextCtrl();
1721 break;
1722
1723 case TVN_SELCHANGING:
1724 case TVN_ITEMEXPANDING:
1725 // return TRUE to prevent the action from happening
1726 *result = !event.IsAllowed();
1727 break;
1728
1729 case TVN_GETDISPINFO:
1730 // NB: so far the user can't set the image himself anyhow, so do it
1731 // anyway - but this may change later
1732 if ( /* !processed && */ 1 )
1733 {
1734 wxTreeItemId item = event.m_item;
1735 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1736 if ( info->item.mask & TVIF_IMAGE )
1737 {
1738 info->item.iImage =
1739 DoGetItemImageFromData
1740 (
1741 item,
1742 IsExpanded(item) ? wxTreeItemIcon_Expanded
1743 : wxTreeItemIcon_Normal
1744 );
1745 }
1746 if ( info->item.mask & TVIF_SELECTEDIMAGE )
1747 {
1748 info->item.iSelectedImage =
1749 DoGetItemImageFromData
1750 (
1751 item,
1752 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
1753 : wxTreeItemIcon_Selected
1754 );
1755 }
1756 }
1757 break;
1758
1759 //default:
1760 // for the other messages the return value is ignored and there is
1761 // nothing special to do
1762 }
1763
1764 return processed;
1765}
1766
1767// ----------------------------------------------------------------------------
1768// Tree event
1769// ----------------------------------------------------------------------------
1770
1771IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
1772
1773wxTreeEvent::wxTreeEvent(wxEventType commandType, int id)
1774 : wxNotifyEvent(commandType, id)
1775{
1776 m_code = 0;
1777 m_itemOld = 0;
1778}
1779
1780#endif // __WIN95__
1781