]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectrl.cpp
* Fixed Async -> sync in wxExecute
[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
360wxTreeCtrl::wxTreeCtrl(wxWindow *parent, const wxWindowID id,
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
384bool wxTreeCtrl::Create(wxWindow *parent, const wxWindowID id,
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
74bedbeb 630bool wxTreeCtrl::DeleteAllItems()
c801d85f
KB
631{
632 delete m_anchor;
633 m_anchor = NULL;
634 Refresh();
635 return TRUE;
636};
637
638bool wxTreeCtrl::GetItem( wxTreeItem &info ) const
639{
640 wxGenericTreeItem *i = FindItem( info.m_itemId );
641 if (!i) return FALSE;
642 i->GetItem( info );
643 return TRUE;
644};
645
74bedbeb 646long wxTreeCtrl::GetItemData( long item ) const
c801d85f
KB
647{
648 wxGenericTreeItem *i = FindItem( item );
649 if (!i) return 0;
650 return i->m_data;
651};
652
74bedbeb 653wxString wxTreeCtrl::GetItemText( long item ) const
c801d85f
KB
654{
655 wxGenericTreeItem *i = FindItem( item );
656 if (!i) return "";
657 return i->m_text;
658};
659
74bedbeb
VZ
660int wxTreeCtrl::GetItemImage(long item) const
661{
662 wxGenericTreeItem *i = FindItem( item );
663 return i == 0 ? -1 : i->GetImage();
664}
665
666long wxTreeCtrl::GetParent( long item ) const
c801d85f
KB
667{
668 wxGenericTreeItem *i = FindItem( item );
669 if (!i) return -1;
670 i = i->m_parent;
671 if (!i) return -1;
672 return i->m_parent->m_itemId;
673};
674
74bedbeb 675long wxTreeCtrl::GetRootItem() const
c801d85f
KB
676{
677 if (m_anchor) return m_anchor->m_itemId;
678 return -1;
679};
680
74bedbeb 681long wxTreeCtrl::GetSelection() const
c801d85f
KB
682{
683 return 0;
684};
685
74bedbeb 686bool wxTreeCtrl::SelectItem( long WXUNUSED(item) ) const
c801d85f
KB
687{
688 return FALSE;
689};
690
74bedbeb 691bool wxTreeCtrl::ItemHasChildren( long item ) const
c801d85f
KB
692{
693 wxGenericTreeItem *i = FindItem( item );
694 if (!i) return FALSE;
695 return i->m_hasChildren;
696};
697
74bedbeb 698void wxTreeCtrl::SetIndent( int indent )
c801d85f
KB
699{
700 m_indent = indent;
701 Refresh();
702};
703
74bedbeb 704int wxTreeCtrl::GetIndent() const
c801d85f
KB
705{
706 return m_indent;
707};
708
709bool wxTreeCtrl::SetItem( wxTreeItem &info )
710{
711 wxGenericTreeItem *i = FindItem( info.m_itemId );
712 if (!i) return FALSE;
713 wxClientDC dc(this);
714 i->SetItem( info, &dc );
74bedbeb 715 Refresh();
c801d85f
KB
716 return TRUE;
717};
718
74bedbeb 719bool wxTreeCtrl::SetItemData( long item, long data )
c801d85f
KB
720{
721 wxGenericTreeItem *i = FindItem( item );
722 if (!i) return FALSE;
723 i->m_data = data;
724 return TRUE;
725};
726
74bedbeb 727bool wxTreeCtrl::SetItemText( long item, const wxString &text )
c801d85f
KB
728{
729 wxGenericTreeItem *i = FindItem( item );
730 if (!i) return FALSE;
731 wxClientDC dc(this);
732 i->SetText( text, &dc );
733 return TRUE;
734};
735
74bedbeb
VZ
736void wxTreeCtrl::SetItemImage(long item, int image, int imageSel) const
737{
738 wxGenericTreeItem *i = FindItem( item );
739 if ( i != 0 ) {
740 i->SetImage(image);
741 i->SetSelectedImage(imageSel);
742 }
743}
744
c801d85f
KB
745long wxTreeCtrl::HitTest( const wxPoint& point, int &flags )
746{
747 flags = 0;
748 if (!m_anchor) return -1;
749 return m_anchor->HitTest( point, flags );
750};
751
74bedbeb 752void wxTreeCtrl::AdjustMyScrollbars()
c801d85f
KB
753{
754 if (m_anchor)
755 {
756 int x = 0;
757 int y = 0;
758 m_anchor->GetSize( x, y );
759 y += 2*m_lineHeight;
760 int x_pos = GetScrollPos( wxHORIZONTAL );
761 int y_pos = GetScrollPos( wxVERTICAL );
762 SetScrollbars( 10, 10, x/10, y/10, x_pos, y_pos );
763 }
764 else
765 {
766 SetScrollbars( 0, 0, 0, 0 );
767 };
768};
769
770void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxPaintDC &dc, int level, int &y )
771{
772 int horizX = level*m_indent+10;
773 int oldY = y;
774 wxNode *node = item->m_children.First();
775 while (node)
776 {
777 wxGenericTreeItem *child = (wxGenericTreeItem *)node->Data();
778 dc.SetPen( m_dottedPen );
29d87bba 779
c801d85f 780 child->SetCross( horizX+15, y );
29d87bba 781
c801d85f
KB
782 if (!node->Next())
783 {
784 if (level != 0) oldY -= (m_lineHeight-5);
785 dc.DrawLine( horizX, oldY, horizX, y );
786 };
29d87bba 787
c801d85f
KB
788 child->m_x = horizX+33;
789 child->m_y = y-m_lineHeight/3;
790 child->m_height = m_lineHeight;
29d87bba 791
c801d85f
KB
792 if (IsExposed( 0, child->m_y-2, 10000, m_lineHeight+4 ))
793 {
29d87bba
VZ
794 int startX = horizX,
795 endX = horizX + 10;
796
797 if (!(node->Previous()) && (level == 0))
798 startX -= 10;
799 if (!child->HasChildren())
800 endX += 20;
c801d85f 801 dc.DrawLine( startX, y, endX, y );
29d87bba
VZ
802
803 if (child->HasChildren())
c801d85f
KB
804 {
805 dc.DrawLine( horizX+20, y, horizX+30, y );
806 dc.SetPen( *wxGREY_PEN );
807 dc.DrawRectangle( horizX+10, y-4, 11, 9 );
808 dc.SetPen( *wxBLACK_PEN );
809 dc.DrawLine( horizX+13, y, horizX+17, y );
810 if (child->HasPlus())
811 dc.DrawLine( horizX+15, y-2, horizX+15, y+2 );
812 };
c801d85f
KB
813
814 if (child->HasHilight())
815 {
29d87bba
VZ
816 dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
817#if 0 // VZ: this code leaves horizontal stripes when item is unselected
818 dc.SetBrush( *m_hilightBrush );
819 if (m_hasFocus)
820 dc.SetPen( wxBLACK_PEN );
821 else
822 dc.SetPen( wxTRANSPARENT_PEN );
823 long tw, th;
824 dc.GetTextExtent( child->m_text, &tw, &th );
825 dc.DrawRectangle( child->m_x-2, child->m_y-2, tw+4, th+4 );
826#else
827 int modeOld = dc.GetBackgroundMode();
828 dc.SetTextBackground( *wxBLACK );
829 dc.SetBackgroundMode(wxSOLID);
830#endif // 0
831
832 dc.DrawText( child->m_text, child->m_x, child->m_y );
833
834#if 0 // VZ: same as above
835 dc.SetPen( *wxBLACK_PEN );
836#else
837 dc.SetBackgroundMode(modeOld);
838 dc.SetTextBackground( *wxWHITE );
839 dc.SetBrush( *wxWHITE_BRUSH );
840#endif
841 dc.SetTextForeground( *wxBLACK );
842 }
843 else
844 dc.DrawText( child->m_text, child->m_x, child->m_y );
c801d85f 845 };
29d87bba 846
c801d85f 847 y += m_lineHeight;
74bedbeb 848 if ( child->IsExpanded() && child->NumberOfVisibleChildren() > 0)
c801d85f
KB
849 PaintLevel( child, dc, level+1, y );
850 node = node->Next();
851 };
852};
853
854void wxTreeCtrl::OnPaint( const wxPaintEvent &WXUNUSED(event) )
855{
856 if (!m_anchor) return;
857
858 if (!m_dc)
29d87bba 859 {
c801d85f
KB
860 m_dc = new wxPaintDC(this);
861 PrepareDC( *m_dc );
862 };
29d87bba 863
c801d85f 864 m_dc->SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
29d87bba 865
c801d85f
KB
866 m_dc->SetPen( m_dottedPen );
867 m_lineHeight = (int)(m_dc->GetCharHeight() + 4);
29d87bba 868
c801d85f
KB
869 int y = m_lineHeight / 2 + 2;
870 PaintLevel( m_anchor, *m_dc, 0, y );
871};
872
873void wxTreeCtrl::OnSetFocus( const wxFocusEvent &WXUNUSED(event) )
874{
875 m_hasFocus = TRUE;
876 if (m_current) RefreshLine( m_current );
877};
878
879void wxTreeCtrl::OnKillFocus( const wxFocusEvent &WXUNUSED(event) )
880{
881 m_hasFocus = FALSE;
882 if (m_current) RefreshLine( m_current );
883};
884
885void wxTreeCtrl::OnChar( wxKeyEvent &event )
886{
887 event.Skip();
888};
889
890void wxTreeCtrl::OnMouse( const wxMouseEvent &event )
891{
892 if (!event.LeftDown() &&
893 !event.LeftDClick()) return;
29d87bba 894
c801d85f
KB
895 wxClientDC dc(this);
896 PrepareDC(dc);
897 long x = dc.DeviceToLogicalX( (long)event.GetX() );
898 long y = dc.DeviceToLogicalY( (long)event.GetY() );
29d87bba 899
c801d85f
KB
900 int flag = 0;
901 long id = HitTest( wxPoint(x,y), flag );
29d87bba
VZ
902 if (id == -1)
903 return;
c801d85f 904 wxGenericTreeItem *item = FindItem( id );
29d87bba 905
c801d85f
KB
906 if (!item) return;
907 if ((flag != wxTREE_HITTEST_ONITEMBUTTON) &&
908 (flag != wxTREE_HITTEST_ONITEMLABEL)) return;
29d87bba 909
c801d85f
KB
910 if (m_current != item)
911 {
912 if (m_current)
913 {
914 m_current->SetHilight( FALSE );
915 RefreshLine( m_current );
916 };
917 m_current = item;
918 m_current->SetHilight( TRUE );
919 RefreshLine( m_current );
920 m_current->SendSelected( this );
921 };
29d87bba
VZ
922
923 if (event.LeftDClick())
924 m_current->SendKeyDown( this );
925
c801d85f
KB
926 if (flag == wxTREE_HITTEST_ONITEMBUTTON)
927 {
928 ExpandItem( item->m_itemId, wxTREE_EXPAND_TOGGLE );
929 return;
930 };
931};
932
933void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxPaintDC &dc, int level, int &y )
934{
935 int horizX = level*m_indent+10;
936 wxNode *node = item->m_children.First();
937 while (node)
938 {
939 wxGenericTreeItem *child = (wxGenericTreeItem *)node->Data();
940 dc.SetPen( m_dottedPen );
29d87bba
VZ
941
942 int startX = horizX,
943 endX = horizX + 10;
944
945 if (!node->Previous() && (level == 0))
946 startX -= 10;
947 if (!child->HasChildren())
948 endX += 20;
949
c801d85f
KB
950 child->m_x = horizX+33;
951 child->m_y = y-m_lineHeight/3-2;
952 child->m_height = m_lineHeight;
29d87bba 953
c801d85f 954 y += m_lineHeight;
74bedbeb 955 if ( child->IsExpanded() && child->NumberOfVisibleChildren() > 0 )
c801d85f 956 CalculateLevel( child, dc, level+1, y );
29d87bba 957
c801d85f
KB
958 node = node->Next();
959 };
960};
961
74bedbeb 962void wxTreeCtrl::CalculatePositions()
c801d85f 963{
29d87bba
VZ
964 if (!m_anchor)
965 return;
966
c801d85f
KB
967 wxClientDC dc(this);
968 PrepareDC( dc );
29d87bba 969
c801d85f 970 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
29d87bba 971
c801d85f
KB
972 dc.SetPen( m_dottedPen );
973 m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 974
c801d85f
KB
975 int y = m_lineHeight / 2 + 2;
976 CalculateLevel( m_anchor, dc, 0, y );
977};
978
979wxGenericTreeItem *wxTreeCtrl::FindItem( long itemId ) const
980{
981 if (!m_anchor) return NULL;
982 return m_anchor->FindItem( itemId );
983};
984
985void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
986{
987 if (!item) return;
988 wxClientDC dc(this);
989 PrepareDC( dc );
990 wxRect rect;
991 rect.x = dc.LogicalToDeviceX( item->m_x-2 );
992 rect.y = dc.LogicalToDeviceY( item->m_y-2 );
993 rect.width = 1000;
994 rect.height = dc.GetCharHeight()+4;
995 Refresh( TRUE, &rect );
996};
997
998
999