]> git.saurik.com Git - wxWidgets.git/blob - src/common/datavcmn.cpp
Update OpenVMS setup
[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/log.h"
24 #endif
25
26 const wxChar wxDataViewCtrlNameStr[] = wxT("dataviewCtrl");
27
28
29 // ---------------------------------------------------------
30 // wxDataViewListModel
31 // ---------------------------------------------------------
32
33 wxDataViewListModel::wxDataViewListModel()
34 {
35 m_notifiers.DeleteContents( true );
36 }
37
38 wxDataViewListModel::~wxDataViewListModel()
39 {
40 }
41
42 bool wxDataViewListModel::RowAppended()
43 {
44 bool ret = true;
45
46 wxList::compatibility_iterator node = m_notifiers.GetFirst();
47 while (node)
48 {
49 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
50 if (!notifier->RowAppended())
51 ret = false;
52 node = node->GetNext();
53 }
54
55 return ret;
56 }
57
58 bool wxDataViewListModel::RowPrepended()
59 {
60 bool ret = true;
61
62 wxList::compatibility_iterator node = m_notifiers.GetFirst();
63 while (node)
64 {
65 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
66 if (!notifier->RowPrepended())
67 ret = false;
68 node = node->GetNext();
69 }
70
71 return ret;
72 }
73
74 bool wxDataViewListModel::RowInserted( unsigned int before )
75 {
76 bool ret = true;
77
78 wxList::compatibility_iterator node = m_notifiers.GetFirst();
79 while (node)
80 {
81 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
82 if (!notifier->RowInserted(before))
83 ret = false;
84 node = node->GetNext();
85 }
86
87 return ret;
88 }
89
90 bool wxDataViewListModel::RowDeleted( unsigned int row )
91 {
92 bool ret = true;
93
94 wxList::compatibility_iterator node = m_notifiers.GetFirst();
95 while (node)
96 {
97 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
98 if (!notifier->RowDeleted( row ))
99 ret = false;
100 node = node->GetNext();
101 }
102
103 return ret;
104 }
105
106 bool wxDataViewListModel::RowChanged( unsigned int row )
107 {
108 bool ret = true;
109
110 wxList::compatibility_iterator node = m_notifiers.GetFirst();
111 while (node)
112 {
113 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
114 if (!notifier->RowChanged( row ))
115 ret = false;
116 node = node->GetNext();
117 }
118
119 return ret;
120 }
121
122 bool wxDataViewListModel::ValueChanged( unsigned int col, unsigned int row )
123 {
124 bool ret = true;
125
126 wxList::compatibility_iterator node = m_notifiers.GetFirst();
127 while (node)
128 {
129 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
130 if (!notifier->ValueChanged( col, row ))
131 ret = false;
132 node = node->GetNext();
133 }
134
135 return ret;
136 }
137
138 bool wxDataViewListModel::RowsReordered( unsigned int *new_order )
139 {
140 bool ret = true;
141
142 wxList::compatibility_iterator node = m_notifiers.GetFirst();
143 while (node)
144 {
145 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
146 if (!notifier->RowsReordered( new_order ))
147 ret = false;
148 node = node->GetNext();
149 }
150
151 return ret;
152 }
153
154 bool wxDataViewListModel::Cleared()
155 {
156 bool ret = true;
157
158 wxList::compatibility_iterator node = m_notifiers.GetFirst();
159 while (node)
160 {
161 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
162 if (!notifier->Cleared())
163 ret = false;
164 node = node->GetNext();
165 }
166
167 return ret;
168 }
169
170 void wxDataViewListModel::AddNotifier( wxDataViewListModelNotifier *notifier )
171 {
172 m_notifiers.Append( notifier );
173 notifier->SetOwner( this );
174 }
175
176 void wxDataViewListModel::RemoveNotifier( wxDataViewListModelNotifier *notifier )
177 {
178 m_notifiers.DeleteObject( notifier );
179 }
180
181 // ---------------------------------------------------------
182 // wxDataViewSortedListModelNotifier
183 // ---------------------------------------------------------
184
185 class wxDataViewSortedListModelNotifier: public wxDataViewListModelNotifier
186 {
187 public:
188 wxDataViewSortedListModelNotifier( wxDataViewSortedListModel *model )
189 { m_model = model; }
190
191 virtual bool RowAppended()
192 { return m_model->ChildRowAppended(); }
193
194 virtual bool RowPrepended()
195 { return m_model->ChildRowPrepended(); }
196
197 virtual bool RowInserted( unsigned int before )
198 { return m_model->ChildRowInserted( before ); }
199
200 virtual bool RowDeleted( unsigned int row )
201 { return m_model->ChildRowDeleted( row ); }
202
203 virtual bool RowChanged( unsigned int row )
204 { return m_model->ChildRowChanged( row ); }
205
206 virtual bool ValueChanged( unsigned int col, unsigned int row )
207 { return m_model->ChildValueChanged( col, row); }
208
209 virtual bool RowsReordered( unsigned int *new_order )
210 { return m_model->ChildRowsReordered( new_order ); }
211
212 virtual bool Cleared()
213 { return m_model->ChildCleared(); }
214
215 wxDataViewSortedListModel *m_model;
216 };
217
218 // ---------------------------------------------------------
219 // wxDataViewSortedListModel compare function
220 // ---------------------------------------------------------
221
222 int wxCALLBACK wxDataViewListModelSortedDefaultCompare
223 (unsigned int row1, unsigned int row2, unsigned int col, wxDataViewListModel* model )
224 {
225 wxVariant value1,value2;
226 model->GetValue( value1, col, row1 );
227 model->GetValue( value2, col, row2 );
228 if (value1.GetType() == wxT("string"))
229 {
230 wxString str1 = value1.GetString();
231 wxString str2 = value2.GetString();
232 return str1.Cmp( str2 );
233 }
234 if (value1.GetType() == wxT("long"))
235 {
236 long l1 = value1.GetLong();
237 long l2 = value2.GetLong();
238 return l1-l2;
239 }
240 if (value1.GetType() == wxT("double"))
241 {
242 double d1 = value1.GetDouble();
243 double d2 = value2.GetDouble();
244 if (d1 == d2) return 0;
245 if (d1 < d2) return 1;
246 return -1;
247 }
248 if (value1.GetType() == wxT("datetime"))
249 {
250 wxDateTime dt1 = value1.GetDateTime();
251 wxDateTime dt2 = value2.GetDateTime();
252 if (dt1.IsEqualTo(dt2)) return 0;
253 if (dt1.IsEarlierThan(dt2)) return 1;
254 return -1;
255 }
256
257 return 0;
258 }
259
260 int wxCALLBACK wxDataViewListModelSortedDefaultCompareDescending
261 (unsigned int row1, unsigned int row2, unsigned int col, wxDataViewListModel* model )
262 {
263 return wxDataViewListModelSortedDefaultCompare( row2, row1, col, model );
264 }
265
266 static wxDataViewListModelCompare s_CmpFunc;
267 static wxDataViewListModel *s_CmpModel;
268 static unsigned int s_CmpCol;
269
270 int LINKAGEMODE wxDataViewIntermediateCmp( unsigned int row1, unsigned int row2 )
271 {
272 return s_CmpFunc( row1, row2, s_CmpCol, s_CmpModel );
273 }
274
275 // ---------------------------------------------------------
276 // wxDataViewSortedListModel
277 // ---------------------------------------------------------
278
279 wxDataViewSortedListModel::wxDataViewSortedListModel( wxDataViewListModel *child ) :
280 m_array( wxDataViewIntermediateCmp )
281 {
282 m_child = child;
283
284 m_ascending = true;
285
286 m_notifierOnChild = new wxDataViewSortedListModelNotifier( this );
287 m_child->AddNotifier( m_notifierOnChild );
288
289 m_child->IncRef();
290
291 Resort();
292 }
293
294 wxDataViewSortedListModel::~wxDataViewSortedListModel()
295 {
296 DetachChild();
297 }
298
299 void wxDataViewSortedListModel::DetachChild()
300 {
301 if (m_child)
302 {
303 m_child->RemoveNotifier( m_notifierOnChild );
304 m_child->DecRef();
305 }
306
307 m_child = NULL;
308 }
309
310 // FIXME
311 void wxDataViewSortedListModel::InitStatics()
312 {
313 s_CmpCol = 0;
314 s_CmpModel = m_child;
315 if (m_ascending)
316 s_CmpFunc = wxDataViewListModelSortedDefaultCompare;
317 else
318 s_CmpFunc = wxDataViewListModelSortedDefaultCompareDescending;
319 }
320
321 void wxDataViewSortedListModel::Resort()
322 {
323 InitStatics();
324
325 if (!m_child) return;
326
327 unsigned int n = m_child->GetRowCount();
328
329 unsigned int i;
330 if (n != m_array.GetCount())
331 {
332 // probably sorted for the first time -> no reordered
333 // -- just create index and leave
334 m_array.Clear();
335 for (i = 0; i < n; i++)
336 m_array.Add( i );
337 return;
338 }
339
340 unsigned int *old_array = new unsigned int[ n ];
341
342 for (i = 0; i < n; i++)
343 old_array[i] = m_array[ i ];
344
345 m_array.Clear();
346 for (i = 0; i < n; i++)
347 m_array.Add( i );
348
349 unsigned int *order = new unsigned int[ n ];
350
351 for (i = 0; i < n; i++)
352 {
353 unsigned int new_value = m_array[i];
354
355 unsigned int old_pos;
356 for (old_pos = 0; old_pos < n; old_pos++)
357 if (old_array[old_pos] == new_value)
358 break;
359 order[i] = old_pos;
360 }
361
362 delete [] old_array;
363
364 wxDataViewListModel::RowsReordered( order );
365
366 delete [] order;
367 }
368
369 #if 0
370 static void Dump( wxDataViewListModel *model, unsigned int col )
371 {
372 unsigned int n = model->GetRowCount();
373 unsigned int i;
374 for (i = 0; i < n; i++)
375 {
376 wxVariant variant;
377 model->GetValue( variant, col, i );
378 wxString tmp;
379 tmp = variant.GetString();
380 wxPrintf( wxT("%d: %s\n"), (int) i, tmp.c_str() );
381 }
382 }
383 #endif
384
385 bool wxDataViewSortedListModel::ChildRowAppended()
386 {
387 // no need to fix up array
388
389 unsigned int len = m_array.GetCount();
390
391 unsigned int pos = m_array.Add( len );
392
393 if (pos == 0)
394 return wxDataViewListModel::RowPrepended();
395
396 if (pos == len)
397 return wxDataViewListModel::RowAppended();
398
399 return wxDataViewListModel::RowInserted( pos );
400 }
401
402 bool wxDataViewSortedListModel::ChildRowPrepended()
403 {
404 // fix up array
405 unsigned int i;
406 unsigned int len = m_array.GetCount();
407 for (i = 0; i < len; i++)
408 {
409 unsigned int value = m_array[i];
410 m_array[i] = value+1;
411 }
412
413 unsigned int pos = m_array.Add( 0 );
414
415 if (pos == 0)
416 return wxDataViewListModel::RowPrepended();
417
418 if (pos == len)
419 return wxDataViewListModel::RowAppended();
420
421 return wxDataViewListModel::RowInserted( pos );
422 }
423
424 bool wxDataViewSortedListModel::ChildRowInserted( unsigned int before )
425 {
426 // fix up array
427 unsigned int i;
428 unsigned int len = m_array.GetCount();
429 for (i = 0; i < len; i++)
430 {
431 unsigned int value = m_array[i];
432 if (value >= before)
433 m_array[i] = value+1;
434 }
435
436 unsigned int pos = m_array.Add( before );
437
438 if (pos == 0)
439 return wxDataViewListModel::RowPrepended();
440
441 if (pos == len)
442 return wxDataViewListModel::RowAppended();
443
444 return wxDataViewListModel::RowInserted( pos );
445 }
446
447 bool wxDataViewSortedListModel::ChildRowDeleted( unsigned int row )
448 {
449 unsigned int i;
450 unsigned int len = m_array.GetCount();
451 int pos = -1;
452 for (i = 0; i < len; i++)
453 {
454 unsigned int value = m_array[i];
455 if (value == row)
456 {
457 // delete later
458 pos = (int) i;
459 }
460 else
461 {
462 // Fix up array
463 if (value > row)
464 m_array[i] = value-1;
465 }
466 }
467
468 if (pos == -1)
469 return false; // we should probably assert
470
471 // remove
472 m_array.RemoveAt( (unsigned int) pos );
473
474 return wxDataViewListModel::RowDeleted( (unsigned int) pos);
475 }
476
477 bool wxDataViewSortedListModel::ChildRowChanged( unsigned int row )
478 {
479 unsigned int i;
480 unsigned int len = m_array.GetCount();
481
482 // Remove and readd sorted. Find out at which
483 // position it was and where it ended.
484 unsigned int start_pos = 0,end_pos = 0;
485 for (i = 0; i < len; i++)
486 if (m_array[i] == row)
487 {
488 start_pos = i;
489 break;
490 }
491 m_array.RemoveAt( start_pos );
492 m_array.Add( row );
493
494 for (i = 0; i < len; i++)
495 if (m_array[i] == row)
496 {
497 end_pos = i;
498 break;
499 }
500
501 if (end_pos == start_pos)
502 return wxDataViewListModel::RowChanged( start_pos );
503
504 // Create an array where order[old] -> new_pos, so that
505 // if nothing changed order[0] -> 0 etc.
506 unsigned int *order = new unsigned int[ len ];
507 // Fill up initial values.
508 for (i = 0; i < len; i++)
509 order[i] = i;
510
511 if (start_pos < end_pos)
512 {
513 for (i = start_pos; i < end_pos; i++)
514 order[i] = order[i+1];
515 order[end_pos] = start_pos;
516 }
517 else
518 {
519 for (i = end_pos; i > start_pos; i--)
520 order[i] = order[i-1];
521 order[start_pos] = end_pos;
522 }
523
524 wxDataViewListModel::RowsReordered( order );
525
526 delete [] order;
527
528 return true;
529 }
530
531 bool wxDataViewSortedListModel::ChildValueChanged( unsigned int col, unsigned int row )
532 {
533 unsigned int i;
534 unsigned int len = m_array.GetCount();
535
536 // Remove and readd sorted. Find out at which
537 // position it was and where it ended.
538 unsigned int start_pos = 0,end_pos = 0;
539 for (i = 0; i < len; i++)
540 if (m_array[i] == row)
541 {
542 start_pos = i;
543 break;
544 }
545 m_array.RemoveAt( start_pos );
546 m_array.Add( row );
547
548 for (i = 0; i < len; i++)
549 if (m_array[i] == row)
550 {
551 end_pos = i;
552 break;
553 }
554
555 if (end_pos == start_pos)
556 return wxDataViewListModel::ValueChanged( col, start_pos );
557
558 // Create an array where order[old] -> new_pos, so that
559 // if nothing changed order[0] -> 0 etc.
560 unsigned int *order = new unsigned int[ len ];
561 // Fill up initial values.
562 for (i = 0; i < len; i++)
563 order[i] = i;
564
565 if (start_pos < end_pos)
566 {
567 for (i = start_pos; i < end_pos; i++)
568 order[i] = order[i+1];
569 order[end_pos] = start_pos;
570 }
571 else
572 {
573 for (i = end_pos; i > start_pos; i--)
574 order[i] = order[i-1];
575 order[start_pos] = end_pos;
576 }
577
578 wxDataViewListModel::RowsReordered( order );
579
580 delete [] order;
581
582 return true;
583 }
584
585 bool wxDataViewSortedListModel::ChildRowsReordered( unsigned int *WXUNUSED(new_order) )
586 {
587 // Nothing needs to be done. If the sort criteria
588 // of this list don't change, the order of the
589 // items of the child list isn't relevant.
590 return true;
591 }
592
593 bool wxDataViewSortedListModel::ChildCleared()
594 {
595 return wxDataViewListModel::Cleared();
596 }
597
598 unsigned int wxDataViewSortedListModel::GetRowCount() const
599 {
600 if (!m_child) return 0;
601
602 return m_child->GetRowCount();
603 }
604
605 unsigned int wxDataViewSortedListModel::GetColumnCount() const
606 {
607 if (!m_child) return 0;
608
609 return m_child->GetColumnCount();
610 }
611
612 wxString wxDataViewSortedListModel::GetColumnType( unsigned int col ) const
613 {
614 return m_child->GetColumnType( col );
615 }
616
617 void wxDataViewSortedListModel::GetValue( wxVariant &variant, unsigned int col, unsigned int row ) const
618 {
619 unsigned int child_row = m_array[row];
620 m_child->GetValue( variant, col, child_row );
621 }
622
623 bool wxDataViewSortedListModel::SetValue( const wxVariant &variant, unsigned int col, unsigned int row )
624 {
625 unsigned int child_row = m_array[row];
626 bool ret = m_child->SetValue( variant, col, child_row );
627
628 // Do nothing here as the change in the
629 // child model will be reported back.
630
631 return ret;
632 }
633
634 bool wxDataViewSortedListModel::RowAppended()
635 {
636 // you can only append
637 bool ret = m_child->RowAppended();
638
639 // Do nothing here as the change in the
640 // child model will be reported back.
641
642 return ret;
643 }
644
645 bool wxDataViewSortedListModel::RowPrepended()
646 {
647 // you can only append
648 bool ret = m_child->RowAppended();
649
650 // Do nothing here as the change in the
651 // child model will be reported back.
652
653 return ret;
654 }
655
656 bool wxDataViewSortedListModel::RowInserted( unsigned int WXUNUSED(before) )
657 {
658 // you can only append
659 bool ret = m_child->RowAppended();
660
661 // Do nothing here as the change in the
662 // child model will be reported back.
663
664 return ret;
665 }
666
667 bool wxDataViewSortedListModel::RowDeleted( unsigned int row )
668 {
669 unsigned int child_row = m_array[row];
670
671 bool ret = m_child->RowDeleted( child_row );
672
673 // Do nothing here as the change in the
674 // child model will be reported back.
675
676 return ret;
677 }
678
679 bool wxDataViewSortedListModel::RowChanged( unsigned int row )
680 {
681 unsigned int child_row = m_array[row];
682 bool ret = m_child->RowChanged( child_row );
683
684 // Do nothing here as the change in the
685 // child model will be reported back.
686
687 return ret;
688 }
689
690 bool wxDataViewSortedListModel::ValueChanged( unsigned int col, unsigned int row )
691 {
692 unsigned int child_row = m_array[row];
693 bool ret = m_child->ValueChanged( col, child_row );
694
695 // Do nothing here as the change in the
696 // child model will be reported back.
697
698 return ret;
699 }
700
701 bool wxDataViewSortedListModel::RowsReordered( unsigned int *WXUNUSED(new_order) )
702 {
703 // We sort them ourselves.
704
705 return false;
706 }
707
708 bool wxDataViewSortedListModel::Cleared()
709 {
710 bool ret = m_child->Cleared();
711
712 // Do nothing here as the change in the
713 // child model will be reported back.
714
715 return ret;
716 }
717
718 // ---------------------------------------------------------
719 // wxDataViewRendererBase
720 // ---------------------------------------------------------
721
722 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase, wxObject)
723
724 wxDataViewRendererBase::wxDataViewRendererBase( const wxString &varianttype,
725 wxDataViewCellMode WXUNUSED(mode),
726 int WXUNUSED(align) )
727 {
728 m_variantType = varianttype;
729 m_editorCtrl = NULL;
730 m_row = (unsigned int) -1;
731 }
732
733 const wxDataViewCtrl* wxDataViewRendererBase::GetView() const
734 {
735 return wx_const_cast(wxDataViewRendererBase*, this)->GetOwner()->GetOwner();
736 }
737
738 bool wxDataViewRendererBase::StartEditing( unsigned int row, wxRect labelRect )
739 {
740 m_row = row; // remember for later
741
742 unsigned int col = GetOwner()->GetModelColumn();
743 wxVariant value;
744 GetOwner()->GetOwner()->GetModel()->GetValue( value, col, row );
745
746 m_editorCtrl = CreateEditorCtrl( GetOwner()->GetOwner()->GetMainWindow(), labelRect, value );
747
748 wxDataViewEditorCtrlEvtHandler *handler =
749 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl, (wxDataViewRenderer*) this );
750
751 m_editorCtrl->PushEventHandler( handler );
752
753 #if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
754 handler->SetFocusOnIdle();
755 #else
756 m_editorCtrl->SetFocus();
757 #endif
758
759 return true;
760 }
761
762 void wxDataViewRendererBase::CancelEditing()
763 {
764 wxPendingDelete.Append( m_editorCtrl );
765
766 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
767
768 // m_editorCtrl->PopEventHandler( true );
769 }
770
771 bool wxDataViewRendererBase::FinishEditing()
772 {
773 wxVariant value;
774 GetValueFromEditorCtrl( m_editorCtrl, value );
775
776 wxPendingDelete.Append( m_editorCtrl );
777
778 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
779
780 if (!Validate(value))
781 return false;
782
783 unsigned int col = GetOwner()->GetModelColumn();
784 GetOwner()->GetOwner()->GetModel()->SetValue( value, col, m_row );
785 GetOwner()->GetOwner()->GetModel()->ValueChanged( col, m_row );
786
787 // m_editorCtrl->PopEventHandler( true );
788
789 return true;
790 }
791
792 //-----------------------------------------------------------------------------
793 // wxDataViewEditorCtrlEvtHandler
794 //-----------------------------------------------------------------------------
795
796 BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler, wxEvtHandler)
797 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar)
798 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus)
799 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle)
800 END_EVENT_TABLE()
801
802 wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
803 wxControl *editorCtrl,
804 wxDataViewRenderer *owner )
805 {
806 m_owner = owner;
807 m_editorCtrl = editorCtrl;
808
809 m_finished = false;
810 }
811
812 void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent &event )
813 {
814 if (m_focusOnIdle)
815 {
816 m_focusOnIdle = false;
817 if (wxWindow::FindFocus() != m_editorCtrl)
818 m_editorCtrl->SetFocus();
819 }
820
821 event.Skip();
822 }
823
824 void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent &event )
825 {
826 switch ( event.m_keyCode )
827 {
828 case WXK_RETURN:
829 m_finished = true;
830 m_owner->FinishEditing();
831 break;
832
833 case WXK_ESCAPE:
834 m_finished = true;
835 m_owner->CancelEditing();
836 break;
837
838 default:
839 event.Skip();
840 }
841 }
842
843 void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent &event )
844 {
845 if (!m_finished)
846 {
847 m_finished = true;
848 m_owner->FinishEditing();
849 }
850
851 event.Skip();
852 }
853
854 // ---------------------------------------------------------
855 // wxDataViewColumnBase
856 // ---------------------------------------------------------
857
858 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase, wxObject)
859
860 wxDataViewColumnBase::wxDataViewColumnBase(const wxString& WXUNUSED(title),
861 wxDataViewRenderer *renderer,
862 unsigned int model_column,
863 int WXUNUSED(width),
864 wxAlignment WXUNUSED(align),
865 int WXUNUSED(flags))
866 {
867 m_renderer = renderer;
868 m_model_column = model_column;
869 m_owner = NULL;
870 m_renderer->SetOwner( (wxDataViewColumn*) this );
871
872 // NOTE: the wxDataViewColumn's ctor must store the width, align, flags
873 // parameters inside the native control!
874 }
875
876 wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap& bitmap,
877 wxDataViewRenderer *renderer,
878 unsigned int model_column,
879 int WXUNUSED(width),
880 wxAlignment WXUNUSED(align),
881 int WXUNUSED(flags) )
882 {
883 m_renderer = renderer;
884 m_model_column = model_column;
885 m_bitmap = bitmap;
886 m_owner = NULL;
887 m_renderer->SetOwner( (wxDataViewColumn*) this );
888 }
889
890 wxDataViewColumnBase::~wxDataViewColumnBase()
891 {
892 if (m_renderer)
893 delete m_renderer;
894 }
895
896 int wxDataViewColumnBase::GetFlags() const
897 {
898 int ret = 0;
899
900 if (IsSortable())
901 ret |= wxDATAVIEW_COL_SORTABLE;
902 if (IsResizeable())
903 ret |= wxDATAVIEW_COL_RESIZABLE;
904 if (IsHidden())
905 ret |= wxDATAVIEW_COL_HIDDEN;
906
907 return ret;
908 }
909
910 void wxDataViewColumnBase::SetFlags(int flags)
911 {
912 SetSortable((flags & wxDATAVIEW_COL_SORTABLE) != 0);
913 SetResizeable((flags & wxDATAVIEW_COL_RESIZABLE) != 0);
914 SetHidden((flags & wxDATAVIEW_COL_HIDDEN) != 0);
915 }
916
917 // ---------------------------------------------------------
918 // wxDataViewEventListModelNotifier
919 // ---------------------------------------------------------
920
921 class WXDLLIMPEXP_ADV wxDataViewEventListModelNotifier: public wxDataViewListModelNotifier
922 {
923 public:
924 wxDataViewEventListModelNotifier( wxDataViewCtrl *ctrl ) { m_ctrl = ctrl; }
925
926 bool SendEvent( wxEventType event_type, unsigned int row = 0, unsigned int col = 0 )
927 {
928 wxDataViewEvent event( event_type, m_ctrl->GetId() );
929 event.SetEventObject( m_ctrl );
930 event.SetModel( m_ctrl->GetModel() );
931 event.SetRow( row );
932 event.SetColumn( col );
933 m_ctrl->GetEventHandler()->ProcessEvent( event );
934 return true;
935 }
936
937 virtual bool RowAppended() { return SendEvent( wxEVT_COMMAND_DATAVIEW_MODEL_ROW_APPENDED ); }
938 virtual bool RowPrepended() { return SendEvent( wxEVT_COMMAND_DATAVIEW_MODEL_ROW_PREPENDED ); }
939 virtual bool RowInserted( unsigned int before )
940 { return SendEvent( wxEVT_COMMAND_DATAVIEW_MODEL_ROW_INSERTED, before ); }
941 virtual bool RowDeleted( unsigned int row )
942 { return SendEvent( wxEVT_COMMAND_DATAVIEW_MODEL_ROW_DELETED, row ); }
943 virtual bool RowChanged( unsigned int row )
944 { return SendEvent( wxEVT_COMMAND_DATAVIEW_MODEL_ROW_CHANGED, row ); }
945 virtual bool ValueChanged( unsigned int col, unsigned int row )
946 { return SendEvent( wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED, row, col ); }
947 virtual bool RowsReordered( unsigned int * WXUNUSED(new_order) )
948 { return SendEvent( wxEVT_COMMAND_DATAVIEW_MODEL_ROWS_REORDERED ); }
949 virtual bool Cleared() { return SendEvent( wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED ); }
950
951 private:
952 wxDataViewCtrl *m_ctrl;
953 };
954
955
956 // ---------------------------------------------------------
957 // wxDataViewCtrlBase
958 // ---------------------------------------------------------
959
960 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl)
961
962 wxDataViewCtrlBase::wxDataViewCtrlBase()
963 {
964 m_model = NULL;
965 m_cols.DeleteContents( true );
966 m_eventNotifier = NULL;
967 }
968
969 wxDataViewCtrlBase::~wxDataViewCtrlBase()
970 {
971 // IMPORTANT: before calling DecRef() on our model (since it may
972 // result in a free() call), erase all columns (since
973 // they hold a pointer to our model)
974 m_cols.Clear();
975
976 if (m_model)
977 {
978 if (m_eventNotifier)
979 m_model->RemoveNotifier( m_eventNotifier );
980 m_eventNotifier = NULL;
981
982 m_model->DecRef();
983 m_model = NULL;
984 }
985 }
986
987 bool wxDataViewCtrlBase::AssociateModel( wxDataViewListModel *model )
988 {
989 if (m_model)
990 {
991 if (m_eventNotifier)
992 m_model->RemoveNotifier( m_eventNotifier );
993 m_eventNotifier = NULL;
994
995 m_model->DecRef(); // discard old model, if any
996 }
997
998 // add our own reference to the new model:
999 m_model = model;
1000 if (m_model)
1001 {
1002 m_model->IncRef();
1003 m_eventNotifier = new wxDataViewEventListModelNotifier( (wxDataViewCtrl*) this );
1004 m_model->AddNotifier( m_eventNotifier );
1005 }
1006
1007 return true;
1008 }
1009
1010 wxDataViewListModel* wxDataViewCtrlBase::GetModel()
1011 {
1012 return m_model;
1013 }
1014
1015 bool wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column,
1016 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1017 {
1018 return AppendColumn( new wxDataViewColumn( label,
1019 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
1020 model_column, width, align, flags ) );
1021 }
1022
1023 bool wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column,
1024 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1025 {
1026 return AppendColumn( new wxDataViewColumn( label,
1027 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
1028 model_column, width, align, flags ) );
1029 }
1030
1031 bool wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column,
1032 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1033 {
1034 return AppendColumn( new wxDataViewColumn( label,
1035 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
1036 model_column, width, align, flags ) );
1037 }
1038
1039 bool wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column,
1040 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1041 {
1042 return AppendColumn( new wxDataViewColumn( label,
1043 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
1044 model_column, width, align, flags ) );
1045 }
1046
1047 bool wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column,
1048 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1049 {
1050 return AppendColumn( new wxDataViewColumn( label,
1051 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
1052 model_column, width, align, flags ) );
1053 }
1054
1055 bool wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column,
1056 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1057 {
1058 return AppendColumn( new wxDataViewColumn( label,
1059 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
1060 model_column, width, align, flags ) );
1061 }
1062
1063 bool wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column,
1064 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1065 {
1066 return AppendColumn( new wxDataViewColumn( label,
1067 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
1068 model_column, width, align, flags ) );
1069 }
1070
1071 bool wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column,
1072 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1073 {
1074 return AppendColumn( new wxDataViewColumn( label,
1075 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
1076 model_column, width, align, flags ) );
1077 }
1078
1079 bool wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column,
1080 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1081 {
1082 return AppendColumn( new wxDataViewColumn( label,
1083 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
1084 model_column, width, align, flags ) );
1085 }
1086
1087 bool wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column,
1088 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1089 {
1090 return AppendColumn( new wxDataViewColumn( label,
1091 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
1092 model_column, width, align, flags ) );
1093 }
1094
1095 bool wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
1096 {
1097 m_cols.Append( (wxObject*) col );
1098 col->SetOwner( (wxDataViewCtrl*) this );
1099 return true;
1100 }
1101
1102 unsigned int wxDataViewCtrlBase::GetColumnCount() const
1103 {
1104 return m_cols.GetCount();
1105 }
1106
1107 bool wxDataViewCtrlBase::DeleteColumn( unsigned int WXUNUSED(pos) )
1108 {
1109 return false;
1110 }
1111
1112 bool wxDataViewCtrlBase::ClearColumns()
1113 {
1114 return false;
1115 }
1116
1117 wxDataViewColumn* wxDataViewCtrlBase::GetColumn( unsigned int pos )
1118 {
1119 return (wxDataViewColumn*) m_cols[ pos ];
1120 }
1121
1122 // ---------------------------------------------------------
1123 // wxDataViewEvent
1124 // ---------------------------------------------------------
1125
1126 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent)
1127
1128 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ROW_SELECTED)
1129 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ROW_ACTIVATED)
1130 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK)
1131 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK)
1132 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED)
1133
1134 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ROW_APPENDED)
1135 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ROW_PREPENDED)
1136 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ROW_INSERTED)
1137 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ROW_DELETED)
1138 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ROW_CHANGED)
1139 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED)
1140 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ROWS_REORDERED)
1141 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED)
1142
1143 #endif