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         if ( xmin <= 0.0 )
140         {
141             xmin = 1.0;
142             fl_set_input( xypui->xmin, "1.0" );
143         }
144 
145         if ( xmax <= 0.0 )
146         {
147             xmax = 10.0;
148             fl_set_input( xypui->xmax, "10.0" );
149         }
150 
151         fl_set_xyplot_xbounds( xypui->xyplot, xmin, xmax );
152 
153         fl_get_xyplot_xbounds( xypui->xyplot, &xmin, &xmax );
154         sprintf( buf, "%g", xmin );
155         fl_set_input( xypui->xmin, buf );
156         sprintf( buf, "%g", xmax );
157         fl_set_input( xypui->xmax, buf );
158     }
159     else
160     {
161         float ymin = strtod( fl_get_input( xypui->ymin ), NULL );
162         float ymax = strtod( fl_get_input( xypui->ymax ), NULL );
163 
164         if ( ymin <= 0.0 )
165         {
166             ymin = 1.0;
167             fl_set_input( xypui->ymin, "1.0" );
168         }
169 
170         if ( ymax <= 0.0 )
171         {
172             ymax = 10.0;
173             fl_set_input( xypui->ymax, "10.0" );
174         }
175 
176         fl_set_xyplot_ybounds( xypui->xyplot, ymin, ymax );
177 
178         fl_get_xyplot_ybounds( xypui->xyplot, &ymin, &ymax );
179         sprintf( buf, "%g", ymin );
180         fl_set_input( xypui->ymin, buf );
181         sprintf( buf, "%g", ymax );
182         fl_set_input( xypui->ymax, buf );
183     }
184 }
185 
186 
187 /***************************************
188  ***************************************/
189 
190 int
main(int argc,char * argv[])191 main( int    argc,
192       char * argv[ ] )
193 {
194     float x[ 11 ],
195           y[ 11 ];
196     int i;
197 
198     fl_initialize( &argc, argv, "FormDemo", 0, 0 );
199     xypui = create_form_axypform( );
200 
201     /* Fill-in form initialization code */
202 
203     fl_set_xyplot_ybounds( xypui->xyplot, 1.0, 10.0 );
204 
205     for ( i  = 0; i < 10; i++ )
206         x[ i ] = y[ i ] = i + 1;
207 
208     fl_add_xyplot_overlay( xypui->xyplot, 1, x, y, 10, FL_YELLOW );
209     fl_set_xyplot_overlay_type( xypui->xyplot, 1, FL_LINEPOINTS_XYPLOT );
210     fl_set_xyplot_interpolate( xypui->xyplot, 1, 2, 0.1 );
211 
212     srand( time( NULL ) );
213 
214     for ( i = 0; i < 10; i++ )
215         y[ i ] +=  ( double ) rand( ) / RAND_MAX - 0.5;
216 
217     fl_set_xyplot_data( xypui->xyplot, x, y, 10,
218                         "Active (log) xyplot with overlay",
219                         "x-axis", "y-axis" );
220     fl_set_xyplot_linewidth( xypui->xyplot, 0, 2 );
221     fl_set_xyplot_xgrid( xypui->xyplot, FL_GRID_MINOR );
222 
223     /* Show the first form */
224 
225     fl_show_form( xypui->axypform, FL_PLACE_MOUSE | FL_FREE_SIZE,
226                   FL_FULLBORDER, "xyplotactivelog" );
227 
228     fl_do_forms( );
229 
230     return 0;
231 }
232 
233 
234 /***************************************
235  ***************************************/
236 
237 FD_axypform *
create_form_axypform(void)238 create_form_axypform( void )
239 {
240     FL_OBJECT *obj;
241     FD_axypform *fdui = fl_calloc( 1, sizeof *fdui );
242 
243     fdui->axypform = fl_bgn_form( FL_NO_BOX, 431, 301 );
244 
245     fl_add_box( FL_UP_BOX, 0, 0, 431, 301, "" );
246 
247     fdui->xyplot = obj = fl_add_xyplot( FL_ACTIVE_XYPLOT, 20, 50, 285, 235,
248                                         "" );
249 
250     fl_set_xyplot_xscale( obj, FL_LOG, 2.0 );
251     fl_set_xyplot_yscale( obj, FL_LOG, 2.0 );
252 
253     fl_set_object_boxtype( obj, FL_DOWN_BOX );
254     fl_set_object_color( obj, FL_BLACK, FL_GREEN );
255     fl_set_object_lalign( obj, fl_to_inside_lalign( FL_ALIGN_BOTTOM ) );
256     fl_set_object_callback( obj, xyplot_cb, 0 );
257     fl_set_object_gravity( obj, FL_NorthWest, FL_SouthEast );
258 
259     obj = fl_add_checkbutton( FL_PUSH_BUTTON, 315, 40, 80, 25, "AlwaysReturn" );
260     fl_set_object_color( obj, FL_COL1, FL_BLUE );
261     fl_set_object_callback( obj, alwaysreturn_cb, 0 );
262     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
263 
264     obj = fl_add_checkbutton( FL_PUSH_BUTTON, 315, 65, 80, 25, "Interpolate" );
265     fl_set_object_color( obj, FL_COL1, FL_BLUE );
266     fl_set_object_callback( obj, interpolate_cb, 0 );
267     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
268 
269     obj = fl_add_checkbutton( FL_PUSH_BUTTON, 315, 90, 85, 25, "InspectOnly" );
270     fl_set_object_color( obj, FL_COL1, FL_BLUE );
271     fl_set_object_callback( obj, inspect_cb, 0 );
272     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
273 
274     obj = fl_add_checkbutton( FL_PUSH_BUTTON, 315, 120, 85, 25, "NoTics" );
275     fl_set_object_color( obj, FL_COL1, FL_BLUE );
276     fl_set_object_callback( obj, notic_cb, 0 );
277     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
278 
279     fdui->xmin = obj = fl_add_input( FL_FLOAT_INPUT, 315, 150, 50, 20,
280                                      " x_min" );
281     fl_set_input( obj, "1.0" );
282     fl_set_object_lalign( obj, FL_ALIGN_RIGHT );
283     fl_set_object_callback( obj, bounds_cb, 0 );
284     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
285 
286     fdui->xmax = obj = fl_add_input( FL_FLOAT_INPUT, 315, 170, 50, 20,
287                                      " x_max" );
288     fl_set_input( obj, "10.0" );
289     fl_set_object_lalign( obj, FL_ALIGN_RIGHT );
290     fl_set_object_callback( obj, bounds_cb, 0 );
291     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
292 
293     fdui->ymin = obj = fl_add_input( FL_FLOAT_INPUT, 315, 200, 50, 20,
294                                      " y_min" );
295     fl_set_input( obj, "1.0" );
296     fl_set_object_lalign( obj, FL_ALIGN_RIGHT );
297     fl_set_object_callback( obj, bounds_cb, 1 );
298     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
299 
300     fdui->ymax = obj = fl_add_input( FL_FLOAT_INPUT, 315, 220, 50, 20,
301                                      " y_max" );
302     fl_set_input( obj, "10.0" );
303     fl_set_object_lalign( obj, FL_ALIGN_RIGHT );
304     fl_set_object_callback( obj, bounds_cb, 1 );
305     fl_set_object_gravity( obj, FL_NorthEast, FL_NorthEast );
306 
307     fdui->status = obj = fl_add_box( FL_BORDER_BOX, 20, 15, 285, 25, "" );
308     fl_set_object_boxtype( obj, FL_DOWN_BOX );
309     fl_set_object_gravity( obj, FL_NorthWest, FL_NorthEast );
310     fl_set_object_lalign( obj, FL_ALIGN_CENTER );
311 
312     obj = fl_add_button( FL_NORMAL_BUTTON, 325, 250, 90, 30, "Done" );
313     fl_set_object_gravity( obj, FL_SouthEast, FL_SouthEast );
314 
315     fl_end_form( );
316 
317   return fdui;
318 }
319 
320 
321 /*
322  * Local variables:
323  * tab-width: 4
324  * indent-tabs-mode: nil
325  * End:
326  */
327