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