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
15 * License along with XForms; see the file COPYING. If not, write to
16 * the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
17 * MA 02111-1307, USA.
18 */
19
20
21 /*
22 * Demo showing the use of a free obejct
23 *
24 * This file is part of xforms package
25 * T.C. Zhao and M. Overmars
26 *
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <stdlib.h>
34 #include "include/forms.h"
35
36 /**** Forms and Objects ****/
37
38 typedef struct {
39 FL_FORM * drawfree;
40 void * vdata;
41 char * cdata;
42 long ldata;
43 FL_OBJECT * freeobj;
44 FL_OBJECT * figgrp;
45 FL_OBJECT * colgrp;
46 FL_OBJECT * colorobj;
47 FL_OBJECT * rsli;
48 FL_OBJECT * gsli;
49 FL_OBJECT * bsli;
50 FL_OBJECT * miscgrp;
51 FL_OBJECT * sizegrp;
52 FL_OBJECT * hsli;
53 FL_OBJECT * wsli;
54 FL_OBJECT * drobj[ 3 ];
55 } FD_drawfree;
56
57 extern FD_drawfree * create_form_drawfree( void );
58
59 static FD_drawfree * drawui;
60 extern void draw_initialize( FD_drawfree * );
61
62 static int max_w = 150,
63 max_h = 150;
64 static Display *dpy;
65
66
67 /***************************************
68 ***************************************/
69
70 int
main(int argc,char * argv[])71 main( int argc,
72 char * argv[ ] )
73 {
74 dpy = fl_initialize( &argc, argv, "FormDemo", 0, 0 );
75 drawui = create_form_drawfree( );
76 fl_set_object_color( drawui->colorobj, FL_FREE_COL1, FL_FREE_COL1 );
77 draw_initialize( drawui );
78 fl_show_form( drawui->drawfree, FL_PLACE_CENTER | FL_FREE_SIZE,
79 FL_FULLBORDER, "FreeObject" );
80 fl_do_forms( );
81
82 fl_free( drawui );
83 fl_finish( );
84 return 0;
85 }
86
87
88 /* Structure mantainace */
89
90 typedef void ( *DrawFunc )( int, int, int, int, int, unsigned long );
91
92 /***************************************
93 ***************************************/
94
95 void
draw_triangle(int fill,int x,int y,int w,int h,unsigned long col)96 draw_triangle( int fill,
97 int x,
98 int y,
99 int w,
100 int h,
101 unsigned long col )
102 {
103 XPoint xpoint[ 4 ];
104 GC gc = fl_state[ fl_vmode ].gc[ 0 ];
105 Window win = fl_winget( );
106
107 xpoint[ 0 ].x = x;
108 xpoint[ 1 ].x = x + w / 2;
109 xpoint[ 2 ].x = x + w - 1;
110
111 xpoint[ 0 ].y = y + h - 1;
112 xpoint[ 1 ].y = y;
113 xpoint[ 2 ].y = y + h - 1;
114
115 XSetForeground( dpy, gc, fl_get_pixel( col ) );
116
117 if ( fill )
118 XFillPolygon( dpy, win, gc, xpoint, 3, Nonconvex, Unsorted );
119 else
120 {
121 xpoint[ 3 ].x = xpoint[ 0 ].x;
122 xpoint[ 3 ].y = xpoint[ 0 ].y;
123 XDrawLines( dpy, win, gc, xpoint, 4, CoordModeOrigin );
124 }
125 }
126
127
128 static DrawFunc drawfunc[ ] = { fl_oval,
129 fl_rectangle,
130 draw_triangle
131 };
132
133 typedef struct
134 {
135 DrawFunc drawit;
136 int x,
137 y,
138 w,
139 h,
140 fill,
141 c[ 3 ];
142 int newfig;
143 FL_COLOR col;
144 } DrawFigure;
145
146 static DrawFigure saved_figure[ 800 ],
147 *cur_fig;
148
149 /***************************************
150 ***************************************/
151
draw_initialize(FD_drawfree * ui)152 void draw_initialize( FD_drawfree * ui )
153 {
154 fl_set_form_minsize( ui->drawfree, 530, 490);
155 fl_set_object_gravity( ui->colgrp, WestGravity, WestGravity );
156 fl_set_object_gravity( ui->sizegrp, SouthWestGravity, SouthWestGravity );
157 fl_set_object_gravity( ui->figgrp, NorthWestGravity, NorthWestGravity );
158 fl_set_object_gravity( ui->miscgrp, SouthGravity, SouthGravity );
159 fl_set_object_resize( ui->miscgrp, FL_RESIZE_NONE );
160
161 cur_fig = saved_figure;
162 cur_fig->c[ 0 ] = cur_fig->c[ 1 ] = cur_fig->c[ 2 ] = 127,
163 cur_fig->w = cur_fig->h = 30;
164 cur_fig->drawit = fl_oval;
165 cur_fig->fill = 1;
166 cur_fig->col = FL_FREE_COL1 + 1;
167
168 fl_mapcolor( FL_FREE_COL1,
169 cur_fig->c[ 0 ], cur_fig->c[ 1 ], cur_fig->c[ 2 ] );
170 fl_mapcolor( cur_fig->col,
171 cur_fig->c[ 0 ], cur_fig->c[ 1 ], cur_fig->c[ 2 ] );
172
173 fl_set_slider_bounds( ui->wsli, 1, max_w );
174 fl_set_slider_bounds( ui->hsli, 1, max_h );
175 fl_set_slider_precision( ui->wsli, 0 );
176 fl_set_slider_precision( ui->hsli, 0 );
177 fl_set_slider_value( ui->wsli, cur_fig->w );
178 fl_set_slider_value( ui->hsli, cur_fig->h );
179
180 /* color sliders */
181
182 fl_set_slider_bounds( ui->rsli, 1.0, 0 );
183 fl_set_slider_bounds( ui->gsli, 1.0, 0 );
184 fl_set_slider_bounds( ui->bsli, 1.0, 0 );
185
186 /* intial drawing function */
187
188 fl_set_button( ui->drobj[ 0 ], 1 );
189
190 /* Setup the color slider so we can find out colorobject from
191 the callback funtions. This is not necessary as drawui
192 is static, this is done to show how to access other objects
193 from an object callback function */
194
195 ui->rsli->u_vdata = ui;
196 ui->gsli->u_vdata = ui;
197 ui->bsli->u_vdata = ui;
198 }
199
200
201 /***************************************
202 ***************************************/
203
204 void
switch_object(FL_OBJECT * ob FL_UNUSED_ARG,long which)205 switch_object( FL_OBJECT * ob FL_UNUSED_ARG,
206 long which )
207 {
208 cur_fig->drawit = drawfunc[ which ];
209 }
210
211
212 /***************************************
213 ***************************************/
214
215 void
change_color(FL_OBJECT * ob,long which)216 change_color( FL_OBJECT * ob,
217 long which )
218 {
219 cur_fig->c[ which ] = fl_get_slider_value(ob) * 255.01;
220 fl_mapcolor( cur_fig->col,
221 cur_fig->c[ 0 ], cur_fig->c[ 1 ], cur_fig->c[ 2 ] );
222 fl_mapcolor( FL_FREE_COL1,
223 cur_fig->c[ 0 ], cur_fig->c[ 1 ], cur_fig->c[ 2 ] );
224 fl_redraw_object( ( ( FD_drawfree * ) ob->u_vdata )->colorobj );
225 }
226
227
228 /***************************************
229 ***************************************/
230
231 void
fill_cb(FL_OBJECT * ob,long notused FL_UNUSED_ARG)232 fill_cb( FL_OBJECT * ob,
233 long notused FL_UNUSED_ARG )
234 {
235 cur_fig->fill = !fl_get_button( ob );
236 }
237
238
239 /***************************************
240 ***************************************/
241
242 void
change_size(FL_OBJECT * ob,long which)243 change_size( FL_OBJECT * ob,
244 long which )
245 {
246 if ( which == 0 )
247 cur_fig->w = fl_get_slider_value( ob );
248 else
249 cur_fig->h = fl_get_slider_value( ob );
250 }
251
252
253 /***************************************
254 ***************************************/
255
256 void
refresh_cb(FL_OBJECT * ob FL_UNUSED_ARG,long which FL_UNUSED_ARG)257 refresh_cb( FL_OBJECT * ob FL_UNUSED_ARG,
258 long which FL_UNUSED_ARG )
259 {
260 fl_redraw_object( drawui->freeobj );
261 }
262
263
264 /***************************************
265 ***************************************/
266
267 void
clear_cb(FL_OBJECT * ob FL_UNUSED_ARG,long notused FL_UNUSED_ARG)268 clear_cb( FL_OBJECT * ob FL_UNUSED_ARG,
269 long notused FL_UNUSED_ARG )
270 {
271 saved_figure[ 0 ] = *cur_fig;
272 cur_fig = saved_figure;
273 fl_redraw_object( drawui->freeobj );
274 }
275
276
277 /***************************************
278 * The routine that does drawing
279 ***************************************/
280
281 int
freeobject_handler(FL_OBJECT * ob,int event,FL_Coord mx,FL_Coord my,int key,void * xev FL_UNUSED_ARG)282 freeobject_handler( FL_OBJECT * ob,
283 int event,
284 FL_Coord mx,
285 FL_Coord my,
286 int key,
287 void * xev FL_UNUSED_ARG )
288 {
289 DrawFigure *dr;
290
291 switch ( event )
292 {
293 case FL_DRAW:
294 if ( cur_fig->newfig == 1 )
295 {
296 cur_fig->drawit( cur_fig->fill,
297 cur_fig->x + ob->x,
298 cur_fig->y + ob->y,
299 cur_fig->w, cur_fig->h, cur_fig->col );
300 }
301 else
302 {
303 fl_draw_box( ob->boxtype, ob->x, ob->y, ob->w,
304 ob->h, ob->col1, ob->bw );
305
306 for ( dr = saved_figure; dr < cur_fig; dr++ )
307 {
308 dr->drawit( dr->fill, dr->x + ob->x,
309 dr->y + ob->y,
310 dr->w, dr->h, dr->col);
311 }
312 }
313 cur_fig->newfig = 0;
314 break;
315
316 case FL_PUSH:
317 if ( key != 2 )
318 {
319 cur_fig->x = mx - cur_fig->w / 2;
320 cur_fig->y = my - cur_fig->h / 2;
321
322 /* convert position to relative to the free object */
323
324 cur_fig->x -= ob->x;
325 cur_fig->y -= ob->y;
326
327 cur_fig->newfig = 1;
328 fl_redraw_object( ob );
329 *(cur_fig+1) = *cur_fig;
330 fl_mapcolor( cur_fig->col + 1, cur_fig->c[ 0 ],
331 cur_fig->c[ 1 ], cur_fig->c[ 2 ] );
332 cur_fig++;
333 cur_fig->col++;
334 }
335 break;
336 }
337
338 return 0;
339 }
340
341
342 /***************************************
343 ***************************************/
344
345 FD_drawfree *
create_form_drawfree(void)346 create_form_drawfree( void )
347 {
348 FL_OBJECT *obj;
349 FD_drawfree *fdui = fl_calloc(1, sizeof *fdui );
350
351 fdui->drawfree = fl_bgn_form( FL_NO_BOX, 530, 490 );
352
353 fl_add_box( FL_UP_BOX, 0, 0, 530, 490, "" );
354
355 obj = fl_add_frame( FL_DOWN_FRAME, 145, 30, 370, 405, "" );
356 fl_set_object_gravity( obj, FL_NorthWest, FL_SouthEast );
357
358 fdui->freeobj = obj = fl_add_free( FL_NORMAL_FREE, 145, 30, 370, 405,"",
359 freeobject_handler );
360 fl_set_object_gravity( obj, FL_NorthWest, FL_SouthEast );
361
362 obj = fl_add_checkbutton( FL_PUSH_BUTTON, 15, 25, 100, 35, "Outline" );
363 fl_set_object_color( obj, FL_MCOL,FL_BLUE );
364 fl_set_object_gravity( obj, FL_NorthWest, FL_NorthWest );
365 fl_set_object_callback( obj, fill_cb, 0 );
366
367 fdui->figgrp = fl_bgn_group( );
368
369 fdui->drobj[ 0 ] = obj = fl_add_button( FL_RADIO_BUTTON, 10, 60, 40, 40,
370 "@#circle" );
371 fl_set_object_lcolor( obj, FL_YELLOW );
372 fl_set_object_callback( obj, switch_object, 0 );
373
374 fdui->drobj[ 1 ] = obj = fl_add_button( FL_RADIO_BUTTON, 50, 60, 40, 40,
375 "@#square" );
376 fl_set_object_lcolor( obj, FL_YELLOW );
377 fl_set_object_callback( obj, switch_object, 1 );
378
379 fdui->drobj[ 2 ] = obj = fl_add_button( FL_RADIO_BUTTON, 90, 60, 40, 40,
380 "@#8>" );
381 fl_set_object_lcolor( obj, FL_YELLOW );
382 fl_set_object_callback( obj, switch_object, 2 );
383 fl_end_group( );
384
385 fdui->colgrp = fl_bgn_group();
386
387 fdui->colorobj = fl_add_box( FL_BORDER_BOX, 25, 140, 90, 25, "" );
388
389 fdui->rsli = obj = fl_add_slider( FL_VERT_FILL_SLIDER, 25, 170, 30, 125,
390 "" );
391 fl_set_object_color( obj, FL_COL1, FL_RED );
392 fl_set_object_callback( obj, change_color, 0 );
393 fl_set_slider_return( obj, FL_RETURN_CHANGED );
394
395 fdui->gsli = obj = fl_add_slider( FL_VERT_FILL_SLIDER, 55, 170, 30, 125,
396 "" );
397 fl_set_object_color( obj, FL_COL1, FL_GREEN );
398 fl_set_object_callback( obj, change_color, 1 );
399 fl_set_slider_return( obj, FL_RETURN_CHANGED );
400
401 fdui->bsli = obj = fl_add_slider( FL_VERT_FILL_SLIDER, 85, 170, 30, 125,
402 "" );
403 fl_set_object_color( obj, FL_COL1, FL_BLUE );
404 fl_set_object_callback( obj, change_color, 2 );
405 fl_set_slider_return( obj, FL_RETURN_CHANGED );
406
407 fl_end_group( );
408
409 fdui->miscgrp = fl_bgn_group( );
410
411 obj = fl_add_button( FL_NORMAL_BUTTON, 420, 455, 105, 30, "Quit");
412 fl_set_button_shortcut( obj,"Qq#q", 1 );
413
414 obj = fl_add_button( FL_NORMAL_BUTTON, 280, 445, 105, 30, "Refresh" );
415 fl_set_object_callback( obj, refresh_cb, 0 );
416
417 obj = fl_add_button( FL_NORMAL_BUTTON, 165, 445, 105, 30, "Clear" );
418 fl_set_object_callback( obj, clear_cb, 0 );
419
420 fl_end_group( );
421
422 fdui->sizegrp = fl_bgn_group( );
423
424 fdui->hsli = obj = fl_add_valslider( FL_HOR_SLIDER, 15, 410, 120, 25,
425 "Height" );
426 fl_set_object_lalign( obj, FL_ALIGN_TOP );
427 fl_set_object_callback( obj, change_size, 1 );
428 fl_set_slider_return( obj, FL_RETURN_CHANGED );
429
430 fdui->wsli = obj = fl_add_valslider( FL_HOR_SLIDER, 15, 370, 120, 25,
431 "Width" );
432 fl_set_object_lalign( obj, FL_ALIGN_TOP );
433 fl_set_object_callback( obj, change_size, 0 );
434 fl_set_slider_return( obj, FL_RETURN_CHANGED );
435
436 fl_end_group( );
437
438 fl_end_form( );
439
440 return fdui;
441 }
442
443
444 /*
445 * Local variables:
446 * tab-width: 4
447 * indent-tabs-mode: nil
448 * End:
449 */
450