]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectrl.cpp
corrected wxListCtrl compilation problems
[wxWidgets.git] / src / generic / treectrl.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: treectrl.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
389cdc7a 6// Id: $Id$
c801d85f 7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
29d87bba 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
12#pragma implementation "treectrl.h"
13#endif
14
15#include "wx/treectrl.h"
16#include "wx/settings.h"
389cdc7a 17#include "wx/log.h"
f4e325b3 18#include <wx/intl.h>
c801d85f
KB
19
20//-----------------------------------------------------------------------------
21// wxTreeItem
22//-----------------------------------------------------------------------------
23
24IMPLEMENT_DYNAMIC_CLASS(wxTreeItem, wxObject)
25
74bedbeb 26wxTreeItem::wxTreeItem()
c801d85f
KB
27{
28 m_mask = 0;
29 m_itemId = 0;
30 m_state = 0;
31 m_stateMask = 0;
32 m_image = -1;
33 m_selectedImage = -1;
34 m_children = 0;
35 m_data = 0;
36};
37
38//-----------------------------------------------------------------------------
39// wxTreeEvent
40//-----------------------------------------------------------------------------
41
42IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent,wxCommandEvent)
43
7798a18e 44wxTreeEvent::wxTreeEvent( wxEventType commandType, int id ) :
c801d85f
KB
45 wxCommandEvent( commandType, id )
46{
47 m_code = 0;
48 m_oldItem = 0;
49};
50
51//-----------------------------------------------------------------------------
52// wxGenericTreeItem
53//-----------------------------------------------------------------------------
54
55IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeItem,wxObject)
56
57wxGenericTreeItem::wxGenericTreeItem( wxGenericTreeItem *parent )
58{
59 Reset();
60 m_parent = parent;
61 m_hasHilight = FALSE;
62};
63
64wxGenericTreeItem::wxGenericTreeItem( wxGenericTreeItem *parent, const wxTreeItem& item, wxDC *dc )
65{
66 Reset();
67 SetItem( item, dc );
68 m_parent = parent;
69 m_hasHilight = FALSE;
70};
71
72void wxGenericTreeItem::SetItem( const wxTreeItem &item, wxDC *dc )
73{
29d87bba 74 if ((item.m_mask & wxTREE_MASK_HANDLE) == wxTREE_MASK_HANDLE)
c801d85f
KB
75 m_itemId = item.m_itemId;
76 if ((item.m_mask & wxTREE_MASK_STATE) == wxTREE_MASK_STATE)
77 m_state = item.m_state;
78 if ((item.m_mask & wxTREE_MASK_TEXT) == wxTREE_MASK_TEXT)
79 m_text = item.m_text;
80 if ((item.m_mask & wxTREE_MASK_IMAGE) == wxTREE_MASK_IMAGE)
81 m_image = item.m_image;
82 if ((item.m_mask & wxTREE_MASK_SELECTED_IMAGE) == wxTREE_MASK_SELECTED_IMAGE)
83 m_selectedImage = item.m_selectedImage;
84 if ((item.m_mask & wxTREE_MASK_CHILDREN) == wxTREE_MASK_CHILDREN)
85 m_hasChildren = (item.m_children > 0);
86 if ((item.m_mask & wxTREE_MASK_DATA) == wxTREE_MASK_DATA)
87 m_data = item.m_data;
88 long lw = 0;
89 long lh = 0;
90 dc->GetTextExtent( m_text, &lw, &lh );
91 m_width = lw;
92 m_height = lh;
93};
94
95void wxGenericTreeItem::SetText( const wxString &text, wxDC *dc )
96{
97 m_text = text;
98 long lw = 0;
99 long lh = 0;
100 dc->GetTextExtent( m_text, &lw, &lh );
101 m_width = lw;
102 m_height = lh;
103};
104
74bedbeb 105void wxGenericTreeItem::Reset()
c801d85f
KB
106{
107 m_itemId = -1;
108 m_state = 0;
109 m_text = "";
110 m_image = -1;
111 m_selectedImage = -1;
112// m_children = 0;
113 m_hasChildren = FALSE;
114 m_data = 0;
115 m_x = 0;
116 m_y = 0;
117 m_height = 0;
118 m_width = 0;
119 m_xCross = 0;
120 m_yCross = 0;
121 m_level = 0;
122 m_children.DeleteContents( TRUE );
74bedbeb 123 m_isCollapsed = TRUE;
c801d85f
KB
124 m_parent = NULL;
125};
126
127void wxGenericTreeItem::GetItem( wxTreeItem &item ) const
128{
129 if ((item.m_mask & wxTREE_MASK_STATE) == wxTREE_MASK_STATE)
130 item.m_state = m_state;
131 if ((item.m_mask & wxTREE_MASK_TEXT) == wxTREE_MASK_TEXT)
132 item.m_text = m_text;
133 if ((item.m_mask & wxTREE_MASK_IMAGE) == wxTREE_MASK_IMAGE)
134 item.m_image = m_image;
135 if ((item.m_mask & wxTREE_MASK_SELECTED_IMAGE) == wxTREE_MASK_SELECTED_IMAGE)
136 item.m_selectedImage = m_selectedImage;
137 if ((item.m_mask & wxTREE_MASK_CHILDREN) == wxTREE_MASK_CHILDREN)
138 item.m_children = (int)m_hasChildren;
139 if ((item.m_mask & wxTREE_MASK_DATA) == wxTREE_MASK_DATA)
140 item.m_data = m_data;
141};
142
74bedbeb 143bool wxGenericTreeItem::HasChildren()
c801d85f
KB
144{
145 return m_hasChildren;
146};
147
74bedbeb 148bool wxGenericTreeItem::HasPlus()
c801d85f 149{
74bedbeb
VZ
150 if ( !HasChildren() )
151 return FALSE;
152
153 return !IsExpanded();
c801d85f
KB
154};
155
74bedbeb 156int wxGenericTreeItem::NumberOfVisibleDescendents()
c801d85f
KB
157{
158 int ret = m_children.Number();
159 wxNode *node = m_children.First();
160 while (node)
161 {
162 wxGenericTreeItem *item = (wxGenericTreeItem*)node->Data();
163 ret += item->NumberOfVisibleDescendents();
164 node = node->Next();
165 };
166 return ret;
167};
168
74bedbeb 169int wxGenericTreeItem::NumberOfVisibleChildren()
c801d85f 170{
74bedbeb 171 return m_isCollapsed ? 0 : m_children.Number();
c801d85f
KB
172};
173
174wxGenericTreeItem *wxGenericTreeItem::FindItem( long itemId ) const
175{
176 if (m_itemId == itemId) return (wxGenericTreeItem*)(this);
177 wxNode *node = m_children.First();
178 while (node)
179 {
180 wxGenericTreeItem *item = (wxGenericTreeItem*)node->Data();
181 wxGenericTreeItem *res = item->FindItem( itemId );
182 if (res) return (wxGenericTreeItem*)(res);
183 node = node->Next();
184 };
185 return NULL;
186};
187
188void wxGenericTreeItem::AddChild( wxGenericTreeItem *child )
189{
190 m_children.Append( child );
191};
192
193void wxGenericTreeItem::SetCross( int x, int y )
194{
195 m_xCross = x;
196 m_yCross = y;
197};
198
199void wxGenericTreeItem::GetSize( int &x, int &y )
200{
201 if (y < m_y + 10) y = m_y +10;
202 int width = m_x + m_width;
203 if (width > x) x = width;
204 wxNode *node = m_children.First();
205 while (node)
206 {
207 wxGenericTreeItem *item = (wxGenericTreeItem*)node->Data();
208 item->GetSize( x, y );
209 node = node->Next();
210 };
211};
212
213long wxGenericTreeItem::HitTest( const wxPoint& point, int &flags )
214{
4c681997 215 if ((point.y > m_y) && (point.y < m_y+m_height))
c801d85f
KB
216 {
217 if ((point.x > m_xCross-5) &&
218 (point.x < m_xCross+5) &&
219 (point.y > m_yCross-5) &&
220 (point.y < m_yCross+5) &&
29d87bba 221 (m_hasChildren))
c801d85f
KB
222 {
223 flags = wxTREE_HITTEST_ONITEMBUTTON;
224 return m_itemId;
225 };
226 if ((point.x > m_x) && (point.x < m_x+m_width))
227 {
228 flags = wxTREE_HITTEST_ONITEMLABEL;
229 return m_itemId;
230 };
231 if (point.x > m_x)
232 {
233 flags = wxTREE_HITTEST_ONITEMRIGHT;
234 return m_itemId;
235 };
236 flags = wxTREE_HITTEST_ONITEMINDENT;
237 return m_itemId;
238 }
239 else
240 {
e2414cbe 241 if (!m_isCollapsed)
c801d85f 242 {
e2414cbe
RR
243 wxNode *node = m_children.First();
244 while (node)
245 {
246 wxGenericTreeItem *child = (wxGenericTreeItem*)node->Data();
247 long res = child->HitTest( point, flags );
248 if (res != -1) return res;
249 node = node->Next();
250 };
c801d85f
KB
251 };
252 };
253 return -1;
254};
255
256void wxGenericTreeItem::PrepareEvent( wxTreeEvent &event )
257{
258 event.m_item.m_itemId = m_itemId;
259 event.m_item.m_state = m_state;
260 event.m_item.m_text = m_text;
261 event.m_item.m_image = m_image;
262 event.m_item.m_selectedImage = m_selectedImage;
263 event.m_item.m_children = (int)m_hasChildren;
264 event.m_item.m_data = m_data;
265 event.m_oldItem = 0;
266 event.m_code = 0;
267 event.m_pointDrag.x = 0;
268 event.m_pointDrag.y = 0;
269};
270
271void wxGenericTreeItem::SendKeyDown( wxWindow *target )
272{
273 wxTreeEvent event( wxEVT_COMMAND_TREE_KEY_DOWN, target->GetId() );
274 PrepareEvent( event );
275 event.SetEventObject( target );
276 target->ProcessEvent( event );
277};
278
279void wxGenericTreeItem::SendSelected( wxWindow *target )
280{
281 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGED, target->GetId() );
282 PrepareEvent( event );
283 event.SetEventObject( target );
284 target->ProcessEvent( event );
285};
286
287void wxGenericTreeItem::SendDelete( wxWindow *target )
288{
74bedbeb 289 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, target->GetId() );
c801d85f
KB
290 PrepareEvent( event );
291 event.SetEventObject( target );
292 target->ProcessEvent( event );
293};
294
295void wxGenericTreeItem::SendExpand( wxWindow *target )
296{
74bedbeb
VZ
297 m_isCollapsed = FALSE;
298
299 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, target->GetId() );
74bedbeb
VZ
300 event.SetEventObject( target );
301 PrepareEvent( event );
302 target->ProcessEvent( event );
303
304 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
c801d85f 305 PrepareEvent( event );
74bedbeb
VZ
306 target->ProcessEvent( event );
307};
308
309void wxGenericTreeItem::SendCollapse( wxWindow *target )
310{
311 m_isCollapsed = TRUE;
312
4c681997 313 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, target->GetId() );
c801d85f 314 event.SetEventObject( target );
74bedbeb
VZ
315 PrepareEvent( event );
316 target->ProcessEvent( event );
317
4c681997 318 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
74bedbeb 319 PrepareEvent( event );
c801d85f
KB
320 target->ProcessEvent( event );
321};
322
323void wxGenericTreeItem::SetHilight( bool set )
324{
325 m_hasHilight = set;
326};
327
74bedbeb 328bool wxGenericTreeItem::HasHilight()
c801d85f
KB
329{
330 return m_hasHilight;
331};
332
333//-----------------------------------------------------------------------------
334// wxTreeCtrl
335//-----------------------------------------------------------------------------
336
4c681997 337IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl,wxScrolledWindow)
c801d85f
KB
338
339BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow)
340 EVT_PAINT (wxTreeCtrl::OnPaint)
341 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse)
342 EVT_CHAR (wxTreeCtrl::OnChar)
343 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus)
344 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus)
345END_EVENT_TABLE()
346
74bedbeb 347wxTreeCtrl::wxTreeCtrl()
c801d85f
KB
348{
349 m_current = NULL;
350 m_anchor = NULL;
351 m_hasFocus = FALSE;
352 m_xScroll = 0;
353 m_yScroll = 0;
354 m_lastId = 0;
355 m_lineHeight = 10;
356 m_indent = 15;
357 m_isCreated = FALSE;
c801d85f 358 m_hilightBrush = new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), wxSOLID );
e2414cbe
RR
359 m_imageList = NULL;
360 m_smallImageList = NULL;
c801d85f
KB
361};
362
debe6624 363wxTreeCtrl::wxTreeCtrl(wxWindow *parent, wxWindowID id,
4c681997 364 const wxPoint& pos, const wxSize& size,
74bedbeb 365 long style, const wxString& name )
c801d85f
KB
366{
367 m_current = NULL;
368 m_anchor = NULL;
369 m_hasFocus = FALSE;
370 m_xScroll = 0;
371 m_yScroll = 0;
372 m_lastId = 0;
373 m_lineHeight = 10;
374 m_indent = 15;
375 m_isCreated = FALSE;
c801d85f 376 m_hilightBrush = new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), wxSOLID );
e2414cbe
RR
377 m_imageList = NULL;
378 m_smallImageList = NULL;
c801d85f
KB
379 Create( parent, id, pos, size, style, name );
380};
381
74bedbeb 382wxTreeCtrl::~wxTreeCtrl()
c801d85f 383{
c801d85f
KB
384};
385
debe6624 386bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
4c681997
RR
387 const wxPoint& pos, const wxSize& size,
388 long style, const wxString& name )
c801d85f
KB
389{
390 wxScrolledWindow::Create( parent, id, pos, size, style, name );
391 SetBackgroundColour( *wxWHITE );
392 m_dottedPen = wxPen( *wxBLACK, 0, 0 );
393 return TRUE;
394};
395
74bedbeb 396int wxTreeCtrl::GetCount() const
c801d85f
KB
397{
398 if (!m_anchor) return 0;
399 return m_anchor->NumberOfVisibleDescendents();
400};
401
74bedbeb
VZ
402long wxTreeCtrl::InsertItem( long parent, const wxString& label, int image,
403 int selImage, long WXUNUSED(insertAfter) )
c801d85f
KB
404{
405 wxGenericTreeItem *p = NULL;
406 if (parent == 0)
407 {
408 if (m_anchor) return -1;
409 }
410 else
411 {
412 p = FindItem( parent );
413 if (!p) return -1;
414 };
415 wxTreeItem item;
416 m_lastId++;
417 item.m_mask = wxTREE_MASK_HANDLE;
418 item.m_itemId = m_lastId;
419 if (!label.IsNull() || (label == ""))
420 {
421 item.m_text = label;
422 item.m_mask |= wxTREE_MASK_TEXT;
423 };
424 if (image >= 0)
425 {
426 item.m_image = image;
427 item.m_mask |= wxTREE_MASK_IMAGE;
428 };
429 if (selImage >= 0)
430 {
431 item.m_selectedImage = selImage;
432 item.m_mask |= wxTREE_MASK_SELECTED_IMAGE;
433 };
29d87bba 434
c801d85f
KB
435 wxClientDC dc(this);
436 wxGenericTreeItem *new_child = new wxGenericTreeItem( p, item, &dc );
29d87bba 437 if (p)
c801d85f
KB
438 p->AddChild( new_child );
439 else
440 m_anchor = new_child;
29d87bba 441
c801d85f
KB
442 if (p)
443 {
444 CalculatePositions();
29d87bba 445
e2414cbe 446 if (!p->HasChildren()) p->m_hasChildren = TRUE;
389cdc7a 447
c801d85f
KB
448 int ch = 0;
449 GetClientSize( NULL, &ch );
29d87bba 450
d4c99d6f
RR
451 PrepareDC( dc );
452
c801d85f 453 wxRectangle rect;
d4c99d6f
RR
454 rect.x = dc.LogicalToDeviceX( 0 );
455 rect.y = 0;
456 rect.width = 10000;
457 rect.height = ch;
29d87bba 458
c801d85f
KB
459 if (p->m_children.Number() == 1)
460 {
461 rect.y = dc.LogicalToDeviceY( p->m_y );
462 }
463 else
464 {
465 wxNode *node = p->m_children.Member( new_child )->Previous();
466 wxGenericTreeItem* last_child = (wxGenericTreeItem*)node->Data();
467 rect.y = dc.LogicalToDeviceY( last_child->m_y );
468 };
29d87bba 469
c801d85f 470 AdjustMyScrollbars();
29d87bba 471
c801d85f
KB
472 if (rect.height > 0) Refresh( FALSE, &rect);
473 }
474 else
475 {
476 AdjustMyScrollbars();
29d87bba 477
c801d85f
KB
478 Refresh();
479 };
29d87bba 480
c801d85f
KB
481 return m_lastId;
482};
483
74bedbeb 484long wxTreeCtrl::InsertItem( long parent, wxTreeItem &info, long WXUNUSED(insertAfter) )
c801d85f
KB
485{
486 int oldMask = info.m_mask;
487 wxGenericTreeItem *p = NULL;
488 if (parent == 0)
489 {
490 if (m_anchor) return -1;
491 }
492 else
493 {
494 p = FindItem( parent );
495 if (!p)
496 {
497 printf( "TreeItem not found.\n" );
498 return -1;
499 };
500 };
501 long ret = 0;
502 if ((info.m_mask & wxTREE_MASK_HANDLE) == 0)
503 {
504 m_lastId++;
505 info.m_itemId = m_lastId;
506 info.m_mask |= wxTREE_MASK_HANDLE;
507 ret = m_lastId;
508 }
509 else
510 {
511 ret = info.m_itemId;
512 };
29d87bba 513
c801d85f
KB
514 wxClientDC dc(this);
515 wxGenericTreeItem *new_child = new wxGenericTreeItem( p, info, &dc );
29d87bba 516 if (p)
c801d85f
KB
517 p->AddChild( new_child );
518 else
519 m_anchor = new_child;
29d87bba 520
c801d85f
KB
521 if (p)
522 {
523 CalculatePositions();
389cdc7a 524
e2414cbe 525 if (!p->HasChildren()) p->m_hasChildren = TRUE;
29d87bba 526
c801d85f
KB
527 int ch = 0;
528 GetClientSize( NULL, &ch );
29d87bba 529
d4c99d6f
RR
530 PrepareDC( dc );
531
c801d85f 532 wxRectangle rect;
d4c99d6f
RR
533 rect.x = dc.LogicalToDeviceX( 0 );
534 rect.y = 0;
535 rect.width = 10000;
536 rect.height = ch;
29d87bba 537
c801d85f
KB
538 if (p->m_children.Number() == 1)
539 {
540 rect.y = dc.LogicalToDeviceY( p->m_y );
541 }
542 else
543 {
544 wxNode *node = p->m_children.Member( new_child )->Previous();
545 wxGenericTreeItem* last_child = (wxGenericTreeItem*)node->Data();
546 rect.y = dc.LogicalToDeviceY( last_child->m_y );
547 };
29d87bba 548
c801d85f 549 AdjustMyScrollbars();
29d87bba 550
c801d85f
KB
551 if (rect.height > 0) Refresh( FALSE, &rect);
552 }
553 else
554 {
555 AdjustMyScrollbars();
29d87bba 556
c801d85f
KB
557 Refresh();
558 };
29d87bba 559
c801d85f
KB
560 info.m_mask = oldMask;
561 return ret;
562};
563
74bedbeb 564bool wxTreeCtrl::ExpandItem( long item, int action )
c801d85f
KB
565{
566 wxGenericTreeItem *i = FindItem( item );
74bedbeb
VZ
567 if (!i)
568 return FALSE;
569
c801d85f
KB
570 switch (action)
571 {
572 case wxTREE_EXPAND_EXPAND:
573 {
574 i->SendExpand( this );
575 break;
74bedbeb
VZ
576 }
577
c801d85f
KB
578 case wxTREE_EXPAND_COLLAPSE_RESET:
579 case wxTREE_EXPAND_COLLAPSE:
580 {
581 wxNode *node = i->m_children.First();
582 while (node)
583 {
584 wxGenericTreeItem *child = (wxGenericTreeItem*)node->Data();
74bedbeb
VZ
585 if ( child->IsExpanded() )
586 ExpandItem( child->m_itemId, wxTREE_EXPAND_COLLAPSE );
587 node = node->Next();
c801d85f 588 };
29d87bba 589
e2414cbe 590 CalculatePositions();
389cdc7a 591
74bedbeb 592 i->SendCollapse( this );
c801d85f 593 break;
74bedbeb
VZ
594 }
595
c801d85f
KB
596 case wxTREE_EXPAND_TOGGLE:
597 {
74bedbeb 598 if ( i->IsExpanded() )
c801d85f 599 ExpandItem( item, wxTREE_EXPAND_COLLAPSE );
74bedbeb
VZ
600 else
601 ExpandItem( item, wxTREE_EXPAND_EXPAND );
602 return TRUE;
603 }
c801d85f 604 };
74bedbeb 605
d4c99d6f
RR
606 wxClientDC dc(this);
607 PrepareDC(dc);
608
74bedbeb
VZ
609 int cw = 0;
610 int ch = 0;
611 GetClientSize( &cw, &ch );
d4c99d6f 612
74bedbeb 613 wxRect rect;
d4c99d6f 614 rect.x = dc.LogicalToDeviceX( 0 );
74bedbeb 615 rect.width = cw;
74bedbeb 616 rect.y = dc.LogicalToDeviceY( i->m_y );
d4c99d6f 617 rect.height = ch;
74bedbeb
VZ
618 Refresh( TRUE, &rect );
619
620 AdjustMyScrollbars();
621
c801d85f
KB
622 return TRUE;
623};
624
a32dd690
VZ
625void wxTreeCtrl::DeleteItem( long item )
626{
627 wxGenericTreeItem *pItem = FindItem( item );
1a5a8367 628 wxCHECK_RET( pItem != NULL, _("wxTreeCtrl::DeleteItem: no such pItem.") );
389cdc7a 629
a32dd690 630 pItem->m_parent->m_children.DeleteObject(pItem);
389cdc7a 631
4c681997
RR
632 Refresh();
633}
634
635void wxTreeCtrl::DeleteChildren( long item )
636{
637 wxGenericTreeItem *pItem = FindItem( item );
1a5a8367 638 wxCHECK_RET( pItem != NULL, _("wxTreeCtrl::DeleteChildren: no such pItem.") );
389cdc7a 639
4c681997 640 pItem->m_children.Clear();
389cdc7a 641
a32dd690
VZ
642 Refresh();
643}
644
74bedbeb 645bool wxTreeCtrl::DeleteAllItems()
c801d85f
KB
646{
647 delete m_anchor;
648 m_anchor = NULL;
649 Refresh();
650 return TRUE;
651};
652
653bool wxTreeCtrl::GetItem( wxTreeItem &info ) const
654{
655 wxGenericTreeItem *i = FindItem( info.m_itemId );
656 if (!i) return FALSE;
657 i->GetItem( info );
658 return TRUE;
659};
660
74bedbeb 661long wxTreeCtrl::GetItemData( long item ) const
c801d85f
KB
662{
663 wxGenericTreeItem *i = FindItem( item );
664 if (!i) return 0;
665 return i->m_data;
666};
667
74bedbeb 668wxString wxTreeCtrl::GetItemText( long item ) const
c801d85f
KB
669{
670 wxGenericTreeItem *i = FindItem( item );
671 if (!i) return "";
672 return i->m_text;
673};
674
74bedbeb
VZ
675int wxTreeCtrl::GetItemImage(long item) const
676{
677 wxGenericTreeItem *i = FindItem( item );
678 return i == 0 ? -1 : i->GetImage();
679}
680
681long wxTreeCtrl::GetParent( long item ) const
c801d85f
KB
682{
683 wxGenericTreeItem *i = FindItem( item );
684 if (!i) return -1;
685 i = i->m_parent;
686 if (!i) return -1;
687 return i->m_parent->m_itemId;
688};
689
74bedbeb 690long wxTreeCtrl::GetRootItem() const
c801d85f
KB
691{
692 if (m_anchor) return m_anchor->m_itemId;
693 return -1;
694};
695
74bedbeb 696long wxTreeCtrl::GetSelection() const
c801d85f 697{
389cdc7a 698 return m_current ? m_current->GetItemId() : -1;
c801d85f
KB
699};
700
389cdc7a 701bool wxTreeCtrl::SelectItem(long itemId)
c801d85f 702{
389cdc7a
VZ
703 wxGenericTreeItem *pItem = FindItem(itemId);
704 if ( !pItem ) {
1a5a8367 705 wxLogDebug(_("Can't select an item %d which doesn't exist."), itemId);
389cdc7a
VZ
706
707 return FALSE;
708 }
709
d37a02d0 710 SelectItem(pItem);
389cdc7a
VZ
711
712 return TRUE;
c801d85f
KB
713};
714
c614ed58 715void wxTreeCtrl::SelectItem(wxGenericTreeItem *item)
389cdc7a
VZ
716{
717 if (m_current != item)
718 {
719 if (m_current)
720 {
721 m_current->SetHilight( FALSE );
722 RefreshLine( m_current );
723 };
724 m_current = item;
725 m_current->SetHilight( TRUE );
726 RefreshLine( m_current );
727
c614ed58 728 m_current->SendSelected( this );
389cdc7a
VZ
729 }
730}
731
74bedbeb 732bool wxTreeCtrl::ItemHasChildren( long item ) const
c801d85f
KB
733{
734 wxGenericTreeItem *i = FindItem( item );
735 if (!i) return FALSE;
736 return i->m_hasChildren;
737};
738
74bedbeb 739void wxTreeCtrl::SetIndent( int indent )
c801d85f
KB
740{
741 m_indent = indent;
742 Refresh();
743};
744
74bedbeb 745int wxTreeCtrl::GetIndent() const
c801d85f
KB
746{
747 return m_indent;
748};
749
750bool wxTreeCtrl::SetItem( wxTreeItem &info )
751{
752 wxGenericTreeItem *i = FindItem( info.m_itemId );
753 if (!i) return FALSE;
754 wxClientDC dc(this);
755 i->SetItem( info, &dc );
74bedbeb 756 Refresh();
c801d85f
KB
757 return TRUE;
758};
759
74bedbeb 760bool wxTreeCtrl::SetItemData( long item, long data )
c801d85f
KB
761{
762 wxGenericTreeItem *i = FindItem( item );
763 if (!i) return FALSE;
764 i->m_data = data;
765 return TRUE;
766};
767
74bedbeb 768bool wxTreeCtrl::SetItemText( long item, const wxString &text )
c801d85f
KB
769{
770 wxGenericTreeItem *i = FindItem( item );
771 if (!i) return FALSE;
772 wxClientDC dc(this);
773 i->SetText( text, &dc );
774 return TRUE;
775};
776
74bedbeb
VZ
777void wxTreeCtrl::SetItemImage(long item, int image, int imageSel) const
778{
779 wxGenericTreeItem *i = FindItem( item );
780 if ( i != 0 ) {
781 i->SetImage(image);
782 i->SetSelectedImage(imageSel);
783 }
784}
785
c801d85f
KB
786long wxTreeCtrl::HitTest( const wxPoint& point, int &flags )
787{
788 flags = 0;
789 if (!m_anchor) return -1;
790 return m_anchor->HitTest( point, flags );
791};
792
e2414cbe
RR
793wxImageList *wxTreeCtrl::GetImageList( int which ) const
794{
795 if (which == wxIMAGE_LIST_NORMAL) return m_imageList;
796 return m_smallImageList;
797};
798
799void wxTreeCtrl::SetImageList( wxImageList *imageList, int which )
800{
801 if (which == wxIMAGE_LIST_NORMAL)
802 {
803 if (m_imageList) delete m_imageList;
804 m_imageList = imageList;
805 }
806 else
807 {
808 if (m_smallImageList) delete m_smallImageList;
809 m_smallImageList = imageList;
810 };
811};
812
74bedbeb 813void wxTreeCtrl::AdjustMyScrollbars()
c801d85f
KB
814{
815 if (m_anchor)
816 {
817 int x = 0;
818 int y = 0;
819 m_anchor->GetSize( x, y );
820 y += 2*m_lineHeight;
821 int x_pos = GetScrollPos( wxHORIZONTAL );
822 int y_pos = GetScrollPos( wxVERTICAL );
823 SetScrollbars( 10, 10, x/10, y/10, x_pos, y_pos );
824 }
825 else
826 {
827 SetScrollbars( 0, 0, 0, 0 );
828 };
829};
830
831void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxPaintDC &dc, int level, int &y )
832{
4c681997 833 int horizX = level*m_indent;
389cdc7a 834
4c681997
RR
835 item->m_x = horizX+33;
836 item->m_y = y-m_lineHeight/3;
837 item->m_height = m_lineHeight;
389cdc7a 838
4c681997
RR
839 item->SetCross( horizX+15, y );
840
c801d85f 841 int oldY = y;
389cdc7a 842
d4c99d6f
RR
843 int exposed_x = dc.LogicalToDeviceX( 0 );
844 int exposed_y = dc.LogicalToDeviceY( item->m_y-2 );
845
846 if (IsExposed( exposed_x, exposed_y, 1000, m_lineHeight+4 ))
c801d85f 847 {
4c681997
RR
848 int startX = horizX;
849 int endX = horizX + 10;
29d87bba 850
4c681997 851 if (!item->HasChildren()) endX += 20;
d4c99d6f 852
389cdc7a 853 dc.DrawLine( startX, y, endX, y );
29d87bba 854
4c681997 855 if (item->HasChildren())
c801d85f
KB
856 {
857 dc.DrawLine( horizX+20, y, horizX+30, y );
858 dc.SetPen( *wxGREY_PEN );
859 dc.DrawRectangle( horizX+10, y-4, 11, 9 );
860 dc.SetPen( *wxBLACK_PEN );
4c681997
RR
861 dc.DrawLine( horizX+13, y, horizX+18, y );
862 if (item->HasPlus())
863 dc.DrawLine( horizX+15, y-2, horizX+15, y+3 );
c801d85f 864 };
c801d85f 865
4c681997 866 if (item->HasHilight())
c801d85f 867 {
29d87bba 868 dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
29d87bba 869 dc.SetBrush( *m_hilightBrush );
29d87bba 870 long tw, th;
4c681997 871 dc.GetTextExtent( item->m_text, &tw, &th );
e2414cbe 872 if (m_hasFocus)
389cdc7a 873 {
e2414cbe
RR
874 dc.SetPen( *wxBLACK_PEN );
875 dc.DrawRectangle( item->m_x-2, item->m_y-2, tw+4, th+4 );
389cdc7a
VZ
876 }
877 else
878 {
879 dc.SetPen( *wxTRANSPARENT_PEN );
e2414cbe 880 dc.DrawRectangle( item->m_x-2, item->m_y-2, tw+4, th+4 );
389cdc7a 881 }
4c681997 882 dc.DrawText( item->m_text, item->m_x, item->m_y );
29d87bba 883
29d87bba 884 dc.SetPen( *wxBLACK_PEN );
29d87bba 885 dc.SetTextForeground( *wxBLACK );
389cdc7a 886 dc.SetBrush( *wxWHITE_BRUSH );
29d87bba
VZ
887 }
888 else
4c681997 889 {
d4c99d6f 890 dc.SetBrush( *wxWHITE_BRUSH );
389cdc7a 891 dc.SetPen( *wxTRANSPARENT_PEN );
4c681997
RR
892 long tw, th;
893 dc.GetTextExtent( item->m_text, &tw, &th );
894 dc.DrawRectangle( item->m_x-2, item->m_y-2, tw+4, th+4 );
895 dc.DrawText( item->m_text, item->m_x, item->m_y );
896 dc.SetPen( *wxBLACK_PEN );
897 };
898 };
899
900 if (item->NumberOfVisibleChildren() == 0) return;
e2414cbe 901
389cdc7a
VZ
902 int semiOldY = y;
903
4c681997
RR
904 wxNode *node = item->m_children.First();
905 while (node)
906 {
907 wxGenericTreeItem *child = (wxGenericTreeItem *)node->Data();
29d87bba 908
c801d85f 909 y += m_lineHeight;
e2414cbe 910 semiOldY = y;
389cdc7a 911
4c681997 912 PaintLevel( child, dc, level+1, y );
389cdc7a 913
c801d85f
KB
914 node = node->Next();
915 };
4c681997 916
e2414cbe 917 dc.DrawLine( horizX+15, oldY+5, horizX+15, semiOldY );
4c681997 918}
c801d85f
KB
919
920void wxTreeCtrl::OnPaint( const wxPaintEvent &WXUNUSED(event) )
921{
922 if (!m_anchor) return;
923
d4c99d6f
RR
924 wxPaintDC dc(this);
925 PrepareDC( dc );
29d87bba 926
d4c99d6f 927 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
29d87bba 928
d4c99d6f
RR
929 dc.SetPen( m_dottedPen );
930 m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 931
c801d85f 932 int y = m_lineHeight / 2 + 2;
d4c99d6f 933 PaintLevel( m_anchor, dc, 0, y );
c801d85f
KB
934};
935
936void wxTreeCtrl::OnSetFocus( const wxFocusEvent &WXUNUSED(event) )
937{
938 m_hasFocus = TRUE;
939 if (m_current) RefreshLine( m_current );
940};
941
942void wxTreeCtrl::OnKillFocus( const wxFocusEvent &WXUNUSED(event) )
943{
944 m_hasFocus = FALSE;
945 if (m_current) RefreshLine( m_current );
946};
947
948void wxTreeCtrl::OnChar( wxKeyEvent &event )
949{
950 event.Skip();
951};
952
953void wxTreeCtrl::OnMouse( const wxMouseEvent &event )
954{
955 if (!event.LeftDown() &&
956 !event.LeftDClick()) return;
29d87bba 957
c801d85f
KB
958 wxClientDC dc(this);
959 PrepareDC(dc);
960 long x = dc.DeviceToLogicalX( (long)event.GetX() );
961 long y = dc.DeviceToLogicalY( (long)event.GetY() );
29d87bba 962
c801d85f
KB
963 int flag = 0;
964 long id = HitTest( wxPoint(x,y), flag );
29d87bba
VZ
965 if (id == -1)
966 return;
c801d85f 967 wxGenericTreeItem *item = FindItem( id );
29d87bba 968
c801d85f
KB
969 if (!item) return;
970 if ((flag != wxTREE_HITTEST_ONITEMBUTTON) &&
971 (flag != wxTREE_HITTEST_ONITEMLABEL)) return;
29d87bba 972
389cdc7a 973 SelectItem(item);
29d87bba
VZ
974
975 if (event.LeftDClick())
976 m_current->SendKeyDown( this );
977
c801d85f
KB
978 if (flag == wxTREE_HITTEST_ONITEMBUTTON)
979 {
980 ExpandItem( item->m_itemId, wxTREE_EXPAND_TOGGLE );
981 return;
982 };
983};
984
985void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxPaintDC &dc, int level, int &y )
986{
4c681997 987 int horizX = level*m_indent;
389cdc7a 988
4c681997
RR
989 item->m_x = horizX+33;
990 item->m_y = y-m_lineHeight/3-2;
991 item->m_height = m_lineHeight;
992
993 if (item->NumberOfVisibleChildren() == 0) return;
389cdc7a 994
c801d85f
KB
995 wxNode *node = item->m_children.First();
996 while (node)
997 {
998 wxGenericTreeItem *child = (wxGenericTreeItem *)node->Data();
389cdc7a 999
c801d85f 1000 y += m_lineHeight;
4c681997 1001 CalculateLevel( child, dc, level+1, y );
29d87bba 1002
c801d85f
KB
1003 node = node->Next();
1004 };
1005};
1006
74bedbeb 1007void wxTreeCtrl::CalculatePositions()
c801d85f 1008{
29d87bba
VZ
1009 if (!m_anchor)
1010 return;
1011
c801d85f
KB
1012 wxClientDC dc(this);
1013 PrepareDC( dc );
29d87bba 1014
c801d85f 1015 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
29d87bba 1016
c801d85f
KB
1017 dc.SetPen( m_dottedPen );
1018 m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 1019
c801d85f
KB
1020 int y = m_lineHeight / 2 + 2;
1021 CalculateLevel( m_anchor, dc, 0, y );
1022};
1023
1024wxGenericTreeItem *wxTreeCtrl::FindItem( long itemId ) const
1025{
1026 if (!m_anchor) return NULL;
1027 return m_anchor->FindItem( itemId );
1028};
1029
1030void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
1031{
1032 if (!item) return;
1033 wxClientDC dc(this);
1034 PrepareDC( dc );
1035 wxRect rect;
1036 rect.x = dc.LogicalToDeviceX( item->m_x-2 );
1037 rect.y = dc.LogicalToDeviceY( item->m_y-2 );
1038 rect.width = 1000;
d4c99d6f 1039 rect.height = dc.GetCharHeight()+6;
c801d85f
KB
1040 Refresh( TRUE, &rect );
1041};
1042
1043
1044