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