1 /*
2 * This file is part of XForms.
3 *
4 * XForms is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2.1, or
7 * (at your option) any later version.
8 *
9 * XForms is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with XForms; see the file COPYING. If not, write to
16 * the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
17 * MA 02111-1307, USA.
18 */
19
20
21 /*
22 * A demo of a moving chart
23 *
24 * This file is part of xforms package
25 * M. Overmars and T.C. Zhao (1997)
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <math.h>
33 #include "include/forms.h"
34
35
36 static long func = 1;
37 static double x = 0.0;
38 static double step = 0.15;
39
40 static FL_FORM * form;
41
42 static FL_OBJECT * chartobj,
43 * sinobj,
44 * exitbut,
45 * stepobj,
46 * timerobj;
47
48 static void create_form_form(void);
49
50
51 /***************************************
52 ***************************************/
53
54 static void
set_function(FL_OBJECT * obj FL_UNUSED_ARG,long arg)55 set_function( FL_OBJECT * obj FL_UNUSED_ARG,
56 long arg )
57 {
58 func = arg;
59 fl_clear_chart( chartobj );
60 x = 0.0;
61 }
62
63
64 /***************************************
65 ***************************************/
66
67 static void
set_step(FL_OBJECT * obj FL_UNUSED_ARG,long arg FL_UNUSED_ARG)68 set_step( FL_OBJECT * obj FL_UNUSED_ARG,
69 long arg FL_UNUSED_ARG )
70 {
71 step = fl_get_slider_value( stepobj );
72 }
73
74
75 /***************************************
76 ***************************************/
77
78 static double
next_step(void)79 next_step( void )
80 {
81 double res = 0.0;
82
83 switch ( func)
84 {
85 case 1:
86 res = sin( x );
87 break;
88
89 case 2:
90 res = sin( 2 * x ) * cos( x );
91 break;
92
93 case 3:
94 res = sin( 2 * x ) + cos( x );
95 break;
96
97 case 4:
98 res = sin( 3 * x ) + cos( x );
99 break;
100
101 case 5:
102 res = sin( x ) * sin( x ) + cos( x );
103 break;
104
105 case 6:
106 res = sin( x ) * sin( x ) * sin( x );
107 break;
108 }
109
110 x += step;
111
112 return res;
113 }
114
115
116 /***************************************
117 ***************************************/
118
119 static void
timer_cb(FL_OBJECT * obj,long data FL_UNUSED_ARG)120 timer_cb( FL_OBJECT * obj,
121 long data FL_UNUSED_ARG )
122 {
123 fl_insert_chart_value( chartobj, 1, next_step( ), "", 1 );
124 fl_set_timer( obj, 0.05 );
125 }
126
127
128 /***************************************
129 ***************************************/
130
131 int
main(int argc,char * argv[])132 main( int argc,
133 char * argv[ ] )
134 {
135 fl_flip_yorigin( );
136 fl_initialize( &argc, argv, "FormDemo", 0, 0 );
137 create_form_form( );
138 fl_set_chart_bounds( chartobj, -1.5, 1.5 );
139 fl_set_chart_maxnumb( chartobj, 80 );
140 fl_set_chart_autosize( chartobj, 0 );
141 fl_set_button( sinobj, 1 );
142 fl_set_slider_value( stepobj, 0.5 );
143 fl_set_slider_bounds( stepobj, 0.0, 1.0 );
144
145 fl_show_form( form,FL_PLACE_CENTER | FL_FREE_SIZE, FL_TRANSIENT,
146 "StripChart" );
147
148 fl_set_timer( timerobj, 0.05 );
149
150 while ( fl_do_forms( ) != exitbut )
151 /* empty */ ;
152
153 fl_finish( );
154 return 0;
155 }
156
157
158 /***************************************
159 ***************************************/
160
161 static void
create_form_form(void)162 create_form_form( void )
163 {
164 FL_OBJECT *obj;
165
166 form = fl_bgn_form( FL_NO_BOX, 490, 320 );
167
168 fl_add_box( FL_BORDER_BOX, 0, 0, 490, 320, "" );
169
170 chartobj = obj = fl_add_chart( FL_LINE_CHART, 20, 160, 390, 140, "" );
171 fl_set_object_color( obj, FL_BLACK, FL_INACTIVE );
172 fl_set_object_dblbuffer( obj, 1 );
173
174 fl_bgn_group( );
175
176 sinobj = obj = fl_add_lightbutton( FL_RADIO_BUTTON, 30, 120, 170, 30,
177 "sin(x)" );
178 fl_set_object_boxtype( obj, FL_BORDER_BOX );
179 fl_set_object_callback( obj, set_function, 1 );
180
181 obj = fl_add_lightbutton( FL_RADIO_BUTTON, 30, 90, 170, 30,
182 "sin(2x)*cos(x)" );
183 fl_set_object_boxtype( obj, FL_BORDER_BOX );
184 fl_set_object_callback( obj, set_function, 2 );
185
186 obj = fl_add_lightbutton( FL_RADIO_BUTTON, 30, 60, 170, 30,
187 "sin(2x)+cos(x)" );
188 fl_set_object_boxtype( obj, FL_BORDER_BOX );
189 fl_set_object_callback( obj, set_function, 3 );
190
191 obj = fl_add_lightbutton( FL_RADIO_BUTTON, 240, 120, 160, 30,
192 "sin(3x)+cos(x)" );
193 fl_set_object_boxtype( obj, FL_BORDER_BOX );
194 fl_set_object_callback( obj, set_function, 4 );
195
196 obj = fl_add_lightbutton( FL_RADIO_BUTTON, 240, 90, 160, 30,
197 "sin(x)^2 + cos(x)" );
198 fl_set_object_boxtype( obj, FL_BORDER_BOX );
199 fl_set_object_callback( obj, set_function, 5 );
200
201 obj = fl_add_lightbutton( FL_RADIO_BUTTON, 240, 60, 160, 30,
202 "sin(x)^3" );
203 fl_set_object_boxtype( obj, FL_BORDER_BOX );
204 fl_set_object_callback( obj, set_function, 6 );
205
206 fl_end_group( );
207
208 exitbut = obj = fl_add_button( FL_NORMAL_BUTTON, 150, 20, 140, 30, "Exit" );
209 fl_set_object_boxtype( obj, FL_BORDER_BOX );
210
211 stepobj = obj = fl_add_valslider( FL_VERT_SLIDER, 430, 20, 40, 280, "" );
212 fl_set_object_return( obj, FL_RETURN_END_CHANGED );
213 fl_set_object_boxtype( obj, FL_BORDER_BOX );
214 fl_set_object_callback( obj, set_step, 0 );
215
216 timerobj = obj = fl_add_timer( FL_HIDDEN_TIMER, 0, 0, 0, 0, "" );
217 fl_set_object_callback( obj, timer_cb, 0 );
218
219 fl_end_form( );
220 }
221
222
223 /*
224 * Local variables:
225 * tab-width: 4
226 * indent-tabs-mode: nil
227 * End:
228 */
229