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