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