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