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 interaction with a canvas object.
23 *
24 * This file is part of xforms package
25 * T.C. Zhao and M. Overmars (1997)
26 *
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "include/forms.h"
34 #include <stdlib.h>
35
36
37 /**** Forms and Objects ****/
38
39 typedef struct {
40 FL_FORM * canvasform;
41 void * vdata;
42 char * cdata;
43 long ldata;
44 FL_OBJECT * canvas;
45 FL_OBJECT * br;
46 FL_OBJECT * keyboard;
47 FL_OBJECT * mouse;
48 FL_OBJECT * move;
49 FL_OBJECT * misc;
50 } FD_canvasform;
51
52 FD_canvasform *fd_canvasform;
53
54 static GC canvasGC;
55
56 /***************************************
57 ***************************************/
58
59 static int
canvas_expose(FL_OBJECT * ob FL_UNUSED_ARG,Window win,int w,int h,XEvent * ev FL_UNUSED_ARG,void * d)60 canvas_expose( FL_OBJECT * ob FL_UNUSED_ARG,
61 Window win,
62 int w,
63 int h,
64 XEvent * ev FL_UNUSED_ARG,
65 void * d )
66 {
67 FD_canvasform *ui = d;
68
69 XFillRectangle( fl_get_display( ), win, canvasGC, 0, 0, w, h );
70 fl_addto_browser( ui->br, "Expose" );
71 return 0;
72 }
73
74
75 /***************************************
76 ***************************************/
77
78 static int
canvas_key(FL_OBJECT * ob FL_UNUSED_ARG,Window win FL_UNUSED_ARG,int w FL_UNUSED_ARG,int h FL_UNUSED_ARG,XEvent * ev,void * d)79 canvas_key( FL_OBJECT * ob FL_UNUSED_ARG,
80 Window win FL_UNUSED_ARG,
81 int w FL_UNUSED_ARG,
82 int h FL_UNUSED_ARG,
83 XEvent * ev,
84 void * d )
85 {
86 FD_canvasform *ui = d;
87 char buf[ 128 ];
88 KeySym *keysymT;
89 int dummy;
90
91 keysymT = XGetKeyboardMapping( fl_display, ev->xkey.keycode, 1, &dummy);
92 sprintf( buf, "KeyPress: keysym=%ld", ( long ) keysymT[ 0 ] );
93 XFree( keysymT );
94 fl_addto_browser( ui->br, buf );
95 return 0;
96 }
97
98
99 /***************************************
100 ***************************************/
101
102 static int
canvas_but(FL_OBJECT * ob FL_UNUSED_ARG,Window win FL_UNUSED_ARG,int w FL_UNUSED_ARG,int h FL_UNUSED_ARG,XEvent * ev,void * d)103 canvas_but( FL_OBJECT * ob FL_UNUSED_ARG,
104 Window win FL_UNUSED_ARG,
105 int w FL_UNUSED_ARG,
106 int h FL_UNUSED_ARG,
107 XEvent * ev,
108 void * d )
109 {
110 FD_canvasform *ui = d;
111 char buf[ 128 ];
112
113 sprintf( buf, "Button%s: %d", ev->type == ButtonPress? "Press" : "Release",
114 ev->xbutton.button );
115 fl_addto_browser( ui->br, buf );
116 return 0;
117 }
118
119
120 /***************************************
121 ***************************************/
122
123 static int
canvas_move(FL_OBJECT * ob FL_UNUSED_ARG,Window win FL_UNUSED_ARG,int w FL_UNUSED_ARG,int h FL_UNUSED_ARG,XEvent * ev,void * d)124 canvas_move( FL_OBJECT * ob FL_UNUSED_ARG,
125 Window win FL_UNUSED_ARG,
126 int w FL_UNUSED_ARG,
127 int h FL_UNUSED_ARG,
128 XEvent * ev,
129 void * d )
130 {
131 FD_canvasform *ui = d;
132 char buf[ 128 ];
133
134 sprintf( buf, "Position: %d %d", ev->xmotion.x, ev->xmotion.y );
135 fl_addto_browser( ui->br, buf );
136 return 0;
137 }
138
139
140 /***************************************
141 ***************************************/
142
143 static int
canvas_misc(FL_OBJECT * ob FL_UNUSED_ARG,Window win FL_UNUSED_ARG,int w FL_UNUSED_ARG,int h FL_UNUSED_ARG,XEvent * ev,void * d)144 canvas_misc( FL_OBJECT * ob FL_UNUSED_ARG,
145 Window win FL_UNUSED_ARG,
146 int w FL_UNUSED_ARG,
147 int h FL_UNUSED_ARG,
148 XEvent * ev,
149 void * d )
150 {
151 FD_canvasform *ui = d;
152 fl_addto_browser( ui->br, ev->xcrossing.type == EnterNotify ?
153 "Enter canvas" : "Leave canvas" );
154 return 0;
155 }
156
157
158 /***************************************
159 ***************************************/
160
161 static void
init_canvas(FD_canvasform * fdui)162 init_canvas( FD_canvasform * fdui )
163 {
164 fl_add_canvas_handler( fdui->canvas, Expose, canvas_expose, fdui );
165 fl_add_canvas_handler( fdui->canvas, KeyPress, canvas_key, fdui );
166 fl_add_canvas_handler( fdui->canvas, ButtonPress, canvas_but, fdui );
167 fl_add_canvas_handler( fdui->canvas, ButtonRelease, canvas_but, fdui );
168 fl_set_button( fdui->mouse, 1 );
169 fl_set_button( fdui->keyboard, 1 );
170 canvasGC = XCreateGC( fl_get_display( ),fl_state[ fl_vmode ].trailblazer,
171 0, 0 );
172 XSetForeground( fl_get_display( ), canvasGC, fl_get_flcolor( FL_BLACK ) );
173 }
174
175
176 /* callbacks */
177
178 /***************************************
179 ***************************************/
180
181 static void
sensitive_setting(FL_OBJECT * ob,long event)182 sensitive_setting( FL_OBJECT * ob,
183 long event )
184 {
185 FL_HANDLE_CANVAS hc;
186 int count = 1;
187 int events[ 2 ] = { event, 0 };
188
189
190 switch ( event )
191 {
192 case KeyPress:
193 hc = canvas_key;
194 break;
195
196 case ButtonPress:
197 hc = canvas_but;
198 events[ 1 ] = ButtonRelease;
199 count = 2;
200 break;
201
202 case EnterNotify:
203 hc = canvas_misc;
204 events[ 1 ] = LeaveNotify;
205 count = 2;
206 break;
207
208 case MotionNotify:
209 hc = canvas_move;
210 break;
211
212 default:
213 return;
214 }
215
216 if ( fl_get_button( ob ) )
217 while ( count > 0 )
218 fl_add_canvas_handler( fd_canvasform->canvas, events[ --count ],
219 hc, fd_canvasform );
220 else
221 while ( count > 0 )
222 fl_remove_canvas_handler( fd_canvasform->canvas, events[ --count ],
223 hc );
224 }
225
226
227 /***************************************
228 ***************************************/
229
230 static void
disable_it(FL_OBJECT * ob,long data FL_UNUSED_ARG)231 disable_it( FL_OBJECT * ob,
232 long data FL_UNUSED_ARG )
233 {
234 if ( fl_get_button( ob ) )
235 fl_deactivate_object( fd_canvasform->canvas );
236 else
237 fl_activate_object( fd_canvasform->canvas );
238 }
239
240
241 /***************************************
242 ***************************************/
243
244 static void
hide_it(FL_OBJECT * ob,long all FL_UNUSED_ARG)245 hide_it( FL_OBJECT * ob,
246 long all FL_UNUSED_ARG )
247 {
248 if ( fl_object_is_visible( fd_canvasform->canvas ) )
249 {
250 fl_hide_object( fd_canvasform->canvas );
251 fl_set_object_label( ob, "ShowCanvas" );
252 }
253 else
254 {
255 fl_show_object( fd_canvasform->canvas );
256 fl_set_object_label( ob, "HideCanvas" );
257 }
258 }
259
260
261 /***************************************
262 ***************************************/
263
264 static void
clear_list(FL_OBJECT * ob FL_UNUSED_ARG,long what FL_UNUSED_ARG)265 clear_list( FL_OBJECT * ob FL_UNUSED_ARG,
266 long what FL_UNUSED_ARG )
267 {
268 fl_clear_browser( fd_canvasform->br );
269 }
270
271
272 /***************************************
273 ***************************************/
274
275 static FD_canvasform *
create_form_canvasform(void)276 create_form_canvasform( void )
277 {
278 FL_OBJECT *obj;
279 FD_canvasform *fdui = fl_calloc( 1, sizeof *fdui );
280
281 fdui->canvasform = fl_bgn_form( FL_NO_BOX, 450, 280 );
282
283 fl_add_box( FL_UP_BOX, 0, 0, 450, 280, "" );
284
285 fdui->canvas = fl_add_canvas( FL_NORMAL_CANVAS, 20, 40, 155, 187, "" );
286
287 fdui->br = fl_add_browser( FL_NORMAL_BROWSER, 188, 40, 152, 187, "" );
288
289 obj = fl_add_text( FL_NORMAL_TEXT, 103, 10, 150, 20, "Canvas Events" );
290 fl_set_object_lsize( obj, FL_MEDIUM_SIZE );
291 fl_set_object_lalign( obj, FL_ALIGN_CENTER );
292 fl_set_object_lstyle( obj, FL_BOLD_STYLE );
293
294 fdui->keyboard = obj = fl_add_checkbutton( FL_PUSH_BUTTON, 345, 40, 76, 26,
295 "Keyboard" );
296 fl_set_object_color( obj, FL_COL1, FL_BLUE );
297 fl_set_object_callback( obj, sensitive_setting, KeyPress );
298
299 fdui->mouse = obj = fl_add_checkbutton( FL_PUSH_BUTTON, 345, 70, 76, 26,
300 "Buttons" );
301 fl_set_object_color( obj, FL_COL1, FL_BLUE );
302 fl_set_object_callback( obj, sensitive_setting, ButtonPress );
303
304 fdui->move = obj = fl_add_checkbutton( FL_PUSH_BUTTON, 345, 100, 74, 26,
305 "Movements" );
306 fl_set_object_color( obj, FL_COL1, FL_BLUE );
307 fl_set_object_callback( obj, sensitive_setting, MotionNotify );
308
309 fdui->misc = obj = fl_add_checkbutton( FL_PUSH_BUTTON, 345, 130, 74, 26,
310 "Enter\nLeave" );
311 fl_set_object_color( obj, FL_COL1, FL_BLUE );
312 fl_set_object_callback( obj, sensitive_setting, EnterNotify );
313
314 obj = fl_add_button( FL_PUSH_BUTTON, 30, 240, 90, 27, "Deactivate" );
315 fl_set_object_callback( obj, disable_it, 0 );
316
317 obj = fl_add_button( FL_NORMAL_BUTTON, 130, 240, 90, 27, "Hide canvas" );
318 fl_set_object_callback( obj, hide_it, 0 );
319
320 obj = fl_add_button( FL_NORMAL_BUTTON, 230, 240, 90, 27, "Clear" );
321 fl_set_object_callback( obj, clear_list, 0 );
322
323 fl_add_button( FL_NORMAL_BUTTON, 330, 240, 90, 27, "Done" );
324
325 fl_end_form( );
326
327 return fdui;
328 }
329
330
331 /***************************************
332 ***************************************/
333
334 int
main(int argc,char * argv[])335 main( int argc,
336 char * argv[ ] )
337 {
338
339 fl_initialize( &argc, argv, "FormDemo", 0, 0 );
340 fd_canvasform = create_form_canvasform( );
341
342 init_canvas( fd_canvasform );
343
344 fl_show_form( fd_canvasform->canvasform,
345 FL_PLACE_FREE, FL_FULLBORDER, "canvasform" );
346
347 fl_do_forms();
348
349 fl_finish( );
350 fl_free( fd_canvasform );
351
352 return 0;
353 }
354
355
356 /*
357 * Local variables:
358 * tab-width: 4
359 * indent-tabs-mode: nil
360 * End:
361 */
362