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