From 17cbc2446bbbb51f2989e967e08317f6d02f4f61 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 11 Nov 2009 01:50:06 +0000 Subject: [PATCH] Implement text ellipsizing for wxDataViewCustomRenderer in wxGTK. Add another virtual function (GtkGetTextRenderer()) to the base class which allows us to reuse the same code setting the "ellipsize" property that we already used for wxDataViewTextRenderer for wxDataViewCustomRenderer as well. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62608 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/gtk/dvrenderer.h | 6 ++++++ include/wx/gtk/dvrenderers.h | 5 ++++- src/gtk/dataview.cpp | 41 +++++++++++++++++++++++++++++------- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/include/wx/gtk/dvrenderer.h b/include/wx/gtk/dvrenderer.h index 72f1d4a106..cb7d617830 100644 --- a/include/wx/gtk/dvrenderer.h +++ b/include/wx/gtk/dvrenderer.h @@ -12,6 +12,7 @@ #ifndef _WX_GTK_DVRENDERER_H_ #define _WX_GTK_DVRENDERER_H_ +typedef struct _GtkCellRendererText GtkCellRendererText; typedef struct _GtkTreeViewColumn GtkTreeViewColumn; // ---------------------------------------------------------------------------- @@ -72,6 +73,11 @@ public: bool GtkIsUsingDefaultAttrs() const { return m_usingDefaultAttrs; } void GtkSetUsingDefaultAttrs(bool def) { m_usingDefaultAttrs = def; } + // return the text renderer used by this renderer for setting text cell + // specific attributes: can return NULL if this renderer doesn't render any + // text + virtual GtkCellRendererText *GtkGetTextRenderer() const { return NULL; } + protected: virtual void GtkOnCellChanged(const wxVariant& value, const wxDataViewItem& item, diff --git a/include/wx/gtk/dvrenderers.h b/include/wx/gtk/dvrenderers.h index b22ef67eea..0e8ba0a591 100644 --- a/include/wx/gtk/dvrenderers.h +++ b/include/wx/gtk/dvrenderers.h @@ -13,7 +13,6 @@ #define _WX_GTK_DVRENDERERS_H_ typedef struct _GdkRectangle GdkRectangle; -typedef struct _GtkCellRendererText GtkCellRendererText; // --------------------------------------------------------- // wxDataViewTextRenderer @@ -47,6 +46,8 @@ public: virtual bool GtkSupportsAttrs() const { return true; } virtual bool GtkSetAttr(const wxDataViewItemAttr& attr); + virtual GtkCellRendererText *GtkGetTextRenderer() const; + protected: // implementation of Set/GetValue() bool SetTextValue(const wxString& str); @@ -140,6 +141,8 @@ public: return !attr.IsDefault(); } + virtual GtkCellRendererText *GtkGetTextRenderer() const; + protected: bool Init(wxDataViewCellMode mode, int align); diff --git a/src/gtk/dataview.cpp b/src/gtk/dataview.cpp index f77f50d776..f4e95b42ea 100644 --- a/src/gtk/dataview.cpp +++ b/src/gtk/dataview.cpp @@ -1681,12 +1681,16 @@ void wxDataViewRenderer::EnableEllipsize(wxEllipsizeMode mode) if ( gtk_check_version(2, 6, 0) != NULL ) return; + GtkCellRendererText * const rend = GtkGetTextRenderer(); + if ( !rend ) + return; + // we use the same values in wxEllipsizeMode as PangoEllipsizeMode so we // can just cast between them GValue gvalue = { 0, }; g_value_init( &gvalue, PANGO_TYPE_ELLIPSIZE_MODE ); g_value_set_enum( &gvalue, static_cast(mode) ); - g_object_set_property( G_OBJECT(m_renderer), "ellipsize", &gvalue ); + g_object_set_property( G_OBJECT(rend), "ellipsize", &gvalue ); g_value_unset( &gvalue ); } @@ -1695,9 +1699,13 @@ wxEllipsizeMode wxDataViewRenderer::GetEllipsizeMode() const if ( gtk_check_version(2, 6, 0) != NULL ) return wxELLIPSIZE_NONE; + GtkCellRendererText * const rend = GtkGetTextRenderer(); + if ( !rend ) + return wxELLIPSIZE_NONE; + GValue gvalue = { 0, }; g_value_init( &gvalue, PANGO_TYPE_ELLIPSIZE_MODE ); - g_object_get_property( G_OBJECT(m_renderer), "ellipsize", &gvalue ); + g_object_get_property( G_OBJECT(rend), "ellipsize", &gvalue ); wxEllipsizeMode mode = static_cast(g_value_get_enum( &gvalue )); g_value_unset( &gvalue ); @@ -1916,7 +1924,12 @@ void wxDataViewTextRenderer::SetAlignment( int align ) bool wxDataViewTextRenderer::GtkSetAttr(const wxDataViewItemAttr& attr) { - return GtkApplyAttr(GTK_CELL_RENDERER_TEXT(m_renderer), attr); + return GtkApplyAttr(GtkGetTextRenderer(), attr); +} + +GtkCellRendererText *wxDataViewTextRenderer::GtkGetTextRenderer() const +{ + return GTK_CELL_RENDERER_TEXT(m_renderer); } // --------------------------------------------------------- @@ -2130,29 +2143,41 @@ wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString &varianttype, Init(mode, align); } +GtkCellRendererText *wxDataViewCustomRenderer::GtkGetTextRenderer() const +{ + if ( !m_text_renderer ) + { + // we create it on demand so need to do it even from a const function + const_cast(this)-> + m_text_renderer = GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new()); + } + + return m_text_renderer; +} + void wxDataViewCustomRenderer::RenderText( const wxString &text, int xoffset, wxRect cell, wxDC *WXUNUSED(dc), int WXUNUSED(state) ) { - if (!m_text_renderer) - m_text_renderer = GTK_CELL_RENDERER_TEXT(gtk_cell_renderer_text_new()); + + GtkCellRendererText * const textRenderer = GtkGetTextRenderer(); GValue gvalue = { 0, }; g_value_init( &gvalue, G_TYPE_STRING ); g_value_set_string( &gvalue, wxGTK_CONV_FONT( text, GetOwner()->GetOwner()->GetFont() ) ); - g_object_set_property( G_OBJECT(m_text_renderer), "text", &gvalue ); + g_object_set_property( G_OBJECT(textRenderer), "text", &gvalue ); g_value_unset( &gvalue ); - GtkApplyAttr(m_text_renderer, GetAttr()); + GtkApplyAttr(textRenderer, GetAttr()); GdkRectangle cell_area; wxRectToGDKRect(cell, cell_area); cell_area.x += xoffset; cell_area.width -= xoffset; - gtk_cell_renderer_render( GTK_CELL_RENDERER(m_text_renderer), + gtk_cell_renderer_render( GTK_CELL_RENDERER(textRenderer), m_renderParams.window, m_renderParams.widget, m_renderParams.background_area, -- 2.45.2