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 /* This demo shows the use of a overlay positioner with a position validator */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include <stdio.h>
28 #include <math.h>
29 #include "include/forms.h"
30 #include "colorwheel.xpm"
31 
32 
33 static FL_OBJECT * xval,
34                  * yval;
35 
36 
37 /***************************************
38  * Validator function for positioner, makes
39  sure that it's restricted to a circle
40  ***************************************/
41 
42 static int
validator(FL_OBJECT * obj FL_UNUSED_ARG,double x,double y,double * x_repl,double * y_repl)43 validator( FL_OBJECT * obj  FL_UNUSED_ARG,
44            double      x,
45            double      y,
46            double    * x_repl,
47            double    * y_repl )
48 {
49     double angle;
50 
51     /* If the new position is within the circle (with radius 1) it's fine,
52        tell the positioner to use it as is */
53 
54     if ( x * x + y * y <= 1 )
55         return FL_POSITIONER_VALID;
56 
57     /* Otherwise replace the new position by one at the circle's border
58        in the direction from the center to the new coordinates */
59 
60     angle = atan2( y, x );
61     *x_repl = cos( angle );
62     *y_repl = sin( angle );
63 
64     /* Tell the positioner to use the values in x_repl and y_repl */
65 
66     return FL_POSITIONER_REPLACED;
67 }
68 
69 
70 /***************************************
71  * Callback routine for new position
72  ***************************************/
73 
74 static void
positioner_cb(FL_OBJECT * obj,long data FL_UNUSED_ARG)75 positioner_cb( FL_OBJECT * obj,
76                long        data   FL_UNUSED_ARG )
77 {
78     double x = fl_get_positioner_xvalue( obj ),
79            y = fl_get_positioner_yvalue( obj );
80     double angle = 45 * atan2( y, x ) / atan( 1 );
81 
82     if ( angle < 0 )
83         angle += 360;
84 
85     fl_set_object_label_f( xval, "%f", angle );
86     fl_set_object_label_f( yval, "%f", sqrt( x * x + y * y ) );
87 }
88 
89 
90 /***************************************
91  * Creates the form and then does the interaction
92  ***************************************/
93 
94 int
main(int argc,char * argv[])95 main( int    argc,
96       char * argv[ ] )
97 {
98     FL_FORM   * form;
99     FL_OBJECT * pm;
100     FL_OBJECT * pos;
101 
102     fl_initialize( &argc, argv, "FormDemo", 0, 0 );
103 
104     form = fl_bgn_form( FL_UP_BOX, 400, 280 );
105 
106     pm = fl_add_pixmap( FL_NORMAL_PIXMAP, 40, 40, 203, 203, "" );
107     fl_set_pixmap_data( pm, colorwheel );
108 
109     pos = fl_add_positioner( FL_OVERLAY_POSITIONER, 40, 40, 203, 203, "" );
110     fl_set_positioner_xbounds( pos, -1, 1 );
111     fl_set_positioner_ybounds( pos, -1, 1 );
112     fl_set_object_callback( pos, positioner_cb, 0 );
113     fl_set_positioner_xvalue( pos, 0.0 );
114     fl_set_positioner_yvalue( pos, 0.0 );
115     fl_set_positioner_validator( pos, validator );
116     fl_set_object_color( pos, FL_COL1, FL_BLACK );
117 
118     xval = fl_add_box( FL_DOWN_BOX, 270, 40, 100, 30, "" );
119     fl_set_object_color( xval, FL_COL1, FL_COL1 );
120 
121     yval = fl_add_box( FL_DOWN_BOX, 270, 90, 100, 30, "" );
122     fl_set_object_color( yval, FL_COL1, FL_COL1 );
123 
124     fl_add_button( FL_NORMAL_BUTTON, 270, 210, 100, 30, "Exit" );
125 
126     fl_end_form( );
127 
128     fl_show_form( form, FL_PLACE_CENTER, FL_FULLBORDER, "positioner_v" );
129 
130     positioner_cb( pos, 0 );
131 
132     fl_do_forms( );
133     fl_finish( );
134 
135     return 0;
136 }
137 
138 
139 /*
140  * Local variables:
141  * tab-width: 4
142  * indent-tabs-mode: nil
143  * End:
144  */
145