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