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_pixmap.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 pixmap/bitmap class specific attributes. Should
27 * be absorbed into button class.
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "fd_main.h"
35 #include "fd_spec.h"
36 #include "fd_iconinfo.h"
37 #include <ctype.h>
38 #include "sp_pixmap.h"
39 #include "spec/pixmap_spec.h"
40
41
42 static FD_pixmapattrib *px_attrib;
43 static FL_OBJECT * curobj;
44
45 static FL_OBJECT * create_default_pixmap( FL_OBJECT * obj );
46 static void get_data_name( FL_OBJECT * obj,
47 IconInfo * inf );
48
49
50 /***************************************
51 ***************************************/
52
53 FL_FORM *
pixmap_create_spec_form(void)54 pixmap_create_spec_form( void )
55 {
56 if ( px_attrib )
57 return px_attrib->pixmapattrib;
58
59 px_attrib = create_form_pixmapattrib( );
60
61 fl_addto_choice( px_attrib->pixalign,
62 align_name( FL_ALIGN_CENTER, 0 ) );
63 fl_addto_choice( px_attrib->pixalign, align_name( FL_ALIGN_TOP, 0 ) );
64 fl_addto_choice( px_attrib->pixalign,
65 align_name( FL_ALIGN_BOTTOM, 0 ) );
66 fl_addto_choice( px_attrib->pixalign, align_name( FL_ALIGN_LEFT, 0 ) );
67 fl_addto_choice( px_attrib->pixalign, align_name( FL_ALIGN_RIGHT, 0 ) );
68 fl_show_object( px_attrib->pixalign );
69
70 fl_set_input_return( px_attrib->filename, FL_RETURN_END );
71
72 return px_attrib->pixmapattrib;
73 }
74
75
76 /***************************************
77 ***************************************/
78
79 void
pixmap_adjust_spec_form(FL_OBJECT * obj)80 pixmap_adjust_spec_form( FL_OBJECT * obj )
81 {
82 curobj = obj;
83
84 if ( obj->objclass == FL_PIXMAP )
85 {
86 fl_show_object( px_attrib->pixalign );
87 fl_set_object_label( px_attrib->frame, "Pixmap Attributes" );
88 }
89 else
90 {
91 fl_hide_object( px_attrib->pixalign );
92 fl_set_object_label( px_attrib->frame, "Bitmap Attributes" );
93 }
94 }
95
96
97 /***************************************
98 ***************************************/
99
100 void
pixmap_fill_in_spec_form(FL_OBJECT * obj)101 pixmap_fill_in_spec_form( FL_OBJECT * obj )
102 {
103 IconInfo *info = get_iconinfo( obj );
104
105 fl_set_button( px_attrib->use_data, info->use_data );
106 fl_set_button( px_attrib->fullpath, info->fullpath );
107 fl_set_choice_text( px_attrib->pixalign, align_name( info->align, 0 ) );
108 fl_set_input( px_attrib->filename, info->filename );
109 }
110
111
112 /***************************************
113 ***************************************/
114
115 void
pixmap_reread_spec_form(FL_OBJECT * obj FL_UNUSED_ARG)116 pixmap_reread_spec_form( FL_OBJECT * obj FL_UNUSED_ARG )
117 {
118 if ( curobj )
119 {
120 pixmap_filename_change( px_attrib->filename, 0 );
121 redraw_the_form( 0 );
122 }
123 }
124
125
126 /***************************************
127 ***************************************/
128
129 void
pixmap_restore_spec(FL_OBJECT * obj)130 pixmap_restore_spec( FL_OBJECT * obj )
131 {
132 IconInfo *info = get_iconinfo( obj );
133
134 if ( *info->filename )
135 {
136 if ( obj->objclass == FL_PIXMAP )
137 fl_set_pixmap_file( obj, info->filename );
138 else
139 fl_set_bitmap_file( obj, info->filename );
140 }
141 else
142 {
143 /* Show the default broken image link image */
144
145 if ( obj->objclass == FL_PIXMAP )
146 set_testing_pixmap( obj );
147 else
148 set_testing_bitmap( obj );
149 }
150
151 if ( obj->objclass == FL_PIXMAP )
152 fl_set_pixmap_align( obj, info->align, info->dx, info->dy );
153 }
154
155
156 /***************************************
157 ***************************************/
158
159 void
pixmap_emit_spec_fd_code(FILE * fp,FL_OBJECT * obj)160 pixmap_emit_spec_fd_code( FILE * fp,
161 FL_OBJECT * obj )
162 {
163 FL_OBJECT *defobj = create_default_pixmap( obj );
164 IconInfo *info = get_iconinfo( obj ),
165 *definfo;
166
167 get_superspec( defobj );
168 definfo = get_iconinfo( defobj );
169
170 get_data_name( obj, info );
171
172 if ( *info->filename )
173 {
174 fprintf( fp, " file: %s\n", info->filename );
175 fprintf( fp, " fullpath: %d\n", info->fullpath );
176 }
177
178 if ( *info->data && *info->filename )
179 fprintf( fp, " data: %s\n", info->data );
180
181 if ( *info->width )
182 fprintf( fp, " width: %s\n", info->width );
183
184 if ( *info->height )
185 fprintf( fp, " height: %s\n", info->height );
186
187 if ( info->align != definfo->align )
188 fprintf( fp, " align: %s\n",
189 align_name( fl_to_inside_lalign( info->align ), 0 ) );
190
191 free_iconinfo( defobj );
192 free_superspec( defobj );
193 fl_free_object( defobj );
194 }
195
196
197 /***************************************
198 ***************************************/
199
200 void
pixmap_emit_spec_c_code(FILE * fp,FL_OBJECT * obj)201 pixmap_emit_spec_c_code( FILE * fp,
202 FL_OBJECT * obj )
203 {
204 FL_OBJECT *defobj = create_default_pixmap( obj );
205 IconInfo *info = get_iconinfo( obj ),
206 *definfo;
207
208 get_superspec( defobj );
209 definfo = get_iconinfo( defobj );
210
211 if ( *info->filename && ! info->use_data )
212 fprintf( fp, " fl_set_%s_file( obj, \"%s\" );\n",
213 obj->objclass == FL_PIXMAP ? "pixmap" : "bitmap",
214 info->filename );
215
216 if ( *info->data && info->use_data && *info->filename )
217 {
218 if ( obj->objclass == FL_PIXMAP )
219 fprintf( fp, " fl_set_pixmap_data( obj, %s );\n",
220 info->data );
221 else
222 fprintf( fp, " fl_set_bitmap_data( obj, %s, %s, "
223 "( unsigned char * ) %s );\n",
224 info->width, info->height, info->data );
225 }
226
227 if ( obj->objclass == FL_PIXMAP && info->align != definfo->align )
228 fprintf( fp, " fl_set_pixmap_align( obj, %s, %d, %d );\n",
229 align_name( fl_to_inside_lalign(info->align ), 1 ), info->dx,
230 info->dy );
231
232 free_iconinfo( defobj );
233 free_superspec( defobj );
234 fl_free_object( defobj );
235 }
236
237
238 /***************************************
239 ***************************************/
240
241 void
pixmap_emit_spec_header(FILE * fp,FL_OBJECT * obj)242 pixmap_emit_spec_header( FILE * fp,
243 FL_OBJECT * obj )
244 {
245 IconInfo *info = get_iconinfo( obj );
246
247 if ( info->use_data && *info->data && *info->filename )
248 {
249 const char *fn = info->fullpath ? info->filename
250 : file_tail( info->filename );
251
252 if ( ! is_duplicate_info( fn ) )
253 fprintf( fp, "#include \"%s\"\n", fn );
254 }
255 }
256
257
258 /***************************************
259 ***************************************/
260
261 void
pixmapusedata_change(FL_OBJECT * obj,long data FL_UNUSED_ARG)262 pixmapusedata_change( FL_OBJECT * obj,
263 long data FL_UNUSED_ARG )
264 {
265 get_iconinfo( curobj )->use_data = fl_get_button( obj );
266 }
267
268
269 /***************************************
270 ***************************************/
271
272 void
pixmapfullpath_cb(FL_OBJECT * obj,long data FL_UNUSED_ARG)273 pixmapfullpath_cb( FL_OBJECT * obj,
274 long data FL_UNUSED_ARG )
275 {
276 get_iconinfo( curobj )->fullpath = fl_get_button( obj );
277 }
278
279
280 /***************************************
281 ***************************************/
282
283 void
pixmap_filename_change(FL_OBJECT * obj,long data FL_UNUSED_ARG)284 pixmap_filename_change( FL_OBJECT * obj,
285 long data FL_UNUSED_ARG )
286 {
287 IconInfo *info = get_iconinfo( curobj );
288
289 strcpy( info->filename, fl_get_input( obj ) );
290
291 if ( *info->filename )
292 ( curobj->objclass == FL_PIXMAP ?
293 fl_set_pixmap_file : fl_set_bitmap_file )
294 ( curobj, info->filename );
295 else
296 {
297 if ( obj->objclass == FL_PIXMAP )
298 set_testing_pixmap( obj );
299 else
300 set_testing_bitmap( obj );
301 }
302
303 redraw_the_form( 0 );
304 }
305
306
307 /***************************************
308 ***************************************/
309
310 void
pixmaplookfor_pixmapfile_cb(FL_OBJECT * obj FL_UNUSED_ARG,long data FL_UNUSED_ARG)311 pixmaplookfor_pixmapfile_cb( FL_OBJECT * obj FL_UNUSED_ARG,
312 long data FL_UNUSED_ARG )
313 {
314 const char *fn;
315 char buf[ 2048 ];
316 char *cwd;
317
318 fl_use_fselector( XPM_FSELECTOR );
319 fl_set_fselector_placement( FL_PLACE_MOUSE );
320
321 if ( curobj->objclass == FL_PIXMAP )
322 fn = fl_show_fselector( "XPM file", "", "*.xpm", "" );
323 else
324 fn = fl_show_fselector( "XBM file", "", "*.xbm", "" );
325
326 if ( ! fn )
327 return;
328
329 cwd = fli_getcwd( buf, sizeof buf - 2 );
330 if ( cwd && strstr( fn, cwd ) )
331 fn += strlen( cwd ) + 1;
332
333 fl_set_input( px_attrib->filename, fn );
334 fl_call_object_callback( px_attrib->filename );
335 }
336
337
338 /***************************************
339 ***************************************/
340
341 void
pixmapalign_change(FL_OBJECT * obj,long data FL_UNUSED_ARG)342 pixmapalign_change( FL_OBJECT * obj,
343 long data FL_UNUSED_ARG )
344 {
345 IconInfo *info = get_iconinfo( curobj );
346
347 info->align = fl_to_inside_lalign( align_val( fl_get_choice_text( obj ) ) );
348 fl_set_pixmap_align( curobj, info->align, info->dx, info->dy );
349 redraw_the_form( 0 );
350 }
351
352
353 /***************************************
354 ***************************************/
355
356 static FL_OBJECT *
create_default_pixmap(FL_OBJECT * obj)357 create_default_pixmap( FL_OBJECT * obj )
358 {
359 FL_OBJECT *defobj = NULL;
360
361 /* Create a default object */
362
363 if ( obj->objclass == FL_PIXMAP )
364 defobj = fl_create_pixmap( obj->type, 0, 0, 0, 0, "" );
365 else
366 defobj = fl_create_bitmapbutton( obj->type, 0, 0, 0, 0, "" );
367
368 return defobj;
369 }
370
371
372 /***************************************
373 ***************************************/
374
375 static void
get_data_name(FL_OBJECT * ob,IconInfo * info)376 get_data_name( FL_OBJECT * ob,
377 IconInfo * info )
378 {
379 FILE *fp;
380
381 *info->data = '\0';
382 *info->width = '\0';
383 *info->height = '\0';
384
385 if ( ! info->use_data || ! *info->filename )
386 return;
387
388 if ( ( fp = fopen( info->filename, "r" ) ) )
389 {
390 if ( ob->objclass == FL_PIXMAP )
391 get_xpm_stuff( info->data, fp );
392 else
393 get_xbm_stuff( info, fp );
394
395 fclose( fp );
396 }
397
398 if ( ! fp || ! *info->data )
399 {
400 fprintf( stderr, "Can't open or read %s\n", info->filename );
401 *info->filename = '\0';
402 }
403 }
404
405
406 #include "spec/pixmap_spec.c"
407
408
409 /*
410 * Local variables:
411 * tab-width: 4
412 * indent-tabs-mode: nil
413 * End:
414 */
415
416