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_choice.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 choice class specific attributes.
27  */
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include "fd_main.h"
34 #include "fd_spec.h"
35 #include "sp_choice.h"
36 #include "private/pchoice.h"
37 #include "spec/choice_spec.h"
38 
39 static FD_choiceattrib *choice_attrib;
40 static FL_OBJECT * curobj;
41 
42 /***************************************
43  ***************************************/
44 
45 FL_FORM *
choice_create_spec_form(void)46 choice_create_spec_form( void )
47 {
48     if ( choice_attrib )
49         return choice_attrib->choiceattrib;
50 
51     choice_attrib = create_form_choiceattrib( );
52 
53     fl_addto_choice( choice_attrib->mode, get_pupmode_string( ) );
54     fl_addto_choice( choice_attrib->scope, "local|global" );
55     fl_set_choice_item_mode( choice_attrib->mode, 3, FL_PUP_GRAY );
56     fl_set_choice_item_mode( choice_attrib->mode, 4, FL_PUP_GRAY );
57     fl_set_choice_item_mode( choice_attrib->mode, 5, FL_PUP_GRAY );
58 
59     fl_addto_choice( choice_attrib->align,
60                      align_name( FL_ALIGN_CENTER, 0 ) );
61     fl_addto_choice( choice_attrib->align, align_name( FL_ALIGN_TOP, 0 ) );
62     fl_addto_choice( choice_attrib->align,
63                      align_name( FL_ALIGN_BOTTOM, 0 ) );
64     fl_addto_choice( choice_attrib->align, align_name( FL_ALIGN_LEFT, 0 ) );
65     fl_addto_choice( choice_attrib->align, align_name( FL_ALIGN_RIGHT, 0 ) );
66 
67     fl_set_browser_dblclick_callback( choice_attrib->content_br,
68                                       change_choice_item_cb, 0 );
69 
70     return choice_attrib->choiceattrib;
71 }
72 
73 
74 /***************************************
75  ***************************************/
76 
77 void
choice_fill_in_spec_form(FL_OBJECT * obj)78 choice_fill_in_spec_form( FL_OBJECT  * obj )
79 {
80     FLI_CHOICE_SPEC *sp = obj->spec;
81     SuperSPEC *ssp = get_superspec( obj );
82     int i;
83 
84     curobj = obj;
85 
86     fl_freeze_form( choice_attrib->content_br->form );
87 
88     fl_set_button( choice_attrib->new_menuapi, ssp->new_menuapi );
89     fl_set_counter_value( choice_attrib->val, sp->val );
90     fl_set_choice_text( choice_attrib->align, align_name( sp->align, 0 ) );
91     fl_set_choice( choice_attrib->scope, ssp->global_scope + 1 );
92     fl_clear_browser( choice_attrib->content_br );
93 
94     for ( i = 1; i <= sp->numitems; i++ )
95         fl_add_browser_line( choice_attrib->content_br, sp->items[ i ] );
96 
97     fl_set_counter_bounds( choice_attrib->val, 1,
98                            sp->numitems >= 1 ? sp->numitems : 1 );
99 
100     fl_unfreeze_form( choice_attrib->content_br->form );
101 }
102 
103 
104 /***************************************
105  ***************************************/
106 
107 void
choice_emit_spec_fd_code(FILE * fp,FL_OBJECT * obj)108 choice_emit_spec_fd_code( FILE      * fp,
109                           FL_OBJECT * obj )
110 {
111     FL_OBJECT *defobj = fl_create_choice( obj->type, 0, 0, 0, 0, "" );
112     FLI_CHOICE_SPEC *sp    = obj->spec,
113                     *defsp = defobj->spec;
114     SuperSPEC *ssp    = get_superspec( obj ),
115               *defssp = get_superspec( defobj );
116     int i;
117 
118 
119     /* Create a default object */
120 
121     if ( sp->align != defsp->align )
122         fprintf( fp, "    align: %s\n", align_name( sp->align, 0 ) );
123     if ( ssp->new_menuapi != defssp->new_menuapi )
124         fprintf( fp, "    struct: %d\n", ssp->new_menuapi );
125 
126     for ( i = 1; i <= sp->numitems; i++ )
127     {
128         fprintf( fp, "    content: %s\n", sp->items[ i ] );
129         if ( sp->mode[ i ] != defsp->mode[ i ] )
130             fprintf( fp, "    mode: %s\n", get_pupmode_name( sp->mode[ i ] ) );
131         if ( sp->shortcut[ i ] && *sp->shortcut[ i ] )
132             fprintf( fp, "    shortcut: %s\n", sp->shortcut[ i ] );
133     }
134 
135     if ( sp->val != defsp->val )
136         fprintf( fp, "    value: %d\n", sp->val );
137 
138     free_superspec( defobj );
139     fl_free_object( defobj );
140 }
141 
142 
143 /***************************************
144  ***************************************/
145 
146 void
choice_emit_spec_c_code(FILE * fp,FL_OBJECT * obj)147 choice_emit_spec_c_code( FILE     * fp,
148                          FL_OBJECT * obj )
149 {
150     FL_OBJECT *defobj = fl_create_choice( obj->type, 0, 0, 0, 0, "" );
151     FLI_CHOICE_SPEC *sp    = obj->spec,
152                     *defsp = defobj->spec;
153     SuperSPEC *ssp = get_superspec( obj );
154 
155 
156     if ( sp->align != defsp->align )
157         fprintf( fp, "    fl_set_choice_align( obj, %s );\n",
158                  align_name( sp->align, 1 ) );
159 
160     if ( sp->numitems >= 1 )
161     {
162         if ( ssp->new_menuapi )
163             fprintf( fp, "    fl_set_choice_entries( obj, %s );\n",
164                      ssp->misc_char );
165         else
166         {
167             int i;
168 
169             for ( i = 1; i <= sp->numitems; i++ )
170             {
171                 fprintf( fp, "    fl_addto_choice( obj, \"%s\" );\n",
172                          sp->items[ i ] );
173                 if ( sp->mode[ i ] != defsp->mode[ i ] )
174                     fprintf( fp, "    fl_set_choice_item_mode( obj, %d, "
175                              "%s );\n", i, get_pupmode_name( sp->mode[ i ] ) );
176                 if ( sp->shortcut[ i ] && *sp->shortcut[ i ] )
177                     fprintf( fp, "    fl_set_choice_item_shortcut( obj, %d, "
178                              "\"%s\" );\n", i, sp->shortcut[ i ] );
179             }
180         }
181 
182         if ( sp->val != defsp->val )
183             fprintf( fp, "    fl_set_choice( obj, %d );\n", sp->val );
184     }
185 
186     fl_free_object( defobj );
187 }
188 
189 
190 /***************************************
191  * Callbacks and freeobj handles for form choiceattrib
192  ***************************************/
193 
194 void
add_choice_item_cb(FL_OBJECT * obj FL_UNUSED_ARG,long data FL_UNUSED_ARG)195 add_choice_item_cb( FL_OBJECT * obj   FL_UNUSED_ARG,
196                     long        data  FL_UNUSED_ARG )
197 {
198     const char *s = fl_get_input( choice_attrib->input );
199     const char *sc = fl_get_input( choice_attrib->shortcut );
200     const char *mode = fl_get_choice_text( choice_attrib->mode );
201     int i;
202 
203     if ( ! s || ! *s )
204         return;
205 
206     fl_addto_browser( choice_attrib->content_br, s );
207     i = fl_addto_choice( curobj, s );
208     fl_set_choice_item_shortcut( curobj, i, sc );
209     fl_set_choice_item_mode( curobj, i, get_pupmode_value( mode ) );
210 
211     if ( fl_get_button( choice_attrib->auto_clear ) )
212         clear_choice_field_cb( choice_attrib->auto_clear, 0 );
213 
214     redraw_the_form( 0 );
215 
216     fl_set_counter_bounds( choice_attrib->val, 1,
217                            fl_get_choice_maxitems( curobj ) );
218 }
219 
220 
221 /***************************************
222  ***************************************/
223 
224 void
replace_choice_item_cb(FL_OBJECT * obj FL_UNUSED_ARG,long data FL_UNUSED_ARG)225 replace_choice_item_cb( FL_OBJECT * obj   FL_UNUSED_ARG,
226                         long        data  FL_UNUSED_ARG )
227 {
228     int i = fl_get_browser( choice_attrib->content_br );
229     const char *s = fl_get_input( choice_attrib->input );
230     const char *sc = fl_get_input( choice_attrib->shortcut );
231     const char *mode = fl_get_choice_text( choice_attrib->mode );
232 
233     if ( ! *s || i <= 0 )
234         return;
235 
236     fl_replace_browser_line( choice_attrib->content_br, i, s );
237     fl_replace_choice( curobj, i, s );
238     fl_set_choice_item_shortcut( curobj, i, sc );
239     fl_set_choice_item_mode( curobj, i, get_pupmode_value( mode ) );
240 
241     if ( fl_get_button( choice_attrib->auto_clear ) )
242         clear_choice_field_cb( choice_attrib->auto_clear, 0 );
243 
244     redraw_the_form( 0 );
245 }
246 
247 
248 /***************************************
249  ***************************************/
250 
251 void
delete_choice_item_cb(FL_OBJECT * obj FL_UNUSED_ARG,long data FL_UNUSED_ARG)252 delete_choice_item_cb( FL_OBJECT * obj   FL_UNUSED_ARG,
253                        long        data  FL_UNUSED_ARG )
254 {
255     int i = fl_get_browser( choice_attrib->content_br );
256 
257     if ( i <= 0 )
258         return;
259 
260     fl_delete_browser_line( choice_attrib->content_br, i );
261     fl_delete_choice( curobj, i );
262     redraw_the_form( 0 );
263 }
264 
265 
266 /***************************************
267  ***************************************/
268 
269 void
change_choice_item_cb(FL_OBJECT * obj FL_UNUSED_ARG,long data FL_UNUSED_ARG)270 change_choice_item_cb( FL_OBJECT * obj   FL_UNUSED_ARG,
271                        long        data  FL_UNUSED_ARG )
272 {
273     int i = fl_get_browser( choice_attrib->content_br );
274     FLI_CHOICE_SPEC *sp = curobj->spec;
275 
276     if ( i <= 0 )
277         return;
278 
279     fl_set_input( choice_attrib->input,
280                   fl_get_browser_line( choice_attrib->content_br, i ) );
281     if ( sp->shortcut[ i ] )
282         fl_set_input( choice_attrib->shortcut, sp->shortcut[ i ] );
283     fl_set_choice_text( choice_attrib->mode,
284                         get_pupmode_name( sp->mode[ i ] ) + 3 );
285 }
286 
287 
288 /***************************************
289  ***************************************/
290 
291 void
clear_choice_field_cb(FL_OBJECT * obj FL_UNUSED_ARG,long data FL_UNUSED_ARG)292 clear_choice_field_cb( FL_OBJECT * obj   FL_UNUSED_ARG,
293                        long        data  FL_UNUSED_ARG )
294 {
295     fl_set_input( choice_attrib->input, "" );
296     fl_set_input( choice_attrib->shortcut, "" );
297     fl_set_choice( choice_attrib->mode, 1 );
298 }
299 
300 
301 /***************************************
302  ***************************************/
303 
304 void
choice_align_cb(FL_OBJECT * obj,long data FL_UNUSED_ARG)305 choice_align_cb( FL_OBJECT * obj,
306                  long        data  FL_UNUSED_ARG )
307 {
308     fl_set_choice_align( curobj, align_val( fl_get_choice_text( obj ) ) );
309     redraw_the_form( 0 );
310 }
311 
312 
313 /***************************************
314  ***************************************/
315 
316 void
choice_val_cb(FL_OBJECT * obj,long data FL_UNUSED_ARG)317 choice_val_cb( FL_OBJECT * obj,
318                long        data  FL_UNUSED_ARG )
319 {
320     fl_set_choice( curobj, FL_nint( fl_get_counter_value( obj ) ) );
321     redraw_the_form( 0 );
322 }
323 
324 
325 /***************************************
326  ***************************************/
327 
328 void
use_struct_cb(FL_OBJECT * obj,long data FL_UNUSED_ARG)329 use_struct_cb( FL_OBJECT * obj,
330                long        data  FL_UNUSED_ARG )
331 {
332     ( ( SuperSPEC * ) curobj->u_vdata )->new_menuapi = fl_get_button( obj );
333 }
334 
335 
336 /***************************************
337  ***************************************/
338 
339 void
choiceentry_scope_cb(FL_OBJECT * obj,long data FL_UNUSED_ARG)340 choiceentry_scope_cb( FL_OBJECT * obj,
341                       long        data  FL_UNUSED_ARG )
342 {
343     ( ( SuperSPEC * ) curobj->u_vdata )->global_scope =
344                                              ( fl_get_choice( obj ) - 1 ) > 0;
345 }
346 
347 
348 #include "spec/choice_spec.c"
349 
350 
351 /*
352  * Local variables:
353  * tab-width: 4
354  * indent-tabs-mode: nil
355  * End:
356  */
357