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