]> git.saurik.com Git - wxWidgets.git/blame - src/common/datavcmn.cpp
compilation fix after HandleWindowEvent() changes
[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
52e750fc 22#include "wx/spinctrl.h"
52e750fc 23
f554a14b 24#ifndef WX_PRECOMP
c3c62822
PC
25 #include "wx/dc.h"
26 #include "wx/settings.h"
f554a14b 27 #include "wx/log.h"
e94d0c1e 28 #include "wx/icon.h"
c3c62822 29 #include "wx/crt.h"
f554a14b
WS
30#endif
31
4a2e5ee8 32const wxChar wxDataViewCtrlNameStr[] = wxT("dataviewCtrl");
239eaa41 33
239eaa41 34
d5025dc0
RR
35bool operator == (const wxDataViewItem &left, const wxDataViewItem &right)
36{
37 return (left.GetID() == right.GetID() );
38}
39
c3c62822
PC
40#ifdef __WXDEBUG__
41void wxDataViewItem::Print(const wxString& text) const
42{
43 wxPrintf("item %s: %l\n", text, (long)m_id);
44}
45#endif
d5025dc0 46
f554a14b 47// ---------------------------------------------------------
854cdb09 48// wxDataViewModelNotifier
f554a14b 49// ---------------------------------------------------------
239eaa41 50
8ab03001 51#include "wx/listimpl.cpp"
a76c2f37 52WX_DEFINE_LIST(wxDataViewModelNotifiers)
9d8fe14a 53
854cdb09
RR
54bool wxDataViewModelNotifier::ItemsAdded( const wxDataViewItem &parent, const wxDataViewItemArray &items )
55{
56 size_t count = items.GetCount();
57 size_t i;
58 for (i = 0; i < count; i++)
59 if (!ItemAdded( parent, items[i] )) return false;
b5ec7dd6 60
854cdb09
RR
61 return true;
62}
63
64bool wxDataViewModelNotifier::ItemsDeleted( const wxDataViewItem &parent, const wxDataViewItemArray &items )
65{
66 size_t count = items.GetCount();
67 size_t i;
68 for (i = 0; i < count; i++)
69 if (!ItemDeleted( parent, items[i] )) return false;
b5ec7dd6 70
854cdb09
RR
71 return true;
72}
73
74bool wxDataViewModelNotifier::ItemsChanged( const wxDataViewItemArray &items )
75{
76 size_t count = items.GetCount();
77 size_t i;
78 for (i = 0; i < count; i++)
79 if (!ItemChanged( items[i] )) return false;
b5ec7dd6 80
854cdb09
RR
81 return true;
82}
83
84// ---------------------------------------------------------
85// wxDataViewModel
86// ---------------------------------------------------------
87
e0062c04 88wxDataViewModel::wxDataViewModel()
239eaa41 89{
8f850e28 90 m_notifiers.DeleteContents( true );
239eaa41
RR
91}
92
e0062c04 93bool wxDataViewModel::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
239eaa41 94{
8f850e28
RR
95 bool ret = true;
96
9d8fe14a
RR
97 wxDataViewModelNotifiers::iterator iter;
98 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
8f850e28 99 {
9d8fe14a 100 wxDataViewModelNotifier* notifier = *iter;
e0062c04 101 if (!notifier->ItemAdded( parent, item ))
8f850e28 102 ret = false;
8f850e28 103 }
f554a14b 104
8f850e28 105 return ret;
239eaa41
RR
106}
107
469d3e9b 108bool wxDataViewModel::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
239eaa41 109{
8f850e28
RR
110 bool ret = true;
111
9d8fe14a
RR
112 wxDataViewModelNotifiers::iterator iter;
113 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
8f850e28 114 {
9d8fe14a 115 wxDataViewModelNotifier* notifier = *iter;
469d3e9b 116 if (!notifier->ItemDeleted( parent, item ))
8f850e28 117 ret = false;
8f850e28 118 }
f554a14b 119
8f850e28 120 return ret;
239eaa41
RR
121}
122
e0062c04 123bool wxDataViewModel::ItemChanged( const wxDataViewItem &item )
239eaa41 124{
8f850e28
RR
125 bool ret = true;
126
9d8fe14a
RR
127 wxDataViewModelNotifiers::iterator iter;
128 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
8f850e28 129 {
9d8fe14a 130 wxDataViewModelNotifier* notifier = *iter;
e0062c04 131 if (!notifier->ItemChanged( item ))
8f850e28 132 ret = false;
8f850e28 133 }
f554a14b 134
8f850e28 135 return ret;
239eaa41
RR
136}
137
854cdb09
RR
138bool wxDataViewModel::ItemsAdded( const wxDataViewItem &parent, const wxDataViewItemArray &items )
139{
140 bool ret = true;
141
142 wxDataViewModelNotifiers::iterator iter;
143 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
144 {
145 wxDataViewModelNotifier* notifier = *iter;
146 if (!notifier->ItemsAdded( parent, items ))
147 ret = false;
148 }
149
150 return ret;
151}
152
153bool wxDataViewModel::ItemsDeleted( const wxDataViewItem &parent, const wxDataViewItemArray &items )
154{
155 bool ret = true;
156
157 wxDataViewModelNotifiers::iterator iter;
158 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
159 {
160 wxDataViewModelNotifier* notifier = *iter;
161 if (!notifier->ItemsDeleted( parent, items ))
162 ret = false;
163 }
164
165 return ret;
166}
167
168bool wxDataViewModel::ItemsChanged( const wxDataViewItemArray &items )
169{
170 bool ret = true;
171
172 wxDataViewModelNotifiers::iterator iter;
173 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
174 {
175 wxDataViewModelNotifier* notifier = *iter;
176 if (!notifier->ItemsChanged( items ))
177 ret = false;
178 }
179
180 return ret;
181}
182
e0062c04 183bool wxDataViewModel::ValueChanged( const wxDataViewItem &item, unsigned int col )
239eaa41 184{
8f850e28 185 bool ret = true;
b5d777c7 186
9d8fe14a
RR
187 wxDataViewModelNotifiers::iterator iter;
188 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
b5d777c7 189 {
9d8fe14a 190 wxDataViewModelNotifier* notifier = *iter;
e0062c04 191 if (!notifier->ValueChanged( item, col ))
8f850e28 192 ret = false;
b5d777c7 193 }
f554a14b 194
b5d777c7 195 return ret;
239eaa41
RR
196}
197
e0062c04 198bool wxDataViewModel::Cleared()
4eccd3a1
RR
199{
200 bool ret = true;
201
9d8fe14a
RR
202 wxDataViewModelNotifiers::iterator iter;
203 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
4eccd3a1 204 {
9d8fe14a 205 wxDataViewModelNotifier* notifier = *iter;
8f850e28
RR
206 if (!notifier->Cleared())
207 ret = false;
8f850e28 208 }
f554a14b 209
8f850e28 210 return ret;
239eaa41
RR
211}
212
ef427989
RR
213void wxDataViewModel::Resort()
214{
9d8fe14a
RR
215 wxDataViewModelNotifiers::iterator iter;
216 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
ef427989 217 {
9d8fe14a 218 wxDataViewModelNotifier* notifier = *iter;
ef427989 219 notifier->Resort();
ef427989
RR
220 }
221}
222
e0062c04 223void wxDataViewModel::AddNotifier( wxDataViewModelNotifier *notifier )
239eaa41 224{
9d8fe14a 225 m_notifiers.push_back( notifier );
8f850e28 226 notifier->SetOwner( this );
239eaa41
RR
227}
228
e0062c04 229void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier *notifier )
239eaa41 230{
8f850e28 231 m_notifiers.DeleteObject( notifier );
239eaa41
RR
232}
233
7ee7191c
RR
234int wxDataViewModel::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
235 unsigned int column, bool ascending )
ef427989 236{
cf283a47 237 // sort branches before leaves
ed903e42
RR
238 bool item1_is_container = IsContainer(item1);
239 bool item2_is_container = IsContainer(item2);
900af470 240
ed903e42 241 if (item1_is_container && !item2_is_container)
cf283a47 242 return 1;
ed903e42 243 if (item2_is_container && !item1_is_container)
cf283a47 244 return -1;
900af470 245
ef427989 246 wxVariant value1,value2;
7ee7191c
RR
247 GetValue( value1, item1, column );
248 GetValue( value2, item2, column );
249
250 if (!ascending)
251 {
252 wxVariant temp = value1;
253 value1 = value2;
254 value2 = temp;
255 }
cf283a47 256
ef427989
RR
257 if (value1.GetType() == wxT("string"))
258 {
259 wxString str1 = value1.GetString();
260 wxString str2 = value2.GetString();
0be79c8a 261 int res = str1.Cmp( str2 );
cf283a47
RR
262 if (res) return res;
263 } else
ef427989
RR
264 if (value1.GetType() == wxT("long"))
265 {
266 long l1 = value1.GetLong();
267 long l2 = value2.GetLong();
cf283a47
RR
268 long res = l1-l2;
269 if (res) return res;
270 } else
ef427989
RR
271 if (value1.GetType() == wxT("double"))
272 {
273 double d1 = value1.GetDouble();
274 double d2 = value2.GetDouble();
ef427989 275 if (d1 < d2) return 1;
cf283a47
RR
276 if (d1 > d2) return -1;
277 } else
ef427989
RR
278 if (value1.GetType() == wxT("datetime"))
279 {
280 wxDateTime dt1 = value1.GetDateTime();
281 wxDateTime dt2 = value2.GetDateTime();
ef427989 282 if (dt1.IsEarlierThan(dt2)) return 1;
cf283a47 283 if (dt2.IsEarlierThan(dt1)) return -11;
ef427989
RR
284 }
285
cf283a47 286 // items must be different
900af470
JS
287 unsigned long litem1 = (unsigned long) item1.GetID();
288 unsigned long litem2 = (unsigned long) item2.GetID();
b7fe2261 289
7ee7191c
RR
290 if (!ascending)
291 return litem2-litem2;
cf283a47 292
cf283a47 293 return litem1-litem2;
ef427989
RR
294}
295
c534e696
RR
296// ---------------------------------------------------------
297// wxDataViewIndexListModel
298// ---------------------------------------------------------
299
300wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size )
301{
a3cc79d9 302#ifndef __WXMAC__
2056dede
RR
303 m_useHash = false;
304#else
305 m_useHash = true;
306#endif
517166e6 307
2056dede
RR
308 if (m_useHash)
309 {
310 // IDs are ordered until an item gets deleted or inserted
311 m_ordered = true;
312
313 // build initial index
314 unsigned int i;
315 for (i = 1; i < initial_size+1; i++)
316 m_hash.Add( (void*) i );
317 m_lastIndex = initial_size + 1;
318 }
319 else
320 {
321 m_lastIndex = initial_size-1;
322 }
c534e696
RR
323}
324
325wxDataViewIndexListModel::~wxDataViewIndexListModel()
326{
327}
328
329void wxDataViewIndexListModel::RowPrepended()
330{
2056dede
RR
331 if (m_useHash)
332 {
333 m_ordered = false;
f8cad1a1 334
2056dede
RR
335 unsigned int id = m_lastIndex++;
336 m_hash.Insert( (void*) id, 0 );
337 wxDataViewItem item( (void*) id );
338 ItemAdded( wxDataViewItem(0), item );
339 }
340 else
341 {
342 m_lastIndex++;
343 wxDataViewItem item( (void*) 0 );
344 ItemAdded( wxDataViewItem(0), item );
345 }
c534e696
RR
346}
347
348void wxDataViewIndexListModel::RowInserted( unsigned int before )
349{
2056dede
RR
350 if (m_useHash)
351 {
352 m_ordered = false;
f8cad1a1 353
2056dede
RR
354 unsigned int id = m_lastIndex++;
355 m_hash.Insert( (void*) id, before );
356 wxDataViewItem item( (void*) id );
357 ItemAdded( wxDataViewItem(0), item );
358 }
359 else
360 {
361 m_lastIndex++;
362 wxDataViewItem item( (void*) before );
363 ItemAdded( wxDataViewItem(0), item );
364 }
c534e696
RR
365}
366
367void wxDataViewIndexListModel::RowAppended()
368{
2056dede
RR
369 if (m_useHash)
370 {
371 unsigned int id = m_lastIndex++;
372 m_hash.Add( (void*) id );
373 wxDataViewItem item( (void*) id );
374 ItemAdded( wxDataViewItem(0), item );
375 }
376 else
377 {
378 m_lastIndex++;
379 wxDataViewItem item( (void*) m_lastIndex );
380 ItemAdded( wxDataViewItem(0), item );
381 }
c534e696
RR
382}
383
384void wxDataViewIndexListModel::RowDeleted( unsigned int row )
385{
2056dede
RR
386 if (m_useHash)
387 {
388 wxDataViewItem item( m_hash[row] );
389 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item );
390 m_hash.RemoveAt( row );
391 }
392 else
393 {
394 wxDataViewItem item( (void*) row );
395 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item );
396 m_lastIndex++;
397 }
c534e696
RR
398}
399
400void wxDataViewIndexListModel::RowChanged( unsigned int row )
401{
402 wxDataViewModel::ItemChanged( GetItem(row) );
403}
404
405void wxDataViewIndexListModel::RowValueChanged( unsigned int row, unsigned int col )
406{
407 wxDataViewModel::ValueChanged( GetItem(row), col );
408}
409
410unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem &item ) const
411{
2056dede 412 if (m_useHash)
517166e6 413 {
2056dede
RR
414 if (m_ordered)
415 {
416 unsigned int pos = wxPtrToUInt( item.GetID() );
417 return pos-1;
418 }
517166e6 419
2056dede
RR
420 // assert for not found
421 return (unsigned int) m_hash.Index( item.GetID() );
422 }
423 else
424 {
425 return wxPtrToUInt( item.GetID() );
426 }
c534e696
RR
427}
428
429wxDataViewItem wxDataViewIndexListModel::GetItem( unsigned int row ) const
430{
2056dede
RR
431 if (m_useHash)
432 {
433 wxASSERT( row < m_hash.GetCount() );
434 return wxDataViewItem( m_hash[row] );
435 }
436 else
437 {
438 return wxDataViewItem( (void*) row );
439 }
c534e696
RR
440}
441
517166e6
RR
442bool wxDataViewIndexListModel::HasDefaultCompare() const
443{
444 return !m_ordered;
445}
446
b7fe2261
VZ
447int wxDataViewIndexListModel::Compare(const wxDataViewItem& item1,
448 const wxDataViewItem& item2,
449 unsigned int WXUNUSED(column),
450 bool ascending)
c534e696 451{
2056dede 452 if (m_ordered || !m_useHash)
517166e6 453 {
ec935ba8
PC
454 unsigned int pos1 = wxPtrToUInt(item1.GetID());
455 unsigned int pos2 = wxPtrToUInt(item2.GetID());
517166e6
RR
456
457 if (ascending)
458 return pos1 - pos2;
459 else
460 return pos2 - pos1;
461 }
462
7ee7191c
RR
463 if (ascending)
464 return GetRow(item1) - GetRow(item2);
b7fe2261 465
7ee7191c 466 return GetRow(item2) - GetRow(item1);
c534e696
RR
467}
468
900af470 469void wxDataViewIndexListModel::GetValue( wxVariant &variant,
c534e696
RR
470 const wxDataViewItem &item, unsigned int col ) const
471{
900af470 472 GetValue( variant, GetRow(item), col );
c534e696
RR
473}
474
900af470 475bool wxDataViewIndexListModel::SetValue( const wxVariant &variant,
c534e696
RR
476 const wxDataViewItem &item, unsigned int col )
477{
478 return SetValue( variant, GetRow(item), col );
479}
480
4264606e
RR
481bool wxDataViewIndexListModel::GetAttr( const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr )
482{
483 return GetAttr( GetRow(item), col, attr );
484}
485
900af470 486wxDataViewItem wxDataViewIndexListModel::GetParent( const wxDataViewItem & WXUNUSED(item) ) const
c534e696
RR
487{
488 return wxDataViewItem(0);
489}
490
491bool wxDataViewIndexListModel::IsContainer( const wxDataViewItem &item ) const
492{
493 // only the invisible root item has children
494 if (!item.IsOk())
495 return true;
900af470 496
c534e696
RR
497 return false;
498}
499
74fe973b 500unsigned int wxDataViewIndexListModel::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
c534e696 501{
eff112a6 502 if (!m_useHash)
2056dede
RR
503 return 0; // error
504
74fe973b
RR
505 if (item.IsOk())
506 return 0;
c534e696 507
74fe973b 508 children = m_hash;
b7fe2261 509
74fe973b 510 return m_hash.GetCount();
c534e696
RR
511}
512
89352653
RR
513//-----------------------------------------------------------------------------
514// wxDataViewIconText
515//-----------------------------------------------------------------------------
516
517IMPLEMENT_DYNAMIC_CLASS(wxDataViewIconText,wxObject)
518
d350fbec 519IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxDataViewIconText, WXDLLIMPEXP_ADV)
89352653
RR
520
521bool operator == (const wxDataViewIconText &one, const wxDataViewIconText &two)
522{
523 if (one.GetText() != two.GetText()) return false;
524 if (one.IsSameAs(two)) return false;
525 return true;
526}
527
f554a14b 528// ---------------------------------------------------------
baa9ebc4 529// wxDataViewRendererBase
f554a14b 530// ---------------------------------------------------------
6842a71a 531
baa9ebc4 532IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase, wxObject)
6842a71a 533
900af470 534wxDataViewRendererBase::wxDataViewRendererBase( const wxString &varianttype,
9861f022
RR
535 wxDataViewCellMode WXUNUSED(mode),
536 int WXUNUSED(align) )
6842a71a
RR
537{
538 m_variantType = varianttype;
1e510b1e 539 m_editorCtrl = NULL;
9861f022
RR
540}
541
542const wxDataViewCtrl* wxDataViewRendererBase::GetView() const
543{
544 return wx_const_cast(wxDataViewRendererBase*, this)->GetOwner()->GetOwner();
6842a71a 545}
f554a14b 546
e0062c04 547bool wxDataViewRendererBase::StartEditing( const wxDataViewItem &item, wxRect labelRect )
1e510b1e 548{
e0062c04 549 m_item = item; // remember for later
900af470 550
1e510b1e
RR
551 unsigned int col = GetOwner()->GetModelColumn();
552 wxVariant value;
e0062c04 553 GetOwner()->GetOwner()->GetModel()->GetValue( value, item, col );
900af470 554
1e510b1e 555 m_editorCtrl = CreateEditorCtrl( GetOwner()->GetOwner()->GetMainWindow(), labelRect, value );
900af470
JS
556
557 wxDataViewEditorCtrlEvtHandler *handler =
30715fa1 558 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl, (wxDataViewRenderer*) this );
900af470 559
30715fa1 560 m_editorCtrl->PushEventHandler( handler );
900af470 561
30715fa1
RR
562#if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
563 handler->SetFocusOnIdle();
564#else
1e510b1e 565 m_editorCtrl->SetFocus();
30715fa1
RR
566#endif
567
afebb87b
RR
568 // Now we should send Editing Started event
569 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, GetOwner()->GetOwner()->GetId() );
570 event.SetDataViewColumn( GetOwner() );
571 event.SetModel( GetOwner()->GetOwner()->GetModel() );
572 event.SetItem( item );
afebb87b
RR
573 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event );
574
1e510b1e
RR
575 return true;
576}
577
578void wxDataViewRendererBase::CancelEditing()
579{
2fa73716 580 wxPendingDelete.Append( m_editorCtrl );
900af470 581
1e510b1e 582 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
900af470 583
2fa73716 584 // m_editorCtrl->PopEventHandler( true );
1e510b1e
RR
585}
586
587bool wxDataViewRendererBase::FinishEditing()
588{
1e510b1e
RR
589 wxVariant value;
590 GetValueFromEditorCtrl( m_editorCtrl, value );
591
2fa73716 592 wxPendingDelete.Append( m_editorCtrl );
900af470 593
1e510b1e 594 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
900af470 595
1e510b1e
RR
596 if (!Validate(value))
597 return false;
900af470 598
1e510b1e 599 unsigned int col = GetOwner()->GetModelColumn();
e0062c04
RR
600 GetOwner()->GetOwner()->GetModel()->SetValue( value, m_item, col );
601 GetOwner()->GetOwner()->GetModel()->ValueChanged( m_item, col );
900af470 602
2fa73716 603 // m_editorCtrl->PopEventHandler( true );
b7fe2261 604
afebb87b
RR
605 // Now we should send Editing Done event
606 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, GetOwner()->GetOwner()->GetId() );
607 event.SetDataViewColumn( GetOwner() );
608 event.SetModel( GetOwner()->GetOwner()->GetModel() );
8fdaf7de 609 event.SetItem( m_item );
afebb87b 610 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event );
900af470 611
1e510b1e
RR
612 return true;
613}
614
615//-----------------------------------------------------------------------------
616// wxDataViewEditorCtrlEvtHandler
617//-----------------------------------------------------------------------------
618
619BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler, wxEvtHandler)
620 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar)
621 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus)
30715fa1 622 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle)
1e510b1e
RR
623END_EVENT_TABLE()
624
625wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
626 wxControl *editorCtrl,
627 wxDataViewRenderer *owner )
628{
629 m_owner = owner;
630 m_editorCtrl = editorCtrl;
631
632 m_finished = false;
633}
634
30715fa1
RR
635void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent &event )
636{
637 if (m_focusOnIdle)
638 {
639 m_focusOnIdle = false;
640 if (wxWindow::FindFocus() != m_editorCtrl)
641 m_editorCtrl->SetFocus();
642 }
900af470 643
30715fa1
RR
644 event.Skip();
645}
646
1e510b1e
RR
647void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent &event )
648{
649 switch ( event.m_keyCode )
650 {
651 case WXK_RETURN:
652 m_finished = true;
653 m_owner->FinishEditing();
654 break;
655
656 case WXK_ESCAPE:
657 m_finished = true;
658 m_owner->CancelEditing();
659 break;
660
661 default:
662 event.Skip();
663 }
664}
665
666void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent &event )
667{
668 if (!m_finished)
669 {
670 m_finished = true;
671 m_owner->FinishEditing();
672 }
673
1e510b1e
RR
674 event.Skip();
675}
676
f554a14b 677// ---------------------------------------------------------
fa28826d 678// wxDataViewColumnBase
f554a14b 679// ---------------------------------------------------------
fa28826d
RR
680
681IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase, wxObject)
682
9861f022 683wxDataViewColumnBase::wxDataViewColumnBase(const wxString& WXUNUSED(title),
baa9ebc4 684 wxDataViewRenderer *renderer,
0a71f9e9 685 unsigned int model_column,
008b5a66 686 int WXUNUSED(width),
87f0efe2 687 wxAlignment WXUNUSED(align),
900af470 688 int WXUNUSED(flags))
fa28826d 689{
baa9ebc4 690 m_renderer = renderer;
6842a71a 691 m_model_column = model_column;
6842a71a 692 m_owner = NULL;
baa9ebc4 693 m_renderer->SetOwner( (wxDataViewColumn*) this );
9861f022
RR
694
695 // NOTE: the wxDataViewColumn's ctor must store the width, align, flags
696 // parameters inside the native control!
6842a71a
RR
697}
698
07a84e7b
RR
699wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap& bitmap,
700 wxDataViewRenderer *renderer,
701 unsigned int model_column,
702 int WXUNUSED(width),
87f0efe2 703 wxAlignment WXUNUSED(align),
900af470 704 int WXUNUSED(flags) )
07a84e7b
RR
705{
706 m_renderer = renderer;
707 m_model_column = model_column;
07a84e7b
RR
708 m_bitmap = bitmap;
709 m_owner = NULL;
710 m_renderer->SetOwner( (wxDataViewColumn*) this );
711}
712
6842a71a
RR
713wxDataViewColumnBase::~wxDataViewColumnBase()
714{
baa9ebc4
RR
715 if (m_renderer)
716 delete m_renderer;
fa28826d
RR
717}
718
9861f022 719int wxDataViewColumnBase::GetFlags() const
fa28826d 720{
9861f022 721 int ret = 0;
fa28826d 722
9861f022
RR
723 if (IsSortable())
724 ret |= wxDATAVIEW_COL_SORTABLE;
725 if (IsResizeable())
726 ret |= wxDATAVIEW_COL_RESIZABLE;
727 if (IsHidden())
728 ret |= wxDATAVIEW_COL_HIDDEN;
fa28826d 729
9861f022 730 return ret;
07a84e7b
RR
731}
732
9861f022 733void wxDataViewColumnBase::SetFlags(int flags)
07a84e7b 734{
9861f022
RR
735 SetSortable((flags & wxDATAVIEW_COL_SORTABLE) != 0);
736 SetResizeable((flags & wxDATAVIEW_COL_RESIZABLE) != 0);
737 SetHidden((flags & wxDATAVIEW_COL_HIDDEN) != 0);
07a84e7b
RR
738}
739
f554a14b 740// ---------------------------------------------------------
239eaa41 741// wxDataViewCtrlBase
f554a14b 742// ---------------------------------------------------------
239eaa41
RR
743
744IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl)
745
746wxDataViewCtrlBase::wxDataViewCtrlBase()
747{
748 m_model = NULL;
3b6280be
RR
749 m_expander_column = 0;
750 m_indent = 8;
239eaa41
RR
751}
752
753wxDataViewCtrlBase::~wxDataViewCtrlBase()
754{
87f0efe2
RR
755 if (m_model)
756 {
757 m_model->DecRef();
758 m_model = NULL;
759 }
239eaa41
RR
760}
761
e0062c04 762bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel *model )
239eaa41 763{
1821abd1
RR
764 if (m_model)
765 {
87f0efe2 766 m_model->DecRef(); // discard old model, if any
1821abd1 767 }
87f0efe2
RR
768
769 // add our own reference to the new model:
239eaa41 770 m_model = model;
1821abd1
RR
771 if (m_model)
772 {
900af470 773 m_model->IncRef();
1821abd1 774 }
f554a14b 775
239eaa41
RR
776 return true;
777}
778
e0062c04 779wxDataViewModel* wxDataViewCtrlBase::GetModel()
239eaa41
RR
780{
781 return m_model;
782}
783
a75124d0
RR
784const wxDataViewModel* wxDataViewCtrlBase::GetModel() const
785{
786 return m_model;
787}
788
c7074d44
RR
789wxDataViewColumn *
790wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column,
87f0efe2 791 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
fa28826d 792{
c7074d44 793 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 794 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
c7074d44
RR
795 model_column, width, align, flags );
796 AppendColumn( ret );
797 return ret;
fa28826d
RR
798}
799
b04fcede
RR
800wxDataViewColumn *
801wxDataViewCtrlBase::AppendIconTextColumn( const wxString &label, unsigned int model_column,
802 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
803{
804 wxDataViewColumn *ret = new wxDataViewColumn( label,
805 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode, (int)align ),
806 model_column, width, align, flags );
807 AppendColumn( ret );
808 return ret;
809}
810
c7074d44
RR
811wxDataViewColumn *
812wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column,
87f0efe2 813 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
605c2c4a 814{
b7fe2261 815
c7074d44 816 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 817 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
c7074d44
RR
818 model_column, width, align, flags );
819 AppendColumn( ret );
820 return ret;
605c2c4a
RR
821}
822
c7074d44
RR
823wxDataViewColumn *
824wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column,
87f0efe2 825 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
ad63bf41 826{
c7074d44 827 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 828 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
c7074d44
RR
829 model_column, width, align, flags );
830 AppendColumn( ret );
831 return ret;
ad63bf41
RR
832}
833
c7074d44
RR
834wxDataViewColumn *
835wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column,
87f0efe2 836 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
4d496ecb 837{
c7074d44 838 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 839 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
c7074d44
RR
840 model_column, width, align, flags );
841 AppendColumn( ret );
842 return ret;
4d496ecb
RR
843}
844
c7074d44
RR
845wxDataViewColumn *
846wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column,
87f0efe2 847 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 848{
c7074d44 849 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 850 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
c7074d44
RR
851 model_column, width, align, flags );
852 AppendColumn( ret );
853 return ret;
07a84e7b
RR
854}
855
c7074d44
RR
856wxDataViewColumn *
857wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 858 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 859{
c7074d44 860 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 861 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
c7074d44
RR
862 model_column, width, align, flags );
863 AppendColumn( ret );
864 return ret;
b04fcede
RR
865}
866
867wxDataViewColumn *
868wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap &label, unsigned int model_column,
869 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
870{
871 wxDataViewColumn *ret = new wxDataViewColumn( label,
872 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode, (int)align ),
873 model_column, width, align, flags );
874 AppendColumn( ret );
875 return ret;
07a84e7b
RR
876}
877
c7074d44
RR
878wxDataViewColumn *
879wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 880 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 881{
c7074d44 882 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 883 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
c7074d44
RR
884 model_column, width, align, flags );
885 AppendColumn( ret );
886 return ret;
07a84e7b
RR
887}
888
c7074d44
RR
889wxDataViewColumn *
890wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 891 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 892{
c7074d44 893 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 894 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
c7074d44
RR
895 model_column, width, align, flags );
896 AppendColumn( ret );
897 return ret;
07a84e7b
RR
898}
899
c7074d44
RR
900wxDataViewColumn *
901wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 902 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 903{
c7074d44 904 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 905 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
c7074d44
RR
906 model_column, width, align, flags );
907 AppendColumn( ret );
908 return ret;
07a84e7b
RR
909}
910
c7074d44
RR
911wxDataViewColumn *
912wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column,
87f0efe2 913 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
07a84e7b 914{
c7074d44 915 wxDataViewColumn *ret = new wxDataViewColumn( label,
900af470 916 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
c7074d44
RR
917 model_column, width, align, flags );
918 AppendColumn( ret );
919 return ret;
07a84e7b
RR
920}
921
736fe67c
RR
922wxDataViewColumn *
923wxDataViewCtrlBase::PrependTextColumn( const wxString &label, unsigned int model_column,
924 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
925{
926 wxDataViewColumn *ret = new wxDataViewColumn( label,
927 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
928 model_column, width, align, flags );
929 PrependColumn( ret );
930 return ret;
931}
932
933wxDataViewColumn *
934wxDataViewCtrlBase::PrependIconTextColumn( const wxString &label, unsigned int model_column,
935 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
936{
937 wxDataViewColumn *ret = new wxDataViewColumn( label,
938 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode, (int)align ),
939 model_column, width, align, flags );
940 PrependColumn( ret );
941 return ret;
942}
943
944wxDataViewColumn *
945wxDataViewCtrlBase::PrependToggleColumn( const wxString &label, unsigned int model_column,
946 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
947{
948
949 wxDataViewColumn *ret = new wxDataViewColumn( label,
950 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
951 model_column, width, align, flags );
952 PrependColumn( ret );
953 return ret;
954}
955
956wxDataViewColumn *
957wxDataViewCtrlBase::PrependProgressColumn( const wxString &label, unsigned int model_column,
958 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
959{
960 wxDataViewColumn *ret = new wxDataViewColumn( label,
961 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
962 model_column, width, align, flags );
963 PrependColumn( ret );
964 return ret;
965}
966
967wxDataViewColumn *
968wxDataViewCtrlBase::PrependDateColumn( const wxString &label, unsigned int model_column,
969 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
970{
971 wxDataViewColumn *ret = new wxDataViewColumn( label,
972 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
973 model_column, width, align, flags );
974 PrependColumn( ret );
975 return ret;
976}
977
978wxDataViewColumn *
979wxDataViewCtrlBase::PrependBitmapColumn( const wxString &label, unsigned int model_column,
980 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
981{
982 wxDataViewColumn *ret = new wxDataViewColumn( label,
983 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
984 model_column, width, align, flags );
985 PrependColumn( ret );
986 return ret;
987}
988
989wxDataViewColumn *
990wxDataViewCtrlBase::PrependTextColumn( const wxBitmap &label, unsigned int model_column,
991 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
992{
993 wxDataViewColumn *ret = new wxDataViewColumn( label,
994 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
995 model_column, width, align, flags );
996 PrependColumn( ret );
997 return ret;
998}
999
1000wxDataViewColumn *
1001wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap &label, unsigned int model_column,
1002 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1003{
1004 wxDataViewColumn *ret = new wxDataViewColumn( label,
1005 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode, (int)align ),
1006 model_column, width, align, flags );
1007 PrependColumn( ret );
1008 return ret;
1009}
1010
1011wxDataViewColumn *
1012wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap &label, unsigned int model_column,
1013 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1014{
1015 wxDataViewColumn *ret = new wxDataViewColumn( label,
1016 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
1017 model_column, width, align, flags );
1018 PrependColumn( ret );
1019 return ret;
1020}
1021
1022wxDataViewColumn *
1023wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap &label, unsigned int model_column,
1024 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1025{
1026 wxDataViewColumn *ret = new wxDataViewColumn( label,
1027 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
1028 model_column, width, align, flags );
1029 PrependColumn( ret );
1030 return ret;
1031}
1032
1033wxDataViewColumn *
1034wxDataViewCtrlBase::PrependDateColumn( const wxBitmap &label, unsigned int model_column,
1035 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1036{
1037 wxDataViewColumn *ret = new wxDataViewColumn( label,
1038 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
1039 model_column, width, align, flags );
1040 PrependColumn( ret );
1041 return ret;
1042}
1043
1044wxDataViewColumn *
1045wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap &label, unsigned int model_column,
1046 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1047{
1048 wxDataViewColumn *ret = new wxDataViewColumn( label,
1049 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
1050 model_column, width, align, flags );
1051 PrependColumn( ret );
1052 return ret;
1053}
1054
c7074d44
RR
1055bool
1056wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
fa28826d 1057{
6842a71a 1058 col->SetOwner( (wxDataViewCtrl*) this );
fa28826d
RR
1059 return true;
1060}
1061
736fe67c
RR
1062bool
1063wxDataViewCtrlBase::PrependColumn( wxDataViewColumn *col )
1064{
1065 col->SetOwner( (wxDataViewCtrl*) this );
1066 return true;
1067}
1068
eb7f97f8
RR
1069// ---------------------------------------------------------
1070// wxDataViewEvent
1071// ---------------------------------------------------------
1072
1073IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent)
1074
d86c1870
RR
1075DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED)
1076
e0062c04 1077DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED)
6977c3bf 1078DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING)
3ecb8a53 1079DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED)
6977c3bf 1080DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING)
3ecb8a53 1081DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED)
e0000f94
RR
1082DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED)
1083DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE)
6608fdab 1084DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED)
e0000f94 1085
31fb32e1
RR
1086DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK)
1087DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK)
c0a66d92 1088DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED)
eb7f97f8 1089
52e750fc
RR
1090
1091// -------------------------------------
1092// wxDataViewSpinRenderer
1093// -------------------------------------
1094
1095wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min, int max, wxDataViewCellMode mode, int alignment ) :
b5ec7dd6
VZ
1096 wxDataViewCustomRenderer( "long", mode, alignment )
1097{
52e750fc
RR
1098 m_min = min;
1099 m_max = max;
1100}
b5ec7dd6 1101
52e750fc 1102wxControl* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
b5ec7dd6 1103{
52e750fc 1104 long l = value;
b5ec7dd6 1105 return new wxSpinCtrl( parent, wxID_ANY, wxEmptyString,
52e750fc
RR
1106 labelRect.GetTopLeft(), labelRect.GetSize(), wxSP_ARROW_KEYS, m_min, m_max, l );
1107}
b5ec7dd6 1108
52e750fc 1109bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
b5ec7dd6 1110{
52e750fc
RR
1111 wxSpinCtrl *sc = (wxSpinCtrl*) editor;
1112 long l = sc->GetValue();
1113 value = l;
1114 return true;
1115}
b5ec7dd6 1116
52e750fc
RR
1117bool wxDataViewSpinRenderer::Render( wxRect rect, wxDC *dc, int state )
1118{
1119 wxString str;
1120 str.Printf( "%d", (int) m_data );
1121 RenderText( str, 0, rect, dc, state );
1122 return true;
1123}
b5ec7dd6 1124
52e750fc
RR
1125wxSize wxDataViewSpinRenderer::GetSize() const
1126{
1127 return wxSize(80,16);
1128}
b5ec7dd6 1129
52e750fc
RR
1130bool wxDataViewSpinRenderer::SetValue( const wxVariant &value )
1131{
1132 m_data = value.GetLong();
1133 return true;
1134}
b5ec7dd6 1135
52e750fc
RR
1136bool wxDataViewSpinRenderer::GetValue( wxVariant &value ) const
1137{
1138 value = m_data;
1139 return true;
1140}
1141
e94d0c1e
RR
1142//-----------------------------------------------------------------------------
1143// wxDataViewTreeStore
1144//-----------------------------------------------------------------------------
1145
b5ec7dd6
VZ
1146wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1147 wxDataViewTreeStoreNode *parent,
e94d0c1e
RR
1148 const wxString &text, const wxIcon &icon, wxClientData *data )
1149{
1150 m_parent = parent;
1151 m_text = text;
1152 m_icon = icon;
1153 m_data = data;
1154}
1155
1156wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1157{
1158 if (m_data)
1159 delete m_data;
1160}
b5ec7dd6 1161
e94d0c1e 1162#include "wx/listimpl.cpp"
a76c2f37 1163WX_DEFINE_LIST(wxDataViewTreeStoreNodeList)
e94d0c1e 1164
b5ec7dd6
VZ
1165wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1166 wxDataViewTreeStoreNode *parent, const wxString &text,
e94d0c1e
RR
1167 const wxIcon &icon, const wxIcon &expanded, wxClientData *data ) :
1168 wxDataViewTreeStoreNode( parent, text, icon, data )
1169{
1170 m_iconExpanded = expanded;
a75124d0 1171 m_isExpanded = false;
e94d0c1e
RR
1172 m_children.DeleteContents(true);
1173}
1174
1175wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1176{
1177}
1178
1179//-----------------------------------------------------------------------------
1180
1181wxDataViewTreeStore::wxDataViewTreeStore()
1182{
1183 m_root = new wxDataViewTreeStoreContainerNode( NULL, wxEmptyString );
1184}
1185
1186wxDataViewTreeStore::~wxDataViewTreeStore()
1187{
1188 delete m_root;
1189}
1190
b5ec7dd6 1191wxDataViewItem wxDataViewTreeStore::AppendItem( const wxDataViewItem& parent,
e94d0c1e
RR
1192 const wxString &text, const wxIcon &icon, wxClientData *data )
1193{
1194 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1195 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1196
1197 wxDataViewTreeStoreNode *node =
e94d0c1e
RR
1198 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1199 parent_node->GetChildren().Append( node );
b5ec7dd6 1200
e94d0c1e
RR
1201 // notify control
1202 ItemAdded( parent, node->GetItem() );
b5ec7dd6 1203
e94d0c1e
RR
1204 return node->GetItem();
1205}
1206
1207wxDataViewItem wxDataViewTreeStore::PrependItem( const wxDataViewItem& parent,
1208 const wxString &text, const wxIcon &icon, wxClientData *data )
1209{
1210 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1211 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1212
1213 wxDataViewTreeStoreNode *node =
e94d0c1e
RR
1214 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1215 parent_node->GetChildren().Insert( node );
b5ec7dd6 1216
e94d0c1e
RR
1217 // notify control
1218 ItemAdded( parent, node->GetItem() );
b5ec7dd6 1219
e94d0c1e
RR
1220 return node->GetItem();
1221}
1222
b5ec7dd6
VZ
1223wxDataViewItem
1224wxDataViewTreeStore::InsertItem(const wxDataViewItem& WXUNUSED(parent),
1225 const wxDataViewItem& WXUNUSED(previous),
1226 const wxString& WXUNUSED(text),
1227 const wxIcon& WXUNUSED(icon),
1228 wxClientData * WXUNUSED(data))
e94d0c1e
RR
1229{
1230 return wxDataViewItem(0);
1231}
1232
b5ec7dd6
VZ
1233wxDataViewItem wxDataViewTreeStore::PrependContainer( const wxDataViewItem& parent,
1234 const wxString &text, const wxIcon &icon, const wxIcon &expanded,
e94d0c1e
RR
1235 wxClientData *data )
1236{
1237 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1238 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1239
1240 wxDataViewTreeStoreContainerNode *node =
e94d0c1e
RR
1241 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1242 parent_node->GetChildren().Insert( node );
b5ec7dd6 1243
e94d0c1e
RR
1244 // notify control
1245 ItemAdded( parent, node->GetItem() );
b5ec7dd6 1246
e94d0c1e
RR
1247 return node->GetItem();
1248}
1249
b5ec7dd6
VZ
1250wxDataViewItem
1251wxDataViewTreeStore::AppendContainer(const wxDataViewItem& parent,
1252 const wxString &text,
1253 const wxIcon& icon,
1254 const wxIcon& expanded,
1255 wxClientData * data)
e94d0c1e
RR
1256{
1257 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1258 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6
VZ
1259
1260 wxDataViewTreeStoreContainerNode *node =
e94d0c1e
RR
1261 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1262 parent_node->GetChildren().Append( node );
b5ec7dd6 1263
e94d0c1e
RR
1264 // notify control
1265 ItemAdded( parent, node->GetItem() );
b5ec7dd6 1266
e94d0c1e
RR
1267 return node->GetItem();
1268}
1269
b5ec7dd6
VZ
1270wxDataViewItem
1271wxDataViewTreeStore::InsertContainer(const wxDataViewItem& WXUNUSED(parent),
1272 const wxDataViewItem& WXUNUSED(previous),
1273 const wxString& WXUNUSED(text),
1274 const wxIcon& WXUNUSED(icon),
1275 const wxIcon& WXUNUSED(expanded),
1276 wxClientData * WXUNUSED(data))
e94d0c1e
RR
1277{
1278 return wxDataViewItem(0);
1279}
1280
1281wxDataViewItem wxDataViewTreeStore::GetNthChild( const wxDataViewItem& parent, unsigned int pos ) const
1282{
1283 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1284 if (!parent_node) return wxDataViewItem(0);
b5ec7dd6 1285
e94d0c1e
RR
1286 wxDataViewTreeStoreNodeList::compatibility_iterator node = parent_node->GetChildren().Item( pos );
1287 if (node)
1288 return node->GetData();
b5ec7dd6 1289
e94d0c1e
RR
1290 return wxDataViewItem(0);
1291}
1292
1293int wxDataViewTreeStore::GetChildCount( const wxDataViewItem& parent ) const
1294{
1295 wxDataViewTreeStoreNode *node = FindNode( parent );
1296 if (!node) return -1;
b5ec7dd6 1297
e94d0c1e
RR
1298 if (!node->IsContainer())
1299 return 0;
b5ec7dd6 1300
e94d0c1e 1301 wxDataViewTreeStoreContainerNode *container_node = (wxDataViewTreeStoreContainerNode*) node;
b5ec7dd6 1302 return (int) container_node->GetChildren().GetCount();
e94d0c1e
RR
1303}
1304
1305void wxDataViewTreeStore::SetItemText( const wxDataViewItem& item, const wxString &text )
1306{
1307 wxDataViewTreeStoreNode *node = FindNode( item );
1308 if (!node) return;
b5ec7dd6 1309
e94d0c1e 1310 node->SetText( text );
b5ec7dd6 1311
e94d0c1e
RR
1312 // notify control
1313 ValueChanged( item, 0 );
1314}
1315
1316wxString wxDataViewTreeStore::GetItemText( const wxDataViewItem& item ) const
1317{
1318 wxDataViewTreeStoreNode *node = FindNode( item );
1319 if (!node) return wxEmptyString;
b5ec7dd6 1320
e94d0c1e
RR
1321 return node->GetText();
1322}
1323
1324void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
1325{
1326 wxDataViewTreeStoreNode *node = FindNode( item );
1327 if (!node) return;
b5ec7dd6 1328
e94d0c1e 1329 node->SetIcon( icon );
b5ec7dd6 1330
e94d0c1e
RR
1331 // notify control
1332 ValueChanged( item, 0 );
1333}
1334
1335const wxIcon &wxDataViewTreeStore::GetItemIcon( const wxDataViewItem& item ) const
1336{
1337 wxDataViewTreeStoreNode *node = FindNode( item );
1338 if (!node) return wxNullIcon;
b5ec7dd6 1339
e94d0c1e
RR
1340 return node->GetIcon();
1341}
1342
1343void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
1344{
1345 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1346 if (!node) return;
b5ec7dd6 1347
e94d0c1e 1348 node->SetExpandedIcon( icon );
b5ec7dd6 1349
e94d0c1e
RR
1350 // notify control
1351 ValueChanged( item, 0 );
1352}
1353
1354const wxIcon &wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem& item ) const
1355{
1356 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1357 if (!node) return wxNullIcon;
b5ec7dd6 1358
e94d0c1e
RR
1359 return node->GetExpandedIcon();
1360}
1361
1362void wxDataViewTreeStore::SetItemData( const wxDataViewItem& item, wxClientData *data )
1363{
1364 wxDataViewTreeStoreNode *node = FindNode( item );
1365 if (!node) return;
b5ec7dd6 1366
e94d0c1e 1367 node->SetData( data );
b5ec7dd6 1368
e94d0c1e
RR
1369 // notify control? only sensible when sorting on client data
1370 // ValueChanged( item, 0 );
1371}
1372
1373wxClientData *wxDataViewTreeStore::GetItemData( const wxDataViewItem& item ) const
1374{
1375 wxDataViewTreeStoreNode *node = FindNode( item );
1376 if (!node) return NULL;
b5ec7dd6 1377
e94d0c1e
RR
1378 return node->GetData();
1379}
1380
1381void wxDataViewTreeStore::DeleteItem( const wxDataViewItem& item )
1382{
1383 if (!item.IsOk()) return;
1384
1385 wxDataViewItem parent_item = GetParent( item );
1386
1387 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent_item );
1388 if (!parent_node) return;
b5ec7dd6 1389
e94d0c1e
RR
1390 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1391 if (!node) return;
b5ec7dd6 1392
e94d0c1e 1393 parent_node->GetChildren().DeleteObject( node );
b5ec7dd6 1394
e94d0c1e
RR
1395 // notify control
1396 ItemDeleted( parent_item, item );
1397}
1398
1399void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem& item )
1400{
1401 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1402 if (!node) return;
b5ec7dd6 1403
e94d0c1e
RR
1404 wxDataViewItemArray array;
1405 wxDataViewTreeStoreNodeList::iterator iter;
1406 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
1407 {
1408 wxDataViewTreeStoreNode* child = *iter;
1409 array.Add( child->GetItem() );
1410 }
b5ec7dd6 1411
e94d0c1e 1412 node->GetChildren().clear();
b5ec7dd6 1413
e94d0c1e
RR
1414 // notify control
1415 ItemsDeleted( item, array );
1416}
1417
1418void wxDataViewTreeStore::DeleteAllItems()
1419{
1420 // TODO
1421}
1422
b5ec7dd6
VZ
1423void
1424wxDataViewTreeStore::GetValue(wxVariant &variant,
1425 const wxDataViewItem &item,
1426 unsigned int WXUNUSED(col)) const
e94d0c1e
RR
1427{
1428 // if (col != 0) return;
b5ec7dd6 1429
e94d0c1e
RR
1430 wxDataViewTreeStoreNode *node = FindNode( item );
1431 if (!node) return;
b5ec7dd6 1432
a75124d0
RR
1433 wxIcon icon( node->GetIcon());
1434 if (node->IsContainer())
1435 {
1436 wxDataViewTreeStoreContainerNode *container = (wxDataViewTreeStoreContainerNode*) node;
1437 if (container->IsExpanded() && container->GetExpandedIcon().IsOk())
1438 icon = container->GetExpandedIcon();
1439 }
1440
1441 wxDataViewIconText data( node->GetText(), icon );
b5ec7dd6 1442
e94d0c1e
RR
1443 variant << data;
1444}
1445
b5ec7dd6
VZ
1446bool
1447wxDataViewTreeStore::SetValue(const wxVariant& variant,
1448 const wxDataViewItem& item,
1449 unsigned int WXUNUSED(col))
e94d0c1e
RR
1450{
1451 // if (col != 0) return false;
b5ec7dd6 1452
e94d0c1e
RR
1453 wxDataViewTreeStoreNode *node = FindNode( item );
1454 if (!node) return false;
b5ec7dd6 1455
e94d0c1e 1456 wxDataViewIconText data;
b5ec7dd6 1457
e94d0c1e 1458 data << variant;
b5ec7dd6 1459
e94d0c1e
RR
1460 node->SetText( data.GetText() );
1461 node->SetIcon( data.GetIcon() );
b5ec7dd6 1462
e94d0c1e
RR
1463 return true;
1464}
1465
1466wxDataViewItem wxDataViewTreeStore::GetParent( const wxDataViewItem &item ) const
1467{
1468 wxDataViewTreeStoreNode *node = FindNode( item );
1469 if (!node) return wxDataViewItem(0);
b5ec7dd6 1470
e94d0c1e
RR
1471 wxDataViewTreeStoreNode *parent = node->GetParent();
1472 if (!parent) return wxDataViewItem(0);
b5ec7dd6 1473
e94d0c1e
RR
1474 if (parent == m_root)
1475 return wxDataViewItem(0);
b5ec7dd6 1476
e94d0c1e
RR
1477 return parent->GetItem();
1478}
1479
1480bool wxDataViewTreeStore::IsContainer( const wxDataViewItem &item ) const
1481{
1482 wxDataViewTreeStoreNode *node = FindNode( item );
1483 if (!node) return false;
b5ec7dd6 1484
e94d0c1e
RR
1485 return node->IsContainer();
1486}
1487
1488unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
1489{
1490 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1491 if (!node) return 0;
1492
1493 wxDataViewTreeStoreNodeList::iterator iter;
1494 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
1495 {
1496 wxDataViewTreeStoreNode* child = *iter;
1497 children.Add( child->GetItem() );
1498 }
b5ec7dd6 1499
e94d0c1e
RR
1500 return node->GetChildren().GetCount();
1501}
1502
b5ec7dd6 1503int wxDataViewTreeStore::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
e94d0c1e
RR
1504 unsigned int WXUNUSED(column), bool WXUNUSED(ascending) )
1505{
1506 wxDataViewTreeStoreNode *node1 = FindNode( item1 );
1507 wxDataViewTreeStoreNode *node2 = FindNode( item2 );
b5ec7dd6 1508
e94d0c1e
RR
1509 if (!node1 || !node2)
1510 return 0;
b5ec7dd6
VZ
1511
1512 wxDataViewTreeStoreContainerNode* parent1 =
e94d0c1e 1513 (wxDataViewTreeStoreContainerNode*) node1->GetParent();
b5ec7dd6 1514 wxDataViewTreeStoreContainerNode* parent2 =
e94d0c1e 1515 (wxDataViewTreeStoreContainerNode*) node2->GetParent();
b5ec7dd6 1516
e94d0c1e
RR
1517 if (parent1 != parent2)
1518 {
1519 wxLogError( wxT("Comparing items with different parent.") );
1520 return 0;
1521 }
b5ec7dd6 1522
e94d0c1e
RR
1523 if (node1->IsContainer() && !!node2->IsContainer())
1524 return 1;
b5ec7dd6 1525
e94d0c1e
RR
1526 if (node2->IsContainer() && !!node1->IsContainer())
1527 return -1;
b5ec7dd6 1528
e94d0c1e
RR
1529 return parent1->GetChildren().IndexOf( node1 ) - parent1->GetChildren().IndexOf( node2 );
1530}
1531
1532wxDataViewTreeStoreNode *wxDataViewTreeStore::FindNode( const wxDataViewItem &item ) const
1533{
1534 if (!item.IsOk())
1535 return m_root;
b5ec7dd6 1536
e94d0c1e
RR
1537 return (wxDataViewTreeStoreNode*) item.GetID();
1538}
1539
1540wxDataViewTreeStoreContainerNode *wxDataViewTreeStore::FindContainerNode( const wxDataViewItem &item ) const
1541{
1542 if (!item.IsOk())
1543 return (wxDataViewTreeStoreContainerNode*) m_root;
1544
1545 wxDataViewTreeStoreNode* node = (wxDataViewTreeStoreNode*) item.GetID();
b5ec7dd6 1546
e94d0c1e
RR
1547 if (!node->IsContainer())
1548 return NULL;
b5ec7dd6 1549
e94d0c1e
RR
1550 return (wxDataViewTreeStoreContainerNode*) node;
1551}
1552
a75124d0
RR
1553//-----------------------------------------------------------------------------
1554// wxDataViewTreeCtrl
1555//-----------------------------------------------------------------------------
1556
1557IMPLEMENT_DYNAMIC_CLASS(wxDataViewTreeCtrl,wxDataViewCtrl)
1558
1559BEGIN_EVENT_TABLE(wxDataViewTreeCtrl,wxDataViewCtrl)
1560 EVT_DATAVIEW_ITEM_EXPANDED(-1, wxDataViewTreeCtrl::OnExpanded)
1561 EVT_DATAVIEW_ITEM_COLLAPSED(-1, wxDataViewTreeCtrl::OnCollapsed)
9c150d5f 1562 EVT_SIZE( wxDataViewTreeCtrl::OnSize )
a75124d0
RR
1563END_EVENT_TABLE()
1564
1565wxDataViewTreeCtrl::wxDataViewTreeCtrl()
1566{
1567 m_imageList = NULL;
1568}
1569
1570wxDataViewTreeCtrl::wxDataViewTreeCtrl( wxWindow *parent, wxWindowID id,
1571 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
1572{
1573 m_imageList = NULL;
1574 Create( parent, id, pos, size, style, validator );
1575
1576 wxDataViewTreeStore *store = new wxDataViewTreeStore;
1577 AssociateModel( store );
1578 store->DecRef();
9c150d5f
RR
1579
1580 wxDataViewColumn *col = AppendIconTextColumn( "", 0, wxDATAVIEW_CELL_INERT, 40 );
1581
1582#if !defined(__WXGTK20__)
1583 wxSize cient_size = GetClientSize();
1584 col->SetWidth( size.x );
1585#endif
a75124d0
RR
1586}
1587
1588wxDataViewTreeCtrl::~wxDataViewTreeCtrl()
1589{
1590 if (m_imageList)
1591 delete m_imageList;
1592}
1593
1594bool wxDataViewTreeCtrl::Create( wxWindow *parent, wxWindowID id,
1595 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
1596{
1597 return wxDataViewCtrl::Create( parent, id, pos, size, style, validator );
1598}
1599
1600void wxDataViewTreeCtrl::SetImageList( wxImageList *imagelist )
1601{
1602 if (m_imageList)
1603 delete m_imageList;
1604
1605 m_imageList = imagelist;
1606}
1607
1608wxDataViewItem wxDataViewTreeCtrl::AppendItem( const wxDataViewItem& parent,
1609 const wxString &text, int iconIndex, wxClientData *data )
1610{
1611 wxIcon icon = wxNullIcon;
1612 if (m_imageList && (iconIndex != -1))
1613 icon = m_imageList->GetIcon( iconIndex );
1614
1615 return GetStore()->AppendItem( parent, text, icon, data );
1616}
1617
1618wxDataViewItem wxDataViewTreeCtrl::PrependItem( const wxDataViewItem& parent,
1619 const wxString &text, int iconIndex, wxClientData *data )
1620{
1621 wxIcon icon = wxNullIcon;
1622 if (m_imageList && (iconIndex != -1))
1623 icon = m_imageList->GetIcon( iconIndex );
1624
1625 return GetStore()->PrependItem( parent, text, icon, data );
1626}
1627
1628wxDataViewItem wxDataViewTreeCtrl::InsertItem( const wxDataViewItem& parent, const wxDataViewItem& previous,
1629 const wxString &text, int iconIndex, wxClientData *data )
1630{
1631 wxIcon icon = wxNullIcon;
1632 if (m_imageList && (iconIndex != -1))
1633 icon = m_imageList->GetIcon( iconIndex );
1634
1635 return GetStore()->InsertItem( parent, previous, text, icon, data );
1636}
1637
1638wxDataViewItem wxDataViewTreeCtrl::PrependContainer( const wxDataViewItem& parent,
1639 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
1640{
1641 wxIcon icon = wxNullIcon;
1642 if (m_imageList && (iconIndex != -1))
1643 icon = m_imageList->GetIcon( iconIndex );
1644
1645 wxIcon expanded = wxNullIcon;
1646 if (m_imageList && (expandedIndex != -1))
1647 expanded = m_imageList->GetIcon( expandedIndex );
1648
1649 return GetStore()->PrependContainer( parent, text, icon, expanded, data );
1650}
1651
1652wxDataViewItem wxDataViewTreeCtrl::AppendContainer( const wxDataViewItem& parent,
1653 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
1654{
1655 wxIcon icon = wxNullIcon;
1656 if (m_imageList && (iconIndex != -1))
1657 icon = m_imageList->GetIcon( iconIndex );
1658
1659 wxIcon expanded = wxNullIcon;
1660 if (m_imageList && (expandedIndex != -1))
1661 expanded = m_imageList->GetIcon( expandedIndex );
1662
1663 return GetStore()->AppendContainer( parent, text, icon, expanded, data );
1664}
1665
1666wxDataViewItem wxDataViewTreeCtrl::InsertContainer( const wxDataViewItem& parent, const wxDataViewItem& previous,
1667 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
1668{
1669 wxIcon icon = wxNullIcon;
1670 if (m_imageList && (iconIndex != -1))
1671 icon = m_imageList->GetIcon( iconIndex );
1672
1673 wxIcon expanded = wxNullIcon;
1674 if (m_imageList && (expandedIndex != -1))
1675 expanded = m_imageList->GetIcon( expandedIndex );
1676
1677 return GetStore()->InsertContainer( parent, previous, text, icon, expanded, data );
1678}
1679
a75124d0
RR
1680void wxDataViewTreeCtrl::OnExpanded( wxDataViewEvent &event )
1681{
1682 if (m_imageList) return;
1683
1684 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
1685 if (!container) return;
1686
1687 container->SetExpanded( true );
1688 GetStore()->ItemChanged( event.GetItem() );
1689}
1690
1691void wxDataViewTreeCtrl::OnCollapsed( wxDataViewEvent &event )
1692{
1693 if (m_imageList) return;
1694
1695 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
1696 if (!container) return;
1697
1698 container->SetExpanded( false );
1699 GetStore()->ItemChanged( event.GetItem() );
1700}
1701
1a07a730
RR
1702void wxDataViewTreeCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
1703{
1704#if !defined(__WXGTK20__)
9c150d5f
RR
1705 wxSize size = GetClientSize();
1706 wxDataViewColumn *col = GetColumn( 0 );
1707 col->SetWidth( size.x );
1a07a730
RR
1708#endif
1709}
a75124d0 1710
b5ec7dd6 1711#endif // wxUSE_DATAVIEWCTRL
e0000f94 1712