ergtkcellrendererborder.c

Go to the documentation of this file.
00001 
00002 /*
00003  * File Name: ergtkcellrendererborder.c
00004  */
00005 
00006 /*
00007  * This file is part of libergtk.
00008  *
00009  * libergtk is free software: you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation, either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * libergtk is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00021  */
00022  
00023 /**
00024  * Copyright (C) 2009 iRex Technologies B.V.
00025  * All rights reserved.
00026  */
00027 
00028 //----------------------------------------------------------------------------
00029 // Include Files
00030 //----------------------------------------------------------------------------
00031 
00032 // system include files, between < >
00033 #include <glib.h>
00034 
00035 // ereader include files, between < >
00036 
00037 // local include files, between " "
00038 #include "ergtk_log.h"
00039 #include "ergtkcellrendererborder.h"
00040 
00041 
00042 //----------------------------------------------------------------------------
00043 // Type Declarations
00044 //----------------------------------------------------------------------------
00045 
00046 // class-specific properties
00047 typedef enum
00048         {
00049             PROP_NOT_USED = 0,      // not sure whether param_id zero is allowed
00050             PROP_BORDER_WIDTH,      // border width in pixels
00051             PROP_BORDER_OFFSET,     // border offset from boundaries in pixels
00052             PROP_BORDER_COLOR,      // border colour as a string
00053             NUM_PROPS
00054         } prop_t;
00055 
00056 // instance private data
00057 typedef struct
00058         {
00059             guint       border_width;           // border width in pixels
00060             guint       border_offset;          // border offset from boundaries in pixels
00061             gchar       *border_color;          // border colour as a string
00062         } erGtkCellRendererBorderPrivate;
00063     
00064 
00065 //----------------------------------------------------------------------------
00066 // Global Constants
00067 //----------------------------------------------------------------------------
00068 
00069 static const gchar      *PROPNAME_BORDER_WIDTH  = "border-width";
00070 static const gchar      *PROPNAME_BORDER_OFFSET = "border-offset";
00071 static const gchar      *PROPNAME_BORDER_COLOR  = "border-color";
00072 
00073 
00074 //----------------------------------------------------------------------------
00075 // Static Variables
00076 //----------------------------------------------------------------------------
00077 
00078 static GtkCellRendererTextClass* g_parent_class = NULL;
00079 
00080 
00081 //============================================================================
00082 // Local Function Definitions
00083 //============================================================================
00084 
00085 // Functions for GObject derived object:
00086 //   Overloaded methods for base class
00087 static void get_property_impl ( GObject    *object,
00088                                 guint       param_id,
00089                                 GValue     *value,
00090                                 GParamSpec *pspec );
00091 
00092 static void set_property_impl ( GObject      *object,
00093                                 guint         param_id,
00094                                 const GValue *value,
00095                                 GParamSpec   *pspec );
00096 
00097 static void render_impl (GtkCellRenderer *cell,
00098                          GdkWindow       *window,
00099                          GtkWidget       *widget,
00100                          GdkRectangle    *bg_area,
00101                          GdkRectangle    *cell_area,
00102                          GdkRectangle    *expose_area,
00103                          guint            flags);
00104 
00105 //   Overloaded signal handlers for base class
00106 
00107 //   Implementation of virtual methods
00108 
00109 //   Local functions
00110 static void     ergtk_cell_renderer_border_class_init     (erGtkCellRendererBorderClass *klass);
00111 static void     ergtk_cell_renderer_border_init           (erGtkCellRendererBorder *thiz);
00112 static void     ergtk_cell_renderer_border_finalize       (GObject *obj);
00113 
00114 
00115 //============================================================================
00116 // Functions Implementation
00117 //============================================================================
00118 
00119 // get object type
00120 GType ergtk_cell_renderer_border_get_type (void)
00121 {
00122     static GType class_type = 0;
00123 
00124     if (class_type == 0)
00125     {
00126         static const GTypeInfo class_info =
00127         {
00128             sizeof(erGtkCellRendererBorderClass),                   // class_size
00129             NULL,                                                   // base_init
00130             NULL,                                                   // base_finalize
00131             (GClassInitFunc) ergtk_cell_renderer_border_class_init, // class_init
00132             NULL,                                                   // class_finalize
00133             NULL,                                                   // class_data
00134             sizeof(erGtkCellRendererBorder),                        // instance_size
00135             0,                                                      // n_preallocs
00136             (GInstanceInitFunc) ergtk_cell_renderer_border_init,    // instance_init
00137             NULL                                                    // value_table
00138         };
00139 
00140         class_type = g_type_register_static( GTK_TYPE_CELL_RENDERER_TEXT,   // parent_type
00141                                              "erGtkCellRendererBorder",     // type_name
00142                                              &class_info,                   // info
00143                                              0 );                           // flags
00144     }
00145 
00146     return class_type;
00147 }
00148 
00149 
00150 // class init
00151 static void ergtk_cell_renderer_border_class_init (erGtkCellRendererBorderClass* klass)
00152 {
00153     GObjectClass         *object_class        = (GObjectClass *) klass;
00154     GtkCellRendererClass *cell_renderer_class = (GtkCellRendererClass *) klass;
00155 
00156     GParamSpec  *pspec;
00157 
00158     // remember parent class struct, needed for chaining up to parent class
00159     g_parent_class = g_type_class_peek_parent(klass);
00160 
00161     // overload some virtual methods
00162     cell_renderer_class->render = render_impl;
00163     object_class->get_property  = get_property_impl;
00164     object_class->set_property  = set_property_impl;
00165     object_class->finalize      = ergtk_cell_renderer_border_finalize;
00166 
00167     // reserve space for instance private data
00168     g_type_class_add_private (klass, sizeof(erGtkCellRendererBorderPrivate));
00169 
00170     // install custom properties
00171     //   border width
00172     pspec = g_param_spec_uint( PROPNAME_BORDER_WIDTH,
00173                                PROPNAME_BORDER_WIDTH,
00174                                "Border width in pixels",
00175                                0,      // min.
00176                                10,     // max.
00177                                1,      // default
00178                                G_PARAM_READWRITE | G_PARAM_CONSTRUCT );
00179     g_object_class_install_property( object_class, PROP_BORDER_WIDTH, pspec);
00180     //   border offset
00181     pspec = g_param_spec_uint( PROPNAME_BORDER_OFFSET,
00182                                PROPNAME_BORDER_OFFSET,
00183                                "Border width in pixels",
00184                                0,      // min.
00185                                10,     // max.
00186                                0,      // default
00187                                G_PARAM_READWRITE | G_PARAM_CONSTRUCT );
00188     g_object_class_install_property( object_class, PROP_BORDER_OFFSET, pspec);
00189     //   border colour
00190     pspec = g_param_spec_string( PROPNAME_BORDER_COLOR,
00191                                  PROPNAME_BORDER_COLOR,
00192                                  "Border colour as a string",
00193                                  "",        // default
00194                                  G_PARAM_READWRITE | G_PARAM_CONSTRUCT );
00195     g_object_class_install_property( object_class, PROP_BORDER_COLOR, pspec);
00196 }
00197 
00198 
00199 // instance init
00200 static void ergtk_cell_renderer_border_init (erGtkCellRendererBorder *thiz)
00201 {
00202     g_return_if_fail( IS_ERGTK_CELL_RENDERER_BORDER(thiz) );
00203 
00204     erGtkCellRendererBorderPrivate *priv = ERGTK_CELL_RENDERER_BORDER_GET_PRIVATE(thiz);
00205 
00206     // instance data
00207     //   public
00208     //   private
00209     priv->border_color = NULL;
00210 }
00211 
00212 
00213 // finalize instance
00214 static void ergtk_cell_renderer_border_finalize (GObject *obj)
00215 {
00216     g_return_if_fail( IS_ERGTK_CELL_RENDERER_BORDER(obj) );
00217 
00218     erGtkCellRendererBorderPrivate *priv = ERGTK_CELL_RENDERER_BORDER_GET_PRIVATE(obj);
00219 
00220     // destroy instance data
00221     //   private
00222     g_free(priv->border_color);
00223     //   public: none
00224 
00225     // chain to parent class
00226     ((GObjectClass*)g_parent_class)->finalize( obj );
00227 }
00228 
00229 
00230 // create object
00231 GtkCellRenderer *ergtk_cell_renderer_border_new (void)
00232 {
00233     erGtkCellRendererBorder        *thiz = (erGtkCellRendererBorder*) g_object_new(ERGTK_CELL_RENDERER_BORDER_TYPE, NULL);
00234     //erGtkCellRendererBorderPrivate *priv = ERGTK_CELL_RENDERER_BORDER_GET_PRIVATE(thiz);
00235 
00236     return (GtkCellRenderer*) thiz;
00237 }
00238 
00239 
00240 // get class-specific property
00241 static void get_property_impl ( GObject    *object,
00242                                 guint       param_id,
00243                                 GValue     *value,
00244                                 GParamSpec *pspec )
00245 {
00246     g_return_if_fail( IS_ERGTK_CELL_RENDERER_BORDER(object) );
00247 
00248     erGtkCellRendererBorderPrivate *priv = ERGTK_CELL_RENDERER_BORDER_GET_PRIVATE(object);
00249 
00250     switch (param_id)
00251     {
00252         case PROP_BORDER_WIDTH:
00253             g_value_set_uint (value, priv->border_width);
00254             break;
00255 
00256         case PROP_BORDER_OFFSET:
00257             g_value_set_uint (value, priv->border_offset);
00258             break;
00259 
00260         case PROP_BORDER_COLOR:
00261             g_value_set_string (value, priv->border_color);
00262             break;
00263 
00264         default:
00265             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
00266     }
00267 }
00268 
00269 
00270 // set class-specific property
00271 static void set_property_impl ( GObject      *object,
00272                                 guint         param_id,
00273                                 const GValue *value,
00274                                 GParamSpec   *pspec )
00275 {
00276     g_return_if_fail( IS_ERGTK_CELL_RENDERER_BORDER(object) );
00277 
00278     erGtkCellRendererBorderPrivate *priv = ERGTK_CELL_RENDERER_BORDER_GET_PRIVATE(object);
00279 
00280     guint        u;
00281     gchar        **cpp;
00282 
00283     switch (param_id)
00284     {
00285         case PROP_BORDER_WIDTH:
00286             u = g_value_get_uint(value);
00287             priv->border_width = u;
00288             LOGPRINTF("border-width set to [%u]", u);
00289             break;
00290 
00291         case PROP_BORDER_OFFSET:
00292             u = g_value_get_uint(value);
00293             priv->border_offset = u;
00294             LOGPRINTF("border-offset set to [%u]", u);
00295             break;
00296 
00297         case PROP_BORDER_COLOR:
00298             cpp = &(priv->border_color);
00299             g_free(*cpp);
00300             *cpp = g_strdup(g_value_get_string(value));
00301             LOGPRINTF("border-color set to [%s]", *cpp);
00302             break;
00303 
00304         default:
00305             G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec);
00306     }
00307 }
00308 
00309 
00310 // render one cell
00311 static void render_impl (GtkCellRenderer *cell,
00312                          GdkWindow       *window,
00313                          GtkWidget       *widget,
00314                          GdkRectangle    *bg_area,
00315                          GdkRectangle    *cell_area,
00316                          GdkRectangle    *expose_area,
00317                          guint            flags)
00318 {
00319     g_return_if_fail( IS_ERGTK_CELL_RENDERER_BORDER(cell) );
00320 
00321     erGtkCellRendererBorder        *thiz = (erGtkCellRendererBorder *) cell;
00322     erGtkCellRendererBorderPrivate *priv = ERGTK_CELL_RENDERER_BORDER_GET_PRIVATE(thiz);
00323 
00324     gint border = priv->border_offset;
00325     gint x      = 0               + border;
00326     gint y      = bg_area->y      + border;
00327     gint w      = bg_area->x      + bg_area->width - (2 * border) - 1;
00328     gint h      = bg_area->height                  - (2 * border);
00329 
00330     gtk_paint_box( widget->style, 
00331                    window, 
00332                    GTK_STATE_NORMAL,
00333                    GTK_SHADOW_NONE, 
00334                    NULL, widget, "ergtkcellrendererborder", 
00335                    x, y, w, h );
00336 }
Generated by  doxygen 1.6.2-20100208