]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treectlg.cpp
1. some more tests in console
[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_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 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX ) );
667 // m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
668 m_dottedPen = wxPen( "grey", 0, 0 );
669
670 return TRUE;
671 }
672
673 wxGenericTreeCtrl::~wxGenericTreeCtrl()
674 {
675 wxDELETE( m_hilightBrush );
676
677 DeleteAllItems();
678
679 delete m_renameTimer;
680 if (m_ownsImageListNormal) delete m_imageListNormal;
681 if (m_ownsImageListState) delete m_imageListState;
682 }
683
684 // -----------------------------------------------------------------------------
685 // accessors
686 // -----------------------------------------------------------------------------
687
688 size_t wxGenericTreeCtrl::GetCount() const
689 {
690 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
691 }
692
693 void wxGenericTreeCtrl::SetIndent(unsigned int indent)
694 {
695 m_indent = indent;
696 m_dirty = TRUE;
697 }
698
699 void wxGenericTreeCtrl::SetSpacing(unsigned int spacing)
700 {
701 m_spacing = spacing;
702 m_dirty = TRUE;
703 }
704
705 size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
706 {
707 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
708
709 return ((wxGenericTreeItem*) item.m_pItem)->GetChildrenCount(recursively);
710 }
711
712 // -----------------------------------------------------------------------------
713 // functions to work with tree items
714 // -----------------------------------------------------------------------------
715
716 wxString wxGenericTreeCtrl::GetItemText(const wxTreeItemId& item) const
717 {
718 wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
719
720 return ((wxGenericTreeItem*) item.m_pItem)->GetText();
721 }
722
723 int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId& item,
724 wxTreeItemIcon which) const
725 {
726 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
727
728 return ((wxGenericTreeItem*) item.m_pItem)->GetImage(which);
729 }
730
731 wxTreeItemData *wxGenericTreeCtrl::GetItemData(const wxTreeItemId& item) const
732 {
733 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
734
735 return ((wxGenericTreeItem*) item.m_pItem)->GetData();
736 }
737
738 void wxGenericTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
739 {
740 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
741
742 wxClientDC dc(this);
743 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
744 pItem->SetText(text);
745 CalculateSize(pItem, dc);
746 RefreshLine(pItem);
747 }
748
749 void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId& item,
750 int image,
751 wxTreeItemIcon which)
752 {
753 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
754
755 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
756 pItem->SetImage(image, which);
757
758 wxClientDC dc(this);
759 CalculateSize(pItem, dc);
760 RefreshLine(pItem);
761 }
762
763 void wxGenericTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
764 {
765 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
766
767 ((wxGenericTreeItem*) item.m_pItem)->SetData(data);
768 }
769
770 void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
771 {
772 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
773
774 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
775 pItem->SetHasPlus(has);
776 RefreshLine(pItem);
777 }
778
779 void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
780 {
781 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
782
783 // avoid redrawing the tree if no real change
784 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
785 if ( pItem->IsBold() != bold )
786 {
787 pItem->SetBold(bold);
788 RefreshLine(pItem);
789 }
790 }
791
792 void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
793 const wxColour& col)
794 {
795 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
796
797 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
798 pItem->Attr().SetTextColour(col);
799 RefreshLine(pItem);
800 }
801
802 void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
803 const wxColour& col)
804 {
805 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
806
807 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
808 pItem->Attr().SetBackgroundColour(col);
809 RefreshLine(pItem);
810 }
811
812 void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
813 {
814 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
815
816 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
817 pItem->Attr().SetFont(font);
818 RefreshLine(pItem);
819 }
820
821 // -----------------------------------------------------------------------------
822 // item status inquiries
823 // -----------------------------------------------------------------------------
824
825 bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId& item) const
826 {
827 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
828
829 // An item is only visible if it's not a descendant of a collapsed item
830 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
831 wxGenericTreeItem* parent = pItem->GetParent();
832 while (parent)
833 {
834 if (!parent->IsExpanded())
835 return FALSE;
836 parent = parent->GetParent();
837 }
838
839 int startX, startY;
840 GetViewStart(& startX, & startY);
841
842 wxSize clientSize = GetClientSize();
843
844 wxRect rect;
845 if (!GetBoundingRect(item, rect))
846 return FALSE;
847 if (rect.GetWidth() == 0 || rect.GetHeight() == 0)
848 return FALSE;
849 if (rect.GetBottom() < 0 || rect.GetTop() > clientSize.y)
850 return FALSE;
851 if (rect.GetRight() < 0 || rect.GetLeft() > clientSize.x)
852 return FALSE;
853
854 return TRUE;
855 }
856
857 bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
858 {
859 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
860
861 return !((wxGenericTreeItem*) item.m_pItem)->GetChildren().IsEmpty();
862 }
863
864 bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId& item) const
865 {
866 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
867
868 return ((wxGenericTreeItem*) item.m_pItem)->IsExpanded();
869 }
870
871 bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId& item) const
872 {
873 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
874
875 return ((wxGenericTreeItem*) item.m_pItem)->IsSelected();
876 }
877
878 bool wxGenericTreeCtrl::IsBold(const wxTreeItemId& item) const
879 {
880 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
881
882 return ((wxGenericTreeItem*) item.m_pItem)->IsBold();
883 }
884
885 // -----------------------------------------------------------------------------
886 // navigation
887 // -----------------------------------------------------------------------------
888
889 wxTreeItemId wxGenericTreeCtrl::GetParent(const wxTreeItemId& item) const
890 {
891 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
892
893 return ((wxGenericTreeItem*) item.m_pItem)->GetParent();
894 }
895
896 wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
897 {
898 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
899
900 cookie = 0;
901 return GetNextChild(item, cookie);
902 }
903
904 wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
905 {
906 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
907
908 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
909 if ( (size_t)cookie < children.Count() )
910 {
911 return children.Item((size_t)cookie++);
912 }
913 else
914 {
915 // there are no more of them
916 return wxTreeItemId();
917 }
918 }
919
920 wxTreeItemId wxGenericTreeCtrl::GetLastChild(const wxTreeItemId& item) const
921 {
922 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
923
924 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
925 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
926 }
927
928 wxTreeItemId wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
929 {
930 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
931
932 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
933 wxGenericTreeItem *parent = i->GetParent();
934 if ( parent == NULL )
935 {
936 // root item doesn't have any siblings
937 return wxTreeItemId();
938 }
939
940 wxArrayGenericTreeItems& siblings = parent->GetChildren();
941 int index = siblings.Index(i);
942 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
943
944 size_t n = (size_t)(index + 1);
945 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
946 }
947
948 wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
949 {
950 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
951
952 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
953 wxGenericTreeItem *parent = i->GetParent();
954 if ( parent == NULL )
955 {
956 // root item doesn't have any siblings
957 return wxTreeItemId();
958 }
959
960 wxArrayGenericTreeItems& siblings = parent->GetChildren();
961 int index = siblings.Index(i);
962 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
963
964 return index == 0 ? wxTreeItemId()
965 : wxTreeItemId(siblings[(size_t)(index - 1)]);
966 }
967
968 // Only for internal use right now, but should probably be public
969 wxTreeItemId wxGenericTreeCtrl::GetNext(const wxTreeItemId& item) const
970 {
971 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
972
973 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
974
975 // First see if there are any children.
976 wxArrayGenericTreeItems& children = i->GetChildren();
977 if (children.GetCount() > 0)
978 {
979 return children.Item(0);
980 }
981 else
982 {
983 // Try a sibling of this or ancestor instead
984 wxTreeItemId p = item;
985 wxTreeItemId toFind;
986 do
987 {
988 toFind = GetNextSibling(p);
989 p = GetParent(p);
990 } while (p.IsOk() && !toFind.IsOk());
991 return toFind;
992 }
993 }
994
995 wxTreeItemId wxGenericTreeCtrl::GetPrev(const wxTreeItemId& item) const
996 {
997 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
998
999 wxFAIL_MSG(wxT("not implemented"));
1000
1001 return wxTreeItemId();
1002 }
1003
1004 wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const
1005 {
1006 wxTreeItemId id = GetRootItem();
1007 if (!id.IsOk())
1008 return id;
1009
1010 do
1011 {
1012 if (IsVisible(id))
1013 return id;
1014 id = GetNext(id);
1015 } while (id.IsOk());
1016
1017 return wxTreeItemId();
1018 }
1019
1020 wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
1021 {
1022 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1023
1024 wxTreeItemId id = item;
1025 while (id.IsOk())
1026 {
1027 id = GetNext(id);
1028
1029 if (id.IsOk() && IsVisible(id))
1030 return id;
1031 }
1032 return wxTreeItemId();
1033 }
1034
1035 wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1036 {
1037 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1038
1039 wxFAIL_MSG(wxT("not implemented"));
1040
1041 return wxTreeItemId();
1042 }
1043
1044 // -----------------------------------------------------------------------------
1045 // operations
1046 // -----------------------------------------------------------------------------
1047
1048 wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
1049 size_t previous,
1050 const wxString& text,
1051 int image, int selImage,
1052 wxTreeItemData *data)
1053 {
1054 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1055 if ( !parent )
1056 {
1057 // should we give a warning here?
1058 return AddRoot(text, image, selImage, data);
1059 }
1060
1061 wxClientDC dc(this);
1062 wxGenericTreeItem *item =
1063 new wxGenericTreeItem( parent, text, dc, image, selImage, data );
1064
1065 if ( data != NULL )
1066 {
1067 data->m_pItem = (long) item;
1068 }
1069
1070 parent->Insert( item, previous );
1071
1072 m_dirty = TRUE;
1073
1074 return item;
1075 }
1076
1077 wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
1078 int image, int selImage,
1079 wxTreeItemData *data)
1080 {
1081 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
1082
1083 wxClientDC dc(this);
1084 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
1085 image, selImage, data);
1086 if ( data != NULL )
1087 {
1088 data->m_pItem = (long) m_anchor;
1089 }
1090
1091 if (!HasFlag(wxTR_MULTIPLE))
1092 {
1093 m_current = m_key_current = m_anchor;
1094 m_current->SetHilight( TRUE );
1095 }
1096
1097 m_dirty = TRUE;
1098
1099 return m_anchor;
1100 }
1101
1102 wxTreeItemId wxGenericTreeCtrl::PrependItem(const wxTreeItemId& parent,
1103 const wxString& text,
1104 int image, int selImage,
1105 wxTreeItemData *data)
1106 {
1107 return DoInsertItem(parent, 0u, text, image, selImage, data);
1108 }
1109
1110 wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
1111 const wxTreeItemId& idPrevious,
1112 const wxString& text,
1113 int image, int selImage,
1114 wxTreeItemData *data)
1115 {
1116 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1117 if ( !parent )
1118 {
1119 // should we give a warning here?
1120 return AddRoot(text, image, selImage, data);
1121 }
1122
1123 int index = parent->GetChildren().Index((wxGenericTreeItem*) idPrevious.m_pItem);
1124 wxASSERT_MSG( index != wxNOT_FOUND,
1125 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1126
1127 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1128 }
1129
1130 wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
1131 size_t before,
1132 const wxString& text,
1133 int image, int selImage,
1134 wxTreeItemData *data)
1135 {
1136 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1137 if ( !parent )
1138 {
1139 // should we give a warning here?
1140 return AddRoot(text, image, selImage, data);
1141 }
1142
1143 return DoInsertItem(parentId, before, text, image, selImage, data);
1144 }
1145
1146 wxTreeItemId wxGenericTreeCtrl::AppendItem(const wxTreeItemId& parentId,
1147 const wxString& text,
1148 int image, int selImage,
1149 wxTreeItemData *data)
1150 {
1151 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1152 if ( !parent )
1153 {
1154 // should we give a warning here?
1155 return AddRoot(text, image, selImage, data);
1156 }
1157
1158 return DoInsertItem( parent, parent->GetChildren().Count(), text,
1159 image, selImage, data);
1160 }
1161
1162 void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
1163 {
1164 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
1165 event.m_item = (long) item;
1166 event.SetEventObject( this );
1167 ProcessEvent( event );
1168 }
1169
1170 void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
1171 {
1172 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1173 item->DeleteChildren(this);
1174
1175 m_dirty = TRUE;
1176 }
1177
1178 void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
1179 {
1180 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1181
1182 // don't stay with invalid m_key_current or we will crash in the next call
1183 // to OnChar()
1184 bool changeKeyCurrent = FALSE;
1185 wxGenericTreeItem *itemKey = m_key_current;
1186 while ( itemKey && !changeKeyCurrent )
1187 {
1188 if ( itemKey == item )
1189 {
1190 // m_key_current is a descendant of the item being deleted
1191 changeKeyCurrent = TRUE;
1192 }
1193 else
1194 {
1195 itemKey = itemKey->GetParent();
1196 }
1197 }
1198
1199 wxGenericTreeItem *parent = item->GetParent();
1200 if ( parent )
1201 {
1202 parent->GetChildren().Remove( item ); // remove by value
1203 }
1204
1205 if ( changeKeyCurrent )
1206 {
1207 // may be NULL or not
1208 m_key_current = parent;
1209 }
1210
1211 item->DeleteChildren(this);
1212 SendDeleteEvent(item);
1213 delete item;
1214
1215 m_dirty = TRUE;
1216 }
1217
1218 void wxGenericTreeCtrl::DeleteAllItems()
1219 {
1220 if ( m_anchor )
1221 {
1222 m_anchor->DeleteChildren(this);
1223 delete m_anchor;
1224
1225 m_anchor = NULL;
1226
1227 m_dirty = TRUE;
1228 }
1229 }
1230
1231 void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId)
1232 {
1233 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1234
1235 wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") );
1236
1237 if ( !item->HasPlus() )
1238 return;
1239
1240 if ( item->IsExpanded() )
1241 return;
1242
1243 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
1244 event.m_item = (long) item;
1245 event.SetEventObject( this );
1246
1247 if ( ProcessEvent( event ) && !event.IsAllowed() )
1248 {
1249 // cancelled by program
1250 return;
1251 }
1252
1253 item->Expand();
1254 CalculatePositions();
1255
1256 RefreshSubtree(item);
1257
1258 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1259 ProcessEvent( event );
1260 }
1261
1262 void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId& item)
1263 {
1264 Expand(item);
1265 if ( IsExpanded(item) )
1266 {
1267 long cookie;
1268 wxTreeItemId child = GetFirstChild(item, cookie);
1269 while ( child.IsOk() )
1270 {
1271 ExpandAll(child);
1272
1273 child = GetNextChild(item, cookie);
1274 }
1275 }
1276 }
1277
1278 void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
1279 {
1280 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1281
1282 if ( !item->IsExpanded() )
1283 return;
1284
1285 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
1286 event.m_item = (long) item;
1287 event.SetEventObject( this );
1288 if ( ProcessEvent( event ) && !event.IsAllowed() )
1289 {
1290 // cancelled by program
1291 return;
1292 }
1293
1294 item->Collapse();
1295
1296 wxArrayGenericTreeItems& children = item->GetChildren();
1297 size_t count = children.Count();
1298 for ( size_t n = 0; n < count; n++ )
1299 {
1300 Collapse(children[n]);
1301 }
1302
1303 CalculatePositions();
1304
1305 RefreshSubtree(item);
1306
1307 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1308 ProcessEvent( event );
1309 }
1310
1311 void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1312 {
1313 Collapse(item);
1314 DeleteChildren(item);
1315 }
1316
1317 void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId)
1318 {
1319 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1320
1321 if (item->IsExpanded())
1322 Collapse(itemId);
1323 else
1324 Expand(itemId);
1325 }
1326
1327 void wxGenericTreeCtrl::Unselect()
1328 {
1329 if (m_current)
1330 {
1331 m_current->SetHilight( FALSE );
1332 RefreshLine( m_current );
1333 }
1334 }
1335
1336 void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
1337 {
1338 if (item->IsSelected())
1339 {
1340 item->SetHilight(FALSE);
1341 RefreshLine(item);
1342 }
1343
1344 if (item->HasChildren())
1345 {
1346 wxArrayGenericTreeItems& children = item->GetChildren();
1347 size_t count = children.Count();
1348 for ( size_t n = 0; n < count; ++n )
1349 {
1350 UnselectAllChildren(children[n]);
1351 }
1352 }
1353 }
1354
1355 void wxGenericTreeCtrl::UnselectAll()
1356 {
1357 UnselectAllChildren((wxGenericTreeItem*) GetRootItem().m_pItem);
1358 }
1359
1360 // Recursive function !
1361 // To stop we must have crt_item<last_item
1362 // Algorithm :
1363 // Tag all next children, when no more children,
1364 // Move to parent (not to tag)
1365 // Keep going... if we found last_item, we stop.
1366 bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1367 {
1368 wxGenericTreeItem *parent = crt_item->GetParent();
1369
1370 if (parent == NULL) // This is root item
1371 return TagAllChildrenUntilLast(crt_item, last_item, select);
1372
1373 wxArrayGenericTreeItems& children = parent->GetChildren();
1374 int index = children.Index(crt_item);
1375 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1376
1377 size_t count = children.Count();
1378 for (size_t n=(size_t)(index+1); n<count; ++n)
1379 {
1380 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1381 }
1382
1383 return TagNextChildren(parent, last_item, select);
1384 }
1385
1386 bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1387 {
1388 crt_item->SetHilight(select);
1389 RefreshLine(crt_item);
1390
1391 if (crt_item==last_item)
1392 return TRUE;
1393
1394 if (crt_item->HasChildren())
1395 {
1396 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1397 size_t count = children.Count();
1398 for ( size_t n = 0; n < count; ++n )
1399 {
1400 if (TagAllChildrenUntilLast(children[n], last_item, select))
1401 return TRUE;
1402 }
1403 }
1404
1405 return FALSE;
1406 }
1407
1408 void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
1409 {
1410 // item2 is not necessary after item1
1411 wxGenericTreeItem *first=NULL, *last=NULL;
1412
1413 // choice first' and 'last' between item1 and item2
1414 if (item1->GetY()<item2->GetY())
1415 {
1416 first=item1;
1417 last=item2;
1418 }
1419 else
1420 {
1421 first=item2;
1422 last=item1;
1423 }
1424
1425 bool select = m_current->IsSelected();
1426
1427 if ( TagAllChildrenUntilLast(first,last,select) )
1428 return;
1429
1430 TagNextChildren(first,last,select);
1431 }
1432
1433 void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId,
1434 bool unselect_others,
1435 bool extended_select)
1436 {
1437 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
1438
1439 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
1440 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1441
1442 //wxCHECK_RET( ( (!unselect_others) && is_single),
1443 // wxT("this is a single selection tree") );
1444
1445 // to keep going anyhow !!!
1446 if (is_single)
1447 {
1448 if (item->IsSelected())
1449 return; // nothing to do
1450 unselect_others = TRUE;
1451 extended_select = FALSE;
1452 }
1453 else if ( unselect_others && item->IsSelected() )
1454 {
1455 // selection change if there is more than one item currently selected
1456 wxArrayTreeItemIds selected_items;
1457 if ( GetSelections(selected_items) == 1 )
1458 return;
1459 }
1460
1461 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
1462 event.m_item = (long) item;
1463 event.m_itemOld = (long) m_current;
1464 event.SetEventObject( this );
1465 // TODO : Here we don't send any selection mode yet !
1466
1467 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
1468 return;
1469
1470 // ctrl press
1471 if (unselect_others)
1472 {
1473 if (is_single) Unselect(); // to speed up thing
1474 else UnselectAll();
1475 }
1476
1477 // shift press
1478 if (extended_select)
1479 {
1480 if ( !m_current )
1481 {
1482 m_current =
1483 m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
1484 }
1485
1486 // don't change the mark (m_current)
1487 SelectItemRange(m_current, item);
1488 }
1489 else
1490 {
1491 bool select=TRUE; // the default
1492
1493 // Check if we need to toggle hilight (ctrl mode)
1494 if (!unselect_others)
1495 select=!item->IsSelected();
1496
1497 m_current = m_key_current = item;
1498 m_current->SetHilight(select);
1499 RefreshLine( m_current );
1500 }
1501
1502 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1503 GetEventHandler()->ProcessEvent( event );
1504 }
1505
1506 void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
1507 wxArrayTreeItemIds &array) const
1508 {
1509 if ( item->IsSelected() )
1510 array.Add(wxTreeItemId(item));
1511
1512 if ( item->HasChildren() )
1513 {
1514 wxArrayGenericTreeItems& children = item->GetChildren();
1515 size_t count = children.GetCount();
1516 for ( size_t n = 0; n < count; ++n )
1517 FillArray(children[n], array);
1518 }
1519 }
1520
1521 size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
1522 {
1523 array.Empty();
1524 wxTreeItemId idRoot = GetRootItem();
1525 if ( idRoot.IsOk() )
1526 {
1527 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
1528 }
1529 //else: the tree is empty, so no selections
1530
1531 return array.Count();
1532 }
1533
1534 void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1535 {
1536 if (!item.IsOk()) return;
1537
1538 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
1539
1540 // first expand all parent branches
1541 wxGenericTreeItem *parent = gitem->GetParent();
1542 while ( parent )
1543 {
1544 Expand(parent);
1545 parent = parent->GetParent();
1546 }
1547
1548 //if (parent) CalculatePositions();
1549
1550 ScrollTo(item);
1551 }
1552
1553 void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
1554 {
1555 if (!item.IsOk()) return;
1556
1557 // We have to call this here because the label in
1558 // question might just have been added and no screen
1559 // update taken place.
1560 if (m_dirty) wxYield();
1561
1562 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
1563
1564 // now scroll to the item
1565 int item_y = gitem->GetY();
1566
1567 int start_x = 0;
1568 int start_y = 0;
1569 ViewStart( &start_x, &start_y );
1570 start_y *= PIXELS_PER_UNIT;
1571
1572 int client_h = 0;
1573 int client_w = 0;
1574 GetClientSize( &client_w, &client_h );
1575
1576 if (item_y < start_y+3)
1577 {
1578 // going down
1579 int x = 0;
1580 int y = 0;
1581 m_anchor->GetSize( x, y, this );
1582 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1583 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1584 int x_pos = GetScrollPos( wxHORIZONTAL );
1585 // Item should appear at top
1586 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
1587 }
1588 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
1589 {
1590 // going up
1591 int x = 0;
1592 int y = 0;
1593 m_anchor->GetSize( x, y, this );
1594 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1595 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1596 item_y += PIXELS_PER_UNIT+2;
1597 int x_pos = GetScrollPos( wxHORIZONTAL );
1598 // Item should appear at bottom
1599 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 );
1600 }
1601 }
1602
1603 // FIXME: tree sorting functions are not reentrant and not MT-safe!
1604 static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
1605
1606 static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
1607 wxGenericTreeItem **item2)
1608 {
1609 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1610
1611 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
1612 }
1613
1614 int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1615 const wxTreeItemId& item2)
1616 {
1617 return wxStrcmp(GetItemText(item1), GetItemText(item2));
1618 }
1619
1620 void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
1621 {
1622 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
1623
1624 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1625
1626 wxCHECK_RET( !s_treeBeingSorted,
1627 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1628
1629 wxArrayGenericTreeItems& children = item->GetChildren();
1630 if ( children.Count() > 1 )
1631 {
1632 s_treeBeingSorted = this;
1633 children.Sort(tree_ctrl_compare_func);
1634 s_treeBeingSorted = NULL;
1635
1636 m_dirty = TRUE;
1637 }
1638 //else: don't make the tree dirty as nothing changed
1639 }
1640
1641 wxImageList *wxGenericTreeCtrl::GetImageList() const
1642 {
1643 return m_imageListNormal;
1644 }
1645
1646 wxImageList *wxGenericTreeCtrl::GetStateImageList() const
1647 {
1648 return m_imageListState;
1649 }
1650
1651 void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
1652 {
1653 if (m_ownsImageListNormal) delete m_imageListNormal;
1654
1655 m_imageListNormal = imageList;
1656 m_ownsImageListNormal = FALSE;
1657
1658 if ( !m_imageListNormal )
1659 return;
1660
1661 // Calculate a m_lineHeight value from the image sizes.
1662 // May be toggle off. Then wxGenericTreeCtrl will spread when
1663 // necessary (which might look ugly).
1664 wxClientDC dc(this);
1665 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1666 int width = 0, height = 0,
1667 n = m_imageListNormal->GetImageCount();
1668
1669 for (int i = 0; i < n ; i++)
1670 {
1671 m_imageListNormal->GetSize(i, width, height);
1672 if (height > m_lineHeight) m_lineHeight = height;
1673 }
1674
1675 if (m_lineHeight < 40)
1676 m_lineHeight += 2; // at least 2 pixels
1677 else
1678 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
1679 }
1680
1681 void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
1682 {
1683 if (m_ownsImageListState) delete m_imageListState;
1684 m_imageListState = imageList;
1685 m_ownsImageListState = FALSE;
1686 }
1687
1688 void wxGenericTreeCtrl::AssignImageList(wxImageList *imageList)
1689 {
1690 SetImageList(imageList);
1691 m_ownsImageListNormal = TRUE;
1692 }
1693
1694 void wxGenericTreeCtrl::AssignStateImageList(wxImageList *imageList)
1695 {
1696 SetStateImageList(imageList);
1697 m_ownsImageListState = TRUE;
1698 }
1699
1700 // -----------------------------------------------------------------------------
1701 // helpers
1702 // -----------------------------------------------------------------------------
1703
1704 void wxGenericTreeCtrl::AdjustMyScrollbars()
1705 {
1706 if (m_anchor)
1707 {
1708 int x = 0;
1709 int y = 0;
1710 m_anchor->GetSize( x, y, this );
1711 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1712 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1713 int x_pos = GetScrollPos( wxHORIZONTAL );
1714 int y_pos = GetScrollPos( wxVERTICAL );
1715 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
1716 }
1717 else
1718 {
1719 SetScrollbars( 0, 0, 0, 0 );
1720 }
1721 }
1722
1723 int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
1724 {
1725 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
1726 return item->GetHeight();
1727 else
1728 return m_lineHeight;
1729 }
1730
1731 void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
1732 {
1733 wxTreeItemAttr *attr = item->GetAttributes();
1734 if ( attr && attr->HasFont() )
1735 dc.SetFont(attr->GetFont());
1736 else if (item->IsBold())
1737 dc.SetFont(m_boldFont);
1738
1739 long text_w = 0;
1740 long text_h = 0;
1741 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
1742
1743 int image_h = 0;
1744 int image_w = 0;
1745 int image = item->GetCurrentImage();
1746 if ( image != NO_IMAGE )
1747 {
1748 if ( m_imageListNormal )
1749 {
1750 m_imageListNormal->GetSize( image, image_w, image_h );
1751 image_w += 4;
1752 }
1753 else
1754 {
1755 image = NO_IMAGE;
1756 }
1757 }
1758
1759 int total_h = GetLineHeight(item);
1760
1761 if (item->IsSelected())
1762 dc.SetBrush(*m_hilightBrush);
1763 else
1764 {
1765 wxColour colBg;
1766 if ( attr && attr->HasBackgroundColour() )
1767 colBg = attr->GetBackgroundColour();
1768 else
1769 colBg = m_backgroundColour;
1770 dc.SetBrush(wxBrush(colBg, wxSOLID));
1771 }
1772
1773 dc.DrawRectangle( item->GetX()-2, item->GetY(), item->GetWidth()+2, total_h );
1774
1775 if ( image != NO_IMAGE )
1776 {
1777 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
1778 m_imageListNormal->Draw( image, dc,
1779 item->GetX(),
1780 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
1781 wxIMAGELIST_DRAW_TRANSPARENT );
1782 dc.DestroyClippingRegion();
1783 }
1784
1785 dc.SetBackgroundMode(wxTRANSPARENT);
1786 int extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
1787 dc.DrawText( item->GetText(),
1788 (wxCoord)(image_w + item->GetX()),
1789 (wxCoord)(item->GetY() + extraH));
1790
1791 // restore normal font
1792 dc.SetFont( m_normalFont );
1793 }
1794
1795 // Now y stands for the top of the item, whereas it used to stand for middle !
1796 void wxGenericTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
1797 {
1798 int horizX = level*m_indent;
1799
1800 item->SetX( horizX+m_indent+m_spacing );
1801 item->SetY( y );
1802
1803 int oldY = y;
1804 y+=GetLineHeight(item)/2;
1805
1806 item->SetCross( horizX+m_indent, y );
1807
1808 int exposed_x = dc.LogicalToDeviceX( 0 );
1809 int exposed_y = dc.LogicalToDeviceY( item->GetY() );
1810
1811 bool drawLines = ((GetWindowStyle() & wxTR_NO_LINES) == 0);
1812
1813 if (IsExposed( exposed_x, exposed_y, 10000, GetLineHeight(item) )) // 10000 = very much
1814 {
1815 int startX = horizX;
1816 int endX = horizX + (m_indent-5);
1817
1818 // if (!item->HasChildren()) endX += (m_indent+5);
1819 if (!item->HasChildren()) endX += 20;
1820
1821 if (drawLines)
1822 dc.DrawLine( startX, y, endX, y );
1823
1824 if (item->HasPlus())
1825 {
1826 if (drawLines)
1827 dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y );
1828 dc.SetPen( *wxGREY_PEN );
1829 dc.SetBrush( *wxWHITE_BRUSH );
1830 dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 );
1831
1832 dc.SetPen( *wxBLACK_PEN );
1833 dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y );
1834 if (!item->IsExpanded())
1835 dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 );
1836
1837 dc.SetPen( m_dottedPen );
1838 }
1839
1840 wxPen *pen = wxTRANSPARENT_PEN;
1841 wxColour colText;
1842
1843 if ( item->IsSelected() )
1844 {
1845 colText = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
1846
1847 if ( m_hasFocus )
1848 pen = wxBLACK_PEN;
1849
1850 }
1851 else
1852 {
1853 wxTreeItemAttr *attr = item->GetAttributes();
1854 if ( attr && attr->HasTextColour() )
1855 colText = attr->GetTextColour();
1856 else
1857 colText = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOWTEXT );
1858 }
1859
1860 // prepare to draw
1861 dc.SetTextForeground(colText);
1862 dc.SetPen(*pen);
1863
1864 // draw
1865 PaintItem(item, dc);
1866
1867 // restore DC objects
1868 dc.SetBrush( *wxWHITE_BRUSH );
1869 dc.SetPen( m_dottedPen );
1870 dc.SetTextForeground( *wxBLACK );
1871 }
1872
1873 y = oldY+GetLineHeight(item);
1874
1875 if (item->IsExpanded())
1876 {
1877 oldY+=GetLineHeight(item)/2;
1878 int semiOldY=0;
1879
1880 wxArrayGenericTreeItems& children = item->GetChildren();
1881 size_t n, count = children.Count();
1882 for ( n = 0; n < count; ++n )
1883 {
1884 semiOldY=y;
1885 PaintLevel( children[n], dc, level+1, y );
1886 }
1887
1888 // it may happen that the item is expanded but has no items (when you
1889 // delete all its children for example) - don't draw the vertical line
1890 // in this case
1891 if (count > 0)
1892 {
1893 semiOldY+=GetLineHeight(children[--n])/2;
1894 if (drawLines)
1895 dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY );
1896 }
1897 }
1898 }
1899
1900 void wxGenericTreeCtrl::DrawDropEffect(wxGenericTreeItem *item)
1901 {
1902 if ( item )
1903 {
1904 if ( item->HasPlus() )
1905 {
1906 // it's a folder, indicate it by a border
1907 DrawBorder(item);
1908 }
1909 else
1910 {
1911 // draw a line under the drop target because the item will be
1912 // dropped there
1913 DrawLine(item, TRUE /* below */);
1914 }
1915
1916 SetCursor(wxCURSOR_BULLSEYE);
1917 }
1918 else
1919 {
1920 // can't drop here
1921 SetCursor(wxCURSOR_NO_ENTRY);
1922 }
1923 }
1924
1925 void wxGenericTreeCtrl::DrawBorder(const wxTreeItemId &item)
1926 {
1927 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
1928
1929 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1930
1931 wxClientDC dc(this);
1932 PrepareDC( dc );
1933 dc.SetLogicalFunction(wxINVERT);
1934 dc.SetBrush(*wxTRANSPARENT_BRUSH);
1935
1936 int w = i->GetWidth() + 2;
1937 int h = GetLineHeight(i) + 2;
1938
1939 dc.DrawRectangle( i->GetX() - 1, i->GetY() - 1, w, h);
1940 }
1941
1942 void wxGenericTreeCtrl::DrawLine(const wxTreeItemId &item, bool below)
1943 {
1944 wxCHECK_RET( item.IsOk(), _T("invalid item in wxGenericTreeCtrl::DrawLine") );
1945
1946 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
1947
1948 wxClientDC dc(this);
1949 PrepareDC( dc );
1950 dc.SetLogicalFunction(wxINVERT);
1951
1952 int x = i->GetX(),
1953 y = i->GetY();
1954 if ( below )
1955 {
1956 y += GetLineHeight(i) - 1;
1957 }
1958
1959 dc.DrawLine( x, y, x + i->GetWidth(), y);
1960 }
1961
1962 // -----------------------------------------------------------------------------
1963 // wxWindows callbacks
1964 // -----------------------------------------------------------------------------
1965
1966 void wxGenericTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
1967 {
1968 wxPaintDC dc(this);
1969 PrepareDC( dc );
1970
1971 if ( !m_anchor)
1972 return;
1973
1974 dc.SetFont( m_normalFont );
1975 dc.SetPen( m_dottedPen );
1976
1977 // this is now done dynamically
1978 //if(GetImageList() == NULL)
1979 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
1980
1981 int y = 2;
1982 PaintLevel( m_anchor, dc, 0, y );
1983 }
1984
1985 void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1986 {
1987 m_hasFocus = TRUE;
1988
1989 if (m_current) RefreshLine( m_current );
1990 }
1991
1992 void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
1993 {
1994 m_hasFocus = FALSE;
1995
1996 if (m_current) RefreshLine( m_current );
1997 }
1998
1999 void wxGenericTreeCtrl::OnChar( wxKeyEvent &event )
2000 {
2001 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
2002 te.m_code = (int)event.KeyCode();
2003 te.SetEventObject( this );
2004 GetEventHandler()->ProcessEvent( te );
2005
2006 if ( (m_current == 0) || (m_key_current == 0) )
2007 {
2008 event.Skip();
2009 return;
2010 }
2011
2012 // how should the selection work for this event?
2013 bool is_multiple, extended_select, unselect_others;
2014 EventFlagsToSelType(GetWindowStyleFlag(),
2015 event.ShiftDown(),
2016 event.ControlDown(),
2017 is_multiple, extended_select, unselect_others);
2018
2019 // + : Expand
2020 // - : Collaspe
2021 // * : Expand all/Collapse all
2022 // ' ' | return : activate
2023 // up : go up (not last children!)
2024 // down : go down
2025 // left : go to parent
2026 // right : open if parent and go next
2027 // home : go to root
2028 // end : go to last item without opening parents
2029 switch (event.KeyCode())
2030 {
2031 case '+':
2032 case WXK_ADD:
2033 if (m_current->HasPlus() && !IsExpanded(m_current))
2034 {
2035 Expand(m_current);
2036 }
2037 break;
2038
2039 case '*':
2040 case WXK_MULTIPLY:
2041 if ( !IsExpanded(m_current) )
2042 {
2043 // expand all
2044 ExpandAll(m_current);
2045 break;
2046 }
2047 //else: fall through to Collapse() it
2048
2049 case '-':
2050 case WXK_SUBTRACT:
2051 if (IsExpanded(m_current))
2052 {
2053 Collapse(m_current);
2054 }
2055 break;
2056
2057 case ' ':
2058 case WXK_RETURN:
2059 {
2060 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2061 event.m_item = (long) m_current;
2062 event.m_code = 0;
2063 event.SetEventObject( this );
2064 GetEventHandler()->ProcessEvent( event );
2065 }
2066 break;
2067
2068 // up goes to the previous sibling or to the last of its children if
2069 // it's expanded
2070 case WXK_UP:
2071 {
2072 wxTreeItemId prev = GetPrevSibling( m_key_current );
2073 if (!prev)
2074 {
2075 prev = GetParent( m_key_current );
2076 if (prev)
2077 {
2078 long cockie = 0;
2079 wxTreeItemId current = m_key_current;
2080 if (current == GetFirstChild( prev, cockie ))
2081 {
2082 // otherwise we return to where we came from
2083 SelectItem( prev, unselect_others, extended_select );
2084 m_key_current= (wxGenericTreeItem*) prev.m_pItem;
2085 EnsureVisible( prev );
2086 break;
2087 }
2088 }
2089 }
2090 if (prev)
2091 {
2092 while ( IsExpanded(prev) && HasChildren(prev) )
2093 {
2094 wxTreeItemId child = GetLastChild(prev);
2095 if ( child )
2096 {
2097 prev = child;
2098 }
2099 }
2100
2101 SelectItem( prev, unselect_others, extended_select );
2102 m_key_current=(wxGenericTreeItem*) prev.m_pItem;
2103 EnsureVisible( prev );
2104 }
2105 }
2106 break;
2107
2108 // left arrow goes to the parent
2109 case WXK_LEFT:
2110 {
2111 wxTreeItemId prev = GetParent( m_current );
2112 if (prev)
2113 {
2114 EnsureVisible( prev );
2115 SelectItem( prev, unselect_others, extended_select );
2116 }
2117 }
2118 break;
2119
2120 case WXK_RIGHT:
2121 // this works the same as the down arrow except that we also expand the
2122 // item if it wasn't expanded yet
2123 Expand(m_current);
2124 // fall through
2125
2126 case WXK_DOWN:
2127 {
2128 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
2129 {
2130 long cookie = 0;
2131 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
2132 SelectItem( child, unselect_others, extended_select );
2133 m_key_current=(wxGenericTreeItem*) child.m_pItem;
2134 EnsureVisible( child );
2135 }
2136 else
2137 {
2138 wxTreeItemId next = GetNextSibling( m_key_current );
2139 if (!next)
2140 {
2141 wxTreeItemId current = m_key_current;
2142 while (current && !next)
2143 {
2144 current = GetParent( current );
2145 if (current) next = GetNextSibling( current );
2146 }
2147 }
2148 if (next)
2149 {
2150 SelectItem( next, unselect_others, extended_select );
2151 m_key_current=(wxGenericTreeItem*) next.m_pItem;
2152 EnsureVisible( next );
2153 }
2154 }
2155 }
2156 break;
2157
2158 // <End> selects the last visible tree item
2159 case WXK_END:
2160 {
2161 wxTreeItemId last = GetRootItem();
2162
2163 while ( last.IsOk() && IsExpanded(last) )
2164 {
2165 wxTreeItemId lastChild = GetLastChild(last);
2166
2167 // it may happen if the item was expanded but then all of
2168 // its children have been deleted - so IsExpanded() returned
2169 // TRUE, but GetLastChild() returned invalid item
2170 if ( !lastChild )
2171 break;
2172
2173 last = lastChild;
2174 }
2175
2176 if ( last.IsOk() )
2177 {
2178 EnsureVisible( last );
2179 SelectItem( last, unselect_others, extended_select );
2180 }
2181 }
2182 break;
2183
2184 // <Home> selects the root item
2185 case WXK_HOME:
2186 {
2187 wxTreeItemId prev = GetRootItem();
2188 if (prev)
2189 {
2190 EnsureVisible( prev );
2191 SelectItem( prev, unselect_others, extended_select );
2192 }
2193 }
2194 break;
2195
2196 default:
2197 event.Skip();
2198 }
2199 }
2200
2201 wxTreeItemId wxGenericTreeCtrl::HitTest(const wxPoint& point, int& flags)
2202 {
2203 // We have to call this here because the label in
2204 // question might just have been added and no screen
2205 // update taken place.
2206 if (m_dirty) wxYield();
2207
2208 wxClientDC dc(this);
2209 PrepareDC(dc);
2210 wxCoord x = dc.DeviceToLogicalX( point.x );
2211 wxCoord y = dc.DeviceToLogicalY( point.y );
2212 int w, h;
2213 GetSize(&w, &h);
2214
2215 flags=0;
2216 if (point.x<0) flags|=wxTREE_HITTEST_TOLEFT;
2217 if (point.x>w) flags|=wxTREE_HITTEST_TORIGHT;
2218 if (point.y<0) flags|=wxTREE_HITTEST_ABOVE;
2219 if (point.y>h) flags|=wxTREE_HITTEST_BELOW;
2220
2221 return m_anchor->HitTest( wxPoint(x, y), this, flags);
2222 }
2223
2224 // get the bounding rectangle of the item (or of its label only)
2225 bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
2226 wxRect& rect,
2227 bool textOnly) const
2228 {
2229 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2230
2231 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2232
2233 int startX, startY;
2234 GetViewStart(& startX, & startY);
2235
2236 rect.x = i->GetX() - startX*PIXELS_PER_UNIT;
2237 rect.y = i->GetY() - startY*PIXELS_PER_UNIT;
2238 rect.width = i->GetWidth();
2239 //rect.height = i->GetHeight();
2240 rect.height = GetLineHeight(i);
2241
2242 return TRUE;
2243 }
2244
2245 /* **** */
2246
2247 void wxGenericTreeCtrl::Edit( const wxTreeItemId& item )
2248 {
2249 if (!item.IsOk()) return;
2250
2251 m_currentEdit = (wxGenericTreeItem*) item.m_pItem;
2252
2253 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
2254 te.m_item = (long) m_currentEdit;
2255 te.SetEventObject( this );
2256 GetEventHandler()->ProcessEvent( te );
2257
2258 if (!te.IsAllowed()) return;
2259
2260 // We have to call this here because the label in
2261 // question might just have been added and no screen
2262 // update taken place.
2263 if (m_dirty) wxYield();
2264
2265 wxString s = m_currentEdit->GetText();
2266 int x = m_currentEdit->GetX();
2267 int y = m_currentEdit->GetY();
2268 int w = m_currentEdit->GetWidth();
2269 int h = m_currentEdit->GetHeight();
2270
2271 int image_h = 0;
2272 int image_w = 0;
2273
2274 int image = m_currentEdit->GetCurrentImage();
2275 if ( image != NO_IMAGE )
2276 {
2277 if ( m_imageListNormal )
2278 {
2279 m_imageListNormal->GetSize( image, image_w, image_h );
2280 image_w += 4;
2281 }
2282 else
2283 {
2284 wxFAIL_MSG(_T("you must create an image list to use images!"));
2285 }
2286 }
2287 x += image_w;
2288 w -= image_w + 4; // I don't know why +4 is needed
2289
2290 wxClientDC dc(this);
2291 PrepareDC( dc );
2292 x = dc.LogicalToDeviceX( x );
2293 y = dc.LogicalToDeviceY( y );
2294
2295 wxTreeTextCtrl *text = new wxTreeTextCtrl(
2296 this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) );
2297 text->SetFocus();
2298 }
2299
2300 void wxGenericTreeCtrl::OnRenameTimer()
2301 {
2302 Edit( m_current );
2303 }
2304
2305 void wxGenericTreeCtrl::OnRenameAccept()
2306 {
2307 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
2308 le.m_item = (long) m_currentEdit;
2309 le.SetEventObject( this );
2310 le.m_label = m_renameRes;
2311 GetEventHandler()->ProcessEvent( le );
2312
2313 if (!le.IsAllowed()) return;
2314
2315 SetItemText( m_currentEdit, m_renameRes );
2316 }
2317
2318 void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
2319 {
2320 if ( !m_anchor ) return;
2321
2322 // we process left mouse up event (enables in-place edit), right down
2323 // (pass to the user code), left dbl click (activate item) and
2324 // dragging/moving events for items drag-and-drop
2325 if ( !(event.LeftDown() ||
2326 event.LeftUp() ||
2327 event.RightDown() ||
2328 event.LeftDClick() ||
2329 event.Dragging() ||
2330 ((event.Moving() || event.RightUp()) && m_isDragging)) )
2331 {
2332 event.Skip();
2333
2334 return;
2335 }
2336
2337 wxClientDC dc(this);
2338 PrepareDC(dc);
2339 wxCoord x = dc.DeviceToLogicalX( event.GetX() );
2340 wxCoord y = dc.DeviceToLogicalY( event.GetY() );
2341
2342 int flags = 0;
2343 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), this, flags);
2344
2345 if ( event.Dragging() && !m_isDragging )
2346 {
2347 if (m_dragCount == 0)
2348 m_dragStart = wxPoint(x,y);
2349
2350 m_dragCount++;
2351
2352 if (m_dragCount != 3)
2353 {
2354 // wait until user drags a bit further...
2355 return;
2356 }
2357
2358 wxEventType command = event.RightIsDown()
2359 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2360 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
2361
2362 wxTreeEvent nevent( command, GetId() );
2363 nevent.m_item = (long) m_current;
2364 nevent.SetEventObject(this);
2365
2366 // by default the dragging is not supported, the user code must
2367 // explicitly allow the event for it to take place
2368 nevent.Veto();
2369
2370 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
2371 {
2372 // we're going to drag this item
2373 m_isDragging = TRUE;
2374
2375 // remember the old cursor because we will change it while
2376 // dragging
2377 m_oldCursor = m_cursor;
2378
2379 // in a single selection control, hide the selection temporarily
2380 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
2381 {
2382 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
2383
2384 if ( m_oldSelection )
2385 {
2386 m_oldSelection->SetHilight(FALSE);
2387 RefreshLine(m_oldSelection);
2388 }
2389 }
2390
2391 CaptureMouse();
2392 }
2393 }
2394 else if ( event.Moving() )
2395 {
2396 if ( item != m_dropTarget )
2397 {
2398 // unhighlight the previous drop target
2399 DrawDropEffect(m_dropTarget);
2400
2401 m_dropTarget = item;
2402
2403 // highlight the current drop target if any
2404 DrawDropEffect(m_dropTarget);
2405
2406 wxYield();
2407 }
2408 }
2409 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
2410 {
2411 // erase the highlighting
2412 DrawDropEffect(m_dropTarget);
2413
2414 // generate the drag end event
2415 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId());
2416
2417 event.m_item = (long) item;
2418 event.m_pointDrag = wxPoint(x, y);
2419 event.SetEventObject(this);
2420
2421 (void)GetEventHandler()->ProcessEvent(event);
2422
2423 m_isDragging = FALSE;
2424 m_dropTarget = (wxGenericTreeItem *)NULL;
2425
2426 if ( m_oldSelection )
2427 {
2428 m_oldSelection->SetHilight(TRUE);
2429 RefreshLine(m_oldSelection);
2430 m_oldSelection = (wxGenericTreeItem *)NULL;
2431 }
2432
2433 ReleaseMouse();
2434
2435 SetCursor(m_oldCursor);
2436
2437 wxYield();
2438 }
2439 else
2440 {
2441 // here we process only the messages which happen on tree items
2442
2443 m_dragCount = 0;
2444
2445 if (item == NULL) return; /* we hit the blank area */
2446
2447 if ( event.RightDown() )
2448 {
2449 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
2450 nevent.m_item = (long) item;
2451 nevent.m_code = 0;
2452 nevent.SetEventObject(this);
2453 GetEventHandler()->ProcessEvent(nevent);
2454 }
2455 else if ( event.LeftUp() )
2456 {
2457 if ( m_lastOnSame )
2458 {
2459 if ( (item == m_current) &&
2460 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
2461 HasFlag(wxTR_EDIT_LABELS) )
2462 {
2463 if ( m_renameTimer->IsRunning() )
2464 m_renameTimer->Stop();
2465
2466 m_renameTimer->Start( 100, TRUE );
2467 }
2468
2469 m_lastOnSame = FALSE;
2470 }
2471 }
2472 else
2473 {
2474 if ( event.LeftDown() )
2475 {
2476 m_lastOnSame = item == m_current;
2477 }
2478
2479 // how should the selection work for this event?
2480 bool is_multiple, extended_select, unselect_others;
2481 EventFlagsToSelType(GetWindowStyleFlag(),
2482 event.ShiftDown(),
2483 event.ControlDown(),
2484 is_multiple, extended_select, unselect_others);
2485
2486 if ( (flags & wxTREE_HITTEST_ONITEMBUTTON) && event.LeftDown() )
2487 {
2488 Toggle( item );
2489 if ( is_multiple )
2490 return;
2491 }
2492
2493 SelectItem(item, unselect_others, extended_select);
2494
2495 if ( event.LeftDClick() )
2496 {
2497 // double clicking should not start editing the item label
2498 m_renameTimer->Stop();
2499 m_lastOnSame = FALSE;
2500
2501 wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2502 nevent.m_item = (long) item;
2503 nevent.m_code = 0;
2504 nevent.SetEventObject( this );
2505 GetEventHandler()->ProcessEvent( nevent );
2506 }
2507 }
2508 }
2509 }
2510
2511 void wxGenericTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
2512 {
2513 /* after all changes have been done to the tree control,
2514 * we actually redraw the tree when everything is over */
2515
2516 if (!m_dirty)
2517 return;
2518
2519 m_dirty = FALSE;
2520
2521 CalculatePositions();
2522 Refresh();
2523 AdjustMyScrollbars();
2524 }
2525
2526 void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
2527 {
2528 wxCoord text_w = 0;
2529 wxCoord text_h = 0;
2530
2531 if (item->IsBold())
2532 dc.SetFont(m_boldFont);
2533
2534 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
2535 text_h+=2;
2536
2537 // restore normal font
2538 dc.SetFont( m_normalFont );
2539
2540 int image_h = 0;
2541 int image_w = 0;
2542 int image = item->GetCurrentImage();
2543 if ( image != NO_IMAGE )
2544 {
2545 if ( m_imageListNormal )
2546 {
2547 m_imageListNormal->GetSize( image, image_w, image_h );
2548 image_w += 4;
2549 }
2550 }
2551
2552 int total_h = (image_h > text_h) ? image_h : text_h;
2553
2554 if (total_h < 40)
2555 total_h += 2; // at least 2 pixels
2556 else
2557 total_h += total_h/10; // otherwise 10% extra spacing
2558
2559 item->SetHeight(total_h);
2560 if (total_h>m_lineHeight)
2561 m_lineHeight=total_h;
2562
2563 item->SetWidth(image_w+text_w+2);
2564 }
2565
2566 // -----------------------------------------------------------------------------
2567 // for developper : y is now the top of the level
2568 // not the middle of it !
2569 void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
2570 {
2571 int horizX = level*m_indent;
2572
2573 CalculateSize( item, dc );
2574
2575 // set its position
2576 item->SetX( horizX+m_indent+m_spacing );
2577 item->SetY( y );
2578 y+=GetLineHeight(item);
2579
2580 if ( !item->IsExpanded() )
2581 {
2582 // we dont need to calculate collapsed branches
2583 return;
2584 }
2585
2586 wxArrayGenericTreeItems& children = item->GetChildren();
2587 size_t n, count = children.Count();
2588 for (n = 0; n < count; ++n )
2589 CalculateLevel( children[n], dc, level+1, y ); // recurse
2590 }
2591
2592 void wxGenericTreeCtrl::CalculatePositions()
2593 {
2594 if ( !m_anchor ) return;
2595
2596 wxClientDC dc(this);
2597 PrepareDC( dc );
2598
2599 dc.SetFont( m_normalFont );
2600
2601 dc.SetPen( m_dottedPen );
2602 //if(GetImageList() == NULL)
2603 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2604
2605 int y = 2;
2606 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
2607 }
2608
2609 void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
2610 {
2611 if (m_dirty) return;
2612
2613 wxClientDC dc(this);
2614 PrepareDC(dc);
2615
2616 int cw = 0;
2617 int ch = 0;
2618 GetClientSize( &cw, &ch );
2619
2620 wxRect rect;
2621 rect.x = dc.LogicalToDeviceX( 0 );
2622 rect.width = cw;
2623 rect.y = dc.LogicalToDeviceY( item->GetY() );
2624 rect.height = ch;
2625
2626 Refresh( TRUE, &rect );
2627
2628 AdjustMyScrollbars();
2629 }
2630
2631 void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
2632 {
2633 if (m_dirty) return;
2634
2635 wxClientDC dc(this);
2636 PrepareDC( dc );
2637
2638 int cw = 0;
2639 int ch = 0;
2640 GetClientSize( &cw, &ch );
2641
2642 wxRect rect;
2643 rect.x = dc.LogicalToDeviceX( 0 );
2644 rect.y = dc.LogicalToDeviceY( item->GetY() );
2645 rect.width = cw;
2646 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
2647
2648 Refresh( TRUE, &rect );
2649 }
2650