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