File: colbrowser.c

package info (click to toggle)
libforms 1.0.93sp1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,548 kB
  • ctags: 9,107
  • sloc: ansic: 97,227; sh: 9,236; makefile: 858
file content (435 lines) | stat: -rw-r--r-- 10,173 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
 *  This file is part of XForms.
 *
 *  XForms is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation; either version 2.1, or
 *  (at your option) any later version.
 *
 *  XForms is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with XForms; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
 *  MA 02111-1307, USA.
 */


/*
 * Test browser/slider and color handling. Select a colorname,
 * the program will show the color. Change the sliders, the
 * program will pick a colorname that has the closest color.
 *
 *  This file is part of xforms package.
 *  T.C. Zhao and M. Overmars  (1997)
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "include/forms.h"
#include <stdlib.h>

#define MAX_RGB 3000

static FL_FORM *cl;
static FL_OBJECT *rescol,
                 *dbobj,
                 *colbr,
                 *rs,
                 *gs,
                 *bs;
char dbname[ FL_PATH_MAX] ;
static void create_form_cl( void );
static int load_browser( char * );

/* the RGB data file does not have a standard location on unix. */

#ifdef __VMS
  static char *rgbfile = "SYS$MANAGER:DECW$RGB.DAT";
#else
#ifdef __EMX__   /* OS2 */
  static char *rgbfile = "/XFree86/lib/X11/rgb.txt";
#else
  static char *rgbfile = "/usr/lib/X11/rgb.txt";
#endif
#endif

typedef struct { int r,
	                 g,
	                 b;
} RGBdb;

static RGBdb rgbdb[ MAX_RGB ];


/***************************************
 ***************************************/

int
main( int    argc,
	  char * argv[ ] )
{
    fl_initialize( &argc, argv, "FormDemo", 0, 0 );

    create_form_cl( );
    strcpy( dbname, rgbfile );

    if ( load_browser( dbname ) )
		fl_set_object_label( dbobj, dbname );
	else
		fl_set_object_label( dbobj, "None" );

    fl_set_form_minsize( cl, cl->w , cl->h );
    fl_set_form_maxsize( cl, 2 * cl->w , 2 * cl->h );
    fl_show_form( cl, FL_PLACE_FREE, FL_TRANSIENT, "RGB Browser" );

	fl_do_forms( );

    return 0;
}


/***************************************
 ***************************************/

static void
set_entry( int i )
{
    RGBdb *db = rgbdb + i;

    fl_freeze_form( cl );
    fl_mapcolor( FL_FREE_COL4 + i, db->r, db->g, db->b );
    fl_mapcolor( FL_FREE_COL4, db->r, db->g, db->b );
    fl_set_slider_value( rs, db->r );
    fl_set_slider_value( gs, db->g );
    fl_set_slider_value( bs, db->b );
    fl_redraw_object( rescol );
    fl_unfreeze_form( cl );
}


/***************************************
 ***************************************/

static void
br_cb( FL_OBJECT * ob,
	   long        q   FL_UNUSED_ARG )
{
    int r = fl_get_browser( ob );

    if ( r <= 0 )
		return;
    set_entry( r - 1 );
}


/***************************************
 ***************************************/

static int
read_entry( FILE * fp,
			int  * r,
			int  * g,
			int  * b,
			char * name )
{
    int  n;
    char buf[ 512 ],
		 *p;

    if ( ! fgets( buf, sizeof buf, fp ) )
		return 0;

    if ( *buf == '!' )
		if ( ! fgets( buf, sizeof buf, fp ) )
			 return 0;

    if ( sscanf( buf, " %d %d %d %n", r, g, b, &n ) != 3 )
		return 0;

    p = buf + n;

    /* Remove all spaces */

    while ( *p )
    {
		if ( * p != ' ' && *p != '\n' )
			*name++ = *p;
		p++;
    }
    *name = '\0';

    return ! ( feof( fp ) || ferror( fp ) );
}


/***************************************
 ***************************************/

static int
load_browser( char * fname )
{
    FILE *fp;
    RGBdb *db = rgbdb,
		  *dbs = db + MAX_RGB;
    int r,
		g,
		b,
		lr = -1,
		lg = -1,
		lb = -1;
    char name[ 256 ],
		buf[ 256 ];

#ifdef __EMX__
    extern char *__XOS2RedirRoot( const char * );
    if ( ! ( fp = fopen( __XOS2RedirRoot( fname ), "r" ) ) )
#else
    if ( ! ( fp = fopen( fname, "r" ) ) )
#endif
    {
		fl_show_alert( "Load", fname, "Can't open database file", 0 );
		return 0;
    }

    /* read the items */

    fl_freeze_form( cl );

    while ( db < dbs && read_entry( fp, &r, &g, &b, name ) )
    {
		db->r = r;
		db->g = g;
		db->b = b;

		/* unique the entries on the fly */

		if ( lr != r || lg != g || lb != b )
		{
			db++;
			lr = r;
			lg = g;
			lb = b;
			sprintf( buf, "(%3d %3d %3d) %s", r, g, b, name );
			fl_addto_browser( colbr, buf );
		}
    }

    fclose( fp );

    if ( db < dbs )
		db->r = 1000;		/* sentinel */
    else
    {
		db--;
		db->r = 1000;
    }

    fl_set_browser_topline( colbr, 1 );
    fl_select_browser_line( colbr, 1 );
    set_entry( 0 );
    fl_unfreeze_form( cl );

    return 1;
}


/***************************************
 ***************************************/

static int
search_entry( int r,
			  int g,
			  int b )
{
    RGBdb *db = rgbdb;
    int i,
		j,
		diffr,
		diffg,
		diffb;
    unsigned int diff,
		         mindiff;

    mindiff = ~0;

    for ( i = j = 0; db->r < 256; db++, i++ )
    {
		diffr = r - db->r;
		diffg = g - db->g;
		diffb = b - db->b;

#ifdef FL_LINEAR
		diff = ( int ) ( 3.0 * FL_abs( r - db->r ) +
						 5.9 * FL_abs( g - db->g ) +
						 1.1 * FL_abs(b - db->b  ) );
#else
        diff = ( int ) ( 3.0 * ( diffr * diffr ) +
						 5.9 * ( diffg * diffg ) +
						 1.1 * ( diffb * diffb ) );
#endif

		if ( mindiff > diff )
		{
			mindiff = diff;
			j = i;
		}
    }

    return j;
}


/***************************************
 ***************************************/

static void
search_rgb( FL_OBJECT * ob  FL_UNUSED_ARG,
			long        q   FL_UNUSED_ARG )
{
    int r,
		g,
		b,
		i;
    int top  = fl_get_browser_topline( colbr );

    r = ( int ) fl_get_slider_value( rs );
    g = ( int ) fl_get_slider_value( gs );
    b = ( int ) fl_get_slider_value( bs );

    fl_freeze_form( cl );
    fl_mapcolor( FL_FREE_COL4, r, g, b );
    fl_redraw_object( rescol );
    i = search_entry( r, g, b );

    /* change topline only if necessary */

    if ( i < top || i > top + 15 )
		fl_set_browser_topline( colbr, i - 8 );
    fl_select_browser_line( colbr, i + 1 );
    fl_unfreeze_form( cl );
}


/***************************************
 * change database
 ***************************************/

static void
db_cb( FL_OBJECT * ob,
	   long        q   FL_UNUSED_ARG )
{
    const char *p = fl_show_input( "Enter new database name", dbname );
    char buf[ 512 ];

    if ( ! p || strcmp( p, dbname ) == 0 )
		return;

    strcpy( buf, p );
    if ( load_browser( buf ) )
	{
		strcpy( dbname, buf );
		fl_set_object_label( ob, dbname );
	}
}


/***************************************
 ***************************************/

static void
done_cb( FL_OBJECT * ob  FL_UNUSED_ARG,
		 long        q   FL_UNUSED_ARG )
{
    fl_finish( );
    exit( 0 );
}


/***************************************
 ***************************************/

static void
create_form_cl( void )
{
    FL_OBJECT *obj;

    if ( cl )
		return;

    cl = fl_bgn_form( FL_NO_BOX, 330, 385 );

    obj = fl_add_box( FL_UP_BOX, 0, 0, 330, 385, "" );
    fl_set_object_color( obj, FL_COL1, FL_COL1 );

    obj = fl_add_box( FL_NO_BOX, 40, 10, 250, 30, "Color Browser" );
    fl_set_object_lcol( obj, FL_RED );
    fl_set_object_lsize( obj, FL_HUGE_SIZE );
    fl_set_object_lstyle( obj, FL_BOLD_STYLE + FL_SHADOW_STYLE );
	fl_set_object_gravity( obj, FL_North, FL_North );
	fl_set_object_resize( obj, FL_RESIZE_NONE );

    dbobj = obj = fl_add_button( FL_NORMAL_BUTTON, 40, 50, 250, 25, "" );
    fl_set_object_boxtype( obj, FL_BORDER_BOX );
    fl_set_object_color( obj,
						 fl_get_visual_depth( )==1 ? FL_WHITE:  FL_COL1,
						  FL_COL1 );
    fl_set_object_callback( obj, db_cb, 0 );
	fl_set_object_gravity( obj, FL_North, FL_North );
	fl_set_object_resize( obj, FL_RESIZE_X );

    rescol = obj = fl_add_box( FL_FLAT_BOX, 225, 90, 90, 35, "" );
    fl_set_object_color( obj, FL_FREE_COL4, FL_FREE_COL4 );
    fl_set_object_boxtype( obj, FL_BORDER_BOX );
	fl_set_object_resize( obj, FL_RESIZE_NONE );
	fl_set_object_gravity( obj, FL_NorthEast, FL_East );

    rs = obj = fl_add_valslider( FL_VERT_FILL_SLIDER, 225, 130, 30, 200, "" );
    fl_set_object_color( obj,  FL_COL1, FL_RED );
    fl_set_slider_bounds( obj, 0, 255 );
    fl_set_slider_precision( obj, 0 );
    fl_set_object_callback( obj, search_rgb, 0 );
    fl_set_slider_return( obj, 0 );
	fl_set_object_resize( obj, FL_RESIZE_Y );
	fl_set_object_gravity( obj, FL_NorthEast, FL_SouthEast );
	fl_set_object_return( obj, FL_RETURN_CHANGED );

    gs = obj = fl_add_valslider( FL_VERT_FILL_SLIDER, 255, 130, 30, 200, "" );
    fl_set_object_color( obj,  FL_COL1, FL_GREEN );
    fl_set_slider_bounds( obj, 0, 255 );
    fl_set_slider_precision( obj, 0 );
    fl_set_object_callback( obj, search_rgb, 1 );
    fl_set_slider_return( obj, 0 );
	fl_set_object_resize( obj, FL_RESIZE_Y );
	fl_set_object_gravity( obj, FL_NorthEast, FL_SouthEast );
	fl_set_object_return( obj, FL_RETURN_CHANGED );

    bs = obj = fl_add_valslider( FL_VERT_FILL_SLIDER, 285, 130, 30, 200, "" );
    fl_set_object_color( obj,  FL_COL1, FL_BLUE );
    fl_set_slider_bounds( obj, 0, 255 );
    fl_set_slider_precision( obj, 0 );
    fl_set_object_callback( obj, search_rgb, 2 );
    fl_set_slider_return( obj, 0 );
	fl_set_object_resize( obj, FL_RESIZE_Y );
	fl_set_object_gravity( obj, FL_NorthEast, FL_SouthEast );
	fl_set_object_return( obj, FL_RETURN_CHANGED );

    colbr = obj = fl_add_browser( FL_HOLD_BROWSER, 10, 90, 205, 240, "" );
    fl_set_browser_fontstyle( obj, FL_FIXED_STYLE );
    fl_set_object_callback( obj, br_cb, 0 );
	fl_set_object_gravity( obj, FL_NorthWest, FL_SouthEast );

    obj = fl_add_button( FL_NORMAL_BUTTON, 135, 345, 80, 30, "Done" );
    fl_set_object_callback( obj, done_cb, 0 );
	fl_set_object_gravity( obj, FL_South, FL_South );
	fl_set_object_resize( obj, FL_RESIZE_NONE );

    fl_end_form( );

    fl_scale_form( cl, 1.1, 1.0 );
}