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