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