File: BarGauge.c

package info (click to toggle)
xsysinfo 1.7-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 180 kB
  • ctags: 118
  • sloc: ansic: 787; sh: 129; makefile: 44
file content (365 lines) | stat: -rw-r--r-- 11,650 bytes parent folder | download | duplicates (8)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * BarGauge: Widget to periodically display values in form of a horizontal bar.
 * loosely based on Athena StripChart Widget.
 *
 * Author: Gabor Herr		herr@iti.informatik.th-darmstadt.de
 *
 * $Id: BarGauge.c,v 1.3 1993/02/07 23:54:15 gabor Exp $
 */

/***********************************************************
Copyright 1992, 1993 by Gabor Herr
Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
and the Massachusetts Institute of Technology, Cambridge, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Gabor Herr or Digital or MIT not
be used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

GABOR HERR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
GABOR HERR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

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

#include <stdio.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/XawInit.h>
#include "BarGaugeP.h"
#include <X11/Xfuncs.h>

#ifdef USE_MILLI_SECONDS       /* (ThMO) */
#  define MS_PER_SEC 1         /* value already expressed in ms (ThMO) */
#else
#  define MS_PER_SEC 1000
#endif

#define min(a,b)	((a) < (b) ? (a) : (b))

/* Private Data */

#define offset(field) XtOffsetOf(BarGaugeRec, field)

static XtResource resources[] = {
	{XtNwidth, XtCWidth, XtRDimension, sizeof(Dimension),
	 offset(core.width), XtRImmediate, (XtPointer) 120},
	{XtNheight, XtCHeight, XtRDimension, sizeof(Dimension),
	 offset(core.height), XtRImmediate, (XtPointer) 12},
	{XtNautoAdjust, XtCAutoAdjust, XtRBoolean, sizeof(Boolean),
	 offset(bar_gauge.auto_adjust), XtRImmediate, (XtPointer) False},
	{XtNsegments, XtCSegments, XtRInt, sizeof(int),
	 offset(bar_gauge.segments), XtRImmediate, (XtPointer) 1},
	{XtNsegmentGap, XtCSegmentGap, XtRInt, sizeof(int),
	 offset(bar_gauge.segment_gap), XtRImmediate, (XtPointer) 2},
	{XtNupdate, XtCInterval, XtRInt, sizeof(int),
#ifdef USE_MILLI_SECONDS       /* (ThMO) */
	 offset(bar_gauge.update), XtRImmediate, (XtPointer) 500},
#else
	 offset(bar_gauge.update), XtRImmediate, (XtPointer) 10},
#endif
        {XtNgetValue, XtCCallback, XtRCallback, sizeof(XtPointer),
	 offset(bar_gauge.get_value), XtRImmediate, (XtPointer) NULL},
	{XtNmaxValue, XtCMaxValue, XtRFloat, sizeof(float),
	 offset(bar_gauge.max_value), XtRString, (XtPointer) "1.0"},
	{XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
	 offset(bar_gauge.fgpixel[0]), XtRString, XtDefaultForeground},
	{XtNforeground1, XtCForeground, XtRPixel, sizeof(Pixel),
	 offset(bar_gauge.fgpixel[1]), XtRString, XtDefaultForeground},
	{XtNforeground2, XtCForeground, XtRPixel, sizeof(Pixel),
	 offset(bar_gauge.fgpixel[2]), XtRString, XtDefaultForeground},
	{XtNforeground3, XtCForeground, XtRPixel, sizeof(Pixel),
	 offset(bar_gauge.fgpixel[3]), XtRString, XtDefaultForeground},
	{XtNforeground4, XtCForeground, XtRPixel, sizeof(Pixel),
	 offset(bar_gauge.fgpixel[4]), XtRString, XtDefaultForeground},
	{XtNforeground5, XtCForeground, XtRPixel, sizeof(Pixel),
	 offset(bar_gauge.fgpixel[5]), XtRString, XtDefaultForeground},
	{XtNforeground6, XtCForeground, XtRPixel, sizeof(Pixel),
	 offset(bar_gauge.fgpixel[6]), XtRString, XtDefaultForeground},
	{XtNforeground7, XtCForeground, XtRPixel, sizeof(Pixel),
	 offset(bar_gauge.fgpixel[7]), XtRString, XtDefaultForeground},
};

#undef offset

static void Initialize(), Destroy(), Redisplay();
static Boolean SetValues();
static void repaint_window();

BarGaugeClassRec barGaugeClassRec = {
	{ /* core fields */
		(WidgetClass) &simpleClassRec, /* superclass		*/
		"BarGauge",                    /* class_name		*/
		sizeof(BarGaugeRec),           /* size			*/
		XawInitializeWidgetSet,        /* class_initialize	*/
		NULL,                          /* class_part_initialize	*/
		FALSE,                         /* class_inited		*/
		Initialize,                    /* initialize		*/
		NULL,                          /* initialize_hook	*/
		XtInheritRealize,              /* realize		*/
		NULL,                          /* actions		*/
		0,                             /* num_actions		*/
		resources,                     /* resources		*/
		XtNumber(resources),           /* num_resources		*/
		NULLQUARK,                     /* xrm_class		*/
		TRUE,                          /* compress_motion	*/
		XtExposeCompressMultiple |
		XtExposeGraphicsExposeMerged,  /* compress_exposure	*/
		TRUE,                          /* compress_enterleave	*/
		FALSE,                         /* visible_interest	*/
		Destroy,                       /* destroy		*/
		NULL,                          /* resize		*/
		Redisplay,                     /* expose		*/
		SetValues,                     /* set_values		*/
		NULL,                          /* set_values_hook	*/
		NULL,                          /* set_values_almost	*/
		NULL,                          /* get_values_hook	*/
		NULL,                          /* accept_focus		*/
		XtVersion,                     /* version		*/
		NULL,                          /* callback_private	*/
		NULL,                          /* tm_table		*/
		XtInheritQueryGeometry,        /* query_geometry	*/
		XtInheritDisplayAccelerator,   /* display_accelerator	*/
		NULL                           /* extension		*/
	},
	{ /* Simple class fields */
		XtInheritChangeSensitive       /* change_sensitive	*/
	}
};

WidgetClass barGaugeWidgetClass = (WidgetClass) &barGaugeClassRec;

/****************************************************************
 *
 * Private Procedures
 *
 ****************************************************************/

static void draw_it();

/* ARGSUSED */
static void Initialize (greq, gnew)
	Widget greq, gnew;
{
	BarGaugeWidget w = (BarGaugeWidget)gnew;
	XGCValues gcval;
	int i;

	if (w->bar_gauge.update > 0)
		w->bar_gauge.interval_id = XtAppAddTimeOut
			( XtWidgetToApplicationContext(gnew),
			  w->bar_gauge.update * MS_PER_SEC, 
			  draw_it, (XtPointer) gnew );

	if (w->bar_gauge.segments > XG_NUM_SEGMENTS ||
	    w->bar_gauge.auto_adjust )
		w->bar_gauge.segments = XG_NUM_SEGMENTS;

	/* reset values */
	for ( i = 0; i < XG_NUM_SEGMENTS; i++ )
		w->bar_gauge.last_values[i] = 0.0;
					
	/* create GCs for each active segment */
	for ( i = 0; i < w->bar_gauge.segments; i++ ) {
		gcval.foreground = w->bar_gauge.fgpixel[i];
		w->bar_gauge.fgGC[i] = XtGetGC( (Widget) w, GCForeground,
						&gcval );
	}
}
 
static void Destroy (gw)
	Widget gw;
{
	BarGaugeWidget w = (BarGaugeWidget)gw;
	int i;

	if (w->bar_gauge.update > 0)
		XtRemoveTimeOut (w->bar_gauge.interval_id);

	for ( i = 0; i < w->bar_gauge.segments; i++ )
		XtReleaseGC( (Widget) w, w->bar_gauge.fgGC[i] );
}

/*
 * NOTE: This function really needs to recieve graphics exposure 
 *       events, but since this is not easily supported until R4 I am
 *       going to hold off until then.
 */

/* ARGSUSED */
static void Redisplay(w, event, region)
	Widget w;
XEvent *event;
Region region;
{
	(void) repaint_window ((BarGaugeWidget)w);
}

/* ARGSUSED */
static void 
draw_it(client_data, id)
	XtPointer client_data;
XtIntervalId *id;		/* unused */
{
	BarGaugeWidget w = (BarGaugeWidget)client_data;
	XgBarGaugeValues vals;
   
	if (w->bar_gauge.update > 0)
		w->bar_gauge.interval_id =
			XtAppAddTimeOut(XtWidgetToApplicationContext((Widget)w),
					w->bar_gauge.update*MS_PER_SEC,draw_it,
					client_data);

	if (w->bar_gauge.get_value == NULL)
		return;

	vals.num_values = w->bar_gauge.segments;
	vals.values     = w->bar_gauge.last_values;
	XtCallCallbacks( (Widget)w, XtNgetValue, (XtPointer)&vals );
   
	if (XtIsRealized((Widget)w))
		repaint_window(w);
} /* draw_it */

static void 
repaint_window(w)
	BarGaugeWidget w;
{
	double x_exact, error = 0.0;
	int x, lastx;
	int i;
	int n;
	Dimension valuewidth;
  
	if (XtIsRealized((Widget)w)) {

		XClearWindow( XtDisplay (w), XtWindow (w));

		if (w->bar_gauge.auto_adjust) {

			n = (w->bar_gauge.last_values[0] /
			     w->bar_gauge.max_value) + 1.0;
			if (n > XG_NUM_SEGMENTS) n = XG_NUM_SEGMENTS;
			valuewidth = (w->core.width - (n - 1) *
				      w->bar_gauge.segment_gap) / n;

			for ( lastx = i = 0; i < n - 1; i++ ) {
				XFillRectangle(XtDisplay(w), XtWindow(w),
					       w->bar_gauge.fgGC[i],
					       lastx, 0, valuewidth,
					       w->core.height);
				lastx += valuewidth + w->bar_gauge.segment_gap;
			}

			x = valuewidth * ( w->bar_gauge.last_values[0] -
					   (n - 1) * w->bar_gauge.max_value);
			XFillRectangle(XtDisplay(w), XtWindow(w),
				       w->bar_gauge.fgGC[i],
				       lastx, 0, x, w->core.height);
	  	     
		}
		else {
    	
			valuewidth = w->core.width -
				(w->bar_gauge.segments - 1) *
				w->bar_gauge.segment_gap;
	
			for ( lastx = i = 0; i < w->bar_gauge.segments; i++ ) {

				x_exact = valuewidth *
					(w->bar_gauge.last_values[i] /
					 w->bar_gauge.max_value);
				x = (int)(x_exact - error + 0.5);
#ifndef ALLOW_EMPTY_SEGMENTS
				/*  don't let segment disappear,
				 *  make it at least 1 pixel wide
				 */
				if (x <= 0) x = 1;
#endif
				error += x - x_exact;
       
				XFillRectangle(XtDisplay(w), XtWindow(w),
					       w->bar_gauge.fgGC[i],
					       lastx, 0, x, w->core.height);

				lastx += (x + w->bar_gauge.segment_gap);
			}
		}

		XFlush(XtDisplay(w));		    /* Flush output buffers */

	}
}

/* ARGSUSED */
static Boolean SetValues (current, request, new)
	Widget current, request, new;
{
	BarGaugeWidget old = (BarGaugeWidget)current;
	BarGaugeWidget w = (BarGaugeWidget)new;
	Boolean ret_val = FALSE;
	XGCValues gcval;
	int i;
	int checkgc;

#define has_changed(resource) \
	(old->bar_gauge.resource != w->bar_gauge.resource)
    
		if (has_changed(update)) {
			if (old->bar_gauge.update > 0)
				XtRemoveTimeOut (old->bar_gauge.interval_id);
			if (w->bar_gauge.update > 0)
				w->bar_gauge.interval_id = XtAppAddTimeOut
					(XtWidgetToApplicationContext(new),
					 w->bar_gauge.update * MS_PER_SEC,
					 draw_it, (XtPointer)w);
		}

	if (has_changed(auto_adjust) && w->bar_gauge.auto_adjust)
		w->bar_gauge.segments = XG_NUM_SEGMENTS;
    
	if (has_changed(segments)) {
		if (old->bar_gauge.segments > w->bar_gauge.segments) {
			/* free GC's, that we don't need any more */
			for ( i = w->bar_gauge.segments;
			      i < old->bar_gauge.segments; i++ )
				XtReleaseGC((Widget)old,old->bar_gauge.fgGC[i]);
		}
		else {
			/* allocate GC's, for the new segments */
			for ( i = old->bar_gauge.segments;
			      i < w->bar_gauge.segments; i++ ) {
				gcval.foreground = w->bar_gauge.fgpixel[i];
				w->bar_gauge.fgGC[i] = XtGetGC( (Widget) w,
								GCForeground,
								&gcval );
			}
		}

		ret_val = True;
	}

	checkgc = min( w->bar_gauge.segments, old->bar_gauge.segments );

	for ( i = 0; i < checkgc; i++ )
		if ( has_changed( fgpixel[i] ) ) {
			XtReleaseGC( (Widget) old, old->bar_gauge.fgGC[i] );
			gcval.foreground = w->bar_gauge.fgpixel[i];
			w->bar_gauge.fgGC[i] = XtGetGC( (Widget) w,
							GCForeground, &gcval );

			ret_val = True;
		}
    
	return( ret_val );
}