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