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