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