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