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