]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treectlg.cpp
Added chapter on collection and container classes to contents
[wxWidgets.git] / src / generic / treectlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: treectlg.cpp
3 // Purpose: generic tree control implementation
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
7 // Id: $Id$
8 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // =============================================================================
13 // declarations
14 // =============================================================================
15
16 // -----------------------------------------------------------------------------
17 // headers
18 // -----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "treectlg.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 #include "wx/generic/treectlg.h"
32 #include "wx/imaglist.h"
33 #include "wx/settings.h"
34 #include "wx/log.h"
35 #include "wx/intl.h"
36 #include "wx/dynarray.h"
37 #include "wx/arrimpl.cpp"
38 #include "wx/dcclient.h"
39 #include "wx/msgdlg.h"
40
41 // -----------------------------------------------------------------------------
42 // array types
43 // -----------------------------------------------------------------------------
44
45 class WXDLLEXPORT wxGenericTreeItem;
46
47 WX_DEFINE_ARRAY(wxGenericTreeItem *, wxArrayGenericTreeItems);
48 //WX_DEFINE_OBJARRAY(wxArrayTreeItemIds);
49
50 // ----------------------------------------------------------------------------
51 // constants
52 // ----------------------------------------------------------------------------
53
54 static const int NO_IMAGE = -1;
55
56 #define PIXELS_PER_UNIT 10
57
58 // -----------------------------------------------------------------------------
59 // private classes
60 // -----------------------------------------------------------------------------
61
62 // timer used for enabling in-place edit
63 class WXDLLEXPORT wxTreeRenameTimer: public wxTimer
64 {
65 public:
66 wxTreeRenameTimer( wxGenericTreeCtrl *owner );
67
68 void Notify();
69
70 private:
71 wxGenericTreeCtrl *m_owner;
72 };
73
74 // control used for in-place edit
75 class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl
76 {
77 public:
78 wxTreeTextCtrl() { }
79 wxTreeTextCtrl( wxWindow *parent,
80 const wxWindowID id,
81 bool *accept,
82 wxString *res,
83 wxGenericTreeCtrl *owner,
84 const wxString &value = wxEmptyString,
85 const wxPoint &pos = wxDefaultPosition,
86 const wxSize &size = wxDefaultSize,
87 int style = wxSIMPLE_BORDER,
88 const wxValidator& validator = wxDefaultValidator,
89 const wxString &name = wxTextCtrlNameStr );
90
91 void OnChar( wxKeyEvent &event );
92 void OnKillFocus( wxFocusEvent &event );
93
94 private:
95 bool *m_accept;
96 wxString *m_res;
97 wxGenericTreeCtrl *m_owner;
98 wxString m_startValue;
99
100 DECLARE_EVENT_TABLE()
101 DECLARE_DYNAMIC_CLASS(wxTreeTextCtrl);
102 };
103
104 // a tree item
105 class WXDLLEXPORT wxGenericTreeItem
106 {
107 public:
108 // ctors & dtor
109 wxGenericTreeItem() { m_data = NULL; }
110 wxGenericTreeItem( wxGenericTreeItem *parent,
111 const wxString& text,
112 wxDC& dc,
113 int image, int selImage,
114 wxTreeItemData *data );
115
116 ~wxGenericTreeItem();
117
118 // trivial accessors
119 wxArrayGenericTreeItems& GetChildren() { return m_children; }
120
121 const wxString& GetText() const { return m_text; }
122 int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const
123 { return m_images[which]; }
124 wxTreeItemData *GetData() const { return m_data; }
125
126 // returns the current image for the item (depending on its
127 // selected/expanded/whatever state)
128 int GetCurrentImage() const;
129
130 void SetText( const wxString &text );
131 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
132 void SetData(wxTreeItemData *data) { m_data = data; }
133
134 void SetHasPlus(bool has = TRUE) { m_hasPlus = has; }
135
136 void SetBold(bool bold) { m_isBold = bold; }
137
138 int GetX() const { return m_x; }
139 int GetY() const { return m_y; }
140
141 void SetX(int x) { m_x = x; }
142 void SetY(int y) { m_y = y; }
143
144 int GetHeight() const { return m_height; }
145 int GetWidth() const { return m_width; }
146
147 void SetHeight(int h) { m_height = h; }
148 void SetWidth(int w) { m_width = w; }
149
150
151 wxGenericTreeItem *GetParent() const { return m_parent; }
152
153 // operations
154 // deletes all children notifying the treectrl about it if !NULL
155 // pointer given
156 void DeleteChildren(wxGenericTreeCtrl *tree = NULL);
157 // FIXME don't know what is it for
158 void Reset();
159
160 // get count of all children (and grand children if 'recursively')
161 size_t GetChildrenCount(bool recursively = TRUE) const;
162
163 void Insert(wxGenericTreeItem *child, size_t index)
164 { m_children.Insert(child, index); }
165
166 void SetCross( int x, int y );
167 void GetSize( int &x, int &y, const wxGenericTreeCtrl* );
168
169 // return the item at given position (or NULL if no item), onButton is
170 // TRUE if the point belongs to the item's button, otherwise it lies
171 // on the button's label
172 wxGenericTreeItem *HitTest( const wxPoint& point, const wxGenericTreeCtrl *, int &flags);
173
174 void Expand() { m_isCollapsed = FALSE; }
175 void Collapse() { m_isCollapsed = TRUE; }
176
177 void SetHilight( bool set = TRUE ) { m_hasHilight = set; }
178
179 // status inquiries
180 bool HasChildren() const { return !m_children.IsEmpty(); }
181 bool IsSelected() const { return m_hasHilight; }
182 bool IsExpanded() const { return !m_isCollapsed; }
183 bool HasPlus() const { return m_hasPlus || HasChildren(); }
184 bool IsBold() const { return m_isBold; }
185
186 // attributes
187 // get them - may be NULL
188 wxTreeItemAttr *GetAttributes() const { return m_attr; }
189 // get them ensuring that the pointer is not NULL
190 wxTreeItemAttr& Attr()
191 {
192 if ( !m_attr )
193 m_attr = new wxTreeItemAttr;
194
195 return *m_attr;
196 }
197
198 private:
199 wxString m_text;
200
201 // tree ctrl images for the normal, selected, expanded and
202 // expanded+selected states
203 int m_images[wxTreeItemIcon_Max];
204
205 wxTreeItemData *m_data;
206
207 // use bitfields to save size
208 int m_isCollapsed :1;
209 int m_hasHilight :1; // same as focused
210 int m_hasPlus :1; // used for item which doesn't have
211 // children but has a [+] button
212 int m_isBold :1; // render the label in bold font
213
214 wxCoord m_x, m_y;
215 wxCoord m_height, m_width;
216 int m_xCross, m_yCross;
217 int m_level;
218
219 wxArrayGenericTreeItems m_children;
220 wxGenericTreeItem *m_parent;
221
222 wxTreeItemAttr *m_attr;
223 };
224
225 // =============================================================================
226 // implementation
227 // =============================================================================
228
229 // ----------------------------------------------------------------------------
230 // private functions
231 // ----------------------------------------------------------------------------
232
233 // translate the key or mouse event flags to the type of selection we're
234 // dealing with
235 static void EventFlagsToSelType(long style,
236 bool shiftDown,
237 bool ctrlDown,
238 bool &is_multiple,
239 bool &extended_select,
240 bool &unselect_others)
241 {
242 is_multiple = (style & wxTR_MULTIPLE) != 0;
243 extended_select = shiftDown && is_multiple;
244 unselect_others = !(extended_select || (ctrlDown && is_multiple));
245 }
246
247 // -----------------------------------------------------------------------------
248 // wxTreeRenameTimer (internal)
249 // -----------------------------------------------------------------------------
250
251 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl *owner )
252 {
253 m_owner = owner;
254 }
255
256 void wxTreeRenameTimer::Notify()
257 {
258 m_owner->OnRenameTimer();
259 }
260
261 //-----------------------------------------------------------------------------
262 // wxTreeTextCtrl (internal)
263 //-----------------------------------------------------------------------------
264
265 IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl,wxTextCtrl);
266
267 BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
268 EVT_CHAR (wxTreeTextCtrl::OnChar)
269 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
270 END_EVENT_TABLE()
271
272 wxTreeTextCtrl::wxTreeTextCtrl( wxWindow *parent,
273 const wxWindowID id,
274 bool *accept,
275 wxString *res,
276 wxGenericTreeCtrl *owner,
277 const wxString &value,
278 const wxPoint &pos,
279 const wxSize &size,
280 int style,
281 const wxValidator& validator,
282 const wxString &name )
283 : wxTextCtrl( parent, id, value, pos, size, style, validator, name )
284 {
285 m_res = res;
286 m_accept = accept;
287 m_owner = owner;
288 (*m_accept) = FALSE;
289 (*m_res) = wxEmptyString;
290 m_startValue = value;
291 }
292
293 void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
294 {
295 if (event.m_keyCode == WXK_RETURN)
296 {
297 (*m_accept) = TRUE;
298 (*m_res) = GetValue();
299
300 if (!wxPendingDelete.Member(this))
301 wxPendingDelete.Append(this);
302
303 if ((*m_accept) && ((*m_res) != m_startValue))
304 m_owner->OnRenameAccept();
305
306 return;
307 }
308 if (event.m_keyCode == WXK_ESCAPE)
309 {
310 (*m_accept) = FALSE;
311 (*m_res) = "";
312
313 if (!wxPendingDelete.Member(this))
314 wxPendingDelete.Append(this);
315
316 return;
317 }
318 event.Skip();
319 }
320
321 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
322 {
323 if (!wxPendingDelete.Member(this))
324 wxPendingDelete.Append(this);
325
326 if ((*m_accept) && ((*m_res) != m_startValue))
327 m_owner->OnRenameAccept();
328 }
329
330 #if 0
331 // -----------------------------------------------------------------------------
332 // wxTreeEvent
333 // -----------------------------------------------------------------------------
334
335 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
336
337 wxTreeEvent::wxTreeEvent( wxEventType commandType, int id )
338 : wxNotifyEvent( commandType, id )
339 {
340 m_code = 0;
341 m_itemOld = (wxGenericTreeItem *)NULL;
342 }
343 #endif
344
345 // -----------------------------------------------------------------------------
346 // wxGenericTreeItem
347 // -----------------------------------------------------------------------------
348
349 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
350 const wxString& text,
351 wxDC& WXUNUSED(dc),
352 int image, int selImage,
353 wxTreeItemData *data)
354 : m_text(text)
355 {
356 m_images[wxTreeItemIcon_Normal] = image;
357 m_images[wxTreeItemIcon_Selected] = selImage;
358 m_images[wxTreeItemIcon_Expanded] = NO_IMAGE;
359 m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE;
360
361 m_data = data;
362 m_x = m_y = 0;
363 m_xCross = m_yCross = 0;
364
365 m_level = 0;
366
367 m_isCollapsed = TRUE;
368 m_hasHilight = FALSE;
369 m_hasPlus = FALSE;
370 m_isBold = FALSE;
371
372 m_parent = parent;
373
374 m_attr = (wxTreeItemAttr *)NULL;
375
376 // We don't know the height here yet.
377 m_width = 0;
378 m_height = 0;
379 }
380
381 wxGenericTreeItem::~wxGenericTreeItem()
382 {
383 delete m_data;
384
385 delete m_attr;
386
387 wxASSERT_MSG( m_children.IsEmpty(),
388 wxT("please call DeleteChildren() before deleting the item") );
389 }
390
391 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl *tree)
392 {
393 size_t count = m_children.Count();
394 for ( size_t n = 0; n < count; n++ )
395 {
396 wxGenericTreeItem *child = m_children[n];
397 if (tree)
398 tree->SendDeleteEvent(child);
399
400 child->DeleteChildren(tree);
401 delete child;
402 }
403
404 m_children.Empty();
405 }
406
407 void wxGenericTreeItem::SetText( const wxString &text )
408 {
409 m_text = text;
410 }
411
412 void wxGenericTreeItem::Reset()
413 {
414 m_text.Empty();
415 for ( int i = 0; i < wxTreeItemIcon_Max; i++ )
416 {
417 m_images[i] = NO_IMAGE;
418 }
419
420 m_data = NULL;
421 m_x = m_y =
422 m_height = m_width = 0;
423 m_xCross =
424 m_yCross = 0;
425
426 m_level = 0;
427
428 DeleteChildren();
429 m_isCollapsed = TRUE;
430
431 m_parent = (wxGenericTreeItem *)NULL;
432 }
433
434 size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
435 {
436 size_t count = m_children.Count();
437 if ( !recursively )
438 return count;
439
440 size_t total = count;
441 for (size_t n = 0; n < count; ++n)
442 {
443 total += m_children[n]->GetChildrenCount();
444 }
445
446 return total;
447 }
448
449 void wxGenericTreeItem::SetCross( int x, int y )
450 {
451 m_xCross = x;
452 m_yCross = y;
453 }
454
455 void wxGenericTreeItem::GetSize( int &x, int &y, const wxGenericTreeCtrl *theTree )
456 {
457 int bottomY=m_y+theTree->GetLineHeight(this);
458 if ( y < bottomY ) y = bottomY;
459 int width = m_x + m_width;
460 if ( x < width ) x = width;
461
462 if (IsExpanded())
463 {
464 size_t count = m_children.Count();
465 for ( size_t n = 0; n < count; ++n )
466 {
467 m_children[n]->GetSize( x, y, theTree );
468 }
469 }
470 }
471
472 wxGenericTreeItem *wxGenericTreeItem::HitTest( const wxPoint& point,
473 const wxGenericTreeCtrl *theTree,
474 int &flags)
475 {
476 if ((point.y > m_y) && (point.y < m_y + theTree->GetLineHeight(this)))
477 {
478 if (point.y < m_y+theTree->GetLineHeight(this)/2 )
479 flags |= wxTREE_HITTEST_ONITEMUPPERPART;
480 else
481 flags |= wxTREE_HITTEST_ONITEMLOWERPART;
482
483 // 5 is the size of the plus sign
484 if ((point.x > m_xCross-5) && (point.x < m_xCross+5) &&
485 (point.y > m_yCross-5) && (point.y < m_yCross+5) &&
486 (IsExpanded() || HasPlus()))
487 {
488 flags|=wxTREE_HITTEST_ONITEMBUTTON;
489 return this;
490 }
491
492 if ((point.x >= m_x) && (point.x <= m_x+m_width))
493 {
494 int image_w = -1;
495 int image_h;
496
497 // assuming every image (normal and selected ) has the same size !
498 if ( (GetImage() != NO_IMAGE) && theTree->m_imageListNormal )
499 theTree->m_imageListNormal->GetSize(GetImage(), image_w, image_h);
500
501 if ((image_w != -1) && (point.x <= m_x + image_w + 1))
502 flags |= wxTREE_HITTEST_ONITEMICON;
503 else
504 flags |= wxTREE_HITTEST_ONITEMLABEL;
505
506 return this;
507 }
508
509 if (point.x < m_x)
510 flags |= wxTREE_HITTEST_ONITEMINDENT;
511 if (point.x > m_x+m_width)
512 flags |= wxTREE_HITTEST_ONITEMRIGHT;
513
514 return this;
515 }
516 else
517 {
518 if (!m_isCollapsed)
519 {
520 size_t count = m_children.Count();
521 for ( size_t n = 0; n < count; n++ )
522 {
523 wxGenericTreeItem *res = m_children[n]->HitTest( point, theTree, flags );
524 if ( res != NULL )
525 return res;
526 }
527 }
528 }
529
530 flags|=wxTREE_HITTEST_NOWHERE;
531
532 return (wxGenericTreeItem*) NULL;
533 }
534
535 int wxGenericTreeItem::GetCurrentImage() const
536 {
537 int image = NO_IMAGE;
538 if ( IsExpanded() )
539 {
540 if ( IsSelected() )
541 {
542 image = GetImage(wxTreeItemIcon_SelectedExpanded);
543 }
544
545 if ( image == NO_IMAGE )
546 {
547 // we usually fall back to the normal item, but try just the
548 // expanded one (and not selected) first in this case
549 image = GetImage(wxTreeItemIcon_Expanded);
550 }
551 }
552 else // not expanded
553 {
554 if ( IsSelected() )
555 image = GetImage(wxTreeItemIcon_Selected);
556 }
557
558 // may be it doesn't have the specific image we want, try the default one
559 // instead
560 if ( image == NO_IMAGE )
561 {
562 image = GetImage();
563 }
564
565 return image;
566 }
567
568 // -----------------------------------------------------------------------------
569 // wxGenericTreeCtrl implementation
570 // -----------------------------------------------------------------------------
571
572 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl, wxScrolledWindow)
573
574 BEGIN_EVENT_TABLE(wxGenericTreeCtrl,wxScrolledWindow)
575 EVT_PAINT (wxGenericTreeCtrl::OnPaint)
576 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse)
577 EVT_CHAR (wxGenericTreeCtrl::OnChar)
578 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus)
579 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus)
580 EVT_IDLE (wxGenericTreeCtrl::OnIdle)
581 END_EVENT_TABLE()
582
583 #if !defined(__WXMSW__) || defined(__WIN16__)
584 /*
585 * wxTreeCtrl has to be a real class or we have problems with
586 * the run-time information.
587 */
588
589 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxGenericTreeCtrl)
590 #endif
591
592 // -----------------------------------------------------------------------------
593 // construction/destruction
594 // -----------------------------------------------------------------------------
595
596 void wxGenericTreeCtrl::Init()
597 {
598 m_current =
599 m_key_current =
600 m_anchor = (wxGenericTreeItem *) NULL;
601 m_hasFocus = FALSE;
602 m_dirty = FALSE;
603
604 m_xScroll = 0;
605 m_yScroll = 0;
606 m_lineHeight = 10;
607 m_indent = 15;
608 m_spacing = 18;
609
610 m_hilightBrush = new wxBrush
611 (
612 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT),
613 wxSOLID
614 );
615
616 m_imageListNormal =
617 m_imageListState = (wxImageList *) NULL;
618
619 m_dragCount = 0;
620 m_isDragging = FALSE;
621 m_dropTarget =
622 m_oldSelection = (wxGenericTreeItem *)NULL;
623
624 m_renameTimer = new wxTreeRenameTimer( this );
625 m_lastOnSame = FALSE;
626
627 m_normalFont = wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT );
628 m_boldFont = wxFont( m_normalFont.GetPointSize(),
629 m_normalFont.GetFamily(),
630 m_normalFont.GetStyle(),
631 wxBOLD,
632 m_normalFont.GetUnderlined());
633 }
634
635 bool wxGenericTreeCtrl::Create(wxWindow *parent, wxWindowID id,
636 const wxPoint& pos, const wxSize& size,
637 long style,
638 const wxValidator &validator,
639 const wxString& name )
640 {
641 Init();
642
643 wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
644
645 #if wxUSE_VALIDATORS
646 SetValidator( validator );
647 #endif
648
649 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX ) );
650 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
651 m_dottedPen = wxPen( "grey", 0, 0 );
652
653 return TRUE;
654 }
655
656 wxGenericTreeCtrl::~wxGenericTreeCtrl()
657 {
658 wxDELETE( m_hilightBrush );
659
660 DeleteAllItems();
661
662 delete m_renameTimer;
663 }
664
665 // -----------------------------------------------------------------------------
666 // accessors
667 // -----------------------------------------------------------------------------
668
669 size_t wxGenericTreeCtrl::GetCount() const
670 {
671 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
672 }
673
674 void wxGenericTreeCtrl::SetIndent(unsigned int indent)
675 {
676 m_indent = indent;
677 m_dirty = TRUE;
678 }
679
680 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing)
681 {
682 m_spacing = spacing;
683 m_dirty = TRUE;
684 }
685
686 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
687 {
688 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
689
690 return ((wxGenericTreeItem*) item.m_pItem)->GetChildrenCount(recursively);
691 }
692
693 // -----------------------------------------------------------------------------
694 // functions to work with tree items
695 // -----------------------------------------------------------------------------
696
697 wxString wxGenericTreeCtrl::GetItemText(const wxTreeItemId& item) const
698 {
699 wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
700
701 return ((wxGenericTreeItem*) item.m_pItem)->GetText();
702 }
703
704 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId& item,
705 wxTreeItemIcon which) const
706 {
707 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
708
709 return ((wxGenericTreeItem*) item.m_pItem)->GetImage(which);
710 }
711
712 wxTreeItemData *wxGenericTreeCtrl::GetItemData(const wxTreeItemId& item) const
713 {
714 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
715
716 return ((wxGenericTreeItem*) item.m_pItem)->GetData();
717 }
718
719 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
720 {
721 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
722
723 wxClientDC dc(this);
724 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
725 pItem->SetText(text);
726 CalculateSize(pItem, dc);
727 RefreshLine(pItem);
728 }
729
730 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId& item,
731 int image,
732 wxTreeItemIcon which)
733 {
734 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
735
736 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
737 pItem->SetImage(image, which);
738
739 wxClientDC dc(this);
740 CalculateSize(pItem, dc);
741 RefreshLine(pItem);
742 }
743
744 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
745 {
746 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
747
748 ((wxGenericTreeItem*) item.m_pItem)->SetData(data);
749 }
750
751 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
752 {
753 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
754
755 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
756 pItem->SetHasPlus(has);
757 RefreshLine(pItem);
758 }
759
760 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
761 {
762 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
763
764 // avoid redrawing the tree if no real change
765 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
766 if ( pItem->IsBold() != bold )
767 {
768 pItem->SetBold(bold);
769 RefreshLine(pItem);
770 }
771 }
772
773 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
774 const wxColour& col)
775 {
776 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
777
778 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
779 pItem->Attr().SetTextColour(col);
780 RefreshLine(pItem);
781 }
782
783 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
784 const wxColour& col)
785 {
786 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
787
788 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
789 pItem->Attr().SetBackgroundColour(col);
790 RefreshLine(pItem);
791 }
792
793 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
794 {
795 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
796
797 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
798 pItem->Attr().SetFont(font);
799 RefreshLine(pItem);
800 }
801
802 // -----------------------------------------------------------------------------
803 // item status inquiries
804 // -----------------------------------------------------------------------------
805
806 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId& WXUNUSED(item)) const
807 {
808 wxFAIL_MSG(wxT("not implemented"));
809
810 return TRUE;
811 }
812
813 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
814 {
815 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
816
817 return !((wxGenericTreeItem*) item.m_pItem)->GetChildren().IsEmpty();
818 }
819
820 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId& item) const
821 {
822 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
823
824 return ((wxGenericTreeItem*) item.m_pItem)->IsExpanded();
825 }
826
827 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId& item) const
828 {
829 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
830
831 return ((wxGenericTreeItem*) item.m_pItem)->IsSelected();
832 }
833
834 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId& item) const
835 {
836 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
837
838 return ((wxGenericTreeItem*) item.m_pItem)->IsBold();
839 }
840
841 // -----------------------------------------------------------------------------
842 // navigation
843 // -----------------------------------------------------------------------------
844
845 wxTreeItemId wxGenericTreeCtrl::GetParent(const wxTreeItemId& item) const
846 {
847 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
848
849 return ((wxGenericTreeItem*) item.m_pItem)->GetParent();
850 }
851
852 wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
853 {
854 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
855
856 cookie = 0;
857 return GetNextChild(item, cookie);
858 }
859
860 wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
861 {
862 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
863
864 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
865 if ( (size_t)cookie < children.Count() )
866 {
867 return children.Item((size_t)cookie++);
868 }
869 else
870 {
871 // there are no more of them
872 return wxTreeItemId();
873 }
874 }
875
876 wxTreeItemId wxGenericTreeCtrl::GetLastChild(const wxTreeItemId& item) const
877 {
878 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
879
880 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
881 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
882 }
883
884 wxTreeItemId wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
885 {
886 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
887
888 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
889 wxGenericTreeItem *parent = i->GetParent();
890 if ( parent == NULL )
891 {
892 // root item doesn't have any siblings
893 return wxTreeItemId();
894 }
895
896 wxArrayGenericTreeItems& siblings = parent->GetChildren();
897 int index = siblings.Index(i);
898 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
899
900 size_t n = (size_t)(index + 1);
901 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
902 }
903
904 wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
905 {
906 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
907
908 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
909 wxGenericTreeItem *parent = i->GetParent();
910 if ( parent == NULL )
911 {
912 // root item doesn't have any siblings
913 return wxTreeItemId();
914 }
915
916 wxArrayGenericTreeItems& siblings = parent->GetChildren();
917 int index = siblings.Index(i);
918 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
919
920 return index == 0 ? wxTreeItemId()
921 : wxTreeItemId(siblings[(size_t)(index - 1)]);
922 }
923
924 wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const
925 {
926 wxFAIL_MSG(wxT("not implemented"));
927
928 return wxTreeItemId();
929 }
930
931 wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
932 {
933 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
934
935 wxFAIL_MSG(wxT("not implemented"));
936
937 return wxTreeItemId();
938 }
939
940 wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
941 {
942 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
943
944 wxFAIL_MSG(wxT("not implemented"));
945
946 return wxTreeItemId();
947 }
948
949 // -----------------------------------------------------------------------------
950 // operations
951 // -----------------------------------------------------------------------------
952
953 wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
954 size_t previous,
955 const wxString& text,
956 int image, int selImage,
957 wxTreeItemData *data)
958 {
959 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
960 if ( !parent )
961 {
962 // should we give a warning here?
963 return AddRoot(text, image, selImage, data);
964 }
965
966 wxClientDC dc(this);
967 wxGenericTreeItem *item =
968 new wxGenericTreeItem( parent, text, dc, image, selImage, data );
969
970 if ( data != NULL )
971 {
972 data->m_pItem = item;
973 }
974
975 parent->Insert( item, previous );
976
977 m_dirty = TRUE;
978
979 return item;
980 }
981
982 wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
983 int image, int selImage,
984 wxTreeItemData *data)
985 {
986 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
987
988 wxClientDC dc(this);
989 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
990 image, selImage, data);
991 if ( data != NULL )
992 {
993 data->m_pItem = m_anchor;
994 }
995
996 if (!HasFlag(wxTR_MULTIPLE))
997 {
998 m_current = m_key_current = m_anchor;
999 m_current->SetHilight( TRUE );
1000 }
1001
1002 m_dirty = TRUE;
1003
1004 return m_anchor;
1005 }
1006
1007 wxTreeItemId wxGenericTreeCtrl::PrependItem(const wxTreeItemId& parent,
1008 const wxString& text,
1009 int image, int selImage,
1010 wxTreeItemData *data)
1011 {
1012 return DoInsertItem(parent, 0u, text, image, selImage, data);
1013 }
1014
1015 wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
1016 const wxTreeItemId& idPrevious,
1017 const wxString& text,
1018 int image, int selImage,
1019 wxTreeItemData *data)
1020 {
1021 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1022 if ( !parent )
1023 {
1024 // should we give a warning here?
1025 return AddRoot(text, image, selImage, data);
1026 }
1027
1028 int index = parent->GetChildren().Index((wxGenericTreeItem*) idPrevious.m_pItem);
1029 wxASSERT_MSG( index != wxNOT_FOUND,
1030 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1031
1032 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1033 }
1034
1035 wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
1036 size_t before,
1037 const wxString& text,
1038 int image, int selImage,
1039 wxTreeItemData *data)
1040 {
1041 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1042 if ( !parent )
1043 {
1044 // should we give a warning here?
1045 return AddRoot(text, image, selImage, data);
1046 }
1047
1048 return DoInsertItem(parentId, before, text, image, selImage, data);
1049 }
1050
1051 wxTreeItemId wxGenericTreeCtrl::AppendItem(const wxTreeItemId& parentId,
1052 const wxString& text,
1053 int image, int selImage,
1054 wxTreeItemData *data)
1055 {
1056 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1057 if ( !parent )
1058 {
1059 // should we give a warning here?
1060 return AddRoot(text, image, selImage, data);
1061 }
1062
1063 return DoInsertItem( parent, parent->GetChildren().Count(), text,
1064 image, selImage, data);
1065 }
1066
1067 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
1068 {
1069 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
1070 event.m_item = item;
1071 event.SetEventObject( this );
1072 ProcessEvent( event );
1073 }
1074
1075 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
1076 {
1077 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1078 item->DeleteChildren(this);
1079
1080 m_dirty = TRUE;
1081 }
1082
1083 void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
1084 {
1085 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1086
1087 // don't stay with invalid m_key_current or we will crash in the next call
1088 // to OnChar()
1089 bool changeKeyCurrent = FALSE;
1090 wxGenericTreeItem *itemKey = m_key_current;
1091 while ( itemKey && !changeKeyCurrent )
1092 {
1093 if ( itemKey == item )
1094 {
1095 // m_key_current is a descendant of the item being deleted
1096 changeKeyCurrent = TRUE;
1097 }
1098 else
1099 {
1100 itemKey = itemKey->GetParent();
1101 }
1102 }
1103
1104 wxGenericTreeItem *parent = item->GetParent();
1105 if ( parent )
1106 {
1107 parent->GetChildren().Remove( item ); // remove by value
1108 }
1109
1110 if ( changeKeyCurrent )
1111 {
1112 // may be NULL or not
1113 m_key_current = parent;
1114 }
1115
1116 item->DeleteChildren(this);
1117 SendDeleteEvent(item);
1118 delete item;
1119
1120 m_dirty = TRUE;
1121 }
1122
1123 void wxGenericTreeCtrl::DeleteAllItems()
1124 {
1125 if ( m_anchor )
1126 {
1127 m_anchor->DeleteChildren(this);
1128 delete m_anchor;
1129
1130 m_anchor = NULL;
1131
1132 m_dirty = TRUE;
1133 }
1134 }
1135
1136 void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId)
1137 {
1138 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1139
1140 wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") );
1141
1142 if ( !item->HasPlus() )
1143 return;
1144
1145 if ( item->IsExpanded() )
1146 return;
1147
1148 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
1149 event.m_item = item;
1150 event.SetEventObject( this );
1151
1152 if ( ProcessEvent( event ) && !event.IsAllowed() )
1153 {
1154 // cancelled by program
1155 return;
1156 }
1157
1158 item->Expand();
1159 CalculatePositions();
1160
1161 RefreshSubtree(item);
1162
1163 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1164 ProcessEvent( event );
1165 }
1166
1167 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId& item)
1168 {
1169 Expand(item);
1170 if ( IsExpanded(item) )
1171 {
1172 long cookie;
1173 wxTreeItemId child = GetFirstChild(item, cookie);
1174 while ( child.IsOk() )
1175 {
1176 ExpandAll(child);
1177
1178 child = GetNextChild(item, cookie);
1179 }
1180 }
1181 }
1182
1183 void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
1184 {
1185 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1186
1187 if ( !item->IsExpanded() )
1188 return;
1189
1190 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
1191 event.m_item = item;
1192 event.SetEventObject( this );
1193 if ( ProcessEvent( event ) && !event.IsAllowed() )
1194 {
1195 // cancelled by program
1196 return;
1197 }
1198
1199 item->Collapse();
1200
1201 wxArrayGenericTreeItems& children = item->GetChildren();
1202 size_t count = children.Count();
1203 for ( size_t n = 0; n < count; n++ )
1204 {
1205 Collapse(children[n]);
1206 }
1207
1208 CalculatePositions();
1209
1210 RefreshSubtree(item);
1211
1212 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1213 ProcessEvent( event );
1214 }
1215
1216 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1217 {
1218 Collapse(item);
1219 DeleteChildren(item);
1220 }
1221
1222 void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId)
1223 {
1224 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1225
1226 if (item->IsExpanded())
1227 Collapse(itemId);
1228 else
1229 Expand(itemId);
1230 }
1231
1232 void wxGenericTreeCtrl::Unselect()
1233 {
1234 if (m_current)
1235 {
1236 m_current->SetHilight( FALSE );
1237 RefreshLine( m_current );
1238 }
1239 }
1240
1241 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
1242 {
1243 if (item->IsSelected())
1244 {
1245 item->SetHilight(FALSE);
1246 RefreshLine(item);
1247 }
1248
1249 if (item->HasChildren())
1250 {
1251 wxArrayGenericTreeItems& children = item->GetChildren();
1252 size_t count = children.Count();
1253 for ( size_t n = 0; n < count; ++n )
1254 {
1255 UnselectAllChildren(children[n]);
1256 }
1257 }
1258 }
1259
1260 void wxGenericTreeCtrl::UnselectAll()
1261 {
1262 UnselectAllChildren((wxGenericTreeItem*) GetRootItem().m_pItem);
1263 }
1264
1265 // Recursive function !
1266 // To stop we must have crt_item<last_item
1267 // Algorithm :
1268 // Tag all next children, when no more children,
1269 // Move to parent (not to tag)
1270 // Keep going... if we found last_item, we stop.
1271 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1272 {
1273 wxGenericTreeItem *parent = crt_item->GetParent();
1274
1275 if (parent == NULL) // This is root item
1276 return TagAllChildrenUntilLast(crt_item, last_item, select);
1277
1278 wxArrayGenericTreeItems& children = parent->GetChildren();
1279 int index = children.Index(crt_item);
1280 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1281
1282 size_t count = children.Count();
1283 for (size_t n=(size_t)(index+1); n<count; ++n)
1284 {
1285 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1286 }
1287
1288 return TagNextChildren(parent, last_item, select);
1289 }
1290
1291 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1292 {
1293 crt_item->SetHilight(select);
1294 RefreshLine(crt_item);
1295
1296 if (crt_item==last_item)
1297 return TRUE;
1298
1299 if (crt_item->HasChildren())
1300 {
1301 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1302 size_t count = children.Count();
1303 for ( size_t n = 0; n < count; ++n )
1304 {
1305 if (TagAllChildrenUntilLast(children[n], last_item, select))
1306 return TRUE;
1307 }
1308 }
1309
1310 return FALSE;
1311 }
1312
1313 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
1314 {
1315 // item2 is not necessary after item1
1316 wxGenericTreeItem *first=NULL, *last=NULL;
1317
1318 // choice first' and 'last' between item1 and item2
1319 if (item1->GetY()<item2->GetY())
1320 {
1321 first=item1;
1322 last=item2;
1323 }
1324 else
1325 {
1326 first=item2;
1327 last=item1;
1328 }
1329
1330 bool select = m_current->IsSelected();
1331
1332 if ( TagAllChildrenUntilLast(first,last,select) )
1333 return;
1334
1335 TagNextChildren(first,last,select);
1336 }
1337
1338 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId,
1339 bool unselect_others,
1340 bool extended_select)
1341 {
1342 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
1343
1344 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
1345 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1346
1347 //wxCHECK_RET( ( (!unselect_others) && is_single),
1348 // wxT("this is a single selection tree") );
1349
1350 // to keep going anyhow !!!
1351 if (is_single)
1352 {
1353 if (item->IsSelected())
1354 return; // nothing to do
1355 unselect_others = TRUE;
1356 extended_select = FALSE;
1357 }
1358 else if ( unselect_others && item->IsSelected() )
1359 {
1360 // selection change if there is more than one item currently selected
1361 wxArrayTreeItemIds selected_items;
1362 if ( GetSelections(selected_items) == 1 )
1363 return;
1364 }
1365
1366 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
1367 event.m_item = item;
1368 event.m_itemOld = m_current;
1369 event.SetEventObject( this );
1370 // TODO : Here we don't send any selection mode yet !
1371
1372 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
1373 return;
1374
1375 // ctrl press
1376 if (unselect_others)
1377 {
1378 if (is_single) Unselect(); // to speed up thing
1379 else UnselectAll();
1380 }
1381
1382 // shift press
1383 if (extended_select)
1384 {
1385 if ( !m_current )
1386 {
1387 m_current =
1388 m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
1389 }
1390
1391 // don't change the mark (m_current)
1392 SelectItemRange(m_current, item);
1393 }
1394 else
1395 {
1396 bool select=TRUE; // the default
1397
1398 // Check if we need to toggle hilight (ctrl mode)
1399 if (!unselect_others)
1400 select=!item->IsSelected();
1401
1402 m_current = m_key_current = item;
1403 m_current->SetHilight(select);
1404 RefreshLine( m_current );
1405 }
1406
1407 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1408 GetEventHandler()->ProcessEvent( event );
1409 }
1410
1411 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
1412 wxArrayTreeItemIds &array) const
1413 {
1414 if ( item->IsSelected() )
1415 array.Add(wxTreeItemId(item));
1416
1417 if ( item->HasChildren() )
1418 {
1419 wxArrayGenericTreeItems& children = item->GetChildren();
1420 size_t count = children.GetCount();
1421 for ( size_t n = 0; n < count; ++n )
1422 FillArray(children[n], array);
1423 }
1424 }
1425
1426 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
1427 {
1428 array.Empty();
1429 wxTreeItemId idRoot = GetRootItem();
1430 if ( idRoot.IsOk() )
1431 {
1432 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
1433 }
1434 //else: the tree is empty, so no selections
1435
1436 return array.Count();
1437 }
1438
1439 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1440 {
1441 if (!item.IsOk()) return;
1442
1443 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
1444
1445 // first expand all parent branches
1446 wxGenericTreeItem *parent = gitem->GetParent();
1447 while ( parent )
1448 {
1449 Expand(parent);
1450 parent = parent->GetParent();
1451 }
1452
1453 //if (parent) CalculatePositions();
1454
1455 ScrollTo(item);
1456 }
1457
1458 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
1459 {
1460 if (!item.IsOk()) return;
1461
1462 // We have to call this here because the label in
1463 // question might just have been added and no screen
1464 // update taken place.
1465 if (m_dirty) wxYield();
1466
1467 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
1468
1469 // now scroll to the item
1470 int item_y = gitem->GetY();
1471
1472 int start_x = 0;
1473 int start_y = 0;
1474 ViewStart( &start_x, &start_y );
1475 start_y *= PIXELS_PER_UNIT;
1476
1477 int client_h = 0;
1478 int client_w = 0;
1479 GetClientSize( &client_w, &client_h );
1480
1481 if (item_y < start_y+3)
1482 {
1483 // going down
1484 int x = 0;
1485 int y = 0;
1486 m_anchor->GetSize( x, y, this );
1487 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1488 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1489 int x_pos = GetScrollPos( wxHORIZONTAL );
1490 // Item should appear at top
1491 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
1492 }
1493 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
1494 {
1495 // going up
1496 int x = 0;
1497 int y = 0;
1498 m_anchor->GetSize( x, y, this );
1499 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1500 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1501 item_y += PIXELS_PER_UNIT+2;
1502 int x_pos = GetScrollPos( wxHORIZONTAL );
1503 // Item should appear at bottom
1504 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, (item_y+GetLineHeight(gitem)-client_h)/PIXELS_PER_UNIT );
1505 }
1506 }
1507
1508 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1509 static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
1510
1511 static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
1512 wxGenericTreeItem **item2)
1513 {
1514 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1515
1516 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
1517 }
1518
1519 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1520 const wxTreeItemId& item2)
1521 {
1522 return wxStrcmp(GetItemText(item1), GetItemText(item2));
1523 }
1524
1525 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
1526 {
1527 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
1528
1529 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1530
1531 wxCHECK_RET( !s_treeBeingSorted,
1532 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1533
1534 wxArrayGenericTreeItems& children = item->GetChildren();
1535 if ( children.Count() > 1 )
1536 {
1537 s_treeBeingSorted = this;
1538 children.Sort(tree_ctrl_compare_func);
1539 s_treeBeingSorted = NULL;
1540
1541 m_dirty = TRUE;
1542 }
1543 //else: don't make the tree dirty as nothing changed
1544 }
1545
1546 wxImageList *wxGenericTreeCtrl::GetImageList() const
1547 {
1548 return m_imageListNormal;
1549 }
1550
1551 wxImageList *wxGenericTreeCtrl::GetStateImageList() const
1552 {
1553 return m_imageListState;
1554 }
1555
1556 void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
1557 {
1558 m_imageListNormal = imageList;
1559
1560 if ( !m_imageListNormal )
1561 return;
1562
1563 // Calculate a m_lineHeight value from the image sizes.
1564 // May be toggle off. Then wxGenericTreeCtrl will spread when
1565 // necessary (which might look ugly).
1566 wxClientDC dc(this);
1567 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1568 int width = 0, height = 0,
1569 n = m_imageListNormal->GetImageCount();
1570
1571 for (int i = 0; i < n ; i++)
1572 {
1573 m_imageListNormal->GetSize(i, width, height);
1574 if (height > m_lineHeight) m_lineHeight = height;
1575 }
1576
1577 if (m_lineHeight < 40)
1578 m_lineHeight += 2; // at least 2 pixels
1579 else
1580 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
1581 }
1582
1583 void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
1584 {
1585 m_imageListState = imageList;
1586 }
1587
1588 // -----------------------------------------------------------------------------
1589 // helpers
1590 // -----------------------------------------------------------------------------
1591
1592 void wxGenericTreeCtrl::AdjustMyScrollbars()
1593 {
1594 if (m_anchor)
1595 {
1596 int x = 0;
1597 int y = 0;
1598 m_anchor->GetSize( x, y, this );
1599 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1600 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1601 int x_pos = GetScrollPos( wxHORIZONTAL );
1602 int y_pos = GetScrollPos( wxVERTICAL );
1603 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
1604 }
1605 else
1606 {
1607 SetScrollbars( 0, 0, 0, 0 );
1608 }
1609 }
1610
1611 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
1612 {
1613 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
1614 return item->GetHeight();
1615 else
1616 return m_lineHeight;
1617 }
1618
1619 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
1620 {
1621 wxTreeItemAttr *attr = item->GetAttributes();
1622 if ( attr && attr->HasFont() )
1623 dc.SetFont(attr->GetFont());
1624 else if (item->IsBold())
1625 dc.SetFont(m_boldFont);
1626
1627 long text_w = 0;
1628 long text_h = 0;
1629 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
1630
1631 int image_h = 0;
1632 int image_w = 0;
1633 int image = item->GetCurrentImage();
1634 if ( image != NO_IMAGE )
1635 {
1636 if ( m_imageListNormal )
1637 {
1638 m_imageListNormal->GetSize( image, image_w, image_h );
1639 image_w += 4;
1640 }
1641 else
1642 {
1643 image = NO_IMAGE;
1644 }
1645 }
1646
1647 int total_h = GetLineHeight(item);
1648
1649 if (item->IsSelected())
1650 dc.SetBrush(*m_hilightBrush);
1651 else
1652 {
1653 wxColour colBg;
1654 if ( attr && attr->HasBackgroundColour() )
1655 colBg = attr->GetBackgroundColour();
1656 else
1657 colBg = m_backgroundColour;
1658 dc.SetBrush(wxBrush(colBg, wxSOLID));
1659 }
1660
1661 dc.DrawRectangle( item->GetX()-2, item->GetY(), item->GetWidth()+2, total_h );
1662
1663 if ( image != NO_IMAGE )
1664 {
1665 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
1666 m_imageListNormal->Draw( image, dc,
1667 item->GetX(),
1668 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
1669 wxIMAGELIST_DRAW_TRANSPARENT );
1670 dc.DestroyClippingRegion();
1671 }
1672
1673 dc.SetBackgroundMode(wxTRANSPARENT);
1674 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
1675 dc.DrawText( item->GetText(),
1676 (wxCoord)(image_w + item->GetX()),
1677 (wxCoord)(item->GetY() + extraH));
1678
1679 // restore normal font
1680 dc.SetFont( m_normalFont );
1681 }
1682
1683 // Now y stands for the top of the item, whereas it used to stand for middle !
1684 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
1685 {
1686 int horizX = level*m_indent;
1687
1688 item->SetX( horizX+m_indent+m_spacing );
1689 item->SetY( y );
1690
1691 int oldY = y;
1692 y+=GetLineHeight(item)/2;
1693
1694 item->SetCross( horizX+m_indent, y );
1695
1696 int exposed_x = dc.LogicalToDeviceX( 0 );
1697 int exposed_y = dc.LogicalToDeviceY( item->GetY() );
1698
1699 bool drawLines = ((GetWindowStyle() & wxTR_NO_LINES) == 0);
1700
1701 if (IsExposed( exposed_x, exposed_y, 10000, GetLineHeight(item) )) // 10000 = very much
1702 {
1703 int startX = horizX;
1704 int endX = horizX + (m_indent-5);
1705
1706 // if (!item->HasChildren()) endX += (m_indent+5);
1707 if (!item->HasChildren()) endX += 20;
1708
1709 if (drawLines)
1710 dc.DrawLine( startX, y, endX, y );
1711
1712 if (item->HasPlus())
1713 {
1714 if (drawLines)
1715 dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y );
1716 dc.SetPen( *wxGREY_PEN );
1717 dc.SetBrush( *wxWHITE_BRUSH );
1718 dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 );
1719
1720 dc.SetPen( *wxBLACK_PEN );
1721 dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y );
1722 if (!item->IsExpanded())
1723 dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 );
1724
1725 dc.SetPen( m_dottedPen );
1726 }
1727
1728 wxPen *pen = wxTRANSPARENT_PEN;
1729 wxColour colText;
1730
1731 if ( item->IsSelected() )
1732 {
1733 colText = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
1734
1735 if ( m_hasFocus )
1736 pen = wxBLACK_PEN;
1737
1738 }
1739 else
1740 {
1741 wxTreeItemAttr *attr = item->GetAttributes();
1742 if ( attr && attr->HasTextColour() )
1743 colText = attr->GetTextColour();
1744 else
1745 colText = *wxBLACK;
1746 }
1747
1748 // prepare to draw
1749 dc.SetTextForeground(colText);
1750 dc.SetPen(*pen);
1751
1752 // draw
1753 PaintItem(item, dc);
1754
1755 // restore DC objects
1756 dc.SetBrush( *wxWHITE_BRUSH );
1757 dc.SetPen( m_dottedPen );
1758 dc.SetTextForeground( *wxBLACK );
1759 }
1760
1761 y = oldY+GetLineHeight(item);
1762
1763 if (item->IsExpanded())
1764 {
1765 oldY+=GetLineHeight(item)/2;
1766 int semiOldY=0;
1767
1768 wxArrayGenericTreeItems& children = item->GetChildren();
1769 size_t n, count = children.Count();
1770 for ( n = 0; n < count; ++n )
1771 {
1772 semiOldY=y;
1773 PaintLevel( children[n], dc, level+1, y );
1774 }
1775
1776 // it may happen that the item is expanded but has no items (when you
1777 // delete all its children for example) - don't draw the vertical line
1778 // in this case
1779 if (count > 0)
1780 {
1781 semiOldY+=GetLineHeight(children[--n])/2;
1782 if (drawLines)
1783 dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY );
1784 }
1785 }
1786 }
1787
1788 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
1789 {
1790 if ( item )
1791 {
1792 if ( item->HasPlus() )
1793 {
1794 // it's a folder, indicate it by a border
1795 DrawBorder(item);
1796 }
1797 else
1798 {
1799 // draw a line under the drop target because the item will be
1800 // dropped there
1801 DrawLine(item, TRUE /* below */);
1802 }
1803
1804 SetCursor(wxCURSOR_BULLSEYE);
1805 }
1806 else
1807 {
1808 // can't drop here
1809 SetCursor(wxCURSOR_NO_ENTRY);
1810 }
1811 }
1812
1813 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item)
1814 {
1815 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
1816
1817 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1818
1819 wxClientDC dc(this);
1820 PrepareDC( dc );
1821 dc.SetLogicalFunction(wxINVERT);
1822 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1823
1824 int w = i->GetWidth() + 2;
1825 int h = GetLineHeight(i) + 2;
1826
1827 dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h);
1828 }
1829
1830 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
1831 {
1832 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
1833
1834 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1835
1836 wxClientDC dc(this);
1837 PrepareDC( dc );
1838 dc.SetLogicalFunction(wxINVERT);
1839
1840 int x = i->GetX(),
1841 y = i->GetY();
1842 if ( below )
1843 {
1844 y += GetLineHeight(i) - 1;
1845 }
1846
1847 dc.DrawLine( x, y, x + i->GetWidth(), y);
1848 }
1849
1850 // -----------------------------------------------------------------------------
1851 // wxWindows callbacks
1852 // -----------------------------------------------------------------------------
1853
1854 void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
1855 {
1856 wxPaintDC dc(this);
1857 PrepareDC( dc );
1858
1859 if ( !m_anchor)
1860 return;
1861
1862 dc.SetFont( m_normalFont );
1863 dc.SetPen( m_dottedPen );
1864
1865 // this is now done dynamically
1866 //if(GetImageList() == NULL)
1867 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1868
1869 int y = 2;
1870 PaintLevel( m_anchor, dc, 0, y );
1871 }
1872
1873 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1874 {
1875 m_hasFocus = TRUE;
1876
1877 if (m_current) RefreshLine( m_current );
1878 }
1879
1880 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
1881 {
1882 m_hasFocus = FALSE;
1883
1884 if (m_current) RefreshLine( m_current );
1885 }
1886
1887 void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
1888 {
1889 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
1890 te.m_code = (int)event.KeyCode();
1891 te.SetEventObject( this );
1892 GetEventHandler()->ProcessEvent( te );
1893
1894 if ( (m_current == 0) || (m_key_current == 0) )
1895 {
1896 event.Skip();
1897 return;
1898 }
1899
1900 // how should the selection work for this event?
1901 bool is_multiple, extended_select, unselect_others;
1902 EventFlagsToSelType(GetWindowStyleFlag(),
1903 event.ShiftDown(),
1904 event.ControlDown(),
1905 is_multiple, extended_select, unselect_others);
1906
1907 // + : Expand
1908 // - : Collaspe
1909 // * : Expand all/Collapse all
1910 // ' ' | return : activate
1911 // up : go up (not last children!)
1912 // down : go down
1913 // left : go to parent
1914 // right : open if parent and go next
1915 // home : go to root
1916 // end : go to last item without opening parents
1917 switch (event.KeyCode())
1918 {
1919 case '+':
1920 case WXK_ADD:
1921 if (m_current->HasPlus() && !IsExpanded(m_current))
1922 {
1923 Expand(m_current);
1924 }
1925 break;
1926
1927 case '*':
1928 case WXK_MULTIPLY:
1929 if ( !IsExpanded(m_current) )
1930 {
1931 // expand all
1932 ExpandAll(m_current);
1933 break;
1934 }
1935 //else: fall through to Collapse() it
1936
1937 case '-':
1938 case WXK_SUBTRACT:
1939 if (IsExpanded(m_current))
1940 {
1941 Collapse(m_current);
1942 }
1943 break;
1944
1945 case ' ':
1946 case WXK_RETURN:
1947 {
1948 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1949 event.m_item = m_current;
1950 event.m_code = 0;
1951 event.SetEventObject( this );
1952 GetEventHandler()->ProcessEvent( event );
1953 }
1954 break;
1955
1956 // up goes to the previous sibling or to the last of its children if
1957 // it's expanded
1958 case WXK_UP:
1959 {
1960 wxTreeItemId prev = GetPrevSibling( m_key_current );
1961 if (!prev)
1962 {
1963 prev = GetParent( m_key_current );
1964 if (prev)
1965 {
1966 long cockie = 0;
1967 wxTreeItemId current = m_key_current;
1968 if (current == GetFirstChild( prev, cockie ))
1969 {
1970 // otherwise we return to where we came from
1971 SelectItem( prev, unselect_others, extended_select );
1972 m_key_current= (wxGenericTreeItem*) prev.m_pItem;
1973 EnsureVisible( prev );
1974 break;
1975 }
1976 }
1977 }
1978 if (prev)
1979 {
1980 while ( IsExpanded(prev) && HasChildren(prev) )
1981 {
1982 wxTreeItemId child = GetLastChild(prev);
1983 if ( child )
1984 {
1985 prev = child;
1986 }
1987 }
1988
1989 SelectItem( prev, unselect_others, extended_select );
1990 m_key_current=(wxGenericTreeItem*) prev.m_pItem;
1991 EnsureVisible( prev );
1992 }
1993 }
1994 break;
1995
1996 // left arrow goes to the parent
1997 case WXK_LEFT:
1998 {
1999 wxTreeItemId prev = GetParent( m_current );
2000 if (prev)
2001 {
2002 EnsureVisible( prev );
2003 SelectItem( prev, unselect_others, extended_select );
2004 }
2005 }
2006 break;
2007
2008 case WXK_RIGHT:
2009 // this works the same as the down arrow except that we also expand the
2010 // item if it wasn't expanded yet
2011 Expand(m_current);
2012 // fall through
2013
2014 case WXK_DOWN:
2015 {
2016 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
2017 {
2018 long cookie = 0;
2019 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
2020 SelectItem( child, unselect_others, extended_select );
2021 m_key_current=(wxGenericTreeItem*) child.m_pItem;
2022 EnsureVisible( child );
2023 }
2024 else
2025 {
2026 wxTreeItemId next = GetNextSibling( m_key_current );
2027 if (!next)
2028 {
2029 wxTreeItemId current = m_key_current;
2030 while (current && !next)
2031 {
2032 current = GetParent( current );
2033 if (current) next = GetNextSibling( current );
2034 }
2035 }
2036 if (next)
2037 {
2038 SelectItem( next, unselect_others, extended_select );
2039 m_key_current=(wxGenericTreeItem*) next.m_pItem;
2040 EnsureVisible( next );
2041 }
2042 }
2043 }
2044 break;
2045
2046 // <End> selects the last visible tree item
2047 case WXK_END:
2048 {
2049 wxTreeItemId last = GetRootItem();
2050
2051 while ( last.IsOk() && IsExpanded(last) )
2052 {
2053 wxTreeItemId lastChild = GetLastChild(last);
2054
2055 // it may happen if the item was expanded but then all of
2056 // its children have been deleted - so IsExpanded() returned
2057 // TRUE, but GetLastChild() returned invalid item
2058 if ( !lastChild )
2059 break;
2060
2061 last = lastChild;
2062 }
2063
2064 if ( last.IsOk() )
2065 {
2066 EnsureVisible( last );
2067 SelectItem( last, unselect_others, extended_select );
2068 }
2069 }
2070 break;
2071
2072 // <Home> selects the root item
2073 case WXK_HOME:
2074 {
2075 wxTreeItemId prev = GetRootItem();
2076 if (prev)
2077 {
2078 EnsureVisible( prev );
2079 SelectItem( prev, unselect_others, extended_select );
2080 }
2081 }
2082 break;
2083
2084 default:
2085 event.Skip();
2086 }
2087 }
2088
2089 wxTreeItemId wxGenericTreeCtrl::HitTest(const wxPoint& point, int& flags)
2090 {
2091 // We have to call this here because the label in
2092 // question might just have been added and no screen
2093 // update taken place.
2094 if (m_dirty) wxYield();
2095
2096 wxClientDC dc(this);
2097 PrepareDC(dc);
2098 wxCoord x = dc.DeviceToLogicalX( point.x );
2099 wxCoord y = dc.DeviceToLogicalY( point.y );
2100 int w, h;
2101 GetSize(&w, &h);
2102
2103 flags=0;
2104 if (point.x<0) flags|=wxTREE_HITTEST_TOLEFT;
2105 if (point.x>w) flags|=wxTREE_HITTEST_TORIGHT;
2106 if (point.y<0) flags|=wxTREE_HITTEST_ABOVE;
2107 if (point.y>h) flags|=wxTREE_HITTEST_BELOW;
2108
2109 return m_anchor->HitTest( wxPoint(x, y), this, flags);
2110 }
2111
2112 // get the bounding rectangle of the item (or of its label only)
2113 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
2114 wxRect& rect,
2115 bool textOnly) const
2116 {
2117 wxCHECK2_MSG( item.IsOk(), FALSE, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2118
2119 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2120
2121 int startX, startY;
2122 GetViewStart(& startX, & startY);
2123
2124 rect.x = i->GetX() - startX*PIXELS_PER_UNIT; rect.y = i->GetY()*PIXELS_PER_UNIT;
2125 rect.width = i->GetWidth(); rect.height = i->GetHeight();
2126
2127 return TRUE;
2128
2129 // wxFAIL_MSG(wxT("GetBoundingRect unimplemented"));
2130 // return FALSE;
2131 }
2132
2133 /* **** */
2134
2135 void wxGenericTreeCtrl::Edit( const wxTreeItemId& item )
2136 {
2137 if (!item.IsOk()) return;
2138
2139 m_currentEdit = (wxGenericTreeItem*) item.m_pItem;
2140
2141 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
2142 te.m_item = m_currentEdit;
2143 te.SetEventObject( this );
2144 GetEventHandler()->ProcessEvent( te );
2145
2146 if (!te.IsAllowed()) return;
2147
2148 // We have to call this here because the label in
2149 // question might just have been added and no screen
2150 // update taken place.
2151 if (m_dirty) wxYield();
2152
2153 wxString s = m_currentEdit->GetText();
2154 int x = m_currentEdit->GetX();
2155 int y = m_currentEdit->GetY();
2156 int w = m_currentEdit->GetWidth();
2157 int h = m_currentEdit->GetHeight();
2158
2159 int image_h = 0;
2160 int image_w = 0;
2161
2162 int image = m_currentEdit->GetCurrentImage();
2163 if ( image != NO_IMAGE )
2164 {
2165 if ( m_imageListNormal )
2166 {
2167 m_imageListNormal->GetSize( image, image_w, image_h );
2168 image_w += 4;
2169 }
2170 else
2171 {
2172 wxFAIL_MSG(_T("you must create an image list to use images!"));
2173 }
2174 }
2175 x += image_w;
2176 w -= image_w + 4; // I don't know why +4 is needed
2177
2178 wxClientDC dc(this);
2179 PrepareDC( dc );
2180 x = dc.LogicalToDeviceX( x );
2181 y = dc.LogicalToDeviceY( y );
2182
2183 wxTreeTextCtrl *text = new wxTreeTextCtrl(
2184 this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) );
2185 text->SetFocus();
2186 }
2187
2188 void wxGenericTreeCtrl::OnRenameTimer()
2189 {
2190 Edit( m_current );
2191 }
2192
2193 void wxGenericTreeCtrl::OnRenameAccept()
2194 {
2195 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
2196 le.m_item = m_currentEdit;
2197 le.SetEventObject( this );
2198 le.m_label = m_renameRes;
2199 GetEventHandler()->ProcessEvent( le );
2200
2201 if (!le.IsAllowed()) return;
2202
2203 SetItemText( m_currentEdit, m_renameRes );
2204 }
2205
2206 void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
2207 {
2208 if ( !m_anchor ) return;
2209
2210 // we process left mouse up event (enables in-place edit), right down
2211 // (pass to the user code), left dbl click (activate item) and
2212 // dragging/moving events for items drag-and-drop
2213 if ( !(event.LeftDown() ||
2214 event.LeftUp() ||
2215 event.RightDown() ||
2216 event.LeftDClick() ||
2217 event.Dragging() ||
2218 ((event.Moving() || event.RightUp()) && m_isDragging)) )
2219 {
2220 event.Skip();
2221
2222 return;
2223 }
2224
2225 wxClientDC dc(this);
2226 PrepareDC(dc);
2227 wxCoord x = dc.DeviceToLogicalX( event.GetX() );
2228 wxCoord y = dc.DeviceToLogicalY( event.GetY() );
2229
2230 int flags = 0;
2231 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), this, flags);
2232
2233 if ( event.Dragging() && !m_isDragging )
2234 {
2235 if (m_dragCount == 0)
2236 m_dragStart = wxPoint(x,y);
2237
2238 m_dragCount++;
2239
2240 if (m_dragCount != 3)
2241 {
2242 // wait until user drags a bit further...
2243 return;
2244 }
2245
2246 wxEventType command = event.RightIsDown()
2247 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2248 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
2249
2250 wxTreeEvent nevent( command, GetId() );
2251 nevent.m_item = m_current;
2252 nevent.SetEventObject(this);
2253
2254 // by default the dragging is not supported, the user code must
2255 // explicitly allow the event for it to take place
2256 nevent.Veto();
2257
2258 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
2259 {
2260 // we're going to drag this item
2261 m_isDragging = TRUE;
2262
2263 // remember the old cursor because we will change it while
2264 // dragging
2265 m_oldCursor = m_cursor;
2266
2267 // in a single selection control, hide the selection temporarily
2268 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
2269 {
2270 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
2271
2272 if ( m_oldSelection )
2273 {
2274 m_oldSelection->SetHilight(FALSE);
2275 RefreshLine(m_oldSelection);
2276 }
2277 }
2278
2279 CaptureMouse();
2280 }
2281 }
2282 else if ( event.Moving() )
2283 {
2284 if ( item != m_dropTarget )
2285 {
2286 // unhighlight the previous drop target
2287 DrawDropEffect(m_dropTarget);
2288
2289 m_dropTarget = item;
2290
2291 // highlight the current drop target if any
2292 DrawDropEffect(m_dropTarget);
2293
2294 wxYield();
2295 }
2296 }
2297 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
2298 {
2299 // erase the highlighting
2300 DrawDropEffect(m_dropTarget);
2301
2302 // generate the drag end event
2303 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId());
2304
2305 event.m_item = item;
2306 event.m_pointDrag = wxPoint(x, y);
2307 event.SetEventObject(this);
2308
2309 (void)GetEventHandler()->ProcessEvent(event);
2310
2311 m_isDragging = FALSE;
2312 m_dropTarget = (wxGenericTreeItem *)NULL;
2313
2314 if ( m_oldSelection )
2315 {
2316 m_oldSelection->SetHilight(TRUE);
2317 RefreshLine(m_oldSelection);
2318 m_oldSelection = (wxGenericTreeItem *)NULL;
2319 }
2320
2321 ReleaseMouse();
2322
2323 SetCursor(m_oldCursor);
2324
2325 wxYield();
2326 }
2327 else
2328 {
2329 // here we process only the messages which happen on tree items
2330
2331 m_dragCount = 0;
2332
2333 if (item == NULL) return; /* we hit the blank area */
2334
2335 if ( event.RightDown() )
2336 {
2337 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
2338 nevent.m_item = item;
2339 nevent.m_code = 0;
2340 nevent.SetEventObject(this);
2341 GetEventHandler()->ProcessEvent(nevent);
2342 }
2343 else if ( event.LeftUp() && m_lastOnSame )
2344 {
2345 if ( (item == m_current) &&
2346 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
2347 HasFlag(wxTR_EDIT_LABELS) )
2348 {
2349 m_renameTimer->Start( 100, TRUE );
2350 }
2351
2352 m_lastOnSame = FALSE;
2353 }
2354 else
2355 {
2356 if ( event.LeftDown() )
2357 {
2358 m_lastOnSame = item == m_current;
2359 }
2360
2361 // how should the selection work for this event?
2362 bool is_multiple, extended_select, unselect_others;
2363 EventFlagsToSelType(GetWindowStyleFlag(),
2364 event.ShiftDown(),
2365 event.ControlDown(),
2366 is_multiple, extended_select, unselect_others);
2367
2368 if ( (flags & wxTREE_HITTEST_ONITEMBUTTON) && event.LeftDown() )
2369 {
2370 Toggle( item );
2371 if ( is_multiple )
2372 return;
2373 }
2374
2375 SelectItem(item, unselect_others, extended_select);
2376
2377 if ( event.LeftDClick() )
2378 {
2379 // double clicking should not start editing the item label
2380 m_renameTimer->Stop();
2381 m_lastOnSame = FALSE;
2382
2383 wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2384 nevent.m_item = item;
2385 nevent.m_code = 0;
2386 nevent.SetEventObject( this );
2387 GetEventHandler()->ProcessEvent( nevent );
2388 }
2389 }
2390 }
2391 }
2392
2393 void wxGenericTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
2394 {
2395 /* after all changes have been done to the tree control,
2396 * we actually redraw the tree when everything is over */
2397
2398 if (!m_dirty)
2399 return;
2400
2401 m_dirty = FALSE;
2402
2403 CalculatePositions();
2404 Refresh();
2405 AdjustMyScrollbars();
2406 }
2407
2408 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
2409 {
2410 wxCoord text_w = 0;
2411 wxCoord text_h = 0;
2412
2413 if (item->IsBold())
2414 dc.SetFont(m_boldFont);
2415
2416 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
2417 text_h+=2;
2418
2419 // restore normal font
2420 dc.SetFont( m_normalFont );
2421
2422 int image_h = 0;
2423 int image_w = 0;
2424 int image = item->GetCurrentImage();
2425 if ( image != NO_IMAGE )
2426 {
2427 if ( m_imageListNormal )
2428 {
2429 m_imageListNormal->GetSize( image, image_w, image_h );
2430 image_w += 4;
2431 }
2432 }
2433
2434 int total_h = (image_h > text_h) ? image_h : text_h;
2435
2436 if (total_h < 40)
2437 total_h += 2; // at least 2 pixels
2438 else
2439 total_h += total_h/10; // otherwise 10% extra spacing
2440
2441 item->SetHeight(total_h);
2442 if (total_h>m_lineHeight)
2443 m_lineHeight=total_h;
2444
2445 item->SetWidth(image_w+text_w+2);
2446 }
2447
2448 // -----------------------------------------------------------------------------
2449 // for developper : y is now the top of the level
2450 // not the middle of it !
2451 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
2452 {
2453 int horizX = level*m_indent;
2454
2455 CalculateSize( item, dc );
2456
2457 // set its position
2458 item->SetX( horizX+m_indent+m_spacing );
2459 item->SetY( y );
2460 y+=GetLineHeight(item);
2461
2462 if ( !item->IsExpanded() )
2463 {
2464 // we dont need to calculate collapsed branches
2465 return;
2466 }
2467
2468 wxArrayGenericTreeItems& children = item->GetChildren();
2469 size_t n, count = children.Count();
2470 for (n = 0; n < count; ++n )
2471 CalculateLevel( children[n], dc, level+1, y ); // recurse
2472 }
2473
2474 void wxGenericTreeCtrl::CalculatePositions()
2475 {
2476 if ( !m_anchor ) return;
2477
2478 wxClientDC dc(this);
2479 PrepareDC( dc );
2480
2481 dc.SetFont( m_normalFont );
2482
2483 dc.SetPen( m_dottedPen );
2484 //if(GetImageList() == NULL)
2485 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2486
2487 int y = 2;
2488 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
2489 }
2490
2491 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
2492 {
2493 if (m_dirty) return;
2494
2495 wxClientDC dc(this);
2496 PrepareDC(dc);
2497
2498 int cw = 0;
2499 int ch = 0;
2500 GetClientSize( &cw, &ch );
2501
2502 wxRect rect;
2503 rect.x = dc.LogicalToDeviceX( 0 );
2504 rect.width = cw;
2505 rect.y = dc.LogicalToDeviceY( item->GetY() );
2506 rect.height = ch;
2507
2508 Refresh( TRUE, &rect );
2509
2510 AdjustMyScrollbars();
2511 }
2512
2513 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
2514 {
2515 if (m_dirty) return;
2516
2517 wxClientDC dc(this);
2518 PrepareDC( dc );
2519
2520 int cw = 0;
2521 int ch = 0;
2522 GetClientSize( &cw, &ch );
2523
2524 wxRect rect;
2525 rect.x = dc.LogicalToDeviceX( 0 );
2526 rect.y = dc.LogicalToDeviceY( item->GetY() );
2527 rect.width = cw;
2528 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
2529
2530 Refresh( TRUE, &rect );
2531 }
2532