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