]> git.saurik.com Git - wxWidgets.git/blob - src/msw/treectrl.cpp
If we have multiple filters in a Save dialog, don't use
[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 ( wxTheApp->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) )
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 SetSize(pos.x, pos.y, size.x, size.y);
759
760 wxSetCCUnicodeFormat(GetHwnd());
761
762 return true;
763 }
764
765 wxTreeCtrl::~wxTreeCtrl()
766 {
767 // delete any attributes
768 if ( m_hasAnyAttr )
769 {
770 WX_CLEAR_HASH_MAP(wxMapTreeAttr, m_attrs);
771
772 // prevent TVN_DELETEITEM handler from deleting the attributes again!
773 m_hasAnyAttr = false;
774 }
775
776 DeleteTextCtrl();
777
778 // delete user data to prevent memory leaks
779 // also deletes hidden root node storage.
780 DeleteAllItems();
781
782 if (m_ownsImageListNormal) delete m_imageListNormal;
783 if (m_ownsImageListState) delete m_imageListState;
784 }
785
786 // ----------------------------------------------------------------------------
787 // accessors
788 // ----------------------------------------------------------------------------
789
790 /* static */ wxVisualAttributes
791 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
792 {
793 wxVisualAttributes attrs = GetCompositeControlsDefaultAttributes(variant);
794
795 // common controls have their own default font
796 attrs.font = wxGetCCDefaultFont();
797
798 return attrs;
799 }
800
801
802 // simple wrappers which add error checking in debug mode
803
804 bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const
805 {
806 wxCHECK_MSG( tvItem->hItem != TVI_ROOT, false,
807 _T("can't retrieve virtual root item") );
808
809 if ( !TreeView_GetItem(GetHwnd(), tvItem) )
810 {
811 wxLogLastError(wxT("TreeView_GetItem"));
812
813 return false;
814 }
815
816 return true;
817 }
818
819 void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem)
820 {
821 if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 )
822 {
823 wxLogLastError(wxT("TreeView_SetItem"));
824 }
825 }
826
827 size_t wxTreeCtrl::GetCount() const
828 {
829 return (size_t)TreeView_GetCount(GetHwnd());
830 }
831
832 unsigned int wxTreeCtrl::GetIndent() const
833 {
834 return TreeView_GetIndent(GetHwnd());
835 }
836
837 void wxTreeCtrl::SetIndent(unsigned int indent)
838 {
839 TreeView_SetIndent(GetHwnd(), indent);
840 }
841
842 wxImageList *wxTreeCtrl::GetImageList() const
843 {
844 return m_imageListNormal;
845 }
846
847 wxImageList *wxTreeCtrl::GetStateImageList() const
848 {
849 return m_imageListState;
850 }
851
852 void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
853 {
854 // no error return
855 TreeView_SetImageList(GetHwnd(),
856 imageList ? imageList->GetHIMAGELIST() : 0,
857 which);
858 }
859
860 void wxTreeCtrl::SetImageList(wxImageList *imageList)
861 {
862 if (m_ownsImageListNormal)
863 delete m_imageListNormal;
864
865 SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL);
866 m_ownsImageListNormal = false;
867 }
868
869 void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
870 {
871 if (m_ownsImageListState) delete m_imageListState;
872 SetAnyImageList(m_imageListState = imageList, TVSIL_STATE);
873 m_ownsImageListState = false;
874 }
875
876 void wxTreeCtrl::AssignImageList(wxImageList *imageList)
877 {
878 SetImageList(imageList);
879 m_ownsImageListNormal = true;
880 }
881
882 void wxTreeCtrl::AssignStateImageList(wxImageList *imageList)
883 {
884 SetStateImageList(imageList);
885 m_ownsImageListState = true;
886 }
887
888 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
889 bool recursively) const
890 {
891 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
892
893 TraverseCounter counter(this, item, recursively);
894 return counter.GetCount() - 1;
895 }
896
897 // ----------------------------------------------------------------------------
898 // control colours
899 // ----------------------------------------------------------------------------
900
901 bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour)
902 {
903 #if !wxUSE_COMCTL32_SAFELY
904 if ( !wxWindowBase::SetBackgroundColour(colour) )
905 return false;
906
907 SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel());
908 #endif
909
910 return true;
911 }
912
913 bool wxTreeCtrl::SetForegroundColour(const wxColour &colour)
914 {
915 #if !wxUSE_COMCTL32_SAFELY
916 if ( !wxWindowBase::SetForegroundColour(colour) )
917 return false;
918
919 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel());
920 #endif
921
922 return true;
923 }
924
925 // ----------------------------------------------------------------------------
926 // Item access
927 // ----------------------------------------------------------------------------
928
929 wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
930 {
931 wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
932
933 wxChar buf[512]; // the size is arbitrary...
934
935 wxTreeViewItem tvItem(item, TVIF_TEXT);
936 tvItem.pszText = buf;
937 tvItem.cchTextMax = WXSIZEOF(buf);
938 if ( !DoGetItem(&tvItem) )
939 {
940 // don't return some garbage which was on stack, but an empty string
941 buf[0] = wxT('\0');
942 }
943
944 return wxString(buf);
945 }
946
947 void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
948 {
949 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
950
951 if ( IS_VIRTUAL_ROOT(item) )
952 return;
953
954 wxTreeViewItem tvItem(item, TVIF_TEXT);
955 tvItem.pszText = (wxChar *)text.c_str(); // conversion is ok
956 DoSetItem(&tvItem);
957
958 // when setting the text of the item being edited, the text control should
959 // be updated to reflect the new text as well, otherwise calling
960 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
961 //
962 // don't use GetEditControl() here because m_textCtrl is not set yet
963 HWND hwndEdit = TreeView_GetEditControl(GetHwnd());
964 if ( hwndEdit )
965 {
966 if ( item == GetSelection() )
967 {
968 ::SetWindowText(hwndEdit, text);
969 }
970 }
971 }
972
973 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId& item,
974 wxTreeItemIcon which) const
975 {
976 wxTreeViewItem tvItem(item, TVIF_PARAM);
977 if ( !DoGetItem(&tvItem) )
978 {
979 return -1;
980 }
981
982 return ((wxTreeItemIndirectData *)tvItem.lParam)->GetImage(which);
983 }
984
985 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId& item,
986 int image,
987 wxTreeItemIcon which) const
988 {
989 wxTreeViewItem tvItem(item, TVIF_PARAM);
990 if ( !DoGetItem(&tvItem) )
991 {
992 return;
993 }
994
995 wxTreeItemIndirectData *data = ((wxTreeItemIndirectData *)tvItem.lParam);
996
997 data->SetImage(image, which);
998
999 // make sure that we have selected images as well
1000 if ( which == wxTreeItemIcon_Normal &&
1001 !data->HasImage(wxTreeItemIcon_Selected) )
1002 {
1003 data->SetImage(image, wxTreeItemIcon_Selected);
1004 }
1005
1006 if ( which == wxTreeItemIcon_Expanded &&
1007 !data->HasImage(wxTreeItemIcon_SelectedExpanded) )
1008 {
1009 data->SetImage(image, wxTreeItemIcon_SelectedExpanded);
1010 }
1011 }
1012
1013 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId& item,
1014 int image,
1015 int imageSel)
1016 {
1017 wxTreeViewItem tvItem(item, TVIF_IMAGE | TVIF_SELECTEDIMAGE);
1018 tvItem.iSelectedImage = imageSel;
1019 tvItem.iImage = image;
1020 DoSetItem(&tvItem);
1021 }
1022
1023 int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
1024 wxTreeItemIcon which) const
1025 {
1026 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
1027
1028 if ( (HITEM(item) == TVI_ROOT) && (m_windowStyle & wxTR_HIDE_ROOT) )
1029 {
1030 // TODO: Maybe a hidden root can still provide images?
1031 return -1;
1032 }
1033
1034 if ( HasIndirectData(item) )
1035 {
1036 return DoGetItemImageFromData(item, which);
1037 }
1038
1039 UINT mask;
1040 switch ( which )
1041 {
1042 default:
1043 wxFAIL_MSG( wxT("unknown tree item image type") );
1044
1045 case wxTreeItemIcon_Normal:
1046 mask = TVIF_IMAGE;
1047 break;
1048
1049 case wxTreeItemIcon_Selected:
1050 mask = TVIF_SELECTEDIMAGE;
1051 break;
1052
1053 case wxTreeItemIcon_Expanded:
1054 case wxTreeItemIcon_SelectedExpanded:
1055 return -1;
1056 }
1057
1058 wxTreeViewItem tvItem(item, mask);
1059 DoGetItem(&tvItem);
1060
1061 return mask == TVIF_IMAGE ? tvItem.iImage : tvItem.iSelectedImage;
1062 }
1063
1064 void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
1065 wxTreeItemIcon which)
1066 {
1067 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1068
1069 if ( IS_VIRTUAL_ROOT(item) )
1070 {
1071 // TODO: Maybe a hidden root can still store images?
1072 return;
1073 }
1074
1075 int imageNormal,
1076 imageSel;
1077
1078 switch ( which )
1079 {
1080 default:
1081 wxFAIL_MSG( wxT("unknown tree item image type") );
1082 // fall through
1083
1084 case wxTreeItemIcon_Normal:
1085 {
1086 const int imageNormalOld = GetItemImage(item);
1087 const int imageSelOld =
1088 GetItemImage(item, wxTreeItemIcon_Selected);
1089
1090 // always set the normal image
1091 imageNormal = image;
1092
1093 // if the selected and normal images were the same, they should
1094 // be the same after the update, otherwise leave the selected
1095 // image as it was
1096 imageSel = imageNormalOld == imageSelOld ? image : imageSelOld;
1097 }
1098 break;
1099
1100 case wxTreeItemIcon_Selected:
1101 imageNormal = GetItemImage(item);
1102 imageSel = image;
1103 break;
1104
1105 case wxTreeItemIcon_Expanded:
1106 case wxTreeItemIcon_SelectedExpanded:
1107 if ( !HasIndirectData(item) )
1108 {
1109 // we need to get the old images first, because after we create
1110 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1111 // get the images
1112 imageNormal = GetItemImage(item);
1113 imageSel = GetItemImage(item, wxTreeItemIcon_Selected);
1114
1115 // if it doesn't have it yet, add it
1116 wxTreeItemIndirectData *data = new
1117 wxTreeItemIndirectData(this, item);
1118
1119 // copy the data to the new location
1120 data->SetImage(imageNormal, wxTreeItemIcon_Normal);
1121 data->SetImage(imageSel, wxTreeItemIcon_Selected);
1122 }
1123
1124 DoSetItemImageFromData(item, image, which);
1125
1126 // reset the normal/selected images because we won't use them any
1127 // more - now they're stored inside the indirect data
1128 imageNormal =
1129 imageSel = I_IMAGECALLBACK;
1130 break;
1131 }
1132
1133 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1134 // change both normal and selected image - otherwise the change simply
1135 // doesn't take place!
1136 DoSetItemImages(item, imageNormal, imageSel);
1137 }
1138
1139 wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
1140 {
1141 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
1142
1143 wxTreeViewItem tvItem(item, TVIF_PARAM);
1144
1145 // Hidden root may have data.
1146 if ( IS_VIRTUAL_ROOT(item) )
1147 {
1148 return GET_VIRTUAL_ROOT()->GetData();
1149 }
1150
1151 // Visible node.
1152 if ( !DoGetItem(&tvItem) )
1153 {
1154 return NULL;
1155 }
1156
1157 wxTreeItemData *data = (wxTreeItemData *)tvItem.lParam;
1158 if ( IsDataIndirect(data) )
1159 {
1160 data = ((wxTreeItemIndirectData *)data)->GetData();
1161 }
1162
1163 return data;
1164 }
1165
1166 void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
1167 {
1168 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1169
1170 if ( IS_VIRTUAL_ROOT(item) )
1171 {
1172 GET_VIRTUAL_ROOT()->SetData(data);
1173 }
1174
1175 // first, associate this piece of data with this item
1176 if ( data )
1177 {
1178 data->SetId(item);
1179 }
1180
1181 wxTreeViewItem tvItem(item, TVIF_PARAM);
1182
1183 if ( HasIndirectData(item) )
1184 {
1185 if ( DoGetItem(&tvItem) )
1186 {
1187 ((wxTreeItemIndirectData *)tvItem.lParam)->SetData(data);
1188 }
1189 else
1190 {
1191 wxFAIL_MSG( wxT("failed to change tree items data") );
1192 }
1193 }
1194 else
1195 {
1196 tvItem.lParam = (LPARAM)data;
1197 DoSetItem(&tvItem);
1198 }
1199 }
1200
1201 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId& item,
1202 wxTreeItemIndirectData *data)
1203 {
1204 // this should never happen because it's unnecessary and will probably lead
1205 // to crash too because the code elsewhere supposes that the pointer the
1206 // wxTreeItemIndirectData has is a real wxItemData and not
1207 // wxTreeItemIndirectData as well
1208 wxASSERT_MSG( !HasIndirectData(item), wxT("setting indirect data twice?") );
1209
1210 SetItemData(item, data);
1211 }
1212
1213 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId& item) const
1214 {
1215 // query the item itself
1216 wxTreeViewItem tvItem(item, TVIF_PARAM);
1217 if ( !DoGetItem(&tvItem) )
1218 {
1219 return false;
1220 }
1221
1222 wxTreeItemData *data = (wxTreeItemData *)tvItem.lParam;
1223
1224 return data && IsDataIndirect(data);
1225 }
1226
1227 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
1228 {
1229 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1230
1231 if ( IS_VIRTUAL_ROOT(item) )
1232 return;
1233
1234 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1235 tvItem.cChildren = (int)has;
1236 DoSetItem(&tvItem);
1237 }
1238
1239 void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
1240 {
1241 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1242
1243 if ( IS_VIRTUAL_ROOT(item) )
1244 return;
1245
1246 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1247 tvItem.state = bold ? TVIS_BOLD : 0;
1248 DoSetItem(&tvItem);
1249 }
1250
1251 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
1252 {
1253 if ( IS_VIRTUAL_ROOT(item) )
1254 return;
1255
1256 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
1257 tvItem.state = highlight ? TVIS_DROPHILITED : 0;
1258 DoSetItem(&tvItem);
1259 }
1260
1261 void wxTreeCtrl::RefreshItem(const wxTreeItemId& item)
1262 {
1263 if ( IS_VIRTUAL_ROOT(item) )
1264 return;
1265
1266 wxRect rect;
1267 if ( GetBoundingRect(item, rect) )
1268 {
1269 RefreshRect(rect);
1270 }
1271 }
1272
1273 wxColour wxTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
1274 {
1275 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
1276
1277 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
1278 return it == m_attrs.end() ? wxNullColour : it->second->GetTextColour();
1279 }
1280
1281 wxColour wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const
1282 {
1283 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
1284
1285 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
1286 return it == m_attrs.end() ? wxNullColour : it->second->GetBackgroundColour();
1287 }
1288
1289 wxFont wxTreeCtrl::GetItemFont(const wxTreeItemId& item) const
1290 {
1291 wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") );
1292
1293 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
1294 return it == m_attrs.end() ? wxNullFont : it->second->GetFont();
1295 }
1296
1297 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
1298 const wxColour& col)
1299 {
1300 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1301
1302 wxTreeItemAttr *attr;
1303 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
1304 if ( it == m_attrs.end() )
1305 {
1306 m_hasAnyAttr = true;
1307
1308 m_attrs[item.m_pItem] =
1309 attr = new wxTreeItemAttr;
1310 }
1311 else
1312 {
1313 attr = it->second;
1314 }
1315
1316 attr->SetTextColour(col);
1317
1318 RefreshItem(item);
1319 }
1320
1321 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
1322 const wxColour& col)
1323 {
1324 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1325
1326 wxTreeItemAttr *attr;
1327 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
1328 if ( it == m_attrs.end() )
1329 {
1330 m_hasAnyAttr = true;
1331
1332 m_attrs[item.m_pItem] =
1333 attr = new wxTreeItemAttr;
1334 }
1335 else // already in the hash
1336 {
1337 attr = it->second;
1338 }
1339
1340 attr->SetBackgroundColour(col);
1341
1342 RefreshItem(item);
1343 }
1344
1345 void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
1346 {
1347 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1348
1349 wxTreeItemAttr *attr;
1350 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
1351 if ( it == m_attrs.end() )
1352 {
1353 m_hasAnyAttr = true;
1354
1355 m_attrs[item.m_pItem] =
1356 attr = new wxTreeItemAttr;
1357 }
1358 else // already in the hash
1359 {
1360 attr = it->second;
1361 }
1362
1363 attr->SetFont(font);
1364
1365 RefreshItem(item);
1366 }
1367
1368 // ----------------------------------------------------------------------------
1369 // Item status
1370 // ----------------------------------------------------------------------------
1371
1372 bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
1373 {
1374 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
1375
1376 if ( item == wxTreeItemId(TVI_ROOT) )
1377 {
1378 // virtual (hidden) root is never visible
1379 return false;
1380 }
1381
1382 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1383 RECT rect;
1384
1385 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1386 // the HTREEITEM with TVM_GETITEMRECT
1387 *(HTREEITEM *)&rect = HITEM(item);
1388
1389 // true means to get rect for just the text, not the whole line
1390 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT, true, (LPARAM)&rect) )
1391 {
1392 // if TVM_GETITEMRECT returned false, then the item is definitely not
1393 // visible (because its parent is not expanded)
1394 return false;
1395 }
1396
1397 // however if it returned true, the item might still be outside the
1398 // currently visible part of the tree, test for it (notice that partly
1399 // visible means visible here)
1400 return rect.bottom > 0 && rect.top < GetClientSize().y;
1401 }
1402
1403 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
1404 {
1405 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
1406
1407 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1408 DoGetItem(&tvItem);
1409
1410 return tvItem.cChildren != 0;
1411 }
1412
1413 bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
1414 {
1415 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
1416
1417 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
1418 DoGetItem(&tvItem);
1419
1420 return (tvItem.state & TVIS_EXPANDED) != 0;
1421 }
1422
1423 bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
1424 {
1425 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
1426
1427 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
1428 DoGetItem(&tvItem);
1429
1430 return (tvItem.state & TVIS_SELECTED) != 0;
1431 }
1432
1433 bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
1434 {
1435 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
1436
1437 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1438 DoGetItem(&tvItem);
1439
1440 return (tvItem.state & TVIS_BOLD) != 0;
1441 }
1442
1443 // ----------------------------------------------------------------------------
1444 // navigation
1445 // ----------------------------------------------------------------------------
1446
1447 wxTreeItemId wxTreeCtrl::GetRootItem() const
1448 {
1449 // Root may be real (visible) or virtual (hidden).
1450 if ( GET_VIRTUAL_ROOT() )
1451 return TVI_ROOT;
1452
1453 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1454 }
1455
1456 wxTreeItemId wxTreeCtrl::GetSelection() const
1457 {
1458 wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), wxTreeItemId(),
1459 wxT("this only works with single selection controls") );
1460
1461 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1462 }
1463
1464 wxTreeItemId wxTreeCtrl::GetItemParent(const wxTreeItemId& item) const
1465 {
1466 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1467
1468 HTREEITEM hItem;
1469
1470 if ( IS_VIRTUAL_ROOT(item) )
1471 {
1472 // no parent for the virtual root
1473 hItem = 0;
1474 }
1475 else // normal item
1476 {
1477 hItem = TreeView_GetParent(GetHwnd(), HITEM(item));
1478 if ( !hItem && HasFlag(wxTR_HIDE_ROOT) )
1479 {
1480 // the top level items should have the virtual root as their parent
1481 hItem = TVI_ROOT;
1482 }
1483 }
1484
1485 return wxTreeItemId(hItem);
1486 }
1487
1488 wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1489 wxTreeItemIdValue& cookie) const
1490 {
1491 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1492
1493 // remember the last child returned in 'cookie'
1494 cookie = TreeView_GetChild(GetHwnd(), HITEM(item));
1495
1496 return wxTreeItemId(cookie);
1497 }
1498
1499 wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
1500 wxTreeItemIdValue& cookie) const
1501 {
1502 wxTreeItemId item(TreeView_GetNextSibling(GetHwnd(),
1503 HITEM(wxTreeItemId(cookie))));
1504 cookie = item.m_pItem;
1505
1506 return item;
1507 }
1508
1509 #if WXWIN_COMPATIBILITY_2_4
1510
1511 wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1512 long& cookie) const
1513 {
1514 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1515
1516 cookie = (long)TreeView_GetChild(GetHwnd(), HITEM(item));
1517
1518 return wxTreeItemId((void *)cookie);
1519 }
1520
1521 wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
1522 long& cookie) const
1523 {
1524 wxTreeItemId item(TreeView_GetNextSibling
1525 (
1526 GetHwnd(),
1527 HITEM(wxTreeItemId((void *)cookie)
1528 )));
1529 cookie = (long)item.m_pItem;
1530
1531 return item;
1532 }
1533
1534 #endif // WXWIN_COMPATIBILITY_2_4
1535
1536 wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
1537 {
1538 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1539
1540 // can this be done more efficiently?
1541 wxTreeItemIdValue cookie;
1542
1543 wxTreeItemId childLast,
1544 child = GetFirstChild(item, cookie);
1545 while ( child.IsOk() )
1546 {
1547 childLast = child;
1548 child = GetNextChild(item, cookie);
1549 }
1550
1551 return childLast;
1552 }
1553
1554 wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
1555 {
1556 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1557 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item)));
1558 }
1559
1560 wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
1561 {
1562 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1563 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item)));
1564 }
1565
1566 wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
1567 {
1568 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1569 }
1570
1571 wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
1572 {
1573 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1574 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() for must be visible itself!"));
1575
1576 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item)));
1577 }
1578
1579 wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1580 {
1581 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1582 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1583
1584 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item)));
1585 }
1586
1587 // ----------------------------------------------------------------------------
1588 // multiple selections emulation
1589 // ----------------------------------------------------------------------------
1590
1591 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const
1592 {
1593 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
1594
1595 // receive the desired information.
1596 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1597 DoGetItem(&tvItem);
1598
1599 // state image indices are 1 based
1600 return ((tvItem.state >> 12) - 1) == 1;
1601 }
1602
1603 void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check)
1604 {
1605 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1606
1607 // receive the desired information.
1608 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
1609
1610 DoGetItem(&tvItem);
1611
1612 // state images are one-based
1613 tvItem.state = (check ? 2 : 1) << 12;
1614
1615 DoSetItem(&tvItem);
1616 }
1617
1618 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
1619 {
1620 TraverseSelections selector(this, selections);
1621
1622 return selector.GetCount();
1623 }
1624
1625 // ----------------------------------------------------------------------------
1626 // Usual operations
1627 // ----------------------------------------------------------------------------
1628
1629 wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
1630 wxTreeItemId hInsertAfter,
1631 const wxString& text,
1632 int image, int selectedImage,
1633 wxTreeItemData *data)
1634 {
1635 wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()),
1636 wxTreeItemId(),
1637 _T("can't have more than one root in the tree") );
1638
1639 TV_INSERTSTRUCT tvIns;
1640 tvIns.hParent = HITEM(parent);
1641 tvIns.hInsertAfter = HITEM(hInsertAfter);
1642
1643 // this is how we insert the item as the first child: supply a NULL
1644 // hInsertAfter
1645 if ( !tvIns.hInsertAfter )
1646 {
1647 tvIns.hInsertAfter = TVI_FIRST;
1648 }
1649
1650 UINT mask = 0;
1651 if ( !text.IsEmpty() )
1652 {
1653 mask |= TVIF_TEXT;
1654 tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok
1655 }
1656 else
1657 {
1658 tvIns.item.pszText = NULL;
1659 tvIns.item.cchTextMax = 0;
1660 }
1661
1662 if ( image != -1 )
1663 {
1664 mask |= TVIF_IMAGE;
1665 tvIns.item.iImage = image;
1666
1667 if ( selectedImage == -1 )
1668 {
1669 // take the same image for selected icon if not specified
1670 selectedImage = image;
1671 }
1672 }
1673
1674 if ( selectedImage != -1 )
1675 {
1676 mask |= TVIF_SELECTEDIMAGE;
1677 tvIns.item.iSelectedImage = selectedImage;
1678 }
1679
1680 if ( data != NULL )
1681 {
1682 mask |= TVIF_PARAM;
1683 tvIns.item.lParam = (LPARAM)data;
1684 }
1685
1686 tvIns.item.mask = mask;
1687
1688 HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns);
1689 if ( id == 0 )
1690 {
1691 wxLogLastError(wxT("TreeView_InsertItem"));
1692 }
1693
1694 if ( data != NULL )
1695 {
1696 // associate the application tree item with Win32 tree item handle
1697 data->SetId(id);
1698 }
1699
1700 return wxTreeItemId(id);
1701 }
1702
1703 // for compatibility only
1704 #if WXWIN_COMPATIBILITY_2_4
1705
1706 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1707 const wxString& text,
1708 int image, int selImage,
1709 long insertAfter)
1710 {
1711 return DoInsertItem(parent, wxTreeItemId((void *)insertAfter), text,
1712 image, selImage, NULL);
1713 }
1714
1715 #endif // WXWIN_COMPATIBILITY_2_4
1716
1717 wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
1718 int image, int selectedImage,
1719 wxTreeItemData *data)
1720 {
1721
1722 if ( m_windowStyle & wxTR_HIDE_ROOT )
1723 {
1724 // create a virtual root item, the parent for all the others
1725 m_pVirtualRoot = new wxVirtualNode(data);
1726
1727 return TVI_ROOT;
1728 }
1729
1730 return DoInsertItem(wxTreeItemId(), wxTreeItemId(),
1731 text, image, selectedImage, data);
1732 }
1733
1734 wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
1735 const wxString& text,
1736 int image, int selectedImage,
1737 wxTreeItemData *data)
1738 {
1739 return DoInsertItem(parent, TVI_FIRST,
1740 text, image, selectedImage, data);
1741 }
1742
1743 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1744 const wxTreeItemId& idPrevious,
1745 const wxString& text,
1746 int image, int selectedImage,
1747 wxTreeItemData *data)
1748 {
1749 return DoInsertItem(parent, idPrevious, text, image, selectedImage, data);
1750 }
1751
1752 wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
1753 size_t index,
1754 const wxString& text,
1755 int image, int selectedImage,
1756 wxTreeItemData *data)
1757 {
1758 // find the item from index
1759 wxTreeItemIdValue cookie;
1760 wxTreeItemId idPrev, idCur = GetFirstChild(parent, cookie);
1761 while ( index != 0 && idCur.IsOk() )
1762 {
1763 index--;
1764
1765 idPrev = idCur;
1766 idCur = GetNextChild(parent, cookie);
1767 }
1768
1769 // assert, not check: if the index is invalid, we will append the item
1770 // to the end
1771 wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") );
1772
1773 return DoInsertItem(parent, idPrev, text, image, selectedImage, data);
1774 }
1775
1776 wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
1777 const wxString& text,
1778 int image, int selectedImage,
1779 wxTreeItemData *data)
1780 {
1781 return DoInsertItem(parent, TVI_LAST,
1782 text, image, selectedImage, data);
1783 }
1784
1785 void wxTreeCtrl::Delete(const wxTreeItemId& item)
1786 {
1787 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) )
1788 {
1789 wxLogLastError(wxT("TreeView_DeleteItem"));
1790 }
1791 }
1792
1793 // delete all children (but don't delete the item itself)
1794 void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
1795 {
1796 wxTreeItemIdValue cookie;
1797
1798 wxArrayTreeItemIds children;
1799 wxTreeItemId child = GetFirstChild(item, cookie);
1800 while ( child.IsOk() )
1801 {
1802 children.Add(child);
1803
1804 child = GetNextChild(item, cookie);
1805 }
1806
1807 size_t nCount = children.Count();
1808 for ( size_t n = 0; n < nCount; n++ )
1809 {
1810 if ( !TreeView_DeleteItem(GetHwnd(), HITEM_PTR(children[n])) )
1811 {
1812 wxLogLastError(wxT("TreeView_DeleteItem"));
1813 }
1814 }
1815 }
1816
1817 void wxTreeCtrl::DeleteAllItems()
1818 {
1819 // delete the "virtual" root item.
1820 if ( GET_VIRTUAL_ROOT() )
1821 {
1822 delete GET_VIRTUAL_ROOT();
1823 m_pVirtualRoot = NULL;
1824 }
1825
1826 // and all the real items
1827
1828 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1829 {
1830 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1831 }
1832 }
1833
1834 void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
1835 {
1836 wxASSERT_MSG( flag == TVE_COLLAPSE ||
1837 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
1838 flag == TVE_EXPAND ||
1839 flag == TVE_TOGGLE,
1840 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1841
1842 // A hidden root can be neither expanded nor collapsed.
1843 wxCHECK_RET( !(m_windowStyle & wxTR_HIDE_ROOT) || (HITEM(item) != TVI_ROOT),
1844 wxT("Can't expand/collapse hidden root node!") )
1845
1846 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1847 // emulate them. This behaviour has changed slightly with comctl32.dll
1848 // v 4.70 - now it does send them but only the first time. To maintain
1849 // compatible behaviour and also in order to not have surprises with the
1850 // future versions, don't rely on this and still do everything ourselves.
1851 // To avoid that the messages be sent twice when the item is expanded for
1852 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1853
1854 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE);
1855 tvItem.state = 0;
1856 DoSetItem(&tvItem);
1857
1858 if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) != 0 )
1859 {
1860 wxTreeEvent event(wxEVT_NULL, m_windowId);
1861 event.m_item = item;
1862 event.SetEventObject(this);
1863
1864 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1865 // itself
1866 event.SetEventType(gs_expandEvents[IsExpanded(item) ? IDX_EXPAND
1867 : IDX_COLLAPSE]
1868 [IDX_DONE]);
1869
1870 (void)GetEventHandler()->ProcessEvent(event);
1871 }
1872 //else: change didn't took place, so do nothing at all
1873 }
1874
1875 void wxTreeCtrl::Expand(const wxTreeItemId& item)
1876 {
1877 DoExpand(item, TVE_EXPAND);
1878 }
1879
1880 void wxTreeCtrl::Collapse(const wxTreeItemId& item)
1881 {
1882 DoExpand(item, TVE_COLLAPSE);
1883 }
1884
1885 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1886 {
1887 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
1888 }
1889
1890 void wxTreeCtrl::Toggle(const wxTreeItemId& item)
1891 {
1892 DoExpand(item, TVE_TOGGLE);
1893 }
1894
1895 #if WXWIN_COMPATIBILITY_2_4
1896 void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
1897 {
1898 DoExpand(item, action);
1899 }
1900 #endif
1901
1902 void wxTreeCtrl::Unselect()
1903 {
1904 wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE),
1905 wxT("doesn't make sense, may be you want UnselectAll()?") );
1906
1907 // just remove the selection
1908 SelectItem(wxTreeItemId());
1909 }
1910
1911 void wxTreeCtrl::UnselectAll()
1912 {
1913 if ( m_windowStyle & wxTR_MULTIPLE )
1914 {
1915 wxArrayTreeItemIds selections;
1916 size_t count = GetSelections(selections);
1917 for ( size_t n = 0; n < count; n++ )
1918 {
1919 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1920 SetItemCheck(HITEM_PTR(selections[n]), false);
1921 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1922 ::UnselectItem(GetHwnd(), HITEM_PTR(selections[n]));
1923 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1924 }
1925 }
1926 else
1927 {
1928 // just remove the selection
1929 Unselect();
1930 }
1931 }
1932
1933 void wxTreeCtrl::SelectItem(const wxTreeItemId& item, bool select)
1934 {
1935 if ( m_windowStyle & wxTR_MULTIPLE )
1936 {
1937 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1938 // selecting the item means checking it
1939 SetItemCheck(item, select);
1940 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1941 ::SelectItem(GetHwnd(), HITEM(item), select);
1942 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1943 }
1944 else
1945 {
1946 wxASSERT_MSG( select,
1947 _T("SelectItem(false) works only for multiselect") );
1948
1949 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1950 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1951 // send them ourselves
1952
1953 wxTreeEvent event(wxEVT_NULL, m_windowId);
1954 event.m_item = item;
1955 event.SetEventObject(this);
1956
1957 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING);
1958 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
1959 {
1960 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item)) )
1961 {
1962 wxLogLastError(wxT("TreeView_SelectItem"));
1963 }
1964 else // ok
1965 {
1966 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1967 (void)GetEventHandler()->ProcessEvent(event);
1968 }
1969 }
1970 //else: program vetoed the change
1971 }
1972 }
1973
1974 void wxTreeCtrl::UnselectItem(const wxTreeItemId& item)
1975 {
1976 SelectItem(item, false);
1977 }
1978
1979 void wxTreeCtrl::ToggleItemSelection(const wxTreeItemId& item)
1980 {
1981 SelectItem(item, !IsSelected(item));
1982 }
1983
1984 void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1985 {
1986 // no error return
1987 TreeView_EnsureVisible(GetHwnd(), HITEM(item));
1988 }
1989
1990 void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
1991 {
1992 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) )
1993 {
1994 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1995 }
1996 }
1997
1998 wxTextCtrl *wxTreeCtrl::GetEditControl() const
1999 {
2000 return m_textCtrl;
2001 }
2002
2003 void wxTreeCtrl::DeleteTextCtrl()
2004 {
2005 if ( m_textCtrl )
2006 {
2007 // the HWND corresponding to this control is deleted by the tree
2008 // control itself and we don't know when exactly this happens, so check
2009 // if the window still exists before calling UnsubclassWin()
2010 if ( !::IsWindow(GetHwndOf(m_textCtrl)) )
2011 {
2012 m_textCtrl->SetHWND(0);
2013 }
2014
2015 m_textCtrl->UnsubclassWin();
2016 m_textCtrl->SetHWND(0);
2017 delete m_textCtrl;
2018 m_textCtrl = NULL;
2019 }
2020 }
2021
2022 wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
2023 wxClassInfo* textControlClass)
2024 {
2025 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
2026
2027 DeleteTextCtrl();
2028
2029 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
2030 HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item));
2031
2032 // this is not an error - the TVN_BEGINLABELEDIT handler might have
2033 // returned false
2034 if ( !hWnd )
2035 {
2036 delete m_textCtrl;
2037 m_textCtrl = NULL;
2038 return NULL;
2039 }
2040
2041 // textctrl is subclassed in MSWOnNotify
2042 return m_textCtrl;
2043 }
2044
2045 // End label editing, optionally cancelling the edit
2046 void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool discardChanges)
2047 {
2048 TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
2049
2050 DeleteTextCtrl();
2051 }
2052
2053 wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
2054 {
2055 TV_HITTESTINFO hitTestInfo;
2056 hitTestInfo.pt.x = (int)point.x;
2057 hitTestInfo.pt.y = (int)point.y;
2058
2059 TreeView_HitTest(GetHwnd(), &hitTestInfo);
2060
2061 flags = 0;
2062
2063 // avoid repetition
2064 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2065 flags |= wxTREE_HITTEST_##flag
2066
2067 TRANSLATE_FLAG(ABOVE);
2068 TRANSLATE_FLAG(BELOW);
2069 TRANSLATE_FLAG(NOWHERE);
2070 TRANSLATE_FLAG(ONITEMBUTTON);
2071 TRANSLATE_FLAG(ONITEMICON);
2072 TRANSLATE_FLAG(ONITEMINDENT);
2073 TRANSLATE_FLAG(ONITEMLABEL);
2074 TRANSLATE_FLAG(ONITEMRIGHT);
2075 TRANSLATE_FLAG(ONITEMSTATEICON);
2076 TRANSLATE_FLAG(TOLEFT);
2077 TRANSLATE_FLAG(TORIGHT);
2078
2079 #undef TRANSLATE_FLAG
2080
2081 return wxTreeItemId(hitTestInfo.hItem);
2082 }
2083
2084 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
2085 wxRect& rect,
2086 bool textOnly) const
2087 {
2088 RECT rc;
2089
2090 // Virtual root items have no bounding rectangle
2091 if ( IS_VIRTUAL_ROOT(item) )
2092 {
2093 return false;
2094 }
2095
2096 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item),
2097 &rc, textOnly) )
2098 {
2099 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
2100
2101 return true;
2102 }
2103 else
2104 {
2105 // couldn't retrieve rect: for example, item isn't visible
2106 return false;
2107 }
2108 }
2109
2110 // ----------------------------------------------------------------------------
2111 // sorting stuff
2112 // ----------------------------------------------------------------------------
2113
2114 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2115 // functions such as IsDataIndirect()
2116 class wxTreeSortHelper
2117 {
2118 public:
2119 static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree);
2120
2121 private:
2122 static wxTreeItemId GetIdFromData(wxTreeCtrl *tree, LPARAM item)
2123 {
2124 wxTreeItemData *data = (wxTreeItemData *)item;
2125 if ( tree->IsDataIndirect(data) )
2126 {
2127 data = ((wxTreeItemIndirectData *)data)->GetData();
2128 }
2129
2130 return data->GetId();
2131 }
2132 };
2133
2134 int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1,
2135 LPARAM pItem2,
2136 LPARAM htree)
2137 {
2138 wxCHECK_MSG( pItem1 && pItem2, 0,
2139 wxT("sorting tree without data doesn't make sense") );
2140
2141 wxTreeCtrl *tree = (wxTreeCtrl *)htree;
2142
2143 return tree->OnCompareItems(GetIdFromData(tree, pItem1),
2144 GetIdFromData(tree, pItem2));
2145 }
2146
2147 int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
2148 const wxTreeItemId& item2)
2149 {
2150 return wxStrcmp(GetItemText(item1), GetItemText(item2));
2151 }
2152
2153 void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
2154 {
2155 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
2156
2157 // rely on the fact that TreeView_SortChildren does the same thing as our
2158 // default behaviour, i.e. sorts items alphabetically and so call it
2159 // directly if we're not in derived class (much more efficient!)
2160 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
2161 {
2162 TreeView_SortChildren(GetHwnd(), HITEM(item), 0);
2163 }
2164 else
2165 {
2166 TV_SORTCB tvSort;
2167 tvSort.hParent = HITEM(item);
2168 tvSort.lpfnCompare = wxTreeSortHelper::Compare;
2169 tvSort.lParam = (LPARAM)this;
2170 TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
2171 }
2172 }
2173
2174 // ----------------------------------------------------------------------------
2175 // implementation
2176 // ----------------------------------------------------------------------------
2177
2178 bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id)
2179 {
2180 if ( cmd == EN_UPDATE )
2181 {
2182 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
2183 event.SetEventObject( this );
2184 ProcessCommand(event);
2185 }
2186 else if ( cmd == EN_KILLFOCUS )
2187 {
2188 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
2189 event.SetEventObject( this );
2190 ProcessCommand(event);
2191 }
2192 else
2193 {
2194 // nothing done
2195 return false;
2196 }
2197
2198 // command processed
2199 return true;
2200 }
2201
2202 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2203 // only do it during dragging, minimize wxWin overhead (this is important for
2204 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2205 // instead of passing by wxWin events
2206 WXLRESULT wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
2207 {
2208 bool processed = false;
2209 WXLRESULT rc = 0;
2210 bool isMultiple = (GetWindowStyle() & wxTR_MULTIPLE) != 0;
2211
2212 if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
2213 {
2214 // we only process mouse messages here and these parameters have the
2215 // same meaning for all of them
2216 int x = GET_X_LPARAM(lParam),
2217 y = GET_Y_LPARAM(lParam);
2218 HTREEITEM htItem = GetItemFromPoint(GetHwnd(), x, y);
2219
2220 switch ( nMsg )
2221 {
2222 case WM_RBUTTONDOWN:
2223 // if the item we are about to right click on is not already
2224 // selected or if we click outside of any item, remove the
2225 // entire previous selection
2226 if ( !htItem || !::IsItemSelected(GetHwnd(), htItem) )
2227 {
2228 UnselectAll();
2229 }
2230
2231 // select item and set the focus to the
2232 // newly selected item
2233 ::SelectItem(GetHwnd(), htItem);
2234 ::SetFocus(GetHwnd(), htItem);
2235 break;
2236
2237 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2238 case WM_LBUTTONDOWN:
2239 if ( htItem && isMultiple )
2240 {
2241 if ( wParam & MK_CONTROL )
2242 {
2243 SetFocus();
2244
2245 // toggle selected state
2246 ::ToggleItemSelection(GetHwnd(), htItem);
2247
2248 ::SetFocus(GetHwnd(), htItem);
2249
2250 // reset on any click without Shift
2251 m_htSelStart.Unset();
2252
2253 processed = true;
2254 }
2255 else if ( wParam & MK_SHIFT )
2256 {
2257 // this selects all items between the starting one and
2258 // the current
2259
2260 if ( !m_htSelStart )
2261 {
2262 // take the focused item
2263 m_htSelStart = TreeView_GetSelection(GetHwnd());
2264 }
2265
2266 SelectRange(GetHwnd(), HITEM(m_htSelStart), htItem,
2267 !(wParam & MK_CONTROL));
2268
2269 ::SetFocus(GetHwnd(), htItem);
2270
2271 processed = true;
2272 }
2273 else // normal click
2274 {
2275 // avoid doing anything if we click on the only
2276 // currently selected item
2277
2278 wxArrayTreeItemIds selections;
2279 size_t count = GetSelections(selections);
2280 if ( count == 0 ||
2281 count > 1 ||
2282 HITEM_PTR(selections[0]) != htItem )
2283 {
2284 // clear the previously selected items, if the
2285 // user clicked outside of the present selection.
2286 // otherwise, perform the deselection on mouse-up.
2287 // this allows multiple drag and drop to work.
2288
2289 if (IsItemSelected(GetHwnd(), htItem))
2290 {
2291 ::SetFocus(GetHwnd(), htItem);
2292 }
2293 else
2294 {
2295 UnselectAll();
2296
2297 // prevent the click from starting in-place editing
2298 // which should only happen if we click on the
2299 // already selected item (and nothing else is
2300 // selected)
2301
2302 TreeView_SelectItem(GetHwnd(), 0);
2303 ::SelectItem(GetHwnd(), htItem);
2304 }
2305 }
2306
2307 // reset on any click without Shift
2308 m_htSelStart.Unset();
2309 }
2310 }
2311 break;
2312 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2313
2314 case WM_MOUSEMOVE:
2315 if ( m_dragImage )
2316 {
2317 m_dragImage->Move(wxPoint(x, y));
2318 if ( htItem )
2319 {
2320 // highlight the item as target (hiding drag image is
2321 // necessary - otherwise the display will be corrupted)
2322 m_dragImage->Hide();
2323 TreeView_SelectDropTarget(GetHwnd(), htItem);
2324 m_dragImage->Show();
2325 }
2326 }
2327 break;
2328
2329 case WM_LBUTTONUP:
2330
2331 // facilitates multiple drag-and-drop
2332 if (htItem && isMultiple)
2333 {
2334 wxArrayTreeItemIds selections;
2335 size_t count = GetSelections(selections);
2336
2337 if (count > 1 &&
2338 !(wParam & MK_CONTROL) &&
2339 !(wParam & MK_SHIFT))
2340 {
2341 UnselectAll();
2342 TreeView_SelectItem(GetHwnd(), htItem);
2343 }
2344 }
2345
2346 // fall through
2347
2348 case WM_RBUTTONUP:
2349 if ( m_dragImage )
2350 {
2351 m_dragImage->EndDrag();
2352 delete m_dragImage;
2353 m_dragImage = NULL;
2354
2355 // generate the drag end event
2356 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, m_windowId);
2357
2358 event.m_item = htItem;
2359 event.m_pointDrag = wxPoint(x, y);
2360 event.SetEventObject(this);
2361
2362 (void)GetEventHandler()->ProcessEvent(event);
2363
2364 // if we don't do it, the tree seems to think that 2 items
2365 // are selected simultaneously which is quite weird
2366 TreeView_SelectDropTarget(GetHwnd(), 0);
2367 }
2368 break;
2369 }
2370 }
2371 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2372 else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) && isMultiple )
2373 {
2374 // the tree control greys out the selected item when it loses focus and
2375 // paints it as selected again when it regains it, but it won't do it
2376 // for the other items itself - help it
2377 wxArrayTreeItemIds selections;
2378 size_t count = GetSelections(selections);
2379 RECT rect;
2380 for ( size_t n = 0; n < count; n++ )
2381 {
2382 // TreeView_GetItemRect() will return false if item is not visible,
2383 // which may happen perfectly well
2384 if ( TreeView_GetItemRect(GetHwnd(), HITEM_PTR(selections[n]),
2385 &rect, true) )
2386 {
2387 ::InvalidateRect(GetHwnd(), &rect, false);
2388 }
2389 }
2390 }
2391 else if ( nMsg == WM_KEYDOWN && isMultiple )
2392 {
2393 bool bCtrl = wxIsCtrlDown(),
2394 bShift = wxIsShiftDown();
2395
2396 // we handle.arrows and space, but not page up/down and home/end: the
2397 // latter should be easy, but not the former
2398
2399 HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
2400 if ( !m_htSelStart )
2401 {
2402 m_htSelStart = htSel;
2403 }
2404
2405 if ( wParam == VK_SPACE )
2406 {
2407 if ( bCtrl )
2408 {
2409 ::ToggleItemSelection(GetHwnd(), htSel);
2410 }
2411 else
2412 {
2413 UnselectAll();
2414
2415 ::SelectItem(GetHwnd(), htSel);
2416 }
2417
2418 processed = true;
2419 }
2420 else if ( wParam == VK_UP || wParam == VK_DOWN )
2421 {
2422 if ( !bCtrl && !bShift )
2423 {
2424 // no modifiers, just clear selection and then let the default
2425 // processing to take place
2426 UnselectAll();
2427 }
2428 else if ( htSel )
2429 {
2430 (void)wxControl::MSWWindowProc(nMsg, wParam, lParam);
2431
2432 HTREEITEM htNext = (HTREEITEM)(wParam == VK_UP
2433 ? TreeView_GetPrevVisible(GetHwnd(), htSel)
2434 : TreeView_GetNextVisible(GetHwnd(), htSel));
2435
2436 if ( !htNext )
2437 {
2438 // at the top/bottom
2439 htNext = htSel;
2440 }
2441
2442 if ( bShift )
2443 {
2444 SelectRange(GetHwnd(), HITEM(m_htSelStart), htNext);
2445 }
2446 else // bCtrl
2447 {
2448 // without changing selection
2449 ::SetFocus(GetHwnd(), htNext);
2450 }
2451
2452 processed = true;
2453 }
2454 }
2455 }
2456 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2457 else if ( nMsg == WM_CHAR )
2458 {
2459 // don't let the control process Space and Return keys because it
2460 // doesn't do anything useful with them anyhow but always beeps
2461 // annoyingly when it receives them and there is no way to turn it off
2462 // simply if you just process TREEITEM_ACTIVATED event to which Space
2463 // and Enter presses are mapped in your code
2464 if ( wParam == VK_SPACE || wParam == VK_RETURN )
2465 {
2466 processed = true;
2467 }
2468 }
2469
2470 if ( !processed )
2471 rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
2472
2473 return rc;
2474 }
2475
2476 // process WM_NOTIFY Windows message
2477 bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
2478 {
2479 wxTreeEvent event(wxEVT_NULL, m_windowId);
2480 wxEventType eventType = wxEVT_NULL;
2481 NMHDR *hdr = (NMHDR *)lParam;
2482
2483 switch ( hdr->code )
2484 {
2485 case TVN_BEGINDRAG:
2486 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
2487 // fall through
2488
2489 case TVN_BEGINRDRAG:
2490 {
2491 if ( eventType == wxEVT_NULL )
2492 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
2493 //else: left drag, already set above
2494
2495 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2496
2497 event.m_item = tv->itemNew.hItem;
2498 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
2499
2500 // don't allow dragging by default: the user code must
2501 // explicitly say that it wants to allow it to avoid breaking
2502 // the old apps
2503 event.Veto();
2504 }
2505 break;
2506
2507 case TVN_BEGINLABELEDIT:
2508 {
2509 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
2510 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2511
2512 event.m_item = info->item.hItem;
2513 event.m_label = info->item.pszText;
2514 event.m_editCancelled = false;
2515 }
2516 break;
2517
2518 case TVN_DELETEITEM:
2519 {
2520 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
2521 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2522
2523 event.m_item = tv->itemOld.hItem;
2524
2525 if ( m_hasAnyAttr )
2526 {
2527 wxMapTreeAttr::iterator it = m_attrs.find(tv->itemOld.hItem);
2528 if ( it != m_attrs.end() )
2529 {
2530 delete it->second;
2531 m_attrs.erase(it);
2532 }
2533 }
2534 }
2535 break;
2536
2537 case TVN_ENDLABELEDIT:
2538 {
2539 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
2540 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2541
2542 event.m_item = info->item.hItem;
2543 event.m_label = info->item.pszText;
2544 event.m_editCancelled = info->item.pszText == NULL;
2545 break;
2546 }
2547
2548 #ifndef __WXWINCE__
2549 // These *must* not be removed or TVN_GETINFOTIP will
2550 // not be processed each time the mouse is moved
2551 // and the tooltip will only ever update once.
2552 case TTN_NEEDTEXTA:
2553 case TTN_NEEDTEXTW:
2554 {
2555 *result = 0;
2556
2557 break;
2558 }
2559
2560 case TVN_GETINFOTIP:
2561 {
2562 eventType = wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP;
2563 NMTVGETINFOTIP *info = (NMTVGETINFOTIP*)lParam;
2564
2565 // Which item are we trying to get a tooltip for?
2566 event.m_item = info->hItem;
2567
2568 break;
2569 }
2570 #endif
2571
2572 case TVN_GETDISPINFO:
2573 eventType = wxEVT_COMMAND_TREE_GET_INFO;
2574 // fall through
2575
2576 case TVN_SETDISPINFO:
2577 {
2578 if ( eventType == wxEVT_NULL )
2579 eventType = wxEVT_COMMAND_TREE_SET_INFO;
2580 //else: get, already set above
2581
2582 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2583
2584 event.m_item = info->item.hItem;
2585 break;
2586 }
2587
2588 case TVN_ITEMEXPANDING:
2589 case TVN_ITEMEXPANDED:
2590 {
2591 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
2592
2593 int what;
2594 switch ( tv->action )
2595 {
2596 default:
2597 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action);
2598 // fall through
2599
2600 case TVE_EXPAND:
2601 what = IDX_EXPAND;
2602 break;
2603
2604 case TVE_COLLAPSE:
2605 what = IDX_COLLAPSE;
2606 break;
2607 }
2608
2609 int how = hdr->code == TVN_ITEMEXPANDING ? IDX_DOING
2610 : IDX_DONE;
2611
2612 eventType = gs_expandEvents[what][how];
2613
2614 event.m_item = tv->itemNew.hItem;
2615 }
2616 break;
2617
2618 case TVN_KEYDOWN:
2619 {
2620 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
2621 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
2622
2623 // fabricate the lParam and wParam parameters sufficiently
2624 // similar to the ones from a "real" WM_KEYDOWN so that
2625 // CreateKeyEvent() works correctly
2626 WXLPARAM lParam =
2627 (::GetKeyState(VK_MENU) < 0 ? KF_ALTDOWN : 0) << 16;
2628
2629 WXWPARAM wParam = info->wVKey;
2630
2631 int keyCode = wxCharCodeMSWToWX(info->wVKey);
2632 if ( !keyCode )
2633 {
2634 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2635 // simple ASCII key
2636 keyCode = wParam;
2637 }
2638
2639 event.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN,
2640 keyCode,
2641 lParam,
2642 wParam);
2643
2644 // a separate event for Space/Return
2645 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2646 ((info->wVKey == VK_SPACE) || (info->wVKey == VK_RETURN)) )
2647 {
2648 wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
2649 m_windowId);
2650 event2.SetEventObject(this);
2651 if ( !(GetWindowStyle() & wxTR_MULTIPLE) )
2652 {
2653 event2.m_item = GetSelection();
2654 }
2655 //else: don't know how to get it
2656
2657 (void)GetEventHandler()->ProcessEvent(event2);
2658 }
2659 }
2660 break;
2661
2662 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2663 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2664 // we have to handle both messages:
2665 case TVN_SELCHANGEDA:
2666 case TVN_SELCHANGEDW:
2667 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
2668 // fall through
2669
2670 case TVN_SELCHANGINGA:
2671 case TVN_SELCHANGINGW:
2672 {
2673 if ( eventType == wxEVT_NULL )
2674 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
2675 //else: already set above
2676
2677 if (hdr->code == TVN_SELCHANGINGW ||
2678 hdr->code == TVN_SELCHANGEDW)
2679 {
2680 NM_TREEVIEWW* tv = (NM_TREEVIEWW *)lParam;
2681 event.m_item = tv->itemNew.hItem;
2682 event.m_itemOld = tv->itemOld.hItem;
2683 }
2684 else
2685 {
2686 NM_TREEVIEWA* tv = (NM_TREEVIEWA *)lParam;
2687 event.m_item = tv->itemNew.hItem;
2688 event.m_itemOld = tv->itemOld.hItem;
2689 }
2690 }
2691 break;
2692
2693 // instead of explicitly checking for _WIN32_IE, check if the
2694 // required symbols are available in the headers
2695 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2696 case NM_CUSTOMDRAW:
2697 {
2698 LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam;
2699 NMCUSTOMDRAW& nmcd = lptvcd->nmcd;
2700 switch ( nmcd.dwDrawStage )
2701 {
2702 case CDDS_PREPAINT:
2703 // if we've got any items with non standard attributes,
2704 // notify us before painting each item
2705 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
2706 : CDRF_DODEFAULT;
2707 break;
2708
2709 case CDDS_ITEMPREPAINT:
2710 {
2711 wxMapTreeAttr::iterator
2712 it = m_attrs.find((void *)nmcd.dwItemSpec);
2713
2714 if ( it == m_attrs.end() )
2715 {
2716 // nothing to do for this item
2717 *result = CDRF_DODEFAULT;
2718 break;
2719 }
2720
2721 wxTreeItemAttr * const attr = it->second;
2722
2723 // selection colours should override ours,
2724 // otherwise it is too confusing ot the user
2725 if ( !(nmcd.uItemState & CDIS_SELECTED) )
2726 {
2727 wxColour colBack;
2728 if ( attr->HasBackgroundColour() )
2729 {
2730 colBack = attr->GetBackgroundColour();
2731 lptvcd->clrTextBk = wxColourToRGB(colBack);
2732 }
2733 }
2734
2735 // but we still want to keep the special foreground
2736 // colour when we don't have focus (we can't keep
2737 // it when we do, it would usually be unreadable on
2738 // the almost inverted bg colour...)
2739 if ( !(nmcd.uItemState & CDIS_SELECTED) ||
2740 FindFocus() != this )
2741 {
2742 wxColour colText;
2743 if ( attr->HasTextColour() )
2744 {
2745 colText = attr->GetTextColour();
2746 lptvcd->clrText = wxColourToRGB(colText);
2747 }
2748 }
2749
2750 if ( attr->HasFont() )
2751 {
2752 HFONT hFont = GetHfontOf(attr->GetFont());
2753
2754 ::SelectObject(nmcd.hdc, hFont);
2755
2756 *result = CDRF_NEWFONT;
2757 }
2758 else // no specific font
2759 {
2760 *result = CDRF_DODEFAULT;
2761 }
2762 }
2763 break;
2764
2765 default:
2766 *result = CDRF_DODEFAULT;
2767 }
2768 }
2769
2770 // we always process it
2771 return true;
2772 #endif // have owner drawn support in headers
2773
2774 case NM_CLICK:
2775 {
2776 DWORD pos = GetMessagePos();
2777 POINT point;
2778 point.x = LOWORD(pos);
2779 point.y = HIWORD(pos);
2780 ::MapWindowPoints(HWND_DESKTOP, GetHwnd(), &point, 1);
2781 int flags = 0;
2782 wxTreeItemId item = HitTest(wxPoint(point.x, point.y), flags);
2783 if (flags & wxTREE_HITTEST_ONITEMSTATEICON)
2784 {
2785 event.m_item = item;
2786 eventType = wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK;
2787 }
2788 break;
2789 }
2790
2791 case NM_DBLCLK:
2792 case NM_RCLICK:
2793 {
2794 TV_HITTESTINFO tvhti;
2795 ::GetCursorPos(&tvhti.pt);
2796 ::ScreenToClient(GetHwnd(), &tvhti.pt);
2797 if ( TreeView_HitTest(GetHwnd(), &tvhti) )
2798 {
2799 if ( tvhti.flags & TVHT_ONITEM )
2800 {
2801 event.m_item = tvhti.hItem;
2802 eventType = (int)hdr->code == NM_DBLCLK
2803 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2804 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
2805
2806 event.m_pointDrag.x = tvhti.pt.x;
2807 event.m_pointDrag.y = tvhti.pt.y;
2808 }
2809
2810 break;
2811 }
2812 }
2813 // fall through
2814
2815 default:
2816 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2817 }
2818
2819 event.SetEventObject(this);
2820 event.SetEventType(eventType);
2821
2822 bool processed = GetEventHandler()->ProcessEvent(event);
2823
2824 // post processing
2825 switch ( hdr->code )
2826 {
2827 case NM_DBLCLK:
2828 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2829 // the return code of this event handler as the return value for
2830 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2831 // expanded status would never work
2832 *result = false;
2833 break;
2834
2835 case TVN_BEGINDRAG:
2836 case TVN_BEGINRDRAG:
2837 if ( event.IsAllowed() )
2838 {
2839 // normally this is impossible because the m_dragImage is
2840 // deleted once the drag operation is over
2841 wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") );
2842
2843 m_dragImage = new wxDragImage(*this, event.m_item);
2844 m_dragImage->BeginDrag(wxPoint(0, 0), this);
2845 m_dragImage->Show();
2846 }
2847 break;
2848
2849 case TVN_DELETEITEM:
2850 {
2851 // NB: we might process this message using wxWidgets event
2852 // tables, but due to overhead of wxWin event system we
2853 // prefer to do it here ourself (otherwise deleting a tree
2854 // with many items is just too slow)
2855 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
2856
2857 wxTreeItemId item = event.m_item;
2858 if ( HasIndirectData(item) )
2859 {
2860 wxTreeItemIndirectData *data = (wxTreeItemIndirectData *)
2861 tv->itemOld.lParam;
2862 delete data; // can't be NULL here
2863 }
2864 else
2865 {
2866 wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
2867 delete data; // may be NULL, ok
2868 }
2869
2870 processed = true; // Make sure we don't get called twice
2871 }
2872 break;
2873
2874 case TVN_BEGINLABELEDIT:
2875 // return true to cancel label editing
2876 *result = !event.IsAllowed();
2877 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2878 if(event.IsAllowed())
2879 {
2880 HWND hText = TreeView_GetEditControl(GetHwnd());
2881 if(hText != NULL)
2882 {
2883 // MBN: if m_textCtrl already has an HWND, it is a stale
2884 // pointer from a previous edit (because the user
2885 // didn't modify the label before dismissing the control,
2886 // and TVN_ENDLABELEDIT was not sent), so delete it
2887 if(m_textCtrl && m_textCtrl->GetHWND() != 0)
2888 DeleteTextCtrl();
2889 if(!m_textCtrl)
2890 m_textCtrl = new wxTextCtrl();
2891 m_textCtrl->SetParent(this);
2892 m_textCtrl->SetHWND((WXHWND)hText);
2893 m_textCtrl->SubclassWin((WXHWND)hText);
2894
2895 // set wxTE_PROCESS_ENTER style for the text control to
2896 // force it to process the Enter presses itself, otherwise
2897 // they could be stolen from it by the dialog
2898 // navigation code
2899 m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle()
2900 | wxTE_PROCESS_ENTER);
2901 }
2902 }
2903 break;
2904
2905 case TVN_ENDLABELEDIT:
2906 // return true to set the label to the new string: note that we
2907 // also must pretend that we did process the message or it is going
2908 // to be passed to DefWindowProc() which will happily return false
2909 // cancelling the label change
2910 *result = event.IsAllowed();
2911 processed = true;
2912
2913 // ensure that we don't have the text ctrl which is going to be
2914 // deleted any more
2915 DeleteTextCtrl();
2916 break;
2917
2918 #ifndef __WXWINCE__
2919 case TVN_GETINFOTIP:
2920 {
2921 // If the user permitted a tooltip change, change it
2922 if (event.IsAllowed())
2923 {
2924 SetToolTip(event.m_label);
2925 }
2926 }
2927 break;
2928 #endif
2929
2930 case TVN_SELCHANGING:
2931 case TVN_ITEMEXPANDING:
2932 // return true to prevent the action from happening
2933 *result = !event.IsAllowed();
2934 break;
2935
2936 case TVN_ITEMEXPANDED:
2937 // the item is not refreshed properly after expansion when it has
2938 // an image depending on the expanded/collapsed state - bug in
2939 // comctl32.dll or our code?
2940 {
2941 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
2942 wxTreeItemId id(tv->itemNew.hItem);
2943
2944 int image = GetItemImage(id, wxTreeItemIcon_Expanded);
2945 if ( image != -1 )
2946 {
2947 RefreshItem(id);
2948 }
2949 }
2950 break;
2951
2952 case TVN_GETDISPINFO:
2953 // NB: so far the user can't set the image himself anyhow, so do it
2954 // anyway - but this may change later
2955 //if ( /* !processed && */ 1 )
2956 {
2957 wxTreeItemId item = event.m_item;
2958 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2959 if ( info->item.mask & TVIF_IMAGE )
2960 {
2961 info->item.iImage =
2962 DoGetItemImageFromData
2963 (
2964 item,
2965 IsExpanded(item) ? wxTreeItemIcon_Expanded
2966 : wxTreeItemIcon_Normal
2967 );
2968 }
2969 if ( info->item.mask & TVIF_SELECTEDIMAGE )
2970 {
2971 info->item.iSelectedImage =
2972 DoGetItemImageFromData
2973 (
2974 item,
2975 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
2976 : wxTreeItemIcon_Selected
2977 );
2978 }
2979 }
2980 break;
2981
2982 //default:
2983 // for the other messages the return value is ignored and there is
2984 // nothing special to do
2985 }
2986 return processed;
2987 }
2988
2989 // ----------------------------------------------------------------------------
2990 // State control.
2991 // ----------------------------------------------------------------------------
2992
2993 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2994 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2995
2996 void wxTreeCtrl::SetState(const wxTreeItemId& node, int state)
2997 {
2998 TV_ITEM tvi;
2999 tvi.hItem = (HTREEITEM)node.m_pItem;
3000 tvi.mask = TVIF_STATE;
3001 tvi.stateMask = TVIS_STATEIMAGEMASK;
3002
3003 // Select the specified state, or -1 == cycle to the next one.
3004 if ( state == -1 )
3005 {
3006 TreeView_GetItem(GetHwnd(), &tvi);
3007
3008 state = STATEIMAGEMASKTOINDEX(tvi.state) + 1;
3009 if ( state == m_imageListState->GetImageCount() )
3010 state = 1;
3011 }
3012
3013 wxCHECK_RET( state < m_imageListState->GetImageCount(),
3014 _T("wxTreeCtrl::SetState(): item index out of bounds") );
3015
3016 tvi.state = INDEXTOSTATEIMAGEMASK(state);
3017
3018 TreeView_SetItem(GetHwnd(), &tvi);
3019 }
3020
3021 int wxTreeCtrl::GetState(const wxTreeItemId& node)
3022 {
3023 TV_ITEM tvi;
3024 tvi.hItem = (HTREEITEM)node.m_pItem;
3025 tvi.mask = TVIF_STATE;
3026 tvi.stateMask = TVIS_STATEIMAGEMASK;
3027 TreeView_GetItem(GetHwnd(), &tvi);
3028
3029 return STATEIMAGEMASKTOINDEX(tvi.state);
3030 }
3031
3032 #endif // wxUSE_TREECTRL
3033