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