]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treectlg.cpp
Some more printing code to clear most of
[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 and Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // =============================================================================
13 // declarations
14 // =============================================================================
15
16 // -----------------------------------------------------------------------------
17 // headers
18 // -----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "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/treebase.h"
34 #include "wx/generic/treectlg.h"
35 #include "wx/timer.h"
36 #include "wx/textctrl.h"
37 #include "wx/imaglist.h"
38 #include "wx/settings.h"
39 #include "wx/dcclient.h"
40
41 #include "wx/renderer.h"
42
43 #ifdef __WXMAC__
44 #include "wx/mac/private.h"
45 #endif
46
47 // -----------------------------------------------------------------------------
48 // array types
49 // -----------------------------------------------------------------------------
50
51 class WXDLLEXPORT wxGenericTreeItem;
52
53 WX_DEFINE_EXPORTED_ARRAY_PTR(wxGenericTreeItem *, wxArrayGenericTreeItems);
54
55 // ----------------------------------------------------------------------------
56 // constants
57 // ----------------------------------------------------------------------------
58
59 static const int NO_IMAGE = -1;
60
61 static const int PIXELS_PER_UNIT = 10;
62
63 // -----------------------------------------------------------------------------
64 // private classes
65 // -----------------------------------------------------------------------------
66
67 // timer used for enabling in-place edit
68 class WXDLLEXPORT wxTreeRenameTimer: public wxTimer
69 {
70 public:
71 // start editing the current item after half a second (if the mouse hasn't
72 // been clicked/moved)
73 enum { DELAY = 500 };
74
75 wxTreeRenameTimer( wxGenericTreeCtrl *owner );
76
77 virtual void Notify();
78
79 private:
80 wxGenericTreeCtrl *m_owner;
81
82 DECLARE_NO_COPY_CLASS(wxTreeRenameTimer)
83 };
84
85 // control used for in-place edit
86 class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl
87 {
88 public:
89 wxTreeTextCtrl(wxGenericTreeCtrl *owner, wxGenericTreeItem *item);
90
91 // wxGenericTreeCtrl can use this one to abandon editing the given item,
92 // it's not an error to call it if this item is not being edited
93 void StopEditing(wxGenericTreeItem *item)
94 {
95 if ( item == m_itemEdited )
96 DoStopEditing();
97 }
98
99 protected:
100 void OnChar( wxKeyEvent &event );
101 void OnKeyUp( wxKeyEvent &event );
102 void OnKillFocus( wxFocusEvent &event );
103
104 void DoStopEditing()
105 {
106 Finish();
107 m_owner->OnRenameCancelled(m_itemEdited);
108 }
109
110 bool AcceptChanges();
111 void Finish();
112
113 private:
114 wxGenericTreeCtrl *m_owner;
115 wxGenericTreeItem *m_itemEdited;
116 wxString m_startValue;
117 bool m_finished;
118
119 DECLARE_EVENT_TABLE()
120 DECLARE_NO_COPY_CLASS(wxTreeTextCtrl)
121 };
122
123 // timer used to clear wxGenericTreeCtrl::m_findPrefix if no key was pressed
124 // for a sufficiently long time
125 class WXDLLEXPORT wxTreeFindTimer : public wxTimer
126 {
127 public:
128 // reset the current prefix after half a second of inactivity
129 enum { DELAY = 500 };
130
131 wxTreeFindTimer( wxGenericTreeCtrl *owner ) { m_owner = owner; }
132
133 virtual void Notify() { m_owner->m_findPrefix.clear(); }
134
135 private:
136 wxGenericTreeCtrl *m_owner;
137
138 DECLARE_NO_COPY_CLASS(wxTreeFindTimer)
139 };
140
141 // a tree item
142 class WXDLLEXPORT wxGenericTreeItem
143 {
144 public:
145 // ctors & dtor
146 wxGenericTreeItem() { m_data = NULL; }
147 wxGenericTreeItem( wxGenericTreeItem *parent,
148 const wxString& text,
149 int image,
150 int selImage,
151 wxTreeItemData *data );
152
153 ~wxGenericTreeItem();
154
155 // trivial accessors
156 wxArrayGenericTreeItems& GetChildren() { return m_children; }
157
158 const wxString& GetText() const { return m_text; }
159 int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const
160 { return m_images[which]; }
161 wxTreeItemData *GetData() const { return m_data; }
162
163 // returns the current image for the item (depending on its
164 // selected/expanded/whatever state)
165 int GetCurrentImage() const;
166
167 void SetText( const wxString &text );
168 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
169 void SetData(wxTreeItemData *data) { m_data = data; }
170
171 void SetHasPlus(bool has = true) { m_hasPlus = has; }
172
173 void SetBold(bool bold) { m_isBold = bold; }
174
175 int GetX() const { return m_x; }
176 int GetY() const { return m_y; }
177
178 void SetX(int x) { m_x = x; }
179 void SetY(int y) { m_y = y; }
180
181 int GetHeight() const { return m_height; }
182 int GetWidth() const { return m_width; }
183
184 void SetHeight(int h) { m_height = h; }
185 void SetWidth(int w) { m_width = w; }
186
187 wxGenericTreeItem *GetParent() const { return m_parent; }
188
189 // operations
190 // deletes all children notifying the treectrl about it if !NULL
191 // pointer given
192 void DeleteChildren(wxGenericTreeCtrl *tree = NULL);
193
194 // get count of all children (and grand children if 'recursively')
195 size_t GetChildrenCount(bool recursively = true) const;
196
197 void Insert(wxGenericTreeItem *child, size_t index)
198 { m_children.Insert(child, index); }
199
200 void GetSize( int &x, int &y, const wxGenericTreeCtrl* );
201
202 // return the item at given position (or NULL if no item), onButton is
203 // true if the point belongs to the item's button, otherwise it lies
204 // on the item's label
205 wxGenericTreeItem *HitTest( const wxPoint& point,
206 const wxGenericTreeCtrl *,
207 int &flags,
208 int level );
209
210 void Expand() { m_isCollapsed = false; }
211 void Collapse() { m_isCollapsed = true; }
212
213 void SetHilight( bool set = true ) { m_hasHilight = set; }
214
215 // status inquiries
216 bool HasChildren() const { return !m_children.IsEmpty(); }
217 bool IsSelected() const { return m_hasHilight != 0; }
218 bool IsExpanded() const { return !m_isCollapsed; }
219 bool HasPlus() const { return m_hasPlus || HasChildren(); }
220 bool IsBold() const { return m_isBold != 0; }
221
222 // attributes
223 // get them - may be NULL
224 wxTreeItemAttr *GetAttributes() const { return m_attr; }
225 // get them ensuring that the pointer is not NULL
226 wxTreeItemAttr& Attr()
227 {
228 if ( !m_attr )
229 {
230 m_attr = new wxTreeItemAttr;
231 m_ownsAttr = true;
232 }
233 return *m_attr;
234 }
235 // set them
236 void SetAttributes(wxTreeItemAttr *attr)
237 {
238 if ( m_ownsAttr ) delete m_attr;
239 m_attr = attr;
240 m_ownsAttr = false;
241 }
242 // set them and delete when done
243 void AssignAttributes(wxTreeItemAttr *attr)
244 {
245 SetAttributes(attr);
246 m_ownsAttr = true;
247 }
248
249 private:
250 // since there can be very many of these, we save size by chosing
251 // the smallest representation for the elements and by ordering
252 // the members to avoid padding.
253 wxString m_text; // label to be rendered for item
254
255 wxTreeItemData *m_data; // user-provided data
256
257 wxArrayGenericTreeItems m_children; // list of children
258 wxGenericTreeItem *m_parent; // parent of this item
259
260 wxTreeItemAttr *m_attr; // attributes???
261
262 // tree ctrl images for the normal, selected, expanded and
263 // expanded+selected states
264 int m_images[wxTreeItemIcon_Max];
265
266 wxCoord m_x; // (virtual) offset from top
267 wxCoord m_y; // (virtual) offset from left
268 int m_width; // width of this item
269 int m_height; // height of this item
270
271 // use bitfields to save size
272 int m_isCollapsed :1;
273 int m_hasHilight :1; // same as focused
274 int m_hasPlus :1; // used for item which doesn't have
275 // children but has a [+] button
276 int m_isBold :1; // render the label in bold font
277 int m_ownsAttr :1; // delete attribute when done
278
279 DECLARE_NO_COPY_CLASS(wxGenericTreeItem)
280 };
281
282 // =============================================================================
283 // implementation
284 // =============================================================================
285
286 // ----------------------------------------------------------------------------
287 // private functions
288 // ----------------------------------------------------------------------------
289
290 // translate the key or mouse event flags to the type of selection we're
291 // dealing with
292 static void EventFlagsToSelType(long style,
293 bool shiftDown,
294 bool ctrlDown,
295 bool &is_multiple,
296 bool &extended_select,
297 bool &unselect_others)
298 {
299 is_multiple = (style & wxTR_MULTIPLE) != 0;
300 extended_select = shiftDown && is_multiple;
301 unselect_others = !(extended_select || (ctrlDown && is_multiple));
302 }
303
304 // check if the given item is under another one
305 static bool IsDescendantOf(wxGenericTreeItem *parent, wxGenericTreeItem *item)
306 {
307 while ( item )
308 {
309 if ( item == parent )
310 {
311 // item is a descendant of parent
312 return true;
313 }
314
315 item = item->GetParent();
316 }
317
318 return false;
319 }
320
321 // -----------------------------------------------------------------------------
322 // wxTreeRenameTimer (internal)
323 // -----------------------------------------------------------------------------
324
325 wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl *owner )
326 {
327 m_owner = owner;
328 }
329
330 void wxTreeRenameTimer::Notify()
331 {
332 m_owner->OnRenameTimer();
333 }
334
335 //-----------------------------------------------------------------------------
336 // wxTreeTextCtrl (internal)
337 //-----------------------------------------------------------------------------
338
339 BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
340 EVT_CHAR (wxTreeTextCtrl::OnChar)
341 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp)
342 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
343 END_EVENT_TABLE()
344
345 wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl *owner,
346 wxGenericTreeItem *item)
347 : m_itemEdited(item), m_startValue(item->GetText())
348 {
349 m_owner = owner;
350 m_finished = false;
351
352 int w = m_itemEdited->GetWidth(),
353 h = m_itemEdited->GetHeight();
354
355 int x, y;
356 m_owner->CalcScrolledPosition(item->GetX(), item->GetY(), &x, &y);
357
358 int image_h = 0,
359 image_w = 0;
360
361 int image = item->GetCurrentImage();
362 if ( image != NO_IMAGE )
363 {
364 if ( m_owner->m_imageListNormal )
365 {
366 m_owner->m_imageListNormal->GetSize( image, image_w, image_h );
367 image_w += 4;
368 }
369 else
370 {
371 wxFAIL_MSG(_T("you must create an image list to use images!"));
372 }
373 }
374
375 // FIXME: what are all these hardcoded 4, 8 and 11s really?
376 x += image_w;
377 w -= image_w + 4;
378 #ifdef __WXMAC__
379 wxSize bs = DoGetBestSize() ;
380 // edit control height
381 if ( h > bs.y - 8 )
382 {
383 int diff = h - ( bs.y - 8 ) ;
384 h -= diff ;
385 y += diff / 2 ;
386 }
387 #endif
388
389 (void)Create(m_owner, wxID_ANY, m_startValue,
390 wxPoint(x - 4, y - 4), wxSize(w + 11, h + 8));
391 }
392
393 bool wxTreeTextCtrl::AcceptChanges()
394 {
395 const wxString value = GetValue();
396
397 if ( value == m_startValue )
398 {
399 // nothing changed, always accept
400 return true;
401 }
402
403 if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
404 {
405 // vetoed by the user
406 return false;
407 }
408
409 // accepted, do rename the item
410 m_owner->SetItemText(m_itemEdited, value);
411
412 return true;
413 }
414
415 void wxTreeTextCtrl::Finish()
416 {
417 if ( !m_finished )
418 {
419 m_owner->ResetTextControl();
420
421 wxPendingDelete.Append(this);
422
423 m_finished = true;
424
425 m_owner->SetFocus(); // This doesn't work. TODO.
426 }
427 }
428
429 void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
430 {
431 switch ( event.m_keyCode )
432 {
433 case WXK_RETURN:
434 if ( AcceptChanges() )
435 {
436 // Close the text control, changes were accepted
437 Finish();
438 }
439 // else do nothing, do not accept and do not close
440 break;
441
442 case WXK_ESCAPE:
443 DoStopEditing();
444 break;
445
446 default:
447 event.Skip();
448 }
449 }
450
451 void wxTreeTextCtrl::OnKeyUp( wxKeyEvent &event )
452 {
453 if ( !m_finished )
454 {
455 // auto-grow the textctrl:
456 wxSize parentSize = m_owner->GetSize();
457 wxPoint myPos = GetPosition();
458 wxSize mySize = GetSize();
459 int sx, sy;
460 GetTextExtent(GetValue() + _T("M"), &sx, &sy);
461 if (myPos.x + sx > parentSize.x)
462 sx = parentSize.x - myPos.x;
463 if (mySize.x > sx)
464 sx = mySize.x;
465 SetSize(sx, wxDefaultCoord);
466 }
467
468 event.Skip();
469 }
470
471 void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &event )
472 {
473 if ( !m_finished )
474 {
475 AcceptChanges();
476 // We must finish regardless of success, otherwise we'll get
477 // focus problems:
478 Finish();
479 }
480
481 // We must let the native text control handle focus, too, otherwise
482 // it could have problems with the cursor (e.g., in wxGTK):
483 event.Skip();
484 }
485
486 // -----------------------------------------------------------------------------
487 // wxGenericTreeItem
488 // -----------------------------------------------------------------------------
489
490 wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
491 const wxString& text,
492 int image, int selImage,
493 wxTreeItemData *data)
494 : m_text(text)
495 {
496 m_images[wxTreeItemIcon_Normal] = image;
497 m_images[wxTreeItemIcon_Selected] = selImage;
498 m_images[wxTreeItemIcon_Expanded] = NO_IMAGE;
499 m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE;
500
501 m_data = data;
502 m_x = m_y = 0;
503
504 m_isCollapsed = true;
505 m_hasHilight = false;
506 m_hasPlus = false;
507 m_isBold = false;
508
509 m_parent = parent;
510
511 m_attr = (wxTreeItemAttr *)NULL;
512 m_ownsAttr = false;
513
514 // We don't know the height here yet.
515 m_width = 0;
516 m_height = 0;
517 }
518
519 wxGenericTreeItem::~wxGenericTreeItem()
520 {
521 delete m_data;
522
523 if (m_ownsAttr) delete m_attr;
524
525 wxASSERT_MSG( m_children.IsEmpty(),
526 wxT("please call DeleteChildren() before deleting the item") );
527 }
528
529 void wxGenericTreeItem::DeleteChildren(wxGenericTreeCtrl *tree)
530 {
531 size_t count = m_children.Count();
532 for ( size_t n = 0; n < count; n++ )
533 {
534 wxGenericTreeItem *child = m_children[n];
535 if (tree)
536 tree->SendDeleteEvent(child);
537
538 child->DeleteChildren(tree);
539 delete child;
540 }
541
542 m_children.Empty();
543 }
544
545 void wxGenericTreeItem::SetText( const wxString &text )
546 {
547 m_text = text;
548 }
549
550 size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
551 {
552 size_t count = m_children.Count();
553 if ( !recursively )
554 return count;
555
556 size_t total = count;
557 for (size_t n = 0; n < count; ++n)
558 {
559 total += m_children[n]->GetChildrenCount();
560 }
561
562 return total;
563 }
564
565 void wxGenericTreeItem::GetSize( int &x, int &y,
566 const wxGenericTreeCtrl *theButton )
567 {
568 int bottomY=m_y+theButton->GetLineHeight(this);
569 if ( y < bottomY ) y = bottomY;
570 int width = m_x + m_width;
571 if ( x < width ) x = width;
572
573 if (IsExpanded())
574 {
575 size_t count = m_children.Count();
576 for ( size_t n = 0; n < count; ++n )
577 {
578 m_children[n]->GetSize( x, y, theButton );
579 }
580 }
581 }
582
583 wxGenericTreeItem *wxGenericTreeItem::HitTest(const wxPoint& point,
584 const wxGenericTreeCtrl *theCtrl,
585 int &flags,
586 int level)
587 {
588 // for a hidden root node, don't evaluate it, but do evaluate children
589 if ( !(level == 0 && theCtrl->HasFlag(wxTR_HIDE_ROOT)) )
590 {
591 // evaluate the item
592 int h = theCtrl->GetLineHeight(this);
593 if ((point.y > m_y) && (point.y < m_y + h))
594 {
595 int y_mid = m_y + h/2;
596 if (point.y < y_mid )
597 flags |= wxTREE_HITTEST_ONITEMUPPERPART;
598 else
599 flags |= wxTREE_HITTEST_ONITEMLOWERPART;
600
601 int xCross = m_x - theCtrl->GetSpacing();
602 #ifdef __WXMAC__
603 // according to the drawing code the triangels are drawn
604 // at -4 , -4 from the position up to +10/+10 max
605 if ((point.x > xCross-4) && (point.x < xCross+10) &&
606 (point.y > y_mid-4) && (point.y < y_mid+10) &&
607 HasPlus() && theCtrl->HasButtons() )
608 #else
609 // 5 is the size of the plus sign
610 if ((point.x > xCross-6) && (point.x < xCross+6) &&
611 (point.y > y_mid-6) && (point.y < y_mid+6) &&
612 HasPlus() && theCtrl->HasButtons() )
613 #endif
614 {
615 flags |= wxTREE_HITTEST_ONITEMBUTTON;
616 return this;
617 }
618
619 if ((point.x >= m_x) && (point.x <= m_x+m_width))
620 {
621 int image_w = -1;
622 int image_h;
623
624 // assuming every image (normal and selected) has the same size!
625 if ( (GetImage() != NO_IMAGE) && theCtrl->m_imageListNormal )
626 theCtrl->m_imageListNormal->GetSize(GetImage(),
627 image_w, image_h);
628
629 if ((image_w != -1) && (point.x <= m_x + image_w + 1))
630 flags |= wxTREE_HITTEST_ONITEMICON;
631 else
632 flags |= wxTREE_HITTEST_ONITEMLABEL;
633
634 return this;
635 }
636
637 if (point.x < m_x)
638 flags |= wxTREE_HITTEST_ONITEMINDENT;
639 if (point.x > m_x+m_width)
640 flags |= wxTREE_HITTEST_ONITEMRIGHT;
641
642 return this;
643 }
644
645 // if children are expanded, fall through to evaluate them
646 if (m_isCollapsed) return (wxGenericTreeItem*) NULL;
647 }
648
649 // evaluate children
650 size_t count = m_children.Count();
651 for ( size_t n = 0; n < count; n++ )
652 {
653 wxGenericTreeItem *res = m_children[n]->HitTest( point,
654 theCtrl,
655 flags,
656 level + 1 );
657 if ( res != NULL )
658 return res;
659 }
660
661 return (wxGenericTreeItem*) NULL;
662 }
663
664 int wxGenericTreeItem::GetCurrentImage() const
665 {
666 int image = NO_IMAGE;
667 if ( IsExpanded() )
668 {
669 if ( IsSelected() )
670 {
671 image = GetImage(wxTreeItemIcon_SelectedExpanded);
672 }
673
674 if ( image == NO_IMAGE )
675 {
676 // we usually fall back to the normal item, but try just the
677 // expanded one (and not selected) first in this case
678 image = GetImage(wxTreeItemIcon_Expanded);
679 }
680 }
681 else // not expanded
682 {
683 if ( IsSelected() )
684 image = GetImage(wxTreeItemIcon_Selected);
685 }
686
687 // maybe it doesn't have the specific image we want,
688 // try the default one instead
689 if ( image == NO_IMAGE ) image = GetImage();
690
691 return image;
692 }
693
694 // -----------------------------------------------------------------------------
695 // wxGenericTreeCtrl implementation
696 // -----------------------------------------------------------------------------
697
698 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl, wxScrolledWindow)
699
700 BEGIN_EVENT_TABLE(wxGenericTreeCtrl,wxScrolledWindow)
701 EVT_PAINT (wxGenericTreeCtrl::OnPaint)
702 EVT_MOUSE_EVENTS (wxGenericTreeCtrl::OnMouse)
703 EVT_CHAR (wxGenericTreeCtrl::OnChar)
704 EVT_SET_FOCUS (wxGenericTreeCtrl::OnSetFocus)
705 EVT_KILL_FOCUS (wxGenericTreeCtrl::OnKillFocus)
706 EVT_TREE_ITEM_GETTOOLTIP(wxID_ANY, wxGenericTreeCtrl::OnGetToolTip)
707 END_EVENT_TABLE()
708
709 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
710 /*
711 * wxTreeCtrl has to be a real class or we have problems with
712 * the run-time information.
713 */
714
715 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxGenericTreeCtrl)
716 #endif
717
718 // -----------------------------------------------------------------------------
719 // construction/destruction
720 // -----------------------------------------------------------------------------
721
722 void wxGenericTreeCtrl::Init()
723 {
724 m_current = m_key_current = m_anchor = m_select_me = (wxGenericTreeItem *) NULL;
725 m_hasFocus = false;
726 m_dirty = false;
727
728 m_lineHeight = 10;
729 m_indent = 15;
730 m_spacing = 18;
731
732 m_hilightBrush = new wxBrush
733 (
734 wxSystemSettings::GetColour
735 (
736 wxSYS_COLOUR_HIGHLIGHT
737 ),
738 wxSOLID
739 );
740
741 m_hilightUnfocusedBrush = new wxBrush
742 (
743 wxSystemSettings::GetColour
744 (
745 wxSYS_COLOUR_BTNSHADOW
746 ),
747 wxSOLID
748 );
749
750 m_imageListNormal = m_imageListButtons =
751 m_imageListState = (wxImageList *) NULL;
752 m_ownsImageListNormal = m_ownsImageListButtons =
753 m_ownsImageListState = false;
754
755 m_dragCount = 0;
756 m_isDragging = false;
757 m_dropTarget = m_oldSelection = NULL;
758 m_underMouse = NULL;
759 m_textCtrl = NULL;
760
761 m_renameTimer = NULL;
762 m_freezeCount = 0;
763
764 m_findTimer = NULL;
765
766 m_lastOnSame = false;
767
768 #ifdef __WXMAC_CARBON__
769 m_normalFont.MacCreateThemeFont( kThemeViewsFont ) ;
770 #else
771 m_normalFont = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
772 #endif
773 m_boldFont = wxFont(m_normalFont.GetPointSize(),
774 m_normalFont.GetFamily(),
775 m_normalFont.GetStyle(),
776 wxBOLD,
777 m_normalFont.GetUnderlined(),
778 m_normalFont.GetFaceName(),
779 m_normalFont.GetEncoding());
780 }
781
782 bool wxGenericTreeCtrl::Create(wxWindow *parent,
783 wxWindowID id,
784 const wxPoint& pos,
785 const wxSize& size,
786 long style,
787 const wxValidator& wxVALIDATOR_PARAM(validator),
788 const wxString& name )
789 {
790 #ifdef __WXMAC__
791 int major,minor;
792 wxGetOsVersion( &major, &minor );
793
794 style &= ~wxTR_LINES_AT_ROOT;
795 style |= wxTR_NO_LINES;
796 if (major < 10)
797 style |= wxTR_ROW_LINES;
798 #endif // __WXMAC__
799
800 wxScrolledWindow::Create( parent, id, pos, size,
801 style|wxHSCROLL|wxVSCROLL, name );
802
803 // If the tree display has no buttons, but does have
804 // connecting lines, we can use a narrower layout.
805 // It may not be a good idea to force this...
806 if (!HasButtons() && !HasFlag(wxTR_NO_LINES))
807 {
808 m_indent= 10;
809 m_spacing = 10;
810 }
811
812 #if wxUSE_VALIDATORS
813 SetValidator( validator );
814 #endif
815
816 wxVisualAttributes attr = GetDefaultAttributes();
817 SetOwnForegroundColour( attr.colFg );
818 SetOwnBackgroundColour( attr.colBg );
819 if (!m_hasFont)
820 SetOwnFont(attr.font);
821
822 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
823 m_dottedPen = wxPen( wxT("grey"), 0, 0 );
824
825 SetBestSize(size);
826
827 return true;
828 }
829
830 wxGenericTreeCtrl::~wxGenericTreeCtrl()
831 {
832 delete m_hilightBrush;
833 delete m_hilightUnfocusedBrush;
834
835 DeleteAllItems();
836
837 delete m_renameTimer;
838 delete m_findTimer;
839
840 if (m_ownsImageListNormal)
841 delete m_imageListNormal;
842 if (m_ownsImageListState)
843 delete m_imageListState;
844 if (m_ownsImageListButtons)
845 delete m_imageListButtons;
846 }
847
848 // -----------------------------------------------------------------------------
849 // accessors
850 // -----------------------------------------------------------------------------
851
852 size_t wxGenericTreeCtrl::GetCount() const
853 {
854 if ( !m_anchor )
855 {
856 // the tree is empty
857 return 0;
858 }
859
860 size_t count = m_anchor->GetChildrenCount();
861 if ( !HasFlag(wxTR_HIDE_ROOT) )
862 {
863 // take the root itself into account
864 count++;
865 }
866
867 return count;
868 }
869
870 void wxGenericTreeCtrl::SetIndent(unsigned int indent)
871 {
872 m_indent = (unsigned short) indent;
873 m_dirty = true;
874 }
875
876 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing)
877 {
878 m_spacing = (unsigned short) spacing;
879 m_dirty = true;
880 }
881
882 size_t
883 wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
884 bool recursively) const
885 {
886 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
887
888 return ((wxGenericTreeItem*) item.m_pItem)->GetChildrenCount(recursively);
889 }
890
891 void wxGenericTreeCtrl::SetWindowStyle(const long styles)
892 {
893 if (!HasFlag(wxTR_HIDE_ROOT) && (styles & wxTR_HIDE_ROOT))
894 {
895 // if we will hide the root, make sure children are visible
896 m_anchor->SetHasPlus();
897 m_anchor->Expand();
898 CalculatePositions();
899 }
900
901 // right now, just sets the styles. Eventually, we may
902 // want to update the inherited styles, but right now
903 // none of the parents has updatable styles
904 m_windowStyle = styles;
905 m_dirty = true;
906 }
907
908 // -----------------------------------------------------------------------------
909 // functions to work with tree items
910 // -----------------------------------------------------------------------------
911
912 wxString wxGenericTreeCtrl::GetItemText(const wxTreeItemId& item) const
913 {
914 wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
915
916 return ((wxGenericTreeItem*) item.m_pItem)->GetText();
917 }
918
919 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId& item,
920 wxTreeItemIcon which) const
921 {
922 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
923
924 return ((wxGenericTreeItem*) item.m_pItem)->GetImage(which);
925 }
926
927 wxTreeItemData *wxGenericTreeCtrl::GetItemData(const wxTreeItemId& item) const
928 {
929 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
930
931 return ((wxGenericTreeItem*) item.m_pItem)->GetData();
932 }
933
934 wxColour wxGenericTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
935 {
936 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
937
938 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
939 return pItem->Attr().GetTextColour();
940 }
941
942 wxColour wxGenericTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const
943 {
944 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
945
946 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
947 return pItem->Attr().GetBackgroundColour();
948 }
949
950 wxFont wxGenericTreeCtrl::GetItemFont(const wxTreeItemId& item) const
951 {
952 wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") );
953
954 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
955 return pItem->Attr().GetFont();
956 }
957
958 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
959 {
960 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
961
962 wxClientDC dc(this);
963 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
964 pItem->SetText(text);
965 CalculateSize(pItem, dc);
966 RefreshLine(pItem);
967 }
968
969 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId& item,
970 int image,
971 wxTreeItemIcon which)
972 {
973 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
974
975 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
976 pItem->SetImage(image, which);
977
978 wxClientDC dc(this);
979 CalculateSize(pItem, dc);
980 RefreshLine(pItem);
981 }
982
983 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
984 {
985 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
986
987 if (data)
988 data->SetId( item );
989
990 ((wxGenericTreeItem*) item.m_pItem)->SetData(data);
991 }
992
993 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
994 {
995 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
996
997 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
998 pItem->SetHasPlus(has);
999 RefreshLine(pItem);
1000 }
1001
1002 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
1003 {
1004 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1005
1006 // avoid redrawing the tree if no real change
1007 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1008 if ( pItem->IsBold() != bold )
1009 {
1010 pItem->SetBold(bold);
1011 RefreshLine(pItem);
1012 }
1013 }
1014
1015 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
1016 const wxColour& col)
1017 {
1018 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1019
1020 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1021 pItem->Attr().SetTextColour(col);
1022 RefreshLine(pItem);
1023 }
1024
1025 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
1026 const wxColour& col)
1027 {
1028 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1029
1030 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1031 pItem->Attr().SetBackgroundColour(col);
1032 RefreshLine(pItem);
1033 }
1034
1035 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
1036 {
1037 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1038
1039 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1040 pItem->Attr().SetFont(font);
1041 RefreshLine(pItem);
1042 }
1043
1044 bool wxGenericTreeCtrl::SetFont( const wxFont &font )
1045 {
1046 wxScrolledWindow::SetFont(font);
1047
1048 m_normalFont = font ;
1049 m_boldFont = wxFont(m_normalFont.GetPointSize(),
1050 m_normalFont.GetFamily(),
1051 m_normalFont.GetStyle(),
1052 wxBOLD,
1053 m_normalFont.GetUnderlined(),
1054 m_normalFont.GetFaceName(),
1055 m_normalFont.GetEncoding());
1056
1057 return true;
1058 }
1059
1060
1061 // -----------------------------------------------------------------------------
1062 // item status inquiries
1063 // -----------------------------------------------------------------------------
1064
1065 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId& item) const
1066 {
1067 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1068
1069 // An item is only visible if it's not a descendant of a collapsed item
1070 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
1071 wxGenericTreeItem* parent = pItem->GetParent();
1072 while (parent)
1073 {
1074 if (!parent->IsExpanded())
1075 return false;
1076 parent = parent->GetParent();
1077 }
1078
1079 int startX, startY;
1080 GetViewStart(& startX, & startY);
1081
1082 wxSize clientSize = GetClientSize();
1083
1084 wxRect rect;
1085 if (!GetBoundingRect(item, rect))
1086 return false;
1087 if (rect.GetWidth() == 0 || rect.GetHeight() == 0)
1088 return false;
1089 if (rect.GetBottom() < 0 || rect.GetTop() > clientSize.y)
1090 return false;
1091 if (rect.GetRight() < 0 || rect.GetLeft() > clientSize.x)
1092 return false;
1093
1094 return true;
1095 }
1096
1097 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
1098 {
1099 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1100
1101 // consider that the item does have children if it has the "+" button: it
1102 // might not have them (if it had never been expanded yet) but then it
1103 // could have them as well and it's better to err on this side rather than
1104 // disabling some operations which are restricted to the items with
1105 // children for an item which does have them
1106 return ((wxGenericTreeItem*) item.m_pItem)->HasPlus();
1107 }
1108
1109 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId& item) const
1110 {
1111 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1112
1113 return ((wxGenericTreeItem*) item.m_pItem)->IsExpanded();
1114 }
1115
1116 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId& item) const
1117 {
1118 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1119
1120 return ((wxGenericTreeItem*) item.m_pItem)->IsSelected();
1121 }
1122
1123 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId& item) const
1124 {
1125 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1126
1127 return ((wxGenericTreeItem*) item.m_pItem)->IsBold();
1128 }
1129
1130 // -----------------------------------------------------------------------------
1131 // navigation
1132 // -----------------------------------------------------------------------------
1133
1134 wxTreeItemId wxGenericTreeCtrl::GetItemParent(const wxTreeItemId& item) const
1135 {
1136 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1137
1138 return ((wxGenericTreeItem*) item.m_pItem)->GetParent();
1139 }
1140
1141 wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1142 wxTreeItemIdValue& cookie) const
1143 {
1144 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1145
1146 cookie = 0;
1147 return GetNextChild(item, cookie);
1148 }
1149
1150 wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item,
1151 wxTreeItemIdValue& cookie) const
1152 {
1153 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1154
1155 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1156
1157 // it's ok to cast cookie to size_t, we never have indices big enough to
1158 // overflow "void *"
1159 size_t *pIndex = (size_t *)&cookie;
1160 if ( *pIndex < children.Count() )
1161 {
1162 return children.Item((*pIndex)++);
1163 }
1164 else
1165 {
1166 // there are no more of them
1167 return wxTreeItemId();
1168 }
1169 }
1170
1171 #if WXWIN_COMPATIBILITY_2_4
1172
1173 wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1174 long& cookie) const
1175 {
1176 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1177
1178 cookie = 0;
1179 return GetNextChild(item, cookie);
1180 }
1181
1182 wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item,
1183 long& cookie) const
1184 {
1185 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1186
1187 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1188 if ( (size_t)cookie < children.Count() )
1189 {
1190 return children.Item((size_t)cookie++);
1191 }
1192 else
1193 {
1194 // there are no more of them
1195 return wxTreeItemId();
1196 }
1197 }
1198
1199 #endif // WXWIN_COMPATIBILITY_2_4
1200
1201 wxTreeItemId wxGenericTreeCtrl::GetLastChild(const wxTreeItemId& item) const
1202 {
1203 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1204
1205 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
1206 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
1207 }
1208
1209 wxTreeItemId wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
1210 {
1211 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1212
1213 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1214 wxGenericTreeItem *parent = i->GetParent();
1215 if ( parent == NULL )
1216 {
1217 // root item doesn't have any siblings
1218 return wxTreeItemId();
1219 }
1220
1221 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1222 int index = siblings.Index(i);
1223 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1224
1225 size_t n = (size_t)(index + 1);
1226 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
1227 }
1228
1229 wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
1230 {
1231 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1232
1233 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1234 wxGenericTreeItem *parent = i->GetParent();
1235 if ( parent == NULL )
1236 {
1237 // root item doesn't have any siblings
1238 return wxTreeItemId();
1239 }
1240
1241 wxArrayGenericTreeItems& siblings = parent->GetChildren();
1242 int index = siblings.Index(i);
1243 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1244
1245 return index == 0 ? wxTreeItemId()
1246 : wxTreeItemId(siblings[(size_t)(index - 1)]);
1247 }
1248
1249 // Only for internal use right now, but should probably be public
1250 wxTreeItemId wxGenericTreeCtrl::GetNext(const wxTreeItemId& item) const
1251 {
1252 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1253
1254 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1255
1256 // First see if there are any children.
1257 wxArrayGenericTreeItems& children = i->GetChildren();
1258 if (children.GetCount() > 0)
1259 {
1260 return children.Item(0);
1261 }
1262 else
1263 {
1264 // Try a sibling of this or ancestor instead
1265 wxTreeItemId p = item;
1266 wxTreeItemId toFind;
1267 do
1268 {
1269 toFind = GetNextSibling(p);
1270 p = GetItemParent(p);
1271 } while (p.IsOk() && !toFind.IsOk());
1272 return toFind;
1273 }
1274 }
1275
1276 wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const
1277 {
1278 wxTreeItemId id = GetRootItem();
1279 if (!id.IsOk())
1280 return id;
1281
1282 do
1283 {
1284 if (IsVisible(id))
1285 return id;
1286 id = GetNext(id);
1287 } while (id.IsOk());
1288
1289 return wxTreeItemId();
1290 }
1291
1292 wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
1293 {
1294 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1295
1296 wxTreeItemId id = item;
1297 if (id.IsOk())
1298 {
1299 while (id = GetNext(id), id.IsOk())
1300 {
1301 if (IsVisible(id))
1302 return id;
1303 }
1304 }
1305 return wxTreeItemId();
1306 }
1307
1308 wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1309 {
1310 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1311
1312 wxFAIL_MSG(wxT("not implemented"));
1313
1314 return wxTreeItemId();
1315 }
1316
1317 // called by wxTextTreeCtrl when it marks itself for deletion
1318 void wxGenericTreeCtrl::ResetTextControl()
1319 {
1320 m_textCtrl = NULL;
1321 }
1322
1323 // find the first item starting with the given prefix after the given item
1324 wxTreeItemId wxGenericTreeCtrl::FindItem(const wxTreeItemId& idParent,
1325 const wxString& prefixOrig) const
1326 {
1327 // match is case insensitive as this is more convenient to the user: having
1328 // to press Shift-letter to go to the item starting with a capital letter
1329 // would be too bothersome
1330 wxString prefix = prefixOrig.Lower();
1331
1332 // determine the starting point: we shouldn't take the current item (this
1333 // allows to switch between two items starting with the same letter just by
1334 // pressing it) but we shouldn't jump to the next one if the user is
1335 // continuing to type as otherwise he might easily skip the item he wanted
1336 wxTreeItemId id = idParent;
1337 if ( prefix.length() == 1 )
1338 {
1339 id = GetNext(id);
1340 }
1341
1342 // look for the item starting with the given prefix after it
1343 while ( id.IsOk() && !GetItemText(id).Lower().StartsWith(prefix) )
1344 {
1345 id = GetNext(id);
1346 }
1347
1348 // if we haven't found anything...
1349 if ( !id.IsOk() )
1350 {
1351 // ... wrap to the beginning
1352 id = GetRootItem();
1353 if ( HasFlag(wxTR_HIDE_ROOT) )
1354 {
1355 // can't select virtual root
1356 id = GetNext(id);
1357 }
1358
1359 // and try all the items (stop when we get to the one we started from)
1360 while ( id != idParent && !GetItemText(id).Lower().StartsWith(prefix) )
1361 {
1362 id = GetNext(id);
1363 }
1364 }
1365
1366 return id;
1367 }
1368
1369 // -----------------------------------------------------------------------------
1370 // operations
1371 // -----------------------------------------------------------------------------
1372
1373 wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
1374 size_t previous,
1375 const wxString& text,
1376 int image, int selImage,
1377 wxTreeItemData *data)
1378 {
1379 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1380 if ( !parent )
1381 {
1382 // should we give a warning here?
1383 return AddRoot(text, image, selImage, data);
1384 }
1385
1386 m_dirty = true; // do this first so stuff below doesn't cause flicker
1387
1388 wxGenericTreeItem *item =
1389 new wxGenericTreeItem( parent, text, image, selImage, data );
1390
1391 if ( data != NULL )
1392 {
1393 data->m_pItem = item;
1394 }
1395
1396 parent->Insert( item, previous );
1397
1398 return item;
1399 }
1400
1401 wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
1402 int image, int selImage,
1403 wxTreeItemData *data)
1404 {
1405 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
1406
1407 m_dirty = true; // do this first so stuff below doesn't cause flicker
1408
1409 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text,
1410 image, selImage, data);
1411 if ( data != NULL )
1412 {
1413 data->m_pItem = m_anchor;
1414 }
1415
1416 if (HasFlag(wxTR_HIDE_ROOT))
1417 {
1418 // if root is hidden, make sure we can navigate
1419 // into children
1420 m_anchor->SetHasPlus();
1421 m_anchor->Expand();
1422 CalculatePositions();
1423 }
1424
1425 if (!HasFlag(wxTR_MULTIPLE))
1426 {
1427 m_current = m_key_current = m_anchor;
1428 m_current->SetHilight( true );
1429 }
1430
1431 return m_anchor;
1432 }
1433
1434 wxTreeItemId wxGenericTreeCtrl::PrependItem(const wxTreeItemId& parent,
1435 const wxString& text,
1436 int image, int selImage,
1437 wxTreeItemData *data)
1438 {
1439 return DoInsertItem(parent, 0u, text, image, selImage, data);
1440 }
1441
1442 wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
1443 const wxTreeItemId& idPrevious,
1444 const wxString& text,
1445 int image, int selImage,
1446 wxTreeItemData *data)
1447 {
1448 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1449 if ( !parent )
1450 {
1451 // should we give a warning here?
1452 return AddRoot(text, image, selImage, data);
1453 }
1454
1455 int index = -1;
1456 if (idPrevious.IsOk())
1457 {
1458 index = parent->GetChildren().Index((wxGenericTreeItem*) idPrevious.m_pItem);
1459 wxASSERT_MSG( index != wxNOT_FOUND,
1460 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1461 }
1462
1463 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1464 }
1465
1466 wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
1467 size_t before,
1468 const wxString& text,
1469 int image, int selImage,
1470 wxTreeItemData *data)
1471 {
1472 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1473 if ( !parent )
1474 {
1475 // should we give a warning here?
1476 return AddRoot(text, image, selImage, data);
1477 }
1478
1479 return DoInsertItem(parentId, before, text, image, selImage, data);
1480 }
1481
1482 wxTreeItemId wxGenericTreeCtrl::AppendItem(const wxTreeItemId& parentId,
1483 const wxString& text,
1484 int image, int selImage,
1485 wxTreeItemData *data)
1486 {
1487 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1488 if ( !parent )
1489 {
1490 // should we give a warning here?
1491 return AddRoot(text, image, selImage, data);
1492 }
1493
1494 return DoInsertItem( parent, parent->GetChildren().Count(), text,
1495 image, selImage, data);
1496 }
1497
1498 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
1499 {
1500 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
1501 event.m_item = item;
1502 event.SetEventObject( this );
1503 ProcessEvent( event );
1504 }
1505
1506 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
1507 {
1508 m_dirty = true; // do this first so stuff below doesn't cause flicker
1509
1510 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1511 item->DeleteChildren(this);
1512 }
1513
1514 void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
1515 {
1516 m_dirty = true; // do this first so stuff below doesn't cause flicker
1517
1518 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1519
1520 if ( m_textCtrl )
1521 {
1522 // can't delete the item being edited, cancel editing it first
1523 m_textCtrl->StopEditing(item);
1524 }
1525
1526 wxGenericTreeItem *parent = item->GetParent();
1527
1528 // don't keep stale pointers around!
1529 if ( IsDescendantOf(item, m_key_current) )
1530 {
1531 // Don't silently change the selection:
1532 // do it properly in idle time, so event
1533 // handlers get called.
1534
1535 // m_key_current = parent;
1536 m_key_current = NULL;
1537 }
1538
1539 // m_select_me records whether we need to select
1540 // a different item, in idle time.
1541 if ( m_select_me && IsDescendantOf(item, m_select_me) )
1542 {
1543 m_select_me = parent;
1544 }
1545
1546 if ( IsDescendantOf(item, m_current) )
1547 {
1548 // Don't silently change the selection:
1549 // do it properly in idle time, so event
1550 // handlers get called.
1551
1552 // m_current = parent;
1553 m_current = NULL;
1554 m_select_me = parent;
1555 }
1556
1557 // remove the item from the tree
1558 if ( parent )
1559 {
1560 parent->GetChildren().Remove( item ); // remove by value
1561 }
1562 else // deleting the root
1563 {
1564 // nothing will be left in the tree
1565 m_anchor = NULL;
1566 }
1567
1568 // and delete all of its children and the item itself now
1569 item->DeleteChildren(this);
1570 SendDeleteEvent(item);
1571 delete item;
1572 }
1573
1574 void wxGenericTreeCtrl::DeleteAllItems()
1575 {
1576 if ( m_anchor )
1577 {
1578 Delete(m_anchor);
1579 }
1580 }
1581
1582 void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId)
1583 {
1584 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1585
1586 wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") );
1587 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
1588 _T("can't expand hidden root") );
1589
1590 if ( !item->HasPlus() )
1591 return;
1592
1593 if ( item->IsExpanded() )
1594 return;
1595
1596 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
1597 event.m_item = item;
1598 event.SetEventObject( this );
1599
1600 if ( ProcessEvent( event ) && !event.IsAllowed() )
1601 {
1602 // cancelled by program
1603 return;
1604 }
1605
1606 item->Expand();
1607 CalculatePositions();
1608
1609 RefreshSubtree(item);
1610
1611 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1612 ProcessEvent( event );
1613 }
1614
1615 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId& item)
1616 {
1617 if ( !HasFlag(wxTR_HIDE_ROOT) || item != GetRootItem())
1618 {
1619 Expand(item);
1620 if ( !IsExpanded(item) )
1621 return;
1622 }
1623
1624 wxTreeItemIdValue cookie;
1625 wxTreeItemId child = GetFirstChild(item, cookie);
1626 while ( child.IsOk() )
1627 {
1628 ExpandAll(child);
1629
1630 child = GetNextChild(item, cookie);
1631 }
1632 }
1633
1634 void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
1635 {
1636 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
1637 _T("can't collapse hidden root") );
1638
1639 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1640
1641 if ( !item->IsExpanded() )
1642 return;
1643
1644 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
1645 event.m_item = item;
1646 event.SetEventObject( this );
1647 if ( ProcessEvent( event ) && !event.IsAllowed() )
1648 {
1649 // cancelled by program
1650 return;
1651 }
1652
1653 item->Collapse();
1654
1655 #if 0 // TODO why should items be collapsed recursively?
1656 wxArrayGenericTreeItems& children = item->GetChildren();
1657 size_t count = children.Count();
1658 for ( size_t n = 0; n < count; n++ )
1659 {
1660 Collapse(children[n]);
1661 }
1662 #endif
1663
1664 CalculatePositions();
1665
1666 RefreshSubtree(item);
1667
1668 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1669 ProcessEvent( event );
1670 }
1671
1672 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1673 {
1674 Collapse(item);
1675 DeleteChildren(item);
1676 }
1677
1678 void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId)
1679 {
1680 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1681
1682 if (item->IsExpanded())
1683 Collapse(itemId);
1684 else
1685 Expand(itemId);
1686 }
1687
1688 void wxGenericTreeCtrl::Unselect()
1689 {
1690 if (m_current)
1691 {
1692 m_current->SetHilight( false );
1693 RefreshLine( m_current );
1694
1695 m_current = NULL;
1696 m_select_me = NULL;
1697 }
1698 }
1699
1700 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
1701 {
1702 if (item->IsSelected())
1703 {
1704 item->SetHilight(false);
1705 RefreshLine(item);
1706 }
1707
1708 if (item->HasChildren())
1709 {
1710 wxArrayGenericTreeItems& children = item->GetChildren();
1711 size_t count = children.Count();
1712 for ( size_t n = 0; n < count; ++n )
1713 {
1714 UnselectAllChildren(children[n]);
1715 }
1716 }
1717 }
1718
1719 void wxGenericTreeCtrl::UnselectAll()
1720 {
1721 wxTreeItemId rootItem = GetRootItem();
1722
1723 // the tree might not have the root item at all
1724 if ( rootItem )
1725 {
1726 UnselectAllChildren((wxGenericTreeItem*) rootItem.m_pItem);
1727 }
1728 }
1729
1730 // Recursive function !
1731 // To stop we must have crt_item<last_item
1732 // Algorithm :
1733 // Tag all next children, when no more children,
1734 // Move to parent (not to tag)
1735 // Keep going... if we found last_item, we stop.
1736 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1737 {
1738 wxGenericTreeItem *parent = crt_item->GetParent();
1739
1740 if (parent == NULL) // This is root item
1741 return TagAllChildrenUntilLast(crt_item, last_item, select);
1742
1743 wxArrayGenericTreeItems& children = parent->GetChildren();
1744 int index = children.Index(crt_item);
1745 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1746
1747 size_t count = children.Count();
1748 for (size_t n=(size_t)(index+1); n<count; ++n)
1749 {
1750 if (TagAllChildrenUntilLast(children[n], last_item, select)) return true;
1751 }
1752
1753 return TagNextChildren(parent, last_item, select);
1754 }
1755
1756 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1757 {
1758 crt_item->SetHilight(select);
1759 RefreshLine(crt_item);
1760
1761 if (crt_item==last_item)
1762 return true;
1763
1764 if (crt_item->HasChildren())
1765 {
1766 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1767 size_t count = children.Count();
1768 for ( size_t n = 0; n < count; ++n )
1769 {
1770 if (TagAllChildrenUntilLast(children[n], last_item, select))
1771 return true;
1772 }
1773 }
1774
1775 return false;
1776 }
1777
1778 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
1779 {
1780 m_select_me = NULL;
1781
1782 // item2 is not necessary after item1
1783 // choice first' and 'last' between item1 and item2
1784 wxGenericTreeItem *first= (item1->GetY()<item2->GetY()) ? item1 : item2;
1785 wxGenericTreeItem *last = (item1->GetY()<item2->GetY()) ? item2 : item1;
1786
1787 bool select = m_current->IsSelected();
1788
1789 if ( TagAllChildrenUntilLast(first,last,select) )
1790 return;
1791
1792 TagNextChildren(first,last,select);
1793 }
1794
1795 void wxGenericTreeCtrl::DoSelectItem(const wxTreeItemId& itemId,
1796 bool unselect_others,
1797 bool extended_select)
1798 {
1799 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
1800
1801 m_select_me = NULL;
1802
1803 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
1804 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1805
1806 //wxCHECK_RET( ( (!unselect_others) && is_single),
1807 // wxT("this is a single selection tree") );
1808
1809 // to keep going anyhow !!!
1810 if (is_single)
1811 {
1812 if (item->IsSelected())
1813 return; // nothing to do
1814 unselect_others = true;
1815 extended_select = false;
1816 }
1817 else if ( unselect_others && item->IsSelected() )
1818 {
1819 // selection change if there is more than one item currently selected
1820 wxArrayTreeItemIds selected_items;
1821 if ( GetSelections(selected_items) == 1 )
1822 return;
1823 }
1824
1825 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
1826 event.m_item = item;
1827 event.m_itemOld = m_current;
1828 event.SetEventObject( this );
1829 // TODO : Here we don't send any selection mode yet !
1830
1831 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
1832 return;
1833
1834 wxTreeItemId parent = GetItemParent( itemId );
1835 while (parent.IsOk())
1836 {
1837 if (!IsExpanded(parent))
1838 Expand( parent );
1839
1840 parent = GetItemParent( parent );
1841 }
1842
1843 EnsureVisible( itemId );
1844
1845 // ctrl press
1846 if (unselect_others)
1847 {
1848 if (is_single) Unselect(); // to speed up thing
1849 else UnselectAll();
1850 }
1851
1852 // shift press
1853 if (extended_select)
1854 {
1855 if ( !m_current )
1856 {
1857 m_current = m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
1858 }
1859
1860 // don't change the mark (m_current)
1861 SelectItemRange(m_current, item);
1862 }
1863 else
1864 {
1865 bool select = true; // the default
1866
1867 // Check if we need to toggle hilight (ctrl mode)
1868 if (!unselect_others)
1869 select=!item->IsSelected();
1870
1871 m_current = m_key_current = item;
1872 m_current->SetHilight(select);
1873 RefreshLine( m_current );
1874 }
1875
1876 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1877 GetEventHandler()->ProcessEvent( event );
1878 }
1879
1880 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId, bool select)
1881 {
1882 if ( select )
1883 {
1884 DoSelectItem(itemId);
1885 }
1886 else // deselect
1887 {
1888 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1889 wxCHECK_RET( item, wxT("SelectItem(): invalid tree item") );
1890
1891 item->SetHilight(false);
1892 RefreshLine(item);
1893 }
1894 }
1895
1896 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
1897 wxArrayTreeItemIds &array) const
1898 {
1899 if ( item->IsSelected() )
1900 array.Add(wxTreeItemId(item));
1901
1902 if ( item->HasChildren() )
1903 {
1904 wxArrayGenericTreeItems& children = item->GetChildren();
1905 size_t count = children.GetCount();
1906 for ( size_t n = 0; n < count; ++n )
1907 FillArray(children[n], array);
1908 }
1909 }
1910
1911 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
1912 {
1913 array.Empty();
1914 wxTreeItemId idRoot = GetRootItem();
1915 if ( idRoot.IsOk() )
1916 {
1917 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
1918 }
1919 //else: the tree is empty, so no selections
1920
1921 return array.Count();
1922 }
1923
1924 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1925 {
1926 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1927
1928 if (!item.IsOk()) return;
1929
1930 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
1931
1932 // first expand all parent branches
1933 wxGenericTreeItem *parent = gitem->GetParent();
1934
1935 if ( HasFlag(wxTR_HIDE_ROOT) )
1936 {
1937 while ( parent && parent != m_anchor )
1938 {
1939 Expand(parent);
1940 parent = parent->GetParent();
1941 }
1942 }
1943 else
1944 {
1945 while ( parent )
1946 {
1947 Expand(parent);
1948 parent = parent->GetParent();
1949 }
1950 }
1951
1952 //if (parent) CalculatePositions();
1953
1954 ScrollTo(item);
1955 }
1956
1957 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
1958 {
1959 if (!item.IsOk()) return;
1960
1961 // We have to call this here because the label in
1962 // question might just have been added and no screen
1963 // update taken place.
1964 if (m_dirty)
1965 #if defined( __WXMSW__ ) || defined(__WXMAC__)
1966 Update();
1967 #else
1968 wxYieldIfNeeded();
1969 #endif
1970 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
1971
1972 // now scroll to the item
1973 int item_y = gitem->GetY();
1974
1975 int start_x = 0;
1976 int start_y = 0;
1977 GetViewStart( &start_x, &start_y );
1978 start_y *= PIXELS_PER_UNIT;
1979
1980 int client_h = 0;
1981 int client_w = 0;
1982 GetClientSize( &client_w, &client_h );
1983
1984 if (item_y < start_y+3)
1985 {
1986 // going down
1987 int x = 0;
1988 int y = 0;
1989 m_anchor->GetSize( x, y, this );
1990 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1991 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1992 int x_pos = GetScrollPos( wxHORIZONTAL );
1993 // Item should appear at top
1994 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
1995 }
1996 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
1997 {
1998 // going up
1999 int x = 0;
2000 int y = 0;
2001 m_anchor->GetSize( x, y, this );
2002 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2003 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2004 item_y += PIXELS_PER_UNIT+2;
2005 int x_pos = GetScrollPos( wxHORIZONTAL );
2006 // Item should appear at bottom
2007 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 );
2008 }
2009 }
2010
2011 // FIXME: tree sorting functions are not reentrant and not MT-safe!
2012 static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
2013
2014 static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
2015 wxGenericTreeItem **item2)
2016 {
2017 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
2018
2019 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
2020 }
2021
2022 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
2023 const wxTreeItemId& item2)
2024 {
2025 return wxStrcmp(GetItemText(item1), GetItemText(item2));
2026 }
2027
2028 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
2029 {
2030 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
2031
2032 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
2033
2034 wxCHECK_RET( !s_treeBeingSorted,
2035 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
2036
2037 wxArrayGenericTreeItems& children = item->GetChildren();
2038 if ( children.Count() > 1 )
2039 {
2040 m_dirty = true;
2041
2042 s_treeBeingSorted = this;
2043 children.Sort(tree_ctrl_compare_func);
2044 s_treeBeingSorted = NULL;
2045 }
2046 //else: don't make the tree dirty as nothing changed
2047 }
2048
2049 wxImageList *wxGenericTreeCtrl::GetImageList() const
2050 {
2051 return m_imageListNormal;
2052 }
2053
2054 wxImageList *wxGenericTreeCtrl::GetButtonsImageList() const
2055 {
2056 return m_imageListButtons;
2057 }
2058
2059 wxImageList *wxGenericTreeCtrl::GetStateImageList() const
2060 {
2061 return m_imageListState;
2062 }
2063
2064 void wxGenericTreeCtrl::CalculateLineHeight()
2065 {
2066 wxClientDC dc(this);
2067 m_lineHeight = (int)(dc.GetCharHeight() + 4);
2068
2069 if ( m_imageListNormal )
2070 {
2071 // Calculate a m_lineHeight value from the normal Image sizes.
2072 // May be toggle off. Then wxGenericTreeCtrl will spread when
2073 // necessary (which might look ugly).
2074 int n = m_imageListNormal->GetImageCount();
2075 for (int i = 0; i < n ; i++)
2076 {
2077 int width = 0, height = 0;
2078 m_imageListNormal->GetSize(i, width, height);
2079 if (height > m_lineHeight) m_lineHeight = height;
2080 }
2081 }
2082
2083 if (m_imageListButtons)
2084 {
2085 // Calculate a m_lineHeight value from the Button image sizes.
2086 // May be toggle off. Then wxGenericTreeCtrl will spread when
2087 // necessary (which might look ugly).
2088 int n = m_imageListButtons->GetImageCount();
2089 for (int i = 0; i < n ; i++)
2090 {
2091 int width = 0, height = 0;
2092 m_imageListButtons->GetSize(i, width, height);
2093 if (height > m_lineHeight) m_lineHeight = height;
2094 }
2095 }
2096
2097 if (m_lineHeight < 30)
2098 m_lineHeight += 2; // at least 2 pixels
2099 else
2100 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
2101 }
2102
2103 void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
2104 {
2105 if (m_ownsImageListNormal) delete m_imageListNormal;
2106 m_imageListNormal = imageList;
2107 m_ownsImageListNormal = false;
2108 m_dirty = true;
2109 // Don't do any drawing if we're setting the list to NULL,
2110 // since we may be in the process of deleting the tree control.
2111 if (imageList)
2112 CalculateLineHeight();
2113 }
2114
2115 void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
2116 {
2117 if (m_ownsImageListState) delete m_imageListState;
2118 m_imageListState = imageList;
2119 m_ownsImageListState = false;
2120 }
2121
2122 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList *imageList)
2123 {
2124 if (m_ownsImageListButtons) delete m_imageListButtons;
2125 m_imageListButtons = imageList;
2126 m_ownsImageListButtons = false;
2127 m_dirty = true;
2128 CalculateLineHeight();
2129 }
2130
2131 void wxGenericTreeCtrl::AssignImageList(wxImageList *imageList)
2132 {
2133 SetImageList(imageList);
2134 m_ownsImageListNormal = true;
2135 }
2136
2137 void wxGenericTreeCtrl::AssignStateImageList(wxImageList *imageList)
2138 {
2139 SetStateImageList(imageList);
2140 m_ownsImageListState = true;
2141 }
2142
2143 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList *imageList)
2144 {
2145 SetButtonsImageList(imageList);
2146 m_ownsImageListButtons = true;
2147 }
2148
2149 // -----------------------------------------------------------------------------
2150 // helpers
2151 // -----------------------------------------------------------------------------
2152
2153 void wxGenericTreeCtrl::AdjustMyScrollbars()
2154 {
2155 if (m_anchor)
2156 {
2157 int x = 0, y = 0;
2158 m_anchor->GetSize( x, y, this );
2159 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2160 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
2161 int x_pos = GetScrollPos( wxHORIZONTAL );
2162 int y_pos = GetScrollPos( wxVERTICAL );
2163 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
2164 }
2165 else
2166 {
2167 SetScrollbars( 0, 0, 0, 0 );
2168 }
2169 }
2170
2171 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
2172 {
2173 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
2174 return item->GetHeight();
2175 else
2176 return m_lineHeight;
2177 }
2178
2179 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
2180 {
2181 // TODO implement "state" icon on items
2182
2183 wxTreeItemAttr *attr = item->GetAttributes();
2184 if ( attr && attr->HasFont() )
2185 dc.SetFont(attr->GetFont());
2186 else if (item->IsBold())
2187 dc.SetFont(m_boldFont);
2188
2189 long text_w = 0, text_h = 0;
2190 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
2191
2192 int image_h = 0, image_w = 0;
2193 int image = item->GetCurrentImage();
2194 if ( image != NO_IMAGE )
2195 {
2196 if ( m_imageListNormal )
2197 {
2198 m_imageListNormal->GetSize( image, image_w, image_h );
2199 image_w += 4;
2200 }
2201 else
2202 {
2203 image = NO_IMAGE;
2204 }
2205 }
2206
2207 int total_h = GetLineHeight(item);
2208
2209 if ( item->IsSelected() )
2210 {
2211 // under mac selections are only a rectangle in case they don't have the focus
2212 #ifdef __WXMAC__
2213 if ( !m_hasFocus )
2214 {
2215 dc.SetBrush( *wxTRANSPARENT_BRUSH ) ;
2216 dc.SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) , 1 , wxSOLID ) ) ;
2217 }
2218 else
2219 {
2220 dc.SetBrush( *m_hilightBrush ) ;
2221 }
2222 #else
2223 dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush));
2224 #endif
2225 }
2226 else
2227 {
2228 wxColour colBg;
2229 if ( attr && attr->HasBackgroundColour() )
2230 colBg = attr->GetBackgroundColour();
2231 else
2232 colBg = m_backgroundColour;
2233 dc.SetBrush(wxBrush(colBg, wxSOLID));
2234 }
2235
2236 int offset = HasFlag(wxTR_ROW_LINES) ? 1 : 0;
2237
2238 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT) )
2239 {
2240 int x, y, w, h;
2241
2242 DoGetPosition(&x, &y);
2243 DoGetSize(&w, &h);
2244 dc.DrawRectangle(x, item->GetY()+offset, w, total_h-offset);
2245 }
2246 else
2247 {
2248 if ( item->IsSelected() && image != NO_IMAGE )
2249 {
2250 // If it's selected, and there's an image, then we should
2251 // take care to leave the area under the image painted in the
2252 // background colour.
2253 dc.DrawRectangle( item->GetX() + image_w - 2, item->GetY()+offset,
2254 item->GetWidth() - image_w + 2, total_h-offset );
2255 }
2256 else
2257 {
2258 dc.DrawRectangle( item->GetX()-2, item->GetY()+offset,
2259 item->GetWidth()+2, total_h-offset );
2260 }
2261 }
2262
2263 if ( image != NO_IMAGE )
2264 {
2265 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
2266 m_imageListNormal->Draw( image, dc,
2267 item->GetX(),
2268 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
2269 wxIMAGELIST_DRAW_TRANSPARENT );
2270 dc.DestroyClippingRegion();
2271 }
2272
2273 dc.SetBackgroundMode(wxTRANSPARENT);
2274 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
2275 dc.DrawText( item->GetText(),
2276 (wxCoord)(image_w + item->GetX()),
2277 (wxCoord)(item->GetY() + extraH));
2278
2279 // restore normal font
2280 dc.SetFont( m_normalFont );
2281 }
2282
2283 // Now y stands for the top of the item, whereas it used to stand for middle !
2284 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
2285 {
2286 int x = level*m_indent;
2287 if (!HasFlag(wxTR_HIDE_ROOT))
2288 {
2289 x += m_indent;
2290 }
2291 else if (level == 0)
2292 {
2293 // always expand hidden root
2294 int origY = y;
2295 wxArrayGenericTreeItems& children = item->GetChildren();
2296 int count = children.Count();
2297 if (count > 0)
2298 {
2299 int n = 0, oldY;
2300 do {
2301 oldY = y;
2302 PaintLevel(children[n], dc, 1, y);
2303 } while (++n < count);
2304
2305 if (!HasFlag(wxTR_NO_LINES) && HasFlag(wxTR_LINES_AT_ROOT) && count > 0)
2306 {
2307 // draw line down to last child
2308 origY += GetLineHeight(children[0])>>1;
2309 oldY += GetLineHeight(children[n-1])>>1;
2310 dc.DrawLine(3, origY, 3, oldY);
2311 }
2312 }
2313 return;
2314 }
2315
2316 item->SetX(x+m_spacing);
2317 item->SetY(y);
2318
2319 int h = GetLineHeight(item);
2320 int y_top = y;
2321 int y_mid = y_top + (h>>1);
2322 y += h;
2323
2324 int exposed_x = dc.LogicalToDeviceX(0);
2325 int exposed_y = dc.LogicalToDeviceY(y_top);
2326
2327 if (IsExposed(exposed_x, exposed_y, 10000, h)) // 10000 = very much
2328 {
2329 wxPen *pen =
2330 #ifndef __WXMAC__
2331 // don't draw rect outline if we already have the
2332 // background color under Mac
2333 (item->IsSelected() && m_hasFocus) ? wxBLACK_PEN :
2334 #endif // !__WXMAC__
2335 wxTRANSPARENT_PEN;
2336
2337 wxColour colText;
2338 if ( item->IsSelected() )
2339 {
2340 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
2341 }
2342 else
2343 {
2344 wxTreeItemAttr *attr = item->GetAttributes();
2345 if (attr && attr->HasTextColour())
2346 colText = attr->GetTextColour();
2347 else
2348 colText = GetForegroundColour();
2349 }
2350
2351 // prepare to draw
2352 dc.SetTextForeground(colText);
2353 dc.SetPen(*pen);
2354
2355 // draw
2356 PaintItem(item, dc);
2357
2358 if (HasFlag(wxTR_ROW_LINES))
2359 {
2360 // if the background colour is white, choose a
2361 // contrasting color for the lines
2362 dc.SetPen(*((GetBackgroundColour() == *wxWHITE)
2363 ? wxMEDIUM_GREY_PEN : wxWHITE_PEN));
2364 dc.DrawLine(0, y_top, 10000, y_top);
2365 dc.DrawLine(0, y, 10000, y);
2366 }
2367
2368 // restore DC objects
2369 dc.SetBrush(*wxWHITE_BRUSH);
2370 dc.SetPen(m_dottedPen);
2371 dc.SetTextForeground(*wxBLACK);
2372
2373 if ( !HasFlag(wxTR_NO_LINES) )
2374 {
2375 // draw the horizontal line here
2376 int x_start = x;
2377 if (x > (signed)m_indent)
2378 x_start -= m_indent;
2379 else if (HasFlag(wxTR_LINES_AT_ROOT))
2380 x_start = 3;
2381 dc.DrawLine(x_start, y_mid, x + m_spacing, y_mid);
2382 }
2383
2384 // should the item show a button?
2385 if ( item->HasPlus() && HasButtons() )
2386 {
2387 if ( m_imageListButtons )
2388 {
2389 // draw the image button here
2390 int image_h = 0,
2391 image_w = 0;
2392 int image = item->IsExpanded() ? wxTreeItemIcon_Expanded
2393 : wxTreeItemIcon_Normal;
2394 if ( item->IsSelected() )
2395 image += wxTreeItemIcon_Selected - wxTreeItemIcon_Normal;
2396
2397 m_imageListButtons->GetSize(image, image_w, image_h);
2398 int xx = x - image_w/2;
2399 int yy = y_mid - image_h/2;
2400
2401 wxDCClipper clip(dc, xx, yy, image_w, image_h);
2402 m_imageListButtons->Draw(image, dc, xx, yy,
2403 wxIMAGELIST_DRAW_TRANSPARENT);
2404 }
2405 else // no custom buttons
2406 {
2407 static const int wImage = 9;
2408 static const int hImage = 9;
2409
2410 int flag = 0;
2411 if (item->IsExpanded())
2412 flag |= wxCONTROL_EXPANDED;
2413 if (item == m_underMouse)
2414 flag |= wxCONTROL_CURRENT;
2415
2416 wxRendererNative::Get().DrawTreeItemButton
2417 (
2418 this,
2419 dc,
2420 wxRect(x - wImage/2,
2421 y_mid - hImage/2,
2422 wImage, hImage),
2423 flag
2424 );
2425 }
2426 }
2427 }
2428
2429 if (item->IsExpanded())
2430 {
2431 wxArrayGenericTreeItems& children = item->GetChildren();
2432 int count = children.Count();
2433 if (count > 0)
2434 {
2435 int n = 0, oldY;
2436 ++level;
2437 do {
2438 oldY = y;
2439 PaintLevel(children[n], dc, level, y);
2440 } while (++n < count);
2441
2442 if (!HasFlag(wxTR_NO_LINES) && count > 0)
2443 {
2444 // draw line down to last child
2445 oldY += GetLineHeight(children[n-1])>>1;
2446 if (HasButtons()) y_mid += 5;
2447
2448 // Only draw the portion of the line that is visible, in case it is huge
2449 wxCoord xOrigin=0, yOrigin=0, width, height;
2450 dc.GetDeviceOrigin(&xOrigin, &yOrigin);
2451 yOrigin = abs(yOrigin);
2452 GetClientSize(&width, &height);
2453
2454 // Move end points to the begining/end of the view?
2455 if (y_mid < yOrigin)
2456 y_mid = yOrigin;
2457 if (oldY > yOrigin + height)
2458 oldY = yOrigin + height;
2459
2460 // after the adjustments if y_mid is larger than oldY then the line
2461 // isn't visible at all so don't draw anything
2462 if (y_mid < oldY)
2463 dc.DrawLine(x, y_mid, x, oldY);
2464 }
2465 }
2466 }
2467 }
2468
2469 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
2470 {
2471 if ( item )
2472 {
2473 if ( item->HasPlus() )
2474 {
2475 // it's a folder, indicate it by a border
2476 DrawBorder(item);
2477 }
2478 else
2479 {
2480 // draw a line under the drop target because the item will be
2481 // dropped there
2482 DrawLine(item, true /* below */);
2483 }
2484
2485 SetCursor(wxCURSOR_BULLSEYE);
2486 }
2487 else
2488 {
2489 // can't drop here
2490 SetCursor(wxCURSOR_NO_ENTRY);
2491 }
2492 }
2493
2494 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item)
2495 {
2496 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2497
2498 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2499
2500 wxClientDC dc(this);
2501 PrepareDC( dc );
2502 dc.SetLogicalFunction(wxINVERT);
2503 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2504
2505 int w = i->GetWidth() + 2;
2506 int h = GetLineHeight(i) + 2;
2507
2508 dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h);
2509 }
2510
2511 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
2512 {
2513 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2514
2515 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2516
2517 wxClientDC dc(this);
2518 PrepareDC( dc );
2519 dc.SetLogicalFunction(wxINVERT);
2520
2521 int x = i->GetX(),
2522 y = i->GetY();
2523 if ( below )
2524 {
2525 y += GetLineHeight(i) - 1;
2526 }
2527
2528 dc.DrawLine( x, y, x + i->GetWidth(), y);
2529 }
2530
2531 // -----------------------------------------------------------------------------
2532 // wxWidgets callbacks
2533 // -----------------------------------------------------------------------------
2534
2535 void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
2536 {
2537 wxPaintDC dc(this);
2538 PrepareDC( dc );
2539
2540 if ( !m_anchor)
2541 return;
2542
2543 dc.SetFont( m_normalFont );
2544 dc.SetPen( m_dottedPen );
2545
2546 // this is now done dynamically
2547 //if(GetImageList() == NULL)
2548 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2549
2550 int y = 2;
2551 PaintLevel( m_anchor, dc, 0, y );
2552 }
2553
2554 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &event )
2555 {
2556 m_hasFocus = true;
2557
2558 RefreshSelected();
2559
2560 event.Skip();
2561 }
2562
2563 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &event )
2564 {
2565 m_hasFocus = false;
2566
2567 RefreshSelected();
2568
2569 event.Skip();
2570 }
2571
2572 void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
2573 {
2574 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
2575 te.m_evtKey = event;
2576 te.SetEventObject( this );
2577 if ( GetEventHandler()->ProcessEvent( te ) )
2578 {
2579 // intercepted by the user code
2580 return;
2581 }
2582
2583 if ( (m_current == 0) || (m_key_current == 0) )
2584 {
2585 event.Skip();
2586 return;
2587 }
2588
2589 // how should the selection work for this event?
2590 bool is_multiple, extended_select, unselect_others;
2591 EventFlagsToSelType(GetWindowStyleFlag(),
2592 event.ShiftDown(),
2593 event.ControlDown(),
2594 is_multiple, extended_select, unselect_others);
2595
2596 // + : Expand
2597 // - : Collaspe
2598 // * : Expand all/Collapse all
2599 // ' ' | return : activate
2600 // up : go up (not last children!)
2601 // down : go down
2602 // left : go to parent
2603 // right : open if parent and go next
2604 // home : go to root
2605 // end : go to last item without opening parents
2606 // alnum : start or continue searching for the item with this prefix
2607 int keyCode = event.GetKeyCode();
2608 switch ( keyCode )
2609 {
2610 case '+':
2611 case WXK_ADD:
2612 if (m_current->HasPlus() && !IsExpanded(m_current))
2613 {
2614 Expand(m_current);
2615 }
2616 break;
2617
2618 case '*':
2619 case WXK_MULTIPLY:
2620 if ( !IsExpanded(m_current) )
2621 {
2622 // expand all
2623 ExpandAll(m_current);
2624 break;
2625 }
2626 //else: fall through to Collapse() it
2627
2628 case '-':
2629 case WXK_SUBTRACT:
2630 if (IsExpanded(m_current))
2631 {
2632 Collapse(m_current);
2633 }
2634 break;
2635
2636 case ' ':
2637 case WXK_RETURN:
2638 if ( !event.HasModifiers() )
2639 {
2640 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2641 event.m_item = m_current;
2642 event.SetEventObject( this );
2643 GetEventHandler()->ProcessEvent( event );
2644 }
2645
2646 // in any case, also generate the normal key event for this key,
2647 // even if we generated the ACTIVATED event above: this is what
2648 // wxMSW does and it makes sense because you might not want to
2649 // process ACTIVATED event at all and handle Space and Return
2650 // directly (and differently) which would be impossible otherwise
2651 event.Skip();
2652 break;
2653
2654 // up goes to the previous sibling or to the last
2655 // of its children if it's expanded
2656 case WXK_UP:
2657 {
2658 wxTreeItemId prev = GetPrevSibling( m_key_current );
2659 if (!prev)
2660 {
2661 prev = GetItemParent( m_key_current );
2662 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2663 {
2664 break; // don't go to root if it is hidden
2665 }
2666 if (prev)
2667 {
2668 wxTreeItemIdValue cookie;
2669 wxTreeItemId current = m_key_current;
2670 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2671 if (current == GetFirstChild( prev, cookie ))
2672 {
2673 // otherwise we return to where we came from
2674 DoSelectItem( prev, unselect_others, extended_select );
2675 m_key_current= (wxGenericTreeItem*) prev.m_pItem;
2676 break;
2677 }
2678 }
2679 }
2680 if (prev)
2681 {
2682 while ( IsExpanded(prev) && HasChildren(prev) )
2683 {
2684 wxTreeItemId child = GetLastChild(prev);
2685 if ( child )
2686 {
2687 prev = child;
2688 }
2689 }
2690
2691 DoSelectItem( prev, unselect_others, extended_select );
2692 m_key_current=(wxGenericTreeItem*) prev.m_pItem;
2693 }
2694 }
2695 break;
2696
2697 // left arrow goes to the parent
2698 case WXK_LEFT:
2699 {
2700 wxTreeItemId prev = GetItemParent( m_current );
2701 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2702 {
2703 // don't go to root if it is hidden
2704 prev = GetPrevSibling( m_current );
2705 }
2706 if (prev)
2707 {
2708 DoSelectItem( prev, unselect_others, extended_select );
2709 }
2710 }
2711 break;
2712
2713 case WXK_RIGHT:
2714 // this works the same as the down arrow except that we
2715 // also expand the item if it wasn't expanded yet
2716 Expand(m_current);
2717 // fall through
2718
2719 case WXK_DOWN:
2720 {
2721 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
2722 {
2723 wxTreeItemIdValue cookie;
2724 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
2725 DoSelectItem( child, unselect_others, extended_select );
2726 m_key_current=(wxGenericTreeItem*) child.m_pItem;
2727 }
2728 else
2729 {
2730 wxTreeItemId next = GetNextSibling( m_key_current );
2731 if (!next)
2732 {
2733 wxTreeItemId current = m_key_current;
2734 while (current.IsOk() && !next)
2735 {
2736 current = GetItemParent( current );
2737 if (current) next = GetNextSibling( current );
2738 }
2739 }
2740 if (next)
2741 {
2742 DoSelectItem( next, unselect_others, extended_select );
2743 m_key_current=(wxGenericTreeItem*) next.m_pItem;
2744 }
2745 }
2746 }
2747 break;
2748
2749 // <End> selects the last visible tree item
2750 case WXK_END:
2751 {
2752 wxTreeItemId last = GetRootItem();
2753
2754 while ( last.IsOk() && IsExpanded(last) )
2755 {
2756 wxTreeItemId lastChild = GetLastChild(last);
2757
2758 // it may happen if the item was expanded but then all of
2759 // its children have been deleted - so IsExpanded() returned
2760 // true, but GetLastChild() returned invalid item
2761 if ( !lastChild )
2762 break;
2763
2764 last = lastChild;
2765 }
2766
2767 if ( last.IsOk() )
2768 {
2769 DoSelectItem( last, unselect_others, extended_select );
2770 }
2771 }
2772 break;
2773
2774 // <Home> selects the root item
2775 case WXK_HOME:
2776 {
2777 wxTreeItemId prev = GetRootItem();
2778 if (!prev)
2779 break;
2780
2781 if ( HasFlag(wxTR_HIDE_ROOT) )
2782 {
2783 wxTreeItemIdValue cookie;
2784 prev = GetFirstChild(prev, cookie);
2785 if (!prev)
2786 break;
2787 }
2788
2789 DoSelectItem( prev, unselect_others, extended_select );
2790 }
2791 break;
2792
2793 default:
2794 // do not use wxIsalnum() here
2795 if ( !event.HasModifiers() &&
2796 ((keyCode >= '0' && keyCode <= '9') ||
2797 (keyCode >= 'a' && keyCode <= 'z') ||
2798 (keyCode >= 'A' && keyCode <= 'Z' )))
2799 {
2800 // find the next item starting with the given prefix
2801 wxChar ch = (wxChar)keyCode;
2802
2803 wxTreeItemId id = FindItem(m_current, m_findPrefix + ch);
2804 if ( !id.IsOk() )
2805 {
2806 // no such item
2807 break;
2808 }
2809
2810 SelectItem(id);
2811
2812 m_findPrefix += ch;
2813
2814 // also start the timer to reset the current prefix if the user
2815 // doesn't press any more alnum keys soon -- we wouldn't want
2816 // to use this prefix for a new item search
2817 if ( !m_findTimer )
2818 {
2819 m_findTimer = new wxTreeFindTimer(this);
2820 }
2821
2822 m_findTimer->Start(wxTreeFindTimer::DELAY, wxTIMER_ONE_SHOT);
2823 }
2824 else
2825 {
2826 event.Skip();
2827 }
2828 }
2829 }
2830
2831 wxTreeItemId wxGenericTreeCtrl::HitTest(const wxPoint& point, int& flags)
2832 {
2833 // JACS: removed wxYieldIfNeeded() because it can cause the window
2834 // to be deleted from under us if a close window event is pending
2835
2836 int w, h;
2837 GetSize(&w, &h);
2838 flags=0;
2839 if (point.x<0) flags |= wxTREE_HITTEST_TOLEFT;
2840 if (point.x>w) flags |= wxTREE_HITTEST_TORIGHT;
2841 if (point.y<0) flags |= wxTREE_HITTEST_ABOVE;
2842 if (point.y>h) flags |= wxTREE_HITTEST_BELOW;
2843 if (flags) return wxTreeItemId();
2844
2845 if (m_anchor == NULL)
2846 {
2847 flags = wxTREE_HITTEST_NOWHERE;
2848 return wxTreeItemId();
2849 }
2850
2851 wxGenericTreeItem *hit = m_anchor->HitTest(CalcUnscrolledPosition(point),
2852 this, flags, 0);
2853 if (hit == NULL)
2854 {
2855 flags = wxTREE_HITTEST_NOWHERE;
2856 return wxTreeItemId();
2857 }
2858 return hit;
2859 }
2860
2861 // get the bounding rectangle of the item (or of its label only)
2862 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
2863 wxRect& rect,
2864 bool WXUNUSED(textOnly)) const
2865 {
2866 wxCHECK_MSG( item.IsOk(), false, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2867
2868 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2869
2870 int startX, startY;
2871 GetViewStart(& startX, & startY);
2872
2873 rect.x = i->GetX() - startX*PIXELS_PER_UNIT;
2874 rect.y = i->GetY() - startY*PIXELS_PER_UNIT;
2875 rect.width = i->GetWidth();
2876 //rect.height = i->GetHeight();
2877 rect.height = GetLineHeight(i);
2878
2879 return true;
2880 }
2881
2882 void wxGenericTreeCtrl::Edit( const wxTreeItemId& item )
2883 {
2884 wxCHECK_RET( item.IsOk(), _T("can't edit an invalid item") );
2885
2886 wxGenericTreeItem *itemEdit = (wxGenericTreeItem *)item.m_pItem;
2887
2888 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
2889 te.m_item = itemEdit;
2890 te.SetEventObject( this );
2891 if ( GetEventHandler()->ProcessEvent( te ) && !te.IsAllowed() )
2892 {
2893 // vetoed by user
2894 return;
2895 }
2896
2897 // We have to call this here because the label in
2898 // question might just have been added and no screen
2899 // update taken place.
2900 if ( m_dirty )
2901 #if defined( __WXMSW__ ) || defined(__WXMAC__)
2902 Update();
2903 #else
2904 wxYieldIfNeeded();
2905 #endif
2906
2907 m_textCtrl = new wxTreeTextCtrl(this, itemEdit);
2908
2909 m_textCtrl->SetFocus();
2910 }
2911
2912 // returns a pointer to the text edit control if the item is being
2913 // edited, NULL otherwise (it's assumed that no more than one item may
2914 // be edited simultaneously)
2915 wxTextCtrl* wxGenericTreeCtrl::GetEditControl() const
2916 {
2917 return m_textCtrl;
2918 }
2919
2920 bool wxGenericTreeCtrl::OnRenameAccept(wxGenericTreeItem *item,
2921 const wxString& value)
2922 {
2923 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
2924 le.m_item = item;
2925 le.SetEventObject( this );
2926 le.m_label = value;
2927 le.m_editCancelled = false;
2928
2929 return !GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
2930 }
2931
2932 void wxGenericTreeCtrl::OnRenameCancelled(wxGenericTreeItem *item)
2933 {
2934 // let owner know that the edit was cancelled
2935 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
2936 le.m_item = item;
2937 le.SetEventObject( this );
2938 le.m_label = wxEmptyString;
2939 le.m_editCancelled = true;
2940
2941 GetEventHandler()->ProcessEvent( le );
2942 }
2943
2944 void wxGenericTreeCtrl::OnRenameTimer()
2945 {
2946 Edit( m_current );
2947 }
2948
2949 void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
2950 {
2951 if ( !m_anchor ) return;
2952
2953 wxPoint pt = CalcUnscrolledPosition(event.GetPosition());
2954
2955 // Is the mouse over a tree item button?
2956 int flags = 0;
2957 wxGenericTreeItem *thisItem = m_anchor->HitTest(pt, this, flags, 0);
2958 wxGenericTreeItem *underMouse = thisItem;
2959 #if wxUSE_TOOLTIPS
2960 bool underMouseChanged = (underMouse != m_underMouse) ;
2961 #endif // wxUSE_TOOLTIPS
2962
2963 if ((underMouse) &&
2964 (flags & wxTREE_HITTEST_ONITEMBUTTON) &&
2965 (!event.LeftIsDown()) &&
2966 (!m_isDragging) &&
2967 (!m_renameTimer || !m_renameTimer->IsRunning()))
2968 {
2969 }
2970 else
2971 {
2972 underMouse = NULL;
2973 }
2974
2975 if (underMouse != m_underMouse)
2976 {
2977 if (m_underMouse)
2978 {
2979 // unhighlight old item
2980 wxGenericTreeItem *tmp = m_underMouse;
2981 m_underMouse = NULL;
2982 RefreshLine( tmp );
2983 }
2984
2985 m_underMouse = underMouse;
2986 if (m_underMouse)
2987 RefreshLine( m_underMouse );
2988 }
2989
2990 #if wxUSE_TOOLTIPS
2991 // Determines what item we are hovering over and need a tooltip for
2992 wxTreeItemId hoverItem = thisItem;
2993
2994 // We do not want a tooltip if we are dragging, or if the rename timer is running
2995 if (underMouseChanged && hoverItem.IsOk() && !m_isDragging && (!m_renameTimer || !m_renameTimer->IsRunning()))
2996 {
2997 // Ask the tree control what tooltip (if any) should be shown
2998 wxTreeEvent hevent(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP, GetId());
2999 hevent.m_item = hoverItem;
3000 hevent.SetEventObject(this);
3001
3002 if ( GetEventHandler()->ProcessEvent(hevent) && hevent.IsAllowed() )
3003 {
3004 SetToolTip(hevent.m_label);
3005 }
3006 }
3007 #endif
3008
3009 // we process left mouse up event (enables in-place edit), right down
3010 // (pass to the user code), left dbl click (activate item) and
3011 // dragging/moving events for items drag-and-drop
3012 if ( !(event.LeftDown() ||
3013 event.LeftUp() ||
3014 event.RightDown() ||
3015 event.LeftDClick() ||
3016 event.Dragging() ||
3017 ((event.Moving() || event.RightUp()) && m_isDragging)) )
3018 {
3019 event.Skip();
3020
3021 return;
3022 }
3023
3024
3025 flags = 0;
3026 wxGenericTreeItem *item = m_anchor->HitTest(pt, this, flags, 0);
3027
3028 if ( event.Dragging() && !m_isDragging )
3029 {
3030 if (m_dragCount == 0)
3031 m_dragStart = pt;
3032
3033 m_dragCount++;
3034
3035 if (m_dragCount != 3)
3036 {
3037 // wait until user drags a bit further...
3038 return;
3039 }
3040
3041 wxEventType command = event.RightIsDown()
3042 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
3043 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
3044
3045 wxTreeEvent nevent( command, GetId() );
3046 nevent.m_item = m_current;
3047 nevent.SetEventObject(this);
3048
3049 // by default the dragging is not supported, the user code must
3050 // explicitly allow the event for it to take place
3051 nevent.Veto();
3052
3053 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
3054 {
3055 // we're going to drag this item
3056 m_isDragging = true;
3057
3058 // remember the old cursor because we will change it while
3059 // dragging
3060 m_oldCursor = m_cursor;
3061
3062 // in a single selection control, hide the selection temporarily
3063 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
3064 {
3065 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
3066
3067 if ( m_oldSelection )
3068 {
3069 m_oldSelection->SetHilight(false);
3070 RefreshLine(m_oldSelection);
3071 }
3072 }
3073
3074 CaptureMouse();
3075 }
3076 }
3077 else if ( event.Moving() )
3078 {
3079 if ( item != m_dropTarget )
3080 {
3081 // unhighlight the previous drop target
3082 DrawDropEffect(m_dropTarget);
3083
3084 m_dropTarget = item;
3085
3086 // highlight the current drop target if any
3087 DrawDropEffect(m_dropTarget);
3088
3089 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3090 Update();
3091 #else
3092 wxYieldIfNeeded();
3093 #endif
3094 }
3095 }
3096 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
3097 {
3098 // erase the highlighting
3099 DrawDropEffect(m_dropTarget);
3100
3101 if ( m_oldSelection )
3102 {
3103 m_oldSelection->SetHilight(true);
3104 RefreshLine(m_oldSelection);
3105 m_oldSelection = (wxGenericTreeItem *)NULL;
3106 }
3107
3108 // generate the drag end event
3109 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId());
3110
3111 event.m_item = item;
3112 event.m_pointDrag = pt;
3113 event.SetEventObject(this);
3114
3115 (void)GetEventHandler()->ProcessEvent(event);
3116
3117 m_isDragging = false;
3118 m_dropTarget = (wxGenericTreeItem *)NULL;
3119
3120 ReleaseMouse();
3121
3122 SetCursor(m_oldCursor);
3123
3124 #if defined( __WXMSW__ ) || defined(__WXMAC__)
3125 Update();
3126 #else
3127 wxYieldIfNeeded();
3128 #endif
3129 }
3130 else
3131 {
3132 // here we process only the messages which happen on tree items
3133
3134 m_dragCount = 0;
3135
3136 if (item == NULL) return; /* we hit the blank area */
3137
3138 if ( event.RightDown() )
3139 {
3140 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
3141 nevent.m_item = item;
3142 nevent.m_pointDrag = CalcScrolledPosition(pt);
3143 nevent.SetEventObject(this);
3144 GetEventHandler()->ProcessEvent(nevent);
3145 }
3146 else if ( event.LeftUp() )
3147 {
3148 // this facilitates multiple-item drag-and-drop
3149
3150 if (item && HasFlag(wxTR_MULTIPLE))
3151 {
3152 wxArrayTreeItemIds selections;
3153 size_t count = GetSelections(selections);
3154
3155 if (count > 1 &&
3156 !event.ControlDown() &&
3157 !event.ShiftDown())
3158 {
3159 DoSelectItem(item, true, false);
3160 }
3161 }
3162
3163 if ( m_lastOnSame )
3164 {
3165 if ( (item == m_current) &&
3166 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
3167 HasFlag(wxTR_EDIT_LABELS) )
3168 {
3169 if ( m_renameTimer )
3170 {
3171 if ( m_renameTimer->IsRunning() )
3172 m_renameTimer->Stop();
3173 }
3174 else
3175 {
3176 m_renameTimer = new wxTreeRenameTimer( this );
3177 }
3178
3179 m_renameTimer->Start( wxTreeRenameTimer::DELAY, true );
3180 }
3181
3182 m_lastOnSame = false;
3183 }
3184 }
3185 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
3186 {
3187 if ( event.LeftDown() )
3188 {
3189 m_lastOnSame = item == m_current;
3190 }
3191
3192 if ( flags & wxTREE_HITTEST_ONITEMBUTTON )
3193 {
3194 // only toggle the item for a single click, double click on
3195 // the button doesn't do anything (it toggles the item twice)
3196 if ( event.LeftDown() )
3197 {
3198 Toggle( item );
3199 }
3200
3201 // don't select the item if the button was clicked
3202 return;
3203 }
3204
3205
3206 // clear the previously selected items, if the
3207 // user clicked outside of the present selection.
3208 // otherwise, perform the deselection on mouse-up.
3209 // this allows multiple drag and drop to work.
3210
3211 if (!IsSelected(item))
3212 {
3213 // how should the selection work for this event?
3214 bool is_multiple, extended_select, unselect_others;
3215 EventFlagsToSelType(GetWindowStyleFlag(),
3216 event.ShiftDown(),
3217 event.ControlDown(),
3218 is_multiple, extended_select, unselect_others);
3219
3220 DoSelectItem(item, unselect_others, extended_select);
3221 }
3222
3223
3224 // For some reason, Windows isn't recognizing a left double-click,
3225 // so we need to simulate it here. Allow 200 milliseconds for now.
3226 if ( event.LeftDClick() )
3227 {
3228 // double clicking should not start editing the item label
3229 if ( m_renameTimer )
3230 m_renameTimer->Stop();
3231
3232 m_lastOnSame = false;
3233
3234 // send activate event first
3235 wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
3236 nevent.m_item = item;
3237 nevent.m_pointDrag = CalcScrolledPosition(pt);
3238 nevent.SetEventObject( this );
3239 if ( !GetEventHandler()->ProcessEvent( nevent ) )
3240 {
3241 // if the user code didn't process the activate event,
3242 // handle it ourselves by toggling the item when it is
3243 // double clicked
3244 if ( item->HasPlus() )
3245 {
3246 Toggle(item);
3247 }
3248 }
3249 }
3250 }
3251 }
3252 }
3253
3254 void wxGenericTreeCtrl::OnInternalIdle()
3255 {
3256 wxWindow::OnInternalIdle();
3257
3258 // Check if we need to select the root item
3259 // because nothing else has been selected.
3260 // Delaying it means that we can invoke event handlers
3261 // as required, when a first item is selected.
3262 if (!HasFlag(wxTR_MULTIPLE) && !GetSelection().IsOk())
3263 {
3264 if (m_select_me)
3265 SelectItem(m_select_me);
3266 else if (GetRootItem().IsOk())
3267 SelectItem(GetRootItem());
3268 }
3269
3270 /* after all changes have been done to the tree control,
3271 * we actually redraw the tree when everything is over */
3272
3273 if (!m_dirty) return;
3274 if (m_freezeCount) return;
3275
3276 m_dirty = false;
3277
3278 CalculatePositions();
3279 Refresh();
3280 AdjustMyScrollbars();
3281 }
3282
3283 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
3284 {
3285 wxCoord text_w = 0;
3286 wxCoord text_h = 0;
3287
3288 wxTreeItemAttr *attr = item->GetAttributes();
3289 if ( attr && attr->HasFont() )
3290 dc.SetFont(attr->GetFont());
3291 else if ( item->IsBold() )
3292 dc.SetFont(m_boldFont);
3293 else
3294 dc.SetFont(m_normalFont);
3295
3296 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
3297 text_h+=2;
3298
3299 // restore normal font
3300 dc.SetFont( m_normalFont );
3301
3302 int image_h = 0;
3303 int image_w = 0;
3304 int image = item->GetCurrentImage();
3305 if ( image != NO_IMAGE )
3306 {
3307 if ( m_imageListNormal )
3308 {
3309 m_imageListNormal->GetSize( image, image_w, image_h );
3310 image_w += 4;
3311 }
3312 }
3313
3314 int total_h = (image_h > text_h) ? image_h : text_h;
3315
3316 if (total_h < 30)
3317 total_h += 2; // at least 2 pixels
3318 else
3319 total_h += total_h/10; // otherwise 10% extra spacing
3320
3321 item->SetHeight(total_h);
3322 if (total_h>m_lineHeight)
3323 m_lineHeight=total_h;
3324
3325 item->SetWidth(image_w+text_w+2);
3326 }
3327
3328 // -----------------------------------------------------------------------------
3329 // for developper : y is now the top of the level
3330 // not the middle of it !
3331 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
3332 {
3333 int x = level*m_indent;
3334 if (!HasFlag(wxTR_HIDE_ROOT))
3335 {
3336 x += m_indent;
3337 }
3338 else if (level == 0)
3339 {
3340 // a hidden root is not evaluated, but its
3341 // children are always calculated
3342 goto Recurse;
3343 }
3344
3345 CalculateSize( item, dc );
3346
3347 // set its position
3348 item->SetX( x+m_spacing );
3349 item->SetY( y );
3350 y += GetLineHeight(item);
3351
3352 if ( !item->IsExpanded() )
3353 {
3354 // we don't need to calculate collapsed branches
3355 return;
3356 }
3357
3358 Recurse:
3359 wxArrayGenericTreeItems& children = item->GetChildren();
3360 size_t n, count = children.Count();
3361 ++level;
3362 for (n = 0; n < count; ++n )
3363 CalculateLevel( children[n], dc, level, y ); // recurse
3364 }
3365
3366 void wxGenericTreeCtrl::CalculatePositions()
3367 {
3368 if ( !m_anchor ) return;
3369
3370 wxClientDC dc(this);
3371 PrepareDC( dc );
3372
3373 dc.SetFont( m_normalFont );
3374
3375 dc.SetPen( m_dottedPen );
3376 //if(GetImageList() == NULL)
3377 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3378
3379 int y = 2;
3380 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
3381 }
3382
3383 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
3384 {
3385 if (m_dirty) return;
3386 if (m_freezeCount) return;
3387
3388 wxSize client = GetClientSize();
3389
3390 wxRect rect;
3391 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
3392 rect.width = client.x;
3393 rect.height = client.y;
3394
3395 Refresh(true, &rect);
3396
3397 AdjustMyScrollbars();
3398 }
3399
3400 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
3401 {
3402 if (m_dirty) return;
3403 if (m_freezeCount) return;
3404
3405 wxRect rect;
3406 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
3407 rect.width = GetClientSize().x;
3408 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
3409
3410 Refresh(true, &rect);
3411 }
3412
3413 void wxGenericTreeCtrl::RefreshSelected()
3414 {
3415 if (m_freezeCount) return;
3416
3417 // TODO: this is awfully inefficient, we should keep the list of all
3418 // selected items internally, should be much faster
3419 if ( m_anchor )
3420 RefreshSelectedUnder(m_anchor);
3421 }
3422
3423 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem *item)
3424 {
3425 if (m_freezeCount) return;
3426
3427 if ( item->IsSelected() )
3428 RefreshLine(item);
3429
3430 const wxArrayGenericTreeItems& children = item->GetChildren();
3431 size_t count = children.GetCount();
3432 for ( size_t n = 0; n < count; n++ )
3433 {
3434 RefreshSelectedUnder(children[n]);
3435 }
3436 }
3437
3438 void wxGenericTreeCtrl::Freeze()
3439 {
3440 m_freezeCount++;
3441 }
3442
3443 void wxGenericTreeCtrl::Thaw()
3444 {
3445 wxCHECK_RET( m_freezeCount > 0, _T("thawing unfrozen tree control?") );
3446
3447 if ( !--m_freezeCount )
3448 {
3449 Refresh();
3450 }
3451 }
3452
3453 // ----------------------------------------------------------------------------
3454 // changing colours: we need to refresh the tree control
3455 // ----------------------------------------------------------------------------
3456
3457 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour& colour)
3458 {
3459 if ( !wxWindow::SetBackgroundColour(colour) )
3460 return false;
3461
3462 if (m_freezeCount) return true;
3463
3464 Refresh();
3465
3466 return true;
3467 }
3468
3469 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour& colour)
3470 {
3471 if ( !wxWindow::SetForegroundColour(colour) )
3472 return false;
3473
3474 if (m_freezeCount) return true;
3475
3476 Refresh();
3477
3478 return true;
3479 }
3480
3481 // Process the tooltip event, to speed up event processing.
3482 // Doesn't actually get a tooltip.
3483 void wxGenericTreeCtrl::OnGetToolTip( wxTreeEvent &event )
3484 {
3485 event.Veto();
3486 }
3487
3488
3489 wxSize wxGenericTreeCtrl::DoGetBestSize() const
3490 {
3491 // something is better than nothing...
3492 // 100x80 is what the MSW version will get from the default
3493 // wxControl::DoGetBestSize
3494 return wxSize(100,80);
3495 }
3496
3497
3498 // NOTE: If using the wxListBox visual attributes works everywhere then this can
3499 // be removed, as well as the #else case below.
3500 #define _USE_VISATTR 0
3501
3502 #if _USE_VISATTR
3503 #include "wx/listbox.h"
3504 #endif
3505
3506 //static
3507 wxVisualAttributes
3508 #if _USE_VISATTR
3509 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
3510 #else
3511 wxGenericTreeCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
3512 #endif
3513 {
3514 #if _USE_VISATTR
3515 // Use the same color scheme as wxListBox
3516 return wxListBox::GetClassDefaultAttributes(variant);
3517 #else
3518 wxVisualAttributes attr;
3519 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
3520 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
3521 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
3522 return attr;
3523 #endif
3524 }
3525
3526 #endif // wxUSE_TREECTRL