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