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