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