1 /* $Xorg: LED.c,v 1.3 2000/08/17 19:54:51 cpqbld Exp $ */
2 /************************************************************
3 Copyright (c) 1995 by Silicon Graphics Computer Systems, Inc.
5 Permission to use, copy, modify, and distribute this
6 software and its documentation for any purpose and without
7 fee is hereby granted, provided that the above copyright
8 notice appear in all copies and that both that copyright
9 notice and this permission notice appear in supporting
10 documentation, and that the name of Silicon Graphics not be
11 used in advertising or publicity pertaining to distribution
12 of the software without specific prior written permission.
13 Silicon Graphics makes no representation about the suitability
14 of this software for any purpose. It is provided "as is"
15 without any express or implied warranty.
17 SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
18 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
19 AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
20 GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
21 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
23 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
24 THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 ********************************************************/
27 /* $XFree86: xc/programs/xkbutils/LED.c,v 1.4 2001/01/17 23:46:13 dawes Exp $ */
29 #include <X11/IntrinsicP.h>
30 #include <X11/StringDefs.h>
32 #include <X11/Xaw/XawInit.h>
36 /* needed for abs() */
39 #define streq(a,b) (strcmp( (a), (b) ) == 0)
45 /****************************************************************
47 * Full class record constant
49 ****************************************************************/
53 #define offset(field) XtOffsetOf(LEDRec, field)
54 static XtResource resources[] = {
55 {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
56 offset(led.foreground), XtRString, XtDefaultForeground},
57 {XtNon, XtCOn, XtRBoolean, sizeof(Boolean),
58 offset(led.on), XtRImmediate, (XtPointer)False},
59 {XtNtopColor, XtCTopColor, XtRPixel, sizeof(Pixel),
60 offset(led.top_color), XtRString, "black"},
61 {XtNbottomColor, XtCBottomColor, XtRPixel, sizeof(Pixel),
62 offset(led.bottom_color), XtRString, "white"},
63 {XtNonColor, XtCOnColor, XtRPixel, sizeof(Pixel),
64 offset(led.on_color), XtRString, "green"},
65 {XtNoffColor, XtCOffColor, XtRPixel, sizeof(Pixel),
66 offset(led.off_color), XtRString, "#005000"},
67 {XtNbevel, XtCBevel, XtRDimension, sizeof(Dimension),
68 offset(led.bevel), XtRImmediate, (XtPointer)1},
69 {XtNledWidth, XtCLedWidth, XtRDimension, sizeof(Dimension),
70 offset(led.led_width), XtRImmediate, (XtPointer)6},
71 {XtNledHeight, XtCLedHeight, XtRDimension, sizeof(Dimension),
72 offset(led.led_height), XtRImmediate, (XtPointer)12}
76 static void ClassInitialize ( void );
77 static void Initialize ( Widget request, Widget new, ArgList args,
79 static void Realize ( Widget w, Mask * mask, XSetWindowAttributes * xswa );
80 static void Resize ( Widget w );
81 static Boolean SetValues ( Widget current, Widget request, Widget new,
82 ArgList args, Cardinal *num_args );
83 static void Destroy ( Widget w );
84 static XtGeometryResult QueryGeometry ( Widget w, XtWidgetGeometry *intended,
85 XtWidgetGeometry *preferred );
87 LEDClassRec ledClassRec = {
89 /* core_class fields */
90 /* superclass */ (WidgetClass) &simpleClassRec,
91 /* class_name */ "LED",
92 /* widget_size */ sizeof(LEDRec),
93 /* class_initialize */ ClassInitialize,
94 /* class_part_initialize */ NULL,
95 /* class_inited */ FALSE,
96 /* initialize */ Initialize,
97 /* initialize_hook */ NULL,
98 /* realize */ Realize,
101 /* resources */ resources,
102 /* num_resources */ XtNumber(resources),
103 /* xrm_class */ NULLQUARK,
104 /* compress_motion */ TRUE,
105 /* compress_exposure */ TRUE,
106 /* compress_enterleave */ TRUE,
107 /* visible_interest */ FALSE,
108 /* destroy */ Destroy,
110 /* expose */ XtInheritExpose,
111 /* set_values */ SetValues,
112 /* set_values_hook */ NULL,
113 /* set_values_almost */ XtInheritSetValuesAlmost,
114 /* get_values_hook */ NULL,
115 /* accept_focus */ NULL,
116 /* version */ XtVersion,
117 /* callback_private */ NULL,
119 /* query_geometry */ QueryGeometry,
120 /* display_accelerator */ XtInheritDisplayAccelerator,
123 /* Simple class fields initialization */
125 /* change_sensitive */ XtInheritChangeSensitive
127 /* LED class fields initialization */
132 WidgetClass ledWidgetClass = (WidgetClass)&ledClassRec;
133 /****************************************************************
137 ****************************************************************/
140 ClassInitialize(void)
142 XawInitializeWidgetSet();
146 GetPixmaps(LEDWidget lw)
152 Pixmap pix,on_pixmap,off_pixmap;
153 Dimension bevel,width,height;
155 dpy= XtDisplay((Widget)lw);
156 root= RootWindowOfScreen(XtScreen((Widget)lw));
157 if (lw->led.on_pixmap!=None) {
158 XFreePixmap(dpy,lw->led.on_pixmap);
159 lw->led.on_pixmap= None;
161 if (lw->led.off_pixmap!=None) {
162 XFreePixmap(dpy,lw->led.off_pixmap);
163 lw->led.off_pixmap= None;
165 lw->led.on_pixmap= on_pixmap= XCreatePixmap(dpy,root,
166 lw->core.width,lw->core.height,lw->core.depth);
167 lw->led.off_pixmap= off_pixmap= XCreatePixmap(dpy,root,
168 lw->core.width,lw->core.height,lw->core.depth);
170 values.foreground = lw->led.top_color;
171 gc= XCreateGC(dpy,lw->led.on_pixmap,(unsigned)GCForeground,&values);
172 bevel= lw->led.bevel;
173 width= lw->core.width;
174 height= lw->core.height;
175 XFillRectangle(dpy,on_pixmap,gc,0,0,width,height);
176 XFillRectangle(dpy,off_pixmap,gc,0,0,width,height);
177 XSetForeground(dpy,gc,lw->led.bottom_color);
178 XFillRectangle(dpy,on_pixmap,gc,bevel,bevel,width-bevel,height-bevel);
179 XFillRectangle(dpy,off_pixmap,gc,bevel,bevel,width-bevel,height-bevel);
180 XSetForeground(dpy,gc,lw->led.on_color);
181 XFillRectangle(dpy,on_pixmap,gc,bevel,bevel,width-2*bevel,height-2*bevel);
182 XSetForeground(dpy,gc,lw->led.off_color);
183 XFillRectangle(dpy,off_pixmap,gc,bevel,bevel,width-2*bevel,height-2*bevel);
185 if (lw->led.on) pix= on_pixmap;
186 else pix= off_pixmap;
187 if (XtWindow((Widget)lw)!=None)
188 XSetWindowBackgroundPixmap(dpy,XtWindow((Widget)lw),pix);
194 Initialize(Widget request, Widget new, ArgList args, Cardinal *num_args)
196 LEDWidget lw = (LEDWidget) new;
198 if (lw->core.height == 0)
199 lw->core.height = lw->led.led_height;
200 if (lw->core.width == 0)
201 lw->core.width = lw->led.led_width;
202 lw->core.border_width= 0;
203 if (lw->led.bevel==0)
205 lw->led.on_pixmap= lw->led.off_pixmap= None;
206 (*XtClass(new)->core_class.resize) ((Widget)lw);
211 Realize(Widget w, Mask *mask, XSetWindowAttributes *xswa)
213 LEDWidget lw = (LEDWidget)w;
214 WidgetClass super = simpleWidgetClass;
217 (*super->core_class.realize)(w,mask,xswa);
218 if (lw->led.on) pix= lw->led.on_pixmap;
219 else pix= lw->led.off_pixmap;
220 XSetWindowBackgroundPixmap(XtDisplay(w),XtWindow(w),pix);
227 GetPixmaps((LEDWidget)w);
232 * Set specified arguments into widget
236 SetValues(Widget current, Widget request, Widget new,
237 ArgList args, Cardinal *num_args)
239 LEDWidget curlw = (LEDWidget) current;
240 LEDWidget newlw = (LEDWidget) new;
244 if (curlw->led.foreground != newlw->led.foreground
245 || curlw->core.background_pixel != newlw->core.background_pixel
246 || curlw->led.on_color != newlw->led.on_color
247 || curlw->led.off_color != newlw->led.off_color
248 || curlw->core.width != curlw->core.width
249 || curlw->core.height != curlw->core.height) {
253 if (curlw->led.on!=newlw->led.on) {
256 if (newlw->led.on) pix= newlw->led.on_pixmap;
257 else pix= newlw->led.off_pixmap;
259 if (XtWindow(newlw)!=None)
260 XSetWindowBackgroundPixmap(XtDisplay(newlw),XtWindow(newlw),pix);
269 LEDWidget lw = (LEDWidget)w;
271 if (lw->led.on_pixmap!=None) {
272 XFreePixmap(XtDisplay(w),lw->led.on_pixmap);
273 lw->led.on_pixmap= None;
275 if (lw->led.off_pixmap!=None) {
276 XFreePixmap(XtDisplay(w),lw->led.off_pixmap);
277 lw->led.off_pixmap= None;
283 static XtGeometryResult
284 QueryGeometry(Widget w, XtWidgetGeometry *intended,
285 XtWidgetGeometry *preferred)
287 LEDWidget lw = (LEDWidget)w;
289 preferred->request_mode = CWWidth | CWHeight;
290 preferred->width = lw->led.led_height;
291 preferred->height = lw->led.led_width;
292 if ( ((intended->request_mode & (CWWidth | CWHeight))
293 == (CWWidth | CWHeight)) &&
294 intended->width == preferred->width &&
295 intended->height == preferred->height)
296 return XtGeometryYes;
297 else if (preferred->width == w->core.width &&
298 preferred->height == w->core.height)
301 return XtGeometryAlmost;