]> git.saurik.com Git - wxWidgets.git/blame - src/msw/treectrl.cpp
fixed translatable strings handling in XRC
[wxWidgets.git] / src / msw / treectrl.cpp
CommitLineData
b823f5a1 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: src/msw/treectrl.cpp
b823f5a1
JS
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// ----------------------------------------------------------------------------
1e6feb95 19
2bda0e17 20#ifdef __GNUG__
08b7c251 21 #pragma implementation "treectrl.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
08b7c251 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_TREECTRL
32
0c589ad0
BM
33#include "wx/msw/private.h"
34
1e6feb95
VZ
35// Set this to 1 to be _absolutely_ sure that repainting will work for all
36// comctl32.dll versions
f6bcfd97
BP
37#define wxUSE_COMCTL32_SAFELY 0
38
0c589ad0
BM
39// Mingw32 is a bit mental even though this is done in winundef
40#ifdef GetFirstChild
d220ae32 41 #undef GetFirstChild
0c589ad0 42#endif
d220ae32 43
0c589ad0 44#ifdef GetNextSibling
d220ae32 45 #undef GetNextSibling
2bda0e17
KB
46#endif
47
2bda0e17
KB
48#if defined(__WIN95__)
49
c6f4913a 50#include "wx/app.h"
08b7c251 51#include "wx/log.h"
ce3ed50d 52#include "wx/dynarray.h"
08b7c251 53#include "wx/imaglist.h"
77cff606 54#include "wx/settings.h"
484523cf 55#include "wx/msw/treectrl.h"
23f681ec
VZ
56#include "wx/msw/dragimag.h"
57
c42404a5
VZ
58#ifdef __GNUWIN32_OLD__
59 #include "wx/msw/gnuwin32/extra.h"
65fd5cb0 60#endif
9a05fd8d 61
ae090fdb 62#if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
08b7c251 63 #include <commctrl.h>
2bda0e17
KB
64#endif
65
66// Bug in headers, sometimes
67#ifndef TVIS_FOCUSED
08b7c251 68 #define TVIS_FOCUSED 0x0001
2bda0e17
KB
69#endif
70
bb448552
VZ
71#ifndef TV_FIRST
72 #define TV_FIRST 0x1100
73#endif
74
b94ae1ea
VZ
75#ifndef TVS_CHECKBOXES
76 #define TVS_CHECKBOXES 0x0100
77#endif
78
5ece068d
VZ
79#ifndef TVS_FULLROWSELECT
80 #define TVS_FULLROWSELECT 0x1000
81#endif
82
bb448552
VZ
83// old headers might miss these messages (comctl32.dll 4.71+ only)
84#ifndef TVM_SETBKCOLOR
85 #define TVM_SETBKCOLOR (TV_FIRST + 29)
86 #define TVM_SETTEXTCOLOR (TV_FIRST + 30)
87#endif
88
3f7bc32b
VZ
89// a macro to hide the ugliness of nested casts
90#define HITEM(item) (HTREEITEM)(WXHTREEITEM)(item)
91
92// the native control doesn't support multiple selections under MSW and we
93// have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
94// checkboxes be the selection status (checked == selected) or by really
95// emulating everything, i.e. intercepting mouse and key events &c. The first
96// approach is much easier but doesn't work with comctl32.dll < 4.71 and also
97// looks quite ugly.
98#define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
99
3f7bc32b
VZ
100// ----------------------------------------------------------------------------
101// private functions
102// ----------------------------------------------------------------------------
103
104// wrapper for TreeView_HitTest
105static HTREEITEM GetItemFromPoint(HWND hwndTV, int x, int y)
106{
107 TV_HITTESTINFO tvht;
108 tvht.pt.x = x;
109 tvht.pt.y = y;
110
3f7bc32b
VZ
111 return (HTREEITEM)TreeView_HitTest(hwndTV, &tvht);
112}
113
114#if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
115
116// wrappers for TreeView_GetItem/TreeView_SetItem
117static bool IsItemSelected(HWND hwndTV, HTREEITEM hItem)
118{
119 TV_ITEM tvi;
120 tvi.mask = TVIF_STATE | TVIF_HANDLE;
121 tvi.stateMask = TVIS_SELECTED;
122 tvi.hItem = hItem;
123
124 if ( !TreeView_GetItem(hwndTV, &tvi) )
125 {
f6bcfd97 126 wxLogLastError(wxT("TreeView_GetItem"));
3f7bc32b
VZ
127 }
128
129 return (tvi.state & TVIS_SELECTED) != 0;
130}
131
132static void SelectItem(HWND hwndTV, HTREEITEM hItem, bool select = TRUE)
133{
134 TV_ITEM tvi;
135 tvi.mask = TVIF_STATE | TVIF_HANDLE;
136 tvi.stateMask = TVIS_SELECTED;
137 tvi.state = select ? TVIS_SELECTED : 0;
138 tvi.hItem = hItem;
139
140 if ( TreeView_SetItem(hwndTV, &tvi) == -1 )
141 {
f6bcfd97 142 wxLogLastError(wxT("TreeView_SetItem"));
3f7bc32b
VZ
143 }
144}
145
146static inline void UnselectItem(HWND hwndTV, HTREEITEM htItem)
147{
148 SelectItem(hwndTV, htItem, FALSE);
149}
150
151static inline void ToggleItemSelection(HWND hwndTV, HTREEITEM htItem)
152{
153 SelectItem(hwndTV, htItem, !IsItemSelected(hwndTV, htItem));
154}
155
156// helper function which selects all items in a range and, optionally,
157// unselects all others
158static void SelectRange(HWND hwndTV,
159 HTREEITEM htFirst,
160 HTREEITEM htLast,
161 bool unselectOthers = TRUE)
162{
163 // find the first (or last) item and select it
164 bool cont = TRUE;
2c8e4738 165 HTREEITEM htItem = (HTREEITEM)TreeView_GetRoot(hwndTV);
3f7bc32b
VZ
166 while ( htItem && cont )
167 {
168 if ( (htItem == htFirst) || (htItem == htLast) )
169 {
170 if ( !IsItemSelected(hwndTV, htItem) )
171 {
172 SelectItem(hwndTV, htItem);
173 }
174
175 cont = FALSE;
176 }
177 else
178 {
179 if ( unselectOthers && IsItemSelected(hwndTV, htItem) )
180 {
181 UnselectItem(hwndTV, htItem);
182 }
183 }
184
2c8e4738 185 htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
3f7bc32b
VZ
186 }
187
188 // select the items in range
189 cont = htFirst != htLast;
190 while ( htItem && cont )
191 {
192 if ( !IsItemSelected(hwndTV, htItem) )
193 {
194 SelectItem(hwndTV, htItem);
195 }
196
197 cont = (htItem != htFirst) && (htItem != htLast);
198
2c8e4738 199 htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
3f7bc32b
VZ
200 }
201
202 // unselect the rest
203 if ( unselectOthers )
204 {
205 while ( htItem )
206 {
207 if ( IsItemSelected(hwndTV, htItem) )
208 {
209 UnselectItem(hwndTV, htItem);
210 }
211
2c8e4738 212 htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
3f7bc32b
VZ
213 }
214 }
215
216 // seems to be necessary - otherwise the just selected items don't always
217 // appear as selected
218 UpdateWindow(hwndTV);
219}
220
221// helper function which tricks the standard control into changing the focused
222// item without changing anything else (if someone knows why Microsoft doesn't
223// allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
224static void SetFocus(HWND hwndTV, HTREEITEM htItem)
225{
226 // the current focus
2c8e4738 227 HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(hwndTV);
3f7bc32b
VZ
228
229 if ( htItem )
230 {
231 // set the focus
232 if ( htItem != htFocus )
233 {
234 // remember the selection state of the item
235 bool wasSelected = IsItemSelected(hwndTV, htItem);
236
237 if ( htFocus && IsItemSelected(hwndTV, htFocus) )
238 {
239 // prevent the tree from unselecting the old focus which it
240 // would do by default (TreeView_SelectItem unselects the
241 // focused item)
242 TreeView_SelectItem(hwndTV, 0);
243 SelectItem(hwndTV, htFocus);
244 }
245
246 TreeView_SelectItem(hwndTV, htItem);
247
248 if ( !wasSelected )
249 {
250 // need to clear the selection which TreeView_SelectItem() gave
251 // us
252 UnselectItem(hwndTV, htItem);
253 }
254 //else: was selected, still selected - ok
255 }
256 //else: nothing to do, focus already there
257 }
258 else
259 {
260 if ( htFocus )
261 {
262 bool wasFocusSelected = IsItemSelected(hwndTV, htFocus);
263
264 // just clear the focus
265 TreeView_SelectItem(hwndTV, 0);
266
267 if ( wasFocusSelected )
268 {
269 // restore the selection state
270 SelectItem(hwndTV, htFocus);
271 }
272 }
273 //else: nothing to do, no focus already
274 }
275}
276
277#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
278
08b7c251
VZ
279// ----------------------------------------------------------------------------
280// private classes
281// ----------------------------------------------------------------------------
2bda0e17 282
08b7c251 283// a convenient wrapper around TV_ITEM struct which adds a ctor
f3ef286f 284#ifdef __VISUALC__
3f7bc32b 285#pragma warning( disable : 4097 ) // inheriting from typedef
f3ef286f
JS
286#endif
287
08b7c251
VZ
288struct wxTreeViewItem : public TV_ITEM
289{
9dfbf520
VZ
290 wxTreeViewItem(const wxTreeItemId& item, // the item handle
291 UINT mask_, // fields which are valid
292 UINT stateMask_ = 0) // for TVIF_STATE only
08b7c251 293 {
9dfbf520
VZ
294 // hItem member is always valid
295 mask = mask_ | TVIF_HANDLE;
08b7c251 296 stateMask = stateMask_;
3f7bc32b 297 hItem = HITEM(item);
08b7c251
VZ
298 }
299};
f3ef286f
JS
300
301#ifdef __VISUALC__
197dd9af 302#pragma warning( default : 4097 )
f3ef286f 303#endif
2bda0e17 304
9dfbf520
VZ
305// a class which encapsulates the tree traversal logic: it vists all (unless
306// OnVisit() returns FALSE) items under the given one
307class wxTreeTraversal
308{
309public:
310 wxTreeTraversal(const wxTreeCtrl *tree)
311 {
312 m_tree = tree;
313 }
314
315 // do traverse the tree: visit all items (recursively by default) under the
316 // given one; return TRUE if all items were traversed or FALSE if the
317 // traversal was aborted because OnVisit returned FALSE
318 bool DoTraverse(const wxTreeItemId& root, bool recursively = TRUE);
319
320 // override this function to do whatever is needed for each item, return
321 // FALSE to stop traversing
322 virtual bool OnVisit(const wxTreeItemId& item) = 0;
323
324protected:
325 const wxTreeCtrl *GetTree() const { return m_tree; }
326
327private:
328 bool Traverse(const wxTreeItemId& root, bool recursively);
329
330 const wxTreeCtrl *m_tree;
331};
332
74b31181
VZ
333// internal class for getting the selected items
334class TraverseSelections : public wxTreeTraversal
335{
336public:
337 TraverseSelections(const wxTreeCtrl *tree,
338 wxArrayTreeItemIds& selections)
339 : wxTreeTraversal(tree), m_selections(selections)
340 {
341 m_selections.Empty();
342
343 DoTraverse(tree->GetRootItem());
344 }
345
346 virtual bool OnVisit(const wxTreeItemId& item)
347 {
3f7bc32b 348#if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
74b31181 349 if ( GetTree()->IsItemChecked(item) )
3f7bc32b
VZ
350#else
351 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item)) )
352#endif
74b31181
VZ
353 {
354 m_selections.Add(item);
355 }
356
357 return TRUE;
358 }
359
e47c4d48
VZ
360 size_t GetCount() const { return m_selections.GetCount(); }
361
74b31181
VZ
362private:
363 wxArrayTreeItemIds& m_selections;
364};
365
366// internal class for counting tree items
367class TraverseCounter : public wxTreeTraversal
368{
369public:
370 TraverseCounter(const wxTreeCtrl *tree,
371 const wxTreeItemId& root,
372 bool recursively)
373 : wxTreeTraversal(tree)
374 {
375 m_count = 0;
376
377 DoTraverse(root, recursively);
378 }
379
33ac7e6f 380 virtual bool OnVisit(const wxTreeItemId& WXUNUSED(item))
74b31181
VZ
381 {
382 m_count++;
383
384 return TRUE;
385 }
386
387 size_t GetCount() const { return m_count; }
388
389private:
390 size_t m_count;
391};
392
393// ----------------------------------------------------------------------------
394// This class is needed for support of different images: the Win32 common
395// control natively supports only 2 images (the normal one and another for the
396// selected state). We wish to provide support for 2 more of them for folder
397// items (i.e. those which have children): for expanded state and for expanded
398// selected state. For this we use this structure to store the additional items
399// images.
400//
401// There is only one problem with this: when we retrieve the item's data, we
402// don't know whether we get a pointer to wxTreeItemData or
502a2b18
VZ
403// wxTreeItemIndirectData. So we always set the item id to an invalid value
404// in this class and the code using the client data checks for it and retrieves
405// the real client data in this case.
74b31181 406// ----------------------------------------------------------------------------
696e1ea0 407
502a2b18 408class wxTreeItemIndirectData : public wxTreeItemData
74b31181
VZ
409{
410public:
411 // ctor associates this data with the item and the real item data becomes
412 // available through our GetData() method
413 wxTreeItemIndirectData(wxTreeCtrl *tree, const wxTreeItemId& item)
414 {
415 for ( size_t n = 0; n < WXSIZEOF(m_images); n++ )
416 {
417 m_images[n] = -1;
418 }
419
420 // save the old data
421 m_data = tree->GetItemData(item);
422
423 // and set ourselves as the new one
424 tree->SetIndirectItemData(item, this);
cf48ccc0
VZ
425
426 // we must have the invalid value for the item
427 m_pItem = 0l;
74b31181
VZ
428 }
429
430 // dtor deletes the associated data as well
502a2b18 431 virtual ~wxTreeItemIndirectData() { delete m_data; }
74b31181
VZ
432
433 // accessors
434 // get the real data associated with the item
435 wxTreeItemData *GetData() const { return m_data; }
436 // change it
437 void SetData(wxTreeItemData *data) { m_data = data; }
438
439 // do we have such image?
440 bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; }
441 // get image
442 int GetImage(wxTreeItemIcon which) const { return m_images[which]; }
443 // change it
444 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
445
446private:
447 // all the images associated with the item
448 int m_images[wxTreeItemIcon_Max];
449
502a2b18 450 // the real client data
74b31181
VZ
451 wxTreeItemData *m_data;
452};
453
23f681ec 454// ----------------------------------------------------------------------------
3f7bc32b 455// wxWin macros
08b7c251
VZ
456// ----------------------------------------------------------------------------
457
23f681ec 458IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl)
2bda0e17 459
08b7c251 460// ----------------------------------------------------------------------------
deb1de30 461// constants
08b7c251
VZ
462// ----------------------------------------------------------------------------
463
deb1de30
VZ
464// indices in gs_expandEvents table below
465enum
466{
467 IDX_COLLAPSE,
468 IDX_EXPAND,
469 IDX_WHAT_MAX
470};
471
472enum
473{
474 IDX_DONE,
475 IDX_DOING,
476 IDX_HOW_MAX
477};
478
479// handy table for sending events - it has to be initialized during run-time
480// now so can't be const any more
481static /* const */ wxEventType gs_expandEvents[IDX_WHAT_MAX][IDX_HOW_MAX];
482
483/*
484 but logically it's a const table with the following entries:
485=
2bda0e17 486{
08b7c251
VZ
487 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
488 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
489};
deb1de30 490*/
08b7c251
VZ
491
492// ============================================================================
493// implementation
494// ============================================================================
495
9dfbf520
VZ
496// ----------------------------------------------------------------------------
497// tree traversal
498// ----------------------------------------------------------------------------
499
500bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively)
501{
502 if ( !OnVisit(root) )
503 return FALSE;
504
505 return Traverse(root, recursively);
506}
507
508bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively)
509{
510 long cookie;
511 wxTreeItemId child = m_tree->GetFirstChild(root, cookie);
512 while ( child.IsOk() )
513 {
514 // depth first traversal
515 if ( recursively && !Traverse(child, TRUE) )
516 return FALSE;
517
518 if ( !OnVisit(child) )
519 return FALSE;
520
521 child = m_tree->GetNextChild(root, cookie);
522 }
523
524 return TRUE;
525}
526
08b7c251
VZ
527// ----------------------------------------------------------------------------
528// construction and destruction
529// ----------------------------------------------------------------------------
530
531void wxTreeCtrl::Init()
532{
533 m_imageListNormal = NULL;
534 m_imageListState = NULL;
0377efa2 535 m_ownsImageListNormal = m_ownsImageListState = FALSE;
08b7c251 536 m_textCtrl = NULL;
696e1ea0 537 m_hasAnyAttr = FALSE;
23f681ec 538 m_dragImage = NULL;
3f7bc32b 539 m_htSelStart = 0;
9ab5ef13 540
deb1de30
VZ
541 // initialize the global array of events now as it can't be done statically
542 // with the wxEVT_XXX values being allocated during run-time only
543 gs_expandEvents[IDX_COLLAPSE][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED;
544 gs_expandEvents[IDX_COLLAPSE][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING;
545 gs_expandEvents[IDX_EXPAND][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_EXPANDED;
546 gs_expandEvents[IDX_EXPAND][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_EXPANDING;
2bda0e17
KB
547}
548
9dfbf520
VZ
549bool wxTreeCtrl::Create(wxWindow *parent,
550 wxWindowID id,
551 const wxPoint& pos,
552 const wxSize& size,
553 long style,
554 const wxValidator& validator,
08b7c251 555 const wxString& name)
2bda0e17 556{
08b7c251 557 Init();
2bda0e17 558
9dfbf520
VZ
559 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
560 return FALSE;
2bda0e17 561
5ea47806 562 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP |
b0766406
JS
563 TVS_SHOWSELALWAYS;
564
565 if ( m_windowStyle & wxCLIP_SIBLINGS )
566 wstyle |= WS_CLIPSIBLINGS;
2bda0e17 567
63da7df7
JS
568 if ((m_windowStyle & wxTR_NO_LINES) == 0)
569 wstyle |= TVS_HASLINES;
08b7c251
VZ
570 if ( m_windowStyle & wxTR_HAS_BUTTONS )
571 wstyle |= TVS_HASBUTTONS;
2bda0e17 572
08b7c251
VZ
573 if ( m_windowStyle & wxTR_EDIT_LABELS )
574 wstyle |= TVS_EDITLABELS;
2bda0e17 575
08b7c251
VZ
576 if ( m_windowStyle & wxTR_LINES_AT_ROOT )
577 wstyle |= TVS_LINESATROOT;
deb1de30 578
c6f4913a 579 if ( m_windowStyle & wxTR_FULL_ROW_HIGHLIGHT )
deb1de30 580 {
c6f4913a
VS
581 if ( wxTheApp->GetComCtl32Version() >= 471 )
582 wstyle |= TVS_FULLROWSELECT;
583 }
584
3f7bc32b
VZ
585 // using TVS_CHECKBOXES for emulation of a multiselection tree control
586 // doesn't work without the new enough headers
587#if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
588 !defined( __GNUWIN32_OLD__ ) && \
c42404a5
VZ
589 !defined( __BORLANDC__ ) && \
590 !defined( __WATCOMC__ ) && \
591 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
a20a10fe 592
9dfbf520
VZ
593 // we emulate the multiple selection tree controls by using checkboxes: set
594 // up the image list we need for this if we do have multiple selections
595 if ( m_windowStyle & wxTR_MULTIPLE )
10fcf31a 596 wstyle |= TVS_CHECKBOXES;
3f7bc32b 597#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
9dfbf520 598
08b7c251 599 // Create the tree control.
9dfbf520
VZ
600 if ( !MSWCreateControl(WC_TREEVIEW, wstyle) )
601 return FALSE;
602
f6bcfd97 603#if wxUSE_COMCTL32_SAFELY
a756f210 604 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
f6bcfd97
BP
605 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
606#elif 1
a756f210 607 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
f6bcfd97 608 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
fbdcff4a
JS
609#else
610 // This works around a bug in the Windows tree control whereby for some versions
611 // of comctrl32, setting any colour actually draws the background in black.
612 // This will initialise the background to the system colour.
f6bcfd97
BP
613 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
614 // Assume the user has an updated comctl32.dll.
fbdcff4a 615 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0,-1);
a756f210 616 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
f6bcfd97 617 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
fbdcff4a
JS
618#endif
619
5aeeab14 620
9dfbf520
VZ
621 // VZ: this is some experimental code which may be used to get the
622 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
623 // AFAIK, the standard DLL does about the same thing anyhow.
624#if 0
625 if ( m_windowStyle & wxTR_MULTIPLE )
626 {
627 wxBitmap bmp;
628
629 // create the DC compatible with the current screen
630 HDC hdcMem = CreateCompatibleDC(NULL);
631
632 // create a mono bitmap of the standard size
633 int x = GetSystemMetrics(SM_CXMENUCHECK);
634 int y = GetSystemMetrics(SM_CYMENUCHECK);
635 wxImageList imagelistCheckboxes(x, y, FALSE, 2);
636 HBITMAP hbmpCheck = CreateBitmap(x, y, // bitmap size
637 1, // # of color planes
638 1, // # bits needed for one pixel
639 0); // array containing colour data
640 SelectObject(hdcMem, hbmpCheck);
641
642 // then draw a check mark into it
643 RECT rect = { 0, 0, x, y };
644 if ( !::DrawFrameControl(hdcMem, &rect,
645 DFC_BUTTON,
646 DFCS_BUTTONCHECK | DFCS_CHECKED) )
647 {
223d09f6 648 wxLogLastError(wxT("DrawFrameControl(check)"));
9dfbf520
VZ
649 }
650
651 bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
652 imagelistCheckboxes.Add(bmp);
653
654 if ( !::DrawFrameControl(hdcMem, &rect,
655 DFC_BUTTON,
656 DFCS_BUTTONCHECK) )
657 {
223d09f6 658 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
9dfbf520
VZ
659 }
660
661 bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
662 imagelistCheckboxes.Add(bmp);
663
664 // clean up
665 ::DeleteDC(hdcMem);
666
667 // set the imagelist
668 SetStateImageList(&imagelistCheckboxes);
669 }
670#endif // 0
671
672 SetSize(pos.x, pos.y, size.x, size.y);
2bda0e17 673
08b7c251 674 return TRUE;
2bda0e17
KB
675}
676
08b7c251 677wxTreeCtrl::~wxTreeCtrl()
2bda0e17 678{
696e1ea0
VZ
679 // delete any attributes
680 if ( m_hasAnyAttr )
681 {
682 for ( wxNode *node = m_attrs.Next(); node; node = m_attrs.Next() )
683 {
684 delete (wxTreeItemAttr *)node->Data();
685 }
686
687 // prevent TVN_DELETEITEM handler from deleting the attributes again!
688 m_hasAnyAttr = FALSE;
689 }
690
08b7c251 691 DeleteTextCtrl();
2bda0e17 692
08b7c251
VZ
693 // delete user data to prevent memory leaks
694 DeleteAllItems();
33ac7e6f 695
0377efa2
VS
696 if (m_ownsImageListNormal) delete m_imageListNormal;
697 if (m_ownsImageListState) delete m_imageListState;
2bda0e17
KB
698}
699
08b7c251
VZ
700// ----------------------------------------------------------------------------
701// accessors
702// ----------------------------------------------------------------------------
2bda0e17 703
08b7c251 704// simple wrappers which add error checking in debug mode
2bda0e17 705
08b7c251 706bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const
2bda0e17 707{
d220ae32 708 if ( !TreeView_GetItem(GetHwnd(), tvItem) )
2bda0e17 709 {
f6bcfd97 710 wxLogLastError(wxT("TreeView_GetItem"));
08b7c251
VZ
711
712 return FALSE;
713 }
714
715 return TRUE;
2bda0e17
KB
716}
717
08b7c251 718void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem)
2bda0e17 719{
d220ae32 720 if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 )
2bda0e17 721 {
f6bcfd97 722 wxLogLastError(wxT("TreeView_SetItem"));
08b7c251 723 }
2bda0e17
KB
724}
725
08b7c251 726size_t wxTreeCtrl::GetCount() const
2bda0e17 727{
d220ae32 728 return (size_t)TreeView_GetCount(GetHwnd());
2bda0e17
KB
729}
730
08b7c251 731unsigned int wxTreeCtrl::GetIndent() const
2bda0e17 732{
d220ae32 733 return TreeView_GetIndent(GetHwnd());
2bda0e17
KB
734}
735
08b7c251 736void wxTreeCtrl::SetIndent(unsigned int indent)
2bda0e17 737{
d220ae32 738 TreeView_SetIndent(GetHwnd(), indent);
2bda0e17
KB
739}
740
08b7c251 741wxImageList *wxTreeCtrl::GetImageList() const
2bda0e17 742{
08b7c251 743 return m_imageListNormal;
2bda0e17
KB
744}
745
08b7c251 746wxImageList *wxTreeCtrl::GetStateImageList() const
2bda0e17 747{
08b7c251 748 return m_imageListNormal;
2bda0e17
KB
749}
750
08b7c251 751void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
2bda0e17 752{
08b7c251 753 // no error return
d220ae32 754 TreeView_SetImageList(GetHwnd(),
08b7c251
VZ
755 imageList ? imageList->GetHIMAGELIST() : 0,
756 which);
2bda0e17
KB
757}
758
08b7c251 759void wxTreeCtrl::SetImageList(wxImageList *imageList)
2bda0e17 760{
0377efa2 761 if (m_ownsImageListNormal) delete m_imageListNormal;
08b7c251 762 SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL);
0377efa2 763 m_ownsImageListNormal = FALSE;
2bda0e17
KB
764}
765
08b7c251 766void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
2bda0e17 767{
0377efa2 768 if (m_ownsImageListState) delete m_imageListState;
08b7c251 769 SetAnyImageList(m_imageListState = imageList, TVSIL_STATE);
0377efa2
VS
770 m_ownsImageListState = FALSE;
771}
772
773void wxTreeCtrl::AssignImageList(wxImageList *imageList)
774{
775 SetImageList(imageList);
776 m_ownsImageListNormal = TRUE;
777}
778
779void wxTreeCtrl::AssignStateImageList(wxImageList *imageList)
780{
781 SetStateImageList(imageList);
782 m_ownsImageListState = TRUE;
2bda0e17
KB
783}
784
33961d59
RR
785size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
786 bool recursively) const
787{
788 TraverseCounter counter(this, item, recursively);
23fd5130 789
73974df1 790 return counter.GetCount() - 1;
23fd5130
VZ
791}
792
bb448552
VZ
793// ----------------------------------------------------------------------------
794// control colours
795// ----------------------------------------------------------------------------
796
797bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour)
798{
f6bcfd97 799#if !wxUSE_COMCTL32_SAFELY
bb448552
VZ
800 if ( !wxWindowBase::SetBackgroundColour(colour) )
801 return FALSE;
802
803 SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel());
f6bcfd97 804#endif
bb448552
VZ
805
806 return TRUE;
807}
808
809bool wxTreeCtrl::SetForegroundColour(const wxColour &colour)
810{
f6bcfd97 811#if !wxUSE_COMCTL32_SAFELY
bb448552
VZ
812 if ( !wxWindowBase::SetForegroundColour(colour) )
813 return FALSE;
814
815 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel());
f6bcfd97 816#endif
bb448552
VZ
817
818 return TRUE;
819}
820
08b7c251
VZ
821// ----------------------------------------------------------------------------
822// Item access
823// ----------------------------------------------------------------------------
824
825wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
2bda0e17 826{
837e5743 827 wxChar buf[512]; // the size is arbitrary...
02ce7b72 828
08b7c251
VZ
829 wxTreeViewItem tvItem(item, TVIF_TEXT);
830 tvItem.pszText = buf;
831 tvItem.cchTextMax = WXSIZEOF(buf);
832 if ( !DoGetItem(&tvItem) )
833 {
834 // don't return some garbage which was on stack, but an empty string
223d09f6 835 buf[0] = wxT('\0');
08b7c251 836 }
2bda0e17 837
08b7c251
VZ
838 return wxString(buf);
839}
2bda0e17 840
08b7c251
VZ
841void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
842{
843 wxTreeViewItem tvItem(item, TVIF_TEXT);
837e5743 844 tvItem.pszText = (wxChar *)text.c_str(); // conversion is ok
08b7c251
VZ
845 DoSetItem(&tvItem);
846}
2bda0e17 847
74b31181
VZ
848int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId& item,
849 wxTreeItemIcon which) const
850{
851 wxTreeViewItem tvItem(item, TVIF_PARAM);
852 if ( !DoGetItem(&tvItem) )
853 {
854 return -1;
855 }
856
857 return ((wxTreeItemIndirectData *)tvItem.lParam)->GetImage(which);
858}
859
860void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId& item,
861 int image,
862 wxTreeItemIcon which) const
863{
864 wxTreeViewItem tvItem(item, TVIF_PARAM);
865 if ( !DoGetItem(&tvItem) )
866 {
867 return;
868 }
869
870 wxTreeItemIndirectData *data = ((wxTreeItemIndirectData *)tvItem.lParam);
871
872 data->SetImage(image, which);
873
874 // make sure that we have selected images as well
875 if ( which == wxTreeItemIcon_Normal &&
876 !data->HasImage(wxTreeItemIcon_Selected) )
877 {
878 data->SetImage(image, wxTreeItemIcon_Selected);
879 }
880
881 if ( which == wxTreeItemIcon_Expanded &&
882 !data->HasImage(wxTreeItemIcon_SelectedExpanded) )
883 {
884 data->SetImage(image, wxTreeItemIcon_SelectedExpanded);
885 }
886}
887
9dfbf520
VZ
888void wxTreeCtrl::DoSetItemImages(const wxTreeItemId& item,
889 int image,
890 int imageSel)
891{
892 wxTreeViewItem tvItem(item, TVIF_IMAGE | TVIF_SELECTEDIMAGE);
893 tvItem.iSelectedImage = imageSel;
894 tvItem.iImage = image;
895 DoSetItem(&tvItem);
896}
897
74b31181
VZ
898int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
899 wxTreeItemIcon which) const
08b7c251 900{
74b31181
VZ
901 if ( HasIndirectData(item) )
902 {
903 return DoGetItemImageFromData(item, which);
904 }
2bda0e17 905
74b31181
VZ
906 UINT mask;
907 switch ( which )
908 {
909 default:
223d09f6 910 wxFAIL_MSG( wxT("unknown tree item image type") );
2bda0e17 911
74b31181
VZ
912 case wxTreeItemIcon_Normal:
913 mask = TVIF_IMAGE;
914 break;
2bda0e17 915
74b31181
VZ
916 case wxTreeItemIcon_Selected:
917 mask = TVIF_SELECTEDIMAGE;
918 break;
919
920 case wxTreeItemIcon_Expanded:
921 case wxTreeItemIcon_SelectedExpanded:
922 return -1;
923 }
924
925 wxTreeViewItem tvItem(item, mask);
08b7c251 926 DoGetItem(&tvItem);
2bda0e17 927
74b31181 928 return mask == TVIF_IMAGE ? tvItem.iImage : tvItem.iSelectedImage;
2bda0e17
KB
929}
930
74b31181
VZ
931void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
932 wxTreeItemIcon which)
2bda0e17 933{
74b31181
VZ
934 int imageNormal, imageSel;
935 switch ( which )
936 {
937 default:
223d09f6 938 wxFAIL_MSG( wxT("unknown tree item image type") );
74b31181
VZ
939
940 case wxTreeItemIcon_Normal:
941 imageNormal = image;
942 imageSel = GetItemSelectedImage(item);
943 break;
944
945 case wxTreeItemIcon_Selected:
946 imageNormal = GetItemImage(item);
947 imageSel = image;
948 break;
949
950 case wxTreeItemIcon_Expanded:
951 case wxTreeItemIcon_SelectedExpanded:
952 if ( !HasIndirectData(item) )
953 {
954 // we need to get the old images first, because after we create
955 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
956 // get the images
957 imageNormal = GetItemImage(item);
958 imageSel = GetItemSelectedImage(item);
959
960 // if it doesn't have it yet, add it
961 wxTreeItemIndirectData *data = new
962 wxTreeItemIndirectData(this, item);
963
964 // copy the data to the new location
965 data->SetImage(imageNormal, wxTreeItemIcon_Normal);
966 data->SetImage(imageSel, wxTreeItemIcon_Selected);
967 }
968
969 DoSetItemImageFromData(item, image, which);
970
971 // reset the normal/selected images because we won't use them any
972 // more - now they're stored inside the indirect data
973 imageNormal =
974 imageSel = I_IMAGECALLBACK;
975 break;
976 }
977
9dfbf520
VZ
978 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
979 // change both normal and selected image - otherwise the change simply
980 // doesn't take place!
74b31181 981 DoSetItemImages(item, imageNormal, imageSel);
2bda0e17
KB
982}
983
08b7c251 984wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
2bda0e17 985{
08b7c251
VZ
986 wxTreeViewItem tvItem(item, TVIF_PARAM);
987 if ( !DoGetItem(&tvItem) )
988 {
989 return NULL;
990 }
2bda0e17 991
502a2b18
VZ
992 wxTreeItemData *data = (wxTreeItemData *)tvItem.lParam;
993 if ( IsDataIndirect(data) )
74b31181 994 {
502a2b18 995 data = ((wxTreeItemIndirectData *)data)->GetData();
74b31181 996 }
502a2b18
VZ
997
998 return data;
2bda0e17
KB
999}
1000
08b7c251 1001void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
2bda0e17 1002{
188781db
VZ
1003 // first, associate this piece of data with this item
1004 if ( data )
1005 {
1006 data->SetId(item);
1007 }
1008
08b7c251 1009 wxTreeViewItem tvItem(item, TVIF_PARAM);
74b31181
VZ
1010
1011 if ( HasIndirectData(item) )
1012 {
1013 if ( DoGetItem(&tvItem) )
1014 {
1015 ((wxTreeItemIndirectData *)tvItem.lParam)->SetData(data);
1016 }
1017 else
1018 {
223d09f6 1019 wxFAIL_MSG( wxT("failed to change tree items data") );
74b31181
VZ
1020 }
1021 }
1022 else
1023 {
1024 tvItem.lParam = (LPARAM)data;
1025 DoSetItem(&tvItem);
1026 }
1027}
1028
1029void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId& item,
1030 wxTreeItemIndirectData *data)
1031{
1032 // this should never happen because it's unnecessary and will probably lead
1033 // to crash too because the code elsewhere supposes that the pointer the
1034 // wxTreeItemIndirectData has is a real wxItemData and not
1035 // wxTreeItemIndirectData as well
223d09f6 1036 wxASSERT_MSG( !HasIndirectData(item), wxT("setting indirect data twice?") );
74b31181 1037
502a2b18 1038 SetItemData(item, data);
74b31181
VZ
1039}
1040
1041bool wxTreeCtrl::HasIndirectData(const wxTreeItemId& item) const
1042{
502a2b18
VZ
1043 // query the item itself
1044 wxTreeViewItem tvItem(item, TVIF_PARAM);
1045 if ( !DoGetItem(&tvItem) )
1046 {
1047 return FALSE;
1048 }
1049
1050 wxTreeItemData *data = (wxTreeItemData *)tvItem.lParam;
1051
1052 return data && IsDataIndirect(data);
08b7c251 1053}
2bda0e17 1054
3a5a2f56
VZ
1055void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
1056{
1057 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1058 tvItem.cChildren = (int)has;
1059 DoSetItem(&tvItem);
1060}
1061
add28c55
VZ
1062void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
1063{
1064 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1065 tvItem.state = bold ? TVIS_BOLD : 0;
1066 DoSetItem(&tvItem);
1067}
1068
58a8ab88
JS
1069void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
1070{
1071 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
1072 tvItem.state = highlight ? TVIS_DROPHILITED : 0;
1073 DoSetItem(&tvItem);
1074}
1075
d00407b2
VZ
1076void wxTreeCtrl::RefreshItem(const wxTreeItemId& item)
1077{
1078 wxRect rect;
1079 if ( GetBoundingRect(item, rect) )
1080 {
1081 RefreshRect(rect);
1082 }
1083}
1084
696e1ea0
VZ
1085void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
1086 const wxColour& col)
1087{
1088 m_hasAnyAttr = TRUE;
1089
1090 long id = (long)(WXHTREEITEM)item;
1091 wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id);
1092 if ( !attr )
1093 {
1094 attr = new wxTreeItemAttr;
1095 m_attrs.Put(id, (wxObject *)attr);
1096 }
1097
1098 attr->SetTextColour(col);
d00407b2
VZ
1099
1100 RefreshItem(item);
696e1ea0
VZ
1101}
1102
1103void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
1104 const wxColour& col)
1105{
1106 m_hasAnyAttr = TRUE;
1107
1108 long id = (long)(WXHTREEITEM)item;
1109 wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id);
1110 if ( !attr )
1111 {
1112 attr = new wxTreeItemAttr;
1113 m_attrs.Put(id, (wxObject *)attr);
1114 }
1115
1116 attr->SetBackgroundColour(col);
d00407b2
VZ
1117
1118 RefreshItem(item);
696e1ea0
VZ
1119}
1120
1121void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
1122{
1123 m_hasAnyAttr = TRUE;
1124
1125 long id = (long)(WXHTREEITEM)item;
1126 wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id);
1127 if ( !attr )
1128 {
1129 attr = new wxTreeItemAttr;
1130 m_attrs.Put(id, (wxObject *)attr);
1131 }
1132
1133 attr->SetFont(font);
d00407b2
VZ
1134
1135 RefreshItem(item);
696e1ea0
VZ
1136}
1137
08b7c251
VZ
1138// ----------------------------------------------------------------------------
1139// Item status
1140// ----------------------------------------------------------------------------
2bda0e17 1141
08b7c251
VZ
1142bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
1143{
add28c55 1144 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
08b7c251 1145 RECT rect;
955be36c
VZ
1146
1147 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1148 // the HTREEITEM with TVM_GETITEMRECT
1149 *(WXHTREEITEM *)&rect = (WXHTREEITEM)item;
1150
1151 // FALSE means get item rect for the whole item, not only text
1c74a900 1152 return SendMessage(GetHwnd(), TVM_GETITEMRECT, FALSE, (LPARAM)&rect) != 0;
2bda0e17
KB
1153}
1154
08b7c251 1155bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
2bda0e17 1156{
08b7c251
VZ
1157 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1158 DoGetItem(&tvItem);
2bda0e17 1159
08b7c251 1160 return tvItem.cChildren != 0;
2bda0e17
KB
1161}
1162
08b7c251 1163bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
2bda0e17 1164{
08b7c251
VZ
1165 // probably not a good idea to put it here
1166 //wxASSERT( ItemHasChildren(item) );
2bda0e17 1167
08b7c251
VZ
1168 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
1169 DoGetItem(&tvItem);
2bda0e17 1170
08b7c251 1171 return (tvItem.state & TVIS_EXPANDED) != 0;
2bda0e17
KB
1172}
1173
08b7c251 1174bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
2bda0e17 1175{
08b7c251
VZ
1176 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
1177 DoGetItem(&tvItem);
2bda0e17 1178
08b7c251 1179 return (tvItem.state & TVIS_SELECTED) != 0;
2bda0e17
KB
1180}
1181
add28c55
VZ
1182bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
1183{
1184 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1185 DoGetItem(&tvItem);
1186
1187 return (tvItem.state & TVIS_BOLD) != 0;
1188}
1189
08b7c251
VZ
1190// ----------------------------------------------------------------------------
1191// navigation
1192// ----------------------------------------------------------------------------
2bda0e17 1193
08b7c251
VZ
1194wxTreeItemId wxTreeCtrl::GetRootItem() const
1195{
d220ae32 1196 return wxTreeItemId((WXHTREEITEM) TreeView_GetRoot(GetHwnd()));
08b7c251 1197}
2bda0e17 1198
08b7c251
VZ
1199wxTreeItemId wxTreeCtrl::GetSelection() const
1200{
ad90972f 1201 wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), (long)(WXHTREEITEM)0,
223d09f6 1202 wxT("this only works with single selection controls") );
9dfbf520 1203
d220ae32 1204 return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(GetHwnd()));
2bda0e17
KB
1205}
1206
08b7c251 1207wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
2bda0e17 1208{
3f7bc32b 1209 return wxTreeItemId((WXHTREEITEM) TreeView_GetParent(GetHwnd(), HITEM(item)));
08b7c251 1210}
2bda0e17 1211
08b7c251 1212wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
06e38c8e 1213 long& _cookie) const
08b7c251
VZ
1214{
1215 // remember the last child returned in 'cookie'
3f7bc32b 1216 _cookie = (long)TreeView_GetChild(GetHwnd(), HITEM(item));
2bda0e17 1217
06e38c8e 1218 return wxTreeItemId((WXHTREEITEM)_cookie);
2bda0e17
KB
1219}
1220
08b7c251 1221wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
06e38c8e 1222 long& _cookie) const
2bda0e17 1223{
d220ae32 1224 wxTreeItemId l = wxTreeItemId((WXHTREEITEM)TreeView_GetNextSibling(GetHwnd(),
3f7bc32b 1225 HITEM(_cookie)));
23fd5130
VZ
1226 _cookie = (long)l;
1227
2e5dddb0 1228 return l;
08b7c251 1229}
2bda0e17 1230
978f38c2
VZ
1231wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
1232{
1233 // can this be done more efficiently?
1234 long cookie;
1235
1236 wxTreeItemId childLast,
2165ad93 1237 child = GetFirstChild(item, cookie);
978f38c2
VZ
1238 while ( child.IsOk() )
1239 {
1240 childLast = child;
2165ad93 1241 child = GetNextChild(item, cookie);
978f38c2
VZ
1242 }
1243
1244 return childLast;
1245}
1246
08b7c251
VZ
1247wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
1248{
3f7bc32b 1249 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(GetHwnd(), HITEM(item)));
2bda0e17
KB
1250}
1251
08b7c251 1252wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
2bda0e17 1253{
3f7bc32b 1254 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(GetHwnd(), HITEM(item)));
2bda0e17
KB
1255}
1256
08b7c251 1257wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
2bda0e17 1258{
d220ae32 1259 return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(GetHwnd()));
2bda0e17
KB
1260}
1261
08b7c251 1262wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
2bda0e17 1263{
f6bcfd97 1264 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() for must be visible itself!"));
02ce7b72 1265
3f7bc32b 1266 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(GetHwnd(), HITEM(item)));
08b7c251 1267}
02ce7b72 1268
08b7c251
VZ
1269wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1270{
f6bcfd97 1271 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() for must be visible itself!"));
02ce7b72 1272
3f7bc32b 1273 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(GetHwnd(), HITEM(item)));
08b7c251 1274}
02ce7b72 1275
9dfbf520
VZ
1276// ----------------------------------------------------------------------------
1277// multiple selections emulation
1278// ----------------------------------------------------------------------------
1279
1280bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const
1281{
1282 // receive the desired information.
1283 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1284 DoGetItem(&tvItem);
1285
1286 // state image indices are 1 based
1287 return ((tvItem.state >> 12) - 1) == 1;
1288}
1289
1290void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check)
1291{
1292 // receive the desired information.
1293 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1294
1295 // state images are one-based
1296 tvItem.state = (check ? 2 : 1) << 12;
1297
1298 DoSetItem(&tvItem);
1299}
1300
33961d59
RR
1301size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
1302{
1303 TraverseSelections selector(this, selections);
9dfbf520 1304
e47c4d48 1305 return selector.GetCount();
9dfbf520
VZ
1306}
1307
08b7c251
VZ
1308// ----------------------------------------------------------------------------
1309// Usual operations
1310// ----------------------------------------------------------------------------
02ce7b72 1311
08b7c251
VZ
1312wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
1313 wxTreeItemId hInsertAfter,
1314 const wxString& text,
1315 int image, int selectedImage,
1316 wxTreeItemData *data)
1317{
0e935169
VZ
1318 wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()),
1319 wxTreeItemId(),
1320 _T("can't have more than one root in the tree") );
1321
08b7c251 1322 TV_INSERTSTRUCT tvIns;
3f7bc32b
VZ
1323 tvIns.hParent = HITEM(parent);
1324 tvIns.hInsertAfter = HITEM(hInsertAfter);
58a8ab88 1325
74b31181
VZ
1326 // this is how we insert the item as the first child: supply a NULL
1327 // hInsertAfter
1328 if ( !tvIns.hInsertAfter )
58a8ab88
JS
1329 {
1330 tvIns.hInsertAfter = TVI_FIRST;
1331 }
1332
08b7c251
VZ
1333 UINT mask = 0;
1334 if ( !text.IsEmpty() )
1335 {
1336 mask |= TVIF_TEXT;
837e5743 1337 tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok
08b7c251 1338 }
f6bcfd97
BP
1339 else
1340 {
1341 tvIns.item.pszText = NULL;
1342 tvIns.item.cchTextMax = 0;
1343 }
02ce7b72 1344
08b7c251
VZ
1345 if ( image != -1 )
1346 {
1347 mask |= TVIF_IMAGE;
1348 tvIns.item.iImage = image;
3a5a2f56 1349
6b037754 1350 if ( selectedImage == -1 )
3a5a2f56
VZ
1351 {
1352 // take the same image for selected icon if not specified
1353 selectedImage = image;
1354 }
08b7c251 1355 }
02ce7b72 1356
08b7c251
VZ
1357 if ( selectedImage != -1 )
1358 {
1359 mask |= TVIF_SELECTEDIMAGE;
1360 tvIns.item.iSelectedImage = selectedImage;
1361 }
02ce7b72 1362
08b7c251
VZ
1363 if ( data != NULL )
1364 {
1365 mask |= TVIF_PARAM;
1366 tvIns.item.lParam = (LPARAM)data;
1367 }
02ce7b72 1368
08b7c251 1369 tvIns.item.mask = mask;
02ce7b72 1370
d220ae32 1371 HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns);
08b7c251
VZ
1372 if ( id == 0 )
1373 {
f6bcfd97 1374 wxLogLastError(wxT("TreeView_InsertItem"));
08b7c251 1375 }
02ce7b72 1376
fd3f686c
VZ
1377 if ( data != NULL )
1378 {
1379 // associate the application tree item with Win32 tree item handle
1380 data->SetId((WXHTREEITEM)id);
1381 }
1382
06e38c8e 1383 return wxTreeItemId((WXHTREEITEM)id);
2bda0e17
KB
1384}
1385
08b7c251
VZ
1386// for compatibility only
1387wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1388 const wxString& text,
1389 int image, int selImage,
1390 long insertAfter)
2bda0e17 1391{
06e38c8e 1392 return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text,
08b7c251 1393 image, selImage, NULL);
2bda0e17
KB
1394}
1395
08b7c251
VZ
1396wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
1397 int image, int selectedImage,
1398 wxTreeItemData *data)
2bda0e17 1399{
ad90972f 1400 return DoInsertItem(wxTreeItemId((long) (WXHTREEITEM) 0), (long)(WXHTREEITEM) 0,
08b7c251 1401 text, image, selectedImage, data);
2bda0e17
KB
1402}
1403
08b7c251
VZ
1404wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
1405 const wxString& text,
1406 int image, int selectedImage,
1407 wxTreeItemData *data)
2bda0e17 1408{
06e38c8e 1409 return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST,
08b7c251 1410 text, image, selectedImage, data);
2bda0e17
KB
1411}
1412
08b7c251
VZ
1413wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1414 const wxTreeItemId& idPrevious,
1415 const wxString& text,
1416 int image, int selectedImage,
1417 wxTreeItemData *data)
2bda0e17 1418{
08b7c251 1419 return DoInsertItem(parent, idPrevious, text, image, selectedImage, data);
2bda0e17
KB
1420}
1421
2ef31e80
VZ
1422wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1423 size_t index,
1424 const wxString& text,
1425 int image, int selectedImage,
1426 wxTreeItemData *data)
1427{
1428 // find the item from index
1429 long cookie;
1430 wxTreeItemId idPrev, idCur = GetFirstChild(parent, cookie);
1431 while ( index != 0 && idCur.IsOk() )
1432 {
1433 index--;
1434
1435 idPrev = idCur;
1436 idCur = GetNextChild(parent, cookie);
1437 }
1438
1439 // assert, not check: if the index is invalid, we will append the item
1440 // to the end
1441 wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") );
1442
1443 return DoInsertItem(parent, idPrev, text, image, selectedImage, data);
1444}
1445
08b7c251
VZ
1446wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
1447 const wxString& text,
1448 int image, int selectedImage,
1449 wxTreeItemData *data)
2bda0e17 1450{
06e38c8e 1451 return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST,
08b7c251 1452 text, image, selectedImage, data);
2bda0e17
KB
1453}
1454
08b7c251 1455void wxTreeCtrl::Delete(const wxTreeItemId& item)
2bda0e17 1456{
3f7bc32b 1457 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) )
bbcdf8bc 1458 {
f6bcfd97 1459 wxLogLastError(wxT("TreeView_DeleteItem"));
bbcdf8bc 1460 }
bbcdf8bc
JS
1461}
1462
23fd5130
VZ
1463// delete all children (but don't delete the item itself)
1464void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
1465{
1466 long cookie;
1467
1468 wxArrayLong children;
1469 wxTreeItemId child = GetFirstChild(item, cookie);
1470 while ( child.IsOk() )
1471 {
1472 children.Add((long)(WXHTREEITEM)child);
1473
1474 child = GetNextChild(item, cookie);
1475 }
1476
1477 size_t nCount = children.Count();
1478 for ( size_t n = 0; n < nCount; n++ )
1479 {
d220ae32 1480 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)children[n]) )
23fd5130 1481 {
f6bcfd97 1482 wxLogLastError(wxT("TreeView_DeleteItem"));
23fd5130
VZ
1483 }
1484 }
1485}
1486
08b7c251 1487void wxTreeCtrl::DeleteAllItems()
bbcdf8bc 1488{
d220ae32 1489 if ( !TreeView_DeleteAllItems(GetHwnd()) )
bbcdf8bc 1490 {
f6bcfd97 1491 wxLogLastError(wxT("TreeView_DeleteAllItems"));
bbcdf8bc 1492 }
2bda0e17
KB
1493}
1494
08b7c251 1495void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
2bda0e17 1496{
dd3646fd
VZ
1497 wxASSERT_MSG( flag == TVE_COLLAPSE ||
1498 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
1499 flag == TVE_EXPAND ||
1500 flag == TVE_TOGGLE,
223d09f6 1501 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
08b7c251
VZ
1502
1503 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
d220ae32
VZ
1504 // emulate them. This behaviour has changed slightly with comctl32.dll
1505 // v 4.70 - now it does send them but only the first time. To maintain
1506 // compatible behaviour and also in order to not have surprises with the
1507 // future versions, don't rely on this and still do everything ourselves.
1508 // To avoid that the messages be sent twice when the item is expanded for
1509 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1510
1511 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE);
1512 tvItem.state = 0;
1513 DoSetItem(&tvItem);
1514
3f7bc32b 1515 if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) != 0 )
08b7c251
VZ
1516 {
1517 wxTreeEvent event(wxEVT_NULL, m_windowId);
1518 event.m_item = item;
08b7c251 1519 event.SetEventObject(this);
2bda0e17 1520
deb1de30
VZ
1521 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1522 // itself
1523 event.SetEventType(gs_expandEvents[IsExpanded(item) ? IDX_EXPAND
1524 : IDX_COLLAPSE]
1525 [IDX_DONE]);
2bda0e17 1526
deb1de30 1527 (void)GetEventHandler()->ProcessEvent(event);
08b7c251 1528 }
d220ae32 1529 //else: change didn't took place, so do nothing at all
2bda0e17
KB
1530}
1531
08b7c251 1532void wxTreeCtrl::Expand(const wxTreeItemId& item)
2bda0e17 1533{
08b7c251 1534 DoExpand(item, TVE_EXPAND);
2bda0e17 1535}
2bda0e17 1536
08b7c251 1537void wxTreeCtrl::Collapse(const wxTreeItemId& item)
2bda0e17 1538{
08b7c251 1539 DoExpand(item, TVE_COLLAPSE);
2bda0e17
KB
1540}
1541
08b7c251 1542void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
2bda0e17 1543{
dd3646fd 1544 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
2bda0e17
KB
1545}
1546
08b7c251 1547void wxTreeCtrl::Toggle(const wxTreeItemId& item)
2bda0e17 1548{
08b7c251 1549 DoExpand(item, TVE_TOGGLE);
2bda0e17
KB
1550}
1551
42c5812d
UU
1552void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
1553{
9dfbf520 1554 DoExpand(item, action);
42c5812d
UU
1555}
1556
08b7c251 1557void wxTreeCtrl::Unselect()
2bda0e17 1558{
3f7bc32b
VZ
1559 wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE),
1560 wxT("doesn't make sense, may be you want UnselectAll()?") );
9dfbf520
VZ
1561
1562 // just remove the selection
ad90972f 1563 SelectItem(wxTreeItemId((long) (WXHTREEITEM) 0));
08b7c251 1564}
02ce7b72 1565
9dfbf520 1566void wxTreeCtrl::UnselectAll()
08b7c251 1567{
9dfbf520 1568 if ( m_windowStyle & wxTR_MULTIPLE )
2bda0e17 1569 {
9dfbf520
VZ
1570 wxArrayTreeItemIds selections;
1571 size_t count = GetSelections(selections);
1572 for ( size_t n = 0; n < count; n++ )
d220ae32 1573 {
3f7bc32b 1574#if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
9dfbf520 1575 SetItemCheck(selections[n], FALSE);
3f7bc32b
VZ
1576#else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1577 ::UnselectItem(GetHwnd(), HITEM(selections[n]));
1578#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
d220ae32 1579 }
9dfbf520
VZ
1580 }
1581 else
1582 {
1583 // just remove the selection
1584 Unselect();
1585 }
1586}
1587
1588void wxTreeCtrl::SelectItem(const wxTreeItemId& item)
1589{
1590 if ( m_windowStyle & wxTR_MULTIPLE )
1591 {
3f7bc32b 1592#if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
9dfbf520
VZ
1593 // selecting the item means checking it
1594 SetItemCheck(item);
3f7bc32b
VZ
1595#else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1596 ::SelectItem(GetHwnd(), HITEM(item));
1597#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
9dfbf520
VZ
1598 }
1599 else
1600 {
1601 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1602 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1603 // send them ourselves
1604
1605 wxTreeEvent event(wxEVT_NULL, m_windowId);
1606 event.m_item = item;
1607 event.SetEventObject(this);
1608
1609 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING);
1610 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
d220ae32 1611 {
3f7bc32b 1612 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item)) )
9dfbf520 1613 {
f6bcfd97 1614 wxLogLastError(wxT("TreeView_SelectItem"));
9dfbf520
VZ
1615 }
1616 else
1617 {
1618 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1619 (void)GetEventHandler()->ProcessEvent(event);
1620 }
d220ae32 1621 }
9dfbf520 1622 //else: program vetoed the change
2bda0e17 1623 }
08b7c251 1624}
2bda0e17 1625
08b7c251
VZ
1626void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1627{
1628 // no error return
3f7bc32b 1629 TreeView_EnsureVisible(GetHwnd(), HITEM(item));
08b7c251
VZ
1630}
1631
1632void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
1633{
3f7bc32b 1634 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) )
2bda0e17 1635 {
f6bcfd97 1636 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
2bda0e17 1637 }
08b7c251
VZ
1638}
1639
1640wxTextCtrl* wxTreeCtrl::GetEditControl() const
1641{
b94ae1ea
VZ
1642 // normally, we could try to do something like this to return something
1643 // even when the editing was started by the user and not by calling
1644 // EditLabel() - but as nobody has asked for this so far and there might be
1645 // problems in the code below, I leave it disabled for now (VZ)
1646#if 0
1647 if ( !m_textCtrl )
1648 {
1649 HWND hwndText = TreeView_GetEditControl(GetHwnd());
1650 if ( hwndText )
1651 {
1652 m_textCtrl = new wxTextCtrl(this, -1);
1653 m_textCtrl->Hide();
1654 m_textCtrl->SetHWND((WXHWND)hwndText);
1655 }
1656 //else: not editing label right now
1657 }
1658#endif // 0
1659
08b7c251
VZ
1660 return m_textCtrl;
1661}
1662
1663void wxTreeCtrl::DeleteTextCtrl()
1664{
1665 if ( m_textCtrl )
2bda0e17 1666 {
b6a3d6ad
VZ
1667 // the HWND corresponding to this control is deleted by the tree
1668 // control itself and we don't know when exactly this happens, so check
1669 // if the window still exists before calling UnsubclassWin()
1670 if ( !::IsWindow(GetHwndOf(m_textCtrl)) )
1671 {
1672 m_textCtrl->SetHWND(0);
1673 }
1674
08b7c251
VZ
1675 m_textCtrl->UnsubclassWin();
1676 m_textCtrl->SetHWND(0);
1677 delete m_textCtrl;
1678 m_textCtrl = NULL;
2bda0e17 1679 }
08b7c251 1680}
2bda0e17 1681
08b7c251
VZ
1682wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
1683 wxClassInfo* textControlClass)
1684{
1685 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
1686
b94ae1ea
VZ
1687 DeleteTextCtrl();
1688
3f7bc32b 1689 HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item));
2bda0e17 1690
5ea47806
VZ
1691 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1692 // returned FALSE
1693 if ( !hWnd )
1694 {
1695 return NULL;
1696 }
2bda0e17 1697
08b7c251 1698 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
c8f14476 1699 m_textCtrl->SetParent(this);
08b7c251
VZ
1700 m_textCtrl->SetHWND((WXHWND)hWnd);
1701 m_textCtrl->SubclassWin((WXHWND)hWnd);
2bda0e17 1702
08b7c251 1703 return m_textCtrl;
2bda0e17
KB
1704}
1705
08b7c251 1706// End label editing, optionally cancelling the edit
33ac7e6f 1707void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool discardChanges)
2bda0e17 1708{
d220ae32 1709 TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
08b7c251
VZ
1710
1711 DeleteTextCtrl();
2bda0e17
KB
1712}
1713
08b7c251 1714wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
2bda0e17 1715{
08b7c251
VZ
1716 TV_HITTESTINFO hitTestInfo;
1717 hitTestInfo.pt.x = (int)point.x;
1718 hitTestInfo.pt.y = (int)point.y;
2bda0e17 1719
d220ae32 1720 TreeView_HitTest(GetHwnd(), &hitTestInfo);
2bda0e17 1721
08b7c251
VZ
1722 flags = 0;
1723
1724 // avoid repetition
1725 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1726 flags |= wxTREE_HITTEST_##flag
1727
1728 TRANSLATE_FLAG(ABOVE);
1729 TRANSLATE_FLAG(BELOW);
1730 TRANSLATE_FLAG(NOWHERE);
1731 TRANSLATE_FLAG(ONITEMBUTTON);
1732 TRANSLATE_FLAG(ONITEMICON);
1733 TRANSLATE_FLAG(ONITEMINDENT);
1734 TRANSLATE_FLAG(ONITEMLABEL);
1735 TRANSLATE_FLAG(ONITEMRIGHT);
1736 TRANSLATE_FLAG(ONITEMSTATEICON);
1737 TRANSLATE_FLAG(TOLEFT);
1738 TRANSLATE_FLAG(TORIGHT);
2bda0e17 1739
08b7c251
VZ
1740 #undef TRANSLATE_FLAG
1741
06e38c8e 1742 return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem);
08b7c251
VZ
1743}
1744
f7c832a7
VZ
1745bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
1746 wxRect& rect,
1747 bool textOnly) const
1748{
1749 RECT rc;
3f7bc32b 1750 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item),
f7c832a7
VZ
1751 &rc, textOnly) )
1752 {
1753 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
1754
1755 return TRUE;
1756 }
1757 else
1758 {
1759 // couldn't retrieve rect: for example, item isn't visible
1760 return FALSE;
1761 }
1762}
1763
23fd5130
VZ
1764// ----------------------------------------------------------------------------
1765// sorting stuff
1766// ----------------------------------------------------------------------------
f7c832a7 1767
502a2b18
VZ
1768// this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1769// functions such as IsDataIndirect()
1770class wxTreeSortHelper
1771{
1772public:
1773 static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree);
1774
1775private:
1776 static wxTreeItemId GetIdFromData(wxTreeCtrl *tree, LPARAM item)
1777 {
1778 wxTreeItemData *data = (wxTreeItemData *)item;
1779 if ( tree->IsDataIndirect(data) )
1780 {
1781 data = ((wxTreeItemIndirectData *)data)->GetData();
1782 }
1783
1784 return data->GetId();
1785 }
1786};
1787
1788int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1,
1789 LPARAM pItem2,
1790 LPARAM htree)
23fd5130 1791{
096c9f9b 1792 wxCHECK_MSG( pItem1 && pItem2, 0,
223d09f6 1793 wxT("sorting tree without data doesn't make sense") );
096c9f9b 1794
502a2b18
VZ
1795 wxTreeCtrl *tree = (wxTreeCtrl *)htree;
1796
1797 return tree->OnCompareItems(GetIdFromData(tree, pItem1),
1798 GetIdFromData(tree, pItem2));
23fd5130
VZ
1799}
1800
95aabccc
VZ
1801int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1802 const wxTreeItemId& item2)
08b7c251 1803{
837e5743 1804 return wxStrcmp(GetItemText(item1), GetItemText(item2));
95aabccc
VZ
1805}
1806
1807void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
1808{
1809 // rely on the fact that TreeView_SortChildren does the same thing as our
23fd5130
VZ
1810 // default behaviour, i.e. sorts items alphabetically and so call it
1811 // directly if we're not in derived class (much more efficient!)
1812 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
2bda0e17 1813 {
3f7bc32b 1814 TreeView_SortChildren(GetHwnd(), HITEM(item), 0);
2bda0e17 1815 }
08b7c251 1816 else
2bda0e17 1817 {
62448488 1818 TV_SORTCB tvSort;
3f7bc32b 1819 tvSort.hParent = HITEM(item);
502a2b18 1820 tvSort.lpfnCompare = wxTreeSortHelper::Compare;
23fd5130 1821 tvSort.lParam = (LPARAM)this;
d220ae32 1822 TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
2bda0e17 1823 }
08b7c251 1824}
2bda0e17 1825
08b7c251
VZ
1826// ----------------------------------------------------------------------------
1827// implementation
1828// ----------------------------------------------------------------------------
2bda0e17 1829
08b7c251
VZ
1830bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1831{
1832 if ( cmd == EN_UPDATE )
2bda0e17 1833 {
08b7c251
VZ
1834 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1835 event.SetEventObject( this );
1836 ProcessCommand(event);
2bda0e17 1837 }
08b7c251 1838 else if ( cmd == EN_KILLFOCUS )
2bda0e17 1839 {
08b7c251
VZ
1840 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1841 event.SetEventObject( this );
1842 ProcessCommand(event);
2bda0e17 1843 }
08b7c251 1844 else
2bda0e17 1845 {
08b7c251
VZ
1846 // nothing done
1847 return FALSE;
2bda0e17 1848 }
08b7c251
VZ
1849
1850 // command processed
1851 return TRUE;
1852}
1853
23f681ec
VZ
1854// we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1855// only do it during dragging, minimize wxWin overhead (this is important for
1856// WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1857// instead of passing by wxWin events
1858long wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
1859{
3f7bc32b
VZ
1860 bool processed = FALSE;
1861 long rc = 0;
3f7bc32b
VZ
1862 bool isMultiple = (GetWindowStyle() & wxTR_MULTIPLE) != 0;
1863
1864 if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
23f681ec 1865 {
3f7bc32b
VZ
1866 // we only process mouse messages here and these parameters have the same
1867 // meaning for all of them
1868 int x = GET_X_LPARAM(lParam),
1869 y = GET_Y_LPARAM(lParam);
1870 HTREEITEM htItem = GetItemFromPoint(GetHwnd(), x, y);
1871
23f681ec
VZ
1872 switch ( nMsg )
1873 {
3f7bc32b
VZ
1874#if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1875 case WM_LBUTTONDOWN:
1876 if ( htItem && isMultiple )
23f681ec 1877 {
3f7bc32b
VZ
1878 if ( wParam & MK_CONTROL )
1879 {
1880 SetFocus();
23f681ec 1881
3f7bc32b
VZ
1882 // toggle selected state
1883 ToggleItemSelection(GetHwnd(), htItem);
1884
1885 ::SetFocus(GetHwnd(), htItem);
1886
1887 // reset on any click without Shift
1888 m_htSelStart = 0;
1889
1890 processed = TRUE;
1891 }
1892 else if ( wParam & MK_SHIFT )
1893 {
1894 // this selects all items between the starting one and
1895 // the current
1896
1897 if ( !m_htSelStart )
1898 {
1899 // take the focused item
1900 m_htSelStart = (WXHTREEITEM)
1901 TreeView_GetSelection(GetHwnd());
1902 }
1903
1904 SelectRange(GetHwnd(), HITEM(m_htSelStart), htItem,
1905 !(wParam & MK_CONTROL));
1906
1907 ::SetFocus(GetHwnd(), htItem);
23f681ec 1908
3f7bc32b
VZ
1909 processed = TRUE;
1910 }
1911 else // normal click
1912 {
1913 // clear the selection and then let the default handler
1914 // do the job
1915 UnselectAll();
1916
1917 // prevent the click from starting in-place editing
1918 // when there was no selection in the control
1919 TreeView_SelectItem(GetHwnd(), 0);
1920
1921 // reset on any click without Shift
1922 m_htSelStart = 0;
1923 }
1924 }
1925 break;
1926#endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1927
1928 case WM_MOUSEMOVE:
1929 if ( m_dragImage )
1930 {
afff720b 1931 m_dragImage->Move(wxPoint(x, y));
3f7bc32b 1932 if ( htItem )
23f681ec
VZ
1933 {
1934 // highlight the item as target (hiding drag image is
1935 // necessary - otherwise the display will be corrupted)
68be9f09 1936 m_dragImage->Hide();
3f7bc32b 1937 TreeView_SelectDropTarget(GetHwnd(), htItem);
68be9f09 1938 m_dragImage->Show();
23f681ec
VZ
1939 }
1940 }
1941 break;
1942
1943 case WM_LBUTTONUP:
1944 case WM_RBUTTONUP:
3f7bc32b 1945 if ( m_dragImage )
23f681ec 1946 {
68be9f09 1947 m_dragImage->EndDrag();
23f681ec
VZ
1948 delete m_dragImage;
1949 m_dragImage = NULL;
1950
1951 // generate the drag end event
1952 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, m_windowId);
1953
3f7bc32b 1954 event.m_item = (WXHTREEITEM)htItem;
23f681ec
VZ
1955 event.m_pointDrag = wxPoint(x, y);
1956 event.SetEventObject(this);
1957
1958 (void)GetEventHandler()->ProcessEvent(event);
225fe9d6
VZ
1959
1960 // if we don't do it, the tree seems to think that 2 items
1961 // are selected simultaneously which is quite weird
1962 TreeView_SelectDropTarget(GetHwnd(), 0);
23f681ec
VZ
1963 }
1964 break;
1965 }
1966 }
3f7bc32b
VZ
1967#if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1968 else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) && isMultiple )
1969 {
1970 // the tree control greys out the selected item when it loses focus and
1971 // paints it as selected again when it regains it, but it won't do it
1972 // for the other items itself - help it
1973 wxArrayTreeItemIds selections;
1974 size_t count = GetSelections(selections);
1975 RECT rect;
1976 for ( size_t n = 0; n < count; n++ )
1977 {
1978 // TreeView_GetItemRect() will return FALSE if item is not visible,
1979 // which may happen perfectly well
1980 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections[n]),
1981 &rect, TRUE) )
1982 {
1983 ::InvalidateRect(GetHwnd(), &rect, FALSE);
1984 }
1985 }
1986 }
1987 else if ( nMsg == WM_KEYDOWN && isMultiple )
1988 {
1989 bool bCtrl = wxIsCtrlDown(),
1990 bShift = wxIsShiftDown();
1991
1992 // we handle.arrows and space, but not page up/down and home/end: the
1993 // latter should be easy, but not the former
1994
2c8e4738 1995 HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
3f7bc32b
VZ
1996 if ( !m_htSelStart )
1997 {
1998 m_htSelStart = (WXHTREEITEM)htSel;
1999 }
2000
2001 if ( wParam == VK_SPACE )
2002 {
2003 if ( bCtrl )
2004 {
2005 ToggleItemSelection(GetHwnd(), htSel);
2006 }
2007 else
2008 {
2009 UnselectAll();
2010
2011 ::SelectItem(GetHwnd(), htSel);
2012 }
23f681ec 2013
3f7bc32b
VZ
2014 processed = TRUE;
2015 }
2016 else if ( wParam == VK_UP || wParam == VK_DOWN )
2017 {
2018 if ( !bCtrl && !bShift )
2019 {
2020 // no modifiers, just clear selection and then let the default
2021 // processing to take place
2022 UnselectAll();
2023 }
2024 else if ( htSel )
2025 {
2026 (void)wxControl::MSWWindowProc(nMsg, wParam, lParam);
2027
2c8e4738
VZ
2028 HTREEITEM htNext = (HTREEITEM)(wParam == VK_UP
2029 ? TreeView_GetPrevVisible(GetHwnd(), htSel)
2030 : TreeView_GetNextVisible(GetHwnd(), htSel));
3f7bc32b
VZ
2031
2032 if ( !htNext )
2033 {
2034 // at the top/bottom
2035 htNext = htSel;
2036 }
2037
2038 if ( bShift )
2039 {
2040 SelectRange(GetHwnd(), HITEM(m_htSelStart), htNext);
2041 }
2042 else // bCtrl
2043 {
2044 // without changing selection
2045 ::SetFocus(GetHwnd(), htNext);
2046 }
2047
2048 processed = TRUE;
2049 }
2050 }
2051 }
2052#endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
3f7bc32b
VZ
2053 if ( !processed )
2054 rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
2055
2056 return rc;
23f681ec
VZ
2057}
2058
08b7c251 2059// process WM_NOTIFY Windows message
a23fd0e1 2060bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
08b7c251
VZ
2061{
2062 wxTreeEvent event(wxEVT_NULL, m_windowId);
2063 wxEventType eventType = wxEVT_NULL;
2064 NMHDR *hdr = (NMHDR *)lParam;
2065
2066 switch ( hdr->code )
2bda0e17 2067 {
08b7c251
VZ
2068 case TVN_BEGINDRAG:
2069 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
2070 // fall through
2071
2072 case TVN_BEGINRDRAG:
2073 {
2074 if ( eventType == wxEVT_NULL )
2075 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
2076 //else: left drag, already set above
2077
2078 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2079
06e38c8e 2080 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
08b7c251 2081 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
23f681ec
VZ
2082
2083 // don't allow dragging by default: the user code must
2084 // explicitly say that it wants to allow it to avoid breaking
2085 // the old apps
2086 event.Veto();
08b7c251 2087 }
696e1ea0 2088 break;
08b7c251
VZ
2089
2090 case TVN_BEGINLABELEDIT:
2091 {
2092 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
2093 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2094
06e38c8e 2095 event.m_item = (WXHTREEITEM) info->item.hItem;
5ea47806 2096 event.m_label = info->item.pszText;
08b7c251 2097 }
696e1ea0 2098 break;
08b7c251
VZ
2099
2100 case TVN_DELETEITEM:
2101 {
2102 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
2103 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2104
696e1ea0
VZ
2105 event.m_item = (WXHTREEITEM)tv->itemOld.hItem;
2106
2107 if ( m_hasAnyAttr )
2108 {
2109 delete (wxTreeItemAttr *)m_attrs.
2110 Delete((long)tv->itemOld.hItem);
2111 }
08b7c251 2112 }
696e1ea0 2113 break;
08b7c251
VZ
2114
2115 case TVN_ENDLABELEDIT:
2116 {
2117 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
2118 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2119
5ea47806
VZ
2120 event.m_item = (WXHTREEITEM)info->item.hItem;
2121 event.m_label = info->item.pszText;
1ee4ead5
VZ
2122 if (info->item.pszText == NULL)
2123 return FALSE;
08b7c251
VZ
2124 break;
2125 }
2126
2127 case TVN_GETDISPINFO:
2128 eventType = wxEVT_COMMAND_TREE_GET_INFO;
2129 // fall through
2130
2131 case TVN_SETDISPINFO:
2132 {
2133 if ( eventType == wxEVT_NULL )
2134 eventType = wxEVT_COMMAND_TREE_SET_INFO;
2135 //else: get, already set above
2136
2137 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2138
06e38c8e 2139 event.m_item = (WXHTREEITEM) info->item.hItem;
08b7c251
VZ
2140 break;
2141 }
2142
2143 case TVN_ITEMEXPANDING:
08b7c251
VZ
2144 case TVN_ITEMEXPANDED:
2145 {
2146 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
2147
deb1de30 2148 int what;
08b7c251
VZ
2149 switch ( tv->action )
2150 {
deb1de30
VZ
2151 default:
2152 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action);
2153 // fall through
2154
08b7c251 2155 case TVE_EXPAND:
deb1de30 2156 what = IDX_EXPAND;
08b7c251
VZ
2157 break;
2158
2159 case TVE_COLLAPSE:
deb1de30 2160 what = IDX_COLLAPSE;
08b7c251 2161 break;
08b7c251
VZ
2162 }
2163
deb1de30
VZ
2164 int how = (int)hdr->code == TVN_ITEMEXPANDING ? IDX_DOING
2165 : IDX_DONE;
2166
2167 eventType = gs_expandEvents[what][how];
08b7c251 2168
06e38c8e 2169 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
08b7c251 2170 }
696e1ea0 2171 break;
08b7c251
VZ
2172
2173 case TVN_KEYDOWN:
2174 {
2175 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
2176 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
2177
b09bda68
VZ
2178 // we pass 0 as last CreateKeyEvent() parameter because we
2179 // don't have access to the real key press flags here - but as
2180 // it is only used to determin wxKeyEvent::m_altDown flag it's
2181 // not too bad
8191741f
VZ
2182 event.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN,
2183 wxCharCodeMSWToWX(info->wVKey),
2184 0);
23fd5130 2185
3f7bc32b
VZ
2186 // a separate event for Space/Return
2187 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2188 ((info->wVKey == VK_SPACE) || (info->wVKey == VK_RETURN)) )
23fd5130
VZ
2189 {
2190 wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
2191 m_windowId);
2192 event2.SetEventObject(this);
3f7bc32b
VZ
2193 if ( !(GetWindowStyle() & wxTR_MULTIPLE) )
2194 {
2195 event2.m_item = GetSelection();
2196 }
2197 //else: don't know how to get it
23fd5130 2198
3f7bc32b 2199 (void)GetEventHandler()->ProcessEvent(event2);
23fd5130 2200 }
08b7c251 2201 }
696e1ea0 2202 break;
08b7c251
VZ
2203
2204 case TVN_SELCHANGED:
2205 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
2206 // fall through
2207
2208 case TVN_SELCHANGING:
2209 {
2210 if ( eventType == wxEVT_NULL )
2211 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
2212 //else: already set above
2213
2214 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
2215
06e38c8e
JS
2216 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
2217 event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem;
08b7c251 2218 }
696e1ea0
VZ
2219 break;
2220
bf9b6266 2221#if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
696e1ea0
VZ
2222 case NM_CUSTOMDRAW:
2223 {
2224 LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam;
2225 NMCUSTOMDRAW& nmcd = lptvcd->nmcd;
d7926e0b 2226 switch ( nmcd.dwDrawStage )
696e1ea0
VZ
2227 {
2228 case CDDS_PREPAINT:
2229 // if we've got any items with non standard attributes,
2230 // notify us before painting each item
2231 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
2232 : CDRF_DODEFAULT;
d7926e0b 2233 break;
696e1ea0
VZ
2234
2235 case CDDS_ITEMPREPAINT:
2236 {
2237 wxTreeItemAttr *attr =
2238 (wxTreeItemAttr *)m_attrs.Get(nmcd.dwItemSpec);
2239
2240 if ( !attr )
2241 {
2242 // nothing to do for this item
d7926e0b
VZ
2243 *result = CDRF_DODEFAULT;
2244 break;
696e1ea0
VZ
2245 }
2246
2247 HFONT hFont;
2248 wxColour colText, colBack;
2249 if ( attr->HasFont() )
2250 {
2251 wxFont font = attr->GetFont();
2252 hFont = (HFONT)font.GetResourceHandle();
2253 }
2254 else
2255 {
2256 hFont = 0;
2257 }
2258
2259 if ( attr->HasTextColour() )
2260 {
2261 colText = attr->GetTextColour();
2262 }
2263 else
2264 {
2265 colText = GetForegroundColour();
2266 }
2267
2268 // selection colours should override ours
2269 if ( nmcd.uItemState & CDIS_SELECTED )
2270 {
2271 DWORD clrBk = ::GetSysColor(COLOR_HIGHLIGHT);
2272 lptvcd->clrTextBk = clrBk;
2273
2274 // try to make the text visible
2275 lptvcd->clrText = wxColourToRGB(colText);
2276 lptvcd->clrText |= ~clrBk;
2277 lptvcd->clrText &= 0x00ffffff;
2278 }
2279 else
2280 {
2281 if ( attr->HasBackgroundColour() )
2282 {
2283 colBack = attr->GetBackgroundColour();
2284 }
2285 else
2286 {
2287 colBack = GetBackgroundColour();
2288 }
2289
2290 lptvcd->clrText = wxColourToRGB(colText);
2291 lptvcd->clrTextBk = wxColourToRGB(colBack);
2292 }
2293
2294 // note that if we wanted to set colours for
2295 // individual columns (subitems), we would have
2296 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2297 if ( hFont )
2298 {
2299 ::SelectObject(nmcd.hdc, hFont);
2300
2301 *result = CDRF_NEWFONT;
2302 }
2303 else
2304 {
2305 *result = CDRF_DODEFAULT;
2306 }
696e1ea0 2307 }
d7926e0b 2308 break;
696e1ea0
VZ
2309
2310 default:
2311 *result = CDRF_DODEFAULT;
696e1ea0
VZ
2312 }
2313 }
d7926e0b
VZ
2314
2315 // we always process it
2316 return TRUE;
6ecfe2ac 2317#endif // _WIN32_IE >= 0x300
08b7c251 2318
f6bcfd97
BP
2319 case NM_DBLCLK:
2320 case NM_RCLICK:
2321 {
2322 TV_HITTESTINFO tvhti;
2323 ::GetCursorPos(&tvhti.pt);
2324 ::ScreenToClient(GetHwnd(), &tvhti.pt);
2325 if ( TreeView_HitTest(GetHwnd(), &tvhti) )
2326 {
2327 if ( tvhti.flags & TVHT_ONITEM )
2328 {
2329 event.m_item = (WXHTREEITEM) tvhti.hItem;
2330 eventType = (int)hdr->code == NM_DBLCLK
2331 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2332 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
8591268f
VZ
2333
2334 event.m_pointDrag.x = tvhti.pt.x;
2335 event.m_pointDrag.y = tvhti.pt.y;
f6bcfd97
BP
2336 }
2337
2338 break;
2339 }
2340 }
2341 // fall through
2342
08b7c251 2343 default:
a23fd0e1 2344 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2bda0e17 2345 }
08b7c251
VZ
2346
2347 event.SetEventObject(this);
2348 event.SetEventType(eventType);
2349
fd3f686c 2350 bool processed = GetEventHandler()->ProcessEvent(event);
08b7c251
VZ
2351
2352 // post processing
5ea47806 2353 switch ( hdr->code )
2bda0e17 2354 {
f6bcfd97
BP
2355 case NM_DBLCLK:
2356 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2357 // the return code of this event handler as the return value for
2358 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2359 // expanded status would never work
2360 *result = FALSE;
2361 break;
2362
23f681ec
VZ
2363 case TVN_BEGINDRAG:
2364 case TVN_BEGINRDRAG:
2365 if ( event.IsAllowed() )
2366 {
2367 // normally this is impossible because the m_dragImage is
2368 // deleted once the drag operation is over
2369 wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") );
2370
2371 m_dragImage = new wxDragImage(*this, event.m_item);
2372 m_dragImage->BeginDrag(wxPoint(0, 0), this);
68be9f09 2373 m_dragImage->Show();
23f681ec
VZ
2374 }
2375 break;
2376
5ea47806
VZ
2377 case TVN_DELETEITEM:
2378 {
2379 // NB: we might process this message using wxWindows event
2380 // tables, but due to overhead of wxWin event system we
2381 // prefer to do it here ourself (otherwise deleting a tree
2382 // with many items is just too slow)
2383 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
74b31181
VZ
2384
2385 wxTreeItemId item = event.m_item;
2386 if ( HasIndirectData(item) )
2387 {
2388 wxTreeItemIndirectData *data = (wxTreeItemIndirectData *)
2389 tv->itemOld.lParam;
2390 delete data; // can't be NULL here
74b31181
VZ
2391 }
2392 else
2393 {
2394 wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
2395 delete data; // may be NULL, ok
2396 }
08b7c251 2397
5ea47806
VZ
2398 processed = TRUE; // Make sure we don't get called twice
2399 }
2400 break;
2401
2402 case TVN_BEGINLABELEDIT:
2403 // return TRUE to cancel label editing
2404 *result = !event.IsAllowed();
2405 break;
2406
2407 case TVN_ENDLABELEDIT:
188781db
VZ
2408 // return TRUE to set the label to the new string: note that we
2409 // also must pretend that we did process the message or it is going
2410 // to be passed to DefWindowProc() which will happily return FALSE
2411 // cancelling the label change
5ea47806 2412 *result = event.IsAllowed();
188781db 2413 processed = TRUE;
5ea47806
VZ
2414
2415 // ensure that we don't have the text ctrl which is going to be
2416 // deleted any more
2417 DeleteTextCtrl();
2418 break;
2419
2420 case TVN_SELCHANGING:
2421 case TVN_ITEMEXPANDING:
2422 // return TRUE to prevent the action from happening
2423 *result = !event.IsAllowed();
2424 break;
2425
deb1de30
VZ
2426 case TVN_ITEMEXPANDED:
2427 // the item is not refreshed properly after expansion when it has
2428 // an image depending on the expanded/collapsed state - bug in
2429 // comctl32.dll or our code?
2430 {
2431 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
2432 if ( tv->action == TVE_EXPAND )
2433 {
2434 wxTreeItemId id = (WXHTREEITEM)tv->itemNew.hItem;
2435
2436 int image = GetItemImage(id, wxTreeItemIcon_Expanded);
2437 if ( image != -1 )
2438 {
2439 RefreshItem(id);
2440 }
2441 }
2442 }
2443 break;
2444
74b31181
VZ
2445 case TVN_GETDISPINFO:
2446 // NB: so far the user can't set the image himself anyhow, so do it
2447 // anyway - but this may change later
33ac7e6f 2448// if ( /* !processed && */ 1 )
74b31181
VZ
2449 {
2450 wxTreeItemId item = event.m_item;
2451 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2452 if ( info->item.mask & TVIF_IMAGE )
2453 {
2454 info->item.iImage =
2455 DoGetItemImageFromData
2456 (
2457 item,
2458 IsExpanded(item) ? wxTreeItemIcon_Expanded
2459 : wxTreeItemIcon_Normal
2460 );
2461 }
2462 if ( info->item.mask & TVIF_SELECTEDIMAGE )
2463 {
2464 info->item.iSelectedImage =
2465 DoGetItemImageFromData
2466 (
2467 item,
2468 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
2469 : wxTreeItemIcon_Selected
2470 );
2471 }
deb1de30 2472 }
74b31181
VZ
2473 break;
2474
5ea47806
VZ
2475 //default:
2476 // for the other messages the return value is ignored and there is
2477 // nothing special to do
2478 }
fd3f686c 2479 return processed;
2bda0e17
KB
2480}
2481
08b7c251 2482#endif // __WIN95__
2bda0e17 2483
1e6feb95 2484#endif // wxUSE_TREECTRL