]> git.saurik.com Git - wxWidgets.git/blame - src/common/datavcmn.cpp
bitmap button size was too small when uxtheme functions were used to get the margins
[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
e7445ff8
PC
20#include "wx/dataview.h"
21
f554a14b 22#ifndef WX_PRECOMP
f554a14b
WS
23 #include "wx/log.h"
24#endif
25
4a2e5ee8 26const wxChar wxDataViewCtrlNameStr[] = wxT("dataviewCtrl");
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
85e5bb6a 54 wxList::compatibility_iterator node = m_notifiers.GetFirst();
8f850e28
RR
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
85e5bb6a 70 wxList::compatibility_iterator node = m_notifiers.GetFirst();
8f850e28
RR
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
85e5bb6a 86 wxList::compatibility_iterator node = m_notifiers.GetFirst();
8f850e28
RR
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
85e5bb6a 102 wxList::compatibility_iterator node = m_notifiers.GetFirst();
8f850e28
RR
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
85e5bb6a 118 wxList::compatibility_iterator node = m_notifiers.GetFirst();
8f850e28
RR
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
85e5bb6a 134 wxList::compatibility_iterator 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
85e5bb6a 150 wxList::compatibility_iterator node = m_notifiers.GetFirst();
4eccd3a1
RR
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
85e5bb6a 166 wxList::compatibility_iterator node = m_notifiers.GetFirst();
8f850e28
RR
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{
85e5bb6a 185 wxList::compatibility_iterator node = m_viewingColumns.GetFirst();
b5d777c7
RR
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
27fd2cb6
VZ
518wxDataViewColumnBase::wxDataViewColumnBase(const wxString& title,
519 wxDataViewCell *cell,
520 size_t model_column,
521 int WXUNUSED(fixed_width),
522 wxDataViewColumnSizing WXUNUSED(sizing),
523 int flags )
fa28826d 524{
6842a71a
RR
525 m_cell = cell;
526 m_model_column = model_column;
fa28826d
RR
527 m_flags = flags;
528 m_title = title;
6842a71a
RR
529 m_owner = NULL;
530 m_cell->SetOwner( (wxDataViewColumn*) this );
531}
532
533wxDataViewColumnBase::~wxDataViewColumnBase()
534{
535 if (m_cell)
536 delete m_cell;
f554a14b 537
b5d777c7
RR
538 if (GetOwner())
539 {
540 GetOwner()->GetModel()->RemoveViewingColumn( (wxDataViewColumn*) this );
541 }
fa28826d
RR
542}
543
544void wxDataViewColumnBase::SetTitle( const wxString &title )
545{
546 m_title = title;
547}
548
549wxString wxDataViewColumnBase::GetTitle()
550{
551 return m_title;
552}
553
f554a14b 554// ---------------------------------------------------------
239eaa41 555// wxDataViewCtrlBase
f554a14b 556// ---------------------------------------------------------
239eaa41
RR
557
558IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl)
559
560wxDataViewCtrlBase::wxDataViewCtrlBase()
561{
562 m_model = NULL;
fa28826d 563 m_cols.DeleteContents( true );
239eaa41
RR
564}
565
566wxDataViewCtrlBase::~wxDataViewCtrlBase()
567{
239eaa41
RR
568}
569
6e2e590f 570bool wxDataViewCtrlBase::AssociateModel( wxDataViewListModel *model )
239eaa41 571{
239eaa41 572 m_model = model;
f554a14b 573
239eaa41
RR
574 return true;
575}
576
6e2e590f 577wxDataViewListModel* wxDataViewCtrlBase::GetModel()
239eaa41
RR
578{
579 return m_model;
580}
581
605c2c4a 582bool wxDataViewCtrlBase::AppendTextColumn( const wxString &label, size_t model_column )
fa28826d 583{
6842a71a 584 return AppendColumn( new wxDataViewColumn( label, new wxDataViewTextCell(), model_column ) );
fa28826d
RR
585}
586
605c2c4a
RR
587bool wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, size_t model_column )
588{
533544f2 589 return AppendColumn( new wxDataViewColumn( label, new wxDataViewToggleCell(), model_column, 30 ) );
605c2c4a
RR
590}
591
ad63bf41
RR
592bool wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, size_t model_column )
593{
533544f2 594 return AppendColumn( new wxDataViewColumn( label, new wxDataViewProgressCell(), model_column, 70 ) );
ad63bf41
RR
595}
596
4d496ecb
RR
597bool wxDataViewCtrlBase::AppendDateColumn( const wxString &label, size_t model_column )
598{
599 return AppendColumn( new wxDataViewColumn( label, new wxDataViewDateCell(), model_column ) );
600}
601
fa28826d
RR
602bool wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
603{
604 m_cols.Append( (wxObject*) col );
6842a71a 605 col->SetOwner( (wxDataViewCtrl*) this );
b5d777c7 606 m_model->AddViewingColumn( col, col->GetModelColumn() );
fa28826d
RR
607 return true;
608}
609
610size_t wxDataViewCtrlBase::GetNumberOfColumns()
611{
612 return m_cols.GetCount();
613}
614
f554a14b 615bool wxDataViewCtrlBase::DeleteColumn( size_t WXUNUSED(pos) )
fa28826d
RR
616{
617 return false;
618}
619
620bool wxDataViewCtrlBase::ClearColumns()
621{
622 return false;
623}
624
625wxDataViewColumn* wxDataViewCtrlBase::GetColumn( size_t pos )
626{
627 return (wxDataViewColumn*) m_cols[ pos ];
628}
629
8045736e 630#endif