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