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