]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treectlg.cpp
argh, another IsDialogMessage() fix: we still need to check all non top level parents
[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 // determine the starting point: we shouldn't take the current item (this
1199 // allows to switch between two items starting with the same letter just by
1200 // pressing it) but we shouldn't jump to the next one if the user is
1201 // continuing to type as otherwise he might easily skip the item he wanted
1202 wxTreeItemId id = idParent;
1203 if ( prefix.length() == 1 )
1204 {
1205 id = GetNext(id);
1206 }
1207
1208 // look for the item starting with the given prefix after it
1209 while ( id.IsOk() && !GetItemText(id).Lower().StartsWith(prefix) )
1210 {
1211 id = GetNext(id);
1212 }
1213
1214 // if we haven't found anything...
1215 if ( !id.IsOk() )
1216 {
1217 // ... wrap to the beginning
1218 id = GetRootItem();
1219 if ( HasFlag(wxTR_HIDE_ROOT) )
1220 {
1221 // can't select virtual root
1222 id = GetNext(id);
1223 }
1224
1225 // and try all the items (stop when we get to the one we started from)
1226 while ( id != idParent && !GetItemText(id).Lower().StartsWith(prefix) )
1227 {
1228 id = GetNext(id);
1229 }
1230 }
1231
1232 return id;
1233 }
1234
1235 // -----------------------------------------------------------------------------
1236 // operations
1237 // -----------------------------------------------------------------------------
1238
1239 wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
1240 size_t previous,
1241 const wxString& text,
1242 int image, int selImage,
1243 wxTreeItemData *data)
1244 {
1245 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1246 if ( !parent )
1247 {
1248 // should we give a warning here?
1249 return AddRoot(text, image, selImage, data);
1250 }
1251
1252 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1253
1254 wxGenericTreeItem *item =
1255 new wxGenericTreeItem( parent, text, image, selImage, data );
1256
1257 if ( data != NULL )
1258 {
1259 data->m_pItem = (long) item;
1260 }
1261
1262 parent->Insert( item, previous );
1263
1264 return item;
1265 }
1266
1267 wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
1268 int image, int selImage,
1269 wxTreeItemData *data)
1270 {
1271 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
1272
1273 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1274
1275 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text,
1276 image, selImage, data);
1277 if ( data != NULL )
1278 {
1279 data->m_pItem = (long) m_anchor;
1280 }
1281
1282 if (HasFlag(wxTR_HIDE_ROOT))
1283 {
1284 // if root is hidden, make sure we can navigate
1285 // into children
1286 m_anchor->SetHasPlus();
1287 m_anchor->Expand();
1288 CalculatePositions();
1289 }
1290
1291 if (!HasFlag(wxTR_MULTIPLE))
1292 {
1293 m_current = m_key_current = m_anchor;
1294 m_current->SetHilight( TRUE );
1295 }
1296
1297 return m_anchor;
1298 }
1299
1300 wxTreeItemId wxGenericTreeCtrl::PrependItem(const wxTreeItemId& parent,
1301 const wxString& text,
1302 int image, int selImage,
1303 wxTreeItemData *data)
1304 {
1305 return DoInsertItem(parent, 0u, text, image, selImage, data);
1306 }
1307
1308 wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
1309 const wxTreeItemId& idPrevious,
1310 const wxString& text,
1311 int image, int selImage,
1312 wxTreeItemData *data)
1313 {
1314 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1315 if ( !parent )
1316 {
1317 // should we give a warning here?
1318 return AddRoot(text, image, selImage, data);
1319 }
1320
1321 int index = -1;
1322 if (idPrevious.IsOk())
1323 {
1324 index = parent->GetChildren().Index((wxGenericTreeItem*) idPrevious.m_pItem);
1325 wxASSERT_MSG( index != wxNOT_FOUND,
1326 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1327 }
1328
1329 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1330 }
1331
1332 wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
1333 size_t before,
1334 const wxString& text,
1335 int image, int selImage,
1336 wxTreeItemData *data)
1337 {
1338 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1339 if ( !parent )
1340 {
1341 // should we give a warning here?
1342 return AddRoot(text, image, selImage, data);
1343 }
1344
1345 return DoInsertItem(parentId, before, text, image, selImage, data);
1346 }
1347
1348 wxTreeItemId wxGenericTreeCtrl::AppendItem(const wxTreeItemId& parentId,
1349 const wxString& text,
1350 int image, int selImage,
1351 wxTreeItemData *data)
1352 {
1353 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1354 if ( !parent )
1355 {
1356 // should we give a warning here?
1357 return AddRoot(text, image, selImage, data);
1358 }
1359
1360 return DoInsertItem( parent, parent->GetChildren().Count(), text,
1361 image, selImage, data);
1362 }
1363
1364 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
1365 {
1366 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
1367 event.m_item = (long) item;
1368 event.SetEventObject( this );
1369 ProcessEvent( event );
1370 }
1371
1372 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
1373 {
1374 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1375
1376 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1377 item->DeleteChildren(this);
1378 }
1379
1380 void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
1381 {
1382 m_dirty = TRUE; // do this first so stuff below doesn't cause flicker
1383
1384 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1385
1386 // don't stay with invalid m_key_current or we will crash in
1387 // the next call to OnChar()
1388 bool changeKeyCurrent = FALSE;
1389 wxGenericTreeItem *itemKey = m_key_current;
1390 while ( itemKey )
1391 {
1392 if ( itemKey == item )
1393 {
1394 // m_key_current is a descendant of the item being deleted
1395 changeKeyCurrent = TRUE;
1396 break;
1397 }
1398 itemKey = itemKey->GetParent();
1399 }
1400
1401 wxGenericTreeItem *parent = item->GetParent();
1402 if ( parent )
1403 {
1404 parent->GetChildren().Remove( item ); // remove by value
1405 }
1406
1407 if ( changeKeyCurrent )
1408 {
1409 // may be NULL or not
1410 m_key_current = parent;
1411 }
1412
1413 item->DeleteChildren(this);
1414 SendDeleteEvent(item);
1415 delete item;
1416 }
1417
1418 void wxGenericTreeCtrl::DeleteAllItems()
1419 {
1420 if ( m_anchor )
1421 {
1422 m_dirty = TRUE;
1423
1424 m_anchor->DeleteChildren(this);
1425 delete m_anchor;
1426
1427 m_anchor = NULL;
1428 }
1429 }
1430
1431 void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId)
1432 {
1433 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1434
1435 wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") );
1436 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
1437 _T("can't expand hidden root") );
1438
1439 if ( !item->HasPlus() )
1440 return;
1441
1442 if ( item->IsExpanded() )
1443 return;
1444
1445 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
1446 event.m_item = (long) item;
1447 event.SetEventObject( this );
1448
1449 if ( ProcessEvent( event ) && !event.IsAllowed() )
1450 {
1451 // cancelled by program
1452 return;
1453 }
1454
1455 item->Expand();
1456 CalculatePositions();
1457
1458 RefreshSubtree(item);
1459
1460 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1461 ProcessEvent( event );
1462 }
1463
1464 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId& item)
1465 {
1466 Expand(item);
1467 if ( IsExpanded(item) )
1468 {
1469 long cookie;
1470 wxTreeItemId child = GetFirstChild(item, cookie);
1471 while ( child.IsOk() )
1472 {
1473 ExpandAll(child);
1474
1475 child = GetNextChild(item, cookie);
1476 }
1477 }
1478 }
1479
1480 void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
1481 {
1482 wxCHECK_RET( !HasFlag(wxTR_HIDE_ROOT) || itemId != GetRootItem(),
1483 _T("can't collapse hidden root") );
1484
1485 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1486
1487 if ( !item->IsExpanded() )
1488 return;
1489
1490 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
1491 event.m_item = (long) item;
1492 event.SetEventObject( this );
1493 if ( ProcessEvent( event ) && !event.IsAllowed() )
1494 {
1495 // cancelled by program
1496 return;
1497 }
1498
1499 item->Collapse();
1500
1501 #if 0 // TODO why should items be collapsed recursively?
1502 wxArrayGenericTreeItems& children = item->GetChildren();
1503 size_t count = children.Count();
1504 for ( size_t n = 0; n < count; n++ )
1505 {
1506 Collapse(children[n]);
1507 }
1508 #endif
1509
1510 CalculatePositions();
1511
1512 RefreshSubtree(item);
1513
1514 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1515 ProcessEvent( event );
1516 }
1517
1518 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1519 {
1520 Collapse(item);
1521 DeleteChildren(item);
1522 }
1523
1524 void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId)
1525 {
1526 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1527
1528 if (item->IsExpanded())
1529 Collapse(itemId);
1530 else
1531 Expand(itemId);
1532 }
1533
1534 void wxGenericTreeCtrl::Unselect()
1535 {
1536 if (m_current)
1537 {
1538 m_current->SetHilight( FALSE );
1539 RefreshLine( m_current );
1540 }
1541 }
1542
1543 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
1544 {
1545 if (item->IsSelected())
1546 {
1547 item->SetHilight(FALSE);
1548 RefreshLine(item);
1549 }
1550
1551 if (item->HasChildren())
1552 {
1553 wxArrayGenericTreeItems& children = item->GetChildren();
1554 size_t count = children.Count();
1555 for ( size_t n = 0; n < count; ++n )
1556 {
1557 UnselectAllChildren(children[n]);
1558 }
1559 }
1560 }
1561
1562 void wxGenericTreeCtrl::UnselectAll()
1563 {
1564 wxTreeItemId rootItem = GetRootItem();
1565
1566 // the tree might not have the root item at all
1567 if ( rootItem )
1568 {
1569 UnselectAllChildren((wxGenericTreeItem*) rootItem.m_pItem);
1570 }
1571 }
1572
1573 // Recursive function !
1574 // To stop we must have crt_item<last_item
1575 // Algorithm :
1576 // Tag all next children, when no more children,
1577 // Move to parent (not to tag)
1578 // Keep going... if we found last_item, we stop.
1579 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1580 {
1581 wxGenericTreeItem *parent = crt_item->GetParent();
1582
1583 if (parent == NULL) // This is root item
1584 return TagAllChildrenUntilLast(crt_item, last_item, select);
1585
1586 wxArrayGenericTreeItems& children = parent->GetChildren();
1587 int index = children.Index(crt_item);
1588 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1589
1590 size_t count = children.Count();
1591 for (size_t n=(size_t)(index+1); n<count; ++n)
1592 {
1593 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1594 }
1595
1596 return TagNextChildren(parent, last_item, select);
1597 }
1598
1599 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1600 {
1601 crt_item->SetHilight(select);
1602 RefreshLine(crt_item);
1603
1604 if (crt_item==last_item)
1605 return TRUE;
1606
1607 if (crt_item->HasChildren())
1608 {
1609 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1610 size_t count = children.Count();
1611 for ( size_t n = 0; n < count; ++n )
1612 {
1613 if (TagAllChildrenUntilLast(children[n], last_item, select))
1614 return TRUE;
1615 }
1616 }
1617
1618 return FALSE;
1619 }
1620
1621 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
1622 {
1623 // item2 is not necessary after item1
1624 wxGenericTreeItem *first=NULL, *last=NULL;
1625
1626 // choice first' and 'last' between item1 and item2
1627 if (item1->GetY()<item2->GetY())
1628 {
1629 first=item1;
1630 last=item2;
1631 }
1632 else
1633 {
1634 first=item2;
1635 last=item1;
1636 }
1637
1638 bool select = m_current->IsSelected();
1639
1640 if ( TagAllChildrenUntilLast(first,last,select) )
1641 return;
1642
1643 TagNextChildren(first,last,select);
1644 }
1645
1646 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId,
1647 bool unselect_others,
1648 bool extended_select)
1649 {
1650 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
1651
1652 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
1653 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1654
1655 //wxCHECK_RET( ( (!unselect_others) && is_single),
1656 // wxT("this is a single selection tree") );
1657
1658 // to keep going anyhow !!!
1659 if (is_single)
1660 {
1661 if (item->IsSelected())
1662 return; // nothing to do
1663 unselect_others = TRUE;
1664 extended_select = FALSE;
1665 }
1666 else if ( unselect_others && item->IsSelected() )
1667 {
1668 // selection change if there is more than one item currently selected
1669 wxArrayTreeItemIds selected_items;
1670 if ( GetSelections(selected_items) == 1 )
1671 return;
1672 }
1673
1674 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
1675 event.m_item = (long) item;
1676 event.m_itemOld = (long) m_current;
1677 event.SetEventObject( this );
1678 // TODO : Here we don't send any selection mode yet !
1679
1680 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
1681 return;
1682
1683 wxTreeItemId parent = GetParent( itemId );
1684 while (parent.IsOk())
1685 {
1686 if (!IsExpanded(parent))
1687 Expand( parent );
1688
1689 parent = GetParent( parent );
1690 }
1691
1692 EnsureVisible( itemId );
1693
1694 // ctrl press
1695 if (unselect_others)
1696 {
1697 if (is_single) Unselect(); // to speed up thing
1698 else UnselectAll();
1699 }
1700
1701 // shift press
1702 if (extended_select)
1703 {
1704 if ( !m_current )
1705 {
1706 m_current = m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
1707 }
1708
1709 // don't change the mark (m_current)
1710 SelectItemRange(m_current, item);
1711 }
1712 else
1713 {
1714 bool select=TRUE; // the default
1715
1716 // Check if we need to toggle hilight (ctrl mode)
1717 if (!unselect_others)
1718 select=!item->IsSelected();
1719
1720 m_current = m_key_current = item;
1721 m_current->SetHilight(select);
1722 RefreshLine( m_current );
1723 }
1724
1725 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1726 GetEventHandler()->ProcessEvent( event );
1727 }
1728
1729 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
1730 wxArrayTreeItemIds &array) const
1731 {
1732 if ( item->IsSelected() )
1733 array.Add(wxTreeItemId(item));
1734
1735 if ( item->HasChildren() )
1736 {
1737 wxArrayGenericTreeItems& children = item->GetChildren();
1738 size_t count = children.GetCount();
1739 for ( size_t n = 0; n < count; ++n )
1740 FillArray(children[n], array);
1741 }
1742 }
1743
1744 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
1745 {
1746 array.Empty();
1747 wxTreeItemId idRoot = GetRootItem();
1748 if ( idRoot.IsOk() )
1749 {
1750 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
1751 }
1752 //else: the tree is empty, so no selections
1753
1754 return array.Count();
1755 }
1756
1757 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1758 {
1759 if (!item.IsOk()) return;
1760
1761 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
1762
1763 // first expand all parent branches
1764 wxGenericTreeItem *parent = gitem->GetParent();
1765
1766 if ( HasFlag(wxTR_HIDE_ROOT) )
1767 {
1768 while ( parent != m_anchor )
1769 {
1770 Expand(parent);
1771 parent = parent->GetParent();
1772 }
1773 }
1774 else
1775 {
1776 while ( parent )
1777 {
1778 Expand(parent);
1779 parent = parent->GetParent();
1780 }
1781 }
1782
1783 //if (parent) CalculatePositions();
1784
1785 ScrollTo(item);
1786 }
1787
1788 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
1789 {
1790 if (!item.IsOk()) return;
1791
1792 // We have to call this here because the label in
1793 // question might just have been added and no screen
1794 // update taken place.
1795 if (m_dirty) wxYieldIfNeeded();
1796
1797 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
1798
1799 // now scroll to the item
1800 int item_y = gitem->GetY();
1801
1802 int start_x = 0;
1803 int start_y = 0;
1804 GetViewStart( &start_x, &start_y );
1805 start_y *= PIXELS_PER_UNIT;
1806
1807 int client_h = 0;
1808 int client_w = 0;
1809 GetClientSize( &client_w, &client_h );
1810
1811 if (item_y < start_y+3)
1812 {
1813 // going down
1814 int x = 0;
1815 int y = 0;
1816 m_anchor->GetSize( x, y, this );
1817 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1818 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1819 int x_pos = GetScrollPos( wxHORIZONTAL );
1820 // Item should appear at top
1821 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
1822 }
1823 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
1824 {
1825 // going up
1826 int x = 0;
1827 int y = 0;
1828 m_anchor->GetSize( x, y, this );
1829 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1830 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1831 item_y += PIXELS_PER_UNIT+2;
1832 int x_pos = GetScrollPos( wxHORIZONTAL );
1833 // Item should appear at bottom
1834 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 );
1835 }
1836 }
1837
1838 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1839 static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
1840
1841 static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
1842 wxGenericTreeItem **item2)
1843 {
1844 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1845
1846 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
1847 }
1848
1849 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1850 const wxTreeItemId& item2)
1851 {
1852 return wxStrcmp(GetItemText(item1), GetItemText(item2));
1853 }
1854
1855 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
1856 {
1857 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
1858
1859 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1860
1861 wxCHECK_RET( !s_treeBeingSorted,
1862 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1863
1864 wxArrayGenericTreeItems& children = item->GetChildren();
1865 if ( children.Count() > 1 )
1866 {
1867 m_dirty = TRUE;
1868
1869 s_treeBeingSorted = this;
1870 children.Sort(tree_ctrl_compare_func);
1871 s_treeBeingSorted = NULL;
1872 }
1873 //else: don't make the tree dirty as nothing changed
1874 }
1875
1876 wxImageList *wxGenericTreeCtrl::GetImageList() const
1877 {
1878 return m_imageListNormal;
1879 }
1880
1881 wxImageList *wxGenericTreeCtrl::GetButtonsImageList() const
1882 {
1883 return m_imageListButtons;
1884 }
1885
1886 wxImageList *wxGenericTreeCtrl::GetStateImageList() const
1887 {
1888 return m_imageListState;
1889 }
1890
1891 void wxGenericTreeCtrl::CalculateLineHeight()
1892 {
1893 wxClientDC dc(this);
1894 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1895
1896 if ( m_imageListNormal )
1897 {
1898 // Calculate a m_lineHeight value from the normal Image sizes.
1899 // May be toggle off. Then wxGenericTreeCtrl will spread when
1900 // necessary (which might look ugly).
1901 int n = m_imageListNormal->GetImageCount();
1902 for (int i = 0; i < n ; i++)
1903 {
1904 int width = 0, height = 0;
1905 m_imageListNormal->GetSize(i, width, height);
1906 if (height > m_lineHeight) m_lineHeight = height;
1907 }
1908 }
1909
1910 if (m_imageListButtons)
1911 {
1912 // Calculate a m_lineHeight value from the Button image sizes.
1913 // May be toggle off. Then wxGenericTreeCtrl will spread when
1914 // necessary (which might look ugly).
1915 int n = m_imageListButtons->GetImageCount();
1916 for (int i = 0; i < n ; i++)
1917 {
1918 int width = 0, height = 0;
1919 m_imageListButtons->GetSize(i, width, height);
1920 if (height > m_lineHeight) m_lineHeight = height;
1921 }
1922 }
1923
1924 if (m_lineHeight < 30)
1925 m_lineHeight += 2; // at least 2 pixels
1926 else
1927 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
1928 }
1929
1930 void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
1931 {
1932 if (m_ownsImageListNormal) delete m_imageListNormal;
1933 m_imageListNormal = imageList;
1934 m_ownsImageListNormal = FALSE;
1935 m_dirty = TRUE;
1936 // Don't do any drawing if we're setting the list to NULL,
1937 // since we may be in the process of deleting the tree control.
1938 if (imageList)
1939 CalculateLineHeight();
1940 }
1941
1942 void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
1943 {
1944 if (m_ownsImageListState) delete m_imageListState;
1945 m_imageListState = imageList;
1946 m_ownsImageListState = FALSE;
1947 }
1948
1949 void wxGenericTreeCtrl::SetButtonsImageList(wxImageList *imageList)
1950 {
1951 if (m_ownsImageListButtons) delete m_imageListButtons;
1952 m_imageListButtons = imageList;
1953 m_ownsImageListButtons = FALSE;
1954 m_dirty = TRUE;
1955 CalculateLineHeight();
1956 }
1957
1958 void wxGenericTreeCtrl::AssignImageList(wxImageList *imageList)
1959 {
1960 SetImageList(imageList);
1961 m_ownsImageListNormal = TRUE;
1962 }
1963
1964 void wxGenericTreeCtrl::AssignStateImageList(wxImageList *imageList)
1965 {
1966 SetStateImageList(imageList);
1967 m_ownsImageListState = TRUE;
1968 }
1969
1970 void wxGenericTreeCtrl::AssignButtonsImageList(wxImageList *imageList)
1971 {
1972 SetButtonsImageList(imageList);
1973 m_ownsImageListButtons = TRUE;
1974 }
1975
1976 // -----------------------------------------------------------------------------
1977 // helpers
1978 // -----------------------------------------------------------------------------
1979
1980 void wxGenericTreeCtrl::AdjustMyScrollbars()
1981 {
1982 if (m_anchor)
1983 {
1984 int x = 0, y = 0;
1985 m_anchor->GetSize( x, y, this );
1986 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1987 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1988 int x_pos = GetScrollPos( wxHORIZONTAL );
1989 int y_pos = GetScrollPos( wxVERTICAL );
1990 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
1991 }
1992 else
1993 {
1994 SetScrollbars( 0, 0, 0, 0 );
1995 }
1996 }
1997
1998 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
1999 {
2000 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
2001 return item->GetHeight();
2002 else
2003 return m_lineHeight;
2004 }
2005
2006 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
2007 {
2008 // TODO implement "state" icon on items
2009
2010 wxTreeItemAttr *attr = item->GetAttributes();
2011 if ( attr && attr->HasFont() )
2012 dc.SetFont(attr->GetFont());
2013 else if (item->IsBold())
2014 dc.SetFont(m_boldFont);
2015
2016 long text_w = 0, text_h = 0;
2017 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
2018
2019 int image_h = 0, image_w = 0;
2020 int image = item->GetCurrentImage();
2021 if ( image != NO_IMAGE )
2022 {
2023 if ( m_imageListNormal )
2024 {
2025 m_imageListNormal->GetSize( image, image_w, image_h );
2026 image_w += 4;
2027 }
2028 else
2029 {
2030 image = NO_IMAGE;
2031 }
2032 }
2033
2034 int total_h = GetLineHeight(item);
2035
2036 if ( item->IsSelected() )
2037 {
2038 dc.SetBrush(*(m_hasFocus ? m_hilightBrush : m_hilightUnfocusedBrush));
2039 }
2040 else
2041 {
2042 wxColour colBg;
2043 if ( attr && attr->HasBackgroundColour() )
2044 colBg = attr->GetBackgroundColour();
2045 else
2046 colBg = m_backgroundColour;
2047 dc.SetBrush(wxBrush(colBg, wxSOLID));
2048 }
2049
2050 int offset = HasFlag(wxTR_ROW_LINES) ? 1 : 0;
2051
2052 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT) )
2053 {
2054 int x, y, w, h;
2055
2056 DoGetPosition(&x, &y);
2057 DoGetSize(&w, &h);
2058 dc.DrawRectangle(x, item->GetY()+offset, w, total_h-offset);
2059 }
2060 else
2061 {
2062 if ( item->IsSelected() && image != NO_IMAGE )
2063 {
2064 // If it's selected, and there's an image, then we should
2065 // take care to leave the area under the image painted in the
2066 // background colour.
2067 dc.DrawRectangle( item->GetX() + image_w - 2, item->GetY()+offset,
2068 item->GetWidth() - image_w + 2, total_h-offset );
2069 }
2070 else
2071 {
2072 dc.DrawRectangle( item->GetX()-2, item->GetY()+offset,
2073 item->GetWidth()+2, total_h-offset );
2074 }
2075 }
2076
2077 if ( image != NO_IMAGE )
2078 {
2079 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
2080 m_imageListNormal->Draw( image, dc,
2081 item->GetX(),
2082 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
2083 wxIMAGELIST_DRAW_TRANSPARENT );
2084 dc.DestroyClippingRegion();
2085 }
2086
2087 dc.SetBackgroundMode(wxTRANSPARENT);
2088 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
2089 dc.DrawText( item->GetText(),
2090 (wxCoord)(image_w + item->GetX()),
2091 (wxCoord)(item->GetY() + extraH));
2092
2093 // restore normal font
2094 dc.SetFont( m_normalFont );
2095 }
2096
2097 // Now y stands for the top of the item, whereas it used to stand for middle !
2098 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
2099 {
2100 int x = level*m_indent;
2101 if (!HasFlag(wxTR_HIDE_ROOT))
2102 {
2103 x += m_indent;
2104 }
2105 else if (level == 0)
2106 {
2107 // always expand hidden root
2108 int origY = y;
2109 wxArrayGenericTreeItems& children = item->GetChildren();
2110 int count = children.Count();
2111 if (count > 0)
2112 {
2113 int n = 0, oldY;
2114 do {
2115 oldY = y;
2116 PaintLevel(children[n], dc, 1, y);
2117 } while (++n < count);
2118
2119 if (!HasFlag(wxTR_NO_LINES) && HasFlag(wxTR_LINES_AT_ROOT) && count > 0)
2120 {
2121 // draw line down to last child
2122 origY += GetLineHeight(children[0])>>1;
2123 oldY += GetLineHeight(children[n-1])>>1;
2124 dc.DrawLine(3, origY, 3, oldY);
2125 }
2126 }
2127 return;
2128 }
2129
2130 item->SetX(x+m_spacing);
2131 item->SetY(y);
2132
2133 int h = GetLineHeight(item);
2134 int y_top = y;
2135 int y_mid = y_top + (h>>1);
2136 y += h;
2137
2138 int exposed_x = dc.LogicalToDeviceX(0);
2139 int exposed_y = dc.LogicalToDeviceY(y_top);
2140
2141 if (IsExposed(exposed_x, exposed_y, 10000, h)) // 10000 = very much
2142 {
2143 wxPen *pen =
2144 #ifndef __WXMAC__
2145 // don't draw rect outline if we already have the
2146 // background color under Mac
2147 (item->IsSelected() && m_hasFocus) ? wxBLACK_PEN :
2148 #endif // !__WXMAC__
2149 wxTRANSPARENT_PEN;
2150
2151 wxColour colText;
2152 if ( item->IsSelected() )
2153 {
2154 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
2155 }
2156 else
2157 {
2158 wxTreeItemAttr *attr = item->GetAttributes();
2159 if (attr && attr->HasTextColour())
2160 colText = attr->GetTextColour();
2161 else
2162 colText = GetForegroundColour();
2163 }
2164
2165 // prepare to draw
2166 dc.SetTextForeground(colText);
2167 dc.SetPen(*pen);
2168
2169 // draw
2170 PaintItem(item, dc);
2171
2172 if (HasFlag(wxTR_ROW_LINES))
2173 {
2174 // if the background colour is white, choose a
2175 // contrasting color for the lines
2176 dc.SetPen(*((GetBackgroundColour() == *wxWHITE)
2177 ? wxMEDIUM_GREY_PEN : wxWHITE_PEN));
2178 dc.DrawLine(0, y_top, 10000, y_top);
2179 dc.DrawLine(0, y, 10000, y);
2180 }
2181
2182 // restore DC objects
2183 dc.SetBrush(*wxWHITE_BRUSH);
2184 dc.SetPen(m_dottedPen);
2185 dc.SetTextForeground(*wxBLACK);
2186
2187 if (item->HasPlus() && HasButtons()) // should the item show a button?
2188 {
2189 if (!HasFlag(wxTR_NO_LINES))
2190 {
2191 if (x > (signed)m_indent)
2192 dc.DrawLine(x - m_indent, y_mid, x - 5, y_mid);
2193 else if (HasFlag(wxTR_LINES_AT_ROOT))
2194 dc.DrawLine(3, y_mid, x - 5, y_mid);
2195 dc.DrawLine(x + 5, y_mid, x + m_spacing, y_mid);
2196 }
2197
2198 if (m_imageListButtons != NULL)
2199 {
2200 // draw the image button here
2201 int image_h = 0, image_w = 0, image = wxTreeItemIcon_Normal;
2202 if (item->IsExpanded()) image = wxTreeItemIcon_Expanded;
2203 if (item->IsSelected())
2204 image += wxTreeItemIcon_Selected - wxTreeItemIcon_Normal;
2205 m_imageListButtons->GetSize(image, image_w, image_h);
2206 int xx = x - (image_w>>1);
2207 int yy = y_mid - (image_h>>1);
2208 dc.SetClippingRegion(xx, yy, image_w, image_h);
2209 m_imageListButtons->Draw(image, dc, xx, yy,
2210 wxIMAGELIST_DRAW_TRANSPARENT);
2211 dc.DestroyClippingRegion();
2212 }
2213 else if (HasFlag(wxTR_TWIST_BUTTONS))
2214 {
2215 // draw the twisty button here
2216
2217 if (HasFlag(wxTR_AQUA_BUTTONS))
2218 {
2219 if (item->IsExpanded())
2220 dc.DrawBitmap( *m_arrowDown, x-5, y_mid-6, TRUE );
2221 else
2222 dc.DrawBitmap( *m_arrowRight, x-5, y_mid-6, TRUE );
2223 }
2224 else
2225 {
2226 dc.SetBrush(*m_hilightBrush);
2227 dc.SetPen(*wxBLACK_PEN);
2228 wxPoint button[3];
2229
2230 if (item->IsExpanded())
2231 {
2232 button[0].x = x-5;
2233 button[0].y = y_mid-2;
2234 button[1].x = x+5;
2235 button[1].y = y_mid-2;
2236 button[2].x = x;
2237 button[2].y = y_mid+3;
2238 }
2239 else
2240 {
2241 button[0].y = y_mid-5;
2242 button[0].x = x-2;
2243 button[1].y = y_mid+5;
2244 button[1].x = x-2;
2245 button[2].y = y_mid;
2246 button[2].x = x+3;
2247 }
2248 dc.DrawPolygon(3, button);
2249 dc.SetPen(m_dottedPen);
2250 }
2251 }
2252 else // if (HasFlag(wxTR_HAS_BUTTONS))
2253 {
2254 // draw the plus sign here
2255 dc.SetPen(*wxGREY_PEN);
2256 dc.SetBrush(*wxWHITE_BRUSH);
2257 dc.DrawRectangle(x-5, y_mid-4, 11, 9);
2258 dc.SetPen(*wxBLACK_PEN);
2259 dc.DrawLine(x-2, y_mid, x+3, y_mid);
2260 if (!item->IsExpanded())
2261 dc.DrawLine(x, y_mid-2, x, y_mid+3);
2262 dc.SetPen(m_dottedPen);
2263 }
2264 }
2265 else if (!HasFlag(wxTR_NO_LINES)) // no button; maybe a line?
2266 {
2267 // draw the horizontal line here
2268 int x_start = x;
2269 if (x > (signed)m_indent)
2270 x_start -= m_indent;
2271 else if (HasFlag(wxTR_LINES_AT_ROOT))
2272 x_start = 3;
2273 dc.DrawLine(x_start, y_mid, x + m_spacing, y_mid);
2274 }
2275 }
2276
2277 if (item->IsExpanded())
2278 {
2279 wxArrayGenericTreeItems& children = item->GetChildren();
2280 int count = children.Count();
2281 if (count > 0)
2282 {
2283 int n = 0, oldY;
2284 ++level;
2285 do {
2286 oldY = y;
2287 PaintLevel(children[n], dc, level, y);
2288 } while (++n < count);
2289
2290 if (!HasFlag(wxTR_NO_LINES) && count > 0)
2291 {
2292 // draw line down to last child
2293 oldY += GetLineHeight(children[n-1])>>1;
2294 if (HasButtons()) y_mid += 5;
2295 dc.DrawLine(x, y_mid, x, oldY);
2296 }
2297 }
2298 }
2299 }
2300
2301 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
2302 {
2303 if ( item )
2304 {
2305 if ( item->HasPlus() )
2306 {
2307 // it's a folder, indicate it by a border
2308 DrawBorder(item);
2309 }
2310 else
2311 {
2312 // draw a line under the drop target because the item will be
2313 // dropped there
2314 DrawLine(item, TRUE /* below */);
2315 }
2316
2317 SetCursor(wxCURSOR_BULLSEYE);
2318 }
2319 else
2320 {
2321 // can't drop here
2322 SetCursor(wxCURSOR_NO_ENTRY);
2323 }
2324 }
2325
2326 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item)
2327 {
2328 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2329
2330 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2331
2332 wxClientDC dc(this);
2333 PrepareDC( dc );
2334 dc.SetLogicalFunction(wxINVERT);
2335 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2336
2337 int w = i->GetWidth() + 2;
2338 int h = GetLineHeight(i) + 2;
2339
2340 dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h);
2341 }
2342
2343 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
2344 {
2345 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
2346
2347 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2348
2349 wxClientDC dc(this);
2350 PrepareDC( dc );
2351 dc.SetLogicalFunction(wxINVERT);
2352
2353 int x = i->GetX(),
2354 y = i->GetY();
2355 if ( below )
2356 {
2357 y += GetLineHeight(i) - 1;
2358 }
2359
2360 dc.DrawLine( x, y, x + i->GetWidth(), y);
2361 }
2362
2363 // -----------------------------------------------------------------------------
2364 // wxWindows callbacks
2365 // -----------------------------------------------------------------------------
2366
2367 void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
2368 {
2369 wxPaintDC dc(this);
2370 PrepareDC( dc );
2371
2372 if ( !m_anchor)
2373 return;
2374
2375 dc.SetFont( m_normalFont );
2376 dc.SetPen( m_dottedPen );
2377
2378 // this is now done dynamically
2379 //if(GetImageList() == NULL)
2380 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2381
2382 int y = 2;
2383 PaintLevel( m_anchor, dc, 0, y );
2384 }
2385
2386 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &event )
2387 {
2388 m_hasFocus = TRUE;
2389
2390 RefreshSelected();
2391
2392 event.Skip();
2393 }
2394
2395 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &event )
2396 {
2397 m_hasFocus = FALSE;
2398
2399 RefreshSelected();
2400
2401 event.Skip();
2402 }
2403
2404 void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
2405 {
2406 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
2407 te.m_evtKey = event;
2408 te.SetEventObject( this );
2409 if ( GetEventHandler()->ProcessEvent( te ) )
2410 {
2411 // intercepted by the user code
2412 return;
2413 }
2414
2415 if ( (m_current == 0) || (m_key_current == 0) )
2416 {
2417 event.Skip();
2418 return;
2419 }
2420
2421 // how should the selection work for this event?
2422 bool is_multiple, extended_select, unselect_others;
2423 EventFlagsToSelType(GetWindowStyleFlag(),
2424 event.ShiftDown(),
2425 event.ControlDown(),
2426 is_multiple, extended_select, unselect_others);
2427
2428 // + : Expand
2429 // - : Collaspe
2430 // * : Expand all/Collapse all
2431 // ' ' | return : activate
2432 // up : go up (not last children!)
2433 // down : go down
2434 // left : go to parent
2435 // right : open if parent and go next
2436 // home : go to root
2437 // end : go to last item without opening parents
2438 // alnum : start or continue searching for the item with this prefix
2439 int keyCode = event.KeyCode();
2440 switch ( keyCode )
2441 {
2442 case '+':
2443 case WXK_ADD:
2444 if (m_current->HasPlus() && !IsExpanded(m_current))
2445 {
2446 Expand(m_current);
2447 }
2448 break;
2449
2450 case '*':
2451 case WXK_MULTIPLY:
2452 if ( !IsExpanded(m_current) )
2453 {
2454 // expand all
2455 ExpandAll(m_current);
2456 break;
2457 }
2458 //else: fall through to Collapse() it
2459
2460 case '-':
2461 case WXK_SUBTRACT:
2462 if (IsExpanded(m_current))
2463 {
2464 Collapse(m_current);
2465 }
2466 break;
2467
2468 case ' ':
2469 case WXK_RETURN:
2470 {
2471 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2472 event.m_item = (long) m_current;
2473 event.SetEventObject( this );
2474 GetEventHandler()->ProcessEvent( event );
2475 }
2476 break;
2477
2478 // up goes to the previous sibling or to the last
2479 // of its children if it's expanded
2480 case WXK_UP:
2481 {
2482 wxTreeItemId prev = GetPrevSibling( m_key_current );
2483 if (!prev)
2484 {
2485 prev = GetParent( m_key_current );
2486 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2487 {
2488 break; // don't go to root if it is hidden
2489 }
2490 if (prev)
2491 {
2492 long cookie = 0;
2493 wxTreeItemId current = m_key_current;
2494 // TODO: Huh? If we get here, we'd better be the first child of our parent. How else could it be?
2495 if (current == GetFirstChild( prev, cookie ))
2496 {
2497 // otherwise we return to where we came from
2498 SelectItem( prev, unselect_others, extended_select );
2499 m_key_current= (wxGenericTreeItem*) prev.m_pItem;
2500 break;
2501 }
2502 }
2503 }
2504 if (prev)
2505 {
2506 while ( IsExpanded(prev) && HasChildren(prev) )
2507 {
2508 wxTreeItemId child = GetLastChild(prev);
2509 if ( child )
2510 {
2511 prev = child;
2512 }
2513 }
2514
2515 SelectItem( prev, unselect_others, extended_select );
2516 m_key_current=(wxGenericTreeItem*) prev.m_pItem;
2517 }
2518 }
2519 break;
2520
2521 // left arrow goes to the parent
2522 case WXK_LEFT:
2523 {
2524 wxTreeItemId prev = GetParent( m_current );
2525 if ((prev == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT))
2526 {
2527 // don't go to root if it is hidden
2528 prev = GetPrevSibling( m_current );
2529 }
2530 if (prev)
2531 {
2532 SelectItem( prev, unselect_others, extended_select );
2533 }
2534 }
2535 break;
2536
2537 case WXK_RIGHT:
2538 // this works the same as the down arrow except that we
2539 // also expand the item if it wasn't expanded yet
2540 Expand(m_current);
2541 // fall through
2542
2543 case WXK_DOWN:
2544 {
2545 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
2546 {
2547 long cookie = 0;
2548 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
2549 SelectItem( child, unselect_others, extended_select );
2550 m_key_current=(wxGenericTreeItem*) child.m_pItem;
2551 }
2552 else
2553 {
2554 wxTreeItemId next = GetNextSibling( m_key_current );
2555 if (!next)
2556 {
2557 wxTreeItemId current = m_key_current;
2558 while (current && !next)
2559 {
2560 current = GetParent( current );
2561 if (current) next = GetNextSibling( current );
2562 }
2563 }
2564 if (next)
2565 {
2566 SelectItem( next, unselect_others, extended_select );
2567 m_key_current=(wxGenericTreeItem*) next.m_pItem;
2568 }
2569 }
2570 }
2571 break;
2572
2573 // <End> selects the last visible tree item
2574 case WXK_END:
2575 {
2576 wxTreeItemId last = GetRootItem();
2577
2578 while ( last.IsOk() && IsExpanded(last) )
2579 {
2580 wxTreeItemId lastChild = GetLastChild(last);
2581
2582 // it may happen if the item was expanded but then all of
2583 // its children have been deleted - so IsExpanded() returned
2584 // TRUE, but GetLastChild() returned invalid item
2585 if ( !lastChild )
2586 break;
2587
2588 last = lastChild;
2589 }
2590
2591 if ( last.IsOk() )
2592 {
2593 SelectItem( last, unselect_others, extended_select );
2594 }
2595 }
2596 break;
2597
2598 // <Home> selects the root item
2599 case WXK_HOME:
2600 {
2601 wxTreeItemId prev = GetRootItem();
2602 if (!prev)
2603 break;
2604
2605 if ( HasFlag(wxTR_HIDE_ROOT) )
2606 {
2607 long dummy;
2608 prev = GetFirstChild(prev, dummy);
2609 if (!prev)
2610 break;
2611 }
2612
2613 SelectItem( prev, unselect_others, extended_select );
2614 }
2615 break;
2616
2617 default:
2618 // do not use wxIsalnum() here
2619 if ( !event.HasModifiers() &&
2620 ((keyCode >= '0' && keyCode <= '9') ||
2621 (keyCode >= 'a' && keyCode <= 'z') ||
2622 (keyCode >= 'A' && keyCode <= 'Z' )))
2623 {
2624 // find the next item starting with the given prefix
2625 char ch = (char)keyCode;
2626
2627 wxTreeItemId id = FindItem(m_current, m_findPrefix + (wxChar)ch);
2628 if ( !id.IsOk() )
2629 {
2630 // no such item
2631 break;
2632 }
2633
2634 SelectItem(id);
2635
2636 m_findPrefix += ch;
2637
2638 // also start the timer to reset the current prefix if the user
2639 // doesn't press any more alnum keys soon -- we wouldn't want
2640 // to use this prefix for a new item search
2641 if ( !m_findTimer )
2642 {
2643 m_findTimer = new wxTreeFindTimer(this);
2644 }
2645
2646 m_findTimer->Start(wxTreeFindTimer::DELAY, wxTIMER_ONE_SHOT);
2647 }
2648 else
2649 {
2650 event.Skip();
2651 }
2652 }
2653 }
2654
2655 wxTreeItemId wxGenericTreeCtrl::HitTest(const wxPoint& point, int& flags)
2656 {
2657 // JACS: removed wxYieldIfNeeded() because it can cause the window
2658 // to be deleted from under us if a close window event is pending
2659
2660 int w, h;
2661 GetSize(&w, &h);
2662 flags=0;
2663 if (point.x<0) flags |= wxTREE_HITTEST_TOLEFT;
2664 if (point.x>w) flags |= wxTREE_HITTEST_TORIGHT;
2665 if (point.y<0) flags |= wxTREE_HITTEST_ABOVE;
2666 if (point.y>h) flags |= wxTREE_HITTEST_BELOW;
2667 if (flags) return wxTreeItemId();
2668
2669 if (m_anchor == NULL)
2670 {
2671 flags = wxTREE_HITTEST_NOWHERE;
2672 return wxTreeItemId();
2673 }
2674
2675 wxGenericTreeItem *hit = m_anchor->HitTest(CalcUnscrolledPosition(point),
2676 this, flags, 0);
2677 if (hit == NULL)
2678 {
2679 flags = wxTREE_HITTEST_NOWHERE;
2680 return wxTreeItemId();
2681 }
2682 return hit;
2683 }
2684
2685 // get the bounding rectangle of the item (or of its label only)
2686 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
2687 wxRect& rect,
2688 bool WXUNUSED(textOnly)) const
2689 {
2690 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2691
2692 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2693
2694 int startX, startY;
2695 GetViewStart(& startX, & startY);
2696
2697 rect.x = i->GetX() - startX*PIXELS_PER_UNIT;
2698 rect.y = i->GetY() - startY*PIXELS_PER_UNIT;
2699 rect.width = i->GetWidth();
2700 //rect.height = i->GetHeight();
2701 rect.height = GetLineHeight(i);
2702
2703 return TRUE;
2704 }
2705
2706 /* **** */
2707
2708 void wxGenericTreeCtrl::Edit( const wxTreeItemId& item )
2709 {
2710 if (!item.IsOk()) return;
2711
2712 m_currentEdit = (wxGenericTreeItem*) item.m_pItem;
2713
2714 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
2715 te.m_item = (long) m_currentEdit;
2716 te.SetEventObject( this );
2717 GetEventHandler()->ProcessEvent( te );
2718
2719 if (!te.IsAllowed()) return;
2720
2721 // We have to call this here because the label in
2722 // question might just have been added and no screen
2723 // update taken place.
2724 if (m_dirty) wxYieldIfNeeded();
2725
2726 wxString s = m_currentEdit->GetText();
2727 int w = m_currentEdit->GetWidth();
2728 int h = m_currentEdit->GetHeight();
2729 int x, y;
2730 CalcScrolledPosition(m_currentEdit->GetX(), m_currentEdit->GetY(), &x, &y);
2731
2732 int image_h = 0;
2733 int image_w = 0;
2734
2735 int image = m_currentEdit->GetCurrentImage();
2736 if ( image != NO_IMAGE )
2737 {
2738 if ( m_imageListNormal )
2739 {
2740 m_imageListNormal->GetSize( image, image_w, image_h );
2741 image_w += 4;
2742 }
2743 else
2744 {
2745 wxFAIL_MSG(_T("you must create an image list to use images!"));
2746 }
2747 }
2748 x += image_w;
2749 w -= image_w + 4; // I don't know why +4 is needed
2750
2751 wxTreeTextCtrl *text = new wxTreeTextCtrl(this, -1,
2752 &m_renameAccept,
2753 &m_renameRes,
2754 this,
2755 s,
2756 wxPoint(x-4,y-4),
2757 wxSize(w+11,h+8));
2758 text->SetFocus();
2759 }
2760
2761 void wxGenericTreeCtrl::OnRenameTimer()
2762 {
2763 Edit( m_current );
2764 }
2765
2766 void wxGenericTreeCtrl::OnRenameAccept()
2767 {
2768 // TODO if the validator fails this causes a crash
2769 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
2770 le.m_item = (long) m_currentEdit;
2771 le.SetEventObject( this );
2772 le.m_label = m_renameRes;
2773 GetEventHandler()->ProcessEvent( le );
2774
2775 if (!le.IsAllowed()) return;
2776
2777 SetItemText( m_currentEdit, m_renameRes );
2778 }
2779
2780 void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
2781 {
2782 if ( !m_anchor ) return;
2783
2784 // we process left mouse up event (enables in-place edit), right down
2785 // (pass to the user code), left dbl click (activate item) and
2786 // dragging/moving events for items drag-and-drop
2787 if ( !(event.LeftDown() ||
2788 event.LeftUp() ||
2789 event.RightDown() ||
2790 event.LeftDClick() ||
2791 event.Dragging() ||
2792 ((event.Moving() || event.RightUp()) && m_isDragging)) )
2793 {
2794 event.Skip();
2795
2796 return;
2797 }
2798
2799 wxPoint pt = CalcUnscrolledPosition(event.GetPosition());
2800
2801 int flags = 0;
2802 wxGenericTreeItem *item = m_anchor->HitTest(pt, this, flags, 0);
2803
2804 if ( event.Dragging() && !m_isDragging )
2805 {
2806 if (m_dragCount == 0)
2807 m_dragStart = pt;
2808
2809 m_dragCount++;
2810
2811 if (m_dragCount != 3)
2812 {
2813 // wait until user drags a bit further...
2814 return;
2815 }
2816
2817 wxEventType command = event.RightIsDown()
2818 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2819 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
2820
2821 wxTreeEvent nevent( command, GetId() );
2822 nevent.m_item = (long) m_current;
2823 nevent.SetEventObject(this);
2824
2825 // by default the dragging is not supported, the user code must
2826 // explicitly allow the event for it to take place
2827 nevent.Veto();
2828
2829 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
2830 {
2831 // we're going to drag this item
2832 m_isDragging = TRUE;
2833
2834 // remember the old cursor because we will change it while
2835 // dragging
2836 m_oldCursor = m_cursor;
2837
2838 // in a single selection control, hide the selection temporarily
2839 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
2840 {
2841 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
2842
2843 if ( m_oldSelection )
2844 {
2845 m_oldSelection->SetHilight(FALSE);
2846 RefreshLine(m_oldSelection);
2847 }
2848 }
2849
2850 CaptureMouse();
2851 }
2852 }
2853 else if ( event.Moving() )
2854 {
2855 if ( item != m_dropTarget )
2856 {
2857 // unhighlight the previous drop target
2858 DrawDropEffect(m_dropTarget);
2859
2860 m_dropTarget = item;
2861
2862 // highlight the current drop target if any
2863 DrawDropEffect(m_dropTarget);
2864
2865 wxYieldIfNeeded();
2866 }
2867 }
2868 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
2869 {
2870 // erase the highlighting
2871 DrawDropEffect(m_dropTarget);
2872
2873 if ( m_oldSelection )
2874 {
2875 m_oldSelection->SetHilight(TRUE);
2876 RefreshLine(m_oldSelection);
2877 m_oldSelection = (wxGenericTreeItem *)NULL;
2878 }
2879
2880 // generate the drag end event
2881 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId());
2882
2883 event.m_item = (long) item;
2884 event.m_pointDrag = pt;
2885 event.SetEventObject(this);
2886
2887 (void)GetEventHandler()->ProcessEvent(event);
2888
2889 m_isDragging = FALSE;
2890 m_dropTarget = (wxGenericTreeItem *)NULL;
2891
2892 ReleaseMouse();
2893
2894 SetCursor(m_oldCursor);
2895
2896 wxYieldIfNeeded();
2897 }
2898 else
2899 {
2900 // here we process only the messages which happen on tree items
2901
2902 m_dragCount = 0;
2903
2904 if (item == NULL) return; /* we hit the blank area */
2905
2906 if ( event.RightDown() )
2907 {
2908 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
2909 nevent.m_item = (long) item;
2910 nevent.m_pointDrag = CalcScrolledPosition(pt);
2911 nevent.SetEventObject(this);
2912 GetEventHandler()->ProcessEvent(nevent);
2913 }
2914 else if ( event.LeftUp() )
2915 {
2916 if ( m_lastOnSame )
2917 {
2918 if ( (item == m_current) &&
2919 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
2920 HasFlag(wxTR_EDIT_LABELS) )
2921 {
2922 if ( m_renameTimer )
2923 {
2924 if ( m_renameTimer->IsRunning() )
2925 m_renameTimer->Stop();
2926 }
2927 else
2928 {
2929 m_renameTimer = new wxTreeRenameTimer( this );
2930 }
2931
2932 m_renameTimer->Start( wxTreeRenameTimer::DELAY, TRUE );
2933 }
2934
2935 m_lastOnSame = FALSE;
2936 }
2937 }
2938 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2939 {
2940 if ( event.LeftDown() )
2941 {
2942 m_lastOnSame = item == m_current;
2943 }
2944
2945 if ( flags & wxTREE_HITTEST_ONITEMBUTTON )
2946 {
2947 // only toggle the item for a single click, double click on
2948 // the button doesn't do anything (it toggles the item twice)
2949 if ( event.LeftDown() )
2950 {
2951 Toggle( item );
2952 }
2953
2954 // don't select the item if the button was clicked
2955 return;
2956 }
2957
2958 // how should the selection work for this event?
2959 bool is_multiple, extended_select, unselect_others;
2960 EventFlagsToSelType(GetWindowStyleFlag(),
2961 event.ShiftDown(),
2962 event.ControlDown(),
2963 is_multiple, extended_select, unselect_others);
2964
2965 SelectItem(item, unselect_others, extended_select);
2966
2967 // For some reason, Windows isn't recognizing a left double-click,
2968 // so we need to simulate it here. Allow 200 milliseconds for now.
2969 if ( event.LeftDClick() )
2970 {
2971 // double clicking should not start editing the item label
2972 if ( m_renameTimer )
2973 m_renameTimer->Stop();
2974
2975 m_lastOnSame = FALSE;
2976
2977 // send activate event first
2978 wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2979 nevent.m_item = (long) item;
2980 nevent.m_pointDrag = CalcScrolledPosition(pt);
2981 nevent.SetEventObject( this );
2982 if ( !GetEventHandler()->ProcessEvent( nevent ) )
2983 {
2984 // if the user code didn't process the activate event,
2985 // handle it ourselves by toggling the item when it is
2986 // double clicked
2987 if ( item->HasPlus() )
2988 {
2989 Toggle(item);
2990 }
2991 }
2992 }
2993 }
2994 }
2995 }
2996
2997 void wxGenericTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
2998 {
2999 /* after all changes have been done to the tree control,
3000 * we actually redraw the tree when everything is over */
3001
3002 if (!m_dirty) return;
3003
3004 m_dirty = FALSE;
3005
3006 CalculatePositions();
3007 Refresh();
3008 AdjustMyScrollbars();
3009 }
3010
3011 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
3012 {
3013 wxCoord text_w = 0;
3014 wxCoord text_h = 0;
3015
3016 wxTreeItemAttr *attr = item->GetAttributes();
3017 if ( attr && attr->HasFont() )
3018 dc.SetFont(attr->GetFont());
3019 else if ( item->IsBold() )
3020 dc.SetFont(m_boldFont);
3021
3022 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
3023 text_h+=2;
3024
3025 // restore normal font
3026 dc.SetFont( m_normalFont );
3027
3028 int image_h = 0;
3029 int image_w = 0;
3030 int image = item->GetCurrentImage();
3031 if ( image != NO_IMAGE )
3032 {
3033 if ( m_imageListNormal )
3034 {
3035 m_imageListNormal->GetSize( image, image_w, image_h );
3036 image_w += 4;
3037 }
3038 }
3039
3040 int total_h = (image_h > text_h) ? image_h : text_h;
3041
3042 if (total_h < 30)
3043 total_h += 2; // at least 2 pixels
3044 else
3045 total_h += total_h/10; // otherwise 10% extra spacing
3046
3047 item->SetHeight(total_h);
3048 if (total_h>m_lineHeight)
3049 m_lineHeight=total_h;
3050
3051 item->SetWidth(image_w+text_w+2);
3052 }
3053
3054 // -----------------------------------------------------------------------------
3055 // for developper : y is now the top of the level
3056 // not the middle of it !
3057 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
3058 {
3059 int x = level*m_indent;
3060 if (!HasFlag(wxTR_HIDE_ROOT))
3061 {
3062 x += m_indent;
3063 }
3064 else if (level == 0)
3065 {
3066 // a hidden root is not evaluated, but its
3067 // children are always calculated
3068 goto Recurse;
3069 }
3070
3071 CalculateSize( item, dc );
3072
3073 // set its position
3074 item->SetX( x+m_spacing );
3075 item->SetY( y );
3076 y += GetLineHeight(item);
3077
3078 if ( !item->IsExpanded() )
3079 {
3080 // we don't need to calculate collapsed branches
3081 return;
3082 }
3083
3084 Recurse:
3085 wxArrayGenericTreeItems& children = item->GetChildren();
3086 size_t n, count = children.Count();
3087 ++level;
3088 for (n = 0; n < count; ++n )
3089 CalculateLevel( children[n], dc, level, y ); // recurse
3090 }
3091
3092 void wxGenericTreeCtrl::CalculatePositions()
3093 {
3094 if ( !m_anchor ) return;
3095
3096 wxClientDC dc(this);
3097 PrepareDC( dc );
3098
3099 dc.SetFont( m_normalFont );
3100
3101 dc.SetPen( m_dottedPen );
3102 //if(GetImageList() == NULL)
3103 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
3104
3105 int y = 2;
3106 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
3107 }
3108
3109 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
3110 {
3111 if (m_dirty) return;
3112
3113 wxSize client = GetClientSize();
3114
3115 wxRect rect;
3116 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
3117 rect.width = client.x;
3118 rect.height = client.y;
3119
3120 Refresh(TRUE, &rect);
3121
3122 AdjustMyScrollbars();
3123 }
3124
3125 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
3126 {
3127 if (m_dirty) return;
3128
3129 wxRect rect;
3130 CalcScrolledPosition(0, item->GetY(), NULL, &rect.y);
3131 rect.width = GetClientSize().x;
3132 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
3133
3134 Refresh(TRUE, &rect);
3135 }
3136
3137 void wxGenericTreeCtrl::RefreshSelected()
3138 {
3139 // TODO: this is awfully inefficient, we should keep the list of all
3140 // selected items internally, should be much faster
3141 if ( m_anchor )
3142 RefreshSelectedUnder(m_anchor);
3143 }
3144
3145 void wxGenericTreeCtrl::RefreshSelectedUnder(wxGenericTreeItem *item)
3146 {
3147 if ( item->IsSelected() )
3148 RefreshLine(item);
3149
3150 const wxArrayGenericTreeItems& children = item->GetChildren();
3151 size_t count = children.GetCount();
3152 for ( size_t n = 0; n < count; n++ )
3153 {
3154 RefreshSelectedUnder(children[n]);
3155 }
3156 }
3157
3158 // ----------------------------------------------------------------------------
3159 // changing colours: we need to refresh the tree control
3160 // ----------------------------------------------------------------------------
3161
3162 bool wxGenericTreeCtrl::SetBackgroundColour(const wxColour& colour)
3163 {
3164 if ( !wxWindow::SetBackgroundColour(colour) )
3165 return FALSE;
3166
3167 Refresh();
3168
3169 return TRUE;
3170 }
3171
3172 bool wxGenericTreeCtrl::SetForegroundColour(const wxColour& colour)
3173 {
3174 if ( !wxWindow::SetForegroundColour(colour) )
3175 return FALSE;
3176
3177 Refresh();
3178
3179 return TRUE;
3180 }
3181
3182 #endif // wxUSE_TREECTRL