]> git.saurik.com Git - wxWidgets.git/blame - src/common/datavcmn.cpp
Forgot this
[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 20#include "wx/dataview.h"
52e750fc 21#include "wx/spinctrl.h"
7448d67c 22#include "wx/choice.h"
52e750fc 23
c232dfe5 24#include "wx/weakref.h"
a881f34e 25#include "wx/vector.h"
c232dfe5 26
f554a14b 27#ifndef WX_PRECOMP
c3c62822
PC
28 #include "wx/dc.h"
29 #include "wx/settings.h"
f554a14b 30 #include "wx/log.h"
e94d0c1e 31 #include "wx/icon.h"
c3c62822 32 #include "wx/crt.h"
f554a14b
WS
33#endif
34
23318a53 35const char wxDataViewCtrlNameStr[] = "dataviewCtrl";
239eaa41 36
239eaa41 37
d5025dc0
RR
38bool operator == (const wxDataViewItem &left, const wxDataViewItem &right)
39{
40 return (left.GetID() == right.GetID() );
41}
42
c3c62822
PC
43#ifdef __WXDEBUG__
44void wxDataViewItem::Print(const wxString& text) const
45{
e913be34 46 wxPrintf(wxT("item %s: %lu\n"), text.GetData(), wxPtrToUInt(m_id));
c3c62822
PC
47}
48#endif
d5025dc0 49
f554a14b 50// ---------------------------------------------------------
854cdb09 51// wxDataViewModelNotifier
f554a14b 52// ---------------------------------------------------------
239eaa41 53
8ab03001 54#include "wx/listimpl.cpp"
a76c2f37 55WX_DEFINE_LIST(wxDataViewModelNotifiers)
9d8fe14a 56
854cdb09
RR
57bool wxDataViewModelNotifier::ItemsAdded( const wxDataViewItem &parent, const wxDataViewItemArray &items )
58{
59 size_t count = items.GetCount();
60 size_t i;
61 for (i = 0; i < count; i++)
62 if (!ItemAdded( parent, items[i] )) return false;
b5ec7dd6 63
854cdb09
RR
64 return true;
65}
66
67bool wxDataViewModelNotifier::ItemsDeleted( const wxDataViewItem &parent, const wxDataViewItemArray &items )
68{
69 size_t count = items.GetCount();
70 size_t i;
71 for (i = 0; i < count; i++)
72 if (!ItemDeleted( parent, items[i] )) return false;
b5ec7dd6 73
854cdb09
RR
74 return true;
75}
76
77bool wxDataViewModelNotifier::ItemsChanged( const wxDataViewItemArray &items )
78{
79 size_t count = items.GetCount();
80 size_t i;
81 for (i = 0; i < count; i++)
82 if (!ItemChanged( items[i] )) return false;
b5ec7dd6 83
854cdb09
RR
84 return true;
85}
86
87// ---------------------------------------------------------
88// wxDataViewModel
89// ---------------------------------------------------------
90
e0062c04 91wxDataViewModel::wxDataViewModel()
239eaa41 92{
8f850e28 93 m_notifiers.DeleteContents( true );
239eaa41
RR
94}
95
e0062c04 96bool wxDataViewModel::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
239eaa41 97{
8f850e28
RR
98 bool ret = true;
99
9d8fe14a
RR
100 wxDataViewModelNotifiers::iterator iter;
101 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
8f850e28 102 {
9d8fe14a 103 wxDataViewModelNotifier* notifier = *iter;
e0062c04 104 if (!notifier->ItemAdded( parent, item ))
8f850e28 105 ret = false;
8f850e28 106 }
f554a14b 107
8f850e28 108 return ret;
239eaa41
RR
109}
110
469d3e9b 111bool wxDataViewModel::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
239eaa41 112{
8f850e28
RR
113 bool ret = true;
114
9d8fe14a
RR
115 wxDataViewModelNotifiers::iterator iter;
116 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
8f850e28 117 {
9d8fe14a 118 wxDataViewModelNotifier* notifier = *iter;
469d3e9b 119 if (!notifier->ItemDeleted( parent, item ))
8f850e28 120 ret = false;
8f850e28 121 }
f554a14b 122
8f850e28 123 return ret;
239eaa41
RR
124}
125
e0062c04 126bool wxDataViewModel::ItemChanged( const wxDataViewItem &item )
239eaa41 127{
8f850e28
RR
128 bool ret = true;
129
9d8fe14a
RR
130 wxDataViewModelNotifiers::iterator iter;
131 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
8f850e28 132 {
9d8fe14a 133 wxDataViewModelNotifier* notifier = *iter;
e0062c04 134 if (!notifier->ItemChanged( item ))
8f850e28 135 ret = false;
8f850e28 136 }
f554a14b 137
8f850e28 138 return ret;
239eaa41
RR
139}
140
854cdb09
RR
141bool wxDataViewModel::ItemsAdded( const wxDataViewItem &parent, const wxDataViewItemArray &items )
142{
143 bool ret = true;
144
145 wxDataViewModelNotifiers::iterator iter;
146 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
147 {
148 wxDataViewModelNotifier* notifier = *iter;
149 if (!notifier->ItemsAdded( parent, items ))
150 ret = false;
151 }
152
153 return ret;
154}
155
156bool wxDataViewModel::ItemsDeleted( const wxDataViewItem &parent, const wxDataViewItemArray &items )
157{
158 bool ret = true;
159
160 wxDataViewModelNotifiers::iterator iter;
161 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
162 {
163 wxDataViewModelNotifier* notifier = *iter;
164 if (!notifier->ItemsDeleted( parent, items ))
165 ret = false;
166 }
167
168 return ret;
169}
170
171bool wxDataViewModel::ItemsChanged( const wxDataViewItemArray &items )
172{
173 bool ret = true;
174
175 wxDataViewModelNotifiers::iterator iter;
176 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
177 {
178 wxDataViewModelNotifier* notifier = *iter;
179 if (!notifier->ItemsChanged( items ))
180 ret = false;
181 }
182
183 return ret;
184}
185
e0062c04 186bool wxDataViewModel::ValueChanged( const wxDataViewItem &item, unsigned int col )
239eaa41 187{
8f850e28 188 bool ret = true;
b5d777c7 189
9d8fe14a
RR
190 wxDataViewModelNotifiers::iterator iter;
191 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
b5d777c7 192 {
9d8fe14a 193 wxDataViewModelNotifier* notifier = *iter;
e0062c04 194 if (!notifier->ValueChanged( item, col ))
8f850e28 195 ret = false;
b5d777c7 196 }
f554a14b 197
b5d777c7 198 return ret;
239eaa41
RR
199}
200
e0062c04 201bool wxDataViewModel::Cleared()
4eccd3a1
RR
202{
203 bool ret = true;
204
9d8fe14a
RR
205 wxDataViewModelNotifiers::iterator iter;
206 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
4eccd3a1 207 {
9d8fe14a 208 wxDataViewModelNotifier* notifier = *iter;
8f850e28
RR
209 if (!notifier->Cleared())
210 ret = false;
8f850e28 211 }
f554a14b 212
8f850e28 213 return ret;
239eaa41
RR
214}
215
ef427989
RR
216void wxDataViewModel::Resort()
217{
9d8fe14a
RR
218 wxDataViewModelNotifiers::iterator iter;
219 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
ef427989 220 {
9d8fe14a 221 wxDataViewModelNotifier* notifier = *iter;
ef427989 222 notifier->Resort();
ef427989
RR
223 }
224}
225
e0062c04 226void wxDataViewModel::AddNotifier( wxDataViewModelNotifier *notifier )
239eaa41 227{
9d8fe14a 228 m_notifiers.push_back( notifier );
8f850e28 229 notifier->SetOwner( this );
239eaa41
RR
230}
231
e0062c04 232void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier *notifier )
239eaa41 233{
8f850e28 234 m_notifiers.DeleteObject( notifier );
239eaa41
RR
235}
236
7ee7191c
RR
237int wxDataViewModel::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
238 unsigned int column, bool ascending )
ef427989 239{
cf283a47 240 // sort branches before leaves
ed903e42
RR
241 bool item1_is_container = IsContainer(item1);
242 bool item2_is_container = IsContainer(item2);
900af470 243
ed903e42 244 if (item1_is_container && !item2_is_container)
cf283a47 245 return 1;
ed903e42 246 if (item2_is_container && !item1_is_container)
cf283a47 247 return -1;
900af470 248
ef427989 249 wxVariant value1,value2;
7ee7191c
RR
250 GetValue( value1, item1, column );
251 GetValue( value2, item2, column );
252
253 if (!ascending)
254 {
255 wxVariant temp = value1;
256 value1 = value2;
257 value2 = temp;
258 }
cf283a47 259
ef427989
RR
260 if (value1.GetType() == wxT("string"))
261 {
262 wxString str1 = value1.GetString();
263 wxString str2 = value2.GetString();
0be79c8a 264 int res = str1.Cmp( str2 );
777f9cef
VZ
265 if (res)
266 return res;
267 }
268 else if (value1.GetType() == wxT("long"))
ef427989
RR
269 {
270 long l1 = value1.GetLong();
271 long l2 = value2.GetLong();
cf283a47 272 long res = l1-l2;
777f9cef
VZ
273 if (res)
274 return res;
275 }
276 else if (value1.GetType() == wxT("double"))
ef427989
RR
277 {
278 double d1 = value1.GetDouble();
279 double d2 = value2.GetDouble();
777f9cef
VZ
280 if (d1 < d2)
281 return 1;
282 if (d1 > d2)
283 return -1;
284 }
285 else if (value1.GetType() == wxT("datetime"))
ef427989
RR
286 {
287 wxDateTime dt1 = value1.GetDateTime();
288 wxDateTime dt2 = value2.GetDateTime();
777f9cef
VZ
289 if (dt1.IsEarlierThan(dt2))
290 return 1;
291 if (dt2.IsEarlierThan(dt1))
292 return -11;
ef427989
RR
293 }
294
cf283a47 295 // items must be different
777f9cef
VZ
296 wxUIntPtr id1 = wxPtrToUInt(item1.GetID()),
297 id2 = wxPtrToUInt(item2.GetID());
b7fe2261 298
777f9cef 299 return ascending ? id1 - id2 : id2 - id1;
ef427989
RR
300}
301
c534e696
RR
302// ---------------------------------------------------------
303// wxDataViewIndexListModel
304// ---------------------------------------------------------
305
e39de702 306static int my_sort( int *v1, int *v2 )
c534e696 307{
e39de702
RR
308 return *v2-*v1;
309}
517166e6 310
e39de702
RR
311
312wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size )
313{
314 // IDs are ordered until an item gets deleted or inserted
315 m_ordered = true;
777f9cef 316
e39de702
RR
317 // build initial index
318 unsigned int i;
319 for (i = 1; i < initial_size+1; i++)
777f9cef 320 m_hash.Add( wxUIntToPtr(i) );
e39de702 321 m_lastIndex = initial_size + 1;
c534e696
RR
322}
323
324wxDataViewIndexListModel::~wxDataViewIndexListModel()
325{
326}
327
33ba5a05
RR
328void wxDataViewIndexListModel::Reset( unsigned int new_size )
329{
e39de702 330 m_hash.Clear();
777f9cef 331
e39de702
RR
332 // IDs are ordered until an item gets deleted or inserted
333 m_ordered = true;
777f9cef 334
e39de702
RR
335 // build initial index
336 unsigned int i;
337 for (i = 1; i < new_size+1; i++)
777f9cef 338 m_hash.Add( wxUIntToPtr(i) );
e39de702 339 m_lastIndex = new_size + 1;
777f9cef 340
33ba5a05
RR
341 wxDataViewModel::Cleared();
342}
343
c534e696
RR
344void wxDataViewIndexListModel::RowPrepended()
345{
e39de702 346 m_ordered = false;
777f9cef 347
e39de702 348 unsigned int id = m_lastIndex++;
777f9cef
VZ
349 m_hash.Insert( wxUIntToPtr(id), 0 );
350 wxDataViewItem item( wxUIntToPtr(id) );
e39de702 351 ItemAdded( wxDataViewItem(0), item );
c534e696
RR
352}
353
354void wxDataViewIndexListModel::RowInserted( unsigned int before )
355{
e39de702 356 m_ordered = false;
777f9cef 357
e39de702 358 unsigned int id = m_lastIndex++;
777f9cef
VZ
359 m_hash.Insert( wxUIntToPtr(id), before );
360 wxDataViewItem item( wxUIntToPtr(id) );
e39de702 361 ItemAdded( wxDataViewItem(0), item );
c534e696
RR
362}
363
364void wxDataViewIndexListModel::RowAppended()
365{
e39de702 366 unsigned int id = m_lastIndex++;
777f9cef
VZ
367 m_hash.Add( wxUIntToPtr(id) );
368 wxDataViewItem item( wxUIntToPtr(id) );
e39de702 369 ItemAdded( wxDataViewItem(0), item );
c534e696
RR
370}
371
372void wxDataViewIndexListModel::RowDeleted( unsigned int row )
373{
e39de702 374 m_ordered = false;
777f9cef 375
e39de702
RR
376 wxDataViewItem item( m_hash[row] );
377 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item );
378 m_hash.RemoveAt( row );
8b6cf9bf
RR
379}
380
381void wxDataViewIndexListModel::RowsDeleted( const wxArrayInt &rows )
382{
383 wxArrayInt sorted = rows;
384 sorted.Sort( my_sort );
777f9cef 385
e39de702 386 m_ordered = false;
777f9cef 387
e39de702
RR
388 wxDataViewItemArray array;
389 unsigned int i;
390 for (i = 0; i < rows.GetCount(); i++)
391 {
8b6cf9bf
RR
392 wxDataViewItem item( m_hash[rows[i]] );
393 array.Add( item );
e39de702
RR
394 }
395 wxDataViewModel::ItemsDeleted( wxDataViewItem(0), array );
777f9cef 396
e39de702 397 for (i = 0; i < sorted.GetCount(); i++)
8b6cf9bf 398 m_hash.RemoveAt( sorted[i] );
8b6cf9bf
RR
399}
400
c534e696
RR
401void wxDataViewIndexListModel::RowChanged( unsigned int row )
402{
403 wxDataViewModel::ItemChanged( GetItem(row) );
404}
405
406void wxDataViewIndexListModel::RowValueChanged( unsigned int row, unsigned int col )
407{
408 wxDataViewModel::ValueChanged( GetItem(row), col );
409}
410
411unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem &item ) const
412{
e39de702 413 if (m_ordered)
517166e6 414 {
2056dede
RR
415 unsigned int pos = wxPtrToUInt( item.GetID() );
416 return pos-1;
2056dede 417 }
777f9cef 418
e39de702
RR
419 // assert for not found
420 return (unsigned int) m_hash.Index( item.GetID() );
c534e696
RR
421}
422
423wxDataViewItem wxDataViewIndexListModel::GetItem( unsigned int row ) const
424{
e39de702
RR
425 wxASSERT( row < m_hash.GetCount() );
426 return wxDataViewItem( m_hash[row] );
c534e696
RR
427}
428
517166e6 429bool wxDataViewIndexListModel::HasDefaultCompare() const
777f9cef 430{
517166e6
RR
431 return !m_ordered;
432}
433
b7fe2261
VZ
434int wxDataViewIndexListModel::Compare(const wxDataViewItem& item1,
435 const wxDataViewItem& item2,
436 unsigned int WXUNUSED(column),
437 bool ascending)
c534e696 438{
e39de702 439 if (m_ordered)
517166e6 440 {
ec935ba8
PC
441 unsigned int pos1 = wxPtrToUInt(item1.GetID());
442 unsigned int pos2 = wxPtrToUInt(item2.GetID());
777f9cef 443
517166e6
RR
444 if (ascending)
445 return pos1 - pos2;
777f9cef 446 else
517166e6
RR
447 return pos2 - pos1;
448 }
777f9cef 449
7ee7191c
RR
450 if (ascending)
451 return GetRow(item1) - GetRow(item2);
b7fe2261 452
7ee7191c 453 return GetRow(item2) - GetRow(item1);
c534e696
RR
454}
455
900af470 456void wxDataViewIndexListModel::GetValue( wxVariant &variant,
c534e696
RR
457 const wxDataViewItem &item, unsigned int col ) const
458{
8eff6c56 459 GetValueByRow( variant, GetRow(item), col );
c534e696
RR
460}
461
900af470 462bool wxDataViewIndexListModel::SetValue( const wxVariant &variant,
c534e696
RR
463 const wxDataViewItem &item, unsigned int col )
464{
8eff6c56 465 return SetValueByRow( variant, GetRow(item), col );
c534e696
RR
466}
467
4264606e
RR
468bool wxDataViewIndexListModel::GetAttr( const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr )
469{
8eff6c56 470 return GetAttrByRow( GetRow(item), col, attr );
4264606e
RR
471}
472
900af470 473wxDataViewItem wxDataViewIndexListModel::GetParent( const wxDataViewItem & WXUNUSED(item) ) const
c534e696
RR
474{
475 return wxDataViewItem(0);
476}
477
478bool wxDataViewIndexListModel::IsContainer( const wxDataViewItem &item ) const
479{
480 // only the invisible root item has children
481 if (!item.IsOk())
482 return true;
900af470 483
c534e696
RR
484 return false;
485}
486
74fe973b 487unsigned int wxDataViewIndexListModel::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
c534e696 488{
74fe973b
RR
489 if (item.IsOk())
490 return 0;
c534e696 491
74fe973b 492 children = m_hash;
777f9cef 493
74fe973b 494 return m_hash.GetCount();
c534e696
RR
495}
496
e39de702
RR
497// ---------------------------------------------------------
498// wxDataViewVirtualListModel
499// ---------------------------------------------------------
500
501#ifndef __WXMAC__
502
503wxDataViewVirtualListModel::wxDataViewVirtualListModel( unsigned int initial_size )
504{
505 m_lastIndex = initial_size-1;
506}
507
508wxDataViewVirtualListModel::~wxDataViewVirtualListModel()
509{
510}
511
512void wxDataViewVirtualListModel::Reset( unsigned int new_size )
513{
514 m_lastIndex = new_size-1;
777f9cef 515
e39de702
RR
516 wxDataViewModel::Cleared();
517}
518
519void wxDataViewVirtualListModel::RowPrepended()
520{
521 m_lastIndex++;
777f9cef 522 wxDataViewItem item( NULL );
e39de702
RR
523 ItemAdded( wxDataViewItem(0), item );
524}
525
526void wxDataViewVirtualListModel::RowInserted( unsigned int before )
527{
528 m_lastIndex++;
777f9cef 529 wxDataViewItem item( wxUIntToPtr(before) );
e39de702
RR
530 ItemAdded( wxDataViewItem(0), item );
531}
532
533void wxDataViewVirtualListModel::RowAppended()
534{
535 m_lastIndex++;
777f9cef 536 wxDataViewItem item( wxUIntToPtr(m_lastIndex) );
e39de702
RR
537 ItemAdded( wxDataViewItem(0), item );
538}
539
540void wxDataViewVirtualListModel::RowDeleted( unsigned int row )
541{
777f9cef 542 wxDataViewItem item( wxUIntToPtr(row) );
e39de702
RR
543 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item );
544 m_lastIndex++;
545}
546
547void wxDataViewVirtualListModel::RowsDeleted( const wxArrayInt &rows )
548{
549 wxArrayInt sorted = rows;
550 sorted.Sort( my_sort );
777f9cef 551
e39de702
RR
552 wxDataViewItemArray array;
553 unsigned int i;
554 for (i = 0; i < sorted.GetCount(); i++)
555 {
777f9cef 556 wxDataViewItem item( wxUIntToPtr(sorted[i]) );
e39de702
RR
557 array.Add( item );
558 }
559 wxDataViewModel::ItemsDeleted( wxDataViewItem(0), array );
777f9cef 560
e39de702
RR
561 m_lastIndex -= rows.GetCount();
562}
563
564void wxDataViewVirtualListModel::RowChanged( unsigned int row )
565{
566 wxDataViewModel::ItemChanged( GetItem(row) );
567}
568
569void wxDataViewVirtualListModel::RowValueChanged( unsigned int row, unsigned int col )
570{
571 wxDataViewModel::ValueChanged( GetItem(row), col );
572}
573
574unsigned int wxDataViewVirtualListModel::GetRow( const wxDataViewItem &item ) const
575{
576 return wxPtrToUInt( item.GetID() );
577}
578
579wxDataViewItem wxDataViewVirtualListModel::GetItem( unsigned int row ) const
580{
777f9cef 581 return wxDataViewItem( wxUIntToPtr(row) );
e39de702
RR
582}
583
584bool wxDataViewVirtualListModel::HasDefaultCompare() const
777f9cef 585{
e39de702
RR
586 return true;
587}
588
589int wxDataViewVirtualListModel::Compare(const wxDataViewItem& item1,
590 const wxDataViewItem& item2,
591 unsigned int WXUNUSED(column),
592 bool ascending)
593{
594 unsigned int pos1 = wxPtrToUInt(item1.GetID());
595 unsigned int pos2 = wxPtrToUInt(item2.GetID());
777f9cef 596
e39de702
RR
597 if (ascending)
598 return pos1 - pos2;
777f9cef 599 else
e39de702
RR
600 return pos2 - pos1;
601}
602
603void wxDataViewVirtualListModel::GetValue( wxVariant &variant,
604 const wxDataViewItem &item, unsigned int col ) const
605{
8eff6c56 606 GetValueByRow( variant, GetRow(item), col );
e39de702
RR
607}
608
609bool wxDataViewVirtualListModel::SetValue( const wxVariant &variant,
610 const wxDataViewItem &item, unsigned int col )
611{
8eff6c56 612 return SetValueByRow( variant, GetRow(item), col );
e39de702
RR
613}
614
615bool wxDataViewVirtualListModel::GetAttr( const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr )
616{
8eff6c56 617 return GetAttrByRow( GetRow(item), col, attr );
e39de702
RR
618}
619
620wxDataViewItem wxDataViewVirtualListModel::GetParent( const wxDataViewItem & WXUNUSED(item) ) const
621{
622 return wxDataViewItem(0);
623}
624
625bool wxDataViewVirtualListModel::IsContainer( const wxDataViewItem &item ) const
626{
627 // only the invisible root item has children
628 if (!item.IsOk())
629 return true;
630
631 return false;
632}
633
634unsigned int wxDataViewVirtualListModel::GetChildren( const wxDataViewItem &WXUNUSED(item), wxDataViewItemArray &WXUNUSED(children) ) const
635{
636 return 0; // should we report an error ?
637}
638
639#endif // __WXMAC__
640
89352653
RR
641//-----------------------------------------------------------------------------
642// wxDataViewIconText
643//-----------------------------------------------------------------------------
644
645IMPLEMENT_DYNAMIC_CLASS(wxDataViewIconText,wxObject)
646
d350fbec 647IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxDataViewIconText, WXDLLIMPEXP_ADV)
89352653
RR
648
649bool operator == (const wxDataViewIconText &one, const wxDataViewIconText &two)
650{
651 if (one.GetText() != two.GetText()) return false;
652 if (one.IsSameAs(two)) return false;
653 return true;
654}
655
f554a14b 656// ---------------------------------------------------------
baa9ebc4 657// wxDataViewRendererBase
f554a14b 658// ---------------------------------------------------------
6842a71a 659
baa9ebc4 660IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase, wxObject)
6842a71a 661
900af470 662wxDataViewRendererBase::wxDataViewRendererBase( const wxString &varianttype,
9861f022
RR
663 wxDataViewCellMode WXUNUSED(mode),
664 int WXUNUSED(align) )
6842a71a
RR
665{
666 m_variantType = varianttype;
f2b7492a 667 m_owner = NULL;
9861f022
RR
668}
669
c232dfe5
RR
670wxDataViewRendererBase::~wxDataViewRendererBase()
671{
672}
673
9861f022
RR
674const wxDataViewCtrl* wxDataViewRendererBase::GetView() const
675{
5c33522f 676 return const_cast<wxDataViewRendererBase*>(this)->GetOwner()->GetOwner();
6842a71a 677}
f554a14b 678
c232dfe5 679class wxKillRef: public wxWindowRef
777f9cef
VZ
680{
681public:
c232dfe5
RR
682 wxKillRef( wxWindow *win ) : wxWindowRef( win ) { }
683 virtual void OnObjectDestroy()
684 {
685 get()->PopEventHandler( true );
095a3221 686 m_pobj = NULL;
c232dfe5
RR
687 delete this;
688 }
689};
690
e0062c04 691bool wxDataViewRendererBase::StartEditing( const wxDataViewItem &item, wxRect labelRect )
1e510b1e 692{
e0062c04 693 m_item = item; // remember for later
900af470 694
1e510b1e
RR
695 unsigned int col = GetOwner()->GetModelColumn();
696 wxVariant value;
e0062c04 697 GetOwner()->GetOwner()->GetModel()->GetValue( value, item, col );
900af470 698
1e510b1e 699 m_editorCtrl = CreateEditorCtrl( GetOwner()->GetOwner()->GetMainWindow(), labelRect, value );
c232dfe5 700 (void) new wxKillRef( m_editorCtrl.get() );
900af470
JS
701
702 wxDataViewEditorCtrlEvtHandler *handler =
30715fa1 703 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl, (wxDataViewRenderer*) this );
900af470 704
30715fa1 705 m_editorCtrl->PushEventHandler( handler );
900af470 706
30715fa1
RR
707#if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
708 handler->SetFocusOnIdle();
709#else
1e510b1e 710 m_editorCtrl->SetFocus();
30715fa1
RR
711#endif
712
afebb87b
RR
713 // Now we should send Editing Started event
714 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, GetOwner()->GetOwner()->GetId() );
715 event.SetDataViewColumn( GetOwner() );
716 event.SetModel( GetOwner()->GetOwner()->GetModel() );
717 event.SetItem( item );
afebb87b
RR
718 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event );
719
1e510b1e
RR
720 return true;
721}
722
723void wxDataViewRendererBase::CancelEditing()
724{
33a1719a 725 if (!m_editorCtrl) return;
23318a53 726
1e510b1e 727 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
777f9cef 728
c232dfe5
RR
729 m_editorCtrl->Hide();
730 wxPendingDelete.Append( m_editorCtrl );
1e510b1e
RR
731}
732
733bool wxDataViewRendererBase::FinishEditing()
734{
33a1719a
RR
735 if (!m_editorCtrl) return true;
736
1e510b1e
RR
737 wxVariant value;
738 GetValueFromEditorCtrl( m_editorCtrl, value );
739
1e510b1e 740 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
900af470 741
c232dfe5
RR
742 m_editorCtrl->Hide();
743 wxPendingDelete.Append( m_editorCtrl );
777f9cef 744
1e510b1e
RR
745 if (!Validate(value))
746 return false;
900af470 747
1e510b1e 748 unsigned int col = GetOwner()->GetModelColumn();
e0062c04
RR
749 GetOwner()->GetOwner()->GetModel()->SetValue( value, m_item, col );
750 GetOwner()->GetOwner()->GetModel()->ValueChanged( m_item, col );
900af470 751
afebb87b
RR
752 // Now we should send Editing Done event
753 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, GetOwner()->GetOwner()->GetId() );
754 event.SetDataViewColumn( GetOwner() );
755 event.SetModel( GetOwner()->GetOwner()->GetModel() );
8fdaf7de 756 event.SetItem( m_item );
afebb87b 757 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event );
900af470 758
1e510b1e
RR
759 return true;
760}
761
762//-----------------------------------------------------------------------------
763// wxDataViewEditorCtrlEvtHandler
764//-----------------------------------------------------------------------------
765
766BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler, wxEvtHandler)
767 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar)
768 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus)
30715fa1 769 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle)
724852da 770 EVT_TEXT_ENTER (-1, wxDataViewEditorCtrlEvtHandler::OnTextEnter)
1e510b1e
RR
771END_EVENT_TABLE()
772
773wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
774 wxControl *editorCtrl,
775 wxDataViewRenderer *owner )
776{
777 m_owner = owner;
778 m_editorCtrl = editorCtrl;
779
780 m_finished = false;
781}
782
30715fa1
RR
783void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent &event )
784{
785 if (m_focusOnIdle)
786 {
787 m_focusOnIdle = false;
788 if (wxWindow::FindFocus() != m_editorCtrl)
789 m_editorCtrl->SetFocus();
790 }
900af470 791
30715fa1
RR
792 event.Skip();
793}
794
724852da
RR
795void wxDataViewEditorCtrlEvtHandler::OnTextEnter( wxCommandEvent &WXUNUSED(event) )
796{
797 m_finished = true;
798 m_owner->FinishEditing();
799}
800
1e510b1e
RR
801void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent &event )
802{
803 switch ( event.m_keyCode )
804 {
805 case WXK_RETURN:
806 m_finished = true;
807 m_owner->FinishEditing();
808 break;
809
810 case WXK_ESCAPE:
724852da 811 {
1e510b1e
RR
812 m_finished = true;
813 m_owner->CancelEditing();
814 break;
724852da 815 }
1e510b1e
RR
816 default:
817 event.Skip();
818 }
819}
820
821void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent &event )
822{
823 if (!m_finished)
824 {
825 m_finished = true;
826 m_owner->FinishEditing();
827 }
828
1e510b1e
RR
829 event.Skip();
830}
831
f554a14b 832// ---------------------------------------------------------
fa28826d 833// wxDataViewColumnBase
f554a14b 834// ---------------------------------------------------------
fa28826d 835
e2bfe673
VZ
836void wxDataViewColumnBase::Init(wxDataViewRenderer *renderer,
837 unsigned int model_column)
07a84e7b
RR
838{
839 m_renderer = renderer;
840 m_model_column = model_column;
07a84e7b
RR
841 m_owner = NULL;
842 m_renderer->SetOwner( (wxDataViewColumn*) this );
843}
844
6842a71a
RR
845wxDataViewColumnBase::~wxDataViewColumnBase()
846{
56873923 847 delete m_renderer;
07a84e7b
RR
848}
849
f554a14b 850// ---------------------------------------------------------
239eaa41 851// wxDataViewCtrlBase
f554a14b 852// ---------------------------------------------------------
239eaa41
RR
853
854IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl)
855
856wxDataViewCtrlBase::wxDataViewCtrlBase()
857{
858 m_model = NULL;
3b6280be
RR
859 m_expander_column = 0;
860 m_indent = 8;
239eaa41
RR
861}
862
863wxDataViewCtrlBase::~wxDataViewCtrlBase()
864{
87f0efe2
RR
865 if (m_model)
866 {
867 m_model->DecRef();
868 m_model = NULL;
869 }
239eaa41
RR
870}
871
e0062c04 872bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel *model )
239eaa41 873{
1821abd1
RR
874 if (m_model)
875 {
87f0efe2 876 m_model->DecRef(); // discard old model, if any
1821abd1 877 }
87f0efe2
RR
878
879 // add our own reference to the new model:
239eaa41 880 m_model = model;
1821abd1
RR
881 if (m_model)
882 {
900af470 883 m_model->IncRef();
1821abd1 884 }
f554a14b 885
239eaa41
RR
886 return true;
887}
888
e0062c04 889wxDataViewModel* wxDataViewCtrlBase::GetModel()
239eaa41
RR
890{
891 return m_model;
892}
893
a75124d0
RR
894const wxDataViewModel* wxDataViewCtrlBase::GetModel() const
895{
896 return m_model;
897}
898
15cac64f
RR
899bool wxDataViewCtrlBase::EnableDragSource( const wxDataFormat &WXUNUSED(format) )
900{
901 return false;
902}
903
4219d8b0 904void wxDataViewCtrlBase::ExpandAncestors( const wxDataViewItem & item )
a881f34e
RR
905{
906 if (!m_model) return;
4219d8b0
RR
907
908 if (!item.IsOk()) return;
a881f34e
RR
909
910 wxVector<wxDataViewItem> parentChain;
911
912 // at first we get all the parents of the selected item
913 wxDataViewItem parent = m_model->GetParent(item);
914 while (parent.IsOk())
915 {
916 parentChain.push_back(parent);
917 parent = m_model->GetParent(parent);
918 }
919
920 // then we expand the parents, starting at the root
921 while (!parentChain.empty())
922 {
923 Expand(parentChain.back());
924 parentChain.pop_back();
925 }
926}
927
c7074d44
RR
928wxDataViewColumn *
929wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column,
87f0efe2 930 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
fa28826d 931{
c7074d44 932 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 933 new wxDataViewTextRenderer( wxT("string"), mode ),
c7074d44
RR
934 model_column, width, align, flags );
935 AppendColumn( ret );
936 return ret;
fa28826d
RR
937}
938
b04fcede
RR
939wxDataViewColumn *
940wxDataViewCtrlBase::AppendIconTextColumn( const wxString &label, unsigned int model_column,
941 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
942{
943 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 944 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
b04fcede
RR
945 model_column, width, align, flags );
946 AppendColumn( ret );
947 return ret;
948}
949
c7074d44
RR
950wxDataViewColumn *
951wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column,
87f0efe2 952 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
605c2c4a 953{
c7074d44 954 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 955 new wxDataViewToggleRenderer( wxT("bool"), mode ),
c7074d44
RR
956 model_column, width, align, flags );
957 AppendColumn( ret );
958 return ret;
605c2c4a
RR
959}
960
c7074d44
RR
961wxDataViewColumn *
962wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column,
87f0efe2 963 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
ad63bf41 964{
c7074d44 965 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 966 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
c7074d44
RR
967 model_column, width, align, flags );
968 AppendColumn( ret );
969 return ret;
ad63bf41
RR
970}
971
c7074d44
RR
972wxDataViewColumn *
973wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column,
87f0efe2 974 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
4d496ecb 975{
c7074d44 976 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 977 new wxDataViewDateRenderer( wxT("datetime"), mode ),
c7074d44
RR
978 model_column, width, align, flags );
979 AppendColumn( ret );
980 return ret;
4d496ecb
RR
981}
982
c7074d44
RR
983wxDataViewColumn *
984wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column,
87f0efe2 985 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 986{
c7074d44 987 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 988 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
c7074d44
RR
989 model_column, width, align, flags );
990 AppendColumn( ret );
991 return ret;
07a84e7b
RR
992}
993
c7074d44
RR
994wxDataViewColumn *
995wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 996 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 997{
c7074d44 998 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 999 new wxDataViewTextRenderer( wxT("string"), mode ),
c7074d44
RR
1000 model_column, width, align, flags );
1001 AppendColumn( ret );
1002 return ret;
b04fcede
RR
1003}
1004
1005wxDataViewColumn *
1006wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap &label, unsigned int model_column,
1007 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1008{
1009 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1010 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
b04fcede
RR
1011 model_column, width, align, flags );
1012 AppendColumn( ret );
1013 return ret;
07a84e7b
RR
1014}
1015
c7074d44
RR
1016wxDataViewColumn *
1017wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1018 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1019{
c7074d44 1020 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1021 new wxDataViewToggleRenderer( wxT("bool"), mode ),
c7074d44
RR
1022 model_column, width, align, flags );
1023 AppendColumn( ret );
1024 return ret;
07a84e7b
RR
1025}
1026
c7074d44
RR
1027wxDataViewColumn *
1028wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1029 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1030{
c7074d44 1031 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1032 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
c7074d44
RR
1033 model_column, width, align, flags );
1034 AppendColumn( ret );
1035 return ret;
07a84e7b
RR
1036}
1037
c7074d44
RR
1038wxDataViewColumn *
1039wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1040 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1041{
c7074d44 1042 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1043 new wxDataViewDateRenderer( wxT("datetime"), mode ),
c7074d44
RR
1044 model_column, width, align, flags );
1045 AppendColumn( ret );
1046 return ret;
07a84e7b
RR
1047}
1048
c7074d44
RR
1049wxDataViewColumn *
1050wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1051 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1052{
c7074d44 1053 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1054 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
c7074d44
RR
1055 model_column, width, align, flags );
1056 AppendColumn( ret );
1057 return ret;
07a84e7b
RR
1058}
1059
736fe67c
RR
1060wxDataViewColumn *
1061wxDataViewCtrlBase::PrependTextColumn( const wxString &label, unsigned int model_column,
1062 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1063{
1064 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1065 new wxDataViewTextRenderer( wxT("string"), mode ),
736fe67c
RR
1066 model_column, width, align, flags );
1067 PrependColumn( ret );
1068 return ret;
1069}
1070
1071wxDataViewColumn *
1072wxDataViewCtrlBase::PrependIconTextColumn( const wxString &label, unsigned int model_column,
1073 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1074{
1075 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1076 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
736fe67c
RR
1077 model_column, width, align, flags );
1078 PrependColumn( ret );
1079 return ret;
1080}
1081
1082wxDataViewColumn *
1083wxDataViewCtrlBase::PrependToggleColumn( const wxString &label, unsigned int model_column,
1084 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1085{
1086
1087 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1088 new wxDataViewToggleRenderer( wxT("bool"), mode ),
736fe67c
RR
1089 model_column, width, align, flags );
1090 PrependColumn( ret );
1091 return ret;
1092}
1093
1094wxDataViewColumn *
1095wxDataViewCtrlBase::PrependProgressColumn( const wxString &label, unsigned int model_column,
1096 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1097{
1098 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1099 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
736fe67c
RR
1100 model_column, width, align, flags );
1101 PrependColumn( ret );
1102 return ret;
1103}
1104
1105wxDataViewColumn *
1106wxDataViewCtrlBase::PrependDateColumn( const wxString &label, unsigned int model_column,
1107 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1108{
1109 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1110 new wxDataViewDateRenderer( wxT("datetime"), mode ),
736fe67c
RR
1111 model_column, width, align, flags );
1112 PrependColumn( ret );
1113 return ret;
1114}
1115
1116wxDataViewColumn *
1117wxDataViewCtrlBase::PrependBitmapColumn( const wxString &label, unsigned int model_column,
1118 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1119{
1120 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1121 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
736fe67c
RR
1122 model_column, width, align, flags );
1123 PrependColumn( ret );
1124 return ret;
1125}
1126
1127wxDataViewColumn *
1128wxDataViewCtrlBase::PrependTextColumn( const wxBitmap &label, unsigned int model_column,
1129 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1130{
1131 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1132 new wxDataViewTextRenderer( wxT("string"), mode ),
736fe67c
RR
1133 model_column, width, align, flags );
1134 PrependColumn( ret );
1135 return ret;
1136}
1137
1138wxDataViewColumn *
1139wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap &label, unsigned int model_column,
1140 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1141{
1142 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1143 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
736fe67c
RR
1144 model_column, width, align, flags );
1145 PrependColumn( ret );
1146 return ret;
1147}
1148
1149wxDataViewColumn *
1150wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap &label, unsigned int model_column,
1151 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1152{
1153 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1154 new wxDataViewToggleRenderer( wxT("bool"), mode ),
736fe67c
RR
1155 model_column, width, align, flags );
1156 PrependColumn( ret );
1157 return ret;
1158}
1159
1160wxDataViewColumn *
1161wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap &label, unsigned int model_column,
1162 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1163{
1164 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1165 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
736fe67c
RR
1166 model_column, width, align, flags );
1167 PrependColumn( ret );
1168 return ret;
1169}
1170
1171wxDataViewColumn *
1172wxDataViewCtrlBase::PrependDateColumn( const wxBitmap &label, unsigned int model_column,
1173 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1174{
1175 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1176 new wxDataViewDateRenderer( wxT("datetime"), mode ),
736fe67c
RR
1177 model_column, width, align, flags );
1178 PrependColumn( ret );
1179 return ret;
1180}
1181
1182wxDataViewColumn *
1183wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap &label, unsigned int model_column,
1184 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1185{
1186 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1187 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
736fe67c
RR
1188 model_column, width, align, flags );
1189 PrependColumn( ret );
1190 return ret;
1191}
1192
c7074d44
RR
1193bool
1194wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
fa28826d 1195{
6842a71a 1196 col->SetOwner( (wxDataViewCtrl*) this );
fa28826d
RR
1197 return true;
1198}
1199
736fe67c
RR
1200bool
1201wxDataViewCtrlBase::PrependColumn( wxDataViewColumn *col )
1202{
1203 col->SetOwner( (wxDataViewCtrl*) this );
1204 return true;
1205}
1206
23318a53 1207bool
19723525
RR
1208wxDataViewCtrlBase::InsertColumn( unsigned int WXUNUSED(pos), wxDataViewColumn *col )
1209{
1210 col->SetOwner( (wxDataViewCtrl*) this );
1211 return true;
1212}
1213
eb7f97f8
RR
1214// ---------------------------------------------------------
1215// wxDataViewEvent
1216// ---------------------------------------------------------
1217
1218IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent)
1219
3c778901
VZ
1220wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, wxDataViewEvent )
1221
1222wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, wxDataViewEvent )
1223wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, wxDataViewEvent )
1224wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, wxDataViewEvent )
1225wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, wxDataViewEvent )
1226wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, wxDataViewEvent )
1227wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, wxDataViewEvent )
1228wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, wxDataViewEvent )
1229wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, wxDataViewEvent )
1230
1231wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, wxDataViewEvent )
1232
1233wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, wxDataViewEvent )
1234wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, wxDataViewEvent )
1235wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, wxDataViewEvent )
1236wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_REORDERED, wxDataViewEvent )
eb7f97f8 1237
591cc82d 1238wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, wxDataViewEvent )
52e750fc
RR
1239
1240// -------------------------------------
1241// wxDataViewSpinRenderer
1242// -------------------------------------
1243
1244wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min, int max, wxDataViewCellMode mode, int alignment ) :
99c75ebc 1245 wxDataViewCustomRenderer(wxT("long"), mode, alignment )
b5ec7dd6 1246{
52e750fc
RR
1247 m_min = min;
1248 m_max = max;
1249}
b5ec7dd6 1250
52e750fc 1251wxControl* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
b5ec7dd6 1252{
52e750fc 1253 long l = value;
659fb68d
RR
1254 wxSize size = labelRect.GetSize();
1255#ifdef __WXMAC__
1256 size = wxSize( wxMax(70,labelRect.width ), -1 );
1257#endif
1258 wxString str;
2ddfe921 1259 str.Printf( wxT("%d"), (int) l );
659fb68d 1260 wxSpinCtrl *sc = new wxSpinCtrl( parent, wxID_ANY, str,
724852da 1261 labelRect.GetTopLeft(), size, wxSP_ARROW_KEYS|wxTE_PROCESS_ENTER, m_min, m_max, l );
659fb68d
RR
1262#ifdef __WXMAC__
1263 size = sc->GetSize();
1264 wxPoint pt = sc->GetPosition();
1265 sc->SetSize( pt.x - 4, pt.y - 4, size.x, size.y );
1266#endif
777f9cef 1267
659fb68d 1268 return sc;
52e750fc 1269}
b5ec7dd6 1270
52e750fc 1271bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
b5ec7dd6 1272{
52e750fc
RR
1273 wxSpinCtrl *sc = (wxSpinCtrl*) editor;
1274 long l = sc->GetValue();
1275 value = l;
1276 return true;
1277}
b5ec7dd6 1278
52e750fc
RR
1279bool wxDataViewSpinRenderer::Render( wxRect rect, wxDC *dc, int state )
1280{
1281 wxString str;
99c75ebc 1282 str.Printf(wxT("%d"), (int) m_data );
52e750fc
RR
1283 RenderText( str, 0, rect, dc, state );
1284 return true;
1285}
b5ec7dd6 1286
52e750fc
RR
1287wxSize wxDataViewSpinRenderer::GetSize() const
1288{
1289 return wxSize(80,16);
1290}
b5ec7dd6 1291
52e750fc
RR
1292bool wxDataViewSpinRenderer::SetValue( const wxVariant &value )
1293{
1294 m_data = value.GetLong();
1295 return true;
1296}
b5ec7dd6 1297
52e750fc
RR
1298bool wxDataViewSpinRenderer::GetValue( wxVariant &value ) const
1299{
1300 value = m_data;
1301 return true;
1302}
1303
7448d67c
RR
1304// -------------------------------------
1305// wxDataViewChoiceRenderer
1306// -------------------------------------
1307
a881f34e 1308#if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXMAC__)
7448d67c
RR
1309
1310wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString& choices, wxDataViewCellMode mode, int alignment ) :
1311 wxDataViewCustomRenderer(wxT("string"), mode, alignment )
1312{
1313 m_choices = choices;
1314}
1315
1316wxControl* wxDataViewChoiceRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
1317{
1318 wxString s = value;
1319 wxSize size = labelRect.GetSize();
1320#ifdef __WXMAC__
1321 size = wxSize( wxMax(70,labelRect.width ), -1 );
1322#endif
1323 wxChoice *c = new wxChoice( parent, wxID_ANY, labelRect.GetTopLeft(), size, m_choices );
1324 c->SetStringSelection( value.GetString() );
1325
1326 return c;
1327}
1328
1329bool wxDataViewChoiceRenderer::GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
1330{
1331 wxChoice *c = (wxChoice*) editor;
1332 wxString s = c->GetStringSelection();
1333 value = s;
1334 return true;
1335}
1336
1337bool wxDataViewChoiceRenderer::Render( wxRect rect, wxDC *dc, int state )
1338{
1339 RenderText( m_data, 0, rect, dc, state );
1340 return true;
1341}
1342
1343wxSize wxDataViewChoiceRenderer::GetSize() const
1344{
1345 return wxSize(80,16);
1346}
1347
1348bool wxDataViewChoiceRenderer::SetValue( const wxVariant &value )
1349{
1350 m_data = value.GetString();
1351 return true;
1352}
1353
1354bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const
1355{
1356 value = m_data;
1357 return true;
1358}
1359
1360#endif
1361
8eff6c56
RR
1362//-----------------------------------------------------------------------------
1363// wxDataViewListStore
1364//-----------------------------------------------------------------------------
1365
1366wxDataViewListStore::wxDataViewListStore()
1367{
1368}
1369
1370wxDataViewListStore::~wxDataViewListStore()
1371{
1372 wxVector<wxDataViewListStoreLine*>::iterator it;
1373 for (it = m_data.begin(); it != m_data.end(); ++it)
1374 {
1375 wxDataViewListStoreLine* line = *it;
1376 delete line;
1377 }
1378}
1379
1380void wxDataViewListStore::PrependColumn( const wxString &varianttype )
1381{
1382 m_cols.Insert( varianttype, 0 );
1383}
1384
1385void wxDataViewListStore::InsertColumn( unsigned int pos, const wxString &varianttype )
1386{
1387 m_cols.Insert( varianttype, pos );
1388}
1389
1390void wxDataViewListStore::AppendColumn( const wxString &varianttype )
1391{
1392 m_cols.Add( varianttype );
1393}
1394
1395unsigned int wxDataViewListStore::GetColumnCount() const
1396{
1397 return m_cols.GetCount();
1398}
1399
1400wxString wxDataViewListStore::GetColumnType( unsigned int pos ) const
1401{
1402 return m_cols[pos];
1403}
1404
1405void wxDataViewListStore::AppendItem( const wxVector<wxVariant> &values, wxClientData *data )
1406{
1407 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1408 line->m_values = values;
1409 m_data.push_back( line );
1410
1411 RowAppended();
1412}
1413
1414void wxDataViewListStore::PrependItem( const wxVector<wxVariant> &values, wxClientData *data )
1415{
1416 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1417 line->m_values = values;
1418 m_data.insert( m_data.begin(), line );
1419
1420 RowPrepended();
1421}
1422
1423void wxDataViewListStore::InsertItem( unsigned int row, const wxVector<wxVariant> &values, wxClientData *data )
1424{
1425 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1426 line->m_values = values;
1427 m_data.insert( m_data.begin()+row, line );
1428
1429 RowInserted( row );
1430}
1431
1432void wxDataViewListStore::DeleteItem( unsigned row )
1433{
1434 wxVector<wxDataViewListStoreLine*>::iterator it = m_data.begin() + row;
1435 m_data.erase( it );
1436
1437 RowDeleted( row );
1438}
1439
1440void wxDataViewListStore::DeleteAllItems()
1441{
1442 wxVector<wxDataViewListStoreLine*>::iterator it;
1443 for (it = m_data.begin(); it != m_data.end(); ++it)
1444 {
1445 wxDataViewListStoreLine* line = *it;
1446 delete line;
1447 }
1448
1449 Reset( 0 );
1450}
1451
1452void wxDataViewListStore::GetValueByRow( wxVariant &value, unsigned int row, unsigned int col ) const
1453{
1454 wxDataViewListStoreLine *line = m_data[row];
1455 value = line->m_values[col];
1456}
1457
1458bool wxDataViewListStore::SetValueByRow( const wxVariant &value, unsigned int row, unsigned int col )
1459{
1460 wxDataViewListStoreLine *line = m_data[row];
1461 line->m_values[col] = value;
1462
1463 return true;
1464}
1465
dc813e6c
RR
1466//-----------------------------------------------------------------------------
1467// wxDataViewListCtrl
1468//-----------------------------------------------------------------------------
1469
1470IMPLEMENT_DYNAMIC_CLASS(wxDataViewListCtrl,wxDataViewCtrl)
1471
1472BEGIN_EVENT_TABLE(wxDataViewListCtrl,wxDataViewCtrl)
1473 EVT_SIZE( wxDataViewListCtrl::OnSize )
1474END_EVENT_TABLE()
1475
1476wxDataViewListCtrl::wxDataViewListCtrl()
1477{
1478}
1479
1480wxDataViewListCtrl::wxDataViewListCtrl( wxWindow *parent, wxWindowID id,
1481 const wxPoint& pos, const wxSize& size, long style,
1482 const wxValidator& validator )
1483{
1484 Create( parent, id, pos, size, style, validator );
1485
1486 wxDataViewListStore *store = new wxDataViewListStore;
1487 AssociateModel( store );
1488 store->DecRef();
1489}
1490
1491wxDataViewListCtrl::~wxDataViewListCtrl()
1492{
1493}
1494
1495
1496bool wxDataViewListCtrl::Create( wxWindow *parent, wxWindowID id,
1497 const wxPoint& pos, const wxSize& size, long style,
1498 const wxValidator& validator )
1499{
1500 return wxDataViewCtrl::Create( parent, id, pos, size, style, validator );
1501}
1502
95b20f41 1503bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *column, const wxString &varianttype )
dc813e6c
RR
1504{
1505 GetStore()->AppendColumn( varianttype );
95b20f41 1506 return wxDataViewCtrl::AppendColumn( column );
dc813e6c
RR
1507}
1508
95b20f41 1509bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *column, const wxString &varianttype )
dc813e6c
RR
1510{
1511 GetStore()->PrependColumn( varianttype );
95b20f41 1512 return wxDataViewCtrl::PrependColumn( column );
dc813e6c
RR
1513}
1514
95b20f41 1515bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *column, const wxString &varianttype )
dc813e6c
RR
1516{
1517 GetStore()->InsertColumn( pos, varianttype );
95b20f41
RR
1518 return wxDataViewCtrl::InsertColumn( pos, column );
1519}
1520
1521bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *col )
1522{
1523 return PrependColumn( col, "string" );
1524}
1525
1526bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
1527{
1528 return InsertColumn( pos, col, "string" );
1529}
1530
1531bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *col )
1532{
1533 return AppendColumn( col, "string" );
dc813e6c
RR
1534}
1535
95b20f41 1536wxDataViewColumn *wxDataViewListCtrl::AppendTextColumn( const wxString &label,
dc813e6c
RR
1537 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1538{
1539 GetStore()->AppendColumn( wxT("string") );
95b20f41
RR
1540
1541 wxDataViewColumn *ret = new wxDataViewColumn( label,
1542 new wxDataViewTextRenderer( wxT("string"), mode ),
1543 GetStore()->GetColumnCount()-1, width, align, flags );
1544
1545 wxDataViewCtrl::AppendColumn( ret );
1546
1547 return ret;
dc813e6c
RR
1548}
1549
95b20f41 1550wxDataViewColumn *wxDataViewListCtrl::AppendToggleColumn( const wxString &label,
dc813e6c
RR
1551 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1552{
1553 GetStore()->AppendColumn( wxT("bool") );
95b20f41
RR
1554
1555 wxDataViewColumn *ret = new wxDataViewColumn( label,
1556 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1557 GetStore()->GetColumnCount()-1, width, align, flags );
1558
1559 wxDataViewCtrl::AppendColumn( ret );
1560
1561 return ret;
dc813e6c
RR
1562}
1563
95b20f41 1564wxDataViewColumn *wxDataViewListCtrl::AppendProgressColumn( const wxString &label,
dc813e6c
RR
1565 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1566{
1567 GetStore()->AppendColumn( wxT("long") );
95b20f41
RR
1568
1569 wxDataViewColumn *ret = new wxDataViewColumn( label,
1570 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1571 GetStore()->GetColumnCount()-1, width, align, flags );
1572
1573 wxDataViewCtrl::AppendColumn( ret );
1574
1575 return ret;
dc813e6c
RR
1576}
1577
95b20f41 1578wxDataViewColumn *wxDataViewListCtrl::AppendIconTextColumn( const wxString &label,
dc813e6c
RR
1579 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1580{
1581 GetStore()->AppendColumn( wxT("wxDataViewIconText") );
95b20f41
RR
1582
1583 wxDataViewColumn *ret = new wxDataViewColumn( label,
1584 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1585 GetStore()->GetColumnCount()-1, width, align, flags );
1586
1587 wxDataViewCtrl::AppendColumn( ret );
1588
1589 return ret;
dc813e6c
RR
1590}
1591
1592void wxDataViewListCtrl::OnSize( wxSizeEvent &event )
1593{
1594 event.Skip( true );
1595}
1596
e94d0c1e
RR
1597//-----------------------------------------------------------------------------
1598// wxDataViewTreeStore
1599//-----------------------------------------------------------------------------
1600
b5ec7dd6
VZ
1601wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1602 wxDataViewTreeStoreNode *parent,
e94d0c1e
RR
1603 const wxString &text, const wxIcon &icon, wxClientData *data )
1604{
1605 m_parent = parent;
1606 m_text = text;
1607 m_icon = icon;
1608 m_data = data;
1609}
1610
1611wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1612{
1613 if (m_data)
1614 delete m_data;
1615}
b5ec7dd6 1616
e94d0c1e 1617#include "wx/listimpl.cpp"
a76c2f37 1618WX_DEFINE_LIST(wxDataViewTreeStoreNodeList)
e94d0c1e 1619
b5ec7dd6
VZ
1620wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1621 wxDataViewTreeStoreNode *parent, const wxString &text,
e94d0c1e
RR
1622 const wxIcon &icon, const wxIcon &expanded, wxClientData *data ) :
1623 wxDataViewTreeStoreNode( parent, text, icon, data )
1624{
1625 m_iconExpanded = expanded;
a75124d0 1626 m_isExpanded = false;
e94d0c1e
RR
1627 m_children.DeleteContents(true);
1628}
1629
1630wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1631{
1632}
1633
1634//-----------------------------------------------------------------------------
1635
1636wxDataViewTreeStore::wxDataViewTreeStore()
1637{
1638 m_root = new wxDataViewTreeStoreContainerNode( NULL, wxEmptyString );
1639}
1640
1641wxDataViewTreeStore::~wxDataViewTreeStore()
1642{
1643 delete m_root;
1644}
1645
b5ec7dd6 1646wxDataViewItem wxDataViewTreeStore::AppendItem( const wxDataViewItem& parent,
e94d0c1e
RR
1647 const wxString &text, const wxIcon &icon, wxClientData *data )
1648{
1649 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1650 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1651
1652 wxDataViewTreeStoreNode *node =
e94d0c1e
RR
1653 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1654 parent_node->GetChildren().Append( node );
b5ec7dd6 1655
e94d0c1e
RR
1656 return node->GetItem();
1657}
1658
1659wxDataViewItem wxDataViewTreeStore::PrependItem( const wxDataViewItem& parent,
1660 const wxString &text, const wxIcon &icon, wxClientData *data )
1661{
1662 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1663 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1664
1665 wxDataViewTreeStoreNode *node =
e94d0c1e
RR
1666 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1667 parent_node->GetChildren().Insert( node );
b5ec7dd6 1668
e94d0c1e
RR
1669 return node->GetItem();
1670}
1671
b5ec7dd6 1672wxDataViewItem
e700e296
RR
1673wxDataViewTreeStore::InsertItem(const wxDataViewItem& parent,
1674 const wxDataViewItem& previous,
1675 const wxString& text,
1676 const wxIcon& icon,
1677 wxClientData *data)
e94d0c1e 1678{
e700e296
RR
1679 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1680 if (!parent_node) return wxDataViewItem(0);
1681
1682 wxDataViewTreeStoreNode *previous_node = FindNode( previous );
1683 int pos = parent_node->GetChildren().IndexOf( previous_node );
1684 if (pos == wxNOT_FOUND) return wxDataViewItem(0);
1685
1686 wxDataViewTreeStoreNode *node =
1687 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1688 parent_node->GetChildren().Insert( (size_t) pos, node );
1689
1690 return node->GetItem();
e94d0c1e
RR
1691}
1692
b5ec7dd6
VZ
1693wxDataViewItem wxDataViewTreeStore::PrependContainer( const wxDataViewItem& parent,
1694 const wxString &text, const wxIcon &icon, const wxIcon &expanded,
e94d0c1e
RR
1695 wxClientData *data )
1696{
1697 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1698 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1699
1700 wxDataViewTreeStoreContainerNode *node =
e94d0c1e
RR
1701 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1702 parent_node->GetChildren().Insert( node );
b5ec7dd6 1703
e94d0c1e
RR
1704 return node->GetItem();
1705}
1706
b5ec7dd6
VZ
1707wxDataViewItem
1708wxDataViewTreeStore::AppendContainer(const wxDataViewItem& parent,
1709 const wxString &text,
1710 const wxIcon& icon,
1711 const wxIcon& expanded,
1712 wxClientData * data)
e94d0c1e
RR
1713{
1714 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1715 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1716
1717 wxDataViewTreeStoreContainerNode *node =
e94d0c1e
RR
1718 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1719 parent_node->GetChildren().Append( node );
b5ec7dd6 1720
e94d0c1e
RR
1721 return node->GetItem();
1722}
1723
b5ec7dd6 1724wxDataViewItem
e700e296
RR
1725wxDataViewTreeStore::InsertContainer(const wxDataViewItem& parent,
1726 const wxDataViewItem& previous,
1727 const wxString& text,
1728 const wxIcon& icon,
1729 const wxIcon& expanded,
1730 wxClientData * data)
e94d0c1e 1731{
e700e296
RR
1732 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1733 if (!parent_node) return wxDataViewItem(0);
1734
1735 wxDataViewTreeStoreNode *previous_node = FindNode( previous );
1736 int pos = parent_node->GetChildren().IndexOf( previous_node );
1737 if (pos == wxNOT_FOUND) return wxDataViewItem(0);
1738
1739 wxDataViewTreeStoreContainerNode *node =
1740 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1741 parent_node->GetChildren().Insert( (size_t) pos, node );
1742
1743 return node->GetItem();
e94d0c1e
RR
1744}
1745
1746wxDataViewItem wxDataViewTreeStore::GetNthChild( const wxDataViewItem& parent, unsigned int pos ) const
1747{
1748 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1749 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6 1750
e94d0c1e
RR
1751 wxDataViewTreeStoreNodeList::compatibility_iterator node = parent_node->GetChildren().Item( pos );
1752 if (node)
1753 return node->GetData();
b5ec7dd6 1754
e94d0c1e
RR
1755 return wxDataViewItem(0);
1756}
1757
1758int wxDataViewTreeStore::GetChildCount( const wxDataViewItem& parent ) const
1759{
1760 wxDataViewTreeStoreNode *node = FindNode( parent );
1761 if (!node) return -1;
b5ec7dd6 1762
e94d0c1e
RR
1763 if (!node->IsContainer())
1764 return 0;
b5ec7dd6 1765
e94d0c1e 1766 wxDataViewTreeStoreContainerNode *container_node = (wxDataViewTreeStoreContainerNode*) node;
b5ec7dd6 1767 return (int) container_node->GetChildren().GetCount();
e94d0c1e
RR
1768}
1769
1770void wxDataViewTreeStore::SetItemText( const wxDataViewItem& item, const wxString &text )
1771{
1772 wxDataViewTreeStoreNode *node = FindNode( item );
1773 if (!node) return;
b5ec7dd6 1774
e94d0c1e 1775 node->SetText( text );
e94d0c1e
RR
1776}
1777
1778wxString wxDataViewTreeStore::GetItemText( const wxDataViewItem& item ) const
1779{
1780 wxDataViewTreeStoreNode *node = FindNode( item );
1781 if (!node) return wxEmptyString;
b5ec7dd6 1782
e94d0c1e
RR
1783 return node->GetText();
1784}
1785
1786void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
1787{
1788 wxDataViewTreeStoreNode *node = FindNode( item );
1789 if (!node) return;
b5ec7dd6 1790
e94d0c1e 1791 node->SetIcon( icon );
e94d0c1e
RR
1792}
1793
1794const wxIcon &wxDataViewTreeStore::GetItemIcon( const wxDataViewItem& item ) const
1795{
1796 wxDataViewTreeStoreNode *node = FindNode( item );
1797 if (!node) return wxNullIcon;
b5ec7dd6 1798
e94d0c1e
RR
1799 return node->GetIcon();
1800}
1801
1802void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
1803{
1804 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1805 if (!node) return;
b5ec7dd6 1806
e94d0c1e 1807 node->SetExpandedIcon( icon );
e94d0c1e
RR
1808}
1809
1810const wxIcon &wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem& item ) const
1811{
1812 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1813 if (!node) return wxNullIcon;
b5ec7dd6 1814
e94d0c1e
RR
1815 return node->GetExpandedIcon();
1816}
1817
1818void wxDataViewTreeStore::SetItemData( const wxDataViewItem& item, wxClientData *data )
1819{
1820 wxDataViewTreeStoreNode *node = FindNode( item );
1821 if (!node) return;
b5ec7dd6 1822
e94d0c1e 1823 node->SetData( data );
e94d0c1e
RR
1824}
1825
1826wxClientData *wxDataViewTreeStore::GetItemData( const wxDataViewItem& item ) const
1827{
1828 wxDataViewTreeStoreNode *node = FindNode( item );
1829 if (!node) return NULL;
b5ec7dd6 1830
e94d0c1e
RR
1831 return node->GetData();
1832}
1833
1834void wxDataViewTreeStore::DeleteItem( const wxDataViewItem& item )
1835{
1836 if (!item.IsOk()) return;
1837
1838 wxDataViewItem parent_item = GetParent( item );
1839
1840 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent_item );
1841 if (!parent_node) return;
b5ec7dd6 1842
e94d0c1e
RR
1843 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1844 if (!node) return;
b5ec7dd6 1845
e94d0c1e 1846 parent_node->GetChildren().DeleteObject( node );
e94d0c1e
RR
1847}
1848
1849void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem& item )
1850{
1851 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1852 if (!node) return;
b5ec7dd6 1853
e94d0c1e 1854 node->GetChildren().clear();
e94d0c1e
RR
1855}
1856
1857void wxDataViewTreeStore::DeleteAllItems()
1858{
1859 // TODO
1860}
1861
b5ec7dd6
VZ
1862void
1863wxDataViewTreeStore::GetValue(wxVariant &variant,
1864 const wxDataViewItem &item,
1865 unsigned int WXUNUSED(col)) const
e94d0c1e
RR
1866{
1867 // if (col != 0) return;
b5ec7dd6 1868
e94d0c1e
RR
1869 wxDataViewTreeStoreNode *node = FindNode( item );
1870 if (!node) return;
b5ec7dd6 1871
a75124d0
RR
1872 wxIcon icon( node->GetIcon());
1873 if (node->IsContainer())
1874 {
1875 wxDataViewTreeStoreContainerNode *container = (wxDataViewTreeStoreContainerNode*) node;
1876 if (container->IsExpanded() && container->GetExpandedIcon().IsOk())
1877 icon = container->GetExpandedIcon();
1878 }
777f9cef 1879
a75124d0 1880 wxDataViewIconText data( node->GetText(), icon );
b5ec7dd6 1881
e94d0c1e
RR
1882 variant << data;
1883}
1884
b5ec7dd6
VZ
1885bool
1886wxDataViewTreeStore::SetValue(const wxVariant& variant,
1887 const wxDataViewItem& item,
1888 unsigned int WXUNUSED(col))
e94d0c1e
RR
1889{
1890 // if (col != 0) return false;
b5ec7dd6 1891
e94d0c1e
RR
1892 wxDataViewTreeStoreNode *node = FindNode( item );
1893 if (!node) return false;
b5ec7dd6 1894
e94d0c1e 1895 wxDataViewIconText data;
b5ec7dd6 1896
e94d0c1e 1897 data << variant;
b5ec7dd6 1898
e94d0c1e
RR
1899 node->SetText( data.GetText() );
1900 node->SetIcon( data.GetIcon() );
b5ec7dd6 1901
e94d0c1e
RR
1902 return true;
1903}
1904
1905wxDataViewItem wxDataViewTreeStore::GetParent( const wxDataViewItem &item ) const
1906{
1907 wxDataViewTreeStoreNode *node = FindNode( item );
1908 if (!node) return wxDataViewItem(0);
b5ec7dd6 1909
e94d0c1e
RR
1910 wxDataViewTreeStoreNode *parent = node->GetParent();
1911 if (!parent) return wxDataViewItem(0);
b5ec7dd6 1912
e94d0c1e
RR
1913 if (parent == m_root)
1914 return wxDataViewItem(0);
b5ec7dd6 1915
e94d0c1e
RR
1916 return parent->GetItem();
1917}
1918
1919bool wxDataViewTreeStore::IsContainer( const wxDataViewItem &item ) const
1920{
1921 wxDataViewTreeStoreNode *node = FindNode( item );
1922 if (!node) return false;
b5ec7dd6 1923
e94d0c1e
RR
1924 return node->IsContainer();
1925}
1926
1927unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
1928{
1929 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1930 if (!node) return 0;
1931
1932 wxDataViewTreeStoreNodeList::iterator iter;
1933 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
1934 {
1935 wxDataViewTreeStoreNode* child = *iter;
1936 children.Add( child->GetItem() );
1937 }
b5ec7dd6 1938
e94d0c1e
RR
1939 return node->GetChildren().GetCount();
1940}
1941
b5ec7dd6 1942int wxDataViewTreeStore::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
e94d0c1e
RR
1943 unsigned int WXUNUSED(column), bool WXUNUSED(ascending) )
1944{
1945 wxDataViewTreeStoreNode *node1 = FindNode( item1 );
1946 wxDataViewTreeStoreNode *node2 = FindNode( item2 );
b5ec7dd6 1947
e94d0c1e
RR
1948 if (!node1 || !node2)
1949 return 0;
b5ec7dd6
VZ
1950
1951 wxDataViewTreeStoreContainerNode* parent1 =
e94d0c1e 1952 (wxDataViewTreeStoreContainerNode*) node1->GetParent();
b5ec7dd6 1953 wxDataViewTreeStoreContainerNode* parent2 =
e94d0c1e 1954 (wxDataViewTreeStoreContainerNode*) node2->GetParent();
b5ec7dd6 1955
e94d0c1e
RR
1956 if (parent1 != parent2)
1957 {
1958 wxLogError( wxT("Comparing items with different parent.") );
1959 return 0;
1960 }
b5ec7dd6 1961
e94d0c1e
RR
1962 if (node1->IsContainer() && !!node2->IsContainer())
1963 return 1;
b5ec7dd6 1964
e94d0c1e
RR
1965 if (node2->IsContainer() && !!node1->IsContainer())
1966 return -1;
b5ec7dd6 1967
e94d0c1e
RR
1968 return parent1->GetChildren().IndexOf( node1 ) - parent1->GetChildren().IndexOf( node2 );
1969}
1970
1971wxDataViewTreeStoreNode *wxDataViewTreeStore::FindNode( const wxDataViewItem &item ) const
1972{
1973 if (!item.IsOk())
1974 return m_root;
b5ec7dd6 1975
e94d0c1e
RR
1976 return (wxDataViewTreeStoreNode*) item.GetID();
1977}
1978
1979wxDataViewTreeStoreContainerNode *wxDataViewTreeStore::FindContainerNode( const wxDataViewItem &item ) const
1980{
1981 if (!item.IsOk())
1982 return (wxDataViewTreeStoreContainerNode*) m_root;
1983
1984 wxDataViewTreeStoreNode* node = (wxDataViewTreeStoreNode*) item.GetID();
b5ec7dd6 1985
e94d0c1e
RR
1986 if (!node->IsContainer())
1987 return NULL;
b5ec7dd6 1988
e94d0c1e
RR
1989 return (wxDataViewTreeStoreContainerNode*) node;
1990}
1991
a75124d0
RR
1992//-----------------------------------------------------------------------------
1993// wxDataViewTreeCtrl
1994//-----------------------------------------------------------------------------
1995
1996IMPLEMENT_DYNAMIC_CLASS(wxDataViewTreeCtrl,wxDataViewCtrl)
1997
1998BEGIN_EVENT_TABLE(wxDataViewTreeCtrl,wxDataViewCtrl)
1999 EVT_DATAVIEW_ITEM_EXPANDED(-1, wxDataViewTreeCtrl::OnExpanded)
2000 EVT_DATAVIEW_ITEM_COLLAPSED(-1, wxDataViewTreeCtrl::OnCollapsed)
9c150d5f 2001 EVT_SIZE( wxDataViewTreeCtrl::OnSize )
a75124d0
RR
2002END_EVENT_TABLE()
2003
2004wxDataViewTreeCtrl::wxDataViewTreeCtrl()
2005{
2006 m_imageList = NULL;
2007}
2008
2009wxDataViewTreeCtrl::wxDataViewTreeCtrl( wxWindow *parent, wxWindowID id,
777f9cef 2010 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
a75124d0
RR
2011{
2012 m_imageList = NULL;
2013 Create( parent, id, pos, size, style, validator );
777f9cef 2014
a75124d0
RR
2015 wxDataViewTreeStore *store = new wxDataViewTreeStore;
2016 AssociateModel( store );
2017 store->DecRef();
777f9cef 2018
ad386793 2019 AppendIconTextColumn(wxString(),0,wxDATAVIEW_CELL_INERT,-1);
a75124d0
RR
2020}
2021
2022wxDataViewTreeCtrl::~wxDataViewTreeCtrl()
2023{
2024 if (m_imageList)
2025 delete m_imageList;
2026}
2027
2028bool wxDataViewTreeCtrl::Create( wxWindow *parent, wxWindowID id,
2029 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
2030{
2031 return wxDataViewCtrl::Create( parent, id, pos, size, style, validator );
2032}
2033
2034void wxDataViewTreeCtrl::SetImageList( wxImageList *imagelist )
2035{
2036 if (m_imageList)
2037 delete m_imageList;
2038
777f9cef 2039 m_imageList = imagelist;
a75124d0 2040}
777f9cef 2041
a75124d0
RR
2042wxDataViewItem wxDataViewTreeCtrl::AppendItem( const wxDataViewItem& parent,
2043 const wxString &text, int iconIndex, wxClientData *data )
2044{
2045 wxIcon icon = wxNullIcon;
2046 if (m_imageList && (iconIndex != -1))
2047 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2048
e700e296
RR
2049 wxDataViewItem res = GetStore()->AppendItem( parent, text, icon, data );
2050
2051 GetStore()->ItemAdded( parent, res );
2052
2053 return res;
a75124d0
RR
2054}
2055
2056wxDataViewItem wxDataViewTreeCtrl::PrependItem( const wxDataViewItem& parent,
2057 const wxString &text, int iconIndex, wxClientData *data )
2058{
2059 wxIcon icon = wxNullIcon;
2060 if (m_imageList && (iconIndex != -1))
2061 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2062
e700e296
RR
2063 wxDataViewItem res = GetStore()->PrependItem( parent, text, icon, data );
2064
2065 GetStore()->ItemAdded( parent, res );
2066
2067 return res;
a75124d0
RR
2068}
2069
2070wxDataViewItem wxDataViewTreeCtrl::InsertItem( const wxDataViewItem& parent, const wxDataViewItem& previous,
2071 const wxString &text, int iconIndex, wxClientData *data )
2072{
2073 wxIcon icon = wxNullIcon;
2074 if (m_imageList && (iconIndex != -1))
2075 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2076
e700e296
RR
2077 wxDataViewItem res = GetStore()->InsertItem( parent, previous, text, icon, data );
2078
2079 GetStore()->ItemAdded( parent, res );
2080
2081 return res;
a75124d0
RR
2082}
2083
2084wxDataViewItem wxDataViewTreeCtrl::PrependContainer( const wxDataViewItem& parent,
2085 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2086{
2087 wxIcon icon = wxNullIcon;
2088 if (m_imageList && (iconIndex != -1))
2089 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2090
a75124d0
RR
2091 wxIcon expanded = wxNullIcon;
2092 if (m_imageList && (expandedIndex != -1))
2093 expanded = m_imageList->GetIcon( expandedIndex );
777f9cef 2094
e700e296
RR
2095 wxDataViewItem res = GetStore()->PrependContainer( parent, text, icon, expanded, data );
2096
2097 GetStore()->ItemAdded( parent, res );
2098
2099 return res;
a75124d0
RR
2100}
2101
2102wxDataViewItem wxDataViewTreeCtrl::AppendContainer( const wxDataViewItem& parent,
2103 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2104{
2105 wxIcon icon = wxNullIcon;
2106 if (m_imageList && (iconIndex != -1))
2107 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2108
a75124d0
RR
2109 wxIcon expanded = wxNullIcon;
2110 if (m_imageList && (expandedIndex != -1))
2111 expanded = m_imageList->GetIcon( expandedIndex );
777f9cef 2112
e700e296
RR
2113 wxDataViewItem res = GetStore()->AppendContainer( parent, text, icon, expanded, data );
2114
2115 GetStore()->ItemAdded( parent, res );
2116
2117 return res;
a75124d0
RR
2118}
2119
2120wxDataViewItem wxDataViewTreeCtrl::InsertContainer( const wxDataViewItem& parent, const wxDataViewItem& previous,
2121 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2122{
2123 wxIcon icon = wxNullIcon;
2124 if (m_imageList && (iconIndex != -1))
2125 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2126
a75124d0
RR
2127 wxIcon expanded = wxNullIcon;
2128 if (m_imageList && (expandedIndex != -1))
2129 expanded = m_imageList->GetIcon( expandedIndex );
777f9cef 2130
e700e296
RR
2131 wxDataViewItem res = GetStore()->InsertContainer( parent, previous, text, icon, expanded, data );
2132
2133 GetStore()->ItemAdded( parent, res );
2134
2135 return res;
2136}
2137
2138void wxDataViewTreeCtrl::SetItemText( const wxDataViewItem& item, const wxString &text )
2139{
2140 GetStore()->SetItemText(item,text);
2141
2142 // notify control
2143 GetStore()->ValueChanged( item, 0 );
2144}
2145
2146void wxDataViewTreeCtrl::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
2147{
2148 GetStore()->SetItemIcon(item,icon);
2149
2150 // notify control
2151 GetStore()->ValueChanged( item, 0 );
2152}
2153
2154void wxDataViewTreeCtrl::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
2155{
2156 GetStore()->SetItemExpandedIcon(item,icon);
2157
2158 // notify control
2159 GetStore()->ValueChanged( item, 0 );
2160}
2161
2162void wxDataViewTreeCtrl::DeleteItem( const wxDataViewItem& item )
2163{
2164 wxDataViewItem parent_item = GetStore()->GetParent( item );
2165
2166 GetStore()->DeleteItem(item);
2167
2168 // notify control
2169 GetStore()->ItemDeleted( parent_item, item );
2170}
2171
2172void wxDataViewTreeCtrl::DeleteChildren( const wxDataViewItem& item )
2173{
2f21b2be
RR
2174 wxDataViewTreeStoreContainerNode *node = GetStore()->FindContainerNode( item );
2175 if (!node) return;
2176
2177 wxDataViewItemArray array;
2178 wxDataViewTreeStoreNodeList::iterator iter;
2179 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
2180 {
2181 wxDataViewTreeStoreNode* child = *iter;
2182 array.Add( child->GetItem() );
2183 }
2184
2185 GetStore()->DeleteChildren( item );
2186
2187 // notify control
2188 GetStore()->ItemsDeleted( item, array );
e700e296
RR
2189}
2190
2191void wxDataViewTreeCtrl::DeleteAllItems()
2192{
2193 GetStore()->DeleteAllItems();
2194
2195 GetStore()->Cleared();
a75124d0
RR
2196}
2197
a75124d0
RR
2198void wxDataViewTreeCtrl::OnExpanded( wxDataViewEvent &event )
2199{
2200 if (m_imageList) return;
777f9cef 2201
a75124d0
RR
2202 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
2203 if (!container) return;
777f9cef 2204
a75124d0 2205 container->SetExpanded( true );
e700e296 2206
a75124d0
RR
2207 GetStore()->ItemChanged( event.GetItem() );
2208}
2209
2210void wxDataViewTreeCtrl::OnCollapsed( wxDataViewEvent &event )
2211{
2212 if (m_imageList) return;
777f9cef 2213
a75124d0
RR
2214 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
2215 if (!container) return;
777f9cef 2216
a75124d0 2217 container->SetExpanded( false );
e700e296 2218
a75124d0
RR
2219 GetStore()->ItemChanged( event.GetItem() );
2220}
2221
a6aaf49f 2222void wxDataViewTreeCtrl::OnSize( wxSizeEvent &event )
1a07a730 2223{
ad386793 2224#if defined(wxUSE_GENERICDATAVIEWCTRL)
4bfd0ed5
VZ
2225 // automatically resize our only column to take the entire control width
2226 if ( GetColumnCount() )
2227 {
2228 wxSize size = GetClientSize();
2229 GetColumn(0)->SetWidth(size.x);
2230 }
1a07a730 2231#endif
a6aaf49f 2232 event.Skip( true );
1a07a730 2233}
a75124d0 2234
b5ec7dd6 2235#endif // wxUSE_DATAVIEWCTRL
e0000f94 2236