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