]> git.saurik.com Git - wxWidgets.git/blob - src/common/datavcmn.cpp
implement click events in wxHeaderCtrl
[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 void wxDataViewColumnBase::Init(wxDataViewRenderer *renderer,
836 unsigned int model_column)
837 {
838 m_renderer = renderer;
839 m_model_column = model_column;
840 m_owner = NULL;
841 m_renderer->SetOwner( (wxDataViewColumn*) this );
842 }
843
844 wxDataViewColumnBase::~wxDataViewColumnBase()
845 {
846 delete m_renderer;
847 }
848
849 // ---------------------------------------------------------
850 // wxDataViewCtrlBase
851 // ---------------------------------------------------------
852
853 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl)
854
855 wxDataViewCtrlBase::wxDataViewCtrlBase()
856 {
857 m_model = NULL;
858 m_expander_column = 0;
859 m_indent = 8;
860 }
861
862 wxDataViewCtrlBase::~wxDataViewCtrlBase()
863 {
864 if (m_model)
865 {
866 m_model->DecRef();
867 m_model = NULL;
868 }
869 }
870
871 bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel *model )
872 {
873 if (m_model)
874 {
875 m_model->DecRef(); // discard old model, if any
876 }
877
878 // add our own reference to the new model:
879 m_model = model;
880 if (m_model)
881 {
882 m_model->IncRef();
883 }
884
885 return true;
886 }
887
888 wxDataViewModel* wxDataViewCtrlBase::GetModel()
889 {
890 return m_model;
891 }
892
893 const wxDataViewModel* wxDataViewCtrlBase::GetModel() const
894 {
895 return m_model;
896 }
897
898 wxDataViewColumn *
899 wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column,
900 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
901 {
902 wxDataViewColumn *ret = new wxDataViewColumn( label,
903 new wxDataViewTextRenderer( wxT("string"), mode ),
904 model_column, width, align, flags );
905 AppendColumn( ret );
906 return ret;
907 }
908
909 wxDataViewColumn *
910 wxDataViewCtrlBase::AppendIconTextColumn( const wxString &label, unsigned int model_column,
911 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
912 {
913 wxDataViewColumn *ret = new wxDataViewColumn( label,
914 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
915 model_column, width, align, flags );
916 AppendColumn( ret );
917 return ret;
918 }
919
920 wxDataViewColumn *
921 wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column,
922 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
923 {
924
925 wxDataViewColumn *ret = new wxDataViewColumn( label,
926 new wxDataViewToggleRenderer( wxT("bool"), mode ),
927 model_column, width, align, flags );
928 AppendColumn( ret );
929 return ret;
930 }
931
932 wxDataViewColumn *
933 wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column,
934 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
935 {
936 wxDataViewColumn *ret = new wxDataViewColumn( label,
937 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
938 model_column, width, align, flags );
939 AppendColumn( ret );
940 return ret;
941 }
942
943 wxDataViewColumn *
944 wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column,
945 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
946 {
947 wxDataViewColumn *ret = new wxDataViewColumn( label,
948 new wxDataViewDateRenderer( wxT("datetime"), mode ),
949 model_column, width, align, flags );
950 AppendColumn( ret );
951 return ret;
952 }
953
954 wxDataViewColumn *
955 wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column,
956 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
957 {
958 wxDataViewColumn *ret = new wxDataViewColumn( label,
959 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
960 model_column, width, align, flags );
961 AppendColumn( ret );
962 return ret;
963 }
964
965 wxDataViewColumn *
966 wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column,
967 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
968 {
969 wxDataViewColumn *ret = new wxDataViewColumn( label,
970 new wxDataViewTextRenderer( wxT("string"), mode ),
971 model_column, width, align, flags );
972 AppendColumn( ret );
973 return ret;
974 }
975
976 wxDataViewColumn *
977 wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap &label, unsigned int model_column,
978 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
979 {
980 wxDataViewColumn *ret = new wxDataViewColumn( label,
981 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
982 model_column, width, align, flags );
983 AppendColumn( ret );
984 return ret;
985 }
986
987 wxDataViewColumn *
988 wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column,
989 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
990 {
991 wxDataViewColumn *ret = new wxDataViewColumn( label,
992 new wxDataViewToggleRenderer( wxT("bool"), mode ),
993 model_column, width, align, flags );
994 AppendColumn( ret );
995 return ret;
996 }
997
998 wxDataViewColumn *
999 wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column,
1000 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1001 {
1002 wxDataViewColumn *ret = new wxDataViewColumn( label,
1003 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1004 model_column, width, align, flags );
1005 AppendColumn( ret );
1006 return ret;
1007 }
1008
1009 wxDataViewColumn *
1010 wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column,
1011 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1012 {
1013 wxDataViewColumn *ret = new wxDataViewColumn( label,
1014 new wxDataViewDateRenderer( wxT("datetime"), mode ),
1015 model_column, width, align, flags );
1016 AppendColumn( ret );
1017 return ret;
1018 }
1019
1020 wxDataViewColumn *
1021 wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column,
1022 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1023 {
1024 wxDataViewColumn *ret = new wxDataViewColumn( label,
1025 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
1026 model_column, width, align, flags );
1027 AppendColumn( ret );
1028 return ret;
1029 }
1030
1031 wxDataViewColumn *
1032 wxDataViewCtrlBase::PrependTextColumn( const wxString &label, unsigned int model_column,
1033 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1034 {
1035 wxDataViewColumn *ret = new wxDataViewColumn( label,
1036 new wxDataViewTextRenderer( wxT("string"), mode ),
1037 model_column, width, align, flags );
1038 PrependColumn( ret );
1039 return ret;
1040 }
1041
1042 wxDataViewColumn *
1043 wxDataViewCtrlBase::PrependIconTextColumn( const wxString &label, unsigned int model_column,
1044 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1045 {
1046 wxDataViewColumn *ret = new wxDataViewColumn( label,
1047 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1048 model_column, width, align, flags );
1049 PrependColumn( ret );
1050 return ret;
1051 }
1052
1053 wxDataViewColumn *
1054 wxDataViewCtrlBase::PrependToggleColumn( const wxString &label, unsigned int model_column,
1055 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1056 {
1057
1058 wxDataViewColumn *ret = new wxDataViewColumn( label,
1059 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1060 model_column, width, align, flags );
1061 PrependColumn( ret );
1062 return ret;
1063 }
1064
1065 wxDataViewColumn *
1066 wxDataViewCtrlBase::PrependProgressColumn( const wxString &label, unsigned int model_column,
1067 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1068 {
1069 wxDataViewColumn *ret = new wxDataViewColumn( label,
1070 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1071 model_column, width, align, flags );
1072 PrependColumn( ret );
1073 return ret;
1074 }
1075
1076 wxDataViewColumn *
1077 wxDataViewCtrlBase::PrependDateColumn( const wxString &label, unsigned int model_column,
1078 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1079 {
1080 wxDataViewColumn *ret = new wxDataViewColumn( label,
1081 new wxDataViewDateRenderer( wxT("datetime"), mode ),
1082 model_column, width, align, flags );
1083 PrependColumn( ret );
1084 return ret;
1085 }
1086
1087 wxDataViewColumn *
1088 wxDataViewCtrlBase::PrependBitmapColumn( const wxString &label, unsigned int model_column,
1089 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1090 {
1091 wxDataViewColumn *ret = new wxDataViewColumn( label,
1092 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
1093 model_column, width, align, flags );
1094 PrependColumn( ret );
1095 return ret;
1096 }
1097
1098 wxDataViewColumn *
1099 wxDataViewCtrlBase::PrependTextColumn( const wxBitmap &label, unsigned int model_column,
1100 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1101 {
1102 wxDataViewColumn *ret = new wxDataViewColumn( label,
1103 new wxDataViewTextRenderer( wxT("string"), mode ),
1104 model_column, width, align, flags );
1105 PrependColumn( ret );
1106 return ret;
1107 }
1108
1109 wxDataViewColumn *
1110 wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap &label, unsigned int model_column,
1111 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1112 {
1113 wxDataViewColumn *ret = new wxDataViewColumn( label,
1114 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1115 model_column, width, align, flags );
1116 PrependColumn( ret );
1117 return ret;
1118 }
1119
1120 wxDataViewColumn *
1121 wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap &label, unsigned int model_column,
1122 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1123 {
1124 wxDataViewColumn *ret = new wxDataViewColumn( label,
1125 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1126 model_column, width, align, flags );
1127 PrependColumn( ret );
1128 return ret;
1129 }
1130
1131 wxDataViewColumn *
1132 wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap &label, unsigned int model_column,
1133 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1134 {
1135 wxDataViewColumn *ret = new wxDataViewColumn( label,
1136 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1137 model_column, width, align, flags );
1138 PrependColumn( ret );
1139 return ret;
1140 }
1141
1142 wxDataViewColumn *
1143 wxDataViewCtrlBase::PrependDateColumn( const wxBitmap &label, unsigned int model_column,
1144 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1145 {
1146 wxDataViewColumn *ret = new wxDataViewColumn( label,
1147 new wxDataViewDateRenderer( wxT("datetime"), mode ),
1148 model_column, width, align, flags );
1149 PrependColumn( ret );
1150 return ret;
1151 }
1152
1153 wxDataViewColumn *
1154 wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap &label, unsigned int model_column,
1155 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1156 {
1157 wxDataViewColumn *ret = new wxDataViewColumn( label,
1158 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
1159 model_column, width, align, flags );
1160 PrependColumn( ret );
1161 return ret;
1162 }
1163
1164 bool
1165 wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
1166 {
1167 col->SetOwner( (wxDataViewCtrl*) this );
1168 return true;
1169 }
1170
1171 bool
1172 wxDataViewCtrlBase::PrependColumn( wxDataViewColumn *col )
1173 {
1174 col->SetOwner( (wxDataViewCtrl*) this );
1175 return true;
1176 }
1177
1178 bool
1179 wxDataViewCtrlBase::InsertColumn( unsigned int WXUNUSED(pos), wxDataViewColumn *col )
1180 {
1181 col->SetOwner( (wxDataViewCtrl*) this );
1182 return true;
1183 }
1184
1185 // ---------------------------------------------------------
1186 // wxDataViewEvent
1187 // ---------------------------------------------------------
1188
1189 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent)
1190
1191 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED)
1192
1193 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED)
1194 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING)
1195 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED)
1196 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING)
1197 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED)
1198 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED)
1199 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE)
1200 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED)
1201
1202 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU)
1203
1204 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK)
1205 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK)
1206 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED)
1207 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_REORDERED)
1208
1209
1210 // -------------------------------------
1211 // wxDataViewSpinRenderer
1212 // -------------------------------------
1213
1214 wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min, int max, wxDataViewCellMode mode, int alignment ) :
1215 wxDataViewCustomRenderer(wxT("long"), mode, alignment )
1216 {
1217 m_min = min;
1218 m_max = max;
1219 }
1220
1221 wxControl* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
1222 {
1223 long l = value;
1224 wxSize size = labelRect.GetSize();
1225 #ifdef __WXMAC__
1226 size = wxSize( wxMax(70,labelRect.width ), -1 );
1227 #endif
1228 wxString str;
1229 str.Printf( wxT("%d"), (int) l );
1230 wxSpinCtrl *sc = new wxSpinCtrl( parent, wxID_ANY, str,
1231 labelRect.GetTopLeft(), size, wxSP_ARROW_KEYS|wxTE_PROCESS_ENTER, m_min, m_max, l );
1232 #ifdef __WXMAC__
1233 size = sc->GetSize();
1234 wxPoint pt = sc->GetPosition();
1235 sc->SetSize( pt.x - 4, pt.y - 4, size.x, size.y );
1236 #endif
1237
1238 return sc;
1239 }
1240
1241 bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
1242 {
1243 wxSpinCtrl *sc = (wxSpinCtrl*) editor;
1244 long l = sc->GetValue();
1245 value = l;
1246 return true;
1247 }
1248
1249 bool wxDataViewSpinRenderer::Render( wxRect rect, wxDC *dc, int state )
1250 {
1251 wxString str;
1252 str.Printf(wxT("%d"), (int) m_data );
1253 RenderText( str, 0, rect, dc, state );
1254 return true;
1255 }
1256
1257 wxSize wxDataViewSpinRenderer::GetSize() const
1258 {
1259 return wxSize(80,16);
1260 }
1261
1262 bool wxDataViewSpinRenderer::SetValue( const wxVariant &value )
1263 {
1264 m_data = value.GetLong();
1265 return true;
1266 }
1267
1268 bool wxDataViewSpinRenderer::GetValue( wxVariant &value ) const
1269 {
1270 value = m_data;
1271 return true;
1272 }
1273
1274 // -------------------------------------
1275 // wxDataViewChoiceRenderer
1276 // -------------------------------------
1277
1278 #ifndef __WXGTK20__
1279
1280 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString& choices, wxDataViewCellMode mode, int alignment ) :
1281 wxDataViewCustomRenderer(wxT("string"), mode, alignment )
1282 {
1283 m_choices = choices;
1284 }
1285
1286 wxControl* wxDataViewChoiceRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
1287 {
1288 wxString s = value;
1289 wxSize size = labelRect.GetSize();
1290 #ifdef __WXMAC__
1291 size = wxSize( wxMax(70,labelRect.width ), -1 );
1292 #endif
1293 wxChoice *c = new wxChoice( parent, wxID_ANY, labelRect.GetTopLeft(), size, m_choices );
1294 c->SetStringSelection( value.GetString() );
1295
1296 return c;
1297 }
1298
1299 bool wxDataViewChoiceRenderer::GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
1300 {
1301 wxChoice *c = (wxChoice*) editor;
1302 wxString s = c->GetStringSelection();
1303 value = s;
1304 return true;
1305 }
1306
1307 bool wxDataViewChoiceRenderer::Render( wxRect rect, wxDC *dc, int state )
1308 {
1309 RenderText( m_data, 0, rect, dc, state );
1310 return true;
1311 }
1312
1313 wxSize wxDataViewChoiceRenderer::GetSize() const
1314 {
1315 return wxSize(80,16);
1316 }
1317
1318 bool wxDataViewChoiceRenderer::SetValue( const wxVariant &value )
1319 {
1320 m_data = value.GetString();
1321 return true;
1322 }
1323
1324 bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const
1325 {
1326 value = m_data;
1327 return true;
1328 }
1329
1330 #endif
1331
1332 //-----------------------------------------------------------------------------
1333 // wxDataViewTreeStore
1334 //-----------------------------------------------------------------------------
1335
1336 wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1337 wxDataViewTreeStoreNode *parent,
1338 const wxString &text, const wxIcon &icon, wxClientData *data )
1339 {
1340 m_parent = parent;
1341 m_text = text;
1342 m_icon = icon;
1343 m_data = data;
1344 }
1345
1346 wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1347 {
1348 if (m_data)
1349 delete m_data;
1350 }
1351
1352 #include "wx/listimpl.cpp"
1353 WX_DEFINE_LIST(wxDataViewTreeStoreNodeList)
1354
1355 wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1356 wxDataViewTreeStoreNode *parent, const wxString &text,
1357 const wxIcon &icon, const wxIcon &expanded, wxClientData *data ) :
1358 wxDataViewTreeStoreNode( parent, text, icon, data )
1359 {
1360 m_iconExpanded = expanded;
1361 m_isExpanded = false;
1362 m_children.DeleteContents(true);
1363 }
1364
1365 wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1366 {
1367 }
1368
1369 //-----------------------------------------------------------------------------
1370
1371 wxDataViewTreeStore::wxDataViewTreeStore()
1372 {
1373 m_root = new wxDataViewTreeStoreContainerNode( NULL, wxEmptyString );
1374 }
1375
1376 wxDataViewTreeStore::~wxDataViewTreeStore()
1377 {
1378 delete m_root;
1379 }
1380
1381 wxDataViewItem wxDataViewTreeStore::AppendItem( const wxDataViewItem& parent,
1382 const wxString &text, const wxIcon &icon, wxClientData *data )
1383 {
1384 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1385 if (!parent_node) return wxDataViewItem(0);
1386
1387 wxDataViewTreeStoreNode *node =
1388 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1389 parent_node->GetChildren().Append( node );
1390
1391 // notify control
1392 ItemAdded( parent, node->GetItem() );
1393
1394 return node->GetItem();
1395 }
1396
1397 wxDataViewItem wxDataViewTreeStore::PrependItem( const wxDataViewItem& parent,
1398 const wxString &text, const wxIcon &icon, wxClientData *data )
1399 {
1400 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1401 if (!parent_node) return wxDataViewItem(0);
1402
1403 wxDataViewTreeStoreNode *node =
1404 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1405 parent_node->GetChildren().Insert( node );
1406
1407 // notify control
1408 ItemAdded( parent, node->GetItem() );
1409
1410 return node->GetItem();
1411 }
1412
1413 wxDataViewItem
1414 wxDataViewTreeStore::InsertItem(const wxDataViewItem& WXUNUSED(parent),
1415 const wxDataViewItem& WXUNUSED(previous),
1416 const wxString& WXUNUSED(text),
1417 const wxIcon& WXUNUSED(icon),
1418 wxClientData * WXUNUSED(data))
1419 {
1420 return wxDataViewItem(0);
1421 }
1422
1423 wxDataViewItem wxDataViewTreeStore::PrependContainer( const wxDataViewItem& parent,
1424 const wxString &text, const wxIcon &icon, const wxIcon &expanded,
1425 wxClientData *data )
1426 {
1427 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1428 if (!parent_node) return wxDataViewItem(0);
1429
1430 wxDataViewTreeStoreContainerNode *node =
1431 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1432 parent_node->GetChildren().Insert( node );
1433
1434 // notify control
1435 ItemAdded( parent, node->GetItem() );
1436
1437 return node->GetItem();
1438 }
1439
1440 wxDataViewItem
1441 wxDataViewTreeStore::AppendContainer(const wxDataViewItem& parent,
1442 const wxString &text,
1443 const wxIcon& icon,
1444 const wxIcon& expanded,
1445 wxClientData * data)
1446 {
1447 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1448 if (!parent_node) return wxDataViewItem(0);
1449
1450 wxDataViewTreeStoreContainerNode *node =
1451 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1452 parent_node->GetChildren().Append( node );
1453
1454 // notify control
1455 ItemAdded( parent, node->GetItem() );
1456
1457 return node->GetItem();
1458 }
1459
1460 wxDataViewItem
1461 wxDataViewTreeStore::InsertContainer(const wxDataViewItem& WXUNUSED(parent),
1462 const wxDataViewItem& WXUNUSED(previous),
1463 const wxString& WXUNUSED(text),
1464 const wxIcon& WXUNUSED(icon),
1465 const wxIcon& WXUNUSED(expanded),
1466 wxClientData * WXUNUSED(data))
1467 {
1468 return wxDataViewItem(0);
1469 }
1470
1471 wxDataViewItem wxDataViewTreeStore::GetNthChild( const wxDataViewItem& parent, unsigned int pos ) const
1472 {
1473 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1474 if (!parent_node) return wxDataViewItem(0);
1475
1476 wxDataViewTreeStoreNodeList::compatibility_iterator node = parent_node->GetChildren().Item( pos );
1477 if (node)
1478 return node->GetData();
1479
1480 return wxDataViewItem(0);
1481 }
1482
1483 int wxDataViewTreeStore::GetChildCount( const wxDataViewItem& parent ) const
1484 {
1485 wxDataViewTreeStoreNode *node = FindNode( parent );
1486 if (!node) return -1;
1487
1488 if (!node->IsContainer())
1489 return 0;
1490
1491 wxDataViewTreeStoreContainerNode *container_node = (wxDataViewTreeStoreContainerNode*) node;
1492 return (int) container_node->GetChildren().GetCount();
1493 }
1494
1495 void wxDataViewTreeStore::SetItemText( const wxDataViewItem& item, const wxString &text )
1496 {
1497 wxDataViewTreeStoreNode *node = FindNode( item );
1498 if (!node) return;
1499
1500 node->SetText( text );
1501
1502 // notify control
1503 ValueChanged( item, 0 );
1504 }
1505
1506 wxString wxDataViewTreeStore::GetItemText( const wxDataViewItem& item ) const
1507 {
1508 wxDataViewTreeStoreNode *node = FindNode( item );
1509 if (!node) return wxEmptyString;
1510
1511 return node->GetText();
1512 }
1513
1514 void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
1515 {
1516 wxDataViewTreeStoreNode *node = FindNode( item );
1517 if (!node) return;
1518
1519 node->SetIcon( icon );
1520
1521 // notify control
1522 ValueChanged( item, 0 );
1523 }
1524
1525 const wxIcon &wxDataViewTreeStore::GetItemIcon( const wxDataViewItem& item ) const
1526 {
1527 wxDataViewTreeStoreNode *node = FindNode( item );
1528 if (!node) return wxNullIcon;
1529
1530 return node->GetIcon();
1531 }
1532
1533 void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
1534 {
1535 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1536 if (!node) return;
1537
1538 node->SetExpandedIcon( icon );
1539
1540 // notify control
1541 ValueChanged( item, 0 );
1542 }
1543
1544 const wxIcon &wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem& item ) const
1545 {
1546 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1547 if (!node) return wxNullIcon;
1548
1549 return node->GetExpandedIcon();
1550 }
1551
1552 void wxDataViewTreeStore::SetItemData( const wxDataViewItem& item, wxClientData *data )
1553 {
1554 wxDataViewTreeStoreNode *node = FindNode( item );
1555 if (!node) return;
1556
1557 node->SetData( data );
1558
1559 // notify control? only sensible when sorting on client data
1560 // ValueChanged( item, 0 );
1561 }
1562
1563 wxClientData *wxDataViewTreeStore::GetItemData( const wxDataViewItem& item ) const
1564 {
1565 wxDataViewTreeStoreNode *node = FindNode( item );
1566 if (!node) return NULL;
1567
1568 return node->GetData();
1569 }
1570
1571 void wxDataViewTreeStore::DeleteItem( const wxDataViewItem& item )
1572 {
1573 if (!item.IsOk()) return;
1574
1575 wxDataViewItem parent_item = GetParent( item );
1576
1577 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent_item );
1578 if (!parent_node) return;
1579
1580 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1581 if (!node) return;
1582
1583 parent_node->GetChildren().DeleteObject( node );
1584
1585 // notify control
1586 ItemDeleted( parent_item, item );
1587 }
1588
1589 void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem& item )
1590 {
1591 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1592 if (!node) return;
1593
1594 wxDataViewItemArray array;
1595 wxDataViewTreeStoreNodeList::iterator iter;
1596 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
1597 {
1598 wxDataViewTreeStoreNode* child = *iter;
1599 array.Add( child->GetItem() );
1600 }
1601
1602 node->GetChildren().clear();
1603
1604 // notify control
1605 ItemsDeleted( item, array );
1606 }
1607
1608 void wxDataViewTreeStore::DeleteAllItems()
1609 {
1610 // TODO
1611 }
1612
1613 void
1614 wxDataViewTreeStore::GetValue(wxVariant &variant,
1615 const wxDataViewItem &item,
1616 unsigned int WXUNUSED(col)) const
1617 {
1618 // if (col != 0) return;
1619
1620 wxDataViewTreeStoreNode *node = FindNode( item );
1621 if (!node) return;
1622
1623 wxIcon icon( node->GetIcon());
1624 if (node->IsContainer())
1625 {
1626 wxDataViewTreeStoreContainerNode *container = (wxDataViewTreeStoreContainerNode*) node;
1627 if (container->IsExpanded() && container->GetExpandedIcon().IsOk())
1628 icon = container->GetExpandedIcon();
1629 }
1630
1631 wxDataViewIconText data( node->GetText(), icon );
1632
1633 variant << data;
1634 }
1635
1636 bool
1637 wxDataViewTreeStore::SetValue(const wxVariant& variant,
1638 const wxDataViewItem& item,
1639 unsigned int WXUNUSED(col))
1640 {
1641 // if (col != 0) return false;
1642
1643 wxDataViewTreeStoreNode *node = FindNode( item );
1644 if (!node) return false;
1645
1646 wxDataViewIconText data;
1647
1648 data << variant;
1649
1650 node->SetText( data.GetText() );
1651 node->SetIcon( data.GetIcon() );
1652
1653 return true;
1654 }
1655
1656 wxDataViewItem wxDataViewTreeStore::GetParent( const wxDataViewItem &item ) const
1657 {
1658 wxDataViewTreeStoreNode *node = FindNode( item );
1659 if (!node) return wxDataViewItem(0);
1660
1661 wxDataViewTreeStoreNode *parent = node->GetParent();
1662 if (!parent) return wxDataViewItem(0);
1663
1664 if (parent == m_root)
1665 return wxDataViewItem(0);
1666
1667 return parent->GetItem();
1668 }
1669
1670 bool wxDataViewTreeStore::IsContainer( const wxDataViewItem &item ) const
1671 {
1672 wxDataViewTreeStoreNode *node = FindNode( item );
1673 if (!node) return false;
1674
1675 return node->IsContainer();
1676 }
1677
1678 unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
1679 {
1680 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1681 if (!node) return 0;
1682
1683 wxDataViewTreeStoreNodeList::iterator iter;
1684 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
1685 {
1686 wxDataViewTreeStoreNode* child = *iter;
1687 children.Add( child->GetItem() );
1688 }
1689
1690 return node->GetChildren().GetCount();
1691 }
1692
1693 int wxDataViewTreeStore::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
1694 unsigned int WXUNUSED(column), bool WXUNUSED(ascending) )
1695 {
1696 wxDataViewTreeStoreNode *node1 = FindNode( item1 );
1697 wxDataViewTreeStoreNode *node2 = FindNode( item2 );
1698
1699 if (!node1 || !node2)
1700 return 0;
1701
1702 wxDataViewTreeStoreContainerNode* parent1 =
1703 (wxDataViewTreeStoreContainerNode*) node1->GetParent();
1704 wxDataViewTreeStoreContainerNode* parent2 =
1705 (wxDataViewTreeStoreContainerNode*) node2->GetParent();
1706
1707 if (parent1 != parent2)
1708 {
1709 wxLogError( wxT("Comparing items with different parent.") );
1710 return 0;
1711 }
1712
1713 if (node1->IsContainer() && !!node2->IsContainer())
1714 return 1;
1715
1716 if (node2->IsContainer() && !!node1->IsContainer())
1717 return -1;
1718
1719 return parent1->GetChildren().IndexOf( node1 ) - parent1->GetChildren().IndexOf( node2 );
1720 }
1721
1722 wxDataViewTreeStoreNode *wxDataViewTreeStore::FindNode( const wxDataViewItem &item ) const
1723 {
1724 if (!item.IsOk())
1725 return m_root;
1726
1727 return (wxDataViewTreeStoreNode*) item.GetID();
1728 }
1729
1730 wxDataViewTreeStoreContainerNode *wxDataViewTreeStore::FindContainerNode( const wxDataViewItem &item ) const
1731 {
1732 if (!item.IsOk())
1733 return (wxDataViewTreeStoreContainerNode*) m_root;
1734
1735 wxDataViewTreeStoreNode* node = (wxDataViewTreeStoreNode*) item.GetID();
1736
1737 if (!node->IsContainer())
1738 return NULL;
1739
1740 return (wxDataViewTreeStoreContainerNode*) node;
1741 }
1742
1743 //-----------------------------------------------------------------------------
1744 // wxDataViewTreeCtrl
1745 //-----------------------------------------------------------------------------
1746
1747 IMPLEMENT_DYNAMIC_CLASS(wxDataViewTreeCtrl,wxDataViewCtrl)
1748
1749 BEGIN_EVENT_TABLE(wxDataViewTreeCtrl,wxDataViewCtrl)
1750 EVT_DATAVIEW_ITEM_EXPANDED(-1, wxDataViewTreeCtrl::OnExpanded)
1751 EVT_DATAVIEW_ITEM_COLLAPSED(-1, wxDataViewTreeCtrl::OnCollapsed)
1752 EVT_SIZE( wxDataViewTreeCtrl::OnSize )
1753 END_EVENT_TABLE()
1754
1755 wxDataViewTreeCtrl::wxDataViewTreeCtrl()
1756 {
1757 m_imageList = NULL;
1758 }
1759
1760 wxDataViewTreeCtrl::wxDataViewTreeCtrl( wxWindow *parent, wxWindowID id,
1761 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
1762 {
1763 m_imageList = NULL;
1764 Create( parent, id, pos, size, style, validator );
1765
1766 wxDataViewTreeStore *store = new wxDataViewTreeStore;
1767 AssociateModel( store );
1768 store->DecRef();
1769
1770 AppendIconTextColumn(wxString(),0,wxDATAVIEW_CELL_INERT,-1);
1771 }
1772
1773 wxDataViewTreeCtrl::~wxDataViewTreeCtrl()
1774 {
1775 if (m_imageList)
1776 delete m_imageList;
1777 }
1778
1779 bool wxDataViewTreeCtrl::Create( wxWindow *parent, wxWindowID id,
1780 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
1781 {
1782 return wxDataViewCtrl::Create( parent, id, pos, size, style, validator );
1783 }
1784
1785 void wxDataViewTreeCtrl::SetImageList( wxImageList *imagelist )
1786 {
1787 if (m_imageList)
1788 delete m_imageList;
1789
1790 m_imageList = imagelist;
1791 }
1792
1793 wxDataViewItem wxDataViewTreeCtrl::AppendItem( const wxDataViewItem& parent,
1794 const wxString &text, int iconIndex, wxClientData *data )
1795 {
1796 wxIcon icon = wxNullIcon;
1797 if (m_imageList && (iconIndex != -1))
1798 icon = m_imageList->GetIcon( iconIndex );
1799
1800 return GetStore()->AppendItem( parent, text, icon, data );
1801 }
1802
1803 wxDataViewItem wxDataViewTreeCtrl::PrependItem( const wxDataViewItem& parent,
1804 const wxString &text, int iconIndex, wxClientData *data )
1805 {
1806 wxIcon icon = wxNullIcon;
1807 if (m_imageList && (iconIndex != -1))
1808 icon = m_imageList->GetIcon( iconIndex );
1809
1810 return GetStore()->PrependItem( parent, text, icon, data );
1811 }
1812
1813 wxDataViewItem wxDataViewTreeCtrl::InsertItem( const wxDataViewItem& parent, const wxDataViewItem& previous,
1814 const wxString &text, int iconIndex, wxClientData *data )
1815 {
1816 wxIcon icon = wxNullIcon;
1817 if (m_imageList && (iconIndex != -1))
1818 icon = m_imageList->GetIcon( iconIndex );
1819
1820 return GetStore()->InsertItem( parent, previous, text, icon, data );
1821 }
1822
1823 wxDataViewItem wxDataViewTreeCtrl::PrependContainer( const wxDataViewItem& parent,
1824 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
1825 {
1826 wxIcon icon = wxNullIcon;
1827 if (m_imageList && (iconIndex != -1))
1828 icon = m_imageList->GetIcon( iconIndex );
1829
1830 wxIcon expanded = wxNullIcon;
1831 if (m_imageList && (expandedIndex != -1))
1832 expanded = m_imageList->GetIcon( expandedIndex );
1833
1834 return GetStore()->PrependContainer( parent, text, icon, expanded, data );
1835 }
1836
1837 wxDataViewItem wxDataViewTreeCtrl::AppendContainer( const wxDataViewItem& parent,
1838 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
1839 {
1840 wxIcon icon = wxNullIcon;
1841 if (m_imageList && (iconIndex != -1))
1842 icon = m_imageList->GetIcon( iconIndex );
1843
1844 wxIcon expanded = wxNullIcon;
1845 if (m_imageList && (expandedIndex != -1))
1846 expanded = m_imageList->GetIcon( expandedIndex );
1847
1848 return GetStore()->AppendContainer( parent, text, icon, expanded, data );
1849 }
1850
1851 wxDataViewItem wxDataViewTreeCtrl::InsertContainer( const wxDataViewItem& parent, const wxDataViewItem& previous,
1852 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
1853 {
1854 wxIcon icon = wxNullIcon;
1855 if (m_imageList && (iconIndex != -1))
1856 icon = m_imageList->GetIcon( iconIndex );
1857
1858 wxIcon expanded = wxNullIcon;
1859 if (m_imageList && (expandedIndex != -1))
1860 expanded = m_imageList->GetIcon( expandedIndex );
1861
1862 return GetStore()->InsertContainer( parent, previous, text, icon, expanded, data );
1863 }
1864
1865 void wxDataViewTreeCtrl::OnExpanded( wxDataViewEvent &event )
1866 {
1867 if (m_imageList) return;
1868
1869 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
1870 if (!container) return;
1871
1872 container->SetExpanded( true );
1873 GetStore()->ItemChanged( event.GetItem() );
1874 }
1875
1876 void wxDataViewTreeCtrl::OnCollapsed( wxDataViewEvent &event )
1877 {
1878 if (m_imageList) return;
1879
1880 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
1881 if (!container) return;
1882
1883 container->SetExpanded( false );
1884 GetStore()->ItemChanged( event.GetItem() );
1885 }
1886
1887 void wxDataViewTreeCtrl::OnSize( wxSizeEvent &event )
1888 {
1889 #if defined(wxUSE_GENERICDATAVIEWCTRL)
1890 wxSize size = GetClientSize();
1891 wxDataViewColumn *col = GetColumn( 0 );
1892 if (col)
1893 col->SetWidth( size.x );
1894 #endif
1895 event.Skip( true );
1896 }
1897
1898 #endif // wxUSE_DATAVIEWCTRL
1899