]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectrl.cpp
wxCHECK/wxCHECK_RET changes
[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
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
22IMPLEMENT_DYNAMIC_CLASS(wxTreeItem, wxObject)
23
24wxTreeItem::wxTreeItem(void)
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
42wxTreeEvent::wxTreeEvent( WXTYPE commandType, int id ) :
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{
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
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
103void wxGenericTreeItem::Reset(void)
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_parent = NULL;
122};
123
124void wxGenericTreeItem::GetItem( wxTreeItem &item ) const
125{
126 if ((item.m_mask & wxTREE_MASK_STATE) == wxTREE_MASK_STATE)
127 item.m_state = m_state;
128 if ((item.m_mask & wxTREE_MASK_TEXT) == wxTREE_MASK_TEXT)
129 item.m_text = m_text;
130 if ((item.m_mask & wxTREE_MASK_IMAGE) == wxTREE_MASK_IMAGE)
131 item.m_image = m_image;
132 if ((item.m_mask & wxTREE_MASK_SELECTED_IMAGE) == wxTREE_MASK_SELECTED_IMAGE)
133 item.m_selectedImage = m_selectedImage;
134 if ((item.m_mask & wxTREE_MASK_CHILDREN) == wxTREE_MASK_CHILDREN)
135 item.m_children = (int)m_hasChildren;
136 if ((item.m_mask & wxTREE_MASK_DATA) == wxTREE_MASK_DATA)
137 item.m_data = m_data;
138};
139
140bool wxGenericTreeItem::HasChildren(void)
141{
142 return m_hasChildren;
143};
144
145bool wxGenericTreeItem::HasPlus(void)
146{
147 return (m_hasChildren && (m_children.Number() == 0));
148};
149
150int wxGenericTreeItem::NumberOfVisibleDescendents(void)
151{
152 int ret = m_children.Number();
153 wxNode *node = m_children.First();
154 while (node)
155 {
156 wxGenericTreeItem *item = (wxGenericTreeItem*)node->Data();
157 ret += item->NumberOfVisibleDescendents();
158 node = node->Next();
159 };
160 return ret;
161};
162
163int wxGenericTreeItem::NumberOfVisibleChildren(void)
164{
165 return m_children.Number();
166};
167
168wxGenericTreeItem *wxGenericTreeItem::FindItem( long itemId ) const
169{
170 if (m_itemId == itemId) return (wxGenericTreeItem*)(this);
171 wxNode *node = m_children.First();
172 while (node)
173 {
174 wxGenericTreeItem *item = (wxGenericTreeItem*)node->Data();
175 wxGenericTreeItem *res = item->FindItem( itemId );
176 if (res) return (wxGenericTreeItem*)(res);
177 node = node->Next();
178 };
179 return NULL;
180};
181
182void wxGenericTreeItem::AddChild( wxGenericTreeItem *child )
183{
184 m_children.Append( child );
185};
186
187void wxGenericTreeItem::SetCross( int x, int y )
188{
189 m_xCross = x;
190 m_yCross = y;
191};
192
193void wxGenericTreeItem::GetSize( int &x, int &y )
194{
195 if (y < m_y + 10) y = m_y +10;
196 int width = m_x + m_width;
197 if (width > x) x = width;
198 wxNode *node = m_children.First();
199 while (node)
200 {
201 wxGenericTreeItem *item = (wxGenericTreeItem*)node->Data();
202 item->GetSize( x, y );
203 node = node->Next();
204 };
205};
206
207long wxGenericTreeItem::HitTest( const wxPoint& point, int &flags )
208{
209 if (m_parent && ((point.y > m_y) && (point.y < m_y+m_height)))
210 {
211 if ((point.x > m_xCross-5) &&
212 (point.x < m_xCross+5) &&
213 (point.y > m_yCross-5) &&
214 (point.y < m_yCross+5) &&
215 (m_hasChildren)
216 )
217 {
218 flags = wxTREE_HITTEST_ONITEMBUTTON;
219 return m_itemId;
220 };
221 if ((point.x > m_x) && (point.x < m_x+m_width))
222 {
223 flags = wxTREE_HITTEST_ONITEMLABEL;
224 return m_itemId;
225 };
226 if (point.x > m_x)
227 {
228 flags = wxTREE_HITTEST_ONITEMRIGHT;
229 return m_itemId;
230 };
231 flags = wxTREE_HITTEST_ONITEMINDENT;
232 return m_itemId;
233 }
234 else
235 {
236 wxNode *node = m_children.First();
237 while (node)
238 {
239 wxGenericTreeItem *child = (wxGenericTreeItem*)node->Data();
240 long res = child->HitTest( point, flags );
241 if (res != -1) return res;
242 node = node->Next();
243 };
244 };
245 return -1;
246};
247
248void wxGenericTreeItem::PrepareEvent( wxTreeEvent &event )
249{
250 event.m_item.m_itemId = m_itemId;
251 event.m_item.m_state = m_state;
252 event.m_item.m_text = m_text;
253 event.m_item.m_image = m_image;
254 event.m_item.m_selectedImage = m_selectedImage;
255 event.m_item.m_children = (int)m_hasChildren;
256 event.m_item.m_data = m_data;
257 event.m_oldItem = 0;
258 event.m_code = 0;
259 event.m_pointDrag.x = 0;
260 event.m_pointDrag.y = 0;
261};
262
263void wxGenericTreeItem::SendKeyDown( wxWindow *target )
264{
265 wxTreeEvent event( wxEVT_COMMAND_TREE_KEY_DOWN, target->GetId() );
266 PrepareEvent( event );
267 event.SetEventObject( target );
268 target->ProcessEvent( event );
269};
270
271void wxGenericTreeItem::SendSelected( wxWindow *target )
272{
273 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGED, target->GetId() );
274 PrepareEvent( event );
275 event.SetEventObject( target );
276 target->ProcessEvent( event );
277};
278
279void wxGenericTreeItem::SendDelete( wxWindow *target )
280{
281 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM,
282 target->GetId() );
283 PrepareEvent( event );
284 event.SetEventObject( target );
285 target->ProcessEvent( event );
286};
287
288void wxGenericTreeItem::SendExpand( wxWindow *target )
289{
290 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDED,
291 target->GetId() );
292 PrepareEvent( event );
293 event.SetEventObject( target );
294 target->ProcessEvent( event );
295};
296
297void wxGenericTreeItem::SetHilight( bool set )
298{
299 m_hasHilight = set;
300};
301
302bool wxGenericTreeItem::HasHilight(void)
303{
304 return m_hasHilight;
305};
306
307//-----------------------------------------------------------------------------
308// wxTreeCtrl
309//-----------------------------------------------------------------------------
310
311IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl,wxScrolledWindow
312)
313
314BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow)
315 EVT_PAINT (wxTreeCtrl::OnPaint)
316 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse)
317 EVT_CHAR (wxTreeCtrl::OnChar)
318 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus)
319 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus)
320END_EVENT_TABLE()
321
322wxTreeCtrl::wxTreeCtrl(void)
323{
324 m_current = NULL;
325 m_anchor = NULL;
326 m_hasFocus = FALSE;
327 m_xScroll = 0;
328 m_yScroll = 0;
329 m_lastId = 0;
330 m_lineHeight = 10;
331 m_indent = 15;
332 m_isCreated = FALSE;
333 m_dc = NULL;
334 m_hilightBrush = new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), wxSOLID );
335};
336
337wxTreeCtrl::wxTreeCtrl(wxWindow *parent, const wxWindowID id,
338 const wxPoint& pos,
339 const wxSize& size,
340 const long style, const wxString& name )
341{
342 m_current = NULL;
343 m_anchor = NULL;
344 m_hasFocus = FALSE;
345 m_xScroll = 0;
346 m_yScroll = 0;
347 m_lastId = 0;
348 m_lineHeight = 10;
349 m_indent = 15;
350 m_isCreated = FALSE;
351 m_dc = NULL;
352 m_hilightBrush = new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), wxSOLID );
353 Create( parent, id, pos, size, style, name );
354};
355
356wxTreeCtrl::~wxTreeCtrl(void)
357{
358 if (m_dc) delete m_dc;
359};
360
361bool wxTreeCtrl::Create(wxWindow *parent, const wxWindowID id,
362 const wxPoint& pos,
363 const wxSize& size,
364 const long style
365, const wxString& name )
366{
367 wxScrolledWindow::Create( parent, id, pos, size, style, name );
368 SetBackgroundColour( *wxWHITE );
369 m_dottedPen = wxPen( *wxBLACK, 0, 0 );
370 return TRUE;
371};
372
373int wxTreeCtrl::GetCount(void) const
374{
375 if (!m_anchor) return 0;
376 return m_anchor->NumberOfVisibleDescendents();
377};
378
379long wxTreeCtrl::InsertItem( const long parent, const wxString& label, const int image,
380 const int selImage, const long WXUNUSED(insertAfter) )
381{
382 wxGenericTreeItem *p = NULL;
383 if (parent == 0)
384 {
385 if (m_anchor) return -1;
386 }
387 else
388 {
389 p = FindItem( parent );
390 if (!p) return -1;
391 };
392 wxTreeItem item;
393 m_lastId++;
394 item.m_mask = wxTREE_MASK_HANDLE;
395 item.m_itemId = m_lastId;
396 if (!label.IsNull() || (label == ""))
397 {
398 item.m_text = label;
399 item.m_mask |= wxTREE_MASK_TEXT;
400 };
401 if (image >= 0)
402 {
403 item.m_image = image;
404 item.m_mask |= wxTREE_MASK_IMAGE;
405 };
406 if (selImage >= 0)
407 {
408 item.m_selectedImage = selImage;
409 item.m_mask |= wxTREE_MASK_SELECTED_IMAGE;
410 };
411
412 wxClientDC dc(this);
413 wxGenericTreeItem *new_child = new wxGenericTreeItem( p, item, &dc );
414 if (p)
415 p->AddChild( new_child );
416 else
417 m_anchor = new_child;
418
419 if (p)
420 {
421 CalculatePositions();
422
423 int ch = 0;
424 GetClientSize( NULL, &ch );
425
426 wxRectangle rect;
427 rect.x = 0; rect.y = 0;
428 rect.width = 10000; rect.height = ch;
429
430 PrepareDC( dc );
431 if (p->m_children.Number() == 1)
432 {
433 rect.y = dc.LogicalToDeviceY( p->m_y );
434 }
435 else
436 {
437 wxNode *node = p->m_children.Member( new_child )->Previous();
438 wxGenericTreeItem* last_child = (wxGenericTreeItem*)node->Data();
439 rect.y = dc.LogicalToDeviceY( last_child->m_y );
440 };
441
442 long doX = 0;
443 long doY = 0;
444 dc.GetDeviceOrigin( &doX, &doY );
445 rect.height = ch-rect.y-doY;
446
447 AdjustMyScrollbars();
448
449 if (rect.height > 0) Refresh( FALSE, &rect);
450 }
451 else
452 {
453 AdjustMyScrollbars();
454
455 Refresh();
456 };
457
458 return m_lastId;
459};
460
461long wxTreeCtrl::InsertItem( const long parent, wxTreeItem &info, const long WXUNUSED(insertAfter) )
462{
463 int oldMask = info.m_mask;
464 wxGenericTreeItem *p = NULL;
465 if (parent == 0)
466 {
467 if (m_anchor) return -1;
468 }
469 else
470 {
471 p = FindItem( parent );
472 if (!p)
473 {
474 printf( "TreeItem not found.\n" );
475 return -1;
476 };
477 };
478 long ret = 0;
479 if ((info.m_mask & wxTREE_MASK_HANDLE) == 0)
480 {
481 m_lastId++;
482 info.m_itemId = m_lastId;
483 info.m_mask |= wxTREE_MASK_HANDLE;
484 ret = m_lastId;
485 }
486 else
487 {
488 ret = info.m_itemId;
489 };
490
491 wxClientDC dc(this);
492 wxGenericTreeItem *new_child = new wxGenericTreeItem( p, info, &dc );
493 if (p)
494 p->AddChild( new_child );
495 else
496 m_anchor = new_child;
497
498 if (p)
499 {
500 CalculatePositions();
501
502 int ch = 0;
503 GetClientSize( NULL, &ch );
504
505 wxRectangle rect;
506 rect.x = 0; rect.y = 0;
507 rect.width = 10000; rect.height = ch;
508
509 PrepareDC( dc );
510 if (p->m_children.Number() == 1)
511 {
512 rect.y = dc.LogicalToDeviceY( p->m_y );
513 }
514 else
515 {
516 wxNode *node = p->m_children.Member( new_child )->Previous();
517 wxGenericTreeItem* last_child = (wxGenericTreeItem*)node->Data();
518 rect.y = dc.LogicalToDeviceY( last_child->m_y );
519 };
520
521 long doX = 0;
522 long doY = 0;
523 dc.GetDeviceOrigin( &doX, &doY );
524 rect.height = ch-rect.y-doY;
525
526 AdjustMyScrollbars();
527
528 if (rect.height > 0) Refresh( FALSE, &rect);
529 }
530 else
531 {
532 AdjustMyScrollbars();
533
534 Refresh();
535 };
536
537 info.m_mask = oldMask;
538 return ret;
539};
540
541bool wxTreeCtrl::ExpandItem( const long item, const int action )
542{
543 wxGenericTreeItem *i = FindItem( item );
544 if (!i) return FALSE;
545 switch (action)
546 {
547 case wxTREE_EXPAND_EXPAND:
548 {
549 i->SendExpand( this );
550 break;
551 };
552 case wxTREE_EXPAND_COLLAPSE_RESET:
553 case wxTREE_EXPAND_COLLAPSE:
554 {
555 wxNode *node = i->m_children.First();
556 while (node)
557 {
558 wxGenericTreeItem *child = (wxGenericTreeItem*)node->Data();
559 child->SendDelete( this );
560 delete node;
561 node = i->m_children.First();
562 };
563
564 int cw = 0;
565 int ch = 0;
566 GetClientSize( &cw, &ch );
567 wxRect rect;
568 rect.x = 0;
569 rect.width = cw;
570 wxClientDC dc(this);
571 PrepareDC(dc);
572 rect.y = dc.LogicalToDeviceY( i->m_y );
573
574 long doY = 0;
575 dc.GetDeviceOrigin( NULL, &doY );
576 rect.height = ch-rect.y-doY;
577 Refresh( TRUE, &rect );
578
579 AdjustMyScrollbars();
580 break;
581 };
582 case wxTREE_EXPAND_TOGGLE:
583 {
584 if (i->HasPlus())
585 ExpandItem( item, wxTREE_EXPAND_EXPAND );
586 else
587 ExpandItem( item, wxTREE_EXPAND_COLLAPSE );
588 break;
589 };
590 };
591 return TRUE;
592};
593
594bool wxTreeCtrl::DeleteAllItems(void)
595{
596 delete m_anchor;
597 m_anchor = NULL;
598 Refresh();
599 return TRUE;
600};
601
602bool wxTreeCtrl::GetItem( wxTreeItem &info ) const
603{
604 wxGenericTreeItem *i = FindItem( info.m_itemId );
605 if (!i) return FALSE;
606 i->GetItem( info );
607 return TRUE;
608};
609
610long wxTreeCtrl::GetItemData( const long item ) const
611{
612 wxGenericTreeItem *i = FindItem( item );
613 if (!i) return 0;
614 return i->m_data;
615};
616
617wxString wxTreeCtrl::GetItemText( const long item ) const
618{
619 wxGenericTreeItem *i = FindItem( item );
620 if (!i) return "";
621 return i->m_text;
622};
623
624long wxTreeCtrl::GetParent( const long item ) const
625{
626 wxGenericTreeItem *i = FindItem( item );
627 if (!i) return -1;
628 i = i->m_parent;
629 if (!i) return -1;
630 return i->m_parent->m_itemId;
631};
632
633long wxTreeCtrl::GetRootItem(void) const
634{
635 if (m_anchor) return m_anchor->m_itemId;
636 return -1;
637};
638
639long wxTreeCtrl::GetSelection(void) const
640{
641 return 0;
642};
643
644bool wxTreeCtrl::SelectItem( const long WXUNUSED(item) ) const
645{
646 return FALSE;
647};
648
649bool wxTreeCtrl::ItemHasChildren( const long item ) const
650{
651 wxGenericTreeItem *i = FindItem( item );
652 if (!i) return FALSE;
653 return i->m_hasChildren;
654};
655
656void wxTreeCtrl::SetIndent( const int indent )
657{
658 m_indent = indent;
659 Refresh();
660};
661
662int wxTreeCtrl::GetIndent(void) const
663{
664 return m_indent;
665};
666
667bool wxTreeCtrl::SetItem( wxTreeItem &info )
668{
669 wxGenericTreeItem *i = FindItem( info.m_itemId );
670 if (!i) return FALSE;
671 wxClientDC dc(this);
672 i->SetItem( info, &dc );
673 return TRUE;
674};
675
676bool wxTreeCtrl::SetItemData( const long item, const long data )
677{
678 wxGenericTreeItem *i = FindItem( item );
679 if (!i) return FALSE;
680 i->m_data = data;
681 return TRUE;
682};
683
684bool wxTreeCtrl::SetItemText( const long item, const wxString &text )
685{
686 wxGenericTreeItem *i = FindItem( item );
687 if (!i) return FALSE;
688 wxClientDC dc(this);
689 i->SetText( text, &dc );
690 return TRUE;
691};
692
693long wxTreeCtrl::HitTest( const wxPoint& point, int &flags )
694{
695 flags = 0;
696 if (!m_anchor) return -1;
697 return m_anchor->HitTest( point, flags );
698};
699
700void wxTreeCtrl::AdjustMyScrollbars(void)
701{
702 if (m_anchor)
703 {
704 int x = 0;
705 int y = 0;
706 m_anchor->GetSize( x, y );
707 y += 2*m_lineHeight;
708 int x_pos = GetScrollPos( wxHORIZONTAL );
709 int y_pos = GetScrollPos( wxVERTICAL );
710 SetScrollbars( 10, 10, x/10, y/10, x_pos, y_pos );
711 }
712 else
713 {
714 SetScrollbars( 0, 0, 0, 0 );
715 };
716};
717
718void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxPaintDC &dc, int level, int &y )
719{
720 int horizX = level*m_indent+10;
721 int oldY = y;
722 wxNode *node = item->m_children.First();
723 while (node)
724 {
725 wxGenericTreeItem *child = (wxGenericTreeItem *)node->Data();
726 dc.SetPen( m_dottedPen );
727
728 child->SetCross( horizX+15, y );
729
730 if (!node->Next())
731 {
732 if (level != 0) oldY -= (m_lineHeight-5);
733 dc.DrawLine( horizX, oldY, horizX, y );
734 };
735
736 child->m_x = horizX+33;
737 child->m_y = y-m_lineHeight/3;
738 child->m_height = m_lineHeight;
739
740 if (IsExposed( 0, child->m_y-2, 10000, m_lineHeight+4 ))
741 {
742 int startX,endX;
743 if ((node->Previous()) || (level != 0))
744 startX = horizX; else startX = horizX-10;
745 if (child->HasChildren())
746 endX = horizX+10; else endX = horizX+30;
747 dc.DrawLine( startX, y, endX, y );
748
749 if (child->HasChildren())
750 {
751 dc.DrawLine( horizX+20, y, horizX+30, y );
752 dc.SetPen( *wxGREY_PEN );
753 dc.DrawRectangle( horizX+10, y-4, 11, 9 );
754 dc.SetPen( *wxBLACK_PEN );
755 dc.DrawLine( horizX+13, y, horizX+17, y );
756 if (child->HasPlus())
757 dc.DrawLine( horizX+15, y-2, horizX+15, y+2 );
758 };
759
760 if (child->HasHilight())
761 {
762 dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
763 dc.SetBrush( *m_hilightBrush );
764 if (m_hasFocus)
765 dc.SetPen( wxBLACK_PEN );
766 else
767 dc.SetPen( wxTRANSPARENT_PEN );
768 long tw = 0;
769 long th = 0;
770 dc.GetTextExtent( child->m_text, &tw, &th );
771 dc.DrawRectangle( child->m_x-2, child->m_y-2, tw+4, th+4 );
772 };
773
774 dc.DrawText( child->m_text, child->m_x, child->m_y );
775
776 if (child->HasHilight())
777 {
778 dc.SetTextForeground( *wxBLACK );
779 dc.SetBrush( *wxWHITE_BRUSH );
780 dc.SetPen( *wxBLACK_PEN );
781 };
782
783 };
784
785 y += m_lineHeight;
786 if (child->NumberOfVisibleChildren() > 0)
787 PaintLevel( child, dc, level+1, y );
788 node = node->Next();
789 };
790};
791
792void wxTreeCtrl::OnPaint( const wxPaintEvent &WXUNUSED(event) )
793{
794 if (!m_anchor) return;
795
796 if (!m_dc)
797 {
798 m_dc = new wxPaintDC(this);
799 PrepareDC( *m_dc );
800 };
801
802 m_dc->SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
803
804 m_dc->SetPen( m_dottedPen );
805 m_lineHeight = (int)(m_dc->GetCharHeight() + 4);
806
807 int y = m_lineHeight / 2 + 2;
808 PaintLevel( m_anchor, *m_dc, 0, y );
809};
810
811void wxTreeCtrl::OnSetFocus( const wxFocusEvent &WXUNUSED(event) )
812{
813 m_hasFocus = TRUE;
814 if (m_current) RefreshLine( m_current );
815};
816
817void wxTreeCtrl::OnKillFocus( const wxFocusEvent &WXUNUSED(event) )
818{
819 m_hasFocus = FALSE;
820 if (m_current) RefreshLine( m_current );
821};
822
823void wxTreeCtrl::OnChar( wxKeyEvent &event )
824{
825 event.Skip();
826};
827
828void wxTreeCtrl::OnMouse( const wxMouseEvent &event )
829{
830 if (!event.LeftDown() &&
831 !event.LeftDClick()) return;
832
833 wxClientDC dc(this);
834 PrepareDC(dc);
835 long x = dc.DeviceToLogicalX( (long)event.GetX() );
836 long y = dc.DeviceToLogicalY( (long)event.GetY() );
837
838 int flag = 0;
839 long id = HitTest( wxPoint(x,y), flag );
840 if (id == -1) return;
841 wxGenericTreeItem *item = FindItem( id );
842
843 if (!item) return;
844 if ((flag != wxTREE_HITTEST_ONITEMBUTTON) &&
845 (flag != wxTREE_HITTEST_ONITEMLABEL)) return;
846
847 if (m_current != item)
848 {
849 if (m_current)
850 {
851 m_current->SetHilight( FALSE );
852 RefreshLine( m_current );
853 };
854 m_current = item;
855 m_current->SetHilight( TRUE );
856 RefreshLine( m_current );
857 m_current->SendSelected( this );
858 };
859
860 if (event.LeftDClick()) m_current->SendKeyDown( this );
861
862 if (flag == wxTREE_HITTEST_ONITEMBUTTON)
863 {
864 ExpandItem( item->m_itemId, wxTREE_EXPAND_TOGGLE );
865 return;
866 };
867};
868
869void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxPaintDC &dc, int level, int &y )
870{
871 int horizX = level*m_indent+10;
872 wxNode *node = item->m_children.First();
873 while (node)
874 {
875 wxGenericTreeItem *child = (wxGenericTreeItem *)node->Data();
876 dc.SetPen( m_dottedPen );
877
878 int startX,endX;
879 if ((node->Previous()) || (level != 0))
880 startX = horizX; else startX = horizX-10;
881 if (child->HasChildren())
882 endX = horizX+10; else endX = horizX+30;
883
884 child->m_x = horizX+33;
885 child->m_y = y-m_lineHeight/3-2;
886 child->m_height = m_lineHeight;
887
888 y += m_lineHeight;
889 if (child->NumberOfVisibleChildren() > 0)
890 CalculateLevel( child, dc, level+1, y );
891
892 node = node->Next();
893 };
894};
895
896void wxTreeCtrl::CalculatePositions(void)
897{
898 if (!m_anchor) return;
899
900 wxClientDC dc(this);
901 PrepareDC( dc );
902
903 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
904
905 dc.SetPen( m_dottedPen );
906 m_lineHeight = (int)(dc.GetCharHeight() + 4);
907
908 int y = m_lineHeight / 2 + 2;
909 CalculateLevel( m_anchor, dc, 0, y );
910};
911
912wxGenericTreeItem *wxTreeCtrl::FindItem( long itemId ) const
913{
914 if (!m_anchor) return NULL;
915 return m_anchor->FindItem( itemId );
916};
917
918void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
919{
920 if (!item) return;
921 wxClientDC dc(this);
922 PrepareDC( dc );
923 wxRect rect;
924 rect.x = dc.LogicalToDeviceX( item->m_x-2 );
925 rect.y = dc.LogicalToDeviceY( item->m_y-2 );
926 rect.width = 1000;
927 rect.height = dc.GetCharHeight()+4;
928 Refresh( TRUE, &rect );
929};
930
931
932