File: cmd.c

package info (click to toggle)
gwave 0.0.4-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,216 kB
  • ctags: 429
  • sloc: ansic: 4,514; makefile: 120; sh: 17
file content (355 lines) | stat: -rw-r--r-- 9,005 bytes parent folder | download
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * cmd.c, part of the gwave waveform viewer tool
 *
 * Functions in this file implement basic user-interface functionality.
 * Later they can become callable from the extension language
 * when we add one (most will will require some glue).
 *
 * Copyright (C) 1998  University of North Carolina at Chapel Hill
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Log: cmd.c,v $
 * Revision 1.3  1998/09/30 21:54:39  tell
 * Restructure zoom commands, creating cmd_zoom_absolute and cmd_zoom_relative
 * to factor out common code.  Add cmd_zoom_cursor and cmd_zoom_window.
 * Add supress_redraw hook for use in update_wavetable to keep things from
 * getting redrawn before we're ready when adding first wave.
 *
 * Revision 1.2  1998/09/17 18:31:58  tell
 * Fixed longstanding bug deleting multiple waves.
 * update scrollbar if min_xval/max_xval change.
 *
 * Revision 1.1  1998/09/01 21:28:02  tell
 * Initial revision
 *
 */

#include <ctype.h>
#include <math.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>

#include <gtk/gtk.h>

#include "reader.h"
#include "gwave.h"

gint cmd_zoom_absolute(double start, double end)
{
	if(start <= end) {
		wtable->start_xval = start;
		wtable->end_xval = end;
	} else {
		wtable->start_xval = end;
		wtable->end_xval = start;
	}

	if(wtable->start_xval < wtable->min_xval)
		wtable->start_xval = wtable->min_xval;
	if(wtable->end_xval > wtable->max_xval)
		wtable->end_xval = wtable->max_xval;

	win_hsadj->page_size = fabs(wtable->end_xval - wtable->start_xval);
	win_hsadj->page_increment = win_hsadj->page_size/2;
	win_hsadj->step_increment = win_hsadj->page_size/100;
	win_hsadj->value = wtable->start_xval;
	win_hsadj->lower = wtable->min_xval;
	win_hsadj->upper = wtable->max_xval;

	gtk_signal_emit_by_name(GTK_OBJECT(win_hsadj), "changed");
	gtk_signal_emit_by_name(GTK_OBJECT(win_hsadj), "value_changed");

	return 0;
}

gint cmd_zoom_relative(double factor)
{
	double ocenter, owidth;
	g_assert(factor > DBL_EPSILON);

	ocenter = (wtable->start_xval + wtable->end_xval)/2;
	owidth = wtable->end_xval - wtable->start_xval;
	
	return cmd_zoom_absolute(ocenter - owidth / (factor * 2),
				 ocenter + owidth / (factor * 2));
}

gint cmd_zoom_in(GtkWidget *widget)
{
	return cmd_zoom_relative(2);
}

gint cmd_zoom_out(GtkWidget *widget)
{
	return cmd_zoom_relative(0.5);
}

gint cmd_zoom_full(GtkWidget *widget)
{
	return cmd_zoom_absolute(wtable->min_xval, wtable->max_xval);
}

gint cmd_zoom_cursors(GtkWidget *widget)
{
	if(!wtable->cursor[0]->shown)
		return 0;
	if(!wtable->cursor[1]->shown)
		return 0;
	return cmd_zoom_absolute(wtable->cursor[0]->xval,
			   wtable->cursor[1]->xval);
}

static void
zoom_window_finish(WavePanel *wp, int x1, int x2, gpointer data)
{
	double nstart, nend;
	nstart  = x2val(wp, x1);
	nend = x2val(wp, x2);
	cmd_zoom_absolute(nstart, nend);
}

gint cmd_zoom_window(GtkWidget *widget)
{
	select_x_range(zoom_window_finish, NULL);
	return 0;
}


typedef struct {
	WavePanel *wp;
	VisibleWave *vw;
} VWListItem;

static GList *vw_delete_list;

static void
vw_wp_list_if_selected(gpointer p, gpointer d)
{
	VisibleWave *vw = (VisibleWave *)p;
	WavePanel *wp = (WavePanel *)d;
	GtkToggleButton *btn = GTK_TOGGLE_BUTTON(vw->button);
	
	if(btn->active) {
		VWListItem *vdi = g_new(VWListItem, 1);
		vdi->wp = wp;
		vdi->vw = vw;
		vw_delete_list = g_list_append(vw_delete_list, vdi);
	}
}

/*
 * cmd_delete_selected_waves
 */
gint cmd_delete_selected_waves(GtkWidget *widget)
{
	int i;
	VWListItem *vdi;

	/* 
	 * Have to build a special list while traversing, and then do the
	 * deletes, because removing elements from the wp->vwlist 
	 * during its g_list_foreach is apparently a no-no.
	 */
	for(i = 0; i < wtable->npanels; i++) {
		WavePanel *wp = &wtable->panels[i];
		g_list_foreach(wp->vwlist, vw_wp_list_if_selected, wp);
	}
	while((vdi = g_list_nth_data(vw_delete_list, 0)) != NULL) {
		remove_wave_from_panel(vdi->wp, vdi->vw);
		vw_delete_list = g_list_remove(vw_delete_list, vdi);
		g_free(vdi);
	}
	
	return 0;
}

/*
 * remove waveform from panel
 * 	Somthing bad will probably happen if the waveform isn't actually
 * 	in the indicated panel.
 */
void
remove_wave_from_panel(WavePanel *wp, VisibleWave *vw)
{

	wp->vwlist = g_list_remove(wp->vwlist, vw);

	gtk_widget_destroy(vw->button);

	gdk_gc_destroy(vw->gc);
	g_free(vw);

	wavepanel_update_data(wp);
	wavetable_update_data();
}

/*
 * Add a waveform to a WavePanel
 */
void
add_var_to_panel(WavePanel *wp, DVar *dv)
{
	VisibleWave *vw;

	vw = g_new0(VisibleWave, 1);
	vw->var = dv;
	vw->colorn = wp->nextcolor;
	wp->nextcolor = (wp->nextcolor + 1)%NWColors;

	wp->vwlist = g_list_append(wp->vwlist, vw);
	wavepanel_update_data(wp);
	wavetable_update_data();

	if(wp->lvbox)  /* add button to Y-label box */
		vw_wp_create_button(vw, wp);
	if(wp->drawing) {
/*		vw_wp_setup_gc(vw, wp); */
		vw_wp_visit_draw(vw, wp);
	}
}

/*
 * called with g_list_foreach to update a WavePanel from all of its
 * VisibleWaves.
 */
static void
vw_wp_visit_update_data(gpointer p, gpointer d)
{
	VisibleWave *vw = (VisibleWave *)p;
	WavePanel *wp = (WavePanel *)d;

	if(vw->var->iv->d.min < wp->min_xval)
		wp->min_xval = vw->var->iv->d.min;
	if(vw->var->iv->d.max > wp->max_xval)
		wp->max_xval = vw->var->iv->d.max;

	if(vw->var->d.min < wp->min_yval)
		wp->min_yval = vw->var->d.min;
	if(vw->var->d.max > wp->max_yval)
		wp->max_yval = vw->var->d.max;
}

/*
 * wavepanel_update_data
 *   update wavepanel values that sumarize things over all of the 
 *   VisibleWaves in the panel.
 */
void
wavepanel_update_data(WavePanel *wp)
{
	char lbuf[128];

	wp->min_xval = G_MAXDOUBLE;
	wp->max_xval = G_MINDOUBLE;
	wp->min_yval = G_MAXDOUBLE;
	wp->max_yval = G_MINDOUBLE;
	g_list_foreach(wp->vwlist, vw_wp_visit_update_data, (gpointer)wp);

	/* set to something reasonable if they didn't change,
	 * like if the panel was empty
	 */
	if(wp->min_xval == G_MAXDOUBLE)
		wp->min_xval = wtable->min_xval;
	if(wp->max_xval == G_MINDOUBLE)
		wp->max_xval = wtable->max_xval;
	if(wp->min_yval == G_MAXDOUBLE)
		wp->min_yval = 0.0;
	if(wp->max_yval == G_MINDOUBLE)
		wp->max_yval = 3.3;

	/* zero height? set to +- 0.1%  so a line is visible in the center */
	if((wp->max_yval - wp->min_yval) < DBL_EPSILON) {
		wp->max_yval *= 1.001;
		wp->min_yval *= 0.999;
	}

	/* if start & end were the same, try updating them
	 * -- this probably isn't quite right.
	 */
	if(fabs(wp->end_xval - wp->start_xval) < DBL_EPSILON) {
		wp->start_xval = wp->min_xval;
		wp->end_xval = wp->max_xval;
	}

	/* Update y-axis labels */
	if(wp->lab_min) {
		sprintf(lbuf, "%.3f", wp->min_yval);
		gtk_label_set(GTK_LABEL(wp->lab_min), lbuf);
	}
	if(wp->lab_max) {
		sprintf(lbuf, "%.3f", wp->max_yval);
		gtk_label_set(GTK_LABEL(wp->lab_max), lbuf);
	}
}

/* Update parameters in wavetable that depend on all panels */
void
wavetable_update_data()
{
	int i;
	WavePanel *wp;
	double old_min_x, old_max_x;
	old_min_x = wtable->min_xval;
	old_max_x = wtable->max_xval;

	wtable->min_xval = G_MAXDOUBLE;
	wtable->max_xval = G_MINDOUBLE;
	for(i = 0; i < wtable->npanels; i++) {
		wp = &wtable->panels[i];
		if(wp->vwlist == NULL)
			continue; /* no waves? min/max for panel are bogus */

		if(wp->min_xval < wtable->min_xval)
			wtable->min_xval = wp->min_xval;
		if(wp->max_xval > wtable->max_xval)
			wtable->max_xval = wp->max_xval;
	}
	/* still nothing? set back to zero */
	if(wtable->min_xval == G_MAXDOUBLE)
		wtable->min_xval = 0;
	if(wtable->max_xval == G_MINDOUBLE)
		wtable->max_xval = 0;

	/* if start & end were the same, try updating them so we can
	* see somthing.
	*/
	if(fabs(wtable->end_xval - wtable->start_xval) < DBL_EPSILON &&
		win_hsadj != NULL) {
		wtable->suppress_redraw = 1;
		cmd_zoom_full(NULL);
		wtable->suppress_redraw = 0;
	} else if((wtable->min_xval != old_min_x 
		  || wtable->max_xval != old_max_x) && win_hsadj) {

		/* min/max changed, might have added first (or removed last)
		 * wave from a file with different range.
		 * Keep start/end the same, but update scrollbar.
		 */
		win_hsadj->lower = wtable->min_xval;
		win_hsadj->upper = wtable->max_xval;
		wtable->suppress_redraw = 1;
		gtk_signal_emit_by_name(GTK_OBJECT(win_hsadj), "value_changed");
		wtable->suppress_redraw = 0;

	}
}