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