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