File: preemptive.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 (266 lines) | stat: -rw-r--r-- 6,674 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
/*
 *  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.
 */


/*
 * Demo showing the use of preemptive and post-object handler,
 * and one possible way of implementing a "tool tip"
 *
 *  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>

typedef struct {
	FL_FORM   * form0;
	void      * vdata;
	char      * cdata;
	long        ldata;
	FL_OBJECT * butt;
	FL_OBJECT * enter;
	FL_OBJECT * leave;
	FL_OBJECT * push;
	FL_OBJECT * release;
	FL_OBJECT * peek;
	FL_OBJECT * override;
	FL_OBJECT * event;
	FL_OBJECT * done;
} FD_form0;

extern FD_form0 *create_form_form0( void );

FD_form0 *fd_form0;


/***************************************
 * Which event to take over is better kept in a state varible
 * even though query of the status via fl_get_button is cheap
 ***************************************/

int
preemptive_handler( FL_OBJECT * ob   FL_UNUSED_ARG,
					int         event,
					FL_Coord    mx   FL_UNUSED_ARG,
					FL_Coord    my   FL_UNUSED_ARG,
					int         key  FL_UNUSED_ARG,
					void *      xev  FL_UNUSED_ARG )
{
    int override = fl_get_button( fd_form0->override );
    char buf[ 128 ];
    char *what = override ? "preempted" : "detected";

    switch( event )
    {
		case FL_ENTER:
			if ( fl_get_button(fd_form0->enter ) )
			{
				sprintf( buf,"%s %s", "FL_ENTER", what );
				fl_set_object_label( fd_form0->event, buf );
				return override ? FL_PREEMPT : 0;
			}
			break;

		case FL_LEAVE:
			if ( fl_get_button( fd_form0->leave ) )
			{
				sprintf( buf,"%s %s", "FL_LEAVE", what);
				fl_set_object_label( fd_form0->event, buf );
				return override ? FL_PREEMPT : 0;
         }
         break;

		case FL_PUSH:
		case FL_MOTION: /* one of the quirks of the button class */
			if ( fl_get_button( fd_form0->push ) )
			{
				sprintf( buf,"%s %s", "FL_PUSH", what );
				fl_set_object_label( fd_form0->event, buf );
				return override ? FL_PREEMPT : 0;
			}
			break;

		case FL_RELEASE:
			if ( fl_get_button( fd_form0->release ) )
			{
				sprintf( buf,"%s %s", "FL_RELEASE", what );
				fl_set_object_label( fd_form0->event, buf );
				return override ? FL_PREEMPT : 0;
         }
         break;
    }

    return 0;
}


#define INTERVAL  800  /* wait this long before showing tip */
static int timeoutID;  /* we can also use ob->u_ldata to hold it */

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

static void
do_tips( int    id  FL_UNUSED_ARG,
		 void * p )
{
	FL_OBJECT *ob = p;

	fl_show_oneliner( ob->u_vdata, ob->form->x + ob->x,
					  ob->form->y + ob->y + ob->h + 1 );
	timeoutID = fl_add_timeout( INTERVAL, do_tips, ob );
}


/***************************************
 * Use the post handler as a tipper
 ***************************************/

int
post_handler( FL_OBJECT * ob,
			  int         event,
			  FL_Coord    mx   FL_UNUSED_ARG,
			  FL_Coord    my   FL_UNUSED_ARG,
			  int         key  FL_UNUSED_ARG,
			  void *      xev  FL_UNUSED_ARG )
{
	if ( ! ob->u_vdata )
		return 0;

	if ( event == FL_ENTER )
		timeoutID = fl_add_timeout( INTERVAL,do_tips, ob );
	else if ( event == FL_LEAVE || event == FL_PUSH )
	{
		fl_hide_oneliner( );
		if( timeoutID )
		{
            fl_remove_timeout( timeoutID );
            timeoutID = 0;
		}
     }

     return 0;
}


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

void
set_tip( FL_OBJECT * ob,
		 char *      s )
{
    ob->u_vdata = s;
    fl_set_object_posthandler( ob, post_handler );
}


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

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

	fd_form0 = create_form_form0( );

	/* Fill-in form initialization code */

	fl_set_button( fd_form0->peek, 1 );
	fl_set_button( fd_form0->enter, 1 );
	fl_set_button( fd_form0->leave, 1 );
	fl_set_button( fd_form0->push, 1 );
	fl_set_button( fd_form0->release, 1 );

	fl_set_object_prehandler( fd_form0->butt, preemptive_handler );

	set_tip( fd_form0->done, "Want to quit ?\nPress me" );
	set_tip( fd_form0->peek, "Turn preempting off" );
	set_tip( fd_form0->override, "Turn preempting on" );

	/* Show the first form */

	fl_show_form( fd_form0->form0, FL_PLACE_CENTER, FL_TRANSIENT,
				  "Preemptive" );

	while ( fl_do_forms( ) != fd_form0->done )
		/* empty */ ;

	return 0;
}


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

FD_form0 *
create_form_form0( void )
{
	FL_OBJECT *obj;
	FD_form0 *fdui = fl_calloc(1, sizeof *fdui );

	fdui->form0 = fl_bgn_form(FL_NO_BOX, 320, 250);

	fl_add_box( FL_UP_BOX, 0, 0, 320, 250, "" );

	fl_add_frame( FL_ENGRAVED_FRAME, 200, 70, 95, 100, "" );

	fdui->butt = fl_add_button( FL_NORMAL_BUTTON, 20, 70, 170, 100,
								"A Button" );

	fdui->enter = fl_add_checkbutton( FL_PUSH_BUTTON, 210, 70, 45, 30,
									  "Enter" );

	fdui->leave = fl_add_checkbutton( FL_PUSH_BUTTON, 210, 95, 40, 30,
									  "Leave" );

	fdui->push = fl_add_checkbutton( FL_PUSH_BUTTON, 210, 120, 50, 30, "Push" );

	fdui->release = fl_add_checkbutton( FL_PUSH_BUTTON, 210, 140, 60, 30,
										"Release" );

	obj = fl_add_text( FL_NORMAL_TEXT, 55, 15, 220, 30, "Pre-emptive Handler" );
    fl_set_object_lsize( obj, FL_MEDIUM_SIZE );
    fl_set_object_lalign( obj, FL_ALIGN_CENTER );
    fl_set_object_lstyle( obj, FL_BOLD_STYLE );

	fdui->peek = obj = fl_add_checkbutton( FL_RADIO_BUTTON, 150, 40, 35, 30,
										   "Peek" );
    fl_set_object_color( obj, FL_COL1, FL_BLUE );

	fdui->override = obj = fl_add_checkbutton( FL_RADIO_BUTTON, 210, 40, 35, 30,
											   "Override" );
    fl_set_object_color( obj, FL_COL1, FL_BLUE );

	fdui->event = fl_add_box( FL_FLAT_BOX, 40, 180, 245, 25, "" );

	fdui->done = fl_add_button( FL_NORMAL_BUTTON, 170, 210, 100, 30, "Done" );

	fl_end_form( );

	return fdui;
}