⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 batterygauge.c

📁 电源管理程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*  Copyright (C) 1999 Nathan Sidwell <nathan@acm.org> *  $Id: BatteryGauge.c,v 1.1 1999/08/02 20:08:10 apenwarr Exp $ *  This file is available under the GNU General Public License. *//*{{{  includes*/#include <X11/X.h>#include <X11/IntrinsicP.h>#include <X11/StringDefs.h>#include <X11/Xmu/Drawing.h>#include <X11/Shell.h>#include <X11/Xaw/Label.h>#include "BatteryGaugeP.h"#include <apm.h>/*}}}*//*{{{  resources*/static XtResource resources[] ={  {XtNorientation, XtCOrientation, XtROrientation, sizeof(XtOrientation),      XtOffsetOf(BatteryGaugeRec, batteryGauge.orientation),      XtRImmediate, (XtPointer)XtorientVertical},  {XtNlength, XtCLength, XtRDimension, sizeof(Dimension),       XtOffsetOf(BatteryGaugeRec, batteryGauge.length),       XtRImmediate, (XtPointer)0},  {XtNthickness, XtCThickness, XtRDimension, sizeof(Dimension),       XtOffsetOf(BatteryGaugeRec, batteryGauge.thickness),       XtRImmediate, (XtPointer)0},  {XtNflash, XtCDelay, XtRInt, sizeof(int),      XtOffsetOf(BatteryGaugeRec, batteryGauge.flashDelay),      XtRImmediate, (XtPointer)250},  {XtNupdate, XtCDelay, XtRInt, sizeof(int),      XtOffsetOf(BatteryGaugeRec, batteryGauge.updateDelay),      XtRImmediate, (XtPointer)15},  {XtNlowColor, XtCForeground, XtRPixel, sizeof(Pixel),      XtOffsetOf(BatteryGaugeRec, batteryGauge.lowColor),      XtRString, (XtPointer)"orange"},  {XtNhighColor, XtCForeground, XtRPixel, sizeof(Pixel),      XtOffsetOf(BatteryGaugeRec, batteryGauge.highColor),      XtRString, (XtPointer)"green"},  {XtNcriticalColor, XtCForeground, XtRPixel, sizeof(Pixel),      XtOffsetOf(BatteryGaugeRec, batteryGauge.criticalColor),      XtRString, (XtPointer)"red"},  {XtNchargingColor, XtCBackground, XtRPixel, sizeof(Pixel),      XtOffsetOf(BatteryGaugeRec, batteryGauge.chargingColor),      XtRString, (XtPointer)"blue"},  {XtNcriticalLevel, XtCValue, XtRInt, sizeof(int),      XtOffsetOf(BatteryGaugeRec, batteryGauge.criticalLevel),      XtRImmediate, (XtPointer)0},  {XtNlevel, XtCValue, XtRInt, sizeof(int),      XtOffsetOf(BatteryGaugeRec, batteryGauge.level),      XtRImmediate, (XtPointer)0},  {XtNstatus, XtCValue, XtRInt, sizeof(int),      XtOffsetOf(BatteryGaugeRec, batteryGauge.status),      XtRImmediate, (XtPointer)-1},  {XtNlifetime, XtCValue, XtRInt, sizeof(int),      XtOffsetOf(BatteryGaugeRec, batteryGauge.lifetime),      XtRImmediate, (XtPointer)0},};/*}}}*//*{{{  translations*/static char translations[] ="\<BtnDown>: Notify()\n\<BtnUp>: Release()\n\";/*}}}*//*{{{  prototypes*//*{{{  methods*/static void ClassInitialize(void);static void Destroy(Widget);static void Initialize(Widget, Widget, ArgList, Cardinal *);static void Realize(Widget, XtValueMask *, XSetWindowAttributes *);static void Redisplay(Widget, XEvent *, Region);static void Resize(Widget);static Boolean SetValues(Widget, Widget, Widget, ArgList, Cardinal *);/*}}}*//*{{{  actions*/static void Notify(Widget, XEvent *, String *, Cardinal *);static void Release(Widget, XEvent *, String *, Cardinal *);/*}}}*//*{{{  helpers*/static void redraw(BatteryGaugeWidget me);static int setPosition(BatteryGaugeWidget me, int);static void setFlashTimeout(BatteryGaugeWidget me);static void flashTimeout(XtPointer, XtIntervalId *);static void updateLabel(BatteryGaugeWidget);static void updateTimeout(XtPointer, XtIntervalId *);/*}}}*//*}}}*//*{{{  actions*/static XtActionsRec actions[] ={  {"Notify", Notify},  {"Release", Release},};/*}}}*/#define SuperClass (&simpleClassRec)/*{{{  BatteryGaugeClassRec batteryGaugeClassRec =*/BatteryGaugeClassRec batteryGaugeClassRec ={  /*{{{  core class part*/  {    (WidgetClass)SuperClass,          /* superclass */    "BatteryGauge",                   /* class_name */    sizeof(BatteryGaugeRec),          /* size */    ClassInitialize,                  /* class_initialize */    NULL,                             /* class_part_initialize */    False,                            /* class_inited */    Initialize,                       /* initialize */    NULL,                             /* initialize_hook */    Realize,                          /* realize */    actions,                          /* actions */    XtNumber(actions),                /* num_actions */    resources,                        /* resources */    XtNumber(resources),              /* num_resources */    NULLQUARK,                        /* xrm_class */    True,                             /* compress_motion */    XtExposeCompressMultiple,         /* compress_exposure */    True,                             /* compress_enterleave */    False,                            /* visible_interest */    Destroy,                          /* destroy */    Resize,                           /* resize */    Redisplay,                        /* expose */    SetValues,                        /* set_values */    NULL,                             /* set_values_hook */    XtInheritSetValuesAlmost,         /* set_values_almost */    NULL,                             /* get_values_hook */    XtInheritAcceptFocus,             /* accept_focus */    XtVersion,                        /* version */    NULL,                             /* callback_private */    translations,                     /* default_translations */    XtInheritQueryGeometry,           /* query_geometry */    XtInheritDisplayAccelerator,      /* display_accelerator */    NULL,                             /* extension */  },  /*}}}*/  /*{{{  simple class part*/  {    XtInheritChangeSensitive      /* change_sensitive */  },  /*}}}*/  /*{{{  batteryGauge class part*/  {  },  /*}}}*/};/*}}}*/WidgetClass batteryGaugeWidgetClass = (WidgetClass)&batteryGaugeClassRec;/*{{{  methods*//*{{{  static void ClassInitialize()*/static void ClassInitialize()/* register the orientation resource converter */{  XtAddConverter(XtRString, XtROrientation, XmuCvtStringToOrientation,      (XtConvertArgList)NULL, (Cardinal)0);  return;}/*}}}*//*{{{  static void Destroy(Widget w)*/static void Destroy(Widget w)/* removes pending timeouts and workprocs */{  BatteryGaugeWidget me;    me = (BatteryGaugeWidget)w;  if(me->batteryGauge.updateTimeout != (XtIntervalId)0)    XtRemoveTimeOut(me->batteryGauge.updateTimeout);  if(me->batteryGauge.flashTimeout != (XtIntervalId)0)    XtRemoveTimeOut(me->batteryGauge.flashTimeout);  if(XtIsRealized((Widget)me))    XFreeGC(XtDisplay((Widget)me), me->batteryGauge.gc);  if(me->batteryGauge.popup)    XtDestroyWidget(me->batteryGauge.popup);  return;}/*}}}*//*{{{  static void Initialize(Widget request_w, Widget new_w, ArgList args, Cardinal *num_args)*/static void Initialize(Widget request_w, Widget new_w, ArgList args, Cardinal *num_args)/* initializes an instance of the batteryGauge widget * set the width and height from the thickness and length */{  BatteryGaugeWidget request_me;  BatteryGaugeWidget new_me;    new_me = (BatteryGaugeWidget)new_w;  request_me = (BatteryGaugeWidget)request_w;  if(!new_me->batteryGauge.thickness)    new_me->batteryGauge.thickness = 8;  if(!new_me->batteryGauge.length)    new_me->batteryGauge.length = new_me->batteryGauge.thickness;  if(new_me->batteryGauge.orientation != XtorientHorizontal)    {      if(!new_me->core.width)	new_me->core.width = new_me->batteryGauge.thickness;      if(!new_me->core.height)	new_me->core.height = new_me->batteryGauge.length;      new_me->batteryGauge.length = new_me->core.height;      new_me->batteryGauge.thickness = new_me->core.width;    }  else    {      if(!new_me->core.width)	new_me->core.width = new_me->batteryGauge.length;      if(!new_me->core.height)	new_me->core.height = new_me->batteryGauge.thickness;      new_me->batteryGauge.length = new_me->core.width;      new_me->batteryGauge.thickness = new_me->core.height;    }  new_me->batteryGauge.inverted = new_me->batteryGauge.flash =      new_me->batteryGauge.notified = 0;  new_me->batteryGauge.popup = new_me->batteryGauge.label = NULL;  new_me->batteryGauge.updateTimeout =      new_me->batteryGauge.flashTimeout = (XtIntervalId)0;  new_me->batteryGauge.popped = 0;  return;}/*}}}*//*{{{  static void Realize(Widget w, XtValueMask *valueMask, XSetWindowAttributes *attributes)*/static void Realize(Widget w, XtValueMask *valueMask, XSetWindowAttributes *attributes)/* realizes the batteryGauge widget * it is at this point that the  GC is allocated * set the idle cursor * clip the limits and value * set the batteryGauge position, but don't paint it */{  BatteryGaugeWidget me;    me = (BatteryGaugeWidget)w;  SuperClass->core_class.realize((Widget)me, valueMask, attributes);  /*{{{  create the gc*/  {    XGCValues gcValues;    XtGCMask  mask;    unsigned  depth;        depth = 1;    gcValues.function = GXcopy;    gcValues.foreground = 0; /* filled prior to drawing */    gcValues.background = 0;    gcValues.fill_style = FillSolid;    mask = GCForeground | GCFunction | GCFillStyle;    me->batteryGauge.gc = XCreateGC(XtDisplay((Widget)me),        XtWindow((Widget)me), mask, &gcValues);  }  /*}}}*/  updateTimeout((XtPointer)me, NULL);  return;}/*}}}*//*{{{  static void Redisplay(Widget  w, XEvent  *event, Region  region)*/static void Redisplay(Widget  w, XEvent  *event, Region  region){  BatteryGaugeWidget  me;    me = (BatteryGaugeWidget)w;  redraw(me);  return;}/*}}}*//*{{{  static void Resize(Widget  w)*/static void Resize(Widget  w)/*  * get the length and thickness from width and height */{  BatteryGaugeWidget me;    me = (BatteryGaugeWidget)w;  if(me->batteryGauge.orientation != XtorientHorizontal)    {      me->batteryGauge.length = me->core.height;      me->batteryGauge.thickness = me->core.width;    }  else    {      me->batteryGauge.length = me->core.width;      me->batteryGauge.thickness = me->core.height;    }  setPosition(me, -1);  return;}/*}}}*//*{{{  static Boolean SetValues(Widget current_w, Widget request_w, Widget new_w, ArgList args, Cardinal *num_args)*/static Boolean SetValues(Widget current_w, Widget request_w, Widget new_w, ArgList args, Cardinal *num_args)/* if thickness, length or orientation changed, then request * new geometry */{  BatteryGaugeWidget current_me;  /* this is how we look now */  BatteryGaugeWidget request_me;  /* this is what we were asked for */  BatteryGaugeWidget new_me;      /* this is what we'll end up with */  Boolean repaint;    current_me = (BatteryGaugeWidget)current_w;  request_me = (BatteryGaugeWidget)request_w;  new_me = (BatteryGaugeWidget)new_w;  repaint = False;  /*{{{  orientation changed?*/  if(new_me->batteryGauge.orientation != current_me->batteryGauge.orientation)    {      repaint = True;      new_me->core.width = new_me->batteryGauge.orientation != XtorientHorizontal ?  	  new_me->batteryGauge.thickness : new_me->batteryGauge.length;      new_me->core.height = new_me->batteryGauge.orientation != XtorientHorizontal ?  	  new_me->batteryGauge.length : new_me->batteryGauge.thickness;    }  /*}}}*/  /*{{{  length changed?*/  if(new_me->batteryGauge.length != current_me->batteryGauge.length)    {      if(new_me->batteryGauge.orientation != XtorientHorizontal)  	new_me->core.height = new_me->batteryGauge.length;      else  	new_me->core.width = new_me->batteryGauge.length;      repaint = True;    }  /*}}}*/  /*{{{  thickness changed?*/  if(new_me->batteryGauge.thickness != current_me->batteryGauge.thickness)    {      if(new_me->batteryGauge.orientation != XtorientHorizontal)  	new_me->core.width = new_me->batteryGauge.thickness;      else  	new_me->core.height = new_me->batteryGauge.thickness;      repaint = True;    }  /*}}}*/  /* we have to reset length and thickness to current dimensions,    * in case we never get resized */  if(new_me->batteryGauge.orientation != XtorientHorizontal)    {      new_me->batteryGauge.length = current_me->core.height;      new_me->batteryGauge.thickness = current_me->core.width;    }  else    {      new_me->batteryGauge.length = current_me->core.width;      new_me->batteryGauge.thickness = current_me->core.height;    }  if(XtIsRealized((Widget)new_me))    {      new_me->batteryGauge.level = current_me->batteryGauge.level;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -