]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/datavcmn.cpp
added missing export declarations for wxGTK internal functions needed outside of...
[wxWidgets.git] / src / common / datavcmn.cpp
... / ...
CommitLineData
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
18#if wxUSE_DATAVIEWCTRL
19
20#include "wx/dataview.h"
21
22#ifndef WX_PRECOMP
23 #include "wx/log.h"
24#endif
25
26const wxChar wxDataViewCtrlNameStr[] = wxT("dataviewCtrl");
27
28
29bool operator == (const wxDataViewItem &left, const wxDataViewItem &right)
30{
31 return (left.GetID() == right.GetID() );
32}
33
34
35// ---------------------------------------------------------
36// wxDataViewModel
37// ---------------------------------------------------------
38
39#include <wx/listimpl.cpp>
40WX_DEFINE_LIST(wxDataViewModelNotifiers);
41
42wxDataViewModel::wxDataViewModel()
43{
44 m_notifiers.DeleteContents( true );
45 m_sortingColumn = 0;
46 m_ascending = true;
47}
48
49bool wxDataViewModel::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
50{
51 bool ret = true;
52
53 wxDataViewModelNotifiers::iterator iter;
54 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
55 {
56 wxDataViewModelNotifier* notifier = *iter;
57 if (!notifier->ItemAdded( parent, item ))
58 ret = false;
59 }
60
61 return ret;
62}
63
64bool wxDataViewModel::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
65{
66 bool ret = true;
67
68 wxDataViewModelNotifiers::iterator iter;
69 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
70 {
71 wxDataViewModelNotifier* notifier = *iter;
72 if (!notifier->ItemDeleted( parent, item ))
73 ret = false;
74 }
75
76 return ret;
77}
78
79bool wxDataViewModel::ItemChanged( const wxDataViewItem &item )
80{
81 bool ret = true;
82
83 wxDataViewModelNotifiers::iterator iter;
84 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
85 {
86 wxDataViewModelNotifier* notifier = *iter;
87 if (!notifier->ItemChanged( item ))
88 ret = false;
89 }
90
91 return ret;
92}
93
94bool wxDataViewModel::ValueChanged( const wxDataViewItem &item, unsigned int col )
95{
96 bool ret = true;
97
98 wxDataViewModelNotifiers::iterator iter;
99 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
100 {
101 wxDataViewModelNotifier* notifier = *iter;
102 if (!notifier->ValueChanged( item, col ))
103 ret = false;
104 }
105
106 return ret;
107}
108
109bool wxDataViewModel::Cleared()
110{
111 bool ret = true;
112
113 wxDataViewModelNotifiers::iterator iter;
114 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
115 {
116 wxDataViewModelNotifier* notifier = *iter;
117 if (!notifier->Cleared())
118 ret = false;
119 }
120
121 return ret;
122}
123
124void wxDataViewModel::Resort()
125{
126 wxDataViewModelNotifiers::iterator iter;
127 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
128 {
129 wxDataViewModelNotifier* notifier = *iter;
130 notifier->Resort();
131 }
132}
133
134void wxDataViewModel::AddNotifier( wxDataViewModelNotifier *notifier )
135{
136 m_notifiers.push_back( notifier );
137 notifier->SetOwner( this );
138}
139
140void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier *notifier )
141{
142 m_notifiers.DeleteObject( notifier );
143}
144
145int wxDataViewModel::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2 )
146{
147 // sort branches before leaves
148 bool item1_is_container = IsContainer(item1);
149 bool item2_is_container = IsContainer(item2);
150
151 if (item1_is_container && !item2_is_container)
152 return 1;
153 if (item2_is_container && !item1_is_container)
154 return -1;
155
156 wxVariant value1,value2;
157 GetValue( value1, item1, m_sortingColumn );
158 GetValue( value2, item2, m_sortingColumn );
159
160 if (!m_ascending)
161 {
162 wxVariant temp = value1;
163 value1 = value2;
164 value2 = temp;
165 }
166
167 if (value1.GetType() == wxT("string"))
168 {
169 wxString str1 = value1.GetString();
170 wxString str2 = value2.GetString();
171 int res = str1.Cmp( str2 );
172 if (res) return res;
173 } else
174 if (value1.GetType() == wxT("long"))
175 {
176 long l1 = value1.GetLong();
177 long l2 = value2.GetLong();
178 long res = l1-l2;
179 if (res) return res;
180 } else
181 if (value1.GetType() == wxT("double"))
182 {
183 double d1 = value1.GetDouble();
184 double d2 = value2.GetDouble();
185 if (d1 < d2) return 1;
186 if (d1 > d2) return -1;
187 } else
188 if (value1.GetType() == wxT("datetime"))
189 {
190 wxDateTime dt1 = value1.GetDateTime();
191 wxDateTime dt2 = value2.GetDateTime();
192 if (dt1.IsEarlierThan(dt2)) return 1;
193 if (dt2.IsEarlierThan(dt1)) return -11;
194 }
195
196 // items must be different
197 unsigned long litem1 = (unsigned long) item1.GetID();
198 unsigned long litem2 = (unsigned long) item2.GetID();
199
200 if (!m_ascending)
201 return litem2-litem1;
202 return litem1-litem2;
203}
204
205// ---------------------------------------------------------
206// wxDataViewIndexListModel
207// ---------------------------------------------------------
208
209wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size )
210{
211 // build initial index
212 unsigned int i;
213 for (i = 1; i < initial_size+1; i++)
214 m_hash.Add( (void*) i );
215 m_lastIndex = initial_size + 1;
216}
217
218wxDataViewIndexListModel::~wxDataViewIndexListModel()
219{
220}
221
222void wxDataViewIndexListModel::RowPrepended()
223{
224 unsigned int id = m_lastIndex++;
225 m_hash.Insert( (void*) id, 0 );
226 wxDataViewItem item( (void*) id );
227 ItemAdded( wxDataViewItem(0), item );
228}
229
230void wxDataViewIndexListModel::RowInserted( unsigned int before )
231{
232 unsigned int id = m_lastIndex++;
233 m_hash.Insert( (void*) id, before );
234 wxDataViewItem item( (void*) id );
235 ItemAdded( wxDataViewItem(0), item );
236}
237
238void wxDataViewIndexListModel::RowAppended()
239{
240 unsigned int id = m_lastIndex++;
241 m_hash.Add( (void*) id );
242 wxDataViewItem item( (void*) id );
243 ItemAdded( wxDataViewItem(0), item );
244}
245
246void wxDataViewIndexListModel::RowDeleted( unsigned int row )
247{
248 wxDataViewItem item( m_hash[row] );
249 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item );
250 m_hash.RemoveAt( row );
251}
252
253void wxDataViewIndexListModel::RowChanged( unsigned int row )
254{
255 wxDataViewModel::ItemChanged( GetItem(row) );
256}
257
258void wxDataViewIndexListModel::RowValueChanged( unsigned int row, unsigned int col )
259{
260 wxDataViewModel::ValueChanged( GetItem(row), col );
261}
262
263unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem &item ) const
264{
265 // assert for not found
266 return (unsigned int) m_hash.Index( item.GetID() );
267}
268
269wxDataViewItem wxDataViewIndexListModel::GetItem( unsigned int row ) const
270{
271 return wxDataViewItem( m_hash[row] );
272}
273
274int wxDataViewIndexListModel::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2 )
275{
276 return GetRow(item1) - GetRow(item2);
277}
278
279void wxDataViewIndexListModel::GetValue( wxVariant &variant,
280 const wxDataViewItem &item, unsigned int col ) const
281{
282 GetValue( variant, GetRow(item), col );
283}
284
285bool wxDataViewIndexListModel::SetValue( const wxVariant &variant,
286 const wxDataViewItem &item, unsigned int col )
287{
288 return SetValue( variant, GetRow(item), col );
289}
290
291wxDataViewItem wxDataViewIndexListModel::GetParent( const wxDataViewItem & WXUNUSED(item) ) const
292{
293 return wxDataViewItem(0);
294}
295
296bool wxDataViewIndexListModel::IsContainer( const wxDataViewItem &item ) const
297{
298 // only the invisible root item has children
299 if (!item.IsOk())
300 return true;
301
302 return false;
303}
304
305wxDataViewItem wxDataViewIndexListModel::GetFirstChild( const wxDataViewItem &parent ) const
306{
307 if (!parent.IsOk())
308 {
309 if (m_hash.GetCount() == 0)
310 return wxDataViewItem(0);
311
312 return wxDataViewItem( m_hash[0]);
313 }
314
315 return wxDataViewItem(0);
316}
317
318wxDataViewItem wxDataViewIndexListModel::GetNextSibling( const wxDataViewItem &item ) const
319{
320 if (!item.IsOk())
321 return wxDataViewItem(0);
322
323 int pos = m_hash.Index( item.GetID() );
324 if ((pos == wxNOT_FOUND) || (pos == (int) (m_hash.GetCount()-1)))
325 return wxDataViewItem(0);
326
327 return wxDataViewItem( m_hash[pos+1] );
328}
329
330// ---------------------------------------------------------
331// wxDataViewRendererBase
332// ---------------------------------------------------------
333
334IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase, wxObject)
335
336wxDataViewRendererBase::wxDataViewRendererBase( const wxString &varianttype,
337 wxDataViewCellMode WXUNUSED(mode),
338 int WXUNUSED(align) )
339{
340 m_variantType = varianttype;
341 m_editorCtrl = NULL;
342}
343
344const wxDataViewCtrl* wxDataViewRendererBase::GetView() const
345{
346 return wx_const_cast(wxDataViewRendererBase*, this)->GetOwner()->GetOwner();
347}
348
349bool wxDataViewRendererBase::StartEditing( const wxDataViewItem &item, wxRect labelRect )
350{
351 m_item = item; // remember for later
352
353 unsigned int col = GetOwner()->GetModelColumn();
354 wxVariant value;
355 GetOwner()->GetOwner()->GetModel()->GetValue( value, item, col );
356
357 m_editorCtrl = CreateEditorCtrl( GetOwner()->GetOwner()->GetMainWindow(), labelRect, value );
358
359 wxDataViewEditorCtrlEvtHandler *handler =
360 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl, (wxDataViewRenderer*) this );
361
362 m_editorCtrl->PushEventHandler( handler );
363
364#if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
365 handler->SetFocusOnIdle();
366#else
367 m_editorCtrl->SetFocus();
368#endif
369
370 return true;
371}
372
373void wxDataViewRendererBase::CancelEditing()
374{
375 wxPendingDelete.Append( m_editorCtrl );
376
377 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
378
379 // m_editorCtrl->PopEventHandler( true );
380}
381
382bool wxDataViewRendererBase::FinishEditing()
383{
384 wxVariant value;
385 GetValueFromEditorCtrl( m_editorCtrl, value );
386
387 wxPendingDelete.Append( m_editorCtrl );
388
389 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
390
391 if (!Validate(value))
392 return false;
393
394 unsigned int col = GetOwner()->GetModelColumn();
395 GetOwner()->GetOwner()->GetModel()->SetValue( value, m_item, col );
396 GetOwner()->GetOwner()->GetModel()->ValueChanged( m_item, col );
397
398 // m_editorCtrl->PopEventHandler( true );
399
400 return true;
401}
402
403//-----------------------------------------------------------------------------
404// wxDataViewEditorCtrlEvtHandler
405//-----------------------------------------------------------------------------
406
407BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler, wxEvtHandler)
408 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar)
409 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus)
410 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle)
411END_EVENT_TABLE()
412
413wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
414 wxControl *editorCtrl,
415 wxDataViewRenderer *owner )
416{
417 m_owner = owner;
418 m_editorCtrl = editorCtrl;
419
420 m_finished = false;
421}
422
423void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent &event )
424{
425 if (m_focusOnIdle)
426 {
427 m_focusOnIdle = false;
428 if (wxWindow::FindFocus() != m_editorCtrl)
429 m_editorCtrl->SetFocus();
430 }
431
432 event.Skip();
433}
434
435void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent &event )
436{
437 switch ( event.m_keyCode )
438 {
439 case WXK_RETURN:
440 m_finished = true;
441 m_owner->FinishEditing();
442 break;
443
444 case WXK_ESCAPE:
445 m_finished = true;
446 m_owner->CancelEditing();
447 break;
448
449 default:
450 event.Skip();
451 }
452}
453
454void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent &event )
455{
456 if (!m_finished)
457 {
458 m_finished = true;
459 m_owner->FinishEditing();
460 }
461
462 event.Skip();
463}
464
465// ---------------------------------------------------------
466// wxDataViewColumnBase
467// ---------------------------------------------------------
468
469IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase, wxObject)
470
471wxDataViewColumnBase::wxDataViewColumnBase(const wxString& WXUNUSED(title),
472 wxDataViewRenderer *renderer,
473 unsigned int model_column,
474 int WXUNUSED(width),
475 wxAlignment WXUNUSED(align),
476 int WXUNUSED(flags))
477{
478 m_renderer = renderer;
479 m_model_column = model_column;
480 m_owner = NULL;
481 m_renderer->SetOwner( (wxDataViewColumn*) this );
482
483 // NOTE: the wxDataViewColumn's ctor must store the width, align, flags
484 // parameters inside the native control!
485}
486
487wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap& bitmap,
488 wxDataViewRenderer *renderer,
489 unsigned int model_column,
490 int WXUNUSED(width),
491 wxAlignment WXUNUSED(align),
492 int WXUNUSED(flags) )
493{
494 m_renderer = renderer;
495 m_model_column = model_column;
496 m_bitmap = bitmap;
497 m_owner = NULL;
498 m_renderer->SetOwner( (wxDataViewColumn*) this );
499}
500
501wxDataViewColumnBase::~wxDataViewColumnBase()
502{
503 if (m_renderer)
504 delete m_renderer;
505}
506
507int wxDataViewColumnBase::GetFlags() const
508{
509 int ret = 0;
510
511 if (IsSortable())
512 ret |= wxDATAVIEW_COL_SORTABLE;
513 if (IsResizeable())
514 ret |= wxDATAVIEW_COL_RESIZABLE;
515 if (IsHidden())
516 ret |= wxDATAVIEW_COL_HIDDEN;
517
518 return ret;
519}
520
521void wxDataViewColumnBase::SetFlags(int flags)
522{
523 SetSortable((flags & wxDATAVIEW_COL_SORTABLE) != 0);
524 SetResizeable((flags & wxDATAVIEW_COL_RESIZABLE) != 0);
525 SetHidden((flags & wxDATAVIEW_COL_HIDDEN) != 0);
526}
527
528// ---------------------------------------------------------
529// wxDataViewCtrlBase
530// ---------------------------------------------------------
531
532IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl)
533
534wxDataViewCtrlBase::wxDataViewCtrlBase()
535{
536 m_model = NULL;
537 m_cols.DeleteContents( true );
538 m_expander_column = 0;
539 m_indent = 8;
540}
541
542wxDataViewCtrlBase::~wxDataViewCtrlBase()
543{
544 // IMPORTANT: before calling DecRef() on our model (since it may
545 // result in a free() call), erase all columns (since
546 // they hold a pointer to our model)
547 m_cols.Clear();
548
549 if (m_model)
550 {
551 m_model->DecRef();
552 m_model = NULL;
553 }
554}
555
556bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel *model )
557{
558 if (m_model)
559 {
560 m_model->DecRef(); // discard old model, if any
561 }
562
563 // add our own reference to the new model:
564 m_model = model;
565 if (m_model)
566 {
567 m_model->IncRef();
568 }
569
570 return true;
571}
572
573wxDataViewModel* wxDataViewCtrlBase::GetModel()
574{
575 return m_model;
576}
577
578bool wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column,
579 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
580{
581 return AppendColumn( new wxDataViewColumn( label,
582 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
583 model_column, width, align, flags ) );
584}
585
586bool wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column,
587 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
588{
589 return AppendColumn( new wxDataViewColumn( label,
590 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
591 model_column, width, align, flags ) );
592}
593
594bool wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column,
595 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
596{
597 return AppendColumn( new wxDataViewColumn( label,
598 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
599 model_column, width, align, flags ) );
600}
601
602bool wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column,
603 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
604{
605 return AppendColumn( new wxDataViewColumn( label,
606 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
607 model_column, width, align, flags ) );
608}
609
610bool wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column,
611 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
612{
613 return AppendColumn( new wxDataViewColumn( label,
614 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
615 model_column, width, align, flags ) );
616}
617
618bool wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column,
619 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
620{
621 return AppendColumn( new wxDataViewColumn( label,
622 new wxDataViewTextRenderer( wxT("string"), mode, (int)align ),
623 model_column, width, align, flags ) );
624}
625
626bool wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column,
627 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
628{
629 return AppendColumn( new wxDataViewColumn( label,
630 new wxDataViewToggleRenderer( wxT("bool"), mode, (int)align ),
631 model_column, width, align, flags ) );
632}
633
634bool wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column,
635 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
636{
637 return AppendColumn( new wxDataViewColumn( label,
638 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode, (int)align ),
639 model_column, width, align, flags ) );
640}
641
642bool wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column,
643 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
644{
645 return AppendColumn( new wxDataViewColumn( label,
646 new wxDataViewDateRenderer( wxT("datetime"), mode, (int)align ),
647 model_column, width, align, flags ) );
648}
649
650bool wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column,
651 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
652{
653 return AppendColumn( new wxDataViewColumn( label,
654 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode, (int)align ),
655 model_column, width, align, flags ) );
656}
657
658bool wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
659{
660 m_cols.Append( (wxObject*) col );
661 col->SetOwner( (wxDataViewCtrl*) this );
662 return true;
663}
664
665unsigned int wxDataViewCtrlBase::GetColumnCount() const
666{
667 return m_cols.GetCount();
668}
669
670bool wxDataViewCtrlBase::DeleteColumn( unsigned int WXUNUSED(pos) )
671{
672 return false;
673}
674
675bool wxDataViewCtrlBase::ClearColumns()
676{
677 return false;
678}
679
680wxDataViewColumn* wxDataViewCtrlBase::GetColumn( unsigned int pos )
681{
682 return (wxDataViewColumn*) m_cols[ pos ];
683}
684
685// ---------------------------------------------------------
686// wxDataViewEvent
687// ---------------------------------------------------------
688
689IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent)
690
691DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED)
692DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED)
693DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK)
694DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK)
695DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED)
696
697DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED)
698DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED)
699DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED)
700DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED)
701DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED)
702
703#endif