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