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