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