[1a1357]: examples / basics_libglade.c Maximize Restore History

Download this file

basics_libglade.c    226 lines (184 with data), 6.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* $Id: basics_libglade.c 4 2008-06-22 09:19:11Z rbock $ */
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/* GtkDatabox - An extension to the gtk+ library
* Copyright (C) 1998 - 2006 Dr. Roland Bock
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <gtk/gtk.h>
#include <glib/gprintf.h>
#include <gtkdatabox.h>
#include <gtkdatabox_points.h>
#include <gtkdatabox_ruler.h>
#include <gtkdatabox_lines.h>
#include <gtkdatabox_bars.h>
#include <gtkdatabox_cross_simple.h>
#include <math.h>
#include <config.h>
#ifdef USE_LIBGLADE
#include <glade/glade.h>
#endif
#define POINTS 2000
#define STEPS 50
#define BARS 25
#define MARKER 10
#ifdef USE_LIBGLADE
/*----------------------------------------------------------------
* databox basics
*----------------------------------------------------------------*/
#define GLADE_FILE "basics_libglade.glade"
static void
create_basics ()
{
GtkWidget *window = NULL;
GtkWidget *box;
GtkWidget *scrollbar;
GtkWidget *ruler;
GtkDataboxGraph *graph;
gfloat min_x, max_x;
gfloat min_y, max_y;
gfloat *X;
gfloat *Y;
gfloat buffer;
GdkRGBA color;
gint i;
GladeXML *gxml;
gxml = glade_xml_new (GLADE_FILE, NULL, NULL);
/* This is important */
glade_xml_signal_autoconnect (gxml);
window = glade_xml_get_widget (gxml, "window");
/* You could associate a scrollbar with a GtkDatabox widget either
* this way ...*/
scrollbar = glade_xml_get_widget (gxml, "scrollbar_x");
box = glade_xml_get_widget (gxml, "box");
gtk_databox_set_adjustment_x (GTK_DATABOX (box),
gtk_range_get_adjustment (GTK_RANGE
(scrollbar)));
/* or this way ... */
scrollbar = glade_xml_get_widget (gxml, "scrollbar_y");
gtk_databox_set_adjustment_y (GTK_DATABOX (box),
gtk_range_get_adjustment (GTK_RANGE
(scrollbar)));
ruler = glade_xml_get_widget (gxml, "ruler_x");
gtk_databox_set_ruler_x (GTK_DATABOX (box), GTK_DATABOX_RULER (ruler));
ruler = glade_xml_get_widget (gxml, "ruler_y");
gtk_databox_set_ruler_y (GTK_DATABOX (box), GTK_DATABOX_RULER (ruler));
/* end of gtk_databox_create_box_with_scrollbars_and_rulers */
color.red = 0.33;
color.green = 0.33;
color.blue = 0.33;
gtk_widget_override_background_color (box, GTK_STATE_FLAG_NORMAL, &color);
X = g_new0 (gfloat, POINTS);
Y = g_new0 (gfloat, POINTS);
for (i = 0; i < POINTS; i++)
{
X[i] = i;
Y[i] = 100. * sin (i * 2 * G_PI / POINTS);
}
color.red = 0;
color.green = 1;
color.blue = 0;
color.alpha = 1;
graph = gtk_databox_points_new (POINTS, X, Y, &color, 1);
gtk_databox_graph_add (GTK_DATABOX (box), graph);
X = g_new0 (gfloat, STEPS);
Y = g_new0 (gfloat, STEPS);
for (i = 0; i < STEPS / 2; i++)
{
X[i * 2] = X[i * 2 + 1] = 2 * (i + .5) * POINTS / STEPS;
if (i > 0)
Y[i * 2 - 1] = 100. * sin ((i * 2) * 2 * G_PI / STEPS);
Y[i * 2] = 100. * sin ((i * 2) * 2 * G_PI / STEPS);
if (i == STEPS / 2 - 1)
Y[i * 2 + 1] = 100. * sin (((i + 1) * 2) * 2 * G_PI / STEPS);
}
color.red = 1;
color.green = 0;
color.blue = 0;
color.alpha = 1;
graph = gtk_databox_lines_new (STEPS, X, Y, &color, 1);
gtk_databox_graph_add (GTK_DATABOX (box), graph);
X = g_new0 (gfloat, BARS);
Y = g_new0 (gfloat, BARS);
for (i = 0; i < BARS; i++)
{
X[i] = i * POINTS / BARS;
Y[i] = 80. * sin (i * 2 * G_PI / BARS);
}
color.red = 0;
color.green = 1;
color.blue = 1;
color.alpha = 1;
graph = gtk_databox_bars_new (BARS, X, Y, &color, 1);
gtk_databox_graph_add (GTK_DATABOX (box), graph);
color.red = 0.5;
color.green = 0.5;
color.blue = 0.5;
graph = gtk_databox_cross_simple_new (&color, 0);
gtk_databox_graph_add (GTK_DATABOX (box), graph);
/* Instead of doing stuff what you see below, you could call
* gtk_databox_auto_rescale (GTK_DATABOX (box), 0.05);
* as is done in the other examples
* Of course, you are more flexible in adjusting the scaling by doing
* it yourself, though. */
if (0 >
gtk_databox_calculate_extrema (GTK_DATABOX (box), &min_x, &max_x,
&min_y, &max_y))
{
g_warning ("Calculating extrema failed. Resorting to default values");
min_x = -100.;
min_y = -100.;
max_x = +100.;
max_y = +100.;
}
else
{
gfloat width = max_x - min_x;
gfloat height = max_y - min_y;
gfloat border = 0.05;
min_x -= border * width;
min_y -= border * height;
max_x += border * width;
max_y += border * height;
}
/* We want larger y values to be higher on the screen */
buffer = min_y;
min_y = max_y;
max_y = buffer;
gtk_databox_set_total_limits (GTK_DATABOX (box), min_x, max_x, min_y,
max_y);
/* end of gtk_databox_auto_rescale */
gtk_widget_show (window);
gdk_window_set_cursor (box->window, gdk_cursor_new (GDK_CROSS));
}
gint
main (gint argc, char *argv[])
{
gtk_init (&argc, &argv);
create_basics ();
gtk_main ();
return 0;
}
#else /* USE_LIBGLADE */
/* Build some dummy program if libglade is not used. */
gint
main ()
{
g_printf
("This program does nothing useful unless you call the configure script with --enable-libglade\n");
return 0;
}
#endif /* USE_LIBGLADE */