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