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