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 preemptive and post-object handler,
23  * and one possible way of implementing a "tool tip"
24  *
25  *  This file is part of xforms package
26  *  T.C. Zhao and M. Overmars (1997)
27  *
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #include "include/forms.h"
35 #include <stdlib.h>
36 
37 typedef struct {
38     FL_FORM   * form0;
39     void      * vdata;
40     char      * cdata;
41     long        ldata;
42     FL_OBJECT * butt;
43     FL_OBJECT * enter;
44     FL_OBJECT * leave;
45     FL_OBJECT * push;
46     FL_OBJECT * release;
47     FL_OBJECT * peek;
48     FL_OBJECT * override;
49     FL_OBJECT * event;
50     FL_OBJECT * done;
51 } FD_form0;
52 
53 extern FD_form0 *create_form_form0( void );
54 
55 FD_form0 *fd_form0;
56 
57 
58 /***************************************
59  * Which event to take over is better kept in a state varible
60  * even though query of the status via fl_get_button is cheap
61  ***************************************/
62 
63 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)64 preemptive_handler( FL_OBJECT * ob   FL_UNUSED_ARG,
65                     int         event,
66                     FL_Coord    mx   FL_UNUSED_ARG,
67                     FL_Coord    my   FL_UNUSED_ARG,
68                     int         key  FL_UNUSED_ARG,
69                     void *      xev  FL_UNUSED_ARG )
70 {
71     int override = fl_get_button( fd_form0->override );
72     char buf[ 128 ];
73     char *what = override ? "preempted" : "detected";
74 
75     switch( event )
76     {
77         case FL_ENTER:
78             if ( fl_get_button(fd_form0->enter ) )
79             {
80                 sprintf( buf,"%s %s", "FL_ENTER", what );
81                 fl_set_object_label( fd_form0->event, buf );
82                 return override ? FL_PREEMPT : 0;
83             }
84             break;
85 
86         case FL_LEAVE:
87             if ( fl_get_button( fd_form0->leave ) )
88             {
89                 sprintf( buf,"%s %s", "FL_LEAVE", what);
90                 fl_set_object_label( fd_form0->event, buf );
91                 return override ? FL_PREEMPT : 0;
92          }
93          break;
94 
95         case FL_PUSH:
96         case FL_MOTION: /* one of the quirks of the button class */
97             if ( fl_get_button( fd_form0->push ) )
98             {
99                 sprintf( buf,"%s %s", "FL_PUSH", what );
100                 fl_set_object_label( fd_form0->event, buf );
101                 return override ? FL_PREEMPT : 0;
102             }
103             break;
104 
105         case FL_RELEASE:
106             if ( fl_get_button( fd_form0->release ) )
107             {
108                 sprintf( buf,"%s %s", "FL_RELEASE", what );
109                 fl_set_object_label( fd_form0->event, buf );
110                 return override ? FL_PREEMPT : 0;
111          }
112          break;
113     }
114 
115     return 0;
116 }
117 
118 
119 #define INTERVAL  800  /* wait this long before showing tip */
120 static int timeoutID;  /* we can also use ob->u_ldata to hold it */
121 
122 /***************************************
123  ***************************************/
124 
125 static void
do_tips(int id FL_UNUSED_ARG,void * p)126 do_tips( int    id  FL_UNUSED_ARG,
127          void * p )
128 {
129     FL_OBJECT *ob = p;
130 
131     fl_show_oneliner( ob->u_vdata, ob->form->x + ob->x,
132                       ob->form->y + ob->y + ob->h + 1 );
133     timeoutID = fl_add_timeout( INTERVAL, do_tips, ob );
134 }
135 
136 
137 /***************************************
138  * Use the post handler as a tipper
139  ***************************************/
140 
141 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)142 post_handler( FL_OBJECT * ob,
143               int         event,
144               FL_Coord    mx   FL_UNUSED_ARG,
145               FL_Coord    my   FL_UNUSED_ARG,
146               int         key  FL_UNUSED_ARG,
147               void *      xev  FL_UNUSED_ARG )
148 {
149     if ( ! ob->u_vdata )
150         return 0;
151 
152     if ( event == FL_ENTER )
153         timeoutID = fl_add_timeout( INTERVAL,do_tips, ob );
154     else if ( event == FL_LEAVE || event == FL_PUSH )
155     {
156         fl_hide_oneliner( );
157         if( timeoutID )
158         {
159             fl_remove_timeout( timeoutID );
160             timeoutID = 0;
161         }
162      }
163 
164      return 0;
165 }
166 
167 
168 /***************************************
169  ***************************************/
170 
171 void
set_tip(FL_OBJECT * ob,char * s)172 set_tip( FL_OBJECT * ob,
173          char *      s )
174 {
175     ob->u_vdata = s;
176     fl_set_object_posthandler( ob, post_handler );
177 }
178 
179 
180 /***************************************
181  ***************************************/
182 
183 int
main(int argc,char * argv[])184 main( int    argc,
185       char * argv[ ] )
186 {
187     fl_initialize( &argc, argv, "FormDemo", 0, 0 );
188 
189     fd_form0 = create_form_form0( );
190 
191     /* Fill-in form initialization code */
192 
193     fl_set_button( fd_form0->peek, 1 );
194     fl_set_button( fd_form0->enter, 1 );
195     fl_set_button( fd_form0->leave, 1 );
196     fl_set_button( fd_form0->push, 1 );
197     fl_set_button( fd_form0->release, 1 );
198 
199     fl_set_object_prehandler( fd_form0->butt, preemptive_handler );
200 
201     set_tip( fd_form0->done, "Want to quit ?\nPress me" );
202     set_tip( fd_form0->peek, "Turn preempting off" );
203     set_tip( fd_form0->override, "Turn preempting on" );
204 
205     /* Show the first form */
206 
207     fl_show_form( fd_form0->form0, FL_PLACE_CENTER, FL_TRANSIENT,
208                   "Preemptive" );
209 
210     while ( fl_do_forms( ) != fd_form0->done )
211         /* empty */ ;
212 
213     return 0;
214 }
215 
216 
217 /***************************************
218  ***************************************/
219 
220 FD_form0 *
create_form_form0(void)221 create_form_form0( void )
222 {
223     FL_OBJECT *obj;
224     FD_form0 *fdui = fl_calloc(1, sizeof *fdui );
225 
226     fdui->form0 = fl_bgn_form(FL_NO_BOX, 320, 250);
227 
228     fl_add_box( FL_UP_BOX, 0, 0, 320, 250, "" );
229 
230     fl_add_frame( FL_ENGRAVED_FRAME, 200, 70, 95, 100, "" );
231 
232     fdui->butt = fl_add_button( FL_NORMAL_BUTTON, 20, 70, 170, 100,
233                                 "A Button" );
234 
235     fdui->enter = fl_add_checkbutton( FL_PUSH_BUTTON, 210, 70, 45, 30,
236                                       "Enter" );
237 
238     fdui->leave = fl_add_checkbutton( FL_PUSH_BUTTON, 210, 95, 40, 30,
239                                       "Leave" );
240 
241     fdui->push = fl_add_checkbutton( FL_PUSH_BUTTON, 210, 120, 50, 30, "Push" );
242 
243     fdui->release = fl_add_checkbutton( FL_PUSH_BUTTON, 210, 140, 60, 30,
244                                         "Release" );
245 
246     obj = fl_add_text( FL_NORMAL_TEXT, 55, 15, 220, 30, "Pre-emptive Handler" );
247     fl_set_object_lsize( obj, FL_MEDIUM_SIZE );
248     fl_set_object_lalign( obj, FL_ALIGN_CENTER );
249     fl_set_object_lstyle( obj, FL_BOLD_STYLE );
250 
251     fdui->peek = obj = fl_add_checkbutton( FL_RADIO_BUTTON, 150, 40, 35, 30,
252                                            "Peek" );
253     fl_set_object_color( obj, FL_COL1, FL_BLUE );
254 
255     fdui->override = obj = fl_add_checkbutton( FL_RADIO_BUTTON, 210, 40, 35, 30,
256                                                "Override" );
257     fl_set_object_color( obj, FL_COL1, FL_BLUE );
258 
259     fdui->event = fl_add_box( FL_FLAT_BOX, 40, 180, 245, 25, "" );
260 
261     fdui->done = fl_add_button( FL_NORMAL_BUTTON, 170, 210, 100, 30, "Done" );
262 
263     fl_end_form( );
264 
265     return fdui;
266 }
267 
268 
269 /*
270  * Local variables:
271  * tab-width: 4
272  * indent-tabs-mode: nil
273  * End:
274  */
275