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