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