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