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