]> git.saurik.com Git - wxWidgets.git/blob - src/common/datavcmn.cpp
Ignore non-existant string selection in wxComboBox::SetValue() in read-only mode...
[wxWidgets.git] / src / common / datavcmn.cpp
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/dc.h"
24 #include "wx/settings.h"
25 #include "wx/log.h"
26 #include "wx/crt.h"
27 #endif
28
29 #include "wx/spinctrl.h"
30 #include "wx/choice.h"
31 #include "wx/imaglist.h"
32
33 const char wxDataViewCtrlNameStr[] = "dataviewCtrl";
34
35 namespace
36 {
37
38 // Custom handler pushed on top of the edit control used by wxDataViewCtrl to
39 // forward some events to the main control itself.
40 class wxDataViewEditorCtrlEvtHandler: public wxEvtHandler
41 {
42 public:
43 wxDataViewEditorCtrlEvtHandler(wxControl *editor, wxDataViewRenderer *owner)
44 {
45 m_editorCtrl = editor;
46 m_owner = owner;
47
48 m_finished = false;
49 }
50
51 void AcceptChangesAndFinish();
52 void SetFocusOnIdle( bool focus = true ) { m_focusOnIdle = focus; }
53
54 protected:
55 void OnChar( wxKeyEvent &event );
56 void OnTextEnter( wxCommandEvent &event );
57 void OnKillFocus( wxFocusEvent &event );
58 void OnIdle( wxIdleEvent &event );
59
60 private:
61 wxDataViewRenderer *m_owner;
62 wxControl *m_editorCtrl;
63 bool m_finished;
64 bool m_focusOnIdle;
65
66 private:
67 DECLARE_EVENT_TABLE()
68 };
69
70 } // anonymous namespace
71
72 // ---------------------------------------------------------
73 // wxDataViewModelNotifier
74 // ---------------------------------------------------------
75
76 #include "wx/listimpl.cpp"
77 WX_DEFINE_LIST(wxDataViewModelNotifiers)
78
79 bool wxDataViewModelNotifier::ItemsAdded( const wxDataViewItem &parent, const wxDataViewItemArray &items )
80 {
81 size_t count = items.GetCount();
82 size_t i;
83 for (i = 0; i < count; i++)
84 if (!ItemAdded( parent, items[i] )) return false;
85
86 return true;
87 }
88
89 bool wxDataViewModelNotifier::ItemsDeleted( const wxDataViewItem &parent, const wxDataViewItemArray &items )
90 {
91 size_t count = items.GetCount();
92 size_t i;
93 for (i = 0; i < count; i++)
94 if (!ItemDeleted( parent, items[i] )) return false;
95
96 return true;
97 }
98
99 bool wxDataViewModelNotifier::ItemsChanged( const wxDataViewItemArray &items )
100 {
101 size_t count = items.GetCount();
102 size_t i;
103 for (i = 0; i < count; i++)
104 if (!ItemChanged( items[i] )) return false;
105
106 return true;
107 }
108
109 // ---------------------------------------------------------
110 // wxDataViewModel
111 // ---------------------------------------------------------
112
113 wxDataViewModel::wxDataViewModel()
114 {
115 m_notifiers.DeleteContents( true );
116 }
117
118 bool wxDataViewModel::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
119 {
120 bool ret = true;
121
122 wxDataViewModelNotifiers::iterator iter;
123 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
124 {
125 wxDataViewModelNotifier* notifier = *iter;
126 if (!notifier->ItemAdded( parent, item ))
127 ret = false;
128 }
129
130 return ret;
131 }
132
133 bool wxDataViewModel::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
134 {
135 bool ret = true;
136
137 wxDataViewModelNotifiers::iterator iter;
138 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
139 {
140 wxDataViewModelNotifier* notifier = *iter;
141 if (!notifier->ItemDeleted( parent, item ))
142 ret = false;
143 }
144
145 return ret;
146 }
147
148 bool wxDataViewModel::ItemChanged( const wxDataViewItem &item )
149 {
150 bool ret = true;
151
152 wxDataViewModelNotifiers::iterator iter;
153 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
154 {
155 wxDataViewModelNotifier* notifier = *iter;
156 if (!notifier->ItemChanged( item ))
157 ret = false;
158 }
159
160 return ret;
161 }
162
163 bool wxDataViewModel::ItemsAdded( const wxDataViewItem &parent, const wxDataViewItemArray &items )
164 {
165 bool ret = true;
166
167 wxDataViewModelNotifiers::iterator iter;
168 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
169 {
170 wxDataViewModelNotifier* notifier = *iter;
171 if (!notifier->ItemsAdded( parent, items ))
172 ret = false;
173 }
174
175 return ret;
176 }
177
178 bool wxDataViewModel::ItemsDeleted( const wxDataViewItem &parent, const wxDataViewItemArray &items )
179 {
180 bool ret = true;
181
182 wxDataViewModelNotifiers::iterator iter;
183 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
184 {
185 wxDataViewModelNotifier* notifier = *iter;
186 if (!notifier->ItemsDeleted( parent, items ))
187 ret = false;
188 }
189
190 return ret;
191 }
192
193 bool wxDataViewModel::ItemsChanged( const wxDataViewItemArray &items )
194 {
195 bool ret = true;
196
197 wxDataViewModelNotifiers::iterator iter;
198 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
199 {
200 wxDataViewModelNotifier* notifier = *iter;
201 if (!notifier->ItemsChanged( items ))
202 ret = false;
203 }
204
205 return ret;
206 }
207
208 bool wxDataViewModel::ValueChanged( const wxDataViewItem &item, unsigned int col )
209 {
210 bool ret = true;
211
212 wxDataViewModelNotifiers::iterator iter;
213 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
214 {
215 wxDataViewModelNotifier* notifier = *iter;
216 if (!notifier->ValueChanged( item, col ))
217 ret = false;
218 }
219
220 return ret;
221 }
222
223 bool wxDataViewModel::Cleared()
224 {
225 bool ret = true;
226
227 wxDataViewModelNotifiers::iterator iter;
228 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
229 {
230 wxDataViewModelNotifier* notifier = *iter;
231 if (!notifier->Cleared())
232 ret = false;
233 }
234
235 return ret;
236 }
237
238 void wxDataViewModel::Resort()
239 {
240 wxDataViewModelNotifiers::iterator iter;
241 for (iter = m_notifiers.begin(); iter != m_notifiers.end(); ++iter)
242 {
243 wxDataViewModelNotifier* notifier = *iter;
244 notifier->Resort();
245 }
246 }
247
248 void wxDataViewModel::AddNotifier( wxDataViewModelNotifier *notifier )
249 {
250 m_notifiers.push_back( notifier );
251 notifier->SetOwner( this );
252 }
253
254 void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier *notifier )
255 {
256 m_notifiers.DeleteObject( notifier );
257 }
258
259 int wxDataViewModel::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
260 unsigned int column, bool ascending ) const
261 {
262 // sort branches before leaves
263 bool item1_is_container = IsContainer(item1);
264 bool item2_is_container = IsContainer(item2);
265
266 if (item1_is_container && !item2_is_container)
267 return 1;
268 if (item2_is_container && !item1_is_container)
269 return -1;
270
271 wxVariant value1,value2;
272 GetValue( value1, item1, column );
273 GetValue( value2, item2, column );
274
275 if (!ascending)
276 {
277 wxVariant temp = value1;
278 value1 = value2;
279 value2 = temp;
280 }
281
282 if (value1.GetType() == wxT("string"))
283 {
284 wxString str1 = value1.GetString();
285 wxString str2 = value2.GetString();
286 int res = str1.Cmp( str2 );
287 if (res)
288 return res;
289 }
290 else if (value1.GetType() == wxT("long"))
291 {
292 long l1 = value1.GetLong();
293 long l2 = value2.GetLong();
294 long res = l1-l2;
295 if (res)
296 return res;
297 }
298 else if (value1.GetType() == wxT("double"))
299 {
300 double d1 = value1.GetDouble();
301 double d2 = value2.GetDouble();
302 if (d1 < d2)
303 return 1;
304 if (d1 > d2)
305 return -1;
306 }
307 else if (value1.GetType() == wxT("datetime"))
308 {
309 wxDateTime dt1 = value1.GetDateTime();
310 wxDateTime dt2 = value2.GetDateTime();
311 if (dt1.IsEarlierThan(dt2))
312 return 1;
313 if (dt2.IsEarlierThan(dt1))
314 return -1;
315 }
316
317 // items must be different
318 wxUIntPtr id1 = wxPtrToUInt(item1.GetID()),
319 id2 = wxPtrToUInt(item2.GetID());
320
321 return ascending ? id1 - id2 : id2 - id1;
322 }
323
324 // ---------------------------------------------------------
325 // wxDataViewIndexListModel
326 // ---------------------------------------------------------
327
328 static int my_sort( int *v1, int *v2 )
329 {
330 return *v2-*v1;
331 }
332
333
334 wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size )
335 {
336 // IDs are ordered until an item gets deleted or inserted
337 m_ordered = true;
338
339 // build initial index
340 unsigned int i;
341 for (i = 1; i < initial_size+1; i++)
342 m_hash.Add( wxUIntToPtr(i) );
343 m_nextFreeID = initial_size + 1;
344 }
345
346 void wxDataViewIndexListModel::Reset( unsigned int new_size )
347 {
348 m_hash.Clear();
349
350 // IDs are ordered until an item gets deleted or inserted
351 m_ordered = true;
352
353 // build initial index
354 unsigned int i;
355 for (i = 1; i < new_size+1; i++)
356 m_hash.Add( wxUIntToPtr(i) );
357
358 m_nextFreeID = new_size + 1;
359
360 /* wxDataViewModel:: */ Cleared();
361 }
362
363 void wxDataViewIndexListModel::RowPrepended()
364 {
365 m_ordered = false;
366
367 unsigned int id = m_nextFreeID;
368 m_nextFreeID++;
369
370 m_hash.Insert( wxUIntToPtr(id), 0 );
371 wxDataViewItem item( wxUIntToPtr(id) );
372 ItemAdded( wxDataViewItem(0), item );
373
374 }
375
376 void wxDataViewIndexListModel::RowInserted( unsigned int before )
377 {
378 m_ordered = false;
379
380 unsigned int id = m_nextFreeID;
381 m_nextFreeID++;
382
383 m_hash.Insert( wxUIntToPtr(id), before );
384 wxDataViewItem item( wxUIntToPtr(id) );
385 ItemAdded( wxDataViewItem(0), item );
386 }
387
388 void wxDataViewIndexListModel::RowAppended()
389 {
390 unsigned int id = m_nextFreeID;
391 m_nextFreeID++;
392
393 m_hash.Add( wxUIntToPtr(id) );
394 wxDataViewItem item( wxUIntToPtr(id) );
395 ItemAdded( wxDataViewItem(0), item );
396 }
397
398 void wxDataViewIndexListModel::RowDeleted( unsigned int row )
399 {
400 m_ordered = false;
401
402 wxDataViewItem item( m_hash[row] );
403 /* wxDataViewModel:: */ ItemDeleted( wxDataViewItem(0), item );
404 m_hash.RemoveAt( row );
405 }
406
407 void wxDataViewIndexListModel::RowsDeleted( const wxArrayInt &rows )
408 {
409 wxArrayInt sorted = rows;
410 sorted.Sort( my_sort );
411
412 m_ordered = false;
413
414 wxDataViewItemArray array;
415 unsigned int i;
416 for (i = 0; i < rows.GetCount(); i++)
417 {
418 wxDataViewItem item( m_hash[rows[i]] );
419 array.Add( item );
420 }
421 /* wxDataViewModel:: */ ItemsDeleted( wxDataViewItem(0), array );
422
423 for (i = 0; i < sorted.GetCount(); i++)
424 m_hash.RemoveAt( sorted[i] );
425 }
426
427 void wxDataViewIndexListModel::RowChanged( unsigned int row )
428 {
429 /* wxDataViewModel:: */ ItemChanged( GetItem(row) );
430 }
431
432 void wxDataViewIndexListModel::RowValueChanged( unsigned int row, unsigned int col )
433 {
434 /* wxDataViewModel:: */ ValueChanged( GetItem(row), col );
435 }
436
437 unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem &item ) const
438 {
439 if (m_ordered)
440 return wxPtrToUInt(item.GetID())-1;
441
442 // assert for not found
443 return (unsigned int) m_hash.Index( item.GetID() );
444 }
445
446 wxDataViewItem wxDataViewIndexListModel::GetItem( unsigned int row ) const
447 {
448 wxASSERT( row < m_hash.GetCount() );
449 return wxDataViewItem( m_hash[row] );
450 }
451
452 bool wxDataViewIndexListModel::HasDefaultCompare() const
453 {
454 return !m_ordered;
455 }
456
457 int wxDataViewIndexListModel::Compare(const wxDataViewItem& item1,
458 const wxDataViewItem& item2,
459 unsigned int WXUNUSED(column),
460 bool ascending) const
461 {
462 if (m_ordered)
463 {
464 unsigned int pos1 = wxPtrToUInt(item1.GetID()); // -1 not needed here
465 unsigned int pos2 = wxPtrToUInt(item2.GetID()); // -1 not needed here
466
467 if (ascending)
468 return pos1 - pos2;
469 else
470 return pos2 - pos1;
471 }
472
473 if (ascending)
474 return GetRow(item1) - GetRow(item2);
475
476 return GetRow(item2) - GetRow(item1);
477 }
478
479 unsigned int wxDataViewIndexListModel::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
480 {
481 if (item.IsOk())
482 return 0;
483
484 children = m_hash;
485
486 return m_hash.GetCount();
487 }
488
489 // ---------------------------------------------------------
490 // wxDataViewVirtualListModel
491 // ---------------------------------------------------------
492
493 #ifndef __WXMAC__
494
495 wxDataViewVirtualListModel::wxDataViewVirtualListModel( unsigned int initial_size )
496 {
497 m_size = initial_size;
498 }
499
500 void wxDataViewVirtualListModel::Reset( unsigned int new_size )
501 {
502 m_size = new_size;
503
504 /* wxDataViewModel:: */ Cleared();
505 }
506
507 void wxDataViewVirtualListModel::RowPrepended()
508 {
509 m_size++;
510 wxDataViewItem item( wxUIntToPtr(1) );
511 ItemAdded( wxDataViewItem(0), item );
512 }
513
514 void wxDataViewVirtualListModel::RowInserted( unsigned int before )
515 {
516 m_size++;
517 wxDataViewItem item( wxUIntToPtr(before+1) );
518 ItemAdded( wxDataViewItem(0), item );
519 }
520
521 void wxDataViewVirtualListModel::RowAppended()
522 {
523 m_size++;
524 wxDataViewItem item( wxUIntToPtr(m_size) );
525 ItemAdded( wxDataViewItem(0), item );
526 }
527
528 void wxDataViewVirtualListModel::RowDeleted( unsigned int row )
529 {
530 m_size--;
531 wxDataViewItem item( wxUIntToPtr(row+1) );
532 /* wxDataViewModel:: */ ItemDeleted( wxDataViewItem(0), item );
533 }
534
535 void wxDataViewVirtualListModel::RowsDeleted( const wxArrayInt &rows )
536 {
537 m_size -= rows.GetCount();
538
539 wxArrayInt sorted = rows;
540 sorted.Sort( my_sort );
541
542 wxDataViewItemArray array;
543 unsigned int i;
544 for (i = 0; i < sorted.GetCount(); i++)
545 {
546 wxDataViewItem item( wxUIntToPtr(sorted[i]+1) );
547 array.Add( item );
548 }
549 /* wxDataViewModel:: */ ItemsDeleted( wxDataViewItem(0), array );
550 }
551
552 void wxDataViewVirtualListModel::RowChanged( unsigned int row )
553 {
554 /* wxDataViewModel:: */ ItemChanged( GetItem(row) );
555 }
556
557 void wxDataViewVirtualListModel::RowValueChanged( unsigned int row, unsigned int col )
558 {
559 /* wxDataViewModel:: */ ValueChanged( GetItem(row), col );
560 }
561
562 unsigned int wxDataViewVirtualListModel::GetRow( const wxDataViewItem &item ) const
563 {
564 return wxPtrToUInt( item.GetID() ) -1;
565 }
566
567 wxDataViewItem wxDataViewVirtualListModel::GetItem( unsigned int row ) const
568 {
569 return wxDataViewItem( wxUIntToPtr(row+1) );
570 }
571
572 bool wxDataViewVirtualListModel::HasDefaultCompare() const
573 {
574 return true;
575 }
576
577 int wxDataViewVirtualListModel::Compare(const wxDataViewItem& item1,
578 const wxDataViewItem& item2,
579 unsigned int WXUNUSED(column),
580 bool ascending) const
581 {
582 unsigned int pos1 = wxPtrToUInt(item1.GetID()); // -1 not needed here
583 unsigned int pos2 = wxPtrToUInt(item2.GetID()); // -1 not needed here
584
585 if (ascending)
586 return pos1 - pos2;
587 else
588 return pos2 - pos1;
589 }
590
591 unsigned int wxDataViewVirtualListModel::GetChildren( const wxDataViewItem &WXUNUSED(item), wxDataViewItemArray &WXUNUSED(children) ) const
592 {
593 return 0; // should we report an error ?
594 }
595
596 #endif // __WXMAC__
597
598 //-----------------------------------------------------------------------------
599 // wxDataViewIconText
600 //-----------------------------------------------------------------------------
601
602 IMPLEMENT_DYNAMIC_CLASS(wxDataViewIconText,wxObject)
603
604 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxDataViewIconText, WXDLLIMPEXP_ADV)
605
606 // ---------------------------------------------------------
607 // wxDataViewRendererBase
608 // ---------------------------------------------------------
609
610 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase, wxObject)
611
612 wxDataViewRendererBase::wxDataViewRendererBase( const wxString &varianttype,
613 wxDataViewCellMode WXUNUSED(mode),
614 int WXUNUSED(align) )
615 {
616 m_variantType = varianttype;
617 m_owner = NULL;
618 }
619
620 wxDataViewRendererBase::~wxDataViewRendererBase()
621 {
622 }
623
624 const wxDataViewCtrl* wxDataViewRendererBase::GetView() const
625 {
626 return const_cast<wxDataViewRendererBase*>(this)->GetOwner()->GetOwner();
627 }
628
629 bool wxDataViewRendererBase::StartEditing( const wxDataViewItem &item, wxRect labelRect )
630 {
631 wxDataViewCtrl* dv_ctrl = GetOwner()->GetOwner();
632
633 // Before doing anything we send an event asking if editing of this item is really wanted.
634 wxDataViewEvent start_event( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING, dv_ctrl->GetId() );
635 start_event.SetDataViewColumn( GetOwner() );
636 start_event.SetModel( dv_ctrl->GetModel() );
637 start_event.SetItem( item );
638 start_event.SetEventObject( dv_ctrl );
639 dv_ctrl->GetEventHandler()->ProcessEvent( start_event );
640 if( !start_event.IsAllowed() )
641 return false;
642
643 m_item = item; // remember for later
644
645 unsigned int col = GetOwner()->GetModelColumn();
646 wxVariant value;
647 dv_ctrl->GetModel()->GetValue( value, item, col );
648
649 m_editorCtrl = CreateEditorCtrl( dv_ctrl->GetMainWindow(), labelRect, value );
650
651 // there might be no editor control for the given item
652 if(!m_editorCtrl)
653 return false;
654
655 wxDataViewEditorCtrlEvtHandler *handler =
656 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl, (wxDataViewRenderer*) this );
657
658 m_editorCtrl->PushEventHandler( handler );
659
660 #if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
661 handler->SetFocusOnIdle();
662 #else
663 m_editorCtrl->SetFocus();
664 #endif
665
666 // Now we should send Editing Started event
667 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, dv_ctrl->GetId() );
668 event.SetDataViewColumn( GetOwner() );
669 event.SetModel( dv_ctrl->GetModel() );
670 event.SetItem( item );
671 event.SetEventObject( dv_ctrl );
672 dv_ctrl->GetEventHandler()->ProcessEvent( event );
673
674 return true;
675 }
676
677 void wxDataViewRendererBase::DestroyEditControl()
678 {
679 // Hide the control immediately but don't delete it yet as there could be
680 // some pending messages for it.
681 m_editorCtrl->Hide();
682
683 wxEvtHandler * const handler = m_editorCtrl->PopEventHandler();
684
685 wxPendingDelete.Append(handler);
686 wxPendingDelete.Append(m_editorCtrl);
687 }
688
689 void wxDataViewRendererBase::CancelEditing()
690 {
691 if (!m_editorCtrl)
692 return;
693
694 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
695
696 DestroyEditControl();
697 }
698
699 bool wxDataViewRendererBase::FinishEditing()
700 {
701 if (!m_editorCtrl)
702 return true;
703
704 wxVariant value;
705 GetValueFromEditorCtrl( m_editorCtrl, value );
706
707 wxDataViewCtrl* dv_ctrl = GetOwner()->GetOwner();
708
709 dv_ctrl->GetMainWindow()->SetFocus();
710
711 DestroyEditControl();
712
713 if (!Validate(value))
714 return false;
715
716 unsigned int col = GetOwner()->GetModelColumn();
717 dv_ctrl->GetModel()->ChangeValue(value, m_item, col);
718
719 // Now we should send Editing Done event
720 wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, dv_ctrl->GetId() );
721 event.SetDataViewColumn( GetOwner() );
722 event.SetModel( dv_ctrl->GetModel() );
723 event.SetItem( m_item );
724 event.SetEventObject( dv_ctrl );
725 dv_ctrl->GetEventHandler()->ProcessEvent( event );
726
727 return true;
728 }
729
730 // ----------------------------------------------------------------------------
731 // wxDataViewCustomRendererBase
732 // ----------------------------------------------------------------------------
733
734 void
735 wxDataViewCustomRendererBase::WXCallRender(wxRect rectCell, wxDC *dc, int state)
736 {
737 wxCHECK_RET( dc, "no DC to draw on in custom renderer?" );
738
739 // adjust the rectangle ourselves to account for the alignment
740 wxRect rectItem = rectCell;
741 const int align = GetAlignment();
742 if ( align != wxDVR_DEFAULT_ALIGNMENT )
743 {
744 const wxSize size = GetSize();
745
746 // take alignment into account only if there is enough space, otherwise
747 // show as much contents as possible
748 //
749 // notice that many existing renderers (e.g. wxDataViewSpinRenderer)
750 // return hard-coded size which can be more than they need and if we
751 // trusted their GetSize() we'd draw the text out of cell bounds
752 // entirely
753
754 if ( size.x >= 0 && size.x < rectCell.width )
755 {
756 if ( align & wxALIGN_CENTER_HORIZONTAL )
757 rectItem.x += (rectCell.width - size.x)/2;
758 else if ( align & wxALIGN_RIGHT )
759 rectItem.x += rectCell.width - size.x;
760 // else: wxALIGN_LEFT is the default
761
762 rectItem.width = size.x;
763 }
764
765 if ( size.y >= 0 && size.y < rectCell.height )
766 {
767 if ( align & wxALIGN_CENTER_VERTICAL )
768 rectItem.y += (rectCell.height - size.y)/2;
769 else if ( align & wxALIGN_BOTTOM )
770 rectItem.y += rectCell.height - size.y;
771 // else: wxALIGN_TOP is the default
772
773 rectItem.height = size.y;
774 }
775 }
776
777
778 // set up the DC attributes
779
780 // override custom foreground with the standard one for the selected items
781 // because we currently don't allow changing the selection background and
782 // custom colours may be unreadable on it
783 wxColour col;
784 if ( state & wxDATAVIEW_CELL_SELECTED )
785 col = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
786 else if ( m_attr.HasColour() )
787 col = m_attr.GetColour();
788 else // use default foreground
789 col = GetOwner()->GetOwner()->GetForegroundColour();
790
791 wxDCTextColourChanger changeFg(*dc, col);
792
793 wxDCFontChanger changeFont(*dc);
794 if ( m_attr.HasFont() )
795 {
796 wxFont font(dc->GetFont());
797 if ( m_attr.GetBold() )
798 font.MakeBold();
799 if ( m_attr.GetItalic() )
800 font.MakeItalic();
801
802 changeFont.Set(font);
803 }
804
805 Render(rectItem, dc, state);
806 }
807
808 void
809 wxDataViewCustomRendererBase::RenderText(const wxString& text,
810 int xoffset,
811 wxRect rect,
812 wxDC *dc,
813 int WXUNUSED(state))
814 {
815 wxRect rectText = rect;
816 rectText.x += xoffset;
817 rectText.width -= xoffset;
818
819 // check if we want to ellipsize the text if it doesn't fit
820 wxString ellipsizedText;
821 if ( GetEllipsizeMode() != wxELLIPSIZE_NONE )
822 {
823 ellipsizedText = wxControl::Ellipsize
824 (
825 text,
826 *dc,
827 GetEllipsizeMode(),
828 rectText.width,
829 wxELLIPSIZE_FLAGS_NONE
830 );
831 }
832
833 // get the alignment to use
834 int align = GetAlignment();
835 if ( align == wxDVR_DEFAULT_ALIGNMENT )
836 {
837 // if we don't have an explicit alignment ourselves, use that of the
838 // column in horizontal direction and default vertical alignment
839 align = GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL;
840 }
841
842 dc->DrawLabel(ellipsizedText.empty() ? text : ellipsizedText,
843 rectText, align);
844 }
845
846 //-----------------------------------------------------------------------------
847 // wxDataViewEditorCtrlEvtHandler
848 //-----------------------------------------------------------------------------
849
850 BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler, wxEvtHandler)
851 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar)
852 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus)
853 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle)
854 EVT_TEXT_ENTER (-1, wxDataViewEditorCtrlEvtHandler::OnTextEnter)
855 END_EVENT_TABLE()
856
857 void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent &event )
858 {
859 if (m_focusOnIdle)
860 {
861 m_focusOnIdle = false;
862 if (wxWindow::FindFocus() != m_editorCtrl)
863 m_editorCtrl->SetFocus();
864 }
865
866 event.Skip();
867 }
868
869 void wxDataViewEditorCtrlEvtHandler::OnTextEnter( wxCommandEvent &WXUNUSED(event) )
870 {
871 m_finished = true;
872 m_owner->FinishEditing();
873 }
874
875 void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent &event )
876 {
877 switch ( event.m_keyCode )
878 {
879 case WXK_RETURN:
880 m_finished = true;
881 m_owner->FinishEditing();
882 break;
883
884 case WXK_ESCAPE:
885 {
886 m_finished = true;
887 m_owner->CancelEditing();
888 break;
889 }
890 default:
891 event.Skip();
892 }
893 }
894
895 void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent &event )
896 {
897 if (!m_finished)
898 {
899 m_finished = true;
900 m_owner->FinishEditing();
901 }
902
903 event.Skip();
904 }
905
906 // ---------------------------------------------------------
907 // wxDataViewColumnBase
908 // ---------------------------------------------------------
909
910 void wxDataViewColumnBase::Init(wxDataViewRenderer *renderer,
911 unsigned int model_column)
912 {
913 m_renderer = renderer;
914 m_model_column = model_column;
915 m_owner = NULL;
916 m_renderer->SetOwner( (wxDataViewColumn*) this );
917 }
918
919 wxDataViewColumnBase::~wxDataViewColumnBase()
920 {
921 delete m_renderer;
922 }
923
924 // ---------------------------------------------------------
925 // wxDataViewCtrlBase
926 // ---------------------------------------------------------
927
928 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase, wxControl)
929
930 wxDataViewCtrlBase::wxDataViewCtrlBase()
931 {
932 m_model = NULL;
933 m_expander_column = 0;
934 m_indent = 8;
935 }
936
937 wxDataViewCtrlBase::~wxDataViewCtrlBase()
938 {
939 if (m_model)
940 {
941 m_model->DecRef();
942 m_model = NULL;
943 }
944 }
945
946 bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel *model )
947 {
948 if (m_model)
949 {
950 m_model->DecRef(); // discard old model, if any
951 }
952
953 // add our own reference to the new model:
954 m_model = model;
955 if (m_model)
956 {
957 m_model->IncRef();
958 }
959
960 return true;
961 }
962
963 wxDataViewModel* wxDataViewCtrlBase::GetModel()
964 {
965 return m_model;
966 }
967
968 const wxDataViewModel* wxDataViewCtrlBase::GetModel() const
969 {
970 return m_model;
971 }
972
973 void wxDataViewCtrlBase::ExpandAncestors( const wxDataViewItem & item )
974 {
975 if (!m_model) return;
976
977 if (!item.IsOk()) return;
978
979 wxVector<wxDataViewItem> parentChain;
980
981 // at first we get all the parents of the selected item
982 wxDataViewItem parent = m_model->GetParent(item);
983 while (parent.IsOk())
984 {
985 parentChain.push_back(parent);
986 parent = m_model->GetParent(parent);
987 }
988
989 // then we expand the parents, starting at the root
990 while (!parentChain.empty())
991 {
992 Expand(parentChain.back());
993 parentChain.pop_back();
994 }
995 }
996
997 wxDataViewItem wxDataViewCtrlBase::GetCurrentItem() const
998 {
999 return HasFlag(wxDV_MULTIPLE) ? DoGetCurrentItem()
1000 : GetSelection();
1001 }
1002
1003 void wxDataViewCtrlBase::SetCurrentItem(const wxDataViewItem& item)
1004 {
1005 wxCHECK_RET( item.IsOk(), "Can't make current an invalid item." );
1006
1007 if ( HasFlag(wxDV_MULTIPLE) )
1008 DoSetCurrentItem(item);
1009 else
1010 Select(item);
1011 }
1012
1013 wxDataViewColumn *
1014 wxDataViewCtrlBase::AppendTextColumn( const wxString &label, unsigned int model_column,
1015 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1016 {
1017 wxDataViewColumn *ret = new wxDataViewColumn( label,
1018 new wxDataViewTextRenderer( wxT("string"), mode ),
1019 model_column, width, align, flags );
1020 AppendColumn( ret );
1021 return ret;
1022 }
1023
1024 wxDataViewColumn *
1025 wxDataViewCtrlBase::AppendIconTextColumn( const wxString &label, unsigned int model_column,
1026 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1027 {
1028 wxDataViewColumn *ret = new wxDataViewColumn( label,
1029 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1030 model_column, width, align, flags );
1031 AppendColumn( ret );
1032 return ret;
1033 }
1034
1035 wxDataViewColumn *
1036 wxDataViewCtrlBase::AppendToggleColumn( const wxString &label, unsigned int model_column,
1037 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1038 {
1039 wxDataViewColumn *ret = new wxDataViewColumn( label,
1040 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1041 model_column, width, align, flags );
1042 AppendColumn( ret );
1043 return ret;
1044 }
1045
1046 wxDataViewColumn *
1047 wxDataViewCtrlBase::AppendProgressColumn( const wxString &label, unsigned int model_column,
1048 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1049 {
1050 wxDataViewColumn *ret = new wxDataViewColumn( label,
1051 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1052 model_column, width, align, flags );
1053 AppendColumn( ret );
1054 return ret;
1055 }
1056
1057 wxDataViewColumn *
1058 wxDataViewCtrlBase::AppendDateColumn( const wxString &label, unsigned int model_column,
1059 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1060 {
1061 wxDataViewColumn *ret = new wxDataViewColumn( label,
1062 new wxDataViewDateRenderer( wxT("datetime"), mode ),
1063 model_column, width, align, flags );
1064 AppendColumn( ret );
1065 return ret;
1066 }
1067
1068 wxDataViewColumn *
1069 wxDataViewCtrlBase::AppendBitmapColumn( const wxString &label, unsigned int model_column,
1070 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1071 {
1072 wxDataViewColumn *ret = new wxDataViewColumn( label,
1073 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
1074 model_column, width, align, flags );
1075 AppendColumn( ret );
1076 return ret;
1077 }
1078
1079 wxDataViewColumn *
1080 wxDataViewCtrlBase::AppendTextColumn( const wxBitmap &label, unsigned int model_column,
1081 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1082 {
1083 wxDataViewColumn *ret = new wxDataViewColumn( label,
1084 new wxDataViewTextRenderer( wxT("string"), mode ),
1085 model_column, width, align, flags );
1086 AppendColumn( ret );
1087 return ret;
1088 }
1089
1090 wxDataViewColumn *
1091 wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap &label, unsigned int model_column,
1092 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1093 {
1094 wxDataViewColumn *ret = new wxDataViewColumn( label,
1095 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1096 model_column, width, align, flags );
1097 AppendColumn( ret );
1098 return ret;
1099 }
1100
1101 wxDataViewColumn *
1102 wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap &label, unsigned int model_column,
1103 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1104 {
1105 wxDataViewColumn *ret = new wxDataViewColumn( label,
1106 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1107 model_column, width, align, flags );
1108 AppendColumn( ret );
1109 return ret;
1110 }
1111
1112 wxDataViewColumn *
1113 wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap &label, unsigned int model_column,
1114 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1115 {
1116 wxDataViewColumn *ret = new wxDataViewColumn( label,
1117 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1118 model_column, width, align, flags );
1119 AppendColumn( ret );
1120 return ret;
1121 }
1122
1123 wxDataViewColumn *
1124 wxDataViewCtrlBase::AppendDateColumn( const wxBitmap &label, unsigned int model_column,
1125 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1126 {
1127 wxDataViewColumn *ret = new wxDataViewColumn( label,
1128 new wxDataViewDateRenderer( wxT("datetime"), mode ),
1129 model_column, width, align, flags );
1130 AppendColumn( ret );
1131 return ret;
1132 }
1133
1134 wxDataViewColumn *
1135 wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap &label, unsigned int model_column,
1136 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1137 {
1138 wxDataViewColumn *ret = new wxDataViewColumn( label,
1139 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
1140 model_column, width, align, flags );
1141 AppendColumn( ret );
1142 return ret;
1143 }
1144
1145 wxDataViewColumn *
1146 wxDataViewCtrlBase::PrependTextColumn( const wxString &label, unsigned int model_column,
1147 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1148 {
1149 wxDataViewColumn *ret = new wxDataViewColumn( label,
1150 new wxDataViewTextRenderer( wxT("string"), mode ),
1151 model_column, width, align, flags );
1152 PrependColumn( ret );
1153 return ret;
1154 }
1155
1156 wxDataViewColumn *
1157 wxDataViewCtrlBase::PrependIconTextColumn( const wxString &label, unsigned int model_column,
1158 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1159 {
1160 wxDataViewColumn *ret = new wxDataViewColumn( label,
1161 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1162 model_column, width, align, flags );
1163 PrependColumn( ret );
1164 return ret;
1165 }
1166
1167 wxDataViewColumn *
1168 wxDataViewCtrlBase::PrependToggleColumn( const wxString &label, unsigned int model_column,
1169 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1170 {
1171
1172 wxDataViewColumn *ret = new wxDataViewColumn( label,
1173 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1174 model_column, width, align, flags );
1175 PrependColumn( ret );
1176 return ret;
1177 }
1178
1179 wxDataViewColumn *
1180 wxDataViewCtrlBase::PrependProgressColumn( const wxString &label, unsigned int model_column,
1181 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1182 {
1183 wxDataViewColumn *ret = new wxDataViewColumn( label,
1184 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1185 model_column, width, align, flags );
1186 PrependColumn( ret );
1187 return ret;
1188 }
1189
1190 wxDataViewColumn *
1191 wxDataViewCtrlBase::PrependDateColumn( const wxString &label, unsigned int model_column,
1192 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1193 {
1194 wxDataViewColumn *ret = new wxDataViewColumn( label,
1195 new wxDataViewDateRenderer( wxT("datetime"), mode ),
1196 model_column, width, align, flags );
1197 PrependColumn( ret );
1198 return ret;
1199 }
1200
1201 wxDataViewColumn *
1202 wxDataViewCtrlBase::PrependBitmapColumn( const wxString &label, unsigned int model_column,
1203 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1204 {
1205 wxDataViewColumn *ret = new wxDataViewColumn( label,
1206 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
1207 model_column, width, align, flags );
1208 PrependColumn( ret );
1209 return ret;
1210 }
1211
1212 wxDataViewColumn *
1213 wxDataViewCtrlBase::PrependTextColumn( const wxBitmap &label, unsigned int model_column,
1214 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1215 {
1216 wxDataViewColumn *ret = new wxDataViewColumn( label,
1217 new wxDataViewTextRenderer( wxT("string"), mode ),
1218 model_column, width, align, flags );
1219 PrependColumn( ret );
1220 return ret;
1221 }
1222
1223 wxDataViewColumn *
1224 wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap &label, unsigned int model_column,
1225 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1226 {
1227 wxDataViewColumn *ret = new wxDataViewColumn( label,
1228 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1229 model_column, width, align, flags );
1230 PrependColumn( ret );
1231 return ret;
1232 }
1233
1234 wxDataViewColumn *
1235 wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap &label, unsigned int model_column,
1236 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1237 {
1238 wxDataViewColumn *ret = new wxDataViewColumn( label,
1239 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1240 model_column, width, align, flags );
1241 PrependColumn( ret );
1242 return ret;
1243 }
1244
1245 wxDataViewColumn *
1246 wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap &label, unsigned int model_column,
1247 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1248 {
1249 wxDataViewColumn *ret = new wxDataViewColumn( label,
1250 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1251 model_column, width, align, flags );
1252 PrependColumn( ret );
1253 return ret;
1254 }
1255
1256 wxDataViewColumn *
1257 wxDataViewCtrlBase::PrependDateColumn( const wxBitmap &label, unsigned int model_column,
1258 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1259 {
1260 wxDataViewColumn *ret = new wxDataViewColumn( label,
1261 new wxDataViewDateRenderer( wxT("datetime"), mode ),
1262 model_column, width, align, flags );
1263 PrependColumn( ret );
1264 return ret;
1265 }
1266
1267 wxDataViewColumn *
1268 wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap &label, unsigned int model_column,
1269 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1270 {
1271 wxDataViewColumn *ret = new wxDataViewColumn( label,
1272 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode ),
1273 model_column, width, align, flags );
1274 PrependColumn( ret );
1275 return ret;
1276 }
1277
1278 bool
1279 wxDataViewCtrlBase::AppendColumn( wxDataViewColumn *col )
1280 {
1281 col->SetOwner( (wxDataViewCtrl*) this );
1282 return true;
1283 }
1284
1285 bool
1286 wxDataViewCtrlBase::PrependColumn( wxDataViewColumn *col )
1287 {
1288 col->SetOwner( (wxDataViewCtrl*) this );
1289 return true;
1290 }
1291
1292 bool
1293 wxDataViewCtrlBase::InsertColumn( unsigned int WXUNUSED(pos), wxDataViewColumn *col )
1294 {
1295 col->SetOwner( (wxDataViewCtrl*) this );
1296 return true;
1297 }
1298
1299 // ---------------------------------------------------------
1300 // wxDataViewEvent
1301 // ---------------------------------------------------------
1302
1303 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent,wxNotifyEvent)
1304
1305 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, wxDataViewEvent );
1306
1307 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, wxDataViewEvent );
1308 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, wxDataViewEvent );
1309 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, wxDataViewEvent );
1310 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, wxDataViewEvent );
1311 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, wxDataViewEvent );
1312 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, wxDataViewEvent );
1313 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING, wxDataViewEvent );
1314 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, wxDataViewEvent );
1315 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED, wxDataViewEvent );
1316
1317 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, wxDataViewEvent );
1318
1319 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, wxDataViewEvent );
1320 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, wxDataViewEvent );
1321 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, wxDataViewEvent );
1322 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_REORDERED, wxDataViewEvent );
1323
1324 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_CACHE_HINT, wxDataViewEvent );
1325
1326 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG, wxDataViewEvent );
1327 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE, wxDataViewEvent );
1328 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_DROP, wxDataViewEvent );
1329
1330
1331
1332 // -------------------------------------
1333 // wxDataViewSpinRenderer
1334 // -------------------------------------
1335
1336 wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min, int max, wxDataViewCellMode mode, int alignment ) :
1337 wxDataViewCustomRenderer(wxT("long"), mode, alignment )
1338 {
1339 m_min = min;
1340 m_max = max;
1341 }
1342
1343 wxControl* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
1344 {
1345 long l = value;
1346 wxSize size = labelRect.GetSize();
1347 #ifdef __WXMAC__
1348 size = wxSize( wxMax(70,labelRect.width ), -1 );
1349 #endif
1350 wxString str;
1351 str.Printf( wxT("%d"), (int) l );
1352 wxSpinCtrl *sc = new wxSpinCtrl( parent, wxID_ANY, str,
1353 labelRect.GetTopLeft(), size, wxSP_ARROW_KEYS|wxTE_PROCESS_ENTER, m_min, m_max, l );
1354 #ifdef __WXMAC__
1355 size = sc->GetSize();
1356 wxPoint pt = sc->GetPosition();
1357 sc->SetSize( pt.x - 4, pt.y - 4, size.x, size.y );
1358 #endif
1359
1360 return sc;
1361 }
1362
1363 bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
1364 {
1365 wxSpinCtrl *sc = (wxSpinCtrl*) editor;
1366 long l = sc->GetValue();
1367 value = l;
1368 return true;
1369 }
1370
1371 bool wxDataViewSpinRenderer::Render( wxRect rect, wxDC *dc, int state )
1372 {
1373 wxString str;
1374 str.Printf(wxT("%d"), (int) m_data );
1375 RenderText( str, 0, rect, dc, state );
1376 return true;
1377 }
1378
1379 wxSize wxDataViewSpinRenderer::GetSize() const
1380 {
1381 return wxSize(80,16);
1382 }
1383
1384 bool wxDataViewSpinRenderer::SetValue( const wxVariant &value )
1385 {
1386 m_data = value.GetLong();
1387 return true;
1388 }
1389
1390 bool wxDataViewSpinRenderer::GetValue( wxVariant &value ) const
1391 {
1392 value = m_data;
1393 return true;
1394 }
1395
1396 // -------------------------------------
1397 // wxDataViewChoiceRenderer
1398 // -------------------------------------
1399
1400 #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXOSX_CARBON__)
1401
1402 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString& choices, wxDataViewCellMode mode, int alignment ) :
1403 wxDataViewCustomRenderer(wxT("string"), mode, alignment )
1404 {
1405 m_choices = choices;
1406 }
1407
1408 wxControl* wxDataViewChoiceRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
1409 {
1410 wxChoice* c = new wxChoice(parent, wxID_ANY, labelRect.GetTopLeft(), wxDefaultSize, m_choices );
1411 c->Move(labelRect.GetRight() - c->GetRect().width, wxDefaultCoord);
1412 c->SetStringSelection( value.GetString() );
1413 return c;
1414 }
1415
1416 bool wxDataViewChoiceRenderer::GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
1417 {
1418 wxChoice *c = (wxChoice*) editor;
1419 wxString s = c->GetStringSelection();
1420 value = s;
1421 return true;
1422 }
1423
1424 bool wxDataViewChoiceRenderer::Render( wxRect rect, wxDC *dc, int state )
1425 {
1426 RenderText( m_data, 0, rect, dc, state );
1427 return true;
1428 }
1429
1430 wxSize wxDataViewChoiceRenderer::GetSize() const
1431 {
1432 return wxSize(80,16);
1433 }
1434
1435 bool wxDataViewChoiceRenderer::SetValue( const wxVariant &value )
1436 {
1437 m_data = value.GetString();
1438 return true;
1439 }
1440
1441 bool wxDataViewChoiceRenderer::GetValue( wxVariant &value ) const
1442 {
1443 value = m_data;
1444 return true;
1445 }
1446
1447 // ----------------------------------------------------------------------------
1448 // wxDataViewChoiceByIndexRenderer
1449 // ----------------------------------------------------------------------------
1450
1451 wxDataViewChoiceByIndexRenderer::wxDataViewChoiceByIndexRenderer( const wxArrayString &choices,
1452 wxDataViewCellMode mode, int alignment ) :
1453 wxDataViewChoiceRenderer( choices, mode, alignment )
1454 {
1455 }
1456
1457 wxControl* wxDataViewChoiceByIndexRenderer::CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value )
1458 {
1459 wxVariant string_value = GetChoice( value.GetLong() );
1460
1461 return wxDataViewChoiceRenderer::CreateEditorCtrl( parent, labelRect, string_value );
1462 }
1463
1464 bool wxDataViewChoiceByIndexRenderer::GetValueFromEditorCtrl( wxControl* editor, wxVariant &value )
1465 {
1466 wxVariant string_value;
1467 if (!wxDataViewChoiceRenderer::GetValueFromEditorCtrl( editor, string_value ))
1468 return false;
1469
1470 value = (long) GetChoices().Index( string_value.GetString() );
1471 return true;
1472 }
1473
1474 bool wxDataViewChoiceByIndexRenderer::SetValue( const wxVariant &value )
1475 {
1476 wxVariant string_value = GetChoice( value.GetLong() );
1477 return wxDataViewChoiceRenderer::SetValue( string_value );
1478 }
1479
1480 bool wxDataViewChoiceByIndexRenderer::GetValue( wxVariant &value ) const
1481 {
1482 wxVariant string_value;
1483 if (!wxDataViewChoiceRenderer::GetValue( string_value ))
1484 return false;
1485
1486 value = (long) GetChoices().Index( string_value.GetString() );
1487 return true;
1488 }
1489
1490 #endif
1491
1492 //-----------------------------------------------------------------------------
1493 // wxDataViewListStore
1494 //-----------------------------------------------------------------------------
1495
1496 wxDataViewListStore::wxDataViewListStore()
1497 {
1498 }
1499
1500 wxDataViewListStore::~wxDataViewListStore()
1501 {
1502 wxVector<wxDataViewListStoreLine*>::iterator it;
1503 for (it = m_data.begin(); it != m_data.end(); ++it)
1504 {
1505 wxDataViewListStoreLine* line = *it;
1506 delete line;
1507 }
1508 }
1509
1510 void wxDataViewListStore::PrependColumn( const wxString &varianttype )
1511 {
1512 m_cols.Insert( varianttype, 0 );
1513 }
1514
1515 void wxDataViewListStore::InsertColumn( unsigned int pos, const wxString &varianttype )
1516 {
1517 m_cols.Insert( varianttype, pos );
1518 }
1519
1520 void wxDataViewListStore::AppendColumn( const wxString &varianttype )
1521 {
1522 m_cols.Add( varianttype );
1523 }
1524
1525 unsigned int wxDataViewListStore::GetColumnCount() const
1526 {
1527 return m_cols.GetCount();
1528 }
1529
1530 wxString wxDataViewListStore::GetColumnType( unsigned int pos ) const
1531 {
1532 return m_cols[pos];
1533 }
1534
1535 void wxDataViewListStore::AppendItem( const wxVector<wxVariant> &values, wxClientData *data )
1536 {
1537 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1538 line->m_values = values;
1539 m_data.push_back( line );
1540
1541 RowAppended();
1542 }
1543
1544 void wxDataViewListStore::PrependItem( const wxVector<wxVariant> &values, wxClientData *data )
1545 {
1546 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1547 line->m_values = values;
1548 m_data.insert( m_data.begin(), line );
1549
1550 RowPrepended();
1551 }
1552
1553 void wxDataViewListStore::InsertItem( unsigned int row, const wxVector<wxVariant> &values,
1554 wxClientData *data )
1555 {
1556 wxDataViewListStoreLine *line = new wxDataViewListStoreLine( data );
1557 line->m_values = values;
1558 m_data.insert( m_data.begin()+row, line );
1559
1560 RowInserted( row );
1561 }
1562
1563 void wxDataViewListStore::DeleteItem( unsigned int row )
1564 {
1565 wxVector<wxDataViewListStoreLine*>::iterator it = m_data.begin() + row;
1566 delete *it;
1567 m_data.erase( it );
1568
1569 RowDeleted( row );
1570 }
1571
1572 void wxDataViewListStore::DeleteAllItems()
1573 {
1574 wxVector<wxDataViewListStoreLine*>::iterator it;
1575 for (it = m_data.begin(); it != m_data.end(); ++it)
1576 {
1577 wxDataViewListStoreLine* line = *it;
1578 delete line;
1579 }
1580
1581 m_data.clear();
1582
1583 Reset( 0 );
1584 }
1585
1586 void wxDataViewListStore::GetValueByRow( wxVariant &value, unsigned int row, unsigned int col ) const
1587 {
1588 wxDataViewListStoreLine *line = m_data[row];
1589 value = line->m_values[col];
1590 }
1591
1592 bool wxDataViewListStore::SetValueByRow( const wxVariant &value, unsigned int row, unsigned int col )
1593 {
1594 wxDataViewListStoreLine *line = m_data[row];
1595 line->m_values[col] = value;
1596
1597 return true;
1598 }
1599
1600 //-----------------------------------------------------------------------------
1601 // wxDataViewListCtrl
1602 //-----------------------------------------------------------------------------
1603
1604 IMPLEMENT_DYNAMIC_CLASS(wxDataViewListCtrl,wxDataViewCtrl)
1605
1606 BEGIN_EVENT_TABLE(wxDataViewListCtrl,wxDataViewCtrl)
1607 EVT_SIZE( wxDataViewListCtrl::OnSize )
1608 END_EVENT_TABLE()
1609
1610 wxDataViewListCtrl::wxDataViewListCtrl()
1611 {
1612 }
1613
1614 wxDataViewListCtrl::wxDataViewListCtrl( wxWindow *parent, wxWindowID id,
1615 const wxPoint& pos, const wxSize& size, long style,
1616 const wxValidator& validator )
1617 {
1618 Create( parent, id, pos, size, style, validator );
1619 }
1620
1621 wxDataViewListCtrl::~wxDataViewListCtrl()
1622 {
1623 }
1624
1625
1626 bool wxDataViewListCtrl::Create( wxWindow *parent, wxWindowID id,
1627 const wxPoint& pos, const wxSize& size, long style,
1628 const wxValidator& validator )
1629 {
1630 if ( !wxDataViewCtrl::Create( parent, id, pos, size, style, validator ) )
1631 return false;
1632
1633 wxDataViewListStore *store = new wxDataViewListStore;
1634 AssociateModel( store );
1635 store->DecRef();
1636
1637 return true;
1638 }
1639
1640 bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *column, const wxString &varianttype )
1641 {
1642 GetStore()->AppendColumn( varianttype );
1643 return wxDataViewCtrl::AppendColumn( column );
1644 }
1645
1646 bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *column, const wxString &varianttype )
1647 {
1648 GetStore()->PrependColumn( varianttype );
1649 return wxDataViewCtrl::PrependColumn( column );
1650 }
1651
1652 bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *column, const wxString &varianttype )
1653 {
1654 GetStore()->InsertColumn( pos, varianttype );
1655 return wxDataViewCtrl::InsertColumn( pos, column );
1656 }
1657
1658 bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn *col )
1659 {
1660 return PrependColumn( col, "string" );
1661 }
1662
1663 bool wxDataViewListCtrl::InsertColumn( unsigned int pos, wxDataViewColumn *col )
1664 {
1665 return InsertColumn( pos, col, "string" );
1666 }
1667
1668 bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn *col )
1669 {
1670 return AppendColumn( col, "string" );
1671 }
1672
1673 wxDataViewColumn *wxDataViewListCtrl::AppendTextColumn( const wxString &label,
1674 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1675 {
1676 GetStore()->AppendColumn( wxT("string") );
1677
1678 wxDataViewColumn *ret = new wxDataViewColumn( label,
1679 new wxDataViewTextRenderer( wxT("string"), mode ),
1680 GetStore()->GetColumnCount()-1, width, align, flags );
1681
1682 wxDataViewCtrl::AppendColumn( ret );
1683
1684 return ret;
1685 }
1686
1687 wxDataViewColumn *wxDataViewListCtrl::AppendToggleColumn( const wxString &label,
1688 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1689 {
1690 GetStore()->AppendColumn( wxT("bool") );
1691
1692 wxDataViewColumn *ret = new wxDataViewColumn( label,
1693 new wxDataViewToggleRenderer( wxT("bool"), mode ),
1694 GetStore()->GetColumnCount()-1, width, align, flags );
1695
1696 wxDataViewCtrl::AppendColumn( ret );
1697
1698 return ret;
1699 }
1700
1701 wxDataViewColumn *wxDataViewListCtrl::AppendProgressColumn( const wxString &label,
1702 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1703 {
1704 GetStore()->AppendColumn( wxT("long") );
1705
1706 wxDataViewColumn *ret = new wxDataViewColumn( label,
1707 new wxDataViewProgressRenderer( wxEmptyString, wxT("long"), mode ),
1708 GetStore()->GetColumnCount()-1, width, align, flags );
1709
1710 wxDataViewCtrl::AppendColumn( ret );
1711
1712 return ret;
1713 }
1714
1715 wxDataViewColumn *wxDataViewListCtrl::AppendIconTextColumn( const wxString &label,
1716 wxDataViewCellMode mode, int width, wxAlignment align, int flags )
1717 {
1718 GetStore()->AppendColumn( wxT("wxDataViewIconText") );
1719
1720 wxDataViewColumn *ret = new wxDataViewColumn( label,
1721 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode ),
1722 GetStore()->GetColumnCount()-1, width, align, flags );
1723
1724 wxDataViewCtrl::AppendColumn( ret );
1725
1726 return ret;
1727 }
1728
1729 void wxDataViewListCtrl::OnSize( wxSizeEvent &event )
1730 {
1731 event.Skip( true );
1732 }
1733
1734 //-----------------------------------------------------------------------------
1735 // wxDataViewTreeStore
1736 //-----------------------------------------------------------------------------
1737
1738 wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1739 wxDataViewTreeStoreNode *parent,
1740 const wxString &text, const wxIcon &icon, wxClientData *data )
1741 {
1742 m_parent = parent;
1743 m_text = text;
1744 m_icon = icon;
1745 m_data = data;
1746 }
1747
1748 wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1749 {
1750 if (m_data)
1751 delete m_data;
1752 }
1753
1754 #include "wx/listimpl.cpp"
1755 WX_DEFINE_LIST(wxDataViewTreeStoreNodeList)
1756
1757 wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1758 wxDataViewTreeStoreNode *parent, const wxString &text,
1759 const wxIcon &icon, const wxIcon &expanded, wxClientData *data ) :
1760 wxDataViewTreeStoreNode( parent, text, icon, data )
1761 {
1762 m_iconExpanded = expanded;
1763 m_isExpanded = false;
1764 m_children.DeleteContents(true);
1765 }
1766
1767 wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1768 {
1769 }
1770
1771 //-----------------------------------------------------------------------------
1772
1773 wxDataViewTreeStore::wxDataViewTreeStore()
1774 {
1775 m_root = new wxDataViewTreeStoreContainerNode( NULL, wxEmptyString );
1776 }
1777
1778 wxDataViewTreeStore::~wxDataViewTreeStore()
1779 {
1780 delete m_root;
1781 }
1782
1783 wxDataViewItem wxDataViewTreeStore::AppendItem( const wxDataViewItem& parent,
1784 const wxString &text, const wxIcon &icon, wxClientData *data )
1785 {
1786 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1787 if (!parent_node) return wxDataViewItem(0);
1788
1789 wxDataViewTreeStoreNode *node =
1790 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1791 parent_node->GetChildren().Append( node );
1792
1793 return node->GetItem();
1794 }
1795
1796 wxDataViewItem wxDataViewTreeStore::PrependItem( const wxDataViewItem& parent,
1797 const wxString &text, const wxIcon &icon, wxClientData *data )
1798 {
1799 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1800 if (!parent_node) return wxDataViewItem(0);
1801
1802 wxDataViewTreeStoreNode *node =
1803 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1804 parent_node->GetChildren().Insert( node );
1805
1806 return node->GetItem();
1807 }
1808
1809 wxDataViewItem
1810 wxDataViewTreeStore::InsertItem(const wxDataViewItem& parent,
1811 const wxDataViewItem& previous,
1812 const wxString& text,
1813 const wxIcon& icon,
1814 wxClientData *data)
1815 {
1816 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1817 if (!parent_node) return wxDataViewItem(0);
1818
1819 wxDataViewTreeStoreNode *previous_node = FindNode( previous );
1820 int pos = parent_node->GetChildren().IndexOf( previous_node );
1821 if (pos == wxNOT_FOUND) return wxDataViewItem(0);
1822
1823 wxDataViewTreeStoreNode *node =
1824 new wxDataViewTreeStoreNode( parent_node, text, icon, data );
1825 parent_node->GetChildren().Insert( (size_t) pos, node );
1826
1827 return node->GetItem();
1828 }
1829
1830 wxDataViewItem wxDataViewTreeStore::PrependContainer( const wxDataViewItem& parent,
1831 const wxString &text, const wxIcon &icon, const wxIcon &expanded,
1832 wxClientData *data )
1833 {
1834 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1835 if (!parent_node) return wxDataViewItem(0);
1836
1837 wxDataViewTreeStoreContainerNode *node =
1838 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1839 parent_node->GetChildren().Insert( node );
1840
1841 return node->GetItem();
1842 }
1843
1844 wxDataViewItem
1845 wxDataViewTreeStore::AppendContainer(const wxDataViewItem& parent,
1846 const wxString &text,
1847 const wxIcon& icon,
1848 const wxIcon& expanded,
1849 wxClientData * data)
1850 {
1851 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1852 if (!parent_node) return wxDataViewItem(0);
1853
1854 wxDataViewTreeStoreContainerNode *node =
1855 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1856 parent_node->GetChildren().Append( node );
1857
1858 return node->GetItem();
1859 }
1860
1861 wxDataViewItem
1862 wxDataViewTreeStore::InsertContainer(const wxDataViewItem& parent,
1863 const wxDataViewItem& previous,
1864 const wxString& text,
1865 const wxIcon& icon,
1866 const wxIcon& expanded,
1867 wxClientData * data)
1868 {
1869 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1870 if (!parent_node) return wxDataViewItem(0);
1871
1872 wxDataViewTreeStoreNode *previous_node = FindNode( previous );
1873 int pos = parent_node->GetChildren().IndexOf( previous_node );
1874 if (pos == wxNOT_FOUND) return wxDataViewItem(0);
1875
1876 wxDataViewTreeStoreContainerNode *node =
1877 new wxDataViewTreeStoreContainerNode( parent_node, text, icon, expanded, data );
1878 parent_node->GetChildren().Insert( (size_t) pos, node );
1879
1880 return node->GetItem();
1881 }
1882
1883 bool wxDataViewTreeStore::IsContainer( const wxDataViewItem& item ) const
1884 {
1885 wxDataViewTreeStoreNode *node = FindNode( item );
1886 if (!node) return false;
1887
1888 return node->IsContainer();
1889 }
1890
1891 wxDataViewItem wxDataViewTreeStore::GetNthChild( const wxDataViewItem& parent, unsigned int pos ) const
1892 {
1893 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent );
1894 if (!parent_node) return wxDataViewItem(0);
1895
1896 wxDataViewTreeStoreNodeList::compatibility_iterator node = parent_node->GetChildren().Item( pos );
1897 if (node)
1898 return node->GetData();
1899
1900 return wxDataViewItem(0);
1901 }
1902
1903 int wxDataViewTreeStore::GetChildCount( const wxDataViewItem& parent ) const
1904 {
1905 wxDataViewTreeStoreNode *node = FindNode( parent );
1906 if (!node) return -1;
1907
1908 if (!node->IsContainer())
1909 return 0;
1910
1911 wxDataViewTreeStoreContainerNode *container_node = (wxDataViewTreeStoreContainerNode*) node;
1912 return (int) container_node->GetChildren().GetCount();
1913 }
1914
1915 void wxDataViewTreeStore::SetItemText( const wxDataViewItem& item, const wxString &text )
1916 {
1917 wxDataViewTreeStoreNode *node = FindNode( item );
1918 if (!node) return;
1919
1920 node->SetText( text );
1921 }
1922
1923 wxString wxDataViewTreeStore::GetItemText( const wxDataViewItem& item ) const
1924 {
1925 wxDataViewTreeStoreNode *node = FindNode( item );
1926 if (!node) return wxEmptyString;
1927
1928 return node->GetText();
1929 }
1930
1931 void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
1932 {
1933 wxDataViewTreeStoreNode *node = FindNode( item );
1934 if (!node) return;
1935
1936 node->SetIcon( icon );
1937 }
1938
1939 const wxIcon &wxDataViewTreeStore::GetItemIcon( const wxDataViewItem& item ) const
1940 {
1941 wxDataViewTreeStoreNode *node = FindNode( item );
1942 if (!node) return wxNullIcon;
1943
1944 return node->GetIcon();
1945 }
1946
1947 void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
1948 {
1949 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1950 if (!node) return;
1951
1952 node->SetExpandedIcon( icon );
1953 }
1954
1955 const wxIcon &wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem& item ) const
1956 {
1957 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1958 if (!node) return wxNullIcon;
1959
1960 return node->GetExpandedIcon();
1961 }
1962
1963 void wxDataViewTreeStore::SetItemData( const wxDataViewItem& item, wxClientData *data )
1964 {
1965 wxDataViewTreeStoreNode *node = FindNode( item );
1966 if (!node) return;
1967
1968 node->SetData( data );
1969 }
1970
1971 wxClientData *wxDataViewTreeStore::GetItemData( const wxDataViewItem& item ) const
1972 {
1973 wxDataViewTreeStoreNode *node = FindNode( item );
1974 if (!node) return NULL;
1975
1976 return node->GetData();
1977 }
1978
1979 void wxDataViewTreeStore::DeleteItem( const wxDataViewItem& item )
1980 {
1981 if (!item.IsOk()) return;
1982
1983 wxDataViewItem parent_item = GetParent( item );
1984
1985 wxDataViewTreeStoreContainerNode *parent_node = FindContainerNode( parent_item );
1986 if (!parent_node) return;
1987
1988 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1989 if (!node) return;
1990
1991 parent_node->GetChildren().DeleteObject( node );
1992 }
1993
1994 void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem& item )
1995 {
1996 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
1997 if (!node) return;
1998
1999 node->GetChildren().clear();
2000 }
2001
2002 void wxDataViewTreeStore::DeleteAllItems()
2003 {
2004 DeleteChildren(m_root);
2005 }
2006
2007 void
2008 wxDataViewTreeStore::GetValue(wxVariant &variant,
2009 const wxDataViewItem &item,
2010 unsigned int WXUNUSED(col)) const
2011 {
2012 // if (col != 0) return;
2013
2014 wxDataViewTreeStoreNode *node = FindNode( item );
2015 if (!node) return;
2016
2017 wxIcon icon( node->GetIcon());
2018 if (node->IsContainer())
2019 {
2020 wxDataViewTreeStoreContainerNode *container = (wxDataViewTreeStoreContainerNode*) node;
2021 if (container->IsExpanded() && container->GetExpandedIcon().IsOk())
2022 icon = container->GetExpandedIcon();
2023 }
2024
2025 wxDataViewIconText data( node->GetText(), icon );
2026
2027 variant << data;
2028 }
2029
2030 bool
2031 wxDataViewTreeStore::SetValue(const wxVariant& variant,
2032 const wxDataViewItem& item,
2033 unsigned int WXUNUSED(col))
2034 {
2035 // if (col != 0) return false;
2036
2037 wxDataViewTreeStoreNode *node = FindNode( item );
2038 if (!node) return false;
2039
2040 wxDataViewIconText data;
2041
2042 data << variant;
2043
2044 node->SetText( data.GetText() );
2045 node->SetIcon( data.GetIcon() );
2046
2047 return true;
2048 }
2049
2050 wxDataViewItem wxDataViewTreeStore::GetParent( const wxDataViewItem &item ) const
2051 {
2052 wxDataViewTreeStoreNode *node = FindNode( item );
2053 if (!node) return wxDataViewItem(0);
2054
2055 wxDataViewTreeStoreNode *parent = node->GetParent();
2056 if (!parent) return wxDataViewItem(0);
2057
2058 if (parent == m_root)
2059 return wxDataViewItem(0);
2060
2061 return parent->GetItem();
2062 }
2063
2064 unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const
2065 {
2066 wxDataViewTreeStoreContainerNode *node = FindContainerNode( item );
2067 if (!node) return 0;
2068
2069 wxDataViewTreeStoreNodeList::iterator iter;
2070 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
2071 {
2072 wxDataViewTreeStoreNode* child = *iter;
2073 children.Add( child->GetItem() );
2074 }
2075
2076 return node->GetChildren().GetCount();
2077 }
2078
2079 int wxDataViewTreeStore::Compare( const wxDataViewItem &item1, const wxDataViewItem &item2,
2080 unsigned int WXUNUSED(column), bool WXUNUSED(ascending) ) const
2081 {
2082 wxDataViewTreeStoreNode *node1 = FindNode( item1 );
2083 wxDataViewTreeStoreNode *node2 = FindNode( item2 );
2084
2085 if (!node1 || !node2)
2086 return 0;
2087
2088 wxDataViewTreeStoreContainerNode* parent1 =
2089 (wxDataViewTreeStoreContainerNode*) node1->GetParent();
2090 wxDataViewTreeStoreContainerNode* parent2 =
2091 (wxDataViewTreeStoreContainerNode*) node2->GetParent();
2092
2093 if (parent1 != parent2)
2094 {
2095 wxLogError( wxT("Comparing items with different parent.") );
2096 return 0;
2097 }
2098
2099 if (node1->IsContainer() && !node2->IsContainer())
2100 return -1;
2101
2102 if (node2->IsContainer() && !node1->IsContainer())
2103 return 1;
2104
2105 return parent1->GetChildren().IndexOf( node1 ) - parent2->GetChildren().IndexOf( node2 );
2106 }
2107
2108 wxDataViewTreeStoreNode *wxDataViewTreeStore::FindNode( const wxDataViewItem &item ) const
2109 {
2110 if (!item.IsOk())
2111 return m_root;
2112
2113 return (wxDataViewTreeStoreNode*) item.GetID();
2114 }
2115
2116 wxDataViewTreeStoreContainerNode *wxDataViewTreeStore::FindContainerNode( const wxDataViewItem &item ) const
2117 {
2118 if (!item.IsOk())
2119 return (wxDataViewTreeStoreContainerNode*) m_root;
2120
2121 wxDataViewTreeStoreNode* node = (wxDataViewTreeStoreNode*) item.GetID();
2122
2123 if (!node->IsContainer())
2124 return NULL;
2125
2126 return (wxDataViewTreeStoreContainerNode*) node;
2127 }
2128
2129 //-----------------------------------------------------------------------------
2130 // wxDataViewTreeCtrl
2131 //-----------------------------------------------------------------------------
2132
2133 IMPLEMENT_DYNAMIC_CLASS(wxDataViewTreeCtrl,wxDataViewCtrl)
2134
2135 BEGIN_EVENT_TABLE(wxDataViewTreeCtrl,wxDataViewCtrl)
2136 EVT_DATAVIEW_ITEM_EXPANDED(-1, wxDataViewTreeCtrl::OnExpanded)
2137 EVT_DATAVIEW_ITEM_COLLAPSED(-1, wxDataViewTreeCtrl::OnCollapsed)
2138 EVT_SIZE( wxDataViewTreeCtrl::OnSize )
2139 END_EVENT_TABLE()
2140
2141 wxDataViewTreeCtrl::~wxDataViewTreeCtrl()
2142 {
2143 delete m_imageList;
2144 }
2145
2146 bool wxDataViewTreeCtrl::Create( wxWindow *parent, wxWindowID id,
2147 const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
2148 {
2149 if ( !wxDataViewCtrl::Create( parent, id, pos, size, style, validator ) )
2150 return false;
2151
2152 // create the standard model and a column in the tree
2153 wxDataViewTreeStore *store = new wxDataViewTreeStore;
2154 AssociateModel( store );
2155 store->DecRef();
2156
2157 AppendIconTextColumn
2158 (
2159 wxString(), // no label (header is not shown anyhow)
2160 0, // the only model column
2161 wxDATAVIEW_CELL_EDITABLE,
2162 -1, // default width
2163 wxALIGN_NOT, // and alignment
2164 0 // not resizeable
2165 );
2166
2167 return true;
2168 }
2169
2170 void wxDataViewTreeCtrl::SetImageList( wxImageList *imagelist )
2171 {
2172 delete m_imageList;
2173
2174 m_imageList = imagelist;
2175 }
2176
2177 wxDataViewItem wxDataViewTreeCtrl::AppendItem( const wxDataViewItem& parent,
2178 const wxString &text, int iconIndex, wxClientData *data )
2179 {
2180 wxIcon icon = wxNullIcon;
2181 if (m_imageList && (iconIndex != -1))
2182 icon = m_imageList->GetIcon( iconIndex );
2183
2184 wxDataViewItem res = GetStore()->AppendItem( parent, text, icon, data );
2185
2186 GetStore()->ItemAdded( parent, res );
2187
2188 return res;
2189 }
2190
2191 wxDataViewItem wxDataViewTreeCtrl::PrependItem( const wxDataViewItem& parent,
2192 const wxString &text, int iconIndex, wxClientData *data )
2193 {
2194 wxIcon icon = wxNullIcon;
2195 if (m_imageList && (iconIndex != -1))
2196 icon = m_imageList->GetIcon( iconIndex );
2197
2198 wxDataViewItem res = GetStore()->PrependItem( parent, text, icon, data );
2199
2200 GetStore()->ItemAdded( parent, res );
2201
2202 return res;
2203 }
2204
2205 wxDataViewItem wxDataViewTreeCtrl::InsertItem( const wxDataViewItem& parent, const wxDataViewItem& previous,
2206 const wxString &text, int iconIndex, wxClientData *data )
2207 {
2208 wxIcon icon = wxNullIcon;
2209 if (m_imageList && (iconIndex != -1))
2210 icon = m_imageList->GetIcon( iconIndex );
2211
2212 wxDataViewItem res = GetStore()->InsertItem( parent, previous, text, icon, data );
2213
2214 GetStore()->ItemAdded( parent, res );
2215
2216 return res;
2217 }
2218
2219 wxDataViewItem wxDataViewTreeCtrl::PrependContainer( const wxDataViewItem& parent,
2220 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2221 {
2222 wxIcon icon = wxNullIcon;
2223 if (m_imageList && (iconIndex != -1))
2224 icon = m_imageList->GetIcon( iconIndex );
2225
2226 wxIcon expanded = wxNullIcon;
2227 if (m_imageList && (expandedIndex != -1))
2228 expanded = m_imageList->GetIcon( expandedIndex );
2229
2230 wxDataViewItem res = GetStore()->PrependContainer( parent, text, icon, expanded, data );
2231
2232 GetStore()->ItemAdded( parent, res );
2233
2234 return res;
2235 }
2236
2237 wxDataViewItem wxDataViewTreeCtrl::AppendContainer( const wxDataViewItem& parent,
2238 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2239 {
2240 wxIcon icon = wxNullIcon;
2241 if (m_imageList && (iconIndex != -1))
2242 icon = m_imageList->GetIcon( iconIndex );
2243
2244 wxIcon expanded = wxNullIcon;
2245 if (m_imageList && (expandedIndex != -1))
2246 expanded = m_imageList->GetIcon( expandedIndex );
2247
2248 wxDataViewItem res = GetStore()->AppendContainer( parent, text, icon, expanded, data );
2249
2250 GetStore()->ItemAdded( parent, res );
2251
2252 return res;
2253 }
2254
2255 wxDataViewItem wxDataViewTreeCtrl::InsertContainer( const wxDataViewItem& parent, const wxDataViewItem& previous,
2256 const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
2257 {
2258 wxIcon icon = wxNullIcon;
2259 if (m_imageList && (iconIndex != -1))
2260 icon = m_imageList->GetIcon( iconIndex );
2261
2262 wxIcon expanded = wxNullIcon;
2263 if (m_imageList && (expandedIndex != -1))
2264 expanded = m_imageList->GetIcon( expandedIndex );
2265
2266 wxDataViewItem res = GetStore()->InsertContainer( parent, previous, text, icon, expanded, data );
2267
2268 GetStore()->ItemAdded( parent, res );
2269
2270 return res;
2271 }
2272
2273 void wxDataViewTreeCtrl::SetItemText( const wxDataViewItem& item, const wxString &text )
2274 {
2275 GetStore()->SetItemText(item,text);
2276
2277 // notify control
2278 GetStore()->ValueChanged( item, 0 );
2279 }
2280
2281 void wxDataViewTreeCtrl::SetItemIcon( const wxDataViewItem& item, const wxIcon &icon )
2282 {
2283 GetStore()->SetItemIcon(item,icon);
2284
2285 // notify control
2286 GetStore()->ValueChanged( item, 0 );
2287 }
2288
2289 void wxDataViewTreeCtrl::SetItemExpandedIcon( const wxDataViewItem& item, const wxIcon &icon )
2290 {
2291 GetStore()->SetItemExpandedIcon(item,icon);
2292
2293 // notify control
2294 GetStore()->ValueChanged( item, 0 );
2295 }
2296
2297 void wxDataViewTreeCtrl::DeleteItem( const wxDataViewItem& item )
2298 {
2299 wxDataViewItem parent_item = GetStore()->GetParent( item );
2300
2301 GetStore()->DeleteItem(item);
2302
2303 // notify control
2304 GetStore()->ItemDeleted( parent_item, item );
2305 }
2306
2307 void wxDataViewTreeCtrl::DeleteChildren( const wxDataViewItem& item )
2308 {
2309 wxDataViewTreeStoreContainerNode *node = GetStore()->FindContainerNode( item );
2310 if (!node) return;
2311
2312 wxDataViewItemArray array;
2313 wxDataViewTreeStoreNodeList::iterator iter;
2314 for (iter = node->GetChildren().begin(); iter != node->GetChildren().end(); iter++)
2315 {
2316 wxDataViewTreeStoreNode* child = *iter;
2317 array.Add( child->GetItem() );
2318 }
2319
2320 GetStore()->DeleteChildren( item );
2321
2322 // notify control
2323 GetStore()->ItemsDeleted( item, array );
2324 }
2325
2326 void wxDataViewTreeCtrl::DeleteAllItems()
2327 {
2328 GetStore()->DeleteAllItems();
2329
2330 GetStore()->Cleared();
2331 }
2332
2333 void wxDataViewTreeCtrl::OnExpanded( wxDataViewEvent &event )
2334 {
2335 if (m_imageList) return;
2336
2337 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
2338 if (!container) return;
2339
2340 container->SetExpanded( true );
2341
2342 GetStore()->ItemChanged( event.GetItem() );
2343 }
2344
2345 void wxDataViewTreeCtrl::OnCollapsed( wxDataViewEvent &event )
2346 {
2347 if (m_imageList) return;
2348
2349 wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
2350 if (!container) return;
2351
2352 container->SetExpanded( false );
2353
2354 GetStore()->ItemChanged( event.GetItem() );
2355 }
2356
2357 void wxDataViewTreeCtrl::OnSize( wxSizeEvent &event )
2358 {
2359 #if defined(wxUSE_GENERICDATAVIEWCTRL)
2360 // automatically resize our only column to take the entire control width
2361 if ( GetColumnCount() )
2362 {
2363 wxSize size = GetClientSize();
2364 GetColumn(0)->SetWidth(size.x);
2365 }
2366 #endif
2367 event.Skip( true );
2368 }
2369
2370 #endif // wxUSE_DATAVIEWCTRL
2371