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