]> git.saurik.com Git - wxWidgets.git/blob - src/common/datavcmn.cpp
Added event skeletons, defines, event ids..
[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 // wxDataViewModel
30 // ---------------------------------------------------------
31
32 IMPLEMENT_ABSTRACT_CLASS(wxDataViewModel, wxObject)
33
34 // ---------------------------------------------------------
35 // wxDataViewListModel
36 // ---------------------------------------------------------
37
38 IMPLEMENT_ABSTRACT_CLASS(wxDataViewListModel, wxDataViewModel)
39
40 wxDataViewListModel::wxDataViewListModel()
41 {
42 m_viewingColumns.DeleteContents( true );
43 m_notifiers.DeleteContents( true );
44 }
45
46 wxDataViewListModel::~wxDataViewListModel()
47 {
48 }
49
50 bool wxDataViewListModel::RowAppended()
51 {
52 bool ret = true;
53
54 wxList::compatibility_iterator node = m_notifiers.GetFirst();
55 while (node)
56 {
57 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
58 if (!notifier->RowAppended())
59 ret = false;
60 node = node->GetNext();
61 }
62
63 return ret;
64 }
65
66 bool wxDataViewListModel::RowPrepended()
67 {
68 bool ret = true;
69
70 wxList::compatibility_iterator node = m_notifiers.GetFirst();
71 while (node)
72 {
73 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
74 if (!notifier->RowPrepended())
75 ret = false;
76 node = node->GetNext();
77 }
78
79 return ret;
80 }
81
82 bool wxDataViewListModel::RowInserted( unsigned int before )
83 {
84 bool ret = true;
85
86 wxList::compatibility_iterator node = m_notifiers.GetFirst();
87 while (node)
88 {
89 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
90 if (!notifier->RowInserted(before))
91 ret = false;
92 node = node->GetNext();
93 }
94
95 return ret;
96 }
97
98 bool wxDataViewListModel::RowDeleted( unsigned int row )
99 {
100 bool ret = true;
101
102 wxList::compatibility_iterator node = m_notifiers.GetFirst();
103 while (node)
104 {
105 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
106 if (!notifier->RowDeleted( row ))
107 ret = false;
108 node = node->GetNext();
109 }
110
111 return ret;
112 }
113
114 bool wxDataViewListModel::RowChanged( unsigned int row )
115 {
116 bool ret = true;
117
118 wxList::compatibility_iterator node = m_notifiers.GetFirst();
119 while (node)
120 {
121 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
122 if (!notifier->RowChanged( row ))
123 ret = false;
124 node = node->GetNext();
125 }
126
127 return ret;
128 }
129
130 bool wxDataViewListModel::ValueChanged( unsigned int col, unsigned int row )
131 {
132 bool ret = true;
133
134 wxList::compatibility_iterator node = m_notifiers.GetFirst();
135 while (node)
136 {
137 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
138 if (!notifier->ValueChanged( col, row ))
139 ret = false;
140 node = node->GetNext();
141 }
142
143 return ret;
144 }
145
146 bool wxDataViewListModel::RowsReordered( unsigned int *new_order )
147 {
148 bool ret = true;
149
150 wxList::compatibility_iterator node = m_notifiers.GetFirst();
151 while (node)
152 {
153 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
154 if (!notifier->RowsReordered( new_order ))
155 ret = false;
156 node = node->GetNext();
157 }
158
159 return ret;
160 }
161
162 bool wxDataViewListModel::Cleared()
163 {
164 bool ret = true;
165
166 wxList::compatibility_iterator node = m_notifiers.GetFirst();
167 while (node)
168 {
169 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
170 if (!notifier->Cleared())
171 ret = false;
172 node = node->GetNext();
173 }
174
175 return ret;
176 }
177
178 void wxDataViewListModel::AddViewingColumn( wxDataViewColumn *view_column, unsigned int model_column )
179 {
180 m_viewingColumns.Append( new wxDataViewViewingColumn( view_column, model_column ) );
181 }
182
183 void wxDataViewListModel::RemoveViewingColumn( wxDataViewColumn *column )
184 {
185 wxList::compatibility_iterator node = m_viewingColumns.GetFirst();
186 while (node)
187 {
188 wxDataViewViewingColumn* tmp = (wxDataViewViewingColumn*) node->GetData();
189
190 if (tmp->m_viewColumn == column)
191 {
192 m_viewingColumns.DeleteObject( tmp );
193 return;
194 }
195
196 node = node->GetNext();
197 }
198 }
199
200 void wxDataViewListModel::AddNotifier( wxDataViewListModelNotifier *notifier )
201 {
202 m_notifiers.Append( notifier );
203 notifier->SetOwner( this );
204 }
205
206 void wxDataViewListModel::RemoveNotifier( wxDataViewListModelNotifier *notifier )
207 {
208 m_notifiers.DeleteObject( notifier );
209 }
210
211 // ---------------------------------------------------------
212 // wxDataViewSortedListModelNotifier
213 // ---------------------------------------------------------
214
215 class wxDataViewSortedListModelNotifier: public wxDataViewListModelNotifier
216 {
217 public:
218 wxDataViewSortedListModelNotifier( wxDataViewSortedListModel *model )
219 { m_model = model; }
220
221 virtual bool RowAppended()
222 { return m_model->ChildRowAppended(); }
223
224 virtual bool RowPrepended()
225 { return m_model->ChildRowPrepended(); }
226
227 virtual bool RowInserted( unsigned int before )
228 { return m_model->ChildRowInserted( before ); }
229
230 virtual bool RowDeleted( unsigned int row )
231 { return m_model->ChildRowDeleted( row ); }
232
233 virtual bool RowChanged( unsigned int row )
234 { return m_model->ChildRowChanged( row ); }
235
236 virtual bool ValueChanged( unsigned int col, unsigned int row )
237 { return m_model->ChildValueChanged( col, row); }
238
239 virtual bool RowsReordered( unsigned int *new_order )
240 { return m_model->ChildRowsReordered( new_order ); }
241
242 virtual bool Cleared()
243 { return m_model->ChildCleared(); }
244
245 wxDataViewSortedListModel *m_model;
246 };
247
248 // ---------------------------------------------------------
249 // wxDataViewSortedListModel compare function
250 // ---------------------------------------------------------
251
252 int wxCALLBACK wxDataViewListModelSortedDefaultCompare
253 (unsigned int row1, unsigned int row2, unsigned int col, wxDataViewListModel* model )
254 {
255 wxVariant value1,value2;
256 model->GetValue( value1, col, row1 );
257 model->GetValue( value2, col, row2 );
258 if (value1.GetType() == wxT("string"))
259 {
260 wxString str1 = value1.GetString();
261 wxString str2 = value2.GetString();
262 return str1.Cmp( str2 );
263 }
264 if (value1.GetType() == wxT("long"))
265 {
266 long l1 = value1.GetLong();
267 long l2 = value2.GetLong();
268 return l1-l2;
269 }
270 if (value1.GetType() == wxT("double"))
271 {
272 double d1 = value1.GetDouble();
273 double d2 = value2.GetDouble();
274 if (d1 == d2) return 0;
275 if (d1 < d2) return 1;
276 return -1;
277 }
278 if (value1.GetType() == wxT("datetime"))
279 {
280 wxDateTime dt1 = value1.GetDateTime();
281 wxDateTime dt2 = value2.GetDateTime();
282 if (dt1.IsEqualTo(dt2)) return 0;
283 if (dt1.IsEarlierThan(dt2)) return 1;
284 return -1;
285 }
286
287 return 0;
288 }
289
290 static wxDataViewListModelCompare s_CmpFunc;
291 static wxDataViewListModel *s_CmpModel;
292 static unsigned int s_CmpCol;
293
294 int LINKAGEMODE wxDataViewIntermediateCmp( unsigned int row1, unsigned int row2 )
295 {
296 return s_CmpFunc( row1, row2, s_CmpCol, s_CmpModel );
297 }
298
299 // ---------------------------------------------------------
300 // wxDataViewSortedListModel
301 // ---------------------------------------------------------
302
303 IMPLEMENT_ABSTRACT_CLASS(wxDataViewSortedListModel, wxDataViewListModel)
304
305 wxDataViewSortedListModel::wxDataViewSortedListModel( wxDataViewListModel *child ) :
306 m_array( wxDataViewIntermediateCmp )
307 {
308 m_child = child;
309 s_CmpCol = 0;
310 s_CmpModel = child;
311 s_CmpFunc = wxDataViewListModelSortedDefaultCompare;
312
313 m_notifierOnChild = new wxDataViewSortedListModelNotifier( this );
314 m_child->AddNotifier( m_notifierOnChild );
315
316 Resort();
317 }
318
319 wxDataViewSortedListModel::~wxDataViewSortedListModel()
320 {
321 m_child->RemoveNotifier( m_notifierOnChild );
322 }
323
324 void wxDataViewSortedListModel::Resort()
325 {
326 m_array.Clear();
327 unsigned int n = m_child->GetNumberOfRows();
328 unsigned int i;
329 for (i = 0; i < n; i++)
330 m_array.Add( i );
331 }
332
333 #if 0
334 static void Dump( wxDataViewListModel *model, unsigned int col )
335 {
336 unsigned int n = model->GetNumberOfRows();
337 unsigned int i;
338 for (i = 0; i < n; i++)
339 {
340 wxVariant variant;
341 model->GetValue( variant, col, i );
342 wxString tmp;
343 tmp = variant.GetString();
344 wxPrintf( wxT("%d: %s\n"), (int) i, tmp.c_str() );
345 }
346 }
347 #endif
348
349 bool wxDataViewSortedListModel::ChildRowAppended()
350 {
351 // no need to fix up array
352
353 unsigned int len = m_array.GetCount();
354
355 unsigned int pos = m_array.Add( len );
356
357 if (pos == 0)
358 return wxDataViewListModel::RowPrepended();
359
360 if (pos == len)
361 return wxDataViewListModel::RowAppended();
362
363 return wxDataViewListModel::RowInserted( pos );
364 }
365
366 bool wxDataViewSortedListModel::ChildRowPrepended()
367 {
368 // fix up array
369 unsigned int i;
370 unsigned int len = m_array.GetCount();
371 for (i = 0; i < len; i++)
372 {
373 unsigned int value = m_array[i];
374 m_array[i] = value+1;
375 }
376
377 unsigned int pos = m_array.Add( 0 );
378
379 if (pos == 0)
380 return wxDataViewListModel::RowPrepended();
381
382 if (pos == len)
383 return wxDataViewListModel::RowAppended();
384
385 return wxDataViewListModel::RowInserted( pos );
386 }
387
388 bool wxDataViewSortedListModel::ChildRowInserted( unsigned int before )
389 {
390 // fix up array
391 unsigned int i;
392 unsigned int len = m_array.GetCount();
393 for (i = 0; i < len; i++)
394 {
395 unsigned int value = m_array[i];
396 if (value >= before)
397 m_array[i] = value+1;
398 }
399
400 unsigned int pos = m_array.Add( before );
401
402 if (pos == 0)
403 return wxDataViewListModel::RowPrepended();
404
405 if (pos == len)
406 return wxDataViewListModel::RowAppended();
407
408 return wxDataViewListModel::RowInserted( pos );
409 }
410
411 bool wxDataViewSortedListModel::ChildRowDeleted( unsigned int row )
412 {
413 unsigned int i;
414 unsigned int len = m_array.GetCount();
415 int pos = -1;
416 for (i = 0; i < len; i++)
417 {
418 unsigned int value = m_array[i];
419 if (value == row)
420 {
421 // delete later
422 pos = (int) i;
423 }
424 else
425 {
426 // Fix up array
427 if (value > row)
428 m_array[i] = value-1;
429 }
430 }
431
432 if (pos == -1)
433 return false; // we should probably assert
434
435 // remove
436 m_array.RemoveAt( (unsigned int) pos );
437
438 return wxDataViewListModel::RowDeleted( (unsigned int) pos);
439 }
440
441 bool wxDataViewSortedListModel::ChildRowChanged( unsigned int row )
442 {
443 unsigned int i;
444 unsigned int len = m_array.GetCount();
445
446 // Remove and readd sorted. Find out at which
447 // position it was and where it ended.
448 unsigned int start_pos = 0,end_pos = 0;
449 for (i = 0; i < len; i++)
450 if (m_array[i] == row)
451 {
452 start_pos = i;
453 break;
454 }
455 m_array.RemoveAt( start_pos );
456 m_array.Add( row );
457
458 for (i = 0; i < len; i++)
459 if (m_array[i] == row)
460 {
461 end_pos = i;
462 break;
463 }
464
465 if (end_pos == start_pos)
466 return wxDataViewListModel::RowChanged( start_pos );
467
468 // Create an array where order[old] -> new_pos, so that
469 // if nothing changed order[0] -> 0 etc.
470 unsigned int *order = new unsigned int[ len ];
471 // Fill up initial values.
472 for (i = 0; i < len; i++)
473 order[i] = i;
474
475 if (start_pos < end_pos)
476 {
477 for (i = start_pos; i < end_pos; i++)
478 order[i] = order[i+1];
479 order[end_pos] = start_pos;
480 }
481 else
482 {
483 for (i = end_pos; i > start_pos; i--)
484 order[i] = order[i-1];
485 order[start_pos] = end_pos;
486 }
487
488 wxDataViewListModel::RowsReordered( order );
489
490 delete [] order;
491
492 return true;
493 }
494
495 bool wxDataViewSortedListModel::ChildValueChanged( unsigned int col, unsigned int row )
496 {
497 unsigned int i;
498 unsigned int len = m_array.GetCount();
499
500 // Remove and readd sorted. Find out at which
501 // position it was and where it ended.
502 unsigned int start_pos = 0,end_pos = 0;
503 for (i = 0; i < len; i++)
504 if (m_array[i] == row)
505 {
506 start_pos = i;
507 break;
508 }
509 m_array.RemoveAt( start_pos );
510 m_array.Add( row );
511
512 for (i = 0; i < len; i++)
513 if (m_array[i] == row)
514 {
515 end_pos = i;
516 break;
517 }
518
519 if (end_pos == start_pos)
520 return wxDataViewListModel::ValueChanged( col, start_pos );
521
522 // Create an array where order[old] -> new_pos, so that
523 // if nothing changed order[0] -> 0 etc.
524 unsigned int *order = new unsigned int[ len ];
525 // Fill up initial values.
526 for (i = 0; i < len; i++)
527 order[i] = i;
528
529 if (start_pos < end_pos)
530 {
531 for (i = start_pos; i < end_pos; i++)
532 order[i] = order[i+1];
533 order[end_pos] = start_pos;
534 }
535 else
536 {
537 for (i = end_pos; i > start_pos; i--)
538 order[i] = order[i-1];
539 order[start_pos] = end_pos;
540 }
541
542 wxDataViewListModel::RowsReordered( order );
543
544 delete [] order;
545
546 return true;
547 }
548
549 bool wxDataViewSortedListModel::ChildRowsReordered( unsigned int *WXUNUSED(new_order) )
550 {
551 // Nothing needs to be done. If the sort criteria
552 // of this list don't change, the order of the
553 // items of the child list isn't relevant.
554 return true;
555 }
556
557 bool wxDataViewSortedListModel::ChildCleared()
558 {
559 return wxDataViewListModel::Cleared();
560 }
561
562 unsigned int wxDataViewSortedListModel::GetNumberOfRows()
563 {
564 return m_array.GetCount();
565 }
566
567 unsigned int wxDataViewSortedListModel::GetNumberOfCols()
568 {
569 return m_child->GetNumberOfCols();
570 }
571
572 wxString wxDataViewSortedListModel::GetColType( unsigned int col )
573 {
574 return m_child->GetColType( col );
575 }
576
577 void wxDataViewSortedListModel::GetValue( wxVariant &variant, unsigned int col, unsigned int row )
578 {
579 unsigned int child_row = m_array[row];
580 m_child->GetValue( variant, col, child_row );
581 }
582
583 bool wxDataViewSortedListModel::SetValue( wxVariant &variant, unsigned int col, unsigned int row )
584 {
585 unsigned int child_row = m_array[row];
586 bool ret = m_child->SetValue( variant, col, child_row );
587
588 // Do nothing here as the change in the
589 // child model will be reported back.
590
591 return ret;
592 }
593
594 bool wxDataViewSortedListModel::RowAppended()
595 {
596 // you can only append
597 bool ret = m_child->RowAppended();
598
599 // Do nothing here as the change in the
600 // child model will be reported back.
601
602 return ret;
603 }
604
605 bool wxDataViewSortedListModel::RowPrepended()
606 {
607 // you can only append
608 bool ret = m_child->RowAppended();
609
610 // Do nothing here as the change in the
611 // child model will be reported back.
612
613 return ret;
614 }
615
616 bool wxDataViewSortedListModel::RowInserted( unsigned int WXUNUSED(before) )
617 {
618 // you can only append
619 bool ret = m_child->RowAppended();
620
621 // Do nothing here as the change in the
622 // child model will be reported back.
623
624 return ret;
625 }
626
627 bool wxDataViewSortedListModel::RowDeleted( unsigned int row )
628 {
629 unsigned int child_row = m_array[row];
630
631 bool ret = m_child->RowDeleted( child_row );
632
633 // Do nothing here as the change in the
634 // child model will be reported back.
635
636 return ret;
637 }
638
639 bool wxDataViewSortedListModel::RowChanged( unsigned int row )
640 {
641 unsigned int child_row = m_array[row];
642 bool ret = m_child->RowChanged( child_row );
643
644 // Do nothing here as the change in the
645 // child model will be reported back.
646
647 return ret;
648 }
649
650 bool wxDataViewSortedListModel::ValueChanged( unsigned int col, unsigned int row )
651 {
652 unsigned int child_row = m_array[row];
653 bool ret = m_child->ValueChanged( col, child_row );
654
655 // Do nothing here as the change in the
656 // child model will be reported back.
657
658 return ret;
659 }
660
661 bool wxDataViewSortedListModel::RowsReordered( unsigned int *WXUNUSED(new_order) )
662 {
663 // We sort them ourselves.
664
665 return false;
666 }
667
668 bool wxDataViewSortedListModel::Cleared()
669 {
670 bool ret = m_child->Cleared();
671
672 // Do nothing here as the change in the
673 // child model will be reported back.
674
675 return ret;
676 }
677
678 // ---------------------------------------------------------
679 // wxDataViewCellBase
680 // ---------------------------------------------------------
681
682 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCellBase, wxObject)
683
684 wxDataViewCellBase::wxDataViewCellBase( const wxString &varianttype, wxDataViewCellMode mode )
685 {
686 m_variantType = varianttype;
687 m_mode = mode;
688 }
689
690 // ---------------------------------------------------------
691 // wxDataViewColumnBase
692 // ---------------------------------------------------------
693
694 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase, wxObject)
695
696 wxDataViewColumnBase::wxDataViewColumnBase(const wxString& title,
697 wxDataViewCell *cell,
698 unsigned int model_column,
699 int WXUNUSED(width),
700 int flags )
701 {
702 m_cell = cell;
703 m_model_column = model_column;
704 m_flags = flags;
705 m_title = title;
706 m_owner = NULL;
707 m_cell->SetOwner( (wxDataViewColumn*) this );
708 }
709
710 wxDataViewColumnBase::~wxDataViewColumnBase()
711 {
712 if (m_cell)
713 delete m_cell;
714
715 if (GetOwner())
716 {
717 GetOwner()->GetModel()->RemoveViewingColumn( (wxDataViewColumn*) this );
718 }
719 }
720
721 void wxDataViewColumnBase::SetTitle( const wxString &title )
722 {
723 m_title = title;
724 }
725
726 wxString wxDataViewColumnBase::GetTitle()
727 {
728 return m_title;
729 }
730
731 // ---------------------------------------------------------
732 // wxDataViewCtrlBase
733 // ---------------------------------------------------------
734
735 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl)
736
737 wxDataViewCtrlBase::wxDataViewCtrlBase()
738 {
739 m_model = NULL;
740 m_cols.DeleteContents( true );
741 }
742
743 wxDataViewCtrlBase::~wxDataViewCtrlBase()
744 {
745 }
746
747 bool wxDataViewCtrlBase::AssociateModel( wxDataViewListModel *model )
748 {
749 m_model = model;
750
751 return true;
752 }
753
754 wxDataViewListModel* wxDataViewCtrlBase::GetModel()
755 {
756 return m_model;
757 }
758
759 bool wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column )
760 {
761 return AppendColumn( new wxDataViewColumn( label, new wxDataViewTextCell(), model_column ) );
762 }
763
764 bool wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column )
765 {
766 return AppendColumn( new wxDataViewColumn( label, new wxDataViewToggleCell(), model_column, 30 ) );
767 }
768
769 bool wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column )
770 {
771 return AppendColumn( new wxDataViewColumn( label, new wxDataViewProgressCell(), model_column, 70 ) );
772 }
773
774 bool wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column )
775 {
776 return AppendColumn( new wxDataViewColumn( label, new wxDataViewDateCell(), model_column ) );
777 }
778
779 bool wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
780 {
781 m_cols.Append( (wxObject*) col );
782 col->SetOwner( (wxDataViewCtrl*) this );
783 m_model->AddViewingColumn( col, col->GetModelColumn() );
784 return true;
785 }
786
787 unsigned int wxDataViewCtrlBase::GetNumberOfColumns()
788 {
789 return m_cols.GetCount();
790 }
791
792 bool wxDataViewCtrlBase::DeleteColumn( unsigned int WXUNUSED(pos) )
793 {
794 return false;
795 }
796
797 bool wxDataViewCtrlBase::ClearColumns()
798 {
799 return false;
800 }
801
802 wxDataViewColumn* wxDataViewCtrlBase::GetColumn( unsigned int pos )
803 {
804 return (wxDataViewColumn*) m_cols[ pos ];
805 }
806
807 // ---------------------------------------------------------
808 // wxDataViewEvent
809 // ---------------------------------------------------------
810
811 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent)
812
813 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ROW_SELECTED)
814
815
816 #endif