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