]> git.saurik.com Git - wxWidgets.git/blob - src/common/datavcmn.cpp
Add support for column header images to wxListCtrl XRC handler.
[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 #ifndef WX_PRECOMP
23 #include "wx/dc.h"
24 #include "wx/settings.h"
25 #include "wx/log.h"
26 #include "wx/crt.h"
27 #endif
28
29 #include "wx/spinctrl.h"
30 #include "wx/choice.h"
31 #include "wx/imaglist.h"
32
33 const char wxDataViewCtrlNameStr[] = "dataviewCtrl";
34
35 namespace
36 {
37
38 // Custom handler pushed on top of the edit control used by wxDataViewCtrl to
39 // forward some events to the main control itself.
40 class wxDataViewEditorCtrlEvtHandler: public wxEvtHandler
41 {
42 public:
43 wxDataViewEditorCtrlEvtHandler(wxWindow *editor, wxDataViewRenderer *owner)
44 {
45 m_editorCtrl = editor;
46 m_owner = owner;
47
48 m_finished = false;
49 }
50
51 void AcceptChangesAndFinish();
52 void SetFocusOnIdle( bool focus = true ) { m_focusOnIdle = focus; }
53
54 protected:
55 void OnChar( wxKeyEvent &event );
56 void OnTextEnter( wxCommandEvent &event );
57 void OnKillFocus( wxFocusEvent &event );
58 void OnIdle( wxIdleEvent &event );
59
60 private:
61 wxDataViewRenderer *m_owner;
62 wxWindow *m_editorCtrl;
63 bool m_finished;
64 bool m_focusOnIdle;
65
66 private:
67 DECLARE_EVENT_TABLE()
68 };
69
70 } // anonymous namespace
71
72 // ---------------------------------------------------------
73 // wxDataViewItemAttr
74 // ---------------------------------------------------------
75
76 wxFont wxDataViewItemAttr::GetEffectiveFont(const wxFont& font) const
77 {
78 if ( !HasFont() )
79 return font;
80
81 wxFont f(font);
82 if ( GetBold() )
83 f.MakeBold();
84 if ( GetItalic() )
85 f.MakeItalic();
86 return f;
87 }
88
89
90 // ---------------------------------------------------------
91 // wxDataViewModelNotifier
92 // ---------------------------------------------------------
93
94 #include "wx/listimpl.cpp"
95 WX_DEFINE_LIST(wxDataViewModelNotifiers)
96
97 bool wxDataViewModelNotifier::ItemsAdded( const wxDataViewItem &parent, const wxDataViewItemArray &items )
98 {
99 size_t count = items.GetCount();
100 size_t i;
101 for (i = 0; i < count; i++)
102 if (!ItemAdded( parent, items[i] )) return false;
103
104 return true;
105 }
106
107 bool wxDataViewModelNotifier::ItemsDeleted( const wxDataViewItem &parent, const wxDataViewItemArray &items )
108 {
109 size_t count = items.GetCount();
110 size_t i;
111 for (i = 0; i < count; i++)
112 if (!ItemDeleted( parent, items[i] )) return false;
113
114 return true;
115 }
116
117 bool wxDataViewModelNotifier::ItemsChanged( const wxDataViewItemArray &items )
118 {
119 size_t count = items.GetCount();
120 size_t i;
121 for (i = 0; i < count; i++)
122 if (!ItemChanged( items[i] )) return false;
123
124 return true;
125 }
126
127 // ---------------------------------------------------------
128 // wxDataViewModel
129 // ---------------------------------------------------------
130
131 wxDataViewModel::wxDataViewModel()
132 {
133 m_notifiers.DeleteContents( true );
134 }
135
136 bool wxDataViewModel::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
137 {
138 bool ret = true;
139
140 wxDataViewModelNotifiers::iterator iter;
141 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
142 {
143 wxDataViewModelNotifier* notifier = *iter;
144 if (!notifier->ItemAdded( parent, item ))
145 ret = false;
146 }
147
148 return ret;
149 }
150
151 bool wxDataViewModel::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
152 {
153 bool ret = true;
154
155 wxDataViewModelNotifiers::iterator iter;
156 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
157 {
158 wxDataViewModelNotifier* notifier = *iter;
159 if (!notifier->ItemDeleted( parent, item ))
160 ret = false;
161 }
162
163 return ret;
164 }
165
166 bool wxDataViewModel::ItemChanged( const wxDataViewItem &item )
167 {
168 bool ret = true;
169
170 wxDataViewModelNotifiers::iterator iter;
171 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
172 {
173 wxDataViewModelNotifier* notifier = *iter;
174 if (!notifier->ItemChanged( item ))
175 ret = false;
176 }
177
178 return ret;
179 }
180
181 bool wxDataViewModel::ItemsAdded( const wxDataViewItem &parent, const wxDataViewItemArray &items )
182 {
183 bool ret = true;
184
185 wxDataViewModelNotifiers::iterator iter;
186 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
187 {
188 wxDataViewModelNotifier* notifier = *iter;
189 if (!notifier->ItemsAdded( parent, items ))
190 ret = false;
191 }
192
193 return ret;
194 }
195
196 bool wxDataViewModel::ItemsDeleted( const wxDataViewItem &parent, const wxDataViewItemArray &items )
197 {
198 bool ret = true;
199
200 wxDataViewModelNotifiers::iterator iter;
201 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
202 {
203 wxDataViewModelNotifier* notifier = *iter;
204 if (!notifier->ItemsDeleted( parent, items ))
205 ret = false;
206 }
207
208 return ret;
209 }
210
211 bool wxDataViewModel::ItemsChanged( const wxDataViewItemArray &items )
212 {
213 bool ret = true;
214
215 wxDataViewModelNotifiers::iterator iter;
216 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
217 {
218 wxDataViewModelNotifier* notifier = *iter;
219 if (!notifier->ItemsChanged( items ))
220 ret = false;
221 }
222
223 return ret;
224 }
225
226 bool wxDataViewModel::ValueChanged( const wxDataViewItem &item, unsigned int col )
227 {
228 bool ret = true;
229
230 wxDataViewModelNotifiers::iterator iter;
231 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
232 {
233 wxDataViewModelNotifier* notifier = *iter;
234 if (!notifier->ValueChanged( item, col ))
235 ret = false;
236 }
237
238 return ret;
239 }
240
241 bool wxDataViewModel::Cleared()
242 {
243 bool ret = true;
244
245 wxDataViewModelNotifiers::iterator iter;
246 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
247 {
248 wxDataViewModelNotifier* notifier = *iter;
249 if (!notifier->Cleared())
250 ret = false;
251 }
252
253 return ret;
254 }
255
256 bool wxDataViewModel::BeforeReset()
257 {
258 bool ret = true;
259
260 wxDataViewModelNotifiers::iterator iter;
261 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
262 {
263 wxDataViewModelNotifier* notifier = *iter;
264 if (!notifier->BeforeReset())
265 ret = false;
266 }
267
268 return ret;
269 }
270
271 bool wxDataViewModel::AfterReset()
272 {
273 bool ret = true;
274
275 wxDataViewModelNotifiers::iterator iter;
276 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
277 {
278 wxDataViewModelNotifier* notifier = *iter;
279 if (!notifier->AfterReset())
280 ret = false;
281 }
282
283 return ret;
284 }
285
286 void wxDataViewModel::Resort()
287 {
288 wxDataViewModelNotifiers::iterator iter;
289 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
290 {
291 wxDataViewModelNotifier* notifier = *iter;
292 notifier->Resort();
293 }
294 }
295
296 void wxDataViewModel::AddNotifier( wxDataViewModelNotifier *notifier )
297 {
298 m_notifiers.push_back( notifier );
299 notifier->SetOwner( this );
300 }
301
302 void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier *notifier )
303 {
304 m_notifiers.DeleteObject( notifier );
305 }
306
307 int wxDataViewModel::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
308 unsigned int column, bool ascending ) const
309 {
310 // sort branches before leaves
311 bool item1_is_container = IsContainer(item1);
312 bool item2_is_container = IsContainer(item2);
313
314 if (item1_is_container && !item2_is_container)
315 return 1;
316 if (item2_is_container && !item1_is_container)
317 return -1;
318
319 wxVariant value1,value2;
320 GetValue( value1, item1, column );
321 GetValue( value2, item2, column );
322
323 if (!ascending)
324 {
325 wxVariant temp = value1;
326 value1 = value2;
327 value2 = temp;
328 }
329
330 if (value1.GetType() == wxT("string"))
331 {
332 wxString str1 = value1.GetString();
333 wxString str2 = value2.GetString();
334 int res = str1.Cmp( str2 );
335 if (res)
336 return res;
337 }
338 else if (value1.GetType() == wxT("long"))
339 {
340 long l1 = value1.GetLong();
341 long l2 = value2.GetLong();
342 long res = l1-l2;
343 if (res)
344 return res;
345 }
346 else if (value1.GetType() == wxT("double"))
347 {
348 double d1 = value1.GetDouble();
349 double d2 = value2.GetDouble();
350 if (d1 < d2)
351 return 1;
352 if (d1 > d2)
353 return -1;
354 }
355 else if (value1.GetType() == wxT("datetime"))
356 {
357 wxDateTime dt1 = value1.GetDateTime();
358 wxDateTime dt2 = value2.GetDateTime();
359 if (dt1.IsEarlierThan(dt2))
360 return 1;
361 if (dt2.IsEarlierThan(dt1))
362 return -1;
363 }
364
365 // items must be different
366 wxUIntPtr id1 = wxPtrToUInt(item1.GetID()),
367 id2 = wxPtrToUInt(item2.GetID());
368
369 return ascending ? id1 - id2 : id2 - id1;
370 }
371
372 // ---------------------------------------------------------
373 // wxDataViewIndexListModel
374 // ---------------------------------------------------------
375
376 static int my_sort( int *v1, int *v2 )
377 {
378 return *v2-*v1;
379 }
380
381
382 wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size )
383 {
384 // IDs are ordered until an item gets deleted or inserted
385 m_ordered = true;
386
387 // build initial index
388 unsigned int i;
389 for (i = 1; i < initial_size+1; i++)
390 m_hash.Add( wxUIntToPtr(i) );
391 m_nextFreeID = initial_size + 1;
392 }
393
394 void wxDataViewIndexListModel::Reset( unsigned int new_size )
395 {
396 /* wxDataViewModel:: */ BeforeReset();
397
398 m_hash.Clear();
399
400 // IDs are ordered until an item gets deleted or inserted
401 m_ordered = true;
402
403 // build initial index
404 unsigned int i;
405 for (i = 1; i < new_size+1; i++)
406 m_hash.Add( wxUIntToPtr(i) );
407
408 m_nextFreeID = new_size + 1;
409
410 /* wxDataViewModel:: */ AfterReset();
411 }
412
413 void wxDataViewIndexListModel::RowPrepended()
414 {
415 m_ordered = false;
416
417 unsigned int id = m_nextFreeID;
418 m_nextFreeID++;
419
420 m_hash.Insert( wxUIntToPtr(id), 0 );
421 wxDataViewItem item( wxUIntToPtr(id) );
422 ItemAdded( wxDataViewItem(0), item );
423
424 }
425
426 void wxDataViewIndexListModel::RowInserted( unsigned int before )
427 {
428 m_ordered = false;
429
430 unsigned int id = m_nextFreeID;
431 m_nextFreeID++;
432
433 m_hash.Insert( wxUIntToPtr(id), before );
434 wxDataViewItem item( wxUIntToPtr(id) );
435 ItemAdded( wxDataViewItem(0), item );
436 }
437
438 void wxDataViewIndexListModel::RowAppended()
439 {
440 unsigned int id = m_nextFreeID;
441 m_nextFreeID++;
442
443 m_hash.Add( wxUIntToPtr(id) );
444 wxDataViewItem item( wxUIntToPtr(id) );
445 ItemAdded( wxDataViewItem(0), item );
446 }
447
448 void wxDataViewIndexListModel::RowDeleted( unsigned int row )
449 {
450 m_ordered = false;
451
452 wxDataViewItem item( m_hash[row] );
453 /* wxDataViewModel:: */ ItemDeleted( wxDataViewItem(0), item );
454 m_hash.RemoveAt( row );
455 }
456
457 void wxDataViewIndexListModel::RowsDeleted( const wxArrayInt &rows )
458 {
459 wxArrayInt sorted = rows;
460 sorted.Sort( my_sort );
461
462 m_ordered = false;
463
464 wxDataViewItemArray array;
465 unsigned int i;
466 for (i = 0; i < rows.GetCount(); i++)
467 {
468 wxDataViewItem item( m_hash[rows[i]] );
469 array.Add( item );
470 }
471 /* wxDataViewModel:: */ ItemsDeleted( wxDataViewItem(0), array );
472
473 for (i = 0; i < sorted.GetCount(); i++)
474 m_hash.RemoveAt( sorted[i] );
475 }
476
477 void wxDataViewIndexListModel::RowChanged( unsigned int row )
478 {
479 /* wxDataViewModel:: */ ItemChanged( GetItem(row) );
480 }
481
482 void wxDataViewIndexListModel::RowValueChanged( unsigned int row, unsigned int col )
483 {
484 /* wxDataViewModel:: */ ValueChanged( GetItem(row), col );
485 }
486
487 unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem &item ) const
488 {
489 if (m_ordered)
490 return wxPtrToUInt(item.GetID())-1;
491
492 // assert for not found
493 return (unsigned int) m_hash.Index( item.GetID() );
494 }
495
496 wxDataViewItem wxDataViewIndexListModel::GetItem( unsigned int row ) const
497 {
498 wxASSERT( row < m_hash.GetCount() );
499 return wxDataViewItem( m_hash[row] );
500 }
501
502 bool wxDataViewIndexListModel::HasDefaultCompare() const
503 {
504 return !m_ordered;
505 }
506
507 int wxDataViewIndexListModel::Compare(const wxDataViewItem& item1,
508 const wxDataViewItem& item2,
509 unsigned int WXUNUSED(column),
510 bool ascending) const
511 {
512 if (m_ordered)
513 {
514 unsigned int pos1 = wxPtrToUInt(item1.GetID()); // -1 not needed here
515 unsigned int pos2 = wxPtrToUInt(item2.GetID()); // -1 not needed here
516
517 if (ascending)
518 return pos1 - pos2;
519 else
520 return pos2 - pos1;
521 }
522
523 if (ascending)
524 return GetRow(item1) - GetRow(item2);
525
526 return GetRow(item2) - GetRow(item1);
527 }
528
529 unsigned int wxDataViewIndexListModel::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
530 {
531 if (item.IsOk())
532 return 0;
533
534 children = m_hash;
535
536 return m_hash.GetCount();
537 }
538
539 // ---------------------------------------------------------
540 // wxDataViewVirtualListModel
541 // ---------------------------------------------------------
542
543 #ifndef __WXMAC__
544
545 wxDataViewVirtualListModel::wxDataViewVirtualListModel( unsigned int initial_size )
546 {
547 m_size = initial_size;
548 }
549
550 void wxDataViewVirtualListModel::Reset( unsigned int new_size )
551 {
552 /* wxDataViewModel:: */ BeforeReset();
553
554 m_size = new_size;
555
556 /* wxDataViewModel:: */ AfterReset();
557 }
558
559 void wxDataViewVirtualListModel::RowPrepended()
560 {
561 m_size++;
562 wxDataViewItem item( wxUIntToPtr(1) );
563 ItemAdded( wxDataViewItem(0), item );
564 }
565
566 void wxDataViewVirtualListModel::RowInserted( unsigned int before )
567 {
568 m_size++;
569 wxDataViewItem item( wxUIntToPtr(before+1) );
570 ItemAdded( wxDataViewItem(0), item );
571 }
572
573 void wxDataViewVirtualListModel::RowAppended()
574 {
575 m_size++;
576 wxDataViewItem item( wxUIntToPtr(m_size) );
577 ItemAdded( wxDataViewItem(0), item );
578 }
579
580 void wxDataViewVirtualListModel::RowDeleted( unsigned int row )
581 {
582 m_size--;
583 wxDataViewItem item( wxUIntToPtr(row+1) );
584 /* wxDataViewModel:: */ ItemDeleted( wxDataViewItem(0), item );
585 }
586
587 void wxDataViewVirtualListModel::RowsDeleted( const wxArrayInt &rows )
588 {
589 m_size -= rows.GetCount();
590
591 wxArrayInt sorted = rows;
592 sorted.Sort( my_sort );
593
594 wxDataViewItemArray array;
595 unsigned int i;
596 for (i = 0; i < sorted.GetCount(); i++)
597 {
598 wxDataViewItem item( wxUIntToPtr(sorted[i]+1) );
599 array.Add( item );
600 }
601 /* wxDataViewModel:: */ ItemsDeleted( wxDataViewItem(0), array );
602 }
603
604 void wxDataViewVirtualListModel::RowChanged( unsigned int row )
605 {
606 /* wxDataViewModel:: */ ItemChanged( GetItem(row) );
607 }
608
609 void wxDataViewVirtualListModel::RowValueChanged( unsigned int row, unsigned int col )
610 {
611 /* wxDataViewModel:: */ ValueChanged( GetItem(row), col );
612 }
613
614 unsigned int wxDataViewVirtualListModel::GetRow( const wxDataViewItem &item ) const
615 {
616 return wxPtrToUInt( item.GetID() ) -1;
617 }
618
619 wxDataViewItem wxDataViewVirtualListModel::GetItem( unsigned int row ) const
620 {
621 return wxDataViewItem( wxUIntToPtr(row+1) );
622 }
623
624 bool wxDataViewVirtualListModel::HasDefaultCompare() const
625 {
626 return true;
627 }
628
629 int wxDataViewVirtualListModel::Compare(const wxDataViewItem& item1,
630 const wxDataViewItem& item2,
631 unsigned int WXUNUSED(column),
632 bool ascending) const
633 {
634 unsigned int pos1 = wxPtrToUInt(item1.GetID()); // -1 not needed here
635 unsigned int pos2 = wxPtrToUInt(item2.GetID()); // -1 not needed here
636
637 if (ascending)
638 return pos1 - pos2;
639 else
640 return pos2 - pos1;
641 }
642
643 unsigned int wxDataViewVirtualListModel::GetChildren( const wxDataViewItem &WXUNUSED(item), wxDataViewItemArray &WXUNUSED(children) ) const
644 {
645 return 0; // should we report an error ?
646 }
647
648 #endif // __WXMAC__
649
650 //-----------------------------------------------------------------------------
651 // wxDataViewIconText
652 //-----------------------------------------------------------------------------
653
654 IMPLEMENT_DYNAMIC_CLASS(wxDataViewIconText,wxObject)
655
656 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxDataViewIconText, WXDLLIMPEXP_ADV)
657
658 // ---------------------------------------------------------
659 // wxDataViewRendererBase
660 // ---------------------------------------------------------
661
662 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase, wxObject)
663
664 wxDataViewRendererBase::wxDataViewRendererBase( const wxString &varianttype,
665 wxDataViewCellMode WXUNUSED(mode),
666 int WXUNUSED(align) )
667 {
668 m_variantType = varianttype;
669 m_owner = NULL;
670 }
671
672 wxDataViewRendererBase::~wxDataViewRendererBase()
673 {
674 }
675
676 const wxDataViewCtrl* wxDataViewRendererBase::GetView() const
677 {
678 return const_cast<wxDataViewRendererBase*>(this)->GetOwner()->GetOwner();
679 }
680
681 bool wxDataViewRendererBase::StartEditing( const wxDataViewItem &item, wxRect labelRect )
682 {
683 wxDataViewCtrl* dv_ctrl = GetOwner()->GetOwner();
684
685 // Before doing anything we send an event asking if editing of this item is really wanted.
686 wxDataViewEvent start_event( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING, dv_ctrl->GetId() );
687 start_event.SetDataViewColumn( GetOwner() );
688 start_event.SetModel( dv_ctrl->GetModel() );
689 start_event.SetItem( item );
690 start_event.SetEventObject( dv_ctrl );
691 dv_ctrl->GetEventHandler()->ProcessEvent( start_event );
692 if( !start_event.IsAllowed() )
693 return false;
694
695 m_item = item; // remember for later
696
697 unsigned int col = GetOwner()->GetModelColumn();
698 wxVariant value;
699 dv_ctrl->GetModel()->GetValue( value, item, col );
700
701 m_editorCtrl = CreateEditorCtrl( dv_ctrl->GetMainWindow(), labelRect, value );
702
703 // there might be no editor control for the given item
704 if(!m_editorCtrl)
705 return false;
706
707 wxDataViewEditorCtrlEvtHandler *handler =
708 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl, (wxDataViewRenderer*) this );
709
710 m_editorCtrl->PushEventHandler( handler );
711
712 #if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
713 handler->SetFocusOnIdle();
714 #else
715 m_editorCtrl->SetFocus();
716 #endif
717
718 // Now we should send Editing Started event
719 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, dv_ctrl->GetId() );
720 event.SetDataViewColumn( GetOwner() );
721 event.SetModel( dv_ctrl->GetModel() );
722 event.SetItem( item );
723 event.SetEventObject( dv_ctrl );
724 dv_ctrl->GetEventHandler()->ProcessEvent( event );
725
726 return true;
727 }
728
729 void wxDataViewRendererBase::DestroyEditControl()
730 {
731 // Remove our event handler first to prevent it from (recursively) calling
732 // us again as it would do via a call to FinishEditing() when the editor
733 // loses focus when we hide it below.
734 wxEvtHandler * const handler = m_editorCtrl->PopEventHandler();
735
736 // Hide the control immediately but don't delete it yet as there could be
737 // some pending messages for it.
738 m_editorCtrl->Hide();
739
740 wxPendingDelete.Append(handler);
741 wxPendingDelete.Append(m_editorCtrl);
742 }
743
744 void wxDataViewRendererBase::CancelEditing()
745 {
746 if (!m_editorCtrl)
747 return;
748
749 DestroyEditControl();
750 }
751
752 bool wxDataViewRendererBase::FinishEditing()
753 {
754 if (!m_editorCtrl)
755 return true;
756
757 wxVariant value;
758 GetValueFromEditorCtrl( m_editorCtrl, value );
759
760 wxDataViewCtrl* dv_ctrl = GetOwner()->GetOwner();
761
762 dv_ctrl->GetMainWindow()->SetFocus();
763
764 DestroyEditControl();
765
766 if (!Validate(value))
767 return false;
768
769 unsigned int col = GetOwner()->GetModelColumn();
770 dv_ctrl->GetModel()->ChangeValue(value, m_item, col);
771
772 // Now we should send Editing Done event
773 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, dv_ctrl->GetId() );
774 event.SetDataViewColumn( GetOwner() );
775 event.SetModel( dv_ctrl->GetModel() );
776 event.SetItem( m_item );
777 event.SetEventObject( dv_ctrl );
778 dv_ctrl->GetEventHandler()->ProcessEvent( event );
779
780 return true;
781 }
782
783 void wxDataViewRendererBase::PrepareForItem(const wxDataViewModel *model,
784 const wxDataViewItem& item,
785 unsigned column)
786 {
787 wxVariant value;
788 model->GetValue(value, item, column);
789 SetValue(value);
790
791 wxDataViewItemAttr attr;
792 model->GetAttr(item, column, attr);
793 SetAttr(attr);
794
795 SetEnabled(model->IsEnabled(item, column));
796 }
797
798
799 // ----------------------------------------------------------------------------
800 // wxDataViewCustomRendererBase
801 // ----------------------------------------------------------------------------
802
803 void
804 wxDataViewCustomRendererBase::WXCallRender(wxRect rectCell, wxDC *dc, int state)
805 {
806 wxCHECK_RET( dc, "no DC to draw on in custom renderer?" );
807
808 // adjust the rectangle ourselves to account for the alignment
809 wxRect rectItem = rectCell;
810 const int align = GetAlignment();
811 if ( align != wxDVR_DEFAULT_ALIGNMENT )
812 {
813 const wxSize size = GetSize();
814
815 // take alignment into account only if there is enough space, otherwise
816 // show as much contents as possible
817 //
818 // notice that many existing renderers (e.g. wxDataViewSpinRenderer)
819 // return hard-coded size which can be more than they need and if we
820 // trusted their GetSize() we'd draw the text out of cell bounds
821 // entirely
822
823 if ( size.x >= 0 && size.x < rectCell.width )
824 {
825 if ( align & wxALIGN_CENTER_HORIZONTAL )
826 rectItem.x += (rectCell.width - size.x)/2;
827 else if ( align & wxALIGN_RIGHT )
828 rectItem.x += rectCell.width - size.x;
829 // else: wxALIGN_LEFT is the default
830
831 rectItem.width = size.x;
832 }
833
834 if ( size.y >= 0 && size.y < rectCell.height )
835 {
836 if ( align & wxALIGN_CENTER_VERTICAL )
837 rectItem.y += (rectCell.height - size.y)/2;
838 else if ( align & wxALIGN_BOTTOM )
839 rectItem.y += rectCell.height - size.y;
840 // else: wxALIGN_TOP is the default
841
842 rectItem.height = size.y;
843 }
844 }
845
846
847 // set up the DC attributes
848
849 // override custom foreground with the standard one for the selected items
850 // because we currently don't allow changing the selection background and
851 // custom colours may be unreadable on it
852 wxColour col;
853 if ( state & wxDATAVIEW_CELL_SELECTED )
854 col = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
855 else if ( m_attr.HasColour() )
856 col = m_attr.GetColour();
857 else // use default foreground
858 col = GetOwner()->GetOwner()->GetForegroundColour();
859
860 wxDCTextColourChanger changeFg(*dc, col);
861
862 wxDCFontChanger changeFont(*dc);
863 if ( m_attr.HasFont() )
864 changeFont.Set(m_attr.GetEffectiveFont(dc->GetFont()));
865
866 Render(rectItem, dc, state);
867 }
868
869 wxSize wxDataViewCustomRendererBase::GetTextExtent(const wxString& str) const
870 {
871 const wxDataViewCtrl *view = GetView();
872
873 if ( m_attr.HasFont() )
874 {
875 wxFont font(m_attr.GetEffectiveFont(view->GetFont()));
876 wxSize size;
877 view->GetTextExtent(str, &size.x, &size.y, NULL, NULL, &font);
878 return size;
879 }
880 else
881 {
882 return view->GetTextExtent(str);
883 }
884 }
885
886 void
887 wxDataViewCustomRendererBase::RenderText(const wxString& text,
888 int xoffset,
889 wxRect rect,
890 wxDC *dc,
891 int WXUNUSED(state))
892 {
893 wxRect rectText = rect;
894 rectText.x += xoffset;
895 rectText.width -= xoffset;
896
897 // check if we want to ellipsize the text if it doesn't fit
898 wxString ellipsizedText;
899 if ( GetEllipsizeMode() != wxELLIPSIZE_NONE )
900 {
901 ellipsizedText = wxControl::Ellipsize
902 (
903 text,
904 *dc,
905 GetEllipsizeMode(),
906 rectText.width,
907 wxELLIPSIZE_FLAGS_NONE
908 );
909 }
910
911 // get the alignment to use
912 int align = GetAlignment();
913 if ( align == wxDVR_DEFAULT_ALIGNMENT )
914 {
915 // if we don't have an explicit alignment ourselves, use that of the
916 // column in horizontal direction and default vertical alignment
917 align = GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL;
918 }
919
920 dc->DrawLabel(ellipsizedText.empty() ? text : ellipsizedText,
921 rectText, align);
922 }
923
924 //-----------------------------------------------------------------------------
925 // wxDataViewEditorCtrlEvtHandler
926 //-----------------------------------------------------------------------------
927
928 BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler, wxEvtHandler)
929 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar)
930 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus)
931 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle)
932 EVT_TEXT_ENTER (-1, wxDataViewEditorCtrlEvtHandler::OnTextEnter)
933 END_EVENT_TABLE()
934
935 void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent &event )
936 {
937 if (m_focusOnIdle)
938 {
939 m_focusOnIdle = false;
940 if (wxWindow::FindFocus() != m_editorCtrl)
941 m_editorCtrl->SetFocus();
942 }
943
944 event.Skip();
945 }
946
947 void wxDataViewEditorCtrlEvtHandler::OnTextEnter( wxCommandEvent &WXUNUSED(event) )
948 {
949 m_finished = true;
950 m_owner->FinishEditing();
951 }
952
953 void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent &event )
954 {
955 switch ( event.m_keyCode )
956 {
957 case WXK_RETURN:
958 m_finished = true;
959 m_owner->FinishEditing();
960 break;
961
962 case WXK_ESCAPE:
963 {
964 m_finished = true;
965 m_owner->CancelEditing();
966 break;
967 }
968 default:
969 event.Skip();
970 }
971 }
972
973 void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent &event )
974 {
975 if (!m_finished)
976 {
977 m_finished = true;
978 m_owner->FinishEditing();
979 }
980
981 event.Skip();
982 }
983
984 // ---------------------------------------------------------
985 // wxDataViewColumnBase
986 // ---------------------------------------------------------
987
988 void wxDataViewColumnBase::Init(wxDataViewRenderer *renderer,
989 unsigned int model_column)
990 {
991 m_renderer = renderer;
992 m_model_column = model_column;
993 m_owner = NULL;
994 m_renderer->SetOwner( (wxDataViewColumn*) this );
995 }
996
997 wxDataViewColumnBase::~wxDataViewColumnBase()
998 {
999 delete m_renderer;
1000 }
1001
1002 // ---------------------------------------------------------
1003 // wxDataViewCtrlBase
1004 // ---------------------------------------------------------
1005
1006 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl)
1007
1008 wxDataViewCtrlBase::wxDataViewCtrlBase()
1009 {
1010 m_model = NULL;
1011 m_expander_column = 0;
1012 m_indent = 8;
1013 }
1014
1015 wxDataViewCtrlBase::~wxDataViewCtrlBase()
1016 {
1017 if (m_model)
1018 {
1019 m_model->DecRef();
1020 m_model = NULL;
1021 }
1022 }
1023
1024 bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel *model )
1025 {
1026 if (m_model)
1027 {
1028 m_model->DecRef(); // discard old model, if any
1029 }
1030
1031 // add our own reference to the new model:
1032 m_model = model;
1033 if (m_model)
1034 {
1035 m_model->IncRef();
1036 }
1037
1038 return true;
1039 }
1040
1041 wxDataViewModel* wxDataViewCtrlBase::GetModel()
1042 {
1043 return m_model;
1044 }
1045
1046 const wxDataViewModel* wxDataViewCtrlBase::GetModel() const
1047 {
1048 return m_model;
1049 }
1050
1051 void wxDataViewCtrlBase::ExpandAncestors( const wxDataViewItem & item )
1052 {
1053 if (!m_model) return;
1054
1055 if (!item.IsOk()) return;
1056
1057 wxVector<wxDataViewItem> parentChain;
1058
1059 // at first we get all the parents of the selected item
1060 wxDataViewItem parent = m_model->GetParent(item);
1061 while (parent.IsOk())
1062 {
1063 parentChain.push_back(parent);
1064 parent = m_model->GetParent(parent);
1065 }
1066
1067 // then we expand the parents, starting at the root
1068 while (!parentChain.empty())
1069 {
1070 Expand(parentChain.back());
1071 parentChain.pop_back();
1072 }
1073 }
1074
1075 wxDataViewItem wxDataViewCtrlBase::GetCurrentItem() const
1076 {
1077 return HasFlag(wxDV_MULTIPLE) ? DoGetCurrentItem()
1078 : GetSelection();
1079 }
1080
1081 void wxDataViewCtrlBase::SetCurrentItem(const wxDataViewItem& item)
1082 {
1083 wxCHECK_RET( item.IsOk(), "Can't make current an invalid item." );
1084
1085 if ( HasFlag(wxDV_MULTIPLE) )
1086 DoSetCurrentItem(item);
1087 else
1088 Select(item);
1089 }
1090
1091 wxDataViewColumn *
1092 wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column,
1093 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1094 {
1095 wxDataViewColumn *ret = new wxDataViewColumn( label,
1096 new wxDataViewTextRenderer( wxT("string"), mode ),
1097 model_column, width, align, flags );
1098 AppendColumn( ret );
1099 return ret;
1100 }
1101
1102 wxDataViewColumn *
1103 wxDataViewCtrlBase::AppendIconTextColumn( const wxString &label, unsigned int model_column,
1104 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1105 {
1106 wxDataViewColumn *ret = new wxDataViewColumn( label,
1107 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1108 model_column, width, align, flags );
1109 AppendColumn( ret );
1110 return ret;
1111 }
1112
1113 wxDataViewColumn *
1114 wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column,
1115 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1116 {
1117 wxDataViewColumn *ret = new wxDataViewColumn( label,
1118 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1119 model_column, width, align, flags );
1120 AppendColumn( ret );
1121 return ret;
1122 }
1123
1124 wxDataViewColumn *
1125 wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column,
1126 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1127 {
1128 wxDataViewColumn *ret = new wxDataViewColumn( label,
1129 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1130 model_column, width, align, flags );
1131 AppendColumn( ret );
1132 return ret;
1133 }
1134
1135 wxDataViewColumn *
1136 wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column,
1137 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1138 {
1139 wxDataViewColumn *ret = new wxDataViewColumn( label,
1140 new wxDataViewDateRenderer( wxT("datetime"), mode ),
1141 model_column, width, align, flags );
1142 AppendColumn( ret );
1143 return ret;
1144 }
1145
1146 wxDataViewColumn *
1147 wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column,
1148 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1149 {
1150 wxDataViewColumn *ret = new wxDataViewColumn( label,
1151 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
1152 model_column, width, align, flags );
1153 AppendColumn( ret );
1154 return ret;
1155 }
1156
1157 wxDataViewColumn *
1158 wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column,
1159 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1160 {
1161 wxDataViewColumn *ret = new wxDataViewColumn( label,
1162 new wxDataViewTextRenderer( wxT("string"), mode ),
1163 model_column, width, align, flags );
1164 AppendColumn( ret );
1165 return ret;
1166 }
1167
1168 wxDataViewColumn *
1169 wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap &label, unsigned int model_column,
1170 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1171 {
1172 wxDataViewColumn *ret = new wxDataViewColumn( label,
1173 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1174 model_column, width, align, flags );
1175 AppendColumn( ret );
1176 return ret;
1177 }
1178
1179 wxDataViewColumn *
1180 wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column,
1181 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1182 {
1183 wxDataViewColumn *ret = new wxDataViewColumn( label,
1184 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1185 model_column, width, align, flags );
1186 AppendColumn( ret );
1187 return ret;
1188 }
1189
1190 wxDataViewColumn *
1191 wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column,
1192 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1193 {
1194 wxDataViewColumn *ret = new wxDataViewColumn( label,
1195 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1196 model_column, width, align, flags );
1197 AppendColumn( ret );
1198 return ret;
1199 }
1200
1201 wxDataViewColumn *
1202 wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column,
1203 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1204 {
1205 wxDataViewColumn *ret = new wxDataViewColumn( label,
1206 new wxDataViewDateRenderer( wxT("datetime"), mode ),
1207 model_column, width, align, flags );
1208 AppendColumn( ret );
1209 return ret;
1210 }
1211
1212 wxDataViewColumn *
1213 wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column,
1214 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1215 {
1216 wxDataViewColumn *ret = new wxDataViewColumn( label,
1217 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
1218 model_column, width, align, flags );
1219 AppendColumn( ret );
1220 return ret;
1221 }
1222
1223 wxDataViewColumn *
1224 wxDataViewCtrlBase::PrependTextColumn( const wxString &label, unsigned int model_column,
1225 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1226 {
1227 wxDataViewColumn *ret = new wxDataViewColumn( label,
1228 new wxDataViewTextRenderer( wxT("string"), mode ),
1229 model_column, width, align, flags );
1230 PrependColumn( ret );
1231 return ret;
1232 }
1233
1234 wxDataViewColumn *
1235 wxDataViewCtrlBase::PrependIconTextColumn( const wxString &label, unsigned int model_column,
1236 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1237 {
1238 wxDataViewColumn *ret = new wxDataViewColumn( label,
1239 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1240 model_column, width, align, flags );
1241 PrependColumn( ret );
1242 return ret;
1243 }
1244
1245 wxDataViewColumn *
1246 wxDataViewCtrlBase::PrependToggleColumn( const wxString &label, unsigned int model_column,
1247 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1248 {
1249
1250 wxDataViewColumn *ret = new wxDataViewColumn( label,
1251 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1252 model_column, width, align, flags );
1253 PrependColumn( ret );
1254 return ret;
1255 }
1256
1257 wxDataViewColumn *
1258 wxDataViewCtrlBase::PrependProgressColumn( const wxString &label, unsigned int model_column,
1259 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1260 {
1261 wxDataViewColumn *ret = new wxDataViewColumn( label,
1262 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1263 model_column, width, align, flags );
1264 PrependColumn( ret );
1265 return ret;
1266 }
1267
1268 wxDataViewColumn *
1269 wxDataViewCtrlBase::PrependDateColumn( const wxString &label, unsigned int model_column,
1270 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1271 {
1272 wxDataViewColumn *ret = new wxDataViewColumn( label,
1273 new wxDataViewDateRenderer( wxT("datetime"), mode ),
1274 model_column, width, align, flags );
1275 PrependColumn( ret );
1276 return ret;
1277 }
1278
1279 wxDataViewColumn *
1280 wxDataViewCtrlBase::PrependBitmapColumn( const wxString &label, unsigned int model_column,
1281 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1282 {
1283 wxDataViewColumn *ret = new wxDataViewColumn( label,
1284 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
1285 model_column, width, align, flags );
1286 PrependColumn( ret );
1287 return ret;
1288 }
1289
1290 wxDataViewColumn *
1291 wxDataViewCtrlBase::PrependTextColumn( const wxBitmap &label, unsigned int model_column,
1292 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1293 {
1294 wxDataViewColumn *ret = new wxDataViewColumn( label,
1295 new wxDataViewTextRenderer( wxT("string"), mode ),
1296 model_column, width, align, flags );
1297 PrependColumn( ret );
1298 return ret;
1299 }
1300
1301 wxDataViewColumn *
1302 wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap &label, unsigned int model_column,
1303 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1304 {
1305 wxDataViewColumn *ret = new wxDataViewColumn( label,
1306 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1307 model_column, width, align, flags );
1308 PrependColumn( ret );
1309 return ret;
1310 }
1311
1312 wxDataViewColumn *
1313 wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap &label, unsigned int model_column,
1314 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1315 {
1316 wxDataViewColumn *ret = new wxDataViewColumn( label,
1317 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1318 model_column, width, align, flags );
1319 PrependColumn( ret );
1320 return ret;
1321 }
1322
1323 wxDataViewColumn *
1324 wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap &label, unsigned int model_column,
1325 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1326 {
1327 wxDataViewColumn *ret = new wxDataViewColumn( label,
1328 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1329 model_column, width, align, flags );
1330 PrependColumn( ret );
1331 return ret;
1332 }
1333
1334 wxDataViewColumn *
1335 wxDataViewCtrlBase::PrependDateColumn( const wxBitmap &label, unsigned int model_column,
1336 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1337 {
1338 wxDataViewColumn *ret = new wxDataViewColumn( label,
1339 new wxDataViewDateRenderer( wxT("datetime"), mode ),
1340 model_column, width, align, flags );
1341 PrependColumn( ret );
1342 return ret;
1343 }
1344
1345 wxDataViewColumn *
1346 wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap &label, unsigned int model_column,
1347 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1348 {
1349 wxDataViewColumn *ret = new wxDataViewColumn( label,
1350 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
1351 model_column, width, align, flags );
1352 PrependColumn( ret );
1353 return ret;
1354 }
1355
1356 bool
1357 wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
1358 {
1359 col->SetOwner( (wxDataViewCtrl*) this );
1360 return true;
1361 }
1362
1363 bool
1364 wxDataViewCtrlBase::PrependColumn( wxDataViewColumn *col )
1365 {
1366 col->SetOwner( (wxDataViewCtrl*) this );
1367 return true;
1368 }
1369
1370 bool
1371 wxDataViewCtrlBase::InsertColumn( unsigned int WXUNUSED(pos), wxDataViewColumn *col )
1372 {
1373 col->SetOwner( (wxDataViewCtrl*) this );
1374 return true;
1375 }
1376
1377 // ---------------------------------------------------------
1378 // wxDataViewEvent
1379 // ---------------------------------------------------------
1380
1381 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent)
1382
1383 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, wxDataViewEvent );
1384
1385 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, wxDataViewEvent );
1386 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, wxDataViewEvent );
1387 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, wxDataViewEvent );
1388 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, wxDataViewEvent );
1389 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, wxDataViewEvent );
1390 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, wxDataViewEvent );
1391 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING, wxDataViewEvent );
1392 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, wxDataViewEvent );
1393 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, wxDataViewEvent );
1394
1395 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, wxDataViewEvent );
1396
1397 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, wxDataViewEvent );
1398 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, wxDataViewEvent );
1399 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, wxDataViewEvent );
1400 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_REORDERED, wxDataViewEvent );
1401
1402 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_CACHE_HINT, wxDataViewEvent );
1403
1404 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, wxDataViewEvent );
1405 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, wxDataViewEvent );
1406 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, wxDataViewEvent );
1407
1408
1409
1410 // -------------------------------------
1411 // wxDataViewSpinRenderer
1412 // -------------------------------------
1413
1414 wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min, int max, wxDataViewCellMode mode, int alignment ) :
1415 wxDataViewCustomRenderer(wxT("long"), mode, alignment )
1416 {
1417 m_min = min;
1418 m_max = max;
1419 }
1420
1421 wxWindow* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
1422 {
1423 long l = value;
1424 wxSize size = labelRect.GetSize();
1425 #ifdef __WXMAC__
1426 size = wxSize( wxMax(70,labelRect.width ), -1 );
1427 #endif
1428 wxString str;
1429 str.Printf( wxT("%d"), (int) l );
1430 wxSpinCtrl *sc = new wxSpinCtrl( parent, wxID_ANY, str,
1431 labelRect.GetTopLeft(), size, wxSP_ARROW_KEYS|wxTE_PROCESS_ENTER, m_min, m_max, l );
1432 #ifdef __WXMAC__
1433 size = sc->GetSize();
1434 wxPoint pt = sc->GetPosition();
1435 sc->SetSize( pt.x - 4, pt.y - 4, size.x, size.y );
1436 #endif
1437
1438 return sc;
1439 }
1440
1441 bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value )
1442 {
1443 wxSpinCtrl *sc = (wxSpinCtrl*) editor;
1444 long l = sc->GetValue();
1445 value = l;
1446 return true;
1447 }
1448
1449 bool wxDataViewSpinRenderer::Render( wxRect rect, wxDC *dc, int state )
1450 {
1451 wxString str;
1452 str.Printf(wxT("%d"), (int) m_data );
1453 RenderText( str, 0, rect, dc, state );
1454 return true;
1455 }
1456
1457 wxSize wxDataViewSpinRenderer::GetSize() const
1458 {
1459 wxSize sz = GetTextExtent(wxString::Format("%d", (int)m_data));
1460
1461 // Allow some space for the spin buttons, which is approximately the size
1462 // of a scrollbar (and getting pixel-exact value would be complicated).
1463 // Also add some whitespace between the text and the button:
1464 sz.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1465 sz.x += GetTextExtent("M").x;
1466
1467 return sz;
1468 }
1469
1470 bool wxDataViewSpinRenderer::SetValue( const wxVariant &value )
1471 {
1472 m_data = value.GetLong();
1473 return true;
1474 }
1475
1476 bool wxDataViewSpinRenderer::GetValue( wxVariant &value ) const
1477 {
1478 value = m_data;
1479 return true;
1480 }
1481
1482 // -------------------------------------
1483 // wxDataViewChoiceRenderer
1484 // -------------------------------------
1485
1486 #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXOSX_CARBON__)
1487
1488 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString& choices, wxDataViewCellMode mode, int alignment ) :
1489 wxDataViewCustomRenderer(wxT("string"), mode, alignment )
1490 {
1491 m_choices = choices;
1492 }
1493
1494 wxWindow* wxDataViewChoiceRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
1495 {
1496 wxChoice* c = new wxChoice
1497 (
1498 parent,
1499 wxID_ANY,
1500 labelRect.GetTopLeft(),
1501 wxSize(labelRect.GetWidth(), -1),
1502 m_choices
1503 );
1504 c->Move(labelRect.GetRight() - c->GetRect().width, wxDefaultCoord);
1505 c->SetStringSelection( value.GetString() );
1506 return c;
1507 }
1508
1509 bool wxDataViewChoiceRenderer::GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value )
1510 {
1511 wxChoice *c = (wxChoice*) editor;
1512 wxString s = c->GetStringSelection();
1513 value = s;
1514 return true;
1515 }
1516
1517 bool wxDataViewChoiceRenderer::Render( wxRect rect, wxDC *dc, int state )
1518 {
1519 RenderText( m_data, 0, rect, dc, state );
1520 return true;
1521 }
1522
1523 wxSize wxDataViewChoiceRenderer::GetSize() const
1524 {
1525 wxSize sz;
1526
1527 for ( wxArrayString::const_iterator i = m_choices.begin(); i != m_choices.end(); ++i )
1528 sz.IncTo(GetTextExtent(*i));
1529
1530 // Allow some space for the right-side button, which is approximately the
1531 // size of a scrollbar (and getting pixel-exact value would be complicated).
1532 // Also add some whitespace between the text and the button:
1533 sz.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1534 sz.x += GetTextExtent("M").x;
1535
1536 return sz;
1537 }
1538
1539 bool wxDataViewChoiceRenderer::SetValue( const wxVariant &value )
1540 {
1541 m_data = value.GetString();
1542 return true;
1543 }
1544
1545 bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const
1546 {
1547 value = m_data;
1548 return true;
1549 }
1550
1551 // ----------------------------------------------------------------------------
1552 // wxDataViewChoiceByIndexRenderer
1553 // ----------------------------------------------------------------------------
1554
1555 wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString &choices,
1556 wxDataViewCellMode mode, int alignment ) :
1557 wxDataViewChoiceRenderer( choices, mode, alignment )
1558 {
1559 }
1560
1561 wxWindow* wxDataViewChoiceByIndexRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
1562 {
1563 wxVariant string_value = GetChoice( value.GetLong() );
1564
1565 return wxDataViewChoiceRenderer::CreateEditorCtrl( parent, labelRect, string_value );
1566 }
1567
1568 bool wxDataViewChoiceByIndexRenderer::GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value )
1569 {
1570 wxVariant string_value;
1571 if (!wxDataViewChoiceRenderer::GetValueFromEditorCtrl( editor, string_value ))
1572 return false;
1573
1574 value = (long) GetChoices().Index( string_value.GetString() );
1575 return true;
1576 }
1577
1578 bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant &value )
1579 {
1580 wxVariant string_value = GetChoice( value.GetLong() );
1581 return wxDataViewChoiceRenderer::SetValue( string_value );
1582 }
1583
1584 bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant &value ) const
1585 {
1586 wxVariant string_value;
1587 if (!wxDataViewChoiceRenderer::GetValue( string_value ))
1588 return false;
1589
1590 value = (long) GetChoices().Index( string_value.GetString() );
1591 return true;
1592 }
1593
1594 #endif
1595
1596 //-----------------------------------------------------------------------------
1597 // wxDataViewListStore
1598 //-----------------------------------------------------------------------------
1599
1600 wxDataViewListStore::wxDataViewListStore()
1601 {
1602 }
1603
1604 wxDataViewListStore::~wxDataViewListStore()
1605 {
1606 wxVector<wxDataViewListStoreLine*>::iterator it;
1607 for (it = m_data.begin(); it != m_data.end(); ++it)
1608 {
1609 wxDataViewListStoreLine* line = *it;
1610 delete line;
1611 }
1612 }
1613
1614 void wxDataViewListStore::PrependColumn( const wxString &varianttype )
1615 {
1616 m_cols.Insert( varianttype, 0 );
1617 }
1618
1619 void wxDataViewListStore::InsertColumn( unsigned int pos, const wxString &varianttype )
1620 {
1621 m_cols.Insert( varianttype, pos );
1622 }
1623
1624 void wxDataViewListStore::AppendColumn( const wxString &varianttype )
1625 {
1626 m_cols.Add( varianttype );
1627 }
1628
1629 unsigned int wxDataViewListStore::GetColumnCount() const
1630 {
1631 return m_cols.GetCount();
1632 }
1633
1634 wxString wxDataViewListStore::GetColumnType( unsigned int pos ) const
1635 {
1636 return m_cols[pos];
1637 }
1638
1639 void wxDataViewListStore::AppendItem( const wxVector<wxVariant> &values, wxClientData *data )
1640 {
1641 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1642 line->m_values = values;
1643 m_data.push_back( line );
1644
1645 RowAppended();
1646 }
1647
1648 void wxDataViewListStore::PrependItem( const wxVector<wxVariant> &values, wxClientData *data )
1649 {
1650 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1651 line->m_values = values;
1652 m_data.insert( m_data.begin(), line );
1653
1654 RowPrepended();
1655 }
1656
1657 void wxDataViewListStore::InsertItem( unsigned int row, const wxVector<wxVariant> &values,
1658 wxClientData *data )
1659 {
1660 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1661 line->m_values = values;
1662 m_data.insert( m_data.begin()+row, line );
1663
1664 RowInserted( row );
1665 }
1666
1667 void wxDataViewListStore::DeleteItem( unsigned int row )
1668 {
1669 wxVector<wxDataViewListStoreLine*>::iterator it = m_data.begin() + row;
1670 delete *it;
1671 m_data.erase( it );
1672
1673 RowDeleted( row );
1674 }
1675
1676 void wxDataViewListStore::DeleteAllItems()
1677 {
1678 wxVector<wxDataViewListStoreLine*>::iterator it;
1679 for (it = m_data.begin(); it != m_data.end(); ++it)
1680 {
1681 wxDataViewListStoreLine* line = *it;
1682 delete line;
1683 }
1684
1685 m_data.clear();
1686
1687 Reset( 0 );
1688 }
1689
1690 void wxDataViewListStore::GetValueByRow( wxVariant &value, unsigned int row, unsigned int col ) const
1691 {
1692 wxDataViewListStoreLine *line = m_data[row];
1693 value = line->m_values[col];
1694 }
1695
1696 bool wxDataViewListStore::SetValueByRow( const wxVariant &value, unsigned int row, unsigned int col )
1697 {
1698 wxDataViewListStoreLine *line = m_data[row];
1699 line->m_values[col] = value;
1700
1701 return true;
1702 }
1703
1704 //-----------------------------------------------------------------------------
1705 // wxDataViewListCtrl
1706 //-----------------------------------------------------------------------------
1707
1708 IMPLEMENT_DYNAMIC_CLASS(wxDataViewListCtrl,wxDataViewCtrl)
1709
1710 BEGIN_EVENT_TABLE(wxDataViewListCtrl,wxDataViewCtrl)
1711 EVT_SIZE( wxDataViewListCtrl::OnSize )
1712 END_EVENT_TABLE()
1713
1714 wxDataViewListCtrl::wxDataViewListCtrl()
1715 {
1716 }
1717
1718 wxDataViewListCtrl::wxDataViewListCtrl( wxWindow *parent, wxWindowID id,
1719 const wxPoint& pos, const wxSize& size, long style,
1720 const wxValidator& validator )
1721 {
1722 Create( parent, id, pos, size, style, validator );
1723 }
1724
1725 wxDataViewListCtrl::~wxDataViewListCtrl()
1726 {
1727 }
1728
1729
1730 bool wxDataViewListCtrl::Create( wxWindow *parent, wxWindowID id,
1731 const wxPoint& pos, const wxSize& size, long style,
1732 const wxValidator& validator )
1733 {
1734 if ( !wxDataViewCtrl::Create( parent, id, pos, size, style, validator ) )
1735 return false;
1736
1737 wxDataViewListStore *store = new wxDataViewListStore;
1738 AssociateModel( store );
1739 store->DecRef();
1740
1741 return true;
1742 }
1743
1744 bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *column, const wxString &varianttype )
1745 {
1746 GetStore()->AppendColumn( varianttype );
1747 return wxDataViewCtrl::AppendColumn( column );
1748 }
1749
1750 bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *column, const wxString &varianttype )
1751 {
1752 GetStore()->PrependColumn( varianttype );
1753 return wxDataViewCtrl::PrependColumn( column );
1754 }
1755
1756 bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *column, const wxString &varianttype )
1757 {
1758 GetStore()->InsertColumn( pos, varianttype );
1759 return wxDataViewCtrl::InsertColumn( pos, column );
1760 }
1761
1762 bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *col )
1763 {
1764 return PrependColumn( col, "string" );
1765 }
1766
1767 bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
1768 {
1769 return InsertColumn( pos, col, "string" );
1770 }
1771
1772 bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *col )
1773 {
1774 return AppendColumn( col, "string" );
1775 }
1776
1777 wxDataViewColumn *wxDataViewListCtrl::AppendTextColumn( const wxString &label,
1778 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1779 {
1780 GetStore()->AppendColumn( wxT("string") );
1781
1782 wxDataViewColumn *ret = new wxDataViewColumn( label,
1783 new wxDataViewTextRenderer( wxT("string"), mode ),
1784 GetStore()->GetColumnCount()-1, width, align, flags );
1785
1786 wxDataViewCtrl::AppendColumn( ret );
1787
1788 return ret;
1789 }
1790
1791 wxDataViewColumn *wxDataViewListCtrl::AppendToggleColumn( const wxString &label,
1792 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1793 {
1794 GetStore()->AppendColumn( wxT("bool") );
1795
1796 wxDataViewColumn *ret = new wxDataViewColumn( label,
1797 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1798 GetStore()->GetColumnCount()-1, width, align, flags );
1799
1800 wxDataViewCtrl::AppendColumn( ret );
1801
1802 return ret;
1803 }
1804
1805 wxDataViewColumn *wxDataViewListCtrl::AppendProgressColumn( const wxString &label,
1806 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1807 {
1808 GetStore()->AppendColumn( wxT("long") );
1809
1810 wxDataViewColumn *ret = new wxDataViewColumn( label,
1811 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1812 GetStore()->GetColumnCount()-1, width, align, flags );
1813
1814 wxDataViewCtrl::AppendColumn( ret );
1815
1816 return ret;
1817 }
1818
1819 wxDataViewColumn *wxDataViewListCtrl::AppendIconTextColumn( const wxString &label,
1820 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1821 {
1822 GetStore()->AppendColumn( wxT("wxDataViewIconText") );
1823
1824 wxDataViewColumn *ret = new wxDataViewColumn( label,
1825 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1826 GetStore()->GetColumnCount()-1, width, align, flags );
1827
1828 wxDataViewCtrl::AppendColumn( ret );
1829
1830 return ret;
1831 }
1832
1833 void wxDataViewListCtrl::OnSize( wxSizeEvent &event )
1834 {
1835 event.Skip( true );
1836 }
1837
1838 //-----------------------------------------------------------------------------
1839 // wxDataViewTreeStore
1840 //-----------------------------------------------------------------------------
1841
1842 wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1843 wxDataViewTreeStoreNode *parent,
1844 const wxString &text, const wxIcon &icon, wxClientData *data )
1845 {
1846 m_parent = parent;
1847 m_text = text;
1848 m_icon = icon;
1849 m_data = data;
1850 }
1851
1852 wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1853 {
1854 if (m_data)
1855 delete m_data;
1856 }
1857
1858 #include "wx/listimpl.cpp"
1859 WX_DEFINE_LIST(wxDataViewTreeStoreNodeList)
1860
1861 wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1862 wxDataViewTreeStoreNode *parent, const wxString &text,
1863 const wxIcon &icon, const wxIcon &expanded, wxClientData *data ) :
1864 wxDataViewTreeStoreNode( parent, text, icon, data )
1865 {
1866 m_iconExpanded = expanded;
1867 m_isExpanded = false;
1868 m_children.DeleteContents(true);
1869 }
1870
1871 wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1872 {
1873 }
1874
1875 //-----------------------------------------------------------------------------
1876
1877 wxDataViewTreeStore::wxDataViewTreeStore()
1878 {
1879 m_root = new wxDataViewTreeStoreContainerNode( NULL, wxEmptyString );
1880 }
1881
1882 wxDataViewTreeStore::~wxDataViewTreeStore()
1883 {
1884 delete m_root;
1885 }
1886
1887 wxDataViewItem wxDataViewTreeStore::AppendItem( const wxDataViewItem& parent,
1888 const wxString &text, const wxIcon &icon, wxClientData *data )
1889 {
1890 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1891 if (!parent_node) return wxDataViewItem(0);
1892
1893 wxDataViewTreeStoreNode *node =
1894 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1895 parent_node->GetChildren().Append( node );
1896
1897 return node->GetItem();
1898 }
1899
1900 wxDataViewItem wxDataViewTreeStore::PrependItem( const wxDataViewItem& parent,
1901 const wxString &text, const wxIcon &icon, wxClientData *data )
1902 {
1903 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1904 if (!parent_node) return wxDataViewItem(0);
1905
1906 wxDataViewTreeStoreNode *node =
1907 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1908 parent_node->GetChildren().Insert( node );
1909
1910 return node->GetItem();
1911 }
1912
1913 wxDataViewItem
1914 wxDataViewTreeStore::InsertItem(const wxDataViewItem& parent,
1915 const wxDataViewItem& previous,
1916 const wxString& text,
1917 const wxIcon& icon,
1918 wxClientData *data)
1919 {
1920 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1921 if (!parent_node) return wxDataViewItem(0);
1922
1923 wxDataViewTreeStoreNode *previous_node = FindNode( previous );
1924 int pos = parent_node->GetChildren().IndexOf( previous_node );
1925 if (pos == wxNOT_FOUND) return wxDataViewItem(0);
1926
1927 wxDataViewTreeStoreNode *node =
1928 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1929 parent_node->GetChildren().Insert( (size_t) pos, node );
1930
1931 return node->GetItem();
1932 }
1933
1934 wxDataViewItem wxDataViewTreeStore::PrependContainer( const wxDataViewItem& parent,
1935 const wxString &text, const wxIcon &icon, const wxIcon &expanded,
1936 wxClientData *data )
1937 {
1938 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1939 if (!parent_node) return wxDataViewItem(0);
1940
1941 wxDataViewTreeStoreContainerNode *node =
1942 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1943 parent_node->GetChildren().Insert( node );
1944
1945 return node->GetItem();
1946 }
1947
1948 wxDataViewItem
1949 wxDataViewTreeStore::AppendContainer(const wxDataViewItem& parent,
1950 const wxString &text,
1951 const wxIcon& icon,
1952 const wxIcon& expanded,
1953 wxClientData * data)
1954 {
1955 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1956 if (!parent_node) return wxDataViewItem(0);
1957
1958 wxDataViewTreeStoreContainerNode *node =
1959 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1960 parent_node->GetChildren().Append( node );
1961
1962 return node->GetItem();
1963 }
1964
1965 wxDataViewItem
1966 wxDataViewTreeStore::InsertContainer(const wxDataViewItem& parent,
1967 const wxDataViewItem& previous,
1968 const wxString& text,
1969 const wxIcon& icon,
1970 const wxIcon& expanded,
1971 wxClientData * data)
1972 {
1973 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1974 if (!parent_node) return wxDataViewItem(0);
1975
1976 wxDataViewTreeStoreNode *previous_node = FindNode( previous );
1977 int pos = parent_node->GetChildren().IndexOf( previous_node );
1978 if (pos == wxNOT_FOUND) return wxDataViewItem(0);
1979
1980 wxDataViewTreeStoreContainerNode *node =
1981 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1982 parent_node->GetChildren().Insert( (size_t) pos, node );
1983
1984 return node->GetItem();
1985 }
1986
1987 bool wxDataViewTreeStore::IsContainer( const wxDataViewItem& item ) const
1988 {
1989 wxDataViewTreeStoreNode *node = FindNode( item );
1990 if (!node) return false;
1991
1992 return node->IsContainer();
1993 }
1994
1995 wxDataViewItem wxDataViewTreeStore::GetNthChild( const wxDataViewItem& parent, unsigned int pos ) const
1996 {
1997 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1998 if (!parent_node) return wxDataViewItem(0);
1999
2000 wxDataViewTreeStoreNodeList::compatibility_iterator node = parent_node->GetChildren().Item( pos );
2001 if (node)
2002 return node->GetData();
2003
2004 return wxDataViewItem(0);
2005 }
2006
2007 int wxDataViewTreeStore::GetChildCount( const wxDataViewItem& parent ) const
2008 {
2009 wxDataViewTreeStoreNode *node = FindNode( parent );
2010 if (!node) return -1;
2011
2012 if (!node->IsContainer())
2013 return 0;
2014
2015 wxDataViewTreeStoreContainerNode *container_node = (wxDataViewTreeStoreContainerNode*) node;
2016 return (int) container_node->GetChildren().GetCount();
2017 }
2018
2019 void wxDataViewTreeStore::SetItemText( const wxDataViewItem& item, const wxString &text )
2020 {
2021 wxDataViewTreeStoreNode *node = FindNode( item );
2022 if (!node) return;
2023
2024 node->SetText( text );
2025 }
2026
2027 wxString wxDataViewTreeStore::GetItemText( const wxDataViewItem& item ) const
2028 {
2029 wxDataViewTreeStoreNode *node = FindNode( item );
2030 if (!node) return wxEmptyString;
2031
2032 return node->GetText();
2033 }
2034
2035 void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
2036 {
2037 wxDataViewTreeStoreNode *node = FindNode( item );
2038 if (!node) return;
2039
2040 node->SetIcon( icon );
2041 }
2042
2043 const wxIcon &wxDataViewTreeStore::GetItemIcon( const wxDataViewItem& item ) const
2044 {
2045 wxDataViewTreeStoreNode *node = FindNode( item );
2046 if (!node) return wxNullIcon;
2047
2048 return node->GetIcon();
2049 }
2050
2051 void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
2052 {
2053 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
2054 if (!node) return;
2055
2056 node->SetExpandedIcon( icon );
2057 }
2058
2059 const wxIcon &wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem& item ) const
2060 {
2061 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
2062 if (!node) return wxNullIcon;
2063
2064 return node->GetExpandedIcon();
2065 }
2066
2067 void wxDataViewTreeStore::SetItemData( const wxDataViewItem& item, wxClientData *data )
2068 {
2069 wxDataViewTreeStoreNode *node = FindNode( item );
2070 if (!node) return;
2071
2072 node->SetData( data );
2073 }
2074
2075 wxClientData *wxDataViewTreeStore::GetItemData( const wxDataViewItem& item ) const
2076 {
2077 wxDataViewTreeStoreNode *node = FindNode( item );
2078 if (!node) return NULL;
2079
2080 return node->GetData();
2081 }
2082
2083 void wxDataViewTreeStore::DeleteItem( const wxDataViewItem& item )
2084 {
2085 if (!item.IsOk()) return;
2086
2087 wxDataViewItem parent_item = GetParent( item );
2088
2089 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent_item );
2090 if (!parent_node) return;
2091
2092 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
2093 if (!node) return;
2094
2095 parent_node->GetChildren().DeleteObject( node );
2096 }
2097
2098 void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem& item )
2099 {
2100 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
2101 if (!node) return;
2102
2103 node->GetChildren().clear();
2104 }
2105
2106 void wxDataViewTreeStore::DeleteAllItems()
2107 {
2108 DeleteChildren(m_root);
2109 }
2110
2111 void
2112 wxDataViewTreeStore::GetValue(wxVariant &variant,
2113 const wxDataViewItem &item,
2114 unsigned int WXUNUSED(col)) const
2115 {
2116 // if (col != 0) return;
2117
2118 wxDataViewTreeStoreNode *node = FindNode( item );
2119 if (!node) return;
2120
2121 wxIcon icon( node->GetIcon());
2122 if (node->IsContainer())
2123 {
2124 wxDataViewTreeStoreContainerNode *container = (wxDataViewTreeStoreContainerNode*) node;
2125 if (container->IsExpanded() && container->GetExpandedIcon().IsOk())
2126 icon = container->GetExpandedIcon();
2127 }
2128
2129 wxDataViewIconText data( node->GetText(), icon );
2130
2131 variant << data;
2132 }
2133
2134 bool
2135 wxDataViewTreeStore::SetValue(const wxVariant& variant,
2136 const wxDataViewItem& item,
2137 unsigned int WXUNUSED(col))
2138 {
2139 // if (col != 0) return false;
2140
2141 wxDataViewTreeStoreNode *node = FindNode( item );
2142 if (!node) return false;
2143
2144 wxDataViewIconText data;
2145
2146 data << variant;
2147
2148 node->SetText( data.GetText() );
2149 node->SetIcon( data.GetIcon() );
2150
2151 return true;
2152 }
2153
2154 wxDataViewItem wxDataViewTreeStore::GetParent( const wxDataViewItem &item ) const
2155 {
2156 wxDataViewTreeStoreNode *node = FindNode( item );
2157 if (!node) return wxDataViewItem(0);
2158
2159 wxDataViewTreeStoreNode *parent = node->GetParent();
2160 if (!parent) return wxDataViewItem(0);
2161
2162 if (parent == m_root)
2163 return wxDataViewItem(0);
2164
2165 return parent->GetItem();
2166 }
2167
2168 unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
2169 {
2170 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
2171 if (!node) return 0;
2172
2173 wxDataViewTreeStoreNodeList::iterator iter;
2174 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
2175 {
2176 wxDataViewTreeStoreNode* child = *iter;
2177 children.Add( child->GetItem() );
2178 }
2179
2180 return node->GetChildren().GetCount();
2181 }
2182
2183 int wxDataViewTreeStore::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
2184 unsigned int WXUNUSED(column), bool WXUNUSED(ascending) ) const
2185 {
2186 wxDataViewTreeStoreNode *node1 = FindNode( item1 );
2187 wxDataViewTreeStoreNode *node2 = FindNode( item2 );
2188
2189 if (!node1 || !node2)
2190 return 0;
2191
2192 wxDataViewTreeStoreContainerNode* parent1 =
2193 (wxDataViewTreeStoreContainerNode*) node1->GetParent();
2194 wxDataViewTreeStoreContainerNode* parent2 =
2195 (wxDataViewTreeStoreContainerNode*) node2->GetParent();
2196
2197 if (parent1 != parent2)
2198 {
2199 wxLogError( wxT("Comparing items with different parent.") );
2200 return 0;
2201 }
2202
2203 if (node1->IsContainer() && !node2->IsContainer())
2204 return -1;
2205
2206 if (node2->IsContainer() && !node1->IsContainer())
2207 return 1;
2208
2209 return parent1->GetChildren().IndexOf( node1 ) - parent2->GetChildren().IndexOf( node2 );
2210 }
2211
2212 wxDataViewTreeStoreNode *wxDataViewTreeStore::FindNode( const wxDataViewItem &item ) const
2213 {
2214 if (!item.IsOk())
2215 return m_root;
2216
2217 return (wxDataViewTreeStoreNode*) item.GetID();
2218 }
2219
2220 wxDataViewTreeStoreContainerNode *wxDataViewTreeStore::FindContainerNode( const wxDataViewItem &item ) const
2221 {
2222 if (!item.IsOk())
2223 return (wxDataViewTreeStoreContainerNode*) m_root;
2224
2225 wxDataViewTreeStoreNode* node = (wxDataViewTreeStoreNode*) item.GetID();
2226
2227 if (!node->IsContainer())
2228 return NULL;
2229
2230 return (wxDataViewTreeStoreContainerNode*) node;
2231 }
2232
2233 //-----------------------------------------------------------------------------
2234 // wxDataViewTreeCtrl
2235 //-----------------------------------------------------------------------------
2236
2237 IMPLEMENT_DYNAMIC_CLASS(wxDataViewTreeCtrl,wxDataViewCtrl)
2238
2239 BEGIN_EVENT_TABLE(wxDataViewTreeCtrl,wxDataViewCtrl)
2240 EVT_DATAVIEW_ITEM_EXPANDED(-1, wxDataViewTreeCtrl::OnExpanded)
2241 EVT_DATAVIEW_ITEM_COLLAPSED(-1, wxDataViewTreeCtrl::OnCollapsed)
2242 EVT_SIZE( wxDataViewTreeCtrl::OnSize )
2243 END_EVENT_TABLE()
2244
2245 wxDataViewTreeCtrl::~wxDataViewTreeCtrl()
2246 {
2247 delete m_imageList;
2248 }
2249
2250 bool wxDataViewTreeCtrl::Create( wxWindow *parent, wxWindowID id,
2251 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
2252 {
2253 if ( !wxDataViewCtrl::Create( parent, id, pos, size, style, validator ) )
2254 return false;
2255
2256 // create the standard model and a column in the tree
2257 wxDataViewTreeStore *store = new wxDataViewTreeStore;
2258 AssociateModel( store );
2259 store->DecRef();
2260
2261 AppendIconTextColumn
2262 (
2263 wxString(), // no label (header is not shown anyhow)
2264 0, // the only model column
2265 wxDATAVIEW_CELL_EDITABLE,
2266 -1, // default width
2267 wxALIGN_NOT, // and alignment
2268 0 // not resizable
2269 );
2270
2271 return true;
2272 }
2273
2274 void wxDataViewTreeCtrl::SetImageList( wxImageList *imagelist )
2275 {
2276 delete m_imageList;
2277
2278 m_imageList = imagelist;
2279 }
2280
2281 wxDataViewItem wxDataViewTreeCtrl::AppendItem( const wxDataViewItem& parent,
2282 const wxString &text, int iconIndex, wxClientData *data )
2283 {
2284 wxIcon icon = wxNullIcon;
2285 if (m_imageList && (iconIndex != -1))
2286 icon = m_imageList->GetIcon( iconIndex );
2287
2288 wxDataViewItem res = GetStore()->AppendItem( parent, text, icon, data );
2289
2290 GetStore()->ItemAdded( parent, res );
2291
2292 return res;
2293 }
2294
2295 wxDataViewItem wxDataViewTreeCtrl::PrependItem( const wxDataViewItem& parent,
2296 const wxString &text, int iconIndex, wxClientData *data )
2297 {
2298 wxIcon icon = wxNullIcon;
2299 if (m_imageList && (iconIndex != -1))
2300 icon = m_imageList->GetIcon( iconIndex );
2301
2302 wxDataViewItem res = GetStore()->PrependItem( parent, text, icon, data );
2303
2304 GetStore()->ItemAdded( parent, res );
2305
2306 return res;
2307 }
2308
2309 wxDataViewItem wxDataViewTreeCtrl::InsertItem( const wxDataViewItem& parent, const wxDataViewItem& previous,
2310 const wxString &text, int iconIndex, wxClientData *data )
2311 {
2312 wxIcon icon = wxNullIcon;
2313 if (m_imageList && (iconIndex != -1))
2314 icon = m_imageList->GetIcon( iconIndex );
2315
2316 wxDataViewItem res = GetStore()->InsertItem( parent, previous, text, icon, data );
2317
2318 GetStore()->ItemAdded( parent, res );
2319
2320 return res;
2321 }
2322
2323 wxDataViewItem wxDataViewTreeCtrl::PrependContainer( const wxDataViewItem& parent,
2324 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2325 {
2326 wxIcon icon = wxNullIcon;
2327 if (m_imageList && (iconIndex != -1))
2328 icon = m_imageList->GetIcon( iconIndex );
2329
2330 wxIcon expanded = wxNullIcon;
2331 if (m_imageList && (expandedIndex != -1))
2332 expanded = m_imageList->GetIcon( expandedIndex );
2333
2334 wxDataViewItem res = GetStore()->PrependContainer( parent, text, icon, expanded, data );
2335
2336 GetStore()->ItemAdded( parent, res );
2337
2338 return res;
2339 }
2340
2341 wxDataViewItem wxDataViewTreeCtrl::AppendContainer( const wxDataViewItem& parent,
2342 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2343 {
2344 wxIcon icon = wxNullIcon;
2345 if (m_imageList && (iconIndex != -1))
2346 icon = m_imageList->GetIcon( iconIndex );
2347
2348 wxIcon expanded = wxNullIcon;
2349 if (m_imageList && (expandedIndex != -1))
2350 expanded = m_imageList->GetIcon( expandedIndex );
2351
2352 wxDataViewItem res = GetStore()->AppendContainer( parent, text, icon, expanded, data );
2353
2354 GetStore()->ItemAdded( parent, res );
2355
2356 return res;
2357 }
2358
2359 wxDataViewItem wxDataViewTreeCtrl::InsertContainer( const wxDataViewItem& parent, const wxDataViewItem& previous,
2360 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2361 {
2362 wxIcon icon = wxNullIcon;
2363 if (m_imageList && (iconIndex != -1))
2364 icon = m_imageList->GetIcon( iconIndex );
2365
2366 wxIcon expanded = wxNullIcon;
2367 if (m_imageList && (expandedIndex != -1))
2368 expanded = m_imageList->GetIcon( expandedIndex );
2369
2370 wxDataViewItem res = GetStore()->InsertContainer( parent, previous, text, icon, expanded, data );
2371
2372 GetStore()->ItemAdded( parent, res );
2373
2374 return res;
2375 }
2376
2377 void wxDataViewTreeCtrl::SetItemText( const wxDataViewItem& item, const wxString &text )
2378 {
2379 GetStore()->SetItemText(item,text);
2380
2381 // notify control
2382 GetStore()->ValueChanged( item, 0 );
2383 }
2384
2385 void wxDataViewTreeCtrl::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
2386 {
2387 GetStore()->SetItemIcon(item,icon);
2388
2389 // notify control
2390 GetStore()->ValueChanged( item, 0 );
2391 }
2392
2393 void wxDataViewTreeCtrl::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
2394 {
2395 GetStore()->SetItemExpandedIcon(item,icon);
2396
2397 // notify control
2398 GetStore()->ValueChanged( item, 0 );
2399 }
2400
2401 void wxDataViewTreeCtrl::DeleteItem( const wxDataViewItem& item )
2402 {
2403 wxDataViewItem parent_item = GetStore()->GetParent( item );
2404
2405 GetStore()->DeleteItem(item);
2406
2407 // notify control
2408 GetStore()->ItemDeleted( parent_item, item );
2409 }
2410
2411 void wxDataViewTreeCtrl::DeleteChildren( const wxDataViewItem& item )
2412 {
2413 wxDataViewTreeStoreContainerNode *node = GetStore()->FindContainerNode( item );
2414 if (!node) return;
2415
2416 wxDataViewItemArray array;
2417 wxDataViewTreeStoreNodeList::iterator iter;
2418 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
2419 {
2420 wxDataViewTreeStoreNode* child = *iter;
2421 array.Add( child->GetItem() );
2422 }
2423
2424 GetStore()->DeleteChildren( item );
2425
2426 // notify control
2427 GetStore()->ItemsDeleted( item, array );
2428 }
2429
2430 void wxDataViewTreeCtrl::DeleteAllItems()
2431 {
2432 GetStore()->DeleteAllItems();
2433
2434 GetStore()->Cleared();
2435 }
2436
2437 void wxDataViewTreeCtrl::OnExpanded( wxDataViewEvent &event )
2438 {
2439 if (m_imageList) return;
2440
2441 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
2442 if (!container) return;
2443
2444 container->SetExpanded( true );
2445
2446 GetStore()->ItemChanged( event.GetItem() );
2447 }
2448
2449 void wxDataViewTreeCtrl::OnCollapsed( wxDataViewEvent &event )
2450 {
2451 if (m_imageList) return;
2452
2453 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
2454 if (!container) return;
2455
2456 container->SetExpanded( false );
2457
2458 GetStore()->ItemChanged( event.GetItem() );
2459 }
2460
2461 void wxDataViewTreeCtrl::OnSize( wxSizeEvent &event )
2462 {
2463 #if defined(wxUSE_GENERICDATAVIEWCTRL)
2464 // automatically resize our only column to take the entire control width
2465 if ( GetColumnCount() )
2466 {
2467 wxSize size = GetClientSize();
2468 GetColumn(0)->SetWidth(size.x);
2469 }
2470 #endif
2471 event.Skip( true );
2472 }
2473
2474 #endif // wxUSE_DATAVIEWCTRL
2475