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