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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "include/forms.h"
26 #include <stdlib.h>
27 #include "time.h"
28 
29 /**** Forms and Objects ****/
30 
31 typedef struct {
32     FL_FORM   * axypform;
33     void      * vdata;
34     long        ldata;
35     FL_OBJECT * xyplot;
36     FL_OBJECT * xmin;
37     FL_OBJECT * xmax;
38     FL_OBJECT * ymin;
39     FL_OBJECT * ymax;
40     FL_OBJECT * status;
41 } FD_axypform;
42 
43 FD_axypform *create_form_axypform( void );
44 
45 FD_axypform *xypui;
46 
47 /* callbacks for form axypform */
48 
49 
50 /***************************************
51  ***************************************/
52 
xyplot_cb(FL_OBJECT * ob,long data FL_UNUSED_ARG)53 void xyplot_cb( FL_OBJECT * ob,
54                 long        data  FL_UNUSED_ARG )
55 {
56     float x, y;
57     int i;
58     char buf[ 64 ];
59 
60     fl_get_xyplot( ob, &x, &y, &i );
61     if ( i < 0 )
62        return;
63 
64     sprintf( buf, "X=%.3f  Y=%.3f", x, y );
65     fl_set_object_label( xypui->status, buf );
66 }
67 
68 
69 /***************************************
70  ***************************************/
71 
72 void
alwaysreturn_cb(FL_OBJECT * ob,long data FL_UNUSED_ARG)73 alwaysreturn_cb( FL_OBJECT * ob,
74                  long        data  FL_UNUSED_ARG )
75 {
76     fl_set_object_return( xypui->xyplot,
77                           fl_get_button( ob ) ?
78                           FL_RETURN_CHANGED : FL_RETURN_END_CHANGED );
79 }
80 
81 
82 /***************************************
83  ***************************************/
84 
85 void
interpolate_cb(FL_OBJECT * ob,long data FL_UNUSED_ARG)86 interpolate_cb( FL_OBJECT * ob,
87                 long        data  FL_UNUSED_ARG )
88 {
89     fl_set_xyplot_interpolate( xypui->xyplot, 0, fl_get_button( ob ) ? 3 : 0,
90                                0.2 );
91 }
92 
93 
94 /***************************************
95  ***************************************/
96 
97 void
inspect_cb(FL_OBJECT * ob,long data FL_UNUSED_ARG)98 inspect_cb( FL_OBJECT * ob,
99             long        data  FL_UNUSED_ARG )
100 {
101     fl_set_xyplot_inspect( xypui->xyplot, fl_get_button( ob ) );
102 }
103 
104 
105 /***************************************
106  ***************************************/
107 
108 void
notic_cb(FL_OBJECT * obj,long data FL_UNUSED_ARG)109 notic_cb( FL_OBJECT * obj,
110           long        data  FL_UNUSED_ARG )
111 {
112     if ( fl_get_button( obj ) )
113     {
114         fl_set_xyplot_xtics( xypui->xyplot, -1, -1 );
115         fl_set_xyplot_ytics( xypui->xyplot, -1, -1 );
116     }
117     else
118     {
119         fl_set_xyplot_xtics( xypui->xyplot, 0, 0 );
120         fl_set_xyplot_ytics( xypui->xyplot, 0, 0 );
121     }
122 }
123 
124 
125 /***************************************
126  ***************************************/
127 
128 void
bounds_cb(FL_OBJECT * ob FL_UNUSED_ARG,long data)129 bounds_cb( FL_OBJECT * ob  FL_UNUSED_ARG,
130            long        data )
131 {
132     char buf[ 50 ];
133 
134     if ( ! data )
135     {
136         float xmin = strtod( fl_get_input( xypui->xmin ), NULL );
137         float xmax = strtod( fl_get_input( xypui->xmax ), NULL );
138 
139         fl_set_xyplot_xbounds( xypui->xyplot, xmin, xmax );
140 
141         fl_get_xyplot_xbounds( xypui->xyplot, &xmin, &xmax );
142         sprintf( buf, "%g", xmin );
143         fl_set_input( xypui->xmin, buf );
144         sprintf( buf, "%g", xmax );
145         fl_set_input( xypui->xmax, buf );
146     }
147     else
148     {
149         float ymin = strtod( fl_get_input( xypui->ymin ), NULL );
150         float ymax = strtod( fl_get_input( xypui->ymax ), NULL );
151 
152         fl_set_xyplot_ybounds( xypui->xyplot, ymin, ymax );
153 
154         fl_get_xyplot_ybounds( xypui->xyplot, &ymin, &ymax );
155         sprintf( buf, "%g", ymin );
156         fl_set_input( xypui->ymin, buf );
157         sprintf( buf, "%g", ymax );
158         fl_set_input( xypui->ymax, buf );
159     }
160 }
161 
162 
163 /***************************************
164  ***************************************/
165 
166 int
main(int argc,char * argv[])167 main( int    argc,
168       char * argv[ ] )
169 {
170     float x[ 11 ],
171           y[ 11 ];
172     int i;
173 
174     fl_initialize( &argc, argv, "FormDemo", 0, 0 );
175     xypui = create_form_axypform( );
176 
177     /* Fill-in form initialization code */
178 
179     fl_set_xyplot_ybounds( xypui->xyplot, 0.0, 10.0 );
180 
181     for ( i  = 0; i <= 10; i++ )
182         x[ i ] = y[ i ] = i;
183 
184     fl_add_xyplot_overlay( xypui->xyplot, 1, x, y, 11, FL_YELLOW );
185     fl_set_xyplot_overlay_type( xypui->xyplot, 1, FL_LINEPOINTS_XYPLOT );
186     fl_set_xyplot_interpolate( xypui->xyplot, 1, 2, 0.1 );
187 
188     srand( time( NULL ) );
189 
190     for ( i = 0; i <= 10; i++ )
191         y[ i ] +=  ( double ) rand( ) / RAND_MAX - 0.5;
192 
193     fl_set_xyplot_data( xypui->xyplot, x, y, 11, "Active xyplot with overlay",
194                         "x-axis", "y-axis" );
195     fl_set_xyplot_linewidth( xypui->xyplot, 0, 2 );
196     fl_set_xyplot_xgrid( xypui->xyplot, FL_GRID_MINOR );
197 
198     /* Show the first form */
199 
200     fl_show_form( xypui->axypform, FL_PLACE_MOUSE | FL_FREE_SIZE,
201                   FL_FULLBORDER, "xyplotactive" );
202 
203     fl_do_forms( );
204 
205     return 0;
206 }
207 
208 
209 /***************************************
210  ***************************************/
211 
212 FD_axypform *
create_form_axypform(void)213 create_form_axypform( void )
214 {
215     FL_OBJECT *obj;
216     FD_axypform *fdui = fl_calloc( 1, sizeof *fdui );
217 
218     fdui->axypform = fl_bgn_form( FL_NO_BOX, 431, 301 );
219 
220     fl_add_box( FL_UP_BOX, 0, 0, 431, 301, "" );
221 
222     fdui->xyplot = obj = fl_add_xyplot( FL_ACTIVE_XYPLOT, 20, 50, 285, 235,
223                                         "" );
224 
225     fl_set_object_boxtype( obj, FL_DOWN_BOX );
226     fl_set_object_color( obj, FL_BLACK, FL_GREEN );
227     fl_set_object_lalign( obj, fl_to_inside_lalign( FL_ALIGN_BOTTOM ) );
228     fl_set_object_callback( obj, xyplot_cb, 0 );
229     fl_set_object_gravity( obj, FL_NorthWest, FL_SouthEast );
230 
231     obj = fl_add_checkbutton( FL_PUSH_BUTTON, 315, 40, 80, 25, "AlwaysReturn" );
232     fl_set_object_color( obj, FL_COL1, FL_BLUE );
233     fl_set_object_callback( obj, alwaysreturn_cb, 0 );
234     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
235 
236     obj = fl_add_checkbutton( FL_PUSH_BUTTON, 315, 65, 80, 25, "Interpolate" );
237     fl_set_object_color( obj, FL_COL1, FL_BLUE );
238     fl_set_object_callback( obj, interpolate_cb, 0 );
239     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
240 
241     obj = fl_add_checkbutton( FL_PUSH_BUTTON, 315, 90, 85, 25, "InspectOnly" );
242     fl_set_object_color( obj, FL_COL1, FL_BLUE );
243     fl_set_object_callback( obj, inspect_cb, 0 );
244     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
245 
246     obj = fl_add_checkbutton( FL_PUSH_BUTTON, 315, 120, 85, 25, "NoTics" );
247     fl_set_object_color( obj, FL_COL1, FL_BLUE );
248     fl_set_object_callback( obj, notic_cb, 0 );
249     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
250 
251 
252     fdui->xmin = obj = fl_add_input( FL_FLOAT_INPUT, 315, 150, 50, 20,
253                                      " x_min" );
254     fl_set_input( obj, "0.0" );
255     fl_set_object_lalign( obj, FL_ALIGN_RIGHT );
256     fl_set_object_callback( obj, bounds_cb, 0 );
257     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
258 
259     fdui->xmax = obj = fl_add_input( FL_FLOAT_INPUT, 315, 170, 50, 20,
260                                      " x_max" );
261     fl_set_input( obj, "10.0" );
262     fl_set_object_lalign( obj, FL_ALIGN_RIGHT );
263     fl_set_object_callback( obj, bounds_cb, 0 );
264     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
265 
266     fdui->ymin = obj = fl_add_input( FL_FLOAT_INPUT, 315, 200, 50, 20,
267                                      " y_min" );
268     fl_set_input( obj, "0.0" );
269     fl_set_object_lalign( obj, FL_ALIGN_RIGHT );
270     fl_set_object_callback( obj, bounds_cb, 1 );
271     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
272 
273     fdui->ymax = obj = fl_add_input( FL_FLOAT_INPUT, 315, 220, 50, 20,
274                                      " y_max" );
275     fl_set_input( obj, "10.0" );
276     fl_set_object_lalign( obj, FL_ALIGN_RIGHT );
277     fl_set_object_callback( obj, bounds_cb, 1 );
278     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
279 
280     fdui->status = obj = fl_add_box( FL_BORDER_BOX, 20, 15, 285, 25, "" );
281     fl_set_object_boxtype( obj, FL_DOWN_BOX );
282     fl_set_object_gravity( obj, FL_NorthWest, FL_NorthEast );
283     fl_set_object_lalign( obj, FL_ALIGN_CENTER );
284 
285     obj = fl_add_button( FL_NORMAL_BUTTON, 325, 250, 90, 30, "Done" );
286     fl_set_object_gravity( obj, FL_SouthEast, FL_SouthEast );
287 
288     fl_end_form( );
289 
290   return fdui;
291 }
292 
293 
294 /*
295  * Local variables:
296  * tab-width: 4
297  * indent-tabs-mode: nil
298  * End:
299  */
300