]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectrl.cpp
A few more things are back to work.
[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
31#include "wx/generic/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"
37#include "wx/dcclient.h"
0659e7ee 38#include "wx/msgdlg.h"
c801d85f 39
f135ff73
VZ
40// -----------------------------------------------------------------------------
41// array types
42// -----------------------------------------------------------------------------
c801d85f 43
1e6d9499
JS
44class WXDLLEXPORT wxGenericTreeItem;
45
f135ff73 46WX_DEFINE_ARRAY(wxGenericTreeItem *, wxArrayTreeItems);
c801d85f 47
f135ff73
VZ
48// -----------------------------------------------------------------------------
49// private classes
50// -----------------------------------------------------------------------------
51
52// a tree item
53class WXDLLEXPORT wxGenericTreeItem
c801d85f 54{
f135ff73
VZ
55public:
56 // ctors & dtor
57 wxGenericTreeItem() { m_data = NULL; }
58 wxGenericTreeItem( wxGenericTreeItem *parent,
59 const wxString& text,
60 wxDC& dc,
61 int image, int selImage,
62 wxTreeItemData *data );
63
ff5bf259 64 ~wxGenericTreeItem();
f135ff73
VZ
65
66 // trivial accessors
67 wxArrayTreeItems& GetChildren() { return m_children; }
68
69 const wxString& GetText() const { return m_text; }
70 int GetImage() const { return m_image; }
71 int GetSelectedImage() const { return m_selImage; }
72 wxTreeItemData *GetData() const { return m_data; }
73
74 void SetText( const wxString &text, wxDC& dc );
75 void SetImage(int image) { m_image = image; }
76 void SetSelectedImage(int image) { m_selImage = image; }
77 void SetData(wxTreeItemData *data) { m_data = data; }
78
79 void SetHasPlus(bool has = TRUE) { m_hasPlus = has; }
80
ef44a621
VZ
81 void SetBold(bool bold) { m_isBold = bold; }
82
f135ff73
VZ
83 int GetX() const { return m_x; }
84 int GetY() const { return m_y; }
85
86 void SetHeight(int h) { m_height = h; }
87
88 void SetX(int x) { m_x = x; }
89 void SetY(int y) { m_y = y; }
90
91 wxGenericTreeItem *GetParent() const { return m_parent; }
c801d85f 92
f135ff73 93 // operations
a43a4f9d
VZ
94 // deletes all children notifying the treectrl about it if !NULL pointer
95 // given
96 void DeleteChildren(wxTreeCtrl *tree = NULL);
97 // FIXME don't know what is it for
f135ff73
VZ
98 void Reset();
99
4832f7c0
VZ
100 // get count of all children (and grand children if 'recursively')
101 size_t GetChildrenCount(bool recursively = TRUE) const;
f135ff73
VZ
102
103 void Insert(wxGenericTreeItem *child, size_t index)
104 { m_children.Insert(child, index); }
105
106 void SetCross( int x, int y );
107 void GetSize( int &x, int &y );
108
109 // return the item at given position (or NULL if no item), onButton is TRUE
110 // if the point belongs to the item's button, otherwise it lies on the
111 // button's label
112 wxGenericTreeItem *HitTest( const wxPoint& point, bool &onButton );
113
114 void Expand() { m_isCollapsed = FALSE; }
115 void Collapse() { m_isCollapsed = TRUE; }
116
117 void SetHilight( bool set = TRUE ) { m_hasHilight = set; }
118
119 // status inquiries
120 bool HasChildren() const { return !m_children.IsEmpty(); }
121 bool HasHilight() const { return m_hasHilight; }
122 bool IsExpanded() const { return !m_isCollapsed; }
123 bool HasPlus() const { return m_hasPlus || HasChildren(); }
ef44a621 124 bool IsBold() const { return m_isBold; }
f135ff73
VZ
125
126private:
127 wxString m_text;
128
129 int m_image,
130 m_selImage;
131
132 wxTreeItemData *m_data;
4832f7c0 133
ef44a621
VZ
134 // use bitfields to save size
135 int m_isCollapsed :1;
136 int m_hasHilight :1; // same as focused
137 int m_hasPlus :1; // used for item which doesn't have
138 // children but still has a [+] button
139 int m_isBold :1; // render the label in bold font
f135ff73
VZ
140
141 int m_x, m_y;
142 long m_height, m_width;
143 int m_xCross, m_yCross;
144 int m_level;
145 wxArrayTreeItems m_children;
146 wxGenericTreeItem *m_parent;
147};
148
149// =============================================================================
150// implementation
151// =============================================================================
152
153// -----------------------------------------------------------------------------
c801d85f 154// wxTreeEvent
f135ff73 155// -----------------------------------------------------------------------------
c801d85f 156
92976ab6 157IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
c801d85f 158
f135ff73 159wxTreeEvent::wxTreeEvent( wxEventType commandType, int id )
92976ab6 160 : wxNotifyEvent( commandType, id )
c801d85f
KB
161{
162 m_code = 0;
f135ff73 163 m_itemOld = (wxGenericTreeItem *)NULL;
edaa81ae 164}
c801d85f 165
f135ff73 166// -----------------------------------------------------------------------------
c801d85f 167// wxGenericTreeItem
f135ff73 168// -----------------------------------------------------------------------------
c801d85f 169
f135ff73
VZ
170wxGenericTreeItem::wxGenericTreeItem(wxGenericTreeItem *parent,
171 const wxString& text,
172 wxDC& dc,
173 int image, int selImage,
174 wxTreeItemData *data)
175 : m_text(text)
c801d85f 176{
f135ff73
VZ
177 m_image = image;
178 m_selImage = selImage;
179 m_data = data;
180 m_x = m_y = 0;
181 m_xCross = m_yCross = 0;
182
183 m_level = 0;
184
185 m_isCollapsed = TRUE;
c801d85f 186 m_hasHilight = FALSE;
df875e59 187 m_hasPlus = FALSE;
ef44a621 188 m_isBold = FALSE;
c801d85f 189
c801d85f 190 m_parent = parent;
f135ff73
VZ
191
192 dc.GetTextExtent( m_text, &m_width, &m_height );
edaa81ae 193}
c801d85f 194
f135ff73 195wxGenericTreeItem::~wxGenericTreeItem()
c801d85f 196{
f135ff73 197 delete m_data;
4832f7c0 198
a43a4f9d 199 wxASSERT_MSG( m_children.IsEmpty(),
87138c52 200 _T("please call DeleteChildren() before deleting the item") );
372edb9d
VZ
201}
202
a43a4f9d 203void wxGenericTreeItem::DeleteChildren(wxTreeCtrl *tree)
372edb9d 204{
f135ff73
VZ
205 size_t count = m_children.Count();
206 for ( size_t n = 0; n < count; n++ )
a43a4f9d
VZ
207 {
208 wxGenericTreeItem *child = m_children[n];
209 if ( tree )
210 {
211 tree->SendDeleteEvent(child);
212 }
213
214 child->DeleteChildren(tree);
215 delete child;
216 }
372edb9d
VZ
217
218 m_children.Empty();
edaa81ae 219}
c801d85f 220
f135ff73 221void wxGenericTreeItem::SetText( const wxString &text, wxDC& dc )
c801d85f
KB
222{
223 m_text = text;
f135ff73
VZ
224
225 dc.GetTextExtent( m_text, &m_width, &m_height );
edaa81ae 226}
c801d85f 227
74bedbeb 228void wxGenericTreeItem::Reset()
c801d85f 229{
f135ff73
VZ
230 m_text.Empty();
231 m_image =
232 m_selImage = -1;
233 m_data = NULL;
234 m_x = m_y =
235 m_height = m_width = 0;
236 m_xCross =
c801d85f 237 m_yCross = 0;
74bedbeb 238
f135ff73 239 m_level = 0;
c801d85f 240
372edb9d 241 DeleteChildren();
f135ff73 242 m_isCollapsed = TRUE;
c801d85f 243
f135ff73 244 m_parent = (wxGenericTreeItem *)NULL;
edaa81ae 245}
c801d85f 246
4832f7c0 247size_t wxGenericTreeItem::GetChildrenCount(bool recursively) const
c801d85f 248{
f135ff73 249 size_t count = m_children.Count();
4832f7c0
VZ
250 if ( !recursively )
251 return count;
252
f135ff73
VZ
253 size_t total = count;
254 for ( size_t n = 0; n < count; n++ )
c801d85f 255 {
4832f7c0 256 total += m_children[n]->GetChildrenCount();
edaa81ae 257 }
c801d85f 258
f135ff73 259 return total;
edaa81ae 260}
c801d85f
KB
261
262void wxGenericTreeItem::SetCross( int x, int y )
263{
264 m_xCross = x;
265 m_yCross = y;
edaa81ae 266}
c801d85f
KB
267
268void wxGenericTreeItem::GetSize( int &x, int &y )
269{
df875e59 270 if ( y < m_y ) y = m_y;
c801d85f
KB
271 int width = m_x + m_width;
272 if (width > x) x = width;
f135ff73 273
df875e59 274 if (IsExpanded())
c801d85f 275 {
df875e59
RR
276 size_t count = m_children.Count();
277 for ( size_t n = 0; n < count; n++ )
4832f7c0 278 {
df875e59
RR
279 m_children[n]->GetSize( x, y );
280 }
edaa81ae
RR
281 }
282}
c801d85f 283
f135ff73
VZ
284wxGenericTreeItem *wxGenericTreeItem::HitTest( const wxPoint& point,
285 bool &onButton )
c801d85f 286{
f135ff73 287 if ((point.y > m_y) && (point.y < m_y + m_height))
c801d85f 288 {
f135ff73 289 // FIXME why +5?
5679f335 290 // Because that is the size of the plus sign, RR
f135ff73
VZ
291 if ((point.x > m_xCross-5) && (point.x < m_xCross+5) &&
292 (point.y > m_yCross-5) && (point.y < m_yCross+5) &&
f9f950fc 293 (IsExpanded() || HasPlus()))
c801d85f 294 {
f135ff73
VZ
295 onButton = TRUE;
296 return this;
edaa81ae 297 }
978f38c2 298
5679f335
RR
299 /* TODO: we should do a query here like
300 m_imageListNormal->GetSize( item->GetImage(), image_w, image_h ); */
a93109d5 301 int w = m_width;
5679f335 302 if (m_image != -1) w += 24;
f135ff73 303
a93109d5 304 if ((point.x > m_x) && (point.x < m_x+w))
c801d85f 305 {
f135ff73
VZ
306 onButton = FALSE;
307 return this;
edaa81ae 308 }
c801d85f
KB
309 }
310 else
311 {
e2414cbe 312 if (!m_isCollapsed)
c801d85f 313 {
f135ff73
VZ
314 size_t count = m_children.Count();
315 for ( size_t n = 0; n < count; n++ )
e2414cbe 316 {
f135ff73
VZ
317 wxGenericTreeItem *res = m_children[n]->HitTest( point, onButton );
318 if ( res != NULL )
319 return res;
edaa81ae
RR
320 }
321 }
322 }
f135ff73
VZ
323
324 return NULL;
edaa81ae 325}
c801d85f 326
f135ff73
VZ
327// -----------------------------------------------------------------------------
328// wxTreeCtrl implementation
329// -----------------------------------------------------------------------------
330
331IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxScrolledWindow)
332
333BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow)
334 EVT_PAINT (wxTreeCtrl::OnPaint)
335 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse)
336 EVT_CHAR (wxTreeCtrl::OnChar)
337 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus)
338 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus)
3db7be80 339 EVT_IDLE (wxTreeCtrl::OnIdle)
f135ff73
VZ
340END_EVENT_TABLE()
341
342// -----------------------------------------------------------------------------
343// construction/destruction
344// -----------------------------------------------------------------------------
345void wxTreeCtrl::Init()
c801d85f 346{
f135ff73
VZ
347 m_current =
348 m_anchor = (wxGenericTreeItem *) NULL;
349 m_hasFocus = FALSE;
3db7be80 350 m_dirty = FALSE;
f135ff73
VZ
351
352 m_xScroll = 0;
353 m_yScroll = 0;
354 m_lineHeight = 10;
355 m_indent = 15;
cf724bce 356 m_spacing = 18;
f135ff73
VZ
357
358 m_hilightBrush = new wxBrush
359 (
360 wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT),
361 wxSOLID
362 );
363
364 m_imageListNormal =
365 m_imageListState = (wxImageList *) NULL;
978f38c2 366
bbe0af5b 367 m_dragCount = 0;
edaa81ae 368}
c801d85f 369
f135ff73
VZ
370bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
371 const wxPoint& pos, const wxSize& size,
978f38c2
VZ
372 long style,
373 const wxValidator &validator,
374 const wxString& name )
c801d85f 375{
f135ff73
VZ
376 Init();
377
a367b9b3 378 wxScrolledWindow::Create( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name );
978f38c2 379
4f22cf8d 380 SetValidator( validator );
f135ff73
VZ
381
382 SetBackgroundColour( *wxWHITE );
383 m_dottedPen = wxPen( *wxBLACK, 0, 0 );
384
385 return TRUE;
edaa81ae 386}
c801d85f 387
f135ff73 388wxTreeCtrl::~wxTreeCtrl()
c801d85f 389{
f135ff73 390 wxDELETE( m_hilightBrush );
a43a4f9d
VZ
391
392 DeleteAllItems();
edaa81ae 393}
c801d85f 394
f135ff73
VZ
395// -----------------------------------------------------------------------------
396// accessors
397// -----------------------------------------------------------------------------
398
399size_t wxTreeCtrl::GetCount() const
c801d85f 400{
4832f7c0 401 return m_anchor == NULL ? 0u : m_anchor->GetChildrenCount();
edaa81ae 402}
c801d85f 403
f135ff73 404void wxTreeCtrl::SetIndent(unsigned int indent)
c801d85f 405{
f135ff73 406 m_indent = indent;
cf724bce
RR
407 m_dirty = TRUE;
408 Refresh();
409}
410
411void wxTreeCtrl::SetSpacing(unsigned int spacing)
412{
413 m_spacing = spacing;
414 m_dirty = TRUE;
f135ff73
VZ
415 Refresh();
416}
74bedbeb 417
4832f7c0
VZ
418size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, bool recursively)
419{
87138c52 420 wxCHECK_MSG( item.IsOk(), 0u, _T("invalid tree item") );
4832f7c0
VZ
421
422 return item.m_pItem->GetChildrenCount(recursively);
423}
424
f135ff73
VZ
425// -----------------------------------------------------------------------------
426// functions to work with tree items
427// -----------------------------------------------------------------------------
74bedbeb 428
f135ff73
VZ
429wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
430{
87138c52 431 wxCHECK_MSG( item.IsOk(), _T(""), _T("invalid tree item") );
4832f7c0 432
f135ff73 433 return item.m_pItem->GetText();
edaa81ae 434}
74bedbeb 435
f135ff73 436int wxTreeCtrl::GetItemImage(const wxTreeItemId& item) const
74bedbeb 437{
87138c52 438 wxCHECK_MSG( item.IsOk(), -1, _T("invalid tree item") );
4832f7c0 439
f135ff73 440 return item.m_pItem->GetImage();
edaa81ae 441}
c801d85f 442
f135ff73 443int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId& item) const
c801d85f 444{
87138c52 445 wxCHECK_MSG( item.IsOk(), -1, _T("invalid tree item") );
4832f7c0 446
f135ff73 447 return item.m_pItem->GetSelectedImage();
edaa81ae 448}
c801d85f 449
f135ff73 450wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
c801d85f 451{
87138c52 452 wxCHECK_MSG( item.IsOk(), NULL, _T("invalid tree item") );
4832f7c0 453
f135ff73 454 return item.m_pItem->GetData();
edaa81ae 455}
c801d85f 456
f135ff73
VZ
457void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
458{
87138c52 459 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
4832f7c0 460
f135ff73 461 wxClientDC dc(this);
f992adf9
VZ
462 wxGenericTreeItem *pItem = item.m_pItem;
463 pItem->SetText(text, dc);
464 RefreshLine(pItem);
f135ff73 465}
c801d85f 466
f135ff73
VZ
467void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image)
468{
87138c52 469 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
4832f7c0 470
f992adf9
VZ
471 wxGenericTreeItem *pItem = item.m_pItem;
472 pItem->SetImage(image);
473 RefreshLine(pItem);
f135ff73 474}
c801d85f 475
f135ff73 476void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId& item, int image)
c801d85f 477{
87138c52 478 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
4832f7c0 479
f992adf9
VZ
480 wxGenericTreeItem *pItem = item.m_pItem;
481 pItem->SetSelectedImage(image);
482 RefreshLine(pItem);
edaa81ae 483}
c801d85f 484
f135ff73 485void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
c801d85f 486{
87138c52 487 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
4832f7c0 488
de646ed1 489 item.m_pItem->SetData(data);
edaa81ae 490}
c801d85f 491
f135ff73 492void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
c801d85f 493{
87138c52 494 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
4832f7c0 495
f992adf9
VZ
496 wxGenericTreeItem *pItem = item.m_pItem;
497 pItem->SetHasPlus(has);
498 RefreshLine(pItem);
edaa81ae 499}
c801d85f 500
ef44a621
VZ
501void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
502{
87138c52 503 wxCHECK_RET( item.IsOk(), _T("invalid tree item") );
ef44a621
VZ
504
505 // avoid redrawing the tree if no real change
506 wxGenericTreeItem *pItem = item.m_pItem;
507 if ( pItem->IsBold() != bold )
508 {
509 pItem->SetBold(bold);
510 RefreshLine(pItem);
511 }
512}
513
f135ff73
VZ
514// -----------------------------------------------------------------------------
515// item status inquiries
516// -----------------------------------------------------------------------------
517
df875e59 518bool wxTreeCtrl::IsVisible(const wxTreeItemId& WXUNUSED(item)) const
c801d85f 519{
87138c52 520 wxFAIL_MSG(_T("not implemented"));
f135ff73 521
c801d85f 522 return TRUE;
edaa81ae 523}
c801d85f 524
f135ff73 525bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
c801d85f 526{
87138c52 527 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
4832f7c0 528
f135ff73 529 return !item.m_pItem->GetChildren().IsEmpty();
edaa81ae 530}
c801d85f 531
f135ff73 532bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
c801d85f 533{
87138c52 534 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
4832f7c0 535
f135ff73
VZ
536 return item.m_pItem->IsExpanded();
537}
29d87bba 538
f135ff73
VZ
539bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
540{
87138c52 541 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
4832f7c0 542
f135ff73
VZ
543 return item.m_pItem->HasHilight();
544}
29d87bba 545
ef44a621
VZ
546bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
547{
87138c52 548 wxCHECK_MSG( item.IsOk(), FALSE, _T("invalid tree item") );
ef44a621
VZ
549
550 return item.m_pItem->IsBold();
551}
552
f135ff73
VZ
553// -----------------------------------------------------------------------------
554// navigation
555// -----------------------------------------------------------------------------
29d87bba 556
f135ff73
VZ
557wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
558{
87138c52 559 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
389cdc7a 560
f135ff73
VZ
561 return item.m_pItem->GetParent();
562}
29d87bba 563
f135ff73
VZ
564wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const
565{
87138c52 566 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
29d87bba 567
f135ff73
VZ
568 cookie = 0;
569 return GetNextChild(item, cookie);
570}
29d87bba 571
f135ff73
VZ
572wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const
573{
87138c52 574 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
29d87bba 575
4832f7c0
VZ
576 wxArrayTreeItems& children = item.m_pItem->GetChildren();
577 if ( (size_t)cookie < children.Count() )
578 {
978f38c2 579 return children.Item(cookie++);
4832f7c0
VZ
580 }
581 else
582 {
583 // there are no more of them
1e6d9499 584 return wxTreeItemId();
4832f7c0 585 }
f135ff73 586}
29d87bba 587
978f38c2
VZ
588wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
589{
87138c52 590 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
978f38c2
VZ
591
592 wxArrayTreeItems& children = item.m_pItem->GetChildren();
0a240683 593 return (children.IsEmpty() ? wxTreeItemId() : wxTreeItemId(children.Last()));
978f38c2
VZ
594}
595
f135ff73
VZ
596wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
597{
87138c52 598 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
f135ff73
VZ
599
600 wxGenericTreeItem *i = item.m_pItem;
601 wxGenericTreeItem *parent = i->GetParent();
602 if ( parent == NULL )
603 {
604 // root item doesn't have any siblings
1e6d9499 605 return wxTreeItemId();
edaa81ae 606 }
4832f7c0 607
f135ff73
VZ
608 wxArrayTreeItems& siblings = parent->GetChildren();
609 int index = siblings.Index(i);
3c67202d 610 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 611
f135ff73 612 size_t n = (size_t)(index + 1);
fdd8d7b5 613 return n == siblings.Count() ? wxTreeItemId() : wxTreeItemId(siblings[n]);
edaa81ae 614}
c801d85f 615
f135ff73 616wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
c801d85f 617{
87138c52 618 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
f135ff73
VZ
619
620 wxGenericTreeItem *i = item.m_pItem;
621 wxGenericTreeItem *parent = i->GetParent();
622 if ( parent == NULL )
c801d85f 623 {
f135ff73 624 // root item doesn't have any siblings
1e6d9499 625 return wxTreeItemId();
edaa81ae 626 }
4832f7c0 627
f135ff73
VZ
628 wxArrayTreeItems& siblings = parent->GetChildren();
629 int index = siblings.Index(i);
3c67202d 630 wxASSERT( index != wxNOT_FOUND ); // I'm not a child of my parent?
29d87bba 631
fdd8d7b5
VZ
632 return index == 0 ? wxTreeItemId()
633 : wxTreeItemId(siblings[(size_t)(index - 1)]);
f135ff73 634}
389cdc7a 635
f135ff73
VZ
636wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
637{
87138c52 638 wxFAIL_MSG(_T("not implemented"));
29d87bba 639
1e6d9499 640 return wxTreeItemId();
f135ff73 641}
29d87bba 642
f135ff73
VZ
643wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
644{
87138c52 645 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
29d87bba 646
87138c52 647 wxFAIL_MSG(_T("not implemented"));
29d87bba 648
1e6d9499 649 return wxTreeItemId();
f135ff73 650}
29d87bba 651
f135ff73
VZ
652wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
653{
87138c52 654 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), _T("invalid tree item") );
29d87bba 655
87138c52 656 wxFAIL_MSG(_T("not implemented"));
29d87bba 657
1e6d9499 658 return wxTreeItemId();
edaa81ae 659}
c801d85f 660
f135ff73
VZ
661// -----------------------------------------------------------------------------
662// operations
663// -----------------------------------------------------------------------------
664
665wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parentId,
666 size_t previous,
667 const wxString& text,
668 int image, int selImage,
669 wxTreeItemData *data)
c801d85f 670{
f135ff73
VZ
671 wxGenericTreeItem *parent = parentId.m_pItem;
672 if ( !parent )
673 {
674 // should we give a warning here?
675 return AddRoot(text, image, selImage, data);
676 }
4832f7c0 677
f135ff73
VZ
678 wxClientDC dc(this);
679 wxGenericTreeItem *item = new wxGenericTreeItem(parent,
680 text, dc,
681 image, selImage,
682 data);
74bedbeb 683
f135ff73 684 if ( data != NULL )
c801d85f 685 {
f135ff73
VZ
686 data->m_pItem = item;
687 }
74bedbeb 688
f135ff73 689 parent->Insert( item, previous );
ef44a621 690
3db7be80 691 m_dirty = TRUE;
389cdc7a 692
f135ff73 693 return item;
4c681997
RR
694}
695
f135ff73
VZ
696wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
697 int image, int selImage,
698 wxTreeItemData *data)
4c681997 699{
87138c52 700 wxCHECK_MSG( !m_anchor, wxTreeItemId(), _T("tree can have only one root") );
389cdc7a 701
f135ff73
VZ
702 wxClientDC dc(this);
703 m_anchor = new wxGenericTreeItem((wxGenericTreeItem *)NULL, text, dc,
704 image, selImage, data);
705 if ( data != NULL )
706 {
707 data->m_pItem = m_anchor;
708 }
389cdc7a 709
f135ff73 710 AdjustMyScrollbars();
a32dd690 711 Refresh();
a32dd690 712
f135ff73 713 return m_anchor;
edaa81ae 714}
c801d85f 715
f135ff73
VZ
716wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
717 const wxString& text,
718 int image, int selImage,
719 wxTreeItemData *data)
c801d85f 720{
f135ff73 721 return DoInsertItem(parent, 0u, text, image, selImage, data);
edaa81ae 722}
c801d85f 723
f135ff73
VZ
724wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parentId,
725 const wxTreeItemId& idPrevious,
726 const wxString& text,
727 int image, int selImage,
728 wxTreeItemData *data)
c801d85f 729{
f135ff73
VZ
730 wxGenericTreeItem *parent = parentId.m_pItem;
731 if ( !parent )
732 {
733 // should we give a warning here?
734 return AddRoot(text, image, selImage, data);
735 }
c801d85f 736
f135ff73 737 int index = parent->GetChildren().Index(idPrevious.m_pItem);
3c67202d 738 wxASSERT_MSG( index != wxNOT_FOUND,
87138c52 739 _T("previous item in wxTreeCtrl::InsertItem() is not a sibling") );
f135ff73 740 return DoInsertItem(parentId, (size_t)index, text, image, selImage, data);
edaa81ae 741}
c801d85f 742
f135ff73
VZ
743wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parentId,
744 const wxString& text,
745 int image, int selImage,
746 wxTreeItemData *data)
74bedbeb 747{
f135ff73
VZ
748 wxGenericTreeItem *parent = parentId.m_pItem;
749 if ( !parent )
750 {
751 // should we give a warning here?
752 return AddRoot(text, image, selImage, data);
753 }
754
755 return DoInsertItem(parent, parent->GetChildren().Count(), text,
756 image, selImage, data);
74bedbeb
VZ
757}
758
a43a4f9d
VZ
759void wxTreeCtrl::SendDeleteEvent(wxGenericTreeItem *item)
760{
761 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, GetId() );
762 event.m_item = item;
763 event.SetEventObject( this );
764 ProcessEvent( event );
765}
766
372edb9d
VZ
767void wxTreeCtrl::DeleteChildren(const wxTreeItemId& itemId)
768{
769 wxGenericTreeItem *item = itemId.m_pItem;
a43a4f9d 770 item->DeleteChildren(this);
372edb9d
VZ
771
772 m_dirty = TRUE;
773}
774
f135ff73 775void wxTreeCtrl::Delete(const wxTreeItemId& itemId)
c801d85f 776{
f135ff73 777 wxGenericTreeItem *item = itemId.m_pItem;
ff5bf259
VZ
778 wxGenericTreeItem *parent = item->GetParent();
779
780 if ( parent )
781 {
782 parent->GetChildren().Remove(item);
783 }
f135ff73 784
a43a4f9d
VZ
785 item->DeleteChildren(this);
786 SendDeleteEvent(item);
f135ff73
VZ
787 delete item;
788
4bb19cfb 789 m_dirty = TRUE;
edaa81ae 790}
c801d85f 791
f135ff73 792void wxTreeCtrl::DeleteAllItems()
c801d85f 793{
f135ff73
VZ
794 if ( m_anchor )
795 {
a43a4f9d 796 m_anchor->DeleteChildren(this);
f135ff73 797 delete m_anchor;
a43a4f9d 798
f135ff73
VZ
799 m_anchor = NULL;
800
4bb19cfb 801 m_dirty = TRUE;
f135ff73 802 }
edaa81ae
RR
803}
804
f135ff73 805void wxTreeCtrl::Expand(const wxTreeItemId& itemId)
edaa81ae 806{
f135ff73
VZ
807 wxGenericTreeItem *item = itemId.m_pItem;
808
978f38c2 809 if ( !item->HasPlus() )
4bb19cfb 810 return;
978f38c2 811
f135ff73
VZ
812 if ( item->IsExpanded() )
813 return;
814
815 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId() );
816 event.m_item = item;
817 event.SetEventObject( this );
818 if ( ProcessEvent( event ) && event.m_code )
819 {
820 // cancelled by program
821 return;
822 }
4832f7c0 823
f135ff73 824 item->Expand();
28ab302b 825 CalculatePositions();
f135ff73
VZ
826
827 RefreshSubtree(item);
828
829 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
830 ProcessEvent( event );
edaa81ae
RR
831}
832
f135ff73 833void wxTreeCtrl::Collapse(const wxTreeItemId& itemId)
edaa81ae 834{
f135ff73
VZ
835 wxGenericTreeItem *item = itemId.m_pItem;
836
837 if ( !item->IsExpanded() )
838 return;
839
840 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId() );
841 event.m_item = item;
842 event.SetEventObject( this );
843 if ( ProcessEvent( event ) && event.m_code )
edaa81ae 844 {
f135ff73
VZ
845 // cancelled by program
846 return;
847 }
4832f7c0 848
f135ff73
VZ
849 item->Collapse();
850
851 wxArrayTreeItems& children = item->GetChildren();
852 size_t count = children.Count();
853 for ( size_t n = 0; n < count; n++ )
854 {
855 Collapse(children[n]);
edaa81ae 856 }
f135ff73
VZ
857
858 CalculatePositions();
859
860 RefreshSubtree(item);
861
862 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
863 ProcessEvent( event );
edaa81ae 864}
c801d85f 865
f135ff73 866void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
c801d85f 867{
f135ff73 868 Collapse(item);
372edb9d 869 DeleteChildren(item);
edaa81ae 870}
c801d85f 871
f135ff73 872void wxTreeCtrl::Toggle(const wxTreeItemId& itemId)
c801d85f 873{
f135ff73 874 wxGenericTreeItem *item = itemId.m_pItem;
389cdc7a 875
f135ff73
VZ
876 if ( item->IsExpanded() )
877 Collapse(itemId);
878 else
879 Expand(itemId);
880}
389cdc7a 881
f135ff73
VZ
882void wxTreeCtrl::Unselect()
883{
884 if ( m_current )
885 {
886 m_current->SetHilight( FALSE );
887 RefreshLine( m_current );
888 }
edaa81ae 889}
c801d85f 890
f135ff73 891void wxTreeCtrl::SelectItem(const wxTreeItemId& itemId)
389cdc7a 892{
f135ff73
VZ
893 wxGenericTreeItem *item = itemId.m_pItem;
894
895 if ( m_current != item )
389cdc7a 896 {
f135ff73
VZ
897 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, GetId() );
898 event.m_item = item;
899 event.m_itemOld = m_current;
900 event.SetEventObject( this );
6daa0637 901 if ( GetEventHandler()->ProcessEvent( event ) && event.WasVetoed() )
f135ff73
VZ
902 return;
903
904 if ( m_current )
389cdc7a
VZ
905 {
906 m_current->SetHilight( FALSE );
907 RefreshLine( m_current );
edaa81ae 908 }
f135ff73 909
389cdc7a
VZ
910 m_current = item;
911 m_current->SetHilight( TRUE );
912 RefreshLine( m_current );
913
f135ff73 914 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
6daa0637 915 GetEventHandler()->ProcessEvent( event );
389cdc7a
VZ
916 }
917}
918
6daa0637 919void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
c801d85f 920{
0659e7ee 921 wxGenericTreeItem *gitem = item.m_pItem;
ef44a621 922
f65635b5
VZ
923 // first expand all parent branches
924 wxGenericTreeItem *parent = gitem->GetParent();
925 while ( parent && !parent->IsExpanded() )
926 {
927 Expand(parent);
928
929 parent = parent->GetParent();
930 }
931
932 // now scroll to the item
0659e7ee 933 int item_y = gitem->GetY();
ef44a621 934
0659e7ee
RR
935 int start_x = 0;
936 int start_y = 0;
937 ViewStart( &start_x, &start_y );
938 start_y *= 10;
978f38c2 939
a93109d5
RR
940 int client_h = 0;
941 int client_w = 0;
942 GetClientSize( &client_w, &client_h );
ef44a621 943
0659e7ee
RR
944 if (item_y < start_y+3)
945 {
946 int x = 0;
947 int y = 0;
948 m_anchor->GetSize( x, y );
949 y += 2*m_lineHeight;
950 int x_pos = GetScrollPos( wxHORIZONTAL );
f65635b5 951 SetScrollbars( 10, 10, x/10, y/10, x_pos, (item_y-client_h/2)/10 );
0659e7ee 952 }
f65635b5 953 else if (item_y > start_y+client_h-16)
0659e7ee
RR
954 {
955 int x = 0;
956 int y = 0;
957 m_anchor->GetSize( x, y );
958 y += 2*m_lineHeight;
959 int x_pos = GetScrollPos( wxHORIZONTAL );
a93109d5 960 SetScrollbars( 10, 10, x/10, y/10, x_pos, (item_y-client_h/2)/10 );
0659e7ee 961 }
edaa81ae 962}
c801d85f 963
df875e59 964void wxTreeCtrl::ScrollTo(const wxTreeItemId& WXUNUSED(item))
c801d85f 965{
87138c52 966 wxFAIL_MSG(_T("not implemented"));
edaa81ae 967}
c801d85f 968
df875e59
RR
969wxTextCtrl *wxTreeCtrl::EditLabel( const wxTreeItemId& WXUNUSED(item),
970 wxClassInfo* WXUNUSED(textCtrlClass) )
c801d85f 971{
87138c52 972 wxFAIL_MSG(_T("not implemented"));
c801d85f 973
bbe0af5b 974 return (wxTextCtrl*)NULL;
edaa81ae 975}
c801d85f 976
f135ff73 977wxTextCtrl *wxTreeCtrl::GetEditControl() const
c801d85f 978{
87138c52 979 wxFAIL_MSG(_T("not implemented"));
c801d85f 980
0659e7ee 981 return (wxTextCtrl*)NULL;
edaa81ae 982}
c801d85f 983
df875e59 984void wxTreeCtrl::EndEditLabel(const wxTreeItemId& WXUNUSED(item), bool WXUNUSED(discardChanges))
74bedbeb 985{
87138c52 986 wxFAIL_MSG(_T("not implemented"));
74bedbeb
VZ
987}
988
e1ee62bd
VZ
989// FIXME: tree sorting functions are not reentrant and not MT-safe!
990static wxTreeCtrl *s_treeBeingSorted = NULL;
0659e7ee 991
e1ee62bd
VZ
992static int tree_ctrl_compare_func(wxGenericTreeItem **item1,
993 wxGenericTreeItem **item2)
edaa81ae 994{
87138c52 995 wxCHECK_MSG( s_treeBeingSorted, 0, _T("bug in wxTreeCtrl::SortChildren()") );
e1ee62bd
VZ
996
997 return s_treeBeingSorted->OnCompareItems(*item1, *item2);
0659e7ee
RR
998}
999
e1ee62bd
VZ
1000int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1001 const wxTreeItemId& item2)
0659e7ee 1002{
87138c52 1003 return wxStrcmp(GetItemText(item1), GetItemText(item2));
e1ee62bd
VZ
1004}
1005
1006void wxTreeCtrl::SortChildren(const wxTreeItemId& itemId)
1007{
87138c52 1008 wxCHECK_RET( itemId.IsOk(), _T("invalid tree item") );
e1ee62bd
VZ
1009
1010 wxGenericTreeItem *item = itemId.m_pItem;
978f38c2 1011
e1ee62bd 1012 wxCHECK_RET( !s_treeBeingSorted,
87138c52 1013 _T("wxTreeCtrl::SortChildren is not reentrant") );
e1ee62bd
VZ
1014
1015 wxArrayTreeItems& children = item->GetChildren();
1016 if ( children.Count() > 1 )
1017 {
1018 s_treeBeingSorted = this;
1019 children.Sort(tree_ctrl_compare_func);
1020 s_treeBeingSorted = NULL;
978f38c2 1021
e1ee62bd
VZ
1022 m_dirty = TRUE;
1023 }
1024 //else: don't make the tree dirty as nothing changed
edaa81ae
RR
1025}
1026
f135ff73 1027wxImageList *wxTreeCtrl::GetImageList() const
edaa81ae 1028{
f135ff73 1029 return m_imageListNormal;
edaa81ae
RR
1030}
1031
f135ff73 1032wxImageList *wxTreeCtrl::GetStateImageList() const
c801d85f 1033{
f135ff73 1034 return m_imageListState;
edaa81ae 1035}
c801d85f 1036
f135ff73 1037void wxTreeCtrl::SetImageList(wxImageList *imageList)
e2414cbe 1038{
d30b4d20
KB
1039 m_imageListNormal = imageList;
1040 // calculate a m_lineHeight value from the image sizes
1041 wxPaintDC dc(this);
1042 PrepareDC( dc );
1043 m_lineHeight = (int)(dc.GetCharHeight() + 4);
1044 int
1045 width = 0,
1046 height = 0,
1047 n = m_imageListNormal->GetImageCount();
1048 for(int i = 0; i < n ; i++)
1049 {
1050 m_imageListNormal->GetSize(i, width, height);
f0594f42 1051 height += height/5; //20% extra spacing
d30b4d20
KB
1052 if(height > m_lineHeight) m_lineHeight = height;
1053 }
edaa81ae 1054}
e2414cbe 1055
f135ff73 1056void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
e2414cbe 1057{
f135ff73 1058 m_imageListState = imageList;
edaa81ae 1059}
e2414cbe 1060
f135ff73
VZ
1061// -----------------------------------------------------------------------------
1062// helpers
1063// -----------------------------------------------------------------------------
0659e7ee 1064
74bedbeb 1065void wxTreeCtrl::AdjustMyScrollbars()
c801d85f 1066{
0659e7ee
RR
1067 if (m_anchor)
1068 {
1069 int x = 0;
1070 int y = 0;
1071 m_anchor->GetSize( x, y );
1072 y += 2*m_lineHeight;
1073 int x_pos = GetScrollPos( wxHORIZONTAL );
1074 int y_pos = GetScrollPos( wxVERTICAL );
1075 SetScrollbars( 10, 10, x/10, y/10, x_pos, y_pos );
1076 }
1077 else
1078 {
1079 SetScrollbars( 0, 0, 0, 0 );
1080 }
edaa81ae 1081}
c801d85f 1082
ef44a621
VZ
1083void wxTreeCtrl::PaintItem(wxGenericTreeItem *item, wxDC& dc)
1084{
f65635b5 1085 // render bold items in bold
bbe0af5b
RR
1086 wxFont fontOld;
1087 wxFont fontNew;
978f38c2 1088
bbe0af5b
RR
1089 if (item->IsBold())
1090 {
1091 fontOld = dc.GetFont();
1092 if (fontOld.Ok())
1093 {
f65635b5 1094 // VZ: is there any better way to make a bold variant of old font?
bbe0af5b
RR
1095 fontNew = wxFont( fontOld.GetPointSize(),
1096 fontOld.GetFamily(),
1097 fontOld.GetStyle(),
1098 wxBOLD,
1099 fontOld.GetUnderlined());
1100 dc.SetFont(fontNew);
1101 }
1102 else
1103 {
87138c52 1104 wxFAIL_MSG(_T("wxDC::GetFont() failed!"));
bbe0af5b
RR
1105 }
1106 }
ef44a621 1107
bbe0af5b
RR
1108 long text_w = 0;
1109 long text_h = 0;
1110 dc.GetTextExtent( item->GetText(), &text_w, &text_h );
ef44a621 1111
bbe0af5b
RR
1112 int image_h = 0;
1113 int image_w = 0;
1114 if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
1115 {
1116 m_imageListNormal->GetSize( item->GetSelectedImage(), image_w, image_h );
1117 image_w += 4;
978f38c2 1118 }
bbe0af5b
RR
1119 else if (item->GetImage() != -1)
1120 {
1121 m_imageListNormal->GetSize( item->GetImage(), image_w, image_h );
1122 image_w += 4;
1123 }
ef44a621 1124
d30b4d20 1125 int total_h = (image_h > text_h) ? image_h : text_h;
f0594f42
KB
1126 if(m_lineHeight > total_h) total_h = m_lineHeight;
1127
5679f335 1128 dc.DrawRectangle( item->GetX()-2, item->GetY(), image_w+text_w+4, total_h );
ef44a621 1129
bbe0af5b
RR
1130 if ((item->IsExpanded()) && (item->GetSelectedImage() != -1))
1131 {
d30b4d20 1132 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
bbe0af5b 1133 m_imageListNormal->Draw( item->GetSelectedImage(), dc,
49cd56ef 1134 item->GetX(),
d701d432 1135 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
1136 wxIMAGELIST_DRAW_TRANSPARENT );
1137 dc.DestroyClippingRegion();
1138 }
1139 else if (item->GetImage() != -1)
1140 {
d30b4d20 1141 dc.SetClippingRegion( item->GetX(), item->GetY(), image_w-2, total_h );
bbe0af5b 1142 m_imageListNormal->Draw( item->GetImage(), dc,
49cd56ef 1143 item->GetX(),
d701d432 1144 item->GetY() +((total_h > image_h)?((total_h-image_h)/2):0),
bbe0af5b
RR
1145 wxIMAGELIST_DRAW_TRANSPARENT );
1146 dc.DestroyClippingRegion();
1147 }
ef44a621 1148
bbe0af5b 1149 dc.SetBackgroundMode(wxTRANSPARENT);
d30b4d20 1150 dc.DrawText( item->GetText(), image_w + item->GetX(), item->GetY()
49cd56ef 1151 + ((total_h > text_h) ? (total_h - text_h)/2 : 0));
ef44a621 1152
f65635b5 1153 // restore normal font for bold items
bbe0af5b
RR
1154 if (fontOld.Ok())
1155 {
1156 dc.SetFont( fontOld);
1157 }
ef44a621
VZ
1158}
1159
16c1f7f3 1160void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 1161{
bbe0af5b 1162 int horizX = level*m_indent;
389cdc7a 1163
cf724bce 1164 item->SetX( horizX+m_indent+m_spacing );
f0594f42 1165 item->SetY( y-m_lineHeight/2 );
bbe0af5b 1166 item->SetHeight( m_lineHeight );
389cdc7a 1167
cf724bce 1168 item->SetCross( horizX+m_indent, y );
4c681997 1169
bbe0af5b 1170 int oldY = y;
389cdc7a 1171
bbe0af5b
RR
1172 int exposed_x = dc.LogicalToDeviceX( 0 );
1173 int exposed_y = dc.LogicalToDeviceY( item->GetY()-2 );
4832f7c0 1174
bbe0af5b
RR
1175 if (IsExposed( exposed_x, exposed_y, 10000, m_lineHeight+4 )) // 10000 = very much
1176 {
1177 int startX = horizX;
cf724bce 1178 int endX = horizX + (m_indent-5);
29d87bba 1179
cf724bce 1180// if (!item->HasChildren()) endX += (m_indent+5);
bbe0af5b 1181 if (!item->HasChildren()) endX += 20;
4832f7c0 1182
bbe0af5b 1183 dc.DrawLine( startX, y, endX, y );
29d87bba 1184
bbe0af5b
RR
1185 if (item->HasPlus())
1186 {
cf724bce 1187 dc.DrawLine( horizX+(m_indent+5), y, horizX+(m_indent+15), y );
bbe0af5b
RR
1188 dc.SetPen( *wxGREY_PEN );
1189 dc.SetBrush( *wxWHITE_BRUSH );
cf724bce 1190 dc.DrawRectangle( horizX+(m_indent-5), y-4, 11, 9 );
bbe0af5b 1191 dc.SetPen( *wxBLACK_PEN );
cf724bce 1192 dc.DrawLine( horizX+(m_indent-2), y, horizX+(m_indent+3), y );
bbe0af5b
RR
1193
1194 if (!item->IsExpanded())
d30b4d20 1195 {
cf724bce 1196 dc.DrawLine( horizX+m_indent, y-2, horizX+m_indent, y+3 );
d30b4d20 1197 }
bbe0af5b 1198 }
c801d85f 1199
bbe0af5b
RR
1200 if (item->HasHilight())
1201 {
1202 dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
a367b9b3 1203
bbe0af5b 1204 dc.SetBrush( *m_hilightBrush );
4832f7c0 1205
bbe0af5b
RR
1206 if (m_hasFocus)
1207 dc.SetPen( *wxBLACK_PEN );
1208 else
1209 dc.SetPen( *wxTRANSPARENT_PEN );
4832f7c0 1210
bbe0af5b 1211 PaintItem(item, dc);
f135ff73 1212
bbe0af5b
RR
1213 dc.SetPen( *wxBLACK_PEN );
1214 dc.SetTextForeground( *wxBLACK );
1215 dc.SetBrush( *wxWHITE_BRUSH );
1216 }
1217 else
1218 {
1219 dc.SetBrush( *wxWHITE_BRUSH );
1220 dc.SetPen( *wxTRANSPARENT_PEN );
4832f7c0 1221
bbe0af5b 1222 PaintItem(item, dc);
4832f7c0 1223
bbe0af5b
RR
1224 dc.SetPen( *wxBLACK_PEN );
1225 }
f135ff73 1226 }
e2414cbe 1227
bbe0af5b
RR
1228 if (item->IsExpanded())
1229 {
1230 int semiOldY = y;
389cdc7a 1231
bbe0af5b
RR
1232 wxArrayTreeItems& children = item->GetChildren();
1233 size_t count = children.Count();
1234 for ( size_t n = 0; n < count; n++ )
1235 {
1236 y += m_lineHeight;
1237 semiOldY = y;
1238 PaintLevel( children[n], dc, level+1, y );
1239 }
389cdc7a 1240
f65635b5
VZ
1241 // it may happen that the item is expanded but has no items (when you
1242 // delete all its children for example) - don't draw the vertical line
1243 // in this case
1244 if (count > 0)
cf724bce 1245 dc.DrawLine( horizX+m_indent, oldY+5, horizX+m_indent, semiOldY );
bbe0af5b 1246 }
4c681997 1247}
c801d85f 1248
f135ff73
VZ
1249// -----------------------------------------------------------------------------
1250// wxWindows callbacks
1251// -----------------------------------------------------------------------------
1252
3db7be80 1253void wxTreeCtrl::OnPaint( wxPaintEvent &WXUNUSED(event) )
c801d85f 1254{
0659e7ee
RR
1255 if ( !m_anchor )
1256 return;
c801d85f 1257
0659e7ee
RR
1258 wxPaintDC dc(this);
1259 PrepareDC( dc );
29d87bba 1260
f60d0f94 1261 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) );
29d87bba 1262
0659e7ee 1263 dc.SetPen( m_dottedPen );
d30b4d20
KB
1264 if(GetImageList() == NULL)
1265 m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 1266
0659e7ee
RR
1267 int y = m_lineHeight / 2 + 2;
1268 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 1269}
c801d85f 1270
3db7be80 1271void wxTreeCtrl::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
c801d85f 1272{
0659e7ee 1273 m_hasFocus = TRUE;
978f38c2 1274
bbe0af5b 1275 if (m_current) RefreshLine( m_current );
edaa81ae 1276}
c801d85f 1277
3db7be80 1278void wxTreeCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
c801d85f 1279{
0659e7ee 1280 m_hasFocus = FALSE;
978f38c2 1281
bbe0af5b 1282 if (m_current) RefreshLine( m_current );
edaa81ae 1283}
c801d85f
KB
1284
1285void wxTreeCtrl::OnChar( wxKeyEvent &event )
1286{
978f38c2
VZ
1287 wxTreeEvent te( wxEVT_COMMAND_TREE_KEY_DOWN, GetId() );
1288 te.m_code = event.KeyCode();
1289 te.SetEventObject( this );
1290 GetEventHandler()->ProcessEvent( te );
435fe83e 1291
978f38c2
VZ
1292 if (m_current == 0)
1293 {
1294 event.Skip();
1295 return;
1296 }
ef44a621 1297
978f38c2
VZ
1298 switch (event.KeyCode())
1299 {
1300 case '+':
1301 case WXK_ADD:
1302 if (m_current->HasPlus() && !IsExpanded(m_current))
1303 {
1304 Expand(m_current);
1305 }
1306 break;
ef44a621 1307
978f38c2
VZ
1308 case '-':
1309 case WXK_SUBTRACT:
1310 if (IsExpanded(m_current))
1311 {
1312 Collapse(m_current);
1313 }
1314 break;
ef44a621 1315
978f38c2
VZ
1316 case '*':
1317 case WXK_MULTIPLY:
1318 Toggle(m_current);
1319 break;
ef44a621 1320
978f38c2
VZ
1321 case ' ':
1322 case WXK_RETURN:
1323 {
1324 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1325 event.m_item = m_current;
1326 event.m_code = 0;
1327 event.SetEventObject( this );
1328 GetEventHandler()->ProcessEvent( event );
1329 }
1330 break;
ef44a621 1331
978f38c2
VZ
1332 // up goes to the previous sibling or to the last of its children if
1333 // it's expanded
1334 case WXK_UP:
1335 {
1336 wxTreeItemId prev = GetPrevSibling( m_current );
1337 if (!prev)
1338 {
1339 prev = GetParent( m_current );
90e58684
RR
1340 if (prev)
1341 {
1342 long cockie = 0;
1343 wxTreeItemId current = m_current;
1344 if (current == GetFirstChild( prev, cockie ))
1345 {
1346 // otherwise we return to where we came from
1347 SelectItem( prev );
1348 EnsureVisible( prev );
1349 break;
1350 }
978f38c2
VZ
1351 }
1352 }
1353 if (prev)
1354 {
69a282d4 1355 while ( IsExpanded(prev) && HasChildren(prev) )
978f38c2 1356 {
69a282d4
VZ
1357 wxTreeItemId child = GetLastChild(prev);
1358 if ( child )
1359 {
1360 prev = child;
1361 }
978f38c2 1362 }
69a282d4 1363
978f38c2
VZ
1364 SelectItem( prev );
1365 EnsureVisible( prev );
1366 }
1367 }
1368 break;
ef44a621 1369
978f38c2
VZ
1370 // left arrow goes to the parent
1371 case WXK_LEFT:
1372 {
1373 wxTreeItemId prev = GetParent( m_current );
1374 if (prev)
1375 {
1376 EnsureVisible( prev );
1377 SelectItem( prev );
1378 }
1379 }
1380 break;
ef44a621 1381
978f38c2
VZ
1382 case WXK_RIGHT:
1383 // this works the same as the down arrow except that we also expand the
1384 // item if it wasn't expanded yet
1385 Expand(m_current);
1386 // fall through
1387
1388 case WXK_DOWN:
ef44a621 1389 {
69a282d4 1390 if (IsExpanded(m_current) && HasChildren(m_current))
978f38c2
VZ
1391 {
1392 long cookie = 0;
1393 wxTreeItemId child = GetFirstChild( m_current, cookie );
1394 SelectItem( child );
1395 EnsureVisible( child );
1396 }
1397 else
1398 {
1399 wxTreeItemId next = GetNextSibling( m_current );
1400 if (next == 0)
1401 {
1402 wxTreeItemId current = m_current;
1403 while (current && !next)
1404 {
1405 current = GetParent( current );
1406 if (current) next = GetNextSibling( current );
1407 }
1408 }
1409 if (next != 0)
1410 {
1411 SelectItem( next );
1412 EnsureVisible( next );
1413 }
1414 }
ef44a621 1415 }
978f38c2 1416 break;
ef44a621 1417
978f38c2
VZ
1418 // <End> selects the last visible tree item
1419 case WXK_END:
1420 {
1421 wxTreeItemId last = GetRootItem();
1422
1423 while ( last.IsOk() && IsExpanded(last) )
1424 {
1425 wxTreeItemId lastChild = GetLastChild(last);
1426
1427 // it may happen if the item was expanded but then all of
1428 // its children have been deleted - so IsExpanded() returned
1429 // TRUE, but GetLastChild() returned invalid item
1430 if ( !lastChild )
1431 break;
1432
1433 last = lastChild;
1434 }
1435
1436 if ( last.IsOk() )
1437 {
1438 EnsureVisible( last );
1439 SelectItem( last );
1440 }
1441 }
1442 break;
1443
1444 // <Home> selects the root item
1445 case WXK_HOME:
1446 {
1447 wxTreeItemId prev = GetRootItem();
1448 if (prev)
1449 {
1450 EnsureVisible( prev );
1451 SelectItem( prev );
1452 }
1453 }
1454 break;
1455
1456 default:
1457 event.Skip();
1458 }
edaa81ae 1459}
c801d85f 1460
4f22cf8d
RR
1461wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& WXUNUSED(flags))
1462{
67a7abf7
VZ
1463 wxClientDC dc(this);
1464 PrepareDC(dc);
1465 long x = dc.DeviceToLogicalX( (long)point.x );
1466 long y = dc.DeviceToLogicalY( (long)point.y );
1467
bbe0af5b 1468 bool onButton = FALSE;
67a7abf7 1469 return m_anchor->HitTest( wxPoint(x, y), onButton );
4f22cf8d
RR
1470}
1471
3db7be80 1472void wxTreeCtrl::OnMouse( wxMouseEvent &event )
c801d85f 1473{
bbe0af5b 1474 if (!event.LeftIsDown()) m_dragCount = 0;
f135ff73 1475
bbe0af5b 1476 if ( !(event.LeftDown() || event.LeftDClick() || event.Dragging()) ) return;
29d87bba 1477
bbe0af5b 1478 if ( !m_anchor ) return;
978f38c2 1479
bbe0af5b
RR
1480 wxClientDC dc(this);
1481 PrepareDC(dc);
1482 long x = dc.DeviceToLogicalX( (long)event.GetX() );
1483 long y = dc.DeviceToLogicalY( (long)event.GetY() );
29d87bba 1484
bbe0af5b
RR
1485 bool onButton = FALSE;
1486 wxGenericTreeItem *item = m_anchor->HitTest( wxPoint(x,y), onButton );
978f38c2 1487
bbe0af5b 1488 if (item == NULL) return; /* we hit the blank area */
29d87bba 1489
bbe0af5b
RR
1490 if (event.Dragging())
1491 {
1492 if (m_dragCount == 2) /* small drag latency (3?) */
1493 {
1494 m_dragCount = 0;
978f38c2 1495
bbe0af5b
RR
1496 wxTreeEvent nevent(wxEVT_COMMAND_TREE_BEGIN_DRAG, GetId());
1497 nevent.m_item = m_current;
1498 nevent.SetEventObject(this);
1499 GetEventHandler()->ProcessEvent(nevent);
1500 }
1501 else
1502 {
1503 m_dragCount++;
1504 }
1505 return;
1506 }
978f38c2 1507
f65635b5
VZ
1508 if (!IsSelected(item))
1509 SelectItem(item); /* we dont support multiple selections, BTW */
29d87bba 1510
bbe0af5b
RR
1511 if (event.LeftDClick())
1512 {
1513 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_ACTIVATED, GetId() );
1514 event.m_item = item;
1515 event.m_code = 0;
1516 event.SetEventObject( this );
1517 GetEventHandler()->ProcessEvent( event );
1518 }
29d87bba 1519
bbe0af5b
RR
1520 if (onButton)
1521 {
1522 Toggle( item );
1523 }
edaa81ae 1524}
c801d85f 1525
3db7be80
RR
1526void wxTreeCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
1527{
bbe0af5b
RR
1528 /* after all changes have been done to the tree control,
1529 * we actually redraw the tree when everything is over */
ef44a621 1530
f65635b5
VZ
1531 if (!m_dirty)
1532 return;
ef44a621 1533
bbe0af5b 1534 m_dirty = FALSE;
3db7be80 1535
bbe0af5b
RR
1536 CalculatePositions();
1537
1538 AdjustMyScrollbars();
3db7be80
RR
1539}
1540
f135ff73 1541// -----------------------------------------------------------------------------
bbe0af5b
RR
1542
1543void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 1544{
bbe0af5b 1545 int horizX = level*m_indent;
389cdc7a 1546
cf724bce 1547 item->SetX( horizX+m_indent+m_spacing );
f0594f42 1548 item->SetY( y-m_lineHeight/2 );
bbe0af5b 1549 item->SetHeight( m_lineHeight );
4c681997 1550
bbe0af5b
RR
1551 if ( !item->IsExpanded() )
1552 {
f65635b5 1553 // we dont need to calculate collapsed branches
bbe0af5b
RR
1554 return;
1555 }
389cdc7a 1556
bbe0af5b
RR
1557 wxArrayTreeItems& children = item->GetChildren();
1558 size_t count = children.Count();
1559 for ( size_t n = 0; n < count; n++ )
1560 {
1561 y += m_lineHeight;
f65635b5 1562 CalculateLevel( children[n], dc, level+1, y ); // recurse
bbe0af5b 1563 }
edaa81ae 1564}
c801d85f 1565
74bedbeb 1566void wxTreeCtrl::CalculatePositions()
c801d85f 1567{
bbe0af5b 1568 if ( !m_anchor ) return;
29d87bba 1569
bbe0af5b
RR
1570 wxClientDC dc(this);
1571 PrepareDC( dc );
29d87bba 1572
bbe0af5b 1573 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_DEFAULT_GUI_FONT ) );
29d87bba 1574
bbe0af5b 1575 dc.SetPen( m_dottedPen );
d30b4d20
KB
1576 if(GetImageList() == NULL)
1577 m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 1578
bbe0af5b 1579 int y = m_lineHeight / 2 + 2;
f65635b5 1580 CalculateLevel( m_anchor, dc, 0, y ); // start recursion
edaa81ae 1581}
c801d85f 1582
f135ff73 1583void wxTreeCtrl::RefreshSubtree(wxGenericTreeItem *item)
c801d85f 1584{
bbe0af5b
RR
1585 wxClientDC dc(this);
1586 PrepareDC(dc);
4832f7c0 1587
bbe0af5b
RR
1588 int cw = 0;
1589 int ch = 0;
1590 GetClientSize( &cw, &ch );
4832f7c0 1591
bbe0af5b
RR
1592 wxRect rect;
1593 rect.x = dc.LogicalToDeviceX( 0 );
1594 rect.width = cw;
1595 rect.y = dc.LogicalToDeviceY( item->GetY() );
1596 rect.height = ch;
f135ff73 1597
bbe0af5b 1598 Refresh( TRUE, &rect );
f135ff73 1599
bbe0af5b 1600 AdjustMyScrollbars();
edaa81ae 1601}
c801d85f
KB
1602
1603void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
1604{
bbe0af5b
RR
1605 wxClientDC dc(this);
1606 PrepareDC( dc );
1607
1608 wxRect rect;
1609 rect.x = dc.LogicalToDeviceX( item->GetX() - 2 );
1610 rect.y = dc.LogicalToDeviceY( item->GetY() - 2 );
1611 rect.width = 1000;
1612 rect.height = dc.GetCharHeight() + 6;
978f38c2 1613
bbe0af5b 1614 Refresh( TRUE, &rect );
edaa81ae 1615}
c801d85f 1616