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