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