"Fossies" - the Fresh Open Source Software Archive

Member "procmeter3-3.6+svn387/xaw/widgets/PMBar.c" (24 Dec 2010, 13546 Bytes) of package /linux/misc/procmeter3-3.6+svn387.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "PMBar.c" see the Fossies "Dox" file reference documentation.

    1 /***************************************
    2   $Header: /home/amb/CVS/procmeter3/xaw/widgets/PMBar.c,v 1.5 2008-04-27 15:21:30 amb Exp $
    3 
    4   ProcMeter Bar Widget Source file (for ProcMeter3 3.5b).
    5   ******************/ /******************
    6   Written by Andrew M. Bishop
    7 
    8   This file Copyright 1996-2008 Andrew M. Bishop
    9   It may be distributed under the GNU Public License, version 2, or
   10   any higher version.  See section COPYING of the GNU Public license
   11   for conditions under which this file may be redistributed.
   12   ***************************************/
   13 
   14 
   15 #include <X11/X.h>
   16 #include <X11/Xlib.h>
   17 #include <X11/Xutil.h>
   18 #include <X11/IntrinsicP.h>
   19 #include <X11/StringDefs.h>
   20 #include <X11/CoreP.h>
   21 
   22 #include "PMGenericP.h"
   23 #include "PMBarP.h"
   24 
   25 #include "procmeter.h"
   26 
   27 static void Initialize(ProcMeterBarWidget request,ProcMeterBarWidget new);
   28 static void Destroy(ProcMeterBarWidget pmw);
   29 static Boolean SetValues(ProcMeterBarWidget current,ProcMeterBarWidget request,ProcMeterBarWidget new);
   30 static void Resize(ProcMeterBarWidget pmw);
   31 static void Redisplay(ProcMeterBarWidget pmw,XEvent *event,Region region);
   32 static void BarResize(ProcMeterBarWidget pmw);
   33 static void BarUpdate(ProcMeterBarWidget pmw,Boolean all);
   34 
   35 static XtResource resources[]=
   36 {
   37  /* The grid parts. */
   38 
   39  {XtNgridUnits, XtCLabel, XtRString, sizeof(XtPointer),
   40   XtOffset(ProcMeterBarWidget,procmeter_bar.grid_units), XtRString, "" },
   41  {XtNgridForeground, XtCForeground, XtRPixel, sizeof(Pixel),
   42   XtOffset(ProcMeterBarWidget,procmeter_bar.grid_pixel),XtRString,XtDefaultBackground},
   43  {XtNgridMin, XtCGridMin, XtRInt, sizeof(int),
   44   XtOffset(ProcMeterBarWidget,procmeter_bar.grid_min), XtRString, "1" },
   45  {XtNgridMax, XtCGridMax, XtRInt, sizeof(int),
   46   XtOffset(ProcMeterBarWidget,procmeter_bar.grid_max), XtRString, "0" }
   47 };
   48 
   49 /*+ The actual ProcMeter Bar Widget Class Record. +*/
   50 ProcMeterBarClassRec procMeterBarClassRec=
   51 {
   52  {
   53   (WidgetClass) &procMeterGenericClassRec,
   54   "ProcMeterBar",
   55   sizeof(ProcMeterBarRec),
   56   NULL,
   57   NULL,
   58   FALSE,
   59   (XtInitProc)Initialize,
   60   NULL,
   61   XtInheritRealize,
   62   NULL,
   63   0,
   64   resources,
   65   XtNumber(resources),
   66   NULLQUARK,
   67   TRUE,
   68   XtExposeCompressMaximal|XtExposeGraphicsExpose,
   69   TRUE,
   70   TRUE,
   71   (XtWidgetProc)Destroy,
   72   (XtWidgetProc)Resize,
   73   (XtExposeProc)Redisplay,
   74   (XtSetValuesFunc)SetValues,
   75   NULL,
   76   XtInheritSetValuesAlmost,
   77   NULL,
   78   NULL,
   79   XtVersion,
   80   NULL,
   81   XtInheritTranslations,
   82   NULL,
   83   NULL,
   84   NULL,
   85  },
   86  {
   87   NULL,
   88   NULL
   89  },
   90  {
   91   0
   92  }
   93 };
   94 
   95 /*+ The actual ProcMeter Bar Widget Class Record masquerading as a WidgetClass type. +*/
   96 WidgetClass procMeterBarWidgetClass=(WidgetClass)&procMeterBarClassRec;
   97 
   98 
   99 /*++++++++++++++++++++++++++++++++++++++
  100   Initialise a new ProcMeter Bar Widget.
  101 
  102   ProcMeterBarWidget request The requested parameters.
  103 
  104   ProcMeterBarWidget new The new parameters that are to be filled in.
  105   ++++++++++++++++++++++++++++++++++++++*/
  106 
  107 static void Initialize(ProcMeterBarWidget request,ProcMeterBarWidget new)
  108 {
  109  int i;
  110  XGCValues values;
  111 
  112  /* The grid parts. */
  113 
  114  new->procmeter_bar.grid_units=XtNewString(request->procmeter_bar.grid_units);
  115 
  116  values.foreground=new->procmeter_bar.grid_pixel;
  117  values.background=new->core.background_pixel;
  118  new->procmeter_bar.grid_gc=XtGetGC((Widget)new,GCForeground|GCBackground,&values);
  119 
  120  if(request->procmeter_bar.grid_min<0)
  121    {
  122     new->procmeter_bar.grid_min=-request->procmeter_bar.grid_min;
  123     new->procmeter_bar.grid_drawn=0;
  124    }
  125  else if(request->procmeter_bar.grid_min>0)
  126    {
  127     new->procmeter_bar.grid_min=request->procmeter_bar.grid_min;
  128     new->procmeter_bar.grid_drawn=1;
  129    }
  130  else /* if(request->procmeter_bar.grid_min==0) */
  131    {
  132     new->procmeter_bar.grid_min=1;
  133     new->procmeter_bar.grid_drawn=1;
  134    }
  135 
  136  if(request->procmeter_bar.grid_max<0)
  137     new->procmeter_bar.grid_max=0;
  138  else
  139     new->procmeter_bar.grid_max=request->procmeter_bar.grid_max;
  140 
  141  if(new->procmeter_bar.grid_max && new->procmeter_bar.grid_max<new->procmeter_bar.grid_min)
  142     new->procmeter_bar.grid_max=new->procmeter_bar.grid_min;
  143 
  144  new->procmeter_bar.grid_num=new->procmeter_bar.grid_min;
  145 
  146  /* The data parts. */
  147 
  148  for(i=0;i<sizeof(new->procmeter_bar.data)/sizeof(new->procmeter_bar.data[0]);i++)
  149     new->procmeter_bar.data[i]=0;
  150 
  151  new->procmeter_bar.data_index=0;
  152 
  153  new->procmeter_bar.data_sum=0;
  154 
  155  /* The rest of the sizing. */
  156 
  157  BarResize(new);
  158 }
  159 
  160 
  161 /*++++++++++++++++++++++++++++++++++++++
  162   Destroy a ProcMeter Bar Widget.
  163 
  164   ProcMeterBarWidget pmw The Widget to destroy.
  165   ++++++++++++++++++++++++++++++++++++++*/
  166 
  167 static void Destroy(ProcMeterBarWidget pmw)
  168 {
  169  XtReleaseGC((Widget)pmw,pmw->procmeter_bar.grid_gc);
  170  XtFree((XtPointer)pmw->procmeter_bar.grid_units);
  171 }
  172 
  173 
  174 /*++++++++++++++++++++++++++++++++++++++
  175   The setvalues procedure that is used to set the values internal to the Widget.
  176 
  177   Boolean SetValues Returns True if the Widget is to be redrawn.
  178 
  179   ProcMeterBarWidget current The current Widget values.
  180 
  181   ProcMeterBarWidget request The requested Widget values.
  182 
  183   ProcMeterBarWidget new The new Widget values to be set up.
  184   ++++++++++++++++++++++++++++++++++++++*/
  185 
  186 static Boolean SetValues(ProcMeterBarWidget current,ProcMeterBarWidget request,ProcMeterBarWidget new)
  187 {
  188  Boolean redraw=False;
  189 
  190  /* The grid parts. */
  191 
  192  if(request->procmeter_bar.grid_units!=current->procmeter_bar.grid_units)
  193    {
  194     XtFree((XtPointer)new->procmeter_bar.grid_units);
  195     new->procmeter_bar.grid_units=XtNewString(request->procmeter_bar.grid_units);
  196 
  197     redraw=True;
  198    }
  199 
  200  if(request->procmeter_bar.grid_pixel!=current->procmeter_bar.grid_pixel)
  201    {
  202     XGCValues xgcv;
  203 
  204     XGetGCValues(XtDisplay(new),new->procmeter_bar.grid_gc,GCForeground|GCBackground,&xgcv);
  205     XtReleaseGC((Widget)new,new->procmeter_bar.grid_gc);
  206     xgcv.foreground=request->procmeter_bar.grid_pixel;
  207     xgcv.background=request->core.background_pixel;
  208     new->procmeter_bar.grid_gc=XtGetGC((Widget)new,GCForeground|GCBackground,&xgcv);
  209 
  210     redraw=True;
  211    }
  212 
  213  if(request->procmeter_bar.grid_min!=current->procmeter_bar.grid_min)
  214    {
  215     if(request->procmeter_bar.grid_min<0)
  216       {
  217        new->procmeter_bar.grid_min=-request->procmeter_bar.grid_min;
  218        new->procmeter_bar.grid_drawn=0;
  219       }
  220     else if(request->procmeter_bar.grid_min>0)
  221       {
  222        new->procmeter_bar.grid_min=request->procmeter_bar.grid_min;
  223        new->procmeter_bar.grid_drawn=1;
  224       }
  225     else /* if(request->procmeter_bar.grid_min==0) */
  226       {
  227        new->procmeter_bar.grid_min=1;
  228        new->procmeter_bar.grid_drawn=1;
  229       }
  230 
  231     if(request->procmeter_bar.grid_min>request->procmeter_bar.grid_max && request->procmeter_bar.grid_max)
  232        new->procmeter_bar.grid_min=request->procmeter_bar.grid_max;
  233 
  234     if(new->procmeter_bar.grid_min>=new->procmeter_bar.grid_num)
  235        new->procmeter_bar.grid_num=new->procmeter_bar.grid_min;
  236 
  237     redraw=True;
  238    }
  239 
  240  if(request->procmeter_bar.grid_max!=current->procmeter_bar.grid_max)
  241    {
  242     if(request->procmeter_bar.grid_max<0)
  243        new->procmeter_bar.grid_max=0;
  244     else
  245        new->procmeter_bar.grid_max=request->procmeter_bar.grid_max;
  246 
  247     if(request->procmeter_bar.grid_max && request->procmeter_bar.grid_max<new->procmeter_bar.grid_min)
  248        new->procmeter_bar.grid_max=new->procmeter_bar.grid_min;
  249 
  250     redraw=True;
  251    }
  252 
  253  if(redraw)
  254     BarResize(new);
  255 
  256  return(redraw);
  257 }
  258 
  259 
  260 /*++++++++++++++++++++++++++++++++++++++
  261   Resize the ProcMeter Bar Widget.
  262 
  263   ProcMeterBarWidget pmw The Widget that is resized.
  264   ++++++++++++++++++++++++++++++++++++++*/
  265 
  266 static void Resize(ProcMeterBarWidget pmw)
  267 {
  268  BarResize(pmw);
  269 }
  270 
  271 
  272 /*++++++++++++++++++++++++++++++++++++++
  273   Redisplay the ProcMeter Widget.
  274 
  275   ProcMeterBarWidget pmw The Widget to redisplay.
  276 
  277   XEvent *event The event that caused the redisplay.
  278 
  279   Region region The region that was exposed.
  280   ++++++++++++++++++++++++++++++++++++++*/
  281 
  282 static void Redisplay(ProcMeterBarWidget pmw,XEvent *event,Region region)
  283 {
  284  if(pmw->core.visible)
  285     BarUpdate(pmw,True);
  286 }
  287 
  288 
  289 /*++++++++++++++++++++++++++++++++++++++
  290   Perform all of the sizing on the Widget when it is created/resized.
  291 
  292   ProcMeterBarWidget pmw The Widget to resize.
  293   ++++++++++++++++++++++++++++++++++++++*/
  294 
  295 static void BarResize(ProcMeterBarWidget pmw)
  296 {
  297  (*procMeterGenericClassRec.procmeter_generic_class.resize)((ProcMeterGenericWidget)pmw);
  298 
  299  pmw->procmeter_generic.label_x=2;
  300 
  301  /* The grid parts. */
  302 
  303  pmw->procmeter_bar.grid_units_x=pmw->core.width-XTextWidth(pmw->procmeter_generic.label_font,pmw->procmeter_bar.grid_units,(int)strlen(pmw->procmeter_bar.grid_units));
  304 
  305  pmw->procmeter_bar.grid_maxvis=pmw->core.width/3;
  306 
  307  if(pmw->procmeter_generic.label_pos==ProcMeterLabelTop)
  308     pmw->procmeter_generic.body_start=pmw->procmeter_generic.label_height;
  309  else
  310     pmw->procmeter_generic.body_start=0;
  311 
  312  if(pmw->procmeter_bar.grid_num>pmw->procmeter_bar.grid_maxvis && pmw->procmeter_bar.grid_drawn)
  313     pmw->procmeter_bar.grid_drawn=-1;
  314  if(pmw->procmeter_bar.grid_num<=pmw->procmeter_bar.grid_maxvis && pmw->procmeter_bar.grid_drawn)
  315     pmw->procmeter_bar.grid_drawn=1;
  316 }
  317 
  318 
  319 /*++++++++++++++++++++++++++++++++++++++
  320   Update the display.
  321 
  322   ProcMeterBarWidget pmw The Widget to update.
  323 
  324   Boolean all Indicates if it all is to be updated including the generic parts.
  325   ++++++++++++++++++++++++++++++++++++++*/
  326 
  327 static void BarUpdate(ProcMeterBarWidget pmw,Boolean all)
  328 {
  329  if(pmw->core.visible)
  330    {
  331     int i;
  332     int scale=PROCMETER_GRAPH_SCALE*pmw->procmeter_bar.grid_num;
  333     Position pos;
  334     Position top_average_bottom,bottom_average_top,average_size;
  335 
  336     if(all)
  337       {
  338        (*procMeterGenericClassRec.procmeter_generic_class.update)((ProcMeterGenericWidget)pmw);
  339 
  340        if(pmw->procmeter_generic.label_pos!=ProcMeterLabelNone)
  341           XDrawString(XtDisplay(pmw),XtWindow(pmw),pmw->procmeter_generic.label_gc,
  342                       pmw->procmeter_bar.grid_units_x,pmw->procmeter_generic.label_y,
  343                       pmw->procmeter_bar.grid_units,(int)strlen(pmw->procmeter_bar.grid_units));
  344 
  345       }
  346     else
  347        XClearArea(XtDisplay(pmw),XtWindow(pmw),
  348                   0              ,pmw->procmeter_generic.body_start,
  349                   pmw->core.width,pmw->procmeter_generic.body_height,False);
  350 
  351     pos=pmw->procmeter_bar.data_sum*pmw->core.width/(scale*2);
  352 
  353     top_average_bottom=pmw->procmeter_generic.body_start+2*(pmw->procmeter_generic.body_height>>3);
  354     bottom_average_top=pmw->procmeter_generic.body_start+pmw->procmeter_generic.body_height-2*(pmw->procmeter_generic.body_height>>3);
  355     average_size=pmw->procmeter_generic.body_height>>3;
  356 
  357     XFillRectangle(XtDisplay(pmw),XtWindow(pmw),pmw->procmeter_generic.body_gc,
  358                    pos-average_size,top_average_bottom-average_size,
  359                    average_size    ,average_size);
  360 
  361     XFillRectangle(XtDisplay(pmw),XtWindow(pmw),pmw->procmeter_generic.body_gc,
  362                    pos-average_size,bottom_average_top,
  363                    average_size    ,average_size);
  364 
  365     pos=pmw->procmeter_bar.data[pmw->procmeter_bar.data_index]*pmw->core.width/scale;
  366 
  367     XFillRectangle(XtDisplay(pmw),XtWindow(pmw),pmw->procmeter_generic.body_gc,
  368                    0  ,top_average_bottom+1,
  369                    pos,bottom_average_top-top_average_bottom-2);
  370 
  371     if(pmw->procmeter_bar.grid_drawn==1)
  372        for(i=1;i<pmw->procmeter_bar.grid_num;i++)
  373          {
  374           pos=i*pmw->core.width/pmw->procmeter_bar.grid_num;
  375           XDrawLine(XtDisplay(pmw),XtWindow(pmw),pmw->procmeter_bar.grid_gc,
  376                     pos,pmw->procmeter_generic.body_start,
  377                     pos,pmw->procmeter_generic.body_height+pmw->procmeter_generic.body_start);
  378          }
  379     else
  380        if(pmw->procmeter_bar.grid_drawn==-1)
  381          {
  382           pos=pmw->procmeter_bar.grid_maxvis*pmw->core.width/pmw->procmeter_bar.grid_num;
  383           XDrawLine(XtDisplay(pmw),XtWindow(pmw),pmw->procmeter_bar.grid_gc,
  384                     pos,pmw->procmeter_generic.body_start,
  385                     pos,pmw->procmeter_generic.body_height+pmw->procmeter_generic.body_start);
  386          }
  387    }
  388 }
  389 
  390 
  391 /*++++++++++++++++++++++++++++++++++++++
  392   Add a data point to the ProcMeter Bar Widget.
  393 
  394   Widget w The ProcMeter Bar Widget.
  395 
  396   unsigned short datum The data point to add.
  397   ++++++++++++++++++++++++++++++++++++++*/
  398 
  399 void ProcMeterBarAddDatum(Widget w,unsigned short datum)
  400 {
  401  ProcMeterBarWidget pmw=(ProcMeterBarWidget)w;
  402  int new_grid_num;
  403  unsigned short old_datum;
  404 
  405  pmw->procmeter_bar.data_index++;
  406  if(pmw->procmeter_bar.data_index==8)
  407     pmw->procmeter_bar.data_index=0;
  408 
  409  old_datum=pmw->procmeter_bar.data[pmw->procmeter_bar.data_index];
  410  pmw->procmeter_bar.data[pmw->procmeter_bar.data_index]=datum;
  411 
  412  pmw->procmeter_bar.data_sum=(pmw->procmeter_bar.data_sum>>1)+datum-(old_datum>>8);
  413 
  414  if((pmw->procmeter_bar.data_sum/2)>datum)
  415     new_grid_num=((pmw->procmeter_bar.data_sum/2)+(PROCMETER_GRAPH_SCALE-1))/PROCMETER_GRAPH_SCALE;
  416  else
  417     new_grid_num=(datum+(PROCMETER_GRAPH_SCALE-1))/PROCMETER_GRAPH_SCALE;
  418 
  419  if(new_grid_num<pmw->procmeter_bar.grid_min)
  420     new_grid_num=pmw->procmeter_bar.grid_min;
  421  if(pmw->procmeter_bar.grid_max && new_grid_num>pmw->procmeter_bar.grid_max)
  422     new_grid_num=pmw->procmeter_bar.grid_max;
  423 
  424  if(new_grid_num!=pmw->procmeter_bar.grid_num)
  425    {
  426     pmw->procmeter_bar.grid_num=new_grid_num;
  427 
  428     if(pmw->procmeter_bar.grid_num>pmw->procmeter_bar.grid_maxvis && pmw->procmeter_bar.grid_drawn)
  429        pmw->procmeter_bar.grid_drawn=-1;
  430     if(pmw->procmeter_bar.grid_num<=pmw->procmeter_bar.grid_maxvis && pmw->procmeter_bar.grid_drawn)
  431        pmw->procmeter_bar.grid_drawn=1;
  432    }
  433 
  434  BarUpdate(pmw,False);
  435 }