]> git.saurik.com Git - wxWidgets.git/blame - src/common/datavcmn.cpp
don't return void values
[wxWidgets.git] / src / common / datavcmn.cpp
CommitLineData
239eaa41
RR
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
8045736e
RR
18#if wxUSE_DATAVIEWCTRL
19
f554a14b
WS
20#ifndef WX_PRECOMP
21 #include "wx/object.h"
22 #include "wx/log.h"
23#endif
24
239eaa41 25#include "wx/image.h"
f554a14b 26#include "wx/dataview.h"
239eaa41 27
f554a14b 28// ---------------------------------------------------------
239eaa41 29// wxDataViewModel
f554a14b 30// ---------------------------------------------------------
239eaa41
RR
31
32IMPLEMENT_ABSTRACT_CLASS(wxDataViewModel, wxObject)
33
f554a14b 34// ---------------------------------------------------------
239eaa41 35// wxDataViewListModel
f554a14b 36// ---------------------------------------------------------
239eaa41
RR
37
38IMPLEMENT_ABSTRACT_CLASS(wxDataViewListModel, wxDataViewModel)
39
40wxDataViewListModel::wxDataViewListModel()
41{
b5d777c7 42 m_viewingColumns.DeleteContents( true );
8f850e28 43 m_notifiers.DeleteContents( true );
239eaa41
RR
44}
45
46wxDataViewListModel::~wxDataViewListModel()
47{
239eaa41
RR
48}
49
50bool wxDataViewListModel::RowAppended()
51{
8f850e28
RR
52 bool ret = true;
53
54 wxNode *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 }
f554a14b 62
8f850e28 63 return ret;
239eaa41
RR
64}
65
66bool wxDataViewListModel::RowPrepended()
67{
8f850e28
RR
68 bool ret = true;
69
70 wxNode *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 }
f554a14b 78
8f850e28 79 return ret;
239eaa41
RR
80}
81
82bool wxDataViewListModel::RowInserted( size_t before )
83{
8f850e28
RR
84 bool ret = true;
85
86 wxNode *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 }
f554a14b 94
8f850e28 95 return ret;
239eaa41
RR
96}
97
98bool wxDataViewListModel::RowDeleted( size_t row )
99{
8f850e28
RR
100 bool ret = true;
101
102 wxNode *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 }
f554a14b 110
8f850e28 111 return ret;
239eaa41
RR
112}
113
114bool wxDataViewListModel::RowChanged( size_t row )
115{
8f850e28
RR
116 bool ret = true;
117
118 wxNode *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 }
f554a14b 126
8f850e28 127 return ret;
239eaa41
RR
128}
129
a7f61f76 130bool wxDataViewListModel::ValueChanged( size_t col, size_t row )
239eaa41 131{
8f850e28 132 bool ret = true;
b5d777c7 133
8f850e28 134 wxNode *node = m_notifiers.GetFirst();
b5d777c7
RR
135 while (node)
136 {
8f850e28
RR
137 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
138 if (!notifier->ValueChanged( col, row ))
139 ret = false;
b5d777c7
RR
140 node = node->GetNext();
141 }
f554a14b 142
b5d777c7 143 return ret;
239eaa41
RR
144}
145
4eccd3a1
RR
146bool wxDataViewListModel::RowsReordered( size_t *new_order )
147{
148 bool ret = true;
149
150 wxNode *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 }
f554a14b 158
4eccd3a1
RR
159 return ret;
160}
161
239eaa41
RR
162bool wxDataViewListModel::Cleared()
163{
8f850e28
RR
164 bool ret = true;
165
166 wxNode *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 }
f554a14b 174
8f850e28 175 return ret;
239eaa41
RR
176}
177
b5d777c7
RR
178void wxDataViewListModel::AddViewingColumn( wxDataViewColumn *view_column, size_t model_column )
179{
180 m_viewingColumns.Append( new wxDataViewViewingColumn( view_column, model_column ) );
181}
182
183void wxDataViewListModel::RemoveViewingColumn( wxDataViewColumn *column )
184{
185 wxNode *node = m_viewingColumns.GetFirst();
186 while (node)
187 {
188 wxDataViewViewingColumn* tmp = (wxDataViewViewingColumn*) node->GetData();
f554a14b 189
b5d777c7
RR
190 if (tmp->m_viewColumn == column)
191 {
192 m_viewingColumns.DeleteObject( tmp );
193 return;
194 }
f554a14b 195
b5d777c7
RR
196 node = node->GetNext();
197 }
198}
199
8f850e28 200void wxDataViewListModel::AddNotifier( wxDataViewListModelNotifier *notifier )
239eaa41 201{
8f850e28
RR
202 m_notifiers.Append( notifier );
203 notifier->SetOwner( this );
239eaa41
RR
204}
205
8f850e28 206void wxDataViewListModel::RemoveNotifier( wxDataViewListModelNotifier *notifier )
239eaa41 207{
8f850e28 208 m_notifiers.DeleteObject( notifier );
239eaa41
RR
209}
210
f554a14b 211// ---------------------------------------------------------
4eccd3a1 212// wxDataViewSortedListModelNotifier
f554a14b 213// ---------------------------------------------------------
4eccd3a1
RR
214
215class wxDataViewSortedListModelNotifier: public wxDataViewListModelNotifier
216{
217public:
218 wxDataViewSortedListModelNotifier( wxDataViewSortedListModel *model )
219 { m_model = model; }
f554a14b 220
4eccd3a1
RR
221 virtual bool RowAppended() { return true; }
222 virtual bool RowPrepended() { return true; }
f554a14b
WS
223 virtual bool RowInserted( size_t WXUNUSED(before) ) { return true; }
224 virtual bool RowDeleted( size_t WXUNUSED(row) ) { return true; }
225 virtual bool RowChanged( size_t WXUNUSED(row) ) { return true; }
4eccd3a1
RR
226 virtual bool ValueChanged( size_t col, size_t row )
227 { return m_model->ChildValueChanged( col, row); }
f554a14b 228 virtual bool RowsReordered( size_t *WXUNUSED(new_order) ) { return true; }
4eccd3a1 229 virtual bool Cleared() { return true; }
f554a14b 230
4eccd3a1
RR
231 wxDataViewSortedListModel *m_model;
232};
233
f554a14b 234// ---------------------------------------------------------
4eccd3a1 235// wxDataViewSortedListModel compare function
f554a14b 236// ---------------------------------------------------------
8981608c
RR
237
238int wxCALLBACK wxDataViewListModelSortedDefaultCompare
239 (size_t row1, size_t row2, size_t col, wxDataViewListModel* model )
240{
3f3af7e7
RR
241 wxVariant value1,value2;
242 model->GetValue( value1, col, row1 );
243 model->GetValue( value2, col, row2 );
8981608c
RR
244 if (value1.GetType() == wxT("string"))
245 {
246 wxString str1 = value1.GetString();
247 wxString str2 = value2.GetString();
248 return str1.Cmp( str2 );
249 }
250 if (value1.GetType() == wxT("long"))
251 {
252 long l1 = value1.GetLong();
253 long l2 = value2.GetLong();
254 return l1-l2;
255 }
256 if (value1.GetType() == wxT("double"))
257 {
258 double d1 = value1.GetDouble();
259 double d2 = value2.GetDouble();
260 if (d1 == d2) return 0;
261 if (d1 < d2) return 1;
262 return -1;
263 }
264 if (value1.GetType() == wxT("datetime"))
265 {
266 wxDateTime dt1 = value1.GetDateTime();
267 wxDateTime dt2 = value2.GetDateTime();
268 if (dt1.IsEqualTo(dt2)) return 0;
269 if (dt1.IsEarlierThan(dt2)) return 1;
270 return -1;
271 }
272
273 return 0;
274}
275
276static wxDataViewListModelCompare s_CmpFunc;
277static wxDataViewListModel *s_CmpModel;
278static size_t s_CmpCol;
279
280int LINKAGEMODE wxDataViewIntermediateCmp( size_t row1, size_t row2 )
281{
282 return s_CmpFunc( row1, row2, s_CmpCol, s_CmpModel );
283}
284
f554a14b 285// ---------------------------------------------------------
4eccd3a1
RR
286// wxDataViewSortedListModel
287// ---------------------------------------------------------
8981608c
RR
288
289IMPLEMENT_ABSTRACT_CLASS(wxDataViewSortedListModel, wxDataViewListModel)
290
291wxDataViewSortedListModel::wxDataViewSortedListModel( wxDataViewListModel *child ) :
292 m_array( wxDataViewIntermediateCmp )
293{
294 m_child = child;
295 s_CmpCol = 0;
296 s_CmpModel = child;
297 s_CmpFunc = wxDataViewListModelSortedDefaultCompare;
f554a14b 298
4eccd3a1
RR
299 m_notifierOnChild = new wxDataViewSortedListModelNotifier( this );
300 m_child->AddNotifier( m_notifierOnChild );
f554a14b
WS
301
302 Resort();
8981608c
RR
303}
304
305wxDataViewSortedListModel::~wxDataViewSortedListModel()
306{
4eccd3a1 307 m_child->RemoveNotifier( m_notifierOnChild );
8981608c
RR
308}
309
8f850e28
RR
310void wxDataViewSortedListModel::Resort()
311{
312 m_array.Clear();
313 size_t n = m_child->GetNumberOfRows();
314 size_t i;
315 for (i = 0; i < n; i++)
316 m_array.Add( i );
317}
318
3f3af7e7
RR
319#if 0
320static void Dump( wxDataViewListModel *model, size_t col )
321{
322 size_t n = model->GetNumberOfRows();
323 size_t i;
324 for (i = 0; i < n; i++)
325 {
326 wxVariant variant;
327 model->GetValue( variant, col, i );
328 wxString tmp;
329 tmp = variant.GetString();
330 wxPrintf( wxT("%d: %s\n"), (int) i, tmp.c_str() );
331 }
332}
333#endif
334
4eccd3a1
RR
335bool wxDataViewSortedListModel::ChildValueChanged( size_t col, size_t row )
336{
337 size_t i;
338 size_t len = m_array.GetCount();
f554a14b 339
4eccd3a1
RR
340 // Remove and readd sorted. Find out at which
341 // position it was and where it ended.
342 size_t start_pos = 0,end_pos = 0;
343 for (i = 0; i < len; i++)
344 if (m_array[i] == row)
345 {
346 start_pos = i;
347 break;
348 }
3f3af7e7 349 m_array.RemoveAt( start_pos );
4eccd3a1 350 m_array.Add( row );
f554a14b 351
4eccd3a1
RR
352 for (i = 0; i < len; i++)
353 if (m_array[i] == row)
354 {
355 end_pos = i;
356 break;
357 }
f554a14b 358
4eccd3a1
RR
359 if (end_pos == start_pos)
360 return wxDataViewListModel::ValueChanged( col, start_pos );
f554a14b 361
4eccd3a1
RR
362 // Create an array where order[old] -> new_pos, so that
363 // if nothing changed order[0] -> 0 etc.
364 size_t *order = new size_t[ len ];
365 // Fill up initial values.
366 for (i = 0; i < len; i++)
367 order[i] = i;
368
369 if (start_pos < end_pos)
370 {
371 for (i = start_pos; i < end_pos; i++)
372 order[i] = order[i+1];
373 order[end_pos] = start_pos;
374 }
375 else
376 {
377 for (i = end_pos; i > start_pos; i--)
378 order[i] = order[i-1];
379 order[start_pos] = end_pos;
f554a14b
WS
380 }
381
3f3af7e7 382 wxDataViewListModel::RowsReordered( order );
f554a14b 383
4eccd3a1 384 delete [] order;
f554a14b 385
4eccd3a1
RR
386 return true;
387}
388
8981608c
RR
389size_t wxDataViewSortedListModel::GetNumberOfRows()
390{
3f3af7e7 391 return m_array.GetCount();
8981608c
RR
392}
393
394size_t wxDataViewSortedListModel::GetNumberOfCols()
395{
396 return m_child->GetNumberOfCols();
397}
398
399wxString wxDataViewSortedListModel::GetColType( size_t col )
400{
401 return m_child->GetColType( col );
402}
403
3f3af7e7 404void wxDataViewSortedListModel::GetValue( wxVariant &variant, size_t col, size_t row )
8981608c
RR
405{
406 size_t child_row = m_array[row];
3f3af7e7 407 m_child->GetValue( variant, col, child_row );
8981608c
RR
408}
409
410bool wxDataViewSortedListModel::SetValue( wxVariant &variant, size_t col, size_t row )
411{
412 size_t child_row = m_array[row];
413 bool ret = m_child->SetValue( variant, col, child_row );
f554a14b 414
4eccd3a1 415 // Resort in ::ChildValueChanged() which gets reported back.
f554a14b 416
8981608c
RR
417 return ret;
418}
419
420bool wxDataViewSortedListModel::RowAppended()
421{
422 // you can only append
423 bool ret = m_child->RowAppended();
f554a14b 424
8981608c 425 // report RowInsrted
f554a14b 426
8981608c
RR
427 return ret;
428}
429
430bool wxDataViewSortedListModel::RowPrepended()
431{
432 // you can only append
433 bool ret = m_child->RowAppended();
f554a14b 434
8981608c 435 // report RowInsrted
f554a14b 436
8981608c
RR
437 return ret;
438}
439
f554a14b 440bool wxDataViewSortedListModel::RowInserted( size_t WXUNUSED(before) )
8981608c
RR
441{
442 // you can only append
443 bool ret = m_child->RowAppended();
f554a14b 444
8981608c 445 // report different RowInsrted
f554a14b 446
8981608c
RR
447 return ret;
448}
449
450bool wxDataViewSortedListModel::RowDeleted( size_t row )
451{
452 size_t child_row = m_array[row];
f554a14b 453
8981608c 454 bool ret = m_child->RowDeleted( child_row );
f554a14b 455
4eccd3a1
RR
456 // Do nothing here as the change in the
457 // child model will be reported back.
f554a14b 458
8981608c
RR
459 return ret;
460}
461
462bool wxDataViewSortedListModel::RowChanged( size_t row )
463{
464 size_t child_row = m_array[row];
465 bool ret = m_child->RowChanged( child_row );
f554a14b 466
4eccd3a1
RR
467 // Do nothing here as the change in the
468 // child model will be reported back.
f554a14b 469
8981608c
RR
470 return ret;
471}
472
473bool wxDataViewSortedListModel::ValueChanged( size_t col, size_t row )
474{
475 size_t child_row = m_array[row];
476 bool ret = m_child->ValueChanged( col, child_row );
f554a14b 477
4eccd3a1
RR
478 // Do nothing here as the change in the
479 // child model will be reported back.
f554a14b 480
8981608c
RR
481 return ret;
482}
483
f554a14b 484bool wxDataViewSortedListModel::RowsReordered( size_t *WXUNUSED(new_order) )
4eccd3a1
RR
485{
486 // We sort them ourselves.
487
488 return false;
489}
490
8981608c
RR
491bool wxDataViewSortedListModel::Cleared()
492{
493 bool ret = m_child->Cleared();
f554a14b 494
8981608c 495 wxDataViewListModel::Cleared();
f554a14b 496
8981608c
RR
497 return ret;
498}
499
f554a14b 500// ---------------------------------------------------------
6842a71a 501// wxDataViewCellBase
f554a14b 502// ---------------------------------------------------------
6842a71a
RR
503
504IMPLEMENT_ABSTRACT_CLASS(wxDataViewCellBase, wxObject)
505
506wxDataViewCellBase::wxDataViewCellBase( const wxString &varianttype, wxDataViewCellMode mode )
507{
508 m_variantType = varianttype;
509 m_mode = mode;
510}
f554a14b
WS
511
512// ---------------------------------------------------------
fa28826d 513// wxDataViewColumnBase
f554a14b 514// ---------------------------------------------------------
fa28826d
RR
515
516IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase, wxObject)
517
6842a71a 518wxDataViewColumnBase::wxDataViewColumnBase( const wxString &title, wxDataViewCell *cell, size_t model_column, int flags)
fa28826d 519{
6842a71a
RR
520 m_cell = cell;
521 m_model_column = model_column;
fa28826d
RR
522 m_flags = flags;
523 m_title = title;
6842a71a
RR
524 m_owner = NULL;
525 m_cell->SetOwner( (wxDataViewColumn*) this );
526}
527
528wxDataViewColumnBase::~wxDataViewColumnBase()
529{
530 if (m_cell)
531 delete m_cell;
f554a14b 532
b5d777c7
RR
533 if (GetOwner())
534 {
535 GetOwner()->GetModel()->RemoveViewingColumn( (wxDataViewColumn*) this );
536 }
fa28826d
RR
537}
538
539void wxDataViewColumnBase::SetTitle( const wxString &title )
540{
541 m_title = title;
542}
543
544wxString wxDataViewColumnBase::GetTitle()
545{
546 return m_title;
547}
548
f554a14b 549// ---------------------------------------------------------
239eaa41 550// wxDataViewCtrlBase
f554a14b 551// ---------------------------------------------------------
239eaa41
RR
552
553IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl)
554
555wxDataViewCtrlBase::wxDataViewCtrlBase()
556{
557 m_model = NULL;
fa28826d 558 m_cols.DeleteContents( true );
239eaa41
RR
559}
560
561wxDataViewCtrlBase::~wxDataViewCtrlBase()
562{
239eaa41
RR
563}
564
6e2e590f 565bool wxDataViewCtrlBase::AssociateModel( wxDataViewListModel *model )
239eaa41 566{
239eaa41 567 m_model = model;
f554a14b 568
239eaa41
RR
569 return true;
570}
571
6e2e590f 572wxDataViewListModel* wxDataViewCtrlBase::GetModel()
239eaa41
RR
573{
574 return m_model;
575}
576
605c2c4a 577bool wxDataViewCtrlBase::AppendTextColumn( const wxString &label, size_t model_column )
fa28826d 578{
6842a71a 579 return AppendColumn( new wxDataViewColumn( label, new wxDataViewTextCell(), model_column ) );
fa28826d
RR
580}
581
605c2c4a
RR
582bool wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, size_t model_column )
583{
584 return AppendColumn( new wxDataViewColumn( label, new wxDataViewToggleCell(), model_column ) );
585}
586
ad63bf41
RR
587bool wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, size_t model_column )
588{
589 return AppendColumn( new wxDataViewColumn( label, new wxDataViewProgressCell(), model_column ) );
590}
591
4d496ecb
RR
592bool wxDataViewCtrlBase::AppendDateColumn( const wxString &label, size_t model_column )
593{
594 return AppendColumn( new wxDataViewColumn( label, new wxDataViewDateCell(), model_column ) );
595}
596
fa28826d
RR
597bool wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
598{
599 m_cols.Append( (wxObject*) col );
6842a71a 600 col->SetOwner( (wxDataViewCtrl*) this );
b5d777c7 601 m_model->AddViewingColumn( col, col->GetModelColumn() );
fa28826d
RR
602 return true;
603}
604
605size_t wxDataViewCtrlBase::GetNumberOfColumns()
606{
607 return m_cols.GetCount();
608}
609
f554a14b 610bool wxDataViewCtrlBase::DeleteColumn( size_t WXUNUSED(pos) )
fa28826d
RR
611{
612 return false;
613}
614
615bool wxDataViewCtrlBase::ClearColumns()
616{
617 return false;
618}
619
620wxDataViewColumn* wxDataViewCtrlBase::GetColumn( size_t pos )
621{
622 return (wxDataViewColumn*) m_cols[ pos ];
623}
624
8045736e 625#endif