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