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