]> git.saurik.com Git - wxWidgets.git/blame - src/common/datavcmn.cpp
fix generation of key events with Alt pressed broken by r59369 (see #626)
[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
8af28fab
FM
1418void wxDataViewListStore::InsertItem( unsigned int row, const wxVector<wxVariant> &values,
1419 wxClientData *data )
8eff6c56
RR
1420{
1421 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1422 line->m_values = values;
1423 m_data.insert( m_data.begin()+row, line );
8c2654ce 1424
8eff6c56
RR
1425 RowInserted( row );
1426}
1427
8af28fab 1428void wxDataViewListStore::DeleteItem( unsigned int row )
8eff6c56
RR
1429{
1430 wxVector<wxDataViewListStoreLine*>::iterator it = m_data.begin() + row;
1431 m_data.erase( it );
8c2654ce 1432
8eff6c56
RR
1433 RowDeleted( row );
1434}
1435
1436void wxDataViewListStore::DeleteAllItems()
1437{
1438 wxVector<wxDataViewListStoreLine*>::iterator it;
1439 for (it = m_data.begin(); it != m_data.end(); ++it)
1440 {
1441 wxDataViewListStoreLine* line = *it;
1442 delete line;
1443 }
8c2654ce 1444
8af28fab
FM
1445 m_data.clear();
1446
8eff6c56
RR
1447 Reset( 0 );
1448}
1449
1450void wxDataViewListStore::GetValueByRow( wxVariant &value, unsigned int row, unsigned int col ) const
1451{
1452 wxDataViewListStoreLine *line = m_data[row];
1453 value = line->m_values[col];
1454}
1455
1456bool wxDataViewListStore::SetValueByRow( const wxVariant &value, unsigned int row, unsigned int col )
1457{
1458 wxDataViewListStoreLine *line = m_data[row];
1459 line->m_values[col] = value;
8c2654ce 1460
8eff6c56
RR
1461 return true;
1462}
1463
dc813e6c
RR
1464//-----------------------------------------------------------------------------
1465// wxDataViewListCtrl
1466//-----------------------------------------------------------------------------
1467
1468IMPLEMENT_DYNAMIC_CLASS(wxDataViewListCtrl,wxDataViewCtrl)
1469
1470BEGIN_EVENT_TABLE(wxDataViewListCtrl,wxDataViewCtrl)
1471 EVT_SIZE( wxDataViewListCtrl::OnSize )
1472END_EVENT_TABLE()
1473
1474wxDataViewListCtrl::wxDataViewListCtrl()
1475{
1476}
1477
1478wxDataViewListCtrl::wxDataViewListCtrl( wxWindow *parent, wxWindowID id,
1479 const wxPoint& pos, const wxSize& size, long style,
1480 const wxValidator& validator )
1481{
1482 Create( parent, id, pos, size, style, validator );
1483
1484 wxDataViewListStore *store = new wxDataViewListStore;
1485 AssociateModel( store );
1486 store->DecRef();
1487}
1488
1489wxDataViewListCtrl::~wxDataViewListCtrl()
1490{
1491}
1492
1493
1494bool wxDataViewListCtrl::Create( wxWindow *parent, wxWindowID id,
1495 const wxPoint& pos, const wxSize& size, long style,
1496 const wxValidator& validator )
1497{
1498 return wxDataViewCtrl::Create( parent, id, pos, size, style, validator );
1499}
1500
95b20f41 1501bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *column, const wxString &varianttype )
dc813e6c
RR
1502{
1503 GetStore()->AppendColumn( varianttype );
95b20f41 1504 return wxDataViewCtrl::AppendColumn( column );
dc813e6c
RR
1505}
1506
95b20f41 1507bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *column, const wxString &varianttype )
dc813e6c
RR
1508{
1509 GetStore()->PrependColumn( varianttype );
95b20f41 1510 return wxDataViewCtrl::PrependColumn( column );
dc813e6c
RR
1511}
1512
95b20f41 1513bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *column, const wxString &varianttype )
dc813e6c
RR
1514{
1515 GetStore()->InsertColumn( pos, varianttype );
95b20f41
RR
1516 return wxDataViewCtrl::InsertColumn( pos, column );
1517}
1518
1519bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *col )
1520{
1521 return PrependColumn( col, "string" );
1522}
1523
1524bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
1525{
1526 return InsertColumn( pos, col, "string" );
1527}
1528
1529bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *col )
1530{
1531 return AppendColumn( col, "string" );
dc813e6c 1532}
8c2654ce 1533
95b20f41 1534wxDataViewColumn *wxDataViewListCtrl::AppendTextColumn( const wxString &label,
dc813e6c
RR
1535 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1536{
1537 GetStore()->AppendColumn( wxT("string") );
8c2654ce 1538
95b20f41
RR
1539 wxDataViewColumn *ret = new wxDataViewColumn( label,
1540 new wxDataViewTextRenderer( wxT("string"), mode ),
1541 GetStore()->GetColumnCount()-1, width, align, flags );
8c2654ce 1542
95b20f41 1543 wxDataViewCtrl::AppendColumn( ret );
8c2654ce 1544
95b20f41 1545 return ret;
dc813e6c
RR
1546}
1547
95b20f41 1548wxDataViewColumn *wxDataViewListCtrl::AppendToggleColumn( const wxString &label,
dc813e6c
RR
1549 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1550{
1551 GetStore()->AppendColumn( wxT("bool") );
8c2654ce 1552
95b20f41
RR
1553 wxDataViewColumn *ret = new wxDataViewColumn( label,
1554 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1555 GetStore()->GetColumnCount()-1, width, align, flags );
8c2654ce 1556
95b20f41 1557 wxDataViewCtrl::AppendColumn( ret );
8c2654ce 1558
95b20f41 1559 return ret;
dc813e6c
RR
1560}
1561
95b20f41 1562wxDataViewColumn *wxDataViewListCtrl::AppendProgressColumn( const wxString &label,
dc813e6c
RR
1563 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1564{
1565 GetStore()->AppendColumn( wxT("long") );
8c2654ce 1566
95b20f41
RR
1567 wxDataViewColumn *ret = new wxDataViewColumn( label,
1568 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1569 GetStore()->GetColumnCount()-1, width, align, flags );
8c2654ce 1570
95b20f41 1571 wxDataViewCtrl::AppendColumn( ret );
8c2654ce 1572
95b20f41 1573 return ret;
dc813e6c
RR
1574}
1575
95b20f41 1576wxDataViewColumn *wxDataViewListCtrl::AppendIconTextColumn( const wxString &label,
dc813e6c
RR
1577 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1578{
1579 GetStore()->AppendColumn( wxT("wxDataViewIconText") );
8c2654ce 1580
95b20f41
RR
1581 wxDataViewColumn *ret = new wxDataViewColumn( label,
1582 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1583 GetStore()->GetColumnCount()-1, width, align, flags );
8c2654ce 1584
95b20f41 1585 wxDataViewCtrl::AppendColumn( ret );
8c2654ce 1586
95b20f41 1587 return ret;
dc813e6c
RR
1588}
1589
1590void wxDataViewListCtrl::OnSize( wxSizeEvent &event )
1591{
1592 event.Skip( true );
1593}
1594
e94d0c1e
RR
1595//-----------------------------------------------------------------------------
1596// wxDataViewTreeStore
1597//-----------------------------------------------------------------------------
1598
b5ec7dd6
VZ
1599wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1600 wxDataViewTreeStoreNode *parent,
e94d0c1e
RR
1601 const wxString &text, const wxIcon &icon, wxClientData *data )
1602{
1603 m_parent = parent;
1604 m_text = text;
1605 m_icon = icon;
1606 m_data = data;
1607}
1608
1609wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1610{
1611 if (m_data)
1612 delete m_data;
1613}
b5ec7dd6 1614
e94d0c1e 1615#include "wx/listimpl.cpp"
a76c2f37 1616WX_DEFINE_LIST(wxDataViewTreeStoreNodeList)
e94d0c1e 1617
b5ec7dd6
VZ
1618wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1619 wxDataViewTreeStoreNode *parent, const wxString &text,
e94d0c1e
RR
1620 const wxIcon &icon, const wxIcon &expanded, wxClientData *data ) :
1621 wxDataViewTreeStoreNode( parent, text, icon, data )
1622{
1623 m_iconExpanded = expanded;
a75124d0 1624 m_isExpanded = false;
e94d0c1e
RR
1625 m_children.DeleteContents(true);
1626}
1627
1628wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1629{
1630}
1631
1632//-----------------------------------------------------------------------------
1633
1634wxDataViewTreeStore::wxDataViewTreeStore()
1635{
1636 m_root = new wxDataViewTreeStoreContainerNode( NULL, wxEmptyString );
1637}
1638
1639wxDataViewTreeStore::~wxDataViewTreeStore()
1640{
1641 delete m_root;
1642}
1643
b5ec7dd6 1644wxDataViewItem wxDataViewTreeStore::AppendItem( const wxDataViewItem& parent,
e94d0c1e
RR
1645 const wxString &text, const wxIcon &icon, wxClientData *data )
1646{
1647 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1648 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1649
1650 wxDataViewTreeStoreNode *node =
e94d0c1e
RR
1651 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1652 parent_node->GetChildren().Append( node );
b5ec7dd6 1653
e94d0c1e
RR
1654 return node->GetItem();
1655}
1656
1657wxDataViewItem wxDataViewTreeStore::PrependItem( const wxDataViewItem& parent,
1658 const wxString &text, const wxIcon &icon, wxClientData *data )
1659{
1660 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1661 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1662
1663 wxDataViewTreeStoreNode *node =
e94d0c1e
RR
1664 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1665 parent_node->GetChildren().Insert( node );
b5ec7dd6 1666
e94d0c1e
RR
1667 return node->GetItem();
1668}
1669
b5ec7dd6 1670wxDataViewItem
e700e296
RR
1671wxDataViewTreeStore::InsertItem(const wxDataViewItem& parent,
1672 const wxDataViewItem& previous,
1673 const wxString& text,
1674 const wxIcon& icon,
1675 wxClientData *data)
e94d0c1e 1676{
e700e296
RR
1677 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1678 if (!parent_node) return wxDataViewItem(0);
1679
1680 wxDataViewTreeStoreNode *previous_node = FindNode( previous );
1681 int pos = parent_node->GetChildren().IndexOf( previous_node );
1682 if (pos == wxNOT_FOUND) return wxDataViewItem(0);
1683
1684 wxDataViewTreeStoreNode *node =
1685 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1686 parent_node->GetChildren().Insert( (size_t) pos, node );
1687
1688 return node->GetItem();
e94d0c1e
RR
1689}
1690
b5ec7dd6
VZ
1691wxDataViewItem wxDataViewTreeStore::PrependContainer( const wxDataViewItem& parent,
1692 const wxString &text, const wxIcon &icon, const wxIcon &expanded,
e94d0c1e
RR
1693 wxClientData *data )
1694{
1695 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1696 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1697
1698 wxDataViewTreeStoreContainerNode *node =
e94d0c1e
RR
1699 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1700 parent_node->GetChildren().Insert( node );
b5ec7dd6 1701
e94d0c1e
RR
1702 return node->GetItem();
1703}
1704
b5ec7dd6
VZ
1705wxDataViewItem
1706wxDataViewTreeStore::AppendContainer(const wxDataViewItem& parent,
1707 const wxString &text,
1708 const wxIcon& icon,
1709 const wxIcon& expanded,
1710 wxClientData * data)
e94d0c1e
RR
1711{
1712 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1713 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1714
1715 wxDataViewTreeStoreContainerNode *node =
e94d0c1e
RR
1716 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1717 parent_node->GetChildren().Append( node );
b5ec7dd6 1718
e94d0c1e
RR
1719 return node->GetItem();
1720}
1721
b5ec7dd6 1722wxDataViewItem
e700e296
RR
1723wxDataViewTreeStore::InsertContainer(const wxDataViewItem& parent,
1724 const wxDataViewItem& previous,
1725 const wxString& text,
1726 const wxIcon& icon,
1727 const wxIcon& expanded,
1728 wxClientData * data)
e94d0c1e 1729{
e700e296
RR
1730 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1731 if (!parent_node) return wxDataViewItem(0);
8c2654ce 1732
e700e296
RR
1733 wxDataViewTreeStoreNode *previous_node = FindNode( previous );
1734 int pos = parent_node->GetChildren().IndexOf( previous_node );
1735 if (pos == wxNOT_FOUND) return wxDataViewItem(0);
8c2654ce 1736
e700e296
RR
1737 wxDataViewTreeStoreContainerNode *node =
1738 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1739 parent_node->GetChildren().Insert( (size_t) pos, node );
1740
1741 return node->GetItem();
e94d0c1e
RR
1742}
1743
1744wxDataViewItem wxDataViewTreeStore::GetNthChild( const wxDataViewItem& parent, unsigned int pos ) const
1745{
1746 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1747 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6 1748
e94d0c1e
RR
1749 wxDataViewTreeStoreNodeList::compatibility_iterator node = parent_node->GetChildren().Item( pos );
1750 if (node)
1751 return node->GetData();
b5ec7dd6 1752
e94d0c1e
RR
1753 return wxDataViewItem(0);
1754}
1755
1756int wxDataViewTreeStore::GetChildCount( const wxDataViewItem& parent ) const
1757{
1758 wxDataViewTreeStoreNode *node = FindNode( parent );
1759 if (!node) return -1;
b5ec7dd6 1760
e94d0c1e
RR
1761 if (!node->IsContainer())
1762 return 0;
b5ec7dd6 1763
e94d0c1e 1764 wxDataViewTreeStoreContainerNode *container_node = (wxDataViewTreeStoreContainerNode*) node;
b5ec7dd6 1765 return (int) container_node->GetChildren().GetCount();
e94d0c1e
RR
1766}
1767
1768void wxDataViewTreeStore::SetItemText( const wxDataViewItem& item, const wxString &text )
1769{
1770 wxDataViewTreeStoreNode *node = FindNode( item );
1771 if (!node) return;
b5ec7dd6 1772
e94d0c1e 1773 node->SetText( text );
e94d0c1e
RR
1774}
1775
1776wxString wxDataViewTreeStore::GetItemText( const wxDataViewItem& item ) const
1777{
1778 wxDataViewTreeStoreNode *node = FindNode( item );
1779 if (!node) return wxEmptyString;
b5ec7dd6 1780
e94d0c1e
RR
1781 return node->GetText();
1782}
1783
1784void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
1785{
1786 wxDataViewTreeStoreNode *node = FindNode( item );
1787 if (!node) return;
b5ec7dd6 1788
e94d0c1e 1789 node->SetIcon( icon );
e94d0c1e
RR
1790}
1791
1792const wxIcon &wxDataViewTreeStore::GetItemIcon( const wxDataViewItem& item ) const
1793{
1794 wxDataViewTreeStoreNode *node = FindNode( item );
1795 if (!node) return wxNullIcon;
b5ec7dd6 1796
e94d0c1e
RR
1797 return node->GetIcon();
1798}
1799
1800void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
1801{
1802 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1803 if (!node) return;
b5ec7dd6 1804
e94d0c1e 1805 node->SetExpandedIcon( icon );
e94d0c1e
RR
1806}
1807
1808const wxIcon &wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem& item ) const
1809{
1810 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1811 if (!node) return wxNullIcon;
b5ec7dd6 1812
e94d0c1e
RR
1813 return node->GetExpandedIcon();
1814}
1815
1816void wxDataViewTreeStore::SetItemData( const wxDataViewItem& item, wxClientData *data )
1817{
1818 wxDataViewTreeStoreNode *node = FindNode( item );
1819 if (!node) return;
b5ec7dd6 1820
e94d0c1e 1821 node->SetData( data );
e94d0c1e
RR
1822}
1823
1824wxClientData *wxDataViewTreeStore::GetItemData( const wxDataViewItem& item ) const
1825{
1826 wxDataViewTreeStoreNode *node = FindNode( item );
1827 if (!node) return NULL;
b5ec7dd6 1828
e94d0c1e
RR
1829 return node->GetData();
1830}
1831
1832void wxDataViewTreeStore::DeleteItem( const wxDataViewItem& item )
1833{
1834 if (!item.IsOk()) return;
1835
1836 wxDataViewItem parent_item = GetParent( item );
1837
1838 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent_item );
1839 if (!parent_node) return;
b5ec7dd6 1840
e94d0c1e
RR
1841 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1842 if (!node) return;
b5ec7dd6 1843
e94d0c1e 1844 parent_node->GetChildren().DeleteObject( node );
e94d0c1e
RR
1845}
1846
1847void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem& item )
1848{
1849 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1850 if (!node) return;
b5ec7dd6 1851
e94d0c1e 1852 node->GetChildren().clear();
e94d0c1e
RR
1853}
1854
1855void wxDataViewTreeStore::DeleteAllItems()
1856{
1857 // TODO
1858}
1859
b5ec7dd6
VZ
1860void
1861wxDataViewTreeStore::GetValue(wxVariant &variant,
1862 const wxDataViewItem &item,
1863 unsigned int WXUNUSED(col)) const
e94d0c1e
RR
1864{
1865 // if (col != 0) return;
b5ec7dd6 1866
e94d0c1e
RR
1867 wxDataViewTreeStoreNode *node = FindNode( item );
1868 if (!node) return;
b5ec7dd6 1869
a75124d0
RR
1870 wxIcon icon( node->GetIcon());
1871 if (node->IsContainer())
1872 {
1873 wxDataViewTreeStoreContainerNode *container = (wxDataViewTreeStoreContainerNode*) node;
1874 if (container->IsExpanded() && container->GetExpandedIcon().IsOk())
1875 icon = container->GetExpandedIcon();
1876 }
777f9cef 1877
a75124d0 1878 wxDataViewIconText data( node->GetText(), icon );
b5ec7dd6 1879
e94d0c1e
RR
1880 variant << data;
1881}
1882
b5ec7dd6
VZ
1883bool
1884wxDataViewTreeStore::SetValue(const wxVariant& variant,
1885 const wxDataViewItem& item,
1886 unsigned int WXUNUSED(col))
e94d0c1e
RR
1887{
1888 // if (col != 0) return false;
b5ec7dd6 1889
e94d0c1e
RR
1890 wxDataViewTreeStoreNode *node = FindNode( item );
1891 if (!node) return false;
b5ec7dd6 1892
e94d0c1e 1893 wxDataViewIconText data;
b5ec7dd6 1894
e94d0c1e 1895 data << variant;
b5ec7dd6 1896
e94d0c1e
RR
1897 node->SetText( data.GetText() );
1898 node->SetIcon( data.GetIcon() );
b5ec7dd6 1899
e94d0c1e
RR
1900 return true;
1901}
1902
1903wxDataViewItem wxDataViewTreeStore::GetParent( const wxDataViewItem &item ) const
1904{
1905 wxDataViewTreeStoreNode *node = FindNode( item );
1906 if (!node) return wxDataViewItem(0);
b5ec7dd6 1907
e94d0c1e
RR
1908 wxDataViewTreeStoreNode *parent = node->GetParent();
1909 if (!parent) return wxDataViewItem(0);
b5ec7dd6 1910
e94d0c1e
RR
1911 if (parent == m_root)
1912 return wxDataViewItem(0);
b5ec7dd6 1913
e94d0c1e
RR
1914 return parent->GetItem();
1915}
1916
1917bool wxDataViewTreeStore::IsContainer( const wxDataViewItem &item ) const
1918{
1919 wxDataViewTreeStoreNode *node = FindNode( item );
1920 if (!node) return false;
b5ec7dd6 1921
e94d0c1e
RR
1922 return node->IsContainer();
1923}
1924
1925unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
1926{
1927 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1928 if (!node) return 0;
1929
1930 wxDataViewTreeStoreNodeList::iterator iter;
1931 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
1932 {
1933 wxDataViewTreeStoreNode* child = *iter;
1934 children.Add( child->GetItem() );
1935 }
b5ec7dd6 1936
e94d0c1e
RR
1937 return node->GetChildren().GetCount();
1938}
1939
b5ec7dd6 1940int wxDataViewTreeStore::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
e94d0c1e
RR
1941 unsigned int WXUNUSED(column), bool WXUNUSED(ascending) )
1942{
1943 wxDataViewTreeStoreNode *node1 = FindNode( item1 );
1944 wxDataViewTreeStoreNode *node2 = FindNode( item2 );
b5ec7dd6 1945
e94d0c1e
RR
1946 if (!node1 || !node2)
1947 return 0;
b5ec7dd6
VZ
1948
1949 wxDataViewTreeStoreContainerNode* parent1 =
e94d0c1e 1950 (wxDataViewTreeStoreContainerNode*) node1->GetParent();
b5ec7dd6 1951 wxDataViewTreeStoreContainerNode* parent2 =
e94d0c1e 1952 (wxDataViewTreeStoreContainerNode*) node2->GetParent();
b5ec7dd6 1953
e94d0c1e
RR
1954 if (parent1 != parent2)
1955 {
1956 wxLogError( wxT("Comparing items with different parent.") );
1957 return 0;
1958 }
b5ec7dd6 1959
e94d0c1e
RR
1960 if (node1->IsContainer() && !!node2->IsContainer())
1961 return 1;
b5ec7dd6 1962
e94d0c1e
RR
1963 if (node2->IsContainer() && !!node1->IsContainer())
1964 return -1;
b5ec7dd6 1965
e94d0c1e
RR
1966 return parent1->GetChildren().IndexOf( node1 ) - parent1->GetChildren().IndexOf( node2 );
1967}
1968
1969wxDataViewTreeStoreNode *wxDataViewTreeStore::FindNode( const wxDataViewItem &item ) const
1970{
1971 if (!item.IsOk())
1972 return m_root;
b5ec7dd6 1973
e94d0c1e
RR
1974 return (wxDataViewTreeStoreNode*) item.GetID();
1975}
1976
1977wxDataViewTreeStoreContainerNode *wxDataViewTreeStore::FindContainerNode( const wxDataViewItem &item ) const
1978{
1979 if (!item.IsOk())
1980 return (wxDataViewTreeStoreContainerNode*) m_root;
1981
1982 wxDataViewTreeStoreNode* node = (wxDataViewTreeStoreNode*) item.GetID();
b5ec7dd6 1983
e94d0c1e
RR
1984 if (!node->IsContainer())
1985 return NULL;
b5ec7dd6 1986
e94d0c1e
RR
1987 return (wxDataViewTreeStoreContainerNode*) node;
1988}
1989
a75124d0
RR
1990//-----------------------------------------------------------------------------
1991// wxDataViewTreeCtrl
1992//-----------------------------------------------------------------------------
1993
1994IMPLEMENT_DYNAMIC_CLASS(wxDataViewTreeCtrl,wxDataViewCtrl)
1995
1996BEGIN_EVENT_TABLE(wxDataViewTreeCtrl,wxDataViewCtrl)
1997 EVT_DATAVIEW_ITEM_EXPANDED(-1, wxDataViewTreeCtrl::OnExpanded)
1998 EVT_DATAVIEW_ITEM_COLLAPSED(-1, wxDataViewTreeCtrl::OnCollapsed)
9c150d5f 1999 EVT_SIZE( wxDataViewTreeCtrl::OnSize )
a75124d0
RR
2000END_EVENT_TABLE()
2001
2002wxDataViewTreeCtrl::wxDataViewTreeCtrl()
2003{
2004 m_imageList = NULL;
2005}
2006
2007wxDataViewTreeCtrl::wxDataViewTreeCtrl( wxWindow *parent, wxWindowID id,
777f9cef 2008 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
a75124d0
RR
2009{
2010 m_imageList = NULL;
2011 Create( parent, id, pos, size, style, validator );
777f9cef 2012
a75124d0
RR
2013 wxDataViewTreeStore *store = new wxDataViewTreeStore;
2014 AssociateModel( store );
2015 store->DecRef();
777f9cef 2016
ad386793 2017 AppendIconTextColumn(wxString(),0,wxDATAVIEW_CELL_INERT,-1);
a75124d0
RR
2018}
2019
2020wxDataViewTreeCtrl::~wxDataViewTreeCtrl()
2021{
2022 if (m_imageList)
2023 delete m_imageList;
2024}
2025
2026bool wxDataViewTreeCtrl::Create( wxWindow *parent, wxWindowID id,
2027 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
2028{
2029 return wxDataViewCtrl::Create( parent, id, pos, size, style, validator );
2030}
2031
2032void wxDataViewTreeCtrl::SetImageList( wxImageList *imagelist )
2033{
2034 if (m_imageList)
2035 delete m_imageList;
2036
777f9cef 2037 m_imageList = imagelist;
a75124d0 2038}
777f9cef 2039
a75124d0
RR
2040wxDataViewItem wxDataViewTreeCtrl::AppendItem( const wxDataViewItem& parent,
2041 const wxString &text, int iconIndex, wxClientData *data )
2042{
2043 wxIcon icon = wxNullIcon;
2044 if (m_imageList && (iconIndex != -1))
2045 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2046
e700e296 2047 wxDataViewItem res = GetStore()->AppendItem( parent, text, icon, data );
8c2654ce 2048
e700e296 2049 GetStore()->ItemAdded( parent, res );
8c2654ce 2050
e700e296 2051 return res;
a75124d0
RR
2052}
2053
2054wxDataViewItem wxDataViewTreeCtrl::PrependItem( const wxDataViewItem& parent,
2055 const wxString &text, int iconIndex, wxClientData *data )
2056{
2057 wxIcon icon = wxNullIcon;
2058 if (m_imageList && (iconIndex != -1))
2059 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2060
e700e296 2061 wxDataViewItem res = GetStore()->PrependItem( parent, text, icon, data );
8c2654ce 2062
e700e296 2063 GetStore()->ItemAdded( parent, res );
8c2654ce 2064
e700e296 2065 return res;
a75124d0
RR
2066}
2067
2068wxDataViewItem wxDataViewTreeCtrl::InsertItem( const wxDataViewItem& parent, const wxDataViewItem& previous,
2069 const wxString &text, int iconIndex, wxClientData *data )
2070{
2071 wxIcon icon = wxNullIcon;
2072 if (m_imageList && (iconIndex != -1))
2073 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2074
e700e296 2075 wxDataViewItem res = GetStore()->InsertItem( parent, previous, text, icon, data );
8c2654ce 2076
e700e296 2077 GetStore()->ItemAdded( parent, res );
8c2654ce 2078
e700e296 2079 return res;
a75124d0
RR
2080}
2081
2082wxDataViewItem wxDataViewTreeCtrl::PrependContainer( const wxDataViewItem& parent,
2083 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2084{
2085 wxIcon icon = wxNullIcon;
2086 if (m_imageList && (iconIndex != -1))
2087 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2088
a75124d0
RR
2089 wxIcon expanded = wxNullIcon;
2090 if (m_imageList && (expandedIndex != -1))
2091 expanded = m_imageList->GetIcon( expandedIndex );
777f9cef 2092
e700e296 2093 wxDataViewItem res = GetStore()->PrependContainer( parent, text, icon, expanded, data );
8c2654ce 2094
e700e296 2095 GetStore()->ItemAdded( parent, res );
8c2654ce 2096
e700e296 2097 return res;
a75124d0
RR
2098}
2099
2100wxDataViewItem wxDataViewTreeCtrl::AppendContainer( const wxDataViewItem& parent,
2101 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2102{
2103 wxIcon icon = wxNullIcon;
2104 if (m_imageList && (iconIndex != -1))
2105 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2106
a75124d0
RR
2107 wxIcon expanded = wxNullIcon;
2108 if (m_imageList && (expandedIndex != -1))
2109 expanded = m_imageList->GetIcon( expandedIndex );
777f9cef 2110
e700e296 2111 wxDataViewItem res = GetStore()->AppendContainer( parent, text, icon, expanded, data );
8c2654ce 2112
e700e296 2113 GetStore()->ItemAdded( parent, res );
8c2654ce 2114
e700e296 2115 return res;
a75124d0
RR
2116}
2117
2118wxDataViewItem wxDataViewTreeCtrl::InsertContainer( const wxDataViewItem& parent, const wxDataViewItem& previous,
2119 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2120{
2121 wxIcon icon = wxNullIcon;
2122 if (m_imageList && (iconIndex != -1))
2123 icon = m_imageList->GetIcon( iconIndex );
777f9cef 2124
a75124d0
RR
2125 wxIcon expanded = wxNullIcon;
2126 if (m_imageList && (expandedIndex != -1))
2127 expanded = m_imageList->GetIcon( expandedIndex );
777f9cef 2128
e700e296 2129 wxDataViewItem res = GetStore()->InsertContainer( parent, previous, text, icon, expanded, data );
8c2654ce 2130
e700e296 2131 GetStore()->ItemAdded( parent, res );
8c2654ce 2132
e700e296
RR
2133 return res;
2134}
2135
2136void wxDataViewTreeCtrl::SetItemText( const wxDataViewItem& item, const wxString &text )
2137{
2138 GetStore()->SetItemText(item,text);
8c2654ce 2139
e700e296
RR
2140 // notify control
2141 GetStore()->ValueChanged( item, 0 );
2142}
2143
2144void wxDataViewTreeCtrl::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
2145{
2146 GetStore()->SetItemIcon(item,icon);
8c2654ce 2147
e700e296
RR
2148 // notify control
2149 GetStore()->ValueChanged( item, 0 );
2150}
2151
2152void wxDataViewTreeCtrl::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
2153{
2154 GetStore()->SetItemExpandedIcon(item,icon);
8c2654ce 2155
e700e296
RR
2156 // notify control
2157 GetStore()->ValueChanged( item, 0 );
2158}
2159
2160void wxDataViewTreeCtrl::DeleteItem( const wxDataViewItem& item )
2161{
2162 wxDataViewItem parent_item = GetStore()->GetParent( item );
2163
2164 GetStore()->DeleteItem(item);
8c2654ce 2165
e700e296
RR
2166 // notify control
2167 GetStore()->ItemDeleted( parent_item, item );
2168}
2169
2170void wxDataViewTreeCtrl::DeleteChildren( const wxDataViewItem& item )
2171{
2f21b2be
RR
2172 wxDataViewTreeStoreContainerNode *node = GetStore()->FindContainerNode( item );
2173 if (!node) return;
2174
2175 wxDataViewItemArray array;
2176 wxDataViewTreeStoreNodeList::iterator iter;
2177 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
2178 {
2179 wxDataViewTreeStoreNode* child = *iter;
2180 array.Add( child->GetItem() );
2181 }
2182
2183 GetStore()->DeleteChildren( item );
2184
2185 // notify control
2186 GetStore()->ItemsDeleted( item, array );
e700e296
RR
2187}
2188
2189void wxDataViewTreeCtrl::DeleteAllItems()
2190{
2191 GetStore()->DeleteAllItems();
8c2654ce 2192
e700e296 2193 GetStore()->Cleared();
a75124d0
RR
2194}
2195
a75124d0
RR
2196void wxDataViewTreeCtrl::OnExpanded( wxDataViewEvent &event )
2197{
2198 if (m_imageList) return;
777f9cef 2199
a75124d0
RR
2200 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
2201 if (!container) return;
777f9cef 2202
a75124d0 2203 container->SetExpanded( true );
8c2654ce 2204
a75124d0
RR
2205 GetStore()->ItemChanged( event.GetItem() );
2206}
2207
2208void wxDataViewTreeCtrl::OnCollapsed( wxDataViewEvent &event )
2209{
2210 if (m_imageList) return;
777f9cef 2211
a75124d0
RR
2212 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
2213 if (!container) return;
777f9cef 2214
a75124d0 2215 container->SetExpanded( false );
8c2654ce 2216
a75124d0
RR
2217 GetStore()->ItemChanged( event.GetItem() );
2218}
2219
a6aaf49f 2220void wxDataViewTreeCtrl::OnSize( wxSizeEvent &event )
1a07a730 2221{
ad386793 2222#if defined(wxUSE_GENERICDATAVIEWCTRL)
4bfd0ed5
VZ
2223 // automatically resize our only column to take the entire control width
2224 if ( GetColumnCount() )
2225 {
2226 wxSize size = GetClientSize();
2227 GetColumn(0)->SetWidth(size.x);
2228 }
1a07a730 2229#endif
a6aaf49f 2230 event.Skip( true );
1a07a730 2231}
a75124d0 2232
b5ec7dd6 2233#endif // wxUSE_DATAVIEWCTRL
e0000f94 2234