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