"Fossies" - the Fresh Open Source Software Archive

Member "gtkdatabox-1.0.0/gtk/gtkdatabox_graph.c" (31 Mar 2021, 10581 Bytes) of package /linux/privat/gtkdatabox-1.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "gtkdatabox_graph.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.9.3.1_vs_1.0.0.

    1 /* $Id: gtkdatabox_graph.c 4 2008-06-22 09:19:11Z rbock $ */
    2 /* GtkDatabox - An extension to the gtk+ library
    3  * Copyright (C) 1998 - 2008  Dr. Roland Bock
    4  *
    5  * This program is free software; you can redistribute it and/or
    6  * modify it under the terms of the GNU Lesser General Public License
    7  * as published by the Free Software Foundation; either version 2.1
    8  * of the License, or (at your option) any later version.
    9  *
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU Lesser General Public License for more details.
   14  *
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program; if not, write to the Free Software
   17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
   18  */
   19 
   20 #include <gtkdatabox_graph.h>
   21 #include <gtk/gtk.h>
   22 
   23 static void gtk_databox_graph_real_draw (GtkDataboxGraph * graph,
   24     GtkDatabox * draw);
   25 static gint gtk_databox_graph_real_calculate_extrema (GtkDataboxGraph * graph,
   26     gfloat * min_x,
   27     gfloat * max_x,
   28     gfloat * min_y,
   29     gfloat * max_y);
   30 
   31 /* IDs of properties */
   32 enum
   33 {
   34   GRAPH_COLOR = 1,
   35   GRAPH_SIZE,
   36   GRAPH_HIDE,
   37 };
   38 
   39 /**
   40  * GtkDataboxGraphPrivate
   41  *
   42  * A private data structure used by the #GtkDataboxGraph. It shields all internal things
   43  * from developers who are just using the object.
   44  *
   45  **/
   46 typedef struct _GtkDataboxGraphPrivate GtkDataboxGraphPrivate;
   47 
   48 struct _GtkDataboxGraphPrivate
   49 {
   50   GdkRGBA color;
   51   gint size;
   52   gboolean hide;
   53   GdkRGBA rgba;
   54 };
   55 
   56 G_DEFINE_TYPE_WITH_PRIVATE(GtkDataboxGraph, gtk_databox_graph,
   57     G_TYPE_OBJECT)
   58 
   59 static void
   60 gtk_databox_graph_set_property (GObject * object,
   61                                 guint property_id,
   62                                 const GValue * value, GParamSpec * pspec)
   63 {
   64   GtkDataboxGraph *graph = GTK_DATABOX_GRAPH (object);
   65 
   66   switch (property_id)
   67     {
   68     case GRAPH_COLOR:
   69     {
   70       gtk_databox_graph_set_color (graph,
   71                                    (GdkRGBA *)
   72                                    g_value_get_pointer (value));
   73     }
   74     break;
   75     case GRAPH_SIZE:
   76     {
   77       gtk_databox_graph_set_size (graph, g_value_get_int (value));
   78     }
   79     break;
   80     case GRAPH_HIDE:
   81     {
   82       gtk_databox_graph_set_hide (graph, g_value_get_boolean (value));
   83     }
   84     break;
   85     default:
   86       /* We don't have any other property... */
   87       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
   88       break;
   89     }
   90 }
   91 
   92 static void
   93 gtk_databox_graph_get_property (GObject * object,
   94                                 guint property_id,
   95                                 GValue * value, GParamSpec * pspec)
   96 {
   97   GtkDataboxGraph *graph = GTK_DATABOX_GRAPH (object);
   98 
   99   switch (property_id)
  100     {
  101     case GRAPH_COLOR:
  102     {
  103       g_value_set_pointer (value, gtk_databox_graph_get_color (graph));
  104     }
  105     break;
  106     case GRAPH_SIZE:
  107     {
  108       g_value_set_int (value, gtk_databox_graph_get_size (graph));
  109     }
  110     break;
  111     case GRAPH_HIDE:
  112     {
  113       g_value_set_boolean (value, gtk_databox_graph_get_hide (graph));
  114     }
  115     break;
  116     default:
  117       /* We don't have any other property... */
  118       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
  119       break;
  120     }
  121 }
  122 
  123 /**
  124  * gtk_databox_graph_create_gc:
  125  * @graph: A #GtkDataboxGraph object
  126  * @box: A #GtkDatabox object
  127  *
  128  * Virtual function which creates a graphics context for the @graph.
  129  *
  130  * Typically called by derived graph objects when the graphics context is needed for the first time.
  131  *
  132  * Return value: The new graphics context.
  133  */
  134 cairo_t*
  135 gtk_databox_graph_create_gc (GtkDataboxGraph * graph,
  136                              GtkDatabox* box)
  137 {
  138   return GTK_DATABOX_GRAPH_GET_CLASS (graph)->create_gc (graph, box);
  139 }
  140 
  141 static cairo_t *
  142 gtk_databox_graph_real_create_gc (GtkDataboxGraph * graph,
  143                                   GtkDatabox* box)
  144 {
  145   GtkDataboxGraphPrivate *priv = gtk_databox_graph_get_instance_private(graph);
  146   cairo_t *cr;
  147 
  148   g_return_val_if_fail (GTK_DATABOX_IS_GRAPH (graph), NULL);
  149 
  150    cr = cairo_create (gtk_databox_get_backing_surface (box));
  151    gdk_cairo_set_source_rgba (cr, &priv->color);
  152    cairo_set_line_width (cr,  (priv->size > 1) ? priv->size : 1);
  153 
  154    return cr;
  155 }
  156 
  157 static void
  158 gtk_databox_graph_class_init (GtkDataboxGraphClass *klass)
  159 {
  160   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  161   GParamSpec *graph_param_spec;
  162 
  163   gobject_class->set_property = gtk_databox_graph_set_property;
  164   gobject_class->get_property = gtk_databox_graph_get_property;
  165 
  166   graph_param_spec = g_param_spec_pointer ("color",
  167                      "Graph color",
  168                      "Color of graph",
  169                      G_PARAM_READWRITE);
  170 
  171   g_object_class_install_property (gobject_class,
  172                                    GRAPH_COLOR, graph_param_spec);
  173 
  174   graph_param_spec = g_param_spec_int ("size", "Graph size", "Size of displayed items", G_MININT, G_MAXINT, 0,  /* default value */
  175                                        G_PARAM_READWRITE);
  176 
  177   g_object_class_install_property (gobject_class,
  178                                    GRAPH_SIZE, graph_param_spec);
  179 
  180   graph_param_spec = g_param_spec_boolean ("hide", "Graph hidden", "Determine if graph is hidden or not", FALSE,    /* default value */
  181                      G_PARAM_READWRITE);
  182 
  183   g_object_class_install_property (gobject_class,
  184                                    GRAPH_HIDE, graph_param_spec);
  185 
  186   klass->draw = gtk_databox_graph_real_draw;
  187   klass->calculate_extrema = gtk_databox_graph_real_calculate_extrema;
  188   klass->create_gc = gtk_databox_graph_real_create_gc;
  189 }
  190 
  191 static void
  192 gtk_databox_graph_init (GtkDataboxGraph *graph)
  193 {
  194     if (graph == NULL) g_warning ("graph_init with NULL");
  195 }
  196 
  197 /**
  198  * gtk_databox_graph_draw:
  199  * @graph: A #GtkDataboxGraph object
  200  * @box: A #GtkDatabox object
  201  *
  202  * Virtual function which draws the #GtkDataboxGraph on the drawing area of the GtkDatabox object.
  203  *
  204  * Typically this function is called by #GtkDatabox objects.
  205  *
  206  */
  207 void
  208 gtk_databox_graph_draw (GtkDataboxGraph * graph, GtkDatabox* box)
  209 {
  210   GtkDataboxGraphPrivate *priv = gtk_databox_graph_get_instance_private(graph);
  211   if (!priv->hide)
  212     GTK_DATABOX_GRAPH_GET_CLASS (graph)->draw (graph, box);
  213 }
  214 
  215 /**
  216  * gtk_databox_graph_calculate_extrema:
  217  * @graph: A #GtkDataboxGraph object
  218  * @min_x: Will be filled with the lowest x value of the dataset
  219  * @max_x: Will be filled with the highest x value of the dataset
  220  * @min_y: Will be filled with the lowest y value of the dataset
  221  * @max_y: Will be filled with the highest y value of the dataset
  222  *
  223  * Virtual function which determines the minimum and maximum x and y values of the values of this
  224  * #GtkDataboxGraph object if applicable (there are graphs which do
  225  * not contain data).
  226  *
  227  * Return value: 0 on success,
  228  *          -1 if no data is available,
  229  *
  230  */
  231 gint
  232 gtk_databox_graph_calculate_extrema (GtkDataboxGraph * graph,
  233                                      gfloat * min_x, gfloat * max_x,
  234                                      gfloat * min_y, gfloat * max_y)
  235 {
  236   return
  237     GTK_DATABOX_GRAPH_GET_CLASS (graph)->calculate_extrema (graph, min_x,
  238         max_x, min_y,
  239         max_y);
  240 }
  241 
  242 static void
  243 gtk_databox_graph_real_draw (GtkDataboxGraph * graph,
  244                              GtkDatabox* box)
  245 {
  246   g_return_if_fail (graph);
  247   g_return_if_fail (box);
  248 
  249   /* We have no data... */
  250   return;
  251 }
  252 
  253 
  254 static gint
  255 gtk_databox_graph_real_calculate_extrema (GtkDataboxGraph * graph,
  256     gfloat * min_x, gfloat * max_x,
  257     gfloat * min_y, gfloat * max_y)
  258 {
  259   g_return_val_if_fail (graph, -1);
  260   g_return_val_if_fail (min_x, -1);
  261   g_return_val_if_fail (max_x, -1);
  262   g_return_val_if_fail (min_y, -1);
  263   g_return_val_if_fail (max_y, -1);
  264 
  265   /* We have no data... */
  266   return -1;
  267 }
  268 
  269 /**
  270  * gtk_databox_graph_set_color:
  271  * @graph: A #GtkDataboxGraph object
  272  * @color: Color which is to be used by the graph object
  273  *
  274  * Sets the color which the #GtkDataboxGraph object is supposed to be using when drawing itself.
  275  *
  276  */
  277 void
  278 gtk_databox_graph_set_color (GtkDataboxGraph * graph, GdkRGBA * color)
  279 {
  280   GtkDataboxGraphPrivate *priv = gtk_databox_graph_get_instance_private(graph);
  281 
  282   g_return_if_fail (GTK_DATABOX_IS_GRAPH (graph));
  283 
  284   priv->color = *color;
  285 
  286   g_object_notify (G_OBJECT (graph), "color");
  287 }
  288 
  289 /**
  290  * gtk_databox_graph_get_color:
  291  * @graph: A #GtkDataboxGraph object
  292  *
  293  * Gets the current color of the graph elements (e.g. points).
  294  *
  295  * Return value: The color of the graph.
  296  *
  297  */
  298 GdkRGBA *
  299 gtk_databox_graph_get_color (GtkDataboxGraph * graph)
  300 {
  301   GtkDataboxGraphPrivate *priv = gtk_databox_graph_get_instance_private(graph);
  302   return &priv->color;
  303 }
  304 
  305 /**
  306  * gtk_databox_graph_set_size:
  307  * @graph: A #GtkDataboxGraph object
  308  * @size: Size of graph elements for the graph object
  309  *
  310  * Sets the size (e.g. line width) which the #GtkDataboxGraph object is supposed to be using when drawing itself.
  311  *
  312  */
  313 void
  314 gtk_databox_graph_set_size (GtkDataboxGraph * graph, gint size)
  315 {
  316   g_return_if_fail (GTK_DATABOX_IS_GRAPH (graph));
  317   GtkDataboxGraphPrivate *priv = gtk_databox_graph_get_instance_private(graph);
  318   priv->size = MAX (1, size);;
  319 
  320   g_object_notify (G_OBJECT (graph), "size");
  321 }
  322 
  323 /**
  324  * gtk_databox_graph_get_size:
  325  * @graph: A #GtkDataboxGraph object
  326  *
  327  * Gets the size of the graph elements (e.g. the line width).
  328  *
  329  * Return value: size of the graph elements
  330  *
  331  */
  332 gint
  333 gtk_databox_graph_get_size (GtkDataboxGraph * graph)
  334 {
  335   g_return_val_if_fail (GTK_DATABOX_IS_GRAPH (graph), -1);
  336   GtkDataboxGraphPrivate *priv = gtk_databox_graph_get_instance_private(graph);
  337   return priv->size;
  338 }
  339 
  340 /**
  341  * gtk_databox_graph_set_hide:
  342  * @graph: A #GtkDataboxGraph object
  343  * @hide: Declares whether should be hidden (true) or not (false).
  344  *
  345  * Hidden graphs are not shown, when the #GtkDatabox containing them is redrawn.
  346  *
  347  */
  348 void
  349 gtk_databox_graph_set_hide (GtkDataboxGraph * graph, gboolean hide)
  350 {
  351   g_return_if_fail (GTK_DATABOX_IS_GRAPH (graph));
  352   GtkDataboxGraphPrivate *priv = gtk_databox_graph_get_instance_private(graph);
  353   priv->hide = hide;
  354 
  355   g_object_notify (G_OBJECT (graph), "hide");
  356 }
  357 
  358 /**
  359  * gtk_databox_graph_get_hide:
  360  * @graph: A #GtkDataboxGraph object
  361  *
  362  * Gets the current "hide" status.
  363  *
  364  * Return value: Whether the graph is hidden (true) or not (false).
  365  *
  366  */
  367 gboolean
  368 gtk_databox_graph_get_hide (GtkDataboxGraph * graph)
  369 {
  370   g_return_val_if_fail (GTK_DATABOX_IS_GRAPH (graph), -1);
  371   GtkDataboxGraphPrivate *priv = gtk_databox_graph_get_instance_private(graph);
  372   return priv->hide;
  373 }