diff --git a/include/wx/osx/cocoa/dataview.h b/include/wx/osx/cocoa/dataview.h index 9daf40170f..a5aa4509b3 100644 --- a/include/wx/osx/cocoa/dataview.h +++ b/include/wx/osx/cocoa/dataview.h @@ -129,17 +129,24 @@ public: // wxDataViewRendererNativeData(void) : m_Object(NULL), m_ColumnCell(NULL) { + Init(); } wxDataViewRendererNativeData(NSCell* initColumnCell) : m_Object(NULL), m_ColumnCell([initColumnCell retain]) { + Init(); } wxDataViewRendererNativeData(NSCell* initColumnCell, id initObject) : m_Object([initObject retain]), m_ColumnCell([initColumnCell retain]) { + Init(); } + ~wxDataViewRendererNativeData() { [m_ColumnCell release]; [m_Object release]; + + [m_origFont release]; + [m_origTextColour release]; } // @@ -191,11 +198,34 @@ public: m_Object = newObject; } -protected: + // The original cell font and text colour stored here are NULL by default and + // are only initialized to the values retrieved from the cell when we change + // them from wxCocoaOutlineView:willDisplayCell:forTableColumn:item: which + // calls our SaveOriginalXXX() methods before changing the cell attributes. + // + // This allows us to avoid doing anything for the columns without any + // attributes but still be able to restore the correct attributes for the + // ones that do. + NSFont *GetOriginalFont() const { return m_origFont; } + NSColor *GetOriginalTextColour() const { return m_origTextColour; } + + void SaveOriginalFont(NSFont *font) + { + m_origFont = [font retain]; + } + + void SaveOriginalTextColour(NSColor *textColour) + { + m_origTextColour = [textColour retain]; + } + private: -// -// variables -// + void Init() + { + m_origFont = NULL; + m_origTextColour = NULL; + } + id m_Item; // item NOT owned by renderer id m_Object; // object that can be used by renderer for storing special data (owned by renderer) @@ -203,6 +233,10 @@ private: NSCell* m_ItemCell; // item's cell is NOT owned by renderer NSTableColumn* m_TableColumnPtr; // column NOT owned by renderer + + // we own those if they're non-NULL + NSFont *m_origFont; + NSColor *m_origTextColour; }; // ============================================================================ diff --git a/samples/dataview/mymodels.cpp b/samples/dataview/mymodels.cpp index 99e2f76edf..13cdd6704f 100644 --- a/samples/dataview/mymodels.cpp +++ b/samples/dataview/mymodels.cpp @@ -418,10 +418,12 @@ void MyListModel::GetValueByRow( wxVariant &variant, } else if (col==2) { - if (row >= m_array.GetCount()) - variant = "plain"; - else - variant = "blue/green/red"; + static const char *labels[5] = + { + "blue", "green", "red", "bold cyan", "default", + }; + + variant = labels[row % 5]; } } @@ -431,11 +433,28 @@ bool MyListModel::GetAttrByRow( unsigned int row, unsigned int col, if (col != 2) return false; - if (row < m_array.GetCount()) + // do what the labels defined above hint at + switch ( row % 5 ) { - attr.SetColour( (row%3) == 0 ? *wxBLUE : - ((row%3) == 1 ? *wxGREEN : *wxRED) ); - attr.SetItalic( (row%10) == 5 ); + case 0: + attr.SetColour(*wxBLUE); + break; + + case 1: + attr.SetColour(*wxGREEN); + break; + + case 2: + attr.SetColour(*wxRED); + break; + + case 3: + attr.SetColour(*wxCYAN); + attr.SetBold(true); + break; + + case 4: + return false; } return true; diff --git a/src/osx/cocoa/dataview.mm b/src/osx/cocoa/dataview.mm index 15a786ec09..da69f41d75 100644 --- a/src/osx/cocoa/dataview.mm +++ b/src/osx/cocoa/dataview.mm @@ -1495,43 +1495,76 @@ wxWidgetImplType* CreateDataView(wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(pare ) ); + wxDataViewRenderer * const renderer = dvCol->GetRenderer(); + wxDataViewRendererNativeData * const data = renderer->GetNativeData(); + wxDataViewItem dvItem([static_cast(item) pointer]); + + // set the font and text colour to use: we need to do it if we had ever + // changed them before, even if this item itself doesn't have any special + // attributes as otherwise it would reuse the attributes from the previous + // cell rendered using the same renderer + NSFont *font = NULL; + NSColor *colText = NULL; + wxDataViewItemAttr attr; if ( model && model->GetAttr(dvItem, dvCol->GetModelColumn(), attr) ) { if ( attr.HasFont() ) { - // FIXME: using wxFont methods here doesn't work for some reason - NSFontManager * const fm = [NSFontManager sharedFontManager]; - NSFont *font = [cell font]; + font = data->GetOriginalFont(); + if ( !font ) + { + // this is the first time we're setting the font, remember the + // original one before changing it + font = [cell font]; + data->SaveOriginalFont(font); + } + if ( font ) { + // FIXME: using wxFont methods here doesn't work for some reason + NSFontManager * const fm = [NSFontManager sharedFontManager]; if ( attr.GetBold() ) font = [fm convertFont:font toHaveTrait:NSBoldFontMask]; if ( attr.GetItalic() ) font = [fm convertFont:font toHaveTrait:NSItalicFontMask]; - - [cell setFont:font]; } //else: can't change font if the cell doesn't have any } - // we can set font for any cell but only NSTextFieldCell provides a - // method for setting text colour so check that this method is - // available before using it - if ( attr.HasColour() && - [cell respondsToSelector:@selector(setTextColor:)] ) + if ( attr.HasColour() ) { - const wxColour& c = attr.GetColour(); - [cell setTextColor:[NSColor colorWithDeviceRed:c.Red() - green:c.Green() - blue:c.Blue() - alpha:c.Alpha()]]; + // we can set font for any cell but only NSTextFieldCell provides + // a method for setting text colour so check that this method is + // available before using it + if ( [cell respondsToSelector:@selector(setTextColor:)] && + [cell respondsToSelector:@selector(textColor)] ) + { + if ( !data->GetOriginalTextColour() ) + { + data->SaveOriginalTextColour([cell textColor]); + } + + const wxColour& c = attr.GetColour(); + colText = [NSColor colorWithDeviceRed:c.Red() + green:c.Green() + blue:c.Blue() + alpha:c.Alpha()]; + } } } - wxDataViewRenderer * const renderer = dvCol->GetRenderer(); - wxDataViewRendererNativeData * const data = renderer->GetNativeData(); + if ( !font ) + font = data->GetOriginalFont(); + if ( !colText ) + colText = data->GetOriginalTextColour(); + + if ( font ) + [cell setFont:font]; + + if ( colText ) + [cell setTextColor:colText]; data->SetColumnPtr(tableColumn); data->SetItem(item);