From 4264606eedb18f626e864020082d8dde8d055eea Mon Sep 17 00:00:00 2001 From: Robert Roebling Date: Thu, 8 Nov 2007 22:51:58 +0000 Subject: [PATCH] Add wxDataViewTextRendererAttr, blind noop under wxMac git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@49730 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/dataview.h | 45 +++++++++++++++- include/wx/generic/dataview.h | 28 +++++++++- include/wx/gtk/dataview.h | 19 +++++++ include/wx/mac/carbon/dataview.h | 16 ++++++ samples/dataview/dataview.cpp | 91 +++++++++----------------------- samples/dataview/null.xpm | 2 +- src/common/datavcmn.cpp | 5 ++ src/generic/datavgen.cpp | 67 ++++++++++++++++++++++- src/gtk/dataview.cpp | 90 +++++++++++++++++++++++++++++-- 9 files changed, 290 insertions(+), 73 deletions(-) diff --git a/include/wx/dataview.h b/include/wx/dataview.h index 11092d7736..022ff17bd5 100644 --- a/include/wx/dataview.h +++ b/include/wx/dataview.h @@ -20,7 +20,6 @@ #include "wx/textctrl.h" #include "wx/bitmap.h" #include "wx/variant.h" -#include "wx/listctrl.h" #include "wx/dynarray.h" #include "wx/icon.h" @@ -118,6 +117,42 @@ private: }; + +// ---------------------------------------------------------------------------- +// wxDataViewItemAttr: a structure containing the visual attributes of an item +// ---------------------------------------------------------------------------- + +// TODO: this should be renamed to wxItemAttr or something general like this + +class WXDLLIMPEXP_ADV wxDataViewItemAttr +{ +public: + // ctors + wxDataViewItemAttr() + { + m_bold = false; + m_italic = false; + } + + // setters + void SetColour(const wxColour& colour) { m_colour = colour; } + void SetBold( bool set ) { m_bold = set; } + void SetItalic( bool set ) { m_italic = set; } + + // accessors + bool HasColour() const { return m_colour.Ok(); } + const wxColour& GetColour() const { return m_colour; } + + bool GetBold() const { return m_bold; } + bool GetItalic() const { return m_italic; } + +private: + wxColour m_colour; + bool m_bold; + bool m_italic; +}; + + // --------------------------------------------------------- // wxDataViewModel // --------------------------------------------------------- @@ -143,6 +178,10 @@ public: virtual bool SetValue( const wxVariant &variant, const wxDataViewItem &item, unsigned int col ) = 0; + // Get text attribute, return false of default attributes should be used + virtual bool GetAttr( const wxDataViewItem &WXUNUSED(item), unsigned int WXUNUSED(col), wxDataViewItemAttr &WXUNUSED(attr) ) + { return false; } + // define hierachy virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const = 0; virtual bool IsContainer( const wxDataViewItem &item ) const = 0; @@ -197,6 +236,9 @@ public: virtual bool SetValue( const wxVariant &variant, unsigned int row, unsigned int col ) = 0; + virtual bool GetAttr( unsigned int WXUNUSED(row), unsigned int WXUNUSED(col), wxDataViewItemAttr &WXUNUSED(attr) ) + { return false; } + void RowPrepended(); void RowInserted( unsigned int before ); void RowAppended(); @@ -221,6 +263,7 @@ public: const wxDataViewItem &item, unsigned int col ) const; virtual bool SetValue( const wxVariant &variant, const wxDataViewItem &item, unsigned int col ); + virtual bool GetAttr( const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr ); virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const; virtual bool IsContainer( const wxDataViewItem &item ) const; virtual unsigned int GetChildren( const wxDataViewItem &item, wxDataViewItemArray &children ) const; diff --git a/include/wx/generic/dataview.h b/include/wx/generic/dataview.h index efa5a11c74..10e8bbf970 100644 --- a/include/wx/generic/dataview.h +++ b/include/wx/generic/dataview.h @@ -78,11 +78,20 @@ public: // Create DC on request virtual wxDC *GetDC(); + + void SetHasAttr( bool set ) { m_hasAttr = set; } + void SetAttr( const wxDataViewItemAttr &attr ) { m_attr = attr; } + bool GetWantsAttr() { return m_wantsAttr; } private: wxDC *m_dc; int m_align; wxDataViewCellMode m_mode; + +protected: + bool m_wantsAttr; + bool m_hasAttr; + wxDataViewItemAttr m_attr; protected: DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewRenderer) @@ -128,13 +137,30 @@ public: virtual wxControl* CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value ); virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value ); -private: +protected: wxString m_text; protected: DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer) }; +// --------------------------------------------------------- +// wxDataViewTextRendererAttr +// --------------------------------------------------------- + +class WXDLLIMPEXP_ADV wxDataViewTextRendererAttr: public wxDataViewTextRenderer +{ +public: + wxDataViewTextRendererAttr( const wxString &varianttype = wxT("string"), + wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT, + int align = wxDVR_DEFAULT_ALIGNMENT ); + + bool Render( wxRect cell, wxDC *dc, int state ); + +protected: + DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRendererAttr) +}; + // --------------------------------------------------------- // wxDataViewBitmapRenderer // --------------------------------------------------------- diff --git a/include/wx/gtk/dataview.h b/include/wx/gtk/dataview.h index 1b6ecf086c..c4167713a4 100644 --- a/include/wx/gtk/dataview.h +++ b/include/wx/gtk/dataview.h @@ -43,6 +43,7 @@ public: // implementation GtkCellRenderer* GetGtkHandle() { return m_renderer; } void GtkInitHandlers(); + virtual bool GtkHasAttributes() { return false; } protected: GtkCellRenderer *m_renderer; @@ -71,6 +72,24 @@ protected: DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer) }; +// --------------------------------------------------------- +// wxDataViewTextRendererAttr +// --------------------------------------------------------- + +class WXDLLIMPEXP_ADV wxDataViewTextRendererAttr: public wxDataViewTextRenderer +{ +public: + wxDataViewTextRendererAttr( const wxString &varianttype = wxT("string"), + wxDataViewCellMode mode = wxDATAVIEW_CELL_INERT, + int align = wxDVR_DEFAULT_ALIGNMENT ); + + // implementation + bool GtkHasAttributes() { return true; } + +protected: + DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRendererAttr) +}; + // --------------------------------------------------------- // wxDataViewBitmapRenderer // --------------------------------------------------------- diff --git a/include/wx/mac/carbon/dataview.h b/include/wx/mac/carbon/dataview.h index b5a4cda393..67b17836cd 100644 --- a/include/wx/mac/carbon/dataview.h +++ b/include/wx/mac/carbon/dataview.h @@ -232,6 +232,22 @@ private: DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRenderer) }; +// --------------------------------------------------------- +// wxDataViewTextRendererAttr +// --------------------------------------------------------- + +class WXDLLIMPEXP_ADV wxDataViewTextRendererAttr: public wxDataViewTextRenderer +{ +public: +// +// constructors / destructor +// + wxDataViewTextRendererAttr(wxString const& varianttype=wxT("string"), wxDataViewCellMode mode=wxDATAVIEW_CELL_INERT, int align=wxDVR_DEFAULT_ALIGNMENT); + +private: + DECLARE_DYNAMIC_CLASS_NO_COPY(wxDataViewTextRendererAttr) +}; + // --------------------------------------------------------- // wxDataViewBitmapRenderer // --------------------------------------------------------- diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp index f5cd7bd4bd..62077163fa 100644 --- a/samples/dataview/dataview.cpp +++ b/samples/dataview/dataview.cpp @@ -39,64 +39,6 @@ #define DATAVIEW_DEFAULT_STYLE (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES) -// ------------------------------------- -// MySpinCtrlInPlaceRenderer -// ------------------------------------- - -class MySpinCtrlInPlaceRenderer: public wxDataViewCustomRenderer -{ -public: - MySpinCtrlInPlaceRenderer() : - wxDataViewCustomRenderer( wxT("long"), wxDATAVIEW_CELL_EDITABLE ) { } - - - virtual bool HasEditorCtrl() - { - return true; - } - virtual wxControl* CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value ) - { - long l = value; - return new wxSpinCtrl( parent, wxID_ANY, wxEmptyString, - labelRect.GetTopLeft(), labelRect.GetSize(), -0, -1, 2010, l ); - } - virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value ) - { - wxSpinCtrl *sc = (wxSpinCtrl*) editor; - long l = sc->GetValue(); - value = l; - return true; - } - - bool Render( wxRect rect, wxDC *dc, int WXUNUSED(state) ) - { - wxString str; - str.Printf( wxT("%d"), (int) m_data ); - dc->SetTextForeground( *wxBLACK ); - dc->DrawText( str, rect.x, rect.y ); - return true; - } - wxSize GetSize() const - { - return wxSize(80,16); - } - bool SetValue( const wxVariant &value ) - { - m_data = value.GetLong(); - return true; - } - bool GetValue( wxVariant &value ) const - { - value = m_data; - return true; - } - -private: - long m_data; -}; - - - // ------------------------------------- // MyMusicModel // ------------------------------------- @@ -444,14 +386,28 @@ public: { wxDataViewIconText data( "test", m_icon ); variant << data; - } - else + } else + if (col==2) { - wxString str; - str.Printf( "row %d col %d", row, col ); - variant = str; + if ((row % 2) == 1) + variant = "Blue"; + else + variant = "Italic"; } } + + virtual bool GetAttr( unsigned int row, unsigned int col, wxDataViewItemAttr &attr ) + { + if (col != 2) + return false; + + if ((row % 2) == 1) + attr.SetColour( *wxBLUE ); + else + attr.SetItalic( true ); + + return true; + } virtual bool SetValue( const wxVariant &variant, unsigned int row, unsigned int col ) @@ -651,7 +607,7 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int m_music_model = new MyMusicModel; m_musicCtrl->AssociateModel( m_music_model.get() ); - wxDataViewColumn *col = m_musicCtrl->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200, + /* wxDataViewColumn *col = */ m_musicCtrl->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200, DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE ); #if 0 // Call this and sorting is enabled @@ -680,7 +636,10 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int m_listCtrl->AppendTextColumn( "editable string", 0, wxDATAVIEW_CELL_EDITABLE, 120 ); m_listCtrl->AppendIconTextColumn( "icon", 1, wxDATAVIEW_CELL_INERT, 60 ); - m_listCtrl->AppendTextColumn( "index", 2, wxDATAVIEW_CELL_INERT, 120 ); + + wxDataViewTextRendererAttr *ra = new wxDataViewTextRendererAttr; + column = new wxDataViewColumn( "attributes", ra, 2 ); + m_listCtrl->AppendColumn( column ); data_sizer->Add( m_listCtrl, 2, wxGROW ); @@ -716,7 +675,7 @@ void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) /* XPM */ -static char *small1_xpm[] = { +static const char *small1_xpm[] = { /* columns rows colors chars-per-pixel */ "16 16 6 1", ". c Black", diff --git a/samples/dataview/null.xpm b/samples/dataview/null.xpm index abf74843b4..e6706c1aca 100644 --- a/samples/dataview/null.xpm +++ b/samples/dataview/null.xpm @@ -1,5 +1,5 @@ /* XPM */ -static char * null_xpm[] = { +static const char * null_xpm[] = { "16 16 11 1", " c None", ". c #000000", diff --git a/src/common/datavcmn.cpp b/src/common/datavcmn.cpp index 65c8932653..b5f49859fc 100644 --- a/src/common/datavcmn.cpp +++ b/src/common/datavcmn.cpp @@ -386,6 +386,11 @@ bool wxDataViewIndexListModel::SetValue( const wxVariant &variant, return SetValue( variant, GetRow(item), col ); } +bool wxDataViewIndexListModel::GetAttr( const wxDataViewItem &item, unsigned int col, wxDataViewItemAttr &attr ) +{ + return GetAttr( GetRow(item), col, attr ); +} + wxDataViewItem wxDataViewIndexListModel::GetParent( const wxDataViewItem & WXUNUSED(item) ) const { return wxDataViewItem(0); diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index a6c94b1e5d..adcf1b3582 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -607,6 +607,8 @@ wxDataViewRenderer::wxDataViewRenderer( const wxString &varianttype, m_dc = NULL; m_align = align; m_mode = mode; + m_wantsAttr = false; + m_hasAttr = false; } wxDataViewRenderer::~wxDataViewRenderer() @@ -713,6 +715,60 @@ wxSize wxDataViewTextRenderer::GetSize() const return wxSize(80,20); } +// --------------------------------------------------------- +// wxDataViewTextRendererAttr +// --------------------------------------------------------- + +IMPLEMENT_CLASS(wxDataViewTextRendererAttr, wxDataViewTextRenderer) + +wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString &varianttype, + wxDataViewCellMode mode, int align ) : + wxDataViewTextRenderer( varianttype, mode, align ) +{ + m_wantsAttr = true; +} + +bool wxDataViewTextRendererAttr::Render( wxRect cell, wxDC *dc, int WXUNUSED(state) ) +{ + wxFont font; + wxColour colour; + + if (m_hasAttr) + { + if (m_attr.HasColour()) + { + colour = dc->GetTextForeground(); + dc->SetTextForeground( m_attr.GetColour() ); + } + + if (m_attr.GetBold() || m_attr.GetItalic()) + { + font = dc->GetFont(); + wxFont myfont = font; + if (m_attr.GetBold()) + myfont.SetWeight( wxFONTWEIGHT_BOLD ); + if (m_attr.GetItalic()) + myfont.SetStyle( wxFONTSTYLE_ITALIC ); + dc->SetFont( myfont ); + } + } + + dc->DrawText( m_text, cell.x, cell.y + ((cell.height - dc->GetCharHeight()) / 2)); + + // restore dc + if (m_hasAttr) + { + if (m_attr.HasColour()) + dc->SetTextForeground( colour ); + + if (m_attr.GetBold() || m_attr.GetItalic()) + dc->SetFont( font ); + } + + return true; +} + + // --------------------------------------------------------- // wxDataViewBitmapRenderer // --------------------------------------------------------- @@ -2363,6 +2419,15 @@ void wxDataViewMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) ) model->GetValue( value, dataitem, col->GetModelColumn()); cell->SetValue( value ); + + if (cell->GetWantsAttr()) + { + wxDataViewItemAttr attr; + bool ret = model->GetAttr( dataitem, col->GetModelColumn(), attr ); + if (ret) + cell->SetAttr( attr ); + cell->SetHasAttr( ret ); + } // update the y offset cell_rect.y = item * m_lineHeight; @@ -3788,7 +3853,7 @@ wxDataViewColumn* wxDataViewCtrl::GetColumn( unsigned int pos ) const bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn *column ) { wxDataViewColumnList::compatibility_iterator ret = m_cols.Find( column ); - if (ret == NULL) + if (!ret) return false; m_cols.Erase(ret); diff --git a/src/gtk/dataview.cpp b/src/gtk/dataview.cpp index f7d910d2de..169dd8c446 100644 --- a/src/gtk/dataview.cpp +++ b/src/gtk/dataview.cpp @@ -1475,6 +1475,18 @@ void wxDataViewTextRenderer::SetAlignment( int align ) g_value_unset( &gvalue ); } +// --------------------------------------------------------- +// wxDataViewTextRendererAttr +// --------------------------------------------------------- + +IMPLEMENT_CLASS(wxDataViewTextRendererAttr,wxDataViewTextRenderer) + +wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString &varianttype, + wxDataViewCellMode mode, int align ) : + wxDataViewTextRenderer( varianttype, mode, align ) +{ +} + // --------------------------------------------------------- // wxDataViewBitmapRenderer // --------------------------------------------------------- @@ -2112,10 +2124,82 @@ static void wxGtkTreeCellDataFunc( GtkTreeViewColumn *column, cell->SetValue( value ); -#if 0 - wxListItemAttr attr; - wx_model->GetAttr( item, attr, cell->GetOwner()->GetModelColumn() ); + if (cell->GtkHasAttributes()) + { + wxDataViewItemAttr attr; + bool colour_set = false; + bool style_set = false; + bool weight_set = false; + + if (wx_model->GetAttr( item, cell->GetOwner()->GetModelColumn(), attr )) + { + // this must be a GtkCellRendererText + wxColour colour = attr.GetColour(); + if (colour.IsOk()) + { + const GdkColor * const gcol = colour.GetColor(); + GValue gvalue = { 0, }; + g_value_init( &gvalue, GDK_TYPE_COLOR ); + g_value_set_boxed( &gvalue, gcol ); + g_object_set_property( G_OBJECT(renderer), "foreground_gdk", &gvalue ); + g_value_unset( &gvalue ); + + colour_set = true; + } + + if (attr.GetItalic()) + { + GValue gvalue = { 0, }; + g_value_init( &gvalue, PANGO_TYPE_STYLE ); + g_value_set_enum( &gvalue, PANGO_STYLE_ITALIC ); + g_object_set_property( G_OBJECT(renderer), "style", &gvalue ); + g_value_unset( &gvalue ); + + style_set = true; + } + + if (attr.GetBold()) + { + GValue gvalue = { 0, }; + g_value_init( &gvalue, PANGO_TYPE_WEIGHT ); + g_value_set_enum( &gvalue, PANGO_WEIGHT_BOLD ); + g_object_set_property( G_OBJECT(renderer), "weight", &gvalue ); + g_value_unset( &gvalue ); + + weight_set = true; + } + } + + if (!style_set) + { + GValue gvalue = { 0, }; + g_value_init( &gvalue, G_TYPE_BOOLEAN ); + g_value_set_boolean( &gvalue, FALSE ); + g_object_set_property( G_OBJECT(renderer), "style-set", &gvalue ); + g_value_unset( &gvalue ); + } + + if (!weight_set) + { + GValue gvalue = { 0, }; + g_value_init( &gvalue, G_TYPE_BOOLEAN ); + g_value_set_boolean( &gvalue, FALSE ); + g_object_set_property( G_OBJECT(renderer), "weight-set", &gvalue ); + g_value_unset( &gvalue ); + } + + if (!colour_set) + { + GValue gvalue = { 0, }; + g_value_init( &gvalue, G_TYPE_BOOLEAN ); + g_value_set_boolean( &gvalue, FALSE ); + g_object_set_property( G_OBJECT(renderer), "foreground-set", &gvalue ); + g_value_unset( &gvalue ); + } + } + +#if 0 if (attr.HasBackgroundColour()) { wxColour colour = attr.GetBackgroundColour(); -- 2.47.2