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