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