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