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