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