tachometer.c
来自「gpsd, a popular GPS daemon.」· C语言 代码 · 共 582 行 · 第 1/2 页
C
582 行
number_x = sin((step + 1.0)*D2R) * radius_x*0.65 + center_x; number_y = cos((step + 1.0)*D2R) * radius_y*0.65 + center_y; if ((int)((330.0 - step) / 30.0) == 1) jump = 3.0; DrawNumbers(w, (unsigned char) ((330.0 - step) / 30.0), number_x, number_y); } else { in_gauge_x = sin(step * D2R) * radius_x * 0.8 + center_x; in_gauge_y = cos(step * D2R) * radius_y * 0.8 + center_y; out_gauge_x = sin(step * D2R) * radius_x * 0.85 + center_x; out_gauge_y = cos(step * D2R) * radius_y * 0.85 + center_y; (void)XDrawLine(XtDisplay(w), XtWindow(w), gc, in_gauge_x, in_gauge_y, out_gauge_x, out_gauge_y); } } /*@ +type +unsignedcompare +compdef @*/ DrawLabelString(w);}static void DrawNeedle(TachometerWidget w, int load){ XPoint points[6]; double cur_theta1, cur_theta2, cur_theta3, cur_theta4, cur_theta5; Cardinal center_x, center_y, radius_x, radius_y; /*@ -type -unsignedcompare -compdef @*/ center_x = w->core.width / 2; center_y = w->core.height / 2; radius_x = center_x - w->tachometer.internal_border; radius_y = center_y - w->tachometer.internal_border; if ((center_x==0) || (center_y==0) || (radius_x<=0) || (radius_y<=0)) return; /* can't draw anything */ cur_theta1 = (double) (330 - (load * 3)) * D2R; cur_theta2 = (double) (330 - (load * 3) + 1) * D2R; cur_theta3 = (double) (330 - (load * 3) - 1) * D2R; cur_theta4 = (330.0 - ((double) load * 3.0) + 7.0) * D2R; cur_theta5 = (330.0 - ((double) load * 3.0) - 7.0) * D2R; points[0].x = (short)(sin(cur_theta1) * radius_x * 0.75 + center_x); points[0].y = (short)(cos(cur_theta1) * radius_y * 0.75 + center_y); points[1].x = (short)(sin(cur_theta2) * radius_x * 0.7 + center_x); points[1].y = (short)(cos(cur_theta2) * radius_y * 0.7 + center_y); points[2].x = (short)(sin(cur_theta4) * radius_x * 0.1 + center_x); points[2].y = (short)(cos(cur_theta4) * radius_y * 0.1 + center_y); points[3].x = (short)(sin(cur_theta5) * radius_x * 0.1 + center_x); points[3].y = (short)(cos(cur_theta5) * radius_y * 0.1 + center_y); points[4].x = (short)(sin(cur_theta3) * radius_x * 0.7 + center_x); points[4].y = (short)(cos(cur_theta3) * radius_y * 0.7 + center_y); /*@ -usedef @*/ points[5].x = points[0].x; points[5].y = points[0].y; /*@ +usedef @*/ (void)XDrawLines(XtDisplay(w), XtWindow(w), w->tachometer.needle_GC, points, 6, CoordModeOrigin); /*@ +type +unsignedcompare +compdef @*/}static void DrawTachometer(TachometerWidget w){ Cardinal center_x, center_y, radius_x, radius_y; /*@ -type -unsignedcompare -compdef @*/ center_x = w->core.width / 2; center_y = w->core.height / 2; radius_x = center_x - w->tachometer.internal_border; radius_y = center_y - w->tachometer.internal_border; if ((center_x==0) || (center_y==0) || (radius_x<=0) || (radius_y<=0)) return; /* Can't draw anything -- no room */ /* Big circle */ FastFillCircle(XtDisplay(w), XtWindow(w), w->tachometer.circle_GC, center_x, center_y, radius_x, radius_y); /* Inner circle same color as the background */ FastFillCircle(XtDisplay(w), XtWindow(w), w->tachometer.background_GC, center_x, center_y, (Cardinal) (radius_x * 0.95), (Cardinal) (radius_y * 0.95)); /* Small circle */ FastFillCircle(XtDisplay(w), XtWindow(w), w->tachometer.circle_GC, center_x, center_y, (Cardinal) (radius_x * 0.1), (Cardinal) (radius_y * 0.1)); /* Draw the details */ DrawGauge(w); DrawNeedle(w, w->tachometer.value); /*@ +type +unsignedcompare +compdef @*/}static void MoveNeedle(TachometerWidget w, int new){ int step, old, loop; old = w->tachometer.value; if (new > 100) new = 100; if (old == new) return; else if (old < new) step = (w->tachometer.speed ? w->tachometer.speed : new - old); else step = (w->tachometer.speed ? -w->tachometer.speed : new - old); /*@ -usedef @*/ if (old < new) { for (loop = old; loop < new; loop += step) DrawNeedle(w, loop); for (loop = old + step; loop <= new; loop += step) DrawNeedle(w, loop); } else { for (loop = old; loop > new; loop += step) DrawNeedle(w, loop); for (loop = old + step; loop >= new; loop += step) DrawNeedle(w, loop); } if (loop != new + step) /* The final needle wasn't printed */ DrawNeedle(w, new); /*@ +usedef @*/ w->tachometer.value = new;}static void GetneedleGC(TachometerWidget ta){ XGCValues values; values.background = ta->core.background_pixel; values.foreground = ta->tachometer.needle ^ ta->core.background_pixel; values.function = GXxor; /*@ -type -compdef -mustfreeonly @*/ ta->tachometer.needle_GC = XtGetGC( (Widget)ta, (unsigned) GCFunction | GCBackground | GCForeground, &values); /*@ +type +compdef +mustfreeonly @*/}static void GetscaleGC(TachometerWidget ta){ XGCValues values; values.foreground = ta->tachometer.scale; values.background = ta->core.background_pixel; /*@ -type -compdef -mustfreeonly @*/ ta->tachometer.scale_GC = XtGetGC( (Widget)ta, (unsigned) GCForeground | GCBackground, &values); /*@ +type +compdef +mustfreeonly @*/}static void GetcircleGC(TachometerWidget ta){ XGCValues values; values.foreground = ta->tachometer.circle; values.background = ta->core.background_pixel; /*@ -type -compdef -mustfreeonly @*/ ta->tachometer.circle_GC = XtGetGC( (Widget)ta, (unsigned) GCForeground | GCBackground, &values); /*@ +type +compdef +mustfreeonly @*/}static void GetbackgroundGC(TachometerWidget ta){ XGCValues values; values.foreground = ta->core.background_pixel; values.background = ta->core.background_pixel; /*@ -type -compdef -mustfreeonly @*/ ta->tachometer.background_GC = XtGetGC( (Widget)ta, (unsigned) GCForeground | GCBackground, &values); /*@ +type +compdef +mustfreeonly @*/}static void Initialize(Widget request UNUSED, Widget new){ TachometerWidget ta = (TachometerWidget) new; GetneedleGC(ta); GetcircleGC(ta); GetscaleGC(ta); GetbackgroundGC(ta); ta->tachometer.width = ta->tachometer.height = 0;} /* Initialize */static void Realize(Widget w, Mask *valueMask, XSetWindowAttributes *attributes){ *valueMask |= CWBitGravity; attributes->bit_gravity = NorthWestGravity; (*superclass->core_class.realize) (w, valueMask, attributes);} /* Realize */static void Redisplay(Widget w, XEvent *event, Region region UNUSED){ if (event->xexpose.count == 0) DrawTachometer((TachometerWidget) w);} /* Redisplay */static void Resize(Widget w){ TachometerWidget ta = (TachometerWidget) w; if ((ta->core.width == ta->tachometer.width) && (ta->core.height == ta->tachometer.height)) /* What resize? We don't see a resize! */ return; (void)XClearWindow(XtDisplay(w), XtWindow(w)); if ((ta->core.width <= ta->tachometer.width) && (ta->core.height <= ta->tachometer.height)) /* Only redraw here if no expose events are going to be */ /* generated, i.e. if the window has not grown horizontally */ /* or vertically. */ DrawTachometer(ta); ta->tachometer.width = ta->core.width; ta->tachometer.height = ta->core.height;} /* Resize */static Boolean SetValues(Widget current, Widget request UNUSED, Widget new)/* Set specified arguments into widget */{ /*@ -type -boolops -predboolothers @*/ Boolean back, changed = (Boolean)False; TachometerWidget curta = (TachometerWidget) current; TachometerWidget newta = (TachometerWidget) new; back = (curta->core.background_pixel != newta->core.background_pixel); if (back || (curta->tachometer.needle != newta->tachometer.needle)) { (void)XtReleaseGC(new, newta->tachometer.needle_GC); GetneedleGC(newta); changed = True; } if (back || (curta->tachometer.scale != newta->tachometer.scale)) { (void)XtReleaseGC(new, newta->tachometer.scale_GC); GetscaleGC(newta); changed = True; } if (back || (curta->tachometer.circle != newta->tachometer.circle)) { (void)XtReleaseGC(new, newta->tachometer.circle_GC); GetcircleGC(newta); changed = True; } if (back) { (void)XtReleaseGC(new, newta->tachometer.background_GC); GetbackgroundGC(newta); changed = True; } if (curta->tachometer.value != newta->tachometer.value) { MoveNeedle(newta, newta->tachometer.value); changed = True; } /*@ +type +boolops +predboolothers @*/ return(changed);}static void Destroy(Widget w){ TachometerWidget ta = (TachometerWidget) w; (void)XtReleaseGC( w, ta->tachometer.needle_GC ); (void)XtReleaseGC( w, ta->tachometer.circle_GC ); (void)XtReleaseGC( w, ta->tachometer.scale_GC ); (void)XtReleaseGC( w, ta->tachometer.background_GC );}/* Exported Procedures */int TachometerGetValue(Widget w){ return(((TachometerWidget) w)->tachometer.value);}int TachometerSetValue(Widget w, int i){ int old; TachometerWidget ta = (TachometerWidget) w; old = ta->tachometer.value; MoveNeedle(ta, i); return(old);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?