]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectrl.cpp
added HasEntry/Group function to wxConfig, corrected entry/group deletion
[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
6// Id:
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"
17
18//-----------------------------------------------------------------------------
19// wxTreeItem
20//-----------------------------------------------------------------------------
21
22IMPLEMENT_DYNAMIC_CLASS(wxTreeItem, wxObject)
23
74bedbeb 24wxTreeItem::wxTreeItem()
c801d85f
KB
25{
26 m_mask = 0;
27 m_itemId = 0;
28 m_state = 0;
29 m_stateMask = 0;
30 m_image = -1;
31 m_selectedImage = -1;
32 m_children = 0;
33 m_data = 0;
34};
35
36//-----------------------------------------------------------------------------
37// wxTreeEvent
38//-----------------------------------------------------------------------------
39
40IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent,wxCommandEvent)
41
7798a18e 42wxTreeEvent::wxTreeEvent( wxEventType commandType, int id ) :
c801d85f
KB
43 wxCommandEvent( commandType, id )
44{
45 m_code = 0;
46 m_oldItem = 0;
47};
48
49//-----------------------------------------------------------------------------
50// wxGenericTreeItem
51//-----------------------------------------------------------------------------
52
53IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeItem,wxObject)
54
55wxGenericTreeItem::wxGenericTreeItem( wxGenericTreeItem *parent )
56{
57 Reset();
58 m_parent = parent;
59 m_hasHilight = FALSE;
60};
61
62wxGenericTreeItem::wxGenericTreeItem( wxGenericTreeItem *parent, const wxTreeItem& item, wxDC *dc )
63{
64 Reset();
65 SetItem( item, dc );
66 m_parent = parent;
67 m_hasHilight = FALSE;
68};
69
70void wxGenericTreeItem::SetItem( const wxTreeItem &item, wxDC *dc )
71{
29d87bba 72 if ((item.m_mask & wxTREE_MASK_HANDLE) == wxTREE_MASK_HANDLE)
c801d85f
KB
73 m_itemId = item.m_itemId;
74 if ((item.m_mask & wxTREE_MASK_STATE) == wxTREE_MASK_STATE)
75 m_state = item.m_state;
76 if ((item.m_mask & wxTREE_MASK_TEXT) == wxTREE_MASK_TEXT)
77 m_text = item.m_text;
78 if ((item.m_mask & wxTREE_MASK_IMAGE) == wxTREE_MASK_IMAGE)
79 m_image = item.m_image;
80 if ((item.m_mask & wxTREE_MASK_SELECTED_IMAGE) == wxTREE_MASK_SELECTED_IMAGE)
81 m_selectedImage = item.m_selectedImage;
82 if ((item.m_mask & wxTREE_MASK_CHILDREN) == wxTREE_MASK_CHILDREN)
83 m_hasChildren = (item.m_children > 0);
84 if ((item.m_mask & wxTREE_MASK_DATA) == wxTREE_MASK_DATA)
85 m_data = item.m_data;
86 long lw = 0;
87 long lh = 0;
88 dc->GetTextExtent( m_text, &lw, &lh );
89 m_width = lw;
90 m_height = lh;
91};
92
93void wxGenericTreeItem::SetText( const wxString &text, wxDC *dc )
94{
95 m_text = text;
96 long lw = 0;
97 long lh = 0;
98 dc->GetTextExtent( m_text, &lw, &lh );
99 m_width = lw;
100 m_height = lh;
101};
102
74bedbeb 103void wxGenericTreeItem::Reset()
c801d85f
KB
104{
105 m_itemId = -1;
106 m_state = 0;
107 m_text = "";
108 m_image = -1;
109 m_selectedImage = -1;
110// m_children = 0;
111 m_hasChildren = FALSE;
112 m_data = 0;
113 m_x = 0;
114 m_y = 0;
115 m_height = 0;
116 m_width = 0;
117 m_xCross = 0;
118 m_yCross = 0;
119 m_level = 0;
120 m_children.DeleteContents( TRUE );
74bedbeb 121 m_isCollapsed = TRUE;
c801d85f
KB
122 m_parent = NULL;
123};
124
125void wxGenericTreeItem::GetItem( wxTreeItem &item ) const
126{
127 if ((item.m_mask & wxTREE_MASK_STATE) == wxTREE_MASK_STATE)
128 item.m_state = m_state;
129 if ((item.m_mask & wxTREE_MASK_TEXT) == wxTREE_MASK_TEXT)
130 item.m_text = m_text;
131 if ((item.m_mask & wxTREE_MASK_IMAGE) == wxTREE_MASK_IMAGE)
132 item.m_image = m_image;
133 if ((item.m_mask & wxTREE_MASK_SELECTED_IMAGE) == wxTREE_MASK_SELECTED_IMAGE)
134 item.m_selectedImage = m_selectedImage;
135 if ((item.m_mask & wxTREE_MASK_CHILDREN) == wxTREE_MASK_CHILDREN)
136 item.m_children = (int)m_hasChildren;
137 if ((item.m_mask & wxTREE_MASK_DATA) == wxTREE_MASK_DATA)
138 item.m_data = m_data;
139};
140
74bedbeb 141bool wxGenericTreeItem::HasChildren()
c801d85f
KB
142{
143 return m_hasChildren;
144};
145
74bedbeb 146bool wxGenericTreeItem::HasPlus()
c801d85f 147{
74bedbeb
VZ
148 if ( !HasChildren() )
149 return FALSE;
150
151 return !IsExpanded();
c801d85f
KB
152};
153
74bedbeb 154int wxGenericTreeItem::NumberOfVisibleDescendents()
c801d85f
KB
155{
156 int ret = m_children.Number();
157 wxNode *node = m_children.First();
158 while (node)
159 {
160 wxGenericTreeItem *item = (wxGenericTreeItem*)node->Data();
161 ret += item->NumberOfVisibleDescendents();
162 node = node->Next();
163 };
164 return ret;
165};
166
74bedbeb 167int wxGenericTreeItem::NumberOfVisibleChildren()
c801d85f 168{
74bedbeb 169 return m_isCollapsed ? 0 : m_children.Number();
c801d85f
KB
170};
171
172wxGenericTreeItem *wxGenericTreeItem::FindItem( long itemId ) const
173{
174 if (m_itemId == itemId) return (wxGenericTreeItem*)(this);
175 wxNode *node = m_children.First();
176 while (node)
177 {
178 wxGenericTreeItem *item = (wxGenericTreeItem*)node->Data();
179 wxGenericTreeItem *res = item->FindItem( itemId );
180 if (res) return (wxGenericTreeItem*)(res);
181 node = node->Next();
182 };
183 return NULL;
184};
185
186void wxGenericTreeItem::AddChild( wxGenericTreeItem *child )
187{
188 m_children.Append( child );
189};
190
191void wxGenericTreeItem::SetCross( int x, int y )
192{
193 m_xCross = x;
194 m_yCross = y;
195};
196
197void wxGenericTreeItem::GetSize( int &x, int &y )
198{
199 if (y < m_y + 10) y = m_y +10;
200 int width = m_x + m_width;
201 if (width > x) x = width;
202 wxNode *node = m_children.First();
203 while (node)
204 {
205 wxGenericTreeItem *item = (wxGenericTreeItem*)node->Data();
206 item->GetSize( x, y );
207 node = node->Next();
208 };
209};
210
211long wxGenericTreeItem::HitTest( const wxPoint& point, int &flags )
212{
213 if (m_parent && ((point.y > m_y) && (point.y < m_y+m_height)))
214 {
215 if ((point.x > m_xCross-5) &&
216 (point.x < m_xCross+5) &&
217 (point.y > m_yCross-5) &&
218 (point.y < m_yCross+5) &&
29d87bba 219 (m_hasChildren))
c801d85f
KB
220 {
221 flags = wxTREE_HITTEST_ONITEMBUTTON;
222 return m_itemId;
223 };
224 if ((point.x > m_x) && (point.x < m_x+m_width))
225 {
226 flags = wxTREE_HITTEST_ONITEMLABEL;
227 return m_itemId;
228 };
229 if (point.x > m_x)
230 {
231 flags = wxTREE_HITTEST_ONITEMRIGHT;
232 return m_itemId;
233 };
234 flags = wxTREE_HITTEST_ONITEMINDENT;
235 return m_itemId;
236 }
237 else
238 {
239 wxNode *node = m_children.First();
240 while (node)
241 {
242 wxGenericTreeItem *child = (wxGenericTreeItem*)node->Data();
243 long res = child->HitTest( point, flags );
244 if (res != -1) return res;
245 node = node->Next();
246 };
247 };
248 return -1;
249};
250
251void wxGenericTreeItem::PrepareEvent( wxTreeEvent &event )
252{
253 event.m_item.m_itemId = m_itemId;
254 event.m_item.m_state = m_state;
255 event.m_item.m_text = m_text;
256 event.m_item.m_image = m_image;
257 event.m_item.m_selectedImage = m_selectedImage;
258 event.m_item.m_children = (int)m_hasChildren;
259 event.m_item.m_data = m_data;
260 event.m_oldItem = 0;
261 event.m_code = 0;
262 event.m_pointDrag.x = 0;
263 event.m_pointDrag.y = 0;
264};
265
266void wxGenericTreeItem::SendKeyDown( wxWindow *target )
267{
268 wxTreeEvent event( wxEVT_COMMAND_TREE_KEY_DOWN, target->GetId() );
269 PrepareEvent( event );
270 event.SetEventObject( target );
271 target->ProcessEvent( event );
272};
273
274void wxGenericTreeItem::SendSelected( wxWindow *target )
275{
276 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGED, target->GetId() );
277 PrepareEvent( event );
278 event.SetEventObject( target );
279 target->ProcessEvent( event );
280};
281
282void wxGenericTreeItem::SendDelete( wxWindow *target )
283{
74bedbeb 284 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, target->GetId() );
c801d85f
KB
285 PrepareEvent( event );
286 event.SetEventObject( target );
287 target->ProcessEvent( event );
288};
289
290void wxGenericTreeItem::SendExpand( wxWindow *target )
291{
74bedbeb
VZ
292 m_isCollapsed = FALSE;
293
294 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, target->GetId() );
295 event.SetCode(wxTREE_EXPAND_EXPAND);
296 event.SetEventObject( target );
297 PrepareEvent( event );
298 target->ProcessEvent( event );
299
300 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
c801d85f 301 PrepareEvent( event );
74bedbeb
VZ
302 target->ProcessEvent( event );
303};
304
305void wxGenericTreeItem::SendCollapse( wxWindow *target )
306{
307 m_isCollapsed = TRUE;
308
309 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, target->GetId() );
310 event.SetCode(wxTREE_EXPAND_COLLAPSE);
c801d85f 311 event.SetEventObject( target );
74bedbeb
VZ
312 PrepareEvent( event );
313 target->ProcessEvent( event );
314
315 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
316 PrepareEvent( event );
c801d85f
KB
317 target->ProcessEvent( event );
318};
319
320void wxGenericTreeItem::SetHilight( bool set )
321{
322 m_hasHilight = set;
323};
324
74bedbeb 325bool wxGenericTreeItem::HasHilight()
c801d85f
KB
326{
327 return m_hasHilight;
328};
329
330//-----------------------------------------------------------------------------
331// wxTreeCtrl
332//-----------------------------------------------------------------------------
333
334IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl,wxScrolledWindow
335)
336
337BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow)
338 EVT_PAINT (wxTreeCtrl::OnPaint)
339 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse)
340 EVT_CHAR (wxTreeCtrl::OnChar)
341 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus)
342 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus)
343END_EVENT_TABLE()
344
74bedbeb 345wxTreeCtrl::wxTreeCtrl()
c801d85f
KB
346{
347 m_current = NULL;
348 m_anchor = NULL;
349 m_hasFocus = FALSE;
350 m_xScroll = 0;
351 m_yScroll = 0;
352 m_lastId = 0;
353 m_lineHeight = 10;
354 m_indent = 15;
355 m_isCreated = FALSE;
356 m_dc = NULL;
357 m_hilightBrush = new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), wxSOLID );
358};
359
debe6624 360wxTreeCtrl::wxTreeCtrl(wxWindow *parent, wxWindowID id,
c801d85f
KB
361 const wxPoint& pos,
362 const wxSize& size,
74bedbeb 363 long style, const wxString& name )
c801d85f
KB
364{
365 m_current = NULL;
366 m_anchor = NULL;
367 m_hasFocus = FALSE;
368 m_xScroll = 0;
369 m_yScroll = 0;
370 m_lastId = 0;
371 m_lineHeight = 10;
372 m_indent = 15;
373 m_isCreated = FALSE;
374 m_dc = NULL;
375 m_hilightBrush = new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), wxSOLID );
376 Create( parent, id, pos, size, style, name );
377};
378
74bedbeb 379wxTreeCtrl::~wxTreeCtrl()
c801d85f
KB
380{
381 if (m_dc) delete m_dc;
382};
383
debe6624 384bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
c801d85f
KB
385 const wxPoint& pos,
386 const wxSize& size,
74bedbeb 387 long style
c801d85f
KB
388, const wxString& name )
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
c801d85f
KB
446 int ch = 0;
447 GetClientSize( NULL, &ch );
29d87bba 448
c801d85f
KB
449 wxRectangle rect;
450 rect.x = 0; rect.y = 0;
451 rect.width = 10000; rect.height = ch;
29d87bba 452
c801d85f
KB
453 PrepareDC( dc );
454 if (p->m_children.Number() == 1)
455 {
456 rect.y = dc.LogicalToDeviceY( p->m_y );
457 }
458 else
459 {
460 wxNode *node = p->m_children.Member( new_child )->Previous();
461 wxGenericTreeItem* last_child = (wxGenericTreeItem*)node->Data();
462 rect.y = dc.LogicalToDeviceY( last_child->m_y );
463 };
29d87bba 464
c801d85f
KB
465 long doX = 0;
466 long doY = 0;
467 dc.GetDeviceOrigin( &doX, &doY );
468 rect.height = ch-rect.y-doY;
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();
29d87bba 524
c801d85f
KB
525 int ch = 0;
526 GetClientSize( NULL, &ch );
29d87bba 527
c801d85f
KB
528 wxRectangle rect;
529 rect.x = 0; rect.y = 0;
530 rect.width = 10000; rect.height = ch;
29d87bba 531
c801d85f
KB
532 PrepareDC( dc );
533 if (p->m_children.Number() == 1)
534 {
535 rect.y = dc.LogicalToDeviceY( p->m_y );
536 }
537 else
538 {
539 wxNode *node = p->m_children.Member( new_child )->Previous();
540 wxGenericTreeItem* last_child = (wxGenericTreeItem*)node->Data();
541 rect.y = dc.LogicalToDeviceY( last_child->m_y );
542 };
29d87bba 543
c801d85f
KB
544 long doX = 0;
545 long doY = 0;
546 dc.GetDeviceOrigin( &doX, &doY );
547 rect.height = ch-rect.y-doY;
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 585#if 0 // VZ: don't delete items when the branch is collapsed
c801d85f 586 child->SendDelete( this );
29d87bba
VZ
587 delete node;
588 node = i->m_children.First();
74bedbeb
VZ
589#else
590 if ( child->IsExpanded() )
591 ExpandItem( child->m_itemId, wxTREE_EXPAND_COLLAPSE );
592 node = node->Next();
593#endif
c801d85f 594 };
29d87bba 595
74bedbeb 596 i->SendCollapse( this );
c801d85f 597 break;
74bedbeb
VZ
598 }
599
c801d85f
KB
600 case wxTREE_EXPAND_TOGGLE:
601 {
74bedbeb 602 if ( i->IsExpanded() )
c801d85f 603 ExpandItem( item, wxTREE_EXPAND_COLLAPSE );
74bedbeb
VZ
604 else
605 ExpandItem( item, wxTREE_EXPAND_EXPAND );
606 return TRUE;
607 }
c801d85f 608 };
74bedbeb
VZ
609
610 int cw = 0;
611 int ch = 0;
612 GetClientSize( &cw, &ch );
613 wxRect rect;
614 rect.x = 0;
615 rect.width = cw;
616 wxClientDC dc(this);
617 PrepareDC(dc);
618 rect.y = dc.LogicalToDeviceY( i->m_y );
619
620 long doY = 0;
621 dc.GetDeviceOrigin( NULL, &doY );
622 rect.height = ch-rect.y-doY;
623 Refresh( TRUE, &rect );
624
625 AdjustMyScrollbars();
626
c801d85f
KB
627 return TRUE;
628};
629
a32dd690
VZ
630void wxTreeCtrl::DeleteItem( long item )
631{
632 wxGenericTreeItem *pItem = FindItem( item );
633 wxCHECK_RET( pItem != NULL, "wxTreeCtrl::DeleteItem: no such pItem." );
634 pItem->m_parent->m_children.DeleteObject(pItem);
635 Refresh();
636}
637
74bedbeb 638bool wxTreeCtrl::DeleteAllItems()
c801d85f
KB
639{
640 delete m_anchor;
641 m_anchor = NULL;
642 Refresh();
643 return TRUE;
644};
645
646bool wxTreeCtrl::GetItem( wxTreeItem &info ) const
647{
648 wxGenericTreeItem *i = FindItem( info.m_itemId );
649 if (!i) return FALSE;
650 i->GetItem( info );
651 return TRUE;
652};
653
74bedbeb 654long wxTreeCtrl::GetItemData( long item ) const
c801d85f
KB
655{
656 wxGenericTreeItem *i = FindItem( item );
657 if (!i) return 0;
658 return i->m_data;
659};
660
74bedbeb 661wxString wxTreeCtrl::GetItemText( long item ) const
c801d85f
KB
662{
663 wxGenericTreeItem *i = FindItem( item );
664 if (!i) return "";
665 return i->m_text;
666};
667
74bedbeb
VZ
668int wxTreeCtrl::GetItemImage(long item) const
669{
670 wxGenericTreeItem *i = FindItem( item );
671 return i == 0 ? -1 : i->GetImage();
672}
673
674long wxTreeCtrl::GetParent( long item ) const
c801d85f
KB
675{
676 wxGenericTreeItem *i = FindItem( item );
677 if (!i) return -1;
678 i = i->m_parent;
679 if (!i) return -1;
680 return i->m_parent->m_itemId;
681};
682
74bedbeb 683long wxTreeCtrl::GetRootItem() const
c801d85f
KB
684{
685 if (m_anchor) return m_anchor->m_itemId;
686 return -1;
687};
688
74bedbeb 689long wxTreeCtrl::GetSelection() const
c801d85f
KB
690{
691 return 0;
692};
693
74bedbeb 694bool wxTreeCtrl::SelectItem( long WXUNUSED(item) ) const
c801d85f
KB
695{
696 return FALSE;
697};
698
74bedbeb 699bool wxTreeCtrl::ItemHasChildren( long item ) const
c801d85f
KB
700{
701 wxGenericTreeItem *i = FindItem( item );
702 if (!i) return FALSE;
703 return i->m_hasChildren;
704};
705
74bedbeb 706void wxTreeCtrl::SetIndent( int indent )
c801d85f
KB
707{
708 m_indent = indent;
709 Refresh();
710};
711
74bedbeb 712int wxTreeCtrl::GetIndent() const
c801d85f
KB
713{
714 return m_indent;
715};
716
717bool wxTreeCtrl::SetItem( wxTreeItem &info )
718{
719 wxGenericTreeItem *i = FindItem( info.m_itemId );
720 if (!i) return FALSE;
721 wxClientDC dc(this);
722 i->SetItem( info, &dc );
74bedbeb 723 Refresh();
c801d85f
KB
724 return TRUE;
725};
726
74bedbeb 727bool wxTreeCtrl::SetItemData( long item, long data )
c801d85f
KB
728{
729 wxGenericTreeItem *i = FindItem( item );
730 if (!i) return FALSE;
731 i->m_data = data;
732 return TRUE;
733};
734
74bedbeb 735bool wxTreeCtrl::SetItemText( long item, const wxString &text )
c801d85f
KB
736{
737 wxGenericTreeItem *i = FindItem( item );
738 if (!i) return FALSE;
739 wxClientDC dc(this);
740 i->SetText( text, &dc );
741 return TRUE;
742};
743
74bedbeb
VZ
744void wxTreeCtrl::SetItemImage(long item, int image, int imageSel) const
745{
746 wxGenericTreeItem *i = FindItem( item );
747 if ( i != 0 ) {
748 i->SetImage(image);
749 i->SetSelectedImage(imageSel);
750 }
751}
752
c801d85f
KB
753long wxTreeCtrl::HitTest( const wxPoint& point, int &flags )
754{
755 flags = 0;
756 if (!m_anchor) return -1;
757 return m_anchor->HitTest( point, flags );
758};
759
74bedbeb 760void wxTreeCtrl::AdjustMyScrollbars()
c801d85f
KB
761{
762 if (m_anchor)
763 {
764 int x = 0;
765 int y = 0;
766 m_anchor->GetSize( x, y );
767 y += 2*m_lineHeight;
768 int x_pos = GetScrollPos( wxHORIZONTAL );
769 int y_pos = GetScrollPos( wxVERTICAL );
770 SetScrollbars( 10, 10, x/10, y/10, x_pos, y_pos );
771 }
772 else
773 {
774 SetScrollbars( 0, 0, 0, 0 );
775 };
776};
777
778void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxPaintDC &dc, int level, int &y )
779{
780 int horizX = level*m_indent+10;
781 int oldY = y;
782 wxNode *node = item->m_children.First();
783 while (node)
784 {
785 wxGenericTreeItem *child = (wxGenericTreeItem *)node->Data();
786 dc.SetPen( m_dottedPen );
29d87bba 787
c801d85f 788 child->SetCross( horizX+15, y );
29d87bba 789
c801d85f
KB
790 if (!node->Next())
791 {
792 if (level != 0) oldY -= (m_lineHeight-5);
793 dc.DrawLine( horizX, oldY, horizX, y );
794 };
29d87bba 795
c801d85f
KB
796 child->m_x = horizX+33;
797 child->m_y = y-m_lineHeight/3;
798 child->m_height = m_lineHeight;
29d87bba 799
c801d85f
KB
800 if (IsExposed( 0, child->m_y-2, 10000, m_lineHeight+4 ))
801 {
29d87bba
VZ
802 int startX = horizX,
803 endX = horizX + 10;
804
805 if (!(node->Previous()) && (level == 0))
806 startX -= 10;
807 if (!child->HasChildren())
808 endX += 20;
c801d85f 809 dc.DrawLine( startX, y, endX, y );
29d87bba
VZ
810
811 if (child->HasChildren())
c801d85f
KB
812 {
813 dc.DrawLine( horizX+20, y, horizX+30, y );
814 dc.SetPen( *wxGREY_PEN );
815 dc.DrawRectangle( horizX+10, y-4, 11, 9 );
816 dc.SetPen( *wxBLACK_PEN );
817 dc.DrawLine( horizX+13, y, horizX+17, y );
818 if (child->HasPlus())
819 dc.DrawLine( horizX+15, y-2, horizX+15, y+2 );
820 };
c801d85f
KB
821
822 if (child->HasHilight())
823 {
29d87bba
VZ
824 dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
825#if 0 // VZ: this code leaves horizontal stripes when item is unselected
826 dc.SetBrush( *m_hilightBrush );
827 if (m_hasFocus)
828 dc.SetPen( wxBLACK_PEN );
829 else
830 dc.SetPen( wxTRANSPARENT_PEN );
831 long tw, th;
832 dc.GetTextExtent( child->m_text, &tw, &th );
833 dc.DrawRectangle( child->m_x-2, child->m_y-2, tw+4, th+4 );
834#else
835 int modeOld = dc.GetBackgroundMode();
836 dc.SetTextBackground( *wxBLACK );
837 dc.SetBackgroundMode(wxSOLID);
838#endif // 0
839
840 dc.DrawText( child->m_text, child->m_x, child->m_y );
841
842#if 0 // VZ: same as above
843 dc.SetPen( *wxBLACK_PEN );
844#else
845 dc.SetBackgroundMode(modeOld);
846 dc.SetTextBackground( *wxWHITE );
847 dc.SetBrush( *wxWHITE_BRUSH );
848#endif
849 dc.SetTextForeground( *wxBLACK );
850 }
851 else
852 dc.DrawText( child->m_text, child->m_x, child->m_y );
c801d85f 853 };
29d87bba 854
c801d85f 855 y += m_lineHeight;
74bedbeb 856 if ( child->IsExpanded() && child->NumberOfVisibleChildren() > 0)
c801d85f
KB
857 PaintLevel( child, dc, level+1, y );
858 node = node->Next();
859 };
860};
861
862void wxTreeCtrl::OnPaint( const wxPaintEvent &WXUNUSED(event) )
863{
864 if (!m_anchor) return;
865
866 if (!m_dc)
29d87bba 867 {
c801d85f
KB
868 m_dc = new wxPaintDC(this);
869 PrepareDC( *m_dc );
870 };
29d87bba 871
c801d85f 872 m_dc->SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
29d87bba 873
c801d85f
KB
874 m_dc->SetPen( m_dottedPen );
875 m_lineHeight = (int)(m_dc->GetCharHeight() + 4);
29d87bba 876
c801d85f
KB
877 int y = m_lineHeight / 2 + 2;
878 PaintLevel( m_anchor, *m_dc, 0, y );
879};
880
881void wxTreeCtrl::OnSetFocus( const wxFocusEvent &WXUNUSED(event) )
882{
883 m_hasFocus = TRUE;
884 if (m_current) RefreshLine( m_current );
885};
886
887void wxTreeCtrl::OnKillFocus( const wxFocusEvent &WXUNUSED(event) )
888{
889 m_hasFocus = FALSE;
890 if (m_current) RefreshLine( m_current );
891};
892
893void wxTreeCtrl::OnChar( wxKeyEvent &event )
894{
895 event.Skip();
896};
897
898void wxTreeCtrl::OnMouse( const wxMouseEvent &event )
899{
900 if (!event.LeftDown() &&
901 !event.LeftDClick()) return;
29d87bba 902
c801d85f
KB
903 wxClientDC dc(this);
904 PrepareDC(dc);
905 long x = dc.DeviceToLogicalX( (long)event.GetX() );
906 long y = dc.DeviceToLogicalY( (long)event.GetY() );
29d87bba 907
c801d85f
KB
908 int flag = 0;
909 long id = HitTest( wxPoint(x,y), flag );
29d87bba
VZ
910 if (id == -1)
911 return;
c801d85f 912 wxGenericTreeItem *item = FindItem( id );
29d87bba 913
c801d85f
KB
914 if (!item) return;
915 if ((flag != wxTREE_HITTEST_ONITEMBUTTON) &&
916 (flag != wxTREE_HITTEST_ONITEMLABEL)) return;
29d87bba 917
c801d85f
KB
918 if (m_current != item)
919 {
920 if (m_current)
921 {
922 m_current->SetHilight( FALSE );
923 RefreshLine( m_current );
924 };
925 m_current = item;
926 m_current->SetHilight( TRUE );
927 RefreshLine( m_current );
928 m_current->SendSelected( this );
929 };
29d87bba
VZ
930
931 if (event.LeftDClick())
932 m_current->SendKeyDown( this );
933
c801d85f
KB
934 if (flag == wxTREE_HITTEST_ONITEMBUTTON)
935 {
936 ExpandItem( item->m_itemId, wxTREE_EXPAND_TOGGLE );
937 return;
938 };
939};
940
941void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxPaintDC &dc, int level, int &y )
942{
943 int horizX = level*m_indent+10;
944 wxNode *node = item->m_children.First();
945 while (node)
946 {
947 wxGenericTreeItem *child = (wxGenericTreeItem *)node->Data();
948 dc.SetPen( m_dottedPen );
29d87bba
VZ
949
950 int startX = horizX,
951 endX = horizX + 10;
952
953 if (!node->Previous() && (level == 0))
954 startX -= 10;
955 if (!child->HasChildren())
956 endX += 20;
957
c801d85f
KB
958 child->m_x = horizX+33;
959 child->m_y = y-m_lineHeight/3-2;
960 child->m_height = m_lineHeight;
29d87bba 961
c801d85f 962 y += m_lineHeight;
74bedbeb 963 if ( child->IsExpanded() && child->NumberOfVisibleChildren() > 0 )
c801d85f 964 CalculateLevel( child, dc, level+1, y );
29d87bba 965
c801d85f
KB
966 node = node->Next();
967 };
968};
969
74bedbeb 970void wxTreeCtrl::CalculatePositions()
c801d85f 971{
29d87bba
VZ
972 if (!m_anchor)
973 return;
974
c801d85f
KB
975 wxClientDC dc(this);
976 PrepareDC( dc );
29d87bba 977
c801d85f 978 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
29d87bba 979
c801d85f
KB
980 dc.SetPen( m_dottedPen );
981 m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 982
c801d85f
KB
983 int y = m_lineHeight / 2 + 2;
984 CalculateLevel( m_anchor, dc, 0, y );
985};
986
987wxGenericTreeItem *wxTreeCtrl::FindItem( long itemId ) const
988{
989 if (!m_anchor) return NULL;
990 return m_anchor->FindItem( itemId );
991};
992
993void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
994{
995 if (!item) return;
996 wxClientDC dc(this);
997 PrepareDC( dc );
998 wxRect rect;
999 rect.x = dc.LogicalToDeviceX( item->m_x-2 );
1000 rect.y = dc.LogicalToDeviceY( item->m_y-2 );
1001 rect.width = 1000;
1002 rect.height = dc.GetCharHeight()+4;
1003 Refresh( TRUE, &rect );
1004};
1005
1006
1007