]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/generic/treectlg.cpp
maintaining 8.1 compat (defining 8.5 calls)
[wxWidgets.git] / src / generic / treectlg.cpp
... / ...
CommitLineData
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
45class WXDLLEXPORT wxGenericTreeItem;
46
47WX_DEFINE_EXPORTED_ARRAY(wxGenericTreeItem *, wxArrayGenericTreeItems);
48//WX_DEFINE_OBJARRAY(wxArrayTreeItemIds);
49
50// ----------------------------------------------------------------------------
51// constants
52// ----------------------------------------------------------------------------
53
54static 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
63class WXDLLEXPORT wxTreeRenameTimer: public wxTimer
64{
65public:
66 wxTreeRenameTimer( wxGenericTreeCtrl *owner );
67
68 void Notify();
69
70private:
71 wxGenericTreeCtrl *m_owner;
72};
73
74// control used for in-place edit
75class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl
76{
77public:
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
95private:
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
106class WXDLLEXPORT wxGenericTreeItem
107{
108public:
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
199private:
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
236static 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
252wxTreeRenameTimer::wxTreeRenameTimer( wxGenericTreeCtrl *owner )
253{
254 m_owner = owner;
255}
256
257void wxTreeRenameTimer::Notify()
258{
259 m_owner->OnRenameTimer();
260}
261
262//-----------------------------------------------------------------------------
263// wxTreeTextCtrl (internal)
264//-----------------------------------------------------------------------------
265
266IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl,wxTextCtrl);
267
268BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
269 EVT_CHAR (wxTreeTextCtrl::OnChar)
270 EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp)
271 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
272END_EVENT_TABLE()
273
274wxTreeTextCtrl::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
295void 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
323void 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
338void 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
352IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
353
354wxTreeEvent::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
366wxGenericTreeItem::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
398wxGenericTreeItem::~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
408void 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
424void wxGenericTreeItem::SetText( const wxString &text )
425{
426 m_text = text;
427}
428
429void 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
451size_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
466void wxGenericTreeItem::SetCross( int x, int y )
467{
468 m_xCross = x;
469 m_yCross = y;
470}
471
472void 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
489wxGenericTreeItem *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
552int 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
589IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeCtrl, wxScrolledWindow)
590
591BEGIN_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)
598END_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
606IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxGenericTreeCtrl)
607#endif
608
609// -----------------------------------------------------------------------------
610// construction/destruction
611// -----------------------------------------------------------------------------
612
613void 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
654bool wxGenericTreeCtrl::Create(wxWindow *parent, wxWindowID id,
655 const wxPoint& pos, const wxSize& size,
656 long style,
657 const wxValidator &validator,
658 const wxString& name )
659{
660 wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
661
662#if wxUSE_VALIDATORS
663 SetValidator( validator );
664#endif
665
666 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX ) );
667// m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
668 m_dottedPen = wxPen( "grey", 0, 0 );
669
670 return TRUE;
671}
672
673wxGenericTreeCtrl::~wxGenericTreeCtrl()
674{
675 wxDELETE( m_hilightBrush );
676
677 DeleteAllItems();
678
679 delete m_renameTimer;
680 if (m_ownsImageListNormal) delete m_imageListNormal;
681 if (m_ownsImageListState) delete m_imageListState;
682}
683
684// -----------------------------------------------------------------------------
685// accessors
686// -----------------------------------------------------------------------------
687
688size_t wxGenericTreeCtrl::GetCount() const
689{
690 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
691}
692
693void wxGenericTreeCtrl::SetIndent(unsigned int indent)
694{
695 m_indent = indent;
696 m_dirty = TRUE;
697}
698
699void wxGenericTreeCtrl::SetSpacing(unsigned int spacing)
700{
701 m_spacing = spacing;
702 m_dirty = TRUE;
703}
704
705size_t wxGenericTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
706{
707 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
708
709 return ((wxGenericTreeItem*) item.m_pItem)->GetChildrenCount(recursively);
710}
711
712// -----------------------------------------------------------------------------
713// functions to work with tree items
714// -----------------------------------------------------------------------------
715
716wxString wxGenericTreeCtrl::GetItemText(const wxTreeItemId& item) const
717{
718 wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
719
720 return ((wxGenericTreeItem*) item.m_pItem)->GetText();
721}
722
723int wxGenericTreeCtrl::GetItemImage(const wxTreeItemId& item,
724 wxTreeItemIcon which) const
725{
726 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
727
728 return ((wxGenericTreeItem*) item.m_pItem)->GetImage(which);
729}
730
731wxTreeItemData *wxGenericTreeCtrl::GetItemData(const wxTreeItemId& item) const
732{
733 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
734
735 return ((wxGenericTreeItem*) item.m_pItem)->GetData();
736}
737
738void wxGenericTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
739{
740 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
741
742 wxClientDC dc(this);
743 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
744 pItem->SetText(text);
745 CalculateSize(pItem, dc);
746 RefreshLine(pItem);
747}
748
749void wxGenericTreeCtrl::SetItemImage(const wxTreeItemId& item,
750 int image,
751 wxTreeItemIcon which)
752{
753 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
754
755 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
756 pItem->SetImage(image, which);
757
758 wxClientDC dc(this);
759 CalculateSize(pItem, dc);
760 RefreshLine(pItem);
761}
762
763void wxGenericTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
764{
765 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
766
767 ((wxGenericTreeItem*) item.m_pItem)->SetData(data);
768}
769
770void wxGenericTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
771{
772 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
773
774 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
775 pItem->SetHasPlus(has);
776 RefreshLine(pItem);
777}
778
779void wxGenericTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
780{
781 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
782
783 // avoid redrawing the tree if no real change
784 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
785 if ( pItem->IsBold() != bold )
786 {
787 pItem->SetBold(bold);
788 RefreshLine(pItem);
789 }
790}
791
792void wxGenericTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
793 const wxColour& col)
794{
795 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
796
797 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
798 pItem->Attr().SetTextColour(col);
799 RefreshLine(pItem);
800}
801
802void wxGenericTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
803 const wxColour& col)
804{
805 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
806
807 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
808 pItem->Attr().SetBackgroundColour(col);
809 RefreshLine(pItem);
810}
811
812void wxGenericTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
813{
814 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
815
816 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
817 pItem->Attr().SetFont(font);
818 RefreshLine(pItem);
819}
820
821bool wxGenericTreeCtrl::SetFont( const wxFont &font )
822{
823 wxScrolledWindow::SetFont(font);
824
825 m_normalFont = font ;
826 m_boldFont = wxFont( m_normalFont.GetPointSize(),
827 m_normalFont.GetFamily(),
828 m_normalFont.GetStyle(),
829 wxBOLD,
830 m_normalFont.GetUnderlined());
831
832 return TRUE;
833}
834
835
836// -----------------------------------------------------------------------------
837// item status inquiries
838// -----------------------------------------------------------------------------
839
840bool wxGenericTreeCtrl::IsVisible(const wxTreeItemId& item) const
841{
842 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
843
844 // An item is only visible if it's not a descendant of a collapsed item
845 wxGenericTreeItem *pItem = (wxGenericTreeItem*) item.m_pItem;
846 wxGenericTreeItem* parent = pItem->GetParent();
847 while (parent)
848 {
849 if (!parent->IsExpanded())
850 return FALSE;
851 parent = parent->GetParent();
852 }
853
854 int startX, startY;
855 GetViewStart(& startX, & startY);
856
857 wxSize clientSize = GetClientSize();
858
859 wxRect rect;
860 if (!GetBoundingRect(item, rect))
861 return FALSE;
862 if (rect.GetWidth() == 0 || rect.GetHeight() == 0)
863 return FALSE;
864 if (rect.GetBottom() < 0 || rect.GetTop() > clientSize.y)
865 return FALSE;
866 if (rect.GetRight() < 0 || rect.GetLeft() > clientSize.x)
867 return FALSE;
868
869 return TRUE;
870}
871
872bool wxGenericTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
873{
874 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
875
876 return !((wxGenericTreeItem*) item.m_pItem)->GetChildren().IsEmpty();
877}
878
879bool wxGenericTreeCtrl::IsExpanded(const wxTreeItemId& item) const
880{
881 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
882
883 return ((wxGenericTreeItem*) item.m_pItem)->IsExpanded();
884}
885
886bool wxGenericTreeCtrl::IsSelected(const wxTreeItemId& item) const
887{
888 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
889
890 return ((wxGenericTreeItem*) item.m_pItem)->IsSelected();
891}
892
893bool wxGenericTreeCtrl::IsBold(const wxTreeItemId& item) const
894{
895 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
896
897 return ((wxGenericTreeItem*) item.m_pItem)->IsBold();
898}
899
900// -----------------------------------------------------------------------------
901// navigation
902// -----------------------------------------------------------------------------
903
904wxTreeItemId wxGenericTreeCtrl::GetParent(const wxTreeItemId& item) const
905{
906 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
907
908 return ((wxGenericTreeItem*) item.m_pItem)->GetParent();
909}
910
911wxTreeItemId wxGenericTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
912{
913 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
914
915 cookie = 0;
916 return GetNextChild(item, cookie);
917}
918
919wxTreeItemId wxGenericTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
920{
921 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
922
923 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
924 if ( (size_t)cookie < children.Count() )
925 {
926 return children.Item((size_t)cookie++);
927 }
928 else
929 {
930 // there are no more of them
931 return wxTreeItemId();
932 }
933}
934
935wxTreeItemId wxGenericTreeCtrl::GetLastChild(const wxTreeItemId& item) const
936{
937 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
938
939 wxArrayGenericTreeItems& children = ((wxGenericTreeItem*) item.m_pItem)->GetChildren();
940 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
941}
942
943wxTreeItemId wxGenericTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
944{
945 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
946
947 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
948 wxGenericTreeItem *parent = i->GetParent();
949 if ( parent == NULL )
950 {
951 // root item doesn't have any siblings
952 return wxTreeItemId();
953 }
954
955 wxArrayGenericTreeItems& siblings = parent->GetChildren();
956 int index = siblings.Index(i);
957 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
958
959 size_t n = (size_t)(index + 1);
960 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
961}
962
963wxTreeItemId wxGenericTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
964{
965 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
966
967 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
968 wxGenericTreeItem *parent = i->GetParent();
969 if ( parent == NULL )
970 {
971 // root item doesn't have any siblings
972 return wxTreeItemId();
973 }
974
975 wxArrayGenericTreeItems& siblings = parent->GetChildren();
976 int index = siblings.Index(i);
977 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
978
979 return index == 0 ? wxTreeItemId()
980 : wxTreeItemId(siblings[(size_t)(index - 1)]);
981}
982
983// Only for internal use right now, but should probably be public
984wxTreeItemId wxGenericTreeCtrl::GetNext(const wxTreeItemId& item) const
985{
986 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
987
988 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
989
990 // First see if there are any children.
991 wxArrayGenericTreeItems& children = i->GetChildren();
992 if (children.GetCount() > 0)
993 {
994 return children.Item(0);
995 }
996 else
997 {
998 // Try a sibling of this or ancestor instead
999 wxTreeItemId p = item;
1000 wxTreeItemId toFind;
1001 do
1002 {
1003 toFind = GetNextSibling(p);
1004 p = GetParent(p);
1005 } while (p.IsOk() && !toFind.IsOk());
1006 return toFind;
1007 }
1008}
1009
1010wxTreeItemId wxGenericTreeCtrl::GetPrev(const wxTreeItemId& item) const
1011{
1012 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1013
1014 wxFAIL_MSG(wxT("not implemented"));
1015
1016 return wxTreeItemId();
1017}
1018
1019wxTreeItemId wxGenericTreeCtrl::GetFirstVisibleItem() const
1020{
1021 wxTreeItemId id = GetRootItem();
1022 if (!id.IsOk())
1023 return id;
1024
1025 do
1026 {
1027 if (IsVisible(id))
1028 return id;
1029 id = GetNext(id);
1030 } while (id.IsOk());
1031
1032 return wxTreeItemId();
1033}
1034
1035wxTreeItemId wxGenericTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
1036{
1037 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1038
1039 wxTreeItemId id = item;
1040 while (id.IsOk())
1041 {
1042 id = GetNext(id);
1043
1044 if (id.IsOk() && IsVisible(id))
1045 return id;
1046 }
1047 return wxTreeItemId();
1048}
1049
1050wxTreeItemId wxGenericTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1051{
1052 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1053
1054 wxFAIL_MSG(wxT("not implemented"));
1055
1056 return wxTreeItemId();
1057}
1058
1059// -----------------------------------------------------------------------------
1060// operations
1061// -----------------------------------------------------------------------------
1062
1063wxTreeItemId wxGenericTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
1064 size_t previous,
1065 const wxString& text,
1066 int image, int selImage,
1067 wxTreeItemData *data)
1068{
1069 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1070 if ( !parent )
1071 {
1072 // should we give a warning here?
1073 return AddRoot(text, image, selImage, data);
1074 }
1075
1076 wxClientDC dc(this);
1077 wxGenericTreeItem *item =
1078 new wxGenericTreeItem( parent, text, dc, image, selImage, data );
1079
1080 if ( data != NULL )
1081 {
1082 data->m_pItem = (long) item;
1083 }
1084
1085 parent->Insert( item, previous );
1086
1087 m_dirty = TRUE;
1088
1089 return item;
1090}
1091
1092wxTreeItemId wxGenericTreeCtrl::AddRoot(const wxString& text,
1093 int image, int selImage,
1094 wxTreeItemData *data)
1095{
1096 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
1097
1098 wxClientDC dc(this);
1099 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
1100 image, selImage, data);
1101 if ( data != NULL )
1102 {
1103 data->m_pItem = (long) m_anchor;
1104 }
1105
1106 if (!HasFlag(wxTR_MULTIPLE))
1107 {
1108 m_current = m_key_current = m_anchor;
1109 m_current->SetHilight( TRUE );
1110 }
1111
1112 m_dirty = TRUE;
1113
1114 return m_anchor;
1115}
1116
1117wxTreeItemId wxGenericTreeCtrl::PrependItem(const wxTreeItemId& parent,
1118 const wxString& text,
1119 int image, int selImage,
1120 wxTreeItemData *data)
1121{
1122 return DoInsertItem(parent, 0u, text, image, selImage, data);
1123}
1124
1125wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
1126 const wxTreeItemId& idPrevious,
1127 const wxString& text,
1128 int image, int selImage,
1129 wxTreeItemData *data)
1130{
1131 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1132 if ( !parent )
1133 {
1134 // should we give a warning here?
1135 return AddRoot(text, image, selImage, data);
1136 }
1137
1138 int index = parent->GetChildren().Index((wxGenericTreeItem*) idPrevious.m_pItem);
1139 wxASSERT_MSG( index != wxNOT_FOUND,
1140 wxT("previous item in wxGenericTreeCtrl::InsertItem() is not a sibling") );
1141
1142 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
1143}
1144
1145wxTreeItemId wxGenericTreeCtrl::InsertItem(const wxTreeItemId& parentId,
1146 size_t before,
1147 const wxString& text,
1148 int image, int selImage,
1149 wxTreeItemData *data)
1150{
1151 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1152 if ( !parent )
1153 {
1154 // should we give a warning here?
1155 return AddRoot(text, image, selImage, data);
1156 }
1157
1158 return DoInsertItem(parentId, before, text, image, selImage, data);
1159}
1160
1161wxTreeItemId wxGenericTreeCtrl::AppendItem(const wxTreeItemId& parentId,
1162 const wxString& text,
1163 int image, int selImage,
1164 wxTreeItemData *data)
1165{
1166 wxGenericTreeItem *parent = (wxGenericTreeItem*) parentId.m_pItem;
1167 if ( !parent )
1168 {
1169 // should we give a warning here?
1170 return AddRoot(text, image, selImage, data);
1171 }
1172
1173 return DoInsertItem( parent, parent->GetChildren().Count(), text,
1174 image, selImage, data);
1175}
1176
1177void wxGenericTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
1178{
1179 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
1180 event.m_item = (long) item;
1181 event.SetEventObject( this );
1182 ProcessEvent( event );
1183}
1184
1185void wxGenericTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
1186{
1187 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1188 item->DeleteChildren(this);
1189
1190 m_dirty = TRUE;
1191}
1192
1193void wxGenericTreeCtrl::Delete(const wxTreeItemId& itemId)
1194{
1195 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1196
1197 // don't stay with invalid m_key_current or we will crash in the next call
1198 // to OnChar()
1199 bool changeKeyCurrent = FALSE;
1200 wxGenericTreeItem *itemKey = m_key_current;
1201 while ( itemKey && !changeKeyCurrent )
1202 {
1203 if ( itemKey == item )
1204 {
1205 // m_key_current is a descendant of the item being deleted
1206 changeKeyCurrent = TRUE;
1207 }
1208 else
1209 {
1210 itemKey = itemKey->GetParent();
1211 }
1212 }
1213
1214 wxGenericTreeItem *parent = item->GetParent();
1215 if ( parent )
1216 {
1217 parent->GetChildren().Remove( item ); // remove by value
1218 }
1219
1220 if ( changeKeyCurrent )
1221 {
1222 // may be NULL or not
1223 m_key_current = parent;
1224 }
1225
1226 item->DeleteChildren(this);
1227 SendDeleteEvent(item);
1228 delete item;
1229
1230 m_dirty = TRUE;
1231}
1232
1233void wxGenericTreeCtrl::DeleteAllItems()
1234{
1235 if ( m_anchor )
1236 {
1237 m_anchor->DeleteChildren(this);
1238 delete m_anchor;
1239
1240 m_anchor = NULL;
1241
1242 m_dirty = TRUE;
1243 }
1244}
1245
1246void wxGenericTreeCtrl::Expand(const wxTreeItemId& itemId)
1247{
1248 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1249
1250 wxCHECK_RET( item, _T("invalid item in wxGenericTreeCtrl::Expand") );
1251
1252 if ( !item->HasPlus() )
1253 return;
1254
1255 if ( item->IsExpanded() )
1256 return;
1257
1258 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
1259 event.m_item = (long) item;
1260 event.SetEventObject( this );
1261
1262 if ( ProcessEvent( event ) && !event.IsAllowed() )
1263 {
1264 // cancelled by program
1265 return;
1266 }
1267
1268 item->Expand();
1269 CalculatePositions();
1270
1271 RefreshSubtree(item);
1272
1273 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1274 ProcessEvent( event );
1275}
1276
1277void wxGenericTreeCtrl::ExpandAll(const wxTreeItemId& item)
1278{
1279 Expand(item);
1280 if ( IsExpanded(item) )
1281 {
1282 long cookie;
1283 wxTreeItemId child = GetFirstChild(item, cookie);
1284 while ( child.IsOk() )
1285 {
1286 ExpandAll(child);
1287
1288 child = GetNextChild(item, cookie);
1289 }
1290 }
1291}
1292
1293void wxGenericTreeCtrl::Collapse(const wxTreeItemId& itemId)
1294{
1295 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1296
1297 if ( !item->IsExpanded() )
1298 return;
1299
1300 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
1301 event.m_item = (long) item;
1302 event.SetEventObject( this );
1303 if ( ProcessEvent( event ) && !event.IsAllowed() )
1304 {
1305 // cancelled by program
1306 return;
1307 }
1308
1309 item->Collapse();
1310
1311 wxArrayGenericTreeItems& children = item->GetChildren();
1312 size_t count = children.Count();
1313 for ( size_t n = 0; n < count; n++ )
1314 {
1315 Collapse(children[n]);
1316 }
1317
1318 CalculatePositions();
1319
1320 RefreshSubtree(item);
1321
1322 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1323 ProcessEvent( event );
1324}
1325
1326void wxGenericTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1327{
1328 Collapse(item);
1329 DeleteChildren(item);
1330}
1331
1332void wxGenericTreeCtrl::Toggle(const wxTreeItemId& itemId)
1333{
1334 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1335
1336 if (item->IsExpanded())
1337 Collapse(itemId);
1338 else
1339 Expand(itemId);
1340}
1341
1342void wxGenericTreeCtrl::Unselect()
1343{
1344 if (m_current)
1345 {
1346 m_current->SetHilight( FALSE );
1347 RefreshLine( m_current );
1348 }
1349}
1350
1351void wxGenericTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
1352{
1353 if (item->IsSelected())
1354 {
1355 item->SetHilight(FALSE);
1356 RefreshLine(item);
1357 }
1358
1359 if (item->HasChildren())
1360 {
1361 wxArrayGenericTreeItems& children = item->GetChildren();
1362 size_t count = children.Count();
1363 for ( size_t n = 0; n < count; ++n )
1364 {
1365 UnselectAllChildren(children[n]);
1366 }
1367 }
1368}
1369
1370void wxGenericTreeCtrl::UnselectAll()
1371{
1372 UnselectAllChildren((wxGenericTreeItem*) GetRootItem().m_pItem);
1373}
1374
1375// Recursive function !
1376// To stop we must have crt_item<last_item
1377// Algorithm :
1378// Tag all next children, when no more children,
1379// Move to parent (not to tag)
1380// Keep going... if we found last_item, we stop.
1381bool wxGenericTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1382{
1383 wxGenericTreeItem *parent = crt_item->GetParent();
1384
1385 if (parent == NULL) // This is root item
1386 return TagAllChildrenUntilLast(crt_item, last_item, select);
1387
1388 wxArrayGenericTreeItems& children = parent->GetChildren();
1389 int index = children.Index(crt_item);
1390 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1391
1392 size_t count = children.Count();
1393 for (size_t n=(size_t)(index+1); n<count; ++n)
1394 {
1395 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1396 }
1397
1398 return TagNextChildren(parent, last_item, select);
1399}
1400
1401bool wxGenericTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1402{
1403 crt_item->SetHilight(select);
1404 RefreshLine(crt_item);
1405
1406 if (crt_item==last_item)
1407 return TRUE;
1408
1409 if (crt_item->HasChildren())
1410 {
1411 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1412 size_t count = children.Count();
1413 for ( size_t n = 0; n < count; ++n )
1414 {
1415 if (TagAllChildrenUntilLast(children[n], last_item, select))
1416 return TRUE;
1417 }
1418 }
1419
1420 return FALSE;
1421}
1422
1423void wxGenericTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
1424{
1425 // item2 is not necessary after item1
1426 wxGenericTreeItem *first=NULL, *last=NULL;
1427
1428 // choice first' and 'last' between item1 and item2
1429 if (item1->GetY()<item2->GetY())
1430 {
1431 first=item1;
1432 last=item2;
1433 }
1434 else
1435 {
1436 first=item2;
1437 last=item1;
1438 }
1439
1440 bool select = m_current->IsSelected();
1441
1442 if ( TagAllChildrenUntilLast(first,last,select) )
1443 return;
1444
1445 TagNextChildren(first,last,select);
1446}
1447
1448void wxGenericTreeCtrl::SelectItem(const wxTreeItemId& itemId,
1449 bool unselect_others,
1450 bool extended_select)
1451{
1452 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
1453
1454 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
1455 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1456
1457 //wxCHECK_RET( ( (!unselect_others) && is_single),
1458 // wxT("this is a single selection tree") );
1459
1460 // to keep going anyhow !!!
1461 if (is_single)
1462 {
1463 if (item->IsSelected())
1464 return; // nothing to do
1465 unselect_others = TRUE;
1466 extended_select = FALSE;
1467 }
1468 else if ( unselect_others && item->IsSelected() )
1469 {
1470 // selection change if there is more than one item currently selected
1471 wxArrayTreeItemIds selected_items;
1472 if ( GetSelections(selected_items) == 1 )
1473 return;
1474 }
1475
1476 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
1477 event.m_item = (long) item;
1478 event.m_itemOld = (long) m_current;
1479 event.SetEventObject( this );
1480 // TODO : Here we don't send any selection mode yet !
1481
1482 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
1483 return;
1484
1485 // ctrl press
1486 if (unselect_others)
1487 {
1488 if (is_single) Unselect(); // to speed up thing
1489 else UnselectAll();
1490 }
1491
1492 // shift press
1493 if (extended_select)
1494 {
1495 if ( !m_current )
1496 {
1497 m_current =
1498 m_key_current = (wxGenericTreeItem*) GetRootItem().m_pItem;
1499 }
1500
1501 // don't change the mark (m_current)
1502 SelectItemRange(m_current, item);
1503 }
1504 else
1505 {
1506 bool select=TRUE; // the default
1507
1508 // Check if we need to toggle hilight (ctrl mode)
1509 if (!unselect_others)
1510 select=!item->IsSelected();
1511
1512 m_current = m_key_current = item;
1513 m_current->SetHilight(select);
1514 RefreshLine( m_current );
1515 }
1516
1517 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1518 GetEventHandler()->ProcessEvent( event );
1519}
1520
1521void wxGenericTreeCtrl::FillArray(wxGenericTreeItem *item,
1522 wxArrayTreeItemIds &array) const
1523{
1524 if ( item->IsSelected() )
1525 array.Add(wxTreeItemId(item));
1526
1527 if ( item->HasChildren() )
1528 {
1529 wxArrayGenericTreeItems& children = item->GetChildren();
1530 size_t count = children.GetCount();
1531 for ( size_t n = 0; n < count; ++n )
1532 FillArray(children[n], array);
1533 }
1534}
1535
1536size_t wxGenericTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
1537{
1538 array.Empty();
1539 wxTreeItemId idRoot = GetRootItem();
1540 if ( idRoot.IsOk() )
1541 {
1542 FillArray((wxGenericTreeItem*) idRoot.m_pItem, array);
1543 }
1544 //else: the tree is empty, so no selections
1545
1546 return array.Count();
1547}
1548
1549void wxGenericTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1550{
1551 if (!item.IsOk()) return;
1552
1553 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
1554
1555 // first expand all parent branches
1556 wxGenericTreeItem *parent = gitem->GetParent();
1557 while ( parent )
1558 {
1559 Expand(parent);
1560 parent = parent->GetParent();
1561 }
1562
1563 //if (parent) CalculatePositions();
1564
1565 ScrollTo(item);
1566}
1567
1568void wxGenericTreeCtrl::ScrollTo(const wxTreeItemId &item)
1569{
1570 if (!item.IsOk()) return;
1571
1572 // We have to call this here because the label in
1573 // question might just have been added and no screen
1574 // update taken place.
1575 if (m_dirty) wxYieldIfNeeded();
1576
1577 wxGenericTreeItem *gitem = (wxGenericTreeItem*) item.m_pItem;
1578
1579 // now scroll to the item
1580 int item_y = gitem->GetY();
1581
1582 int start_x = 0;
1583 int start_y = 0;
1584 ViewStart( &start_x, &start_y );
1585 start_y *= PIXELS_PER_UNIT;
1586
1587 int client_h = 0;
1588 int client_w = 0;
1589 GetClientSize( &client_w, &client_h );
1590
1591 if (item_y < start_y+3)
1592 {
1593 // going down
1594 int x = 0;
1595 int y = 0;
1596 m_anchor->GetSize( x, y, this );
1597 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1598 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1599 int x_pos = GetScrollPos( wxHORIZONTAL );
1600 // Item should appear at top
1601 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
1602 }
1603 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
1604 {
1605 // going up
1606 int x = 0;
1607 int y = 0;
1608 m_anchor->GetSize( x, y, this );
1609 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1610 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1611 item_y += PIXELS_PER_UNIT+2;
1612 int x_pos = GetScrollPos( wxHORIZONTAL );
1613 // Item should appear at bottom
1614 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 );
1615 }
1616}
1617
1618// FIXME: tree sorting functions are not reentrant and not MT-safe!
1619static wxGenericTreeCtrl *s_treeBeingSorted = NULL;
1620
1621static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
1622 wxGenericTreeItem **item2)
1623{
1624 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxGenericTreeCtrl::SortChildren()") );
1625
1626 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
1627}
1628
1629int wxGenericTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1630 const wxTreeItemId& item2)
1631{
1632 return wxStrcmp(GetItemText(item1), GetItemText(item2));
1633}
1634
1635void wxGenericTreeCtrl::SortChildren(const wxTreeItemId& itemId)
1636{
1637 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
1638
1639 wxGenericTreeItem *item = (wxGenericTreeItem*) itemId.m_pItem;
1640
1641 wxCHECK_RET( !s_treeBeingSorted,
1642 wxT("wxGenericTreeCtrl::SortChildren is not reentrant") );
1643
1644 wxArrayGenericTreeItems& children = item->GetChildren();
1645 if ( children.Count() > 1 )
1646 {
1647 s_treeBeingSorted = this;
1648 children.Sort(tree_ctrl_compare_func);
1649 s_treeBeingSorted = NULL;
1650
1651 m_dirty = TRUE;
1652 }
1653 //else: don't make the tree dirty as nothing changed
1654}
1655
1656wxImageList *wxGenericTreeCtrl::GetImageList() const
1657{
1658 return m_imageListNormal;
1659}
1660
1661wxImageList *wxGenericTreeCtrl::GetStateImageList() const
1662{
1663 return m_imageListState;
1664}
1665
1666void wxGenericTreeCtrl::SetImageList(wxImageList *imageList)
1667{
1668 if (m_ownsImageListNormal) delete m_imageListNormal;
1669
1670 m_imageListNormal = imageList;
1671 m_ownsImageListNormal = FALSE;
1672
1673 if ( !m_imageListNormal )
1674 return;
1675
1676 // Calculate a m_lineHeight value from the image sizes.
1677 // May be toggle off. Then wxGenericTreeCtrl will spread when
1678 // necessary (which might look ugly).
1679 wxClientDC dc(this);
1680 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1681 int width = 0, height = 0,
1682 n = m_imageListNormal->GetImageCount();
1683
1684 for (int i = 0; i < n ; i++)
1685 {
1686 m_imageListNormal->GetSize(i, width, height);
1687 if (height > m_lineHeight) m_lineHeight = height;
1688 }
1689
1690 if (m_lineHeight < 40)
1691 m_lineHeight += 2; // at least 2 pixels
1692 else
1693 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
1694}
1695
1696void wxGenericTreeCtrl::SetStateImageList(wxImageList *imageList)
1697{
1698 if (m_ownsImageListState) delete m_imageListState;
1699 m_imageListState = imageList;
1700 m_ownsImageListState = FALSE;
1701}
1702
1703void wxGenericTreeCtrl::AssignImageList(wxImageList *imageList)
1704{
1705 SetImageList(imageList);
1706 m_ownsImageListNormal = TRUE;
1707}
1708
1709void wxGenericTreeCtrl::AssignStateImageList(wxImageList *imageList)
1710{
1711 SetStateImageList(imageList);
1712 m_ownsImageListState = TRUE;
1713}
1714
1715// -----------------------------------------------------------------------------
1716// helpers
1717// -----------------------------------------------------------------------------
1718
1719void wxGenericTreeCtrl::AdjustMyScrollbars()
1720{
1721 if (m_anchor)
1722 {
1723 int x = 0;
1724 int y = 0;
1725 m_anchor->GetSize( x, y, this );
1726 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1727 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1728 int x_pos = GetScrollPos( wxHORIZONTAL );
1729 int y_pos = GetScrollPos( wxVERTICAL );
1730 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
1731 }
1732 else
1733 {
1734 SetScrollbars( 0, 0, 0, 0 );
1735 }
1736}
1737
1738int wxGenericTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
1739{
1740 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
1741 return item->GetHeight();
1742 else
1743 return m_lineHeight;
1744}
1745
1746void wxGenericTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
1747{
1748 wxTreeItemAttr *attr = item->GetAttributes();
1749 if ( attr && attr->HasFont() )
1750 dc.SetFont(attr->GetFont());
1751 else if (item->IsBold())
1752 dc.SetFont(m_boldFont);
1753
1754 long text_w = 0;
1755 long text_h = 0;
1756 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
1757
1758 int image_h = 0;
1759 int image_w = 0;
1760 int image = item->GetCurrentImage();
1761 if ( image != NO_IMAGE )
1762 {
1763 if ( m_imageListNormal )
1764 {
1765 m_imageListNormal->GetSize( image, image_w, image_h );
1766 image_w += 4;
1767 }
1768 else
1769 {
1770 image = NO_IMAGE;
1771 }
1772 }
1773
1774 int total_h = GetLineHeight(item);
1775
1776 if (item->IsSelected())
1777 dc.SetBrush(*m_hilightBrush);
1778 else
1779 {
1780 wxColour colBg;
1781 if ( attr && attr->HasBackgroundColour() )
1782 colBg = attr->GetBackgroundColour();
1783 else
1784 colBg = m_backgroundColour;
1785 dc.SetBrush(wxBrush(colBg, wxSOLID));
1786 }
1787
1788 if (item->IsSelected() && image != NO_IMAGE)
1789 {
1790 // If it's selected, and there's an image, then we should
1791 // take care to leave the area under the image painted in the
1792 // background colour.
1793 dc.DrawRectangle( item->GetX() + image_w - 2, item->GetY(), item->GetWidth() - image_w + 2, total_h );
1794 }
1795 else
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 !
1819void 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
1923void 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
1948void 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
1965void 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
1989void 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
2008void wxGenericTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
2009{
2010 m_hasFocus = TRUE;
2011
2012 if (m_current) RefreshLine( m_current );
2013}
2014
2015void wxGenericTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
2016{
2017 m_hasFocus = FALSE;
2018
2019 if (m_current) RefreshLine( m_current );
2020}
2021
2022void 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
2224wxTreeItemId 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 // JACS: removed this because the yield can cause the window to be
2230 // deleted from under us if a close window event is pending
2231 // if (m_dirty) wxYieldIfNeeded();
2232
2233 wxClientDC dc(this);
2234 PrepareDC(dc);
2235 wxCoord x = dc.DeviceToLogicalX( point.x );
2236 wxCoord y = dc.DeviceToLogicalY( point.y );
2237 int w, h;
2238 GetSize(&w, &h);
2239
2240 flags=0;
2241 if (point.x<0) flags|=wxTREE_HITTEST_TOLEFT;
2242 if (point.x>w) flags|=wxTREE_HITTEST_TORIGHT;
2243 if (point.y<0) flags|=wxTREE_HITTEST_ABOVE;
2244 if (point.y>h) flags|=wxTREE_HITTEST_BELOW;
2245
2246 if (m_anchor)
2247 return m_anchor->HitTest( wxPoint(x, y), this, flags);
2248 else
2249 return wxTreeItemId();
2250}
2251
2252// get the bounding rectangle of the item (or of its label only)
2253bool wxGenericTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
2254 wxRect& rect,
2255 bool WXUNUSED(textOnly)) const
2256{
2257 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid item in wxGenericTreeCtrl::GetBoundingRect") );
2258
2259 wxGenericTreeItem *i = (wxGenericTreeItem*) item.m_pItem;
2260
2261 int startX, startY;
2262 GetViewStart(& startX, & startY);
2263
2264 rect.x = i->GetX() - startX*PIXELS_PER_UNIT;
2265 rect.y = i->GetY() - startY*PIXELS_PER_UNIT;
2266 rect.width = i->GetWidth();
2267 //rect.height = i->GetHeight();
2268 rect.height = GetLineHeight(i);
2269
2270 return TRUE;
2271}
2272
2273/* **** */
2274
2275void wxGenericTreeCtrl::Edit( const wxTreeItemId& item )
2276{
2277 if (!item.IsOk()) return;
2278
2279 m_currentEdit = (wxGenericTreeItem*) item.m_pItem;
2280
2281 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
2282 te.m_item = (long) m_currentEdit;
2283 te.SetEventObject( this );
2284 GetEventHandler()->ProcessEvent( te );
2285
2286 if (!te.IsAllowed()) return;
2287
2288 // We have to call this here because the label in
2289 // question might just have been added and no screen
2290 // update taken place.
2291 if (m_dirty) wxYieldIfNeeded();
2292
2293 wxString s = m_currentEdit->GetText();
2294 int x = m_currentEdit->GetX();
2295 int y = m_currentEdit->GetY();
2296 int w = m_currentEdit->GetWidth();
2297 int h = m_currentEdit->GetHeight();
2298
2299 int image_h = 0;
2300 int image_w = 0;
2301
2302 int image = m_currentEdit->GetCurrentImage();
2303 if ( image != NO_IMAGE )
2304 {
2305 if ( m_imageListNormal )
2306 {
2307 m_imageListNormal->GetSize( image, image_w, image_h );
2308 image_w += 4;
2309 }
2310 else
2311 {
2312 wxFAIL_MSG(_T("you must create an image list to use images!"));
2313 }
2314 }
2315 x += image_w;
2316 w -= image_w + 4; // I don't know why +4 is needed
2317
2318 wxClientDC dc(this);
2319 PrepareDC( dc );
2320 x = dc.LogicalToDeviceX( x );
2321 y = dc.LogicalToDeviceY( y );
2322
2323 wxTreeTextCtrl *text = new wxTreeTextCtrl(this, -1,
2324 &m_renameAccept,
2325 &m_renameRes,
2326 this,
2327 s,
2328 wxPoint(x-4,y-4),
2329 wxSize(w+11,h+8));
2330 text->SetFocus();
2331}
2332
2333void wxGenericTreeCtrl::OnRenameTimer()
2334{
2335 Edit( m_current );
2336}
2337
2338void wxGenericTreeCtrl::OnRenameAccept()
2339{
2340 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
2341 le.m_item = (long) m_currentEdit;
2342 le.SetEventObject( this );
2343 le.m_label = m_renameRes;
2344 GetEventHandler()->ProcessEvent( le );
2345
2346 if (!le.IsAllowed()) return;
2347
2348 SetItemText( m_currentEdit, m_renameRes );
2349}
2350
2351void wxGenericTreeCtrl::OnMouse( wxMouseEvent &event )
2352{
2353 if ( !m_anchor ) return;
2354
2355 // we process left mouse up event (enables in-place edit), right down
2356 // (pass to the user code), left dbl click (activate item) and
2357 // dragging/moving events for items drag-and-drop
2358 if ( !(event.LeftDown() ||
2359 event.LeftUp() ||
2360 event.RightDown() ||
2361 event.LeftDClick() ||
2362 event.Dragging() ||
2363 ((event.Moving() || event.RightUp()) && m_isDragging)) )
2364 {
2365 event.Skip();
2366
2367 return;
2368 }
2369
2370 wxClientDC dc(this);
2371 PrepareDC(dc);
2372 wxCoord x = dc.DeviceToLogicalX( event.GetX() );
2373 wxCoord y = dc.DeviceToLogicalY( event.GetY() );
2374
2375 int flags = 0;
2376 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), this, flags);
2377
2378 if ( event.Dragging() && !m_isDragging )
2379 {
2380 if (m_dragCount == 0)
2381 m_dragStart = wxPoint(x,y);
2382
2383 m_dragCount++;
2384
2385 if (m_dragCount != 3)
2386 {
2387 // wait until user drags a bit further...
2388 return;
2389 }
2390
2391 wxEventType command = event.RightIsDown()
2392 ? wxEVT_COMMAND_TREE_BEGIN_RDRAG
2393 : wxEVT_COMMAND_TREE_BEGIN_DRAG;
2394
2395 wxTreeEvent nevent( command, GetId() );
2396 nevent.m_item = (long) m_current;
2397 nevent.SetEventObject(this);
2398
2399 // by default the dragging is not supported, the user code must
2400 // explicitly allow the event for it to take place
2401 nevent.Veto();
2402
2403 if ( GetEventHandler()->ProcessEvent(nevent) && nevent.IsAllowed() )
2404 {
2405 // we're going to drag this item
2406 m_isDragging = TRUE;
2407
2408 // remember the old cursor because we will change it while
2409 // dragging
2410 m_oldCursor = m_cursor;
2411
2412 // in a single selection control, hide the selection temporarily
2413 if ( !(GetWindowStyleFlag() & wxTR_MULTIPLE) )
2414 {
2415 m_oldSelection = (wxGenericTreeItem*) GetSelection().m_pItem;
2416
2417 if ( m_oldSelection )
2418 {
2419 m_oldSelection->SetHilight(FALSE);
2420 RefreshLine(m_oldSelection);
2421 }
2422 }
2423
2424 CaptureMouse();
2425 }
2426 }
2427 else if ( event.Moving() )
2428 {
2429 if ( item != m_dropTarget )
2430 {
2431 // unhighlight the previous drop target
2432 DrawDropEffect(m_dropTarget);
2433
2434 m_dropTarget = item;
2435
2436 // highlight the current drop target if any
2437 DrawDropEffect(m_dropTarget);
2438
2439 wxYieldIfNeeded();
2440 }
2441 }
2442 else if ( (event.LeftUp() || event.RightUp()) && m_isDragging )
2443 {
2444 // erase the highlighting
2445 DrawDropEffect(m_dropTarget);
2446
2447 // generate the drag end event
2448 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, GetId());
2449
2450 event.m_item = (long) item;
2451 event.m_pointDrag = wxPoint(x, y);
2452 event.SetEventObject(this);
2453
2454 (void)GetEventHandler()->ProcessEvent(event);
2455
2456 m_isDragging = FALSE;
2457 m_dropTarget = (wxGenericTreeItem *)NULL;
2458
2459 if ( m_oldSelection )
2460 {
2461 m_oldSelection->SetHilight(TRUE);
2462 RefreshLine(m_oldSelection);
2463 m_oldSelection = (wxGenericTreeItem *)NULL;
2464 }
2465
2466 ReleaseMouse();
2467
2468 SetCursor(m_oldCursor);
2469
2470 wxYieldIfNeeded();
2471 }
2472 else
2473 {
2474 // here we process only the messages which happen on tree items
2475
2476 m_dragCount = 0;
2477
2478 if (item == NULL) return; /* we hit the blank area */
2479
2480 if ( event.RightDown() )
2481 {
2482 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, GetId());
2483 nevent.m_item = (long) item;
2484 nevent.m_code = 0;
2485 CalcScrolledPosition(x, y,
2486 &nevent.m_pointDrag.x,
2487 &nevent.m_pointDrag.y);
2488 nevent.SetEventObject(this);
2489 GetEventHandler()->ProcessEvent(nevent);
2490 }
2491 else if ( event.LeftUp() )
2492 {
2493 if ( m_lastOnSame )
2494 {
2495 if ( (item == m_current) &&
2496 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
2497 HasFlag(wxTR_EDIT_LABELS) )
2498 {
2499 if ( m_renameTimer->IsRunning() )
2500 m_renameTimer->Stop();
2501
2502 m_renameTimer->Start( 100, TRUE );
2503 }
2504
2505 m_lastOnSame = FALSE;
2506 }
2507 }
2508 else // !RightDown() && !LeftUp() ==> LeftDown() || LeftDClick()
2509 {
2510 if ( event.LeftDown() )
2511 {
2512 m_lastOnSame = item == m_current;
2513 }
2514
2515 if ( flags & wxTREE_HITTEST_ONITEMBUTTON )
2516 {
2517 // only toggle the item for a single click, double click on
2518 // the button doesn't do anything (it toggles the item twice)
2519 if ( event.LeftDown() )
2520 {
2521 Toggle( item );
2522 }
2523
2524 // don't select the item if the button was clicked
2525 return;
2526 }
2527
2528 // how should the selection work for this event?
2529 bool is_multiple, extended_select, unselect_others;
2530 EventFlagsToSelType(GetWindowStyleFlag(),
2531 event.ShiftDown(),
2532 event.ControlDown(),
2533 is_multiple, extended_select, unselect_others);
2534
2535 SelectItem(item, unselect_others, extended_select);
2536
2537 if ( event.LeftDClick() )
2538 {
2539 // double clicking should not start editing the item label
2540 m_renameTimer->Stop();
2541 m_lastOnSame = FALSE;
2542
2543 wxTreeEvent nevent( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2544 nevent.m_item = (long) item;
2545 nevent.m_code = 0;
2546 CalcScrolledPosition(x, y,
2547 &nevent.m_pointDrag.x,
2548 &nevent.m_pointDrag.y);
2549 nevent.SetEventObject( this );
2550 GetEventHandler()->ProcessEvent( nevent );
2551 }
2552 }
2553 }
2554}
2555
2556void wxGenericTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
2557{
2558 /* after all changes have been done to the tree control,
2559 * we actually redraw the tree when everything is over */
2560
2561 if (!m_dirty)
2562 return;
2563
2564 m_dirty = FALSE;
2565
2566 CalculatePositions();
2567 Refresh();
2568 AdjustMyScrollbars();
2569}
2570
2571void wxGenericTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
2572{
2573 wxCoord text_w = 0;
2574 wxCoord text_h = 0;
2575
2576 if (item->IsBold())
2577 dc.SetFont(m_boldFont);
2578
2579 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
2580 text_h+=2;
2581
2582 // restore normal font
2583 dc.SetFont( m_normalFont );
2584
2585 int image_h = 0;
2586 int image_w = 0;
2587 int image = item->GetCurrentImage();
2588 if ( image != NO_IMAGE )
2589 {
2590 if ( m_imageListNormal )
2591 {
2592 m_imageListNormal->GetSize( image, image_w, image_h );
2593 image_w += 4;
2594 }
2595 }
2596
2597 int total_h = (image_h > text_h) ? image_h : text_h;
2598
2599 if (total_h < 40)
2600 total_h += 2; // at least 2 pixels
2601 else
2602 total_h += total_h/10; // otherwise 10% extra spacing
2603
2604 item->SetHeight(total_h);
2605 if (total_h>m_lineHeight)
2606 m_lineHeight=total_h;
2607
2608 item->SetWidth(image_w+text_w+2);
2609}
2610
2611// -----------------------------------------------------------------------------
2612// for developper : y is now the top of the level
2613// not the middle of it !
2614void wxGenericTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
2615{
2616 int horizX = level*m_indent;
2617
2618 CalculateSize( item, dc );
2619
2620 // set its position
2621 item->SetX( horizX+m_indent+m_spacing );
2622 item->SetY( y );
2623 y+=GetLineHeight(item);
2624
2625 if ( !item->IsExpanded() )
2626 {
2627 // we dont need to calculate collapsed branches
2628 return;
2629 }
2630
2631 wxArrayGenericTreeItems& children = item->GetChildren();
2632 size_t n, count = children.Count();
2633 for (n = 0; n < count; ++n )
2634 CalculateLevel( children[n], dc, level+1, y ); // recurse
2635}
2636
2637void wxGenericTreeCtrl::CalculatePositions()
2638{
2639 if ( !m_anchor ) return;
2640
2641 wxClientDC dc(this);
2642 PrepareDC( dc );
2643
2644 dc.SetFont( m_normalFont );
2645
2646 dc.SetPen( m_dottedPen );
2647 //if(GetImageList() == NULL)
2648 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
2649
2650 int y = 2;
2651 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
2652}
2653
2654void wxGenericTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
2655{
2656 if (m_dirty) return;
2657
2658 wxClientDC dc(this);
2659 PrepareDC(dc);
2660
2661 int cw = 0;
2662 int ch = 0;
2663 GetClientSize( &cw, &ch );
2664
2665 wxRect rect;
2666 rect.x = dc.LogicalToDeviceX( 0 );
2667 rect.width = cw;
2668 rect.y = dc.LogicalToDeviceY( item->GetY() );
2669 rect.height = ch;
2670
2671 Refresh( TRUE, &rect );
2672
2673 AdjustMyScrollbars();
2674}
2675
2676void wxGenericTreeCtrl::RefreshLine( wxGenericTreeItem *item )
2677{
2678 if (m_dirty) return;
2679
2680 wxClientDC dc(this);
2681 PrepareDC( dc );
2682
2683 int cw = 0;
2684 int ch = 0;
2685 GetClientSize( &cw, &ch );
2686
2687 wxRect rect;
2688 rect.x = dc.LogicalToDeviceX( 0 );
2689 rect.y = dc.LogicalToDeviceY( item->GetY() );
2690 rect.width = cw;
2691 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
2692
2693 Refresh( TRUE, &rect );
2694}
2695