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