gtkled.c

Go to the documentation of this file.
00001 /* GTK - The GIMP Toolkit
00002  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
00003  *
00004  * GtkLed: Emulate a simple LED (light emitting diode)
00005  * Copyright (C) 1997 Tim Janik
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Library General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Library General Public
00018  * License along with this library; if not, write to the Free
00019  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020  */
00021 #include <config.h>
00022 #include "gtkled.h"
00023 
00024 #define LED_WIDTH       (10)
00025 #define LED_HEIGHT      (5)
00026 #define BOTTOM_SPACE    (2)
00027 
00028 static void gtk_led_class_init   (GtkLedClass    *klass);
00029 static void gtk_led_init         (GtkLed         *led);
00030 static void gtk_led_destroy      (GtkObject      *object);
00031 static void gtk_led_size_request (GtkWidget      *widget,
00032                                   GtkRequisition *requisition);
00033 static gint gtk_led_expose       (GtkWidget      *widget,
00034                                   GdkEventExpose *event);
00035 static void gtk_led_realize      (GtkWidget      *widget);
00036 
00037 
00038 static GtkMiscClass *parent_class = NULL;
00039 
00040 enum {
00041   LED_COLOR_ON,
00042   LED_COLOR_OFF
00043 };
00044 
00045 
00046 GtkType
00047 gtk_led_get_type ()
00048 {
00049   static GtkType led_type = 0;
00050   
00051   if (!led_type)
00052   {
00053     GtkTypeInfo led_info =
00054     {
00055       "GtkLed",
00056       sizeof (GtkLed),
00057       sizeof (GtkLedClass),
00058       (GtkClassInitFunc) gtk_led_class_init,
00059       (GtkObjectInitFunc) gtk_led_init,
00060       /* reserved */ NULL,
00061       /* reserved */ NULL,
00062       (GtkClassInitFunc) NULL,
00063     };
00064     
00065     led_type = gtk_type_unique (gtk_misc_get_type (), &led_info);
00066   }
00067   
00068   return led_type;
00069 }
00070 
00071 void
00072 gtk_led_class_init (GtkLedClass *class)
00073 {
00074   GtkObjectClass *object_class;
00075   GtkWidgetClass *widget_class;
00076   
00077   object_class = (GtkObjectClass*) class;
00078   widget_class = (GtkWidgetClass*) class;
00079   
00080   parent_class = gtk_type_class (gtk_misc_get_type ());
00081   
00082   object_class->destroy = gtk_led_destroy;
00083   
00084   widget_class->size_request  = gtk_led_size_request;
00085   widget_class->expose_event  = gtk_led_expose;
00086   widget_class->realize       = gtk_led_realize;
00087 }
00088 
00089 void
00090 gtk_led_init (GtkLed *led)
00091 {
00092   GtkMisc *misc;
00093   
00094   misc = GTK_MISC (led);
00095 
00096   GTK_WIDGET_SET_FLAGS (led, GTK_NO_WINDOW);
00097   
00098   led->is_on             = FALSE;
00099   led->gc                = NULL;
00100 
00101 }
00102 
00103 GtkWidget*
00104 gtk_led_new ()
00105 {
00106   GtkLed *led;
00107   
00108   led = gtk_type_new (gtk_led_get_type ());
00109 
00110   return GTK_WIDGET (led);
00111 }
00112 
00113 void
00114 gtk_led_set_colors (GtkLed *led, GdkColor *active, GdkColor *inactive)
00115 {
00116 
00117   g_return_if_fail (led != NULL);
00118   g_return_if_fail (GTK_IS_LED (led));
00119 
00120   led->fg[LED_COLOR_ON] = *(active);
00121   led->fg[LED_COLOR_OFF] = *(inactive);
00122 }
00123 
00124 void
00125 gtk_led_set_state (GtkLed       *led,
00126                    GtkStateType widget_state,
00127                    gboolean     on_off)
00128 {
00129   g_return_if_fail (led != NULL);
00130   g_return_if_fail (GTK_IS_LED (led));
00131 
00132   gtk_widget_set_state (GTK_WIDGET (led), widget_state);
00133   gtk_led_switch (led, on_off);
00134 }
00135 
00136 void
00137 gtk_led_switch (GtkLed       *led,
00138                 gboolean     on_off)
00139 {
00140   g_return_if_fail (led != NULL);
00141   g_return_if_fail (GTK_IS_LED (led));
00142 
00143   led->is_on = on_off != FALSE;
00144   gtk_widget_draw (GTK_WIDGET (led), NULL);
00145 }
00146 
00147 gboolean
00148 gtk_led_is_on (GtkLed  *led)
00149 {
00150   g_return_val_if_fail (led != NULL, FALSE);
00151   g_return_val_if_fail (GTK_IS_LED (led), FALSE);
00152   
00153   return led->is_on;
00154 }
00155 
00156 static void
00157 gtk_led_destroy (GtkObject *object)
00158 {
00159   GtkLed *led;
00160   
00161   g_return_if_fail (object != NULL);
00162   g_return_if_fail (GTK_IS_LED (object));
00163   
00164   led = GTK_LED (object);
00165   
00166   if (GTK_WIDGET (object)->parent &&
00167       GTK_WIDGET_MAPPED (object))
00168     gtk_widget_unmap (GTK_WIDGET (object));
00169   
00170   if (GTK_OBJECT_CLASS (parent_class)->destroy)
00171     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
00172 }
00173 
00174 static void
00175 gtk_led_size_request (GtkWidget      *widget,
00176                       GtkRequisition *requisition)
00177 {
00178   GtkLed *led;
00179   
00180   g_return_if_fail (widget != NULL);
00181   g_return_if_fail (GTK_IS_LED (widget));
00182   g_return_if_fail (requisition != NULL);
00183   
00184   led = GTK_LED (widget);
00185   
00186   requisition->width = LED_WIDTH + led->misc.xpad * 2;
00187   requisition->height = LED_HEIGHT + led->misc.ypad * 2 + BOTTOM_SPACE;
00188 }
00189 
00190 static void 
00191 gtk_led_realize (GtkWidget *widget)
00192 {
00193   GtkLed       *led;
00194   GdkColormap  *cmap;
00195 
00196   g_return_if_fail (widget != NULL);
00197   g_return_if_fail (GTK_IS_LED (widget));
00198 
00199   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
00200   led = GTK_LED (widget);
00201   widget->window = gtk_widget_get_parent_window (widget);
00202   gdk_window_ref (widget->window);
00203   widget->style = gtk_style_attach (widget->style, widget->window);
00204   if (!led->gc)
00205     {
00206       cmap = gtk_widget_get_colormap (widget);
00207       
00208       if (!(&(led->fg[LED_COLOR_ON])))
00209         gdk_color_parse ("#00F100", &(led->fg[LED_COLOR_ON]));
00210       gdk_color_alloc (cmap, &(led->fg[LED_COLOR_ON]));
00211       if (!(&(led->fg[LED_COLOR_OFF])))
00212         gdk_color_parse ("#008C00", &(led->fg[LED_COLOR_OFF]));
00213       gdk_color_alloc (cmap, &(led->fg[LED_COLOR_OFF]));
00214       led->gc = gdk_gc_new (widget->window);
00215       gdk_gc_copy (led->gc, widget->style->white_gc);
00216     }
00217         
00218 
00219 }
00220 
00221 static gint
00222 gtk_led_expose (GtkWidget      *widget,
00223                 GdkEventExpose *event)
00224 {
00225   GtkLed   *led;
00226   GtkMisc  *misc;
00227   GdkColor *win_bg;
00228 
00229   g_return_val_if_fail (widget != NULL, FALSE);
00230   g_return_val_if_fail (GTK_IS_LED (widget), FALSE);
00231   g_return_val_if_fail (event != NULL, FALSE);
00232   
00233   led = GTK_LED (widget);
00234   misc = GTK_MISC (widget);
00235 
00236   if (GTK_WIDGET_DRAWABLE (widget))
00237   {
00238     if ((widget->allocation.width >= widget->requisition.width) &&
00239         (widget->allocation.height >= widget->requisition.height))
00240     {
00241       guint x, y;
00242       win_bg = (led->is_on) ? &(led->fg[LED_COLOR_ON]) : 
00243         &(led->fg[LED_COLOR_OFF]);
00244       gdk_gc_set_foreground (led->gc, win_bg);
00245       x = widget->allocation.x + misc->xpad +
00246           (widget->allocation.width - widget->requisition.width) * 
00247         misc->xalign + 0.5;
00248       y = widget->allocation.y + misc->ypad + LED_HEIGHT +
00249           (widget->allocation.height - widget->requisition.height) * 
00250         misc->xalign + 0.5 - BOTTOM_SPACE;
00251 
00252       gtk_draw_shadow (widget->style, widget->window,
00253                        GTK_STATE_NORMAL, GTK_SHADOW_IN,
00254                        x, y,
00255                        LED_WIDTH,
00256                        LED_HEIGHT);
00257 
00258       gdk_draw_rectangle (widget->window,
00259                           led->gc,
00260                           TRUE,
00261                           x + 1, y + 1,
00262                           LED_WIDTH - 2,
00263                           LED_HEIGHT - 2);
00264     }
00265   }
00266   
00267   return TRUE;
00268 }
00269 /* EOF */
00270 

Generated on Sat Apr 1 09:04:12 2006 for GXSM by  doxygen 1.4.6