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