Misc fixes
[wxWidgets.git] / src / msw / treectrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
20 #pragma implementation "treectrl.h"
21 #endif
22
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #include "wx/msw/private.h"
31
32 // Mingw32 is a bit mental even though this is done in winundef
33 #ifdef GetFirstChild
34 #undef GetFirstChild
35 #endif
36
37 #ifdef GetNextSibling
38 #undef GetNextSibling
39 #endif
40
41 #if defined(__WIN95__)
42
43 #include "wx/log.h"
44 #include "wx/dynarray.h"
45 #include "wx/imaglist.h"
46 #include "wx/treectrl.h"
47 #include "wx/settings.h"
48
49 #include "wx/msw/dragimag.h"
50
51 #ifdef __GNUWIN32_OLD__
52 #include "wx/msw/gnuwin32/extra.h"
53 #endif
54
55 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
56 #include <commctrl.h>
57 #endif
58
59 // Bug in headers, sometimes
60 #ifndef TVIS_FOCUSED
61 #define TVIS_FOCUSED 0x0001
62 #endif
63
64 #ifndef TV_FIRST
65 #define TV_FIRST 0x1100
66 #endif
67
68 #ifndef TVS_CHECKBOXES
69 #define TVS_CHECKBOXES 0x0100
70 #endif
71
72 // old headers might miss these messages (comctl32.dll 4.71+ only)
73 #ifndef TVM_SETBKCOLOR
74 #define TVM_SETBKCOLOR (TV_FIRST + 29)
75 #define TVM_SETTEXTCOLOR (TV_FIRST + 30)
76 #endif
77
78 // a macro to hide the ugliness of nested casts
79 #define HITEM(item) (HTREEITEM)(WXHTREEITEM)(item)
80
81 // the native control doesn't support multiple selections under MSW and we
82 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
83 // checkboxes be the selection status (checked == selected) or by really
84 // emulating everything, i.e. intercepting mouse and key events &c. The first
85 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
86 // looks quite ugly.
87 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
88
89 // ----------------------------------------------------------------------------
90 // private functions
91 // ----------------------------------------------------------------------------
92
93 // wrapper for TreeView_HitTest
94 static HTREEITEM GetItemFromPoint(HWND hwndTV, int x, int y)
95 {
96 TV_HITTESTINFO tvht;
97 tvht.pt.x = x;
98 tvht.pt.y = y;
99
100 return (HTREEITEM)TreeView_HitTest(hwndTV, &tvht);
101 }
102
103 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
104
105 // wrappers for TreeView_GetItem/TreeView_SetItem
106 static bool IsItemSelected(HWND hwndTV, HTREEITEM hItem)
107 {
108 TV_ITEM tvi;
109 tvi.mask = TVIF_STATE | TVIF_HANDLE;
110 tvi.stateMask = TVIS_SELECTED;
111 tvi.hItem = hItem;
112
113 if ( !TreeView_GetItem(hwndTV, &tvi) )
114 {
115 wxLogLastError("TreeView_GetItem");
116 }
117
118 return (tvi.state & TVIS_SELECTED) != 0;
119 }
120
121 static void SelectItem(HWND hwndTV, HTREEITEM hItem, bool select = TRUE)
122 {
123 TV_ITEM tvi;
124 tvi.mask = TVIF_STATE | TVIF_HANDLE;
125 tvi.stateMask = TVIS_SELECTED;
126 tvi.state = select ? TVIS_SELECTED : 0;
127 tvi.hItem = hItem;
128
129 if ( TreeView_SetItem(hwndTV, &tvi) == -1 )
130 {
131 wxLogLastError("TreeView_SetItem");
132 }
133 }
134
135 static inline void UnselectItem(HWND hwndTV, HTREEITEM htItem)
136 {
137 SelectItem(hwndTV, htItem, FALSE);
138 }
139
140 static inline void ToggleItemSelection(HWND hwndTV, HTREEITEM htItem)
141 {
142 SelectItem(hwndTV, htItem, !IsItemSelected(hwndTV, htItem));
143 }
144
145 // helper function which selects all items in a range and, optionally,
146 // unselects all others
147 static void SelectRange(HWND hwndTV,
148 HTREEITEM htFirst,
149 HTREEITEM htLast,
150 bool unselectOthers = TRUE)
151 {
152 // find the first (or last) item and select it
153 bool cont = TRUE;
154 HTREEITEM htItem = (HTREEITEM)TreeView_GetRoot(hwndTV);
155 while ( htItem && cont )
156 {
157 if ( (htItem == htFirst) || (htItem == htLast) )
158 {
159 if ( !IsItemSelected(hwndTV, htItem) )
160 {
161 SelectItem(hwndTV, htItem);
162 }
163
164 cont = FALSE;
165 }
166 else
167 {
168 if ( unselectOthers && IsItemSelected(hwndTV, htItem) )
169 {
170 UnselectItem(hwndTV, htItem);
171 }
172 }
173
174 htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
175 }
176
177 // select the items in range
178 cont = htFirst != htLast;
179 while ( htItem && cont )
180 {
181 if ( !IsItemSelected(hwndTV, htItem) )
182 {
183 SelectItem(hwndTV, htItem);
184 }
185
186 cont = (htItem != htFirst) && (htItem != htLast);
187
188 htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
189 }
190
191 // unselect the rest
192 if ( unselectOthers )
193 {
194 while ( htItem )
195 {
196 if ( IsItemSelected(hwndTV, htItem) )
197 {
198 UnselectItem(hwndTV, htItem);
199 }
200
201 htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
202 }
203 }
204
205 // seems to be necessary - otherwise the just selected items don't always
206 // appear as selected
207 UpdateWindow(hwndTV);
208 }
209
210 // helper function which tricks the standard control into changing the focused
211 // item without changing anything else (if someone knows why Microsoft doesn't
212 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
213 static void SetFocus(HWND hwndTV, HTREEITEM htItem)
214 {
215 // the current focus
216 HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(hwndTV);
217
218 if ( htItem )
219 {
220 // set the focus
221 if ( htItem != htFocus )
222 {
223 // remember the selection state of the item
224 bool wasSelected = IsItemSelected(hwndTV, htItem);
225
226 if ( htFocus && IsItemSelected(hwndTV, htFocus) )
227 {
228 // prevent the tree from unselecting the old focus which it
229 // would do by default (TreeView_SelectItem unselects the
230 // focused item)
231 TreeView_SelectItem(hwndTV, 0);
232 SelectItem(hwndTV, htFocus);
233 }
234
235 TreeView_SelectItem(hwndTV, htItem);
236
237 if ( !wasSelected )
238 {
239 // need to clear the selection which TreeView_SelectItem() gave
240 // us
241 UnselectItem(hwndTV, htItem);
242 }
243 //else: was selected, still selected - ok
244 }
245 //else: nothing to do, focus already there
246 }
247 else
248 {
249 if ( htFocus )
250 {
251 bool wasFocusSelected = IsItemSelected(hwndTV, htFocus);
252
253 // just clear the focus
254 TreeView_SelectItem(hwndTV, 0);
255
256 if ( wasFocusSelected )
257 {
258 // restore the selection state
259 SelectItem(hwndTV, htFocus);
260 }
261 }
262 //else: nothing to do, no focus already
263 }
264 }
265
266 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
267
268 // ----------------------------------------------------------------------------
269 // private classes
270 // ----------------------------------------------------------------------------
271
272 // a convenient wrapper around TV_ITEM struct which adds a ctor
273 #ifdef __VISUALC__
274 #pragma warning( disable : 4097 ) // inheriting from typedef
275 #endif
276
277 struct wxTreeViewItem : public TV_ITEM
278 {
279 wxTreeViewItem(const wxTreeItemId& item, // the item handle
280 UINT mask_, // fields which are valid
281 UINT stateMask_ = 0) // for TVIF_STATE only
282 {
283 // hItem member is always valid
284 mask = mask_ | TVIF_HANDLE;
285 stateMask = stateMask_;
286 hItem = HITEM(item);
287 }
288 };
289
290 #ifdef __VISUALC__
291 #pragma warning( default : 4097 )
292 #endif
293
294 // a class which encapsulates the tree traversal logic: it vists all (unless
295 // OnVisit() returns FALSE) items under the given one
296 class wxTreeTraversal
297 {
298 public:
299 wxTreeTraversal(const wxTreeCtrl *tree)
300 {
301 m_tree = tree;
302 }
303
304 // do traverse the tree: visit all items (recursively by default) under the
305 // given one; return TRUE if all items were traversed or FALSE if the
306 // traversal was aborted because OnVisit returned FALSE
307 bool DoTraverse(const wxTreeItemId& root, bool recursively = TRUE);
308
309 // override this function to do whatever is needed for each item, return
310 // FALSE to stop traversing
311 virtual bool OnVisit(const wxTreeItemId& item) = 0;
312
313 protected:
314 const wxTreeCtrl *GetTree() const { return m_tree; }
315
316 private:
317 bool Traverse(const wxTreeItemId& root, bool recursively);
318
319 const wxTreeCtrl *m_tree;
320 };
321
322 // internal class for getting the selected items
323 class TraverseSelections : public wxTreeTraversal
324 {
325 public:
326 TraverseSelections(const wxTreeCtrl *tree,
327 wxArrayTreeItemIds& selections)
328 : wxTreeTraversal(tree), m_selections(selections)
329 {
330 m_selections.Empty();
331
332 DoTraverse(tree->GetRootItem());
333 }
334
335 virtual bool OnVisit(const wxTreeItemId& item)
336 {
337 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
338 if ( GetTree()->IsItemChecked(item) )
339 #else
340 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item)) )
341 #endif
342 {
343 m_selections.Add(item);
344 }
345
346 return TRUE;
347 }
348
349 size_t GetCount() const { return m_selections.GetCount(); }
350
351 private:
352 wxArrayTreeItemIds& m_selections;
353 };
354
355 // internal class for counting tree items
356 class TraverseCounter : public wxTreeTraversal
357 {
358 public:
359 TraverseCounter(const wxTreeCtrl *tree,
360 const wxTreeItemId& root,
361 bool recursively)
362 : wxTreeTraversal(tree)
363 {
364 m_count = 0;
365
366 DoTraverse(root, recursively);
367 }
368
369 virtual bool OnVisit(const wxTreeItemId& item)
370 {
371 m_count++;
372
373 return TRUE;
374 }
375
376 size_t GetCount() const { return m_count; }
377
378 private:
379 size_t m_count;
380 };
381
382 // ----------------------------------------------------------------------------
383 // This class is needed for support of different images: the Win32 common
384 // control natively supports only 2 images (the normal one and another for the
385 // selected state). We wish to provide support for 2 more of them for folder
386 // items (i.e. those which have children): for expanded state and for expanded
387 // selected state. For this we use this structure to store the additional items
388 // images.
389 //
390 // There is only one problem with this: when we retrieve the item's data, we
391 // don't know whether we get a pointer to wxTreeItemData or
392 // wxTreeItemIndirectData. So we have to maintain a list of all items which
393 // have indirect data inside the listctrl itself.
394 // ----------------------------------------------------------------------------
395
396 class wxTreeItemIndirectData
397 {
398 public:
399 // ctor associates this data with the item and the real item data becomes
400 // available through our GetData() method
401 wxTreeItemIndirectData(wxTreeCtrl *tree, const wxTreeItemId& item)
402 {
403 for ( size_t n = 0; n < WXSIZEOF(m_images); n++ )
404 {
405 m_images[n] = -1;
406 }
407
408 // save the old data
409 m_data = tree->GetItemData(item);
410
411 // and set ourselves as the new one
412 tree->SetIndirectItemData(item, this);
413 }
414
415 // dtor deletes the associated data as well
416 ~wxTreeItemIndirectData() { delete m_data; }
417
418 // accessors
419 // get the real data associated with the item
420 wxTreeItemData *GetData() const { return m_data; }
421 // change it
422 void SetData(wxTreeItemData *data) { m_data = data; }
423
424 // do we have such image?
425 bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; }
426 // get image
427 int GetImage(wxTreeItemIcon which) const { return m_images[which]; }
428 // change it
429 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
430
431 private:
432 // all the images associated with the item
433 int m_images[wxTreeItemIcon_Max];
434
435 wxTreeItemData *m_data;
436 };
437
438 // ----------------------------------------------------------------------------
439 // wxWin macros
440 // ----------------------------------------------------------------------------
441
442 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl)
443
444 // ----------------------------------------------------------------------------
445 // variables
446 // ----------------------------------------------------------------------------
447
448 // handy table for sending events
449 static const wxEventType g_events[2][2] =
450 {
451 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
452 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
453 };
454
455 // ============================================================================
456 // implementation
457 // ============================================================================
458
459 // ----------------------------------------------------------------------------
460 // tree traversal
461 // ----------------------------------------------------------------------------
462
463 bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively)
464 {
465 if ( !OnVisit(root) )
466 return FALSE;
467
468 return Traverse(root, recursively);
469 }
470
471 bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively)
472 {
473 long cookie;
474 wxTreeItemId child = m_tree->GetFirstChild(root, cookie);
475 while ( child.IsOk() )
476 {
477 // depth first traversal
478 if ( recursively && !Traverse(child, TRUE) )
479 return FALSE;
480
481 if ( !OnVisit(child) )
482 return FALSE;
483
484 child = m_tree->GetNextChild(root, cookie);
485 }
486
487 return TRUE;
488 }
489
490 // ----------------------------------------------------------------------------
491 // construction and destruction
492 // ----------------------------------------------------------------------------
493
494 void wxTreeCtrl::Init()
495 {
496 m_imageListNormal = NULL;
497 m_imageListState = NULL;
498 m_textCtrl = NULL;
499 m_hasAnyAttr = FALSE;
500 m_dragImage = NULL;
501
502 m_htSelStart = 0;
503 }
504
505 bool wxTreeCtrl::Create(wxWindow *parent,
506 wxWindowID id,
507 const wxPoint& pos,
508 const wxSize& size,
509 long style,
510 const wxValidator& validator,
511 const wxString& name)
512 {
513 Init();
514
515 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
516 return FALSE;
517
518 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP |
519 TVS_HASLINES | TVS_SHOWSELALWAYS;
520
521 if ( m_windowStyle & wxTR_HAS_BUTTONS )
522 wstyle |= TVS_HASBUTTONS;
523
524 if ( m_windowStyle & wxTR_EDIT_LABELS )
525 wstyle |= TVS_EDITLABELS;
526
527 if ( m_windowStyle & wxTR_LINES_AT_ROOT )
528 wstyle |= TVS_LINESATROOT;
529
530 // using TVS_CHECKBOXES for emulation of a multiselection tree control
531 // doesn't work without the new enough headers
532 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
533 !defined( __GNUWIN32_OLD__ ) && \
534 !defined( __BORLANDC__ ) && \
535 !defined( __WATCOMC__ ) && \
536 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
537
538 // we emulate the multiple selection tree controls by using checkboxes: set
539 // up the image list we need for this if we do have multiple selections
540 if ( m_windowStyle & wxTR_MULTIPLE )
541 wstyle |= TVS_CHECKBOXES;
542 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
543
544 // Create the tree control.
545 if ( !MSWCreateControl(WC_TREEVIEW, wstyle) )
546 return FALSE;
547
548 #if 0
549 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
550 #else
551 // This works around a bug in the Windows tree control whereby for some versions
552 // of comctrl32, setting any colour actually draws the background in black.
553 // This will initialise the background to the system colour.
554 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0,-1);
555 wxWindow::SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
556 #endif
557
558 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
559
560 // VZ: this is some experimental code which may be used to get the
561 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
562 // AFAIK, the standard DLL does about the same thing anyhow.
563 #if 0
564 if ( m_windowStyle & wxTR_MULTIPLE )
565 {
566 wxBitmap bmp;
567
568 // create the DC compatible with the current screen
569 HDC hdcMem = CreateCompatibleDC(NULL);
570
571 // create a mono bitmap of the standard size
572 int x = GetSystemMetrics(SM_CXMENUCHECK);
573 int y = GetSystemMetrics(SM_CYMENUCHECK);
574 wxImageList imagelistCheckboxes(x, y, FALSE, 2);
575 HBITMAP hbmpCheck = CreateBitmap(x, y, // bitmap size
576 1, // # of color planes
577 1, // # bits needed for one pixel
578 0); // array containing colour data
579 SelectObject(hdcMem, hbmpCheck);
580
581 // then draw a check mark into it
582 RECT rect = { 0, 0, x, y };
583 if ( !::DrawFrameControl(hdcMem, &rect,
584 DFC_BUTTON,
585 DFCS_BUTTONCHECK | DFCS_CHECKED) )
586 {
587 wxLogLastError(wxT("DrawFrameControl(check)"));
588 }
589
590 bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
591 imagelistCheckboxes.Add(bmp);
592
593 if ( !::DrawFrameControl(hdcMem, &rect,
594 DFC_BUTTON,
595 DFCS_BUTTONCHECK) )
596 {
597 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
598 }
599
600 bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
601 imagelistCheckboxes.Add(bmp);
602
603 // clean up
604 ::DeleteDC(hdcMem);
605
606 // set the imagelist
607 SetStateImageList(&imagelistCheckboxes);
608 }
609 #endif // 0
610
611 SetSize(pos.x, pos.y, size.x, size.y);
612
613 return TRUE;
614 }
615
616 wxTreeCtrl::~wxTreeCtrl()
617 {
618 // delete any attributes
619 if ( m_hasAnyAttr )
620 {
621 for ( wxNode *node = m_attrs.Next(); node; node = m_attrs.Next() )
622 {
623 delete (wxTreeItemAttr *)node->Data();
624 }
625
626 // prevent TVN_DELETEITEM handler from deleting the attributes again!
627 m_hasAnyAttr = FALSE;
628 }
629
630 DeleteTextCtrl();
631
632 // delete user data to prevent memory leaks
633 DeleteAllItems();
634 }
635
636 // ----------------------------------------------------------------------------
637 // accessors
638 // ----------------------------------------------------------------------------
639
640 // simple wrappers which add error checking in debug mode
641
642 bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const
643 {
644 if ( !TreeView_GetItem(GetHwnd(), tvItem) )
645 {
646 wxLogLastError("TreeView_GetItem");
647
648 return FALSE;
649 }
650
651 return TRUE;
652 }
653
654 void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem)
655 {
656 if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 )
657 {
658 wxLogLastError("TreeView_SetItem");
659 }
660 }
661
662 size_t wxTreeCtrl::GetCount() const
663 {
664 return (size_t)TreeView_GetCount(GetHwnd());
665 }
666
667 unsigned int wxTreeCtrl::GetIndent() const
668 {
669 return TreeView_GetIndent(GetHwnd());
670 }
671
672 void wxTreeCtrl::SetIndent(unsigned int indent)
673 {
674 TreeView_SetIndent(GetHwnd(), indent);
675 }
676
677 wxImageList *wxTreeCtrl::GetImageList() const
678 {
679 return m_imageListNormal;
680 }
681
682 wxImageList *wxTreeCtrl::GetStateImageList() const
683 {
684 return m_imageListNormal;
685 }
686
687 void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
688 {
689 // no error return
690 TreeView_SetImageList(GetHwnd(),
691 imageList ? imageList->GetHIMAGELIST() : 0,
692 which);
693 }
694
695 void wxTreeCtrl::SetImageList(wxImageList *imageList)
696 {
697 SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL);
698 }
699
700 void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
701 {
702 SetAnyImageList(m_imageListState = imageList, TVSIL_STATE);
703 }
704
705 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
706 bool recursively) const
707 {
708 TraverseCounter counter(this, item, recursively);
709
710 return counter.GetCount() - 1;
711 }
712
713 // ----------------------------------------------------------------------------
714 // control colours
715 // ----------------------------------------------------------------------------
716
717 bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour)
718 {
719 if ( !wxWindowBase::SetBackgroundColour(colour) )
720 return FALSE;
721
722 SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel());
723
724 return TRUE;
725 }
726
727 bool wxTreeCtrl::SetForegroundColour(const wxColour &colour)
728 {
729 if ( !wxWindowBase::SetForegroundColour(colour) )
730 return FALSE;
731
732 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel());
733
734 return TRUE;
735 }
736
737 // ----------------------------------------------------------------------------
738 // Item access
739 // ----------------------------------------------------------------------------
740
741 wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
742 {
743 wxChar buf[512]; // the size is arbitrary...
744
745 wxTreeViewItem tvItem(item, TVIF_TEXT);
746 tvItem.pszText = buf;
747 tvItem.cchTextMax = WXSIZEOF(buf);
748 if ( !DoGetItem(&tvItem) )
749 {
750 // don't return some garbage which was on stack, but an empty string
751 buf[0] = wxT('\0');
752 }
753
754 return wxString(buf);
755 }
756
757 void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
758 {
759 wxTreeViewItem tvItem(item, TVIF_TEXT);
760 tvItem.pszText = (wxChar *)text.c_str(); // conversion is ok
761 DoSetItem(&tvItem);
762 }
763
764 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId& item,
765 wxTreeItemIcon which) const
766 {
767 wxTreeViewItem tvItem(item, TVIF_PARAM);
768 if ( !DoGetItem(&tvItem) )
769 {
770 return -1;
771 }
772
773 return ((wxTreeItemIndirectData *)tvItem.lParam)->GetImage(which);
774 }
775
776 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId& item,
777 int image,
778 wxTreeItemIcon which) const
779 {
780 wxTreeViewItem tvItem(item, TVIF_PARAM);
781 if ( !DoGetItem(&tvItem) )
782 {
783 return;
784 }
785
786 wxTreeItemIndirectData *data = ((wxTreeItemIndirectData *)tvItem.lParam);
787
788 data->SetImage(image, which);
789
790 // make sure that we have selected images as well
791 if ( which == wxTreeItemIcon_Normal &&
792 !data->HasImage(wxTreeItemIcon_Selected) )
793 {
794 data->SetImage(image, wxTreeItemIcon_Selected);
795 }
796
797 if ( which == wxTreeItemIcon_Expanded &&
798 !data->HasImage(wxTreeItemIcon_SelectedExpanded) )
799 {
800 data->SetImage(image, wxTreeItemIcon_SelectedExpanded);
801 }
802 }
803
804 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId& item,
805 int image,
806 int imageSel)
807 {
808 wxTreeViewItem tvItem(item, TVIF_IMAGE | TVIF_SELECTEDIMAGE);
809 tvItem.iSelectedImage = imageSel;
810 tvItem.iImage = image;
811 DoSetItem(&tvItem);
812 }
813
814 int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
815 wxTreeItemIcon which) const
816 {
817 if ( HasIndirectData(item) )
818 {
819 return DoGetItemImageFromData(item, which);
820 }
821
822 UINT mask;
823 switch ( which )
824 {
825 default:
826 wxFAIL_MSG( wxT("unknown tree item image type") );
827
828 case wxTreeItemIcon_Normal:
829 mask = TVIF_IMAGE;
830 break;
831
832 case wxTreeItemIcon_Selected:
833 mask = TVIF_SELECTEDIMAGE;
834 break;
835
836 case wxTreeItemIcon_Expanded:
837 case wxTreeItemIcon_SelectedExpanded:
838 return -1;
839 }
840
841 wxTreeViewItem tvItem(item, mask);
842 DoGetItem(&tvItem);
843
844 return mask == TVIF_IMAGE ? tvItem.iImage : tvItem.iSelectedImage;
845 }
846
847 void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
848 wxTreeItemIcon which)
849 {
850 int imageNormal, imageSel;
851 switch ( which )
852 {
853 default:
854 wxFAIL_MSG( wxT("unknown tree item image type") );
855
856 case wxTreeItemIcon_Normal:
857 imageNormal = image;
858 imageSel = GetItemSelectedImage(item);
859 break;
860
861 case wxTreeItemIcon_Selected:
862 imageNormal = GetItemImage(item);
863 imageSel = image;
864 break;
865
866 case wxTreeItemIcon_Expanded:
867 case wxTreeItemIcon_SelectedExpanded:
868 if ( !HasIndirectData(item) )
869 {
870 // we need to get the old images first, because after we create
871 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
872 // get the images
873 imageNormal = GetItemImage(item);
874 imageSel = GetItemSelectedImage(item);
875
876 // if it doesn't have it yet, add it
877 wxTreeItemIndirectData *data = new
878 wxTreeItemIndirectData(this, item);
879
880 // copy the data to the new location
881 data->SetImage(imageNormal, wxTreeItemIcon_Normal);
882 data->SetImage(imageSel, wxTreeItemIcon_Selected);
883 }
884
885 DoSetItemImageFromData(item, image, which);
886
887 // reset the normal/selected images because we won't use them any
888 // more - now they're stored inside the indirect data
889 imageNormal =
890 imageSel = I_IMAGECALLBACK;
891 break;
892 }
893
894 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
895 // change both normal and selected image - otherwise the change simply
896 // doesn't take place!
897 DoSetItemImages(item, imageNormal, imageSel);
898 }
899
900 wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
901 {
902 wxTreeViewItem tvItem(item, TVIF_PARAM);
903 if ( !DoGetItem(&tvItem) )
904 {
905 return NULL;
906 }
907
908 if ( HasIndirectData(item) )
909 {
910 return ((wxTreeItemIndirectData *)tvItem.lParam)->GetData();
911 }
912 else
913 {
914 return (wxTreeItemData *)tvItem.lParam;
915 }
916 }
917
918 void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
919 {
920 wxTreeViewItem tvItem(item, TVIF_PARAM);
921
922 if ( HasIndirectData(item) )
923 {
924 if ( DoGetItem(&tvItem) )
925 {
926 ((wxTreeItemIndirectData *)tvItem.lParam)->SetData(data);
927 }
928 else
929 {
930 wxFAIL_MSG( wxT("failed to change tree items data") );
931 }
932 }
933 else
934 {
935 tvItem.lParam = (LPARAM)data;
936 DoSetItem(&tvItem);
937 }
938 }
939
940 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId& item,
941 wxTreeItemIndirectData *data)
942 {
943 // this should never happen because it's unnecessary and will probably lead
944 // to crash too because the code elsewhere supposes that the pointer the
945 // wxTreeItemIndirectData has is a real wxItemData and not
946 // wxTreeItemIndirectData as well
947 wxASSERT_MSG( !HasIndirectData(item), wxT("setting indirect data twice?") );
948
949 SetItemData(item, (wxTreeItemData *)data);
950
951 m_itemsWithIndirectData.Add(item);
952 }
953
954 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId& item) const
955 {
956 return m_itemsWithIndirectData.Index(item) != wxNOT_FOUND;
957 }
958
959 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
960 {
961 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
962 tvItem.cChildren = (int)has;
963 DoSetItem(&tvItem);
964 }
965
966 void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
967 {
968 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
969 tvItem.state = bold ? TVIS_BOLD : 0;
970 DoSetItem(&tvItem);
971 }
972
973 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
974 {
975 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
976 tvItem.state = highlight ? TVIS_DROPHILITED : 0;
977 DoSetItem(&tvItem);
978 }
979
980 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
981 const wxColour& col)
982 {
983 m_hasAnyAttr = TRUE;
984
985 long id = (long)(WXHTREEITEM)item;
986 wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id);
987 if ( !attr )
988 {
989 attr = new wxTreeItemAttr;
990 m_attrs.Put(id, (wxObject *)attr);
991 }
992
993 attr->SetTextColour(col);
994 }
995
996 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
997 const wxColour& col)
998 {
999 m_hasAnyAttr = TRUE;
1000
1001 long id = (long)(WXHTREEITEM)item;
1002 wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id);
1003 if ( !attr )
1004 {
1005 attr = new wxTreeItemAttr;
1006 m_attrs.Put(id, (wxObject *)attr);
1007 }
1008
1009 attr->SetBackgroundColour(col);
1010 }
1011
1012 void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
1013 {
1014 m_hasAnyAttr = TRUE;
1015
1016 long id = (long)(WXHTREEITEM)item;
1017 wxTreeItemAttr *attr = (wxTreeItemAttr *)m_attrs.Get(id);
1018 if ( !attr )
1019 {
1020 attr = new wxTreeItemAttr;
1021 m_attrs.Put(id, (wxObject *)attr);
1022 }
1023
1024 attr->SetFont(font);
1025 }
1026
1027 // ----------------------------------------------------------------------------
1028 // Item status
1029 // ----------------------------------------------------------------------------
1030
1031 bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
1032 {
1033 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1034 RECT rect;
1035
1036 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1037 // the HTREEITEM with TVM_GETITEMRECT
1038 *(WXHTREEITEM *)&rect = (WXHTREEITEM)item;
1039
1040 // FALSE means get item rect for the whole item, not only text
1041 return SendMessage(GetHwnd(), TVM_GETITEMRECT, FALSE, (LPARAM)&rect) != 0;
1042
1043 }
1044
1045 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
1046 {
1047 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1048 DoGetItem(&tvItem);
1049
1050 return tvItem.cChildren != 0;
1051 }
1052
1053 bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
1054 {
1055 // probably not a good idea to put it here
1056 //wxASSERT( ItemHasChildren(item) );
1057
1058 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
1059 DoGetItem(&tvItem);
1060
1061 return (tvItem.state & TVIS_EXPANDED) != 0;
1062 }
1063
1064 bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
1065 {
1066 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
1067 DoGetItem(&tvItem);
1068
1069 return (tvItem.state & TVIS_SELECTED) != 0;
1070 }
1071
1072 bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
1073 {
1074 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1075 DoGetItem(&tvItem);
1076
1077 return (tvItem.state & TVIS_BOLD) != 0;
1078 }
1079
1080 // ----------------------------------------------------------------------------
1081 // navigation
1082 // ----------------------------------------------------------------------------
1083
1084 wxTreeItemId wxTreeCtrl::GetRootItem() const
1085 {
1086 return wxTreeItemId((WXHTREEITEM) TreeView_GetRoot(GetHwnd()));
1087 }
1088
1089 wxTreeItemId wxTreeCtrl::GetSelection() const
1090 {
1091 wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), (WXHTREEITEM)0,
1092 wxT("this only works with single selection controls") );
1093
1094 return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(GetHwnd()));
1095 }
1096
1097 wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
1098 {
1099 return wxTreeItemId((WXHTREEITEM) TreeView_GetParent(GetHwnd(), HITEM(item)));
1100 }
1101
1102 wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1103 long& _cookie) const
1104 {
1105 // remember the last child returned in 'cookie'
1106 _cookie = (long)TreeView_GetChild(GetHwnd(), HITEM(item));
1107
1108 return wxTreeItemId((WXHTREEITEM)_cookie);
1109 }
1110
1111 wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
1112 long& _cookie) const
1113 {
1114 wxTreeItemId l = wxTreeItemId((WXHTREEITEM)TreeView_GetNextSibling(GetHwnd(),
1115 HITEM(_cookie)));
1116 _cookie = (long)l;
1117
1118 return l;
1119 }
1120
1121 wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
1122 {
1123 // can this be done more efficiently?
1124 long cookie;
1125
1126 wxTreeItemId childLast,
1127 child = GetFirstChild(item, cookie);
1128 while ( child.IsOk() )
1129 {
1130 childLast = child;
1131 child = GetNextChild(item, cookie);
1132 }
1133
1134 return childLast;
1135 }
1136
1137 wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
1138 {
1139 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(GetHwnd(), HITEM(item)));
1140 }
1141
1142 wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
1143 {
1144 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(GetHwnd(), HITEM(item)));
1145 }
1146
1147 wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
1148 {
1149 return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(GetHwnd()));
1150 }
1151
1152 wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
1153 {
1154 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() "
1155 "for must be visible itself!"));
1156
1157 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(GetHwnd(), HITEM(item)));
1158 }
1159
1160 wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1161 {
1162 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() "
1163 "for must be visible itself!"));
1164
1165 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(GetHwnd(), HITEM(item)));
1166 }
1167
1168 // ----------------------------------------------------------------------------
1169 // multiple selections emulation
1170 // ----------------------------------------------------------------------------
1171
1172 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const
1173 {
1174 // receive the desired information.
1175 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1176 DoGetItem(&tvItem);
1177
1178 // state image indices are 1 based
1179 return ((tvItem.state >> 12) - 1) == 1;
1180 }
1181
1182 void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check)
1183 {
1184 // receive the desired information.
1185 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1186
1187 // state images are one-based
1188 tvItem.state = (check ? 2 : 1) << 12;
1189
1190 DoSetItem(&tvItem);
1191 }
1192
1193 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
1194 {
1195 TraverseSelections selector(this, selections);
1196
1197 return selector.GetCount();
1198 }
1199
1200 // ----------------------------------------------------------------------------
1201 // Usual operations
1202 // ----------------------------------------------------------------------------
1203
1204 wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
1205 wxTreeItemId hInsertAfter,
1206 const wxString& text,
1207 int image, int selectedImage,
1208 wxTreeItemData *data)
1209 {
1210 wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()),
1211 wxTreeItemId(),
1212 _T("can't have more than one root in the tree") );
1213
1214 TV_INSERTSTRUCT tvIns;
1215 tvIns.hParent = HITEM(parent);
1216 tvIns.hInsertAfter = HITEM(hInsertAfter);
1217
1218 // this is how we insert the item as the first child: supply a NULL
1219 // hInsertAfter
1220 if ( !tvIns.hInsertAfter )
1221 {
1222 tvIns.hInsertAfter = TVI_FIRST;
1223 }
1224
1225 UINT mask = 0;
1226 if ( !text.IsEmpty() )
1227 {
1228 mask |= TVIF_TEXT;
1229 tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok
1230 }
1231
1232 if ( image != -1 )
1233 {
1234 mask |= TVIF_IMAGE;
1235 tvIns.item.iImage = image;
1236
1237 if ( selectedImage == -1 )
1238 {
1239 // take the same image for selected icon if not specified
1240 selectedImage = image;
1241 }
1242 }
1243
1244 if ( selectedImage != -1 )
1245 {
1246 mask |= TVIF_SELECTEDIMAGE;
1247 tvIns.item.iSelectedImage = selectedImage;
1248 }
1249
1250 if ( data != NULL )
1251 {
1252 mask |= TVIF_PARAM;
1253 tvIns.item.lParam = (LPARAM)data;
1254 }
1255
1256 tvIns.item.mask = mask;
1257
1258 HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns);
1259 if ( id == 0 )
1260 {
1261 wxLogLastError("TreeView_InsertItem");
1262 }
1263
1264 if ( data != NULL )
1265 {
1266 // associate the application tree item with Win32 tree item handle
1267 data->SetId((WXHTREEITEM)id);
1268 }
1269
1270 return wxTreeItemId((WXHTREEITEM)id);
1271 }
1272
1273 // for compatibility only
1274 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1275 const wxString& text,
1276 int image, int selImage,
1277 long insertAfter)
1278 {
1279 return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text,
1280 image, selImage, NULL);
1281 }
1282
1283 wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
1284 int image, int selectedImage,
1285 wxTreeItemData *data)
1286 {
1287 return DoInsertItem(wxTreeItemId((WXHTREEITEM) 0), (WXHTREEITEM) 0,
1288 text, image, selectedImage, data);
1289 }
1290
1291 wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
1292 const wxString& text,
1293 int image, int selectedImage,
1294 wxTreeItemData *data)
1295 {
1296 return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST,
1297 text, image, selectedImage, data);
1298 }
1299
1300 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1301 const wxTreeItemId& idPrevious,
1302 const wxString& text,
1303 int image, int selectedImage,
1304 wxTreeItemData *data)
1305 {
1306 return DoInsertItem(parent, idPrevious, text, image, selectedImage, data);
1307 }
1308
1309 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1310 size_t index,
1311 const wxString& text,
1312 int image, int selectedImage,
1313 wxTreeItemData *data)
1314 {
1315 // find the item from index
1316 long cookie;
1317 wxTreeItemId idPrev, idCur = GetFirstChild(parent, cookie);
1318 while ( index != 0 && idCur.IsOk() )
1319 {
1320 index--;
1321
1322 idPrev = idCur;
1323 idCur = GetNextChild(parent, cookie);
1324 }
1325
1326 // assert, not check: if the index is invalid, we will append the item
1327 // to the end
1328 wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") );
1329
1330 return DoInsertItem(parent, idPrev, text, image, selectedImage, data);
1331 }
1332
1333 wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
1334 const wxString& text,
1335 int image, int selectedImage,
1336 wxTreeItemData *data)
1337 {
1338 return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST,
1339 text, image, selectedImage, data);
1340 }
1341
1342 void wxTreeCtrl::Delete(const wxTreeItemId& item)
1343 {
1344 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) )
1345 {
1346 wxLogLastError("TreeView_DeleteItem");
1347 }
1348 }
1349
1350 // delete all children (but don't delete the item itself)
1351 void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
1352 {
1353 long cookie;
1354
1355 wxArrayLong children;
1356 wxTreeItemId child = GetFirstChild(item, cookie);
1357 while ( child.IsOk() )
1358 {
1359 children.Add((long)(WXHTREEITEM)child);
1360
1361 child = GetNextChild(item, cookie);
1362 }
1363
1364 size_t nCount = children.Count();
1365 for ( size_t n = 0; n < nCount; n++ )
1366 {
1367 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)children[n]) )
1368 {
1369 wxLogLastError("TreeView_DeleteItem");
1370 }
1371 }
1372 }
1373
1374 void wxTreeCtrl::DeleteAllItems()
1375 {
1376 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1377 {
1378 wxLogLastError("TreeView_DeleteAllItems");
1379 }
1380 }
1381
1382 void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
1383 {
1384 wxASSERT_MSG( flag == TVE_COLLAPSE ||
1385 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
1386 flag == TVE_EXPAND ||
1387 flag == TVE_TOGGLE,
1388 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1389
1390 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1391 // emulate them. This behaviour has changed slightly with comctl32.dll
1392 // v 4.70 - now it does send them but only the first time. To maintain
1393 // compatible behaviour and also in order to not have surprises with the
1394 // future versions, don't rely on this and still do everything ourselves.
1395 // To avoid that the messages be sent twice when the item is expanded for
1396 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1397
1398 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE);
1399 tvItem.state = 0;
1400 DoSetItem(&tvItem);
1401
1402 if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) != 0 )
1403 {
1404 wxTreeEvent event(wxEVT_NULL, m_windowId);
1405 event.m_item = item;
1406
1407 bool isExpanded = IsExpanded(item);
1408
1409 event.SetEventObject(this);
1410
1411 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1412 event.SetEventType(g_events[isExpanded][TRUE]);
1413 GetEventHandler()->ProcessEvent(event);
1414
1415 event.SetEventType(g_events[isExpanded][FALSE]);
1416 GetEventHandler()->ProcessEvent(event);
1417 }
1418 //else: change didn't took place, so do nothing at all
1419 }
1420
1421 void wxTreeCtrl::Expand(const wxTreeItemId& item)
1422 {
1423 DoExpand(item, TVE_EXPAND);
1424 }
1425
1426 void wxTreeCtrl::Collapse(const wxTreeItemId& item)
1427 {
1428 DoExpand(item, TVE_COLLAPSE);
1429 }
1430
1431 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1432 {
1433 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
1434 }
1435
1436 void wxTreeCtrl::Toggle(const wxTreeItemId& item)
1437 {
1438 DoExpand(item, TVE_TOGGLE);
1439 }
1440
1441 void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
1442 {
1443 DoExpand(item, action);
1444 }
1445
1446 void wxTreeCtrl::Unselect()
1447 {
1448 wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE),
1449 wxT("doesn't make sense, may be you want UnselectAll()?") );
1450
1451 // just remove the selection
1452 SelectItem(wxTreeItemId((WXHTREEITEM) 0));
1453 }
1454
1455 void wxTreeCtrl::UnselectAll()
1456 {
1457 if ( m_windowStyle & wxTR_MULTIPLE )
1458 {
1459 wxArrayTreeItemIds selections;
1460 size_t count = GetSelections(selections);
1461 for ( size_t n = 0; n < count; n++ )
1462 {
1463 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1464 SetItemCheck(selections[n], FALSE);
1465 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1466 ::UnselectItem(GetHwnd(), HITEM(selections[n]));
1467 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1468 }
1469 }
1470 else
1471 {
1472 // just remove the selection
1473 Unselect();
1474 }
1475 }
1476
1477 void wxTreeCtrl::SelectItem(const wxTreeItemId& item)
1478 {
1479 if ( m_windowStyle & wxTR_MULTIPLE )
1480 {
1481 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1482 // selecting the item means checking it
1483 SetItemCheck(item);
1484 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1485 ::SelectItem(GetHwnd(), HITEM(item));
1486 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1487 }
1488 else
1489 {
1490 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1491 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1492 // send them ourselves
1493
1494 wxTreeEvent event(wxEVT_NULL, m_windowId);
1495 event.m_item = item;
1496 event.SetEventObject(this);
1497
1498 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING);
1499 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
1500 {
1501 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item)) )
1502 {
1503 wxLogLastError("TreeView_SelectItem");
1504 }
1505 else
1506 {
1507 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1508 (void)GetEventHandler()->ProcessEvent(event);
1509 }
1510 }
1511 //else: program vetoed the change
1512 }
1513 }
1514
1515 void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1516 {
1517 // no error return
1518 TreeView_EnsureVisible(GetHwnd(), HITEM(item));
1519 }
1520
1521 void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
1522 {
1523 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) )
1524 {
1525 wxLogLastError("TreeView_SelectSetFirstVisible");
1526 }
1527 }
1528
1529 wxTextCtrl* wxTreeCtrl::GetEditControl() const
1530 {
1531 // normally, we could try to do something like this to return something
1532 // even when the editing was started by the user and not by calling
1533 // EditLabel() - but as nobody has asked for this so far and there might be
1534 // problems in the code below, I leave it disabled for now (VZ)
1535 #if 0
1536 if ( !m_textCtrl )
1537 {
1538 HWND hwndText = TreeView_GetEditControl(GetHwnd());
1539 if ( hwndText )
1540 {
1541 m_textCtrl = new wxTextCtrl(this, -1);
1542 m_textCtrl->Hide();
1543 m_textCtrl->SetHWND((WXHWND)hwndText);
1544 }
1545 //else: not editing label right now
1546 }
1547 #endif // 0
1548
1549 return m_textCtrl;
1550 }
1551
1552 void wxTreeCtrl::DeleteTextCtrl()
1553 {
1554 if ( m_textCtrl )
1555 {
1556 m_textCtrl->UnsubclassWin();
1557 m_textCtrl->SetHWND(0);
1558 delete m_textCtrl;
1559 m_textCtrl = NULL;
1560 }
1561 }
1562
1563 wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
1564 wxClassInfo* textControlClass)
1565 {
1566 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
1567
1568 DeleteTextCtrl();
1569
1570 HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item));
1571
1572 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1573 // returned FALSE
1574 if ( !hWnd )
1575 {
1576 return NULL;
1577 }
1578
1579 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
1580 m_textCtrl->SetHWND((WXHWND)hWnd);
1581 m_textCtrl->SubclassWin((WXHWND)hWnd);
1582
1583 return m_textCtrl;
1584 }
1585
1586 // End label editing, optionally cancelling the edit
1587 void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges)
1588 {
1589 TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
1590
1591 DeleteTextCtrl();
1592 }
1593
1594 wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
1595 {
1596 TV_HITTESTINFO hitTestInfo;
1597 hitTestInfo.pt.x = (int)point.x;
1598 hitTestInfo.pt.y = (int)point.y;
1599
1600 TreeView_HitTest(GetHwnd(), &hitTestInfo);
1601
1602 flags = 0;
1603
1604 // avoid repetition
1605 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1606 flags |= wxTREE_HITTEST_##flag
1607
1608 TRANSLATE_FLAG(ABOVE);
1609 TRANSLATE_FLAG(BELOW);
1610 TRANSLATE_FLAG(NOWHERE);
1611 TRANSLATE_FLAG(ONITEMBUTTON);
1612 TRANSLATE_FLAG(ONITEMICON);
1613 TRANSLATE_FLAG(ONITEMINDENT);
1614 TRANSLATE_FLAG(ONITEMLABEL);
1615 TRANSLATE_FLAG(ONITEMRIGHT);
1616 TRANSLATE_FLAG(ONITEMSTATEICON);
1617 TRANSLATE_FLAG(TOLEFT);
1618 TRANSLATE_FLAG(TORIGHT);
1619
1620 #undef TRANSLATE_FLAG
1621
1622 return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem);
1623 }
1624
1625 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
1626 wxRect& rect,
1627 bool textOnly) const
1628 {
1629 RECT rc;
1630 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item),
1631 &rc, textOnly) )
1632 {
1633 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
1634
1635 return TRUE;
1636 }
1637 else
1638 {
1639 // couldn't retrieve rect: for example, item isn't visible
1640 return FALSE;
1641 }
1642 }
1643
1644 // ----------------------------------------------------------------------------
1645 // sorting stuff
1646 // ----------------------------------------------------------------------------
1647
1648 static int CALLBACK TreeView_CompareCallback(wxTreeItemData *pItem1,
1649 wxTreeItemData *pItem2,
1650 wxTreeCtrl *tree)
1651 {
1652 wxCHECK_MSG( pItem1 && pItem2, 0,
1653 wxT("sorting tree without data doesn't make sense") );
1654
1655 return tree->OnCompareItems(pItem1->GetId(), pItem2->GetId());
1656 }
1657
1658 int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1659 const wxTreeItemId& item2)
1660 {
1661 return wxStrcmp(GetItemText(item1), GetItemText(item2));
1662 }
1663
1664 void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
1665 {
1666 // rely on the fact that TreeView_SortChildren does the same thing as our
1667 // default behaviour, i.e. sorts items alphabetically and so call it
1668 // directly if we're not in derived class (much more efficient!)
1669 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
1670 {
1671 TreeView_SortChildren(GetHwnd(), HITEM(item), 0);
1672 }
1673 else
1674 {
1675 TV_SORTCB tvSort;
1676 tvSort.hParent = HITEM(item);
1677 tvSort.lpfnCompare = (PFNTVCOMPARE)TreeView_CompareCallback;
1678 tvSort.lParam = (LPARAM)this;
1679 TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
1680 }
1681 }
1682
1683 // ----------------------------------------------------------------------------
1684 // implementation
1685 // ----------------------------------------------------------------------------
1686
1687 bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1688 {
1689 if ( cmd == EN_UPDATE )
1690 {
1691 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1692 event.SetEventObject( this );
1693 ProcessCommand(event);
1694 }
1695 else if ( cmd == EN_KILLFOCUS )
1696 {
1697 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1698 event.SetEventObject( this );
1699 ProcessCommand(event);
1700 }
1701 else
1702 {
1703 // nothing done
1704 return FALSE;
1705 }
1706
1707 // command processed
1708 return TRUE;
1709 }
1710
1711 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1712 // only do it during dragging, minimize wxWin overhead (this is important for
1713 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1714 // instead of passing by wxWin events
1715 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
1716 {
1717 bool processed = FALSE;
1718 long rc = 0;
1719
1720 bool isMultiple = (GetWindowStyle() & wxTR_MULTIPLE) != 0;
1721
1722 if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
1723 {
1724 // we only process mouse messages here and these parameters have the same
1725 // meaning for all of them
1726 int x = GET_X_LPARAM(lParam),
1727 y = GET_Y_LPARAM(lParam);
1728 HTREEITEM htItem = GetItemFromPoint(GetHwnd(), x, y);
1729
1730 switch ( nMsg )
1731 {
1732 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1733 case WM_LBUTTONDOWN:
1734 if ( htItem && isMultiple )
1735 {
1736 if ( wParam & MK_CONTROL )
1737 {
1738 SetFocus();
1739
1740 // toggle selected state
1741 ToggleItemSelection(GetHwnd(), htItem);
1742
1743 ::SetFocus(GetHwnd(), htItem);
1744
1745 // reset on any click without Shift
1746 m_htSelStart = 0;
1747
1748 processed = TRUE;
1749 }
1750 else if ( wParam & MK_SHIFT )
1751 {
1752 // this selects all items between the starting one and
1753 // the current
1754
1755 if ( !m_htSelStart )
1756 {
1757 // take the focused item
1758 m_htSelStart = (WXHTREEITEM)
1759 TreeView_GetSelection(GetHwnd());
1760 }
1761
1762 SelectRange(GetHwnd(), HITEM(m_htSelStart), htItem,
1763 !(wParam & MK_CONTROL));
1764
1765 ::SetFocus(GetHwnd(), htItem);
1766
1767 processed = TRUE;
1768 }
1769 else // normal click
1770 {
1771 // clear the selection and then let the default handler
1772 // do the job
1773 UnselectAll();
1774
1775 // prevent the click from starting in-place editing
1776 // when there was no selection in the control
1777 TreeView_SelectItem(GetHwnd(), 0);
1778
1779 // reset on any click without Shift
1780 m_htSelStart = 0;
1781 }
1782 }
1783 break;
1784 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1785
1786 case WM_MOUSEMOVE:
1787 if ( m_dragImage )
1788 {
1789 m_dragImage->Move(wxPoint(x, y));
1790 if ( htItem )
1791 {
1792 // highlight the item as target (hiding drag image is
1793 // necessary - otherwise the display will be corrupted)
1794 m_dragImage->Hide();
1795 TreeView_SelectDropTarget(GetHwnd(), htItem);
1796 m_dragImage->Show();
1797 }
1798 }
1799 break;
1800
1801 case WM_LBUTTONUP:
1802 case WM_RBUTTONUP:
1803 if ( m_dragImage )
1804 {
1805 m_dragImage->EndDrag();
1806 delete m_dragImage;
1807 m_dragImage = NULL;
1808
1809 // generate the drag end event
1810 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, m_windowId);
1811
1812 event.m_item = (WXHTREEITEM)htItem;
1813 event.m_pointDrag = wxPoint(x, y);
1814 event.SetEventObject(this);
1815
1816 (void)GetEventHandler()->ProcessEvent(event);
1817
1818 // if we don't do it, the tree seems to think that 2 items
1819 // are selected simultaneously which is quite weird
1820 TreeView_SelectDropTarget(GetHwnd(), 0);
1821 }
1822 break;
1823 }
1824 }
1825 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1826 else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) && isMultiple )
1827 {
1828 // the tree control greys out the selected item when it loses focus and
1829 // paints it as selected again when it regains it, but it won't do it
1830 // for the other items itself - help it
1831 wxArrayTreeItemIds selections;
1832 size_t count = GetSelections(selections);
1833 RECT rect;
1834 for ( size_t n = 0; n < count; n++ )
1835 {
1836 // TreeView_GetItemRect() will return FALSE if item is not visible,
1837 // which may happen perfectly well
1838 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections[n]),
1839 &rect, TRUE) )
1840 {
1841 ::InvalidateRect(GetHwnd(), &rect, FALSE);
1842 }
1843 }
1844 }
1845 else if ( nMsg == WM_KEYDOWN && isMultiple )
1846 {
1847 bool bCtrl = wxIsCtrlDown(),
1848 bShift = wxIsShiftDown();
1849
1850 // we handle.arrows and space, but not page up/down and home/end: the
1851 // latter should be easy, but not the former
1852
1853 HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
1854 if ( !m_htSelStart )
1855 {
1856 m_htSelStart = (WXHTREEITEM)htSel;
1857 }
1858
1859 if ( wParam == VK_SPACE )
1860 {
1861 if ( bCtrl )
1862 {
1863 ToggleItemSelection(GetHwnd(), htSel);
1864 }
1865 else
1866 {
1867 UnselectAll();
1868
1869 ::SelectItem(GetHwnd(), htSel);
1870 }
1871
1872 processed = TRUE;
1873 }
1874 else if ( wParam == VK_UP || wParam == VK_DOWN )
1875 {
1876 if ( !bCtrl && !bShift )
1877 {
1878 // no modifiers, just clear selection and then let the default
1879 // processing to take place
1880 UnselectAll();
1881 }
1882 else if ( htSel )
1883 {
1884 (void)wxControl::MSWWindowProc(nMsg, wParam, lParam);
1885
1886 HTREEITEM htNext = (HTREEITEM)(wParam == VK_UP
1887 ? TreeView_GetPrevVisible(GetHwnd(), htSel)
1888 : TreeView_GetNextVisible(GetHwnd(), htSel));
1889
1890 if ( !htNext )
1891 {
1892 // at the top/bottom
1893 htNext = htSel;
1894 }
1895
1896 if ( bShift )
1897 {
1898 SelectRange(GetHwnd(), HITEM(m_htSelStart), htNext);
1899 }
1900 else // bCtrl
1901 {
1902 // without changing selection
1903 ::SetFocus(GetHwnd(), htNext);
1904 }
1905
1906 processed = TRUE;
1907 }
1908 }
1909 }
1910 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1911
1912 if ( !processed )
1913 rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
1914
1915 return rc;
1916 }
1917
1918 // process WM_NOTIFY Windows message
1919 bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1920 {
1921 wxTreeEvent event(wxEVT_NULL, m_windowId);
1922 wxEventType eventType = wxEVT_NULL;
1923 NMHDR *hdr = (NMHDR *)lParam;
1924
1925 switch ( hdr->code )
1926 {
1927 case NM_RCLICK:
1928 {
1929 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
1930 return TRUE;
1931
1932 TV_HITTESTINFO tvhti;
1933 ::GetCursorPos(&(tvhti.pt));
1934 ::ScreenToClient(GetHwnd(),&(tvhti.pt));
1935 if ( TreeView_HitTest(GetHwnd(),&tvhti) )
1936 {
1937 if( tvhti.flags & TVHT_ONITEM )
1938 {
1939 event.m_item = (WXHTREEITEM) tvhti.hItem;
1940 eventType = wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
1941 }
1942 }
1943 }
1944 break;
1945
1946 case TVN_BEGINDRAG:
1947 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
1948 // fall through
1949
1950 case TVN_BEGINRDRAG:
1951 {
1952 if ( eventType == wxEVT_NULL )
1953 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
1954 //else: left drag, already set above
1955
1956 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
1957
1958 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
1959 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
1960
1961 // don't allow dragging by default: the user code must
1962 // explicitly say that it wants to allow it to avoid breaking
1963 // the old apps
1964 event.Veto();
1965 }
1966 break;
1967
1968 case TVN_BEGINLABELEDIT:
1969 {
1970 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
1971 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1972
1973 event.m_item = (WXHTREEITEM) info->item.hItem;
1974 event.m_label = info->item.pszText;
1975 }
1976 break;
1977
1978 case TVN_DELETEITEM:
1979 {
1980 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
1981 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
1982
1983 event.m_item = (WXHTREEITEM)tv->itemOld.hItem;
1984
1985 if ( m_hasAnyAttr )
1986 {
1987 delete (wxTreeItemAttr *)m_attrs.
1988 Delete((long)tv->itemOld.hItem);
1989 }
1990 }
1991 break;
1992
1993 case TVN_ENDLABELEDIT:
1994 {
1995 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
1996 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1997
1998 event.m_item = (WXHTREEITEM)info->item.hItem;
1999 event.m_label = info->item.pszText;
2000 if (info->item.pszText == NULL)
2001 return FALSE;
2002 break;
2003 }
2004
2005 case TVN_GETDISPINFO:
2006 eventType = wxEVT_COMMAND_TREE_GET_INFO;
2007 // fall through
2008
2009 case TVN_SETDISPINFO:
2010 {
2011 if ( eventType == wxEVT_NULL )
2012 eventType = wxEVT_COMMAND_TREE_SET_INFO;
2013 //else: get, already set above
2014
2015 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2016
2017 event.m_item = (WXHTREEITEM) info->item.hItem;
2018 break;
2019 }
2020
2021 case TVN_ITEMEXPANDING:
2022 event.m_code = FALSE;
2023 // fall through
2024
2025 case TVN_ITEMEXPANDED:
2026 {
2027 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
2028
2029 bool expand = FALSE;
2030 switch ( tv->action )
2031 {
2032 case TVE_EXPAND:
2033 expand = TRUE;
2034 break;
2035
2036 case TVE_COLLAPSE:
2037 expand = FALSE;
2038 break;
2039
2040 default:
2041 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
2042 "message"), tv->action);
2043 }
2044
2045 bool ing = ((int)hdr->code == TVN_ITEMEXPANDING);
2046 eventType = g_events[expand][ing];
2047
2048 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
2049 }
2050 break;
2051
2052 case TVN_KEYDOWN:
2053 {
2054 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
2055 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
2056
2057 event.m_code = wxCharCodeMSWToWX(info->wVKey);
2058
2059 // a separate event for Space/Return
2060 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2061 ((info->wVKey == VK_SPACE) || (info->wVKey == VK_RETURN)) )
2062 {
2063 wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
2064 m_windowId);
2065 event2.SetEventObject(this);
2066 if ( !(GetWindowStyle() & wxTR_MULTIPLE) )
2067 {
2068 event2.m_item = GetSelection();
2069 }
2070 //else: don't know how to get it
2071
2072 (void)GetEventHandler()->ProcessEvent(event2);
2073 }
2074 }
2075 break;
2076
2077 case TVN_SELCHANGED:
2078 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
2079 // fall through
2080
2081 case TVN_SELCHANGING:
2082 {
2083 if ( eventType == wxEVT_NULL )
2084 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
2085 //else: already set above
2086
2087 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
2088
2089 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
2090 event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem;
2091 }
2092 break;
2093
2094 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
2095 case NM_CUSTOMDRAW:
2096 {
2097 LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam;
2098 NMCUSTOMDRAW& nmcd = lptvcd->nmcd;
2099 switch( nmcd.dwDrawStage )
2100 {
2101 case CDDS_PREPAINT:
2102 // if we've got any items with non standard attributes,
2103 // notify us before painting each item
2104 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
2105 : CDRF_DODEFAULT;
2106 return TRUE;
2107
2108 case CDDS_ITEMPREPAINT:
2109 {
2110 wxTreeItemAttr *attr =
2111 (wxTreeItemAttr *)m_attrs.Get(nmcd.dwItemSpec);
2112
2113 if ( !attr )
2114 {
2115 // nothing to do for this item
2116 return CDRF_DODEFAULT;
2117 }
2118
2119 HFONT hFont;
2120 wxColour colText, colBack;
2121 if ( attr->HasFont() )
2122 {
2123 wxFont font = attr->GetFont();
2124 hFont = (HFONT)font.GetResourceHandle();
2125 }
2126 else
2127 {
2128 hFont = 0;
2129 }
2130
2131 if ( attr->HasTextColour() )
2132 {
2133 colText = attr->GetTextColour();
2134 }
2135 else
2136 {
2137 colText = GetForegroundColour();
2138 }
2139
2140 // selection colours should override ours
2141 if ( nmcd.uItemState & CDIS_SELECTED )
2142 {
2143 DWORD clrBk = ::GetSysColor(COLOR_HIGHLIGHT);
2144 lptvcd->clrTextBk = clrBk;
2145
2146 // try to make the text visible
2147 lptvcd->clrText = wxColourToRGB(colText);
2148 lptvcd->clrText |= ~clrBk;
2149 lptvcd->clrText &= 0x00ffffff;
2150 }
2151 else
2152 {
2153 if ( attr->HasBackgroundColour() )
2154 {
2155 colBack = attr->GetBackgroundColour();
2156 }
2157 else
2158 {
2159 colBack = GetBackgroundColour();
2160 }
2161
2162 lptvcd->clrText = wxColourToRGB(colText);
2163 lptvcd->clrTextBk = wxColourToRGB(colBack);
2164 }
2165
2166 // note that if we wanted to set colours for
2167 // individual columns (subitems), we would have
2168 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2169 if ( hFont )
2170 {
2171 ::SelectObject(nmcd.hdc, hFont);
2172
2173 *result = CDRF_NEWFONT;
2174 }
2175 else
2176 {
2177 *result = CDRF_DODEFAULT;
2178 }
2179
2180 return TRUE;
2181 }
2182
2183 default:
2184 *result = CDRF_DODEFAULT;
2185 return TRUE;
2186 }
2187 }
2188 break;
2189 #endif // _WIN32_IE >= 0x300
2190
2191 default:
2192 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2193 }
2194
2195 event.SetEventObject(this);
2196 event.SetEventType(eventType);
2197
2198 bool processed = GetEventHandler()->ProcessEvent(event);
2199
2200 // post processing
2201 switch ( hdr->code )
2202 {
2203 case TVN_BEGINDRAG:
2204 case TVN_BEGINRDRAG:
2205 if ( event.IsAllowed() )
2206 {
2207 // normally this is impossible because the m_dragImage is
2208 // deleted once the drag operation is over
2209 wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") );
2210
2211 m_dragImage = new wxDragImage(*this, event.m_item);
2212 m_dragImage->BeginDrag(wxPoint(0, 0), this);
2213 m_dragImage->Show();
2214 }
2215 break;
2216
2217 case TVN_DELETEITEM:
2218 {
2219 // NB: we might process this message using wxWindows event
2220 // tables, but due to overhead of wxWin event system we
2221 // prefer to do it here ourself (otherwise deleting a tree
2222 // with many items is just too slow)
2223 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
2224
2225 wxTreeItemId item = event.m_item;
2226 if ( HasIndirectData(item) )
2227 {
2228 wxTreeItemIndirectData *data = (wxTreeItemIndirectData *)
2229 tv->itemOld.lParam;
2230 delete data; // can't be NULL here
2231
2232 m_itemsWithIndirectData.Remove(item);
2233 }
2234 else
2235 {
2236 wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
2237 delete data; // may be NULL, ok
2238 }
2239
2240 processed = TRUE; // Make sure we don't get called twice
2241 }
2242 break;
2243
2244 case TVN_BEGINLABELEDIT:
2245 // return TRUE to cancel label editing
2246 *result = !event.IsAllowed();
2247 break;
2248
2249 case TVN_ENDLABELEDIT:
2250 // return TRUE to set the label to the new string
2251 *result = event.IsAllowed();
2252
2253 // ensure that we don't have the text ctrl which is going to be
2254 // deleted any more
2255 DeleteTextCtrl();
2256 break;
2257
2258 case TVN_SELCHANGING:
2259 case TVN_ITEMEXPANDING:
2260 // return TRUE to prevent the action from happening
2261 *result = !event.IsAllowed();
2262 break;
2263
2264 case TVN_GETDISPINFO:
2265 // NB: so far the user can't set the image himself anyhow, so do it
2266 // anyway - but this may change later
2267 if ( /* !processed && */ 1 )
2268 {
2269 wxTreeItemId item = event.m_item;
2270 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2271 if ( info->item.mask & TVIF_IMAGE )
2272 {
2273 info->item.iImage =
2274 DoGetItemImageFromData
2275 (
2276 item,
2277 IsExpanded(item) ? wxTreeItemIcon_Expanded
2278 : wxTreeItemIcon_Normal
2279 );
2280 }
2281 if ( info->item.mask & TVIF_SELECTEDIMAGE )
2282 {
2283 info->item.iSelectedImage =
2284 DoGetItemImageFromData
2285 (
2286 item,
2287 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
2288 : wxTreeItemIcon_Selected
2289 );
2290 }
2291 }
2292 break;
2293
2294 //default:
2295 // for the other messages the return value is ignored and there is
2296 // nothing special to do
2297 }
2298
2299 return processed;
2300 }
2301
2302 // ----------------------------------------------------------------------------
2303 // Tree event
2304 // ----------------------------------------------------------------------------
2305
2306 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
2307
2308 wxTreeEvent::wxTreeEvent(wxEventType commandType, int id)
2309 : wxNotifyEvent(commandType, id)
2310 {
2311 m_code = 0;
2312 m_itemOld = 0;
2313 }
2314
2315 #endif // __WIN95__
2316