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