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