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