]> git.saurik.com Git - wxWidgets.git/blob - src/common/datavcmn.cpp
skip apple options
[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 // ----------------------------------------------------------------------------
775 // wxDataViewCustomRendererBase
776 // ----------------------------------------------------------------------------
777
778 bool wxDataViewCustomRendererBase::ActivateCell(const wxRect& cell,
779 wxDataViewModel *model,
780 const wxDataViewItem & item,
781 unsigned int col,
782 const wxMouseEvent* mouseEvent)
783 {
784 // Compatibility code
785 if ( mouseEvent )
786 return LeftClick(mouseEvent->GetPosition(), cell, model, item, col);
787 else
788 return Activate(cell, model, item, col);
789 }
790
791 void wxDataViewCustomRendererBase::RenderBackground(wxDC* dc, const wxRect& rect)
792 {
793 if ( !m_attr.HasBackgroundColour() )
794 return;
795
796 const wxColour& colour = m_attr.GetBackgroundColour();
797 wxDCPenChanger changePen(*dc, colour);
798 wxDCBrushChanger changeBrush(*dc, colour);
799
800 dc->DrawRectangle(rect);
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 wxDataViewItem wxDataViewCtrlBase::GetSelection() const
1092 {
1093 if ( GetSelectedItemsCount() != 1 )
1094 return wxDataViewItem();
1095
1096 wxDataViewItemArray selections;
1097 GetSelections(selections);
1098 return selections[0];
1099 }
1100
1101 wxDataViewColumn *
1102 wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column,
1103 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1104 {
1105 wxDataViewColumn *ret = new wxDataViewColumn( label,
1106 new wxDataViewTextRenderer( wxT("string"), mode ),
1107 model_column, width, align, flags );
1108 AppendColumn( ret );
1109 return ret;
1110 }
1111
1112 wxDataViewColumn *
1113 wxDataViewCtrlBase::AppendIconTextColumn( const wxString &label, unsigned int model_column,
1114 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1115 {
1116 wxDataViewColumn *ret = new wxDataViewColumn( label,
1117 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1118 model_column, width, align, flags );
1119 AppendColumn( ret );
1120 return ret;
1121 }
1122
1123 wxDataViewColumn *
1124 wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column,
1125 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1126 {
1127 wxDataViewColumn *ret = new wxDataViewColumn( label,
1128 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1129 model_column, width, align, flags );
1130 AppendColumn( ret );
1131 return ret;
1132 }
1133
1134 wxDataViewColumn *
1135 wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column,
1136 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1137 {
1138 wxDataViewColumn *ret = new wxDataViewColumn( label,
1139 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1140 model_column, width, align, flags );
1141 AppendColumn( ret );
1142 return ret;
1143 }
1144
1145 wxDataViewColumn *
1146 wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column,
1147 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1148 {
1149 wxDataViewColumn *ret = new wxDataViewColumn( label,
1150 new wxDataViewDateRenderer( wxT("datetime"), mode ),
1151 model_column, width, align, flags );
1152 AppendColumn( ret );
1153 return ret;
1154 }
1155
1156 wxDataViewColumn *
1157 wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column,
1158 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1159 {
1160 wxDataViewColumn *ret = new wxDataViewColumn( label,
1161 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
1162 model_column, width, align, flags );
1163 AppendColumn( ret );
1164 return ret;
1165 }
1166
1167 wxDataViewColumn *
1168 wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column,
1169 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1170 {
1171 wxDataViewColumn *ret = new wxDataViewColumn( label,
1172 new wxDataViewTextRenderer( wxT("string"), mode ),
1173 model_column, width, align, flags );
1174 AppendColumn( ret );
1175 return ret;
1176 }
1177
1178 wxDataViewColumn *
1179 wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap &label, unsigned int model_column,
1180 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1181 {
1182 wxDataViewColumn *ret = new wxDataViewColumn( label,
1183 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1184 model_column, width, align, flags );
1185 AppendColumn( ret );
1186 return ret;
1187 }
1188
1189 wxDataViewColumn *
1190 wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column,
1191 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1192 {
1193 wxDataViewColumn *ret = new wxDataViewColumn( label,
1194 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1195 model_column, width, align, flags );
1196 AppendColumn( ret );
1197 return ret;
1198 }
1199
1200 wxDataViewColumn *
1201 wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column,
1202 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1203 {
1204 wxDataViewColumn *ret = new wxDataViewColumn( label,
1205 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1206 model_column, width, align, flags );
1207 AppendColumn( ret );
1208 return ret;
1209 }
1210
1211 wxDataViewColumn *
1212 wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column,
1213 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1214 {
1215 wxDataViewColumn *ret = new wxDataViewColumn( label,
1216 new wxDataViewDateRenderer( wxT("datetime"), mode ),
1217 model_column, width, align, flags );
1218 AppendColumn( ret );
1219 return ret;
1220 }
1221
1222 wxDataViewColumn *
1223 wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column,
1224 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1225 {
1226 wxDataViewColumn *ret = new wxDataViewColumn( label,
1227 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
1228 model_column, width, align, flags );
1229 AppendColumn( ret );
1230 return ret;
1231 }
1232
1233 wxDataViewColumn *
1234 wxDataViewCtrlBase::PrependTextColumn( const wxString &label, unsigned int model_column,
1235 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1236 {
1237 wxDataViewColumn *ret = new wxDataViewColumn( label,
1238 new wxDataViewTextRenderer( wxT("string"), mode ),
1239 model_column, width, align, flags );
1240 PrependColumn( ret );
1241 return ret;
1242 }
1243
1244 wxDataViewColumn *
1245 wxDataViewCtrlBase::PrependIconTextColumn( const wxString &label, unsigned int model_column,
1246 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1247 {
1248 wxDataViewColumn *ret = new wxDataViewColumn( label,
1249 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1250 model_column, width, align, flags );
1251 PrependColumn( ret );
1252 return ret;
1253 }
1254
1255 wxDataViewColumn *
1256 wxDataViewCtrlBase::PrependToggleColumn( const wxString &label, unsigned int model_column,
1257 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1258 {
1259
1260 wxDataViewColumn *ret = new wxDataViewColumn( label,
1261 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1262 model_column, width, align, flags );
1263 PrependColumn( ret );
1264 return ret;
1265 }
1266
1267 wxDataViewColumn *
1268 wxDataViewCtrlBase::PrependProgressColumn( const wxString &label, unsigned int model_column,
1269 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1270 {
1271 wxDataViewColumn *ret = new wxDataViewColumn( label,
1272 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1273 model_column, width, align, flags );
1274 PrependColumn( ret );
1275 return ret;
1276 }
1277
1278 wxDataViewColumn *
1279 wxDataViewCtrlBase::PrependDateColumn( const wxString &label, unsigned int model_column,
1280 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1281 {
1282 wxDataViewColumn *ret = new wxDataViewColumn( label,
1283 new wxDataViewDateRenderer( wxT("datetime"), mode ),
1284 model_column, width, align, flags );
1285 PrependColumn( ret );
1286 return ret;
1287 }
1288
1289 wxDataViewColumn *
1290 wxDataViewCtrlBase::PrependBitmapColumn( const wxString &label, unsigned int model_column,
1291 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1292 {
1293 wxDataViewColumn *ret = new wxDataViewColumn( label,
1294 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
1295 model_column, width, align, flags );
1296 PrependColumn( ret );
1297 return ret;
1298 }
1299
1300 wxDataViewColumn *
1301 wxDataViewCtrlBase::PrependTextColumn( const wxBitmap &label, unsigned int model_column,
1302 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1303 {
1304 wxDataViewColumn *ret = new wxDataViewColumn( label,
1305 new wxDataViewTextRenderer( wxT("string"), mode ),
1306 model_column, width, align, flags );
1307 PrependColumn( ret );
1308 return ret;
1309 }
1310
1311 wxDataViewColumn *
1312 wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap &label, unsigned int model_column,
1313 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1314 {
1315 wxDataViewColumn *ret = new wxDataViewColumn( label,
1316 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1317 model_column, width, align, flags );
1318 PrependColumn( ret );
1319 return ret;
1320 }
1321
1322 wxDataViewColumn *
1323 wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap &label, unsigned int model_column,
1324 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1325 {
1326 wxDataViewColumn *ret = new wxDataViewColumn( label,
1327 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1328 model_column, width, align, flags );
1329 PrependColumn( ret );
1330 return ret;
1331 }
1332
1333 wxDataViewColumn *
1334 wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap &label, unsigned int model_column,
1335 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1336 {
1337 wxDataViewColumn *ret = new wxDataViewColumn( label,
1338 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1339 model_column, width, align, flags );
1340 PrependColumn( ret );
1341 return ret;
1342 }
1343
1344 wxDataViewColumn *
1345 wxDataViewCtrlBase::PrependDateColumn( const wxBitmap &label, unsigned int model_column,
1346 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1347 {
1348 wxDataViewColumn *ret = new wxDataViewColumn( label,
1349 new wxDataViewDateRenderer( wxT("datetime"), mode ),
1350 model_column, width, align, flags );
1351 PrependColumn( ret );
1352 return ret;
1353 }
1354
1355 wxDataViewColumn *
1356 wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap &label, unsigned int model_column,
1357 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1358 {
1359 wxDataViewColumn *ret = new wxDataViewColumn( label,
1360 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
1361 model_column, width, align, flags );
1362 PrependColumn( ret );
1363 return ret;
1364 }
1365
1366 bool
1367 wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
1368 {
1369 col->SetOwner( (wxDataViewCtrl*) this );
1370 return true;
1371 }
1372
1373 bool
1374 wxDataViewCtrlBase::PrependColumn( wxDataViewColumn *col )
1375 {
1376 col->SetOwner( (wxDataViewCtrl*) this );
1377 return true;
1378 }
1379
1380 bool
1381 wxDataViewCtrlBase::InsertColumn( unsigned int WXUNUSED(pos), wxDataViewColumn *col )
1382 {
1383 col->SetOwner( (wxDataViewCtrl*) this );
1384 return true;
1385 }
1386
1387 void wxDataViewCtrlBase::StartEditor(const wxDataViewItem& item, unsigned int column)
1388 {
1389 EditItem(item, GetColumn(column));
1390 }
1391
1392 // ---------------------------------------------------------
1393 // wxDataViewEvent
1394 // ---------------------------------------------------------
1395
1396 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent)
1397
1398 wxDEFINE_EVENT( wxEVT_DATAVIEW_SELECTION_CHANGED, wxDataViewEvent );
1399
1400 wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_ACTIVATED, wxDataViewEvent );
1401 wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_COLLAPSING, wxDataViewEvent );
1402 wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_COLLAPSED, wxDataViewEvent );
1403 wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_EXPANDING, wxDataViewEvent );
1404 wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_EXPANDED, wxDataViewEvent );
1405 wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_EDITING_STARTED, wxDataViewEvent );
1406 wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_START_EDITING, wxDataViewEvent );
1407 wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_EDITING_DONE, wxDataViewEvent );
1408 wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, wxDataViewEvent );
1409
1410 wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, wxDataViewEvent );
1411
1412 wxDEFINE_EVENT( wxEVT_DATAVIEW_COLUMN_HEADER_CLICK, wxDataViewEvent );
1413 wxDEFINE_EVENT( wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, wxDataViewEvent );
1414 wxDEFINE_EVENT( wxEVT_DATAVIEW_COLUMN_SORTED, wxDataViewEvent );
1415 wxDEFINE_EVENT( wxEVT_DATAVIEW_COLUMN_REORDERED, wxDataViewEvent );
1416
1417 wxDEFINE_EVENT( wxEVT_DATAVIEW_CACHE_HINT, wxDataViewEvent );
1418
1419 wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_BEGIN_DRAG, wxDataViewEvent );
1420 wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_DROP_POSSIBLE, wxDataViewEvent );
1421 wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_DROP, wxDataViewEvent );
1422
1423
1424
1425 // -------------------------------------
1426 // wxDataViewSpinRenderer
1427 // -------------------------------------
1428
1429 wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min, int max, wxDataViewCellMode mode, int alignment ) :
1430 wxDataViewCustomRenderer(wxT("long"), mode, alignment )
1431 {
1432 m_min = min;
1433 m_max = max;
1434 }
1435
1436 wxWindow* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
1437 {
1438 long l = value;
1439 wxSize size = labelRect.GetSize();
1440 #ifdef __WXMAC__
1441 size = wxSize( wxMax(70,labelRect.width ), -1 );
1442 #endif
1443 wxString str;
1444 str.Printf( wxT("%d"), (int) l );
1445 wxSpinCtrl *sc = new wxSpinCtrl( parent, wxID_ANY, str,
1446 labelRect.GetTopLeft(), size, wxSP_ARROW_KEYS|wxTE_PROCESS_ENTER, m_min, m_max, l );
1447 #ifdef __WXMAC__
1448 size = sc->GetSize();
1449 wxPoint pt = sc->GetPosition();
1450 sc->SetSize( pt.x - 4, pt.y - 4, size.x, size.y );
1451 #endif
1452
1453 return sc;
1454 }
1455
1456 bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value )
1457 {
1458 wxSpinCtrl *sc = (wxSpinCtrl*) editor;
1459 long l = sc->GetValue();
1460 value = l;
1461 return true;
1462 }
1463
1464 bool wxDataViewSpinRenderer::Render( wxRect rect, wxDC *dc, int state )
1465 {
1466 wxString str;
1467 str.Printf(wxT("%d"), (int) m_data );
1468 RenderText( str, 0, rect, dc, state );
1469 return true;
1470 }
1471
1472 wxSize wxDataViewSpinRenderer::GetSize() const
1473 {
1474 wxSize sz = GetTextExtent(wxString::Format("%d", (int)m_data));
1475
1476 // Allow some space for the spin buttons, which is approximately the size
1477 // of a scrollbar (and getting pixel-exact value would be complicated).
1478 // Also add some whitespace between the text and the button:
1479 sz.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1480 sz.x += GetTextExtent("M").x;
1481
1482 return sz;
1483 }
1484
1485 bool wxDataViewSpinRenderer::SetValue( const wxVariant &value )
1486 {
1487 m_data = value.GetLong();
1488 return true;
1489 }
1490
1491 bool wxDataViewSpinRenderer::GetValue( wxVariant &value ) const
1492 {
1493 value = m_data;
1494 return true;
1495 }
1496
1497 // -------------------------------------
1498 // wxDataViewChoiceRenderer
1499 // -------------------------------------
1500
1501 #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXOSX_CARBON__)
1502
1503 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString& choices, wxDataViewCellMode mode, int alignment ) :
1504 wxDataViewCustomRenderer(wxT("string"), mode, alignment )
1505 {
1506 m_choices = choices;
1507 }
1508
1509 wxWindow* wxDataViewChoiceRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
1510 {
1511 wxChoice* c = new wxChoice
1512 (
1513 parent,
1514 wxID_ANY,
1515 labelRect.GetTopLeft(),
1516 wxSize(labelRect.GetWidth(), -1),
1517 m_choices
1518 );
1519 c->Move(labelRect.GetRight() - c->GetRect().width, wxDefaultCoord);
1520 c->SetStringSelection( value.GetString() );
1521 return c;
1522 }
1523
1524 bool wxDataViewChoiceRenderer::GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value )
1525 {
1526 wxChoice *c = (wxChoice*) editor;
1527 wxString s = c->GetStringSelection();
1528 value = s;
1529 return true;
1530 }
1531
1532 bool wxDataViewChoiceRenderer::Render( wxRect rect, wxDC *dc, int state )
1533 {
1534 RenderText( m_data, 0, rect, dc, state );
1535 return true;
1536 }
1537
1538 wxSize wxDataViewChoiceRenderer::GetSize() const
1539 {
1540 wxSize sz;
1541
1542 for ( wxArrayString::const_iterator i = m_choices.begin(); i != m_choices.end(); ++i )
1543 sz.IncTo(GetTextExtent(*i));
1544
1545 // Allow some space for the right-side button, which is approximately the
1546 // size of a scrollbar (and getting pixel-exact value would be complicated).
1547 // Also add some whitespace between the text and the button:
1548 sz.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1549 sz.x += GetTextExtent("M").x;
1550
1551 return sz;
1552 }
1553
1554 bool wxDataViewChoiceRenderer::SetValue( const wxVariant &value )
1555 {
1556 m_data = value.GetString();
1557 return true;
1558 }
1559
1560 bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const
1561 {
1562 value = m_data;
1563 return true;
1564 }
1565
1566 // ----------------------------------------------------------------------------
1567 // wxDataViewChoiceByIndexRenderer
1568 // ----------------------------------------------------------------------------
1569
1570 wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString &choices,
1571 wxDataViewCellMode mode, int alignment ) :
1572 wxDataViewChoiceRenderer( choices, mode, alignment )
1573 {
1574 }
1575
1576 wxWindow* wxDataViewChoiceByIndexRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
1577 {
1578 wxVariant string_value = GetChoice( value.GetLong() );
1579
1580 return wxDataViewChoiceRenderer::CreateEditorCtrl( parent, labelRect, string_value );
1581 }
1582
1583 bool wxDataViewChoiceByIndexRenderer::GetValueFromEditorCtrl( wxWindow* editor, wxVariant &value )
1584 {
1585 wxVariant string_value;
1586 if (!wxDataViewChoiceRenderer::GetValueFromEditorCtrl( editor, string_value ))
1587 return false;
1588
1589 value = (long) GetChoices().Index( string_value.GetString() );
1590 return true;
1591 }
1592
1593 bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant &value )
1594 {
1595 wxVariant string_value = GetChoice( value.GetLong() );
1596 return wxDataViewChoiceRenderer::SetValue( string_value );
1597 }
1598
1599 bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant &value ) const
1600 {
1601 wxVariant string_value;
1602 if (!wxDataViewChoiceRenderer::GetValue( string_value ))
1603 return false;
1604
1605 value = (long) GetChoices().Index( string_value.GetString() );
1606 return true;
1607 }
1608
1609 #endif
1610
1611 // ---------------------------------------------------------
1612 // wxDataViewDateRenderer
1613 // ---------------------------------------------------------
1614
1615 #if (defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXGTK__)) && wxUSE_DATEPICKCTRL
1616
1617 wxDataViewDateRenderer::wxDataViewDateRenderer(const wxString& varianttype,
1618 wxDataViewCellMode mode, int align)
1619 : wxDataViewCustomRenderer(varianttype, mode, align)
1620 {
1621 }
1622
1623 wxWindow *
1624 wxDataViewDateRenderer::CreateEditorCtrl(wxWindow *parent, wxRect labelRect, const wxVariant& value)
1625 {
1626 return new wxDatePickerCtrl
1627 (
1628 parent,
1629 wxID_ANY,
1630 value.GetDateTime(),
1631 labelRect.GetTopLeft(),
1632 labelRect.GetSize()
1633 );
1634 }
1635
1636 bool wxDataViewDateRenderer::GetValueFromEditorCtrl(wxWindow *editor, wxVariant& value)
1637 {
1638 wxDatePickerCtrl *ctrl = static_cast<wxDatePickerCtrl*>(editor);
1639 value = ctrl->GetValue();
1640 return true;
1641 }
1642
1643 bool wxDataViewDateRenderer::SetValue(const wxVariant& value)
1644 {
1645 m_date = value.GetDateTime();
1646 return true;
1647 }
1648
1649 bool wxDataViewDateRenderer::GetValue(wxVariant& value) const
1650 {
1651 value = m_date;
1652 return true;
1653 }
1654
1655 bool wxDataViewDateRenderer::Render(wxRect cell, wxDC* dc, int state)
1656 {
1657 wxString tmp = m_date.FormatDate();
1658 RenderText( tmp, 0, cell, dc, state );
1659 return true;
1660 }
1661
1662 wxSize wxDataViewDateRenderer::GetSize() const
1663 {
1664 return GetTextExtent(m_date.FormatDate());
1665 }
1666
1667 #endif // (defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXGTK__)) && wxUSE_DATEPICKCTRL
1668
1669 //-----------------------------------------------------------------------------
1670 // wxDataViewListStore
1671 //-----------------------------------------------------------------------------
1672
1673 wxDataViewListStore::wxDataViewListStore()
1674 {
1675 }
1676
1677 wxDataViewListStore::~wxDataViewListStore()
1678 {
1679 wxVector<wxDataViewListStoreLine*>::iterator it;
1680 for (it = m_data.begin(); it != m_data.end(); ++it)
1681 {
1682 wxDataViewListStoreLine* line = *it;
1683 delete line;
1684 }
1685 }
1686
1687 void wxDataViewListStore::PrependColumn( const wxString &varianttype )
1688 {
1689 m_cols.Insert( varianttype, 0 );
1690 }
1691
1692 void wxDataViewListStore::InsertColumn( unsigned int pos, const wxString &varianttype )
1693 {
1694 m_cols.Insert( varianttype, pos );
1695 }
1696
1697 void wxDataViewListStore::AppendColumn( const wxString &varianttype )
1698 {
1699 m_cols.Add( varianttype );
1700 }
1701
1702 unsigned int wxDataViewListStore::GetColumnCount() const
1703 {
1704 return m_cols.GetCount();
1705 }
1706
1707 unsigned int wxDataViewListStore::GetItemCount() const
1708 {
1709 return m_data.size();
1710 }
1711
1712 wxString wxDataViewListStore::GetColumnType( unsigned int pos ) const
1713 {
1714 return m_cols[pos];
1715 }
1716
1717 void wxDataViewListStore::AppendItem( const wxVector<wxVariant> &values, wxUIntPtr data )
1718 {
1719 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1720 line->m_values = values;
1721 m_data.push_back( line );
1722
1723 RowAppended();
1724 }
1725
1726 void wxDataViewListStore::PrependItem( const wxVector<wxVariant> &values, wxUIntPtr data )
1727 {
1728 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1729 line->m_values = values;
1730 m_data.insert( m_data.begin(), line );
1731
1732 RowPrepended();
1733 }
1734
1735 void wxDataViewListStore::InsertItem( unsigned int row, const wxVector<wxVariant> &values,
1736 wxUIntPtr data )
1737 {
1738 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1739 line->m_values = values;
1740 m_data.insert( m_data.begin()+row, line );
1741
1742 RowInserted( row );
1743 }
1744
1745 void wxDataViewListStore::DeleteItem( unsigned int row )
1746 {
1747 wxVector<wxDataViewListStoreLine*>::iterator it = m_data.begin() + row;
1748 delete *it;
1749 m_data.erase( it );
1750
1751 RowDeleted( row );
1752 }
1753
1754 void wxDataViewListStore::DeleteAllItems()
1755 {
1756 wxVector<wxDataViewListStoreLine*>::iterator it;
1757 for (it = m_data.begin(); it != m_data.end(); ++it)
1758 {
1759 wxDataViewListStoreLine* line = *it;
1760 delete line;
1761 }
1762
1763 m_data.clear();
1764
1765 Reset( 0 );
1766 }
1767
1768 void wxDataViewListStore::SetItemData( const wxDataViewItem& item, wxUIntPtr data )
1769 {
1770 wxDataViewListStoreLine* line = m_data[GetRow(item)];
1771 if (!line) return;
1772
1773 line->SetData( data );
1774 }
1775
1776 wxUIntPtr wxDataViewListStore::GetItemData( const wxDataViewItem& item ) const
1777 {
1778 wxDataViewListStoreLine* line = m_data[GetRow(item)];
1779 if (!line) return static_cast<wxUIntPtr>(NULL);
1780
1781 return line->GetData();
1782 }
1783
1784 void wxDataViewListStore::GetValueByRow( wxVariant &value, unsigned int row, unsigned int col ) const
1785 {
1786 wxDataViewListStoreLine *line = m_data[row];
1787 value = line->m_values[col];
1788 }
1789
1790 bool wxDataViewListStore::SetValueByRow( const wxVariant &value, unsigned int row, unsigned int col )
1791 {
1792 wxDataViewListStoreLine *line = m_data[row];
1793 line->m_values[col] = value;
1794
1795 return true;
1796 }
1797
1798 //-----------------------------------------------------------------------------
1799 // wxDataViewListCtrl
1800 //-----------------------------------------------------------------------------
1801
1802 IMPLEMENT_DYNAMIC_CLASS(wxDataViewListCtrl,wxDataViewCtrl)
1803
1804 BEGIN_EVENT_TABLE(wxDataViewListCtrl,wxDataViewCtrl)
1805 EVT_SIZE( wxDataViewListCtrl::OnSize )
1806 END_EVENT_TABLE()
1807
1808 wxDataViewListCtrl::wxDataViewListCtrl()
1809 {
1810 }
1811
1812 wxDataViewListCtrl::wxDataViewListCtrl( wxWindow *parent, wxWindowID id,
1813 const wxPoint& pos, const wxSize& size, long style,
1814 const wxValidator& validator )
1815 {
1816 Create( parent, id, pos, size, style, validator );
1817 }
1818
1819 wxDataViewListCtrl::~wxDataViewListCtrl()
1820 {
1821 }
1822
1823
1824 bool wxDataViewListCtrl::Create( wxWindow *parent, wxWindowID id,
1825 const wxPoint& pos, const wxSize& size, long style,
1826 const wxValidator& validator )
1827 {
1828 if ( !wxDataViewCtrl::Create( parent, id, pos, size, style, validator ) )
1829 return false;
1830
1831 wxDataViewListStore *store = new wxDataViewListStore;
1832 AssociateModel( store );
1833 store->DecRef();
1834
1835 return true;
1836 }
1837
1838 bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *column, const wxString &varianttype )
1839 {
1840 GetStore()->AppendColumn( varianttype );
1841 return wxDataViewCtrl::AppendColumn( column );
1842 }
1843
1844 bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *column, const wxString &varianttype )
1845 {
1846 GetStore()->PrependColumn( varianttype );
1847 return wxDataViewCtrl::PrependColumn( column );
1848 }
1849
1850 bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *column, const wxString &varianttype )
1851 {
1852 GetStore()->InsertColumn( pos, varianttype );
1853 return wxDataViewCtrl::InsertColumn( pos, column );
1854 }
1855
1856 bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *col )
1857 {
1858 return PrependColumn( col, "string" );
1859 }
1860
1861 bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
1862 {
1863 return InsertColumn( pos, col, "string" );
1864 }
1865
1866 bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *col )
1867 {
1868 return AppendColumn( col, "string" );
1869 }
1870
1871 wxDataViewColumn *wxDataViewListCtrl::AppendTextColumn( const wxString &label,
1872 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1873 {
1874 GetStore()->AppendColumn( wxT("string") );
1875
1876 wxDataViewColumn *ret = new wxDataViewColumn( label,
1877 new wxDataViewTextRenderer( wxT("string"), mode ),
1878 GetStore()->GetColumnCount()-1, width, align, flags );
1879
1880 wxDataViewCtrl::AppendColumn( ret );
1881
1882 return ret;
1883 }
1884
1885 wxDataViewColumn *wxDataViewListCtrl::AppendToggleColumn( const wxString &label,
1886 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1887 {
1888 GetStore()->AppendColumn( wxT("bool") );
1889
1890 wxDataViewColumn *ret = new wxDataViewColumn( label,
1891 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1892 GetStore()->GetColumnCount()-1, width, align, flags );
1893
1894 wxDataViewCtrl::AppendColumn( ret );
1895
1896 return ret;
1897 }
1898
1899 wxDataViewColumn *wxDataViewListCtrl::AppendProgressColumn( const wxString &label,
1900 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1901 {
1902 GetStore()->AppendColumn( wxT("long") );
1903
1904 wxDataViewColumn *ret = new wxDataViewColumn( label,
1905 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1906 GetStore()->GetColumnCount()-1, width, align, flags );
1907
1908 wxDataViewCtrl::AppendColumn( ret );
1909
1910 return ret;
1911 }
1912
1913 wxDataViewColumn *wxDataViewListCtrl::AppendIconTextColumn( const wxString &label,
1914 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1915 {
1916 GetStore()->AppendColumn( wxT("wxDataViewIconText") );
1917
1918 wxDataViewColumn *ret = new wxDataViewColumn( label,
1919 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1920 GetStore()->GetColumnCount()-1, width, align, flags );
1921
1922 wxDataViewCtrl::AppendColumn( ret );
1923
1924 return ret;
1925 }
1926
1927 void wxDataViewListCtrl::OnSize( wxSizeEvent &event )
1928 {
1929 event.Skip( true );
1930 }
1931
1932 //-----------------------------------------------------------------------------
1933 // wxDataViewTreeStore
1934 //-----------------------------------------------------------------------------
1935
1936 wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1937 wxDataViewTreeStoreNode *parent,
1938 const wxString &text, const wxIcon &icon, wxClientData *data )
1939 {
1940 m_parent = parent;
1941 m_text = text;
1942 m_icon = icon;
1943 m_data = data;
1944 }
1945
1946 wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1947 {
1948 if (m_data)
1949 delete m_data;
1950 }
1951
1952 #include "wx/listimpl.cpp"
1953 WX_DEFINE_LIST(wxDataViewTreeStoreNodeList)
1954
1955 wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1956 wxDataViewTreeStoreNode *parent, const wxString &text,
1957 const wxIcon &icon, const wxIcon &expanded, wxClientData *data ) :
1958 wxDataViewTreeStoreNode( parent, text, icon, data )
1959 {
1960 m_iconExpanded = expanded;
1961 m_isExpanded = false;
1962 m_children.DeleteContents(true);
1963 }
1964
1965 wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1966 {
1967 }
1968
1969 //-----------------------------------------------------------------------------
1970
1971 wxDataViewTreeStore::wxDataViewTreeStore()
1972 {
1973 m_root = new wxDataViewTreeStoreContainerNode( NULL, wxEmptyString );
1974 }
1975
1976 wxDataViewTreeStore::~wxDataViewTreeStore()
1977 {
1978 delete m_root;
1979 }
1980
1981 wxDataViewItem wxDataViewTreeStore::AppendItem( const wxDataViewItem& parent,
1982 const wxString &text, const wxIcon &icon, wxClientData *data )
1983 {
1984 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1985 if (!parent_node) return wxDataViewItem(0);
1986
1987 wxDataViewTreeStoreNode *node =
1988 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1989 parent_node->GetChildren().Append( node );
1990
1991 return node->GetItem();
1992 }
1993
1994 wxDataViewItem wxDataViewTreeStore::PrependItem( const wxDataViewItem& parent,
1995 const wxString &text, const wxIcon &icon, wxClientData *data )
1996 {
1997 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1998 if (!parent_node) return wxDataViewItem(0);
1999
2000 wxDataViewTreeStoreNode *node =
2001 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
2002 parent_node->GetChildren().Insert( node );
2003
2004 return node->GetItem();
2005 }
2006
2007 wxDataViewItem
2008 wxDataViewTreeStore::InsertItem(const wxDataViewItem& parent,
2009 const wxDataViewItem& previous,
2010 const wxString& text,
2011 const wxIcon& icon,
2012 wxClientData *data)
2013 {
2014 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
2015 if (!parent_node) return wxDataViewItem(0);
2016
2017 wxDataViewTreeStoreNode *previous_node = FindNode( previous );
2018 int pos = parent_node->GetChildren().IndexOf( previous_node );
2019 if (pos == wxNOT_FOUND) return wxDataViewItem(0);
2020
2021 wxDataViewTreeStoreNode *node =
2022 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
2023 parent_node->GetChildren().Insert( (size_t) pos, node );
2024
2025 return node->GetItem();
2026 }
2027
2028 wxDataViewItem wxDataViewTreeStore::PrependContainer( const wxDataViewItem& parent,
2029 const wxString &text, const wxIcon &icon, const wxIcon &expanded,
2030 wxClientData *data )
2031 {
2032 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
2033 if (!parent_node) return wxDataViewItem(0);
2034
2035 wxDataViewTreeStoreContainerNode *node =
2036 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
2037 parent_node->GetChildren().Insert( node );
2038
2039 return node->GetItem();
2040 }
2041
2042 wxDataViewItem
2043 wxDataViewTreeStore::AppendContainer(const wxDataViewItem& parent,
2044 const wxString &text,
2045 const wxIcon& icon,
2046 const wxIcon& expanded,
2047 wxClientData * data)
2048 {
2049 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
2050 if (!parent_node) return wxDataViewItem(0);
2051
2052 wxDataViewTreeStoreContainerNode *node =
2053 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
2054 parent_node->GetChildren().Append( node );
2055
2056 return node->GetItem();
2057 }
2058
2059 wxDataViewItem
2060 wxDataViewTreeStore::InsertContainer(const wxDataViewItem& parent,
2061 const wxDataViewItem& previous,
2062 const wxString& text,
2063 const wxIcon& icon,
2064 const wxIcon& expanded,
2065 wxClientData * data)
2066 {
2067 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
2068 if (!parent_node) return wxDataViewItem(0);
2069
2070 wxDataViewTreeStoreNode *previous_node = FindNode( previous );
2071 int pos = parent_node->GetChildren().IndexOf( previous_node );
2072 if (pos == wxNOT_FOUND) return wxDataViewItem(0);
2073
2074 wxDataViewTreeStoreContainerNode *node =
2075 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
2076 parent_node->GetChildren().Insert( (size_t) pos, node );
2077
2078 return node->GetItem();
2079 }
2080
2081 bool wxDataViewTreeStore::IsContainer( const wxDataViewItem& item ) const
2082 {
2083 wxDataViewTreeStoreNode *node = FindNode( item );
2084 if (!node) return false;
2085
2086 return node->IsContainer();
2087 }
2088
2089 wxDataViewItem wxDataViewTreeStore::GetNthChild( const wxDataViewItem& parent, unsigned int pos ) const
2090 {
2091 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
2092 if (!parent_node) return wxDataViewItem(0);
2093
2094 wxDataViewTreeStoreNodeList::compatibility_iterator node = parent_node->GetChildren().Item( pos );
2095 if (node)
2096 return wxDataViewItem(node->GetData());
2097
2098 return wxDataViewItem(0);
2099 }
2100
2101 int wxDataViewTreeStore::GetChildCount( const wxDataViewItem& parent ) const
2102 {
2103 wxDataViewTreeStoreNode *node = FindNode( parent );
2104 if (!node) return -1;
2105
2106 if (!node->IsContainer())
2107 return 0;
2108
2109 wxDataViewTreeStoreContainerNode *container_node = (wxDataViewTreeStoreContainerNode*) node;
2110 return (int) container_node->GetChildren().GetCount();
2111 }
2112
2113 void wxDataViewTreeStore::SetItemText( const wxDataViewItem& item, const wxString &text )
2114 {
2115 wxDataViewTreeStoreNode *node = FindNode( item );
2116 if (!node) return;
2117
2118 node->SetText( text );
2119 }
2120
2121 wxString wxDataViewTreeStore::GetItemText( const wxDataViewItem& item ) const
2122 {
2123 wxDataViewTreeStoreNode *node = FindNode( item );
2124 if (!node) return wxEmptyString;
2125
2126 return node->GetText();
2127 }
2128
2129 void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
2130 {
2131 wxDataViewTreeStoreNode *node = FindNode( item );
2132 if (!node) return;
2133
2134 node->SetIcon( icon );
2135 }
2136
2137 const wxIcon &wxDataViewTreeStore::GetItemIcon( const wxDataViewItem& item ) const
2138 {
2139 wxDataViewTreeStoreNode *node = FindNode( item );
2140 if (!node) return wxNullIcon;
2141
2142 return node->GetIcon();
2143 }
2144
2145 void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
2146 {
2147 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
2148 if (!node) return;
2149
2150 node->SetExpandedIcon( icon );
2151 }
2152
2153 const wxIcon &wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem& item ) const
2154 {
2155 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
2156 if (!node) return wxNullIcon;
2157
2158 return node->GetExpandedIcon();
2159 }
2160
2161 void wxDataViewTreeStore::SetItemData( const wxDataViewItem& item, wxClientData *data )
2162 {
2163 wxDataViewTreeStoreNode *node = FindNode( item );
2164 if (!node) return;
2165
2166 node->SetData( data );
2167 }
2168
2169 wxClientData *wxDataViewTreeStore::GetItemData( const wxDataViewItem& item ) const
2170 {
2171 wxDataViewTreeStoreNode *node = FindNode( item );
2172 if (!node) return NULL;
2173
2174 return node->GetData();
2175 }
2176
2177 void wxDataViewTreeStore::DeleteItem( const wxDataViewItem& item )
2178 {
2179 if (!item.IsOk()) return;
2180
2181 wxDataViewItem parent_item = GetParent( item );
2182
2183 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent_item );
2184 if (!parent_node) return;
2185
2186 parent_node->GetChildren().DeleteObject( FindNode(item) );
2187 }
2188
2189 void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem& item )
2190 {
2191 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
2192 if (!node) return;
2193
2194 node->GetChildren().clear();
2195 }
2196
2197 void wxDataViewTreeStore::DeleteAllItems()
2198 {
2199 DeleteChildren(wxDataViewItem(m_root));
2200 }
2201
2202 void
2203 wxDataViewTreeStore::GetValue(wxVariant &variant,
2204 const wxDataViewItem &item,
2205 unsigned int WXUNUSED(col)) const
2206 {
2207 // if (col != 0) return;
2208
2209 wxDataViewTreeStoreNode *node = FindNode( item );
2210 if (!node) return;
2211
2212 wxIcon icon( node->GetIcon());
2213 if (node->IsContainer())
2214 {
2215 wxDataViewTreeStoreContainerNode *container = (wxDataViewTreeStoreContainerNode*) node;
2216 if (container->IsExpanded() && container->GetExpandedIcon().IsOk())
2217 icon = container->GetExpandedIcon();
2218 }
2219
2220 wxDataViewIconText data( node->GetText(), icon );
2221
2222 variant << data;
2223 }
2224
2225 bool
2226 wxDataViewTreeStore::SetValue(const wxVariant& variant,
2227 const wxDataViewItem& item,
2228 unsigned int WXUNUSED(col))
2229 {
2230 // if (col != 0) return false;
2231
2232 wxDataViewTreeStoreNode *node = FindNode( item );
2233 if (!node) return false;
2234
2235 wxDataViewIconText data;
2236
2237 data << variant;
2238
2239 node->SetText( data.GetText() );
2240 node->SetIcon( data.GetIcon() );
2241
2242 return true;
2243 }
2244
2245 wxDataViewItem wxDataViewTreeStore::GetParent( const wxDataViewItem &item ) const
2246 {
2247 wxDataViewTreeStoreNode *node = FindNode( item );
2248 if (!node) return wxDataViewItem(0);
2249
2250 wxDataViewTreeStoreNode *parent = node->GetParent();
2251 if (!parent) return wxDataViewItem(0);
2252
2253 if (parent == m_root)
2254 return wxDataViewItem(0);
2255
2256 return parent->GetItem();
2257 }
2258
2259 unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
2260 {
2261 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
2262 if (!node) return 0;
2263
2264 wxDataViewTreeStoreNodeList::iterator iter;
2265 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
2266 {
2267 wxDataViewTreeStoreNode* child = *iter;
2268 children.Add( child->GetItem() );
2269 }
2270
2271 return node->GetChildren().GetCount();
2272 }
2273
2274 int wxDataViewTreeStore::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
2275 unsigned int WXUNUSED(column), bool WXUNUSED(ascending) ) const
2276 {
2277 wxDataViewTreeStoreNode *node1 = FindNode( item1 );
2278 wxDataViewTreeStoreNode *node2 = FindNode( item2 );
2279
2280 if (!node1 || !node2)
2281 return 0;
2282
2283 wxDataViewTreeStoreContainerNode* parent1 =
2284 (wxDataViewTreeStoreContainerNode*) node1->GetParent();
2285 wxDataViewTreeStoreContainerNode* parent2 =
2286 (wxDataViewTreeStoreContainerNode*) node2->GetParent();
2287
2288 if (parent1 != parent2)
2289 {
2290 wxLogError( wxT("Comparing items with different parent.") );
2291 return 0;
2292 }
2293
2294 if (node1->IsContainer() && !node2->IsContainer())
2295 return -1;
2296
2297 if (node2->IsContainer() && !node1->IsContainer())
2298 return 1;
2299
2300 return parent1->GetChildren().IndexOf( node1 ) - parent2->GetChildren().IndexOf( node2 );
2301 }
2302
2303 wxDataViewTreeStoreNode *wxDataViewTreeStore::FindNode( const wxDataViewItem &item ) const
2304 {
2305 if (!item.IsOk())
2306 return m_root;
2307
2308 return (wxDataViewTreeStoreNode*) item.GetID();
2309 }
2310
2311 wxDataViewTreeStoreContainerNode *wxDataViewTreeStore::FindContainerNode( const wxDataViewItem &item ) const
2312 {
2313 if (!item.IsOk())
2314 return (wxDataViewTreeStoreContainerNode*) m_root;
2315
2316 wxDataViewTreeStoreNode* node = (wxDataViewTreeStoreNode*) item.GetID();
2317
2318 if (!node->IsContainer())
2319 return NULL;
2320
2321 return (wxDataViewTreeStoreContainerNode*) node;
2322 }
2323
2324 //-----------------------------------------------------------------------------
2325 // wxDataViewTreeCtrl
2326 //-----------------------------------------------------------------------------
2327
2328 IMPLEMENT_DYNAMIC_CLASS(wxDataViewTreeCtrl,wxDataViewCtrl)
2329
2330 BEGIN_EVENT_TABLE(wxDataViewTreeCtrl,wxDataViewCtrl)
2331 EVT_DATAVIEW_ITEM_EXPANDED(-1, wxDataViewTreeCtrl::OnExpanded)
2332 EVT_DATAVIEW_ITEM_COLLAPSED(-1, wxDataViewTreeCtrl::OnCollapsed)
2333 EVT_SIZE( wxDataViewTreeCtrl::OnSize )
2334 END_EVENT_TABLE()
2335
2336 bool wxDataViewTreeCtrl::Create( wxWindow *parent, wxWindowID id,
2337 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
2338 {
2339 if ( !wxDataViewCtrl::Create( parent, id, pos, size, style, validator ) )
2340 return false;
2341
2342 // create the standard model and a column in the tree
2343 wxDataViewTreeStore *store = new wxDataViewTreeStore;
2344 AssociateModel( store );
2345 store->DecRef();
2346
2347 AppendIconTextColumn
2348 (
2349 wxString(), // no label (header is not shown anyhow)
2350 0, // the only model column
2351 wxDATAVIEW_CELL_EDITABLE,
2352 -1, // default width
2353 wxALIGN_NOT, // and alignment
2354 0 // not resizable
2355 );
2356
2357 return true;
2358 }
2359
2360 wxDataViewItem wxDataViewTreeCtrl::AppendItem( const wxDataViewItem& parent,
2361 const wxString &text, int iconIndex, wxClientData *data )
2362 {
2363 wxDataViewItem res = GetStore()->
2364 AppendItem( parent, text, GetImage(iconIndex), data );
2365
2366 GetStore()->ItemAdded( parent, res );
2367
2368 return res;
2369 }
2370
2371 wxDataViewItem wxDataViewTreeCtrl::PrependItem( const wxDataViewItem& parent,
2372 const wxString &text, int iconIndex, wxClientData *data )
2373 {
2374 wxDataViewItem res = GetStore()->
2375 PrependItem( parent, text, GetImage(iconIndex), data );
2376
2377 GetStore()->ItemAdded( parent, res );
2378
2379 return res;
2380 }
2381
2382 wxDataViewItem wxDataViewTreeCtrl::InsertItem( const wxDataViewItem& parent, const wxDataViewItem& previous,
2383 const wxString &text, int iconIndex, wxClientData *data )
2384 {
2385 wxDataViewItem res = GetStore()->
2386 InsertItem( parent, previous, text, GetImage(iconIndex), data );
2387
2388 GetStore()->ItemAdded( parent, res );
2389
2390 return res;
2391 }
2392
2393 wxDataViewItem wxDataViewTreeCtrl::PrependContainer( const wxDataViewItem& parent,
2394 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2395 {
2396 wxDataViewItem res = GetStore()->
2397 PrependContainer( parent, text,
2398 GetImage(iconIndex), GetImage(expandedIndex), data );
2399
2400 GetStore()->ItemAdded( parent, res );
2401
2402 return res;
2403 }
2404
2405 wxDataViewItem wxDataViewTreeCtrl::AppendContainer( const wxDataViewItem& parent,
2406 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2407 {
2408 wxDataViewItem res = GetStore()->
2409 AppendContainer( parent, text,
2410 GetImage(iconIndex), GetImage(expandedIndex), data );
2411
2412 GetStore()->ItemAdded( parent, res );
2413
2414 return res;
2415 }
2416
2417 wxDataViewItem wxDataViewTreeCtrl::InsertContainer( const wxDataViewItem& parent, const wxDataViewItem& previous,
2418 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2419 {
2420 wxDataViewItem res = GetStore()->
2421 InsertContainer( parent, previous, text,
2422 GetImage(iconIndex), GetImage(expandedIndex), data );
2423
2424 GetStore()->ItemAdded( parent, res );
2425
2426 return res;
2427 }
2428
2429 void wxDataViewTreeCtrl::SetItemText( const wxDataViewItem& item, const wxString &text )
2430 {
2431 GetStore()->SetItemText(item,text);
2432
2433 // notify control
2434 GetStore()->ValueChanged( item, 0 );
2435 }
2436
2437 void wxDataViewTreeCtrl::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
2438 {
2439 GetStore()->SetItemIcon(item,icon);
2440
2441 // notify control
2442 GetStore()->ValueChanged( item, 0 );
2443 }
2444
2445 void wxDataViewTreeCtrl::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
2446 {
2447 GetStore()->SetItemExpandedIcon(item,icon);
2448
2449 // notify control
2450 GetStore()->ValueChanged( item, 0 );
2451 }
2452
2453 void wxDataViewTreeCtrl::DeleteItem( const wxDataViewItem& item )
2454 {
2455 wxDataViewItem parent_item = GetStore()->GetParent( item );
2456
2457 GetStore()->DeleteItem(item);
2458
2459 // notify control
2460 GetStore()->ItemDeleted( parent_item, item );
2461 }
2462
2463 void wxDataViewTreeCtrl::DeleteChildren( const wxDataViewItem& item )
2464 {
2465 wxDataViewTreeStoreContainerNode *node = GetStore()->FindContainerNode( item );
2466 if (!node) return;
2467
2468 wxDataViewItemArray array;
2469 wxDataViewTreeStoreNodeList::iterator iter;
2470 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
2471 {
2472 wxDataViewTreeStoreNode* child = *iter;
2473 array.Add( child->GetItem() );
2474 }
2475
2476 GetStore()->DeleteChildren( item );
2477
2478 // notify control
2479 GetStore()->ItemsDeleted( item, array );
2480 }
2481
2482 void wxDataViewTreeCtrl::DeleteAllItems()
2483 {
2484 GetStore()->DeleteAllItems();
2485
2486 GetStore()->Cleared();
2487 }
2488
2489 void wxDataViewTreeCtrl::OnExpanded( wxDataViewEvent &event )
2490 {
2491 if (HasImageList()) return;
2492
2493 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
2494 if (!container) return;
2495
2496 container->SetExpanded( true );
2497
2498 GetStore()->ItemChanged( event.GetItem() );
2499 }
2500
2501 void wxDataViewTreeCtrl::OnCollapsed( wxDataViewEvent &event )
2502 {
2503 if (HasImageList()) return;
2504
2505 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
2506 if (!container) return;
2507
2508 container->SetExpanded( false );
2509
2510 GetStore()->ItemChanged( event.GetItem() );
2511 }
2512
2513 void wxDataViewTreeCtrl::OnSize( wxSizeEvent &event )
2514 {
2515 #if defined(wxUSE_GENERICDATAVIEWCTRL)
2516 // automatically resize our only column to take the entire control width
2517 if ( GetColumnCount() )
2518 {
2519 wxSize size = GetClientSize();
2520 GetColumn(0)->SetWidth(size.x);
2521 }
2522 #endif
2523 event.Skip( true );
2524 }
2525
2526 #endif // wxUSE_DATAVIEWCTRL
2527