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