1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/dataview_osx.cpp
3 // Purpose: wxDataViewCtrl native mac implementation
5 // Id: $Id: dataview_osx.cpp 58317 2009-01-27
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #if (wxUSE_DATAVIEWCTRL != 0) && (!defined(wxUSE_GENERICDATAVIEWCTRL) || (wxUSE_GENERICDATAVIEWCTRL == 0))
19 #include "wx/settings.h"
20 #include "wx/dcclient.h"
24 #include "wx/osx/core/dataview.h"
25 #include "wx/osx/private.h"
26 #include "wx/renderer.h"
28 // ============================================================================
29 // Helper functions for dataviewe implementation on OSX
30 // ============================================================================
31 wxString
ConcatenateDataViewItemValues(wxDataViewCtrl
const* dataViewCtrlPtr
, wxDataViewItem
const& dataViewItem
)
33 wxString dataString
; // contains the TAB concatenated data
36 for (size_t i
=0; i
<dataViewCtrlPtr
->GetColumnCount(); i
++)
38 // variable definition:
41 dataViewCtrlPtr
->GetModel()->GetValue(dataValue
,dataViewItem
,dataViewCtrlPtr
->GetColumn(i
)->GetModelColumn());
43 dataString
<< wxT('\t');
44 dataString
<< dataValue
.MakeString();
49 // ============================================================================
50 // wxOSXDataViewModelNotifier
51 // ============================================================================
52 class wxOSXDataViewModelNotifier
: public wxDataViewModelNotifier
56 // constructors / destructor
58 wxOSXDataViewModelNotifier(wxDataViewCtrl
* initDataViewCtrlPtr
);
61 // inherited methods from wxDataViewModelNotifier
63 virtual bool ItemAdded (wxDataViewItem
const &parent
, wxDataViewItem
const &item
);
64 virtual bool ItemsAdded (wxDataViewItem
const& parent
, wxDataViewItemArray
const& items
);
65 virtual bool ItemChanged (wxDataViewItem
const& item
);
66 virtual bool ItemsChanged(wxDataViewItemArray
const& items
);
67 virtual bool ItemDeleted (wxDataViewItem
const& parent
, wxDataViewItem
const& item
);
68 virtual bool ItemsDeleted(wxDataViewItem
const& parent
, wxDataViewItemArray
const& items
);
69 virtual bool ValueChanged(wxDataViewItem
const& item
, unsigned int col
);
70 virtual bool Cleared();
71 virtual void Resort();
74 // if the dataview control can have a variable row height this method sets the dataview's control row height of
75 // the passed item to the maximum value occupied by the item in all columns
76 void AdjustRowHeight(wxDataViewItem
const& item
);
77 // ... and the same method for a couple of items:
78 void AdjustRowHeights(wxDataViewItemArray
const& items
);
81 wxDataViewCtrl
* m_DataViewCtrlPtr
;
85 // constructors / destructor
87 wxOSXDataViewModelNotifier::wxOSXDataViewModelNotifier(wxDataViewCtrl
* initDataViewCtrlPtr
)
88 :m_DataViewCtrlPtr(initDataViewCtrlPtr
)
90 if (initDataViewCtrlPtr
== NULL
)
91 wxFAIL_MSG(_("Pointer to dataview control must not be NULL"));
94 bool wxOSXDataViewModelNotifier::ItemAdded(wxDataViewItem
const& parent
, wxDataViewItem
const& item
)
99 wxCHECK_MSG(item
.IsOk(),false,_("Added item is invalid."));
100 noFailureFlag
= m_DataViewCtrlPtr
->GetDataViewPeer()->Add(parent
,item
);
101 AdjustRowHeight(item
);
102 return noFailureFlag
;
105 bool wxOSXDataViewModelNotifier::ItemsAdded(wxDataViewItem
const& parent
, wxDataViewItemArray
const& items
)
110 // insert all valid items into control:
111 noFailureFlag
= m_DataViewCtrlPtr
->GetDataViewPeer()->Add(parent
,items
);
112 // adjust row heights:
113 AdjustRowHeights(items
);
115 return noFailureFlag
;
118 bool wxOSXDataViewModelNotifier::ItemChanged(wxDataViewItem
const& item
)
120 wxCHECK_MSG(item
.IsOk(), false,_("Changed item is invalid."));
121 wxCHECK_MSG(GetOwner() != NULL
,false,_("Owner not initialized."));
122 if (m_DataViewCtrlPtr
->GetDataViewPeer()->Update(GetOwner()->GetParent(item
),item
))
124 // sent the equivalent wxWidget event:
125 wxDataViewEvent
dataViewEvent(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
,m_DataViewCtrlPtr
->GetId());
127 dataViewEvent
.SetEventObject(m_DataViewCtrlPtr
);
128 dataViewEvent
.SetItem(item
);
129 // sent the equivalent wxWidget event:
130 m_DataViewCtrlPtr
->HandleWindowEvent(dataViewEvent
);
131 // row height may have to be adjusted:
132 AdjustRowHeight(item
);
140 bool wxOSXDataViewModelNotifier::ItemsChanged(wxDataViewItemArray
const& items
)
142 size_t const noOfItems
= items
.GetCount();
144 wxDataViewEvent
dataViewEvent(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
,m_DataViewCtrlPtr
->GetId());
147 dataViewEvent
.SetEventObject(m_DataViewCtrlPtr
);
148 for (size_t indexItem
=0; indexItem
<noOfItems
; ++indexItem
)
149 if (m_DataViewCtrlPtr
->GetDataViewPeer()->Update(GetOwner()->GetParent(items
[indexItem
]),items
[indexItem
]))
151 // send for all changed items a wxWidget event:
152 dataViewEvent
.SetItem(items
[indexItem
]);
153 m_DataViewCtrlPtr
->HandleWindowEvent(dataViewEvent
);
157 // if this location is reached all items have been updated:
158 AdjustRowHeights(items
);
163 bool wxOSXDataViewModelNotifier::ItemDeleted(wxDataViewItem
const& parent
, wxDataViewItem
const& item
)
168 wxCHECK_MSG(item
.IsOk(),false,_("To be deleted item is invalid."));
169 // when this method is called and currently an item is being edited this item may have already been deleted in the model (the passed item and the being edited item have
170 // not to be identical because the being edited item might be below the passed item in the hierarchy);
171 // to prevent the control trying to ask the model to update an already deleted item the control is informed that currently a deleting process
172 // has been started and that variables can currently not be updated even when requested by the system:
173 m_DataViewCtrlPtr
->SetDeleting(true);
174 noFailureFlag
= m_DataViewCtrlPtr
->GetDataViewPeer()->Remove(parent
,item
);
175 // enable automatic updating again:
176 m_DataViewCtrlPtr
->SetDeleting(false);
178 return noFailureFlag
;
181 bool wxOSXDataViewModelNotifier::ItemsDeleted(wxDataViewItem
const& parent
, wxDataViewItemArray
const& items
)
186 // when this method is called and currently an item is being edited this item may have already been deleted in the model (the passed item and the being edited item have
187 // not to be identical because the being edited item might be below the passed item in the hierarchy);
188 // to prevent the control trying to ask the model to update an already deleted item the control is informed that currently a deleting process
189 // has been started and that variables can currently not be updated even when requested by the system:
190 m_DataViewCtrlPtr
->SetDeleting(true);
191 // delete all specified items:
192 noFailureFlag
= m_DataViewCtrlPtr
->GetDataViewPeer()->Remove(parent
,items
);
193 // enable automatic updating again:
194 m_DataViewCtrlPtr
->SetDeleting(false);
196 return noFailureFlag
;
199 bool wxOSXDataViewModelNotifier::ValueChanged(wxDataViewItem
const& item
, unsigned int col
)
201 wxCHECK_MSG(item
.IsOk(), false,_("Passed item is invalid."));
202 wxCHECK_MSG(GetOwner() != NULL
,false,_("Owner not initialized."));
203 if (m_DataViewCtrlPtr
->GetDataViewPeer()->Update(GetOwner()->GetParent(item
),item
))
205 wxDataViewEvent
dataViewEvent(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
,m_DataViewCtrlPtr
->GetId());
207 dataViewEvent
.SetEventObject(m_DataViewCtrlPtr
);
208 dataViewEvent
.SetColumn(col
);
209 dataViewEvent
.SetItem(item
);
210 // send the equivalent wxWidget event:
211 m_DataViewCtrlPtr
->HandleWindowEvent(dataViewEvent
);
219 bool wxOSXDataViewModelNotifier::Cleared()
221 return m_DataViewCtrlPtr
->GetDataViewPeer()->Reload();
224 void wxOSXDataViewModelNotifier::Resort()
226 m_DataViewCtrlPtr
->GetDataViewPeer()->Resort();
229 void wxOSXDataViewModelNotifier::AdjustRowHeight(wxDataViewItem
const& item
)
231 if ((m_DataViewCtrlPtr
->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
) != 0)
233 wxDataViewModel
*model
= GetOwner();
235 int height
= 20; // TODO find out standard height
236 unsigned int num
= m_DataViewCtrlPtr
->GetColumnCount();
238 for (col
= 0; col
< num
; col
++)
240 wxDataViewColumn
* column(m_DataViewCtrlPtr
->GetColumnPtr(col
));
242 if (!(column
->IsHidden()))
244 wxDataViewCustomRenderer
*renderer
= dynamic_cast<wxDataViewCustomRenderer
*>(column
->GetRenderer());
248 model
->GetValue( value
, item
, column
->GetModelColumn() );
249 renderer
->SetValue( value
);
250 height
= wxMax( height
, renderer
->GetSize().y
);
255 m_DataViewCtrlPtr
->GetDataViewPeer()->SetRowHeight(item
,height
);
259 void wxOSXDataViewModelNotifier::AdjustRowHeights(wxDataViewItemArray
const& items
)
261 if ((m_DataViewCtrlPtr
->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
) != 0)
263 size_t const noOfItems
= items
.GetCount();
265 wxDataViewModel
*model
= GetOwner();
267 for (size_t itemIndex
=0; itemIndex
<noOfItems
; ++itemIndex
)
269 int height
= 20; // TODO find out standard height
270 unsigned int num
= m_DataViewCtrlPtr
->GetColumnCount();
273 for (col
= 0; col
< num
; col
++)
275 wxDataViewColumn
* column(m_DataViewCtrlPtr
->GetColumnPtr(col
));
277 if (!(column
->IsHidden()))
279 wxDataViewCustomRenderer
*renderer
= dynamic_cast<wxDataViewCustomRenderer
*>(column
->GetRenderer());
283 model
->GetValue( value
, items
[itemIndex
], column
->GetModelColumn() );
284 renderer
->SetValue( value
);
285 height
= wxMax( height
, renderer
->GetSize().y
);
290 m_DataViewCtrlPtr
->GetDataViewPeer()->SetRowHeight(items
[itemIndex
],height
);
295 // ---------------------------------------------------------
296 // wxDataViewCustomRenderer
297 // The constructor, the implementation macro and environment
298 // dependent methods can be found in the environment's
300 // ---------------------------------------------------------
301 wxDataViewCustomRenderer::~wxDataViewCustomRenderer()
306 wxDC
* wxDataViewCustomRenderer::GetDC()
308 if ((m_DCPtr
== NULL
) && (GetOwner() != NULL
) && (GetOwner()->GetOwner() != NULL
))
309 m_DCPtr
= new wxClientDC(GetOwner()->GetOwner());
313 void wxDataViewCustomRenderer::SetDC(wxDC
* newDCPtr
)
319 //-----------------------------------------------------------------------------
321 //-----------------------------------------------------------------------------
323 wxDataViewCtrl::~wxDataViewCtrl()
328 void wxDataViewCtrl::Init()
330 m_CustomRendererPtr
= NULL
;
332 m_macIsUserPane
= false;
336 bool wxDataViewCtrl::Create(wxWindow
*parent
,
341 const wxValidator
& validator
,
342 const wxString
& name
)
344 if (!(wxControl::Create(parent
,id
,pos
,size
,style
,validator
,name
)))
346 m_peer
= ::CreateDataView(this,parent
,id
,pos
,size
,style
,GetExtraStyle());
348 MacPostControlCreate(pos
,size
);
353 bool wxDataViewCtrl::AssociateModel(wxDataViewModel
* model
)
355 wxDataViewWidgetImpl
* dataViewWidgetPtr(GetDataViewPeer());
358 wxCHECK_MSG(dataViewWidgetPtr
!= NULL
,false,_("Pointer to native control must not be NULL."));
359 if (wxDataViewCtrlBase::AssociateModel(model
) && dataViewWidgetPtr
->AssociateModel(model
))
362 model
->AddNotifier(new wxOSXDataViewModelNotifier(this));
369 bool wxDataViewCtrl::AppendColumn(wxDataViewColumn
* columnPtr
)
371 return wxDataViewCtrl::InsertColumn( GetColumnCount(), columnPtr
);
374 bool wxDataViewCtrl::PrependColumn(wxDataViewColumn
* columnPtr
)
376 return wxDataViewCtrl::InsertColumn( 0, columnPtr
);
379 bool wxDataViewCtrl::InsertColumn(unsigned int pos
, wxDataViewColumn
* columnPtr
)
381 wxDataViewWidgetImpl
* dataViewWidgetPtr(GetDataViewPeer());
383 // first, some error checking:
384 wxCHECK_MSG(dataViewWidgetPtr
!= NULL
, false,_("Pointer to native control must not be NULL."));
385 wxCHECK_MSG(columnPtr
!= NULL
, false,_("Column pointer must not be NULL."));
386 wxCHECK_MSG(columnPtr
->GetRenderer() != NULL
, false,_("Column does not have a renderer."));
387 wxCHECK_MSG(GetModel() != NULL
, false,_("No model associated with control."));
388 wxCHECK_MSG((columnPtr
->GetModelColumn() >= 0) &&
389 (columnPtr
->GetModelColumn() < GetModel()->GetColumnCount()),false,_("Column's model column has no equivalent in the associated model."));
391 // add column to wxWidget's internal structure:
392 if (wxDataViewCtrlBase::InsertColumn(pos
,columnPtr
))
394 m_ColumnPtrs
.Add(columnPtr
);
395 // if the insertion in the native control is successful the rest can also be initialized:
396 if (dataViewWidgetPtr
->InsertColumn(pos
,columnPtr
))
398 // make sure that the data is up-to-date...
399 // if the newly appended column is the first column add the initial data to the control and mark the column as an expander column,
400 // otherwise ask the control to 'update' the data in the newly appended column:
401 if (GetColumnCount() == 1)
402 SetExpanderColumn(columnPtr
);
409 m_ColumnPtrs
.Remove(columnPtr
);
411 // and send a message in debug mode:
412 wxFAIL_MSG(_("Column could not be added to native control."));
421 wxFAIL_MSG(_("Could not add column to internal structures."));
427 bool wxDataViewCtrl::ClearColumns()
429 if (GetDataViewPeer()->ClearColumns())
431 WX_CLEAR_ARRAY(m_ColumnPtrs
);
438 bool wxDataViewCtrl::DeleteColumn(wxDataViewColumn
* columnPtr
)
440 if (GetDataViewPeer()->DeleteColumn(columnPtr
))
442 m_ColumnPtrs
.Remove(columnPtr
);
450 wxDataViewColumn
* wxDataViewCtrl::GetColumn(unsigned int pos
) const
452 return GetDataViewPeer()->GetColumn(pos
);
455 unsigned int wxDataViewCtrl::GetColumnCount() const
457 return m_ColumnPtrs
.GetCount();
460 int wxDataViewCtrl::GetColumnPosition(wxDataViewColumn
const* columnPtr
) const
462 return GetDataViewPeer()->GetColumnPosition(columnPtr
);
465 void wxDataViewCtrl::Collapse(wxDataViewItem
const& item
)
467 GetDataViewPeer()->Collapse(item
);
470 void wxDataViewCtrl::EnsureVisible(wxDataViewItem
const& item
, wxDataViewColumn
const* columnPtr
)
474 ExpandAncestors(item
); // make sure that the item exists in the control
475 GetDataViewPeer()->EnsureVisible(item
,columnPtr
);
479 void wxDataViewCtrl::Expand(wxDataViewItem
const& item
)
481 return GetDataViewPeer()->Expand(item
);
484 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
486 return (item
.IsOk() && GetDataViewPeer()->IsExpanded(item
));
489 wxDataViewColumn
* wxDataViewCtrl::GetSortingColumn() const
491 return GetDataViewPeer()->GetSortingColumn();
494 unsigned int wxDataViewCtrl::GetCount() const
496 return GetDataViewPeer()->GetCount();
499 wxRect
wxDataViewCtrl::GetItemRect(wxDataViewItem
const& item
, wxDataViewColumn
const* columnPtr
) const
501 if (item
.IsOk() && (columnPtr
!= NULL
))
502 return GetDataViewPeer()->GetRectangle(item
,columnPtr
);
507 wxDataViewItem
wxDataViewCtrl::GetSelection() const
509 wxDataViewItemArray itemIDs
;
512 if (GetDataViewPeer()->GetSelections(itemIDs
) > 0)
515 return wxDataViewItem();
518 int wxDataViewCtrl::GetSelections(wxDataViewItemArray
& sel
) const
520 return GetDataViewPeer()->GetSelections(sel
);
523 void wxDataViewCtrl::HitTest(wxPoint
const& point
, wxDataViewItem
& item
, wxDataViewColumn
*& columnPtr
) const
525 return GetDataViewPeer()->HitTest(point
,item
,columnPtr
);
528 bool wxDataViewCtrl::IsSelected(wxDataViewItem
const& item
) const
530 return GetDataViewPeer()->IsSelected(item
);
533 void wxDataViewCtrl::Select(wxDataViewItem
const& item
)
537 ExpandAncestors(item
); // make sure that the item exists in the control
538 GetDataViewPeer()->Select(item
);
542 void wxDataViewCtrl::SelectAll()
544 GetDataViewPeer()->SelectAll();
547 void wxDataViewCtrl::SetSelections(wxDataViewItemArray
const& sel
)
549 size_t const noOfSelections
= sel
.GetCount();
553 wxDataViewItem last_parent
;
556 // make sure that all to be selected items are visible in the control:
557 for (i
= 0; i
< noOfSelections
; i
++)
559 wxDataViewItem item
= sel
[i
];
560 wxDataViewItem parent
= GetModel()->GetParent( item
);
562 if (parent
.IsOk() && (parent
!= last_parent
))
563 ExpandAncestors(item
);
564 last_parent
= parent
;
567 // finally select the items:
568 wxDataViewWidgetImpl
* dataViewWidgetPtr(GetDataViewPeer()); // variable definition for abbreviational purposes
570 for (i
=0; i
<noOfSelections
; ++i
)
571 dataViewWidgetPtr
->Select(sel
[i
]);
574 void wxDataViewCtrl::Unselect(wxDataViewItem
const& item
)
577 GetDataViewPeer()->Unselect(item
);
580 void wxDataViewCtrl::UnselectAll()
582 GetDataViewPeer()->UnselectAll();
588 wxDataViewWidgetImpl
* wxDataViewCtrl::GetDataViewPeer() const
590 return dynamic_cast<wxDataViewWidgetImpl
*>(GetPeer());
593 void wxDataViewCtrl::AddChildren(wxDataViewItem
const& parentItem
)
597 wxDataViewItemArray items
;
600 wxCHECK_RET(GetModel() != NULL
,_("Model pointer not initialized."));
601 noOfChildren
= GetModel()->GetChildren(parentItem
,items
);
602 (void) GetModel()->ItemsAdded(parentItem
,items
);
605 void wxDataViewCtrl::FinishCustomItemEditing()
607 if (GetCustomRendererItem().IsOk())
609 GetCustomRendererPtr()->FinishEditing();
610 SetCustomRendererItem(wxDataViewItem());
611 SetCustomRendererPtr (NULL
);
617 wxDataViewCtrl::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
619 wxVisualAttributes attr
;
621 attr
.colFg
= wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT
);
622 attr
.colBg
= wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
);
623 static wxFont font
= wxFont(wxOSX_SYSTEM_FONT_VIEWS
);
629 // inherited methods from wxDataViewCtrlBase
630 void wxDataViewCtrl::DoSetExpanderColumn()
632 if (GetExpanderColumn() != NULL
)
633 GetDataViewPeer()->DoSetExpanderColumn(GetExpanderColumn());
636 void wxDataViewCtrl::DoSetIndent()
638 GetDataViewPeer()->DoSetIndent(GetIndent());
642 void wxDataViewCtrl::OnSize(wxSizeEvent
& event
)
644 unsigned int const noOfColumns
= GetColumnCount();
647 // reset DC of all custom renderers because DC has changed:
648 for (unsigned int i
=0; i
<noOfColumns
; ++i
)
650 wxDataViewColumn
* dataViewColumnPtr(GetColumn(i
));
652 if (dataViewColumnPtr
!= NULL
)
654 wxDataViewCustomRenderer
* dataViewCustomRendererPtr(dynamic_cast<wxDataViewCustomRenderer
*>(dataViewColumnPtr
->GetRenderer()));
656 if (dataViewCustomRendererPtr
!= NULL
)
657 dataViewCustomRendererPtr
->SetDC(NULL
);
661 // update the layout of the native control after a size event:
662 GetDataViewPeer()->OnSize();
667 wxSize
wxDataViewCtrl::DoGetBestSize() const
669 wxSize best
= wxControl::DoGetBestSize();
675 void wxDataViewCtrl::OnMouse(wxMouseEvent
& event
)
679 if (GetModel() == NULL
)
683 // Doesn't compile anymore
684 wxMacDataViewDataBrowserListViewControlPointer
MacDataViewListCtrlPtr(dynamic_cast<wxMacDataViewDataBrowserListViewControlPointer
>(m_peer
));
687 wxDataViewItemArray items
;
688 NoOfChildren
= GetModel()->GetChildren( wxDataViewItem(), items
);
689 if (NoOfChildren
== 0)
691 wxDataViewItem firstChild
= items
[0];
693 UInt16 headerHeight
= 0;
694 MacDataViewListCtrlPtr
->GetHeaderButtonHeight(&headerHeight
);
697 if (event
.GetY() < headerHeight
)
699 unsigned int col_count
= GetColumnCount();
701 for (col
= 0; col
< col_count
; col
++)
703 wxDataViewColumn
*column
= GetColumn( col
);
704 if (column
->IsHidden())
708 ::GetDataBrowserItemPartBounds( MacDataViewListCtrlPtr
->GetControlRef(),
709 reinterpret_cast<DataBrowserItemID
>(firstChild
.GetID()), column
->GetPropertyID(),
710 kDataBrowserPropertyEnclosingPart
, &itemrect
);
712 if (abs( event
.GetX() - itemrect
.right
) < 3)
714 if (column
->GetFlags() & wxDATAVIEW_COL_RESIZABLE
)
715 SetCursor( wxCursor( wxCURSOR_SIZEWE
) );
717 SetCursor( *wxSTANDARD_CURSOR
);
724 SetCursor( *wxSTANDARD_CURSOR
);
728 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
,wxDataViewCtrlBase
)
730 BEGIN_EVENT_TABLE(wxDataViewCtrl
,wxDataViewCtrlBase
)
731 EVT_SIZE(wxDataViewCtrl::OnSize
)
732 EVT_MOTION(wxDataViewCtrl::OnMouse
)
735 #endif // (wxUSE_DATAVIEWCTRL != 0) && (!defined(wxUSE_GENERICDATAVIEWCTRL) || (wxUSE_GENERICDATAVIEWCTRL == 0))