"Fossies" - the Fresh Open Source Software Archive

Member "gtkdatabox-1.0.0/gtk/gtkdatabox_regions.c" (31 Mar 2021, 8797 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_regions.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_regions.c 4 2008-06-22 09:19:11Z rbock $ */
    2 /* GtkDatabox - An extension to the gtk+ library
    3  * Copyright (C) 2011 - 2012  Dr. Matt Flax <flatmax@>
    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_regions.h>
   21 
   22 static void gtk_databox_regions_real_draw (GtkDataboxGraph * regions,
   23                     GtkDatabox* box);
   24 
   25 /**
   26  * GtkDataboxRegionsPrivate
   27  *
   28  * A private data structure used by the #GtkDataboxRegions. It shields all internal things
   29  * from developers who are just using the object.
   30  *
   31  **/
   32 typedef struct _GtkDataboxRegionsPrivate GtkDataboxRegionsPrivate;
   33 
   34 struct _GtkDataboxRegionsPrivate
   35 {
   36    gint16 *xpixels;
   37    gint16 *y1pixels;
   38    gint16 *y2pixels;
   39    guint pixelsalloc;
   40 };
   41 
   42 G_DEFINE_TYPE_WITH_PRIVATE(GtkDataboxRegions, gtk_databox_regions,
   43     GTK_DATABOX_TYPE_XYYC_GRAPH)
   44 
   45 static void
   46 regions_finalize (GObject * object)
   47 {
   48    GtkDataboxRegions *regions = GTK_DATABOX_REGIONS (object);
   49    GtkDataboxRegionsPrivate *priv=gtk_databox_regions_get_instance_private(regions);
   50    g_free (priv->xpixels);
   51    g_free (priv->y1pixels);
   52    g_free (priv->y2pixels);
   53 
   54    /* Chain up to the parent class */
   55    G_OBJECT_CLASS (gtk_databox_regions_parent_class)->finalize (object);
   56 }
   57 
   58 static void
   59 gtk_databox_regions_class_init (GtkDataboxRegionsClass *klass )
   60 {
   61    GtkDataboxGraphClass *graph_class = GTK_DATABOX_GRAPH_CLASS (klass);
   62    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
   63 
   64    gobject_class->finalize = regions_finalize;
   65 
   66    graph_class->draw = gtk_databox_regions_real_draw;
   67 }
   68 
   69 static void
   70 gtk_databox_regions_complete (GtkDataboxRegions * regions)
   71 {
   72    GtkDataboxRegionsPrivate *priv=gtk_databox_regions_get_instance_private(regions);
   73 
   74    priv->xpixels = NULL;
   75    priv->y1pixels = NULL;
   76    priv->y2pixels = NULL;
   77    priv->pixelsalloc = 0;
   78 }
   79 
   80 static void
   81 gtk_databox_regions_init (GtkDataboxRegions *regions)
   82 {
   83    g_signal_connect (regions, "notify::length",
   84              G_CALLBACK (gtk_databox_regions_complete), NULL);
   85 }
   86 
   87 /**
   88  * gtk_databox_regions_new:
   89  * @len: length of @X, @Y1  and @Y2
   90  * @X: array of ordinates
   91  * @Y1: array of co-ordinates
   92  * @Y2: array of co-ordinates
   93  * @color: color of the markers
   94  *
   95  * Creates a new #GtkDataboxRegions object which can be added to a #GtkDatabox widget
   96  *
   97  * Return value: A new #GtkDataboxRegions object
   98  **/
   99 GtkDataboxGraph *
  100 gtk_databox_regions_new (guint len, gfloat * X, gfloat * Y1, gfloat * Y2, GdkRGBA * color)
  101 {
  102    GtkDataboxRegions *regions;
  103    g_return_val_if_fail (X, NULL);
  104    g_return_val_if_fail (Y1, NULL);
  105    g_return_val_if_fail (Y2, NULL);
  106    g_return_val_if_fail ((len > 0), NULL);
  107 
  108    regions = g_object_new (GTK_DATABOX_TYPE_REGIONS,
  109             "X-Values", X,
  110             "Y1-Values", Y1,
  111             "Y2-Values", Y2,
  112             "xstart", 0,
  113             "y1start", 0,
  114             "y2start", 0,
  115             "xstride", 1,
  116             "y1stride", 1,
  117             "y2stride", 1,
  118             "xtype", G_TYPE_FLOAT,
  119             "ytype", G_TYPE_FLOAT,
  120             "length", len,
  121             "maxlen", len,
  122             "color", color,NULL);
  123 
  124    return GTK_DATABOX_GRAPH (regions);
  125 }
  126 
  127 /**
  128  * gtk_databox_regions_new_full:
  129  * @maxlen: maximum length of @X and @Y
  130  * @len: actual number of @X and @Y values to plot
  131  * @X: array of ordinates
  132  * @Y1: array of co-ordinates
  133  * @Y2: array of co-ordinates
  134  * @xstart: the first element in the X array to plot (usually 0)
  135  * @y1start: the first element in the Y1 array to plot (usually 0)
  136  * @y2start: the first element in the Y2 array to plot (usually 0)
  137  * @xstride: successive elements in the X array are separated by this much (1 if array, ncols if matrix)
  138  * @y1stride: successive elements in the Y1 array are separated by this much (1 if array, ncols if matrix)
  139  * @y2stride: successive elements in the Y2 array are separated by this much (1 if array, ncols if matrix)
  140  * @xtype: the GType of the X array elements.  G_TYPE_FLOAT, G_TYPE_DOUBLE, etc.
  141  * @ytype: the GType of the Y1/Y2 array elements.  G_TYPE_FLOAT, G_TYPE_DOUBLE, etc.
  142  * @color: color of the markers
  143  *
  144  * Creates a new #GtkDataboxRegions object which can be added to a #GtkDatabox widget
  145  *
  146  * Return value: A new #GtkDataboxRegions object
  147  **/
  148 GtkDataboxGraph *
  149 gtk_databox_regions_new_full (guint maxlen, guint len,
  150             void * X, guint xstart, guint xstride, GType xtype,
  151             void * Y1, guint y1start, guint y1stride,
  152             void * Y2, guint y2start, guint y2stride, GType ytype,
  153             GdkRGBA * color)
  154 {
  155    GtkDataboxRegions *regions;
  156    g_return_val_if_fail (X, NULL);
  157    g_return_val_if_fail (Y1, NULL);
  158    g_return_val_if_fail (Y2, NULL);
  159    g_return_val_if_fail ((len > 0), NULL);
  160 
  161    regions = g_object_new (GTK_DATABOX_TYPE_REGIONS,
  162             "X-Values", X,
  163             "Y1-Values", Y1,
  164             "Y2-Values", Y2,
  165             "xstart", xstart,
  166             "y1start", y1start,
  167             "y2start", y2start,
  168             "xstride", xstride,
  169             "y1stride", y1stride,
  170             "y2stride", y2stride,
  171             "xtype", xtype,
  172             "ytype", ytype,
  173             "length", len,
  174             "maxlen", maxlen,
  175             "color", color,NULL);
  176 
  177    return GTK_DATABOX_GRAPH (regions);
  178 }
  179 
  180 static void
  181 gtk_databox_regions_real_draw (GtkDataboxGraph * graph,
  182                 GtkDatabox* box)
  183 {
  184    GtkDataboxRegions *regions = GTK_DATABOX_REGIONS (graph);
  185    GtkDataboxRegionsPrivate *priv=gtk_databox_regions_get_instance_private(regions);
  186    GdkPoint data[4];
  187    guint i = 0;
  188    void *X;
  189    void *Y1;
  190    void *Y2;
  191    guint len, maxlen;
  192    cairo_t *cr;
  193    gint16 *xpixels, *y1pixels, *y2pixels;
  194    guint xstart, xstride, y1start, y1stride, y2start, y2stride;
  195    GType xtype, ytype;
  196 
  197    g_return_if_fail (GTK_DATABOX_IS_REGIONS (regions));
  198    g_return_if_fail (GTK_IS_DATABOX (box));
  199 
  200    if (gtk_databox_get_scale_type_y (box) == GTK_DATABOX_SCALE_LOG)
  201       g_warning
  202      ("gtk_databox_regions do not work well with logarithmic scale in Y axis");
  203 
  204    len = gtk_databox_xyyc_graph_get_length (GTK_DATABOX_XYYC_GRAPH (graph));
  205    maxlen = gtk_databox_xyyc_graph_get_maxlen (GTK_DATABOX_XYYC_GRAPH (graph));
  206 
  207    if (priv->pixelsalloc < len)
  208    {
  209     priv->pixelsalloc = len;
  210     priv->xpixels = (gint16 *)g_realloc(priv->xpixels, len * sizeof(gint16));
  211     priv->y1pixels = (gint16 *)g_realloc(priv->y1pixels, len * sizeof(gint16));
  212     priv->y2pixels = (gint16 *)g_realloc(priv->y2pixels, len * sizeof(gint16));
  213    }
  214 
  215    xpixels = priv->xpixels;
  216    y1pixels = priv->y1pixels;
  217    y2pixels = priv->y2pixels;
  218 
  219    X = gtk_databox_xyyc_graph_get_X (GTK_DATABOX_XYYC_GRAPH (graph));
  220    xstart = gtk_databox_xyyc_graph_get_xstart (GTK_DATABOX_XYYC_GRAPH (graph));
  221    xstride = gtk_databox_xyyc_graph_get_xstride (GTK_DATABOX_XYYC_GRAPH (graph));
  222    xtype = gtk_databox_xyyc_graph_get_xtype (GTK_DATABOX_XYYC_GRAPH (graph));
  223    gtk_databox_values_to_xpixels(box, xpixels, X, xtype, maxlen, xstart, xstride, len);
  224 
  225    ytype = gtk_databox_xyyc_graph_get_ytype (GTK_DATABOX_XYYC_GRAPH (graph));
  226    Y1 = gtk_databox_xyyc_graph_get_Y1 (GTK_DATABOX_XYYC_GRAPH (graph));
  227    y1start = gtk_databox_xyyc_graph_get_y1start (GTK_DATABOX_XYYC_GRAPH (graph));
  228    y1stride = gtk_databox_xyyc_graph_get_y1stride (GTK_DATABOX_XYYC_GRAPH (graph));
  229    gtk_databox_values_to_ypixels(box, y1pixels, Y1, ytype, maxlen, y1start, y1stride, len);
  230 
  231    Y2 = gtk_databox_xyyc_graph_get_Y2 (GTK_DATABOX_XYYC_GRAPH (graph));
  232    y2start = gtk_databox_xyyc_graph_get_y2start (GTK_DATABOX_XYYC_GRAPH (graph));
  233    y2stride = gtk_databox_xyyc_graph_get_y2stride (GTK_DATABOX_XYYC_GRAPH (graph));
  234    gtk_databox_values_to_ypixels(box, y2pixels, Y2, ytype, maxlen, y2start, y2stride, len);
  235 
  236    cr = gtk_databox_graph_create_gc (graph, box);
  237 
  238    data[2].x = *xpixels;
  239    data[2].y = *y2pixels;
  240    data[3].x = *xpixels;
  241    data[3].y = *y1pixels;
  242    xpixels++; y1pixels++; y2pixels++;
  243    for (i = 0; i < len-1; i++, xpixels++, y1pixels++, y2pixels++)
  244    {
  245       data[0].x = data[2].x; /* 4 points in the polygon */
  246       data[0].y = data[2].y;
  247       data[1].x = data[3].x;
  248       data[1].y = data[3].y;
  249       data[2].x = *xpixels;
  250       data[2].y = *y2pixels;
  251       data[3].x = *xpixels;
  252       data[3].y = *y1pixels;
  253       cairo_move_to(cr, data[1].x, data[1].y);
  254       cairo_line_to(cr, data[0].x, data[0].y);
  255       cairo_line_to(cr, data[2].x, data[2].y);
  256       cairo_line_to(cr, data[3].x, data[3].y);
  257       cairo_close_path  (cr);
  258       cairo_fill(cr);
  259    }
  260    cairo_destroy(cr);
  261 
  262    return;
  263 }