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 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include <ctype.h>
24 
25 #include "fd_main.h"
26 
27 
28 /***************************************
29  * Returns a string suitable to be passed to fl_set_menu() which then
30  * initializes the menu for selection of the return type of an object.
31  * In the calling code some of the entries typically must be disabled,
32  * which can be done with a call of fl_set_menu_item_mode() with the
33  * menu object as the first, the menu item number as the second and
34  * 'FL_PUP_BOX | FL_PUP_GRAY' as the third argument.
35  ***************************************/
36 
37 void
setup_how_return_menu(FL_OBJECT * obj)38 setup_how_return_menu( FL_OBJECT * obj )
39 {
40     fl_set_menu( obj,
41                  "Never%b|"                   /* menu item 1 */
42                  "End & Changed%b|"           /* menu item 2 */
43                  "Whenever Changed%b|"        /* menu item 3 */
44                  "At End%b|"                  /* menu item 4 */
45                  "On Selection%b|"            /* menu item 5 */
46                  "On Deselection%b|"          /* menu item 6 */
47                  "Always%b" );                /* menu item 7 */
48 }
49 
50 
51 /***************************************
52  * Sets the check boxes of the menu entries of a menu as set up with
53  * the string returned by set_up_how_return_menu() (see above) according
54  * to the return settings of an object.
55  ***************************************/
56 
57 void
reset_how_return_menu(FL_OBJECT * menu,unsigned int how_return)58 reset_how_return_menu( FL_OBJECT    * menu,
59                        unsigned int how_return )
60 {
61     int i;
62     unsigned int modes[ 8 ];
63 
64     for ( i = 1; i <= 7; i++ )
65     {
66         modes[ i ] = fl_get_menu_item_mode( menu, i ) & FL_PUP_GRAY;
67         fl_set_menu_item_mode( menu, i, modes[ i ] | FL_PUP_BOX );
68     }
69 
70     if ( how_return == FL_RETURN_NONE )
71         fl_set_menu_item_mode( menu, 1,
72                                modes[ 1 ] | FL_PUP_BOX | FL_PUP_CHECK );
73     else if ( how_return == FL_RETURN_ALWAYS )
74         fl_set_menu_item_mode( menu, 7,
75                                modes[ 7 ] | FL_PUP_BOX | FL_PUP_CHECK );
76     else
77     {
78         if ( how_return & FL_RETURN_END_CHANGED )
79             fl_set_menu_item_mode( menu, 2,
80                                    modes[ 2 ] | FL_PUP_BOX | FL_PUP_CHECK );
81         if ( how_return & FL_RETURN_CHANGED )
82             fl_set_menu_item_mode( menu, 3,
83                                    modes[ 3 ] | FL_PUP_BOX | FL_PUP_CHECK );
84         if ( how_return & FL_RETURN_END )
85             fl_set_menu_item_mode( menu, 4,
86                                    modes[ 4 ] | FL_PUP_BOX | FL_PUP_CHECK );
87         if ( how_return & FL_RETURN_SELECTION )
88             fl_set_menu_item_mode( menu, 5,
89                                    modes[ 5 ] | FL_PUP_BOX | FL_PUP_CHECK );
90         if ( how_return & FL_RETURN_DESELECTION )
91             fl_set_menu_item_mode( menu, 6,
92                                    modes[ 6 ] | FL_PUP_BOX | FL_PUP_CHECK );
93     }
94 }
95 
96 
97 /***************************************
98  ***************************************/
99 
100 void
handle_how_return_changes(FL_OBJECT * menu,FL_OBJECT * target)101 handle_how_return_changes( FL_OBJECT * menu,
102                            FL_OBJECT * target )
103 {
104     int hr = FL_RETURN_NONE;
105 
106     if (    fl_get_menu_item_mode( menu, 1 ) & FL_PUP_CHECK
107          && target->how_return != FL_RETURN_NONE )
108         /* empty */ ;
109     else if (    fl_get_menu_item_mode( menu, 7 ) & FL_PUP_CHECK
110               && target->how_return != FL_RETURN_ALWAYS )
111         hr = FL_RETURN_ALWAYS;
112     else
113     {
114         if (    fl_get_menu_item_mode( menu, 2 ) & FL_PUP_CHECK
115              && ! ( target->how_return & FL_RETURN_END_CHANGED ) )
116             hr = FL_RETURN_END_CHANGED;
117         else
118         {
119             if ( fl_get_menu_item_mode( menu, 3 ) & FL_PUP_CHECK )
120                 hr |= FL_RETURN_CHANGED;
121 
122             if ( fl_get_menu_item_mode( menu, 4 ) & FL_PUP_CHECK )
123                 hr |= FL_RETURN_END;
124         }
125 
126         if ( fl_get_menu_item_mode( menu, 5 ) & FL_PUP_CHECK )
127             hr |= FL_RETURN_SELECTION;
128 
129         if ( fl_get_menu_item_mode( menu, 6 ) & FL_PUP_CHECK )
130             hr |= FL_RETURN_DESELECTION;
131     }
132 
133     fl_set_object_return( target, hr );
134 
135     reset_how_return_menu( menu, hr );
136 }
137 
138 
139 /***************************************
140  ***************************************/
141 
142 #define VN( v )  { v, #v }
143 
144 static FLI_VN_PAIR howreturn[ ] =
145 {
146     VN( FL_RETURN_NONE        ),
147     VN( FL_RETURN_END_CHANGED ),
148     VN( FL_RETURN_CHANGED     ),
149     VN( FL_RETURN_END         ),
150     VN( FL_RETURN_SELECTION   ),
151     VN( FL_RETURN_DESELECTION ),
152     VN( FL_RETURN_ALWAYS      ),
153     { -1, NULL }
154 };
155 
156 
157 /***************************************
158  ***************************************/
159 
160 int
get_how_return_val(const char * s)161 get_how_return_val( const char * s )
162 {
163     char *tmp = fl_strdup( s ),
164          *p = strtok( tmp, "|" ),
165          *st;
166     int val = 0;
167 
168     while ( p )
169     {
170         while ( *p && isspace( ( unsigned char ) *p ) )
171             p++;
172         st = p;
173         while ( *p && ! isspace( ( unsigned char ) *p ) )
174             p++;
175         *p = '\0';
176 
177         if ( ! strcmp( st, "FL_RETURN_NONE" ) )
178         {
179             val = FL_RETURN_NONE;
180             break;
181         }
182         else if ( ! strcmp( st, "FL_RETURN_ALWAYS" ) )
183         {
184             val = FL_RETURN_ALWAYS;
185             break;
186         }
187         else if ( ! strcmp( st, "FL_RETURN_END_CHANGED" ) )
188         {
189             val |= FL_RETURN_END_CHANGED;
190             val &= ~ ( FL_RETURN_CHANGED | FL_RETURN_END );
191         }
192         else if ( ! strcmp( st, "FL_RETURN_CHANGED" ) )
193         {
194             val |= FL_RETURN_CHANGED;
195             val &= ~ FL_RETURN_END_CHANGED;
196         }
197         else if ( ! strcmp( st, "FL_RETURN_END" ) )
198         {
199             val |= FL_RETURN_END;
200             val &= ~ FL_RETURN_END_CHANGED;
201         }
202         else if ( ! strcmp( st, "FL_RETURN_SELECTION" ) )
203             val |= FL_RETURN_SELECTION;
204         else if ( ! strcmp( st, "FL_RETURN_DESELECTION" ) )
205             val |= FL_RETURN_DESELECTION;
206 
207         val |= fli_get_vn_value( howreturn, st );
208         p = strtok( NULL, "|" );
209     }
210 
211     fli_safe_free( tmp );
212 
213     return val;
214 }
215 
216 
217 /***************************************
218  * Returns a string suitable for output in a C file (with 'withspaces'
219  * set to 1) or a .fd file (with 'withspaces' set to 0) of the return
220  * setting of an object as passed as the first argument.
221  ***************************************/
222 
223 const char *
get_how_return_name(unsigned int how_return,int with_spaces)224 get_how_return_name( unsigned int how_return,
225                      int          with_spaces )
226 {
227     static char buf[ 256 ];
228     FLI_VN_PAIR *hr = howreturn;
229 
230     if ( how_return == FL_RETURN_ALWAYS )
231         return "FL_RETURN_ALWAYS";
232 
233     if ( how_return == FL_RETURN_NONE )
234         return "FL_RETURN_NONE";
235 
236     *buf = '\0';
237 
238     while ( ( ++hr )->val != ( int ) FL_RETURN_ALWAYS )
239     {
240         if ( how_return & hr->val )
241             strcat( strcat( buf, with_spaces ? " | " : "|" ), hr->name );
242     }
243 
244     return buf + ( with_spaces ? 3 : 1 );
245 }
246 
247 
248 /***************************************
249  ***************************************/
250 
251 const char *
file_tail(const char * full)252 file_tail( const char * full )
253 {
254     char *p;
255 
256     if ( ( p = strrchr( full, '/' ) ) )
257         return p + 1;
258 
259     return full;
260 }
261 
262 
263 /*
264  * Local variables:
265  * tab-width: 4
266  * indent-tabs-mode: nil
267  * End:
268  */
269