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