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