"Fossies" - the Fresh Open Source Software Archive

Member "gtkdatabox-1.0.0/gtk/gtkdatabox_bars.c" (31 Mar 2021, 7350 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_bars.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_bars.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_bars.h>
   21 
   22 static void gtk_databox_bars_real_draw (GtkDataboxGraph * bars,
   23                     GtkDatabox* box);
   24 /**
   25  * GtkDataboxBarsPrivate
   26  *
   27  * A private data structure used by the #GtkDataboxBars. It shields all internal things
   28  * from developers who are just using the object.
   29  *
   30  **/
   31 typedef struct _GtkDataboxBarsPrivate GtkDataboxBarsPrivate;
   32 
   33 struct _GtkDataboxBarsPrivate
   34 {
   35    gint16 *xpixels;
   36    gint16 *ypixels;
   37    guint pixelsalloc;
   38 };
   39 
   40 G_DEFINE_TYPE_WITH_PRIVATE(GtkDataboxBars, gtk_databox_bars,
   41     GTK_DATABOX_TYPE_XYC_GRAPH)
   42 
   43 static void
   44 bars_finalize (GObject * object)
   45 {
   46    GtkDataboxBars *bars = GTK_DATABOX_BARS (object);
   47    GtkDataboxBarsPrivate *priv=gtk_databox_bars_get_instance_private(bars);
   48 
   49    g_free (priv->xpixels);
   50    g_free (priv->ypixels);
   51 
   52    /* Chain up to the parent class */
   53    G_OBJECT_CLASS (gtk_databox_bars_parent_class)->finalize (object);
   54 }
   55 
   56 static void
   57 gtk_databox_bars_class_init (GtkDataboxBarsClass *klass)
   58 {
   59    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
   60    GtkDataboxGraphClass *graph_class = GTK_DATABOX_GRAPH_CLASS (klass);
   61 
   62    gobject_class->finalize = bars_finalize;
   63 
   64    graph_class->draw = gtk_databox_bars_real_draw;
   65 }
   66 
   67 static void
   68 gtk_databox_bars_complete (GtkDataboxBars * bars)
   69 {
   70    GtkDataboxBarsPrivate *priv=gtk_databox_bars_get_instance_private(bars);
   71    priv->xpixels = NULL;
   72    priv->ypixels = NULL;
   73    priv->pixelsalloc = 0;
   74 }
   75 
   76 static void
   77 gtk_databox_bars_init (GtkDataboxBars *bars)
   78 {
   79    g_signal_connect (bars, "notify::length",
   80              G_CALLBACK (gtk_databox_bars_complete), NULL);
   81 }
   82 
   83 /**
   84  * gtk_databox_bars_new:
   85  * @len: length of @X and @Y
   86  * @X: array of horizontal position values of markers
   87  * @Y: array of vertical position values of markers
   88  * @color: color of the markers
   89  * @size: marker size or line width (depending on the @type)
   90  *
   91  * Creates a new #GtkDataboxBars object which can be added to a #GtkDatabox widget
   92  *
   93  * Return value: A new #GtkDataboxBars object
   94  **/
   95 GtkDataboxGraph *
   96 gtk_databox_bars_new (guint len, gfloat * X, gfloat * Y,
   97               GdkRGBA * color, guint size)
   98 {
   99    GtkDataboxBars *bars;
  100    g_return_val_if_fail (X, NULL);
  101    g_return_val_if_fail (Y, NULL);
  102    g_return_val_if_fail ((len > 0), NULL);
  103 
  104    bars = g_object_new (GTK_DATABOX_TYPE_BARS,
  105             "X-Values", X,
  106             "Y-Values", Y,
  107             "xstart", 0,
  108             "ystart", 0,
  109             "xstride", 1,
  110             "ystride", 1,
  111             "xtype", G_TYPE_FLOAT,
  112             "ytype", G_TYPE_FLOAT,
  113             "length", len,
  114             "maxlen", len,
  115             "color", color, "size", size, NULL);
  116 
  117    return GTK_DATABOX_GRAPH (bars);
  118 }
  119 
  120 /**
  121  * gtk_databox_bars_new_full:
  122  * @maxlen: maximum length of @X and @Y
  123  * @len: actual number of @X and @Y values to plot
  124  * @X: array of horizontal position values of markers
  125  * @Y: array of vertical position values of markers
  126  * @xstart: the first element in the X array to plot (usually 0)
  127  * @ystart: the first element in the Y array to plot (usually 0)
  128  * @xstride: successive elements in the X array are separated by this much (1 if array, ncols if matrix)
  129  * @ystride: successive elements in the Y array are separated by this much (1 if array, ncols if matrix)
  130  * @xtype: the GType of the X array elements.  G_TYPE_FLOAT, G_TYPE_DOUBLE, etc.
  131  * @ytype: the GType of the Y array elements.  G_TYPE_FLOAT, G_TYPE_DOUBLE, etc.
  132  * @color: color of the markers
  133  * @size: marker size or line width (depending on the @type)
  134  *
  135  * Creates a new #GtkDataboxBars object which can be added to a #GtkDatabox widget
  136  *
  137  * Return value: A new #GtkDataboxBars object
  138  **/
  139 GtkDataboxGraph *
  140 gtk_databox_bars_new_full (guint maxlen, guint len,
  141             void * X, guint xstart, guint xstride, GType xtype,
  142             void * Y, guint ystart, guint ystride, GType ytype,
  143             GdkRGBA * color, guint size)
  144 {
  145    GtkDataboxBars *bars;
  146    g_return_val_if_fail (X, NULL);
  147    g_return_val_if_fail (Y, NULL);
  148    g_return_val_if_fail ((len > 0), NULL);
  149 
  150    bars = g_object_new (GTK_DATABOX_TYPE_BARS,
  151             "X-Values", X,
  152             "Y-Values", Y,
  153             "xstart", xstart,
  154             "ystart", ystart,
  155             "xstride", xstride,
  156             "ystride", ystride,
  157             "xtype", xtype,
  158             "ytype", ytype,
  159             "length", len,
  160             "maxlen", maxlen,
  161             "color", color, "size", size, NULL);
  162 
  163    return GTK_DATABOX_GRAPH (bars);
  164 }
  165 
  166 static void
  167 gtk_databox_bars_real_draw (GtkDataboxGraph * graph,
  168                 GtkDatabox* box)
  169 {
  170    GtkDataboxBars *bars = GTK_DATABOX_BARS (graph);
  171    GtkDataboxBarsPrivate *priv=gtk_databox_bars_get_instance_private(bars);
  172    guint i = 0;
  173    void *X;
  174    void *Y;
  175    guint len, maxlen;
  176    gint16 zero = 0;
  177    gfloat fzero = 0.0;
  178    cairo_t *cr;
  179    gint16 *xpixels, *ypixels;
  180    guint xstart, xstride, ystart, ystride;
  181    GType xtype, ytype;
  182 
  183    g_return_if_fail (GTK_DATABOX_IS_BARS (bars));
  184    g_return_if_fail (GTK_IS_DATABOX (box));
  185 
  186    if (gtk_databox_get_scale_type_y (box) == GTK_DATABOX_SCALE_LOG)
  187       g_warning
  188      ("gtk_databox_bars do not work well with logarithmic scale in Y axis");
  189 
  190    cr = gtk_databox_graph_create_gc (graph, box);
  191 
  192    len = gtk_databox_xyc_graph_get_length (GTK_DATABOX_XYC_GRAPH (graph));
  193    maxlen = gtk_databox_xyc_graph_get_maxlen (GTK_DATABOX_XYC_GRAPH (graph));
  194 
  195    if (priv->pixelsalloc < len)
  196    {
  197     priv->pixelsalloc = len;
  198     priv->xpixels = (gint16 *)g_realloc(priv->xpixels, len * sizeof(gint16));
  199     priv->ypixels = (gint16 *)g_realloc(priv->ypixels, len * sizeof(gint16));
  200    }
  201 
  202    xpixels = priv->xpixels;
  203    ypixels = priv->ypixels;
  204 
  205    X = gtk_databox_xyc_graph_get_X (GTK_DATABOX_XYC_GRAPH (graph));
  206    xstart = gtk_databox_xyc_graph_get_xstart (GTK_DATABOX_XYC_GRAPH (graph));
  207    xstride = gtk_databox_xyc_graph_get_xstride (GTK_DATABOX_XYC_GRAPH (graph));
  208    xtype = gtk_databox_xyc_graph_get_xtype (GTK_DATABOX_XYC_GRAPH (graph));
  209    gtk_databox_values_to_xpixels(box, xpixels, X, xtype, maxlen, xstart, xstride, len);
  210 
  211    Y = gtk_databox_xyc_graph_get_Y (GTK_DATABOX_XYC_GRAPH (graph));
  212    ystart = gtk_databox_xyc_graph_get_ystart (GTK_DATABOX_XYC_GRAPH (graph));
  213    ystride = gtk_databox_xyc_graph_get_ystride (GTK_DATABOX_XYC_GRAPH (graph));
  214    ytype = gtk_databox_xyc_graph_get_ytype (GTK_DATABOX_XYC_GRAPH (graph));
  215    gtk_databox_values_to_ypixels(box, ypixels, Y, ytype, maxlen, ystart, ystride, len);
  216 
  217    gtk_databox_values_to_ypixels(box, &zero, &fzero, G_TYPE_FLOAT, 1, 0, 1, 1);
  218 
  219    for (i = 0; i < len; i++, xpixels++, ypixels++)
  220    {
  221       cairo_move_to (cr, *xpixels + 0.5, zero + 0.5);
  222       cairo_line_to (cr, *xpixels + 0.5, *ypixels + 0.5);
  223    }
  224    cairo_stroke(cr);
  225    cairo_destroy(cr);
  226 
  227    return;
  228 }
  229