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