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