]> git.saurik.com Git - wxWidgets.git/blame - src/common/datavcmn.cpp
factor out a whole bunch of duplicated code
[wxWidgets.git] / src / common / datavcmn.cpp
CommitLineData
239eaa41
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/common/datavcmn.cpp
3// Purpose: wxDataViewCtrl base classes and common parts
4// Author: Robert Roebling
5// Created: 2006/02/20
6// RCS-ID: $Id$
7// Copyright: (c) 2006, Robert Roebling
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
8045736e
RR
18#if wxUSE_DATAVIEWCTRL
19
e7445ff8
PC
20#include "wx/dataview.h"
21
52e750fc 22#include "wx/spinctrl.h"
52e750fc 23
f554a14b 24#ifndef WX_PRECOMP
c3c62822
PC
25 #include "wx/dc.h"
26 #include "wx/settings.h"
f554a14b 27 #include "wx/log.h"
e94d0c1e 28 #include "wx/icon.h"
c3c62822 29 #include "wx/crt.h"
f554a14b
WS
30#endif
31
4a2e5ee8 32const wxChar wxDataViewCtrlNameStr[] = wxT("dataviewCtrl");
239eaa41 33
239eaa41 34
d5025dc0
RR
35bool operator == (const wxDataViewItem &left, const wxDataViewItem &right)
36{
37 return (left.GetID() == right.GetID() );
38}
39
c3c62822
PC
40#ifdef __WXDEBUG__
41void wxDataViewItem::Print(const wxString& text) const
42{
43 wxPrintf("item %s: %l\n", text, (long)m_id);
44}
45#endif
d5025dc0 46
f554a14b 47// ---------------------------------------------------------
854cdb09 48// wxDataViewModelNotifier
f554a14b 49// ---------------------------------------------------------
239eaa41 50
8ab03001 51#include "wx/listimpl.cpp"
a76c2f37 52WX_DEFINE_LIST(wxDataViewModelNotifiers)
9d8fe14a 53
854cdb09
RR
54bool wxDataViewModelNotifier::ItemsAdded( const wxDataViewItem &parent, const wxDataViewItemArray &items )
55{
56 size_t count = items.GetCount();
57 size_t i;
58 for (i = 0; i < count; i++)
59 if (!ItemAdded( parent, items[i] )) return false;
b5ec7dd6 60
854cdb09
RR
61 return true;
62}
63
64bool wxDataViewModelNotifier::ItemsDeleted( const wxDataViewItem &parent, const wxDataViewItemArray &items )
65{
66 size_t count = items.GetCount();
67 size_t i;
68 for (i = 0; i < count; i++)
69 if (!ItemDeleted( parent, items[i] )) return false;
b5ec7dd6 70
854cdb09
RR
71 return true;
72}
73
74bool wxDataViewModelNotifier::ItemsChanged( const wxDataViewItemArray &items )
75{
76 size_t count = items.GetCount();
77 size_t i;
78 for (i = 0; i < count; i++)
79 if (!ItemChanged( items[i] )) return false;
b5ec7dd6 80
854cdb09
RR
81 return true;
82}
83
84// ---------------------------------------------------------
85// wxDataViewModel
86// ---------------------------------------------------------
87
e0062c04 88wxDataViewModel::wxDataViewModel()
239eaa41 89{
8f850e28 90 m_notifiers.DeleteContents( true );
239eaa41
RR
91}
92
e0062c04 93bool wxDataViewModel::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
239eaa41 94{
8f850e28
RR
95 bool ret = true;
96
9d8fe14a
RR
97 wxDataViewModelNotifiers::iterator iter;
98 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
8f850e28 99 {
9d8fe14a 100 wxDataViewModelNotifier* notifier = *iter;
e0062c04 101 if (!notifier->ItemAdded( parent, item ))
8f850e28 102 ret = false;
8f850e28 103 }
f554a14b 104
8f850e28 105 return ret;
239eaa41
RR
106}
107
469d3e9b 108bool wxDataViewModel::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
239eaa41 109{
8f850e28
RR
110 bool ret = true;
111
9d8fe14a
RR
112 wxDataViewModelNotifiers::iterator iter;
113 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
8f850e28 114 {
9d8fe14a 115 wxDataViewModelNotifier* notifier = *iter;
469d3e9b 116 if (!notifier->ItemDeleted( parent, item ))
8f850e28 117 ret = false;
8f850e28 118 }
f554a14b 119
8f850e28 120 return ret;
239eaa41
RR
121}
122
e0062c04 123bool wxDataViewModel::ItemChanged( const wxDataViewItem &item )
239eaa41 124{
8f850e28
RR
125 bool ret = true;
126
9d8fe14a
RR
127 wxDataViewModelNotifiers::iterator iter;
128 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
8f850e28 129 {
9d8fe14a 130 wxDataViewModelNotifier* notifier = *iter;
e0062c04 131 if (!notifier->ItemChanged( item ))
8f850e28 132 ret = false;
8f850e28 133 }
f554a14b 134
8f850e28 135 return ret;
239eaa41
RR
136}
137
854cdb09
RR
138bool wxDataViewModel::ItemsAdded( const wxDataViewItem &parent, const wxDataViewItemArray &items )
139{
140 bool ret = true;
141
142 wxDataViewModelNotifiers::iterator iter;
143 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
144 {
145 wxDataViewModelNotifier* notifier = *iter;
146 if (!notifier->ItemsAdded( parent, items ))
147 ret = false;
148 }
149
150 return ret;
151}
152
153bool wxDataViewModel::ItemsDeleted( const wxDataViewItem &parent, const wxDataViewItemArray &items )
154{
155 bool ret = true;
156
157 wxDataViewModelNotifiers::iterator iter;
158 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
159 {
160 wxDataViewModelNotifier* notifier = *iter;
161 if (!notifier->ItemsDeleted( parent, items ))
162 ret = false;
163 }
164
165 return ret;
166}
167
168bool wxDataViewModel::ItemsChanged( const wxDataViewItemArray &items )
169{
170 bool ret = true;
171
172 wxDataViewModelNotifiers::iterator iter;
173 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
174 {
175 wxDataViewModelNotifier* notifier = *iter;
176 if (!notifier->ItemsChanged( items ))
177 ret = false;
178 }
179
180 return ret;
181}
182
e0062c04 183bool wxDataViewModel::ValueChanged( const wxDataViewItem &item, unsigned int col )
239eaa41 184{
8f850e28 185 bool ret = true;
b5d777c7 186
9d8fe14a
RR
187 wxDataViewModelNotifiers::iterator iter;
188 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
b5d777c7 189 {
9d8fe14a 190 wxDataViewModelNotifier* notifier = *iter;
e0062c04 191 if (!notifier->ValueChanged( item, col ))
8f850e28 192 ret = false;
b5d777c7 193 }
f554a14b 194
b5d777c7 195 return ret;
239eaa41
RR
196}
197
e0062c04 198bool wxDataViewModel::Cleared()
4eccd3a1
RR
199{
200 bool ret = true;
201
9d8fe14a
RR
202 wxDataViewModelNotifiers::iterator iter;
203 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
4eccd3a1 204 {
9d8fe14a 205 wxDataViewModelNotifier* notifier = *iter;
8f850e28
RR
206 if (!notifier->Cleared())
207 ret = false;
8f850e28 208 }
f554a14b 209
8f850e28 210 return ret;
239eaa41
RR
211}
212
ef427989
RR
213void wxDataViewModel::Resort()
214{
9d8fe14a
RR
215 wxDataViewModelNotifiers::iterator iter;
216 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
ef427989 217 {
9d8fe14a 218 wxDataViewModelNotifier* notifier = *iter;
ef427989 219 notifier->Resort();
ef427989
RR
220 }
221}
222
e0062c04 223void wxDataViewModel::AddNotifier( wxDataViewModelNotifier *notifier )
239eaa41 224{
9d8fe14a 225 m_notifiers.push_back( notifier );
8f850e28 226 notifier->SetOwner( this );
239eaa41
RR
227}
228
e0062c04 229void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier *notifier )
239eaa41 230{
8f850e28 231 m_notifiers.DeleteObject( notifier );
239eaa41
RR
232}
233
7ee7191c
RR
234int wxDataViewModel::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
235 unsigned int column, bool ascending )
ef427989 236{
cf283a47 237 // sort branches before leaves
ed903e42
RR
238 bool item1_is_container = IsContainer(item1);
239 bool item2_is_container = IsContainer(item2);
900af470 240
ed903e42 241 if (item1_is_container && !item2_is_container)
cf283a47 242 return 1;
ed903e42 243 if (item2_is_container && !item1_is_container)
cf283a47 244 return -1;
900af470 245
ef427989 246 wxVariant value1,value2;
7ee7191c
RR
247 GetValue( value1, item1, column );
248 GetValue( value2, item2, column );
249
250 if (!ascending)
251 {
252 wxVariant temp = value1;
253 value1 = value2;
254 value2 = temp;
255 }
cf283a47 256
ef427989
RR
257 if (value1.GetType() == wxT("string"))
258 {
259 wxString str1 = value1.GetString();
260 wxString str2 = value2.GetString();
0be79c8a 261 int res = str1.Cmp( str2 );
cf283a47
RR
262 if (res) return res;
263 } else
ef427989
RR
264 if (value1.GetType() == wxT("long"))
265 {
266 long l1 = value1.GetLong();
267 long l2 = value2.GetLong();
cf283a47
RR
268 long res = l1-l2;
269 if (res) return res;
270 } else
ef427989
RR
271 if (value1.GetType() == wxT("double"))
272 {
273 double d1 = value1.GetDouble();
274 double d2 = value2.GetDouble();
ef427989 275 if (d1 < d2) return 1;
cf283a47
RR
276 if (d1 > d2) return -1;
277 } else
ef427989
RR
278 if (value1.GetType() == wxT("datetime"))
279 {
280 wxDateTime dt1 = value1.GetDateTime();
281 wxDateTime dt2 = value2.GetDateTime();
ef427989 282 if (dt1.IsEarlierThan(dt2)) return 1;
cf283a47 283 if (dt2.IsEarlierThan(dt1)) return -11;
ef427989
RR
284 }
285
cf283a47 286 // items must be different
900af470
JS
287 unsigned long litem1 = (unsigned long) item1.GetID();
288 unsigned long litem2 = (unsigned long) item2.GetID();
b7fe2261 289
7ee7191c
RR
290 if (!ascending)
291 return litem2-litem2;
cf283a47 292
cf283a47 293 return litem1-litem2;
ef427989
RR
294}
295
c534e696
RR
296// ---------------------------------------------------------
297// wxDataViewIndexListModel
298// ---------------------------------------------------------
299
300wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size )
301{
a3cc79d9 302#ifndef __WXMAC__
2056dede
RR
303 m_useHash = false;
304#else
305 m_useHash = true;
306#endif
517166e6 307
2056dede
RR
308 if (m_useHash)
309 {
310 // IDs are ordered until an item gets deleted or inserted
311 m_ordered = true;
312
313 // build initial index
314 unsigned int i;
315 for (i = 1; i < initial_size+1; i++)
316 m_hash.Add( (void*) i );
317 m_lastIndex = initial_size + 1;
318 }
319 else
320 {
321 m_lastIndex = initial_size-1;
322 }
c534e696
RR
323}
324
325wxDataViewIndexListModel::~wxDataViewIndexListModel()
326{
327}
328
329void wxDataViewIndexListModel::RowPrepended()
330{
2056dede
RR
331 if (m_useHash)
332 {
333 m_ordered = false;
f8cad1a1 334
2056dede
RR
335 unsigned int id = m_lastIndex++;
336 m_hash.Insert( (void*) id, 0 );
337 wxDataViewItem item( (void*) id );
338 ItemAdded( wxDataViewItem(0), item );
339 }
340 else
341 {
342 m_lastIndex++;
343 wxDataViewItem item( (void*) 0 );
344 ItemAdded( wxDataViewItem(0), item );
345 }
c534e696
RR
346}
347
348void wxDataViewIndexListModel::RowInserted( unsigned int before )
349{
2056dede
RR
350 if (m_useHash)
351 {
352 m_ordered = false;
f8cad1a1 353
2056dede
RR
354 unsigned int id = m_lastIndex++;
355 m_hash.Insert( (void*) id, before );
356 wxDataViewItem item( (void*) id );
357 ItemAdded( wxDataViewItem(0), item );
358 }
359 else
360 {
361 m_lastIndex++;
362 wxDataViewItem item( (void*) before );
363 ItemAdded( wxDataViewItem(0), item );
364 }
c534e696
RR
365}
366
367void wxDataViewIndexListModel::RowAppended()
368{
2056dede
RR
369 if (m_useHash)
370 {
371 unsigned int id = m_lastIndex++;
372 m_hash.Add( (void*) id );
373 wxDataViewItem item( (void*) id );
374 ItemAdded( wxDataViewItem(0), item );
375 }
376 else
377 {
378 m_lastIndex++;
379 wxDataViewItem item( (void*) m_lastIndex );
380 ItemAdded( wxDataViewItem(0), item );
381 }
c534e696
RR
382}
383
384void wxDataViewIndexListModel::RowDeleted( unsigned int row )
385{
2056dede
RR
386 if (m_useHash)
387 {
388 wxDataViewItem item( m_hash[row] );
389 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item );
390 m_hash.RemoveAt( row );
391 }
392 else
393 {
394 wxDataViewItem item( (void*) row );
395 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item );
396 m_lastIndex++;
397 }
c534e696
RR
398}
399
400void wxDataViewIndexListModel::RowChanged( unsigned int row )
401{
402 wxDataViewModel::ItemChanged( GetItem(row) );
403}
404
405void wxDataViewIndexListModel::RowValueChanged( unsigned int row, unsigned int col )
406{
407 wxDataViewModel::ValueChanged( GetItem(row), col );
408}
409
410unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem &item ) const
411{
2056dede 412 if (m_useHash)
517166e6 413 {
2056dede
RR
414 if (m_ordered)
415 {
416 unsigned int pos = wxPtrToUInt( item.GetID() );
417 return pos-1;
418 }
517166e6 419
2056dede
RR
420 // assert for not found
421 return (unsigned int) m_hash.Index( item.GetID() );
422 }
423 else
424 {
425 return wxPtrToUInt( item.GetID() );
426 }
c534e696
RR
427}
428
429wxDataViewItem wxDataViewIndexListModel::GetItem( unsigned int row ) const
430{
2056dede
RR
431 if (m_useHash)
432 {
433 wxASSERT( row < m_hash.GetCount() );
434 return wxDataViewItem( m_hash[row] );
435 }
436 else
437 {
438 return wxDataViewItem( (void*) row );
439 }
c534e696
RR
440}
441
517166e6
RR
442bool wxDataViewIndexListModel::HasDefaultCompare() const
443{
444 return !m_ordered;
445}
446
b7fe2261
VZ
447int wxDataViewIndexListModel::Compare(const wxDataViewItem& item1,
448 const wxDataViewItem& item2,
449 unsigned int WXUNUSED(column),
450 bool ascending)
c534e696 451{
2056dede 452 if (m_ordered || !m_useHash)
517166e6 453 {
ec935ba8
PC
454 unsigned int pos1 = wxPtrToUInt(item1.GetID());
455 unsigned int pos2 = wxPtrToUInt(item2.GetID());
517166e6
RR
456
457 if (ascending)
458 return pos1 - pos2;
459 else
460 return pos2 - pos1;
461 }
462
7ee7191c
RR
463 if (ascending)
464 return GetRow(item1) - GetRow(item2);
b7fe2261 465
7ee7191c 466 return GetRow(item2) - GetRow(item1);
c534e696
RR
467}
468
900af470 469void wxDataViewIndexListModel::GetValue( wxVariant &variant,
c534e696
RR
470 const wxDataViewItem &item, unsigned int col ) const
471{
900af470 472 GetValue( variant, GetRow(item), col );
c534e696
RR
473}
474
900af470 475bool wxDataViewIndexListModel::SetValue( const wxVariant &variant,
c534e696
RR
476 const wxDataViewItem &item, unsigned int col )
477{
478 return SetValue( variant, GetRow(item), col );
479}
480
4264606e
RR
481bool wxDataViewIndexListModel::GetAttr( const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr )
482{
483 return GetAttr( GetRow(item), col, attr );
484}
485
900af470 486wxDataViewItem wxDataViewIndexListModel::GetParent( const wxDataViewItem & WXUNUSED(item) ) const
c534e696
RR
487{
488 return wxDataViewItem(0);
489}
490
491bool wxDataViewIndexListModel::IsContainer( const wxDataViewItem &item ) const
492{
493 // only the invisible root item has children
494 if (!item.IsOk())
495 return true;
900af470 496
c534e696
RR
497 return false;
498}
499
74fe973b 500unsigned int wxDataViewIndexListModel::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
c534e696 501{
eff112a6 502 if (!m_useHash)
2056dede
RR
503 return 0; // error
504
74fe973b
RR
505 if (item.IsOk())
506 return 0;
c534e696 507
74fe973b 508 children = m_hash;
b7fe2261 509
74fe973b 510 return m_hash.GetCount();
c534e696
RR
511}
512
89352653
RR
513//-----------------------------------------------------------------------------
514// wxDataViewIconText
515//-----------------------------------------------------------------------------
516
517IMPLEMENT_DYNAMIC_CLASS(wxDataViewIconText,wxObject)
518
d350fbec 519IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxDataViewIconText, WXDLLIMPEXP_ADV)
89352653
RR
520
521bool operator == (const wxDataViewIconText &one, const wxDataViewIconText &two)
522{
523 if (one.GetText() != two.GetText()) return false;
524 if (one.IsSameAs(two)) return false;
525 return true;
526}
527
f554a14b 528// ---------------------------------------------------------
baa9ebc4 529// wxDataViewRendererBase
f554a14b 530// ---------------------------------------------------------
6842a71a 531
baa9ebc4 532IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase, wxObject)
6842a71a 533
900af470 534wxDataViewRendererBase::wxDataViewRendererBase( const wxString &varianttype,
9861f022
RR
535 wxDataViewCellMode WXUNUSED(mode),
536 int WXUNUSED(align) )
6842a71a
RR
537{
538 m_variantType = varianttype;
1e510b1e 539 m_editorCtrl = NULL;
9861f022
RR
540}
541
542const wxDataViewCtrl* wxDataViewRendererBase::GetView() const
543{
544 return wx_const_cast(wxDataViewRendererBase*, this)->GetOwner()->GetOwner();
6842a71a 545}
f554a14b 546
e0062c04 547bool wxDataViewRendererBase::StartEditing( const wxDataViewItem &item, wxRect labelRect )
1e510b1e 548{
e0062c04 549 m_item = item; // remember for later
900af470 550
1e510b1e
RR
551 unsigned int col = GetOwner()->GetModelColumn();
552 wxVariant value;
e0062c04 553 GetOwner()->GetOwner()->GetModel()->GetValue( value, item, col );
900af470 554
1e510b1e 555 m_editorCtrl = CreateEditorCtrl( GetOwner()->GetOwner()->GetMainWindow(), labelRect, value );
900af470
JS
556
557 wxDataViewEditorCtrlEvtHandler *handler =
30715fa1 558 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl, (wxDataViewRenderer*) this );
900af470 559
30715fa1 560 m_editorCtrl->PushEventHandler( handler );
900af470 561
30715fa1
RR
562#if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
563 handler->SetFocusOnIdle();
564#else
1e510b1e 565 m_editorCtrl->SetFocus();
30715fa1
RR
566#endif
567
afebb87b
RR
568 // Now we should send Editing Started event
569 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, GetOwner()->GetOwner()->GetId() );
570 event.SetDataViewColumn( GetOwner() );
571 event.SetModel( GetOwner()->GetOwner()->GetModel() );
572 event.SetItem( item );
afebb87b
RR
573 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event );
574
1e510b1e
RR
575 return true;
576}
577
578void wxDataViewRendererBase::CancelEditing()
579{
2fa73716 580 wxPendingDelete.Append( m_editorCtrl );
900af470 581
1e510b1e 582 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
900af470 583
2fa73716 584 // m_editorCtrl->PopEventHandler( true );
1e510b1e
RR
585}
586
587bool wxDataViewRendererBase::FinishEditing()
588{
1e510b1e
RR
589 wxVariant value;
590 GetValueFromEditorCtrl( m_editorCtrl, value );
591
2fa73716 592 wxPendingDelete.Append( m_editorCtrl );
900af470 593
1e510b1e 594 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
900af470 595
1e510b1e
RR
596 if (!Validate(value))
597 return false;
900af470 598
1e510b1e 599 unsigned int col = GetOwner()->GetModelColumn();
e0062c04
RR
600 GetOwner()->GetOwner()->GetModel()->SetValue( value, m_item, col );
601 GetOwner()->GetOwner()->GetModel()->ValueChanged( m_item, col );
900af470 602
2fa73716 603 // m_editorCtrl->PopEventHandler( true );
b7fe2261 604
afebb87b
RR
605 // Now we should send Editing Done event
606 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, GetOwner()->GetOwner()->GetId() );
607 event.SetDataViewColumn( GetOwner() );
608 event.SetModel( GetOwner()->GetOwner()->GetModel() );
8fdaf7de 609 event.SetItem( m_item );
afebb87b 610 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event );
900af470 611
1e510b1e
RR
612 return true;
613}
614
615//-----------------------------------------------------------------------------
616// wxDataViewEditorCtrlEvtHandler
617//-----------------------------------------------------------------------------
618
619BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler, wxEvtHandler)
620 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar)
621 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus)
30715fa1 622 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle)
1e510b1e
RR
623END_EVENT_TABLE()
624
625wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
626 wxControl *editorCtrl,
627 wxDataViewRenderer *owner )
628{
629 m_owner = owner;
630 m_editorCtrl = editorCtrl;
631
632 m_finished = false;
633}
634
30715fa1
RR
635void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent &event )
636{
637 if (m_focusOnIdle)
638 {
639 m_focusOnIdle = false;
640 if (wxWindow::FindFocus() != m_editorCtrl)
641 m_editorCtrl->SetFocus();
642 }
900af470 643
30715fa1
RR
644 event.Skip();
645}
646
1e510b1e
RR
647void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent &event )
648{
649 switch ( event.m_keyCode )
650 {
651 case WXK_RETURN:
652 m_finished = true;
653 m_owner->FinishEditing();
654 break;
655
656 case WXK_ESCAPE:
657 m_finished = true;
658 m_owner->CancelEditing();
659 break;
660
661 default:
662 event.Skip();
663 }
664}
665
666void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent &event )
667{
668 if (!m_finished)
669 {
670 m_finished = true;
671 m_owner->FinishEditing();
672 }
673
1e510b1e
RR
674 event.Skip();
675}
676
f554a14b 677// ---------------------------------------------------------
fa28826d 678// wxDataViewColumnBase
f554a14b 679// ---------------------------------------------------------
fa28826d
RR
680
681IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase, wxObject)
682
9861f022 683wxDataViewColumnBase::wxDataViewColumnBase(const wxString& WXUNUSED(title),
baa9ebc4 684 wxDataViewRenderer *renderer,
0a71f9e9 685 unsigned int model_column,
008b5a66 686 int WXUNUSED(width),
87f0efe2 687 wxAlignment WXUNUSED(align),
900af470 688 int WXUNUSED(flags))
fa28826d 689{
baa9ebc4 690 m_renderer = renderer;
6842a71a 691 m_model_column = model_column;
6842a71a 692 m_owner = NULL;
baa9ebc4 693 m_renderer->SetOwner( (wxDataViewColumn*) this );
9861f022
RR
694
695 // NOTE: the wxDataViewColumn's ctor must store the width, align, flags
696 // parameters inside the native control!
6842a71a
RR
697}
698
07a84e7b
RR
699wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap& bitmap,
700 wxDataViewRenderer *renderer,
701 unsigned int model_column,
702 int WXUNUSED(width),
87f0efe2 703 wxAlignment WXUNUSED(align),
900af470 704 int WXUNUSED(flags) )
07a84e7b
RR
705{
706 m_renderer = renderer;
707 m_model_column = model_column;
07a84e7b
RR
708 m_bitmap = bitmap;
709 m_owner = NULL;
710 m_renderer->SetOwner( (wxDataViewColumn*) this );
711}
712
6842a71a
RR
713wxDataViewColumnBase::~wxDataViewColumnBase()
714{
baa9ebc4
RR
715 if (m_renderer)
716 delete m_renderer;
fa28826d
RR
717}
718
9861f022 719int wxDataViewColumnBase::GetFlags() const
fa28826d 720{
9861f022 721 int ret = 0;
fa28826d 722
9861f022
RR
723 if (IsSortable())
724 ret |= wxDATAVIEW_COL_SORTABLE;
725 if (IsResizeable())
726 ret |= wxDATAVIEW_COL_RESIZABLE;
727 if (IsHidden())
728 ret |= wxDATAVIEW_COL_HIDDEN;
fa28826d 729
9861f022 730 return ret;
07a84e7b
RR
731}
732
9861f022 733void wxDataViewColumnBase::SetFlags(int flags)
07a84e7b 734{
9861f022
RR
735 SetSortable((flags & wxDATAVIEW_COL_SORTABLE) != 0);
736 SetResizeable((flags & wxDATAVIEW_COL_RESIZABLE) != 0);
737 SetHidden((flags & wxDATAVIEW_COL_HIDDEN) != 0);
07a84e7b
RR
738}
739
f554a14b 740// ---------------------------------------------------------
239eaa41 741// wxDataViewCtrlBase
f554a14b 742// ---------------------------------------------------------
239eaa41
RR
743
744IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl)
745
746wxDataViewCtrlBase::wxDataViewCtrlBase()
747{
748 m_model = NULL;
3b6280be
RR
749 m_expander_column = 0;
750 m_indent = 8;
239eaa41
RR
751}
752
753wxDataViewCtrlBase::~wxDataViewCtrlBase()
754{
87f0efe2
RR
755 if (m_model)
756 {
757 m_model->DecRef();
758 m_model = NULL;
759 }
239eaa41
RR
760}
761
e0062c04 762bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel *model )
239eaa41 763{
1821abd1
RR
764 if (m_model)
765 {
87f0efe2 766 m_model->DecRef(); // discard old model, if any
1821abd1 767 }
87f0efe2
RR
768
769 // add our own reference to the new model:
239eaa41 770 m_model = model;
1821abd1
RR
771 if (m_model)
772 {
900af470 773 m_model->IncRef();
1821abd1 774 }
f554a14b 775
239eaa41
RR
776 return true;
777}
778
e0062c04 779wxDataViewModel* wxDataViewCtrlBase::GetModel()
239eaa41
RR
780{
781 return m_model;
782}
783
a75124d0
RR
784const wxDataViewModel* wxDataViewCtrlBase::GetModel() const
785{
786 return m_model;
787}
788
c7074d44
RR
789wxDataViewColumn *
790wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column,
87f0efe2 791 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
fa28826d 792{
c7074d44 793 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 794 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
c7074d44
RR
795 model_column, width, align, flags );
796 AppendColumn( ret );
797 return ret;
fa28826d
RR
798}
799
b04fcede
RR
800wxDataViewColumn *
801wxDataViewCtrlBase::AppendIconTextColumn( const wxString &label, unsigned int model_column,
802 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
803{
804 wxDataViewColumn *ret = new wxDataViewColumn( label,
805 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode, (int)align ),
806 model_column, width, align, flags );
807 AppendColumn( ret );
808 return ret;
809}
810
c7074d44
RR
811wxDataViewColumn *
812wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column,
87f0efe2 813 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
605c2c4a 814{
b7fe2261 815
c7074d44 816 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 817 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
c7074d44
RR
818 model_column, width, align, flags );
819 AppendColumn( ret );
820 return ret;
605c2c4a
RR
821}
822
c7074d44
RR
823wxDataViewColumn *
824wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column,
87f0efe2 825 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
ad63bf41 826{
c7074d44 827 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 828 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
c7074d44
RR
829 model_column, width, align, flags );
830 AppendColumn( ret );
831 return ret;
ad63bf41
RR
832}
833
c7074d44
RR
834wxDataViewColumn *
835wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column,
87f0efe2 836 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
4d496ecb 837{
c7074d44 838 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 839 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
c7074d44
RR
840 model_column, width, align, flags );
841 AppendColumn( ret );
842 return ret;
4d496ecb
RR
843}
844
c7074d44
RR
845wxDataViewColumn *
846wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column,
87f0efe2 847 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 848{
c7074d44 849 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 850 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
c7074d44
RR
851 model_column, width, align, flags );
852 AppendColumn( ret );
853 return ret;
07a84e7b
RR
854}
855
c7074d44
RR
856wxDataViewColumn *
857wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 858 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 859{
c7074d44 860 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 861 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
c7074d44
RR
862 model_column, width, align, flags );
863 AppendColumn( ret );
864 return ret;
b04fcede
RR
865}
866
867wxDataViewColumn *
868wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap &label, unsigned int model_column,
869 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
870{
871 wxDataViewColumn *ret = new wxDataViewColumn( label,
872 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode, (int)align ),
873 model_column, width, align, flags );
874 AppendColumn( ret );
875 return ret;
07a84e7b
RR
876}
877
c7074d44
RR
878wxDataViewColumn *
879wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 880 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 881{
c7074d44 882 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 883 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
c7074d44
RR
884 model_column, width, align, flags );
885 AppendColumn( ret );
886 return ret;
07a84e7b
RR
887}
888
c7074d44
RR
889wxDataViewColumn *
890wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 891 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 892{
c7074d44 893 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 894 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
c7074d44
RR
895 model_column, width, align, flags );
896 AppendColumn( ret );
897 return ret;
07a84e7b
RR
898}
899
c7074d44
RR
900wxDataViewColumn *
901wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 902 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 903{
c7074d44 904 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 905 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
c7074d44
RR
906 model_column, width, align, flags );
907 AppendColumn( ret );
908 return ret;
07a84e7b
RR
909}
910
c7074d44
RR
911wxDataViewColumn *
912wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 913 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 914{
c7074d44 915 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 916 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
c7074d44
RR
917 model_column, width, align, flags );
918 AppendColumn( ret );
919 return ret;
07a84e7b
RR
920}
921
736fe67c
RR
922wxDataViewColumn *
923wxDataViewCtrlBase::PrependTextColumn( const wxString &label, unsigned int model_column,
924 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
925{
926 wxDataViewColumn *ret = new wxDataViewColumn( label,
927 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
928 model_column, width, align, flags );
929 PrependColumn( ret );
930 return ret;
931}
932
933wxDataViewColumn *
934wxDataViewCtrlBase::PrependIconTextColumn( const wxString &label, unsigned int model_column,
935 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
936{
937 wxDataViewColumn *ret = new wxDataViewColumn( label,
938 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode, (int)align ),
939 model_column, width, align, flags );
940 PrependColumn( ret );
941 return ret;
942}
943
944wxDataViewColumn *
945wxDataViewCtrlBase::PrependToggleColumn( const wxString &label, unsigned int model_column,
946 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
947{
948
949 wxDataViewColumn *ret = new wxDataViewColumn( label,
950 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
951 model_column, width, align, flags );
952 PrependColumn( ret );
953 return ret;
954}
955
956wxDataViewColumn *
957wxDataViewCtrlBase::PrependProgressColumn( const wxString &label, unsigned int model_column,
958 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
959{
960 wxDataViewColumn *ret = new wxDataViewColumn( label,
961 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
962 model_column, width, align, flags );
963 PrependColumn( ret );
964 return ret;
965}
966
967wxDataViewColumn *
968wxDataViewCtrlBase::PrependDateColumn( const wxString &label, unsigned int model_column,
969 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
970{
971 wxDataViewColumn *ret = new wxDataViewColumn( label,
972 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
973 model_column, width, align, flags );
974 PrependColumn( ret );
975 return ret;
976}
977
978wxDataViewColumn *
979wxDataViewCtrlBase::PrependBitmapColumn( const wxString &label, unsigned int model_column,
980 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
981{
982 wxDataViewColumn *ret = new wxDataViewColumn( label,
983 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
984 model_column, width, align, flags );
985 PrependColumn( ret );
986 return ret;
987}
988
989wxDataViewColumn *
990wxDataViewCtrlBase::PrependTextColumn( const wxBitmap &label, unsigned int model_column,
991 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
992{
993 wxDataViewColumn *ret = new wxDataViewColumn( label,
994 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
995 model_column, width, align, flags );
996 PrependColumn( ret );
997 return ret;
998}
999
1000wxDataViewColumn *
1001wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap &label, unsigned int model_column,
1002 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1003{
1004 wxDataViewColumn *ret = new wxDataViewColumn( label,
1005 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode, (int)align ),
1006 model_column, width, align, flags );
1007 PrependColumn( ret );
1008 return ret;
1009}
1010
1011wxDataViewColumn *
1012wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap &label, unsigned int model_column,
1013 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1014{
1015 wxDataViewColumn *ret = new wxDataViewColumn( label,
1016 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
1017 model_column, width, align, flags );
1018 PrependColumn( ret );
1019 return ret;
1020}
1021
1022wxDataViewColumn *
1023wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap &label, unsigned int model_column,
1024 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1025{
1026 wxDataViewColumn *ret = new wxDataViewColumn( label,
1027 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
1028 model_column, width, align, flags );
1029 PrependColumn( ret );
1030 return ret;
1031}
1032
1033wxDataViewColumn *
1034wxDataViewCtrlBase::PrependDateColumn( const wxBitmap &label, unsigned int model_column,
1035 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1036{
1037 wxDataViewColumn *ret = new wxDataViewColumn( label,
1038 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
1039 model_column, width, align, flags );
1040 PrependColumn( ret );
1041 return ret;
1042}
1043
1044wxDataViewColumn *
1045wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap &label, unsigned int model_column,
1046 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1047{
1048 wxDataViewColumn *ret = new wxDataViewColumn( label,
1049 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
1050 model_column, width, align, flags );
1051 PrependColumn( ret );
1052 return ret;
1053}
1054
c7074d44
RR
1055bool
1056wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
fa28826d 1057{
6842a71a 1058 col->SetOwner( (wxDataViewCtrl*) this );
fa28826d
RR
1059 return true;
1060}
1061
736fe67c
RR
1062bool
1063wxDataViewCtrlBase::PrependColumn( wxDataViewColumn *col )
1064{
1065 col->SetOwner( (wxDataViewCtrl*) this );
1066 return true;
1067}
1068
eb7f97f8
RR
1069// ---------------------------------------------------------
1070// wxDataViewEvent
1071// ---------------------------------------------------------
1072
1073IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent)
1074
d86c1870
RR
1075DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED)
1076
e0062c04 1077DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED)
6977c3bf 1078DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING)
3ecb8a53 1079DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED)
6977c3bf 1080DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING)
3ecb8a53 1081DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED)
e0000f94
RR
1082DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED)
1083DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE)
6608fdab 1084DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED)
e0000f94 1085
63779a3d
RR
1086DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU)
1087
31fb32e1
RR
1088DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK)
1089DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK)
c0a66d92 1090DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED)
eb7f97f8 1091
52e750fc
RR
1092
1093// -------------------------------------
1094// wxDataViewSpinRenderer
1095// -------------------------------------
1096
1097wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min, int max, wxDataViewCellMode mode, int alignment ) :
b5ec7dd6
VZ
1098 wxDataViewCustomRenderer( "long", mode, alignment )
1099{
52e750fc
RR
1100 m_min = min;
1101 m_max = max;
1102}
b5ec7dd6 1103
52e750fc 1104wxControl* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
b5ec7dd6 1105{
52e750fc 1106 long l = value;
b5ec7dd6 1107 return new wxSpinCtrl( parent, wxID_ANY, wxEmptyString,
52e750fc
RR
1108 labelRect.GetTopLeft(), labelRect.GetSize(), wxSP_ARROW_KEYS, m_min, m_max, l );
1109}
b5ec7dd6 1110
52e750fc 1111bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
b5ec7dd6 1112{
52e750fc
RR
1113 wxSpinCtrl *sc = (wxSpinCtrl*) editor;
1114 long l = sc->GetValue();
1115 value = l;
1116 return true;
1117}
b5ec7dd6 1118
52e750fc
RR
1119bool wxDataViewSpinRenderer::Render( wxRect rect, wxDC *dc, int state )
1120{
1121 wxString str;
1122 str.Printf( "%d", (int) m_data );
1123 RenderText( str, 0, rect, dc, state );
1124 return true;
1125}
b5ec7dd6 1126
52e750fc
RR
1127wxSize wxDataViewSpinRenderer::GetSize() const
1128{
1129 return wxSize(80,16);
1130}
b5ec7dd6 1131
52e750fc
RR
1132bool wxDataViewSpinRenderer::SetValue( const wxVariant &value )
1133{
1134 m_data = value.GetLong();
1135 return true;
1136}
b5ec7dd6 1137
52e750fc
RR
1138bool wxDataViewSpinRenderer::GetValue( wxVariant &value ) const
1139{
1140 value = m_data;
1141 return true;
1142}
1143
e94d0c1e
RR
1144//-----------------------------------------------------------------------------
1145// wxDataViewTreeStore
1146//-----------------------------------------------------------------------------
1147
b5ec7dd6
VZ
1148wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1149 wxDataViewTreeStoreNode *parent,
e94d0c1e
RR
1150 const wxString &text, const wxIcon &icon, wxClientData *data )
1151{
1152 m_parent = parent;
1153 m_text = text;
1154 m_icon = icon;
1155 m_data = data;
1156}
1157
1158wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1159{
1160 if (m_data)
1161 delete m_data;
1162}
b5ec7dd6 1163
e94d0c1e 1164#include "wx/listimpl.cpp"
a76c2f37 1165WX_DEFINE_LIST(wxDataViewTreeStoreNodeList)
e94d0c1e 1166
b5ec7dd6
VZ
1167wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1168 wxDataViewTreeStoreNode *parent, const wxString &text,
e94d0c1e
RR
1169 const wxIcon &icon, const wxIcon &expanded, wxClientData *data ) :
1170 wxDataViewTreeStoreNode( parent, text, icon, data )
1171{
1172 m_iconExpanded = expanded;
a75124d0 1173 m_isExpanded = false;
e94d0c1e
RR
1174 m_children.DeleteContents(true);
1175}
1176
1177wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1178{
1179}
1180
1181//-----------------------------------------------------------------------------
1182
1183wxDataViewTreeStore::wxDataViewTreeStore()
1184{
1185 m_root = new wxDataViewTreeStoreContainerNode( NULL, wxEmptyString );
1186}
1187
1188wxDataViewTreeStore::~wxDataViewTreeStore()
1189{
1190 delete m_root;
1191}
1192
b5ec7dd6 1193wxDataViewItem wxDataViewTreeStore::AppendItem( const wxDataViewItem& parent,
e94d0c1e
RR
1194 const wxString &text, const wxIcon &icon, wxClientData *data )
1195{
1196 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1197 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1198
1199 wxDataViewTreeStoreNode *node =
e94d0c1e
RR
1200 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1201 parent_node->GetChildren().Append( node );
b5ec7dd6 1202
e94d0c1e
RR
1203 // notify control
1204 ItemAdded( parent, node->GetItem() );
b5ec7dd6 1205
e94d0c1e
RR
1206 return node->GetItem();
1207}
1208
1209wxDataViewItem wxDataViewTreeStore::PrependItem( const wxDataViewItem& parent,
1210 const wxString &text, const wxIcon &icon, wxClientData *data )
1211{
1212 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1213 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1214
1215 wxDataViewTreeStoreNode *node =
e94d0c1e
RR
1216 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1217 parent_node->GetChildren().Insert( node );
b5ec7dd6 1218
e94d0c1e
RR
1219 // notify control
1220 ItemAdded( parent, node->GetItem() );
b5ec7dd6 1221
e94d0c1e
RR
1222 return node->GetItem();
1223}
1224
b5ec7dd6
VZ
1225wxDataViewItem
1226wxDataViewTreeStore::InsertItem(const wxDataViewItem& WXUNUSED(parent),
1227 const wxDataViewItem& WXUNUSED(previous),
1228 const wxString& WXUNUSED(text),
1229 const wxIcon& WXUNUSED(icon),
1230 wxClientData * WXUNUSED(data))
e94d0c1e
RR
1231{
1232 return wxDataViewItem(0);
1233}
1234
b5ec7dd6
VZ
1235wxDataViewItem wxDataViewTreeStore::PrependContainer( const wxDataViewItem& parent,
1236 const wxString &text, const wxIcon &icon, const wxIcon &expanded,
e94d0c1e
RR
1237 wxClientData *data )
1238{
1239 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1240 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1241
1242 wxDataViewTreeStoreContainerNode *node =
e94d0c1e
RR
1243 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1244 parent_node->GetChildren().Insert( node );
b5ec7dd6 1245
e94d0c1e
RR
1246 // notify control
1247 ItemAdded( parent, node->GetItem() );
b5ec7dd6 1248
e94d0c1e
RR
1249 return node->GetItem();
1250}
1251
b5ec7dd6
VZ
1252wxDataViewItem
1253wxDataViewTreeStore::AppendContainer(const wxDataViewItem& parent,
1254 const wxString &text,
1255 const wxIcon& icon,
1256 const wxIcon& expanded,
1257 wxClientData * data)
e94d0c1e
RR
1258{
1259 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1260 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1261
1262 wxDataViewTreeStoreContainerNode *node =
e94d0c1e
RR
1263 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1264 parent_node->GetChildren().Append( node );
b5ec7dd6 1265
e94d0c1e
RR
1266 // notify control
1267 ItemAdded( parent, node->GetItem() );
b5ec7dd6 1268
e94d0c1e
RR
1269 return node->GetItem();
1270}
1271
b5ec7dd6
VZ
1272wxDataViewItem
1273wxDataViewTreeStore::InsertContainer(const wxDataViewItem& WXUNUSED(parent),
1274 const wxDataViewItem& WXUNUSED(previous),
1275 const wxString& WXUNUSED(text),
1276 const wxIcon& WXUNUSED(icon),
1277 const wxIcon& WXUNUSED(expanded),
1278 wxClientData * WXUNUSED(data))
e94d0c1e
RR
1279{
1280 return wxDataViewItem(0);
1281}
1282
1283wxDataViewItem wxDataViewTreeStore::GetNthChild( const wxDataViewItem& parent, unsigned int pos ) const
1284{
1285 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1286 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6 1287
e94d0c1e
RR
1288 wxDataViewTreeStoreNodeList::compatibility_iterator node = parent_node->GetChildren().Item( pos );
1289 if (node)
1290 return node->GetData();
b5ec7dd6 1291
e94d0c1e
RR
1292 return wxDataViewItem(0);
1293}
1294
1295int wxDataViewTreeStore::GetChildCount( const wxDataViewItem& parent ) const
1296{
1297 wxDataViewTreeStoreNode *node = FindNode( parent );
1298 if (!node) return -1;
b5ec7dd6 1299
e94d0c1e
RR
1300 if (!node->IsContainer())
1301 return 0;
b5ec7dd6 1302
e94d0c1e 1303 wxDataViewTreeStoreContainerNode *container_node = (wxDataViewTreeStoreContainerNode*) node;
b5ec7dd6 1304 return (int) container_node->GetChildren().GetCount();
e94d0c1e
RR
1305}
1306
1307void wxDataViewTreeStore::SetItemText( const wxDataViewItem& item, const wxString &text )
1308{
1309 wxDataViewTreeStoreNode *node = FindNode( item );
1310 if (!node) return;
b5ec7dd6 1311
e94d0c1e 1312 node->SetText( text );
b5ec7dd6 1313
e94d0c1e
RR
1314 // notify control
1315 ValueChanged( item, 0 );
1316}
1317
1318wxString wxDataViewTreeStore::GetItemText( const wxDataViewItem& item ) const
1319{
1320 wxDataViewTreeStoreNode *node = FindNode( item );
1321 if (!node) return wxEmptyString;
b5ec7dd6 1322
e94d0c1e
RR
1323 return node->GetText();
1324}
1325
1326void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
1327{
1328 wxDataViewTreeStoreNode *node = FindNode( item );
1329 if (!node) return;
b5ec7dd6 1330
e94d0c1e 1331 node->SetIcon( icon );
b5ec7dd6 1332
e94d0c1e
RR
1333 // notify control
1334 ValueChanged( item, 0 );
1335}
1336
1337const wxIcon &wxDataViewTreeStore::GetItemIcon( const wxDataViewItem& item ) const
1338{
1339 wxDataViewTreeStoreNode *node = FindNode( item );
1340 if (!node) return wxNullIcon;
b5ec7dd6 1341
e94d0c1e
RR
1342 return node->GetIcon();
1343}
1344
1345void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
1346{
1347 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1348 if (!node) return;
b5ec7dd6 1349
e94d0c1e 1350 node->SetExpandedIcon( icon );
b5ec7dd6 1351
e94d0c1e
RR
1352 // notify control
1353 ValueChanged( item, 0 );
1354}
1355
1356const wxIcon &wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem& item ) const
1357{
1358 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1359 if (!node) return wxNullIcon;
b5ec7dd6 1360
e94d0c1e
RR
1361 return node->GetExpandedIcon();
1362}
1363
1364void wxDataViewTreeStore::SetItemData( const wxDataViewItem& item, wxClientData *data )
1365{
1366 wxDataViewTreeStoreNode *node = FindNode( item );
1367 if (!node) return;
b5ec7dd6 1368
e94d0c1e 1369 node->SetData( data );
b5ec7dd6 1370
e94d0c1e
RR
1371 // notify control? only sensible when sorting on client data
1372 // ValueChanged( item, 0 );
1373}
1374
1375wxClientData *wxDataViewTreeStore::GetItemData( const wxDataViewItem& item ) const
1376{
1377 wxDataViewTreeStoreNode *node = FindNode( item );
1378 if (!node) return NULL;
b5ec7dd6 1379
e94d0c1e
RR
1380 return node->GetData();
1381}
1382
1383void wxDataViewTreeStore::DeleteItem( const wxDataViewItem& item )
1384{
1385 if (!item.IsOk()) return;
1386
1387 wxDataViewItem parent_item = GetParent( item );
1388
1389 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent_item );
1390 if (!parent_node) return;
b5ec7dd6 1391
e94d0c1e
RR
1392 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1393 if (!node) return;
b5ec7dd6 1394
e94d0c1e 1395 parent_node->GetChildren().DeleteObject( node );
b5ec7dd6 1396
e94d0c1e
RR
1397 // notify control
1398 ItemDeleted( parent_item, item );
1399}
1400
1401void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem& item )
1402{
1403 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1404 if (!node) return;
b5ec7dd6 1405
e94d0c1e
RR
1406 wxDataViewItemArray array;
1407 wxDataViewTreeStoreNodeList::iterator iter;
1408 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
1409 {
1410 wxDataViewTreeStoreNode* child = *iter;
1411 array.Add( child->GetItem() );
1412 }
b5ec7dd6 1413
e94d0c1e 1414 node->GetChildren().clear();
b5ec7dd6 1415
e94d0c1e
RR
1416 // notify control
1417 ItemsDeleted( item, array );
1418}
1419
1420void wxDataViewTreeStore::DeleteAllItems()
1421{
1422 // TODO
1423}
1424
b5ec7dd6
VZ
1425void
1426wxDataViewTreeStore::GetValue(wxVariant &variant,
1427 const wxDataViewItem &item,
1428 unsigned int WXUNUSED(col)) const
e94d0c1e
RR
1429{
1430 // if (col != 0) return;
b5ec7dd6 1431
e94d0c1e
RR
1432 wxDataViewTreeStoreNode *node = FindNode( item );
1433 if (!node) return;
b5ec7dd6 1434
a75124d0
RR
1435 wxIcon icon( node->GetIcon());
1436 if (node->IsContainer())
1437 {
1438 wxDataViewTreeStoreContainerNode *container = (wxDataViewTreeStoreContainerNode*) node;
1439 if (container->IsExpanded() && container->GetExpandedIcon().IsOk())
1440 icon = container->GetExpandedIcon();
1441 }
1442
1443 wxDataViewIconText data( node->GetText(), icon );
b5ec7dd6 1444
e94d0c1e
RR
1445 variant << data;
1446}
1447
b5ec7dd6
VZ
1448bool
1449wxDataViewTreeStore::SetValue(const wxVariant& variant,
1450 const wxDataViewItem& item,
1451 unsigned int WXUNUSED(col))
e94d0c1e
RR
1452{
1453 // if (col != 0) return false;
b5ec7dd6 1454
e94d0c1e
RR
1455 wxDataViewTreeStoreNode *node = FindNode( item );
1456 if (!node) return false;
b5ec7dd6 1457
e94d0c1e 1458 wxDataViewIconText data;
b5ec7dd6 1459
e94d0c1e 1460 data << variant;
b5ec7dd6 1461
e94d0c1e
RR
1462 node->SetText( data.GetText() );
1463 node->SetIcon( data.GetIcon() );
b5ec7dd6 1464
e94d0c1e
RR
1465 return true;
1466}
1467
1468wxDataViewItem wxDataViewTreeStore::GetParent( const wxDataViewItem &item ) const
1469{
1470 wxDataViewTreeStoreNode *node = FindNode( item );
1471 if (!node) return wxDataViewItem(0);
b5ec7dd6 1472
e94d0c1e
RR
1473 wxDataViewTreeStoreNode *parent = node->GetParent();
1474 if (!parent) return wxDataViewItem(0);
b5ec7dd6 1475
e94d0c1e
RR
1476 if (parent == m_root)
1477 return wxDataViewItem(0);
b5ec7dd6 1478
e94d0c1e
RR
1479 return parent->GetItem();
1480}
1481
1482bool wxDataViewTreeStore::IsContainer( const wxDataViewItem &item ) const
1483{
1484 wxDataViewTreeStoreNode *node = FindNode( item );
1485 if (!node) return false;
b5ec7dd6 1486
e94d0c1e
RR
1487 return node->IsContainer();
1488}
1489
1490unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
1491{
1492 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1493 if (!node) return 0;
1494
1495 wxDataViewTreeStoreNodeList::iterator iter;
1496 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
1497 {
1498 wxDataViewTreeStoreNode* child = *iter;
1499 children.Add( child->GetItem() );
1500 }
b5ec7dd6 1501
e94d0c1e
RR
1502 return node->GetChildren().GetCount();
1503}
1504
b5ec7dd6 1505int wxDataViewTreeStore::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
e94d0c1e
RR
1506 unsigned int WXUNUSED(column), bool WXUNUSED(ascending) )
1507{
1508 wxDataViewTreeStoreNode *node1 = FindNode( item1 );
1509 wxDataViewTreeStoreNode *node2 = FindNode( item2 );
b5ec7dd6 1510
e94d0c1e
RR
1511 if (!node1 || !node2)
1512 return 0;
b5ec7dd6
VZ
1513
1514 wxDataViewTreeStoreContainerNode* parent1 =
e94d0c1e 1515 (wxDataViewTreeStoreContainerNode*) node1->GetParent();
b5ec7dd6 1516 wxDataViewTreeStoreContainerNode* parent2 =
e94d0c1e 1517 (wxDataViewTreeStoreContainerNode*) node2->GetParent();
b5ec7dd6 1518
e94d0c1e
RR
1519 if (parent1 != parent2)
1520 {
1521 wxLogError( wxT("Comparing items with different parent.") );
1522 return 0;
1523 }
b5ec7dd6 1524
e94d0c1e
RR
1525 if (node1->IsContainer() && !!node2->IsContainer())
1526 return 1;
b5ec7dd6 1527
e94d0c1e
RR
1528 if (node2->IsContainer() && !!node1->IsContainer())
1529 return -1;
b5ec7dd6 1530
e94d0c1e
RR
1531 return parent1->GetChildren().IndexOf( node1 ) - parent1->GetChildren().IndexOf( node2 );
1532}
1533
1534wxDataViewTreeStoreNode *wxDataViewTreeStore::FindNode( const wxDataViewItem &item ) const
1535{
1536 if (!item.IsOk())
1537 return m_root;
b5ec7dd6 1538
e94d0c1e
RR
1539 return (wxDataViewTreeStoreNode*) item.GetID();
1540}
1541
1542wxDataViewTreeStoreContainerNode *wxDataViewTreeStore::FindContainerNode( const wxDataViewItem &item ) const
1543{
1544 if (!item.IsOk())
1545 return (wxDataViewTreeStoreContainerNode*) m_root;
1546
1547 wxDataViewTreeStoreNode* node = (wxDataViewTreeStoreNode*) item.GetID();
b5ec7dd6 1548
e94d0c1e
RR
1549 if (!node->IsContainer())
1550 return NULL;
b5ec7dd6 1551
e94d0c1e
RR
1552 return (wxDataViewTreeStoreContainerNode*) node;
1553}
1554
a75124d0
RR
1555//-----------------------------------------------------------------------------
1556// wxDataViewTreeCtrl
1557//-----------------------------------------------------------------------------
1558
1559IMPLEMENT_DYNAMIC_CLASS(wxDataViewTreeCtrl,wxDataViewCtrl)
1560
1561BEGIN_EVENT_TABLE(wxDataViewTreeCtrl,wxDataViewCtrl)
1562 EVT_DATAVIEW_ITEM_EXPANDED(-1, wxDataViewTreeCtrl::OnExpanded)
1563 EVT_DATAVIEW_ITEM_COLLAPSED(-1, wxDataViewTreeCtrl::OnCollapsed)
9c150d5f 1564 EVT_SIZE( wxDataViewTreeCtrl::OnSize )
a75124d0
RR
1565END_EVENT_TABLE()
1566
1567wxDataViewTreeCtrl::wxDataViewTreeCtrl()
1568{
1569 m_imageList = NULL;
1570}
1571
1572wxDataViewTreeCtrl::wxDataViewTreeCtrl( wxWindow *parent, wxWindowID id,
1573 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
1574{
1575 m_imageList = NULL;
1576 Create( parent, id, pos, size, style, validator );
1577
1578 wxDataViewTreeStore *store = new wxDataViewTreeStore;
1579 AssociateModel( store );
1580 store->DecRef();
9c150d5f 1581
a6aaf49f 1582#if !defined(__WXGTK20__) || defined(wxUSE_GENERICDATAVIEWCTRL)
9c150d5f
RR
1583 wxDataViewColumn *col = AppendIconTextColumn( "", 0, wxDATAVIEW_CELL_INERT, 40 );
1584
9c150d5f
RR
1585 wxSize cient_size = GetClientSize();
1586 col->SetWidth( size.x );
a6aaf49f
RR
1587#else
1588 AppendIconTextColumn( "", 0, wxDATAVIEW_CELL_INERT, 40 );
9c150d5f 1589#endif
a75124d0
RR
1590}
1591
1592wxDataViewTreeCtrl::~wxDataViewTreeCtrl()
1593{
1594 if (m_imageList)
1595 delete m_imageList;
1596}
1597
1598bool wxDataViewTreeCtrl::Create( wxWindow *parent, wxWindowID id,
1599 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
1600{
1601 return wxDataViewCtrl::Create( parent, id, pos, size, style, validator );
1602}
1603
1604void wxDataViewTreeCtrl::SetImageList( wxImageList *imagelist )
1605{
1606 if (m_imageList)
1607 delete m_imageList;
1608
1609 m_imageList = imagelist;
1610}
1611
1612wxDataViewItem wxDataViewTreeCtrl::AppendItem( const wxDataViewItem& parent,
1613 const wxString &text, int iconIndex, wxClientData *data )
1614{
1615 wxIcon icon = wxNullIcon;
1616 if (m_imageList && (iconIndex != -1))
1617 icon = m_imageList->GetIcon( iconIndex );
1618
1619 return GetStore()->AppendItem( parent, text, icon, data );
1620}
1621
1622wxDataViewItem wxDataViewTreeCtrl::PrependItem( const wxDataViewItem& parent,
1623 const wxString &text, int iconIndex, wxClientData *data )
1624{
1625 wxIcon icon = wxNullIcon;
1626 if (m_imageList && (iconIndex != -1))
1627 icon = m_imageList->GetIcon( iconIndex );
1628
1629 return GetStore()->PrependItem( parent, text, icon, data );
1630}
1631
1632wxDataViewItem wxDataViewTreeCtrl::InsertItem( const wxDataViewItem& parent, const wxDataViewItem& previous,
1633 const wxString &text, int iconIndex, wxClientData *data )
1634{
1635 wxIcon icon = wxNullIcon;
1636 if (m_imageList && (iconIndex != -1))
1637 icon = m_imageList->GetIcon( iconIndex );
1638
1639 return GetStore()->InsertItem( parent, previous, text, icon, data );
1640}
1641
1642wxDataViewItem wxDataViewTreeCtrl::PrependContainer( const wxDataViewItem& parent,
1643 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
1644{
1645 wxIcon icon = wxNullIcon;
1646 if (m_imageList && (iconIndex != -1))
1647 icon = m_imageList->GetIcon( iconIndex );
1648
1649 wxIcon expanded = wxNullIcon;
1650 if (m_imageList && (expandedIndex != -1))
1651 expanded = m_imageList->GetIcon( expandedIndex );
1652
1653 return GetStore()->PrependContainer( parent, text, icon, expanded, data );
1654}
1655
1656wxDataViewItem wxDataViewTreeCtrl::AppendContainer( const wxDataViewItem& parent,
1657 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
1658{
1659 wxIcon icon = wxNullIcon;
1660 if (m_imageList && (iconIndex != -1))
1661 icon = m_imageList->GetIcon( iconIndex );
1662
1663 wxIcon expanded = wxNullIcon;
1664 if (m_imageList && (expandedIndex != -1))
1665 expanded = m_imageList->GetIcon( expandedIndex );
1666
1667 return GetStore()->AppendContainer( parent, text, icon, expanded, data );
1668}
1669
1670wxDataViewItem wxDataViewTreeCtrl::InsertContainer( const wxDataViewItem& parent, const wxDataViewItem& previous,
1671 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
1672{
1673 wxIcon icon = wxNullIcon;
1674 if (m_imageList && (iconIndex != -1))
1675 icon = m_imageList->GetIcon( iconIndex );
1676
1677 wxIcon expanded = wxNullIcon;
1678 if (m_imageList && (expandedIndex != -1))
1679 expanded = m_imageList->GetIcon( expandedIndex );
1680
1681 return GetStore()->InsertContainer( parent, previous, text, icon, expanded, data );
1682}
1683
a75124d0
RR
1684void wxDataViewTreeCtrl::OnExpanded( wxDataViewEvent &event )
1685{
1686 if (m_imageList) return;
1687
1688 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
1689 if (!container) return;
1690
1691 container->SetExpanded( true );
1692 GetStore()->ItemChanged( event.GetItem() );
1693}
1694
1695void wxDataViewTreeCtrl::OnCollapsed( wxDataViewEvent &event )
1696{
1697 if (m_imageList) return;
1698
1699 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
1700 if (!container) return;
1701
1702 container->SetExpanded( false );
1703 GetStore()->ItemChanged( event.GetItem() );
1704}
1705
a6aaf49f 1706void wxDataViewTreeCtrl::OnSize( wxSizeEvent &event )
1a07a730 1707{
a6aaf49f 1708#if !defined(__WXGTK20__) || defined(wxUSE_GENERICDATAVIEWCTRL)
9c150d5f
RR
1709 wxSize size = GetClientSize();
1710 wxDataViewColumn *col = GetColumn( 0 );
63779a3d 1711 if (col) col->SetWidth( size.x );
1a07a730 1712#endif
a6aaf49f 1713 event.Skip( true );
1a07a730 1714}
a75124d0 1715
b5ec7dd6 1716#endif // wxUSE_DATAVIEWCTRL
e0000f94 1717