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