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