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