gimppixmap.c 4.42 KB
Newer Older
1 2
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3 4 5 6 7 8 9
 *
 * gimppixmap.c
 * Copyright (C) 2000 Michael Natterer <mitch@gimp.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2 of the License, or (at your option) any later version.
11
 *
12 13 14
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 16 17 18 19 20 21 22
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

23 24
#include <stdio.h>

25 26
#include <gtk/gtk.h>

27 28
#include "gimpwidgetstypes.h"

29 30
#include "gimppixmap.h"

31

32 33
static void   gimp_pixmap_class_init        (GimpPixmapClass *klass);
static void   gimp_pixmap_init              (GimpPixmap      *pixmap);
34

35 36
static void   gimp_pixmap_realize           (GtkWidget       *widget);
static void   gimp_pixmap_create_from_xpm_d (GimpPixmap      *pixmap);
37

38

39
static GtkImageClass *parent_class = NULL;
40 41


42
GType
43 44
gimp_pixmap_get_type (void)
{
45
  static GType pixmap_type = 0;
46

47
  if (! pixmap_type)
48
    {
49
      static const GTypeInfo pixmap_info =
50
      {
51 52 53 54 55 56 57 58 59
        sizeof (GimpPixmapClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gimp_pixmap_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data     */
        sizeof (GimpPixmap),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) gimp_pixmap_init,
60 61
      };

62
      pixmap_type = g_type_register_static (GTK_TYPE_IMAGE,
63 64
					    "GimpPixmap",
					    &pixmap_info, 0);
65 66 67 68 69
    }

  return pixmap_type;
}

70 71 72 73 74
static void
gimp_pixmap_class_init (GimpPixmapClass *klass)
{
  GtkWidgetClass *widget_class;

75
  widget_class = GTK_WIDGET_CLASS (klass);
76 77 78 79 80 81 82 83 84 85 86 87

  parent_class = g_type_class_peek_parent (klass);

  widget_class->realize = gimp_pixmap_realize;
}

static void
gimp_pixmap_init (GimpPixmap *pixmap)
{
  pixmap->xpm_data = NULL;
}

88 89 90 91 92 93 94
/**
 * gimp_pixmap_new:
 * @xpm_data: A pointer to a XPM data structure as found in XPM files.
 *
 * Creates a new #GimpPixmap widget.
 *
 * Returns: A pointer to the new #GimpPixmap widget.
95
 **/
96 97 98 99 100
GtkWidget *
gimp_pixmap_new (gchar **xpm_data)
{
  GimpPixmap *pixmap;

Michael Natterer's avatar
Michael Natterer committed
101
  pixmap = g_object_new (GIMP_TYPE_PIXMAP, NULL);
102

103
  gimp_pixmap_set (pixmap, xpm_data);
104

105 106 107 108
  return GTK_WIDGET (pixmap);
}

/**
Michael Natterer's avatar
Michael Natterer committed
109
 * gimp_pixmap_set:
110 111 112 113
 * @pixmap: The pixmap widget you want to set the new xpm_data for.
 * @xpm_data: A pointer to a XPM data structure as found in XPM files.
 *
 * Sets a new image for an existing #GimpPixmap widget.
114
 **/
115 116 117 118 119
void
gimp_pixmap_set (GimpPixmap  *pixmap,
		 gchar      **xpm_data)
{
  g_return_if_fail (GIMP_IS_PIXMAP (pixmap));
120 121 122

  pixmap->xpm_data = xpm_data;

123 124 125 126
  GTK_WIDGET (pixmap)->requisition.width  = 0;
  GTK_WIDGET (pixmap)->requisition.height = 0;

  if (! GTK_WIDGET_REALIZED (GTK_WIDGET (pixmap)))
127
    {
128 129 130 131 132 133
      if (xpm_data)
	{
	  gint width, height;

	  if (sscanf (xpm_data[0], "%d %d", &width, &height) != 2)
	    {
134
	      g_warning ("%s: passed pointer is no XPM data", G_STRLOC);
135 136 137 138 139 140 141 142 143
	    }
	  else
	    {
	      GTK_WIDGET (pixmap)->requisition.width =
		width + GTK_MISC (pixmap)->xpad * 2;
	      GTK_WIDGET (pixmap)->requisition.height =
		height + GTK_MISC (pixmap)->ypad * 2;
	    }
	}
144 145 146
    }
  else
    {
147
      gimp_pixmap_create_from_xpm_d (pixmap);
148
    }
149 150 151 152 153
}

static void
gimp_pixmap_realize (GtkWidget *widget)
{
154
  if (GTK_WIDGET_CLASS (parent_class)->realize)
155
    GTK_WIDGET_CLASS (parent_class)->realize (widget);
156 157 158 159 160 161 162

  gimp_pixmap_create_from_xpm_d (GIMP_PIXMAP (widget));
}

static void
gimp_pixmap_create_from_xpm_d (GimpPixmap *pixmap)
{
163
  GtkStyle   *style;
164 165
  GdkPixmap  *gdk_pixmap = NULL;
  GdkBitmap  *mask       = NULL;
166

167 168 169
  if (pixmap->xpm_data)
    {
      GtkWidget  *widget;
170

171
      widget = GTK_WIDGET (pixmap);
172

173
      style = gtk_widget_get_style (widget);
174

175 176 177 178 179
      gdk_pixmap = gdk_pixmap_create_from_xpm_d (widget->window,
						 &mask,
						 &style->bg[GTK_STATE_NORMAL],
						 pixmap->xpm_data);
    }
180

181
  gtk_image_set_from_pixmap (GTK_IMAGE (pixmap), gdk_pixmap, mask);
182

183
  if (gdk_pixmap)
184
    g_object_unref (gdk_pixmap);
185 186

  if (mask)
187
    g_object_unref (mask);
188
}