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