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