]> git.saurik.com Git - wxWidgets.git/blame - src/msw/treectrl.cpp
Applied Diana's font patch II
[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
ed8f12be 318#if !defined( __GNUWIN32__ ) && !defined( __BORLANDC__ ) && !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
2996bcde 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;
d220ae32 726 return SendMessage(GetHwnd(), TVM_GETITEMRECT, FALSE, (LPARAM)&rect) != 0;
06e38c8e 727
2bda0e17
KB
728}
729
08b7c251 730bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
2bda0e17 731{
08b7c251
VZ
732 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
733 DoGetItem(&tvItem);
2bda0e17 734
08b7c251 735 return tvItem.cChildren != 0;
2bda0e17
KB
736}
737
08b7c251 738bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
2bda0e17 739{
08b7c251
VZ
740 // probably not a good idea to put it here
741 //wxASSERT( ItemHasChildren(item) );
2bda0e17 742
08b7c251
VZ
743 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
744 DoGetItem(&tvItem);
2bda0e17 745
08b7c251 746 return (tvItem.state & TVIS_EXPANDED) != 0;
2bda0e17
KB
747}
748
08b7c251 749bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
2bda0e17 750{
08b7c251
VZ
751 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
752 DoGetItem(&tvItem);
2bda0e17 753
08b7c251 754 return (tvItem.state & TVIS_SELECTED) != 0;
2bda0e17
KB
755}
756
add28c55
VZ
757bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
758{
759 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
760 DoGetItem(&tvItem);
761
762 return (tvItem.state & TVIS_BOLD) != 0;
763}
764
08b7c251
VZ
765// ----------------------------------------------------------------------------
766// navigation
767// ----------------------------------------------------------------------------
2bda0e17 768
08b7c251
VZ
769wxTreeItemId wxTreeCtrl::GetRootItem() const
770{
d220ae32 771 return wxTreeItemId((WXHTREEITEM) TreeView_GetRoot(GetHwnd()));
08b7c251 772}
2bda0e17 773
08b7c251
VZ
774wxTreeItemId wxTreeCtrl::GetSelection() const
775{
9dfbf520 776 wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), (WXHTREEITEM)0,
223d09f6 777 wxT("this only works with single selection controls") );
9dfbf520 778
d220ae32 779 return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(GetHwnd()));
2bda0e17
KB
780}
781
08b7c251 782wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
2bda0e17 783{
d220ae32 784 return wxTreeItemId((WXHTREEITEM) TreeView_GetParent(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
08b7c251 785}
2bda0e17 786
08b7c251 787wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
06e38c8e 788 long& _cookie) const
08b7c251
VZ
789{
790 // remember the last child returned in 'cookie'
d220ae32 791 _cookie = (long)TreeView_GetChild(GetHwnd(), (HTREEITEM) (WXHTREEITEM)item);
2bda0e17 792
06e38c8e 793 return wxTreeItemId((WXHTREEITEM)_cookie);
2bda0e17
KB
794}
795
08b7c251 796wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
06e38c8e 797 long& _cookie) const
2bda0e17 798{
d220ae32 799 wxTreeItemId l = wxTreeItemId((WXHTREEITEM)TreeView_GetNextSibling(GetHwnd(),
23fd5130
VZ
800 (HTREEITEM)(WXHTREEITEM)_cookie));
801 _cookie = (long)l;
802
2e5dddb0 803 return l;
08b7c251 804}
2bda0e17 805
978f38c2
VZ
806wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
807{
808 // can this be done more efficiently?
809 long cookie;
810
811 wxTreeItemId childLast,
2165ad93 812 child = GetFirstChild(item, cookie);
978f38c2
VZ
813 while ( child.IsOk() )
814 {
815 childLast = child;
2165ad93 816 child = GetNextChild(item, cookie);
978f38c2
VZ
817 }
818
819 return childLast;
820}
821
08b7c251
VZ
822wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
823{
d220ae32 824 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
2bda0e17
KB
825}
826
08b7c251 827wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
2bda0e17 828{
d220ae32 829 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
2bda0e17
KB
830}
831
08b7c251 832wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
2bda0e17 833{
d220ae32 834 return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(GetHwnd()));
2bda0e17
KB
835}
836
08b7c251 837wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
2bda0e17 838{
223d09f6 839 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() "
837e5743 840 "for must be visible itself!"));
02ce7b72 841
d220ae32 842 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
08b7c251 843}
02ce7b72 844
08b7c251
VZ
845wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
846{
223d09f6 847 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() "
837e5743 848 "for must be visible itself!"));
02ce7b72 849
d220ae32 850 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
08b7c251 851}
02ce7b72 852
9dfbf520
VZ
853// ----------------------------------------------------------------------------
854// multiple selections emulation
855// ----------------------------------------------------------------------------
856
857bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const
858{
859 // receive the desired information.
860 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
861 DoGetItem(&tvItem);
862
863 // state image indices are 1 based
864 return ((tvItem.state >> 12) - 1) == 1;
865}
866
867void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check)
868{
869 // receive the desired information.
870 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
871
872 // state images are one-based
873 tvItem.state = (check ? 2 : 1) << 12;
874
875 DoSetItem(&tvItem);
876}
877
33961d59
RR
878size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
879{
880 TraverseSelections selector(this, selections);
9dfbf520
VZ
881
882 return selections.GetCount();
883}
884
08b7c251
VZ
885// ----------------------------------------------------------------------------
886// Usual operations
887// ----------------------------------------------------------------------------
02ce7b72 888
08b7c251
VZ
889wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
890 wxTreeItemId hInsertAfter,
891 const wxString& text,
892 int image, int selectedImage,
893 wxTreeItemData *data)
894{
895 TV_INSERTSTRUCT tvIns;
06e38c8e
JS
896 tvIns.hParent = (HTREEITEM) (WXHTREEITEM)parent;
897 tvIns.hInsertAfter = (HTREEITEM) (WXHTREEITEM) hInsertAfter;
58a8ab88 898
74b31181
VZ
899 // this is how we insert the item as the first child: supply a NULL
900 // hInsertAfter
901 if ( !tvIns.hInsertAfter )
58a8ab88
JS
902 {
903 tvIns.hInsertAfter = TVI_FIRST;
904 }
905
08b7c251
VZ
906 UINT mask = 0;
907 if ( !text.IsEmpty() )
908 {
909 mask |= TVIF_TEXT;
837e5743 910 tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok
08b7c251 911 }
02ce7b72 912
08b7c251
VZ
913 if ( image != -1 )
914 {
915 mask |= TVIF_IMAGE;
916 tvIns.item.iImage = image;
3a5a2f56 917
6b037754 918 if ( selectedImage == -1 )
3a5a2f56
VZ
919 {
920 // take the same image for selected icon if not specified
921 selectedImage = image;
922 }
08b7c251 923 }
02ce7b72 924
08b7c251
VZ
925 if ( selectedImage != -1 )
926 {
927 mask |= TVIF_SELECTEDIMAGE;
928 tvIns.item.iSelectedImage = selectedImage;
929 }
02ce7b72 930
08b7c251
VZ
931 if ( data != NULL )
932 {
933 mask |= TVIF_PARAM;
934 tvIns.item.lParam = (LPARAM)data;
935 }
02ce7b72 936
08b7c251 937 tvIns.item.mask = mask;
02ce7b72 938
d220ae32 939 HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns);
08b7c251
VZ
940 if ( id == 0 )
941 {
942 wxLogLastError("TreeView_InsertItem");
943 }
02ce7b72 944
fd3f686c
VZ
945 if ( data != NULL )
946 {
947 // associate the application tree item with Win32 tree item handle
948 data->SetId((WXHTREEITEM)id);
949 }
950
06e38c8e 951 return wxTreeItemId((WXHTREEITEM)id);
2bda0e17
KB
952}
953
08b7c251
VZ
954// for compatibility only
955wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
956 const wxString& text,
957 int image, int selImage,
958 long insertAfter)
2bda0e17 959{
06e38c8e 960 return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text,
08b7c251 961 image, selImage, NULL);
2bda0e17
KB
962}
963
08b7c251
VZ
964wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
965 int image, int selectedImage,
966 wxTreeItemData *data)
2bda0e17 967{
06e38c8e 968 return DoInsertItem(wxTreeItemId((WXHTREEITEM) 0), (WXHTREEITEM) 0,
08b7c251 969 text, image, selectedImage, data);
2bda0e17
KB
970}
971
08b7c251
VZ
972wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
973 const wxString& text,
974 int image, int selectedImage,
975 wxTreeItemData *data)
2bda0e17 976{
06e38c8e 977 return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST,
08b7c251 978 text, image, selectedImage, data);
2bda0e17
KB
979}
980
08b7c251
VZ
981wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
982 const wxTreeItemId& idPrevious,
983 const wxString& text,
984 int image, int selectedImage,
985 wxTreeItemData *data)
2bda0e17 986{
08b7c251 987 return DoInsertItem(parent, idPrevious, text, image, selectedImage, data);
2bda0e17
KB
988}
989
08b7c251
VZ
990wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
991 const wxString& text,
992 int image, int selectedImage,
993 wxTreeItemData *data)
2bda0e17 994{
06e38c8e 995 return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST,
08b7c251 996 text, image, selectedImage, data);
2bda0e17
KB
997}
998
08b7c251 999void wxTreeCtrl::Delete(const wxTreeItemId& item)
2bda0e17 1000{
d220ae32 1001 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item) )
bbcdf8bc 1002 {
08b7c251 1003 wxLogLastError("TreeView_DeleteItem");
bbcdf8bc 1004 }
bbcdf8bc
JS
1005}
1006
23fd5130
VZ
1007// delete all children (but don't delete the item itself)
1008void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
1009{
1010 long cookie;
1011
1012 wxArrayLong children;
1013 wxTreeItemId child = GetFirstChild(item, cookie);
1014 while ( child.IsOk() )
1015 {
1016 children.Add((long)(WXHTREEITEM)child);
1017
1018 child = GetNextChild(item, cookie);
1019 }
1020
1021 size_t nCount = children.Count();
1022 for ( size_t n = 0; n < nCount; n++ )
1023 {
d220ae32 1024 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)children[n]) )
23fd5130
VZ
1025 {
1026 wxLogLastError("TreeView_DeleteItem");
1027 }
1028 }
1029}
1030
08b7c251 1031void wxTreeCtrl::DeleteAllItems()
bbcdf8bc 1032{
d220ae32 1033 if ( !TreeView_DeleteAllItems(GetHwnd()) )
bbcdf8bc 1034 {
08b7c251 1035 wxLogLastError("TreeView_DeleteAllItems");
bbcdf8bc 1036 }
2bda0e17
KB
1037}
1038
08b7c251 1039void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
2bda0e17 1040{
dd3646fd
VZ
1041 wxASSERT_MSG( flag == TVE_COLLAPSE ||
1042 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
1043 flag == TVE_EXPAND ||
1044 flag == TVE_TOGGLE,
223d09f6 1045 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
08b7c251
VZ
1046
1047 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
d220ae32
VZ
1048 // emulate them. This behaviour has changed slightly with comctl32.dll
1049 // v 4.70 - now it does send them but only the first time. To maintain
1050 // compatible behaviour and also in order to not have surprises with the
1051 // future versions, don't rely on this and still do everything ourselves.
1052 // To avoid that the messages be sent twice when the item is expanded for
1053 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1054
1055 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE);
1056 tvItem.state = 0;
1057 DoSetItem(&tvItem);
1058
1059 if ( TreeView_Expand(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item, flag) != 0 )
08b7c251
VZ
1060 {
1061 wxTreeEvent event(wxEVT_NULL, m_windowId);
1062 event.m_item = item;
1063
1064 bool isExpanded = IsExpanded(item);
2bda0e17 1065
08b7c251 1066 event.SetEventObject(this);
2bda0e17 1067
d220ae32 1068 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
08b7c251
VZ
1069 event.SetEventType(g_events[isExpanded][TRUE]);
1070 GetEventHandler()->ProcessEvent(event);
2bda0e17 1071
08b7c251
VZ
1072 event.SetEventType(g_events[isExpanded][FALSE]);
1073 GetEventHandler()->ProcessEvent(event);
1074 }
d220ae32 1075 //else: change didn't took place, so do nothing at all
2bda0e17
KB
1076}
1077
08b7c251 1078void wxTreeCtrl::Expand(const wxTreeItemId& item)
2bda0e17 1079{
08b7c251 1080 DoExpand(item, TVE_EXPAND);
2bda0e17 1081}
2bda0e17 1082
08b7c251 1083void wxTreeCtrl::Collapse(const wxTreeItemId& item)
2bda0e17 1084{
08b7c251 1085 DoExpand(item, TVE_COLLAPSE);
2bda0e17
KB
1086}
1087
08b7c251 1088void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
2bda0e17 1089{
dd3646fd 1090 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
2bda0e17
KB
1091}
1092
08b7c251 1093void wxTreeCtrl::Toggle(const wxTreeItemId& item)
2bda0e17 1094{
08b7c251 1095 DoExpand(item, TVE_TOGGLE);
2bda0e17
KB
1096}
1097
42c5812d
UU
1098void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
1099{
9dfbf520 1100 DoExpand(item, action);
42c5812d
UU
1101}
1102
08b7c251 1103void wxTreeCtrl::Unselect()
2bda0e17 1104{
223d09f6 1105 wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE), wxT("doesn't make sense") );
9dfbf520
VZ
1106
1107 // just remove the selection
06e38c8e 1108 SelectItem(wxTreeItemId((WXHTREEITEM) 0));
08b7c251 1109}
02ce7b72 1110
9dfbf520 1111void wxTreeCtrl::UnselectAll()
08b7c251 1112{
9dfbf520 1113 if ( m_windowStyle & wxTR_MULTIPLE )
2bda0e17 1114 {
9dfbf520
VZ
1115 wxArrayTreeItemIds selections;
1116 size_t count = GetSelections(selections);
1117 for ( size_t n = 0; n < count; n++ )
d220ae32 1118 {
9dfbf520 1119 SetItemCheck(selections[n], FALSE);
d220ae32 1120 }
9dfbf520
VZ
1121 }
1122 else
1123 {
1124 // just remove the selection
1125 Unselect();
1126 }
1127}
1128
1129void wxTreeCtrl::SelectItem(const wxTreeItemId& item)
1130{
1131 if ( m_windowStyle & wxTR_MULTIPLE )
1132 {
1133 // selecting the item means checking it
1134 SetItemCheck(item);
1135 }
1136 else
1137 {
1138 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1139 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1140 // send them ourselves
1141
1142 wxTreeEvent event(wxEVT_NULL, m_windowId);
1143 event.m_item = item;
1144 event.SetEventObject(this);
1145
1146 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING);
1147 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
d220ae32 1148 {
9dfbf520
VZ
1149 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item) )
1150 {
1151 wxLogLastError("TreeView_SelectItem");
1152 }
1153 else
1154 {
1155 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1156 (void)GetEventHandler()->ProcessEvent(event);
1157 }
d220ae32 1158 }
9dfbf520 1159 //else: program vetoed the change
2bda0e17 1160 }
08b7c251 1161}
2bda0e17 1162
08b7c251
VZ
1163void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1164{
1165 // no error return
d220ae32 1166 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item);
08b7c251
VZ
1167}
1168
1169void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
1170{
d220ae32 1171 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item) )
2bda0e17 1172 {
08b7c251 1173 wxLogLastError("TreeView_SelectSetFirstVisible");
2bda0e17 1174 }
08b7c251
VZ
1175}
1176
1177wxTextCtrl* wxTreeCtrl::GetEditControl() const
1178{
1179 return m_textCtrl;
1180}
1181
1182void wxTreeCtrl::DeleteTextCtrl()
1183{
1184 if ( m_textCtrl )
2bda0e17 1185 {
08b7c251
VZ
1186 m_textCtrl->UnsubclassWin();
1187 m_textCtrl->SetHWND(0);
1188 delete m_textCtrl;
1189 m_textCtrl = NULL;
2bda0e17 1190 }
08b7c251 1191}
2bda0e17 1192
08b7c251
VZ
1193wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
1194 wxClassInfo* textControlClass)
1195{
1196 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
1197
d220ae32 1198 HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item);
2bda0e17 1199
5ea47806
VZ
1200 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1201 // returned FALSE
1202 if ( !hWnd )
1203 {
1204 return NULL;
1205 }
2bda0e17 1206
08b7c251 1207 DeleteTextCtrl();
2bda0e17 1208
08b7c251
VZ
1209 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
1210 m_textCtrl->SetHWND((WXHWND)hWnd);
1211 m_textCtrl->SubclassWin((WXHWND)hWnd);
2bda0e17 1212
08b7c251 1213 return m_textCtrl;
2bda0e17
KB
1214}
1215
08b7c251
VZ
1216// End label editing, optionally cancelling the edit
1217void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges)
2bda0e17 1218{
d220ae32 1219 TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
08b7c251
VZ
1220
1221 DeleteTextCtrl();
2bda0e17
KB
1222}
1223
08b7c251 1224wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
2bda0e17 1225{
08b7c251
VZ
1226 TV_HITTESTINFO hitTestInfo;
1227 hitTestInfo.pt.x = (int)point.x;
1228 hitTestInfo.pt.y = (int)point.y;
2bda0e17 1229
d220ae32 1230 TreeView_HitTest(GetHwnd(), &hitTestInfo);
2bda0e17 1231
08b7c251
VZ
1232 flags = 0;
1233
1234 // avoid repetition
1235 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1236 flags |= wxTREE_HITTEST_##flag
1237
1238 TRANSLATE_FLAG(ABOVE);
1239 TRANSLATE_FLAG(BELOW);
1240 TRANSLATE_FLAG(NOWHERE);
1241 TRANSLATE_FLAG(ONITEMBUTTON);
1242 TRANSLATE_FLAG(ONITEMICON);
1243 TRANSLATE_FLAG(ONITEMINDENT);
1244 TRANSLATE_FLAG(ONITEMLABEL);
1245 TRANSLATE_FLAG(ONITEMRIGHT);
1246 TRANSLATE_FLAG(ONITEMSTATEICON);
1247 TRANSLATE_FLAG(TOLEFT);
1248 TRANSLATE_FLAG(TORIGHT);
2bda0e17 1249
08b7c251
VZ
1250 #undef TRANSLATE_FLAG
1251
06e38c8e 1252 return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem);
08b7c251
VZ
1253}
1254
f7c832a7
VZ
1255bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
1256 wxRect& rect,
1257 bool textOnly) const
1258{
1259 RECT rc;
d220ae32 1260 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item,
f7c832a7
VZ
1261 &rc, textOnly) )
1262 {
1263 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
1264
1265 return TRUE;
1266 }
1267 else
1268 {
1269 // couldn't retrieve rect: for example, item isn't visible
1270 return FALSE;
1271 }
1272}
1273
23fd5130
VZ
1274// ----------------------------------------------------------------------------
1275// sorting stuff
1276// ----------------------------------------------------------------------------
f7c832a7 1277
23fd5130
VZ
1278static int CALLBACK TreeView_CompareCallback(wxTreeItemData *pItem1,
1279 wxTreeItemData *pItem2,
1280 wxTreeCtrl *tree)
1281{
096c9f9b 1282 wxCHECK_MSG( pItem1 && pItem2, 0,
223d09f6 1283 wxT("sorting tree without data doesn't make sense") );
096c9f9b 1284
23fd5130
VZ
1285 return tree->OnCompareItems(pItem1->GetId(), pItem2->GetId());
1286}
1287
95aabccc
VZ
1288int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1289 const wxTreeItemId& item2)
08b7c251 1290{
837e5743 1291 return wxStrcmp(GetItemText(item1), GetItemText(item2));
95aabccc
VZ
1292}
1293
1294void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
1295{
1296 // rely on the fact that TreeView_SortChildren does the same thing as our
23fd5130
VZ
1297 // default behaviour, i.e. sorts items alphabetically and so call it
1298 // directly if we're not in derived class (much more efficient!)
1299 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
2bda0e17 1300 {
d220ae32 1301 TreeView_SortChildren(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item, 0);
2bda0e17 1302 }
08b7c251 1303 else
2bda0e17 1304 {
62448488 1305 TV_SORTCB tvSort;
23fd5130
VZ
1306 tvSort.hParent = (HTREEITEM)(WXHTREEITEM)item;
1307 tvSort.lpfnCompare = (PFNTVCOMPARE)TreeView_CompareCallback;
1308 tvSort.lParam = (LPARAM)this;
d220ae32 1309 TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
2bda0e17 1310 }
08b7c251 1311}
2bda0e17 1312
08b7c251
VZ
1313// ----------------------------------------------------------------------------
1314// implementation
1315// ----------------------------------------------------------------------------
2bda0e17 1316
08b7c251
VZ
1317bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1318{
1319 if ( cmd == EN_UPDATE )
2bda0e17 1320 {
08b7c251
VZ
1321 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1322 event.SetEventObject( this );
1323 ProcessCommand(event);
2bda0e17 1324 }
08b7c251 1325 else if ( cmd == EN_KILLFOCUS )
2bda0e17 1326 {
08b7c251
VZ
1327 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1328 event.SetEventObject( this );
1329 ProcessCommand(event);
2bda0e17 1330 }
08b7c251 1331 else
2bda0e17 1332 {
08b7c251
VZ
1333 // nothing done
1334 return FALSE;
2bda0e17 1335 }
08b7c251
VZ
1336
1337 // command processed
1338 return TRUE;
1339}
1340
1341// process WM_NOTIFY Windows message
a23fd0e1 1342bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
08b7c251
VZ
1343{
1344 wxTreeEvent event(wxEVT_NULL, m_windowId);
1345 wxEventType eventType = wxEVT_NULL;
1346 NMHDR *hdr = (NMHDR *)lParam;
1347
1348 switch ( hdr->code )
2bda0e17 1349 {
84a6b859 1350 case NM_RCLICK:
52f13e49
VZ
1351 {
1352 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
1353 return TRUE;
1354
1355 TV_HITTESTINFO tvhti;
1356 ::GetCursorPos(&(tvhti.pt));
1357 ::ScreenToClient(GetHwnd(),&(tvhti.pt));
1358 if ( TreeView_HitTest(GetHwnd(),&tvhti) )
1359 {
1360 if( tvhti.flags & TVHT_ONITEM )
1361 {
1362 event.m_item = (WXHTREEITEM) tvhti.hItem;
1363 eventType=wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
1364 }
1365 }
1366 break;
1367 }
1368
08b7c251
VZ
1369 case TVN_BEGINDRAG:
1370 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
1371 // fall through
1372
1373 case TVN_BEGINRDRAG:
1374 {
1375 if ( eventType == wxEVT_NULL )
1376 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
1377 //else: left drag, already set above
1378
1379 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
1380
06e38c8e 1381 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
08b7c251
VZ
1382 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
1383 break;
1384 }
1385
1386 case TVN_BEGINLABELEDIT:
1387 {
1388 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
1389 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1390
06e38c8e 1391 event.m_item = (WXHTREEITEM) info->item.hItem;
5ea47806 1392 event.m_label = info->item.pszText;
08b7c251
VZ
1393 break;
1394 }
1395
1396 case TVN_DELETEITEM:
1397 {
1398 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
1399 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
1400
06e38c8e 1401 event.m_item = (WXHTREEITEM) tv->itemOld.hItem;
08b7c251
VZ
1402 break;
1403 }
1404
1405 case TVN_ENDLABELEDIT:
1406 {
1407 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
1408 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1409
5ea47806
VZ
1410 event.m_item = (WXHTREEITEM)info->item.hItem;
1411 event.m_label = info->item.pszText;
08b7c251
VZ
1412 break;
1413 }
1414
1415 case TVN_GETDISPINFO:
1416 eventType = wxEVT_COMMAND_TREE_GET_INFO;
1417 // fall through
1418
1419 case TVN_SETDISPINFO:
1420 {
1421 if ( eventType == wxEVT_NULL )
1422 eventType = wxEVT_COMMAND_TREE_SET_INFO;
1423 //else: get, already set above
1424
1425 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1426
06e38c8e 1427 event.m_item = (WXHTREEITEM) info->item.hItem;
08b7c251
VZ
1428 break;
1429 }
1430
1431 case TVN_ITEMEXPANDING:
1432 event.m_code = FALSE;
1433 // fall through
1434
1435 case TVN_ITEMEXPANDED:
1436 {
1437 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
1438
1439 bool expand = FALSE;
1440 switch ( tv->action )
1441 {
1442 case TVE_EXPAND:
1443 expand = TRUE;
1444 break;
1445
1446 case TVE_COLLAPSE:
1447 expand = FALSE;
1448 break;
1449
1450 default:
223d09f6 1451 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
837e5743 1452 "message"), tv->action);
08b7c251
VZ
1453 }
1454
a17e237f 1455 bool ing = ((int)hdr->code == TVN_ITEMEXPANDING);
08b7c251
VZ
1456 eventType = g_events[expand][ing];
1457
06e38c8e 1458 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
08b7c251
VZ
1459 break;
1460 }
1461
1462 case TVN_KEYDOWN:
1463 {
1464 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
1465 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
1466
1467 event.m_code = wxCharCodeMSWToWX(info->wVKey);
23fd5130
VZ
1468
1469 // a separate event for this case
1470 if ( info->wVKey == VK_SPACE || info->wVKey == VK_RETURN )
1471 {
1472 wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
1473 m_windowId);
1474 event2.SetEventObject(this);
1475
1476 GetEventHandler()->ProcessEvent(event2);
1477 }
08b7c251
VZ
1478 break;
1479 }
1480
1481 case TVN_SELCHANGED:
1482 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
1483 // fall through
1484
1485 case TVN_SELCHANGING:
1486 {
1487 if ( eventType == wxEVT_NULL )
1488 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
1489 //else: already set above
1490
1491 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
1492
06e38c8e
JS
1493 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
1494 event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem;
08b7c251
VZ
1495 break;
1496 }
1497
1498 default:
a23fd0e1 1499 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2bda0e17 1500 }
08b7c251
VZ
1501
1502 event.SetEventObject(this);
1503 event.SetEventType(eventType);
1504
fd3f686c 1505 bool processed = GetEventHandler()->ProcessEvent(event);
08b7c251
VZ
1506
1507 // post processing
5ea47806 1508 switch ( hdr->code )
2bda0e17 1509 {
5ea47806
VZ
1510 case TVN_DELETEITEM:
1511 {
1512 // NB: we might process this message using wxWindows event
1513 // tables, but due to overhead of wxWin event system we
1514 // prefer to do it here ourself (otherwise deleting a tree
1515 // with many items is just too slow)
1516 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
74b31181
VZ
1517
1518 wxTreeItemId item = event.m_item;
1519 if ( HasIndirectData(item) )
1520 {
1521 wxTreeItemIndirectData *data = (wxTreeItemIndirectData *)
1522 tv->itemOld.lParam;
1523 delete data; // can't be NULL here
1524
1525 m_itemsWithIndirectData.Remove(item);
1526 }
1527 else
1528 {
1529 wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
1530 delete data; // may be NULL, ok
1531 }
08b7c251 1532
5ea47806
VZ
1533 processed = TRUE; // Make sure we don't get called twice
1534 }
1535 break;
1536
1537 case TVN_BEGINLABELEDIT:
1538 // return TRUE to cancel label editing
1539 *result = !event.IsAllowed();
1540 break;
1541
1542 case TVN_ENDLABELEDIT:
1543 // return TRUE to set the label to the new string
1544 *result = event.IsAllowed();
1545
1546 // ensure that we don't have the text ctrl which is going to be
1547 // deleted any more
1548 DeleteTextCtrl();
1549 break;
1550
1551 case TVN_SELCHANGING:
1552 case TVN_ITEMEXPANDING:
1553 // return TRUE to prevent the action from happening
1554 *result = !event.IsAllowed();
1555 break;
1556
74b31181
VZ
1557 case TVN_GETDISPINFO:
1558 // NB: so far the user can't set the image himself anyhow, so do it
1559 // anyway - but this may change later
1560 if ( /* !processed && */ 1 )
1561 {
1562 wxTreeItemId item = event.m_item;
1563 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1564 if ( info->item.mask & TVIF_IMAGE )
1565 {
1566 info->item.iImage =
1567 DoGetItemImageFromData
1568 (
1569 item,
1570 IsExpanded(item) ? wxTreeItemIcon_Expanded
1571 : wxTreeItemIcon_Normal
1572 );
1573 }
1574 if ( info->item.mask & TVIF_SELECTEDIMAGE )
1575 {
1576 info->item.iSelectedImage =
1577 DoGetItemImageFromData
1578 (
1579 item,
1580 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
1581 : wxTreeItemIcon_Selected
1582 );
1583 }
1584 }
1585 break;
1586
5ea47806
VZ
1587 //default:
1588 // for the other messages the return value is ignored and there is
1589 // nothing special to do
1590 }
fd3f686c
VZ
1591
1592 return processed;
2bda0e17
KB
1593}
1594
08b7c251 1595// ----------------------------------------------------------------------------
2bda0e17 1596// Tree event
08b7c251
VZ
1597// ----------------------------------------------------------------------------
1598
92976ab6 1599IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
2bda0e17 1600
08b7c251 1601wxTreeEvent::wxTreeEvent(wxEventType commandType, int id)
fd3f686c 1602 : wxNotifyEvent(commandType, id)
2bda0e17 1603{
08b7c251
VZ
1604 m_code = 0;
1605 m_itemOld = 0;
2bda0e17
KB
1606}
1607
08b7c251 1608#endif // __WIN95__
2bda0e17 1609