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