gimppixmap.c 4.53 KB
Newer Older
1 2
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3 4 5 6
 *
 * gimppixmap.c
 * Copyright (C) 2000 Michael Natterer <mitch@gimp.org>
 *
7
 * This library is free software: you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 3 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18 19
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
20 21
 */

22 23
#include "config.h"

24
#undef GSEAL_ENABLE
25
#undef GTK_DISABLE_DEPRECATED
26
#undef GDK_DISABLE_DEPRECATED
27

28 29
#include <stdio.h>

30 31
#include <gtk/gtk.h>

32 33
#include "gimpwidgetstypes.h"

34
#undef GIMP_DISABLE_DEPRECATED
35 36
#include "gimppixmap.h"

37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/**
 * SECTION: gimppixmap
 * @title: GimpPixmap
 * @short_description: Widget which creates a #GtkPixmap from XPM data.
 * @see_also: gimp_pixmap_button_new(), #GtkPixmap
 *
 * Widget which creates a #GtkPixmap from XPM data.
 *
 * Use this widget instead of #GtkPixmap if you don't want to worry
 * about the parent container's "realized" state.
 *
 * Note that the drawback of the easy interface is that the actual
 * #GdkPixmap and it's mask have to be constructed every time you call
 * gimp_pixmap_new() and cannot be cached in memory without doing bad
 * hacks.
 **/


56 57
static void   gimp_pixmap_realize           (GtkWidget  *widget);
static void   gimp_pixmap_create_from_xpm_d (GimpPixmap *pixmap);
58

59

60
G_DEFINE_TYPE (GimpPixmap, gimp_pixmap, GTK_TYPE_IMAGE)
61

62
#define parent_class gimp_pixmap_parent_class
63 64


65 66 67
static void
gimp_pixmap_class_init (GimpPixmapClass *klass)
{
68
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
69 70 71 72 73 74 75 76 77 78

  widget_class->realize = gimp_pixmap_realize;
}

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

79 80 81 82 83 84 85
/**
 * 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.
86
 **/
87 88 89
GtkWidget *
gimp_pixmap_new (gchar **xpm_data)
{
90
  GimpPixmap *pixmap = g_object_new (GIMP_TYPE_PIXMAP, NULL);
91

92
  gimp_pixmap_set (pixmap, xpm_data);
93

94 95 96 97
  return GTK_WIDGET (pixmap);
}

/**
98
 * gimp_pixmap_set:
99 100 101 102
 * @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.
103
 **/
104 105
void
gimp_pixmap_set (GimpPixmap  *pixmap,
Sven Neumann's avatar
Sven Neumann committed
106
                 gchar      **xpm_data)
107 108
{
  g_return_if_fail (GIMP_IS_PIXMAP (pixmap));
109 110 111

  pixmap->xpm_data = xpm_data;

112 113 114
  GTK_WIDGET (pixmap)->requisition.width  = 0;
  GTK_WIDGET (pixmap)->requisition.height = 0;

115
  if (! gtk_widget_get_realized (GTK_WIDGET (pixmap)))
116
    {
117
      if (xpm_data)
Sven Neumann's avatar
Sven Neumann committed
118 119 120 121 122 123 124 125 126
        {
          gint width, height;

          if (sscanf (xpm_data[0], "%d %d", &width, &height) != 2)
            {
              g_warning ("%s: passed pointer is no XPM data", G_STRFUNC);
            }
          else
            {
127 128 129 130 131 132
              gint xpad, ypad;

              gtk_misc_get_padding (GTK_MISC (pixmap), &xpad, &ypad);

              GTK_WIDGET (pixmap)->requisition.width  = width + xpad * 2;
              GTK_WIDGET (pixmap)->requisition.height = height + ypad * 2;
Sven Neumann's avatar
Sven Neumann committed
133 134
            }
        }
135 136 137
    }
  else
    {
138
      gimp_pixmap_create_from_xpm_d (pixmap);
139
    }
140 141 142 143 144
}

static void
gimp_pixmap_realize (GtkWidget *widget)
{
145
  if (GTK_WIDGET_CLASS (parent_class)->realize)
146
    GTK_WIDGET_CLASS (parent_class)->realize (widget);
147 148 149 150 151 152 153

  gimp_pixmap_create_from_xpm_d (GIMP_PIXMAP (widget));
}

static void
gimp_pixmap_create_from_xpm_d (GimpPixmap *pixmap)
{
154
  GtkStyle   *style;
155 156
  GdkPixmap  *gdk_pixmap = NULL;
  GdkBitmap  *mask       = NULL;
157

158 159 160
  if (pixmap->xpm_data)
    {
      GtkWidget  *widget;
161

162
      widget = GTK_WIDGET (pixmap);
163

164
      style = gtk_widget_get_style (widget);
165

166
      gdk_pixmap = gdk_pixmap_create_from_xpm_d (gtk_widget_get_window (widget),
Sven Neumann's avatar
Sven Neumann committed
167 168 169
                                                 &mask,
                                                 &style->bg[GTK_STATE_NORMAL],
                                                 pixmap->xpm_data);
170
    }
171

172
  gtk_image_set_from_pixmap (GTK_IMAGE (pixmap), gdk_pixmap, mask);
173

174
  if (gdk_pixmap)
175
    g_object_unref (gdk_pixmap);
176 177

  if (mask)
178
    g_object_unref (mask);
179
}