File: chartstrip.c

package info (click to toggle)
libforms 1.0.93sp1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,548 kB
  • ctags: 9,107
  • sloc: ansic: 97,227; sh: 9,236; makefile: 858
file content (227 lines) | stat: -rw-r--r-- 5,223 bytes parent folder | download | duplicates (2)
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
/*
 *  This file is part of XForms.
 *
 *  XForms 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, or
 *  (at your option) any later version.
 *
 *  XForms 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 Lesser General Public
 *  License along with XForms; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
 *  MA 02111-1307, USA.
 */


/*
 * A demo of a moving chart
 *
 *  This file is part of xforms package
 *  M. Overmars and T.C. Zhao   (1997)
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <math.h>
#include "include/forms.h"

long func = 1;
double x = 0.0;
double step = 0.15;

FL_FORM *form;

FL_OBJECT *chartobj,
          *sinobj,
          *exitbut,
          *stepobj;

static void create_form_form(void);


/***************************************
 ***************************************/

static void
set_function( FL_OBJECT * obj  FL_UNUSED_ARG,
			  long        arg )
{
    func = arg;
    fl_clear_chart( chartobj );
    x = 0.0;
}


/***************************************
 ***************************************/

static void
set_step( FL_OBJECT * obj  FL_UNUSED_ARG,
		  long        arg  FL_UNUSED_ARG )
{
	step = fl_get_slider_value( stepobj );
}


/***************************************
 ***************************************/

static double
next_step( void )
{
	double res = 0.0;

	switch ( func)
	{
		case 1:
			res = sin( x );
			break;

		case 2:
			res = sin( 2 * x ) * cos( x );
			break;

		case 3:
			res = sin( 2 * x ) + cos( x );
			break;

		case 4:
			res = sin( 3 * x ) + cos( x );
			break;

		case 5:
			res = sin( x ) * sin( x ) + cos( x );
			break;

		case 6:
			res = sin( x ) * sin( x ) * sin( x );
			break;
    }

    x += 0.2 * step;

    return res;
}


/***************************************
 ***************************************/

int
idle_cb( XEvent * xev  FL_UNUSED_ARG,
		 void   * d    FL_UNUSED_ARG )
{
    fl_insert_chart_value( chartobj, 1, next_step( ), "", 1 );
    return 0;
}


/***************************************
 ***************************************/

void
add_value( void * xev  FL_UNUSED_ARG,
		   void * a    FL_UNUSED_ARG )
{
	fl_insert_chart_value( chartobj, 1, next_step( ), "", 1 );
}


/***************************************
 ***************************************/

int
main( int    argc,
	  char * argv[ ] )
{
	FL_OBJECT *obj;

	fl_flip_yorigin( );
	fl_initialize( &argc, argv, "FormDemo", 0, 0 );
	create_form_form( );
	fl_set_chart_bounds( chartobj, -1.5, 1.5 );
	fl_set_chart_maxnumb( chartobj, 80 );
	fl_set_chart_autosize( chartobj, 0 );
	fl_set_button( sinobj, 1 );
	fl_set_slider_value( stepobj, 0.5 );
	fl_set_slider_bounds( stepobj, 0.0, 1.0 );

	fl_show_form( form,FL_PLACE_CENTER | FL_FREE_SIZE, FL_TRANSIENT,
				  "StripChart" );

    do
    {
		fl_insert_chart_value( chartobj,1,next_step( ), "", 1 );
		obj = fl_check_forms( );
    } while ( obj != exitbut );

    fl_finish( );
    return 0;
}


/***************************************
 ***************************************/

static void
create_form_form( void )
{
	FL_OBJECT *obj;

	form = fl_bgn_form( FL_NO_BOX, 490, 320 );

	fl_add_box( FL_BORDER_BOX, 0, 0, 490, 320, "" );

	chartobj = obj = fl_add_chart( FL_LINE_CHART, 20, 160, 390, 140, "" );
	fl_set_object_dblbuffer( obj, 1 );

	fl_bgn_group( );

	sinobj = obj = fl_add_lightbutton( FL_RADIO_BUTTON, 30, 120, 170, 30,
									   "sin(x)" );
    fl_set_object_boxtype( obj, FL_BORDER_BOX );
    fl_set_object_callback( obj, set_function, 1 );

	obj = fl_add_lightbutton( FL_RADIO_BUTTON, 30, 90, 170, 30,
							  "sin(2x)*cos(x)" );
    fl_set_object_boxtype( obj, FL_BORDER_BOX );
    fl_set_object_callback( obj, set_function, 2 );

	obj = fl_add_lightbutton( FL_RADIO_BUTTON, 30, 60, 170, 30,
							  "sin(2x)+cos(x)" );
    fl_set_object_boxtype( obj, FL_BORDER_BOX );
    fl_set_object_callback( obj, set_function, 3 );

	obj = fl_add_lightbutton( FL_RADIO_BUTTON, 240, 120, 160, 30,
							  "sin(3x)+cos(x)" );
	fl_set_object_boxtype( obj, FL_BORDER_BOX );
	fl_set_object_callback( obj, set_function, 4 );

	obj = fl_add_lightbutton( FL_RADIO_BUTTON, 240, 90, 160, 30,
							  "sin(x)^2 + cos(x)" );
    fl_set_object_boxtype( obj, FL_BORDER_BOX );
    fl_set_object_callback( obj, set_function, 5 );

	obj = fl_add_lightbutton( FL_RADIO_BUTTON, 240, 60, 160, 30,
							  "sin(x)^3" );
    fl_set_object_boxtype( obj, FL_BORDER_BOX );
    fl_set_object_callback( obj, set_function, 6 );

	fl_end_group( );

	exitbut = obj = fl_add_button( FL_NORMAL_BUTTON, 150, 20, 140, 30, "Exit" );
    fl_set_object_boxtype( obj, FL_BORDER_BOX );

	stepobj = obj = fl_add_valslider( FL_VERT_SLIDER, 430, 20, 40, 280, "" );
    fl_set_object_boxtype( obj, FL_BORDER_BOX );
    fl_set_object_callback( obj, set_step, 0 );

	fl_end_form( );
}