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