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