]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectrl.cpp
Committing in .
[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{
f135ff73
VZ
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
f135ff73
VZ
845 wxClientDC dc(this);
846 wxGenericTreeItem *item = new wxGenericTreeItem(parent,
847 text, dc,
848 image, selImage,
849 data);
74bedbeb 850
f135ff73 851 if ( data != NULL )
c801d85f 852 {
f135ff73
VZ
853 data->m_pItem = item;
854 }
74bedbeb 855
f135ff73 856 parent->Insert( item, previous );
ef44a621 857
3db7be80 858 m_dirty = TRUE;
389cdc7a 859
f135ff73 860 return item;
4c681997
RR
861}
862
f135ff73
VZ
863wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
864 int image, int selImage,
865 wxTreeItemData *data)
4c681997 866{
223d09f6 867 wxCHECK_MSG( !m_anchor, wxTreeItemId(), wxT("tree can have only one root") );
389cdc7a 868
f135ff73
VZ
869 wxClientDC dc(this);
870 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
871 image, selImage, data);
872 if ( data != NULL )
873 {
874 data->m_pItem = m_anchor;
875 }
389cdc7a 876
a32dd690 877 Refresh();
91b8de8d 878 AdjustMyScrollbars();
a32dd690 879
f135ff73 880 return m_anchor;
edaa81ae 881}
c801d85f 882
f135ff73
VZ
883wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
884 const wxString& text,
885 int image, int selImage,
886 wxTreeItemData *data)
c801d85f 887{
f135ff73 888 return DoInsertItem(parent, 0u, text, image, selImage, data);
edaa81ae 889}
c801d85f 890
f135ff73
VZ
891wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId,
892 const wxTreeItemId& idPrevious,
893 const wxString& text,
894 int image, int selImage,
895 wxTreeItemData *data)
c801d85f 896{
f135ff73
VZ
897 wxGenericTreeItem *parent = parentId.m_pItem;
898 if ( !parent )
899 {
900 // should we give a warning here?
901 return AddRoot(text, image, selImage, data);
902 }
c801d85f 903
f135ff73 904 int index = parent->GetChildren().Index(idPrevious.m_pItem);
3c67202d 905 wxASSERT_MSG( index != wxNOT_FOUND,
223d09f6 906 wxT("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
91b8de8d 907 return DoInsertItem(parentId, (size_t)++index, text, image, selImage, data);
edaa81ae 908}
c801d85f 909
f135ff73
VZ
910wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId,
911 const wxString& text,
912 int image, int selImage,
913 wxTreeItemData *data)
74bedbeb 914{
f135ff73
VZ
915 wxGenericTreeItem *parent = parentId.m_pItem;
916 if ( !parent )
917 {
918 // should we give a warning here?
919 return AddRoot(text, image, selImage, data);
920 }
921
922 return DoInsertItem(parent, parent->GetChildren().Count(), text,
923 image, selImage, data);
74bedbeb
VZ
924}
925
a43a4f9d
VZ
926void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
927{
928 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
929 event.m_item = item;
930 event.SetEventObject( this );
931 ProcessEvent( event );
932}
933
372edb9d
VZ
934void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
935{
936 wxGenericTreeItem *item = itemId.m_pItem;
a43a4f9d 937 item->DeleteChildren(this);
372edb9d
VZ
938
939 m_dirty = TRUE;
940}
941
f135ff73 942void wxTreeCtrl::Delete(const wxTreeItemId& itemId)
c801d85f 943{
f135ff73 944 wxGenericTreeItem *item = itemId.m_pItem;
ff5bf259
VZ
945 wxGenericTreeItem *parent = item->GetParent();
946
947 if ( parent )
948 {
949 parent->GetChildren().Remove(item);
950 }
f135ff73 951
a43a4f9d
VZ
952 item->DeleteChildren(this);
953 SendDeleteEvent(item);
f135ff73
VZ
954 delete item;
955
4bb19cfb 956 m_dirty = TRUE;
edaa81ae 957}
c801d85f 958
f135ff73 959void wxTreeCtrl::DeleteAllItems()
c801d85f 960{
f135ff73
VZ
961 if ( m_anchor )
962 {
a43a4f9d 963 m_anchor->DeleteChildren(this);
f135ff73 964 delete m_anchor;
a43a4f9d 965
f135ff73
VZ
966 m_anchor = NULL;
967
4bb19cfb 968 m_dirty = TRUE;
f135ff73 969 }
edaa81ae
RR
970}
971
f135ff73 972void wxTreeCtrl::Expand(const wxTreeItemId& itemId)
edaa81ae 973{
f135ff73
VZ
974 wxGenericTreeItem *item = itemId.m_pItem;
975
978f38c2 976 if ( !item->HasPlus() )
4bb19cfb 977 return;
978f38c2 978
f135ff73
VZ
979 if ( item->IsExpanded() )
980 return;
981
982 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
983 event.m_item = item;
984 event.SetEventObject( this );
004fd0c8 985
d5a07b9e
RR
986// if ( ProcessEvent( event ) && event.m_code ) TODO: Was this a typo ?
987 if ( ProcessEvent( event ) && !event.IsAllowed() )
f135ff73
VZ
988 {
989 // cancelled by program
990 return;
991 }
4832f7c0 992
f135ff73 993 item->Expand();
28ab302b 994 CalculatePositions();
f135ff73
VZ
995
996 RefreshSubtree(item);
997
998 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
999 ProcessEvent( event );
edaa81ae
RR
1000}
1001
f135ff73 1002void wxTreeCtrl::Collapse(const wxTreeItemId& itemId)
edaa81ae 1003{
f135ff73
VZ
1004 wxGenericTreeItem *item = itemId.m_pItem;
1005
1006 if ( !item->IsExpanded() )
1007 return;
1008
1009 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
1010 event.m_item = item;
1011 event.SetEventObject( this );
9f6af110 1012 if ( ProcessEvent( event ) && !event.IsAllowed() )
edaa81ae 1013 {
f135ff73
VZ
1014 // cancelled by program
1015 return;
1016 }
4832f7c0 1017
f135ff73
VZ
1018 item->Collapse();
1019
91b8de8d 1020 wxArrayGenericTreeItems& children = item->GetChildren();
f135ff73
VZ
1021 size_t count = children.Count();
1022 for ( size_t n = 0; n < count; n++ )
1023 {
1024 Collapse(children[n]);
edaa81ae 1025 }
f135ff73
VZ
1026
1027 CalculatePositions();
1028
1029 RefreshSubtree(item);
1030
1031 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
1032 ProcessEvent( event );
edaa81ae 1033}
c801d85f 1034
f135ff73 1035void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
c801d85f 1036{
f135ff73 1037 Collapse(item);
372edb9d 1038 DeleteChildren(item);
edaa81ae 1039}
c801d85f 1040
f135ff73 1041void wxTreeCtrl::Toggle(const wxTreeItemId& itemId)
c801d85f 1042{
f135ff73 1043 wxGenericTreeItem *item = itemId.m_pItem;
389cdc7a 1044
f135ff73
VZ
1045 if ( item->IsExpanded() )
1046 Collapse(itemId);
1047 else
1048 Expand(itemId);
1049}
389cdc7a 1050
f135ff73
VZ
1051void wxTreeCtrl::Unselect()
1052{
1053 if ( m_current )
1054 {
1055 m_current->SetHilight( FALSE );
1056 RefreshLine( m_current );
1057 }
edaa81ae 1058}
c801d85f 1059
88ac883a 1060void wxTreeCtrl::UnselectAllChildren(wxGenericTreeItem *item)
389cdc7a 1061{
88ac883a
VZ
1062 item->SetHilight(FALSE);
1063 RefreshLine(item);
d3a9f4af 1064
88ac883a
VZ
1065 if (item->HasChildren())
1066 {
91b8de8d 1067 wxArrayGenericTreeItems& children = item->GetChildren();
88ac883a
VZ
1068 size_t count = children.Count();
1069 for ( size_t n = 0; n < count; ++n )
c193b707 1070 UnselectAllChildren(children[n]);
88ac883a
VZ
1071 }
1072}
f135ff73 1073
88ac883a
VZ
1074void wxTreeCtrl::UnselectAll()
1075{
1076 UnselectAllChildren(GetRootItem().m_pItem);
1077}
1078
1079// Recursive function !
1080// To stop we must have crt_item<last_item
91b8de8d 1081// Algorithm :
88ac883a 1082// Tag all next children, when no more children,
d3a9f4af 1083// Move to parent (not to tag)
91b8de8d 1084// Keep going... if we found last_item, we stop.
88ac883a
VZ
1085bool wxTreeCtrl::TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1086{
1087 wxGenericTreeItem *parent = crt_item->GetParent();
1088
1089 if ( parent == NULL ) // This is root item
1090 return TagAllChildrenUntilLast(crt_item, last_item, select);
1091
91b8de8d 1092 wxArrayGenericTreeItems& children = parent->GetChildren();
88ac883a
VZ
1093 int index = children.Index(crt_item);
1094 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
1095
1096 size_t count = children.Count();
1097 for (size_t n=(size_t)(index+1); n<count; ++n)
c0de7af4 1098 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
88ac883a
VZ
1099
1100 return TagNextChildren(parent, last_item, select);
1101}
1102
1103bool wxTreeCtrl::TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select)
1104{
1105 crt_item->SetHilight(select);
1106 RefreshLine(crt_item);
d3a9f4af 1107
c0de7af4 1108 if (crt_item==last_item) return TRUE;
88ac883a
VZ
1109
1110 if (crt_item->HasChildren())
1111 {
91b8de8d 1112 wxArrayGenericTreeItems& children = crt_item->GetChildren();
88ac883a
VZ
1113 size_t count = children.Count();
1114 for ( size_t n = 0; n < count; ++n )
c193b707 1115 if (TagAllChildrenUntilLast(children[n], last_item, select)) return TRUE;
88ac883a 1116 }
d3a9f4af 1117
c0de7af4 1118 return FALSE;
88ac883a
VZ
1119}
1120
1121void wxTreeCtrl::SelectItemRange(wxGenericTreeItem *item1, wxGenericTreeItem *item2)
1122{
1123 // item2 is not necessary after item1
1124 wxGenericTreeItem *first=NULL, *last=NULL;
1125
1126 // choice first' and 'last' between item1 and item2
d3a9f4af 1127 if (item1->GetY()<item2->GetY())
8dc99046 1128 {
88ac883a
VZ
1129 first=item1;
1130 last=item2;
8dc99046 1131 }
d3a9f4af 1132 else
8dc99046 1133 {
88ac883a
VZ
1134 first=item2;
1135 last=item1;
8dc99046 1136 }
88ac883a 1137
8dc99046 1138 bool select = m_current->IsSelected();
d3a9f4af 1139
8dc99046
VZ
1140 if ( TagAllChildrenUntilLast(first,last,select) )
1141 return;
88ac883a 1142
d3a9f4af 1143 TagNextChildren(first,last,select);
88ac883a
VZ
1144}
1145
d3a9f4af 1146void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId,
c193b707
VZ
1147 bool unselect_others,
1148 bool extended_select)
d3a9f4af 1149{
223d09f6 1150 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
8b04a037 1151
88ac883a 1152 bool is_single=!(GetWindowStyleFlag() & wxTR_MULTIPLE);
5391f772 1153 wxGenericTreeItem *item = itemId.m_pItem;
88ac883a
VZ
1154
1155 //wxCHECK_RET( ( (!unselect_others) && is_single),
223d09f6 1156 // wxT("this is a single selection tree") );
88ac883a
VZ
1157
1158 // to keep going anyhow !!!
d3a9f4af 1159 if (is_single)
8dc99046
VZ
1160 {
1161 if (item->IsSelected())
1162 return; // nothing to do
1163 unselect_others = TRUE;
1164 extended_select = FALSE;
1165 }
1166 else if ( unselect_others && item->IsSelected() )
1167 {
1168 // selection change if there is more than one item currently selected
1169 wxArrayTreeItemIds selected_items;
1170 if ( GetSelections(selected_items) == 1 )
1171 return;
1172 }
d3a9f4af 1173
f135ff73
VZ
1174 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
1175 event.m_item = item;
1176 event.m_itemOld = m_current;
1177 event.SetEventObject( this );
91b8de8d 1178 // TODO : Here we don't send any selection mode yet !
d3a9f4af 1179
f98e2558 1180 if ( GetEventHandler()->ProcessEvent( event ) && !event.IsAllowed() )
f135ff73
VZ
1181 return;
1182
88ac883a
VZ
1183 // ctrl press
1184 if (unselect_others)
389cdc7a 1185 {
88ac883a 1186 if (is_single) Unselect(); // to speed up thing
c193b707 1187 else UnselectAll();
edaa81ae 1188 }
f135ff73 1189
88ac883a 1190 // shift press
d3a9f4af 1191 if (extended_select)
88ac883a 1192 {
91b8de8d 1193 if (m_current == NULL) m_current=m_key_current=GetRootItem().m_pItem;
88ac883a
VZ
1194 // don't change the mark (m_current)
1195 SelectItemRange(m_current, item);
1196 }
1197 else
1198 {
c0de7af4 1199 bool select=TRUE; // the default
88ac883a 1200
c193b707
VZ
1201 // Check if we need to toggle hilight (ctrl mode)
1202 if (!unselect_others)
8dc99046 1203 select=!item->IsSelected();
88ac883a 1204
91b8de8d 1205 m_current = m_key_current = item;
c193b707
VZ
1206 m_current->SetHilight(select);
1207 RefreshLine( m_current );
88ac883a 1208 }
389cdc7a 1209
f135ff73 1210 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
6daa0637 1211 GetEventHandler()->ProcessEvent( event );
389cdc7a
VZ
1212}
1213
9dfbf520
VZ
1214void wxTreeCtrl::FillArray(wxGenericTreeItem *item,
1215 wxArrayTreeItemIds &array) const
c801d85f 1216{
8dc99046 1217 if ( item->IsSelected() )
9dfbf520 1218 array.Add(wxTreeItemId(item));
91b8de8d 1219
9dfbf520 1220 if ( item->HasChildren() )
91b8de8d 1221 {
9dfbf520
VZ
1222 wxArrayGenericTreeItems& children = item->GetChildren();
1223 size_t count = children.GetCount();
1224 for ( size_t n = 0; n < count; ++n )
1225 FillArray(children[n],array);
91b8de8d
RR
1226 }
1227}
1228
1229size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds &array) const
1230{
1231 array.Empty();
1232 FillArray(GetRootItem().m_pItem, array);
1233
1234 return array.Count();
1235}
1236
1237void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
d3a9f4af 1238{
91b8de8d
RR
1239 if (!item.IsOk()) return;
1240
0659e7ee 1241 wxGenericTreeItem *gitem = item.m_pItem;
ef44a621 1242
f65635b5
VZ
1243 // first expand all parent branches
1244 wxGenericTreeItem *parent = gitem->GetParent();
5391f772 1245 while ( parent )
f65635b5 1246 {
8dc99046 1247 Expand(parent);
f65635b5
VZ
1248 parent = parent->GetParent();
1249 }
1250
5391f772 1251 //if (parent) CalculatePositions();
91b8de8d
RR
1252
1253 ScrollTo(item);
1254}
1255
1256void wxTreeCtrl::ScrollTo(const wxTreeItemId &item)
1257{
1258 if (!item.IsOk()) return;
1259
dc6c62a9
RR
1260 // We have to call this here because the label in
1261 // question might just have been added and no screen
1262 // update taken place.
1263 if (m_dirty) wxYield();
1264
91b8de8d
RR
1265 wxGenericTreeItem *gitem = item.m_pItem;
1266
f65635b5 1267 // now scroll to the item
0659e7ee 1268 int item_y = gitem->GetY();
8dc99046 1269
0659e7ee
RR
1270 int start_x = 0;
1271 int start_y = 0;
1272 ViewStart( &start_x, &start_y );
91b8de8d 1273 start_y *= PIXELS_PER_UNIT;
978f38c2 1274
a93109d5
RR
1275 int client_h = 0;
1276 int client_w = 0;
1277 GetClientSize( &client_w, &client_h );
ef44a621 1278
0659e7ee
RR
1279 if (item_y < start_y+3)
1280 {
91b8de8d 1281 // going down
0659e7ee
RR
1282 int x = 0;
1283 int y = 0;
91b8de8d
RR
1284 m_anchor->GetSize( x, y, this );
1285 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee 1286 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1287 // Item should appear at top
91b8de8d 1288 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, item_y/PIXELS_PER_UNIT );
0659e7ee 1289 }
91b8de8d 1290 else if (item_y+GetLineHeight(gitem) > start_y+client_h)
0659e7ee 1291 {
91b8de8d 1292 // going up
0659e7ee
RR
1293 int x = 0;
1294 int y = 0;
91b8de8d
RR
1295 m_anchor->GetSize( x, y, this );
1296 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
1297 item_y += PIXELS_PER_UNIT+2;
0659e7ee 1298 int x_pos = GetScrollPos( wxHORIZONTAL );
c193b707 1299 // Item should appear at bottom
91b8de8d 1300 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 1301 }
edaa81ae 1302}
c801d85f 1303
e1ee62bd
VZ
1304// FIXME: tree sorting functions are not reentrant and not MT-safe!
1305static wxTreeCtrl *s_treeBeingSorted = NULL;
0659e7ee 1306
004fd0c8 1307static int LINKAGEMODE tree_ctrl_compare_func(wxGenericTreeItem **item1,
e1ee62bd 1308 wxGenericTreeItem **item2)
edaa81ae 1309{
223d09f6 1310 wxCHECK_MSG( s_treeBeingSorted, 0, wxT("bug in wxTreeCtrl::SortChildren()") );
e1ee62bd
VZ
1311
1312 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
0659e7ee
RR
1313}
1314
e1ee62bd
VZ
1315int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1316 const wxTreeItemId& item2)
0659e7ee 1317{
87138c52 1318 return wxStrcmp(GetItemText(item1), GetItemText(item2));
e1ee62bd
VZ
1319}
1320
1321void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId)
1322{
223d09f6 1323 wxCHECK_RET( itemId.IsOk(), wxT("invalid tree item") );
e1ee62bd
VZ
1324
1325 wxGenericTreeItem *item = itemId.m_pItem;
978f38c2 1326
e1ee62bd 1327 wxCHECK_RET( !s_treeBeingSorted,
223d09f6 1328 wxT("wxTreeCtrl::SortChildren is not reentrant") );
e1ee62bd 1329
91b8de8d 1330 wxArrayGenericTreeItems& children = item->GetChildren();
e1ee62bd
VZ
1331 if ( children.Count() > 1 )
1332 {
1333 s_treeBeingSorted = this;
1334 children.Sort(tree_ctrl_compare_func);
1335 s_treeBeingSorted = NULL;
978f38c2 1336
e1ee62bd
VZ
1337 m_dirty = TRUE;
1338 }
1339 //else: don't make the tree dirty as nothing changed
edaa81ae
RR
1340}
1341
f135ff73 1342wxImageList *wxTreeCtrl::GetImageList() const
edaa81ae 1343{
f135ff73 1344 return m_imageListNormal;
edaa81ae
RR
1345}
1346
f135ff73 1347wxImageList *wxTreeCtrl::GetStateImageList() const
c801d85f 1348{
f135ff73 1349 return m_imageListState;
edaa81ae 1350}
c801d85f 1351
f135ff73 1352void wxTreeCtrl::SetImageList(wxImageList *imageList)
e2414cbe 1353{
d30b4d20 1354 m_imageListNormal = imageList;
91b8de8d
RR
1355
1356 // Calculate a m_lineHeight value from the image sizes.
1357 // May be toggle off. Then wxTreeCtrl will spread when
1358 // necessary (which might look ugly).
1359#if 1
1044a386 1360 wxClientDC dc(this);
d30b4d20
KB
1361 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1362 int
1363 width = 0,
1364 height = 0,
1365 n = m_imageListNormal->GetImageCount();
1366 for(int i = 0; i < n ; i++)
1367 {
1368 m_imageListNormal->GetSize(i, width, height);
1369 if(height > m_lineHeight) m_lineHeight = height;
d3a9f4af 1370 }
91b8de8d
RR
1371
1372 if (m_lineHeight<40) m_lineHeight+=4; // at least 4 pixels (odd such that a line can be drawn in between)
1373 else m_lineHeight+=m_lineHeight/10; // otherwise 10% extra spacing
1374
1375#endif
edaa81ae 1376}
e2414cbe 1377
f135ff73 1378void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
e2414cbe 1379{
f135ff73 1380 m_imageListState = imageList;
edaa81ae 1381}
e2414cbe 1382
f135ff73
VZ
1383// -----------------------------------------------------------------------------
1384// helpers
1385// -----------------------------------------------------------------------------
0659e7ee 1386
74bedbeb 1387void wxTreeCtrl::AdjustMyScrollbars()
c801d85f 1388{
0659e7ee
RR
1389 if (m_anchor)
1390 {
1391 int x = 0;
1392 int y = 0;
91b8de8d
RR
1393 m_anchor->GetSize( x, y, this );
1394 //y += GetLineHeight(m_anchor);
c193b707 1395 y += PIXELS_PER_UNIT+2; // one more scrollbar unit + 2 pixels
0659e7ee
RR
1396 int x_pos = GetScrollPos( wxHORIZONTAL );
1397 int y_pos = GetScrollPos( wxVERTICAL );
91b8de8d 1398 SetScrollbars( PIXELS_PER_UNIT, PIXELS_PER_UNIT, x/PIXELS_PER_UNIT, y/PIXELS_PER_UNIT, x_pos, y_pos );
0659e7ee
RR
1399 }
1400 else
1401 {
1402 SetScrollbars( 0, 0, 0, 0 );
1403 }
edaa81ae 1404}
c801d85f 1405
91b8de8d
RR
1406int wxTreeCtrl::GetLineHeight(wxGenericTreeItem *item) const
1407{
dc6c62a9
RR
1408 if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT)
1409 return item->GetHeight();
1410 else
1411 return m_lineHeight;
91b8de8d
RR
1412}
1413
ef44a621
VZ
1414void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
1415{
bbe0af5b 1416 if (item->IsBold())
eff869aa 1417 dc.SetFont(m_boldFont);
ef44a621 1418
bbe0af5b
RR
1419 long text_w = 0;
1420 long text_h = 0;
1421 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
ef44a621 1422
bbe0af5b
RR
1423 int image_h = 0;
1424 int image_w = 0;
8dc99046
VZ
1425 int image = item->GetCurrentImage();
1426 if ( image != NO_IMAGE )
bbe0af5b 1427 {
8dc99046 1428 m_imageListNormal->GetSize( image, image_w, image_h );
bbe0af5b
RR
1429 image_w += 4;
1430 }
ef44a621 1431
91b8de8d
RR
1432 int total_h = GetLineHeight(item);
1433
1434 dc.DrawRectangle( item->GetX()-2, item->GetY(), item->GetWidth()+2, total_h );
ef44a621 1435
8dc99046 1436 if ( image != NO_IMAGE )
bbe0af5b 1437 {
d30b4d20 1438 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
8dc99046 1439 m_imageListNormal->Draw( image, dc,
49cd56ef 1440 item->GetX(),
d701d432 1441 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
1442 wxIMAGELIST_DRAW_TRANSPARENT );
1443 dc.DestroyClippingRegion();
1444 }
ef44a621 1445
bbe0af5b 1446 dc.SetBackgroundMode(wxTRANSPARENT);
d30b4d20 1447 dc.DrawText( item->GetText(), image_w + item->GetX(), item->GetY()
49cd56ef 1448 + ((total_h > text_h) ? (total_h - text_h)/2 : 0));
ef44a621 1449
eff869aa
RR
1450 // restore normal font
1451 dc.SetFont( m_normalFont );
ef44a621
VZ
1452}
1453
91b8de8d 1454// Now y stands for the top of the item, whereas it used to stand for middle !
16c1f7f3 1455void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 1456{
bbe0af5b 1457 int horizX = level*m_indent;
389cdc7a 1458
cf724bce 1459 item->SetX( horizX+m_indent+m_spacing );
91b8de8d 1460 item->SetY( y );
4c681997 1461
bbe0af5b 1462 int oldY = y;
91b8de8d
RR
1463 y+=GetLineHeight(item)/2;
1464
1465 item->SetCross( horizX+m_indent, y );
389cdc7a 1466
bbe0af5b 1467 int exposed_x = dc.LogicalToDeviceX( 0 );
5391f772 1468 int exposed_y = dc.LogicalToDeviceY( item->GetY() );
4832f7c0 1469
5391f772 1470 if (IsExposed( exposed_x, exposed_y, 10000, GetLineHeight(item) )) // 10000 = very much
bbe0af5b
RR
1471 {
1472 int startX = horizX;
cf724bce 1473 int endX = horizX + (m_indent-5);
29d87bba 1474
cf724bce 1475// if (!item->HasChildren()) endX += (m_indent+5);
bbe0af5b 1476 if (!item->HasChildren()) endX += 20;
4832f7c0 1477
bbe0af5b 1478 dc.DrawLine( startX, y, endX, y );
29d87bba 1479
bbe0af5b
RR
1480 if (item->HasPlus())
1481 {
cf724bce 1482 dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y );
bbe0af5b
RR
1483 dc.SetPen( *wxGREY_PEN );
1484 dc.SetBrush( *wxWHITE_BRUSH );
cf724bce 1485 dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 );
9dfbf520 1486
bbe0af5b 1487 dc.SetPen( *wxBLACK_PEN );
cf724bce 1488 dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y );
bbe0af5b 1489 if (!item->IsExpanded())
cf724bce 1490 dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 );
9dfbf520 1491
112c5086 1492 dc.SetPen( m_dottedPen );
bbe0af5b 1493 }
c801d85f 1494
8dc99046 1495 if (item->IsSelected())
bbe0af5b
RR
1496 {
1497 dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
a367b9b3 1498
bbe0af5b 1499 dc.SetBrush( *m_hilightBrush );
4832f7c0 1500
bbe0af5b
RR
1501 if (m_hasFocus)
1502 dc.SetPen( *wxBLACK_PEN );
1503 else
1504 dc.SetPen( *wxTRANSPARENT_PEN );
4832f7c0 1505
bbe0af5b 1506 PaintItem(item, dc);
f135ff73 1507
112c5086 1508 dc.SetPen( m_dottedPen );
bbe0af5b
RR
1509 dc.SetTextForeground( *wxBLACK );
1510 dc.SetBrush( *wxWHITE_BRUSH );
1511 }
1512 else
1513 {
1514 dc.SetBrush( *wxWHITE_BRUSH );
1515 dc.SetPen( *wxTRANSPARENT_PEN );
4832f7c0 1516
bbe0af5b 1517 PaintItem(item, dc);
4832f7c0 1518
112c5086 1519 dc.SetPen( m_dottedPen );
bbe0af5b 1520 }
f135ff73 1521 }
d3a9f4af 1522
91b8de8d 1523 y = oldY+GetLineHeight(item);
e2414cbe 1524
bbe0af5b
RR
1525 if (item->IsExpanded())
1526 {
91b8de8d
RR
1527 oldY+=GetLineHeight(item)/2;
1528 int semiOldY=y; // (=y) for stupid compilator
389cdc7a 1529
91b8de8d
RR
1530 wxArrayGenericTreeItems& children = item->GetChildren();
1531 size_t n, count = children.Count();
1532 for ( n = 0; n < count; ++n )
c193b707
VZ
1533 {
1534 semiOldY=y;
1535 PaintLevel( children[n], dc, level+1, y );
1536 }
389cdc7a 1537
f65635b5
VZ
1538 // it may happen that the item is expanded but has no items (when you
1539 // delete all its children for example) - don't draw the vertical line
1540 // in this case
1541 if (count > 0)
c193b707
VZ
1542 {
1543 semiOldY+=GetLineHeight(children[--n])/2;
cf724bce 1544 dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY );
c193b707 1545 }
bbe0af5b 1546 }
4c681997 1547}
c801d85f 1548
91b8de8d
RR
1549void wxTreeCtrl::DrawBorder(wxTreeItemId &item)
1550{
1551 if (!item) return;
1552
1553 wxGenericTreeItem *i=item.m_pItem;
1554
1044a386 1555 wxClientDC dc(this);
91b8de8d
RR
1556 PrepareDC( dc );
1557 dc.SetLogicalFunction(wxINVERT);
1558
1559 int w,h,x;
1560 ViewStart(&x,&h); // we only need x
1561 GetClientSize(&w,&h); // we only need w
1562
1563 h=GetLineHeight(i)+1;
1564 // 2 white column at border
1565 dc.DrawRectangle( PIXELS_PER_UNIT*x+2, i->GetY()-1, w-6, h);
1566}
1567
1568void wxTreeCtrl::DrawLine(wxTreeItemId &item, bool below)
1569{
1570 if (!item) return;
1571
1572 wxGenericTreeItem *i=item.m_pItem;
1573
1044a386 1574 wxClientDC dc(this);
91b8de8d
RR
1575 PrepareDC( dc );
1576 dc.SetLogicalFunction(wxINVERT);
d3a9f4af 1577
91b8de8d
RR
1578 int w,h,y;
1579 GetSize(&w,&h);
1580
1581 if (below) y=i->GetY()+GetLineHeight(i)-1;
1582 else y=i->GetY();
1583
1584 dc.DrawLine( 0, y, w, y);
1585}
1586
f135ff73
VZ
1587// -----------------------------------------------------------------------------
1588// wxWindows callbacks
1589// -----------------------------------------------------------------------------
1590
3db7be80 1591void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
c801d85f 1592{
91b8de8d 1593 if ( !m_anchor)
0659e7ee 1594 return;
c801d85f 1595
0659e7ee
RR
1596 wxPaintDC dc(this);
1597 PrepareDC( dc );
29d87bba 1598
eff869aa 1599 dc.SetFont( m_normalFont );
0659e7ee 1600 dc.SetPen( m_dottedPen );
eff869aa
RR
1601
1602 // this is now done dynamically
91b8de8d
RR
1603 //if(GetImageList() == NULL)
1604 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 1605
91b8de8d 1606 int y = 2;
0659e7ee 1607 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 1608}
c801d85f 1609
3db7be80 1610void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
c801d85f 1611{
0659e7ee 1612 m_hasFocus = TRUE;
978f38c2 1613
bbe0af5b 1614 if (m_current) RefreshLine( m_current );
edaa81ae 1615}
c801d85f 1616
3db7be80 1617void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
c801d85f 1618{
0659e7ee 1619 m_hasFocus = FALSE;
978f38c2 1620
bbe0af5b 1621 if (m_current) RefreshLine( m_current );
edaa81ae 1622}
c801d85f
KB
1623
1624void wxTreeCtrl::OnChar( wxKeyEvent &event )
1625{
978f38c2
VZ
1626 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
1627 te.m_code = event.KeyCode();
1628 te.SetEventObject( this );
1629 GetEventHandler()->ProcessEvent( te );
435fe83e 1630
91b8de8d 1631 if ( (m_current == 0) || (m_key_current == 0) )
978f38c2
VZ
1632 {
1633 event.Skip();
1634 return;
1635 }
ef44a621 1636
88ac883a
VZ
1637 bool is_multiple=(GetWindowStyleFlag() & wxTR_MULTIPLE);
1638 bool extended_select=(event.ShiftDown() && is_multiple);
1639 bool unselect_others=!(extended_select || (event.ControlDown() && is_multiple));
1640
978f38c2
VZ
1641 switch (event.KeyCode())
1642 {
1643 case '+':
1644 case WXK_ADD:
1645 if (m_current->HasPlus() && !IsExpanded(m_current))
1646 {
1647 Expand(m_current);
1648 }
1649 break;
ef44a621 1650
978f38c2
VZ
1651 case '-':
1652 case WXK_SUBTRACT:
1653 if (IsExpanded(m_current))
1654 {
1655 Collapse(m_current);
1656 }
1657 break;
ef44a621 1658
978f38c2
VZ
1659 case '*':
1660 case WXK_MULTIPLY:
1661 Toggle(m_current);
1662 break;
ef44a621 1663
978f38c2
VZ
1664 case ' ':
1665 case WXK_RETURN:
1666 {
1667 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1668 event.m_item = m_current;
1669 event.m_code = 0;
1670 event.SetEventObject( this );
1671 GetEventHandler()->ProcessEvent( event );
1672 }
1673 break;
ef44a621 1674
978f38c2
VZ
1675 // up goes to the previous sibling or to the last of its children if
1676 // it's expanded
1677 case WXK_UP:
1678 {
91b8de8d 1679 wxTreeItemId prev = GetPrevSibling( m_key_current );
978f38c2
VZ
1680 if (!prev)
1681 {
91b8de8d 1682 prev = GetParent( m_key_current );
c193b707
VZ
1683 if (prev)
1684 {
90e58684 1685 long cockie = 0;
91b8de8d 1686 wxTreeItemId current = m_key_current;
90e58684
RR
1687 if (current == GetFirstChild( prev, cockie ))
1688 {
1689 // otherwise we return to where we came from
88ac883a 1690 SelectItem( prev, unselect_others, extended_select );
c193b707
VZ
1691 m_key_current=prev.m_pItem;
1692 EnsureVisible( prev );
90e58684 1693 break;
c193b707 1694 }
978f38c2
VZ
1695 }
1696 }
1697 if (prev)
1698 {
69a282d4 1699 while ( IsExpanded(prev) && HasChildren(prev) )
978f38c2 1700 {
69a282d4
VZ
1701 wxTreeItemId child = GetLastChild(prev);
1702 if ( child )
1703 {
1704 prev = child;
1705 }
978f38c2 1706 }
69a282d4 1707
88ac883a 1708 SelectItem( prev, unselect_others, extended_select );
c193b707 1709 m_key_current=prev.m_pItem;
978f38c2
VZ
1710 EnsureVisible( prev );
1711 }
1712 }
1713 break;
ef44a621 1714
978f38c2
VZ
1715 // left arrow goes to the parent
1716 case WXK_LEFT:
1717 {
1718 wxTreeItemId prev = GetParent( m_current );
1719 if (prev)
1720 {
1721 EnsureVisible( prev );
88ac883a 1722 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
1723 }
1724 }
1725 break;
ef44a621 1726
978f38c2
VZ
1727 case WXK_RIGHT:
1728 // this works the same as the down arrow except that we also expand the
1729 // item if it wasn't expanded yet
1730 Expand(m_current);
1731 // fall through
1732
1733 case WXK_DOWN:
ef44a621 1734 {
91b8de8d 1735 if (IsExpanded(m_key_current) && HasChildren(m_key_current))
978f38c2
VZ
1736 {
1737 long cookie = 0;
91b8de8d 1738 wxTreeItemId child = GetFirstChild( m_key_current, cookie );
88ac883a 1739 SelectItem( child, unselect_others, extended_select );
c193b707 1740 m_key_current=child.m_pItem;
978f38c2
VZ
1741 EnsureVisible( child );
1742 }
1743 else
1744 {
91b8de8d 1745 wxTreeItemId next = GetNextSibling( m_key_current );
b62c3631
RR
1746// if (next == 0)
1747 if (!next)
978f38c2 1748 {
91b8de8d 1749 wxTreeItemId current = m_key_current;
978f38c2
VZ
1750 while (current && !next)
1751 {
1752 current = GetParent( current );
1753 if (current) next = GetNextSibling( current );
1754 }
1755 }
b62c3631
RR
1756// if (next != 0)
1757 if (next)
978f38c2 1758 {
88ac883a 1759 SelectItem( next, unselect_others, extended_select );
c193b707 1760 m_key_current=next.m_pItem;
978f38c2
VZ
1761 EnsureVisible( next );
1762 }
1763 }
ef44a621 1764 }
978f38c2 1765 break;
ef44a621 1766
978f38c2
VZ
1767 // <End> selects the last visible tree item
1768 case WXK_END:
1769 {
1770 wxTreeItemId last = GetRootItem();
1771
1772 while ( last.IsOk() && IsExpanded(last) )
1773 {
1774 wxTreeItemId lastChild = GetLastChild(last);
1775
1776 // it may happen if the item was expanded but then all of
1777 // its children have been deleted - so IsExpanded() returned
1778 // TRUE, but GetLastChild() returned invalid item
1779 if ( !lastChild )
1780 break;
1781
1782 last = lastChild;
1783 }
1784
1785 if ( last.IsOk() )
1786 {
1787 EnsureVisible( last );
88ac883a 1788 SelectItem( last, unselect_others, extended_select );
978f38c2
VZ
1789 }
1790 }
1791 break;
1792
1793 // <Home> selects the root item
1794 case WXK_HOME:
1795 {
1796 wxTreeItemId prev = GetRootItem();
1797 if (prev)
1798 {
1799 EnsureVisible( prev );
88ac883a 1800 SelectItem( prev, unselect_others, extended_select );
978f38c2
VZ
1801 }
1802 }
1803 break;
1804
1805 default:
1806 event.Skip();
1807 }
edaa81ae 1808}
c801d85f 1809
91b8de8d 1810wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
4f22cf8d 1811{
dc6c62a9
RR
1812 // We have to call this here because the label in
1813 // question might just have been added and no screen
1814 // update taken place.
1815 if (m_dirty) wxYield();
1816
67a7abf7
VZ
1817 wxClientDC dc(this);
1818 PrepareDC(dc);
1819 long x = dc.DeviceToLogicalX( (long)point.x );
1820 long y = dc.DeviceToLogicalY( (long)point.y );
91b8de8d
RR
1821 int w, h;
1822 GetSize(&w, &h);
1823
1824 flags=0;
1825 if (point.x<0) flags|=wxTREE_HITTEST_TOLEFT;
1826 if (point.x>w) flags|=wxTREE_HITTEST_TORIGHT;
1827 if (point.y<0) flags|=wxTREE_HITTEST_ABOVE;
1828 if (point.y>h) flags|=wxTREE_HITTEST_BELOW;
d3a9f4af 1829
91b8de8d 1830 return m_anchor->HitTest( wxPoint(x, y), this, flags);
4f22cf8d
RR
1831}
1832
e179bd65
RR
1833/* **** */
1834
1835void wxTreeCtrl::Edit( const wxTreeItemId& item )
1836{
1837 if (!item.IsOk()) return;
1838
1839 m_currentEdit = item.m_pItem;
9dfbf520 1840
e179bd65
RR
1841 wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, GetId() );
1842 te.m_item = m_currentEdit;
1843 te.SetEventObject( this );
1844 GetEventHandler()->ProcessEvent( te );
1845
1846 if (!te.IsAllowed()) return;
8dc99046 1847
dc6c62a9
RR
1848 // We have to call this here because the label in
1849 // question might just have been added and no screen
1850 // update taken place.
1851 if (m_dirty) wxYield();
9dfbf520 1852
e179bd65
RR
1853 wxString s = m_currentEdit->GetText();
1854 int x = m_currentEdit->GetX();
1855 int y = m_currentEdit->GetY();
1856 int w = m_currentEdit->GetWidth();
1857 int h = m_currentEdit->GetHeight();
9dfbf520 1858
5f1ea0ee
RR
1859 int image_h = 0;
1860 int image_w = 0;
8dc99046
VZ
1861
1862 int image = m_currentEdit->GetCurrentImage();
1863 if ( image != NO_IMAGE )
5f1ea0ee 1864 {
8dc99046 1865 m_imageListNormal->GetSize( image, image_w, image_h );
5f1ea0ee
RR
1866 image_w += 4;
1867 }
1868 x += image_w;
1869 w -= image_w + 4; // I don't know why +4 is needed
e179bd65
RR
1870
1871 wxClientDC dc(this);
1872 PrepareDC( dc );
1873 x = dc.LogicalToDeviceX( x );
1874 y = dc.LogicalToDeviceY( y );
1875
1876 wxTreeTextCtrl *text = new wxTreeTextCtrl(
1877 this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) );
1878 text->SetFocus();
1879}
1880
1881void wxTreeCtrl::OnRenameTimer()
1882{
1883 Edit( m_current );
1884}
1885
1886void wxTreeCtrl::OnRenameAccept()
1887{
1888 wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, GetId() );
1889 le.m_item = m_currentEdit;
1890 le.SetEventObject( this );
1891 le.m_label = m_renameRes;
1892 GetEventHandler()->ProcessEvent( le );
9dfbf520 1893
e179bd65 1894 if (!le.IsAllowed()) return;
9dfbf520 1895
5f1ea0ee 1896 SetItemText( m_currentEdit, m_renameRes );
e179bd65 1897}
9dfbf520 1898
3db7be80 1899void wxTreeCtrl::OnMouse( wxMouseEvent &event )
c801d85f 1900{
fb882e1c 1901 if ( !(event.LeftUp() || event.RightDown() || event.LeftDClick() || event.Dragging()) ) return;
29d87bba 1902
bbe0af5b 1903 if ( !m_anchor ) return;
978f38c2 1904
bbe0af5b
RR
1905 wxClientDC dc(this);
1906 PrepareDC(dc);
1907 long x = dc.DeviceToLogicalX( (long)event.GetX() );
1908 long y = dc.DeviceToLogicalY( (long)event.GetY() );
29d87bba 1909
91b8de8d
RR
1910 int flags=0;
1911 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), this, flags);
1912 bool onButton = flags & wxTREE_HITTEST_ONITEMBUTTON;
978f38c2 1913
bbe0af5b
RR
1914 if (event.Dragging())
1915 {
fd9811b1 1916 if (m_dragCount == 0)
8dc99046
VZ
1917 m_dragStart = wxPoint(x,y);
1918
fd9811b1 1919 m_dragCount++;
8dc99046
VZ
1920
1921 if (m_dragCount != 3) return;
1922
1923 int command = wxEVT_COMMAND_TREE_BEGIN_DRAG;
1924 if (event.RightIsDown()) command = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
1925
fd9811b1
RR
1926 wxTreeEvent nevent( command, GetId() );
1927 nevent.m_item = m_current;
1928 nevent.SetEventObject(this);
1929 GetEventHandler()->ProcessEvent(nevent);
8dc99046 1930 return;
bbe0af5b 1931 }
fd9811b1
RR
1932 else
1933 {
1934 m_dragCount = 0;
1935 }
1936
1937 if (item == NULL) return; /* we hit the blank area */
978f38c2 1938
004fd0c8 1939 if (event.RightDown()) {
fb882e1c
UC
1940 wxTreeEvent nevent(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK,GetId());
1941 nevent.m_item=item;
1942 nevent.m_code=0;
1943 nevent.SetEventObject(this);
1944 GetEventHandler()->ProcessEvent(nevent);
1945 return;
1946 }
1947
9dfbf520
VZ
1948 if (event.LeftUp() && (item == m_current) &&
1949 (flags & wxTREE_HITTEST_ONITEMLABEL) &&
1950 HasFlag(wxTR_EDIT_LABELS) )
e179bd65
RR
1951 {
1952 m_renameTimer->Start( 100, TRUE );
1953 return;
1954 }
9dfbf520 1955
88ac883a
VZ
1956 bool is_multiple=(GetWindowStyleFlag() & wxTR_MULTIPLE);
1957 bool extended_select=(event.ShiftDown() && is_multiple);
1958 bool unselect_others=!(extended_select || (event.ControlDown() && is_multiple));
1959
91b8de8d
RR
1960 if (onButton)
1961 {
1962 Toggle( item );
c193b707
VZ
1963 if (is_multiple)
1964 return;
91b8de8d
RR
1965 }
1966
88ac883a 1967 SelectItem(item, unselect_others, extended_select);
29d87bba 1968
bbe0af5b
RR
1969 if (event.LeftDClick())
1970 {
1971 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1972 event.m_item = item;
1973 event.m_code = 0;
1974 event.SetEventObject( this );
1975 GetEventHandler()->ProcessEvent( event );
1976 }
edaa81ae 1977}
c801d85f 1978
3db7be80
RR
1979void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
1980{
bbe0af5b
RR
1981 /* after all changes have been done to the tree control,
1982 * we actually redraw the tree when everything is over */
ef44a621 1983
f65635b5
VZ
1984 if (!m_dirty)
1985 return;
ef44a621 1986
bbe0af5b 1987 m_dirty = FALSE;
3db7be80 1988
bbe0af5b 1989 CalculatePositions();
91b8de8d 1990 Refresh();
bbe0af5b 1991 AdjustMyScrollbars();
3db7be80
RR
1992}
1993
91b8de8d 1994void wxTreeCtrl::CalculateSize( wxGenericTreeItem *item, wxDC &dc )
d3a9f4af 1995{
91b8de8d
RR
1996 long text_w = 0;
1997 long text_h = 0;
9dfbf520 1998
a62867a5 1999 if (item->IsBold())
eff869aa 2000 dc.SetFont(m_boldFont);
9dfbf520 2001
91b8de8d 2002 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
a62867a5 2003 text_h+=2;
91b8de8d 2004
eff869aa
RR
2005 // restore normal font
2006 dc.SetFont( m_normalFont );
9dfbf520 2007
91b8de8d
RR
2008 int image_h = 0;
2009 int image_w = 0;
8dc99046
VZ
2010 int image = item->GetCurrentImage();
2011 if ( image != NO_IMAGE )
91b8de8d 2012 {
8dc99046 2013 m_imageListNormal->GetSize( image, image_w, image_h );
91b8de8d
RR
2014 image_w += 4;
2015 }
2016
2017 int total_h = (image_h > text_h) ? image_h : text_h;
2018
2019 if (total_h<40) total_h+=4; // at least 4 pixels
2020 else total_h+=total_h/10; // otherwise 10% extra spacing
2021
2022 item->SetHeight(total_h);
2023 if (total_h>m_lineHeight) m_lineHeight=total_h;
bbe0af5b 2024
91b8de8d
RR
2025 item->SetWidth(image_w+text_w+2);
2026}
2027
2028// -----------------------------------------------------------------------------
2029// for developper : y is now the top of the level
2030// not the middle of it !
bbe0af5b 2031void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 2032{
bbe0af5b 2033 int horizX = level*m_indent;
389cdc7a 2034
91b8de8d 2035 CalculateSize( item, dc );
d3a9f4af 2036
91b8de8d 2037 // set its position
cf724bce 2038 item->SetX( horizX+m_indent+m_spacing );
91b8de8d
RR
2039 item->SetY( y );
2040 y+=GetLineHeight(item);
4c681997 2041
bbe0af5b
RR
2042 if ( !item->IsExpanded() )
2043 {
f65635b5 2044 // we dont need to calculate collapsed branches
bbe0af5b
RR
2045 return;
2046 }
389cdc7a 2047
91b8de8d
RR
2048 wxArrayGenericTreeItems& children = item->GetChildren();
2049 size_t n, count = children.Count();
2050 for (n = 0; n < count; ++n )
2051 CalculateLevel( children[n], dc, level+1, y ); // recurse
edaa81ae 2052}
c801d85f 2053
74bedbeb 2054void wxTreeCtrl::CalculatePositions()
c801d85f 2055{
bbe0af5b 2056 if ( !m_anchor ) return;
29d87bba 2057
bbe0af5b
RR
2058 wxClientDC dc(this);
2059 PrepareDC( dc );
29d87bba 2060
eff869aa 2061 dc.SetFont( m_normalFont );
29d87bba 2062
bbe0af5b 2063 dc.SetPen( m_dottedPen );
91b8de8d
RR
2064 //if(GetImageList() == NULL)
2065 // m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 2066
8dc99046 2067 int y = 2;
f65635b5 2068 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
edaa81ae 2069}
c801d85f 2070
f135ff73 2071void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
c801d85f 2072{
bbe0af5b
RR
2073 wxClientDC dc(this);
2074 PrepareDC(dc);
4832f7c0 2075
bbe0af5b
RR
2076 int cw = 0;
2077 int ch = 0;
2078 GetClientSize( &cw, &ch );
4832f7c0 2079
bbe0af5b
RR
2080 wxRect rect;
2081 rect.x = dc.LogicalToDeviceX( 0 );
2082 rect.width = cw;
2083 rect.y = dc.LogicalToDeviceY( item->GetY() );
2084 rect.height = ch;
f135ff73 2085
bbe0af5b 2086 Refresh( TRUE, &rect );
f135ff73 2087
bbe0af5b 2088 AdjustMyScrollbars();
edaa81ae 2089}
c801d85f
KB
2090
2091void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
2092{
bbe0af5b
RR
2093 wxClientDC dc(this);
2094 PrepareDC( dc );
2095
5391f772
SB
2096 int cw = 0;
2097 int ch = 0;
2098 GetClientSize( &cw, &ch );
2099
bbe0af5b 2100 wxRect rect;
5391f772
SB
2101 rect.x = dc.LogicalToDeviceX( 0 );
2102 rect.y = dc.LogicalToDeviceY( item->GetY() );
2103 rect.width = cw;
91b8de8d 2104 rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;
978f38c2 2105
bbe0af5b 2106 Refresh( TRUE, &rect );
edaa81ae 2107}
c801d85f 2108