]> git.saurik.com Git - wxWidgets.git/blob - src/generic/treectrl.cpp
Sorry, I went and removed consts as per the style guide :-)
[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 bool wxTreeCtrl::DeleteAllItems()
631 {
632 delete m_anchor;
633 m_anchor = NULL;
634 Refresh();
635 return TRUE;
636 };
637
638 bool 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
646 long wxTreeCtrl::GetItemData( long item ) const
647 {
648 wxGenericTreeItem *i = FindItem( item );
649 if (!i) return 0;
650 return i->m_data;
651 };
652
653 wxString wxTreeCtrl::GetItemText( long item ) const
654 {
655 wxGenericTreeItem *i = FindItem( item );
656 if (!i) return "";
657 return i->m_text;
658 };
659
660 int wxTreeCtrl::GetItemImage(long item) const
661 {
662 wxGenericTreeItem *i = FindItem( item );
663 return i == 0 ? -1 : i->GetImage();
664 }
665
666 long wxTreeCtrl::GetParent( long item ) const
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
675 long wxTreeCtrl::GetRootItem() const
676 {
677 if (m_anchor) return m_anchor->m_itemId;
678 return -1;
679 };
680
681 long wxTreeCtrl::GetSelection() const
682 {
683 return 0;
684 };
685
686 bool wxTreeCtrl::SelectItem( long WXUNUSED(item) ) const
687 {
688 return FALSE;
689 };
690
691 bool wxTreeCtrl::ItemHasChildren( long item ) const
692 {
693 wxGenericTreeItem *i = FindItem( item );
694 if (!i) return FALSE;
695 return i->m_hasChildren;
696 };
697
698 void wxTreeCtrl::SetIndent( int indent )
699 {
700 m_indent = indent;
701 Refresh();
702 };
703
704 int wxTreeCtrl::GetIndent() const
705 {
706 return m_indent;
707 };
708
709 bool 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 );
715 Refresh();
716 return TRUE;
717 };
718
719 bool wxTreeCtrl::SetItemData( long item, long data )
720 {
721 wxGenericTreeItem *i = FindItem( item );
722 if (!i) return FALSE;
723 i->m_data = data;
724 return TRUE;
725 };
726
727 bool wxTreeCtrl::SetItemText( long item, const wxString &text )
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
736 void 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
745 long 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
752 void wxTreeCtrl::AdjustMyScrollbars()
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
770 void 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 );
779
780 child->SetCross( horizX+15, y );
781
782 if (!node->Next())
783 {
784 if (level != 0) oldY -= (m_lineHeight-5);
785 dc.DrawLine( horizX, oldY, horizX, y );
786 };
787
788 child->m_x = horizX+33;
789 child->m_y = y-m_lineHeight/3;
790 child->m_height = m_lineHeight;
791
792 if (IsExposed( 0, child->m_y-2, 10000, m_lineHeight+4 ))
793 {
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;
801 dc.DrawLine( startX, y, endX, y );
802
803 if (child->HasChildren())
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 };
813
814 if (child->HasHilight())
815 {
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 );
845 };
846
847 y += m_lineHeight;
848 if ( child->IsExpanded() && child->NumberOfVisibleChildren() > 0)
849 PaintLevel( child, dc, level+1, y );
850 node = node->Next();
851 };
852 };
853
854 void wxTreeCtrl::OnPaint( const wxPaintEvent &WXUNUSED(event) )
855 {
856 if (!m_anchor) return;
857
858 if (!m_dc)
859 {
860 m_dc = new wxPaintDC(this);
861 PrepareDC( *m_dc );
862 };
863
864 m_dc->SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
865
866 m_dc->SetPen( m_dottedPen );
867 m_lineHeight = (int)(m_dc->GetCharHeight() + 4);
868
869 int y = m_lineHeight / 2 + 2;
870 PaintLevel( m_anchor, *m_dc, 0, y );
871 };
872
873 void wxTreeCtrl::OnSetFocus( const wxFocusEvent &WXUNUSED(event) )
874 {
875 m_hasFocus = TRUE;
876 if (m_current) RefreshLine( m_current );
877 };
878
879 void wxTreeCtrl::OnKillFocus( const wxFocusEvent &WXUNUSED(event) )
880 {
881 m_hasFocus = FALSE;
882 if (m_current) RefreshLine( m_current );
883 };
884
885 void wxTreeCtrl::OnChar( wxKeyEvent &event )
886 {
887 event.Skip();
888 };
889
890 void wxTreeCtrl::OnMouse( const wxMouseEvent &event )
891 {
892 if (!event.LeftDown() &&
893 !event.LeftDClick()) return;
894
895 wxClientDC dc(this);
896 PrepareDC(dc);
897 long x = dc.DeviceToLogicalX( (long)event.GetX() );
898 long y = dc.DeviceToLogicalY( (long)event.GetY() );
899
900 int flag = 0;
901 long id = HitTest( wxPoint(x,y), flag );
902 if (id == -1)
903 return;
904 wxGenericTreeItem *item = FindItem( id );
905
906 if (!item) return;
907 if ((flag != wxTREE_HITTEST_ONITEMBUTTON) &&
908 (flag != wxTREE_HITTEST_ONITEMLABEL)) return;
909
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 };
922
923 if (event.LeftDClick())
924 m_current->SendKeyDown( this );
925
926 if (flag == wxTREE_HITTEST_ONITEMBUTTON)
927 {
928 ExpandItem( item->m_itemId, wxTREE_EXPAND_TOGGLE );
929 return;
930 };
931 };
932
933 void 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 );
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
950 child->m_x = horizX+33;
951 child->m_y = y-m_lineHeight/3-2;
952 child->m_height = m_lineHeight;
953
954 y += m_lineHeight;
955 if ( child->IsExpanded() && child->NumberOfVisibleChildren() > 0 )
956 CalculateLevel( child, dc, level+1, y );
957
958 node = node->Next();
959 };
960 };
961
962 void wxTreeCtrl::CalculatePositions()
963 {
964 if (!m_anchor)
965 return;
966
967 wxClientDC dc(this);
968 PrepareDC( dc );
969
970 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
971
972 dc.SetPen( m_dottedPen );
973 m_lineHeight = (int)(dc.GetCharHeight() + 4);
974
975 int y = m_lineHeight / 2 + 2;
976 CalculateLevel( m_anchor, dc, 0, y );
977 };
978
979 wxGenericTreeItem *wxTreeCtrl::FindItem( long itemId ) const
980 {
981 if (!m_anchor) return NULL;
982 return m_anchor->FindItem( itemId );
983 };
984
985 void 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