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