]> git.saurik.com Git - wxWidgets.git/blame - src/common/datavcmn.cpp
Add "checked" property for toolbar tool elements in XRC.
[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] );
0bddfd41 454 /* wxDataViewModel:: */ ItemDeleted( wxDataViewItem(0), item );
e39de702 455 m_hash.RemoveAt( row );
8b6cf9bf
RR
456}
457
458void wxDataViewIndexListModel::RowsDeleted( const wxArrayInt &rows )
459{
460 wxArrayInt sorted = rows;
461 sorted.Sort( my_sort );
777f9cef 462
e39de702 463 m_ordered = false;
777f9cef 464
e39de702
RR
465 wxDataViewItemArray array;
466 unsigned int i;
467 for (i = 0; i < rows.GetCount(); i++)
468 {
8b6cf9bf
RR
469 wxDataViewItem item( m_hash[rows[i]] );
470 array.Add( item );
e39de702 471 }
0bddfd41 472 /* wxDataViewModel:: */ ItemsDeleted( wxDataViewItem(0), array );
777f9cef 473
e39de702 474 for (i = 0; i < sorted.GetCount(); i++)
8b6cf9bf 475 m_hash.RemoveAt( sorted[i] );
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{
675}
676
dbab29b9 677wxDataViewCtrl* wxDataViewRendererBase::GetView() const
9861f022 678{
5c33522f 679 return const_cast<wxDataViewRendererBase*>(this)->GetOwner()->GetOwner();
6842a71a 680}
f554a14b 681
e0062c04 682bool wxDataViewRendererBase::StartEditing( const wxDataViewItem &item, wxRect labelRect )
1e510b1e 683{
0a807957
RR
684 wxDataViewCtrl* dv_ctrl = GetOwner()->GetOwner();
685
686 // Before doing anything we send an event asking if editing of this item is really wanted.
687 wxDataViewEvent start_event( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING, dv_ctrl->GetId() );
688 start_event.SetDataViewColumn( GetOwner() );
689 start_event.SetModel( dv_ctrl->GetModel() );
690 start_event.SetItem( item );
691 start_event.SetEventObject( dv_ctrl );
692 dv_ctrl->GetEventHandler()->ProcessEvent( start_event );
693 if( !start_event.IsAllowed() )
694 return false;
695
e0062c04 696 m_item = item; // remember for later
900af470 697
1e510b1e
RR
698 unsigned int col = GetOwner()->GetModelColumn();
699 wxVariant value;
0a807957
RR
700 dv_ctrl->GetModel()->GetValue( value, item, col );
701
702 m_editorCtrl = CreateEditorCtrl( dv_ctrl->GetMainWindow(), labelRect, value );
703
03647350 704 // there might be no editor control for the given item
0a807957
RR
705 if(!m_editorCtrl)
706 return false;
900af470 707
900af470 708 wxDataViewEditorCtrlEvtHandler *handler =
30715fa1 709 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl, (wxDataViewRenderer*) this );
900af470 710
30715fa1 711 m_editorCtrl->PushEventHandler( handler );
900af470 712
30715fa1
RR
713#if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
714 handler->SetFocusOnIdle();
715#else
1e510b1e 716 m_editorCtrl->SetFocus();
30715fa1
RR
717#endif
718
afebb87b 719 // Now we should send Editing Started event
0a807957 720 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, dv_ctrl->GetId() );
afebb87b 721 event.SetDataViewColumn( GetOwner() );
0a807957 722 event.SetModel( dv_ctrl->GetModel() );
afebb87b 723 event.SetItem( item );
0a807957
RR
724 event.SetEventObject( dv_ctrl );
725 dv_ctrl->GetEventHandler()->ProcessEvent( event );
afebb87b 726
1e510b1e
RR
727 return true;
728}
729
66c02e6e
VZ
730void wxDataViewRendererBase::DestroyEditControl()
731{
b2fd3bea
VZ
732 // Remove our event handler first to prevent it from (recursively) calling
733 // us again as it would do via a call to FinishEditing() when the editor
734 // loses focus when we hide it below.
735 wxEvtHandler * const handler = m_editorCtrl->PopEventHandler();
736
66c02e6e
VZ
737 // Hide the control immediately but don't delete it yet as there could be
738 // some pending messages for it.
739 m_editorCtrl->Hide();
740
66c02e6e
VZ
741 wxPendingDelete.Append(handler);
742 wxPendingDelete.Append(m_editorCtrl);
743}
744
1e510b1e
RR
745void wxDataViewRendererBase::CancelEditing()
746{
0a807957
RR
747 if (!m_editorCtrl)
748 return;
23318a53 749
66c02e6e 750 DestroyEditControl();
1e510b1e
RR
751}
752
753bool wxDataViewRendererBase::FinishEditing()
754{
0a807957
RR
755 if (!m_editorCtrl)
756 return true;
33a1719a 757
1e510b1e
RR
758 wxVariant value;
759 GetValueFromEditorCtrl( m_editorCtrl, value );
760
0a807957
RR
761 wxDataViewCtrl* dv_ctrl = GetOwner()->GetOwner();
762
763 dv_ctrl->GetMainWindow()->SetFocus();
900af470 764
66c02e6e 765 DestroyEditControl();
777f9cef 766
2a648479 767 bool isValid = Validate(value);
1e510b1e 768 unsigned int col = GetOwner()->GetModelColumn();
900af470 769
afebb87b 770 // Now we should send Editing Done event
0a807957 771 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, dv_ctrl->GetId() );
afebb87b 772 event.SetDataViewColumn( GetOwner() );
0a807957 773 event.SetModel( dv_ctrl->GetModel() );
8fdaf7de 774 event.SetItem( m_item );
2a648479
VZ
775 event.SetValue( value );
776 event.SetColumn( col );
777 event.SetEditCanceled( !isValid );
0a807957
RR
778 event.SetEventObject( dv_ctrl );
779 dv_ctrl->GetEventHandler()->ProcessEvent( event );
900af470 780
2a648479
VZ
781 if ( isValid && event.IsAllowed() )
782 {
783 dv_ctrl->GetModel()->ChangeValue(value, m_item, col);
784 return true;
785 }
786
787 return false;
1e510b1e
RR
788}
789
f0ccd2cb
VS
790void wxDataViewRendererBase::PrepareForItem(const wxDataViewModel *model,
791 const wxDataViewItem& item,
792 unsigned column)
793{
794 wxVariant value;
795 model->GetValue(value, item, column);
796 SetValue(value);
797
798 wxDataViewItemAttr attr;
799 model->GetAttr(item, column, attr);
800 SetAttr(attr);
98f8e666
VZ
801
802 SetEnabled(model->IsEnabled(item, column));
f0ccd2cb
VS
803}
804
805
62265c2c
VZ
806// ----------------------------------------------------------------------------
807// wxDataViewCustomRendererBase
808// ----------------------------------------------------------------------------
809
810void
811wxDataViewCustomRendererBase::WXCallRender(wxRect rectCell, wxDC *dc, int state)
812{
813 wxCHECK_RET( dc, "no DC to draw on in custom renderer?" );
814
815 // adjust the rectangle ourselves to account for the alignment
816 wxRect rectItem = rectCell;
817 const int align = GetAlignment();
818 if ( align != wxDVR_DEFAULT_ALIGNMENT )
819 {
820 const wxSize size = GetSize();
821
822 // take alignment into account only if there is enough space, otherwise
823 // show as much contents as possible
824 //
825 // notice that many existing renderers (e.g. wxDataViewSpinRenderer)
826 // return hard-coded size which can be more than they need and if we
827 // trusted their GetSize() we'd draw the text out of cell bounds
828 // entirely
829
830 if ( size.x >= 0 && size.x < rectCell.width )
831 {
832 if ( align & wxALIGN_CENTER_HORIZONTAL )
833 rectItem.x += (rectCell.width - size.x)/2;
834 else if ( align & wxALIGN_RIGHT )
835 rectItem.x += rectCell.width - size.x;
836 // else: wxALIGN_LEFT is the default
837
838 rectItem.width = size.x;
839 }
840
841 if ( size.y >= 0 && size.y < rectCell.height )
842 {
843 if ( align & wxALIGN_CENTER_VERTICAL )
844 rectItem.y += (rectCell.height - size.y)/2;
845 else if ( align & wxALIGN_BOTTOM )
846 rectItem.y += rectCell.height - size.y;
847 // else: wxALIGN_TOP is the default
848
849 rectItem.height = size.y;
850 }
851 }
852
853
854 // set up the DC attributes
855
856 // override custom foreground with the standard one for the selected items
857 // because we currently don't allow changing the selection background and
858 // custom colours may be unreadable on it
859 wxColour col;
860 if ( state & wxDATAVIEW_CELL_SELECTED )
861 col = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
862 else if ( m_attr.HasColour() )
863 col = m_attr.GetColour();
864 else // use default foreground
865 col = GetOwner()->GetOwner()->GetForegroundColour();
866
867 wxDCTextColourChanger changeFg(*dc, col);
868
869 wxDCFontChanger changeFont(*dc);
870 if ( m_attr.HasFont() )
86755098 871 changeFont.Set(m_attr.GetEffectiveFont(dc->GetFont()));
62265c2c
VZ
872
873 Render(rectItem, dc, state);
874}
875
86755098
VS
876wxSize wxDataViewCustomRendererBase::GetTextExtent(const wxString& str) const
877{
878 const wxDataViewCtrl *view = GetView();
879
880 if ( m_attr.HasFont() )
881 {
882 wxFont font(m_attr.GetEffectiveFont(view->GetFont()));
883 wxSize size;
884 view->GetTextExtent(str, &size.x, &size.y, NULL, NULL, &font);
885 return size;
886 }
887 else
888 {
889 return view->GetTextExtent(str);
890 }
891}
892
62265c2c
VZ
893void
894wxDataViewCustomRendererBase::RenderText(const wxString& text,
895 int xoffset,
896 wxRect rect,
897 wxDC *dc,
898 int WXUNUSED(state))
899{
900 wxRect rectText = rect;
901 rectText.x += xoffset;
902 rectText.width -= xoffset;
903
904 // check if we want to ellipsize the text if it doesn't fit
905 wxString ellipsizedText;
906 if ( GetEllipsizeMode() != wxELLIPSIZE_NONE )
907 {
908 ellipsizedText = wxControl::Ellipsize
909 (
910 text,
911 *dc,
912 GetEllipsizeMode(),
c3fe02e9 913 rectText.width,
62265c2c
VZ
914 wxELLIPSIZE_FLAGS_NONE
915 );
916 }
917
9e9cfb91
VZ
918 // get the alignment to use
919 int align = GetAlignment();
920 if ( align == wxDVR_DEFAULT_ALIGNMENT )
921 {
922 // if we don't have an explicit alignment ourselves, use that of the
923 // column in horizontal direction and default vertical alignment
924 align = GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL;
925 }
926
62265c2c 927 dc->DrawLabel(ellipsizedText.empty() ? text : ellipsizedText,
9e9cfb91 928 rectText, align);
62265c2c
VZ
929}
930
1e510b1e
RR
931//-----------------------------------------------------------------------------
932// wxDataViewEditorCtrlEvtHandler
933//-----------------------------------------------------------------------------
934
935BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler, wxEvtHandler)
936 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar)
937 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus)
30715fa1 938 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle)
724852da 939 EVT_TEXT_ENTER (-1, wxDataViewEditorCtrlEvtHandler::OnTextEnter)
1e510b1e
RR
940END_EVENT_TABLE()
941
30715fa1
RR
942void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent &event )
943{
944 if (m_focusOnIdle)
945 {
946 m_focusOnIdle = false;
947 if (wxWindow::FindFocus() != m_editorCtrl)
948 m_editorCtrl->SetFocus();
949 }
900af470 950
30715fa1
RR
951 event.Skip();
952}
953
724852da
RR
954void wxDataViewEditorCtrlEvtHandler::OnTextEnter( wxCommandEvent &WXUNUSED(event) )
955{
956 m_finished = true;
957 m_owner->FinishEditing();
958}
959
1e510b1e
RR
960void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent &event )
961{
962 switch ( event.m_keyCode )
963 {
964 case WXK_RETURN:
965 m_finished = true;
966 m_owner->FinishEditing();
967 break;
968
969 case WXK_ESCAPE:
724852da 970 {
1e510b1e
RR
971 m_finished = true;
972 m_owner->CancelEditing();
973 break;
724852da 974 }
1e510b1e
RR
975 default:
976 event.Skip();
977 }
978}
979
980void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent &event )
981{
982 if (!m_finished)
983 {
984 m_finished = true;
985 m_owner->FinishEditing();
986 }
987
1e510b1e
RR
988 event.Skip();
989}
990
f554a14b 991// ---------------------------------------------------------
fa28826d 992// wxDataViewColumnBase
f554a14b 993// ---------------------------------------------------------
fa28826d 994
e2bfe673
VZ
995void wxDataViewColumnBase::Init(wxDataViewRenderer *renderer,
996 unsigned int model_column)
07a84e7b
RR
997{
998 m_renderer = renderer;
999 m_model_column = model_column;
07a84e7b
RR
1000 m_owner = NULL;
1001 m_renderer->SetOwner( (wxDataViewColumn*) this );
1002}
1003
6842a71a
RR
1004wxDataViewColumnBase::~wxDataViewColumnBase()
1005{
56873923 1006 delete m_renderer;
07a84e7b
RR
1007}
1008
f554a14b 1009// ---------------------------------------------------------
239eaa41 1010// wxDataViewCtrlBase
f554a14b 1011// ---------------------------------------------------------
239eaa41
RR
1012
1013IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl)
1014
1015wxDataViewCtrlBase::wxDataViewCtrlBase()
1016{
1017 m_model = NULL;
3b6280be
RR
1018 m_expander_column = 0;
1019 m_indent = 8;
239eaa41
RR
1020}
1021
1022wxDataViewCtrlBase::~wxDataViewCtrlBase()
1023{
87f0efe2
RR
1024 if (m_model)
1025 {
1026 m_model->DecRef();
1027 m_model = NULL;
1028 }
239eaa41
RR
1029}
1030
e0062c04 1031bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel *model )
239eaa41 1032{
1821abd1
RR
1033 if (m_model)
1034 {
87f0efe2 1035 m_model->DecRef(); // discard old model, if any
1821abd1 1036 }
87f0efe2
RR
1037
1038 // add our own reference to the new model:
239eaa41 1039 m_model = model;
1821abd1
RR
1040 if (m_model)
1041 {
900af470 1042 m_model->IncRef();
1821abd1 1043 }
f554a14b 1044
239eaa41
RR
1045 return true;
1046}
1047
e0062c04 1048wxDataViewModel* wxDataViewCtrlBase::GetModel()
239eaa41
RR
1049{
1050 return m_model;
1051}
1052
a75124d0
RR
1053const wxDataViewModel* wxDataViewCtrlBase::GetModel() const
1054{
1055 return m_model;
1056}
1057
4219d8b0 1058void wxDataViewCtrlBase::ExpandAncestors( const wxDataViewItem & item )
a881f34e
RR
1059{
1060 if (!m_model) return;
8c2654ce 1061
4219d8b0 1062 if (!item.IsOk()) return;
a881f34e
RR
1063
1064 wxVector<wxDataViewItem> parentChain;
8c2654ce 1065
a881f34e
RR
1066 // at first we get all the parents of the selected item
1067 wxDataViewItem parent = m_model->GetParent(item);
1068 while (parent.IsOk())
1069 {
1070 parentChain.push_back(parent);
1071 parent = m_model->GetParent(parent);
1072 }
8c2654ce 1073
a881f34e
RR
1074 // then we expand the parents, starting at the root
1075 while (!parentChain.empty())
1076 {
1077 Expand(parentChain.back());
1078 parentChain.pop_back();
1079 }
1080}
1081
80ce465c
VZ
1082wxDataViewItem wxDataViewCtrlBase::GetCurrentItem() const
1083{
1084 return HasFlag(wxDV_MULTIPLE) ? DoGetCurrentItem()
1085 : GetSelection();
1086}
1087
1088void wxDataViewCtrlBase::SetCurrentItem(const wxDataViewItem& item)
1089{
1090 wxCHECK_RET( item.IsOk(), "Can't make current an invalid item." );
1091
1092 if ( HasFlag(wxDV_MULTIPLE) )
1093 DoSetCurrentItem(item);
1094 else
1095 Select(item);
1096}
1097
fa93d732
VZ
1098wxDataViewItem wxDataViewCtrlBase::GetSelection() const
1099{
1100 if ( GetSelectedItemsCount() != 1 )
1101 return wxDataViewItem();
1102
1103 wxDataViewItemArray selections;
1104 GetSelections(selections);
1105 return selections[0];
1106}
1107
c7074d44
RR
1108wxDataViewColumn *
1109wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column,
87f0efe2 1110 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
fa28826d 1111{
c7074d44 1112 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1113 new wxDataViewTextRenderer( wxT("string"), mode ),
c7074d44
RR
1114 model_column, width, align, flags );
1115 AppendColumn( ret );
1116 return ret;
fa28826d
RR
1117}
1118
b04fcede
RR
1119wxDataViewColumn *
1120wxDataViewCtrlBase::AppendIconTextColumn( const wxString &label, unsigned int model_column,
1121 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1122{
1123 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1124 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
b04fcede
RR
1125 model_column, width, align, flags );
1126 AppendColumn( ret );
1127 return ret;
1128}
1129
c7074d44
RR
1130wxDataViewColumn *
1131wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column,
87f0efe2 1132 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
605c2c4a 1133{
c7074d44 1134 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1135 new wxDataViewToggleRenderer( wxT("bool"), mode ),
c7074d44
RR
1136 model_column, width, align, flags );
1137 AppendColumn( ret );
1138 return ret;
605c2c4a
RR
1139}
1140
c7074d44
RR
1141wxDataViewColumn *
1142wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column,
87f0efe2 1143 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
ad63bf41 1144{
c7074d44 1145 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1146 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
c7074d44
RR
1147 model_column, width, align, flags );
1148 AppendColumn( ret );
1149 return ret;
ad63bf41
RR
1150}
1151
c7074d44
RR
1152wxDataViewColumn *
1153wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column,
87f0efe2 1154 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
4d496ecb 1155{
c7074d44 1156 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1157 new wxDataViewDateRenderer( wxT("datetime"), mode ),
c7074d44
RR
1158 model_column, width, align, flags );
1159 AppendColumn( ret );
1160 return ret;
4d496ecb
RR
1161}
1162
c7074d44
RR
1163wxDataViewColumn *
1164wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column,
87f0efe2 1165 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1166{
c7074d44 1167 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1168 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
c7074d44
RR
1169 model_column, width, align, flags );
1170 AppendColumn( ret );
1171 return ret;
07a84e7b
RR
1172}
1173
c7074d44
RR
1174wxDataViewColumn *
1175wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1176 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1177{
c7074d44 1178 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1179 new wxDataViewTextRenderer( wxT("string"), mode ),
c7074d44
RR
1180 model_column, width, align, flags );
1181 AppendColumn( ret );
1182 return ret;
b04fcede
RR
1183}
1184
1185wxDataViewColumn *
1186wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap &label, unsigned int model_column,
1187 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1188{
1189 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1190 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
b04fcede
RR
1191 model_column, width, align, flags );
1192 AppendColumn( ret );
1193 return ret;
07a84e7b
RR
1194}
1195
c7074d44
RR
1196wxDataViewColumn *
1197wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1198 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1199{
c7074d44 1200 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1201 new wxDataViewToggleRenderer( wxT("bool"), mode ),
c7074d44
RR
1202 model_column, width, align, flags );
1203 AppendColumn( ret );
1204 return ret;
07a84e7b
RR
1205}
1206
c7074d44
RR
1207wxDataViewColumn *
1208wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1209 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1210{
c7074d44 1211 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1212 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
c7074d44
RR
1213 model_column, width, align, flags );
1214 AppendColumn( ret );
1215 return ret;
07a84e7b
RR
1216}
1217
c7074d44
RR
1218wxDataViewColumn *
1219wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1220 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1221{
c7074d44 1222 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1223 new wxDataViewDateRenderer( wxT("datetime"), mode ),
c7074d44
RR
1224 model_column, width, align, flags );
1225 AppendColumn( ret );
1226 return ret;
07a84e7b
RR
1227}
1228
c7074d44
RR
1229wxDataViewColumn *
1230wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1231 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1232{
c7074d44 1233 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1234 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
c7074d44
RR
1235 model_column, width, align, flags );
1236 AppendColumn( ret );
1237 return ret;
07a84e7b
RR
1238}
1239
736fe67c
RR
1240wxDataViewColumn *
1241wxDataViewCtrlBase::PrependTextColumn( const wxString &label, unsigned int model_column,
1242 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1243{
1244 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1245 new wxDataViewTextRenderer( wxT("string"), mode ),
736fe67c
RR
1246 model_column, width, align, flags );
1247 PrependColumn( ret );
1248 return ret;
1249}
1250
1251wxDataViewColumn *
1252wxDataViewCtrlBase::PrependIconTextColumn( const wxString &label, unsigned int model_column,
1253 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1254{
1255 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1256 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
736fe67c
RR
1257 model_column, width, align, flags );
1258 PrependColumn( ret );
1259 return ret;
1260}
1261
1262wxDataViewColumn *
1263wxDataViewCtrlBase::PrependToggleColumn( const wxString &label, unsigned int model_column,
1264 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1265{
1266
1267 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1268 new wxDataViewToggleRenderer( wxT("bool"), mode ),
736fe67c
RR
1269 model_column, width, align, flags );
1270 PrependColumn( ret );
1271 return ret;
1272}
1273
1274wxDataViewColumn *
1275wxDataViewCtrlBase::PrependProgressColumn( const wxString &label, unsigned int model_column,
1276 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1277{
1278 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1279 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
736fe67c
RR
1280 model_column, width, align, flags );
1281 PrependColumn( ret );
1282 return ret;
1283}
1284
1285wxDataViewColumn *
1286wxDataViewCtrlBase::PrependDateColumn( const wxString &label, unsigned int model_column,
1287 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1288{
1289 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1290 new wxDataViewDateRenderer( wxT("datetime"), mode ),
736fe67c
RR
1291 model_column, width, align, flags );
1292 PrependColumn( ret );
1293 return ret;
1294}
1295
1296wxDataViewColumn *
1297wxDataViewCtrlBase::PrependBitmapColumn( const wxString &label, unsigned int model_column,
1298 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1299{
1300 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1301 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
736fe67c
RR
1302 model_column, width, align, flags );
1303 PrependColumn( ret );
1304 return ret;
1305}
1306
1307wxDataViewColumn *
1308wxDataViewCtrlBase::PrependTextColumn( const wxBitmap &label, unsigned int model_column,
1309 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1310{
1311 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1312 new wxDataViewTextRenderer( wxT("string"), mode ),
736fe67c
RR
1313 model_column, width, align, flags );
1314 PrependColumn( ret );
1315 return ret;
1316}
1317
1318wxDataViewColumn *
1319wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap &label, unsigned int model_column,
1320 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1321{
1322 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1323 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
736fe67c
RR
1324 model_column, width, align, flags );
1325 PrependColumn( ret );
1326 return ret;
1327}
1328
1329wxDataViewColumn *
1330wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap &label, unsigned int model_column,
1331 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1332{
1333 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1334 new wxDataViewToggleRenderer( wxT("bool"), mode ),
736fe67c
RR
1335 model_column, width, align, flags );
1336 PrependColumn( ret );
1337 return ret;
1338}
1339
1340wxDataViewColumn *
1341wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap &label, unsigned int model_column,
1342 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1343{
1344 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1345 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
736fe67c
RR
1346 model_column, width, align, flags );
1347 PrependColumn( ret );
1348 return ret;
1349}
1350
1351wxDataViewColumn *
1352wxDataViewCtrlBase::PrependDateColumn( const wxBitmap &label, unsigned int model_column,
1353 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1354{
1355 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1356 new wxDataViewDateRenderer( wxT("datetime"), mode ),
736fe67c
RR
1357 model_column, width, align, flags );
1358 PrependColumn( ret );
1359 return ret;
1360}
1361
1362wxDataViewColumn *
1363wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap &label, unsigned int model_column,
1364 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1365{
1366 wxDataViewColumn *ret = new wxDataViewColumn( label,
f2b7492a 1367 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
736fe67c
RR
1368 model_column, width, align, flags );
1369 PrependColumn( ret );
1370 return ret;
1371}
1372
c7074d44
RR
1373bool
1374wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
fa28826d 1375{
6842a71a 1376 col->SetOwner( (wxDataViewCtrl*) this );
fa28826d
RR
1377 return true;
1378}
1379
736fe67c
RR
1380bool
1381wxDataViewCtrlBase::PrependColumn( wxDataViewColumn *col )
1382{
1383 col->SetOwner( (wxDataViewCtrl*) this );
1384 return true;
1385}
1386
23318a53 1387bool
19723525
RR
1388wxDataViewCtrlBase::InsertColumn( unsigned int WXUNUSED(pos), wxDataViewColumn *col )
1389{
1390 col->SetOwner( (wxDataViewCtrl*) this );
1391 return true;
1392}
1393
eb7f97f8
RR
1394// ---------------------------------------------------------
1395// wxDataViewEvent
1396// ---------------------------------------------------------
1397
1398IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent)
1399
9b11752c
VZ
1400wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, wxDataViewEvent );
1401
1402wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, wxDataViewEvent );
1403wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, wxDataViewEvent );
1404wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, wxDataViewEvent );
1405wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, wxDataViewEvent );
1406wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, wxDataViewEvent );
1407wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, wxDataViewEvent );
3e978cf7 1408wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING, wxDataViewEvent );
9b11752c
VZ
1409wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, wxDataViewEvent );
1410wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, wxDataViewEvent );
1411
1412wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, wxDataViewEvent );
1413
1414wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, wxDataViewEvent );
1415wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, wxDataViewEvent );
1416wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, wxDataViewEvent );
1417wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_REORDERED, wxDataViewEvent );
1418
47a8b1e1
VZ
1419wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_CACHE_HINT, wxDataViewEvent );
1420
9b11752c
VZ
1421wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, wxDataViewEvent );
1422wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, wxDataViewEvent );
1423wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, wxDataViewEvent );
52e750fc 1424
47a8b1e1
VZ
1425
1426
52e750fc
RR
1427// -------------------------------------
1428// wxDataViewSpinRenderer
1429// -------------------------------------
1430
1431wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min, int max, wxDataViewCellMode mode, int alignment ) :
99c75ebc 1432 wxDataViewCustomRenderer(wxT("long"), mode, alignment )
b5ec7dd6 1433{
52e750fc
RR
1434 m_min = min;
1435 m_max = max;
1436}
b5ec7dd6 1437
64c70359 1438wxWindow* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
b5ec7dd6 1439{
52e750fc 1440 long l = value;
659fb68d
RR
1441 wxSize size = labelRect.GetSize();
1442#ifdef __WXMAC__
1443 size = wxSize( wxMax(70,labelRect.width ), -1 );
1444#endif
1445 wxString str;
2ddfe921 1446 str.Printf( wxT("%d"), (int) l );
659fb68d 1447 wxSpinCtrl *sc = new wxSpinCtrl( parent, wxID_ANY, str,
724852da 1448 labelRect.GetTopLeft(), size, wxSP_ARROW_KEYS|wxTE_PROCESS_ENTER, m_min, m_max, l );
659fb68d
RR
1449#ifdef __WXMAC__
1450 size = sc->GetSize();
1451 wxPoint pt = sc->GetPosition();
1452 sc->SetSize( pt.x - 4, pt.y - 4, size.x, size.y );
1453#endif
777f9cef 1454
659fb68d 1455 return sc;
52e750fc 1456}
b5ec7dd6 1457
64c70359 1458bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value )
b5ec7dd6 1459{
52e750fc
RR
1460 wxSpinCtrl *sc = (wxSpinCtrl*) editor;
1461 long l = sc->GetValue();
1462 value = l;
1463 return true;
1464}
b5ec7dd6 1465
52e750fc
RR
1466bool wxDataViewSpinRenderer::Render( wxRect rect, wxDC *dc, int state )
1467{
1468 wxString str;
99c75ebc 1469 str.Printf(wxT("%d"), (int) m_data );
52e750fc
RR
1470 RenderText( str, 0, rect, dc, state );
1471 return true;
1472}
b5ec7dd6 1473
52e750fc
RR
1474wxSize wxDataViewSpinRenderer::GetSize() const
1475{
a1c9e7d1
VS
1476 wxSize sz = GetTextExtent(wxString::Format("%d", (int)m_data));
1477
1478 // Allow some space for the spin buttons, which is approximately the size
1479 // of a scrollbar (and getting pixel-exact value would be complicated).
1480 // Also add some whitespace between the text and the button:
1481 sz.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1482 sz.x += GetTextExtent("M").x;
1483
1484 return sz;
52e750fc 1485}
b5ec7dd6 1486
52e750fc
RR
1487bool wxDataViewSpinRenderer::SetValue( const wxVariant &value )
1488{
1489 m_data = value.GetLong();
1490 return true;
1491}
b5ec7dd6 1492
52e750fc
RR
1493bool wxDataViewSpinRenderer::GetValue( wxVariant &value ) const
1494{
1495 value = m_data;
1496 return true;
1497}
1498
7448d67c
RR
1499// -------------------------------------
1500// wxDataViewChoiceRenderer
1501// -------------------------------------
1502
e86edab0 1503#if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXOSX_CARBON__)
7448d67c
RR
1504
1505wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString& choices, wxDataViewCellMode mode, int alignment ) :
1506 wxDataViewCustomRenderer(wxT("string"), mode, alignment )
1507{
1508 m_choices = choices;
1509}
1510
64c70359 1511wxWindow* wxDataViewChoiceRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
7448d67c 1512{
34b239ec
VS
1513 wxChoice* c = new wxChoice
1514 (
1515 parent,
1516 wxID_ANY,
1517 labelRect.GetTopLeft(),
1518 wxSize(labelRect.GetWidth(), -1),
1519 m_choices
1520 );
a3669fa9 1521 c->Move(labelRect.GetRight() - c->GetRect().width, wxDefaultCoord);
7448d67c 1522 c->SetStringSelection( value.GetString() );
7448d67c
RR
1523 return c;
1524}
1525
64c70359 1526bool wxDataViewChoiceRenderer::GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value )
7448d67c
RR
1527{
1528 wxChoice *c = (wxChoice*) editor;
1529 wxString s = c->GetStringSelection();
1530 value = s;
1531 return true;
1532}
1533
1534bool wxDataViewChoiceRenderer::Render( wxRect rect, wxDC *dc, int state )
1535{
1536 RenderText( m_data, 0, rect, dc, state );
1537 return true;
1538}
1539
1540wxSize wxDataViewChoiceRenderer::GetSize() const
1541{
a1c9e7d1
VS
1542 wxSize sz;
1543
1544 for ( wxArrayString::const_iterator i = m_choices.begin(); i != m_choices.end(); ++i )
1545 sz.IncTo(GetTextExtent(*i));
1546
1547 // Allow some space for the right-side button, which is approximately the
1548 // size of a scrollbar (and getting pixel-exact value would be complicated).
1549 // Also add some whitespace between the text and the button:
1550 sz.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1551 sz.x += GetTextExtent("M").x;
1552
1553 return sz;
7448d67c
RR
1554}
1555
1556bool wxDataViewChoiceRenderer::SetValue( const wxVariant &value )
1557{
1558 m_data = value.GetString();
1559 return true;
1560}
1561
1562bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const
1563{
1564 value = m_data;
1565 return true;
1566}
1567
65887bd0
RR
1568// ----------------------------------------------------------------------------
1569// wxDataViewChoiceByIndexRenderer
1570// ----------------------------------------------------------------------------
1571
1572wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString &choices,
1573 wxDataViewCellMode mode, int alignment ) :
1574 wxDataViewChoiceRenderer( choices, mode, alignment )
1575{
1576}
ce00f59b 1577
64c70359 1578wxWindow* wxDataViewChoiceByIndexRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
65887bd0
RR
1579{
1580 wxVariant string_value = GetChoice( value.GetLong() );
ce00f59b 1581
65887bd0
RR
1582 return wxDataViewChoiceRenderer::CreateEditorCtrl( parent, labelRect, string_value );
1583}
1584
64c70359 1585bool wxDataViewChoiceByIndexRenderer::GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value )
65887bd0
RR
1586{
1587 wxVariant string_value;
1588 if (!wxDataViewChoiceRenderer::GetValueFromEditorCtrl( editor, string_value ))
1589 return false;
1590
1591 value = (long) GetChoices().Index( string_value.GetString() );
1592 return true;
1593}
1594
1595bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant &value )
1596{
1597 wxVariant string_value = GetChoice( value.GetLong() );
1598 return wxDataViewChoiceRenderer::SetValue( string_value );
1599}
ce00f59b 1600
65887bd0
RR
1601bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant &value ) const
1602{
1603 wxVariant string_value;
1604 if (!wxDataViewChoiceRenderer::GetValue( string_value ))
1605 return false;
ce00f59b 1606
65887bd0
RR
1607 value = (long) GetChoices().Index( string_value.GetString() );
1608 return true;
1609}
1610
7448d67c
RR
1611#endif
1612
1d3a930e
VS
1613// ---------------------------------------------------------
1614// wxDataViewDateRenderer
1615// ---------------------------------------------------------
1616
1617#if (defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXGTK__)) && wxUSE_DATEPICKCTRL
1618
1619wxDataViewDateRenderer::wxDataViewDateRenderer(const wxString& varianttype,
1620 wxDataViewCellMode mode, int align)
1621 : wxDataViewCustomRenderer(varianttype, mode, align)
1622{
1623}
1624
1625wxWindow *
1626wxDataViewDateRenderer::CreateEditorCtrl(wxWindow *parent, wxRect labelRect, const wxVariant& value)
1627{
1628 return new wxDatePickerCtrl
1629 (
1630 parent,
1631 wxID_ANY,
1632 value.GetDateTime(),
1633 labelRect.GetTopLeft(),
1634 labelRect.GetSize()
1635 );
1636}
1637
1638bool wxDataViewDateRenderer::GetValueFromEditorCtrl(wxWindow *editor, wxVariant& value)
1639{
1640 wxDatePickerCtrl *ctrl = static_cast<wxDatePickerCtrl*>(editor);
1641 value = ctrl->GetValue();
1642 return true;
1643}
1644
1645bool wxDataViewDateRenderer::SetValue(const wxVariant& value)
1646{
1647 m_date = value.GetDateTime();
1648 return true;
1649}
1650
1651bool wxDataViewDateRenderer::GetValue(wxVariant& value) const
1652{
1653 value = m_date;
1654 return true;
1655}
1656
1657bool wxDataViewDateRenderer::Render(wxRect cell, wxDC* dc, int state)
1658{
1659 wxString tmp = m_date.FormatDate();
1660 RenderText( tmp, 0, cell, dc, state );
1661 return true;
1662}
1663
1664wxSize wxDataViewDateRenderer::GetSize() const
1665{
1666 return GetTextExtent(m_date.FormatDate());
1667}
1668
1669#endif // (defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXGTK__)) && wxUSE_DATEPICKCTRL
1670
8eff6c56
RR
1671//-----------------------------------------------------------------------------
1672// wxDataViewListStore
1673//-----------------------------------------------------------------------------
1674
1675wxDataViewListStore::wxDataViewListStore()
1676{
1677}
1678
1679wxDataViewListStore::~wxDataViewListStore()
1680{
1681 wxVector<wxDataViewListStoreLine*>::iterator it;
1682 for (it = m_data.begin(); it != m_data.end(); ++it)
1683 {
1684 wxDataViewListStoreLine* line = *it;
1685 delete line;
1686 }
1687}
1688
1689void wxDataViewListStore::PrependColumn( const wxString &varianttype )
1690{
1691 m_cols.Insert( varianttype, 0 );
1692}
1693
1694void wxDataViewListStore::InsertColumn( unsigned int pos, const wxString &varianttype )
1695{
1696 m_cols.Insert( varianttype, pos );
1697}
1698
1699void wxDataViewListStore::AppendColumn( const wxString &varianttype )
1700{
1701 m_cols.Add( varianttype );
1702}
1703
1704unsigned int wxDataViewListStore::GetColumnCount() const
1705{
1706 return m_cols.GetCount();
1707}
1708
1709wxString wxDataViewListStore::GetColumnType( unsigned int pos ) const
1710{
1711 return m_cols[pos];
1712}
8c2654ce 1713
8eff6c56
RR
1714void wxDataViewListStore::AppendItem( const wxVector<wxVariant> &values, wxClientData *data )
1715{
1716 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1717 line->m_values = values;
1718 m_data.push_back( line );
8c2654ce 1719
8eff6c56
RR
1720 RowAppended();
1721}
1722
1723void wxDataViewListStore::PrependItem( const wxVector<wxVariant> &values, wxClientData *data )
1724{
1725 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1726 line->m_values = values;
1727 m_data.insert( m_data.begin(), line );
8c2654ce 1728
8eff6c56
RR
1729 RowPrepended();
1730}
1731
3569e0d0 1732void wxDataViewListStore::InsertItem( unsigned int row, const wxVector<wxVariant> &values,
8af28fab 1733 wxClientData *data )
8eff6c56
RR
1734{
1735 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1736 line->m_values = values;
1737 m_data.insert( m_data.begin()+row, line );
8c2654ce 1738
8eff6c56
RR
1739 RowInserted( row );
1740}
1741
8af28fab 1742void wxDataViewListStore::DeleteItem( unsigned int row )
8eff6c56
RR
1743{
1744 wxVector<wxDataViewListStoreLine*>::iterator it = m_data.begin() + row;
2d6bba41 1745 delete *it;
8eff6c56 1746 m_data.erase( it );
8c2654ce 1747
8eff6c56
RR
1748 RowDeleted( row );
1749}
1750
1751void wxDataViewListStore::DeleteAllItems()
1752{
1753 wxVector<wxDataViewListStoreLine*>::iterator it;
1754 for (it = m_data.begin(); it != m_data.end(); ++it)
1755 {
1756 wxDataViewListStoreLine* line = *it;
1757 delete line;
1758 }
8c2654ce 1759
8af28fab
FM
1760 m_data.clear();
1761
8eff6c56
RR
1762 Reset( 0 );
1763}
1764
1765void wxDataViewListStore::GetValueByRow( wxVariant &value, unsigned int row, unsigned int col ) const
1766{
1767 wxDataViewListStoreLine *line = m_data[row];
1768 value = line->m_values[col];
1769}
1770
1771bool wxDataViewListStore::SetValueByRow( const wxVariant &value, unsigned int row, unsigned int col )
1772{
1773 wxDataViewListStoreLine *line = m_data[row];
1774 line->m_values[col] = value;
8c2654ce 1775
8eff6c56
RR
1776 return true;
1777}
1778
dc813e6c
RR
1779//-----------------------------------------------------------------------------
1780// wxDataViewListCtrl
1781//-----------------------------------------------------------------------------
1782
1783IMPLEMENT_DYNAMIC_CLASS(wxDataViewListCtrl,wxDataViewCtrl)
1784
1785BEGIN_EVENT_TABLE(wxDataViewListCtrl,wxDataViewCtrl)
1786 EVT_SIZE( wxDataViewListCtrl::OnSize )
1787END_EVENT_TABLE()
1788
1789wxDataViewListCtrl::wxDataViewListCtrl()
1790{
1791}
1792
1793wxDataViewListCtrl::wxDataViewListCtrl( wxWindow *parent, wxWindowID id,
1794 const wxPoint& pos, const wxSize& size, long style,
1795 const wxValidator& validator )
1796{
1797 Create( parent, id, pos, size, style, validator );
dc813e6c
RR
1798}
1799
1800wxDataViewListCtrl::~wxDataViewListCtrl()
1801{
1802}
1803
1804
1805bool wxDataViewListCtrl::Create( wxWindow *parent, wxWindowID id,
1806 const wxPoint& pos, const wxSize& size, long style,
1807 const wxValidator& validator )
1808{
9ba38914
VS
1809 if ( !wxDataViewCtrl::Create( parent, id, pos, size, style, validator ) )
1810 return false;
1811
1812 wxDataViewListStore *store = new wxDataViewListStore;
1813 AssociateModel( store );
1814 store->DecRef();
1815
1816 return true;
dc813e6c
RR
1817}
1818
95b20f41 1819bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *column, const wxString &varianttype )
dc813e6c
RR
1820{
1821 GetStore()->AppendColumn( varianttype );
95b20f41 1822 return wxDataViewCtrl::AppendColumn( column );
dc813e6c
RR
1823}
1824
95b20f41 1825bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *column, const wxString &varianttype )
dc813e6c
RR
1826{
1827 GetStore()->PrependColumn( varianttype );
95b20f41 1828 return wxDataViewCtrl::PrependColumn( column );
dc813e6c
RR
1829}
1830
95b20f41 1831bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *column, const wxString &varianttype )
dc813e6c
RR
1832{
1833 GetStore()->InsertColumn( pos, varianttype );
95b20f41
RR
1834 return wxDataViewCtrl::InsertColumn( pos, column );
1835}
1836
1837bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *col )
1838{
1839 return PrependColumn( col, "string" );
1840}
1841
1842bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
1843{
1844 return InsertColumn( pos, col, "string" );
1845}
1846
1847bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *col )
1848{
1849 return AppendColumn( col, "string" );
dc813e6c 1850}
8c2654ce 1851
3569e0d0 1852wxDataViewColumn *wxDataViewListCtrl::AppendTextColumn( const wxString &label,
dc813e6c
RR
1853 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1854{
1855 GetStore()->AppendColumn( wxT("string") );
8c2654ce 1856
95b20f41
RR
1857 wxDataViewColumn *ret = new wxDataViewColumn( label,
1858 new wxDataViewTextRenderer( wxT("string"), mode ),
1859 GetStore()->GetColumnCount()-1, width, align, flags );
8c2654ce 1860
95b20f41 1861 wxDataViewCtrl::AppendColumn( ret );
8c2654ce 1862
95b20f41 1863 return ret;
dc813e6c
RR
1864}
1865
3569e0d0 1866wxDataViewColumn *wxDataViewListCtrl::AppendToggleColumn( const wxString &label,
dc813e6c
RR
1867 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1868{
1869 GetStore()->AppendColumn( wxT("bool") );
8c2654ce 1870
95b20f41
RR
1871 wxDataViewColumn *ret = new wxDataViewColumn( label,
1872 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1873 GetStore()->GetColumnCount()-1, width, align, flags );
8c2654ce 1874
95b20f41 1875 wxDataViewCtrl::AppendColumn( ret );
8c2654ce 1876
95b20f41 1877 return ret;
dc813e6c
RR
1878}
1879
3569e0d0 1880wxDataViewColumn *wxDataViewListCtrl::AppendProgressColumn( const wxString &label,
dc813e6c
RR
1881 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1882{
1883 GetStore()->AppendColumn( wxT("long") );
8c2654ce 1884
95b20f41
RR
1885 wxDataViewColumn *ret = new wxDataViewColumn( label,
1886 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1887 GetStore()->GetColumnCount()-1, width, align, flags );
8c2654ce 1888
95b20f41 1889 wxDataViewCtrl::AppendColumn( ret );
8c2654ce 1890
95b20f41 1891 return ret;
dc813e6c
RR
1892}
1893
3569e0d0 1894wxDataViewColumn *wxDataViewListCtrl::AppendIconTextColumn( const wxString &label,
dc813e6c
RR
1895 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1896{
1897 GetStore()->AppendColumn( wxT("wxDataViewIconText") );
8c2654ce 1898
95b20f41
RR
1899 wxDataViewColumn *ret = new wxDataViewColumn( label,
1900 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1901 GetStore()->GetColumnCount()-1, width, align, flags );
8c2654ce 1902
95b20f41 1903 wxDataViewCtrl::AppendColumn( ret );
8c2654ce 1904
95b20f41 1905 return ret;
dc813e6c
RR
1906}
1907
1908void wxDataViewListCtrl::OnSize( wxSizeEvent &event )
1909{
1910 event.Skip( true );
1911}
1912
e94d0c1e
RR
1913//-----------------------------------------------------------------------------
1914// wxDataViewTreeStore
1915//-----------------------------------------------------------------------------
1916
b5ec7dd6
VZ
1917wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1918 wxDataViewTreeStoreNode *parent,
e94d0c1e
RR
1919 const wxString &text, const wxIcon &icon, wxClientData *data )
1920{
1921 m_parent = parent;
1922 m_text = text;
1923 m_icon = icon;
1924 m_data = data;
1925}
1926
1927wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1928{
1929 if (m_data)
1930 delete m_data;
1931}
b5ec7dd6 1932
e94d0c1e 1933#include "wx/listimpl.cpp"
a76c2f37 1934WX_DEFINE_LIST(wxDataViewTreeStoreNodeList)
e94d0c1e 1935
b5ec7dd6
VZ
1936wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1937 wxDataViewTreeStoreNode *parent, const wxString &text,
e94d0c1e
RR
1938 const wxIcon &icon, const wxIcon &expanded, wxClientData *data ) :
1939 wxDataViewTreeStoreNode( parent, text, icon, data )
1940{
1941 m_iconExpanded = expanded;
a75124d0 1942 m_isExpanded = false;
e94d0c1e
RR
1943 m_children.DeleteContents(true);
1944}
1945
1946wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1947{
1948}
1949
1950//-----------------------------------------------------------------------------
1951
1952wxDataViewTreeStore::wxDataViewTreeStore()
1953{
1954 m_root = new wxDataViewTreeStoreContainerNode( NULL, wxEmptyString );
1955}
1956
1957wxDataViewTreeStore::~wxDataViewTreeStore()
1958{
1959 delete m_root;
1960}
1961
b5ec7dd6 1962wxDataViewItem wxDataViewTreeStore::AppendItem( const wxDataViewItem& parent,
e94d0c1e
RR
1963 const wxString &text, const wxIcon &icon, wxClientData *data )
1964{
1965 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1966 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1967
1968 wxDataViewTreeStoreNode *node =
e94d0c1e
RR
1969 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1970 parent_node->GetChildren().Append( node );
b5ec7dd6 1971
e94d0c1e
RR
1972 return node->GetItem();
1973}
1974
1975wxDataViewItem wxDataViewTreeStore::PrependItem( const wxDataViewItem& parent,
1976 const wxString &text, const wxIcon &icon, wxClientData *data )
1977{
1978 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1979 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1980
1981 wxDataViewTreeStoreNode *node =
e94d0c1e
RR
1982 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1983 parent_node->GetChildren().Insert( node );
b5ec7dd6 1984
e94d0c1e
RR
1985 return node->GetItem();
1986}
1987
b5ec7dd6 1988wxDataViewItem
e700e296
RR
1989wxDataViewTreeStore::InsertItem(const wxDataViewItem& parent,
1990 const wxDataViewItem& previous,
1991 const wxString& text,
1992 const wxIcon& icon,
1993 wxClientData *data)
e94d0c1e 1994{
e700e296
RR
1995 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1996 if (!parent_node) return wxDataViewItem(0);
1997
1998 wxDataViewTreeStoreNode *previous_node = FindNode( previous );
1999 int pos = parent_node->GetChildren().IndexOf( previous_node );
2000 if (pos == wxNOT_FOUND) return wxDataViewItem(0);
2001
2002 wxDataViewTreeStoreNode *node =
2003 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
2004 parent_node->GetChildren().Insert( (size_t) pos, node );
2005
2006 return node->GetItem();
e94d0c1e
RR
2007}
2008
b5ec7dd6
VZ
2009wxDataViewItem wxDataViewTreeStore::PrependContainer( const wxDataViewItem& parent,
2010 const wxString &text, const wxIcon &icon, const wxIcon &expanded,
e94d0c1e
RR
2011 wxClientData *data )
2012{
2013 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
2014 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
2015
2016 wxDataViewTreeStoreContainerNode *node =
e94d0c1e
RR
2017 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
2018 parent_node->GetChildren().Insert( node );
b5ec7dd6 2019
e94d0c1e
RR
2020 return node->GetItem();
2021}
2022
b5ec7dd6
VZ
2023wxDataViewItem
2024wxDataViewTreeStore::AppendContainer(const wxDataViewItem& parent,
2025 const wxString &text,
2026 const wxIcon& icon,
2027 const wxIcon& expanded,
2028 wxClientData * data)
e94d0c1e
RR
2029{
2030 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
2031 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
2032
2033 wxDataViewTreeStoreContainerNode *node =
e94d0c1e
RR
2034 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
2035 parent_node->GetChildren().Append( node );
b5ec7dd6 2036
e94d0c1e
RR
2037 return node->GetItem();
2038}
2039
b5ec7dd6 2040wxDataViewItem
e700e296
RR
2041wxDataViewTreeStore::InsertContainer(const wxDataViewItem& parent,
2042 const wxDataViewItem& previous,
2043 const wxString& text,
2044 const wxIcon& icon,
2045 const wxIcon& expanded,
2046 wxClientData * data)
e94d0c1e 2047{
e700e296
RR
2048 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
2049 if (!parent_node) return wxDataViewItem(0);
8c2654ce 2050
e700e296
RR
2051 wxDataViewTreeStoreNode *previous_node = FindNode( previous );
2052 int pos = parent_node->GetChildren().IndexOf( previous_node );
2053 if (pos == wxNOT_FOUND) return wxDataViewItem(0);
8c2654ce 2054
e700e296
RR
2055 wxDataViewTreeStoreContainerNode *node =
2056 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
2057 parent_node->GetChildren().Insert( (size_t) pos, node );
2058
2059 return node->GetItem();
e94d0c1e
RR
2060}
2061
8ddda15b
RR
2062bool wxDataViewTreeStore::IsContainer( const wxDataViewItem& item ) const
2063{
2064 wxDataViewTreeStoreNode *node = FindNode( item );
2065 if (!node) return false;
2066
2067 return node->IsContainer();
2068}
2069
e94d0c1e
RR
2070wxDataViewItem wxDataViewTreeStore::GetNthChild( const wxDataViewItem& parent, unsigned int pos ) const
2071{
2072 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
2073 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6 2074
e94d0c1e
RR
2075 wxDataViewTreeStoreNodeList::compatibility_iterator node = parent_node->GetChildren().Item( pos );
2076 if (node)
d5c4a81f 2077 return wxDataViewItem(node->GetData());
b5ec7dd6 2078
e94d0c1e
RR
2079 return wxDataViewItem(0);
2080}
2081
2082int wxDataViewTreeStore::GetChildCount( const wxDataViewItem& parent ) const
2083{
2084 wxDataViewTreeStoreNode *node = FindNode( parent );
2085 if (!node) return -1;
b5ec7dd6 2086
e94d0c1e
RR
2087 if (!node->IsContainer())
2088 return 0;
b5ec7dd6 2089
e94d0c1e 2090 wxDataViewTreeStoreContainerNode *container_node = (wxDataViewTreeStoreContainerNode*) node;
b5ec7dd6 2091 return (int) container_node->GetChildren().GetCount();
e94d0c1e
RR
2092}
2093
2094void wxDataViewTreeStore::SetItemText( const wxDataViewItem& item, const wxString &text )
2095{
2096 wxDataViewTreeStoreNode *node = FindNode( item );
2097 if (!node) return;
b5ec7dd6 2098
e94d0c1e 2099 node->SetText( text );
e94d0c1e
RR
2100}
2101
2102wxString wxDataViewTreeStore::GetItemText( const wxDataViewItem& item ) const
2103{
2104 wxDataViewTreeStoreNode *node = FindNode( item );
2105 if (!node) return wxEmptyString;
b5ec7dd6 2106
e94d0c1e
RR
2107 return node->GetText();
2108}
2109
2110void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
2111{
2112 wxDataViewTreeStoreNode *node = FindNode( item );
2113 if (!node) return;
b5ec7dd6 2114
e94d0c1e 2115 node->SetIcon( icon );
e94d0c1e
RR
2116}
2117
2118const wxIcon &wxDataViewTreeStore::GetItemIcon( const wxDataViewItem& item ) const
2119{
2120 wxDataViewTreeStoreNode *node = FindNode( item );
2121 if (!node) return wxNullIcon;
b5ec7dd6 2122
e94d0c1e
RR
2123 return node->GetIcon();
2124}
2125
2126void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
2127{
2128 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
2129 if (!node) return;
b5ec7dd6 2130
e94d0c1e 2131 node->SetExpandedIcon( icon );
e94d0c1e
RR
2132}
2133
2134const wxIcon &wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem& item ) const
2135{
2136 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
2137 if (!node) return wxNullIcon;
b5ec7dd6 2138
e94d0c1e
RR
2139 return node->GetExpandedIcon();
2140}
2141
2142void wxDataViewTreeStore::SetItemData( const wxDataViewItem& item, wxClientData *data )
2143{
2144 wxDataViewTreeStoreNode *node = FindNode( item );
2145 if (!node) return;
b5ec7dd6 2146
e94d0c1e 2147 node->SetData( data );
e94d0c1e
RR
2148}
2149
2150wxClientData *wxDataViewTreeStore::GetItemData( const wxDataViewItem& item ) const
2151{
2152 wxDataViewTreeStoreNode *node = FindNode( item );
2153 if (!node) return NULL;
b5ec7dd6 2154
e94d0c1e
RR
2155 return node->GetData();
2156}
2157
2158void wxDataViewTreeStore::DeleteItem( const wxDataViewItem& item )
2159{
2160 if (!item.IsOk()) return;
2161
2162 wxDataViewItem parent_item = GetParent( item );
2163
2164 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent_item );
2165 if (!parent_node) return;
b5ec7dd6 2166
e94d0c1e
RR
2167 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
2168 if (!node) return;
b5ec7dd6 2169
e94d0c1e 2170 parent_node->GetChildren().DeleteObject( node );
e94d0c1e
RR
2171}
2172
2173void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem& item )
2174{
2175 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
2176 if (!node) return;
b5ec7dd6 2177
e94d0c1e 2178 node->GetChildren().clear();
e94d0c1e
RR
2179}
2180
2181void wxDataViewTreeStore::DeleteAllItems()
2182{
d5c4a81f 2183 DeleteChildren(wxDataViewItem(m_root));
e94d0c1e
RR
2184}
2185
b5ec7dd6
VZ
2186void
2187wxDataViewTreeStore::GetValue(wxVariant &variant,
2188 const wxDataViewItem &item,
2189 unsigned int WXUNUSED(col)) const
e94d0c1e
RR
2190{
2191 // if (col != 0) return;
b5ec7dd6 2192
e94d0c1e
RR
2193 wxDataViewTreeStoreNode *node = FindNode( item );
2194 if (!node) return;
b5ec7dd6 2195
a75124d0
RR
2196 wxIcon icon( node->GetIcon());
2197 if (node->IsContainer())
2198 {
2199 wxDataViewTreeStoreContainerNode *container = (wxDataViewTreeStoreContainerNode*) node;
2200 if (container->IsExpanded() && container->GetExpandedIcon().IsOk())
2201 icon = container->GetExpandedIcon();
2202 }
777f9cef 2203
a75124d0 2204 wxDataViewIconText data( node->GetText(), icon );
b5ec7dd6 2205
e94d0c1e
RR
2206 variant << data;
2207}
2208
b5ec7dd6
VZ
2209bool
2210wxDataViewTreeStore::SetValue(const wxVariant& variant,
2211 const wxDataViewItem& item,
2212 unsigned int WXUNUSED(col))
e94d0c1e
RR
2213{
2214 // if (col != 0) return false;
b5ec7dd6 2215
e94d0c1e
RR
2216 wxDataViewTreeStoreNode *node = FindNode( item );
2217 if (!node) return false;
b5ec7dd6 2218
e94d0c1e 2219 wxDataViewIconText data;
b5ec7dd6 2220
e94d0c1e 2221 data << variant;
b5ec7dd6 2222
e94d0c1e
RR
2223 node->SetText( data.GetText() );
2224 node->SetIcon( data.GetIcon() );
b5ec7dd6 2225
e94d0c1e
RR
2226 return true;
2227}
2228
2229wxDataViewItem wxDataViewTreeStore::GetParent( const wxDataViewItem &item ) const
2230{
2231 wxDataViewTreeStoreNode *node = FindNode( item );
2232 if (!node) return wxDataViewItem(0);
b5ec7dd6 2233
e94d0c1e
RR
2234 wxDataViewTreeStoreNode *parent = node->GetParent();
2235 if (!parent) return wxDataViewItem(0);
b5ec7dd6 2236
e94d0c1e
RR
2237 if (parent == m_root)
2238 return wxDataViewItem(0);
b5ec7dd6 2239
e94d0c1e
RR
2240 return parent->GetItem();
2241}
2242
e94d0c1e
RR
2243unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
2244{
2245 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
2246 if (!node) return 0;
2247
2248 wxDataViewTreeStoreNodeList::iterator iter;
2249 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
2250 {
2251 wxDataViewTreeStoreNode* child = *iter;
2252 children.Add( child->GetItem() );
2253 }
b5ec7dd6 2254
e94d0c1e
RR
2255 return node->GetChildren().GetCount();
2256}
2257
b5ec7dd6 2258int wxDataViewTreeStore::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
862de7b3 2259 unsigned int WXUNUSED(column), bool WXUNUSED(ascending) ) const
e94d0c1e
RR
2260{
2261 wxDataViewTreeStoreNode *node1 = FindNode( item1 );
2262 wxDataViewTreeStoreNode *node2 = FindNode( item2 );
b5ec7dd6 2263
e94d0c1e
RR
2264 if (!node1 || !node2)
2265 return 0;
b5ec7dd6
VZ
2266
2267 wxDataViewTreeStoreContainerNode* parent1 =
e94d0c1e 2268 (wxDataViewTreeStoreContainerNode*) node1->GetParent();
b5ec7dd6 2269 wxDataViewTreeStoreContainerNode* parent2 =
e94d0c1e 2270 (wxDataViewTreeStoreContainerNode*) node2->GetParent();
b5ec7dd6 2271
e94d0c1e
RR
2272 if (parent1 != parent2)
2273 {
2274 wxLogError( wxT("Comparing items with different parent.") );
2275 return 0;
2276 }
b5ec7dd6 2277
9c205231 2278 if (node1->IsContainer() && !node2->IsContainer())
e94d0c1e 2279 return -1;
b5ec7dd6 2280
9c205231
RR
2281 if (node2->IsContainer() && !node1->IsContainer())
2282 return 1;
2283
2284 return parent1->GetChildren().IndexOf( node1 ) - parent2->GetChildren().IndexOf( node2 );
e94d0c1e
RR
2285}
2286
2287wxDataViewTreeStoreNode *wxDataViewTreeStore::FindNode( const wxDataViewItem &item ) const
2288{
2289 if (!item.IsOk())
2290 return m_root;
b5ec7dd6 2291
e94d0c1e
RR
2292 return (wxDataViewTreeStoreNode*) item.GetID();
2293}
2294
2295wxDataViewTreeStoreContainerNode *wxDataViewTreeStore::FindContainerNode( const wxDataViewItem &item ) const
2296{
2297 if (!item.IsOk())
2298 return (wxDataViewTreeStoreContainerNode*) m_root;
2299
2300 wxDataViewTreeStoreNode* node = (wxDataViewTreeStoreNode*) item.GetID();
b5ec7dd6 2301
e94d0c1e
RR
2302 if (!node->IsContainer())
2303 return NULL;
b5ec7dd6 2304
e94d0c1e
RR
2305 return (wxDataViewTreeStoreContainerNode*) node;
2306}
2307
a75124d0
RR
2308//-----------------------------------------------------------------------------
2309// wxDataViewTreeCtrl
2310//-----------------------------------------------------------------------------
2311
2312IMPLEMENT_DYNAMIC_CLASS(wxDataViewTreeCtrl,wxDataViewCtrl)
2313
2314BEGIN_EVENT_TABLE(wxDataViewTreeCtrl,wxDataViewCtrl)
2315 EVT_DATAVIEW_ITEM_EXPANDED(-1, wxDataViewTreeCtrl::OnExpanded)
2316 EVT_DATAVIEW_ITEM_COLLAPSED(-1, wxDataViewTreeCtrl::OnCollapsed)
9c150d5f 2317 EVT_SIZE( wxDataViewTreeCtrl::OnSize )
a75124d0
RR
2318END_EVENT_TABLE()
2319
43c64cc6 2320bool wxDataViewTreeCtrl::Create( wxWindow *parent, wxWindowID id,
777f9cef 2321 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
a75124d0 2322{
43c64cc6
VZ
2323 if ( !wxDataViewCtrl::Create( parent, id, pos, size, style, validator ) )
2324 return false;
777f9cef 2325
43c64cc6 2326 // create the standard model and a column in the tree
a75124d0
RR
2327 wxDataViewTreeStore *store = new wxDataViewTreeStore;
2328 AssociateModel( store );
2329 store->DecRef();
777f9cef 2330
0e17930f
VZ
2331 AppendIconTextColumn
2332 (
2333 wxString(), // no label (header is not shown anyhow)
2334 0, // the only model column
2335 wxDATAVIEW_CELL_EDITABLE,
2336 -1, // default width
2337 wxALIGN_NOT, // and alignment
d13b34d3 2338 0 // not resizable
0e17930f 2339 );
a75124d0 2340
43c64cc6 2341 return true;
a75124d0
RR
2342}
2343
a75124d0
RR
2344wxDataViewItem wxDataViewTreeCtrl::AppendItem( const wxDataViewItem& parent,
2345 const wxString &text, int iconIndex, wxClientData *data )
2346{
abfdefed
VZ
2347 wxDataViewItem res = GetStore()->
2348 AppendItem( parent, text, GetImage(iconIndex), data );
8c2654ce 2349
e700e296 2350 GetStore()->ItemAdded( parent, res );
8c2654ce 2351
e700e296 2352 return res;
a75124d0
RR
2353}
2354
2355wxDataViewItem wxDataViewTreeCtrl::PrependItem( const wxDataViewItem& parent,
2356 const wxString &text, int iconIndex, wxClientData *data )
2357{
abfdefed
VZ
2358 wxDataViewItem res = GetStore()->
2359 PrependItem( parent, text, GetImage(iconIndex), data );
8c2654ce 2360
e700e296 2361 GetStore()->ItemAdded( parent, res );
8c2654ce 2362
e700e296 2363 return res;
a75124d0
RR
2364}
2365
2366wxDataViewItem wxDataViewTreeCtrl::InsertItem( const wxDataViewItem& parent, const wxDataViewItem& previous,
2367 const wxString &text, int iconIndex, wxClientData *data )
2368{
abfdefed
VZ
2369 wxDataViewItem res = GetStore()->
2370 InsertItem( parent, previous, text, GetImage(iconIndex), data );
8c2654ce 2371
e700e296 2372 GetStore()->ItemAdded( parent, res );
8c2654ce 2373
e700e296 2374 return res;
a75124d0
RR
2375}
2376
2377wxDataViewItem wxDataViewTreeCtrl::PrependContainer( const wxDataViewItem& parent,
2378 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2379{
abfdefed
VZ
2380 wxDataViewItem res = GetStore()->
2381 PrependContainer( parent, text,
2382 GetImage(iconIndex), GetImage(expandedIndex), data );
8c2654ce 2383
e700e296 2384 GetStore()->ItemAdded( parent, res );
8c2654ce 2385
e700e296 2386 return res;
a75124d0
RR
2387}
2388
2389wxDataViewItem wxDataViewTreeCtrl::AppendContainer( const wxDataViewItem& parent,
2390 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2391{
abfdefed
VZ
2392 wxDataViewItem res = GetStore()->
2393 AppendContainer( parent, text,
2394 GetImage(iconIndex), GetImage(expandedIndex), data );
8c2654ce 2395
e700e296 2396 GetStore()->ItemAdded( parent, res );
8c2654ce 2397
e700e296 2398 return res;
a75124d0
RR
2399}
2400
2401wxDataViewItem wxDataViewTreeCtrl::InsertContainer( const wxDataViewItem& parent, const wxDataViewItem& previous,
2402 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2403{
abfdefed
VZ
2404 wxDataViewItem res = GetStore()->
2405 InsertContainer( parent, previous, text,
2406 GetImage(iconIndex), GetImage(expandedIndex), data );
8c2654ce 2407
e700e296 2408 GetStore()->ItemAdded( parent, res );
8c2654ce 2409
e700e296
RR
2410 return res;
2411}
2412
2413void wxDataViewTreeCtrl::SetItemText( const wxDataViewItem& item, const wxString &text )
3569e0d0 2414{
e700e296 2415 GetStore()->SetItemText(item,text);
8c2654ce 2416
e700e296
RR
2417 // notify control
2418 GetStore()->ValueChanged( item, 0 );
2419}
2420
2421void wxDataViewTreeCtrl::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
3569e0d0 2422{
e700e296 2423 GetStore()->SetItemIcon(item,icon);
8c2654ce 2424
e700e296
RR
2425 // notify control
2426 GetStore()->ValueChanged( item, 0 );
2427}
2428
2429void wxDataViewTreeCtrl::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
3569e0d0 2430{
e700e296 2431 GetStore()->SetItemExpandedIcon(item,icon);
8c2654ce 2432
e700e296
RR
2433 // notify control
2434 GetStore()->ValueChanged( item, 0 );
2435}
2436
2437void wxDataViewTreeCtrl::DeleteItem( const wxDataViewItem& item )
3569e0d0 2438{
e700e296
RR
2439 wxDataViewItem parent_item = GetStore()->GetParent( item );
2440
2441 GetStore()->DeleteItem(item);
8c2654ce 2442
e700e296
RR
2443 // notify control
2444 GetStore()->ItemDeleted( parent_item, item );
2445}
2446
2447void wxDataViewTreeCtrl::DeleteChildren( const wxDataViewItem& item )
3569e0d0 2448{
2f21b2be
RR
2449 wxDataViewTreeStoreContainerNode *node = GetStore()->FindContainerNode( item );
2450 if (!node) return;
2451
2452 wxDataViewItemArray array;
2453 wxDataViewTreeStoreNodeList::iterator iter;
2454 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
2455 {
2456 wxDataViewTreeStoreNode* child = *iter;
2457 array.Add( child->GetItem() );
2458 }
2459
2460 GetStore()->DeleteChildren( item );
2461
2462 // notify control
2463 GetStore()->ItemsDeleted( item, array );
e700e296
RR
2464}
2465
2466void wxDataViewTreeCtrl::DeleteAllItems()
3569e0d0 2467{
e700e296 2468 GetStore()->DeleteAllItems();
8c2654ce 2469
e700e296 2470 GetStore()->Cleared();
a75124d0
RR
2471}
2472
a75124d0
RR
2473void wxDataViewTreeCtrl::OnExpanded( wxDataViewEvent &event )
2474{
abfdefed 2475 if (HasImageList()) return;
777f9cef 2476
a75124d0
RR
2477 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
2478 if (!container) return;
777f9cef 2479
a75124d0 2480 container->SetExpanded( true );
8c2654ce 2481
a75124d0
RR
2482 GetStore()->ItemChanged( event.GetItem() );
2483}
2484
2485void wxDataViewTreeCtrl::OnCollapsed( wxDataViewEvent &event )
2486{
abfdefed 2487 if (HasImageList()) return;
777f9cef 2488
a75124d0
RR
2489 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
2490 if (!container) return;
777f9cef 2491
a75124d0 2492 container->SetExpanded( false );
8c2654ce 2493
a75124d0
RR
2494 GetStore()->ItemChanged( event.GetItem() );
2495}
2496
a6aaf49f 2497void wxDataViewTreeCtrl::OnSize( wxSizeEvent &event )
1a07a730 2498{
ad386793 2499#if defined(wxUSE_GENERICDATAVIEWCTRL)
4bfd0ed5
VZ
2500 // automatically resize our only column to take the entire control width
2501 if ( GetColumnCount() )
2502 {
2503 wxSize size = GetClientSize();
2504 GetColumn(0)->SetWidth(size.x);
2505 }
1a07a730 2506#endif
a6aaf49f 2507 event.Skip( true );
1a07a730 2508}
a75124d0 2509
b5ec7dd6 2510#endif // wxUSE_DATAVIEWCTRL
e0000f94 2511