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