"Fossies" - the Fresh Open Source Software Archive 
Member "gtkdatabox-1.0.0/examples/addremove.c" (31 Mar 2021, 6208 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.
See also the latest
Fossies "Diffs" side-by-side code changes report for "addremove.c":
0.9.3.1_vs_1.0.0.
1 /* $Id: addremove.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 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22
23 #include <gtk/gtk.h>
24 #include <gtkdatabox.h>
25 #include <gtkdatabox_points.h>
26 #include <math.h>
27
28 #define POINTS 200
29
30 GtkDataboxGraph **graphs;
31
32 /*----------------------------------------------------------------
33 * databox addremove
34 *----------------------------------------------------------------*/
35 void
36 add_data (GtkDatabox * box, gint index)
37 {
38 GdkRGBA color;
39 gint add_index;
40
41 color.red = 0.5 + 0.5 * (index + 1) / 10;
42 color.green = 1 - 0.5 * index / 10;
43 color.blue = 1;
44 color.alpha = 1;
45
46 gtk_databox_graph_set_color (graphs[index], &color);
47
48 add_index = gtk_databox_graph_add (GTK_DATABOX (box), graphs[index]);
49
50 g_print ("Added graph has index: %d\n", add_index);
51 }
52
53 static gboolean
54 addremove_func (GtkDatabox * box)
55 {
56 static gboolean remove = TRUE;
57 gfloat min_x, max_x;
58 gfloat min_y, max_y;
59 static gint index;
60 static gint hide;
61
62 if (remove)
63 {
64 index = (int) (10. * rand () / (RAND_MAX + 1.0));
65 hide = (int) (2. * rand () / (RAND_MAX + 1.0));
66
67 if (!hide)
68 {
69 g_print ("removing %d\n", index);
70 gtk_databox_graph_remove (box, graphs[index]);
71 g_print ("Removed: %d\n", index);
72 }
73 else
74 {
75 g_print ("hiding %d\n", index);
76 gtk_databox_graph_set_hide (graphs[index], TRUE);
77 }
78 }
79 else
80 {
81 if (!hide)
82 {
83 g_print ("Adding graph %d\n", index);
84 add_data (box, index);
85 }
86 else
87 {
88 g_print ("Showing graph %d\n", index);
89 gtk_databox_graph_set_hide (graphs[index], FALSE);
90
91 }
92 }
93 remove = !remove;
94
95 gtk_databox_auto_rescale (box, 0.05);
96 gtk_databox_calculate_extrema (box, &min_x, &max_x, &min_y, &max_y);
97 g_print ("minX: %10f, minY:%10f, maxX:%10f, maxY:%10f\n", min_x, min_y,
98 max_x, max_y);
99
100 gtk_widget_queue_draw (GTK_WIDGET(box));
101
102 return (TRUE);
103 }
104
105
106 static void
107 create_addremove (void)
108 {
109 GtkWidget *window = NULL;
110 GtkWidget *box1;
111 GtkWidget *box2;
112 GtkWidget *close_button;
113 GtkWidget *box;
114 GtkWidget *table;
115 GtkWidget *label;
116 GtkWidget *separator;
117 GtkDataboxGraph *graph;
118 GdkRGBA color;
119 gfloat *X;
120 gfloat *Y;
121 gint i, j;
122
123 graphs = g_new0 (GtkDataboxGraph *, 10);
124
125 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
126 gtk_widget_set_size_request (window, 400, 400);
127
128 g_signal_connect (G_OBJECT (window), "destroy",
129 G_CALLBACK (gtk_main_quit), NULL);
130
131 gtk_window_set_title (GTK_WINDOW (window), "GtkDatabox: Basics");
132 gtk_container_set_border_width (GTK_CONTAINER (window), 0);
133
134 box1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
135 gtk_container_add (GTK_CONTAINER (window), box1);
136
137 label =
138 gtk_label_new
139 ("When you click on the \"Add/Remove\" button, a graph will be removed.\nWhen you click again, it is added again.\n\n(This is just for testing add and remove functions)");
140 gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
141 separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
142 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, FALSE, 0);
143
144 /* Create a GtkDatabox widget along with scrollbars and rulers */
145 gtk_databox_create_box_with_scrollbars_and_rulers (&box, &table,
146 TRUE, TRUE, TRUE, TRUE);
147
148 gtk_box_pack_start (GTK_BOX (box1), table, TRUE, TRUE, 0);
149
150 color.red = 0.33;
151 color.green = 0.33;
152 color.blue = 0.33;
153 color.alpha = 1.0;
154
155 gtk_databox_set_bg_color (GTK_DATABOX (box), "#555555");
156
157 X = g_new0 (gfloat, POINTS);
158
159 for (i = 0; i < POINTS; i++)
160 {
161 X[i] = i * 5;
162 }
163 for (i = 0; i < 10; i++)
164 {
165 Y = g_new0 (gfloat, POINTS);
166 for (j = 0; j < POINTS; j++)
167 {
168 Y[j] = i * 100.0 * sin (i * j / 50.);
169 }
170 graphs[i] = gtk_databox_points_new (POINTS, X, Y, &color, 1);
171 add_data (GTK_DATABOX (box), i);
172 }
173 X = g_new0 (gfloat, 4);
174 Y = g_new0 (gfloat, 4);
175
176 X[0] = -100.;
177 Y[0] = -1100.;
178 X[1] = -100.;
179 Y[1] = +1100.;
180 X[2] = +1100.;
181 Y[2] = -1100.;
182 X[3] = +1100.;
183 Y[3] = +1100.;
184
185 color.red = 1;
186 color.green = 0;
187 color.blue = 0;
188 color.alpha = 1;
189
190 graph = gtk_databox_points_new (4, X, Y, &color, 3);
191 gtk_databox_graph_add (GTK_DATABOX (box), graph);
192
193 gtk_databox_auto_rescale (GTK_DATABOX (box), 0.05);
194
195 separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
196 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
197
198 box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
199 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
200 gtk_box_pack_end (GTK_BOX (box1), box2, FALSE, TRUE, 0);
201 close_button = gtk_button_new_with_label ("Add/Remove");
202 g_signal_connect_swapped (G_OBJECT (close_button), "clicked",
203 G_CALLBACK (addremove_func), G_OBJECT (box));
204 gtk_box_pack_start (GTK_BOX (box2), close_button, TRUE, TRUE, 0);
205
206 close_button = gtk_button_new_with_label ("Close");
207 g_signal_connect_swapped (G_OBJECT (close_button), "clicked",
208 G_CALLBACK (gtk_main_quit), G_OBJECT (box));
209 gtk_box_pack_start (GTK_BOX (box2), close_button, TRUE, TRUE, 0);
210 gtk_widget_set_can_default(close_button, TRUE);
211 gtk_widget_grab_default (close_button);
212
213 gtk_widget_show_all (window);
214 }
215
216 gint
217 main (gint argc, char *argv[])
218 {
219 gtk_init (&argc, &argv);
220
221 create_addremove ();
222 gtk_main ();
223
224 return 0;
225 }