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