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