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