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