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