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