home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / libgimp / gimppixmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-19  |  4.8 KB  |  198 lines

  1. /* LIBGIMP - The GIMP Library 
  2.  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball 
  3.  *
  4.  * gimppixmap.c
  5.  * Copyright (C) 2000 Michael Natterer <mitch@gimp.org>
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the
  19.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20.  * Boston, MA 02111-1307, USA.
  21.  */
  22.  
  23. #include <stdio.h>
  24.  
  25. #include <gtk/gtk.h>
  26.  
  27. #include "gimppixmap.h"
  28.  
  29.  
  30. static void gimp_pixmap_destroy           (GtkObject  *object);
  31. static void gimp_pixmap_realize           (GtkWidget  *widget);
  32. static void gimp_pixmap_create_from_xpm_d (GimpPixmap *pixmap);
  33.  
  34.  
  35. static GtkPixmapClass *parent_class = NULL;
  36.  
  37.  
  38. static void
  39. gimp_pixmap_destroy (GtkObject *object)
  40. {
  41.   GimpPixmap *pixmap;
  42.  
  43.   g_return_if_fail (pixmap = GIMP_PIXMAP (object));
  44.  
  45.   if (GTK_OBJECT_CLASS (parent_class)->destroy)
  46.     GTK_OBJECT_CLASS (parent_class)->destroy (object);
  47. }
  48.  
  49. static void
  50. gimp_pixmap_class_init (GimpPixmapClass *class)
  51. {
  52.   GtkObjectClass *object_class;
  53.   GtkWidgetClass *widget_class;
  54.  
  55.   object_class = (GtkObjectClass *) class;
  56.   widget_class = (GtkWidgetClass *) class;
  57.  
  58.   parent_class = gtk_type_class (gtk_pixmap_get_type ());
  59.  
  60.   object_class->destroy = gimp_pixmap_destroy;
  61.   widget_class->realize = gimp_pixmap_realize;
  62. }
  63.  
  64. static void
  65. gimp_pixmap_init (GimpPixmap *pixmap)
  66. {
  67.   pixmap->xpm_data = NULL;
  68. }
  69.  
  70. GtkType
  71. gimp_pixmap_get_type (void)
  72. {
  73.   static guint pixmap_type = 0;
  74.  
  75.   if (!pixmap_type)
  76.     {
  77.       GtkTypeInfo pixmap_info =
  78.       {
  79.     "GimpPixmap",
  80.     sizeof (GimpPixmap),
  81.     sizeof (GimpPixmapClass),
  82.     (GtkClassInitFunc) gimp_pixmap_class_init,
  83.     (GtkObjectInitFunc) gimp_pixmap_init,
  84.     /* reserved_1 */ NULL,
  85.     /* reserved_2 */ NULL,
  86.         (GtkClassInitFunc) NULL
  87.       };
  88.  
  89.       pixmap_type = gtk_type_unique (gtk_pixmap_get_type (), &pixmap_info);
  90.     }
  91.  
  92.   return pixmap_type;
  93. }
  94.  
  95. /**
  96.  * gimp_pixmap_new:
  97.  * @xpm_data: A pointer to a XPM data structure as found in XPM files.
  98.  *
  99.  * Creates a new #GimpPixmap widget.
  100.  *
  101.  * Returns: A pointer to the new #GimpPixmap widget.
  102.  **/
  103. GtkWidget *
  104. gimp_pixmap_new (gchar **xpm_data)
  105. {
  106.   GimpPixmap *pixmap;
  107.  
  108.   pixmap = gtk_type_new (gimp_pixmap_get_type ());
  109.  
  110.   gtk_pixmap_set_build_insensitive (GTK_PIXMAP (pixmap), TRUE);
  111.   gimp_pixmap_set (pixmap, xpm_data);
  112.  
  113.   return GTK_WIDGET (pixmap);
  114. }
  115.  
  116. /**
  117.  * gimp_pixmap_set:
  118.  * @pixmap: The pixmap widget you want to set the new xpm_data for.
  119.  * @xpm_data: A pointer to a XPM data structure as found in XPM files.
  120.  *
  121.  * Sets a new image for an existing #GimpPixmap widget.
  122.  **/
  123. void
  124. gimp_pixmap_set (GimpPixmap  *pixmap,
  125.          gchar      **xpm_data)
  126. {
  127.   g_return_if_fail (pixmap != NULL);
  128.   g_return_if_fail (GIMP_IS_PIXMAP (pixmap));
  129.  
  130.   pixmap->xpm_data = xpm_data;
  131.  
  132.   GTK_WIDGET (pixmap)->requisition.width  = 0;
  133.   GTK_WIDGET (pixmap)->requisition.height = 0;
  134.  
  135.   if (! GTK_WIDGET_REALIZED (GTK_WIDGET (pixmap)))
  136.     {
  137.       if (xpm_data)
  138.     {
  139.       gint width, height;
  140.  
  141.       if (sscanf (xpm_data[0], "%d %d", &width, &height) != 2)
  142.         {
  143.           g_warning ("passed pointer is no XPM data");
  144.         }
  145.       else
  146.         {
  147.           GTK_WIDGET (pixmap)->requisition.width =
  148.         width + GTK_MISC (pixmap)->xpad * 2;
  149.           GTK_WIDGET (pixmap)->requisition.height =
  150.         height + GTK_MISC (pixmap)->ypad * 2;
  151.         }
  152.     }
  153.     }
  154.   else
  155.     {
  156.       gimp_pixmap_create_from_xpm_d (pixmap);
  157.     }
  158. }
  159.  
  160. static void
  161. gimp_pixmap_realize (GtkWidget *widget)
  162. {
  163.   if (GTK_WIDGET_CLASS (parent_class)->realize)
  164.     GTK_WIDGET_CLASS (parent_class)->realize (widget);
  165.  
  166.   gimp_pixmap_create_from_xpm_d (GIMP_PIXMAP (widget));
  167. }
  168.  
  169. static void
  170. gimp_pixmap_create_from_xpm_d (GimpPixmap *pixmap)
  171. {
  172.   GtkStyle   *style;
  173.   GdkPixmap  *gdk_pixmap = NULL;
  174.   GdkBitmap  *mask       = NULL;
  175.  
  176.   if (pixmap->xpm_data)
  177.     {
  178.       GtkWidget  *widget;
  179.  
  180.       widget = GTK_WIDGET (pixmap);
  181.  
  182.       style = gtk_widget_get_style (widget);
  183.  
  184.       gdk_pixmap = gdk_pixmap_create_from_xpm_d (widget->window,
  185.                          &mask,
  186.                          &style->bg[GTK_STATE_NORMAL],
  187.                          pixmap->xpm_data);
  188.     }
  189.  
  190.   gtk_pixmap_set (GTK_PIXMAP (pixmap), gdk_pixmap, mask);
  191.  
  192.   if (gdk_pixmap)
  193.     gdk_pixmap_unref (gdk_pixmap);
  194.  
  195.   if (mask)
  196.     gdk_bitmap_unref (mask);
  197. }
  198.