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