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