"Fossies" - the Fresh Open Source Software Archive

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