Merge branch 'mac-dvc-autosize'

Fixes for wxDVC columns autosizing under Mac.

See https://github.com/wxWidgets/wxWidgets/pull/2305
This commit is contained in:
Vadim Zeitlin
2021-04-08 01:39:21 +02:00
3 changed files with 55 additions and 8 deletions

View File

@@ -86,7 +86,7 @@ class wxCocoaDataViewControl;
class wxDataViewColumnNativeData class wxDataViewColumnNativeData
{ {
public: public:
wxDataViewColumnNativeData() : m_NativeColumnPtr(NULL) wxDataViewColumnNativeData() : m_NativeColumnPtr(NULL), m_isLast(false), m_prevWidth(0)
{ {
} }
@@ -105,9 +105,32 @@ public:
m_NativeColumnPtr = newNativeColumnPtr; m_NativeColumnPtr = newNativeColumnPtr;
} }
bool GetIsLast() const
{
return m_isLast;
}
void SetIsLast(bool isLast)
{
m_isLast = isLast;
}
int GetPrevWidth() const
{
return m_prevWidth;
}
void SetPrevWidth(int prevWidth)
{
m_prevWidth = prevWidth;
}
private: private:
// not owned by us // not owned by us
NSTableColumn* m_NativeColumnPtr; NSTableColumn* m_NativeColumnPtr;
bool m_isLast;
int m_prevWidth;
}; };
// ============================================================================ // ============================================================================

View File

@@ -2206,7 +2206,8 @@ bool wxCocoaDataViewControl::InsertColumn(unsigned int pos, wxDataViewColumn* co
void wxCocoaDataViewControl::FitColumnWidthToContent(unsigned int pos) void wxCocoaDataViewControl::FitColumnWidthToContent(unsigned int pos)
{ {
const int count = GetCount(); const int count = GetCount();
NSTableColumn *column = GetColumn(pos)->GetNativeData()->GetNativeColumnPtr(); wxDataViewColumnNativeData *nativeData = GetColumn(pos)->GetNativeData();
NSTableColumn *column = nativeData->GetNativeColumnPtr();
UInt32 const noOfColumns = [[m_OutlineView tableColumns] count]; UInt32 const noOfColumns = [[m_OutlineView tableColumns] count];
class MaxWidthCalculator class MaxWidthCalculator
@@ -2338,10 +2339,31 @@ void wxCocoaDataViewControl::FitColumnWidthToContent(unsigned int pos)
if ( m_expanderWidth == 0 ) if ( m_expanderWidth == 0 )
m_expanderWidth = calculator.GetExpanderWidth(); m_expanderWidth = calculator.GetExpanderWidth();
if ( pos == noOfColumns - 1 ) const bool isLast = pos == noOfColumns - 1;
if ( isLast )
{
// Note that FitColumnWidthToContent() is called whenever a column is
// added, so we might also just temporarily become the last column;
// since we cannot know at this time whether we will just temporarily
// be the last column, we store our current column width in order to
// restore it later in case we suddenly are no longer the last column
// because new columns have been added --> we need to restore our
// previous width in that case because it must not get lost.
nativeData->SetPrevWidth(GetColumn(pos)->GetWidth());
[m_OutlineView sizeLastColumnToFit]; [m_OutlineView sizeLastColumnToFit];
else }
else if ( GetColumn(pos)->GetWidthVariable() == wxCOL_WIDTH_AUTOSIZE )
{
[column setWidth:calculator.GetMaxWidth() + m_expanderWidth]; [column setWidth:calculator.GetMaxWidth() + m_expanderWidth];
}
else if ( nativeData->GetIsLast() )
{
[column setWidth:nativeData->GetPrevWidth()];
}
nativeData->SetIsLast(isLast);
if ( !(GetDataViewCtrl()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) ) if ( !(GetDataViewCtrl()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT) )
{ {

View File

@@ -302,10 +302,7 @@ void wxOSXDataViewModelNotifier::AdjustAutosizedColumns()
unsigned count = m_DataViewCtrlPtr->GetColumnCount(); unsigned count = m_DataViewCtrlPtr->GetColumnCount();
for ( unsigned col = 0; col < count; col++ ) for ( unsigned col = 0; col < count; col++ )
{ {
wxDataViewColumn *column = m_DataViewCtrlPtr->GetColumnPtr(col); m_DataViewCtrlPtr->GetDataViewPeer()->FitColumnWidthToContent(col);
if ( column->GetWidthVariable() == wxCOL_WIDTH_AUTOSIZE )
m_DataViewCtrlPtr->GetDataViewPeer()->FitColumnWidthToContent(col);
} }
} }
@@ -441,6 +438,9 @@ bool wxDataViewCtrl::InsertColumn(unsigned int pos, wxDataViewColumn* columnPtr)
// otherwise ask the control to 'update' the data in the newly appended column: // otherwise ask the control to 'update' the data in the newly appended column:
if (GetColumnCount() == 1) if (GetColumnCount() == 1)
SetExpanderColumn(columnPtr); SetExpanderColumn(columnPtr);
AdjustAutosizedColumns();
// done: // done:
return true; return true;
} }
@@ -482,6 +482,8 @@ bool wxDataViewCtrl::DeleteColumn(wxDataViewColumn* columnPtr)
{ {
m_ColumnPtrs.Remove(columnPtr); m_ColumnPtrs.Remove(columnPtr);
delete columnPtr; delete columnPtr;
AdjustAutosizedColumns();
return true; return true;
} }
else else