]> git.saurik.com Git - wxWidgets.git/blame - src/generic/treectrl.cpp
fix for precompiled headers
[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
389cdc7a 6// Id: $Id$
c801d85f 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"
389cdc7a 17#include "wx/log.h"
f4e325b3 18#include <wx/intl.h>
c801d85f
KB
19
20//-----------------------------------------------------------------------------
21// wxTreeItem
22//-----------------------------------------------------------------------------
23
24IMPLEMENT_DYNAMIC_CLASS(wxTreeItem, wxObject)
25
74bedbeb 26wxTreeItem::wxTreeItem()
c801d85f
KB
27{
28 m_mask = 0;
29 m_itemId = 0;
30 m_state = 0;
31 m_stateMask = 0;
32 m_image = -1;
33 m_selectedImage = -1;
34 m_children = 0;
35 m_data = 0;
edaa81ae 36}
c801d85f
KB
37
38//-----------------------------------------------------------------------------
39// wxTreeEvent
40//-----------------------------------------------------------------------------
41
42IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent,wxCommandEvent)
43
7798a18e 44wxTreeEvent::wxTreeEvent( wxEventType commandType, int id ) :
c801d85f
KB
45 wxCommandEvent( commandType, id )
46{
47 m_code = 0;
48 m_oldItem = 0;
edaa81ae 49}
c801d85f
KB
50
51//-----------------------------------------------------------------------------
52// wxGenericTreeItem
53//-----------------------------------------------------------------------------
54
55IMPLEMENT_DYNAMIC_CLASS(wxGenericTreeItem,wxObject)
56
57wxGenericTreeItem::wxGenericTreeItem( wxGenericTreeItem *parent )
58{
59 Reset();
60 m_parent = parent;
61 m_hasHilight = FALSE;
edaa81ae 62}
c801d85f
KB
63
64wxGenericTreeItem::wxGenericTreeItem( wxGenericTreeItem *parent, const wxTreeItem& item, wxDC *dc )
65{
66 Reset();
67 SetItem( item, dc );
68 m_parent = parent;
69 m_hasHilight = FALSE;
edaa81ae 70}
c801d85f
KB
71
72void wxGenericTreeItem::SetItem( const wxTreeItem &item, wxDC *dc )
73{
29d87bba 74 if ((item.m_mask & wxTREE_MASK_HANDLE) == wxTREE_MASK_HANDLE)
c801d85f
KB
75 m_itemId = item.m_itemId;
76 if ((item.m_mask & wxTREE_MASK_STATE) == wxTREE_MASK_STATE)
77 m_state = item.m_state;
78 if ((item.m_mask & wxTREE_MASK_TEXT) == wxTREE_MASK_TEXT)
79 m_text = item.m_text;
80 if ((item.m_mask & wxTREE_MASK_IMAGE) == wxTREE_MASK_IMAGE)
81 m_image = item.m_image;
82 if ((item.m_mask & wxTREE_MASK_SELECTED_IMAGE) == wxTREE_MASK_SELECTED_IMAGE)
83 m_selectedImage = item.m_selectedImage;
84 if ((item.m_mask & wxTREE_MASK_CHILDREN) == wxTREE_MASK_CHILDREN)
85 m_hasChildren = (item.m_children > 0);
86 if ((item.m_mask & wxTREE_MASK_DATA) == wxTREE_MASK_DATA)
87 m_data = item.m_data;
88 long lw = 0;
89 long lh = 0;
90 dc->GetTextExtent( m_text, &lw, &lh );
91 m_width = lw;
92 m_height = lh;
edaa81ae 93}
c801d85f
KB
94
95void wxGenericTreeItem::SetText( const wxString &text, wxDC *dc )
96{
97 m_text = text;
98 long lw = 0;
99 long lh = 0;
100 dc->GetTextExtent( m_text, &lw, &lh );
101 m_width = lw;
102 m_height = lh;
edaa81ae 103}
c801d85f 104
74bedbeb 105void wxGenericTreeItem::Reset()
c801d85f
KB
106{
107 m_itemId = -1;
108 m_state = 0;
109 m_text = "";
110 m_image = -1;
111 m_selectedImage = -1;
112// m_children = 0;
113 m_hasChildren = FALSE;
114 m_data = 0;
115 m_x = 0;
116 m_y = 0;
117 m_height = 0;
118 m_width = 0;
119 m_xCross = 0;
120 m_yCross = 0;
121 m_level = 0;
122 m_children.DeleteContents( TRUE );
74bedbeb 123 m_isCollapsed = TRUE;
c67daf87 124 m_parent = (wxGenericTreeItem *) NULL;
edaa81ae 125}
c801d85f
KB
126
127void wxGenericTreeItem::GetItem( wxTreeItem &item ) const
128{
129 if ((item.m_mask & wxTREE_MASK_STATE) == wxTREE_MASK_STATE)
130 item.m_state = m_state;
131 if ((item.m_mask & wxTREE_MASK_TEXT) == wxTREE_MASK_TEXT)
132 item.m_text = m_text;
133 if ((item.m_mask & wxTREE_MASK_IMAGE) == wxTREE_MASK_IMAGE)
134 item.m_image = m_image;
135 if ((item.m_mask & wxTREE_MASK_SELECTED_IMAGE) == wxTREE_MASK_SELECTED_IMAGE)
136 item.m_selectedImage = m_selectedImage;
137 if ((item.m_mask & wxTREE_MASK_CHILDREN) == wxTREE_MASK_CHILDREN)
138 item.m_children = (int)m_hasChildren;
139 if ((item.m_mask & wxTREE_MASK_DATA) == wxTREE_MASK_DATA)
140 item.m_data = m_data;
edaa81ae 141}
c801d85f 142
74bedbeb 143bool wxGenericTreeItem::HasChildren()
c801d85f
KB
144{
145 return m_hasChildren;
edaa81ae 146}
c801d85f 147
74bedbeb 148bool wxGenericTreeItem::HasPlus()
c801d85f 149{
74bedbeb
VZ
150 if ( !HasChildren() )
151 return FALSE;
152
153 return !IsExpanded();
edaa81ae 154}
c801d85f 155
74bedbeb 156int wxGenericTreeItem::NumberOfVisibleDescendents()
c801d85f
KB
157{
158 int ret = m_children.Number();
159 wxNode *node = m_children.First();
160 while (node)
161 {
162 wxGenericTreeItem *item = (wxGenericTreeItem*)node->Data();
163 ret += item->NumberOfVisibleDescendents();
164 node = node->Next();
edaa81ae 165 }
c801d85f 166 return ret;
edaa81ae 167}
c801d85f 168
74bedbeb 169int wxGenericTreeItem::NumberOfVisibleChildren()
c801d85f 170{
74bedbeb 171 return m_isCollapsed ? 0 : m_children.Number();
edaa81ae 172}
c801d85f
KB
173
174wxGenericTreeItem *wxGenericTreeItem::FindItem( long itemId ) const
175{
176 if (m_itemId == itemId) return (wxGenericTreeItem*)(this);
177 wxNode *node = m_children.First();
178 while (node)
179 {
180 wxGenericTreeItem *item = (wxGenericTreeItem*)node->Data();
181 wxGenericTreeItem *res = item->FindItem( itemId );
182 if (res) return (wxGenericTreeItem*)(res);
183 node = node->Next();
edaa81ae 184 }
c67daf87 185 return (wxGenericTreeItem *) NULL;
edaa81ae 186}
c801d85f
KB
187
188void wxGenericTreeItem::AddChild( wxGenericTreeItem *child )
189{
190 m_children.Append( child );
edaa81ae 191}
c801d85f
KB
192
193void wxGenericTreeItem::SetCross( int x, int y )
194{
195 m_xCross = x;
196 m_yCross = y;
edaa81ae 197}
c801d85f
KB
198
199void wxGenericTreeItem::GetSize( int &x, int &y )
200{
201 if (y < m_y + 10) y = m_y +10;
202 int width = m_x + m_width;
203 if (width > x) x = width;
204 wxNode *node = m_children.First();
205 while (node)
206 {
207 wxGenericTreeItem *item = (wxGenericTreeItem*)node->Data();
208 item->GetSize( x, y );
209 node = node->Next();
edaa81ae
RR
210 }
211}
c801d85f
KB
212
213long wxGenericTreeItem::HitTest( const wxPoint& point, int &flags )
214{
4c681997 215 if ((point.y > m_y) && (point.y < m_y+m_height))
c801d85f
KB
216 {
217 if ((point.x > m_xCross-5) &&
218 (point.x < m_xCross+5) &&
219 (point.y > m_yCross-5) &&
220 (point.y < m_yCross+5) &&
29d87bba 221 (m_hasChildren))
c801d85f
KB
222 {
223 flags = wxTREE_HITTEST_ONITEMBUTTON;
224 return m_itemId;
edaa81ae 225 }
c801d85f
KB
226 if ((point.x > m_x) && (point.x < m_x+m_width))
227 {
228 flags = wxTREE_HITTEST_ONITEMLABEL;
229 return m_itemId;
edaa81ae 230 }
c801d85f
KB
231 if (point.x > m_x)
232 {
233 flags = wxTREE_HITTEST_ONITEMRIGHT;
234 return m_itemId;
edaa81ae 235 }
c801d85f
KB
236 flags = wxTREE_HITTEST_ONITEMINDENT;
237 return m_itemId;
238 }
239 else
240 {
e2414cbe 241 if (!m_isCollapsed)
c801d85f 242 {
e2414cbe
RR
243 wxNode *node = m_children.First();
244 while (node)
245 {
246 wxGenericTreeItem *child = (wxGenericTreeItem*)node->Data();
247 long res = child->HitTest( point, flags );
248 if (res != -1) return res;
249 node = node->Next();
edaa81ae
RR
250 }
251 }
252 }
c801d85f 253 return -1;
edaa81ae 254}
c801d85f
KB
255
256void wxGenericTreeItem::PrepareEvent( wxTreeEvent &event )
257{
258 event.m_item.m_itemId = m_itemId;
259 event.m_item.m_state = m_state;
260 event.m_item.m_text = m_text;
261 event.m_item.m_image = m_image;
262 event.m_item.m_selectedImage = m_selectedImage;
263 event.m_item.m_children = (int)m_hasChildren;
264 event.m_item.m_data = m_data;
265 event.m_oldItem = 0;
266 event.m_code = 0;
267 event.m_pointDrag.x = 0;
268 event.m_pointDrag.y = 0;
edaa81ae 269}
c801d85f
KB
270
271void wxGenericTreeItem::SendKeyDown( wxWindow *target )
272{
273 wxTreeEvent event( wxEVT_COMMAND_TREE_KEY_DOWN, target->GetId() );
274 PrepareEvent( event );
275 event.SetEventObject( target );
276 target->ProcessEvent( event );
edaa81ae 277}
c801d85f
KB
278
279void wxGenericTreeItem::SendSelected( wxWindow *target )
280{
281 wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGED, target->GetId() );
282 PrepareEvent( event );
283 event.SetEventObject( target );
284 target->ProcessEvent( event );
edaa81ae 285}
c801d85f
KB
286
287void wxGenericTreeItem::SendDelete( wxWindow *target )
288{
74bedbeb 289 wxTreeEvent event( wxEVT_COMMAND_TREE_DELETE_ITEM, target->GetId() );
c801d85f
KB
290 PrepareEvent( event );
291 event.SetEventObject( target );
292 target->ProcessEvent( event );
edaa81ae 293}
c801d85f
KB
294
295void wxGenericTreeItem::SendExpand( wxWindow *target )
296{
74bedbeb
VZ
297 m_isCollapsed = FALSE;
298
299 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_EXPANDING, target->GetId() );
74bedbeb
VZ
300 event.SetEventObject( target );
301 PrepareEvent( event );
302 target->ProcessEvent( event );
303
304 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_EXPANDED);
c801d85f 305 PrepareEvent( event );
74bedbeb 306 target->ProcessEvent( event );
edaa81ae 307}
74bedbeb
VZ
308
309void wxGenericTreeItem::SendCollapse( wxWindow *target )
310{
311 m_isCollapsed = TRUE;
312
4c681997 313 wxTreeEvent event( wxEVT_COMMAND_TREE_ITEM_COLLAPSING, target->GetId() );
c801d85f 314 event.SetEventObject( target );
74bedbeb
VZ
315 PrepareEvent( event );
316 target->ProcessEvent( event );
317
4c681997 318 event.SetEventType(wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
74bedbeb 319 PrepareEvent( event );
c801d85f 320 target->ProcessEvent( event );
edaa81ae 321}
c801d85f
KB
322
323void wxGenericTreeItem::SetHilight( bool set )
324{
325 m_hasHilight = set;
edaa81ae 326}
c801d85f 327
74bedbeb 328bool wxGenericTreeItem::HasHilight()
c801d85f
KB
329{
330 return m_hasHilight;
edaa81ae 331}
c801d85f
KB
332
333//-----------------------------------------------------------------------------
334// wxTreeCtrl
335//-----------------------------------------------------------------------------
336
4c681997 337IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl,wxScrolledWindow)
c801d85f
KB
338
339BEGIN_EVENT_TABLE(wxTreeCtrl,wxScrolledWindow)
340 EVT_PAINT (wxTreeCtrl::OnPaint)
341 EVT_MOUSE_EVENTS (wxTreeCtrl::OnMouse)
342 EVT_CHAR (wxTreeCtrl::OnChar)
343 EVT_SET_FOCUS (wxTreeCtrl::OnSetFocus)
344 EVT_KILL_FOCUS (wxTreeCtrl::OnKillFocus)
345END_EVENT_TABLE()
346
74bedbeb 347wxTreeCtrl::wxTreeCtrl()
c801d85f 348{
c67daf87
UR
349 m_current = (wxGenericTreeItem *) NULL;
350 m_anchor = (wxGenericTreeItem *) NULL;
c801d85f
KB
351 m_hasFocus = FALSE;
352 m_xScroll = 0;
353 m_yScroll = 0;
354 m_lastId = 0;
355 m_lineHeight = 10;
356 m_indent = 15;
357 m_isCreated = FALSE;
c801d85f 358 m_hilightBrush = new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), wxSOLID );
c67daf87
UR
359 m_imageList = (wxImageList *) NULL;
360 m_smallImageList = (wxImageList *) NULL;
edaa81ae 361}
c801d85f 362
debe6624 363wxTreeCtrl::wxTreeCtrl(wxWindow *parent, wxWindowID id,
4c681997 364 const wxPoint& pos, const wxSize& size,
74bedbeb 365 long style, const wxString& name )
c801d85f 366{
c67daf87
UR
367 m_current = (wxGenericTreeItem *) NULL;
368 m_anchor = (wxGenericTreeItem *) NULL;
c801d85f
KB
369 m_hasFocus = FALSE;
370 m_xScroll = 0;
371 m_yScroll = 0;
372 m_lastId = 0;
373 m_lineHeight = 10;
374 m_indent = 15;
375 m_isCreated = FALSE;
c801d85f 376 m_hilightBrush = new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), wxSOLID );
c67daf87
UR
377 m_imageList = (wxImageList *) NULL;
378 m_smallImageList = (wxImageList *) NULL;
c801d85f 379 Create( parent, id, pos, size, style, name );
edaa81ae 380}
c801d85f 381
74bedbeb 382wxTreeCtrl::~wxTreeCtrl()
c801d85f 383{
e55ad60e
RR
384 if (m_hilightBrush) delete m_hilightBrush;
385 if (m_anchor) delete m_anchor;
edaa81ae 386}
c801d85f 387
debe6624 388bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id,
4c681997
RR
389 const wxPoint& pos, const wxSize& size,
390 long style, const wxString& name )
c801d85f
KB
391{
392 wxScrolledWindow::Create( parent, id, pos, size, style, name );
393 SetBackgroundColour( *wxWHITE );
394 m_dottedPen = wxPen( *wxBLACK, 0, 0 );
395 return TRUE;
edaa81ae 396}
c801d85f 397
74bedbeb 398int wxTreeCtrl::GetCount() const
c801d85f
KB
399{
400 if (!m_anchor) return 0;
401 return m_anchor->NumberOfVisibleDescendents();
edaa81ae 402}
c801d85f 403
74bedbeb
VZ
404long wxTreeCtrl::InsertItem( long parent, const wxString& label, int image,
405 int selImage, long WXUNUSED(insertAfter) )
c801d85f 406{
c67daf87 407 wxGenericTreeItem *p = (wxGenericTreeItem *) NULL;
c801d85f
KB
408 if (parent == 0)
409 {
410 if (m_anchor) return -1;
411 }
412 else
413 {
414 p = FindItem( parent );
415 if (!p) return -1;
edaa81ae 416 }
c801d85f
KB
417 wxTreeItem item;
418 m_lastId++;
419 item.m_mask = wxTREE_MASK_HANDLE;
420 item.m_itemId = m_lastId;
421 if (!label.IsNull() || (label == ""))
422 {
423 item.m_text = label;
424 item.m_mask |= wxTREE_MASK_TEXT;
edaa81ae 425 }
c801d85f
KB
426 if (image >= 0)
427 {
428 item.m_image = image;
429 item.m_mask |= wxTREE_MASK_IMAGE;
edaa81ae 430 }
c801d85f
KB
431 if (selImage >= 0)
432 {
433 item.m_selectedImage = selImage;
434 item.m_mask |= wxTREE_MASK_SELECTED_IMAGE;
edaa81ae 435 }
29d87bba 436
c801d85f
KB
437 wxClientDC dc(this);
438 wxGenericTreeItem *new_child = new wxGenericTreeItem( p, item, &dc );
29d87bba 439 if (p)
c801d85f
KB
440 p->AddChild( new_child );
441 else
442 m_anchor = new_child;
29d87bba 443
c801d85f
KB
444 if (p)
445 {
446 CalculatePositions();
29d87bba 447
e2414cbe 448 if (!p->HasChildren()) p->m_hasChildren = TRUE;
389cdc7a 449
a4294b78
JS
450 int cw = 0, ch = 0;
451 GetClientSize( &cw, &ch );
29d87bba 452
d4c99d6f
RR
453 PrepareDC( dc );
454
c801d85f 455 wxRectangle rect;
d4c99d6f
RR
456 rect.x = dc.LogicalToDeviceX( 0 );
457 rect.y = 0;
458 rect.width = 10000;
459 rect.height = ch;
29d87bba 460
c801d85f
KB
461 if (p->m_children.Number() == 1)
462 {
463 rect.y = dc.LogicalToDeviceY( p->m_y );
464 }
465 else
466 {
467 wxNode *node = p->m_children.Member( new_child )->Previous();
468 wxGenericTreeItem* last_child = (wxGenericTreeItem*)node->Data();
469 rect.y = dc.LogicalToDeviceY( last_child->m_y );
edaa81ae 470 }
29d87bba 471
c801d85f 472 AdjustMyScrollbars();
29d87bba 473
c801d85f
KB
474 if (rect.height > 0) Refresh( FALSE, &rect);
475 }
476 else
477 {
478 AdjustMyScrollbars();
29d87bba 479
c801d85f 480 Refresh();
edaa81ae 481 }
29d87bba 482
c801d85f 483 return m_lastId;
edaa81ae 484}
c801d85f 485
74bedbeb 486long wxTreeCtrl::InsertItem( long parent, wxTreeItem &info, long WXUNUSED(insertAfter) )
c801d85f
KB
487{
488 int oldMask = info.m_mask;
c67daf87 489 wxGenericTreeItem *p = (wxGenericTreeItem *) NULL;
c801d85f
KB
490 if (parent == 0)
491 {
492 if (m_anchor) return -1;
493 }
494 else
495 {
496 p = FindItem( parent );
497 if (!p)
498 {
499 printf( "TreeItem not found.\n" );
500 return -1;
edaa81ae
RR
501 }
502 }
c801d85f
KB
503 long ret = 0;
504 if ((info.m_mask & wxTREE_MASK_HANDLE) == 0)
505 {
506 m_lastId++;
507 info.m_itemId = m_lastId;
508 info.m_mask |= wxTREE_MASK_HANDLE;
509 ret = m_lastId;
510 }
511 else
512 {
513 ret = info.m_itemId;
edaa81ae 514 }
29d87bba 515
c801d85f
KB
516 wxClientDC dc(this);
517 wxGenericTreeItem *new_child = new wxGenericTreeItem( p, info, &dc );
29d87bba 518 if (p)
c801d85f
KB
519 p->AddChild( new_child );
520 else
521 m_anchor = new_child;
29d87bba 522
c801d85f
KB
523 if (p)
524 {
525 CalculatePositions();
389cdc7a 526
e2414cbe 527 if (!p->HasChildren()) p->m_hasChildren = TRUE;
29d87bba 528
c801d85f 529 int ch = 0;
c67daf87 530 GetClientSize( (int *) NULL, &ch );
29d87bba 531
d4c99d6f
RR
532 PrepareDC( dc );
533
c801d85f 534 wxRectangle rect;
d4c99d6f
RR
535 rect.x = dc.LogicalToDeviceX( 0 );
536 rect.y = 0;
537 rect.width = 10000;
538 rect.height = ch;
29d87bba 539
c801d85f
KB
540 if (p->m_children.Number() == 1)
541 {
542 rect.y = dc.LogicalToDeviceY( p->m_y );
543 }
544 else
545 {
546 wxNode *node = p->m_children.Member( new_child )->Previous();
547 wxGenericTreeItem* last_child = (wxGenericTreeItem*)node->Data();
548 rect.y = dc.LogicalToDeviceY( last_child->m_y );
edaa81ae 549 }
29d87bba 550
c801d85f 551 AdjustMyScrollbars();
29d87bba 552
c801d85f
KB
553 if (rect.height > 0) Refresh( FALSE, &rect);
554 }
555 else
556 {
557 AdjustMyScrollbars();
29d87bba 558
c801d85f 559 Refresh();
edaa81ae 560 }
29d87bba 561
c801d85f
KB
562 info.m_mask = oldMask;
563 return ret;
edaa81ae 564}
c801d85f 565
74bedbeb 566bool wxTreeCtrl::ExpandItem( long item, int action )
c801d85f
KB
567{
568 wxGenericTreeItem *i = FindItem( item );
74bedbeb
VZ
569 if (!i)
570 return FALSE;
571
c801d85f
KB
572 switch (action)
573 {
574 case wxTREE_EXPAND_EXPAND:
575 {
576 i->SendExpand( this );
577 break;
74bedbeb
VZ
578 }
579
c801d85f
KB
580 case wxTREE_EXPAND_COLLAPSE_RESET:
581 case wxTREE_EXPAND_COLLAPSE:
582 {
583 wxNode *node = i->m_children.First();
584 while (node)
585 {
586 wxGenericTreeItem *child = (wxGenericTreeItem*)node->Data();
74bedbeb
VZ
587 if ( child->IsExpanded() )
588 ExpandItem( child->m_itemId, wxTREE_EXPAND_COLLAPSE );
589 node = node->Next();
edaa81ae 590 }
29d87bba 591
e2414cbe 592 CalculatePositions();
389cdc7a 593
74bedbeb 594 i->SendCollapse( this );
c801d85f 595 break;
74bedbeb
VZ
596 }
597
c801d85f
KB
598 case wxTREE_EXPAND_TOGGLE:
599 {
74bedbeb 600 if ( i->IsExpanded() )
c801d85f 601 ExpandItem( item, wxTREE_EXPAND_COLLAPSE );
74bedbeb
VZ
602 else
603 ExpandItem( item, wxTREE_EXPAND_EXPAND );
604 return TRUE;
605 }
edaa81ae 606 }
74bedbeb 607
d4c99d6f
RR
608 wxClientDC dc(this);
609 PrepareDC(dc);
610
74bedbeb
VZ
611 int cw = 0;
612 int ch = 0;
613 GetClientSize( &cw, &ch );
d4c99d6f 614
74bedbeb 615 wxRect rect;
d4c99d6f 616 rect.x = dc.LogicalToDeviceX( 0 );
74bedbeb 617 rect.width = cw;
74bedbeb 618 rect.y = dc.LogicalToDeviceY( i->m_y );
d4c99d6f 619 rect.height = ch;
74bedbeb
VZ
620 Refresh( TRUE, &rect );
621
622 AdjustMyScrollbars();
623
c801d85f 624 return TRUE;
edaa81ae 625}
c801d85f 626
a32dd690
VZ
627void wxTreeCtrl::DeleteItem( long item )
628{
629 wxGenericTreeItem *pItem = FindItem( item );
1a5a8367 630 wxCHECK_RET( pItem != NULL, _("wxTreeCtrl::DeleteItem: no such pItem.") );
389cdc7a 631
a32dd690 632 pItem->m_parent->m_children.DeleteObject(pItem);
389cdc7a 633
4c681997
RR
634 Refresh();
635}
636
637void wxTreeCtrl::DeleteChildren( long item )
638{
639 wxGenericTreeItem *pItem = FindItem( item );
1a5a8367 640 wxCHECK_RET( pItem != NULL, _("wxTreeCtrl::DeleteChildren: no such pItem.") );
389cdc7a 641
4c681997 642 pItem->m_children.Clear();
389cdc7a 643
a32dd690
VZ
644 Refresh();
645}
646
74bedbeb 647bool wxTreeCtrl::DeleteAllItems()
c801d85f
KB
648{
649 delete m_anchor;
c67daf87 650 m_anchor = (wxGenericTreeItem *) NULL;
c801d85f
KB
651 Refresh();
652 return TRUE;
edaa81ae 653}
c801d85f
KB
654
655bool wxTreeCtrl::GetItem( wxTreeItem &info ) const
656{
657 wxGenericTreeItem *i = FindItem( info.m_itemId );
658 if (!i) return FALSE;
659 i->GetItem( info );
660 return TRUE;
edaa81ae 661}
c801d85f 662
74bedbeb 663long wxTreeCtrl::GetItemData( long item ) const
c801d85f
KB
664{
665 wxGenericTreeItem *i = FindItem( item );
666 if (!i) return 0;
667 return i->m_data;
edaa81ae 668}
c801d85f 669
74bedbeb 670wxString wxTreeCtrl::GetItemText( long item ) const
c801d85f
KB
671{
672 wxGenericTreeItem *i = FindItem( item );
673 if (!i) return "";
674 return i->m_text;
edaa81ae 675}
c801d85f 676
74bedbeb
VZ
677int wxTreeCtrl::GetItemImage(long item) const
678{
679 wxGenericTreeItem *i = FindItem( item );
680 return i == 0 ? -1 : i->GetImage();
681}
682
683long wxTreeCtrl::GetParent( long item ) const
c801d85f
KB
684{
685 wxGenericTreeItem *i = FindItem( item );
686 if (!i) return -1;
687 i = i->m_parent;
688 if (!i) return -1;
689 return i->m_parent->m_itemId;
edaa81ae 690}
c801d85f 691
74bedbeb 692long wxTreeCtrl::GetRootItem() const
c801d85f
KB
693{
694 if (m_anchor) return m_anchor->m_itemId;
695 return -1;
edaa81ae
RR
696}
697
698long wxTreeCtrl::GetChild( long item ) const
699{
700 wxGenericTreeItem *i = FindItem( item );
701 if (!i) return -1;
702 if (!i->HasChildren()) return -1;
703 wxNode *node = i->m_children.First();
704 i = (wxGenericTreeItem *)node->Data();
705 return i->m_itemId;
706}
707
708long wxTreeCtrl::GetNextItem( long item, int code ) const
709{
710 switch (code)
711 {
712 case wxTREE_NEXT_CARET: return GetSelection();
713 case wxTREE_NEXT_CHILD: return GetChild( item );
714 case wxTREE_NEXT_ROOT: return GetRootItem();
715 case wxTREE_NEXT_PARENT: return GetParent( item );
716 case wxTREE_NEXT_NEXT:
717 {
718 wxGenericTreeItem *i = FindItem( item );
719 if (!i) return -1;
720 if (i->m_parent)
721 {
722 wxNode *node = i->m_parent->m_children.Member( i );
723 if (!node) return -1;
724 node = node->Next();
725 if (!node) return -1;
726 i = (wxGenericTreeItem *)node->Data();
727 return i->m_itemId;
728 }
729 break;
730 }
731 case wxTREE_NEXT_PREVIOUS:
732 {
733 wxGenericTreeItem *i = FindItem( item );
734 if (!i) return -1;
735 if (i->m_parent)
736 {
737 wxNode *node = i->m_parent->m_children.Member( i );
738 if (!node) return -1;
739 node = node->Previous();
740 if (!node) return -1;
741 i = (wxGenericTreeItem *)node->Data();
742 return i->m_itemId;
743 }
744 break;
745 }
746 }
747 return -1;
748}
c801d85f 749
74bedbeb 750long wxTreeCtrl::GetSelection() const
c801d85f 751{
389cdc7a 752 return m_current ? m_current->GetItemId() : -1;
edaa81ae 753}
c801d85f 754
edaa81ae 755bool wxTreeCtrl::SelectItem( long itemId )
c801d85f 756{
389cdc7a
VZ
757 wxGenericTreeItem *pItem = FindItem(itemId);
758 if ( !pItem ) {
1a5a8367 759 wxLogDebug(_("Can't select an item %d which doesn't exist."), itemId);
389cdc7a
VZ
760
761 return FALSE;
762 }
763
d37a02d0 764 SelectItem(pItem);
389cdc7a
VZ
765
766 return TRUE;
edaa81ae 767}
c801d85f 768
c614ed58 769void wxTreeCtrl::SelectItem(wxGenericTreeItem *item)
389cdc7a
VZ
770{
771 if (m_current != item)
772 {
773 if (m_current)
774 {
775 m_current->SetHilight( FALSE );
776 RefreshLine( m_current );
edaa81ae 777 }
389cdc7a
VZ
778 m_current = item;
779 m_current->SetHilight( TRUE );
780 RefreshLine( m_current );
781
c614ed58 782 m_current->SendSelected( this );
389cdc7a
VZ
783 }
784}
785
74bedbeb 786bool wxTreeCtrl::ItemHasChildren( long item ) const
c801d85f
KB
787{
788 wxGenericTreeItem *i = FindItem( item );
789 if (!i) return FALSE;
790 return i->m_hasChildren;
edaa81ae 791}
c801d85f 792
74bedbeb 793void wxTreeCtrl::SetIndent( int indent )
c801d85f
KB
794{
795 m_indent = indent;
796 Refresh();
edaa81ae 797}
c801d85f 798
74bedbeb 799int wxTreeCtrl::GetIndent() const
c801d85f
KB
800{
801 return m_indent;
edaa81ae 802}
c801d85f
KB
803
804bool wxTreeCtrl::SetItem( wxTreeItem &info )
805{
806 wxGenericTreeItem *i = FindItem( info.m_itemId );
807 if (!i) return FALSE;
808 wxClientDC dc(this);
809 i->SetItem( info, &dc );
74bedbeb 810 Refresh();
c801d85f 811 return TRUE;
edaa81ae 812}
c801d85f 813
74bedbeb 814bool wxTreeCtrl::SetItemData( long item, long data )
c801d85f
KB
815{
816 wxGenericTreeItem *i = FindItem( item );
817 if (!i) return FALSE;
818 i->m_data = data;
819 return TRUE;
edaa81ae 820}
c801d85f 821
74bedbeb 822bool wxTreeCtrl::SetItemText( long item, const wxString &text )
c801d85f
KB
823{
824 wxGenericTreeItem *i = FindItem( item );
825 if (!i) return FALSE;
826 wxClientDC dc(this);
827 i->SetText( text, &dc );
828 return TRUE;
edaa81ae 829}
c801d85f 830
74bedbeb
VZ
831void wxTreeCtrl::SetItemImage(long item, int image, int imageSel) const
832{
833 wxGenericTreeItem *i = FindItem( item );
edaa81ae
RR
834 if ( i != 0 )
835 {
74bedbeb
VZ
836 i->SetImage(image);
837 i->SetSelectedImage(imageSel);
838 }
839}
840
edaa81ae
RR
841int wxTreeCtrl::GetItemState( long item, long stateMask ) const
842{
843 wxGenericTreeItem *i = FindItem( item );
844 if (!i) return 0;
845 int ret = 0;
846
847 // Currently, an item is always selected _and_ focussed in wxGTK
848
849 if (stateMask & wxTREE_STATE_FOCUSED)
850 if (m_current == i)
851 ret += wxTREE_STATE_FOCUSED;
852
853 if (stateMask & wxTREE_STATE_SELECTED)
854 if (m_current == i)
855 ret += wxTREE_STATE_SELECTED;
856
857 if (stateMask & wxTREE_STATE_EXPANDED)
858 if (i->IsExpanded())
859 ret += wxTREE_STATE_EXPANDED;
860
861 return ret;
862}
863
864bool wxTreeCtrl::SetItemState( long item, long state, long stateMask )
865{
866 wxGenericTreeItem *i = FindItem( item );
867 if (!i) return FALSE;
868
869 if (stateMask & wxTREE_STATE_FOCUSED)
870 {
871 if (state & wxTREE_STATE_FOCUSED)
872 SelectItem( i );
873 }
874
875 if (stateMask & wxTREE_STATE_SELECTED)
876 {
877 if (state & wxTREE_STATE_SELECTED)
878 SelectItem( i );
879 }
880
881 if (stateMask & wxTREE_STATE_EXPANDED)
882 {
883 if (state & wxTREE_STATE_EXPANDED)
884 ExpandItem( i->m_itemId, wxTREE_EXPAND_EXPAND );
885 else
886 ExpandItem( i->m_itemId, wxTREE_EXPAND_COLLAPSE );
887 }
888
889 return TRUE;
890}
891
c801d85f
KB
892long wxTreeCtrl::HitTest( const wxPoint& point, int &flags )
893{
894 flags = 0;
895 if (!m_anchor) return -1;
896 return m_anchor->HitTest( point, flags );
edaa81ae 897}
c801d85f 898
e2414cbe
RR
899wxImageList *wxTreeCtrl::GetImageList( int which ) const
900{
901 if (which == wxIMAGE_LIST_NORMAL) return m_imageList;
902 return m_smallImageList;
edaa81ae 903}
e2414cbe
RR
904
905void wxTreeCtrl::SetImageList( wxImageList *imageList, int which )
906{
907 if (which == wxIMAGE_LIST_NORMAL)
908 {
909 if (m_imageList) delete m_imageList;
910 m_imageList = imageList;
911 }
912 else
913 {
914 if (m_smallImageList) delete m_smallImageList;
915 m_smallImageList = imageList;
edaa81ae
RR
916 }
917}
e2414cbe 918
74bedbeb 919void wxTreeCtrl::AdjustMyScrollbars()
c801d85f
KB
920{
921 if (m_anchor)
922 {
923 int x = 0;
924 int y = 0;
925 m_anchor->GetSize( x, y );
926 y += 2*m_lineHeight;
927 int x_pos = GetScrollPos( wxHORIZONTAL );
928 int y_pos = GetScrollPos( wxVERTICAL );
929 SetScrollbars( 10, 10, x/10, y/10, x_pos, y_pos );
930 }
931 else
932 {
933 SetScrollbars( 0, 0, 0, 0 );
edaa81ae
RR
934 }
935}
c801d85f 936
16c1f7f3 937void wxTreeCtrl::PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 938{
4c681997 939 int horizX = level*m_indent;
389cdc7a 940
4c681997
RR
941 item->m_x = horizX+33;
942 item->m_y = y-m_lineHeight/3;
943 item->m_height = m_lineHeight;
389cdc7a 944
4c681997
RR
945 item->SetCross( horizX+15, y );
946
c801d85f 947 int oldY = y;
389cdc7a 948
d4c99d6f
RR
949 int exposed_x = dc.LogicalToDeviceX( 0 );
950 int exposed_y = dc.LogicalToDeviceY( item->m_y-2 );
951
952 if (IsExposed( exposed_x, exposed_y, 1000, m_lineHeight+4 ))
c801d85f 953 {
4c681997
RR
954 int startX = horizX;
955 int endX = horizX + 10;
29d87bba 956
4c681997 957 if (!item->HasChildren()) endX += 20;
d4c99d6f 958
389cdc7a 959 dc.DrawLine( startX, y, endX, y );
29d87bba 960
4c681997 961 if (item->HasChildren())
c801d85f
KB
962 {
963 dc.DrawLine( horizX+20, y, horizX+30, y );
964 dc.SetPen( *wxGREY_PEN );
965 dc.DrawRectangle( horizX+10, y-4, 11, 9 );
966 dc.SetPen( *wxBLACK_PEN );
4c681997
RR
967 dc.DrawLine( horizX+13, y, horizX+18, y );
968 if (item->HasPlus())
969 dc.DrawLine( horizX+15, y-2, horizX+15, y+3 );
edaa81ae 970 }
c801d85f 971
4c681997 972 if (item->HasHilight())
c801d85f 973 {
29d87bba 974 dc.SetTextForeground( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
29d87bba 975 dc.SetBrush( *m_hilightBrush );
29d87bba 976 long tw, th;
4c681997 977 dc.GetTextExtent( item->m_text, &tw, &th );
e2414cbe 978 if (m_hasFocus)
389cdc7a 979 {
e2414cbe
RR
980 dc.SetPen( *wxBLACK_PEN );
981 dc.DrawRectangle( item->m_x-2, item->m_y-2, tw+4, th+4 );
389cdc7a
VZ
982 }
983 else
984 {
985 dc.SetPen( *wxTRANSPARENT_PEN );
e2414cbe 986 dc.DrawRectangle( item->m_x-2, item->m_y-2, tw+4, th+4 );
389cdc7a 987 }
4c681997 988 dc.DrawText( item->m_text, item->m_x, item->m_y );
29d87bba 989
29d87bba 990 dc.SetPen( *wxBLACK_PEN );
29d87bba 991 dc.SetTextForeground( *wxBLACK );
389cdc7a 992 dc.SetBrush( *wxWHITE_BRUSH );
29d87bba
VZ
993 }
994 else
4c681997 995 {
d4c99d6f 996 dc.SetBrush( *wxWHITE_BRUSH );
389cdc7a 997 dc.SetPen( *wxTRANSPARENT_PEN );
4c681997
RR
998 long tw, th;
999 dc.GetTextExtent( item->m_text, &tw, &th );
1000 dc.DrawRectangle( item->m_x-2, item->m_y-2, tw+4, th+4 );
1001 dc.DrawText( item->m_text, item->m_x, item->m_y );
1002 dc.SetPen( *wxBLACK_PEN );
edaa81ae
RR
1003 }
1004 }
4c681997
RR
1005
1006 if (item->NumberOfVisibleChildren() == 0) return;
e2414cbe 1007
389cdc7a
VZ
1008 int semiOldY = y;
1009
4c681997
RR
1010 wxNode *node = item->m_children.First();
1011 while (node)
1012 {
1013 wxGenericTreeItem *child = (wxGenericTreeItem *)node->Data();
29d87bba 1014
c801d85f 1015 y += m_lineHeight;
e2414cbe 1016 semiOldY = y;
389cdc7a 1017
4c681997 1018 PaintLevel( child, dc, level+1, y );
389cdc7a 1019
c801d85f 1020 node = node->Next();
edaa81ae 1021 }
4c681997 1022
e2414cbe 1023 dc.DrawLine( horizX+15, oldY+5, horizX+15, semiOldY );
4c681997 1024}
c801d85f
KB
1025
1026void wxTreeCtrl::OnPaint( const wxPaintEvent &WXUNUSED(event) )
1027{
1028 if (!m_anchor) return;
1029
d4c99d6f
RR
1030 wxPaintDC dc(this);
1031 PrepareDC( dc );
29d87bba 1032
d4c99d6f 1033 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
29d87bba 1034
d4c99d6f
RR
1035 dc.SetPen( m_dottedPen );
1036 m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 1037
c801d85f 1038 int y = m_lineHeight / 2 + 2;
d4c99d6f 1039 PaintLevel( m_anchor, dc, 0, y );
edaa81ae 1040}
c801d85f
KB
1041
1042void wxTreeCtrl::OnSetFocus( const wxFocusEvent &WXUNUSED(event) )
1043{
1044 m_hasFocus = TRUE;
1045 if (m_current) RefreshLine( m_current );
edaa81ae 1046}
c801d85f
KB
1047
1048void wxTreeCtrl::OnKillFocus( const wxFocusEvent &WXUNUSED(event) )
1049{
1050 m_hasFocus = FALSE;
1051 if (m_current) RefreshLine( m_current );
edaa81ae 1052}
c801d85f
KB
1053
1054void wxTreeCtrl::OnChar( wxKeyEvent &event )
1055{
1056 event.Skip();
edaa81ae 1057}
c801d85f
KB
1058
1059void wxTreeCtrl::OnMouse( const wxMouseEvent &event )
1060{
1061 if (!event.LeftDown() &&
1062 !event.LeftDClick()) return;
29d87bba 1063
c801d85f
KB
1064 wxClientDC dc(this);
1065 PrepareDC(dc);
1066 long x = dc.DeviceToLogicalX( (long)event.GetX() );
1067 long y = dc.DeviceToLogicalY( (long)event.GetY() );
29d87bba 1068
c801d85f
KB
1069 int flag = 0;
1070 long id = HitTest( wxPoint(x,y), flag );
29d87bba
VZ
1071 if (id == -1)
1072 return;
c801d85f 1073 wxGenericTreeItem *item = FindItem( id );
29d87bba 1074
c801d85f
KB
1075 if (!item) return;
1076 if ((flag != wxTREE_HITTEST_ONITEMBUTTON) &&
1077 (flag != wxTREE_HITTEST_ONITEMLABEL)) return;
29d87bba 1078
389cdc7a 1079 SelectItem(item);
29d87bba
VZ
1080
1081 if (event.LeftDClick())
1082 m_current->SendKeyDown( this );
1083
c801d85f
KB
1084 if (flag == wxTREE_HITTEST_ONITEMBUTTON)
1085 {
1086 ExpandItem( item->m_itemId, wxTREE_EXPAND_TOGGLE );
1087 return;
edaa81ae
RR
1088 }
1089}
c801d85f 1090
16c1f7f3 1091void wxTreeCtrl::CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y )
c801d85f 1092{
4c681997 1093 int horizX = level*m_indent;
389cdc7a 1094
4c681997
RR
1095 item->m_x = horizX+33;
1096 item->m_y = y-m_lineHeight/3-2;
1097 item->m_height = m_lineHeight;
1098
1099 if (item->NumberOfVisibleChildren() == 0) return;
389cdc7a 1100
c801d85f
KB
1101 wxNode *node = item->m_children.First();
1102 while (node)
1103 {
1104 wxGenericTreeItem *child = (wxGenericTreeItem *)node->Data();
389cdc7a 1105
c801d85f 1106 y += m_lineHeight;
4c681997 1107 CalculateLevel( child, dc, level+1, y );
29d87bba 1108
c801d85f 1109 node = node->Next();
edaa81ae
RR
1110 }
1111}
c801d85f 1112
74bedbeb 1113void wxTreeCtrl::CalculatePositions()
c801d85f 1114{
29d87bba
VZ
1115 if (!m_anchor)
1116 return;
1117
c801d85f
KB
1118 wxClientDC dc(this);
1119 PrepareDC( dc );
29d87bba 1120
c801d85f 1121 dc.SetFont( wxSystemSettings::GetSystemFont( wxSYS_SYSTEM_FONT ) );
29d87bba 1122
c801d85f
KB
1123 dc.SetPen( m_dottedPen );
1124 m_lineHeight = (int)(dc.GetCharHeight() + 4);
29d87bba 1125
c801d85f
KB
1126 int y = m_lineHeight / 2 + 2;
1127 CalculateLevel( m_anchor, dc, 0, y );
edaa81ae 1128}
c801d85f
KB
1129
1130wxGenericTreeItem *wxTreeCtrl::FindItem( long itemId ) const
1131{
c67daf87 1132 if (!m_anchor) return (wxGenericTreeItem *) NULL;
c801d85f 1133 return m_anchor->FindItem( itemId );
edaa81ae 1134}
c801d85f
KB
1135
1136void wxTreeCtrl::RefreshLine( wxGenericTreeItem *item )
1137{
1138 if (!item) return;
1139 wxClientDC dc(this);
1140 PrepareDC( dc );
1141 wxRect rect;
1142 rect.x = dc.LogicalToDeviceX( item->m_x-2 );
1143 rect.y = dc.LogicalToDeviceY( item->m_y-2 );
1144 rect.width = 1000;
d4c99d6f 1145 rect.height = dc.GetCharHeight()+6;
c801d85f 1146 Refresh( TRUE, &rect );
edaa81ae 1147}
c801d85f
KB
1148
1149
1150