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 fd_groups.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  * This file is part of the Form Designer.
27  *
28  * It maintains a list of groups in the form. Each group can be
29  * assigned a name.
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <ctype.h>
40 
41 #include "fd_main.h"
42 #include "fd/ui_theforms.h"
43 
44 
45 /**** Data Structure maintaining groups in current form ****/
46 
47 static FL_OBJECT **begobj = NULL;     /* Begin objects of the groups */
48 
49 
50 /**** Call-back routines ****/
51 
52 
53 /***************************************
54  * Sets the current group in the form
55  ***************************************/
56 
57 void
group_cb(FL_OBJECT * obj FL_UNUSED_ARG,long arg FL_UNUSED_ARG)58 group_cb( FL_OBJECT * obj  FL_UNUSED_ARG,
59           long        arg  FL_UNUSED_ARG )
60 {
61     int n = fl_get_browser( fd_control->groupbrowser );
62 
63     cur_class = -1;
64 
65     fl_deselect_browser( fd_control->objectbrowser );
66     reset_pallette( );
67     cur_class = -1;
68 
69     if ( n > 0 )
70     {
71         /* In order to stay consistent with the rest of the interface
72            (where adding to the selection requires the <Shift> button
73            to be kept pressed down) we clear the selection of the
74            <Shift> button isn't pressed */
75 
76         if ( ! ( fl_last_event( )->xmotion.state & ShiftMask ) )
77             clear_selection( );
78         addgroupto_selection( begobj[ n ] );
79     }
80     else if ( n < 0 )
81         deletegroupfrom_selection( begobj[ -n ] );
82     redraw_the_form( 0 );
83 }
84 
85 
86 /***************************************
87  * Changes the name of the current group
88  ***************************************/
89 
90 void
changegroupname_cb(FL_OBJECT * obj FL_UNUSED_ARG,long arg FL_UNUSED_ARG)91 changegroupname_cb( FL_OBJECT * obj  FL_UNUSED_ARG,
92                     long        arg  FL_UNUSED_ARG )
93 {
94     char name[ MAX_VAR_LEN ];
95     int i,
96         numb = 0;
97     const char *s;
98 
99     if ( cur_form == NULL )
100         return;
101 
102     for ( i = 1; i <= fl_get_browser_maxline( fd_control->groupbrowser ); i++ )
103         if ( fl_isselected_browser_line( fd_control->groupbrowser, i ) )
104         {
105             if ( numb != 0 )
106             {
107                 fl_show_messages( "Cannot change name of multiple groups" );
108                 return;
109             }
110             numb = i;
111         }
112 
113     if ( numb == 0 )
114     {
115         fl_show_messages( "No group is selected" );
116         return;
117     }
118 
119     get_object_name( begobj[ numb ], name, NULL, NULL );
120 
121  get_changed_group_name:
122 
123     if ( ! ( s = fl_show_input( "Group name (must be usable as "
124                                 "a C variable or empty):", name ) ) )
125         return;
126 
127     if ( *s && ! is_valid_c_name( s ) )
128     {
129         fl_show_alert( "Error", "Invalid C identifier specified for group "
130                        "name:", s, 0 );
131         goto get_changed_group_name;
132     }
133 
134     strcpy( name, s );
135     set_object_name( begobj[ numb ], name, NULL, NULL );
136     fillin_groups( );
137     changed = FL_TRUE;
138 }
139 
140 
141 /***************************************
142  * Fills in the current list of groups
143  ***************************************/
144 
145 void
fillin_groups(void)146 fillin_groups( void )
147 {
148     char name[ MAX_VAR_LEN ];
149     FL_OBJECT *obj,
150               *obj2;
151     int i;
152 
153     if ( cur_form == NULL )
154     {
155         fl_clear_browser( fd_control->groupbrowser );
156         return;
157     }
158 
159     fl_freeze_form( fd_control->groupbrowser->form );
160     fl_clear_browser( fd_control->groupbrowser );
161 
162     /* Check whether groups are empty. */
163 
164     obj = cur_form->first;
165     while ( obj != NULL )
166     {
167         obj2 = obj;
168         obj = obj->next;
169         if (    obj2->objclass == FL_END_GROUP
170              && obj2->prev != NULL
171              && obj2->prev->objclass == FL_BEGIN_GROUP )
172             fl_delete_object( obj2->prev );
173     }
174 
175     /* Put the still existing groups in the list */
176 
177     for ( i = 0, obj = cur_form->first; obj; obj = obj->next )
178         if ( obj->objclass == FL_BEGIN_GROUP )
179             i++;
180 
181     if ( i > 0 )
182     {
183         begobj = fl_realloc( begobj, ( i + 1 ) * sizeof *begobj );
184 
185         for ( i = 1, obj = cur_form->first; obj; obj = obj->next )
186             if ( obj->objclass == FL_BEGIN_GROUP )
187             {
188                 begobj[ i ] = obj;
189                 get_object_name( obj, name, NULL, NULL );
190                 if ( ! *name )
191                     strcpy( name, "<no name>" );
192                 fl_add_browser_line( fd_control->groupbrowser, name );
193                 if ( is_selected( obj ) )
194                     fl_select_browser_line( fd_control->groupbrowser, i );
195             }
196             else if ( obj->objclass == FL_END_GROUP )
197                 i++;
198         }
199 
200     fl_unfreeze_form( fd_control->groupbrowser->form );
201 }
202 
203 
204 /*
205  * Local variables:
206  * tab-width: 4
207  * indent-tabs-mode: nil
208  * End:
209  */
210