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