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