]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectrl.cpp
Added an OS/2 section to the sql defines
[wxWidgets.git] / src / generic / treectrl.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: treectrl.cpp
f135ff73 3// Purpose: generic tree control implementation
c801d85f
KB
4// Author: Robert Roebling
5// Created: 01/02/97
f135ff73 6// Modified: 22/10/98 - almost total rewrite, simpler interface (VZ)
389cdc7a 7// Id: $Id$
c801d85f 8// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
29d87bba 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
f135ff73
VZ
12// =============================================================================
13// declarations
14// =============================================================================
15
16// -----------------------------------------------------------------------------
17// headers
18// -----------------------------------------------------------------------------
19
c801d85f 20#ifdef __GNUG__
f135ff73 21 #pragma implementation "treectrl.h"
c801d85f
KB
22#endif
23
1e6d9499
JS
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28#pragma hdrstop
29#endif
30
f98e2558 31#include "wx/treectrl.h"
f60d0f94 32#include "wx/generic/imaglist.h"
c801d85f 33#include "wx/settings.h"
389cdc7a 34#include "wx/log.h"
f135ff73
VZ
35#include "wx/intl.h"
36#include "wx/dynarray.h"
91b8de8d 37#include "wx/arrimpl.cpp"
f135ff73 38#include "wx/dcclient.h"
0659e7ee 39#include "wx/msgdlg.h"
c801d85f 40
f135ff73
VZ
41// -----------------------------------------------------------------------------
42// array types
43// -----------------------------------------------------------------------------
c801d85f 44
1e6d9499
JS
45class WXDLLEXPORT wxGenericTreeItem;
46
91b8de8d
RR
47WX_DEFINE_ARRAY(wxGenericTreeItem *, wxArrayGenericTreeItems);
48WX_DEFINE_OBJARRAY(wxArrayTreeItemIds);
c801d85f 49
8dc99046
VZ
50// ----------------------------------------------------------------------------
51// constants
52// ----------------------------------------------------------------------------
53
54static const int NO_IMAGE = -1;
55
f135ff73
VZ
56// -----------------------------------------------------------------------------
57// private classes
58// -----------------------------------------------------------------------------
59
60// a tree item
61class WXDLLEXPORT wxGenericTreeItem
c801d85f 62{
f135ff73 63public:
9ec64fa7
VZ
64 // ctors & dtor
65 wxGenericTreeItem() { m_data = NULL; }
66 wxGenericTreeItem( wxGenericTreeItem *parent,
67 const wxString& text,
68 wxDC& dc,
69 int image, int selImage,
70 wxTreeItemData *data );
f135ff73 71
9ec64fa7 72 ~wxGenericTreeItem();
f135ff73 73
9ec64fa7
VZ
74 // trivial accessors
75 wxArrayGenericTreeItems& GetChildren() { return m_children; }
f135ff73 76
9ec64fa7
VZ
77 const wxString& GetText() const { return m_text; }
78 int GetImage(wxTreeItemIcon which = wxTreeItemIcon_Normal) const
8dc99046 79 { return m_images[which]; }
9ec64fa7 80 wxTreeItemData *GetData() const { return m_data; }
f135ff73 81
9ec64fa7
VZ
82 // returns the current image for the item (depending on its
83 // selected/expanded/whatever state)
84 int GetCurrentImage() const;
8dc99046 85
9ec64fa7
VZ
86 void SetText( const wxString &text );
87 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
88 void SetData(wxTreeItemData *data) { m_data = data; }
f135ff73 89
9ec64fa7 90 void SetHasPlus(bool has = TRUE) { m_hasPlus = has; }
f135ff73 91
9ec64fa7 92 void SetBold(bool bold) { m_isBold = bold; }
ef44a621 93
9ec64fa7
VZ
94 int GetX() const { return m_x; }
95 int GetY() const { return m_y; }
f135ff73 96
9ec64fa7
VZ
97 void SetX(int x) { m_x = x; }
98 void SetY(int y) { m_y = y; }
f135ff73 99
9ec64fa7
VZ
100 int GetHeight() const { return m_height; }
101 int GetWidth() const { return m_width; }
91b8de8d 102
9ec64fa7
VZ
103 void SetHeight(int h) { m_height = h; }
104 void SetWidth(int w) { m_width = w; }
91b8de8d
RR
105
106
9ec64fa7 107 wxGenericTreeItem *GetParent() const { return m_parent; }
c801d85f 108
9ec64fa7
VZ
109 // operations
110 // deletes all children notifying the treectrl about it if !NULL
111 // pointer given
112 void DeleteChildren(wxTreeCtrl *tree = NULL);
113 // FIXME don't know what is it for
114 void Reset();
f135ff73 115
9ec64fa7
VZ
116 // get count of all children (and grand children if 'recursively')
117 size_t GetChildrenCount(bool recursively = TRUE) const;
f135ff73 118
9ec64fa7 119 void Insert(wxGenericTreeItem *child, size_t index)
f135ff73
VZ
120 { m_children.Insert(child, index); }
121
9ec64fa7
VZ
122 void SetCross( int x, int y );
123 void GetSize( int &x, int &y, const wxTreeCtrl* );
f135ff73 124
9ec64fa7
VZ
125 // return the item at given position (or NULL if no item), onButton is
126 // TRUE if the point belongs to the item's button, otherwise it lies
127 // on the button's label
128 wxGenericTreeItem *HitTest( const wxPoint& point, const wxTreeCtrl *, int &flags);
f135ff73 129
9ec64fa7
VZ
130 void Expand() { m_isCollapsed = FALSE; }
131 void Collapse() { m_isCollapsed = TRUE; }
f135ff73 132
9ec64fa7 133 void SetHilight( bool set = TRUE ) { m_hasHilight = set; }
f135ff73 134
9ec64fa7
VZ
135 // status inquiries
136 bool HasChildren() const { return !m_children.IsEmpty(); }
137 bool IsSelected() const { return m_hasHilight; }
138 bool IsExpanded() const { return !m_isCollapsed; }
139 bool HasPlus() const { return m_hasPlus || HasChildren(); }
140 bool IsBold() const { return m_isBold; }
141
142 // attributes
143 // get them - may be NULL
144 wxTreeItemAttr *GetAttributes() const { return m_attr; }
145 // get them ensuring that the pointer is not NULL
146 wxTreeItemAttr& Attr()
147 {
148 if ( !m_attr )
149 m_attr = new wxTreeItemAttr;
150
151 return *m_attr;
152 }
f135ff73
VZ
153
154private:
9ec64fa7
VZ
155 wxString m_text;
156
157 // tree ctrl images for the normal, selected, expanded and
158 // expanded+selected states
159 int m_images[wxTreeItemIcon_Max];
160
161 wxTreeItemData *m_data;
162
163 // use bitfields to save size
164 int m_isCollapsed :1;
165 int m_hasHilight :1; // same as focused
166 int m_hasPlus :1; // used for item which doesn't have
167 // children but has a [+] button
168 int m_isBold :1; // render the label in bold font
169
6f2a55e3
VZ
170 wxCoord m_x, m_y;
171 wxCoord m_height, m_width;
9ec64fa7
VZ
172 int m_xCross, m_yCross;
173 int m_level;
174
175 wxArrayGenericTreeItems m_children;
176 wxGenericTreeItem *m_parent;
177
178 wxTreeItemAttr *m_attr;
f135ff73
VZ
179};
180
181// =============================================================================
182// implementation
183// =============================================================================
184
06b466c7
VZ
185// ----------------------------------------------------------------------------
186// private functions
187// ----------------------------------------------------------------------------
188
189// translate the key or mouse event flags to the type of selection we're
190// dealing with
191static void EventFlagsToSelType(long style,
192 bool shiftDown,
193 bool ctrlDown,
194 bool *is_multiple,
195 bool *extended_select,
196 bool *unselect_others)
197{
198 *is_multiple = (style & wxTR_MULTIPLE) != 0;
199 *extended_select = shiftDown && is_multiple;
200 *unselect_others = !(extended_select || (ctrlDown && is_multiple));
201}
e179bd65
RR
202
203// -----------------------------------------------------------------------------
204// wxTreeRenameTimer (internal)
205// -----------------------------------------------------------------------------
206
207wxTreeRenameTimer::wxTreeRenameTimer( wxTreeCtrl *owner )
208{
209 m_owner = owner;
210}
211
212void wxTreeRenameTimer::Notify()
213{
214 m_owner->OnRenameTimer();
215}
216
217//-----------------------------------------------------------------------------
218// wxTreeTextCtrl (internal)
219//-----------------------------------------------------------------------------
220
221IMPLEMENT_DYNAMIC_CLASS(wxTreeTextCtrl,wxTextCtrl);
222
223BEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
224 EVT_CHAR (wxTreeTextCtrl::OnChar)
225 EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
226END_EVENT_TABLE()
227
674ac8b9
VZ
228wxTreeTextCtrl::wxTreeTextCtrl( wxWindow *parent,
229 const wxWindowID id,
230 bool *accept,
231 wxString *res,
232 wxTreeCtrl *owner,
233 const wxString &value,
234 const wxPoint &pos,
235 const wxSize &size,
236 int style,
237 const wxValidator& validator,
238 const wxString &name )
239 : wxTextCtrl( parent, id, value, pos, size, style, validator, name )
e179bd65
RR
240{
241 m_res = res;
242 m_accept = accept;
243 m_owner = owner;
5f1ea0ee
RR
244 (*m_accept) = FALSE;
245 (*m_res) = "";
246 m_startValue = value;
e179bd65
RR
247}
248
249void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
250{
251 if (event.m_keyCode == WXK_RETURN)
252 {
253 (*m_accept) = TRUE;
254 (*m_res) = GetValue();
9dfbf520 255 m_owner->SetFocus();
e179bd65
RR
256 return;
257 }
258 if (event.m_keyCode == WXK_ESCAPE)
259 {
260 (*m_accept) = FALSE;
261 (*m_res) = "";
9dfbf520 262 m_owner->SetFocus();
e179bd65
RR
263 return;
264 }
265 event.Skip();
266}
267
268void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
269{
5f1ea0ee
RR
270 if (wxPendingDelete.Member(this)) return;
271
272 wxPendingDelete.Append(this);
9dfbf520 273
5f1ea0ee
RR
274 if ((*m_accept) && ((*m_res) != m_startValue))
275 m_owner->OnRenameAccept();
e179bd65
RR
276}
277
91b8de8d 278#define PIXELS_PER_UNIT 10
f135ff73 279// -----------------------------------------------------------------------------
c801d85f 280// wxTreeEvent
f135ff73 281// -----------------------------------------------------------------------------
c801d85f 282
e179bd65 283IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
9dfbf520 284
f135ff73 285wxTreeEvent::wxTreeEvent( wxEventType commandType, int id )
92976ab6 286 : wxNotifyEvent( commandType, id )
c801d85f 287{
00e12320
RR
288 m_code = 0;
289 m_itemOld = (wxGenericTreeItem *)NULL;
edaa81ae 290}
c801d85f 291
f135ff73 292// -----------------------------------------------------------------------------
c801d85f 293// wxGenericTreeItem
f135ff73 294// -----------------------------------------------------------------------------
c801d85f 295
f135ff73
VZ
296wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
297 const wxString& text,
406005d2 298 wxDC& WXUNUSED(dc),
f135ff73
VZ
299 int image, int selImage,
300 wxTreeItemData *data)
301 : m_text(text)
c801d85f 302{
00e12320
RR
303 m_images[wxTreeItemIcon_Normal] = image;
304 m_images[wxTreeItemIcon_Selected] = selImage;
305 m_images[wxTreeItemIcon_Expanded] = NO_IMAGE;
306 m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE;
8dc99046 307
00e12320
RR
308 m_data = data;
309 m_x = m_y = 0;
310 m_xCross = m_yCross = 0;
f135ff73 311
00e12320 312 m_level = 0;
f135ff73 313
00e12320
RR
314 m_isCollapsed = TRUE;
315 m_hasHilight = FALSE;
316 m_hasPlus = FALSE;
317 m_isBold = FALSE;
c801d85f 318
00e12320 319 m_parent = parent;
f135ff73 320
00e12320 321 m_attr = (wxTreeItemAttr *)NULL;
06b466c7 322
00e12320
RR
323 // We don't know the height here yet.
324 m_width = 0;
325 m_height = 0;
edaa81ae 326}
c801d85f 327
f135ff73 328wxGenericTreeItem::~wxGenericTreeItem()
c801d85f 329{
00e12320 330 delete m_data;
4832f7c0 331
00e12320 332 delete m_attr;
9ec64fa7 333
00e12320
RR
334 wxASSERT_MSG( m_children.IsEmpty(),
335 wxT("please call DeleteChildren() before deleting the item") );
372edb9d
VZ
336}
337
a43a4f9d 338void wxGenericTreeItem::DeleteChildren(wxTreeCtrl *tree)
372edb9d 339{
00e12320
RR
340 size_t count = m_children.Count();
341 for ( size_t n = 0; n < count; n++ )
a43a4f9d 342 {
00e12320
RR
343 wxGenericTreeItem *child = m_children[n];
344 if (tree)
345 tree->SendDeleteEvent(child);
a43a4f9d 346
00e12320
RR
347 child->DeleteChildren(tree);
348 delete child;
349 }
372edb9d 350
00e12320 351 m_children.Empty();
edaa81ae 352}
c801d85f 353
91b8de8d 354void wxGenericTreeItem::SetText( const wxString &text )
c801d85f 355{
00e12320 356 m_text = text;
edaa81ae 357}
c801d85f 358
74bedbeb 359void wxGenericTreeItem::Reset()
c801d85f 360{
00e12320
RR
361 m_text.Empty();
362 for ( int i = 0; i < wxTreeItemIcon_Max; i++ )
363 {
364 m_images[i] = NO_IMAGE;
365 }
8dc99046 366
00e12320
RR
367 m_data = NULL;
368 m_x = m_y =
369 m_height = m_width = 0;
370 m_xCross =
371 m_yCross = 0;
74bedbeb 372
00e12320 373 m_level = 0;
c801d85f 374
00e12320
RR
375 DeleteChildren();
376 m_isCollapsed = TRUE;
c801d85f 377
00e12320 378 m_parent = (wxGenericTreeItem *)NULL;
edaa81ae 379}
c801d85f 380
4832f7c0 381size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
c801d85f 382{
00e12320
RR
383 size_t count = m_children.Count();
384 if ( !recursively )
385 return count;
4832f7c0 386
00e12320 387 size_t total = count;
f2593d0d 388 for (size_t n = 0; n < count; ++n)
00e12320
RR
389 {
390 total += m_children[n]->GetChildrenCount();
391 }
c801d85f 392
00e12320 393 return total;
edaa81ae 394}
c801d85f
KB
395
396void wxGenericTreeItem::SetCross( int x, int y )
397{
00e12320
RR
398 m_xCross = x;
399 m_yCross = y;
edaa81ae 400}
c801d85f 401
91b8de8d 402void wxGenericTreeItem::GetSize( int &x, int &y, const wxTreeCtrl *theTree )
c801d85f 403{
00e12320
RR
404 int bottomY=m_y+theTree->GetLineHeight(this);
405 if ( y < bottomY ) y = bottomY;
406 int width = m_x + m_width;
407 if ( x < width ) x = width;
f135ff73 408
00e12320 409 if (IsExpanded())
4832f7c0 410 {
00e12320
RR
411 size_t count = m_children.Count();
412 for ( size_t n = 0; n < count; ++n )
413 {
414 m_children[n]->GetSize( x, y, theTree );
415 }
df875e59 416 }
edaa81ae 417}
c801d85f 418
f135ff73 419wxGenericTreeItem *wxGenericTreeItem::HitTest( const wxPoint& point,
c193b707
VZ
420 const wxTreeCtrl *theTree,
421 int &flags)
c801d85f 422{
f2593d0d 423 if ((point.y > m_y) && (point.y < m_y + theTree->GetLineHeight(this)))
c801d85f 424 {
f2593d0d
RR
425 if (point.y<m_y+theTree->GetLineHeight(this)/2)
426 flags |= wxTREE_HITTEST_ONITEMUPPERPART;
427 else
428 flags |= wxTREE_HITTEST_ONITEMLOWERPART;
978f38c2 429
f2593d0d
RR
430 // 5 is the size of the plus sign
431 if ((point.x > m_xCross-5) && (point.x < m_xCross+5) &&
432 (point.y > m_yCross-5) && (point.y < m_yCross+5) &&
433 (IsExpanded() || HasPlus()))
434 {
435 flags|=wxTREE_HITTEST_ONITEMBUTTON;
436 return this;
437 }
d3a9f4af 438
f2593d0d
RR
439 if ((point.x >= m_x) && (point.x <= m_x+m_width))
440 {
441 int image_w = -1;
442 int image_h;
0ae7f2a2 443
f2593d0d
RR
444 // assuming every image (normal and selected ) has the same size !
445 if ( (GetImage() != NO_IMAGE) && theTree->m_imageListNormal )
446 theTree->m_imageListNormal->GetSize(GetImage(), image_w, image_h);
91b8de8d 447
f2593d0d
RR
448 if ((image_w != -1) && (point.x <= m_x + image_w + 1))
449 flags |= wxTREE_HITTEST_ONITEMICON;
450 else
451 flags |= wxTREE_HITTEST_ONITEMLABEL;
91b8de8d 452
f2593d0d
RR
453 return this;
454 }
91b8de8d 455
f2593d0d
RR
456 if (point.x < m_x)
457 flags |= wxTREE_HITTEST_ONITEMINDENT;
458 if (point.x > m_x+m_width)
459 flags |= wxTREE_HITTEST_ONITEMRIGHT;
460
461 return this;
462 }
463 else
c801d85f 464 {
f2593d0d
RR
465 if (!m_isCollapsed)
466 {
467 size_t count = m_children.Count();
468 for ( size_t n = 0; n < count; n++ )
469 {
470 wxGenericTreeItem *res = m_children[n]->HitTest( point, theTree, flags );
471 if ( res != NULL )
472 return res;
473 }
474 }
edaa81ae 475 }
f135ff73 476
f2593d0d 477 flags|=wxTREE_HITTEST_NOWHERE;
06b466c7 478
f2593d0d 479 return (wxGenericTreeItem*) NULL;
edaa81ae 480}
c801d85f 481
8dc99046
VZ
482int wxGenericTreeItem::GetCurrentImage() const
483{
484 int image = NO_IMAGE;
485 if ( IsExpanded() )
486 {
487 if ( IsSelected() )
488 {
489 image = GetImage(wxTreeItemIcon_SelectedExpanded);
490 }
491
492 if ( image == NO_IMAGE )
493 {
494 // we usually fall back to the normal item, but try just the
495 // expanded one (and not selected) first in this case
496 image = GetImage(wxTreeItemIcon_Expanded);
497 }
498 }
499 else // not expanded
500 {
501 if ( IsSelected() )
502 image = GetImage(wxTreeItemIcon_Selected);
503 }
504
505 // may be it doesn't have the specific image we want, try the default one
506 // instead
507 if ( image == NO_IMAGE )
508 {
509 image = GetImage();
510 }
511
512 return image;
513}
514
f135ff73
VZ
515// -----------------------------------------------------------------------------
516// wxTreeCtrl implementation
517// -----------------------------------------------------------------------------
518
519IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxScrolledWindow)
520
521BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow)
00e12320
RR
522 EVT_PAINT (wxTreeCtrl::OnPaint)
523 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse)
524 EVT_CHAR (wxTreeCtrl::OnChar)
525 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus)
526 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus)
527 EVT_IDLE (wxTreeCtrl::OnIdle)
f135ff73
VZ
528END_EVENT_TABLE()
529
530// -----------------------------------------------------------------------------
531// construction/destruction
532// -----------------------------------------------------------------------------
91b8de8d 533
f135ff73 534void wxTreeCtrl::Init()
c801d85f 535{
00e12320
RR
536 m_current =
537 m_key_current =
538 m_anchor = (wxGenericTreeItem *) NULL;
539 m_hasFocus = FALSE;
540 m_dirty = FALSE;
f135ff73 541
00e12320
RR
542 m_xScroll = 0;
543 m_yScroll = 0;
544 m_lineHeight = 10;
545 m_indent = 15;
546 m_spacing = 18;
f135ff73 547
00e12320 548 m_hilightBrush = new wxBrush
f135ff73
VZ
549 (
550 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT),
551 wxSOLID
552 );
553
00e12320
RR
554 m_imageListNormal =
555 m_imageListState = (wxImageList *) NULL;
978f38c2 556
00e12320 557 m_dragCount = 0;
9dfbf520 558
00e12320 559 m_renameTimer = new wxTreeRenameTimer( this );
f38374d0 560
00e12320
RR
561 m_normalFont = wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT );
562 m_boldFont = wxFont( m_normalFont.GetPointSize(),
eff869aa
RR
563 m_normalFont.GetFamily(),
564 m_normalFont.GetStyle(),
565 wxBOLD,
566 m_normalFont.GetUnderlined());
edaa81ae 567}
c801d85f 568
f135ff73
VZ
569bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
570 const wxPoint& pos, const wxSize& size,
978f38c2 571 long style,
674ac8b9
VZ
572 const wxValidator &validator,
573 const wxString& name )
c801d85f 574{
00e12320 575 Init();
f135ff73 576
00e12320 577 wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
978f38c2 578
ce4169a4 579#if wxUSE_VALIDATORS
00e12320 580 SetValidator( validator );
ce4169a4 581#endif
f135ff73 582
00e12320
RR
583 SetBackgroundColour( *wxWHITE );
584// m_dottedPen = wxPen( "grey", 0, wxDOT ); too slow under XFree86
585 m_dottedPen = wxPen( "grey", 0, 0 );
f135ff73 586
00e12320 587 return TRUE;
edaa81ae 588}
c801d85f 589
f135ff73 590wxTreeCtrl::~wxTreeCtrl()
c801d85f 591{
00e12320 592 wxDELETE( m_hilightBrush );
a43a4f9d 593
00e12320 594 DeleteAllItems();
9dfbf520 595
00e12320 596 delete m_renameTimer;
edaa81ae 597}
c801d85f 598
f135ff73
VZ
599// -----------------------------------------------------------------------------
600// accessors
601// -----------------------------------------------------------------------------
602
603size_t wxTreeCtrl::GetCount() const
c801d85f 604{
f2593d0d 605 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
edaa81ae 606}
c801d85f 607
f135ff73 608void wxTreeCtrl::SetIndent(unsigned int indent)
c801d85f 609{
f2593d0d
RR
610 m_indent = indent;
611 m_dirty = TRUE;
cf724bce
RR
612}
613
614void wxTreeCtrl::SetSpacing(unsigned int spacing)
615{
f2593d0d
RR
616 m_spacing = spacing;
617 m_dirty = TRUE;
f135ff73 618}
74bedbeb 619
4832f7c0
VZ
620size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
621{
f2593d0d 622 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
4832f7c0 623
f2593d0d 624 return item.m_pItem->GetChildrenCount(recursively);
4832f7c0
VZ
625}
626
f135ff73
VZ
627// -----------------------------------------------------------------------------
628// functions to work with tree items
629// -----------------------------------------------------------------------------
74bedbeb 630
f135ff73
VZ
631wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
632{
f2593d0d 633 wxCHECK_MSG( item.IsOk(), wxT(""), wxT("invalid tree item") );
4832f7c0 634
f2593d0d 635 return item.m_pItem->GetText();
edaa81ae 636}
74bedbeb 637
8dc99046
VZ
638int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
639 wxTreeItemIcon which) const
74bedbeb 640{
f2593d0d 641 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
4832f7c0 642
f2593d0d 643 return item.m_pItem->GetImage(which);
edaa81ae 644}
c801d85f 645
f135ff73 646wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
c801d85f 647{
f2593d0d 648 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
4832f7c0 649
f2593d0d 650 return item.m_pItem->GetData();
edaa81ae 651}
c801d85f 652
f135ff73
VZ
653void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
654{
f2593d0d 655 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 656
f2593d0d
RR
657 wxClientDC dc(this);
658 wxGenericTreeItem *pItem = item.m_pItem;
659 pItem->SetText(text);
660 CalculateSize(pItem, dc);
661 RefreshLine(pItem);
f135ff73 662}
c801d85f 663
8dc99046
VZ
664void wxTreeCtrl::SetItemImage(const wxTreeItemId& item,
665 int image,
666 wxTreeItemIcon which)
f135ff73 667{
223d09f6 668 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 669
8dc99046
VZ
670 wxGenericTreeItem *pItem = item.m_pItem;
671 pItem->SetImage(image, which);
4832f7c0 672
8dc99046
VZ
673 wxClientDC dc(this);
674 CalculateSize(pItem, dc);
675 RefreshLine(pItem);
edaa81ae 676}
c801d85f 677
f135ff73 678void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
c801d85f 679{
f2593d0d 680 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 681
f2593d0d 682 item.m_pItem->SetData(data);
edaa81ae 683}
c801d85f 684
f135ff73 685void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
c801d85f 686{
f2593d0d 687 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
4832f7c0 688
f2593d0d
RR
689 wxGenericTreeItem *pItem = item.m_pItem;
690 pItem->SetHasPlus(has);
691 RefreshLine(pItem);
edaa81ae 692}
c801d85f 693
ef44a621
VZ
694void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
695{
9ec64fa7 696 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
ef44a621 697
9ec64fa7
VZ
698 // avoid redrawing the tree if no real change
699 wxGenericTreeItem *pItem = item.m_pItem;
700 if ( pItem->IsBold() != bold )
701 {
702 pItem->SetBold(bold);
703 RefreshLine(pItem);
704 }
705}
706
707void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
708 const wxColour& col)
709{
710 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
711
712 wxGenericTreeItem *pItem = item.m_pItem;
713 pItem->Attr().SetTextColour(col);
714 RefreshLine(pItem);
715}
716
717void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
718 const wxColour& col)
719{
720 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
721
722 wxGenericTreeItem *pItem = item.m_pItem;
723 pItem->Attr().SetBackgroundColour(col);
724 RefreshLine(pItem);
725}
726
727void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
728{
729 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
730
731 wxGenericTreeItem *pItem = item.m_pItem;
732 pItem->Attr().SetFont(font);
ef44a621 733 RefreshLine(pItem);
ef44a621
VZ
734}
735
f135ff73
VZ
736// -----------------------------------------------------------------------------
737// item status inquiries
738// -----------------------------------------------------------------------------
739
df875e59 740bool wxTreeCtrl::IsVisible(const wxTreeItemId& WXUNUSED(item)) const
c801d85f 741{
f2593d0d 742 wxFAIL_MSG(wxT("not implemented"));
f135ff73 743
f2593d0d 744 return TRUE;
edaa81ae 745}
c801d85f 746
f135ff73 747bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
c801d85f 748{
f2593d0d 749 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 750
f2593d0d 751 return !item.m_pItem->GetChildren().IsEmpty();
edaa81ae 752}
c801d85f 753
f135ff73 754bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
c801d85f 755{
f2593d0d 756 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 757
f2593d0d 758 return item.m_pItem->IsExpanded();
f135ff73 759}
29d87bba 760
f135ff73
VZ
761bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
762{
f2593d0d 763 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
4832f7c0 764
f2593d0d 765 return item.m_pItem->IsSelected();
f135ff73 766}
29d87bba 767
ef44a621
VZ
768bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
769{
f2593d0d 770 wxCHECK_MSG( item.IsOk(), FALSE, wxT("invalid tree item") );
ef44a621 771
f2593d0d 772 return item.m_pItem->IsBold();
ef44a621
VZ
773}
774
f135ff73
VZ
775// -----------------------------------------------------------------------------
776// navigation
777// -----------------------------------------------------------------------------
29d87bba 778
f135ff73
VZ
779wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
780{
223d09f6 781 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
389cdc7a 782
f135ff73
VZ
783 return item.m_pItem->GetParent();
784}
29d87bba 785
f135ff73
VZ
786wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
787{
223d09f6 788 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 789
f135ff73
VZ
790 cookie = 0;
791 return GetNextChild(item, cookie);
792}
29d87bba 793
f135ff73
VZ
794wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
795{
223d09f6 796 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 797
91b8de8d 798 wxArrayGenericTreeItems& children = item.m_pItem->GetChildren();
4832f7c0
VZ
799 if ( (size_t)cookie < children.Count() )
800 {
06b466c7 801 return children.Item((size_t)cookie++);
4832f7c0
VZ
802 }
803 else
804 {
805 // there are no more of them
1e6d9499 806 return wxTreeItemId();
4832f7c0 807 }
f135ff73 808}
29d87bba 809
978f38c2
VZ
810wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
811{
223d09f6 812 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
978f38c2 813
91b8de8d 814 wxArrayGenericTreeItems& children = item.m_pItem->GetChildren();
0a240683 815 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
978f38c2
VZ
816}
817
f135ff73
VZ
818wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
819{
223d09f6 820 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73
VZ
821
822 wxGenericTreeItem *i = item.m_pItem;
823 wxGenericTreeItem *parent = i->GetParent();
824 if ( parent == NULL )
825 {
826 // root item doesn't have any siblings
1e6d9499 827 return wxTreeItemId();
edaa81ae 828 }
4832f7c0 829
91b8de8d 830 wxArrayGenericTreeItems& siblings = parent->GetChildren();
f135ff73 831 int index = siblings.Index(i);
3c67202d 832 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 833
f135ff73 834 size_t n = (size_t)(index + 1);
fdd8d7b5 835 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
edaa81ae 836}
c801d85f 837
f135ff73 838wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
c801d85f 839{
223d09f6 840 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f135ff73
VZ
841
842 wxGenericTreeItem *i = item.m_pItem;
843 wxGenericTreeItem *parent = i->GetParent();
844 if ( parent == NULL )
c801d85f 845 {
f135ff73 846 // root item doesn't have any siblings
1e6d9499 847 return wxTreeItemId();
edaa81ae 848 }
4832f7c0 849
91b8de8d 850 wxArrayGenericTreeItems& siblings = parent->GetChildren();
f135ff73 851 int index = siblings.Index(i);
3c67202d 852 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 853
fdd8d7b5
VZ
854 return index == 0 ? wxTreeItemId()
855 : wxTreeItemId(siblings[(size_t)(index - 1)]);
f135ff73 856}
389cdc7a 857
f135ff73
VZ
858wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
859{
223d09f6 860 wxFAIL_MSG(wxT("not implemented"));
29d87bba 861
1e6d9499 862 return wxTreeItemId();
f135ff73 863}
29d87bba 864
f135ff73
VZ
865wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
866{
223d09f6 867 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 868
223d09f6 869 wxFAIL_MSG(wxT("not implemented"));
29d87bba 870
1e6d9499 871 return wxTreeItemId();
f135ff73 872}
29d87bba 873
f135ff73
VZ
874wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
875{
223d09f6 876 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
29d87bba 877
223d09f6 878 wxFAIL_MSG(wxT("not implemented"));
29d87bba 879
1e6d9499 880 return wxTreeItemId();
edaa81ae 881}
c801d85f 882
f135ff73
VZ
883// -----------------------------------------------------------------------------
884// operations
885// -----------------------------------------------------------------------------
886
887wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
888 size_t previous,
889 const wxString& text,
890 int image, int selImage,
891 wxTreeItemData *data)
c801d85f 892{
f49f2b0c
RR
893 wxGenericTreeItem *parent = parentId.m_pItem;
894 if ( !parent )
895 {
896 // should we give a warning here?
897 return AddRoot(text, image, selImage, data);
898 }
4832f7c0 899
f49f2b0c 900 wxClientDC dc(this);
f38374d0 901 wxGenericTreeItem *item =
f49f2b0c 902 new wxGenericTreeItem( parent, text, dc, image, selImage, data );
74bedbeb 903
f49f2b0c
RR
904 if ( data != NULL )
905 {
906 data->m_pItem = item;
907 }
74bedbeb 908
f49f2b0c 909 parent->Insert( item, previous );
ef44a621 910
f49f2b0c 911 m_dirty = TRUE;
389cdc7a 912
f49f2b0c 913 return item;
4c681997
RR
914}
915
f135ff73
VZ
916wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
917 int image, int selImage,
918 wxTreeItemData *data)
4c681997 919{
f49f2b0c 920 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
389cdc7a 921
f49f2b0c
RR
922 wxClientDC dc(this);
923 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
f135ff73 924 image, selImage, data);
f49f2b0c
RR
925 if ( data != NULL )
926 {
927 data->m_pItem = m_anchor;
928 }
f38374d0 929
f49f2b0c
RR
930 if (!HasFlag(wxTR_MULTIPLE))
931 {
932 m_current = m_key_current = m_anchor;
06b466c7 933 m_current->SetHilight( TRUE );
f49f2b0c 934 }
389cdc7a 935
f49f2b0c
RR
936 Refresh();
937 AdjustMyScrollbars();
a32dd690 938
f49f2b0c 939 return m_anchor;
edaa81ae 940}
c801d85f 941
f135ff73
VZ
942wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
943 const wxString& text,
944 int image, int selImage,
945 wxTreeItemData *data)
c801d85f 946{
f2593d0d 947 return DoInsertItem(parent, 0u, text, image, selImage, data);
edaa81ae 948}
c801d85f 949
f135ff73
VZ
950wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId,
951 const wxTreeItemId& idPrevious,
952 const wxString& text,
953 int image, int selImage,
954 wxTreeItemData *data)
c801d85f 955{
f2593d0d
RR
956 wxGenericTreeItem *parent = parentId.m_pItem;
957 if ( !parent )
958 {
959 // should we give a warning here?
960 return AddRoot(text, image, selImage, data);
961 }
c801d85f 962
f2593d0d
RR
963 int index = parent->GetChildren().Index(idPrevious.m_pItem);
964 wxASSERT_MSG( index != wxNOT_FOUND,
965 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
06b466c7 966
f2593d0d
RR
967 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
968}
969
970wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId,
971 size_t before,
972 const wxString& text,
973 int image, int selImage,
974 wxTreeItemData *data)
975{
976 wxGenericTreeItem *parent = parentId.m_pItem;
977 if ( !parent )
978 {
979 // should we give a warning here?
980 return AddRoot(text, image, selImage, data);
981 }
982
983 return DoInsertItem(parentId, before, text, image, selImage, data);
edaa81ae 984}
c801d85f 985
f135ff73
VZ
986wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId,
987 const wxString& text,
988 int image, int selImage,
989 wxTreeItemData *data)
74bedbeb 990{
f2593d0d
RR
991 wxGenericTreeItem *parent = parentId.m_pItem;
992 if ( !parent )
993 {
994 // should we give a warning here?
995 return AddRoot(text, image, selImage, data);
996 }
f135ff73 997
f2593d0d
RR
998 return DoInsertItem( parent, parent->GetChildren().Count(), text,
999 image, selImage, data);
74bedbeb
VZ
1000}
1001
a43a4f9d
VZ
1002void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
1003{
f2593d0d
RR
1004 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
1005 event.m_item = item;
1006 event.SetEventObject( this );
1007 ProcessEvent( event );
a43a4f9d
VZ
1008}
1009
372edb9d
VZ
1010void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
1011{
1012 wxGenericTreeItem *item = itemId.m_pItem;
a43a4f9d 1013 item->DeleteChildren(this);
372edb9d
VZ
1014
1015 m_dirty = TRUE;
1016}
1017
f135ff73 1018void wxTreeCtrl::Delete(const wxTreeItemId& itemId)
c801d85f 1019{
97d7bfb8
RR
1020 wxGenericTreeItem *item = itemId.m_pItem;
1021 wxGenericTreeItem *parent = item->GetParent();
ff5bf259 1022
97d7bfb8
RR
1023 if ( parent )
1024 {
1025 parent->GetChildren().Remove( item ); // remove by value
1026 }
f135ff73 1027
97d7bfb8
RR
1028 item->DeleteChildren(this);
1029 SendDeleteEvent(item);
1030 delete item;
f135ff73 1031
97d7bfb8 1032 m_dirty = TRUE;
edaa81ae 1033}
c801d85f 1034
f135ff73 1035void wxTreeCtrl::DeleteAllItems()
c801d85f 1036{
97d7bfb8
RR
1037 if ( m_anchor )
1038 {
1039 m_anchor->DeleteChildren(this);
1040 delete m_anchor;
a43a4f9d 1041
97d7bfb8 1042 m_anchor = NULL;
f135ff73 1043
97d7bfb8
RR
1044 m_dirty = TRUE;
1045 }
edaa81ae
RR
1046}
1047
f135ff73 1048void wxTreeCtrl::Expand(const wxTreeItemId& itemId)
edaa81ae 1049{
f2593d0d 1050 wxGenericTreeItem *item = itemId.m_pItem;
f135ff73 1051
f2593d0d
RR
1052 if ( !item->HasPlus() )
1053 return;
978f38c2 1054
f2593d0d
RR
1055 if ( item->IsExpanded() )
1056 return;
f135ff73 1057
f2593d0d
RR
1058 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
1059 event.m_item = item;
1060 event.SetEventObject( this );
004fd0c8 1061
d5a07b9e 1062// if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
f2593d0d
RR
1063 if ( ProcessEvent( event ) && !event.IsAllowed() )
1064 {
1065 // cancelled by program
1066 return;
1067 }
4832f7c0 1068
f2593d0d
RR
1069 item->Expand();
1070 CalculatePositions();
f135ff73 1071
f2593d0d 1072 RefreshSubtree(item);
f135ff73 1073
f2593d0d
RR
1074 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
1075 ProcessEvent( event );
edaa81ae
RR
1076}
1077
f135ff73 1078void wxTreeCtrl::Collapse(const wxTreeItemId& itemId)
edaa81ae 1079{
f2593d0d 1080 wxGenericTreeItem *item = itemId.m_pItem;
f135ff73 1081
f2593d0d
RR
1082 if ( !item->IsExpanded() )
1083 return;
f135ff73 1084
f2593d0d
RR
1085 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
1086 event.m_item = item;
1087 event.SetEventObject( this );
1088 if ( ProcessEvent( event ) && !event.IsAllowed() )
1089 {
1090 // cancelled by program
1091 return;
1092 }
4832f7c0 1093
f2593d0d 1094 item->Collapse();
f135ff73 1095
f2593d0d
RR
1096 wxArrayGenericTreeItems& children = item->GetChildren();
1097 size_t count = children.Count();
1098 for ( size_t n = 0; n < count; n++ )
1099 {
1100 Collapse(children[n]);
1101 }
f135ff73 1102
f2593d0d 1103 CalculatePositions();
f135ff73 1104
f2593d0d 1105 RefreshSubtree(item);
f135ff73 1106
f2593d0d
RR
1107 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1108 ProcessEvent( event );
edaa81ae 1109}
c801d85f 1110
f135ff73 1111void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
c801d85f 1112{
00e12320
RR
1113 Collapse(item);
1114 DeleteChildren(item);
edaa81ae 1115}
c801d85f 1116
f135ff73 1117void wxTreeCtrl::Toggle(const wxTreeItemId& itemId)
c801d85f 1118{
00e12320 1119 wxGenericTreeItem *item = itemId.m_pItem;
389cdc7a 1120
00e12320
RR
1121 if (item->IsExpanded())
1122 Collapse(itemId);
1123 else
1124 Expand(itemId);
f135ff73 1125}
389cdc7a 1126
f135ff73
VZ
1127void wxTreeCtrl::Unselect()
1128{
00e12320
RR
1129 if (m_current)
1130 {
1131 m_current->SetHilight( FALSE );
1132 RefreshLine( m_current );
1133 }
edaa81ae 1134}
c801d85f 1135
88ac883a 1136void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
389cdc7a 1137{
00e12320
RR
1138 if (item->IsSelected())
1139 {
1140 item->SetHilight(FALSE);
1141 RefreshLine(item);
1142 }
d3a9f4af 1143
00e12320 1144 if (item->HasChildren())
88ac883a 1145 {
00e12320
RR
1146 wxArrayGenericTreeItems& children = item->GetChildren();
1147 size_t count = children.Count();
1148 for ( size_t n = 0; n < count; ++n )
1149 {
1150 UnselectAllChildren(children[n]);
1151 }
88ac883a
VZ
1152 }
1153}
f135ff73 1154
88ac883a
VZ
1155void wxTreeCtrl::UnselectAll()
1156{
00e12320 1157 UnselectAllChildren(GetRootItem().m_pItem);
88ac883a
VZ
1158}
1159
1160// Recursive function !
1161// To stop we must have crt_item<last_item
91b8de8d 1162// Algorithm :
88ac883a 1163// Tag all next children, when no more children,
d3a9f4af 1164// Move to parent (not to tag)
91b8de8d 1165// Keep going... if we found last_item, we stop.
88ac883a
VZ
1166bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1167{
1168 wxGenericTreeItem *parent = crt_item->GetParent();
1169
00e12320
RR
1170 if (parent == NULL) // This is root item
1171 return TagAllChildrenUntilLast(crt_item, last_item, select);
88ac883a 1172
91b8de8d 1173 wxArrayGenericTreeItems& children = parent->GetChildren();
88ac883a
VZ
1174 int index = children.Index(crt_item);
1175 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1176
1177 size_t count = children.Count();
1178 for (size_t n=(size_t)(index+1); n<count; ++n)
00e12320
RR
1179 {
1180 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
1181 }
88ac883a
VZ
1182
1183 return TagNextChildren(parent, last_item, select);
1184}
1185
1186bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1187{
00e12320
RR
1188 crt_item->SetHilight(select);
1189 RefreshLine(crt_item);
d3a9f4af 1190
06b466c7 1191 if (crt_item==last_item)
00e12320 1192 return TRUE;
88ac883a 1193
00e12320 1194 if (crt_item->HasChildren())
88ac883a 1195 {
00e12320
RR
1196 wxArrayGenericTreeItems& children = crt_item->GetChildren();
1197 size_t count = children.Count();
1198 for ( size_t n = 0; n < count; ++n )
1199 {
06b466c7 1200 if (TagAllChildrenUntilLast(children[n], last_item, select))
00e12320 1201 return TRUE;
06b466c7 1202 }
88ac883a 1203 }
d3a9f4af 1204
c0de7af4 1205 return FALSE;
88ac883a
VZ
1206}
1207
1208void wxTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
1209{
f2593d0d
RR
1210 // item2 is not necessary after item1
1211 wxGenericTreeItem *first=NULL, *last=NULL;
88ac883a 1212
f2593d0d
RR
1213 // choice first' and 'last' between item1 and item2
1214 if (item1->GetY()<item2->GetY())
06b466c7 1215 {
f2593d0d
RR
1216 first=item1;
1217 last=item2;
1218 }
1219 else
1220 {
1221 first=item2;
1222 last=item1;
1223 }
88ac883a 1224
f2593d0d 1225 bool select = m_current->IsSelected();
d3a9f4af 1226
f2593d0d
RR
1227 if ( TagAllChildrenUntilLast(first,last,select) )
1228 return;
88ac883a 1229
f2593d0d 1230 TagNextChildren(first,last,select);
88ac883a
VZ
1231}
1232
d3a9f4af 1233void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId,
c193b707
VZ
1234 bool unselect_others,
1235 bool extended_select)
d3a9f4af 1236{
223d09f6 1237 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
8b04a037 1238
88ac883a 1239 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
5391f772 1240 wxGenericTreeItem *item = itemId.m_pItem;
88ac883a
VZ
1241
1242 //wxCHECK_RET( ( (!unselect_others) && is_single),
223d09f6 1243 // wxT("this is a single selection tree") );
88ac883a
VZ
1244
1245 // to keep going anyhow !!!
d3a9f4af 1246 if (is_single)
8dc99046
VZ
1247 {
1248 if (item->IsSelected())
1249 return; // nothing to do
1250 unselect_others = TRUE;
1251 extended_select = FALSE;
1252 }
1253 else if ( unselect_others && item->IsSelected() )
1254 {
1255 // selection change if there is more than one item currently selected
1256 wxArrayTreeItemIds selected_items;
1257 if ( GetSelections(selected_items) == 1 )
1258 return;
1259 }
d3a9f4af 1260
f135ff73
VZ
1261 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
1262 event.m_item = item;
1263 event.m_itemOld = m_current;
1264 event.SetEventObject( this );
91b8de8d 1265 // TODO : Here we don't send any selection mode yet !
d3a9f4af 1266
f98e2558 1267 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
f135ff73
VZ
1268 return;
1269
88ac883a
VZ
1270 // ctrl press
1271 if (unselect_others)
389cdc7a 1272 {
88ac883a 1273 if (is_single) Unselect(); // to speed up thing
c193b707 1274 else UnselectAll();
edaa81ae 1275 }
f135ff73 1276
88ac883a 1277 // shift press
d3a9f4af 1278 if (extended_select)
88ac883a 1279 {
91b8de8d 1280 if (m_current == NULL) m_current=m_key_current=GetRootItem().m_pItem;
88ac883a
VZ
1281 // don't change the mark (m_current)
1282 SelectItemRange(m_current, item);
1283 }
1284 else
1285 {
c0de7af4 1286 bool select=TRUE; // the default
88ac883a 1287
c193b707
VZ
1288 // Check if we need to toggle hilight (ctrl mode)
1289 if (!unselect_others)
8dc99046 1290 select=!item->IsSelected();
88ac883a 1291
91b8de8d 1292 m_current = m_key_current = item;
c193b707
VZ
1293 m_current->SetHilight(select);
1294 RefreshLine( m_current );
88ac883a 1295 }
389cdc7a 1296
f135ff73 1297 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
6daa0637 1298 GetEventHandler()->ProcessEvent( event );
389cdc7a
VZ
1299}
1300
9dfbf520
VZ
1301void wxTreeCtrl::FillArray(wxGenericTreeItem *item,
1302 wxArrayTreeItemIds &array) const
c801d85f 1303{
8dc99046 1304 if ( item->IsSelected() )
9dfbf520 1305 array.Add(wxTreeItemId(item));
91b8de8d 1306
9dfbf520 1307 if ( item->HasChildren() )
91b8de8d 1308 {
9dfbf520
VZ
1309 wxArrayGenericTreeItems& children = item->GetChildren();
1310 size_t count = children.GetCount();
1311 for ( size_t n = 0; n < count; ++n )
1312 FillArray(children[n],array);
91b8de8d
RR
1313 }
1314}
1315
1316size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
1317{
1318 array.Empty();
1319 FillArray(GetRootItem().m_pItem, array);
1320
1321 return array.Count();
1322}
1323
1324void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
d3a9f4af 1325{
91b8de8d
RR
1326 if (!item.IsOk()) return;
1327
0659e7ee 1328 wxGenericTreeItem *gitem = item.m_pItem;
ef44a621 1329
f65635b5
VZ
1330 // first expand all parent branches
1331 wxGenericTreeItem *parent = gitem->GetParent();
5391f772 1332 while ( parent )
f65635b5 1333 {
8dc99046 1334 Expand(parent);
f65635b5
VZ
1335 parent = parent->GetParent();
1336 }
1337
5391f772 1338 //if (parent) CalculatePositions();
91b8de8d
RR
1339
1340 ScrollTo(item);
1341}
1342
1343void wxTreeCtrl::ScrollTo(const wxTreeItemId &item)
1344{
1345 if (!item.IsOk()) return;
1346
dc6c62a9
RR
1347 // We have to call this here because the label in
1348 // question might just have been added and no screen
1349 // update taken place.
1350 if (m_dirty) wxYield();
1351
91b8de8d
RR
1352 wxGenericTreeItem *gitem = item.m_pItem;
1353
f65635b5 1354 // now scroll to the item
0659e7ee 1355 int item_y = gitem->GetY();
8dc99046 1356
0659e7ee
RR
1357 int start_x = 0;
1358 int start_y = 0;
1359 ViewStart( &start_x, &start_y );
91b8de8d 1360 start_y *= PIXELS_PER_UNIT;
978f38c2 1361
a93109d5
RR
1362 int client_h = 0;
1363 int client_w = 0;
1364 GetClientSize( &client_w, &client_h );
ef44a621 1365
0659e7ee
RR
1366 if (item_y < start_y+3)
1367 {
91b8de8d 1368 // going down
0659e7ee
RR
1369 int x = 0;
1370 int y = 0;
91b8de8d
RR
1371 m_anchor->GetSize( x, y, this );
1372 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1373 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee 1374 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1375 // Item should appear at top
91b8de8d 1376 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
0659e7ee 1377 }
91b8de8d 1378 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
0659e7ee 1379 {
c7a9fa36
RR
1380 // going up
1381 int x = 0;
1382 int y = 0;
1383 m_anchor->GetSize( x, y, this );
1384 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1385 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1386 item_y += PIXELS_PER_UNIT+2;
1387 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1388 // Item should appear at bottom
c7a9fa36 1389 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 );
0659e7ee 1390 }
edaa81ae 1391}
c801d85f 1392
e1ee62bd
VZ
1393// FIXME: tree sorting functions are not reentrant and not MT-safe!
1394static wxTreeCtrl *s_treeBeingSorted = NULL;
0659e7ee 1395
004fd0c8 1396static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
e1ee62bd 1397 wxGenericTreeItem **item2)
edaa81ae 1398{
223d09f6 1399 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
e1ee62bd
VZ
1400
1401 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
0659e7ee
RR
1402}
1403
e1ee62bd
VZ
1404int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1405 const wxTreeItemId& item2)
0659e7ee 1406{
87138c52 1407 return wxStrcmp(GetItemText(item1), GetItemText(item2));
e1ee62bd
VZ
1408}
1409
1410void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId)
1411{
223d09f6 1412 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
e1ee62bd
VZ
1413
1414 wxGenericTreeItem *item = itemId.m_pItem;
978f38c2 1415
e1ee62bd 1416 wxCHECK_RET( !s_treeBeingSorted,
223d09f6 1417 wxT("wxTreeCtrl::SortChildren is not reentrant") );
e1ee62bd 1418
91b8de8d 1419 wxArrayGenericTreeItems& children = item->GetChildren();
e1ee62bd
VZ
1420 if ( children.Count() > 1 )
1421 {
1422 s_treeBeingSorted = this;
1423 children.Sort(tree_ctrl_compare_func);
1424 s_treeBeingSorted = NULL;
978f38c2 1425
e1ee62bd
VZ
1426 m_dirty = TRUE;
1427 }
1428 //else: don't make the tree dirty as nothing changed
edaa81ae
RR
1429}
1430
f135ff73 1431wxImageList *wxTreeCtrl::GetImageList() const
edaa81ae 1432{
f135ff73 1433 return m_imageListNormal;
edaa81ae
RR
1434}
1435
f135ff73 1436wxImageList *wxTreeCtrl::GetStateImageList() const
c801d85f 1437{
f135ff73 1438 return m_imageListState;
edaa81ae 1439}
c801d85f 1440
f135ff73 1441void wxTreeCtrl::SetImageList(wxImageList *imageList)
e2414cbe 1442{
f2593d0d 1443 m_imageListNormal = imageList;
91b8de8d 1444
f2593d0d
RR
1445 // Calculate a m_lineHeight value from the image sizes.
1446 // May be toggle off. Then wxTreeCtrl will spread when
1447 // necessary (which might look ugly).
91b8de8d 1448#if 1
f2593d0d
RR
1449 wxClientDC dc(this);
1450 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1451 int width = 0, height = 0,
1452 n = m_imageListNormal->GetImageCount();
06b466c7 1453
f2593d0d
RR
1454 for (int i = 0; i < n ; i++)
1455 {
1456 m_imageListNormal->GetSize(i, width, height);
1457 if (height > m_lineHeight) m_lineHeight = height;
1458 }
91b8de8d 1459
06b466c7 1460 if (m_lineHeight < 40)
f2593d0d 1461 m_lineHeight += 2; // at least 2 pixels
06b466c7 1462 else
f2593d0d 1463 m_lineHeight += m_lineHeight/10; // otherwise 10% extra spacing
91b8de8d 1464#endif
edaa81ae 1465}
e2414cbe 1466
f135ff73 1467void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
e2414cbe 1468{
f135ff73 1469 m_imageListState = imageList;
edaa81ae 1470}
e2414cbe 1471
f135ff73
VZ
1472// -----------------------------------------------------------------------------
1473// helpers
1474// -----------------------------------------------------------------------------
0659e7ee 1475
74bedbeb 1476void wxTreeCtrl::AdjustMyScrollbars()
c801d85f 1477{
0659e7ee
RR
1478 if (m_anchor)
1479 {
1480 int x = 0;
1481 int y = 0;
91b8de8d 1482 m_anchor->GetSize( x, y, this );
c193b707 1483 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
c7a9fa36 1484 x += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee
RR
1485 int x_pos = GetScrollPos( wxHORIZONTAL );
1486 int y_pos = GetScrollPos( wxVERTICAL );
91b8de8d 1487 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
0659e7ee
RR
1488 }
1489 else
1490 {
1491 SetScrollbars( 0, 0, 0, 0 );
1492 }
edaa81ae 1493}
c801d85f 1494
91b8de8d
RR
1495int wxTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
1496{
dc6c62a9
RR
1497 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
1498 return item->GetHeight();
1499 else
1500 return m_lineHeight;
91b8de8d
RR
1501}
1502
ef44a621
VZ
1503void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
1504{
9ec64fa7
VZ
1505 wxTreeItemAttr *attr = item->GetAttributes();
1506 if ( attr && attr->HasFont() )
1507 dc.SetFont(attr->GetFont());
1508 else if (item->IsBold())
eff869aa 1509 dc.SetFont(m_boldFont);
ef44a621 1510
bbe0af5b
RR
1511 long text_w = 0;
1512 long text_h = 0;
1513 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
ef44a621 1514
bbe0af5b
RR
1515 int image_h = 0;
1516 int image_w = 0;
8dc99046
VZ
1517 int image = item->GetCurrentImage();
1518 if ( image != NO_IMAGE )
bbe0af5b 1519 {
8dc99046 1520 m_imageListNormal->GetSize( image, image_w, image_h );
bbe0af5b
RR
1521 image_w += 4;
1522 }
ef44a621 1523
91b8de8d
RR
1524 int total_h = GetLineHeight(item);
1525
1526 dc.DrawRectangle( item->GetX()-2, item->GetY(), item->GetWidth()+2, total_h );
ef44a621 1527
8dc99046 1528 if ( image != NO_IMAGE )
bbe0af5b 1529 {
d30b4d20 1530 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
8dc99046 1531 m_imageListNormal->Draw( image, dc,
49cd56ef 1532 item->GetX(),
d701d432 1533 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
1534 wxIMAGELIST_DRAW_TRANSPARENT );
1535 dc.DestroyClippingRegion();
1536 }
ef44a621 1537
9ec64fa7
VZ
1538 bool hasBgCol = attr && attr->HasBackgroundColour();
1539 dc.SetBackgroundMode(hasBgCol ? wxSOLID : wxTRANSPARENT);
1540 if ( hasBgCol )
1541 dc.SetTextBackground(attr->GetBackgroundColour());
d30b4d20 1542 dc.DrawText( item->GetText(), image_w + item->GetX(), item->GetY()
49cd56ef 1543 + ((total_h > text_h) ? (total_h - text_h)/2 : 0));
ef44a621 1544
eff869aa
RR
1545 // restore normal font
1546 dc.SetFont( m_normalFont );
ef44a621
VZ
1547}
1548
91b8de8d 1549// Now y stands for the top of the item, whereas it used to stand for middle !
16c1f7f3 1550void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 1551{
bbe0af5b 1552 int horizX = level*m_indent;
389cdc7a 1553
cf724bce 1554 item->SetX( horizX+m_indent+m_spacing );
91b8de8d 1555 item->SetY( y );
4c681997 1556
bbe0af5b 1557 int oldY = y;
91b8de8d
RR
1558 y+=GetLineHeight(item)/2;
1559
1560 item->SetCross( horizX+m_indent, y );
389cdc7a 1561
bbe0af5b 1562 int exposed_x = dc.LogicalToDeviceX( 0 );
5391f772 1563 int exposed_y = dc.LogicalToDeviceY( item->GetY() );
4832f7c0 1564
5391f772 1565 if (IsExposed( exposed_x, exposed_y, 10000, GetLineHeight(item) )) // 10000 = very much
bbe0af5b
RR
1566 {
1567 int startX = horizX;
cf724bce 1568 int endX = horizX + (m_indent-5);
29d87bba 1569
cf724bce 1570// if (!item->HasChildren()) endX += (m_indent+5);
bbe0af5b 1571 if (!item->HasChildren()) endX += 20;
4832f7c0 1572
bbe0af5b 1573 dc.DrawLine( startX, y, endX, y );
29d87bba 1574
bbe0af5b
RR
1575 if (item->HasPlus())
1576 {
cf724bce 1577 dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y );
bbe0af5b
RR
1578 dc.SetPen( *wxGREY_PEN );
1579 dc.SetBrush( *wxWHITE_BRUSH );
cf724bce 1580 dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 );
9dfbf520 1581
bbe0af5b 1582 dc.SetPen( *wxBLACK_PEN );
cf724bce 1583 dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y );
bbe0af5b 1584 if (!item->IsExpanded())
cf724bce 1585 dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 );
9dfbf520 1586
112c5086 1587 dc.SetPen( m_dottedPen );
bbe0af5b 1588 }
c801d85f 1589
9ec64fa7
VZ
1590 wxPen *pen = wxTRANSPARENT_PEN;
1591 wxBrush *brush; // FIXME is this really needed?
1592 wxColour colText;
a367b9b3 1593
9ec64fa7
VZ
1594 if ( item->IsSelected() )
1595 {
1596 colText = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
4832f7c0 1597
9ec64fa7 1598 brush = m_hilightBrush;
4832f7c0 1599
9ec64fa7
VZ
1600 if ( m_hasFocus )
1601 pen = wxBLACK_PEN;
f135ff73 1602
bbe0af5b
RR
1603 }
1604 else
1605 {
9ec64fa7
VZ
1606 wxTreeItemAttr *attr = item->GetAttributes();
1607 if ( attr && attr->HasTextColour() )
1608 colText = attr->GetTextColour();
1609 else
1610 colText = *wxBLACK;
4832f7c0 1611
9ec64fa7 1612 brush = wxWHITE_BRUSH;
bbe0af5b 1613 }
9ec64fa7
VZ
1614
1615 // prepare to draw
1616 dc.SetTextForeground(colText);
1617 dc.SetPen(*pen);
1618 dc.SetBrush(*brush);
1619
1620 // draw
1621 PaintItem(item, dc);
1622
1623 // restore DC objects
1624 dc.SetBrush( *wxWHITE_BRUSH );
1625 dc.SetPen( m_dottedPen );
1626 dc.SetTextForeground( *wxBLACK );
f135ff73 1627 }
d3a9f4af 1628
91b8de8d 1629 y = oldY+GetLineHeight(item);
e2414cbe 1630
bbe0af5b
RR
1631 if (item->IsExpanded())
1632 {
91b8de8d 1633 oldY+=GetLineHeight(item)/2;
9ec64fa7 1634 int semiOldY=0;
389cdc7a 1635
91b8de8d
RR
1636 wxArrayGenericTreeItems& children = item->GetChildren();
1637 size_t n, count = children.Count();
1638 for ( n = 0; n < count; ++n )
c193b707
VZ
1639 {
1640 semiOldY=y;
1641 PaintLevel( children[n], dc, level+1, y );
1642 }
389cdc7a 1643
f65635b5
VZ
1644 // it may happen that the item is expanded but has no items (when you
1645 // delete all its children for example) - don't draw the vertical line
1646 // in this case
1647 if (count > 0)
9ec64fa7 1648 {
c193b707 1649 semiOldY+=GetLineHeight(children[--n])/2;
cf724bce 1650 dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY );
9ec64fa7 1651 }
bbe0af5b 1652 }
4c681997 1653}
c801d85f 1654
91b8de8d
RR
1655void wxTreeCtrl::DrawBorder(wxTreeItemId &item)
1656{
1657 if (!item) return;
1658
1659 wxGenericTreeItem *i=item.m_pItem;
1660
1044a386 1661 wxClientDC dc(this);
91b8de8d
RR
1662 PrepareDC( dc );
1663 dc.SetLogicalFunction(wxINVERT);
1664
1665 int w,h,x;
1666 ViewStart(&x,&h); // we only need x
1667 GetClientSize(&w,&h); // we only need w
1668
1669 h=GetLineHeight(i)+1;
1670 // 2 white column at border
1671 dc.DrawRectangle( PIXELS_PER_UNIT*x+2, i->GetY()-1, w-6, h);
1672}
1673
1674void wxTreeCtrl::DrawLine(wxTreeItemId &item, bool below)
1675{
1676 if (!item) return;
1677
1678 wxGenericTreeItem *i=item.m_pItem;
1679
1044a386 1680 wxClientDC dc(this);
91b8de8d
RR
1681 PrepareDC( dc );
1682 dc.SetLogicalFunction(wxINVERT);
d3a9f4af 1683
91b8de8d
RR
1684 int w,h,y;
1685 GetSize(&w,&h);
1686
1687 if (below) y=i->GetY()+GetLineHeight(i)-1;
1688 else y=i->GetY();
1689
1690 dc.DrawLine( 0, y, w, y);
1691}
1692
f135ff73
VZ
1693// -----------------------------------------------------------------------------
1694// wxWindows callbacks
1695// -----------------------------------------------------------------------------
1696
3db7be80 1697void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
c801d85f 1698{
91b8de8d 1699 if ( !m_anchor)
0659e7ee 1700 return;
c801d85f 1701
0659e7ee
RR
1702 wxPaintDC dc(this);
1703 PrepareDC( dc );
29d87bba 1704
eff869aa 1705 dc.SetFont( m_normalFont );
0659e7ee 1706 dc.SetPen( m_dottedPen );
f38374d0 1707
eff869aa 1708 // this is now done dynamically
91b8de8d
RR
1709 //if(GetImageList() == NULL)
1710 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 1711
91b8de8d 1712 int y = 2;
0659e7ee 1713 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 1714}
c801d85f 1715
3db7be80 1716void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
c801d85f 1717{
0659e7ee 1718 m_hasFocus = TRUE;
978f38c2 1719
bbe0af5b 1720 if (m_current) RefreshLine( m_current );
edaa81ae 1721}
c801d85f 1722
3db7be80 1723void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
c801d85f 1724{
0659e7ee 1725 m_hasFocus = FALSE;
978f38c2 1726
bbe0af5b 1727 if (m_current) RefreshLine( m_current );
edaa81ae 1728}
c801d85f
KB
1729
1730void wxTreeCtrl::OnChar( wxKeyEvent &event )
1731{
978f38c2 1732 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
06b466c7 1733 te.m_code = (int)event.KeyCode();
978f38c2
VZ
1734 te.SetEventObject( this );
1735 GetEventHandler()->ProcessEvent( te );
435fe83e 1736
91b8de8d 1737 if ( (m_current == 0) || (m_key_current == 0) )
978f38c2
VZ
1738 {
1739 event.Skip();
1740 return;
1741 }
ef44a621 1742
06b466c7
VZ
1743 // how should the selection work for this event?
1744 bool is_multiple, extended_select, unselect_others;
1745 EventFlagsToSelType(GetWindowStyleFlag(),
1746 event.ShiftDown(),
1747 event.ControlDown(),
1748 &is_multiple, &extended_select, &unselect_others);
88ac883a 1749
978f38c2
VZ
1750 switch (event.KeyCode())
1751 {
1752 case '+':
1753 case WXK_ADD:
1754 if (m_current->HasPlus() && !IsExpanded(m_current))
1755 {
1756 Expand(m_current);
1757 }
1758 break;
ef44a621 1759
978f38c2
VZ
1760 case '-':
1761 case WXK_SUBTRACT:
1762 if (IsExpanded(m_current))
1763 {
1764 Collapse(m_current);
1765 }
1766 break;
ef44a621 1767
978f38c2
VZ
1768 case '*':
1769 case WXK_MULTIPLY:
1770 Toggle(m_current);
1771 break;
ef44a621 1772
978f38c2
VZ
1773 case ' ':
1774 case WXK_RETURN:
1775 {
1776 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1777 event.m_item = m_current;
1778 event.m_code = 0;
1779 event.SetEventObject( this );
1780 GetEventHandler()->ProcessEvent( event );
1781 }
1782 break;
ef44a621 1783
978f38c2
VZ
1784 // up goes to the previous sibling or to the last of its children if
1785 // it's expanded
1786 case WXK_UP:
1787 {
91b8de8d 1788 wxTreeItemId prev = GetPrevSibling( m_key_current );
978f38c2
VZ
1789 if (!prev)
1790 {
91b8de8d 1791 prev = GetParent( m_key_current );
c193b707
VZ
1792 if (prev)
1793 {
90e58684 1794 long cockie = 0;
91b8de8d 1795 wxTreeItemId current = m_key_current;
90e58684
RR
1796 if (current == GetFirstChild( prev, cockie ))
1797 {
1798 // otherwise we return to where we came from
88ac883a 1799 SelectItem( prev, unselect_others, extended_select );
c193b707
VZ
1800 m_key_current=prev.m_pItem;
1801 EnsureVisible( prev );
90e58684 1802 break;
c193b707 1803 }
978f38c2
VZ
1804 }
1805 }
1806 if (prev)
1807 {
69a282d4 1808 while ( IsExpanded(prev) && HasChildren(prev) )
978f38c2 1809 {
69a282d4
VZ
1810 wxTreeItemId child = GetLastChild(prev);
1811 if ( child )
1812 {
1813 prev = child;
1814 }
978f38c2 1815 }
69a282d4 1816
88ac883a 1817 SelectItem( prev, unselect_others, extended_select );
c193b707 1818 m_key_current=prev.m_pItem;
978f38c2
VZ
1819 EnsureVisible( prev );
1820 }
1821 }
1822 break;
ef44a621 1823
978f38c2
VZ
1824 // left arrow goes to the parent
1825 case WXK_LEFT:
1826 {
1827 wxTreeItemId prev = GetParent( m_current );
1828 if (prev)
1829 {
1830 EnsureVisible( prev );
88ac883a 1831 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
1832 }
1833 }
1834 break;
ef44a621 1835
978f38c2
VZ
1836 case WXK_RIGHT:
1837 // this works the same as the down arrow except that we also expand the
1838 // item if it wasn't expanded yet
1839 Expand(m_current);
1840 // fall through
1841
1842 case WXK_DOWN:
ef44a621 1843 {
91b8de8d 1844 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
978f38c2
VZ
1845 {
1846 long cookie = 0;
91b8de8d 1847 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
88ac883a 1848 SelectItem( child, unselect_others, extended_select );
c193b707 1849 m_key_current=child.m_pItem;
978f38c2
VZ
1850 EnsureVisible( child );
1851 }
1852 else
1853 {
91b8de8d 1854 wxTreeItemId next = GetNextSibling( m_key_current );
b62c3631
RR
1855// if (next == 0)
1856 if (!next)
978f38c2 1857 {
91b8de8d 1858 wxTreeItemId current = m_key_current;
978f38c2
VZ
1859 while (current && !next)
1860 {
1861 current = GetParent( current );
1862 if (current) next = GetNextSibling( current );
1863 }
1864 }
b62c3631
RR
1865// if (next != 0)
1866 if (next)
978f38c2 1867 {
88ac883a 1868 SelectItem( next, unselect_others, extended_select );
c193b707 1869 m_key_current=next.m_pItem;
978f38c2
VZ
1870 EnsureVisible( next );
1871 }
1872 }
ef44a621 1873 }
978f38c2 1874 break;
ef44a621 1875
978f38c2
VZ
1876 // <End> selects the last visible tree item
1877 case WXK_END:
1878 {
1879 wxTreeItemId last = GetRootItem();
1880
1881 while ( last.IsOk() && IsExpanded(last) )
1882 {
1883 wxTreeItemId lastChild = GetLastChild(last);
1884
1885 // it may happen if the item was expanded but then all of
1886 // its children have been deleted - so IsExpanded() returned
1887 // TRUE, but GetLastChild() returned invalid item
1888 if ( !lastChild )
1889 break;
1890
1891 last = lastChild;
1892 }
1893
1894 if ( last.IsOk() )
1895 {
1896 EnsureVisible( last );
88ac883a 1897 SelectItem( last, unselect_others, extended_select );
978f38c2
VZ
1898 }
1899 }
1900 break;
1901
1902 // <Home> selects the root item
1903 case WXK_HOME:
1904 {
1905 wxTreeItemId prev = GetRootItem();
1906 if (prev)
1907 {
1908 EnsureVisible( prev );
88ac883a 1909 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
1910 }
1911 }
1912 break;
1913
1914 default:
1915 event.Skip();
1916 }
edaa81ae 1917}
c801d85f 1918
91b8de8d 1919wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
4f22cf8d 1920{
dc6c62a9
RR
1921 // We have to call this here because the label in
1922 // question might just have been added and no screen
1923 // update taken place.
1924 if (m_dirty) wxYield();
1925
67a7abf7
VZ
1926 wxClientDC dc(this);
1927 PrepareDC(dc);
06b466c7
VZ
1928 wxCoord x = dc.DeviceToLogicalX( point.x );
1929 wxCoord y = dc.DeviceToLogicalY( point.y );
91b8de8d
RR
1930 int w, h;
1931 GetSize(&w, &h);
1932
1933 flags=0;
1934 if (point.x<0) flags|=wxTREE_HITTEST_TOLEFT;
1935 if (point.x>w) flags|=wxTREE_HITTEST_TORIGHT;
1936 if (point.y<0) flags|=wxTREE_HITTEST_ABOVE;
1937 if (point.y>h) flags|=wxTREE_HITTEST_BELOW;
d3a9f4af 1938
91b8de8d 1939 return m_anchor->HitTest( wxPoint(x, y), this, flags);
4f22cf8d
RR
1940}
1941
e179bd65
RR
1942/* **** */
1943
1944void wxTreeCtrl::Edit( const wxTreeItemId& item )
1945{
1946 if (!item.IsOk()) return;
1947
1948 m_currentEdit = item.m_pItem;
9dfbf520 1949
e179bd65
RR
1950 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
1951 te.m_item = m_currentEdit;
1952 te.SetEventObject( this );
1953 GetEventHandler()->ProcessEvent( te );
1954
1955 if (!te.IsAllowed()) return;
8dc99046 1956
dc6c62a9
RR
1957 // We have to call this here because the label in
1958 // question might just have been added and no screen
1959 // update taken place.
1960 if (m_dirty) wxYield();
9dfbf520 1961
e179bd65
RR
1962 wxString s = m_currentEdit->GetText();
1963 int x = m_currentEdit->GetX();
1964 int y = m_currentEdit->GetY();
1965 int w = m_currentEdit->GetWidth();
1966 int h = m_currentEdit->GetHeight();
9dfbf520 1967
5f1ea0ee
RR
1968 int image_h = 0;
1969 int image_w = 0;
8dc99046
VZ
1970
1971 int image = m_currentEdit->GetCurrentImage();
1972 if ( image != NO_IMAGE )
5f1ea0ee 1973 {
8dc99046 1974 m_imageListNormal->GetSize( image, image_w, image_h );
5f1ea0ee
RR
1975 image_w += 4;
1976 }
1977 x += image_w;
1978 w -= image_w + 4; // I don't know why +4 is needed
e179bd65
RR
1979
1980 wxClientDC dc(this);
1981 PrepareDC( dc );
1982 x = dc.LogicalToDeviceX( x );
1983 y = dc.LogicalToDeviceY( y );
1984
1985 wxTreeTextCtrl *text = new wxTreeTextCtrl(
1986 this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) );
1987 text->SetFocus();
1988}
1989
1990void wxTreeCtrl::OnRenameTimer()
1991{
1992 Edit( m_current );
1993}
1994
1995void wxTreeCtrl::OnRenameAccept()
1996{
1997 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
1998 le.m_item = m_currentEdit;
1999 le.SetEventObject( this );
2000 le.m_label = m_renameRes;
2001 GetEventHandler()->ProcessEvent( le );
9dfbf520 2002
e179bd65 2003 if (!le.IsAllowed()) return;
9dfbf520 2004
5f1ea0ee 2005 SetItemText( m_currentEdit, m_renameRes );
e179bd65 2006}
9dfbf520 2007
3db7be80 2008void wxTreeCtrl::OnMouse( wxMouseEvent &event )
c801d85f 2009{
fb882e1c 2010 if ( !(event.LeftUp() || event.RightDown() || event.LeftDClick() || event.Dragging()) ) return;
29d87bba 2011
bbe0af5b 2012 if ( !m_anchor ) return;
978f38c2 2013
bbe0af5b
RR
2014 wxClientDC dc(this);
2015 PrepareDC(dc);
06b466c7
VZ
2016 wxCoord x = dc.DeviceToLogicalX( event.GetX() );
2017 wxCoord y = dc.DeviceToLogicalY( event.GetY() );
29d87bba 2018
91b8de8d
RR
2019 int flags=0;
2020 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), this, flags);
2021 bool onButton = flags & wxTREE_HITTEST_ONITEMBUTTON;
978f38c2 2022
bbe0af5b
RR
2023 if (event.Dragging())
2024 {
fd9811b1 2025 if (m_dragCount == 0)
8dc99046
VZ
2026 m_dragStart = wxPoint(x,y);
2027
fd9811b1 2028 m_dragCount++;
8dc99046
VZ
2029
2030 if (m_dragCount != 3) return;
2031
2032 int command = wxEVT_COMMAND_TREE_BEGIN_DRAG;
2033 if (event.RightIsDown()) command = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
2034
fd9811b1
RR
2035 wxTreeEvent nevent( command, GetId() );
2036 nevent.m_item = m_current;
2037 nevent.SetEventObject(this);
2038 GetEventHandler()->ProcessEvent(nevent);
8dc99046 2039 return;
bbe0af5b 2040 }
fd9811b1
RR
2041 else
2042 {
2043 m_dragCount = 0;
2044 }
2045
2046 if (item == NULL) return; /* we hit the blank area */
978f38c2 2047
004fd0c8 2048 if (event.RightDown()) {
06b466c7
VZ
2049 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK,GetId());
2050 nevent.m_item=item;
2051 nevent.m_code=0;
2052 nevent.SetEventObject(this);
2053 GetEventHandler()->ProcessEvent(nevent);
2054 return;
fb882e1c
UC
2055 }
2056
9dfbf520
VZ
2057 if (event.LeftUp() && (item == m_current) &&
2058 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
2059 HasFlag(wxTR_EDIT_LABELS) )
e179bd65
RR
2060 {
2061 m_renameTimer->Start( 100, TRUE );
2062 return;
2063 }
9dfbf520 2064
06b466c7
VZ
2065 // how should the selection work for this event?
2066 bool is_multiple, extended_select, unselect_others;
2067 EventFlagsToSelType(GetWindowStyleFlag(),
2068 event.ShiftDown(),
2069 event.ControlDown(),
2070 &is_multiple, &extended_select, &unselect_others);
88ac883a 2071
91b8de8d
RR
2072 if (onButton)
2073 {
2074 Toggle( item );
c193b707
VZ
2075 if (is_multiple)
2076 return;
91b8de8d
RR
2077 }
2078
88ac883a 2079 SelectItem(item, unselect_others, extended_select);
29d87bba 2080
bbe0af5b
RR
2081 if (event.LeftDClick())
2082 {
2083 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
2084 event.m_item = item;
2085 event.m_code = 0;
2086 event.SetEventObject( this );
2087 GetEventHandler()->ProcessEvent( event );
2088 }
edaa81ae 2089}
c801d85f 2090
3db7be80
RR
2091void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
2092{
bbe0af5b
RR
2093 /* after all changes have been done to the tree control,
2094 * we actually redraw the tree when everything is over */
ef44a621 2095
f65635b5
VZ
2096 if (!m_dirty)
2097 return;
ef44a621 2098
bbe0af5b 2099 m_dirty = FALSE;
3db7be80 2100
bbe0af5b 2101 CalculatePositions();
91b8de8d 2102 Refresh();
bbe0af5b 2103 AdjustMyScrollbars();
3db7be80
RR
2104}
2105
91b8de8d 2106void wxTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
d3a9f4af 2107{
91b8de8d
RR
2108 long text_w = 0;
2109 long text_h = 0;
9dfbf520 2110
a62867a5 2111 if (item->IsBold())
eff869aa 2112 dc.SetFont(m_boldFont);
9dfbf520 2113
91b8de8d 2114 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
a62867a5 2115 text_h+=2;
91b8de8d 2116
eff869aa
RR
2117 // restore normal font
2118 dc.SetFont( m_normalFont );
9dfbf520 2119
91b8de8d
RR
2120 int image_h = 0;
2121 int image_w = 0;
8dc99046
VZ
2122 int image = item->GetCurrentImage();
2123 if ( image != NO_IMAGE )
91b8de8d 2124 {
8dc99046 2125 m_imageListNormal->GetSize( image, image_w, image_h );
91b8de8d
RR
2126 image_w += 4;
2127 }
2128
2129 int total_h = (image_h > text_h) ? image_h : text_h;
2130
06b466c7 2131 if (total_h < 40)
f2593d0d 2132 total_h += 2; // at least 2 pixels
06b466c7 2133 else
f2593d0d 2134 total_h += total_h/10; // otherwise 10% extra spacing
91b8de8d
RR
2135
2136 item->SetHeight(total_h);
2137 if (total_h>m_lineHeight) m_lineHeight=total_h;
bbe0af5b 2138
91b8de8d
RR
2139 item->SetWidth(image_w+text_w+2);
2140}
2141
2142// -----------------------------------------------------------------------------
2143// for developper : y is now the top of the level
2144// not the middle of it !
bbe0af5b 2145void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 2146{
bbe0af5b 2147 int horizX = level*m_indent;
389cdc7a 2148
91b8de8d 2149 CalculateSize( item, dc );
d3a9f4af 2150
91b8de8d 2151 // set its position
cf724bce 2152 item->SetX( horizX+m_indent+m_spacing );
91b8de8d
RR
2153 item->SetY( y );
2154 y+=GetLineHeight(item);
4c681997 2155
bbe0af5b
RR
2156 if ( !item->IsExpanded() )
2157 {
f65635b5 2158 // we dont need to calculate collapsed branches
bbe0af5b
RR
2159 return;
2160 }
389cdc7a 2161
91b8de8d
RR
2162 wxArrayGenericTreeItems& children = item->GetChildren();
2163 size_t n, count = children.Count();
2164 for (n = 0; n < count; ++n )
f2593d0d 2165 CalculateLevel( children[n], dc, level+1, y ); // recurse
edaa81ae 2166}
c801d85f 2167
74bedbeb 2168void wxTreeCtrl::CalculatePositions()
c801d85f 2169{
bbe0af5b 2170 if ( !m_anchor ) return;
29d87bba 2171
bbe0af5b
RR
2172 wxClientDC dc(this);
2173 PrepareDC( dc );
29d87bba 2174
eff869aa 2175 dc.SetFont( m_normalFont );
29d87bba 2176
bbe0af5b 2177 dc.SetPen( m_dottedPen );
91b8de8d
RR
2178 //if(GetImageList() == NULL)
2179 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 2180
8dc99046 2181 int y = 2;
f65635b5 2182 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
edaa81ae 2183}
c801d85f 2184
f135ff73 2185void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
c801d85f 2186{
e6527f9d
RR
2187 if (m_dirty) return;
2188
bbe0af5b
RR
2189 wxClientDC dc(this);
2190 PrepareDC(dc);
4832f7c0 2191
bbe0af5b
RR
2192 int cw = 0;
2193 int ch = 0;
2194 GetClientSize( &cw, &ch );
4832f7c0 2195
bbe0af5b
RR
2196 wxRect rect;
2197 rect.x = dc.LogicalToDeviceX( 0 );
2198 rect.width = cw;
2199 rect.y = dc.LogicalToDeviceY( item->GetY() );
2200 rect.height = ch;
f135ff73 2201
bbe0af5b 2202 Refresh( TRUE, &rect );
f135ff73 2203
bbe0af5b 2204 AdjustMyScrollbars();
edaa81ae 2205}
c801d85f
KB
2206
2207void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
2208{
e6527f9d
RR
2209 if (m_dirty) return;
2210
bbe0af5b
RR
2211 wxClientDC dc(this);
2212 PrepareDC( dc );
2213
5391f772
SB
2214 int cw = 0;
2215 int ch = 0;
2216 GetClientSize( &cw, &ch );
2217
bbe0af5b 2218 wxRect rect;
5391f772
SB
2219 rect.x = dc.LogicalToDeviceX( 0 );
2220 rect.y = dc.LogicalToDeviceY( item->GetY() );
2221 rect.width = cw;
91b8de8d 2222 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
978f38c2 2223
bbe0af5b 2224 Refresh( TRUE, &rect );
edaa81ae 2225}
c801d85f 2226