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