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