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