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_names.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 contains the routines that keep track of the names and
29  * callback routines associated to the different objects.
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include <string.h>
37 #include <stdio.h>
38 #include "fd_main.h"
39 
40 /****
41  * Structure for storing names connected with an object
42  ****/
43 
44 typedef struct
45 {
46     FL_OBJECT * obj;                        /* The object */
47     char        name[    MAX_VAR_LEN ];     /* Its name (if any) */
48     char        cbname[  MAX_VAR_LEN ];     /* Callback Routine */
49     char        argname[ MAX_VAR_LEN ];     /* The argument */
50 } OBJ;
51 
52 static OBJ * objects   = NULL;      /* The stored objects */
53 static int num_objects = 0;         /* Their number */
54 
55 
56 /***************************************
57  * Deletes the list of object name structures
58  ***************************************/
59 
60 void
reset_object_list(void)61 reset_object_list( void )
62 {
63     fli_safe_free( objects );
64     num_objects = 0;
65 }
66 
67 
68 /***************************************
69  * Returns the index of the object in the list (or -1 if it's not found)
70  ***************************************/
71 
72 static int
get_object_numb(const FL_OBJECT * obj)73 get_object_numb( const FL_OBJECT * obj )
74 {
75     int i;
76 
77     for ( i = 0; i < num_objects; i++ )
78         if ( objects[ i ].obj == obj )
79             return i;
80 
81     return -1;
82 }
83 
84 
85 /***************************************
86  ***************************************/
87 
88 static void
check_names(int on)89 check_names( int on )
90 {
91     /* Fill in argument if missing */
92 
93     if ( *objects[ on ].cbname && ! *objects[ on ].argname)
94         strcpy( objects[ on ].argname, "0" );
95 }
96 
97 
98 /***************************************
99  * Returns the names associated with an object
100  ***************************************/
101 
102 void
get_object_name(const FL_OBJECT * obj,char * name,char * cbname,char * argname)103 get_object_name( const FL_OBJECT * obj,
104                  char            * name,
105                  char            * cbname,
106                  char            * argname)
107 {
108     int on = get_object_numb( obj );
109 
110     if ( name )
111         strcpy( name, on == -1 ? "" : objects[ on ].name );
112 
113     if ( cbname )
114         strcpy( cbname, on == -1 ? "" : objects[ on ].cbname );
115 
116     if ( argname )
117         strcpy( argname, on == -1 ? "" : objects[ on ].argname );
118 }
119 
120 
121 /***************************************
122  * Sets the names belonging to an object
123  ***************************************/
124 
125 void
set_object_name(FL_OBJECT * obj,const char * name,const char * cbname,const char * argname)126 set_object_name( FL_OBJECT  * obj,
127                  const char * name,
128                  const char * cbname,
129                  const char * argname )
130 {
131     int on;
132 
133     if ( obj == NULL )
134         return;
135 
136     if ( ( on = get_object_numb( obj ) ) == -1 )
137     {
138         if (    ( ! name    || ! *name    )
139              && ( ! cbname  || ! *cbname  )
140              && ( ! argname || ! *argname ) )
141             return;
142 
143         objects = fl_realloc( objects, ++num_objects * sizeof *objects );
144         on = num_objects - 1;
145 
146         objects[ on ].obj = obj;
147         *objects[ on ].name = *objects[ on ].cbname
148                             = *objects[ on ].argname = '\0';
149     }
150 
151     if ( name )
152         fli_sstrcpy( objects[ on ].name, name,
153                      sizeof objects[ on ].name );
154 
155     if ( cbname)
156         fli_sstrcpy( objects[ on ].cbname, cbname,
157                      sizeof objects[ on ].cbname );
158 
159     if ( argname )
160         fli_sstrcpy( objects[ on ].argname, argname,
161                      sizeof objects[ on ].argname  );
162 
163     check_names( on );
164 }
165 
166 
167 /***************************************
168  ***************************************/
169 
170 const char *
get_object_c_name(FL_OBJECT * obj)171 get_object_c_name( FL_OBJECT * obj )
172 {
173     int on = get_object_numb( obj );
174 
175     return on == -1 ? NULL : objects[ on ].name;
176 }
177 
178 
179 /*
180  * Local variables:
181  * tab-width: 4
182  * indent-tabs-mode: nil
183  * End:
184  */
185