]> git.saurik.com Git - wxWidgets.git/blame - src/common/datavcmn.cpp
return radio box own help text when origin is Origin_Unknown to make sure GetHelpText...
[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
4219d8b0 897void wxDataViewCtrlBase::ExpandAncestors( const wxDataViewItem & item )
a881f34e
RR
898{
899 if (!m_model) return;
8c2654ce 900
4219d8b0 901 if (!item.IsOk()) return;
a881f34e
RR
902
903 wxVector<wxDataViewItem> parentChain;
8c2654ce 904
a881f34e
RR
905 // at first we get all the parents of the selected item
906 wxDataViewItem parent = m_model->GetParent(item);
907 while (parent.IsOk())
908 {
909 parentChain.push_back(parent);
910 parent = m_model->GetParent(parent);
911 }
8c2654ce 912
a881f34e
RR
913 // then we expand the parents, starting at the root
914 while (!parentChain.empty())
915 {
916 Expand(parentChain.back());
917 parentChain.pop_back();
918 }
919}
920
c7074d44
RR
921wxDataViewColumn *
922wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column,
87f0efe2 923 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
fa28826d 924{
c7074d44 925 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 926 new wxDataViewTextRenderer( wxT("string"), mode ),
c7074d44
RR
927 model_column, width, align, flags );
928 AppendColumn( ret );
929 return ret;
fa28826d
RR
930}
931
b04fcede
RR
932wxDataViewColumn *
933wxDataViewCtrlBase::AppendIconTextColumn( const wxString &label, unsigned int model_column,
934 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
935{
936 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 937 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
b04fcede
RR
938 model_column, width, align, flags );
939 AppendColumn( ret );
940 return ret;
941}
942
c7074d44
RR
943wxDataViewColumn *
944wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column,
87f0efe2 945 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
605c2c4a 946{
c7074d44 947 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 948 new wxDataViewToggleRenderer( wxT("bool"), mode ),
c7074d44
RR
949 model_column, width, align, flags );
950 AppendColumn( ret );
951 return ret;
605c2c4a
RR
952}
953
c7074d44
RR
954wxDataViewColumn *
955wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column,
87f0efe2 956 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
ad63bf41 957{
c7074d44 958 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 959 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
c7074d44
RR
960 model_column, width, align, flags );
961 AppendColumn( ret );
962 return ret;
ad63bf41
RR
963}
964
c7074d44
RR
965wxDataViewColumn *
966wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column,
87f0efe2 967 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
4d496ecb 968{
c7074d44 969 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 970 new wxDataViewDateRenderer( wxT("datetime"), mode ),
c7074d44
RR
971 model_column, width, align, flags );
972 AppendColumn( ret );
973 return ret;
4d496ecb
RR
974}
975
c7074d44
RR
976wxDataViewColumn *
977wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column,
87f0efe2 978 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 979{
c7074d44 980 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 981 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
c7074d44
RR
982 model_column, width, align, flags );
983 AppendColumn( ret );
984 return ret;
07a84e7b
RR
985}
986
c7074d44
RR
987wxDataViewColumn *
988wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 989 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 990{
c7074d44 991 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 992 new wxDataViewTextRenderer( wxT("string"), mode ),
c7074d44
RR
993 model_column, width, align, flags );
994 AppendColumn( ret );
995 return ret;
b04fcede
RR
996}
997
998wxDataViewColumn *
999wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap &label, unsigned int model_column,
1000 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1001{
1002 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1003 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
b04fcede
RR
1004 model_column, width, align, flags );
1005 AppendColumn( ret );
1006 return ret;
07a84e7b
RR
1007}
1008
c7074d44
RR
1009wxDataViewColumn *
1010wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1011 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1012{
c7074d44 1013 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1014 new wxDataViewToggleRenderer( wxT("bool"), mode ),
c7074d44
RR
1015 model_column, width, align, flags );
1016 AppendColumn( ret );
1017 return ret;
07a84e7b
RR
1018}
1019
c7074d44
RR
1020wxDataViewColumn *
1021wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1022 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1023{
c7074d44 1024 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1025 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
c7074d44
RR
1026 model_column, width, align, flags );
1027 AppendColumn( ret );
1028 return ret;
07a84e7b
RR
1029}
1030
c7074d44
RR
1031wxDataViewColumn *
1032wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1033 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1034{
c7074d44 1035 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1036 new wxDataViewDateRenderer( wxT("datetime"), mode ),
c7074d44
RR
1037 model_column, width, align, flags );
1038 AppendColumn( ret );
1039 return ret;
07a84e7b
RR
1040}
1041
c7074d44
RR
1042wxDataViewColumn *
1043wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1044 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1045{
c7074d44 1046 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1047 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
c7074d44
RR
1048 model_column, width, align, flags );
1049 AppendColumn( ret );
1050 return ret;
07a84e7b
RR
1051}
1052
736fe67c
RR
1053wxDataViewColumn *
1054wxDataViewCtrlBase::PrependTextColumn( const wxString &label, unsigned int model_column,
1055 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1056{
1057 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1058 new wxDataViewTextRenderer( wxT("string"), mode ),
736fe67c
RR
1059 model_column, width, align, flags );
1060 PrependColumn( ret );
1061 return ret;
1062}
1063
1064wxDataViewColumn *
1065wxDataViewCtrlBase::PrependIconTextColumn( const wxString &label, unsigned int model_column,
1066 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1067{
1068 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1069 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
736fe67c
RR
1070 model_column, width, align, flags );
1071 PrependColumn( ret );
1072 return ret;
1073}
1074
1075wxDataViewColumn *
1076wxDataViewCtrlBase::PrependToggleColumn( const wxString &label, unsigned int model_column,
1077 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1078{
1079
1080 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1081 new wxDataViewToggleRenderer( wxT("bool"), mode ),
736fe67c
RR
1082 model_column, width, align, flags );
1083 PrependColumn( ret );
1084 return ret;
1085}
1086
1087wxDataViewColumn *
1088wxDataViewCtrlBase::PrependProgressColumn( const wxString &label, unsigned int model_column,
1089 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1090{
1091 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1092 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
736fe67c
RR
1093 model_column, width, align, flags );
1094 PrependColumn( ret );
1095 return ret;
1096}
1097
1098wxDataViewColumn *
1099wxDataViewCtrlBase::PrependDateColumn( const wxString &label, unsigned int model_column,
1100 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1101{
1102 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1103 new wxDataViewDateRenderer( wxT("datetime"), mode ),
736fe67c
RR
1104 model_column, width, align, flags );
1105 PrependColumn( ret );
1106 return ret;
1107}
1108
1109wxDataViewColumn *
1110wxDataViewCtrlBase::PrependBitmapColumn( const wxString &label, unsigned int model_column,
1111 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1112{
1113 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1114 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
736fe67c
RR
1115 model_column, width, align, flags );
1116 PrependColumn( ret );
1117 return ret;
1118}
1119
1120wxDataViewColumn *
1121wxDataViewCtrlBase::PrependTextColumn( const wxBitmap &label, unsigned int model_column,
1122 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1123{
1124 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1125 new wxDataViewTextRenderer( wxT("string"), mode ),
736fe67c
RR
1126 model_column, width, align, flags );
1127 PrependColumn( ret );
1128 return ret;
1129}
1130
1131wxDataViewColumn *
1132wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap &label, unsigned int model_column,
1133 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1134{
1135 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1136 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
736fe67c
RR
1137 model_column, width, align, flags );
1138 PrependColumn( ret );
1139 return ret;
1140}
1141
1142wxDataViewColumn *
1143wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap &label, unsigned int model_column,
1144 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1145{
1146 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1147 new wxDataViewToggleRenderer( wxT("bool"), mode ),
736fe67c
RR
1148 model_column, width, align, flags );
1149 PrependColumn( ret );
1150 return ret;
1151}
1152
1153wxDataViewColumn *
1154wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap &label, unsigned int model_column,
1155 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1156{
1157 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1158 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
736fe67c
RR
1159 model_column, width, align, flags );
1160 PrependColumn( ret );
1161 return ret;
1162}
1163
1164wxDataViewColumn *
1165wxDataViewCtrlBase::PrependDateColumn( const wxBitmap &label, unsigned int model_column,
1166 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1167{
1168 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1169 new wxDataViewDateRenderer( wxT("datetime"), mode ),
736fe67c
RR
1170 model_column, width, align, flags );
1171 PrependColumn( ret );
1172 return ret;
1173}
1174
1175wxDataViewColumn *
1176wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap &label, unsigned int model_column,
1177 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1178{
1179 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1180 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
736fe67c
RR
1181 model_column, width, align, flags );
1182 PrependColumn( ret );
1183 return ret;
1184}
1185
c7074d44
RR
1186bool
1187wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
fa28826d 1188{
6842a71a 1189 col->SetOwner( (wxDataViewCtrl*) this );
fa28826d
RR
1190 return true;
1191}
1192
736fe67c
RR
1193bool
1194wxDataViewCtrlBase::PrependColumn( wxDataViewColumn *col )
1195{
1196 col->SetOwner( (wxDataViewCtrl*) this );
1197 return true;
1198}
1199
23318a53 1200bool
19723525
RR
1201wxDataViewCtrlBase::InsertColumn( unsigned int WXUNUSED(pos), wxDataViewColumn *col )
1202{
1203 col->SetOwner( (wxDataViewCtrl*) this );
1204 return true;
1205}
1206
eb7f97f8
RR
1207// ---------------------------------------------------------
1208// wxDataViewEvent
1209// ---------------------------------------------------------
1210
1211IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent)
1212
9b11752c
VZ
1213wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, wxDataViewEvent );
1214
1215wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, wxDataViewEvent );
1216wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, wxDataViewEvent );
1217wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, wxDataViewEvent );
1218wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, wxDataViewEvent );
1219wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, wxDataViewEvent );
1220wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, wxDataViewEvent );
1221wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, wxDataViewEvent );
1222wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, wxDataViewEvent );
1223
1224wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, wxDataViewEvent );
1225
1226wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, wxDataViewEvent );
1227wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, wxDataViewEvent );
1228wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, wxDataViewEvent );
1229wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_REORDERED, wxDataViewEvent );
1230
1231wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, wxDataViewEvent );
1232wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, wxDataViewEvent );
1233wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, wxDataViewEvent );
52e750fc
RR
1234
1235// -------------------------------------
1236// wxDataViewSpinRenderer
1237// -------------------------------------
1238
1239wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min, int max, wxDataViewCellMode mode, int alignment ) :
99c75ebc 1240 wxDataViewCustomRenderer(wxT("long"), mode, alignment )
b5ec7dd6 1241{
52e750fc
RR
1242 m_min = min;
1243 m_max = max;
1244}
b5ec7dd6 1245
52e750fc 1246wxControl* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
b5ec7dd6 1247{
52e750fc 1248 long l = value;
659fb68d
RR
1249 wxSize size = labelRect.GetSize();
1250#ifdef __WXMAC__
1251 size = wxSize( wxMax(70,labelRect.width ), -1 );
1252#endif
1253 wxString str;
2ddfe921 1254 str.Printf( wxT("%d"), (int) l );
659fb68d 1255 wxSpinCtrl *sc = new wxSpinCtrl( parent, wxID_ANY, str,
724852da 1256 labelRect.GetTopLeft(), size, wxSP_ARROW_KEYS|wxTE_PROCESS_ENTER, m_min, m_max, l );
659fb68d
RR
1257#ifdef __WXMAC__
1258 size = sc->GetSize();
1259 wxPoint pt = sc->GetPosition();
1260 sc->SetSize( pt.x - 4, pt.y - 4, size.x, size.y );
1261#endif
777f9cef 1262
659fb68d 1263 return sc;
52e750fc 1264}
b5ec7dd6 1265
52e750fc 1266bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
b5ec7dd6 1267{
52e750fc
RR
1268 wxSpinCtrl *sc = (wxSpinCtrl*) editor;
1269 long l = sc->GetValue();
1270 value = l;
1271 return true;
1272}
b5ec7dd6 1273
52e750fc
RR
1274bool wxDataViewSpinRenderer::Render( wxRect rect, wxDC *dc, int state )
1275{
1276 wxString str;
99c75ebc 1277 str.Printf(wxT("%d"), (int) m_data );
52e750fc
RR
1278 RenderText( str, 0, rect, dc, state );
1279 return true;
1280}
b5ec7dd6 1281
52e750fc
RR
1282wxSize wxDataViewSpinRenderer::GetSize() const
1283{
1284 return wxSize(80,16);
1285}
b5ec7dd6 1286
52e750fc
RR
1287bool wxDataViewSpinRenderer::SetValue( const wxVariant &value )
1288{
1289 m_data = value.GetLong();
1290 return true;
1291}
b5ec7dd6 1292
52e750fc
RR
1293bool wxDataViewSpinRenderer::GetValue( wxVariant &value ) const
1294{
1295 value = m_data;
1296 return true;
1297}
1298
7448d67c
RR
1299// -------------------------------------
1300// wxDataViewChoiceRenderer
1301// -------------------------------------
1302
a881f34e 1303#if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXMAC__)
7448d67c
RR
1304
1305wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString& choices, wxDataViewCellMode mode, int alignment ) :
1306 wxDataViewCustomRenderer(wxT("string"), mode, alignment )
1307{
1308 m_choices = choices;
1309}
1310
1311wxControl* wxDataViewChoiceRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
1312{
1313 wxString s = value;
1314 wxSize size = labelRect.GetSize();
1315#ifdef __WXMAC__
1316 size = wxSize( wxMax(70,labelRect.width ), -1 );
1317#endif
1318 wxChoice *c = new wxChoice( parent, wxID_ANY, labelRect.GetTopLeft(), size, m_choices );
1319 c->SetStringSelection( value.GetString() );
1320
1321 return c;
1322}
1323
1324bool wxDataViewChoiceRenderer::GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
1325{
1326 wxChoice *c = (wxChoice*) editor;
1327 wxString s = c->GetStringSelection();
1328 value = s;
1329 return true;
1330}
1331
1332bool wxDataViewChoiceRenderer::Render( wxRect rect, wxDC *dc, int state )
1333{
1334 RenderText( m_data, 0, rect, dc, state );
1335 return true;
1336}
1337
1338wxSize wxDataViewChoiceRenderer::GetSize() const
1339{
1340 return wxSize(80,16);
1341}
1342
1343bool wxDataViewChoiceRenderer::SetValue( const wxVariant &value )
1344{
1345 m_data = value.GetString();
1346 return true;
1347}
1348
1349bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const
1350{
1351 value = m_data;
1352 return true;
1353}
1354
1355#endif
1356
8eff6c56
RR
1357//-----------------------------------------------------------------------------
1358// wxDataViewListStore
1359//-----------------------------------------------------------------------------
1360
1361wxDataViewListStore::wxDataViewListStore()
1362{
1363}
1364
1365wxDataViewListStore::~wxDataViewListStore()
1366{
1367 wxVector<wxDataViewListStoreLine*>::iterator it;
1368 for (it = m_data.begin(); it != m_data.end(); ++it)
1369 {
1370 wxDataViewListStoreLine* line = *it;
1371 delete line;
1372 }
1373}
1374
1375void wxDataViewListStore::PrependColumn( const wxString &varianttype )
1376{
1377 m_cols.Insert( varianttype, 0 );
1378}
1379
1380void wxDataViewListStore::InsertColumn( unsigned int pos, const wxString &varianttype )
1381{
1382 m_cols.Insert( varianttype, pos );
1383}
1384
1385void wxDataViewListStore::AppendColumn( const wxString &varianttype )
1386{
1387 m_cols.Add( varianttype );
1388}
1389
1390unsigned int wxDataViewListStore::GetColumnCount() const
1391{
1392 return m_cols.GetCount();
1393}
1394
1395wxString wxDataViewListStore::GetColumnType( unsigned int pos ) const
1396{
1397 return m_cols[pos];
1398}
8c2654ce 1399
8eff6c56
RR
1400void wxDataViewListStore::AppendItem( const wxVector<wxVariant> &values, wxClientData *data )
1401{
1402 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1403 line->m_values = values;
1404 m_data.push_back( line );
8c2654ce 1405
8eff6c56
RR
1406 RowAppended();
1407}
1408
1409void wxDataViewListStore::PrependItem( const wxVector<wxVariant> &values, wxClientData *data )
1410{
1411 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1412 line->m_values = values;
1413 m_data.insert( m_data.begin(), line );
8c2654ce 1414
8eff6c56
RR
1415 RowPrepended();
1416}
1417
1418void wxDataViewListStore::InsertItem( unsigned int row, const wxVector<wxVariant> &values, wxClientData *data )
1419{
1420 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1421 line->m_values = values;
1422 m_data.insert( m_data.begin()+row, line );
8c2654ce 1423
8eff6c56
RR
1424 RowInserted( row );
1425}
1426
1427void wxDataViewListStore::DeleteItem( unsigned row )
1428{
1429 wxVector<wxDataViewListStoreLine*>::iterator it = m_data.begin() + row;
1430 m_data.erase( it );
8c2654ce 1431
8eff6c56
RR
1432 RowDeleted( row );
1433}
1434
1435void wxDataViewListStore::DeleteAllItems()
1436{
1437 wxVector<wxDataViewListStoreLine*>::iterator it;
1438 for (it = m_data.begin(); it != m_data.end(); ++it)
1439 {
1440 wxDataViewListStoreLine* line = *it;
1441 delete line;
1442 }
8c2654ce 1443
8eff6c56
RR
1444 Reset( 0 );
1445}
1446
1447void wxDataViewListStore::GetValueByRow( wxVariant &value, unsigned int row, unsigned int col ) const
1448{
1449 wxDataViewListStoreLine *line = m_data[row];
1450 value = line->m_values[col];
1451}
1452
1453bool wxDataViewListStore::SetValueByRow( const wxVariant &value, unsigned int row, unsigned int col )
1454{
1455 wxDataViewListStoreLine *line = m_data[row];
1456 line->m_values[col] = value;
8c2654ce 1457
8eff6c56
RR
1458 return true;
1459}
1460
dc813e6c
RR
1461//-----------------------------------------------------------------------------
1462// wxDataViewListCtrl
1463//-----------------------------------------------------------------------------
1464
1465IMPLEMENT_DYNAMIC_CLASS(wxDataViewListCtrl,wxDataViewCtrl)
1466
1467BEGIN_EVENT_TABLE(wxDataViewListCtrl,wxDataViewCtrl)
1468 EVT_SIZE( wxDataViewListCtrl::OnSize )
1469END_EVENT_TABLE()
1470
1471wxDataViewListCtrl::wxDataViewListCtrl()
1472{
1473}
1474
1475wxDataViewListCtrl::wxDataViewListCtrl( wxWindow *parent, wxWindowID id,
1476 const wxPoint& pos, const wxSize& size, long style,
1477 const wxValidator& validator )
1478{
1479 Create( parent, id, pos, size, style, validator );
1480
1481 wxDataViewListStore *store = new wxDataViewListStore;
1482 AssociateModel( store );
1483 store->DecRef();
1484}
1485
1486wxDataViewListCtrl::~wxDataViewListCtrl()
1487{
1488}
1489
1490
1491bool wxDataViewListCtrl::Create( wxWindow *parent, wxWindowID id,
1492 const wxPoint& pos, const wxSize& size, long style,
1493 const wxValidator& validator )
1494{
1495 return wxDataViewCtrl::Create( parent, id, pos, size, style, validator );
1496}
1497
95b20f41 1498bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *column, const wxString &varianttype )
dc813e6c
RR
1499{
1500 GetStore()->AppendColumn( varianttype );
95b20f41 1501 return wxDataViewCtrl::AppendColumn( column );
dc813e6c
RR
1502}
1503
95b20f41 1504bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *column, const wxString &varianttype )
dc813e6c
RR
1505{
1506 GetStore()->PrependColumn( varianttype );
95b20f41 1507 return wxDataViewCtrl::PrependColumn( column );
dc813e6c
RR
1508}
1509
95b20f41 1510bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *column, const wxString &varianttype )
dc813e6c
RR
1511{
1512 GetStore()->InsertColumn( pos, varianttype );
95b20f41
RR
1513 return wxDataViewCtrl::InsertColumn( pos, column );
1514}
1515
1516bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *col )
1517{
1518 return PrependColumn( col, "string" );
1519}
1520
1521bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
1522{
1523 return InsertColumn( pos, col, "string" );
1524}
1525
1526bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *col )
1527{
1528 return AppendColumn( col, "string" );
dc813e6c 1529}
8c2654ce 1530
95b20f41 1531wxDataViewColumn *wxDataViewListCtrl::AppendTextColumn( const wxString &label,
dc813e6c
RR
1532 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1533{
1534 GetStore()->AppendColumn( wxT("string") );
8c2654ce 1535
95b20f41
RR
1536 wxDataViewColumn *ret = new wxDataViewColumn( label,
1537 new wxDataViewTextRenderer( wxT("string"), mode ),
1538 GetStore()->GetColumnCount()-1, width, align, flags );
8c2654ce 1539
95b20f41 1540 wxDataViewCtrl::AppendColumn( ret );
8c2654ce 1541
95b20f41 1542 return ret;
dc813e6c
RR
1543}
1544
95b20f41 1545wxDataViewColumn *wxDataViewListCtrl::AppendToggleColumn( const wxString &label,
dc813e6c
RR
1546 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1547{
1548 GetStore()->AppendColumn( wxT("bool") );
8c2654ce 1549
95b20f41
RR
1550 wxDataViewColumn *ret = new wxDataViewColumn( label,
1551 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1552 GetStore()->GetColumnCount()-1, width, align, flags );
8c2654ce 1553
95b20f41 1554 wxDataViewCtrl::AppendColumn( ret );
8c2654ce 1555
95b20f41 1556 return ret;
dc813e6c
RR
1557}
1558
95b20f41 1559wxDataViewColumn *wxDataViewListCtrl::AppendProgressColumn( const wxString &label,
dc813e6c
RR
1560 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1561{
1562 GetStore()->AppendColumn( wxT("long") );
8c2654ce 1563
95b20f41
RR
1564 wxDataViewColumn *ret = new wxDataViewColumn( label,
1565 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1566 GetStore()->GetColumnCount()-1, width, align, flags );
8c2654ce 1567
95b20f41 1568 wxDataViewCtrl::AppendColumn( ret );
8c2654ce 1569
95b20f41 1570 return ret;
dc813e6c
RR
1571}
1572
95b20f41 1573wxDataViewColumn *wxDataViewListCtrl::AppendIconTextColumn( const wxString &label,
dc813e6c
RR
1574 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1575{
1576 GetStore()->AppendColumn( wxT("wxDataViewIconText") );
8c2654ce 1577
95b20f41
RR
1578 wxDataViewColumn *ret = new wxDataViewColumn( label,
1579 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1580 GetStore()->GetColumnCount()-1, width, align, flags );
8c2654ce 1581
95b20f41 1582 wxDataViewCtrl::AppendColumn( ret );
8c2654ce 1583
95b20f41 1584 return ret;
dc813e6c
RR
1585}
1586
1587void wxDataViewListCtrl::OnSize( wxSizeEvent &event )
1588{
1589 event.Skip( true );
1590}
1591
e94d0c1e
RR
1592//-----------------------------------------------------------------------------
1593// wxDataViewTreeStore
1594//-----------------------------------------------------------------------------
1595
b5ec7dd6
VZ
1596wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1597 wxDataViewTreeStoreNode *parent,
e94d0c1e
RR
1598 const wxString &text, const wxIcon &icon, wxClientData *data )
1599{
1600 m_parent = parent;
1601 m_text = text;
1602 m_icon = icon;
1603 m_data = data;
1604}
1605
1606wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1607{
1608 if (m_data)
1609 delete m_data;
1610}
b5ec7dd6 1611
e94d0c1e 1612#include "wx/listimpl.cpp"
a76c2f37 1613WX_DEFINE_LIST(wxDataViewTreeStoreNodeList)
e94d0c1e 1614
b5ec7dd6
VZ
1615wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1616 wxDataViewTreeStoreNode *parent, const wxString &text,
e94d0c1e
RR
1617 const wxIcon &icon, const wxIcon &expanded, wxClientData *data ) :
1618 wxDataViewTreeStoreNode( parent, text, icon, data )
1619{
1620 m_iconExpanded = expanded;
a75124d0 1621 m_isExpanded = false;
e94d0c1e
RR
1622 m_children.DeleteContents(true);
1623}
1624
1625wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1626{
1627}
1628
1629//-----------------------------------------------------------------------------
1630
1631wxDataViewTreeStore::wxDataViewTreeStore()
1632{
1633 m_root = new wxDataViewTreeStoreContainerNode( NULL, wxEmptyString );
1634}
1635
1636wxDataViewTreeStore::~wxDataViewTreeStore()
1637{
1638 delete m_root;
1639}
1640
b5ec7dd6 1641wxDataViewItem wxDataViewTreeStore::AppendItem( const wxDataViewItem& parent,
e94d0c1e
RR
1642 const wxString &text, const wxIcon &icon, wxClientData *data )
1643{
1644 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1645 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1646
1647 wxDataViewTreeStoreNode *node =
e94d0c1e
RR
1648 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1649 parent_node->GetChildren().Append( node );
b5ec7dd6 1650
e94d0c1e
RR
1651 return node->GetItem();
1652}
1653
1654wxDataViewItem wxDataViewTreeStore::PrependItem( const wxDataViewItem& parent,
1655 const wxString &text, const wxIcon &icon, wxClientData *data )
1656{
1657 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1658 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1659
1660 wxDataViewTreeStoreNode *node =
e94d0c1e
RR
1661 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1662 parent_node->GetChildren().Insert( node );
b5ec7dd6 1663
e94d0c1e
RR
1664 return node->GetItem();
1665}
1666
b5ec7dd6 1667wxDataViewItem
e700e296
RR
1668wxDataViewTreeStore::InsertItem(const wxDataViewItem& parent,
1669 const wxDataViewItem& previous,
1670 const wxString& text,
1671 const wxIcon& icon,
1672 wxClientData *data)
e94d0c1e 1673{
e700e296
RR
1674 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1675 if (!parent_node) return wxDataViewItem(0);
1676
1677 wxDataViewTreeStoreNode *previous_node = FindNode( previous );
1678 int pos = parent_node->GetChildren().IndexOf( previous_node );
1679 if (pos == wxNOT_FOUND) return wxDataViewItem(0);
1680
1681 wxDataViewTreeStoreNode *node =
1682 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1683 parent_node->GetChildren().Insert( (size_t) pos, node );
1684
1685 return node->GetItem();
e94d0c1e
RR
1686}
1687
b5ec7dd6
VZ
1688wxDataViewItem wxDataViewTreeStore::PrependContainer( const wxDataViewItem& parent,
1689 const wxString &text, const wxIcon &icon, const wxIcon &expanded,
e94d0c1e
RR
1690 wxClientData *data )
1691{
1692 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1693 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1694
1695 wxDataViewTreeStoreContainerNode *node =
e94d0c1e
RR
1696 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1697 parent_node->GetChildren().Insert( node );
b5ec7dd6 1698
e94d0c1e
RR
1699 return node->GetItem();
1700}
1701
b5ec7dd6
VZ
1702wxDataViewItem
1703wxDataViewTreeStore::AppendContainer(const wxDataViewItem& parent,
1704 const wxString &text,
1705 const wxIcon& icon,
1706 const wxIcon& expanded,
1707 wxClientData * data)
e94d0c1e
RR
1708{
1709 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1710 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1711
1712 wxDataViewTreeStoreContainerNode *node =
e94d0c1e
RR
1713 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1714 parent_node->GetChildren().Append( node );
b5ec7dd6 1715
e94d0c1e
RR
1716 return node->GetItem();
1717}
1718
b5ec7dd6 1719wxDataViewItem
e700e296
RR
1720wxDataViewTreeStore::InsertContainer(const wxDataViewItem& parent,
1721 const wxDataViewItem& previous,
1722 const wxString& text,
1723 const wxIcon& icon,
1724 const wxIcon& expanded,
1725 wxClientData * data)
e94d0c1e 1726{
e700e296
RR
1727 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1728 if (!parent_node) return wxDataViewItem(0);
8c2654ce 1729
e700e296
RR
1730 wxDataViewTreeStoreNode *previous_node = FindNode( previous );
1731 int pos = parent_node->GetChildren().IndexOf( previous_node );
1732 if (pos == wxNOT_FOUND) return wxDataViewItem(0);
8c2654ce 1733
e700e296
RR
1734 wxDataViewTreeStoreContainerNode *node =
1735 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1736 parent_node->GetChildren().Insert( (size_t) pos, node );
1737
1738 return node->GetItem();
e94d0c1e
RR
1739}
1740
1741wxDataViewItem wxDataViewTreeStore::GetNthChild( const wxDataViewItem& parent, unsigned int pos ) const
1742{
1743 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1744 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6 1745
e94d0c1e
RR
1746 wxDataViewTreeStoreNodeList::compatibility_iterator node = parent_node->GetChildren().Item( pos );
1747 if (node)
1748 return node->GetData();
b5ec7dd6 1749
e94d0c1e
RR
1750 return wxDataViewItem(0);
1751}
1752
1753int wxDataViewTreeStore::GetChildCount( const wxDataViewItem& parent ) const
1754{
1755 wxDataViewTreeStoreNode *node = FindNode( parent );
1756 if (!node) return -1;
b5ec7dd6 1757
e94d0c1e
RR
1758 if (!node->IsContainer())
1759 return 0;
b5ec7dd6 1760
e94d0c1e 1761 wxDataViewTreeStoreContainerNode *container_node = (wxDataViewTreeStoreContainerNode*) node;
b5ec7dd6 1762 return (int) container_node->GetChildren().GetCount();
e94d0c1e
RR
1763}
1764
1765void wxDataViewTreeStore::SetItemText( const wxDataViewItem& item, const wxString &text )
1766{
1767 wxDataViewTreeStoreNode *node = FindNode( item );
1768 if (!node) return;
b5ec7dd6 1769
e94d0c1e 1770 node->SetText( text );
e94d0c1e
RR
1771}
1772
1773wxString wxDataViewTreeStore::GetItemText( const wxDataViewItem& item ) const
1774{
1775 wxDataViewTreeStoreNode *node = FindNode( item );
1776 if (!node) return wxEmptyString;
b5ec7dd6 1777
e94d0c1e
RR
1778 return node->GetText();
1779}
1780
1781void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
1782{
1783 wxDataViewTreeStoreNode *node = FindNode( item );
1784 if (!node) return;
b5ec7dd6 1785
e94d0c1e 1786 node->SetIcon( icon );
e94d0c1e
RR
1787}
1788
1789const wxIcon &wxDataViewTreeStore::GetItemIcon( const wxDataViewItem& item ) const
1790{
1791 wxDataViewTreeStoreNode *node = FindNode( item );
1792 if (!node) return wxNullIcon;
b5ec7dd6 1793
e94d0c1e
RR
1794 return node->GetIcon();
1795}
1796
1797void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
1798{
1799 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1800 if (!node) return;
b5ec7dd6 1801
e94d0c1e 1802 node->SetExpandedIcon( icon );
e94d0c1e
RR
1803}
1804
1805const wxIcon &wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem& item ) const
1806{
1807 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1808 if (!node) return wxNullIcon;
b5ec7dd6 1809
e94d0c1e
RR
1810 return node->GetExpandedIcon();
1811}
1812
1813void wxDataViewTreeStore::SetItemData( const wxDataViewItem& item, wxClientData *data )
1814{
1815 wxDataViewTreeStoreNode *node = FindNode( item );
1816 if (!node) return;
b5ec7dd6 1817
e94d0c1e 1818 node->SetData( data );
e94d0c1e
RR
1819}
1820
1821wxClientData *wxDataViewTreeStore::GetItemData( const wxDataViewItem& item ) const
1822{
1823 wxDataViewTreeStoreNode *node = FindNode( item );
1824 if (!node) return NULL;
b5ec7dd6 1825
e94d0c1e
RR
1826 return node->GetData();
1827}
1828
1829void wxDataViewTreeStore::DeleteItem( const wxDataViewItem& item )
1830{
1831 if (!item.IsOk()) return;
1832
1833 wxDataViewItem parent_item = GetParent( item );
1834
1835 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent_item );
1836 if (!parent_node) return;
b5ec7dd6 1837
e94d0c1e
RR
1838 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1839 if (!node) return;
b5ec7dd6 1840
e94d0c1e 1841 parent_node->GetChildren().DeleteObject( node );
e94d0c1e
RR
1842}
1843
1844void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem& item )
1845{
1846 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1847 if (!node) return;
b5ec7dd6 1848
e94d0c1e 1849 node->GetChildren().clear();
e94d0c1e
RR
1850}
1851
1852void wxDataViewTreeStore::DeleteAllItems()
1853{
1854 // TODO
1855}
1856
b5ec7dd6
VZ
1857void
1858wxDataViewTreeStore::GetValue(wxVariant &variant,
1859 const wxDataViewItem &item,
1860 unsigned int WXUNUSED(col)) const
e94d0c1e
RR
1861{
1862 // if (col != 0) return;
b5ec7dd6 1863
e94d0c1e
RR
1864 wxDataViewTreeStoreNode *node = FindNode( item );
1865 if (!node) return;
b5ec7dd6 1866
a75124d0
RR
1867 wxIcon icon( node->GetIcon());
1868 if (node->IsContainer())
1869 {
1870 wxDataViewTreeStoreContainerNode *container = (wxDataViewTreeStoreContainerNode*) node;
1871 if (container->IsExpanded() && container->GetExpandedIcon().IsOk())
1872 icon = container->GetExpandedIcon();
1873 }
777f9cef 1874
a75124d0 1875 wxDataViewIconText data( node->GetText(), icon );
b5ec7dd6 1876
e94d0c1e
RR
1877 variant << data;
1878}
1879
b5ec7dd6
VZ
1880bool
1881wxDataViewTreeStore::SetValue(const wxVariant& variant,
1882 const wxDataViewItem& item,
1883 unsigned int WXUNUSED(col))
e94d0c1e
RR
1884{
1885 // if (col != 0) return false;
b5ec7dd6 1886
e94d0c1e
RR
1887 wxDataViewTreeStoreNode *node = FindNode( item );
1888 if (!node) return false;
b5ec7dd6 1889
e94d0c1e 1890 wxDataViewIconText data;
b5ec7dd6 1891
e94d0c1e 1892 data << variant;
b5ec7dd6 1893
e94d0c1e
RR
1894 node->SetText( data.GetText() );
1895 node->SetIcon( data.GetIcon() );
b5ec7dd6 1896
e94d0c1e
RR
1897 return true;
1898}
1899
1900wxDataViewItem wxDataViewTreeStore::GetParent( const wxDataViewItem &item ) const
1901{
1902 wxDataViewTreeStoreNode *node = FindNode( item );
1903 if (!node) return wxDataViewItem(0);
b5ec7dd6 1904
e94d0c1e
RR
1905 wxDataViewTreeStoreNode *parent = node->GetParent();
1906 if (!parent) return wxDataViewItem(0);
b5ec7dd6 1907
e94d0c1e
RR
1908 if (parent == m_root)
1909 return wxDataViewItem(0);
b5ec7dd6 1910
e94d0c1e
RR
1911 return parent->GetItem();
1912}
1913
1914bool wxDataViewTreeStore::IsContainer( const wxDataViewItem &item ) const
1915{
1916 wxDataViewTreeStoreNode *node = FindNode( item );
1917 if (!node) return false;
b5ec7dd6 1918
e94d0c1e
RR
1919 return node->IsContainer();
1920}
1921
1922unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
1923{
1924 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1925 if (!node) return 0;
1926
1927 wxDataViewTreeStoreNodeList::iterator iter;
1928 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
1929 {
1930 wxDataViewTreeStoreNode* child = *iter;
1931 children.Add( child->GetItem() );
1932 }
b5ec7dd6 1933
e94d0c1e
RR
1934 return node->GetChildren().GetCount();
1935}
1936
b5ec7dd6 1937int wxDataViewTreeStore::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
e94d0c1e
RR
1938 unsigned int WXUNUSED(column), bool WXUNUSED(ascending) )
1939{
1940 wxDataViewTreeStoreNode *node1 = FindNode( item1 );
1941 wxDataViewTreeStoreNode *node2 = FindNode( item2 );
b5ec7dd6 1942
e94d0c1e
RR
1943 if (!node1 || !node2)
1944 return 0;
b5ec7dd6
VZ
1945
1946 wxDataViewTreeStoreContainerNode* parent1 =
e94d0c1e 1947 (wxDataViewTreeStoreContainerNode*) node1->GetParent();
b5ec7dd6 1948 wxDataViewTreeStoreContainerNode* parent2 =
e94d0c1e 1949 (wxDataViewTreeStoreContainerNode*) node2->GetParent();
b5ec7dd6 1950
e94d0c1e
RR
1951 if (parent1 != parent2)
1952 {
1953 wxLogError( wxT("Comparing items with different parent.") );
1954 return 0;
1955 }
b5ec7dd6 1956
e94d0c1e
RR
1957 if (node1->IsContainer() && !!node2->IsContainer())
1958 return 1;
b5ec7dd6 1959
e94d0c1e
RR
1960 if (node2->IsContainer() && !!node1->IsContainer())
1961 return -1;
b5ec7dd6 1962
e94d0c1e
RR
1963 return parent1->GetChildren().IndexOf( node1 ) - parent1->GetChildren().IndexOf( node2 );
1964}
1965
1966wxDataViewTreeStoreNode *wxDataViewTreeStore::FindNode( const wxDataViewItem &item ) const
1967{
1968 if (!item.IsOk())
1969 return m_root;
b5ec7dd6 1970
e94d0c1e
RR
1971 return (wxDataViewTreeStoreNode*) item.GetID();
1972}
1973
1974wxDataViewTreeStoreContainerNode *wxDataViewTreeStore::FindContainerNode( const wxDataViewItem &item ) const
1975{
1976 if (!item.IsOk())
1977 return (wxDataViewTreeStoreContainerNode*) m_root;
1978
1979 wxDataViewTreeStoreNode* node = (wxDataViewTreeStoreNode*) item.GetID();
b5ec7dd6 1980
e94d0c1e
RR
1981 if (!node->IsContainer())
1982 return NULL;
b5ec7dd6 1983
e94d0c1e
RR
1984 return (wxDataViewTreeStoreContainerNode*) node;
1985}
1986
a75124d0
RR
1987//-----------------------------------------------------------------------------
1988// wxDataViewTreeCtrl
1989//-----------------------------------------------------------------------------
1990
1991IMPLEMENT_DYNAMIC_CLASS(wxDataViewTreeCtrl,wxDataViewCtrl)
1992
1993BEGIN_EVENT_TABLE(wxDataViewTreeCtrl,wxDataViewCtrl)
1994 EVT_DATAVIEW_ITEM_EXPANDED(-1, wxDataViewTreeCtrl::OnExpanded)
1995 EVT_DATAVIEW_ITEM_COLLAPSED(-1, wxDataViewTreeCtrl::OnCollapsed)
9c150d5f 1996 EVT_SIZE( wxDataViewTreeCtrl::OnSize )
a75124d0
RR
1997END_EVENT_TABLE()
1998
1999wxDataViewTreeCtrl::wxDataViewTreeCtrl()
2000{
2001 m_imageList = NULL;
2002}
2003
2004wxDataViewTreeCtrl::wxDataViewTreeCtrl( wxWindow *parent, wxWindowID id,
777f9cef 2005 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
a75124d0
RR
2006{
2007 m_imageList = NULL;
2008 Create( parent, id, pos, size, style, validator );
777f9cef 2009
a75124d0
RR
2010 wxDataViewTreeStore *store = new wxDataViewTreeStore;
2011 AssociateModel( store );
2012 store->DecRef();
777f9cef 2013
ad386793 2014 AppendIconTextColumn(wxString(),0,wxDATAVIEW_CELL_INERT,-1);
a75124d0
RR
2015}
2016
2017wxDataViewTreeCtrl::~wxDataViewTreeCtrl()
2018{
2019 if (m_imageList)
2020 delete m_imageList;
2021}
2022
2023bool wxDataViewTreeCtrl::Create( wxWindow *parent, wxWindowID id,
2024 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
2025{
2026 return wxDataViewCtrl::Create( parent, id, pos, size, style, validator );
2027}
2028
2029void wxDataViewTreeCtrl::SetImageList( wxImageList *imagelist )
2030{
2031 if (m_imageList)
2032 delete m_imageList;
2033
777f9cef 2034 m_imageList = imagelist;
a75124d0 2035}
777f9cef 2036
a75124d0
RR
2037wxDataViewItem wxDataViewTreeCtrl::AppendItem( const wxDataViewItem& parent,
2038 const wxString &text, int iconIndex, wxClientData *data )
2039{
2040 wxIcon icon = wxNullIcon;
2041 if (m_imageList && (iconIndex != -1))
2042 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2043
e700e296 2044 wxDataViewItem res = GetStore()->AppendItem( parent, text, icon, data );
8c2654ce 2045
e700e296 2046 GetStore()->ItemAdded( parent, res );
8c2654ce 2047
e700e296 2048 return res;
a75124d0
RR
2049}
2050
2051wxDataViewItem wxDataViewTreeCtrl::PrependItem( const wxDataViewItem& parent,
2052 const wxString &text, int iconIndex, wxClientData *data )
2053{
2054 wxIcon icon = wxNullIcon;
2055 if (m_imageList && (iconIndex != -1))
2056 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2057
e700e296 2058 wxDataViewItem res = GetStore()->PrependItem( parent, text, icon, data );
8c2654ce 2059
e700e296 2060 GetStore()->ItemAdded( parent, res );
8c2654ce 2061
e700e296 2062 return res;
a75124d0
RR
2063}
2064
2065wxDataViewItem wxDataViewTreeCtrl::InsertItem( const wxDataViewItem& parent, const wxDataViewItem& previous,
2066 const wxString &text, int iconIndex, wxClientData *data )
2067{
2068 wxIcon icon = wxNullIcon;
2069 if (m_imageList && (iconIndex != -1))
2070 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2071
e700e296 2072 wxDataViewItem res = GetStore()->InsertItem( parent, previous, text, icon, data );
8c2654ce 2073
e700e296 2074 GetStore()->ItemAdded( parent, res );
8c2654ce 2075
e700e296 2076 return res;
a75124d0
RR
2077}
2078
2079wxDataViewItem wxDataViewTreeCtrl::PrependContainer( const wxDataViewItem& parent,
2080 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2081{
2082 wxIcon icon = wxNullIcon;
2083 if (m_imageList && (iconIndex != -1))
2084 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2085
a75124d0
RR
2086 wxIcon expanded = wxNullIcon;
2087 if (m_imageList && (expandedIndex != -1))
2088 expanded = m_imageList->GetIcon( expandedIndex );
777f9cef 2089
e700e296 2090 wxDataViewItem res = GetStore()->PrependContainer( parent, text, icon, expanded, data );
8c2654ce 2091
e700e296 2092 GetStore()->ItemAdded( parent, res );
8c2654ce 2093
e700e296 2094 return res;
a75124d0
RR
2095}
2096
2097wxDataViewItem wxDataViewTreeCtrl::AppendContainer( const wxDataViewItem& parent,
2098 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2099{
2100 wxIcon icon = wxNullIcon;
2101 if (m_imageList && (iconIndex != -1))
2102 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2103
a75124d0
RR
2104 wxIcon expanded = wxNullIcon;
2105 if (m_imageList && (expandedIndex != -1))
2106 expanded = m_imageList->GetIcon( expandedIndex );
777f9cef 2107
e700e296 2108 wxDataViewItem res = GetStore()->AppendContainer( parent, text, icon, expanded, data );
8c2654ce 2109
e700e296 2110 GetStore()->ItemAdded( parent, res );
8c2654ce 2111
e700e296 2112 return res;
a75124d0
RR
2113}
2114
2115wxDataViewItem wxDataViewTreeCtrl::InsertContainer( const wxDataViewItem& parent, const wxDataViewItem& previous,
2116 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2117{
2118 wxIcon icon = wxNullIcon;
2119 if (m_imageList && (iconIndex != -1))
2120 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2121
a75124d0
RR
2122 wxIcon expanded = wxNullIcon;
2123 if (m_imageList && (expandedIndex != -1))
2124 expanded = m_imageList->GetIcon( expandedIndex );
777f9cef 2125
e700e296 2126 wxDataViewItem res = GetStore()->InsertContainer( parent, previous, text, icon, expanded, data );
8c2654ce 2127
e700e296 2128 GetStore()->ItemAdded( parent, res );
8c2654ce 2129
e700e296
RR
2130 return res;
2131}
2132
2133void wxDataViewTreeCtrl::SetItemText( const wxDataViewItem& item, const wxString &text )
2134{
2135 GetStore()->SetItemText(item,text);
8c2654ce 2136
e700e296
RR
2137 // notify control
2138 GetStore()->ValueChanged( item, 0 );
2139}
2140
2141void wxDataViewTreeCtrl::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
2142{
2143 GetStore()->SetItemIcon(item,icon);
8c2654ce 2144
e700e296
RR
2145 // notify control
2146 GetStore()->ValueChanged( item, 0 );
2147}
2148
2149void wxDataViewTreeCtrl::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
2150{
2151 GetStore()->SetItemExpandedIcon(item,icon);
8c2654ce 2152
e700e296
RR
2153 // notify control
2154 GetStore()->ValueChanged( item, 0 );
2155}
2156
2157void wxDataViewTreeCtrl::DeleteItem( const wxDataViewItem& item )
2158{
2159 wxDataViewItem parent_item = GetStore()->GetParent( item );
2160
2161 GetStore()->DeleteItem(item);
8c2654ce 2162
e700e296
RR
2163 // notify control
2164 GetStore()->ItemDeleted( parent_item, item );
2165}
2166
2167void wxDataViewTreeCtrl::DeleteChildren( const wxDataViewItem& item )
2168{
2f21b2be
RR
2169 wxDataViewTreeStoreContainerNode *node = GetStore()->FindContainerNode( item );
2170 if (!node) return;
2171
2172 wxDataViewItemArray array;
2173 wxDataViewTreeStoreNodeList::iterator iter;
2174 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
2175 {
2176 wxDataViewTreeStoreNode* child = *iter;
2177 array.Add( child->GetItem() );
2178 }
2179
2180 GetStore()->DeleteChildren( item );
2181
2182 // notify control
2183 GetStore()->ItemsDeleted( item, array );
e700e296
RR
2184}
2185
2186void wxDataViewTreeCtrl::DeleteAllItems()
2187{
2188 GetStore()->DeleteAllItems();
8c2654ce 2189
e700e296 2190 GetStore()->Cleared();
a75124d0
RR
2191}
2192
a75124d0
RR
2193void wxDataViewTreeCtrl::OnExpanded( wxDataViewEvent &event )
2194{
2195 if (m_imageList) return;
777f9cef 2196
a75124d0
RR
2197 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
2198 if (!container) return;
777f9cef 2199
a75124d0 2200 container->SetExpanded( true );
8c2654ce 2201
a75124d0
RR
2202 GetStore()->ItemChanged( event.GetItem() );
2203}
2204
2205void wxDataViewTreeCtrl::OnCollapsed( wxDataViewEvent &event )
2206{
2207 if (m_imageList) return;
777f9cef 2208
a75124d0
RR
2209 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
2210 if (!container) return;
777f9cef 2211
a75124d0 2212 container->SetExpanded( false );
8c2654ce 2213
a75124d0
RR
2214 GetStore()->ItemChanged( event.GetItem() );
2215}
2216
a6aaf49f 2217void wxDataViewTreeCtrl::OnSize( wxSizeEvent &event )
1a07a730 2218{
ad386793 2219#if defined(wxUSE_GENERICDATAVIEWCTRL)
4bfd0ed5
VZ
2220 // automatically resize our only column to take the entire control width
2221 if ( GetColumnCount() )
2222 {
2223 wxSize size = GetClientSize();
2224 GetColumn(0)->SetWidth(size.x);
2225 }
1a07a730 2226#endif
a6aaf49f 2227 event.Skip( true );
1a07a730 2228}
a75124d0 2229
b5ec7dd6 2230#endif // wxUSE_DATAVIEWCTRL
e0000f94 2231