]> git.saurik.com Git - wxWidgets.git/blame - src/common/datavcmn.cpp
suppress Sun CC warnings about casting between C++ and extern C function pointers
[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{
8f850e28 35 m_notifiers.DeleteContents( true );
239eaa41
RR
36}
37
38wxDataViewListModel::~wxDataViewListModel()
39{
239eaa41
RR
40}
41
42bool wxDataViewListModel::RowAppended()
43{
8f850e28
RR
44 bool ret = true;
45
85e5bb6a 46 wxList::compatibility_iterator node = m_notifiers.GetFirst();
8f850e28
RR
47 while (node)
48 {
49 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
50 if (!notifier->RowAppended())
51 ret = false;
52 node = node->GetNext();
53 }
f554a14b 54
8f850e28 55 return ret;
239eaa41
RR
56}
57
58bool wxDataViewListModel::RowPrepended()
59{
8f850e28
RR
60 bool ret = true;
61
85e5bb6a 62 wxList::compatibility_iterator node = m_notifiers.GetFirst();
8f850e28
RR
63 while (node)
64 {
65 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
66 if (!notifier->RowPrepended())
67 ret = false;
68 node = node->GetNext();
69 }
f554a14b 70
8f850e28 71 return ret;
239eaa41
RR
72}
73
0a71f9e9 74bool wxDataViewListModel::RowInserted( unsigned int before )
239eaa41 75{
8f850e28
RR
76 bool ret = true;
77
85e5bb6a 78 wxList::compatibility_iterator node = m_notifiers.GetFirst();
8f850e28
RR
79 while (node)
80 {
81 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
82 if (!notifier->RowInserted(before))
83 ret = false;
84 node = node->GetNext();
85 }
f554a14b 86
8f850e28 87 return ret;
239eaa41
RR
88}
89
0a71f9e9 90bool wxDataViewListModel::RowDeleted( unsigned int row )
239eaa41 91{
8f850e28
RR
92 bool ret = true;
93
85e5bb6a 94 wxList::compatibility_iterator node = m_notifiers.GetFirst();
8f850e28
RR
95 while (node)
96 {
97 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
98 if (!notifier->RowDeleted( row ))
99 ret = false;
100 node = node->GetNext();
101 }
f554a14b 102
8f850e28 103 return ret;
239eaa41
RR
104}
105
0a71f9e9 106bool wxDataViewListModel::RowChanged( unsigned int row )
239eaa41 107{
8f850e28
RR
108 bool ret = true;
109
85e5bb6a 110 wxList::compatibility_iterator node = m_notifiers.GetFirst();
8f850e28
RR
111 while (node)
112 {
113 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
114 if (!notifier->RowChanged( row ))
115 ret = false;
116 node = node->GetNext();
117 }
f554a14b 118
8f850e28 119 return ret;
239eaa41
RR
120}
121
0a71f9e9 122bool wxDataViewListModel::ValueChanged( unsigned int col, unsigned int row )
239eaa41 123{
8f850e28 124 bool ret = true;
b5d777c7 125
85e5bb6a 126 wxList::compatibility_iterator node = m_notifiers.GetFirst();
b5d777c7
RR
127 while (node)
128 {
8f850e28
RR
129 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
130 if (!notifier->ValueChanged( col, row ))
131 ret = false;
b5d777c7
RR
132 node = node->GetNext();
133 }
f554a14b 134
b5d777c7 135 return ret;
239eaa41
RR
136}
137
0a71f9e9 138bool wxDataViewListModel::RowsReordered( unsigned int *new_order )
4eccd3a1
RR
139{
140 bool ret = true;
141
85e5bb6a 142 wxList::compatibility_iterator node = m_notifiers.GetFirst();
4eccd3a1
RR
143 while (node)
144 {
145 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
146 if (!notifier->RowsReordered( new_order ))
147 ret = false;
148 node = node->GetNext();
149 }
f554a14b 150
4eccd3a1
RR
151 return ret;
152}
153
239eaa41
RR
154bool wxDataViewListModel::Cleared()
155{
8f850e28
RR
156 bool ret = true;
157
85e5bb6a 158 wxList::compatibility_iterator node = m_notifiers.GetFirst();
8f850e28
RR
159 while (node)
160 {
161 wxDataViewListModelNotifier* notifier = (wxDataViewListModelNotifier*) node->GetData();
162 if (!notifier->Cleared())
163 ret = false;
164 node = node->GetNext();
165 }
f554a14b 166
8f850e28 167 return ret;
239eaa41
RR
168}
169
8f850e28 170void wxDataViewListModel::AddNotifier( wxDataViewListModelNotifier *notifier )
239eaa41 171{
8f850e28
RR
172 m_notifiers.Append( notifier );
173 notifier->SetOwner( this );
239eaa41
RR
174}
175
8f850e28 176void wxDataViewListModel::RemoveNotifier( wxDataViewListModelNotifier *notifier )
239eaa41 177{
8f850e28 178 m_notifiers.DeleteObject( notifier );
239eaa41
RR
179}
180
f554a14b 181// ---------------------------------------------------------
4eccd3a1 182// wxDataViewSortedListModelNotifier
f554a14b 183// ---------------------------------------------------------
4eccd3a1
RR
184
185class wxDataViewSortedListModelNotifier: public wxDataViewListModelNotifier
186{
187public:
188 wxDataViewSortedListModelNotifier( wxDataViewSortedListModel *model )
4627af27 189 { m_model = model; }
f7ed8c89 190
4627af27
RR
191 virtual bool RowAppended()
192 { return m_model->ChildRowAppended(); }
9969fdfe 193
4627af27
RR
194 virtual bool RowPrepended()
195 { return m_model->ChildRowPrepended(); }
9969fdfe 196
0a71f9e9 197 virtual bool RowInserted( unsigned int before )
4627af27 198 { return m_model->ChildRowInserted( before ); }
9969fdfe 199
0a71f9e9 200 virtual bool RowDeleted( unsigned int row )
4627af27 201 { return m_model->ChildRowDeleted( row ); }
9969fdfe 202
0a71f9e9 203 virtual bool RowChanged( unsigned int row )
4627af27 204 { return m_model->ChildRowChanged( row ); }
9969fdfe 205
0a71f9e9 206 virtual bool ValueChanged( unsigned int col, unsigned int row )
4627af27 207 { return m_model->ChildValueChanged( col, row); }
9969fdfe 208
0a71f9e9 209 virtual bool RowsReordered( unsigned int *new_order )
9969fdfe
WS
210 { return m_model->ChildRowsReordered( new_order ); }
211
4627af27
RR
212 virtual bool Cleared()
213 { return m_model->ChildCleared(); }
f554a14b 214
4eccd3a1
RR
215 wxDataViewSortedListModel *m_model;
216};
217
f554a14b 218// ---------------------------------------------------------
4eccd3a1 219// wxDataViewSortedListModel compare function
f554a14b 220// ---------------------------------------------------------
8981608c
RR
221
222int wxCALLBACK wxDataViewListModelSortedDefaultCompare
0a71f9e9 223 (unsigned int row1, unsigned int row2, unsigned int col, wxDataViewListModel* model )
8981608c 224{
3f3af7e7
RR
225 wxVariant value1,value2;
226 model->GetValue( value1, col, row1 );
227 model->GetValue( value2, col, row2 );
8981608c
RR
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
31fb32e1
RR
260int wxCALLBACK wxDataViewListModelSortedDefaultCompareDescending
261 (unsigned int row1, unsigned int row2, unsigned int col, wxDataViewListModel* model )
262{
263 return wxDataViewListModelSortedDefaultCompare( row2, row1, col, model );
264}
265
8981608c
RR
266static wxDataViewListModelCompare s_CmpFunc;
267static wxDataViewListModel *s_CmpModel;
31fb32e1 268static unsigned int s_CmpCol;
8981608c 269
0a71f9e9 270int LINKAGEMODE wxDataViewIntermediateCmp( unsigned int row1, unsigned int row2 )
8981608c
RR
271{
272 return s_CmpFunc( row1, row2, s_CmpCol, s_CmpModel );
273}
274
f554a14b 275// ---------------------------------------------------------
4eccd3a1
RR
276// wxDataViewSortedListModel
277// ---------------------------------------------------------
8981608c 278
8981608c
RR
279wxDataViewSortedListModel::wxDataViewSortedListModel( wxDataViewListModel *child ) :
280 m_array( wxDataViewIntermediateCmp )
281{
282 m_child = child;
31fb32e1
RR
283
284 m_ascending = true;
f554a14b 285
4eccd3a1
RR
286 m_notifierOnChild = new wxDataViewSortedListModelNotifier( this );
287 m_child->AddNotifier( m_notifierOnChild );
c7cf729f
RR
288
289 m_child->IncRef();
f554a14b
WS
290
291 Resort();
8981608c
RR
292}
293
294wxDataViewSortedListModel::~wxDataViewSortedListModel()
295{
c7cf729f 296 DetachChild();
f7ed8c89
RR
297}
298
299void wxDataViewSortedListModel::DetachChild()
300{
c7cf729f
RR
301 if (m_child)
302 {
303 m_child->RemoveNotifier( m_notifierOnChild );
304 m_child->DecRef();
305 }
306
f7ed8c89 307 m_child = NULL;
8981608c
RR
308}
309
31fb32e1
RR
310// FIXME
311void 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
8f850e28
RR
321void wxDataViewSortedListModel::Resort()
322{
31fb32e1
RR
323 InitStatics();
324
c7cf729f
RR
325 if (!m_child) return;
326
9861f022 327 unsigned int n = m_child->GetRowCount();
c7cf729f 328
0a71f9e9 329 unsigned int i;
c7cf729f
RR
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();
8f850e28
RR
346 for (i = 0; i < n; i++)
347 m_array.Add( i );
c7cf729f
RR
348
349 unsigned int *order = new unsigned int[ n ];
c3112d56 350
c7cf729f
RR
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;
8f850e28
RR
367}
368
3f3af7e7 369#if 0
0a71f9e9 370static void Dump( wxDataViewListModel *model, unsigned int col )
3f3af7e7 371{
9861f022 372 unsigned int n = model->GetRowCount();
0a71f9e9 373 unsigned int i;
3f3af7e7
RR
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
4627af27
RR
385bool wxDataViewSortedListModel::ChildRowAppended()
386{
387 // no need to fix up array
9969fdfe 388
0a71f9e9 389 unsigned int len = m_array.GetCount();
9969fdfe 390
0a71f9e9 391 unsigned int pos = m_array.Add( len );
9969fdfe 392
4627af27
RR
393 if (pos == 0)
394 return wxDataViewListModel::RowPrepended();
9969fdfe 395
4627af27
RR
396 if (pos == len)
397 return wxDataViewListModel::RowAppended();
9969fdfe 398
4627af27
RR
399 return wxDataViewListModel::RowInserted( pos );
400}
401
402bool wxDataViewSortedListModel::ChildRowPrepended()
403{
404 // fix up array
0a71f9e9
RR
405 unsigned int i;
406 unsigned int len = m_array.GetCount();
4627af27
RR
407 for (i = 0; i < len; i++)
408 {
0a71f9e9 409 unsigned int value = m_array[i];
4627af27
RR
410 m_array[i] = value+1;
411 }
9969fdfe 412
0a71f9e9 413 unsigned int pos = m_array.Add( 0 );
9969fdfe 414
4627af27
RR
415 if (pos == 0)
416 return wxDataViewListModel::RowPrepended();
9969fdfe 417
4627af27
RR
418 if (pos == len)
419 return wxDataViewListModel::RowAppended();
9969fdfe 420
4627af27
RR
421 return wxDataViewListModel::RowInserted( pos );
422}
423
0a71f9e9 424bool wxDataViewSortedListModel::ChildRowInserted( unsigned int before )
4627af27
RR
425{
426 // fix up array
0a71f9e9
RR
427 unsigned int i;
428 unsigned int len = m_array.GetCount();
4627af27
RR
429 for (i = 0; i < len; i++)
430 {
0a71f9e9 431 unsigned int value = m_array[i];
4627af27
RR
432 if (value >= before)
433 m_array[i] = value+1;
434 }
9969fdfe 435
0a71f9e9 436 unsigned int pos = m_array.Add( before );
9969fdfe 437
4627af27
RR
438 if (pos == 0)
439 return wxDataViewListModel::RowPrepended();
9969fdfe 440
4627af27
RR
441 if (pos == len)
442 return wxDataViewListModel::RowAppended();
9969fdfe 443
4627af27
RR
444 return wxDataViewListModel::RowInserted( pos );
445}
446
0a71f9e9 447bool wxDataViewSortedListModel::ChildRowDeleted( unsigned int row )
4627af27 448{
0a71f9e9
RR
449 unsigned int i;
450 unsigned int len = m_array.GetCount();
4627af27
RR
451 int pos = -1;
452 for (i = 0; i < len; i++)
453 {
0a71f9e9 454 unsigned int value = m_array[i];
4627af27
RR
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 }
9969fdfe 467
4627af27
RR
468 if (pos == -1)
469 return false; // we should probably assert
470
9969fdfe 471 // remove
0a71f9e9 472 m_array.RemoveAt( (unsigned int) pos );
4627af27 473
0a71f9e9 474 return wxDataViewListModel::RowDeleted( (unsigned int) pos);
4627af27
RR
475}
476
0a71f9e9 477bool wxDataViewSortedListModel::ChildRowChanged( unsigned int row )
4627af27 478{
0a71f9e9
RR
479 unsigned int i;
480 unsigned int len = m_array.GetCount();
4627af27
RR
481
482 // Remove and readd sorted. Find out at which
483 // position it was and where it ended.
0a71f9e9 484 unsigned int start_pos = 0,end_pos = 0;
4627af27
RR
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.
0a71f9e9 506 unsigned int *order = new unsigned int[ len ];
4627af27
RR
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
0a71f9e9 531bool wxDataViewSortedListModel::ChildValueChanged( unsigned int col, unsigned int row )
4eccd3a1 532{
0a71f9e9
RR
533 unsigned int i;
534 unsigned int len = m_array.GetCount();
f554a14b 535
4eccd3a1
RR
536 // Remove and readd sorted. Find out at which
537 // position it was and where it ended.
0a71f9e9 538 unsigned int start_pos = 0,end_pos = 0;
4eccd3a1
RR
539 for (i = 0; i < len; i++)
540 if (m_array[i] == row)
541 {
542 start_pos = i;
543 break;
544 }
3f3af7e7 545 m_array.RemoveAt( start_pos );
4eccd3a1 546 m_array.Add( row );
f554a14b 547
4eccd3a1
RR
548 for (i = 0; i < len; i++)
549 if (m_array[i] == row)
550 {
551 end_pos = i;
552 break;
553 }
f554a14b 554
4eccd3a1
RR
555 if (end_pos == start_pos)
556 return wxDataViewListModel::ValueChanged( col, start_pos );
f554a14b 557
4eccd3a1
RR
558 // Create an array where order[old] -> new_pos, so that
559 // if nothing changed order[0] -> 0 etc.
0a71f9e9 560 unsigned int *order = new unsigned int[ len ];
4eccd3a1
RR
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;
f554a14b
WS
576 }
577
3f3af7e7 578 wxDataViewListModel::RowsReordered( order );
f554a14b 579
4eccd3a1 580 delete [] order;
f554a14b 581
4eccd3a1
RR
582 return true;
583}
584
0a71f9e9 585bool wxDataViewSortedListModel::ChildRowsReordered( unsigned int *WXUNUSED(new_order) )
4627af27
RR
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
593bool wxDataViewSortedListModel::ChildCleared()
594{
595 return wxDataViewListModel::Cleared();
596}
597
9861f022 598unsigned int wxDataViewSortedListModel::GetRowCount() const
8981608c 599{
c7cf729f
RR
600 if (!m_child) return 0;
601
602 return m_child->GetRowCount();
8981608c
RR
603}
604
9861f022 605unsigned int wxDataViewSortedListModel::GetColumnCount() const
8981608c 606{
c7cf729f
RR
607 if (!m_child) return 0;
608
9861f022 609 return m_child->GetColumnCount();
8981608c
RR
610}
611
9861f022 612wxString wxDataViewSortedListModel::GetColumnType( unsigned int col ) const
8981608c 613{
9861f022 614 return m_child->GetColumnType( col );
8981608c
RR
615}
616
9861f022 617void wxDataViewSortedListModel::GetValue( wxVariant &variant, unsigned int col, unsigned int row ) const
8981608c 618{
0a71f9e9 619 unsigned int child_row = m_array[row];
3f3af7e7 620 m_child->GetValue( variant, col, child_row );
8981608c
RR
621}
622
2fa73716 623bool wxDataViewSortedListModel::SetValue( const wxVariant &variant, unsigned int col, unsigned int row )
8981608c 624{
0a71f9e9 625 unsigned int child_row = m_array[row];
8981608c 626 bool ret = m_child->SetValue( variant, col, child_row );
f554a14b 627
4627af27
RR
628 // Do nothing here as the change in the
629 // child model will be reported back.
f554a14b 630
8981608c
RR
631 return ret;
632}
633
634bool wxDataViewSortedListModel::RowAppended()
635{
636 // you can only append
637 bool ret = m_child->RowAppended();
f554a14b 638
4627af27
RR
639 // Do nothing here as the change in the
640 // child model will be reported back.
f554a14b 641
8981608c
RR
642 return ret;
643}
644
645bool wxDataViewSortedListModel::RowPrepended()
646{
647 // you can only append
648 bool ret = m_child->RowAppended();
f554a14b 649
4627af27
RR
650 // Do nothing here as the change in the
651 // child model will be reported back.
f554a14b 652
8981608c
RR
653 return ret;
654}
655
0a71f9e9 656bool wxDataViewSortedListModel::RowInserted( unsigned int WXUNUSED(before) )
8981608c
RR
657{
658 // you can only append
659 bool ret = m_child->RowAppended();
f554a14b 660
4627af27
RR
661 // Do nothing here as the change in the
662 // child model will be reported back.
f554a14b 663
8981608c
RR
664 return ret;
665}
666
0a71f9e9 667bool wxDataViewSortedListModel::RowDeleted( unsigned int row )
8981608c 668{
0a71f9e9 669 unsigned int child_row = m_array[row];
f554a14b 670
8981608c 671 bool ret = m_child->RowDeleted( child_row );
f554a14b 672
4eccd3a1
RR
673 // Do nothing here as the change in the
674 // child model will be reported back.
f554a14b 675
8981608c
RR
676 return ret;
677}
678
0a71f9e9 679bool wxDataViewSortedListModel::RowChanged( unsigned int row )
8981608c 680{
0a71f9e9 681 unsigned int child_row = m_array[row];
8981608c 682 bool ret = m_child->RowChanged( child_row );
f554a14b 683
4eccd3a1
RR
684 // Do nothing here as the change in the
685 // child model will be reported back.
f554a14b 686
8981608c
RR
687 return ret;
688}
689
0a71f9e9 690bool wxDataViewSortedListModel::ValueChanged( unsigned int col, unsigned int row )
8981608c 691{
0a71f9e9 692 unsigned int child_row = m_array[row];
8981608c 693 bool ret = m_child->ValueChanged( col, child_row );
f554a14b 694
4eccd3a1
RR
695 // Do nothing here as the change in the
696 // child model will be reported back.
f554a14b 697
8981608c
RR
698 return ret;
699}
700
0a71f9e9 701bool wxDataViewSortedListModel::RowsReordered( unsigned int *WXUNUSED(new_order) )
4eccd3a1
RR
702{
703 // We sort them ourselves.
704
705 return false;
706}
707
8981608c
RR
708bool wxDataViewSortedListModel::Cleared()
709{
710 bool ret = m_child->Cleared();
f554a14b 711
4627af27
RR
712 // Do nothing here as the change in the
713 // child model will be reported back.
9969fdfe 714
8981608c
RR
715 return ret;
716}
717
f554a14b 718// ---------------------------------------------------------
baa9ebc4 719// wxDataViewRendererBase
f554a14b 720// ---------------------------------------------------------
6842a71a 721
baa9ebc4 722IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase, wxObject)
6842a71a 723
9861f022
RR
724wxDataViewRendererBase::wxDataViewRendererBase( const wxString &varianttype,
725 wxDataViewCellMode WXUNUSED(mode),
726 int WXUNUSED(align) )
6842a71a
RR
727{
728 m_variantType = varianttype;
1e510b1e
RR
729 m_editorCtrl = NULL;
730 m_row = (unsigned int) -1;
9861f022
RR
731}
732
733const wxDataViewCtrl* wxDataViewRendererBase::GetView() const
734{
735 return wx_const_cast(wxDataViewRendererBase*, this)->GetOwner()->GetOwner();
6842a71a 736}
f554a14b 737
1e510b1e
RR
738bool 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
30715fa1
RR
748 wxDataViewEditorCtrlEvtHandler *handler =
749 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl, (wxDataViewRenderer*) this );
750
751 m_editorCtrl->PushEventHandler( handler );
1e510b1e 752
30715fa1
RR
753#if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
754 handler->SetFocusOnIdle();
755#else
1e510b1e 756 m_editorCtrl->SetFocus();
30715fa1
RR
757#endif
758
1e510b1e
RR
759 return true;
760}
761
762void wxDataViewRendererBase::CancelEditing()
763{
2fa73716 764 wxPendingDelete.Append( m_editorCtrl );
1e510b1e
RR
765
766 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
2fa73716
RR
767
768 // m_editorCtrl->PopEventHandler( true );
1e510b1e
RR
769}
770
771bool wxDataViewRendererBase::FinishEditing()
772{
1e510b1e
RR
773 wxVariant value;
774 GetValueFromEditorCtrl( m_editorCtrl, value );
775
2fa73716 776 wxPendingDelete.Append( m_editorCtrl );
1e510b1e
RR
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
2fa73716
RR
787 // m_editorCtrl->PopEventHandler( true );
788
1e510b1e
RR
789 return true;
790}
791
792//-----------------------------------------------------------------------------
793// wxDataViewEditorCtrlEvtHandler
794//-----------------------------------------------------------------------------
795
796BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler, wxEvtHandler)
797 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar)
798 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus)
30715fa1 799 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle)
1e510b1e
RR
800END_EVENT_TABLE()
801
802wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
803 wxControl *editorCtrl,
804 wxDataViewRenderer *owner )
805{
806 m_owner = owner;
807 m_editorCtrl = editorCtrl;
808
809 m_finished = false;
810}
811
30715fa1
RR
812void 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
1e510b1e
RR
824void 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
843void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent &event )
844{
845 if (!m_finished)
846 {
847 m_finished = true;
848 m_owner->FinishEditing();
849 }
850
1e510b1e
RR
851 event.Skip();
852}
853
f554a14b 854// ---------------------------------------------------------
fa28826d 855// wxDataViewColumnBase
f554a14b 856// ---------------------------------------------------------
fa28826d
RR
857
858IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase, wxObject)
859
9861f022 860wxDataViewColumnBase::wxDataViewColumnBase(const wxString& WXUNUSED(title),
baa9ebc4 861 wxDataViewRenderer *renderer,
0a71f9e9 862 unsigned int model_column,
008b5a66 863 int WXUNUSED(width),
87f0efe2 864 wxAlignment WXUNUSED(align),
9861f022 865 int WXUNUSED(flags))
fa28826d 866{
baa9ebc4 867 m_renderer = renderer;
6842a71a 868 m_model_column = model_column;
6842a71a 869 m_owner = NULL;
baa9ebc4 870 m_renderer->SetOwner( (wxDataViewColumn*) this );
9861f022
RR
871
872 // NOTE: the wxDataViewColumn's ctor must store the width, align, flags
873 // parameters inside the native control!
6842a71a
RR
874}
875
07a84e7b
RR
876wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap& bitmap,
877 wxDataViewRenderer *renderer,
878 unsigned int model_column,
879 int WXUNUSED(width),
87f0efe2 880 wxAlignment WXUNUSED(align),
9861f022 881 int WXUNUSED(flags) )
07a84e7b
RR
882{
883 m_renderer = renderer;
884 m_model_column = model_column;
07a84e7b
RR
885 m_bitmap = bitmap;
886 m_owner = NULL;
887 m_renderer->SetOwner( (wxDataViewColumn*) this );
888}
889
6842a71a
RR
890wxDataViewColumnBase::~wxDataViewColumnBase()
891{
baa9ebc4
RR
892 if (m_renderer)
893 delete m_renderer;
fa28826d
RR
894}
895
9861f022 896int wxDataViewColumnBase::GetFlags() const
fa28826d 897{
9861f022 898 int ret = 0;
fa28826d 899
9861f022
RR
900 if (IsSortable())
901 ret |= wxDATAVIEW_COL_SORTABLE;
902 if (IsResizeable())
903 ret |= wxDATAVIEW_COL_RESIZABLE;
904 if (IsHidden())
905 ret |= wxDATAVIEW_COL_HIDDEN;
fa28826d 906
9861f022 907 return ret;
07a84e7b
RR
908}
909
9861f022 910void wxDataViewColumnBase::SetFlags(int flags)
07a84e7b 911{
9861f022
RR
912 SetSortable((flags & wxDATAVIEW_COL_SORTABLE) != 0);
913 SetResizeable((flags & wxDATAVIEW_COL_RESIZABLE) != 0);
914 SetHidden((flags & wxDATAVIEW_COL_HIDDEN) != 0);
07a84e7b
RR
915}
916
1821abd1
RR
917// ---------------------------------------------------------
918// wxDataViewEventListModelNotifier
919// ---------------------------------------------------------
920
921class WXDLLIMPEXP_ADV wxDataViewEventListModelNotifier: public wxDataViewListModelNotifier
922{
923public:
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 ); }
b01204c9 947 virtual bool RowsReordered( unsigned int * WXUNUSED(new_order) )
1821abd1
RR
948 { return SendEvent( wxEVT_COMMAND_DATAVIEW_MODEL_ROWS_REORDERED ); }
949 virtual bool Cleared() { return SendEvent( wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED ); }
950
951private:
952 wxDataViewCtrl *m_ctrl;
953};
954
9861f022 955
f554a14b 956// ---------------------------------------------------------
239eaa41 957// wxDataViewCtrlBase
f554a14b 958// ---------------------------------------------------------
239eaa41
RR
959
960IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl)
961
962wxDataViewCtrlBase::wxDataViewCtrlBase()
963{
964 m_model = NULL;
fa28826d 965 m_cols.DeleteContents( true );
1821abd1 966 m_eventNotifier = NULL;
239eaa41
RR
967}
968
969wxDataViewCtrlBase::~wxDataViewCtrlBase()
970{
87f0efe2
RR
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 {
1821abd1
RR
978 if (m_eventNotifier)
979 m_model->RemoveNotifier( m_eventNotifier );
980 m_eventNotifier = NULL;
981
87f0efe2
RR
982 m_model->DecRef();
983 m_model = NULL;
984 }
239eaa41
RR
985}
986
6e2e590f 987bool wxDataViewCtrlBase::AssociateModel( wxDataViewListModel *model )
239eaa41 988{
1821abd1
RR
989 if (m_model)
990 {
991 if (m_eventNotifier)
992 m_model->RemoveNotifier( m_eventNotifier );
993 m_eventNotifier = NULL;
994
87f0efe2 995 m_model->DecRef(); // discard old model, if any
1821abd1 996 }
87f0efe2
RR
997
998 // add our own reference to the new model:
239eaa41 999 m_model = model;
1821abd1
RR
1000 if (m_model)
1001 {
87f0efe2 1002 m_model->IncRef();
1821abd1
RR
1003 m_eventNotifier = new wxDataViewEventListModelNotifier( (wxDataViewCtrl*) this );
1004 m_model->AddNotifier( m_eventNotifier );
1005 }
f554a14b 1006
239eaa41
RR
1007 return true;
1008}
1009
6e2e590f 1010wxDataViewListModel* wxDataViewCtrlBase::GetModel()
239eaa41
RR
1011{
1012 return m_model;
1013}
1014
1286b7ba 1015bool wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column,
87f0efe2 1016 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
fa28826d 1017{
1286b7ba 1018 return AppendColumn( new wxDataViewColumn( label,
9861f022
RR
1019 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
1020 model_column, width, align, flags ) );
fa28826d
RR
1021}
1022
1286b7ba 1023bool wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column,
87f0efe2 1024 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
605c2c4a 1025{
1286b7ba 1026 return AppendColumn( new wxDataViewColumn( label,
9861f022
RR
1027 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
1028 model_column, width, align, flags ) );
605c2c4a
RR
1029}
1030
1286b7ba 1031bool wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column,
87f0efe2 1032 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
ad63bf41 1033{
1286b7ba 1034 return AppendColumn( new wxDataViewColumn( label,
9861f022
RR
1035 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
1036 model_column, width, align, flags ) );
ad63bf41
RR
1037}
1038
1286b7ba 1039bool wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column,
87f0efe2 1040 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
4d496ecb 1041{
1286b7ba 1042 return AppendColumn( new wxDataViewColumn( label,
9861f022
RR
1043 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
1044 model_column, width, align, flags ) );
4d496ecb
RR
1045}
1046
1286b7ba 1047bool wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column,
87f0efe2 1048 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1049{
1286b7ba 1050 return AppendColumn( new wxDataViewColumn( label,
9861f022
RR
1051 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
1052 model_column, width, align, flags ) );
07a84e7b
RR
1053}
1054
1286b7ba 1055bool wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1056 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1057{
1286b7ba 1058 return AppendColumn( new wxDataViewColumn( label,
9861f022
RR
1059 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
1060 model_column, width, align, flags ) );
07a84e7b
RR
1061}
1062
1286b7ba 1063bool wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1064 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1065{
1286b7ba 1066 return AppendColumn( new wxDataViewColumn( label,
9861f022
RR
1067 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
1068 model_column, width, align, flags ) );
07a84e7b
RR
1069}
1070
1286b7ba 1071bool wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1072 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1073{
1286b7ba 1074 return AppendColumn( new wxDataViewColumn( label,
9861f022
RR
1075 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
1076 model_column, width, align, flags ) );
07a84e7b
RR
1077}
1078
1286b7ba 1079bool wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1080 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1081{
1286b7ba 1082 return AppendColumn( new wxDataViewColumn( label,
9861f022
RR
1083 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
1084 model_column, width, align, flags ) );
07a84e7b
RR
1085}
1086
1286b7ba 1087bool wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 1088 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 1089{
1286b7ba 1090 return AppendColumn( new wxDataViewColumn( label,
9861f022
RR
1091 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
1092 model_column, width, align, flags ) );
07a84e7b
RR
1093}
1094
fa28826d
RR
1095bool wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
1096{
1097 m_cols.Append( (wxObject*) col );
6842a71a 1098 col->SetOwner( (wxDataViewCtrl*) this );
fa28826d
RR
1099 return true;
1100}
1101
9861f022 1102unsigned int wxDataViewCtrlBase::GetColumnCount() const
fa28826d
RR
1103{
1104 return m_cols.GetCount();
1105}
1106
0a71f9e9 1107bool wxDataViewCtrlBase::DeleteColumn( unsigned int WXUNUSED(pos) )
fa28826d
RR
1108{
1109 return false;
1110}
1111
1112bool wxDataViewCtrlBase::ClearColumns()
1113{
1114 return false;
1115}
1116
0a71f9e9 1117wxDataViewColumn* wxDataViewCtrlBase::GetColumn( unsigned int pos )
fa28826d
RR
1118{
1119 return (wxDataViewColumn*) m_cols[ pos ];
1120}
1121
eb7f97f8
RR
1122// ---------------------------------------------------------
1123// wxDataViewEvent
1124// ---------------------------------------------------------
1125
1126IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent)
1127
1128DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ROW_SELECTED)
f828871d 1129DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ROW_ACTIVATED)
31fb32e1
RR
1130DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK)
1131DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK)
c0a66d92 1132DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED)
eb7f97f8 1133
1821abd1
RR
1134DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ROW_APPENDED)
1135DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ROW_PREPENDED)
1136DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ROW_INSERTED)
1137DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ROW_DELETED)
1138DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ROW_CHANGED)
1139DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED)
1140DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ROWS_REORDERED)
1141DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED)
eb7f97f8 1142
8045736e 1143#endif