]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treectrl.cpp
added missing DeleteItem(long itemId) function
[wxWidgets.git] / src / generic / treectrl.cpp
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
8 // Licence: wxWindows licence
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
22 IMPLEMENT_DYNAMIC_CLASS(wxTreeItem, wxObject)
23
24 wxTreeItem::wxTreeItem()
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
40 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent,wxCommandEvent)
41
42 wxTreeEvent::wxTreeEvent( wxEventType commandType, int id ) :
43 wxCommandEvent( commandType, id )
44 {
45 m_code = 0;
46 m_oldItem = 0;
47 };
48
49 //-----------------------------------------------------------------------------
50 // wxGenericTreeItem
51 //-----------------------------------------------------------------------------
52
53 IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeItem,wxObject)
54
55 wxGenericTreeItem::wxGenericTreeItem( wxGenericTreeItem *parent )
56 {
57 Reset();
58 m_parent = parent;
59 m_hasHilight = FALSE;
60 };
61
62 wxGenericTreeItem::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
70 void wxGenericTreeItem::SetItem( const wxTreeItem &item, wxDC *dc )
71 {
72 if ((item.m_mask & wxTREE_MASK_HANDLE) == wxTREE_MASK_HANDLE)
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
93 void 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
103 void wxGenericTreeItem::Reset()
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 );
121 m_isCollapsed = TRUE;
122 m_parent = NULL;
123 };
124
125 void 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
141 bool wxGenericTreeItem::HasChildren()
142 {
143 return m_hasChildren;
144 };
145
146 bool wxGenericTreeItem::HasPlus()
147 {
148 if ( !HasChildren() )
149 return FALSE;
150
151 return !IsExpanded();
152 };
153
154 int wxGenericTreeItem::NumberOfVisibleDescendents()
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
167 int wxGenericTreeItem::NumberOfVisibleChildren()
168 {
169 return m_isCollapsed ? 0 : m_children.Number();
170 };
171
172 wxGenericTreeItem *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
186 void wxGenericTreeItem::AddChild( wxGenericTreeItem *child )
187 {
188 m_children.Append( child );
189 };
190
191 void wxGenericTreeItem::SetCross( int x, int y )
192 {
193 m_xCross = x;
194 m_yCross = y;
195 };
196
197 void 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
211 long 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) &&
219 (m_hasChildren))
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
251 void 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
266 void 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
274 void 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
282 void wxGenericTreeItem::SendDelete( wxWindow *target )
283 {
284 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, target->GetId() );
285 PrepareEvent( event );
286 event.SetEventObject( target );
287 target->ProcessEvent( event );
288 };
289
290 void wxGenericTreeItem::SendExpand( wxWindow *target )
291 {
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);
301 PrepareEvent( event );
302 target->ProcessEvent( event );
303 };
304
305 void 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);
311 event.SetEventObject( target );
312 PrepareEvent( event );
313 target->ProcessEvent( event );
314
315 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
316 PrepareEvent( event );
317 target->ProcessEvent( event );
318 };
319
320 void wxGenericTreeItem::SetHilight( bool set )
321 {
322 m_hasHilight = set;
323 };
324
325 bool wxGenericTreeItem::HasHilight()
326 {
327 return m_hasHilight;
328 };
329
330 //-----------------------------------------------------------------------------
331 // wxTreeCtrl
332 //-----------------------------------------------------------------------------
333
334 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl,wxScrolledWindow
335 )
336
337 BEGIN_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)
343 END_EVENT_TABLE()
344
345 wxTreeCtrl::wxTreeCtrl()
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
360 wxTreeCtrl::wxTreeCtrl(wxWindow *parent, wxWindowID id,
361 const wxPoint& pos,
362 const wxSize& size,
363 long style, const wxString& name )
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
379 wxTreeCtrl::~wxTreeCtrl()
380 {
381 if (m_dc) delete m_dc;
382 };
383
384 bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
385 const wxPoint& pos,
386 const wxSize& size,
387 long style
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
396 int wxTreeCtrl::GetCount() const
397 {
398 if (!m_anchor) return 0;
399 return m_anchor->NumberOfVisibleDescendents();
400 };
401
402 long wxTreeCtrl::InsertItem( long parent, const wxString& label, int image,
403 int selImage, long WXUNUSED(insertAfter) )
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 };
434
435 wxClientDC dc(this);
436 wxGenericTreeItem *new_child = new wxGenericTreeItem( p, item, &dc );
437 if (p)
438 p->AddChild( new_child );
439 else
440 m_anchor = new_child;
441
442 if (p)
443 {
444 CalculatePositions();
445
446 int ch = 0;
447 GetClientSize( NULL, &ch );
448
449 wxRectangle rect;
450 rect.x = 0; rect.y = 0;
451 rect.width = 10000; rect.height = ch;
452
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 };
464
465 long doX = 0;
466 long doY = 0;
467 dc.GetDeviceOrigin( &doX, &doY );
468 rect.height = ch-rect.y-doY;
469
470 AdjustMyScrollbars();
471
472 if (rect.height > 0) Refresh( FALSE, &rect);
473 }
474 else
475 {
476 AdjustMyScrollbars();
477
478 Refresh();
479 };
480
481 return m_lastId;
482 };
483
484 long wxTreeCtrl::InsertItem( long parent, wxTreeItem &info, long WXUNUSED(insertAfter) )
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 };
513
514 wxClientDC dc(this);
515 wxGenericTreeItem *new_child = new wxGenericTreeItem( p, info, &dc );
516 if (p)
517 p->AddChild( new_child );
518 else
519 m_anchor = new_child;
520
521 if (p)
522 {
523 CalculatePositions();
524
525 int ch = 0;
526 GetClientSize( NULL, &ch );
527
528 wxRectangle rect;
529 rect.x = 0; rect.y = 0;
530 rect.width = 10000; rect.height = ch;
531
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 };
543
544 long doX = 0;
545 long doY = 0;
546 dc.GetDeviceOrigin( &doX, &doY );
547 rect.height = ch-rect.y-doY;
548
549 AdjustMyScrollbars();
550
551 if (rect.height > 0) Refresh( FALSE, &rect);
552 }
553 else
554 {
555 AdjustMyScrollbars();
556
557 Refresh();
558 };
559
560 info.m_mask = oldMask;
561 return ret;
562 };
563
564 bool wxTreeCtrl::ExpandItem( long item, int action )
565 {
566 wxGenericTreeItem *i = FindItem( item );
567 if (!i)
568 return FALSE;
569
570 switch (action)
571 {
572 case wxTREE_EXPAND_EXPAND:
573 {
574 i->SendExpand( this );
575 break;
576 }
577
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();
585 #if 0 // VZ: don't delete items when the branch is collapsed
586 child->SendDelete( this );
587 delete node;
588 node = i->m_children.First();
589 #else
590 if ( child->IsExpanded() )
591 ExpandItem( child->m_itemId, wxTREE_EXPAND_COLLAPSE );
592 node = node->Next();
593 #endif
594 };
595
596 i->SendCollapse( this );
597 break;
598 }
599
600 case wxTREE_EXPAND_TOGGLE:
601 {
602 if ( i->IsExpanded() )
603 ExpandItem( item, wxTREE_EXPAND_COLLAPSE );
604 else
605 ExpandItem( item, wxTREE_EXPAND_EXPAND );
606 return TRUE;
607 }
608 };
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
627 return TRUE;
628 };
629
630 void 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
638 bool wxTreeCtrl::DeleteAllItems()
639 {
640 delete m_anchor;
641 m_anchor = NULL;
642 Refresh();
643 return TRUE;
644 };
645
646 bool 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
654 long wxTreeCtrl::GetItemData( long item ) const
655 {
656 wxGenericTreeItem *i = FindItem( item );
657 if (!i) return 0;
658 return i->m_data;
659 };
660
661 wxString wxTreeCtrl::GetItemText( long item ) const
662 {
663 wxGenericTreeItem *i = FindItem( item );
664 if (!i) return "";
665 return i->m_text;
666 };
667
668 int wxTreeCtrl::GetItemImage(long item) const
669 {
670 wxGenericTreeItem *i = FindItem( item );
671 return i == 0 ? -1 : i->GetImage();
672 }
673
674 long wxTreeCtrl::GetParent( long item ) const
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
683 long wxTreeCtrl::GetRootItem() const
684 {
685 if (m_anchor) return m_anchor->m_itemId;
686 return -1;
687 };
688
689 long wxTreeCtrl::GetSelection() const
690 {
691 return 0;
692 };
693
694 bool wxTreeCtrl::SelectItem( long WXUNUSED(item) ) const
695 {
696 return FALSE;
697 };
698
699 bool wxTreeCtrl::ItemHasChildren( long item ) const
700 {
701 wxGenericTreeItem *i = FindItem( item );
702 if (!i) return FALSE;
703 return i->m_hasChildren;
704 };
705
706 void wxTreeCtrl::SetIndent( int indent )
707 {
708 m_indent = indent;
709 Refresh();
710 };
711
712 int wxTreeCtrl::GetIndent() const
713 {
714 return m_indent;
715 };
716
717 bool 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 );
723 Refresh();
724 return TRUE;
725 };
726
727 bool wxTreeCtrl::SetItemData( long item, long data )
728 {
729 wxGenericTreeItem *i = FindItem( item );
730 if (!i) return FALSE;
731 i->m_data = data;
732 return TRUE;
733 };
734
735 bool wxTreeCtrl::SetItemText( long item, const wxString &text )
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
744 void 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
753 long 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
760 void wxTreeCtrl::AdjustMyScrollbars()
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
778 void 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 );
787
788 child->SetCross( horizX+15, y );
789
790 if (!node->Next())
791 {
792 if (level != 0) oldY -= (m_lineHeight-5);
793 dc.DrawLine( horizX, oldY, horizX, y );
794 };
795
796 child->m_x = horizX+33;
797 child->m_y = y-m_lineHeight/3;
798 child->m_height = m_lineHeight;
799
800 if (IsExposed( 0, child->m_y-2, 10000, m_lineHeight+4 ))
801 {
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;
809 dc.DrawLine( startX, y, endX, y );
810
811 if (child->HasChildren())
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 };
821
822 if (child->HasHilight())
823 {
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 );
853 };
854
855 y += m_lineHeight;
856 if ( child->IsExpanded() && child->NumberOfVisibleChildren() > 0)
857 PaintLevel( child, dc, level+1, y );
858 node = node->Next();
859 };
860 };
861
862 void wxTreeCtrl::OnPaint( const wxPaintEvent &WXUNUSED(event) )
863 {
864 if (!m_anchor) return;
865
866 if (!m_dc)
867 {
868 m_dc = new wxPaintDC(this);
869 PrepareDC( *m_dc );
870 };
871
872 m_dc->SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
873
874 m_dc->SetPen( m_dottedPen );
875 m_lineHeight = (int)(m_dc->GetCharHeight() + 4);
876
877 int y = m_lineHeight / 2 + 2;
878 PaintLevel( m_anchor, *m_dc, 0, y );
879 };
880
881 void wxTreeCtrl::OnSetFocus( const wxFocusEvent &WXUNUSED(event) )
882 {
883 m_hasFocus = TRUE;
884 if (m_current) RefreshLine( m_current );
885 };
886
887 void wxTreeCtrl::OnKillFocus( const wxFocusEvent &WXUNUSED(event) )
888 {
889 m_hasFocus = FALSE;
890 if (m_current) RefreshLine( m_current );
891 };
892
893 void wxTreeCtrl::OnChar( wxKeyEvent &event )
894 {
895 event.Skip();
896 };
897
898 void wxTreeCtrl::OnMouse( const wxMouseEvent &event )
899 {
900 if (!event.LeftDown() &&
901 !event.LeftDClick()) return;
902
903 wxClientDC dc(this);
904 PrepareDC(dc);
905 long x = dc.DeviceToLogicalX( (long)event.GetX() );
906 long y = dc.DeviceToLogicalY( (long)event.GetY() );
907
908 int flag = 0;
909 long id = HitTest( wxPoint(x,y), flag );
910 if (id == -1)
911 return;
912 wxGenericTreeItem *item = FindItem( id );
913
914 if (!item) return;
915 if ((flag != wxTREE_HITTEST_ONITEMBUTTON) &&
916 (flag != wxTREE_HITTEST_ONITEMLABEL)) return;
917
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 };
930
931 if (event.LeftDClick())
932 m_current->SendKeyDown( this );
933
934 if (flag == wxTREE_HITTEST_ONITEMBUTTON)
935 {
936 ExpandItem( item->m_itemId, wxTREE_EXPAND_TOGGLE );
937 return;
938 };
939 };
940
941 void 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 );
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
958 child->m_x = horizX+33;
959 child->m_y = y-m_lineHeight/3-2;
960 child->m_height = m_lineHeight;
961
962 y += m_lineHeight;
963 if ( child->IsExpanded() && child->NumberOfVisibleChildren() > 0 )
964 CalculateLevel( child, dc, level+1, y );
965
966 node = node->Next();
967 };
968 };
969
970 void wxTreeCtrl::CalculatePositions()
971 {
972 if (!m_anchor)
973 return;
974
975 wxClientDC dc(this);
976 PrepareDC( dc );
977
978 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
979
980 dc.SetPen( m_dottedPen );
981 m_lineHeight = (int)(dc.GetCharHeight() + 4);
982
983 int y = m_lineHeight / 2 + 2;
984 CalculateLevel( m_anchor, dc, 0, y );
985 };
986
987 wxGenericTreeItem *wxTreeCtrl::FindItem( long itemId ) const
988 {
989 if (!m_anchor) return NULL;
990 return m_anchor->FindItem( itemId );
991 };
992
993 void 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