-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrip.c
More file actions
281 lines (246 loc) · 7.19 KB
/
Copy pathstrip.c
File metadata and controls
281 lines (246 loc) · 7.19 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* Stripchart -- the gnome-utils stripchart plotting utility
* Copyright (C) 2000 John Kodis <kodis@jagunet.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* 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
* 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.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include "strip.h"
static void
strip_redraw(Strip *strip)
{
GSList *list;
GtkWidget *widget = GTK_WIDGET(strip);
gint width = widget->allocation.width;
gint height = widget->allocation.height;
gdk_draw_rectangle(widget->window,
widget->style->bg_gc[GTK_WIDGET_STATE(widget)], TRUE,
0,0, width, height);
for (list = CHART(strip)->param; list != NULL; list = g_slist_next(list))
{
gint i, x, y0 = 0, points;
ChartDatum *datum = (ChartDatum *)list->data;
gint h = datum->newest;
ChartPlotStyle plot = datum->plot_style;
ChartScaleStyle scale = datum->scale_style;
if (datum->colors == 0)
chart_assign_color(CHART(strip), datum);
if (plot == chart_plot_indicator)
continue;
if (datum->colors == 0)
continue;
x = width - datum->idle - 1;
points = datum->history_count;
for (i = 0; i < points && 0 <= x; i++)
{
gint y = val2gdk(datum->history[h], datum->adj, height, scale);
switch (plot)
{
default:
case chart_plot_point:
gdk_draw_point(widget->window, datum->gdk_gc[0], x, y);
break;
case chart_plot_line:
if (i)
gdk_draw_line(widget->window, datum->gdk_gc[0], x, y, x+1, y0);
y0 = y;
break;
case chart_plot_solid:
gdk_draw_line(widget->window, datum->gdk_gc[0],
x, y, x, val2gdk(0, datum->adj, height, scale));
break;
}
x--;
if (--h < 0)
h = datum->history_size - 1;
}
}
}
static void
strip_update_by_shifting(Strip *strip)
{
GSList *list;
GtkWidget *widget = GTK_WIDGET(strip);
gint width = widget->allocation.width;
gint height = widget->allocation.height;
gdk_draw_drawable(widget->window,
widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
widget->window, 1,0,0,0, width-1,height);
gdk_draw_rectangle(widget->window,
widget->style->bg_gc[GTK_WIDGET_STATE(widget)],
TRUE, width-1,0, 1,height);
for (list = CHART(strip)->param; list; list = g_slist_next(list))
{
gint h, y, y0;
ChartDatum *datum = (ChartDatum *)list->data;
ChartPlotStyle plot = datum->plot_style;
ChartScaleStyle scale = datum->scale_style;
if (datum->history_count == 0)
{
chart_assign_color(CHART(strip), datum);
continue;
}
if (plot == chart_plot_indicator)
continue;
h = datum->newest;
y = val2gdk(datum->history[h], datum->adj, height, scale);
#ifdef DEBUG
printf("plot %p %f (%d, %f...%f) = %d\n", datum, datum->history[h], height, datum->adj->lower, datum->adj->upper, y);
#endif
switch (plot)
{
default:
case chart_plot_point:
gdk_draw_point(widget->window, datum->gdk_gc[0], width, y);
break;
case chart_plot_line:
if (--h < 0)
h = datum->history_size - 1;
y0 = val2gdk(datum->history[h], datum->adj, height, scale);
gdk_draw_line(widget->window,
datum->gdk_gc[0], width-2,y0, width-1,y);
break;
case chart_plot_solid:
y0 = val2gdk(0, datum->adj, height, scale);
gdk_draw_line(widget->window,
datum->gdk_gc[0], width-1,y, width-1,y0);
break;
}
}
}
static void
strip_overlay_ticks(Strip *strip)
{
GtkWidget *widget = GTK_WIDGET(strip);
gint width = widget->allocation.width;
gint height = widget->allocation.height;
gint i, x, yc = height / 2;
#ifdef DRAW_HORIZONTAL_LINE_ON_STRIP
gdk_draw_line(widget->window, widget->style->fg_gc[0], 0,yc, width-1,yc);
#endif
for (i = 0, x = width - 1; x >= 0; x--, i++)
if ((i % strip->minor_ticks) == 0)
{
gint dy = (i % (strip->minor_ticks * strip->major_ticks)) ? 3 : 5;
gdk_draw_line(widget->window, widget->style->fg_gc[0],
x,yc+dy, x,yc-dy);
}
}
static void
strip_overlay_indicators(Strip *strip)
{
GSList *list;
GtkWidget *widget = GTK_WIDGET(strip);
gint indicator_x = 0, indicator_y = 0, indicator_step = 10;
for (list = CHART(strip)->param; list; list = g_slist_next(list))
if (((ChartDatum *)list->data)->plot_style == chart_plot_indicator)
indicator_x += indicator_step;
for (list = CHART(strip)->param; list; list = g_slist_next(list))
{
ChartDatum *datum = (ChartDatum *)list->data;
ChartPlotStyle plot = datum->plot_style;
if (plot == chart_plot_indicator)
{
gint c = datum->history[datum->newest] + 0.5;
indicator_x -= indicator_step;
if (c > 0)
{
if (c > datum->colors)
c = datum->colors;
gdk_draw_rectangle(widget->window,
widget->style->bg_gc[GTK_WIDGET_STATE(widget)], TRUE,
indicator_x, indicator_y, 1, indicator_step);
gdk_draw_rectangle(
widget->window, datum->gdk_gc[c - 1], TRUE,
indicator_x + 1, indicator_y + 1,
indicator_step - 1, indicator_step - 1);
}
}
}
}
static void
strip_update(Strip *strip)
{
static int show_ticks = 1;
if (!show_ticks)
strip_update_by_shifting(strip);
else
{
strip_redraw(strip);
if (strip->show_ticks)
strip_overlay_ticks(strip);
}
strip_overlay_indicators(strip);
show_ticks = strip->show_ticks;
}
static gboolean
strip_expose(GtkWidget *widget, GdkEventExpose *event, void *nil)
{
Strip *strip = STRIP(widget);
strip_redraw(strip);
if (strip->show_ticks)
strip_overlay_ticks(strip);
strip_overlay_indicators(strip);
return FALSE;
}
static void
strip_configure(GtkWidget *widget, GdkEvent *event, void *nil)
{
Strip *strip = STRIP(widget);
strip_redraw(strip);
if (strip->show_ticks)
strip_overlay_ticks(strip);
}
void
strip_set_ticks(Strip *strip, gint show, gint major, gint minor)
{
strip->show_ticks = show;
if (major)
strip->major_ticks = major;
if (minor)
strip->minor_ticks = minor;
}
void
strip_set_default_history_size(Strip *strip, gint size)
{
CHART(strip)->default_history_size = size;
}
static void
strip_init(Strip *strip)
{
CHART(strip)->default_history_size = 100;
strip_set_ticks(strip, FALSE, 5, 12);
g_signal_connect(strip, "expose_event", G_CALLBACK(strip_expose), NULL);
g_signal_connect(strip, "configure_event", G_CALLBACK(strip_configure), NULL);
g_signal_connect(strip, "chart_post_update", G_CALLBACK(strip_update), NULL);
}
GType
strip_get_type(void)
{
static GType type = 0;
if (type)
return type;
static const GTypeInfo info = {
.class_size = sizeof(StripClass),
.class_init = (GClassInitFunc)NULL,
.instance_size = sizeof(Strip),
.instance_init = (GInstanceInitFunc)strip_init
};
type = g_type_register_static(TYPE_CHART, "Strip", &info, 0);
return type;
}
GtkWidget *
strip_new(void)
{
return gtk_widget_new(TYPE_STRIP, NULL);
}