1 /*
2 Copyright (c) 1990, 1994  X Consortium
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 
21 Except as contained in this notice, the name of the X Consortium shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from the X Consortium.
24  *
25  * Author:  Jim Fulton, MIT X Consortium
26  *
27  * This widget is used for press-and-hold style buttons.
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 #include <X11/IntrinsicP.h>
34 #include <X11/StringDefs.h>		/* for XtN and XtC defines */
35 #include <X11/Xaw3dxft/XawInit.h>		/* for XawInitializeWidgetSet() */
36 #include <X11/Xaw3dxft/RepeaterP.h>		/* us */
37 
38 static void tic(XtPointer, XtIntervalId *);	/* clock timeout */
39 
40 #define DO_CALLBACK(rw) \
41     XtCallCallbackList ((Widget) rw, rw->command.callbacks, (XtPointer)NULL)
42 
43 
44 #define ADD_TIMEOUT(rw,delay) \
45   XtAppAddTimeOut (XtWidgetToApplicationContext ((Widget) rw), \
46 		   (unsigned long) delay, tic, (XtPointer) rw)
47 
48 #define CLEAR_TIMEOUT(rw) \
49   if ((rw)->repeater.timer) { \
50       XtRemoveTimeOut ((rw)->repeater.timer); \
51       (rw)->repeater.timer = 0; \
52   }
53 
54 
55 /*
56  * Translations to give user interface of press-notify...-release_or_leave
57  */
58 static char defaultTranslations[] =
59   "<EnterWindow>:     highlight() \n\
60    <LeaveWindow>:     unhighlight() \n\
61    <Btn1Down>:        set() start() \n\
62    <Btn1Up>:          stop() unset() ";
63 
64 
65 /*
66  * Actions added by this widget
67  */
68 static void ActionStart(Widget, XEvent *, String *, Cardinal *);
69 static void ActionStop(Widget, XEvent *, String *, Cardinal *);
70 
71 static XtActionsRec actions[] = {
72     { "start", ActionStart },		/* trigger timers */
73     { "stop", ActionStop },		/* clear timers */
74 };
75 
76 
77 /*
78  * New resources added by this widget
79  */
80 static XtResource resources[] = {
81 #define off(field) XtOffsetOf(RepeaterRec, repeater.field)
82     { XtNdecay, XtCDecay, XtRInt, sizeof (int),
83 	off(decay), XtRImmediate, (XtPointer) REP_DEF_DECAY },
84     { XtNinitialDelay, XtCDelay, XtRInt, sizeof (int),
85 	off(initial_delay), XtRImmediate, (XtPointer) REP_DEF_INITIAL_DELAY },
86     { XtNminimumDelay, XtCMinimumDelay, XtRInt, sizeof (int),
87 	off(minimum_delay), XtRImmediate, (XtPointer) REP_DEF_MINIMUM_DELAY },
88     { XtNrepeatDelay, XtCDelay, XtRInt, sizeof (int),
89 	off(repeat_delay), XtRImmediate, (XtPointer) REP_DEF_REPEAT_DELAY },
90     { XtNflash, XtCBoolean, XtRBoolean, sizeof (Boolean),
91 	off(flash), XtRImmediate, (XtPointer) FALSE },
92     { XtNstartCallback, XtCStartCallback, XtRCallback, sizeof (XtPointer),
93 	off(start_callbacks), XtRImmediate, (XtPointer) NULL },
94     { XtNstopCallback, XtCStopCallback, XtRCallback, sizeof (XtPointer),
95 	off(stop_callbacks), XtRImmediate, (XtPointer) NULL },
96 #undef off
97 };
98 
99 
100 /*
101  * Class Methods
102  */
103 
104 static void Initialize(Widget, Widget, ArgList, Cardinal *);
105 static void Destroy(Widget);
106 static Boolean SetValues(Widget, Widget, Widget, ArgList, Cardinal *);
107 
108 RepeaterClassRec repeaterClassRec = {
109   { /* core fields */
110     /* superclass		*/	(WidgetClass) &commandClassRec,
111     /* class_name		*/	"Repeater",
112     /* widget_size		*/	sizeof(RepeaterRec),
113     /* class_initialize		*/	XawInitializeWidgetSet,
114     /* class_part_initialize	*/	NULL,
115     /* class_inited		*/	FALSE,
116     /* initialize		*/	Initialize,
117     /* initialize_hook		*/	NULL,
118     /* realize			*/	XtInheritRealize,
119     /* actions			*/	actions,
120     /* num_actions		*/	XtNumber(actions),
121     /* resources		*/	resources,
122     /* num_resources		*/	XtNumber(resources),
123     /* xrm_class		*/	NULLQUARK,
124     /* compress_motion		*/	TRUE,
125     /* compress_exposure	*/	TRUE,
126     /* compress_enterleave	*/	TRUE,
127     /* visible_interest		*/	FALSE,
128     /* destroy			*/	Destroy,
129     /* resize			*/	XtInheritResize,
130     /* expose			*/	XtInheritExpose,
131     /* set_values		*/	SetValues,
132     /* set_values_hook		*/	NULL,
133     /* set_values_almost	*/	XtInheritSetValuesAlmost,
134     /* get_values_hook		*/	NULL,
135     /* accept_focus		*/	NULL,
136     /* version			*/	XtVersion,
137     /* callback_private		*/	NULL,
138     /* tm_table			*/	defaultTranslations,
139     /* query_geometry		*/	XtInheritQueryGeometry,
140     /* display_accelerator	*/	XtInheritDisplayAccelerator,
141     /* extension		*/	NULL
142   },
143   { /* simple fields */
144     /* change_sensitive		*/	XtInheritChangeSensitive
145   },
146   { /* threeD fields */
147     /* shadowdraw		*/	XtInheritXaw3dShadowDraw
148   },
149   { /* label fields */
150     /* ignore			*/	0
151   },
152   { /* command fields */
153     /* ignore			*/	0
154   },
155   { /* repeater fields */
156     /* ignore                   */	0
157   }
158 };
159 
160 WidgetClass repeaterWidgetClass = (WidgetClass) &repeaterClassRec;
161 
162 
163 /*****************************************************************************
164  *                                                                           *
165  *			   repeater utility routines                         *
166  *                                                                           *
167  *****************************************************************************/
168 
169 /* ARGSUSED */
170 static void
tic(XtPointer client_data,XtIntervalId * id)171 tic (XtPointer client_data, XtIntervalId *id)
172 {
173     RepeaterWidget rw = (RepeaterWidget) client_data;
174 
175     rw->repeater.timer = 0;		/* timer is removed */
176     if (rw->repeater.flash) {
177 	XtExposeProc expose;
178 	expose = repeaterWidgetClass->core_class.superclass->core_class.expose;
179 	XClearWindow (XtDisplay((Widget) rw), XtWindow((Widget) rw));
180 	rw->command.set = FALSE;
181 	(*expose) ((Widget) rw, (XEvent *) NULL, (Region) NULL);
182 	XClearWindow (XtDisplay((Widget) rw), XtWindow((Widget) rw));
183 	rw->command.set = TRUE;
184 	(*expose) ((Widget) rw, (XEvent *) NULL, (Region) NULL);
185     }
186     DO_CALLBACK (rw);
187 
188     rw->repeater.timer = ADD_TIMEOUT (rw, rw->repeater.next_delay);
189 
190 					/* decrement delay time, but clamp */
191     if (rw->repeater.decay) {
192 	rw->repeater.next_delay -= rw->repeater.decay;
193 	if (rw->repeater.next_delay < rw->repeater.minimum_delay)
194 	  rw->repeater.next_delay = rw->repeater.minimum_delay;
195     }
196 }
197 
198 
199 /*****************************************************************************
200  *                                                                           *
201  * 			    repeater class methods                           *
202  *                                                                           *
203  *****************************************************************************/
204 
205 /* ARGSUSED */
206 static void
Initialize(Widget greq,Widget gnew,ArgList args,Cardinal * num_args)207 Initialize (Widget greq, Widget gnew, ArgList args, Cardinal *num_args)
208 {
209     RepeaterWidget new = (RepeaterWidget) gnew;
210 
211     if (new->repeater.minimum_delay < 0) new->repeater.minimum_delay = 0;
212     new->repeater.timer = (XtIntervalId) 0;
213 }
214 
215 static void
Destroy(Widget gw)216 Destroy (Widget gw)
217 {
218     CLEAR_TIMEOUT ((RepeaterWidget) gw);
219 }
220 
221 /* ARGSUSED */
222 static Boolean
SetValues(Widget gcur,Widget greq,Widget gnew,ArgList args,Cardinal * num_args)223 SetValues (Widget gcur, Widget greq, Widget gnew, ArgList args, Cardinal *num_args)
224 {
225     RepeaterWidget cur = (RepeaterWidget) gcur;
226     RepeaterWidget new = (RepeaterWidget) gnew;
227     Boolean redisplay = FALSE;
228 
229     if (cur->repeater.minimum_delay != new->repeater.minimum_delay) {
230 	if (new->repeater.next_delay < new->repeater.minimum_delay)
231 	  new->repeater.next_delay = new->repeater.minimum_delay;
232     }
233 
234     return redisplay;
235 }
236 
237 /*****************************************************************************
238  *                                                                           *
239  * 			     repeater action procs                           *
240  *                                                                           *
241  *****************************************************************************/
242 
243 /* ARGSUSED */
244 static void
ActionStart(Widget gw,XEvent * event,String * params,Cardinal * num_params)245 ActionStart (Widget gw, XEvent *event, String *params, Cardinal *num_params)
246 {
247     RepeaterWidget rw = (RepeaterWidget) gw;
248 
249     CLEAR_TIMEOUT (rw);
250     if (rw->repeater.start_callbacks)
251       XtCallCallbackList (gw, rw->repeater.start_callbacks, (XtPointer)NULL);
252 
253     DO_CALLBACK (rw);
254     rw->repeater.timer = ADD_TIMEOUT (rw, rw->repeater.initial_delay);
255     rw->repeater.next_delay = rw->repeater.repeat_delay;
256 }
257 
258 
259 /* ARGSUSED */
260 static void
ActionStop(Widget gw,XEvent * event,String * params,Cardinal * num_params)261 ActionStop (Widget gw, XEvent *event, String *params, Cardinal *num_params)
262 {
263     RepeaterWidget rw = (RepeaterWidget) gw;
264 
265     CLEAR_TIMEOUT ((RepeaterWidget) gw);
266     if (rw->repeater.stop_callbacks)
267       XtCallCallbackList (gw, rw->repeater.stop_callbacks, (XtPointer)NULL);
268 }
269 
270