From 7feaa794bc944e6390080ac022010140541e00dd Mon Sep 17 00:00:00 2001 From: Hartwig Wiesmann Date: Sat, 18 Jul 2015 02:11:27 +0200 Subject: [PATCH] Use available space better in wxDataViewCtrl date renderer in wxOSX. Try to show as much useful information as possible for the available width, notably show the time fully, including seconds, which was never done before. Also add a date column to the dataview sample to allow seeing how this works in practice. See #16640. --- samples/dataview/dataview.cpp | 2 ++ samples/dataview/mymodels.cpp | 6 ++++++ samples/dataview/mymodels.h | 1 + src/osx/cocoa/dataview.mm | 34 +++++++++++++++------------------- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp index 1b536f92f2..cca43f0f4f 100644 --- a/samples/dataview/dataview.cpp +++ b/samples/dataview/dataview.cpp @@ -640,6 +640,8 @@ void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned l wxALIGN_NOT, wxDATAVIEW_COL_REORDERABLE | wxDATAVIEW_COL_SORTABLE); + m_ctrl[1]->AppendDateColumn("date", + MyListModel::Col_Date); m_attributes = new wxDataViewColumn("attributes", new wxDataViewTextRenderer, diff --git a/samples/dataview/mymodels.cpp b/samples/dataview/mymodels.cpp index 23efe59afd..c18f9000b9 100644 --- a/samples/dataview/mymodels.cpp +++ b/samples/dataview/mymodels.cpp @@ -428,6 +428,10 @@ void MyListModel::GetValueByRow( wxVariant &variant, } break; + case Col_Date: + variant = wxDateTime(1, wxDateTime::Jan, 2000).Add(wxTimeSpan(row)); + break; + case Col_TextWithAttr: { static const char *labels[5] = @@ -454,6 +458,7 @@ bool MyListModel::GetAttrByRow( unsigned int row, unsigned int col, switch ( col ) { case Col_EditableText: + case Col_Date: return false; case Col_IconText: @@ -524,6 +529,7 @@ bool MyListModel::SetValueByRow( const wxVariant &variant, } return true; + case Col_Date: case Col_TextWithAttr: case Col_Custom: wxLogError("Cannot edit the column %d", col); diff --git a/samples/dataview/mymodels.h b/samples/dataview/mymodels.h index 8706bbc260..96921c1724 100644 --- a/samples/dataview/mymodels.h +++ b/samples/dataview/mymodels.h @@ -194,6 +194,7 @@ public: { Col_EditableText, Col_IconText, + Col_Date, Col_TextWithAttr, Col_Custom, Col_Max diff --git a/src/osx/cocoa/dataview.mm b/src/osx/cocoa/dataview.mm index 6b69ebe729..4bf947052b 100644 --- a/src/osx/cocoa/dataview.mm +++ b/src/osx/cocoa/dataview.mm @@ -2999,31 +2999,27 @@ bool wxDataViewDateRenderer::MacRender() // in the first instance; but as this is often impossible due to // space restrictions the style is shortened per loop; finally, if // the shortest time and date format does not fit into the cell - // the time part is dropped; remark: the time part itself is not - // modified per iteration loop and only uses the short style, - // means that only the hours and minutes are being shown + // the time part is dropped // GetObject() returns a date for testing the size of a date object [GetNativeData()->GetItemCell() setObjectValue:GetNativeData()->GetObject()]; - [[GetNativeData()->GetItemCell() formatter] setTimeStyle:NSDateFormatterShortStyle]; - for (int dateFormatterStyle=4; dateFormatterStyle>0; --dateFormatterStyle) + + bool formatFound = false; + int dateFormatterStyle = kCFDateFormatterFullStyle; + while ( !formatFound && (dateFormatterStyle > 0) ) { - [[GetNativeData()->GetItemCell() formatter] setDateStyle:(NSDateFormatterStyle)dateFormatterStyle]; - if (dateFormatterStyle == 1) + int timeFormatterStyle = dateFormatterStyle; + + while ( !formatFound && (timeFormatterStyle >= dateFormatterStyle - 1) ) { - // if the shortest style for displaying the date and time - // is too long to be fully visible remove the time part of - // the date: - if ([GetNativeData()->GetItemCell() cellSize].width > [GetNativeData()->GetColumnPtr() width]) - [[GetNativeData()->GetItemCell() formatter] setTimeStyle:NSDateFormatterNoStyle]; - { - // basically not necessary as the loop would end anyway - // but let's save the last comparison - break; - } + [[GetNativeData()->GetItemCell() formatter] setDateStyle:(NSDateFormatterStyle)dateFormatterStyle]; + [[GetNativeData()->GetItemCell() formatter] setTimeStyle:(NSDateFormatterStyle)timeFormatterStyle]; + if ( [GetNativeData()->GetItemCell() cellSize].width <= [GetNativeData()->GetColumnPtr() width] ) + formatFound = true; + else + --timeFormatterStyle; } - else if ([GetNativeData()->GetItemCell() cellSize].width <= [GetNativeData()->GetColumnPtr() width]) - break; + --dateFormatterStyle; } // set data (the style is set by the previous loop); on OSX the // date has to be specified with respect to UTC; in wxWidgets the