]> git.saurik.com Git - wxWidgets.git/blob - src/msw/treectrl.cpp
fix for GetParent() in wxTR_HIDE_ROOT case
[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;
1302
1303 if ( IS_VIRTUAL_ROOT(item) )
1304 {
1305 // no parent for the virtual root
1306 hItem = 0;
1307 }
1308 else // normal item
1309 {
1310 hItem = TreeView_GetParent(GetHwnd(), HITEM(item));
1311 if ( !hItem && HasFlag(wxTR_HIDE_ROOT) )
1312 {
1313 // the top level items should have the virtual root as their parent
1314 hItem = TVI_ROOT;
1315 }
1316 }
1317
1318 return wxTreeItemId((WXHTREEITEM)hItem);
1319 }
1320
1321 wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1322 long& _cookie) const
1323 {
1324 // remember the last child returned in 'cookie'
1325 _cookie = (long)TreeView_GetChild(GetHwnd(), HITEM(item));
1326
1327 return wxTreeItemId((WXHTREEITEM)_cookie);
1328 }
1329
1330 wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
1331 long& _cookie) const
1332 {
1333 wxTreeItemId l = wxTreeItemId((WXHTREEITEM)TreeView_GetNextSibling(GetHwnd(),
1334 HITEM(_cookie)));
1335 _cookie = (long)l;
1336
1337 return l;
1338 }
1339
1340 wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
1341 {
1342 // can this be done more efficiently?
1343 long cookie;
1344
1345 wxTreeItemId childLast,
1346 child = GetFirstChild(item, cookie);
1347 while ( child.IsOk() )
1348 {
1349 childLast = child;
1350 child = GetNextChild(item, cookie);
1351 }
1352
1353 return childLast;
1354 }
1355
1356 wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
1357 {
1358 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(GetHwnd(), HITEM(item)));
1359 }
1360
1361 wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
1362 {
1363 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(GetHwnd(), HITEM(item)));
1364 }
1365
1366 wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
1367 {
1368 return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(GetHwnd()));
1369 }
1370
1371 wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
1372 {
1373 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() for must be visible itself!"));
1374
1375 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(GetHwnd(), HITEM(item)));
1376 }
1377
1378 wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1379 {
1380 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1381
1382 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(GetHwnd(), HITEM(item)));
1383 }
1384
1385 // ----------------------------------------------------------------------------
1386 // multiple selections emulation
1387 // ----------------------------------------------------------------------------
1388
1389 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const
1390 {
1391 // receive the desired information.
1392 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1393 DoGetItem(&tvItem);
1394
1395 // state image indices are 1 based
1396 return ((tvItem.state >> 12) - 1) == 1;
1397 }
1398
1399 void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check)
1400 {
1401 // receive the desired information.
1402 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1403
1404 DoGetItem(&tvItem);
1405
1406 // state images are one-based
1407 tvItem.state = (check ? 2 : 1) << 12;
1408
1409 DoSetItem(&tvItem);
1410 }
1411
1412 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
1413 {
1414 TraverseSelections selector(this, selections);
1415
1416 return selector.GetCount();
1417 }
1418
1419 // ----------------------------------------------------------------------------
1420 // Usual operations
1421 // ----------------------------------------------------------------------------
1422
1423 wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
1424 wxTreeItemId hInsertAfter,
1425 const wxString& text,
1426 int image, int selectedImage,
1427 wxTreeItemData *data)
1428 {
1429 wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()),
1430 wxTreeItemId(),
1431 _T("can't have more than one root in the tree") );
1432
1433 TV_INSERTSTRUCT tvIns;
1434 tvIns.hParent = HITEM(parent);
1435 tvIns.hInsertAfter = HITEM(hInsertAfter);
1436
1437 // this is how we insert the item as the first child: supply a NULL
1438 // hInsertAfter
1439 if ( !tvIns.hInsertAfter )
1440 {
1441 tvIns.hInsertAfter = TVI_FIRST;
1442 }
1443
1444 UINT mask = 0;
1445 if ( !text.IsEmpty() )
1446 {
1447 mask |= TVIF_TEXT;
1448 tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok
1449 }
1450 else
1451 {
1452 tvIns.item.pszText = NULL;
1453 tvIns.item.cchTextMax = 0;
1454 }
1455
1456 if ( image != -1 )
1457 {
1458 mask |= TVIF_IMAGE;
1459 tvIns.item.iImage = image;
1460
1461 if ( selectedImage == -1 )
1462 {
1463 // take the same image for selected icon if not specified
1464 selectedImage = image;
1465 }
1466 }
1467
1468 if ( selectedImage != -1 )
1469 {
1470 mask |= TVIF_SELECTEDIMAGE;
1471 tvIns.item.iSelectedImage = selectedImage;
1472 }
1473
1474 if ( data != NULL )
1475 {
1476 mask |= TVIF_PARAM;
1477 tvIns.item.lParam = (LPARAM)data;
1478 }
1479
1480 tvIns.item.mask = mask;
1481
1482 HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns);
1483 if ( id == 0 )
1484 {
1485 wxLogLastError(wxT("TreeView_InsertItem"));
1486 }
1487
1488 if ( data != NULL )
1489 {
1490 // associate the application tree item with Win32 tree item handle
1491 data->SetId((WXHTREEITEM)id);
1492 }
1493
1494 return wxTreeItemId((WXHTREEITEM)id);
1495 }
1496
1497 // for compatibility only
1498 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1499 const wxString& text,
1500 int image, int selImage,
1501 long insertAfter)
1502 {
1503 return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text,
1504 image, selImage, NULL);
1505 }
1506
1507 wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
1508 int image, int selectedImage,
1509 wxTreeItemData *data)
1510 {
1511
1512 if ( m_windowStyle & wxTR_HIDE_ROOT )
1513 {
1514 // create a virtual root item, the parent for all the others
1515 m_pVirtualRoot = new wxVirtualNode(data);
1516
1517 return TVI_ROOT;
1518 }
1519
1520 return DoInsertItem(wxTreeItemId((WXHTREEITEM) 0), (WXHTREEITEM) 0,
1521 text, image, selectedImage, data);
1522 }
1523
1524 wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
1525 const wxString& text,
1526 int image, int selectedImage,
1527 wxTreeItemData *data)
1528 {
1529 return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST,
1530 text, image, selectedImage, data);
1531 }
1532
1533 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1534 const wxTreeItemId& idPrevious,
1535 const wxString& text,
1536 int image, int selectedImage,
1537 wxTreeItemData *data)
1538 {
1539 return DoInsertItem(parent, idPrevious, text, image, selectedImage, data);
1540 }
1541
1542 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1543 size_t index,
1544 const wxString& text,
1545 int image, int selectedImage,
1546 wxTreeItemData *data)
1547 {
1548 // find the item from index
1549 long cookie;
1550 wxTreeItemId idPrev, idCur = GetFirstChild(parent, cookie);
1551 while ( index != 0 && idCur.IsOk() )
1552 {
1553 index--;
1554
1555 idPrev = idCur;
1556 idCur = GetNextChild(parent, cookie);
1557 }
1558
1559 // assert, not check: if the index is invalid, we will append the item
1560 // to the end
1561 wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") );
1562
1563 return DoInsertItem(parent, idPrev, text, image, selectedImage, data);
1564 }
1565
1566 wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
1567 const wxString& text,
1568 int image, int selectedImage,
1569 wxTreeItemData *data)
1570 {
1571 return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST,
1572 text, image, selectedImage, data);
1573 }
1574
1575 void wxTreeCtrl::Delete(const wxTreeItemId& item)
1576 {
1577 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) )
1578 {
1579 wxLogLastError(wxT("TreeView_DeleteItem"));
1580 }
1581 }
1582
1583 // delete all children (but don't delete the item itself)
1584 void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
1585 {
1586 long cookie;
1587
1588 wxArrayLong children;
1589 wxTreeItemId child = GetFirstChild(item, cookie);
1590 while ( child.IsOk() )
1591 {
1592 children.Add((long)(WXHTREEITEM)child);
1593
1594 child = GetNextChild(item, cookie);
1595 }
1596
1597 size_t nCount = children.Count();
1598 for ( size_t n = 0; n < nCount; n++ )
1599 {
1600 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)children[n]) )
1601 {
1602 wxLogLastError(wxT("TreeView_DeleteItem"));
1603 }
1604 }
1605 }
1606
1607 void wxTreeCtrl::DeleteAllItems()
1608 {
1609 // delete stored root item.
1610 delete GET_VIRTUAL_ROOT();
1611
1612 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1613 {
1614 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1615 }
1616 }
1617
1618 void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
1619 {
1620 wxASSERT_MSG( flag == TVE_COLLAPSE ||
1621 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
1622 flag == TVE_EXPAND ||
1623 flag == TVE_TOGGLE,
1624 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1625
1626 // A hidden root can be neither expanded nor collapsed.
1627 if ( (HITEM(item) == TVI_ROOT) && (m_windowStyle & wxTR_HIDE_ROOT) )
1628 {
1629 // No action will be taken.
1630 return;
1631 }
1632
1633 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1634 // emulate them. This behaviour has changed slightly with comctl32.dll
1635 // v 4.70 - now it does send them but only the first time. To maintain
1636 // compatible behaviour and also in order to not have surprises with the
1637 // future versions, don't rely on this and still do everything ourselves.
1638 // To avoid that the messages be sent twice when the item is expanded for
1639 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1640
1641 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE);
1642 tvItem.state = 0;
1643 DoSetItem(&tvItem);
1644
1645 if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) != 0 )
1646 {
1647 wxTreeEvent event(wxEVT_NULL, m_windowId);
1648 event.m_item = item;
1649 event.SetEventObject(this);
1650
1651 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1652 // itself
1653 event.SetEventType(gs_expandEvents[IsExpanded(item) ? IDX_EXPAND
1654 : IDX_COLLAPSE]
1655 [IDX_DONE]);
1656
1657 (void)GetEventHandler()->ProcessEvent(event);
1658 }
1659 //else: change didn't took place, so do nothing at all
1660 }
1661
1662 void wxTreeCtrl::Expand(const wxTreeItemId& item)
1663 {
1664 DoExpand(item, TVE_EXPAND);
1665 }
1666
1667 void wxTreeCtrl::Collapse(const wxTreeItemId& item)
1668 {
1669 DoExpand(item, TVE_COLLAPSE);
1670 }
1671
1672 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1673 {
1674 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
1675 }
1676
1677 void wxTreeCtrl::Toggle(const wxTreeItemId& item)
1678 {
1679 DoExpand(item, TVE_TOGGLE);
1680 }
1681
1682 void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
1683 {
1684 DoExpand(item, action);
1685 }
1686
1687 void wxTreeCtrl::Unselect()
1688 {
1689 wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE),
1690 wxT("doesn't make sense, may be you want UnselectAll()?") );
1691
1692 // just remove the selection
1693 SelectItem(wxTreeItemId((long) (WXHTREEITEM) 0));
1694 }
1695
1696 void wxTreeCtrl::UnselectAll()
1697 {
1698 if ( m_windowStyle & wxTR_MULTIPLE )
1699 {
1700 wxArrayTreeItemIds selections;
1701 size_t count = GetSelections(selections);
1702 for ( size_t n = 0; n < count; n++ )
1703 {
1704 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1705 SetItemCheck(selections[n], FALSE);
1706 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1707 ::UnselectItem(GetHwnd(), HITEM(selections[n]));
1708 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1709 }
1710 }
1711 else
1712 {
1713 // just remove the selection
1714 Unselect();
1715 }
1716 }
1717
1718 void wxTreeCtrl::SelectItem(const wxTreeItemId& item)
1719 {
1720 if ( m_windowStyle & wxTR_MULTIPLE )
1721 {
1722 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1723 // selecting the item means checking it
1724 SetItemCheck(item);
1725 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1726 ::SelectItem(GetHwnd(), HITEM(item));
1727 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1728 }
1729 else
1730 {
1731 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1732 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1733 // send them ourselves
1734
1735 wxTreeEvent event(wxEVT_NULL, m_windowId);
1736 event.m_item = item;
1737 event.SetEventObject(this);
1738
1739 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING);
1740 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
1741 {
1742 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item)) )
1743 {
1744 wxLogLastError(wxT("TreeView_SelectItem"));
1745 }
1746 else
1747 {
1748 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1749 (void)GetEventHandler()->ProcessEvent(event);
1750 }
1751 }
1752 //else: program vetoed the change
1753 }
1754 }
1755
1756 void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1757 {
1758 // no error return
1759 TreeView_EnsureVisible(GetHwnd(), HITEM(item));
1760 }
1761
1762 void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
1763 {
1764 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) )
1765 {
1766 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1767 }
1768 }
1769
1770 wxTextCtrl* wxTreeCtrl::GetEditControl() const
1771 {
1772 // normally, we could try to do something like this to return something
1773 // even when the editing was started by the user and not by calling
1774 // EditLabel() - but as nobody has asked for this so far and there might be
1775 // problems in the code below, I leave it disabled for now (VZ)
1776 #if 0
1777 if ( !m_textCtrl )
1778 {
1779 HWND hwndText = TreeView_GetEditControl(GetHwnd());
1780 if ( hwndText )
1781 {
1782 m_textCtrl = new wxTextCtrl(this, -1);
1783 m_textCtrl->Hide();
1784 m_textCtrl->SetHWND((WXHWND)hwndText);
1785 }
1786 //else: not editing label right now
1787 }
1788 #endif // 0
1789
1790 return m_textCtrl;
1791 }
1792
1793 void wxTreeCtrl::DeleteTextCtrl()
1794 {
1795 if ( m_textCtrl )
1796 {
1797 // the HWND corresponding to this control is deleted by the tree
1798 // control itself and we don't know when exactly this happens, so check
1799 // if the window still exists before calling UnsubclassWin()
1800 if ( !::IsWindow(GetHwndOf(m_textCtrl)) )
1801 {
1802 m_textCtrl->SetHWND(0);
1803 }
1804
1805 m_textCtrl->UnsubclassWin();
1806 m_textCtrl->SetHWND(0);
1807 delete m_textCtrl;
1808 m_textCtrl = NULL;
1809 }
1810 }
1811
1812 wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
1813 wxClassInfo* textControlClass)
1814 {
1815 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
1816
1817 DeleteTextCtrl();
1818
1819 HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item));
1820
1821 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1822 // returned FALSE
1823 if ( !hWnd )
1824 {
1825 return NULL;
1826 }
1827
1828 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
1829 m_textCtrl->SetParent(this);
1830 m_textCtrl->SetHWND((WXHWND)hWnd);
1831 m_textCtrl->SubclassWin((WXHWND)hWnd);
1832
1833 return m_textCtrl;
1834 }
1835
1836 // End label editing, optionally cancelling the edit
1837 void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool discardChanges)
1838 {
1839 TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
1840
1841 DeleteTextCtrl();
1842 }
1843
1844 wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
1845 {
1846 TV_HITTESTINFO hitTestInfo;
1847 hitTestInfo.pt.x = (int)point.x;
1848 hitTestInfo.pt.y = (int)point.y;
1849
1850 TreeView_HitTest(GetHwnd(), &hitTestInfo);
1851
1852 flags = 0;
1853
1854 // avoid repetition
1855 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1856 flags |= wxTREE_HITTEST_##flag
1857
1858 TRANSLATE_FLAG(ABOVE);
1859 TRANSLATE_FLAG(BELOW);
1860 TRANSLATE_FLAG(NOWHERE);
1861 TRANSLATE_FLAG(ONITEMBUTTON);
1862 TRANSLATE_FLAG(ONITEMICON);
1863 TRANSLATE_FLAG(ONITEMINDENT);
1864 TRANSLATE_FLAG(ONITEMLABEL);
1865 TRANSLATE_FLAG(ONITEMRIGHT);
1866 TRANSLATE_FLAG(ONITEMSTATEICON);
1867 TRANSLATE_FLAG(TOLEFT);
1868 TRANSLATE_FLAG(TORIGHT);
1869
1870 #undef TRANSLATE_FLAG
1871
1872 return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem);
1873 }
1874
1875 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
1876 wxRect& rect,
1877 bool textOnly) const
1878 {
1879 RECT rc;
1880 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item),
1881 &rc, textOnly) )
1882 {
1883 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
1884
1885 return TRUE;
1886 }
1887 else
1888 {
1889 // couldn't retrieve rect: for example, item isn't visible
1890 return FALSE;
1891 }
1892 }
1893
1894 // ----------------------------------------------------------------------------
1895 // sorting stuff
1896 // ----------------------------------------------------------------------------
1897
1898 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1899 // functions such as IsDataIndirect()
1900 class wxTreeSortHelper
1901 {
1902 public:
1903 static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree);
1904
1905 private:
1906 static wxTreeItemId GetIdFromData(wxTreeCtrl *tree, LPARAM item)
1907 {
1908 wxTreeItemData *data = (wxTreeItemData *)item;
1909 if ( tree->IsDataIndirect(data) )
1910 {
1911 data = ((wxTreeItemIndirectData *)data)->GetData();
1912 }
1913
1914 return data->GetId();
1915 }
1916 };
1917
1918 int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1,
1919 LPARAM pItem2,
1920 LPARAM htree)
1921 {
1922 wxCHECK_MSG( pItem1 && pItem2, 0,
1923 wxT("sorting tree without data doesn't make sense") );
1924
1925 wxTreeCtrl *tree = (wxTreeCtrl *)htree;
1926
1927 return tree->OnCompareItems(GetIdFromData(tree, pItem1),
1928 GetIdFromData(tree, pItem2));
1929 }
1930
1931 int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1932 const wxTreeItemId& item2)
1933 {
1934 return wxStrcmp(GetItemText(item1), GetItemText(item2));
1935 }
1936
1937 void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
1938 {
1939 // rely on the fact that TreeView_SortChildren does the same thing as our
1940 // default behaviour, i.e. sorts items alphabetically and so call it
1941 // directly if we're not in derived class (much more efficient!)
1942 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
1943 {
1944 TreeView_SortChildren(GetHwnd(), HITEM(item), 0);
1945 }
1946 else
1947 {
1948 TV_SORTCB tvSort;
1949 tvSort.hParent = HITEM(item);
1950 tvSort.lpfnCompare = wxTreeSortHelper::Compare;
1951 tvSort.lParam = (LPARAM)this;
1952 TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
1953 }
1954 }
1955
1956 // ----------------------------------------------------------------------------
1957 // implementation
1958 // ----------------------------------------------------------------------------
1959
1960 bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1961 {
1962 if ( cmd == EN_UPDATE )
1963 {
1964 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1965 event.SetEventObject( this );
1966 ProcessCommand(event);
1967 }
1968 else if ( cmd == EN_KILLFOCUS )
1969 {
1970 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1971 event.SetEventObject( this );
1972 ProcessCommand(event);
1973 }
1974 else
1975 {
1976 // nothing done
1977 return FALSE;
1978 }
1979
1980 // command processed
1981 return TRUE;
1982 }
1983
1984 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1985 // only do it during dragging, minimize wxWin overhead (this is important for
1986 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1987 // instead of passing by wxWin events
1988 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
1989 {
1990 bool processed = FALSE;
1991 long rc = 0;
1992 bool isMultiple = (GetWindowStyle() & wxTR_MULTIPLE) != 0;
1993
1994 if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
1995 {
1996 // we only process mouse messages here and these parameters have the same
1997 // meaning for all of them
1998 int x = GET_X_LPARAM(lParam),
1999 y = GET_Y_LPARAM(lParam);
2000 HTREEITEM htItem = GetItemFromPoint(GetHwnd(), x, y);
2001
2002 switch ( nMsg )
2003 {
2004 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2005 case WM_LBUTTONDOWN:
2006 if ( htItem && isMultiple )
2007 {
2008 if ( wParam & MK_CONTROL )
2009 {
2010 SetFocus();
2011
2012 // toggle selected state
2013 ToggleItemSelection(GetHwnd(), htItem);
2014
2015 ::SetFocus(GetHwnd(), htItem);
2016
2017 // reset on any click without Shift
2018 m_htSelStart = 0;
2019
2020 processed = TRUE;
2021 }
2022 else if ( wParam & MK_SHIFT )
2023 {
2024 // this selects all items between the starting one and
2025 // the current
2026
2027 if ( !m_htSelStart )
2028 {
2029 // take the focused item
2030 m_htSelStart = (WXHTREEITEM)
2031 TreeView_GetSelection(GetHwnd());
2032 }
2033
2034 SelectRange(GetHwnd(), HITEM(m_htSelStart), htItem,
2035 !(wParam & MK_CONTROL));
2036
2037 ::SetFocus(GetHwnd(), htItem);
2038
2039 processed = TRUE;
2040 }
2041 else // normal click
2042 {
2043 // clear the selection and then let the default handler
2044 // do the job
2045 UnselectAll();
2046
2047 // prevent the click from starting in-place editing
2048 // when there was no selection in the control
2049 TreeView_SelectItem(GetHwnd(), 0);
2050
2051 // reset on any click without Shift
2052 m_htSelStart = 0;
2053 }
2054 }
2055 break;
2056 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2057
2058 case WM_MOUSEMOVE:
2059 if ( m_dragImage )
2060 {
2061 m_dragImage->Move(wxPoint(x, y));
2062 if ( htItem )
2063 {
2064 // highlight the item as target (hiding drag image is
2065 // necessary - otherwise the display will be corrupted)
2066 m_dragImage->Hide();
2067 TreeView_SelectDropTarget(GetHwnd(), htItem);
2068 m_dragImage->Show();
2069 }
2070 }
2071 break;
2072
2073 case WM_LBUTTONUP:
2074 case WM_RBUTTONUP:
2075 if ( m_dragImage )
2076 {
2077 m_dragImage->EndDrag();
2078 delete m_dragImage;
2079 m_dragImage = NULL;
2080
2081 // generate the drag end event
2082 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, m_windowId);
2083
2084 event.m_item = (WXHTREEITEM)htItem;
2085 event.m_pointDrag = wxPoint(x, y);
2086 event.SetEventObject(this);
2087
2088 (void)GetEventHandler()->ProcessEvent(event);
2089
2090 // if we don't do it, the tree seems to think that 2 items
2091 // are selected simultaneously which is quite weird
2092 TreeView_SelectDropTarget(GetHwnd(), 0);
2093 }
2094 break;
2095 }
2096 }
2097 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2098 else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) && isMultiple )
2099 {
2100 // the tree control greys out the selected item when it loses focus and
2101 // paints it as selected again when it regains it, but it won't do it
2102 // for the other items itself - help it
2103 wxArrayTreeItemIds selections;
2104 size_t count = GetSelections(selections);
2105 RECT rect;
2106 for ( size_t n = 0; n < count; n++ )
2107 {
2108 // TreeView_GetItemRect() will return FALSE if item is not visible,
2109 // which may happen perfectly well
2110 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections[n]),
2111 &rect, TRUE) )
2112 {
2113 ::InvalidateRect(GetHwnd(), &rect, FALSE);
2114 }
2115 }
2116 }
2117 else if ( nMsg == WM_KEYDOWN && isMultiple )
2118 {
2119 bool bCtrl = wxIsCtrlDown(),
2120 bShift = wxIsShiftDown();
2121
2122 // we handle.arrows and space, but not page up/down and home/end: the
2123 // latter should be easy, but not the former
2124
2125 HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
2126 if ( !m_htSelStart )
2127 {
2128 m_htSelStart = (WXHTREEITEM)htSel;
2129 }
2130
2131 if ( wParam == VK_SPACE )
2132 {
2133 if ( bCtrl )
2134 {
2135 ToggleItemSelection(GetHwnd(), htSel);
2136 }
2137 else
2138 {
2139 UnselectAll();
2140
2141 ::SelectItem(GetHwnd(), htSel);
2142 }
2143
2144 processed = TRUE;
2145 }
2146 else if ( wParam == VK_UP || wParam == VK_DOWN )
2147 {
2148 if ( !bCtrl && !bShift )
2149 {
2150 // no modifiers, just clear selection and then let the default
2151 // processing to take place
2152 UnselectAll();
2153 }
2154 else if ( htSel )
2155 {
2156 (void)wxControl::MSWWindowProc(nMsg, wParam, lParam);
2157
2158 HTREEITEM htNext = (HTREEITEM)(wParam == VK_UP
2159 ? TreeView_GetPrevVisible(GetHwnd(), htSel)
2160 : TreeView_GetNextVisible(GetHwnd(), htSel));
2161
2162 if ( !htNext )
2163 {
2164 // at the top/bottom
2165 htNext = htSel;
2166 }
2167
2168 if ( bShift )
2169 {
2170 SelectRange(GetHwnd(), HITEM(m_htSelStart), htNext);
2171 }
2172 else // bCtrl
2173 {
2174 // without changing selection
2175 ::SetFocus(GetHwnd(), htNext);
2176 }
2177
2178 processed = TRUE;
2179 }
2180 }
2181 }
2182 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2183 if ( !processed )
2184 rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
2185
2186 return rc;
2187 }
2188
2189 // process WM_NOTIFY Windows message
2190 bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
2191 {
2192 wxTreeEvent event(wxEVT_NULL, m_windowId);
2193 wxEventType eventType = wxEVT_NULL;
2194 NMHDR *hdr = (NMHDR *)lParam;
2195
2196 switch ( hdr->code )
2197 {
2198 case TVN_BEGINDRAG:
2199 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
2200 // fall through
2201
2202 case TVN_BEGINRDRAG:
2203 {
2204 if ( eventType == wxEVT_NULL )
2205 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
2206 //else: left drag, already set above
2207
2208 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2209
2210 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
2211 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
2212
2213 // don't allow dragging by default: the user code must
2214 // explicitly say that it wants to allow it to avoid breaking
2215 // the old apps
2216 event.Veto();
2217 }
2218 break;
2219
2220 case TVN_BEGINLABELEDIT:
2221 {
2222 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
2223 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2224
2225 event.m_item = (WXHTREEITEM) info->item.hItem;
2226 event.m_label = info->item.pszText;
2227 }
2228 break;
2229
2230 case TVN_DELETEITEM:
2231 {
2232 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
2233 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2234
2235 event.m_item = (WXHTREEITEM)tv->itemOld.hItem;
2236
2237 if ( m_hasAnyAttr )
2238 {
2239 delete (wxTreeItemAttr *)m_attrs.
2240 Delete((long)tv->itemOld.hItem);
2241 }
2242 }
2243 break;
2244
2245 case TVN_ENDLABELEDIT:
2246 {
2247 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
2248 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2249
2250 event.m_item = (WXHTREEITEM)info->item.hItem;
2251 event.m_label = info->item.pszText;
2252 if (info->item.pszText == NULL)
2253 return FALSE;
2254 break;
2255 }
2256
2257 case TVN_GETDISPINFO:
2258 eventType = wxEVT_COMMAND_TREE_GET_INFO;
2259 // fall through
2260
2261 case TVN_SETDISPINFO:
2262 {
2263 if ( eventType == wxEVT_NULL )
2264 eventType = wxEVT_COMMAND_TREE_SET_INFO;
2265 //else: get, already set above
2266
2267 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2268
2269 event.m_item = (WXHTREEITEM) info->item.hItem;
2270 break;
2271 }
2272
2273 case TVN_ITEMEXPANDING:
2274 case TVN_ITEMEXPANDED:
2275 {
2276 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
2277
2278 int what;
2279 switch ( tv->action )
2280 {
2281 default:
2282 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action);
2283 // fall through
2284
2285 case TVE_EXPAND:
2286 what = IDX_EXPAND;
2287 break;
2288
2289 case TVE_COLLAPSE:
2290 what = IDX_COLLAPSE;
2291 break;
2292 }
2293
2294 int how = (int)hdr->code == TVN_ITEMEXPANDING ? IDX_DOING
2295 : IDX_DONE;
2296
2297 eventType = gs_expandEvents[what][how];
2298
2299 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
2300 }
2301 break;
2302
2303 case TVN_KEYDOWN:
2304 {
2305 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
2306 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
2307
2308 // we pass 0 as last CreateKeyEvent() parameter because we
2309 // don't have access to the real key press flags here - but as
2310 // it is only used to determin wxKeyEvent::m_altDown flag it's
2311 // not too bad
2312 event.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN,
2313 wxCharCodeMSWToWX(info->wVKey),
2314 0);
2315
2316 // a separate event for Space/Return
2317 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2318 ((info->wVKey == VK_SPACE) || (info->wVKey == VK_RETURN)) )
2319 {
2320 wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
2321 m_windowId);
2322 event2.SetEventObject(this);
2323 if ( !(GetWindowStyle() & wxTR_MULTIPLE) )
2324 {
2325 event2.m_item = GetSelection();
2326 }
2327 //else: don't know how to get it
2328
2329 (void)GetEventHandler()->ProcessEvent(event2);
2330 }
2331 }
2332 break;
2333
2334 case TVN_SELCHANGED:
2335 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
2336 // fall through
2337
2338 case TVN_SELCHANGING:
2339 {
2340 if ( eventType == wxEVT_NULL )
2341 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
2342 //else: already set above
2343
2344 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
2345
2346 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
2347 event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem;
2348 }
2349 break;
2350
2351 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
2352 case NM_CUSTOMDRAW:
2353 {
2354 LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam;
2355 NMCUSTOMDRAW& nmcd = lptvcd->nmcd;
2356 switch ( nmcd.dwDrawStage )
2357 {
2358 case CDDS_PREPAINT:
2359 // if we've got any items with non standard attributes,
2360 // notify us before painting each item
2361 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
2362 : CDRF_DODEFAULT;
2363 break;
2364
2365 case CDDS_ITEMPREPAINT:
2366 {
2367 wxTreeItemAttr *attr =
2368 (wxTreeItemAttr *)m_attrs.Get(nmcd.dwItemSpec);
2369
2370 if ( !attr )
2371 {
2372 // nothing to do for this item
2373 *result = CDRF_DODEFAULT;
2374 break;
2375 }
2376
2377 HFONT hFont;
2378 wxColour colText, colBack;
2379 if ( attr->HasFont() )
2380 {
2381 wxFont font = attr->GetFont();
2382 hFont = (HFONT)font.GetResourceHandle();
2383 }
2384 else
2385 {
2386 hFont = 0;
2387 }
2388
2389 if ( attr->HasTextColour() )
2390 {
2391 colText = attr->GetTextColour();
2392 }
2393 else
2394 {
2395 colText = GetForegroundColour();
2396 }
2397
2398 // selection colours should override ours
2399 if ( nmcd.uItemState & CDIS_SELECTED )
2400 {
2401 DWORD clrBk = ::GetSysColor(COLOR_HIGHLIGHT);
2402 lptvcd->clrTextBk = clrBk;
2403
2404 // try to make the text visible
2405 lptvcd->clrText = wxColourToRGB(colText);
2406 lptvcd->clrText |= ~clrBk;
2407 lptvcd->clrText &= 0x00ffffff;
2408 }
2409 else
2410 {
2411 if ( attr->HasBackgroundColour() )
2412 {
2413 colBack = attr->GetBackgroundColour();
2414 }
2415 else
2416 {
2417 colBack = GetBackgroundColour();
2418 }
2419
2420 lptvcd->clrText = wxColourToRGB(colText);
2421 lptvcd->clrTextBk = wxColourToRGB(colBack);
2422 }
2423
2424 // note that if we wanted to set colours for
2425 // individual columns (subitems), we would have
2426 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2427 if ( hFont )
2428 {
2429 ::SelectObject(nmcd.hdc, hFont);
2430
2431 *result = CDRF_NEWFONT;
2432 }
2433 else
2434 {
2435 *result = CDRF_DODEFAULT;
2436 }
2437 }
2438 break;
2439
2440 default:
2441 *result = CDRF_DODEFAULT;
2442 }
2443 }
2444
2445 // we always process it
2446 return TRUE;
2447 #endif // _WIN32_IE >= 0x300
2448
2449 case NM_DBLCLK:
2450 case NM_RCLICK:
2451 {
2452 TV_HITTESTINFO tvhti;
2453 ::GetCursorPos(&tvhti.pt);
2454 ::ScreenToClient(GetHwnd(), &tvhti.pt);
2455 if ( TreeView_HitTest(GetHwnd(), &tvhti) )
2456 {
2457 if ( tvhti.flags & TVHT_ONITEM )
2458 {
2459 event.m_item = (WXHTREEITEM) tvhti.hItem;
2460 eventType = (int)hdr->code == NM_DBLCLK
2461 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2462 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
2463
2464 event.m_pointDrag.x = tvhti.pt.x;
2465 event.m_pointDrag.y = tvhti.pt.y;
2466 }
2467
2468 break;
2469 }
2470 }
2471 // fall through
2472
2473 default:
2474 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2475 }
2476
2477 event.SetEventObject(this);
2478 event.SetEventType(eventType);
2479
2480 bool processed = GetEventHandler()->ProcessEvent(event);
2481
2482 // post processing
2483 switch ( hdr->code )
2484 {
2485 case NM_DBLCLK:
2486 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2487 // the return code of this event handler as the return value for
2488 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2489 // expanded status would never work
2490 *result = FALSE;
2491 break;
2492
2493 case TVN_BEGINDRAG:
2494 case TVN_BEGINRDRAG:
2495 if ( event.IsAllowed() )
2496 {
2497 // normally this is impossible because the m_dragImage is
2498 // deleted once the drag operation is over
2499 wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") );
2500
2501 m_dragImage = new wxDragImage(*this, event.m_item);
2502 m_dragImage->BeginDrag(wxPoint(0, 0), this);
2503 m_dragImage->Show();
2504 }
2505 break;
2506
2507 case TVN_DELETEITEM:
2508 {
2509 // NB: we might process this message using wxWindows event
2510 // tables, but due to overhead of wxWin event system we
2511 // prefer to do it here ourself (otherwise deleting a tree
2512 // with many items is just too slow)
2513 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
2514
2515 wxTreeItemId item = event.m_item;
2516 if ( HasIndirectData(item) )
2517 {
2518 wxTreeItemIndirectData *data = (wxTreeItemIndirectData *)
2519 tv->itemOld.lParam;
2520 delete data; // can't be NULL here
2521 }
2522 else
2523 {
2524 wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
2525 delete data; // may be NULL, ok
2526 }
2527
2528 processed = TRUE; // Make sure we don't get called twice
2529 }
2530 break;
2531
2532 case TVN_BEGINLABELEDIT:
2533 // return TRUE to cancel label editing
2534 *result = !event.IsAllowed();
2535 break;
2536
2537 case TVN_ENDLABELEDIT:
2538 // return TRUE to set the label to the new string: note that we
2539 // also must pretend that we did process the message or it is going
2540 // to be passed to DefWindowProc() which will happily return FALSE
2541 // cancelling the label change
2542 *result = event.IsAllowed();
2543 processed = TRUE;
2544
2545 // ensure that we don't have the text ctrl which is going to be
2546 // deleted any more
2547 DeleteTextCtrl();
2548 break;
2549
2550 case TVN_SELCHANGING:
2551 case TVN_ITEMEXPANDING:
2552 // return TRUE to prevent the action from happening
2553 *result = !event.IsAllowed();
2554 break;
2555
2556 case TVN_ITEMEXPANDED:
2557 // the item is not refreshed properly after expansion when it has
2558 // an image depending on the expanded/collapsed state - bug in
2559 // comctl32.dll or our code?
2560 {
2561 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
2562 if ( tv->action == TVE_EXPAND )
2563 {
2564 wxTreeItemId id = (WXHTREEITEM)tv->itemNew.hItem;
2565
2566 int image = GetItemImage(id, wxTreeItemIcon_Expanded);
2567 if ( image != -1 )
2568 {
2569 RefreshItem(id);
2570 }
2571 }
2572 }
2573 break;
2574
2575 case TVN_GETDISPINFO:
2576 // NB: so far the user can't set the image himself anyhow, so do it
2577 // anyway - but this may change later
2578 // if ( /* !processed && */ 1 )
2579 {
2580 wxTreeItemId item = event.m_item;
2581 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2582 if ( info->item.mask & TVIF_IMAGE )
2583 {
2584 info->item.iImage =
2585 DoGetItemImageFromData
2586 (
2587 item,
2588 IsExpanded(item) ? wxTreeItemIcon_Expanded
2589 : wxTreeItemIcon_Normal
2590 );
2591 }
2592 if ( info->item.mask & TVIF_SELECTEDIMAGE )
2593 {
2594 info->item.iSelectedImage =
2595 DoGetItemImageFromData
2596 (
2597 item,
2598 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
2599 : wxTreeItemIcon_Selected
2600 );
2601 }
2602 }
2603 break;
2604
2605 //default:
2606 // for the other messages the return value is ignored and there is
2607 // nothing special to do
2608 }
2609 return processed;
2610 }
2611
2612 #endif // __WIN95__
2613
2614 #endif // wxUSE_TREECTRL