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