]> git.saurik.com Git - wxWidgets.git/blob - src/common/datavcmn.cpp
added 'wxgui' template to make writing makefiles for gui apps a bit easier
[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 wxDataViewItem item( m_hash[row] );
389 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item );
390 m_hash.RemoveAt( row );
391 }
392 else
393 {
394 wxDataViewItem item( (void*) row );
395 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item );
396 m_lastIndex++;
397 }
398 }
399
400 void wxDataViewIndexListModel::RowChanged( unsigned int row )
401 {
402 wxDataViewModel::ItemChanged( GetItem(row) );
403 }
404
405 void wxDataViewIndexListModel::RowValueChanged( unsigned int row, unsigned int col )
406 {
407 wxDataViewModel::ValueChanged( GetItem(row), col );
408 }
409
410 unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem &item ) const
411 {
412 if (m_useHash)
413 {
414 if (m_ordered)
415 {
416 unsigned int pos = wxPtrToUInt( item.GetID() );
417 return pos-1;
418 }
419
420 // assert for not found
421 return (unsigned int) m_hash.Index( item.GetID() );
422 }
423 else
424 {
425 return wxPtrToUInt( item.GetID() );
426 }
427 }
428
429 wxDataViewItem wxDataViewIndexListModel::GetItem( unsigned int row ) const
430 {
431 if (m_useHash)
432 {
433 wxASSERT( row < m_hash.GetCount() );
434 return wxDataViewItem( m_hash[row] );
435 }
436 else
437 {
438 return wxDataViewItem( (void*) row );
439 }
440 }
441
442 bool wxDataViewIndexListModel::HasDefaultCompare() const
443 {
444 return !m_ordered;
445 }
446
447 int wxDataViewIndexListModel::Compare(const wxDataViewItem& item1,
448 const wxDataViewItem& item2,
449 unsigned int WXUNUSED(column),
450 bool ascending)
451 {
452 if (m_ordered || !m_useHash)
453 {
454 unsigned int pos1 = wxPtrToUInt(item1.GetID());
455 unsigned int pos2 = wxPtrToUInt(item2.GetID());
456
457 if (ascending)
458 return pos1 - pos2;
459 else
460 return pos2 - pos1;
461 }
462
463 if (ascending)
464 return GetRow(item1) - GetRow(item2);
465
466 return GetRow(item2) - GetRow(item1);
467 }
468
469 void wxDataViewIndexListModel::GetValue( wxVariant &variant,
470 const wxDataViewItem &item, unsigned int col ) const
471 {
472 GetValue( variant, GetRow(item), col );
473 }
474
475 bool wxDataViewIndexListModel::SetValue( const wxVariant &variant,
476 const wxDataViewItem &item, unsigned int col )
477 {
478 return SetValue( variant, GetRow(item), col );
479 }
480
481 bool wxDataViewIndexListModel::GetAttr( const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr )
482 {
483 return GetAttr( GetRow(item), col, attr );
484 }
485
486 wxDataViewItem wxDataViewIndexListModel::GetParent( const wxDataViewItem & WXUNUSED(item) ) const
487 {
488 return wxDataViewItem(0);
489 }
490
491 bool wxDataViewIndexListModel::IsContainer( const wxDataViewItem &item ) const
492 {
493 // only the invisible root item has children
494 if (!item.IsOk())
495 return true;
496
497 return false;
498 }
499
500 unsigned int wxDataViewIndexListModel::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
501 {
502 if (!m_useHash)
503 return 0; // error
504
505 if (item.IsOk())
506 return 0;
507
508 children = m_hash;
509
510 return m_hash.GetCount();
511 }
512
513 //-----------------------------------------------------------------------------
514 // wxDataViewIconText
515 //-----------------------------------------------------------------------------
516
517 IMPLEMENT_DYNAMIC_CLASS(wxDataViewIconText,wxObject)
518
519 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxDataViewIconText, WXDLLIMPEXP_ADV)
520
521 bool operator == (const wxDataViewIconText &one, const wxDataViewIconText &two)
522 {
523 if (one.GetText() != two.GetText()) return false;
524 if (one.IsSameAs(two)) return false;
525 return true;
526 }
527
528 // ---------------------------------------------------------
529 // wxDataViewRendererBase
530 // ---------------------------------------------------------
531
532 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase, wxObject)
533
534 wxDataViewRendererBase::wxDataViewRendererBase( const wxString &varianttype,
535 wxDataViewCellMode WXUNUSED(mode),
536 int WXUNUSED(align) )
537 {
538 m_variantType = varianttype;
539 m_editorCtrl = NULL;
540 }
541
542 const wxDataViewCtrl* wxDataViewRendererBase::GetView() const
543 {
544 return wx_const_cast(wxDataViewRendererBase*, this)->GetOwner()->GetOwner();
545 }
546
547 bool wxDataViewRendererBase::StartEditing( const wxDataViewItem &item, wxRect labelRect )
548 {
549 m_item = item; // remember for later
550
551 unsigned int col = GetOwner()->GetModelColumn();
552 wxVariant value;
553 GetOwner()->GetOwner()->GetModel()->GetValue( value, item, col );
554
555 m_editorCtrl = CreateEditorCtrl( GetOwner()->GetOwner()->GetMainWindow(), labelRect, value );
556
557 wxDataViewEditorCtrlEvtHandler *handler =
558 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl, (wxDataViewRenderer*) this );
559
560 m_editorCtrl->PushEventHandler( handler );
561
562 #if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
563 handler->SetFocusOnIdle();
564 #else
565 m_editorCtrl->SetFocus();
566 #endif
567
568 // Now we should send Editing Started event
569 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, GetOwner()->GetOwner()->GetId() );
570 event.SetDataViewColumn( GetOwner() );
571 event.SetModel( GetOwner()->GetOwner()->GetModel() );
572 event.SetItem( item );
573 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event );
574
575 return true;
576 }
577
578 void wxDataViewRendererBase::CancelEditing()
579 {
580 wxPendingDelete.Append( m_editorCtrl );
581
582 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
583
584 // m_editorCtrl->PopEventHandler( true );
585 }
586
587 bool wxDataViewRendererBase::FinishEditing()
588 {
589 wxVariant value;
590 GetValueFromEditorCtrl( m_editorCtrl, value );
591
592 wxPendingDelete.Append( m_editorCtrl );
593
594 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
595
596 if (!Validate(value))
597 return false;
598
599 unsigned int col = GetOwner()->GetModelColumn();
600 GetOwner()->GetOwner()->GetModel()->SetValue( value, m_item, col );
601 GetOwner()->GetOwner()->GetModel()->ValueChanged( m_item, col );
602
603 // m_editorCtrl->PopEventHandler( true );
604
605 // Now we should send Editing Done event
606 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, GetOwner()->GetOwner()->GetId() );
607 event.SetDataViewColumn( GetOwner() );
608 event.SetModel( GetOwner()->GetOwner()->GetModel() );
609 event.SetItem( m_item );
610 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event );
611
612 return true;
613 }
614
615 //-----------------------------------------------------------------------------
616 // wxDataViewEditorCtrlEvtHandler
617 //-----------------------------------------------------------------------------
618
619 BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler, wxEvtHandler)
620 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar)
621 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus)
622 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle)
623 END_EVENT_TABLE()
624
625 wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
626 wxControl *editorCtrl,
627 wxDataViewRenderer *owner )
628 {
629 m_owner = owner;
630 m_editorCtrl = editorCtrl;
631
632 m_finished = false;
633 }
634
635 void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent &event )
636 {
637 if (m_focusOnIdle)
638 {
639 m_focusOnIdle = false;
640 if (wxWindow::FindFocus() != m_editorCtrl)
641 m_editorCtrl->SetFocus();
642 }
643
644 event.Skip();
645 }
646
647 void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent &event )
648 {
649 switch ( event.m_keyCode )
650 {
651 case WXK_RETURN:
652 m_finished = true;
653 m_owner->FinishEditing();
654 break;
655
656 case WXK_ESCAPE:
657 m_finished = true;
658 m_owner->CancelEditing();
659 break;
660
661 default:
662 event.Skip();
663 }
664 }
665
666 void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent &event )
667 {
668 if (!m_finished)
669 {
670 m_finished = true;
671 m_owner->FinishEditing();
672 }
673
674 event.Skip();
675 }
676
677 // ---------------------------------------------------------
678 // wxDataViewColumnBase
679 // ---------------------------------------------------------
680
681 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase, wxObject)
682
683 wxDataViewColumnBase::wxDataViewColumnBase(const wxString& WXUNUSED(title),
684 wxDataViewRenderer *renderer,
685 unsigned int model_column,
686 int WXUNUSED(width),
687 wxAlignment WXUNUSED(align),
688 int WXUNUSED(flags))
689 {
690 m_renderer = renderer;
691 m_model_column = model_column;
692 m_owner = NULL;
693 m_renderer->SetOwner( (wxDataViewColumn*) this );
694
695 // NOTE: the wxDataViewColumn's ctor must store the width, align, flags
696 // parameters inside the native control!
697 }
698
699 wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap& bitmap,
700 wxDataViewRenderer *renderer,
701 unsigned int model_column,
702 int WXUNUSED(width),
703 wxAlignment WXUNUSED(align),
704 int WXUNUSED(flags) )
705 {
706 m_renderer = renderer;
707 m_model_column = model_column;
708 m_bitmap = bitmap;
709 m_owner = NULL;
710 m_renderer->SetOwner( (wxDataViewColumn*) this );
711 }
712
713 wxDataViewColumnBase::~wxDataViewColumnBase()
714 {
715 if (m_renderer)
716 delete m_renderer;
717 }
718
719 int wxDataViewColumnBase::GetFlags() const
720 {
721 int ret = 0;
722
723 if (IsSortable())
724 ret |= wxDATAVIEW_COL_SORTABLE;
725 if (IsResizeable())
726 ret |= wxDATAVIEW_COL_RESIZABLE;
727 if (IsHidden())
728 ret |= wxDATAVIEW_COL_HIDDEN;
729
730 return ret;
731 }
732
733 void wxDataViewColumnBase::SetFlags(int flags)
734 {
735 SetSortable((flags & wxDATAVIEW_COL_SORTABLE) != 0);
736 SetResizeable((flags & wxDATAVIEW_COL_RESIZABLE) != 0);
737 SetHidden((flags & wxDATAVIEW_COL_HIDDEN) != 0);
738 }
739
740 // ---------------------------------------------------------
741 // wxDataViewCtrlBase
742 // ---------------------------------------------------------
743
744 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl)
745
746 wxDataViewCtrlBase::wxDataViewCtrlBase()
747 {
748 m_model = NULL;
749 m_expander_column = 0;
750 m_indent = 8;
751 }
752
753 wxDataViewCtrlBase::~wxDataViewCtrlBase()
754 {
755 if (m_model)
756 {
757 m_model->DecRef();
758 m_model = NULL;
759 }
760 }
761
762 bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel *model )
763 {
764 if (m_model)
765 {
766 m_model->DecRef(); // discard old model, if any
767 }
768
769 // add our own reference to the new model:
770 m_model = model;
771 if (m_model)
772 {
773 m_model->IncRef();
774 }
775
776 return true;
777 }
778
779 wxDataViewModel* wxDataViewCtrlBase::GetModel()
780 {
781 return m_model;
782 }
783
784 const wxDataViewModel* wxDataViewCtrlBase::GetModel() const
785 {
786 return m_model;
787 }
788
789 wxDataViewColumn *
790 wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column,
791 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
792 {
793 wxDataViewColumn *ret = new wxDataViewColumn( label,
794 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
795 model_column, width, align, flags );
796 AppendColumn( ret );
797 return ret;
798 }
799
800 wxDataViewColumn *
801 wxDataViewCtrlBase::AppendIconTextColumn( const wxString &label, unsigned int model_column,
802 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
803 {
804 wxDataViewColumn *ret = new wxDataViewColumn( label,
805 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode, (int)align ),
806 model_column, width, align, flags );
807 AppendColumn( ret );
808 return ret;
809 }
810
811 wxDataViewColumn *
812 wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column,
813 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
814 {
815
816 wxDataViewColumn *ret = new wxDataViewColumn( label,
817 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
818 model_column, width, align, flags );
819 AppendColumn( ret );
820 return ret;
821 }
822
823 wxDataViewColumn *
824 wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column,
825 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
826 {
827 wxDataViewColumn *ret = new wxDataViewColumn( label,
828 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
829 model_column, width, align, flags );
830 AppendColumn( ret );
831 return ret;
832 }
833
834 wxDataViewColumn *
835 wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column,
836 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
837 {
838 wxDataViewColumn *ret = new wxDataViewColumn( label,
839 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
840 model_column, width, align, flags );
841 AppendColumn( ret );
842 return ret;
843 }
844
845 wxDataViewColumn *
846 wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column,
847 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
848 {
849 wxDataViewColumn *ret = new wxDataViewColumn( label,
850 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
851 model_column, width, align, flags );
852 AppendColumn( ret );
853 return ret;
854 }
855
856 wxDataViewColumn *
857 wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column,
858 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
859 {
860 wxDataViewColumn *ret = new wxDataViewColumn( label,
861 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
862 model_column, width, align, flags );
863 AppendColumn( ret );
864 return ret;
865 }
866
867 wxDataViewColumn *
868 wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap &label, unsigned int model_column,
869 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
870 {
871 wxDataViewColumn *ret = new wxDataViewColumn( label,
872 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode, (int)align ),
873 model_column, width, align, flags );
874 AppendColumn( ret );
875 return ret;
876 }
877
878 wxDataViewColumn *
879 wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column,
880 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
881 {
882 wxDataViewColumn *ret = new wxDataViewColumn( label,
883 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
884 model_column, width, align, flags );
885 AppendColumn( ret );
886 return ret;
887 }
888
889 wxDataViewColumn *
890 wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column,
891 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
892 {
893 wxDataViewColumn *ret = new wxDataViewColumn( label,
894 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
895 model_column, width, align, flags );
896 AppendColumn( ret );
897 return ret;
898 }
899
900 wxDataViewColumn *
901 wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column,
902 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
903 {
904 wxDataViewColumn *ret = new wxDataViewColumn( label,
905 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
906 model_column, width, align, flags );
907 AppendColumn( ret );
908 return ret;
909 }
910
911 wxDataViewColumn *
912 wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column,
913 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
914 {
915 wxDataViewColumn *ret = new wxDataViewColumn( label,
916 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
917 model_column, width, align, flags );
918 AppendColumn( ret );
919 return ret;
920 }
921
922 wxDataViewColumn *
923 wxDataViewCtrlBase::PrependTextColumn( const wxString &label, unsigned int model_column,
924 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
925 {
926 wxDataViewColumn *ret = new wxDataViewColumn( label,
927 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
928 model_column, width, align, flags );
929 PrependColumn( ret );
930 return ret;
931 }
932
933 wxDataViewColumn *
934 wxDataViewCtrlBase::PrependIconTextColumn( const wxString &label, unsigned int model_column,
935 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
936 {
937 wxDataViewColumn *ret = new wxDataViewColumn( label,
938 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode, (int)align ),
939 model_column, width, align, flags );
940 PrependColumn( ret );
941 return ret;
942 }
943
944 wxDataViewColumn *
945 wxDataViewCtrlBase::PrependToggleColumn( const wxString &label, unsigned int model_column,
946 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
947 {
948
949 wxDataViewColumn *ret = new wxDataViewColumn( label,
950 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
951 model_column, width, align, flags );
952 PrependColumn( ret );
953 return ret;
954 }
955
956 wxDataViewColumn *
957 wxDataViewCtrlBase::PrependProgressColumn( const wxString &label, unsigned int model_column,
958 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
959 {
960 wxDataViewColumn *ret = new wxDataViewColumn( label,
961 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
962 model_column, width, align, flags );
963 PrependColumn( ret );
964 return ret;
965 }
966
967 wxDataViewColumn *
968 wxDataViewCtrlBase::PrependDateColumn( const wxString &label, unsigned int model_column,
969 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
970 {
971 wxDataViewColumn *ret = new wxDataViewColumn( label,
972 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
973 model_column, width, align, flags );
974 PrependColumn( ret );
975 return ret;
976 }
977
978 wxDataViewColumn *
979 wxDataViewCtrlBase::PrependBitmapColumn( const wxString &label, unsigned int model_column,
980 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
981 {
982 wxDataViewColumn *ret = new wxDataViewColumn( label,
983 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
984 model_column, width, align, flags );
985 PrependColumn( ret );
986 return ret;
987 }
988
989 wxDataViewColumn *
990 wxDataViewCtrlBase::PrependTextColumn( const wxBitmap &label, unsigned int model_column,
991 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
992 {
993 wxDataViewColumn *ret = new wxDataViewColumn( label,
994 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
995 model_column, width, align, flags );
996 PrependColumn( ret );
997 return ret;
998 }
999
1000 wxDataViewColumn *
1001 wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap &label, unsigned int model_column,
1002 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1003 {
1004 wxDataViewColumn *ret = new wxDataViewColumn( label,
1005 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode, (int)align ),
1006 model_column, width, align, flags );
1007 PrependColumn( ret );
1008 return ret;
1009 }
1010
1011 wxDataViewColumn *
1012 wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap &label, unsigned int model_column,
1013 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1014 {
1015 wxDataViewColumn *ret = new wxDataViewColumn( label,
1016 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
1017 model_column, width, align, flags );
1018 PrependColumn( ret );
1019 return ret;
1020 }
1021
1022 wxDataViewColumn *
1023 wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap &label, unsigned int model_column,
1024 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1025 {
1026 wxDataViewColumn *ret = new wxDataViewColumn( label,
1027 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
1028 model_column, width, align, flags );
1029 PrependColumn( ret );
1030 return ret;
1031 }
1032
1033 wxDataViewColumn *
1034 wxDataViewCtrlBase::PrependDateColumn( const wxBitmap &label, unsigned int model_column,
1035 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1036 {
1037 wxDataViewColumn *ret = new wxDataViewColumn( label,
1038 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
1039 model_column, width, align, flags );
1040 PrependColumn( ret );
1041 return ret;
1042 }
1043
1044 wxDataViewColumn *
1045 wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap &label, unsigned int model_column,
1046 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1047 {
1048 wxDataViewColumn *ret = new wxDataViewColumn( label,
1049 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
1050 model_column, width, align, flags );
1051 PrependColumn( ret );
1052 return ret;
1053 }
1054
1055 bool
1056 wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
1057 {
1058 col->SetOwner( (wxDataViewCtrl*) this );
1059 return true;
1060 }
1061
1062 bool
1063 wxDataViewCtrlBase::PrependColumn( wxDataViewColumn *col )
1064 {
1065 col->SetOwner( (wxDataViewCtrl*) this );
1066 return true;
1067 }
1068
1069 // ---------------------------------------------------------
1070 // wxDataViewEvent
1071 // ---------------------------------------------------------
1072
1073 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent)
1074
1075 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED)
1076
1077 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED)
1078 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING)
1079 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED)
1080 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING)
1081 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED)
1082 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED)
1083 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE)
1084 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED)
1085
1086 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK)
1087 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK)
1088 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED)
1089
1090
1091 // -------------------------------------
1092 // wxDataViewSpinRenderer
1093 // -------------------------------------
1094
1095 wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min, int max, wxDataViewCellMode mode, int alignment ) :
1096 wxDataViewCustomRenderer( "long", mode, alignment )
1097 {
1098 m_min = min;
1099 m_max = max;
1100 }
1101
1102 wxControl* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
1103 {
1104 long l = value;
1105 return new wxSpinCtrl( parent, wxID_ANY, wxEmptyString,
1106 labelRect.GetTopLeft(), labelRect.GetSize(), wxSP_ARROW_KEYS, m_min, m_max, l );
1107 }
1108
1109 bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
1110 {
1111 wxSpinCtrl *sc = (wxSpinCtrl*) editor;
1112 long l = sc->GetValue();
1113 value = l;
1114 return true;
1115 }
1116
1117 bool wxDataViewSpinRenderer::Render( wxRect rect, wxDC *dc, int state )
1118 {
1119 wxString str;
1120 str.Printf( "%d", (int) m_data );
1121 RenderText( str, 0, rect, dc, state );
1122 return true;
1123 }
1124
1125 wxSize wxDataViewSpinRenderer::GetSize() const
1126 {
1127 return wxSize(80,16);
1128 }
1129
1130 bool wxDataViewSpinRenderer::SetValue( const wxVariant &value )
1131 {
1132 m_data = value.GetLong();
1133 return true;
1134 }
1135
1136 bool wxDataViewSpinRenderer::GetValue( wxVariant &value ) const
1137 {
1138 value = m_data;
1139 return true;
1140 }
1141
1142 //-----------------------------------------------------------------------------
1143 // wxDataViewTreeStore
1144 //-----------------------------------------------------------------------------
1145
1146 wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1147 wxDataViewTreeStoreNode *parent,
1148 const wxString &text, const wxIcon &icon, wxClientData *data )
1149 {
1150 m_parent = parent;
1151 m_text = text;
1152 m_icon = icon;
1153 m_data = data;
1154 }
1155
1156 wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1157 {
1158 if (m_data)
1159 delete m_data;
1160 }
1161
1162 #include "wx/listimpl.cpp"
1163 WX_DEFINE_LIST(wxDataViewTreeStoreNodeList)
1164
1165 wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1166 wxDataViewTreeStoreNode *parent, const wxString &text,
1167 const wxIcon &icon, const wxIcon &expanded, wxClientData *data ) :
1168 wxDataViewTreeStoreNode( parent, text, icon, data )
1169 {
1170 m_iconExpanded = expanded;
1171 m_isExpanded = false;
1172 m_children.DeleteContents(true);
1173 }
1174
1175 wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1176 {
1177 }
1178
1179 //-----------------------------------------------------------------------------
1180
1181 wxDataViewTreeStore::wxDataViewTreeStore()
1182 {
1183 m_root = new wxDataViewTreeStoreContainerNode( NULL, wxEmptyString );
1184 }
1185
1186 wxDataViewTreeStore::~wxDataViewTreeStore()
1187 {
1188 delete m_root;
1189 }
1190
1191 wxDataViewItem wxDataViewTreeStore::AppendItem( const wxDataViewItem& parent,
1192 const wxString &text, const wxIcon &icon, wxClientData *data )
1193 {
1194 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1195 if (!parent_node) return wxDataViewItem(0);
1196
1197 wxDataViewTreeStoreNode *node =
1198 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1199 parent_node->GetChildren().Append( node );
1200
1201 // notify control
1202 ItemAdded( parent, node->GetItem() );
1203
1204 return node->GetItem();
1205 }
1206
1207 wxDataViewItem wxDataViewTreeStore::PrependItem( const wxDataViewItem& parent,
1208 const wxString &text, const wxIcon &icon, wxClientData *data )
1209 {
1210 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1211 if (!parent_node) return wxDataViewItem(0);
1212
1213 wxDataViewTreeStoreNode *node =
1214 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1215 parent_node->GetChildren().Insert( node );
1216
1217 // notify control
1218 ItemAdded( parent, node->GetItem() );
1219
1220 return node->GetItem();
1221 }
1222
1223 wxDataViewItem
1224 wxDataViewTreeStore::InsertItem(const wxDataViewItem& WXUNUSED(parent),
1225 const wxDataViewItem& WXUNUSED(previous),
1226 const wxString& WXUNUSED(text),
1227 const wxIcon& WXUNUSED(icon),
1228 wxClientData * WXUNUSED(data))
1229 {
1230 return wxDataViewItem(0);
1231 }
1232
1233 wxDataViewItem wxDataViewTreeStore::PrependContainer( const wxDataViewItem& parent,
1234 const wxString &text, const wxIcon &icon, const wxIcon &expanded,
1235 wxClientData *data )
1236 {
1237 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1238 if (!parent_node) return wxDataViewItem(0);
1239
1240 wxDataViewTreeStoreContainerNode *node =
1241 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1242 parent_node->GetChildren().Insert( node );
1243
1244 // notify control
1245 ItemAdded( parent, node->GetItem() );
1246
1247 return node->GetItem();
1248 }
1249
1250 wxDataViewItem
1251 wxDataViewTreeStore::AppendContainer(const wxDataViewItem& parent,
1252 const wxString &text,
1253 const wxIcon& icon,
1254 const wxIcon& expanded,
1255 wxClientData * data)
1256 {
1257 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1258 if (!parent_node) return wxDataViewItem(0);
1259
1260 wxDataViewTreeStoreContainerNode *node =
1261 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1262 parent_node->GetChildren().Append( node );
1263
1264 // notify control
1265 ItemAdded( parent, node->GetItem() );
1266
1267 return node->GetItem();
1268 }
1269
1270 wxDataViewItem
1271 wxDataViewTreeStore::InsertContainer(const wxDataViewItem& WXUNUSED(parent),
1272 const wxDataViewItem& WXUNUSED(previous),
1273 const wxString& WXUNUSED(text),
1274 const wxIcon& WXUNUSED(icon),
1275 const wxIcon& WXUNUSED(expanded),
1276 wxClientData * WXUNUSED(data))
1277 {
1278 return wxDataViewItem(0);
1279 }
1280
1281 wxDataViewItem wxDataViewTreeStore::GetNthChild( const wxDataViewItem& parent, unsigned int pos ) const
1282 {
1283 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1284 if (!parent_node) return wxDataViewItem(0);
1285
1286 wxDataViewTreeStoreNodeList::compatibility_iterator node = parent_node->GetChildren().Item( pos );
1287 if (node)
1288 return node->GetData();
1289
1290 return wxDataViewItem(0);
1291 }
1292
1293 int wxDataViewTreeStore::GetChildCount( const wxDataViewItem& parent ) const
1294 {
1295 wxDataViewTreeStoreNode *node = FindNode( parent );
1296 if (!node) return -1;
1297
1298 if (!node->IsContainer())
1299 return 0;
1300
1301 wxDataViewTreeStoreContainerNode *container_node = (wxDataViewTreeStoreContainerNode*) node;
1302 return (int) container_node->GetChildren().GetCount();
1303 }
1304
1305 void wxDataViewTreeStore::SetItemText( const wxDataViewItem& item, const wxString &text )
1306 {
1307 wxDataViewTreeStoreNode *node = FindNode( item );
1308 if (!node) return;
1309
1310 node->SetText( text );
1311
1312 // notify control
1313 ValueChanged( item, 0 );
1314 }
1315
1316 wxString wxDataViewTreeStore::GetItemText( const wxDataViewItem& item ) const
1317 {
1318 wxDataViewTreeStoreNode *node = FindNode( item );
1319 if (!node) return wxEmptyString;
1320
1321 return node->GetText();
1322 }
1323
1324 void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
1325 {
1326 wxDataViewTreeStoreNode *node = FindNode( item );
1327 if (!node) return;
1328
1329 node->SetIcon( icon );
1330
1331 // notify control
1332 ValueChanged( item, 0 );
1333 }
1334
1335 const wxIcon &wxDataViewTreeStore::GetItemIcon( const wxDataViewItem& item ) const
1336 {
1337 wxDataViewTreeStoreNode *node = FindNode( item );
1338 if (!node) return wxNullIcon;
1339
1340 return node->GetIcon();
1341 }
1342
1343 void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
1344 {
1345 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1346 if (!node) return;
1347
1348 node->SetExpandedIcon( icon );
1349
1350 // notify control
1351 ValueChanged( item, 0 );
1352 }
1353
1354 const wxIcon &wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem& item ) const
1355 {
1356 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1357 if (!node) return wxNullIcon;
1358
1359 return node->GetExpandedIcon();
1360 }
1361
1362 void wxDataViewTreeStore::SetItemData( const wxDataViewItem& item, wxClientData *data )
1363 {
1364 wxDataViewTreeStoreNode *node = FindNode( item );
1365 if (!node) return;
1366
1367 node->SetData( data );
1368
1369 // notify control? only sensible when sorting on client data
1370 // ValueChanged( item, 0 );
1371 }
1372
1373 wxClientData *wxDataViewTreeStore::GetItemData( const wxDataViewItem& item ) const
1374 {
1375 wxDataViewTreeStoreNode *node = FindNode( item );
1376 if (!node) return NULL;
1377
1378 return node->GetData();
1379 }
1380
1381 void wxDataViewTreeStore::DeleteItem( const wxDataViewItem& item )
1382 {
1383 if (!item.IsOk()) return;
1384
1385 wxDataViewItem parent_item = GetParent( item );
1386
1387 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent_item );
1388 if (!parent_node) return;
1389
1390 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1391 if (!node) return;
1392
1393 parent_node->GetChildren().DeleteObject( node );
1394
1395 // notify control
1396 ItemDeleted( parent_item, item );
1397 }
1398
1399 void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem& item )
1400 {
1401 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1402 if (!node) return;
1403
1404 wxDataViewItemArray array;
1405 wxDataViewTreeStoreNodeList::iterator iter;
1406 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
1407 {
1408 wxDataViewTreeStoreNode* child = *iter;
1409 array.Add( child->GetItem() );
1410 }
1411
1412 node->GetChildren().clear();
1413
1414 // notify control
1415 ItemsDeleted( item, array );
1416 }
1417
1418 void wxDataViewTreeStore::DeleteAllItems()
1419 {
1420 // TODO
1421 }
1422
1423 void
1424 wxDataViewTreeStore::GetValue(wxVariant &variant,
1425 const wxDataViewItem &item,
1426 unsigned int WXUNUSED(col)) const
1427 {
1428 // if (col != 0) return;
1429
1430 wxDataViewTreeStoreNode *node = FindNode( item );
1431 if (!node) return;
1432
1433 wxIcon icon( node->GetIcon());
1434 if (node->IsContainer())
1435 {
1436 wxDataViewTreeStoreContainerNode *container = (wxDataViewTreeStoreContainerNode*) node;
1437 if (container->IsExpanded() && container->GetExpandedIcon().IsOk())
1438 icon = container->GetExpandedIcon();
1439 }
1440
1441 wxDataViewIconText data( node->GetText(), icon );
1442
1443 variant << data;
1444 }
1445
1446 bool
1447 wxDataViewTreeStore::SetValue(const wxVariant& variant,
1448 const wxDataViewItem& item,
1449 unsigned int WXUNUSED(col))
1450 {
1451 // if (col != 0) return false;
1452
1453 wxDataViewTreeStoreNode *node = FindNode( item );
1454 if (!node) return false;
1455
1456 wxDataViewIconText data;
1457
1458 data << variant;
1459
1460 node->SetText( data.GetText() );
1461 node->SetIcon( data.GetIcon() );
1462
1463 return true;
1464 }
1465
1466 wxDataViewItem wxDataViewTreeStore::GetParent( const wxDataViewItem &item ) const
1467 {
1468 wxDataViewTreeStoreNode *node = FindNode( item );
1469 if (!node) return wxDataViewItem(0);
1470
1471 wxDataViewTreeStoreNode *parent = node->GetParent();
1472 if (!parent) return wxDataViewItem(0);
1473
1474 if (parent == m_root)
1475 return wxDataViewItem(0);
1476
1477 return parent->GetItem();
1478 }
1479
1480 bool wxDataViewTreeStore::IsContainer( const wxDataViewItem &item ) const
1481 {
1482 wxDataViewTreeStoreNode *node = FindNode( item );
1483 if (!node) return false;
1484
1485 return node->IsContainer();
1486 }
1487
1488 unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
1489 {
1490 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1491 if (!node) return 0;
1492
1493 wxDataViewTreeStoreNodeList::iterator iter;
1494 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
1495 {
1496 wxDataViewTreeStoreNode* child = *iter;
1497 children.Add( child->GetItem() );
1498 }
1499
1500 return node->GetChildren().GetCount();
1501 }
1502
1503 int wxDataViewTreeStore::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
1504 unsigned int WXUNUSED(column), bool WXUNUSED(ascending) )
1505 {
1506 wxDataViewTreeStoreNode *node1 = FindNode( item1 );
1507 wxDataViewTreeStoreNode *node2 = FindNode( item2 );
1508
1509 if (!node1 || !node2)
1510 return 0;
1511
1512 wxDataViewTreeStoreContainerNode* parent1 =
1513 (wxDataViewTreeStoreContainerNode*) node1->GetParent();
1514 wxDataViewTreeStoreContainerNode* parent2 =
1515 (wxDataViewTreeStoreContainerNode*) node2->GetParent();
1516
1517 if (parent1 != parent2)
1518 {
1519 wxLogError( wxT("Comparing items with different parent.") );
1520 return 0;
1521 }
1522
1523 if (node1->IsContainer() && !!node2->IsContainer())
1524 return 1;
1525
1526 if (node2->IsContainer() && !!node1->IsContainer())
1527 return -1;
1528
1529 return parent1->GetChildren().IndexOf( node1 ) - parent1->GetChildren().IndexOf( node2 );
1530 }
1531
1532 wxDataViewTreeStoreNode *wxDataViewTreeStore::FindNode( const wxDataViewItem &item ) const
1533 {
1534 if (!item.IsOk())
1535 return m_root;
1536
1537 return (wxDataViewTreeStoreNode*) item.GetID();
1538 }
1539
1540 wxDataViewTreeStoreContainerNode *wxDataViewTreeStore::FindContainerNode( const wxDataViewItem &item ) const
1541 {
1542 if (!item.IsOk())
1543 return (wxDataViewTreeStoreContainerNode*) m_root;
1544
1545 wxDataViewTreeStoreNode* node = (wxDataViewTreeStoreNode*) item.GetID();
1546
1547 if (!node->IsContainer())
1548 return NULL;
1549
1550 return (wxDataViewTreeStoreContainerNode*) node;
1551 }
1552
1553 //-----------------------------------------------------------------------------
1554 // wxDataViewTreeCtrl
1555 //-----------------------------------------------------------------------------
1556
1557 IMPLEMENT_DYNAMIC_CLASS(wxDataViewTreeCtrl,wxDataViewCtrl)
1558
1559 BEGIN_EVENT_TABLE(wxDataViewTreeCtrl,wxDataViewCtrl)
1560 EVT_DATAVIEW_ITEM_EXPANDED(-1, wxDataViewTreeCtrl::OnExpanded)
1561 EVT_DATAVIEW_ITEM_COLLAPSED(-1, wxDataViewTreeCtrl::OnCollapsed)
1562 EVT_SIZE( wxDataViewTreeCtrl::OnSize )
1563 END_EVENT_TABLE()
1564
1565 wxDataViewTreeCtrl::wxDataViewTreeCtrl()
1566 {
1567 m_imageList = NULL;
1568 }
1569
1570 wxDataViewTreeCtrl::wxDataViewTreeCtrl( wxWindow *parent, wxWindowID id,
1571 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
1572 {
1573 m_imageList = NULL;
1574 Create( parent, id, pos, size, style, validator );
1575
1576 wxDataViewTreeStore *store = new wxDataViewTreeStore;
1577 AssociateModel( store );
1578 store->DecRef();
1579
1580 wxDataViewColumn *col = AppendIconTextColumn( "", 0, wxDATAVIEW_CELL_INERT, 40 );
1581
1582 #if !defined(__WXGTK20__)
1583 wxSize cient_size = GetClientSize();
1584 col->SetWidth( size.x );
1585 #endif
1586 }
1587
1588 wxDataViewTreeCtrl::~wxDataViewTreeCtrl()
1589 {
1590 if (m_imageList)
1591 delete m_imageList;
1592 }
1593
1594 bool wxDataViewTreeCtrl::Create( wxWindow *parent, wxWindowID id,
1595 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
1596 {
1597 return wxDataViewCtrl::Create( parent, id, pos, size, style, validator );
1598 }
1599
1600 void wxDataViewTreeCtrl::SetImageList( wxImageList *imagelist )
1601 {
1602 if (m_imageList)
1603 delete m_imageList;
1604
1605 m_imageList = imagelist;
1606 }
1607
1608 wxDataViewItem wxDataViewTreeCtrl::AppendItem( const wxDataViewItem& parent,
1609 const wxString &text, int iconIndex, wxClientData *data )
1610 {
1611 wxIcon icon = wxNullIcon;
1612 if (m_imageList && (iconIndex != -1))
1613 icon = m_imageList->GetIcon( iconIndex );
1614
1615 return GetStore()->AppendItem( parent, text, icon, data );
1616 }
1617
1618 wxDataViewItem wxDataViewTreeCtrl::PrependItem( const wxDataViewItem& parent,
1619 const wxString &text, int iconIndex, wxClientData *data )
1620 {
1621 wxIcon icon = wxNullIcon;
1622 if (m_imageList && (iconIndex != -1))
1623 icon = m_imageList->GetIcon( iconIndex );
1624
1625 return GetStore()->PrependItem( parent, text, icon, data );
1626 }
1627
1628 wxDataViewItem wxDataViewTreeCtrl::InsertItem( const wxDataViewItem& parent, const wxDataViewItem& previous,
1629 const wxString &text, int iconIndex, wxClientData *data )
1630 {
1631 wxIcon icon = wxNullIcon;
1632 if (m_imageList && (iconIndex != -1))
1633 icon = m_imageList->GetIcon( iconIndex );
1634
1635 return GetStore()->InsertItem( parent, previous, text, icon, data );
1636 }
1637
1638 wxDataViewItem wxDataViewTreeCtrl::PrependContainer( const wxDataViewItem& parent,
1639 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
1640 {
1641 wxIcon icon = wxNullIcon;
1642 if (m_imageList && (iconIndex != -1))
1643 icon = m_imageList->GetIcon( iconIndex );
1644
1645 wxIcon expanded = wxNullIcon;
1646 if (m_imageList && (expandedIndex != -1))
1647 expanded = m_imageList->GetIcon( expandedIndex );
1648
1649 return GetStore()->PrependContainer( parent, text, icon, expanded, data );
1650 }
1651
1652 wxDataViewItem wxDataViewTreeCtrl::AppendContainer( const wxDataViewItem& parent,
1653 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
1654 {
1655 wxIcon icon = wxNullIcon;
1656 if (m_imageList && (iconIndex != -1))
1657 icon = m_imageList->GetIcon( iconIndex );
1658
1659 wxIcon expanded = wxNullIcon;
1660 if (m_imageList && (expandedIndex != -1))
1661 expanded = m_imageList->GetIcon( expandedIndex );
1662
1663 return GetStore()->AppendContainer( parent, text, icon, expanded, data );
1664 }
1665
1666 wxDataViewItem wxDataViewTreeCtrl::InsertContainer( const wxDataViewItem& parent, const wxDataViewItem& previous,
1667 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
1668 {
1669 wxIcon icon = wxNullIcon;
1670 if (m_imageList && (iconIndex != -1))
1671 icon = m_imageList->GetIcon( iconIndex );
1672
1673 wxIcon expanded = wxNullIcon;
1674 if (m_imageList && (expandedIndex != -1))
1675 expanded = m_imageList->GetIcon( expandedIndex );
1676
1677 return GetStore()->InsertContainer( parent, previous, text, icon, expanded, data );
1678 }
1679
1680 void wxDataViewTreeCtrl::OnExpanded( wxDataViewEvent &event )
1681 {
1682 if (m_imageList) return;
1683
1684 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
1685 if (!container) return;
1686
1687 container->SetExpanded( true );
1688 GetStore()->ItemChanged( event.GetItem() );
1689 }
1690
1691 void wxDataViewTreeCtrl::OnCollapsed( wxDataViewEvent &event )
1692 {
1693 if (m_imageList) return;
1694
1695 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
1696 if (!container) return;
1697
1698 container->SetExpanded( false );
1699 GetStore()->ItemChanged( event.GetItem() );
1700 }
1701
1702 void wxDataViewTreeCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
1703 {
1704 #if !defined(__WXGTK20__)
1705 wxSize size = GetClientSize();
1706 wxDataViewColumn *col = GetColumn( 0 );
1707 col->SetWidth( size.x );
1708 #endif
1709 }
1710
1711 #endif // wxUSE_DATAVIEWCTRL
1712