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