"Fossies" - the Fresh Open Source Software Archive 
Member "gtkdatabox-1.0.0/gtk/gtkdatabox_offset_bars.c" (31 Mar 2021, 8745 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_offset_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_offset_bars.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_offset_bars.h>
21
22 static void gtk_databox_offset_bars_real_draw (GtkDataboxGraph * bars,
23 GtkDatabox* box);
24
25 /**
26 * GtkDataboxOffsetBarsPrivate
27 *
28 * A private data structure used by the #GtkDataboxOffsetBars. It shields all internal things
29 * from developers who are just using the object.
30 *
31 **/
32 typedef struct _GtkDataboxOffsetBarsPrivate GtkDataboxOffsetBarsPrivate;
33
34 struct _GtkDataboxOffsetBarsPrivate
35 {
36 gint16 *xpixels;
37 gint16 *y1pixels;
38 gint16 *y2pixels;
39 guint pixelsalloc;
40 };
41
42 G_DEFINE_TYPE_WITH_PRIVATE(GtkDataboxOffsetBars, gtk_databox_offset_bars,
43 GTK_DATABOX_TYPE_XYYC_GRAPH)
44
45 static void
46 bars_finalize (GObject * object)
47 {
48 GtkDataboxOffsetBars *bars = GTK_DATABOX_OFFSET_BARS (object);
49 GtkDataboxOffsetBarsPrivate *priv = gtk_databox_offset_bars_get_instance_private(bars);
50
51 g_free (priv->xpixels);
52 g_free (priv->y1pixels);
53 g_free (priv->y2pixels);
54
55 /* Chain up to the parent class */
56 G_OBJECT_CLASS (gtk_databox_offset_bars_parent_class)->finalize (object);
57 }
58
59 static void
60 gtk_databox_offset_bars_class_init (GtkDataboxOffsetBarsClass *klass)
61 {
62 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
63 GtkDataboxGraphClass *graph_class = GTK_DATABOX_GRAPH_CLASS (klass);
64
65 gobject_class->finalize = bars_finalize;
66
67 graph_class->draw = gtk_databox_offset_bars_real_draw;
68 }
69
70 static void
71 gtk_databox_offset_bars_complete (GtkDataboxOffsetBars * bars)
72 {
73 GtkDataboxOffsetBarsPrivate *priv = gtk_databox_offset_bars_get_instance_private(bars);
74
75 priv->xpixels = NULL;
76 priv->y1pixels = NULL;
77 priv->y2pixels = NULL;
78 priv->pixelsalloc = 0;
79 }
80
81 static void
82 gtk_databox_offset_bars_init (GtkDataboxOffsetBars *bars)
83 {
84 g_signal_connect (bars, "notify::length",
85 G_CALLBACK (gtk_databox_offset_bars_complete), NULL);
86 }
87
88 /**
89 * gtk_databox_offset_bars_new:
90 * @len: length of @X, @Y1 and @Y2
91 * @X: array of horizontal position values of markers
92 * @Y1: array of starting vertical position values of markers
93 * @Y2: array of ending vertical position values of markers
94 * @color: color of the markers
95 * @size: marker size or line width (depending on the @type)
96 *
97 * Creates a new #GtkDataboxOffsetBars object which can be added to a #GtkDatabox widget
98 *
99 * Return value: A new #GtkDataboxOffsetBars object
100 **/
101 GtkDataboxGraph *
102 gtk_databox_offset_bars_new (guint len, gfloat * X, gfloat * Y1, gfloat * Y2,
103 GdkRGBA * color, guint size)
104 {
105 GtkDataboxOffsetBars *bars;
106 g_return_val_if_fail (X, NULL);
107 g_return_val_if_fail (Y1, NULL);
108 g_return_val_if_fail (Y2, NULL);
109 g_return_val_if_fail ((len > 0), NULL);
110
111 bars = g_object_new (GTK_DATABOX_TYPE_OFFSET_BARS,
112 "X-Values", X,
113 "Y1-Values", Y1,
114 "Y2-Values", Y2,
115 "xstart", 0,
116 "y1start", 0,
117 "y2start", 0,
118 "xstride", 1,
119 "y1stride", 1,
120 "y2stride", 1,
121 "xtype", G_TYPE_FLOAT,
122 "ytype", G_TYPE_FLOAT,
123 "length", len,
124 "maxlen", len,
125 "color", color, "size", size, NULL);
126
127 return GTK_DATABOX_GRAPH (bars);
128 }
129
130 /**
131 * gtk_databox_offset_bars_new_full:
132 * @maxlen: maximum length of @X and @Y
133 * @len: actual number of @X and @Y values to plot
134 * @X: array of horizontal position values of markers
135 * @Y1: array of starting vertical position values of markers
136 * @Y2: array of ending vertical position values of markers
137 * @xstart: the first element in the X array to plot (usually 0)
138 * @y1start: the first element in the Y1 array to plot (usually 0)
139 * @y2start: the first element in the Y2 array to plot (usually 0)
140 * @xstride: successive elements in the X array are separated by this much (1 if array, ncols if matrix)
141 * @y1stride: successive elements in the Y1 array are separated by this much (1 if array, ncols if matrix)
142 * @y2stride: successive elements in the Y2 array are separated by this much (1 if array, ncols if matrix)
143 * @xtype: the GType of the X array elements. G_TYPE_FLOAT, G_TYPE_DOUBLE, etc.
144 * @ytype: the GType of the Y1/Y2 array elements. G_TYPE_FLOAT, G_TYPE_DOUBLE, etc.
145 * @color: color of the markers
146 * @size: marker size or line width (depending on the @type)
147 *
148 * Creates a new #GtkDataboxOffsetBars object which can be added to a #GtkDatabox widget
149 *
150 * Return value: A new #GtkDataboxOffsetBars object
151 **/
152 GtkDataboxGraph *
153 gtk_databox_offset_bars_new_full (guint maxlen, guint len,
154 void * X, guint xstart, guint xstride, GType xtype,
155 void * Y1, guint y1start, guint y1stride,
156 void * Y2, guint y2start, guint y2stride, GType ytype,
157 GdkRGBA * color, guint size)
158 {
159 GtkDataboxOffsetBars *bars;
160 g_return_val_if_fail (X, NULL);
161 g_return_val_if_fail (Y1, NULL);
162 g_return_val_if_fail (Y2, NULL);
163 g_return_val_if_fail ((len > 0), NULL);
164
165 bars = g_object_new (GTK_DATABOX_TYPE_OFFSET_BARS,
166 "X-Values", X,
167 "Y1-Values", Y1,
168 "Y2-Values", Y2,
169 "xstart", xstart,
170 "y1start", y1start,
171 "y2start", y2start,
172 "xstride", xstride,
173 "y1stride", y1stride,
174 "y2stride", y2stride,
175 "xtype", xtype,
176 "ytype", ytype,
177 "length", len,
178 "maxlen", maxlen,
179 "color", color, "size", size, NULL);
180
181 return GTK_DATABOX_GRAPH (bars);
182 }
183
184 static void
185 gtk_databox_offset_bars_real_draw (GtkDataboxGraph * graph,
186 GtkDatabox* box)
187 {
188 GtkDataboxOffsetBars *bars = GTK_DATABOX_OFFSET_BARS (graph);
189 GtkDataboxOffsetBarsPrivate *priv = gtk_databox_offset_bars_get_instance_private(bars);
190 guint i = 0;
191 void *X;
192 void *Y1;
193 void *Y2;
194 guint len, maxlen;
195 cairo_t *cr;
196 gint16 *xpixels, *y1pixels, *y2pixels;
197 guint xstart, xstride, y1start, y1stride, y2start, y2stride;
198 GType xtype, ytype;
199
200 g_return_if_fail (GTK_DATABOX_IS_OFFSET_BARS (bars));
201 g_return_if_fail (GTK_IS_DATABOX (box));
202
203 if (gtk_databox_get_scale_type_y (box) == GTK_DATABOX_SCALE_LOG)
204 g_warning
205 ("gtk_databox_offset_bars do not work well with logarithmic scale in Y axis");
206
207 len = gtk_databox_xyyc_graph_get_length (GTK_DATABOX_XYYC_GRAPH (graph));
208 maxlen = gtk_databox_xyyc_graph_get_maxlen (GTK_DATABOX_XYYC_GRAPH (graph));
209
210 if (priv->pixelsalloc < len)
211 {
212 priv->pixelsalloc = len;
213 priv->xpixels = (gint16 *)g_realloc(priv->xpixels, len * sizeof(gint16));
214 priv->y1pixels = (gint16 *)g_realloc(priv->y1pixels, len * sizeof(gint16));
215 priv->y2pixels = (gint16 *)g_realloc(priv->y2pixels, len * sizeof(gint16));
216 }
217
218 xpixels = priv->xpixels;
219 y1pixels = priv->y1pixels;
220 y2pixels = priv->y2pixels;
221
222 X = gtk_databox_xyyc_graph_get_X (GTK_DATABOX_XYYC_GRAPH (graph));
223 xstart = gtk_databox_xyyc_graph_get_xstart (GTK_DATABOX_XYYC_GRAPH (graph));
224 xstride = gtk_databox_xyyc_graph_get_xstride (GTK_DATABOX_XYYC_GRAPH (graph));
225 xtype = gtk_databox_xyyc_graph_get_xtype (GTK_DATABOX_XYYC_GRAPH (graph));
226 gtk_databox_values_to_xpixels(box, xpixels, X, xtype, maxlen, xstart, xstride, len);
227
228 ytype = gtk_databox_xyyc_graph_get_ytype (GTK_DATABOX_XYYC_GRAPH (graph));
229 Y1 = gtk_databox_xyyc_graph_get_Y1 (GTK_DATABOX_XYYC_GRAPH (graph));
230 y1start = gtk_databox_xyyc_graph_get_y1start (GTK_DATABOX_XYYC_GRAPH (graph));
231 y1stride = gtk_databox_xyyc_graph_get_y1stride (GTK_DATABOX_XYYC_GRAPH (graph));
232 gtk_databox_values_to_ypixels(box, y1pixels, Y1, ytype, maxlen, y1start, y1stride, len);
233
234 Y2 = gtk_databox_xyyc_graph_get_Y2 (GTK_DATABOX_XYYC_GRAPH (graph));
235 y2start = gtk_databox_xyyc_graph_get_y2start (GTK_DATABOX_XYYC_GRAPH (graph));
236 y2stride = gtk_databox_xyyc_graph_get_y2stride (GTK_DATABOX_XYYC_GRAPH (graph));
237 gtk_databox_values_to_ypixels(box, y2pixels, Y2, ytype, maxlen, y2start, y2stride, len);
238
239 cr = gtk_databox_graph_create_gc (graph, box);
240
241 for (i = 0; i < len; i++, xpixels++, y1pixels++, y2pixels++)
242 {
243 cairo_move_to (cr, *xpixels + 0.5, *y1pixels + 0.5);
244 cairo_line_to (cr, *xpixels + 0.5, *y2pixels + 0.5);
245 }
246 cairo_stroke(cr);
247 cairo_destroy(cr);
248
249 return;
250 }