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