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 License
15  *  along with XForms.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
19 /**
20  * \file sp_counter.c
21  *
22  *  This file is part of XForms package
23  *  Copyright (c) 1996-2002  T.C. Zhao and Mark Overmars
24  *  All rights reserved.
25  *
26  * Settting counter class specific attributes.
27  */
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <float.h>
34 
35 #include "fd_main.h"
36 #include "fd_spec.h"
37 #include "sp_counter.h"
38 #include "private/pcounter.h"
39 #include "private/pvaluator.h"
40 #include "spec/counter_spec.h"
41 
42 static FD_counterattrib * cnt_attrib;
43 static FL_OBJECT * curobj;
44 
45 /***************************************
46  ***************************************/
47 
48 FL_FORM *
counter_create_spec_form(void)49 counter_create_spec_form( void )
50 {
51     if ( cnt_attrib )
52         return cnt_attrib->counterattrib;
53 
54     cnt_attrib = create_form_counterattrib( );
55 
56     setup_how_return_menu( cnt_attrib->returnsetting );
57     fl_set_menu_item_mode( cnt_attrib->returnsetting, 5,
58                            FL_PUP_BOX | FL_PUP_GRAY );
59     fl_set_menu_item_mode( cnt_attrib->returnsetting, 6,
60                            FL_PUP_BOX | FL_PUP_GRAY );
61 
62     fl_set_counter_precision( cnt_attrib->prec, 0 );
63     fl_set_counter_step( cnt_attrib->prec, 1, 2 );
64     fl_set_counter_bounds( cnt_attrib->prec, 0, DBL_DIG );
65 
66     return cnt_attrib->counterattrib;
67 }
68 
69 
70 /***************************************
71  ***************************************/
72 
73 void
counter_adjust_spec_form(FL_OBJECT * obj)74 counter_adjust_spec_form( FL_OBJECT * obj )
75 {
76     curobj = obj;
77 
78     if ( obj->type == FL_SIMPLE_COUNTER )
79         fl_hide_object( cnt_attrib->step2 );
80 }
81 
82 
83 /***************************************
84  ***************************************/
85 
86 void
counter_fill_in_spec_form(FL_OBJECT * obj)87 counter_fill_in_spec_form( FL_OBJECT * obj )
88 {
89     FLI_COUNTER_SPEC *sp = obj->spec;
90 
91     fl_set_counter_value( cnt_attrib->prec, sp->prec );
92 
93     set_finput_value( cnt_attrib->minval,     sp->min,   sp->prec );
94     set_finput_value( cnt_attrib->maxval,     sp->max,   sp->prec );
95     set_finput_value( cnt_attrib->initialval, sp->val,   sp->prec );
96     set_finput_value( cnt_attrib->step1,      sp->sstep, sp->prec );
97     set_finput_value( cnt_attrib->step2,      sp->lstep, sp->prec );
98 
99     reset_how_return_menu( cnt_attrib->returnsetting, curobj->how_return );
100 }
101 
102 
103 /***************************************
104  ***************************************/
105 
106 void
counter_reread_spec_form(FL_OBJECT * obj)107 counter_reread_spec_form( FL_OBJECT * obj )
108 {
109     double r1, r2;
110 
111     if (    get_checked_float( fl_get_input( cnt_attrib->minval ), &r1 )
112          && get_checked_float( fl_get_input( cnt_attrib->maxval ), &r2 ) )
113         fl_set_counter_bounds( obj, r1, r2 );
114 
115     if ( get_checked_float( fl_get_input( cnt_attrib->initialval ), &r1 ) )
116          fl_set_counter_value( obj, r1 );
117 
118     if (    get_checked_float( fl_get_input( cnt_attrib->step1 ), &r1 )
119          && get_checked_float( fl_get_input( cnt_attrib->step2 ), &r2 ) )
120         fl_set_counter_step( obj, r1, r2 );
121 
122     redraw_the_form( 0 );
123 }
124 
125 
126 /***************************************
127  ***************************************/
128 
129 void
counter_emit_spec_fd_code(FILE * fp,FL_OBJECT * obj)130 counter_emit_spec_fd_code( FILE      * fp,
131                            FL_OBJECT * obj )
132 {
133     FL_OBJECT *defobj = fl_create_counter( obj->type, 0, 0, 0, 0, "" );
134     FLI_COUNTER_SPEC *sp    = obj->spec,
135                      *defsp = defobj->spec;
136 
137     if ( sp->min != defsp->min || sp->max != defsp->max )
138         fprintf( fp, "    bounds: %.*f %.*f\n",
139                  sp->prec, sp->min, sp->prec, sp->max );
140 
141     if ( sp->prec != defsp->prec )
142         fprintf( fp, "    precision: %d\n", sp->prec );
143 
144     if ( sp->val != defsp->val )
145         fprintf( fp, "    value: %.*f\n", sp->prec, sp->val );
146 
147     if ( sp->sstep != defsp->sstep )
148         fprintf( fp, "    sstep: %.*f\n", sp->prec, sp->sstep );
149 
150     if ( sp->lstep != defsp->lstep )
151         fprintf( fp, "    lstep: %.*f\n", sp->prec, sp->lstep );
152 
153     fl_free_object( defobj );
154 }
155 
156 
157 /***************************************
158  ***************************************/
159 
160 void
counter_emit_spec_c_code(FILE * fp,FL_OBJECT * obj)161 counter_emit_spec_c_code( FILE      * fp,
162                           FL_OBJECT * obj )
163 {
164     FL_OBJECT *defobj = fl_create_counter( obj->type, 0, 0, 0, 0, "" );
165     FLI_COUNTER_SPEC *sp    = obj->spec,
166                      *defsp = defobj->spec;
167 
168     if ( sp->prec != defsp->prec )
169         fprintf( fp, "    fl_set_counter_precision( obj, %d );\n", sp->prec );
170 
171     if ( sp->min != defsp->min || sp->max != defsp->max )
172         fprintf( fp, "    fl_set_counter_bounds( obj, %.*f, %.*f );\n",
173                  sp->prec, sp->min, sp->prec, sp->max );
174 
175     if ( sp->val != defsp->val )
176         fprintf( fp, "    fl_set_counter_value( obj, %.*f );\n",
177                  sp->prec, sp->val );
178 
179     if ( sp->sstep != defsp->sstep || sp->lstep != defsp->lstep )
180         fprintf( fp, "    fl_set_counter_step( obj, %.*f, %.*f );\n",
181                  sp->prec, sp->sstep, sp->prec, sp->lstep );
182 
183     fl_free_object( defobj );
184 }
185 
186 
187 /***************************************
188  ***************************************/
189 
190 void
cnt_precision_cb(FL_OBJECT * obj,long data FL_UNUSED_ARG)191 cnt_precision_cb( FL_OBJECT * obj,
192                   long        data  FL_UNUSED_ARG )
193 {
194     fl_set_counter_precision( curobj, fl_get_counter_value( obj ) );
195     redraw_the_form( 0 );
196 }
197 
198 
199 /***************************************
200  ***************************************/
201 
202 void
cnt_minmax_change(FL_OBJECT * obj FL_UNUSED_ARG,long data FL_UNUSED_ARG)203 cnt_minmax_change( FL_OBJECT * obj   FL_UNUSED_ARG,
204                    long        data  FL_UNUSED_ARG )
205 {
206     fl_set_counter_bounds( curobj,
207                            get_finput_value( cnt_attrib->minval ),
208                            get_finput_value( cnt_attrib->maxval ) );
209     redraw_the_form( 0 );
210 }
211 
212 
213 /***************************************
214  ***************************************/
215 
216 void
cnt_stepchange_cb(FL_OBJECT * obj FL_UNUSED_ARG,long data FL_UNUSED_ARG)217 cnt_stepchange_cb( FL_OBJECT * obj   FL_UNUSED_ARG,
218                    long        data  FL_UNUSED_ARG )
219 {
220     fl_set_counter_step( curobj,
221                          get_finput_value( cnt_attrib->step1 ),
222                          get_finput_value( cnt_attrib->step2 ) );
223     redraw_the_form( 0 );
224 }
225 
226 
227 /***************************************
228  ***************************************/
229 
230 void
cnt_initialvalue_change(FL_OBJECT * obj,long data FL_UNUSED_ARG)231 cnt_initialvalue_change( FL_OBJECT * obj,
232                          long        data  FL_UNUSED_ARG )
233 {
234     FLI_COUNTER_SPEC *sp = curobj->spec;
235 
236     set_finput_value( obj, get_finput_value( obj ), sp->prec );
237     fl_set_counter_value( curobj, get_finput_value( obj ) );
238     redraw_the_form( 0 );
239 }
240 
241 
242 /***************************************
243  ***************************************/
244 
245 void
cnt_returnsetting_change(FL_OBJECT * obj,long data FL_UNUSED_ARG)246 cnt_returnsetting_change( FL_OBJECT * obj,
247                           long        data  FL_UNUSED_ARG )
248 {
249     handle_how_return_changes( obj, curobj );
250 }
251 
252 
253 #include "spec/counter_spec.c"
254 
255 
256 /*
257  * Local variables:
258  * tab-width: 4
259  * indent-tabs-mode: nil
260  * End:
261  */
262