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