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