"Fossies" - the Fresh Open Source Software Archive

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

    1 /***************************************
    2   $Header: /home/amb/CVS/procmeter3/gtk1/widgets/PMText.c,v 1.3 2003-06-21 18:39:05 amb Exp $
    3 
    4   ProcMeter Text Widget Source file (for ProcMeter 3.4a).
    5   ******************/ /******************
    6   Written by Andrew M. Bishop
    7 
    8   This file Copyright 1996,98,2000,01,02,03 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 #include "PMText.h"
   20 
   21 static void procmetertext_class_init (ProcMeterTextClass *class);
   22 static void procmetertext_init(ProcMeterText *pmw);
   23 static void destroy(GtkObject *object);
   24 static void realize(GtkWidget *widget);
   25 static gint expose(GtkWidget *widget,GdkEventExpose *event);
   26 static void size_request(GtkWidget *widget,GtkRequisition *requisition);
   27 static void size_allocate(GtkWidget *widget,GtkAllocation *allocation);
   28 
   29 static void TextResize(ProcMeterText *pmw);
   30 static void TextUpdate(ProcMeterText *pmw,gboolean all);
   31 
   32 static char *empty_string="";
   33 
   34 static ProcMeterGenericClass *parent_class=NULL;
   35 
   36 
   37 /*++++++++++++++++++++++++++++++++++++++
   38   Returns the type of a Widget.
   39 
   40   guint gtk_procmetertext_get_type Returns a unique pointer to the Widget type.
   41   ++++++++++++++++++++++++++++++++++++++*/
   42 
   43 guint gtk_procmetertext_get_type(void)
   44 {
   45  static guint pmw_type = 0;
   46 
   47  if(!pmw_type)
   48    {
   49     GtkTypeInfo pmw_info={"ProcMeterText",
   50                           sizeof(ProcMeterText),
   51                           sizeof(ProcMeterTextClass),
   52                           (GtkClassInitFunc) procmetertext_class_init,
   53                           (GtkObjectInitFunc) procmetertext_init,
   54                           (GtkArgSetFunc) NULL,
   55                           (GtkArgGetFunc) NULL};
   56 
   57     pmw_type=gtk_type_unique(gtk_procmetergeneric_get_type(),&pmw_info);
   58    }
   59 
   60  return(pmw_type);
   61 }
   62 
   63 
   64 /*++++++++++++++++++++++++++++++++++++++
   65   Initialise the Widget class
   66 
   67   ProcMeterTextClass *class The class of widget to initialise.
   68   ++++++++++++++++++++++++++++++++++++++*/
   69 
   70 static void procmetertext_class_init(ProcMeterTextClass *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=TextResize;
   81  class->update=TextUpdate;
   82 
   83  parent_class=gtk_type_class(gtk_procmetergeneric_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   Initialise an instance of the Widget
   96 
   97   ProcMeterText *pmw The Widget to initialise.
   98   ++++++++++++++++++++++++++++++++++++++*/
   99 
  100 static void procmetertext_init(ProcMeterText *pmw)
  101 {
  102  g_return_if_fail(pmw!=NULL);
  103 
  104  /* The text parts. */
  105 
  106  pmw->text_string=empty_string;
  107 
  108  pmw->text_font=NULL;
  109 
  110  /* The rest of the sizing. */
  111 
  112  TextResize(pmw);
  113 }
  114 
  115 
  116 /*++++++++++++++++++++++++++++++++++++++
  117   Create a new Widget.
  118 
  119   GtkWidget* gtk_procmetertext_new Returns the new widgets.
  120   ++++++++++++++++++++++++++++++++++++++*/
  121 
  122 GtkWidget* gtk_procmetertext_new(void)
  123 {
  124  ProcMeterText *pmw;
  125 
  126  pmw=gtk_type_new(gtk_procmetertext_get_type());
  127 
  128  return(GTK_WIDGET(pmw));
  129 }
  130 
  131 
  132 /*++++++++++++++++++++++++++++++++++++++
  133   Destroy a Widget
  134 
  135   GtkObject *object The widget to destroy.
  136   ++++++++++++++++++++++++++++++++++++++*/
  137 
  138 static void destroy(GtkObject *object)
  139 {
  140  ProcMeterText *pmw;
  141 
  142  g_return_if_fail(object!=NULL);
  143  g_return_if_fail(GTK_IS_PROCMETERTEXT(object));
  144 
  145  pmw=GTK_PROCMETERTEXT(object);
  146 
  147  if(pmw->text_string!=empty_string)
  148     free(pmw->text_string);
  149 
  150  if(GTK_OBJECT_CLASS(parent_class)->destroy)
  151     (*GTK_OBJECT_CLASS(parent_class)->destroy)(object);
  152 }
  153 
  154 
  155 /*++++++++++++++++++++++++++++++++++++++
  156   Realize a widget.
  157 
  158   GtkWidget *widget The widget to realize.
  159   ++++++++++++++++++++++++++++++++++++++*/
  160 
  161 static void realize(GtkWidget *widget)
  162 {
  163  ProcMeterText *pmw;
  164  GdkWindowAttr attributes;
  165  gint attributes_mask;
  166 
  167  g_return_if_fail(widget!=NULL);
  168  g_return_if_fail(GTK_IS_PROCMETERTEXT(widget));
  169 
  170  GTK_WIDGET_SET_FLAGS(widget,GTK_REALIZED);
  171  pmw=GTK_PROCMETERTEXT(widget);
  172 
  173  attributes.x=widget->allocation.x;
  174  attributes.y=widget->allocation.y;
  175  attributes.width=widget->allocation.width;
  176  attributes.height=widget->allocation.height;
  177  attributes.wclass=GDK_INPUT_OUTPUT;
  178  attributes.window_type=GDK_WINDOW_CHILD;
  179  attributes.event_mask=gtk_widget_get_events(widget)|GDK_EXPOSURE_MASK|GDK_BUTTON_PRESS_MASK;
  180  attributes.visual=gtk_widget_get_visual(widget);
  181  attributes.colormap=gtk_widget_get_colormap(widget);
  182 
  183  attributes_mask=GDK_WA_X|GDK_WA_Y|GDK_WA_VISUAL|GDK_WA_COLORMAP;
  184  widget->window=gdk_window_new(widget->parent->window,&attributes,attributes_mask);
  185 
  186  widget->style=gtk_style_attach(widget->style,widget->window);
  187 
  188  gdk_window_set_user_data(widget->window,widget);
  189 
  190  gtk_style_set_background(widget->style,widget->window,GTK_STATE_ACTIVE);
  191 
  192  if(pmw->generic.body_bg_set)
  193     gdk_window_set_background(widget->window,&pmw->generic.body_bg_color);
  194 
  195  TextUpdate(pmw,TRUE);
  196 }
  197 
  198 
  199 /*++++++++++++++++++++++++++++++++++++++
  200   Choose the size that the widget wants to be.
  201 
  202   GtkWidget *widget The widget to be resized.
  203 
  204   GtkRequisition *requisition Returns the request for the size.
  205   ++++++++++++++++++++++++++++++++++++++*/
  206 
  207 static void size_request(GtkWidget *widget,GtkRequisition *requisition)
  208 {
  209  ProcMeterText *pmw;
  210  GdkFont *text_font;
  211 
  212  g_return_if_fail(widget!=NULL);
  213  g_return_if_fail(GTK_IS_PROCMETERTEXT(widget));
  214  g_return_if_fail(requisition!=NULL);
  215 
  216  pmw=GTK_PROCMETERTEXT(widget);
  217  text_font=pmw->text_font?pmw->text_font:pmw->generic.widget.style->font;
  218 
  219  requisition->width=gdk_string_width(text_font,"NNNNNNNNNNNNNNN");
  220  requisition->height=text_font->ascent+text_font->descent+2+pmw->generic.label_height;
  221 }
  222 
  223 
  224 /*++++++++++++++++++++++++++++++++++++++
  225   Change to the size that has been specified by the container.
  226 
  227   GtkWidget *widget The widget that has been resized.
  228 
  229   GtkAllocation *allocation The size information.
  230   ++++++++++++++++++++++++++++++++++++++*/
  231 
  232 static void size_allocate(GtkWidget *widget,GtkAllocation *allocation)
  233 {
  234  g_return_if_fail(widget!=NULL);
  235  g_return_if_fail(GTK_IS_PROCMETERTEXT(widget));
  236  g_return_if_fail(allocation!=NULL);
  237 
  238  widget->allocation=*allocation;
  239  if(GTK_WIDGET_REALIZED(widget))
  240    {
  241     ProcMeterText *pmw=GTK_PROCMETERTEXT(widget);
  242 
  243     gdk_window_move_resize(widget->window,
  244                            allocation->x,allocation->y,
  245                            allocation->width,allocation->height);
  246 
  247     TextResize(pmw);
  248    }
  249 }
  250 
  251 
  252 /*++++++++++++++++++++++++++++++++++++++
  253   Redisplay the ProcMeter Text Widget.
  254 
  255   gint expose Returns false
  256 
  257   GtkWidget *widget The Widget to redisplay.
  258 
  259   GdkEventExpose *event The event that caused the redisplay.
  260   ++++++++++++++++++++++++++++++++++++++*/
  261 
  262 static gint expose(GtkWidget *widget,GdkEventExpose *event)
  263 {
  264  ProcMeterText *pmw;
  265 
  266  g_return_val_if_fail(widget!=NULL,FALSE);
  267  g_return_val_if_fail(GTK_IS_PROCMETERTEXT(widget),FALSE);
  268  g_return_val_if_fail(event!=NULL,FALSE);
  269 
  270  if(event->count>0)
  271     return(FALSE);
  272 
  273  pmw=GTK_PROCMETERTEXT(widget);
  274 
  275  TextUpdate(pmw,TRUE);
  276 
  277  return(FALSE);
  278 }
  279 
  280 
  281 /*++++++++++++++++++++++++++++++++++++++
  282   Perform all of the sizing on the Widget when it is created/resized.
  283 
  284   ProcMeterText *pmw The Widget to resize.
  285   ++++++++++++++++++++++++++++++++++++++*/
  286 
  287 static void TextResize(ProcMeterText *pmw)
  288 {
  289  GdkFont *text_font;
  290 
  291  g_return_if_fail(pmw!=NULL);
  292 
  293  (parent_class->resize)(&pmw->generic);
  294 
  295  /* The text parts. */
  296 
  297  text_font=pmw->text_font?pmw->text_font:pmw->generic.widget.style->font;
  298 
  299  pmw->text_x=(pmw->generic.widget.allocation.width-gdk_string_width(text_font,pmw->text_string))/2;
  300  pmw->text_y=pmw->generic.body_start+1+text_font->ascent;
  301 }
  302 
  303 
  304 /*++++++++++++++++++++++++++++++++++++++
  305   Update the display.
  306 
  307   ProcMeterText *pmw The Widget to update.
  308 
  309   gboolean all Indicates if the whole widget is to be updated.
  310   ++++++++++++++++++++++++++++++++++++++*/
  311 
  312 static void TextUpdate(ProcMeterText *pmw,gboolean all)
  313 {
  314  g_return_if_fail(pmw!=NULL);
  315 
  316  if(GTK_WIDGET_VISIBLE(&pmw->generic.widget))
  317    {
  318     GdkGC *body_gc=pmw->generic.body_gc?pmw->generic.body_gc:pmw->generic.widget.style->fg_gc[GTK_STATE_NORMAL];
  319     GdkFont *text_font=pmw->text_font?pmw->text_font:pmw->generic.widget.style->font;
  320 
  321     if(all)
  322        (parent_class->update)(&pmw->generic);
  323     else
  324        gdk_window_clear_area(pmw->generic.widget.window,
  325                              0,pmw->generic.body_start,
  326                              pmw->generic.widget.allocation.width,pmw->generic.body_height);
  327 
  328     gdk_draw_string(pmw->generic.widget.window,text_font,body_gc,
  329                     pmw->text_x,pmw->text_y,
  330                     pmw->text_string);
  331    }
  332 }
  333 
  334 
  335 /*++++++++++++++++++++++++++++++++++++++
  336   Set the text font for the widget
  337 
  338   ProcMeterText *pmw The widget to set.
  339 
  340   GdkFont *font The font to use.
  341   ++++++++++++++++++++++++++++++++++++++*/
  342 
  343 void ProcMeterTextSetFont(ProcMeterText *pmw,GdkFont *font)
  344 {
  345  pmw->text_font=font;
  346 
  347  TextResize(pmw);
  348 
  349  TextUpdate(pmw,TRUE);
  350 }
  351 
  352 
  353 /*++++++++++++++++++++++++++++++++++++++
  354   Change the data displayed in the ProcMeter Text Widget.
  355 
  356   ProcMeterText *pmw The ProcMeter Text Widget.
  357 
  358   char *text The new string to display.
  359   ++++++++++++++++++++++++++++++++++++++*/
  360 
  361 void ProcMeterTextChangeData(ProcMeterText *pmw,char *text)
  362 {
  363  if(pmw->text_string!=empty_string)
  364     free(pmw->text_string);
  365  pmw->text_string=(char*)malloc(strlen(text)+1);
  366  strcpy(pmw->text_string,text);
  367 
  368  pmw->text_x=(pmw->generic.widget.allocation.width-gdk_string_width(pmw->text_font,pmw->text_string))/2;
  369 
  370  TextUpdate(pmw,FALSE);
  371 }