"Fossies" - the Fresh Open Source Software Archive

Member "procmeter3-3.6+svn387/gtk2/widgets/PMGeneric.c" (24 Dec 2010, 13421 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 "PMGeneric.c" see the Fossies "Dox" file reference documentation.

    1 /***************************************
    2   $Header: /home/amb/CVS/procmeter3/gtk2/widgets/PMGeneric.c,v 1.2 2007-11-21 19:57:18 amb Exp $
    3 
    4   ProcMeter Generic Widget Source file (for ProcMeter 3.5a).
    5   ******************/ /******************
    6   Written by Andrew M. Bishop
    7 
    8   This file Copyright 1996,98,2000,10,02,03,07 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 <stdlib.h>
   16 #include <string.h>
   17 
   18 #include "PMGeneric.h"
   19 
   20 static void procmetergeneric_class_init(ProcMeterGenericClass *class);
   21 static void procmetergeneric_init(ProcMeterGeneric *pmw);
   22 static void destroy(GtkObject *object);
   23 static void realize(GtkWidget *widget);
   24 static gint expose(GtkWidget *widget,GdkEventExpose *event);
   25 static void size_request(GtkWidget *widget,GtkRequisition *requisition);
   26 static void size_allocate(GtkWidget *widget,GtkAllocation *allocation);
   27 
   28 static void GenericResize(ProcMeterGeneric *pmw);
   29 static void GenericUpdate(ProcMeterGeneric *pmw);
   30 
   31 static char *empty_string="";
   32 
   33 static GtkWidgetClass *parent_class=NULL;
   34 
   35 
   36 /*++++++++++++++++++++++++++++++++++++++
   37   Returns the type of a Widget.
   38 
   39   guint procmetergeneric_get_type Returns a unique pointer to the Widget type.
   40   ++++++++++++++++++++++++++++++++++++++*/
   41 
   42 guint gtk_procmetergeneric_get_type(void)
   43 {
   44  static guint pmw_type=0;
   45 
   46  if(!pmw_type)
   47    {
   48     GtkTypeInfo pmw_info={"ProcMeterGeneric",
   49                           sizeof(ProcMeterGeneric),
   50                           sizeof(ProcMeterGenericClass),
   51                           (GtkClassInitFunc)procmetergeneric_class_init,
   52                           (GtkObjectInitFunc)procmetergeneric_init,
   53                           (gpointer) NULL,
   54                           (gpointer) NULL,
   55                           (GtkClassInitFunc) NULL};
   56 
   57     pmw_type=gtk_type_unique(gtk_widget_get_type(),&pmw_info);
   58    }
   59 
   60  return(pmw_type);
   61 }
   62 
   63 
   64 /*++++++++++++++++++++++++++++++++++++++
   65   Initialise the Widget class
   66 
   67   ProcMeterGenericClass *class The class of widget to initialise.
   68   ++++++++++++++++++++++++++++++++++++++*/
   69 
   70 static void procmetergeneric_class_init(ProcMeterGenericClass *class)
   71 {
   72  GtkObjectClass *object_class;
   73  GtkWidgetClass *widget_class;
   74 
   75  g_return_if_fail(class!=NULL);
   76 
   77  object_class=(GtkObjectClass*)class;
   78  widget_class=(GtkWidgetClass*)class;
   79 
   80  class->resize=GenericResize;
   81  class->update=GenericUpdate;
   82 
   83  parent_class=gtk_type_class(gtk_widget_get_type());
   84 
   85  object_class->destroy=destroy;
   86 
   87  widget_class->realize=realize;
   88  widget_class->expose_event=expose;
   89  widget_class->size_request=size_request;
   90  widget_class->size_allocate=size_allocate;
   91 }
   92 
   93 
   94 /*++++++++++++++++++++++++++++++++++++++
   95   Create a new Widget.
   96 
   97   GtkWidget* gtk_procmetergeneric_new Returns the new widgets.
   98   ++++++++++++++++++++++++++++++++++++++*/
   99 
  100 GtkWidget* gtk_procmetergeneric_new(void)
  101 {
  102  ProcMeterGeneric *new;
  103 
  104  new=gtk_type_new(gtk_procmetergeneric_get_type());
  105 
  106  return(GTK_WIDGET(new));
  107 }
  108 
  109 
  110 /*++++++++++++++++++++++++++++++++++++++
  111   Initialise an instance of the Widget
  112 
  113   ProcMeterGeneric *pmw The Widget to initialise.
  114   ++++++++++++++++++++++++++++++++++++++*/
  115 
  116 static void procmetergeneric_init(ProcMeterGeneric *pmw)
  117 {
  118  g_return_if_fail(pmw!=NULL);
  119 
  120  /* The body parts. */
  121 
  122  pmw->body_gc=NULL;
  123 
  124  pmw->body_bg_set=FALSE;
  125 
  126  /* The label parts. */
  127 
  128  pmw->label_pos=ProcMeterLabelBottom;
  129 
  130  pmw->label_string=empty_string;
  131 
  132  pmw->label_font=NULL;
  133 
  134  pmw->label_gc=NULL;
  135 
  136  /* The rest of the sizing. */
  137 
  138  GenericResize(pmw);
  139 }
  140 
  141 
  142 /*++++++++++++++++++++++++++++++++++++++
  143   Destroy a Widget
  144 
  145   GtkObject *object The widget to destroy.
  146   ++++++++++++++++++++++++++++++++++++++*/
  147 
  148 static void destroy(GtkObject *object)
  149 {
  150  ProcMeterGeneric *pmw;
  151 
  152  g_return_if_fail(object!=NULL);
  153  g_return_if_fail(GTK_IS_PROCMETERGENERIC(object));
  154 
  155  pmw=GTK_PROCMETERGENERIC(object);
  156 
  157  if(pmw->label_string!=empty_string)
  158    {
  159     free(pmw->label_string);
  160     pmw->label_string=empty_string;
  161    }
  162  if(pmw->body_gc)
  163    {
  164     gdk_gc_destroy(pmw->body_gc);
  165     pmw->body_gc=NULL;
  166    }
  167  if(pmw->label_gc)
  168    {
  169     gdk_gc_destroy(pmw->label_gc);
  170     pmw->label_gc=NULL;
  171    }
  172 
  173  if(GTK_OBJECT_CLASS(parent_class)->destroy)
  174     (*GTK_OBJECT_CLASS(parent_class)->destroy)(object);
  175 }
  176 
  177 
  178 /*++++++++++++++++++++++++++++++++++++++
  179   Realize a widget.
  180 
  181   GtkWidget *widget The widget to realize.
  182   ++++++++++++++++++++++++++++++++++++++*/
  183 
  184 static void realize(GtkWidget *widget)
  185 {
  186  ProcMeterGeneric *pmw;
  187  GdkWindowAttr attributes;
  188  gint attributes_mask;
  189 
  190  g_return_if_fail(widget!=NULL);
  191  g_return_if_fail(GTK_IS_PROCMETERGENERIC(widget));
  192 
  193  GTK_WIDGET_SET_FLAGS(widget,GTK_REALIZED);
  194  pmw=GTK_PROCMETERGENERIC(widget);
  195 
  196  attributes.x=widget->allocation.x;
  197  attributes.y=widget->allocation.y;
  198  attributes.width=widget->allocation.width;
  199  attributes.height=widget->allocation.height;
  200  attributes.wclass=GDK_INPUT_OUTPUT;
  201  attributes.window_type=GDK_WINDOW_CHILD;
  202  attributes.event_mask=gtk_widget_get_events(widget)|GDK_EXPOSURE_MASK|GDK_BUTTON_PRESS_MASK;
  203  attributes.visual=gtk_widget_get_visual(widget);
  204  attributes.colormap=gtk_widget_get_colormap(widget);
  205 
  206  attributes_mask=GDK_WA_X|GDK_WA_Y|GDK_WA_VISUAL|GDK_WA_COLORMAP;
  207  widget->window=gdk_window_new(widget->parent->window,&attributes,attributes_mask);
  208 
  209  widget->style=gtk_style_attach(widget->style,widget->window);
  210 
  211  gdk_window_set_user_data(widget->window,widget);
  212 
  213  gtk_style_set_background(widget->style,widget->window,GTK_STATE_ACTIVE);
  214 
  215  if(pmw->body_bg_set)
  216     gdk_window_set_background(widget->window,&pmw->body_bg_color);
  217 
  218  GenericUpdate(pmw);
  219 }
  220 
  221 
  222 /*++++++++++++++++++++++++++++++++++++++
  223   Choose the size that the widget wants to be.
  224 
  225   GtkWidget *widget The widget to be resized.
  226 
  227   GtkRequisition *requisition Returns the request for the size.
  228   ++++++++++++++++++++++++++++++++++++++*/
  229 
  230 static void size_request(GtkWidget *widget,GtkRequisition *requisition)
  231 {
  232  ProcMeterGeneric *pmw;
  233 
  234  g_return_if_fail(widget!=NULL);
  235  g_return_if_fail(GTK_IS_PROCMETERGENERIC(widget));
  236  g_return_if_fail(requisition!=NULL);
  237 
  238  pmw=GTK_PROCMETERGENERIC(widget);
  239 
  240  requisition->height=10+pmw->label_height;
  241  requisition->width=50;
  242 }
  243 
  244 
  245 /*++++++++++++++++++++++++++++++++++++++
  246   Change to the size that has been specified by the container.
  247 
  248   GtkWidget *widget The widget that has been resized.
  249 
  250   GtkAllocation *allocation The size information.
  251   ++++++++++++++++++++++++++++++++++++++*/
  252 
  253 static void size_allocate(GtkWidget *widget,GtkAllocation *allocation)
  254 {
  255  g_return_if_fail(widget!=NULL);
  256  g_return_if_fail(GTK_IS_PROCMETERGENERIC(widget));
  257  g_return_if_fail(allocation!=NULL);
  258 
  259  widget->allocation=*allocation;
  260  if(GTK_WIDGET_REALIZED(widget))
  261    {
  262     ProcMeterGeneric *pmw=GTK_PROCMETERGENERIC(widget);
  263 
  264     gdk_window_move_resize(widget->window,
  265                            allocation->x,allocation->y,
  266                            allocation->width,allocation->height);
  267 
  268     GenericResize(pmw);
  269    }
  270 }
  271 
  272 
  273 /*++++++++++++++++++++++++++++++++++++++
  274   Redisplay the ProcMeter Generic Widget.
  275 
  276   gint expose Returns false
  277 
  278   GtkWidget *widget The Widget to redisplay.
  279 
  280   GdkEventExpose *event The event that caused the redisplay.
  281   ++++++++++++++++++++++++++++++++++++++*/
  282 
  283 static gint expose(GtkWidget *widget,GdkEventExpose *event)
  284 {
  285  ProcMeterGeneric *pmw;
  286 
  287  g_return_val_if_fail(widget!=NULL,FALSE);
  288  g_return_val_if_fail(GTK_IS_PROCMETERGENERIC(widget),FALSE);
  289  g_return_val_if_fail(event!=NULL,FALSE);
  290 
  291  if(event->count>0)
  292     return(FALSE);
  293 
  294  pmw=GTK_PROCMETERGENERIC(widget);
  295 
  296  GenericUpdate(pmw);
  297 
  298  return(FALSE);
  299 }
  300 
  301 
  302 /*++++++++++++++++++++++++++++++++++++++
  303   Perform all of the sizing on the Widget when it is created/resized.
  304 
  305   ProcMeterGeneric *pmw The Widget to resize.
  306   ++++++++++++++++++++++++++++++++++++++*/
  307 
  308 static void GenericResize(ProcMeterGeneric *pmw)
  309 {
  310  g_return_if_fail(pmw!=NULL);
  311 
  312  /* The label parts. */
  313 
  314  if(pmw->label_pos)
  315    {
  316     GdkFont *label_font=pmw->label_font?pmw->label_font:gtk_style_get_font(pmw->widget.style);
  317 
  318     pmw->label_height=label_font->ascent+label_font->descent+2;
  319     pmw->label_x=(pmw->widget.allocation.width-gdk_string_width(label_font,pmw->label_string))/2;
  320     if(pmw->label_pos==ProcMeterLabelTop)
  321        pmw->label_y=pmw->label_height-1-label_font->descent;
  322     else
  323        pmw->label_y=pmw->widget.allocation.height-label_font->descent;
  324    }
  325  else
  326    {
  327     pmw->label_height=0;
  328     pmw->label_x=0;
  329     pmw->label_y=0;
  330    }
  331 
  332  /* The body parts. */
  333 
  334  pmw->body_height=pmw->widget.allocation.height-pmw->label_height;
  335 
  336  if(pmw->label_pos==ProcMeterLabelTop)
  337     pmw->body_start=pmw->label_height;
  338  else
  339     pmw->body_start=0;
  340 }
  341 
  342 
  343 /*++++++++++++++++++++++++++++++++++++++
  344   Update the display of the generic part of the widget.
  345 
  346   ProcMeterGeneric *pmw The Widget to update.
  347   ++++++++++++++++++++++++++++++++++++++*/
  348 
  349 static void GenericUpdate(ProcMeterGeneric *pmw)
  350 {
  351  g_return_if_fail(pmw!=NULL);
  352 
  353  if(GTK_WIDGET_VISIBLE(&pmw->widget))
  354    {
  355     GdkFont *label_font=pmw->label_font?pmw->label_font:gtk_style_get_font(pmw->widget.style);
  356     GdkGC *label_gc=pmw->label_gc?pmw->label_gc:pmw->widget.style->fg_gc[GTK_STATE_NORMAL];
  357 
  358     gdk_window_clear(pmw->widget.window);
  359 
  360     if(pmw->label_pos)
  361       {
  362        gdk_draw_string(pmw->widget.window,label_font,label_gc,
  363                        pmw->label_x,pmw->label_y,
  364                        pmw->label_string);
  365 
  366        if(pmw->label_pos==ProcMeterLabelTop)
  367           gdk_draw_line(pmw->widget.window,label_gc,
  368                         0                           ,pmw->label_height-1,
  369                         pmw->widget.allocation.width,pmw->label_height-1);
  370        else
  371           gdk_draw_line(pmw->widget.window,label_gc,
  372                         0                           ,pmw->body_height,
  373                         pmw->widget.allocation.width,pmw->body_height);
  374       }
  375    }
  376 }
  377 
  378 
  379 /*++++++++++++++++++++++++++++++++++++++
  380   Set the body colours of the Widget.
  381 
  382   ProcMeterGeneric *pmw The widget to set.
  383 
  384   GdkColor body_bg_color The body background.
  385   ++++++++++++++++++++++++++++++++++++++*/
  386 
  387 void ProcMeterGenericSetBackgroundColour(ProcMeterGeneric *pmw,GdkColor body_bg_color)
  388 {
  389  pmw->body_bg_color=body_bg_color;
  390 
  391  pmw->body_bg_set=TRUE;
  392 
  393  if(pmw->widget.window)
  394     gdk_window_set_background(pmw->widget.window,&pmw->body_bg_color);
  395 
  396  if(pmw->body_gc)
  397     gdk_gc_set_background(pmw->body_gc,&pmw->body_bg_color);
  398  else
  399    {
  400     GdkGCValues values;
  401 
  402     values.background=pmw->body_bg_color;
  403     pmw->body_gc=gdk_gc_new_with_values(pmw->widget.parent->window,&values,GDK_GC_BACKGROUND);
  404    }
  405 
  406  if(pmw->label_gc)
  407     gdk_gc_set_background(pmw->label_gc,&pmw->body_bg_color);
  408  else
  409    {
  410     GdkGCValues values;
  411 
  412     values.background=pmw->body_bg_color;
  413     pmw->label_gc=gdk_gc_new_with_values(pmw->widget.parent->window,&values,GDK_GC_BACKGROUND);
  414    }
  415 
  416  GenericUpdate(pmw);
  417 }
  418 
  419 
  420 /*++++++++++++++++++++++++++++++++++++++
  421   Set the body colours of the Widget.
  422 
  423   ProcMeterGeneric *pmw The widget to set.
  424 
  425   GdkColor body_fg_color The body foreground.
  426   ++++++++++++++++++++++++++++++++++++++*/
  427 
  428 void ProcMeterGenericSetForegroundColour(ProcMeterGeneric *pmw,GdkColor body_fg_color)
  429 {
  430  pmw->body_fg_color=body_fg_color;
  431 
  432  if(pmw->body_gc)
  433     gdk_gc_set_foreground(pmw->body_gc,&pmw->body_fg_color);
  434  else
  435    {
  436     GdkGCValues values;
  437 
  438     values.foreground=pmw->body_fg_color;
  439     pmw->body_gc=gdk_gc_new_with_values(pmw->widget.parent->window,&values,GDK_GC_FOREGROUND);
  440    }
  441 
  442  GenericUpdate(pmw);
  443 }
  444 
  445 
  446 /*++++++++++++++++++++++++++++++++++++++
  447   Set the label colour of the Widget.
  448 
  449   ProcMeterGeneric *pmw The widget to set.
  450 
  451   GdkColor label_color The label foreground.
  452   ++++++++++++++++++++++++++++++++++++++*/
  453 
  454 void ProcMeterGenericSetLabelColour(ProcMeterGeneric *pmw,GdkColor label_color)
  455 {
  456  pmw->label_color=label_color;
  457 
  458  if(pmw->label_gc)
  459     gdk_gc_set_foreground(pmw->label_gc,&pmw->label_color);
  460  else
  461    {
  462     GdkGCValues values;
  463 
  464     values.foreground=pmw->label_color;
  465     pmw->label_gc=gdk_gc_new_with_values(pmw->widget.parent->window,&values,GDK_GC_FOREGROUND);
  466    }
  467 
  468  GenericUpdate(pmw);
  469 }
  470 
  471 
  472 /*++++++++++++++++++++++++++++++++++++++
  473   Set the label position of the Widget.
  474 
  475   ProcMeterGeneric *pmw The widget to set.
  476 
  477   int label_position The position of the label.
  478   ++++++++++++++++++++++++++++++++++++++*/
  479 
  480 void ProcMeterGenericSetLabelPosition(ProcMeterGeneric *pmw,int label_position)
  481 {
  482  if((label_position==ProcMeterLabelTop) ||
  483     (label_position==ProcMeterLabelNone) ||
  484     (label_position==ProcMeterLabelBottom))
  485    {
  486     pmw->label_pos=label_position;
  487 
  488     GenericResize(pmw);
  489 
  490     GenericUpdate(pmw);
  491    }
  492 }
  493 
  494 
  495 /*++++++++++++++++++++++++++++++++++++++
  496   Set the label font for the widget
  497 
  498   ProcMeterGeneric *pmw The widget to set.
  499 
  500   GdkFont *font The font to use.
  501   ++++++++++++++++++++++++++++++++++++++*/
  502 
  503 void ProcMeterGenericSetLabelFont(ProcMeterGeneric *pmw,GdkFont *font)
  504 {
  505  pmw->label_font=font;
  506 
  507  GenericResize(pmw);
  508 
  509  GenericUpdate(pmw);
  510 }
  511 
  512 
  513 /*++++++++++++++++++++++++++++++++++++++
  514   Set the label for the widget
  515 
  516   ProcMeterGeneric *pmw The widget to set.
  517 
  518   gchar *label The name of the label.
  519   ++++++++++++++++++++++++++++++++++++++*/
  520 
  521 void ProcMeterGenericSetLabel(ProcMeterGeneric *pmw,gchar *label)
  522 {
  523  pmw->label_string=(char*)malloc(strlen(label)+1);
  524  strcpy(pmw->label_string,label);
  525 
  526  GenericUpdate(pmw);
  527 }