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