/[pcsx2_0.9.7]/trunk/3rdparty/wxWidgets/src/msw/treectrl.cpp
ViewVC logotype

Contents of /trunk/3rdparty/wxWidgets/src/msw/treectrl.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22