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