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