gtkpixmap.c 7.67 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
4 5 6
 * Insensitive pixmap building code by Eckehard Berns from GNOME Stock
 * Copyright (C) 1997, 1998 Free Software Foundation
 *
Elliot Lee's avatar
Elliot Lee committed
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
9 10 11 12 13 14
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * 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
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18 19 20
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
21
 */
22 23

/*
24
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
25 26 27 28 29
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

Manish Singh's avatar
Manish Singh committed
30 31
#undef GDK_DISABLE_DEPRECATED

32
#include <math.h>
Elliot Lee's avatar
Elliot Lee committed
33 34 35 36 37 38 39 40
#include "gtkcontainer.h"
#include "gtkpixmap.h"


static void gtk_pixmap_class_init (GtkPixmapClass  *klass);
static void gtk_pixmap_init       (GtkPixmap       *pixmap);
static gint gtk_pixmap_expose     (GtkWidget       *widget,
				   GdkEventExpose  *event);
41
static void gtk_pixmap_finalize   (GObject         *object);
42
static void build_insensitive_pixmap (GtkPixmap *gtkpixmap);
Elliot Lee's avatar
Elliot Lee committed
43

44
static GtkWidgetClass *parent_class;
Elliot Lee's avatar
Elliot Lee committed
45

46
GtkType
47
gtk_pixmap_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
48
{
49
  static GtkType pixmap_type = 0;
Elliot Lee's avatar
Elliot Lee committed
50 51 52

  if (!pixmap_type)
    {
53
      static const GtkTypeInfo pixmap_info =
Elliot Lee's avatar
Elliot Lee committed
54 55 56 57 58 59
      {
	"GtkPixmap",
	sizeof (GtkPixmap),
	sizeof (GtkPixmapClass),
	(GtkClassInitFunc) gtk_pixmap_class_init,
	(GtkObjectInitFunc) gtk_pixmap_init,
60 61
	/* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
62
        (GtkClassInitFunc) NULL,
Elliot Lee's avatar
Elliot Lee committed
63 64
      };

65
      pixmap_type = gtk_type_unique (GTK_TYPE_MISC, &pixmap_info);
Elliot Lee's avatar
Elliot Lee committed
66 67 68 69 70 71 72 73
    }

  return pixmap_type;
}

static void
gtk_pixmap_class_init (GtkPixmapClass *class)
{
74
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
75
  GtkObjectClass *object_class;
Elliot Lee's avatar
Elliot Lee committed
76 77
  GtkWidgetClass *widget_class;

78
  object_class = (GtkObjectClass*) class;
Elliot Lee's avatar
Elliot Lee committed
79
  widget_class = (GtkWidgetClass*) class;
80
  parent_class = gtk_type_class (gtk_misc_get_type ());
Elliot Lee's avatar
Elliot Lee committed
81

82 83
  gobject_class->finalize = gtk_pixmap_finalize;

Elliot Lee's avatar
Elliot Lee committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  widget_class->expose_event = gtk_pixmap_expose;
}

static void
gtk_pixmap_init (GtkPixmap *pixmap)
{
  GTK_WIDGET_SET_FLAGS (pixmap, GTK_NO_WINDOW);

  pixmap->pixmap = NULL;
  pixmap->mask = NULL;
}

GtkWidget*
gtk_pixmap_new (GdkPixmap *val,
		GdkBitmap *mask)
{
  GtkPixmap *pixmap;
101
   
Elliot Lee's avatar
Elliot Lee committed
102
  g_return_val_if_fail (val != NULL, NULL);
103
  
Elliot Lee's avatar
Elliot Lee committed
104
  pixmap = gtk_type_new (gtk_pixmap_get_type ());
105
  
106
  pixmap->build_insensitive = TRUE;
Elliot Lee's avatar
Elliot Lee committed
107
  gtk_pixmap_set (pixmap, val, mask);
108
  
Elliot Lee's avatar
Elliot Lee committed
109 110 111
  return GTK_WIDGET (pixmap);
}

112
static void
113
gtk_pixmap_finalize (GObject *object)
114 115
{
  gtk_pixmap_set (GTK_PIXMAP (object), NULL, NULL);
116 117

  G_OBJECT_CLASS (parent_class)->finalize (object);
118 119
}

Elliot Lee's avatar
Elliot Lee committed
120 121 122 123 124 125 126
void
gtk_pixmap_set (GtkPixmap *pixmap,
		GdkPixmap *val,
		GdkBitmap *mask)
{
  gint width;
  gint height;
127 128
  gint oldwidth;
  gint oldheight;
Elliot Lee's avatar
Elliot Lee committed
129 130 131

  g_return_if_fail (GTK_IS_PIXMAP (pixmap));

132
  if (pixmap->pixmap != val)
Elliot Lee's avatar
Elliot Lee committed
133
    {
134 135
      oldwidth = GTK_WIDGET (pixmap)->requisition.width;
      oldheight = GTK_WIDGET (pixmap)->requisition.height;
136 137
      if (pixmap->pixmap)
	gdk_pixmap_unref (pixmap->pixmap);
138 139
      if (pixmap->pixmap_insensitive)
	gdk_pixmap_unref (pixmap->pixmap_insensitive);
140
      pixmap->pixmap = val;
141
      pixmap->pixmap_insensitive = NULL;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
      if (pixmap->pixmap)
	{
	  gdk_pixmap_ref (pixmap->pixmap);
	  gdk_window_get_size (pixmap->pixmap, &width, &height);
	  GTK_WIDGET (pixmap)->requisition.width =
	    width + GTK_MISC (pixmap)->xpad * 2;
	  GTK_WIDGET (pixmap)->requisition.height =
	    height + GTK_MISC (pixmap)->ypad * 2;
	}
      else
	{
	  GTK_WIDGET (pixmap)->requisition.width = 0;
	  GTK_WIDGET (pixmap)->requisition.height = 0;
	}
      if (GTK_WIDGET_VISIBLE (pixmap))
157 158 159 160 161
	{
	  if ((GTK_WIDGET (pixmap)->requisition.width != oldwidth) ||
	      (GTK_WIDGET (pixmap)->requisition.height != oldheight))
	    gtk_widget_queue_resize (GTK_WIDGET (pixmap));
	  else
162
	    gtk_widget_queue_clear (GTK_WIDGET (pixmap));
163
	}
Elliot Lee's avatar
Elliot Lee committed
164
    }
165 166

  if (pixmap->mask != mask)
Elliot Lee's avatar
Elliot Lee committed
167
    {
168 169 170 171 172
      if (pixmap->mask)
	gdk_bitmap_unref (pixmap->mask);
      pixmap->mask = mask;
      if (pixmap->mask)
	gdk_bitmap_ref (pixmap->mask);
Elliot Lee's avatar
Elliot Lee committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    }
}

void
gtk_pixmap_get (GtkPixmap  *pixmap,
		GdkPixmap **val,
		GdkBitmap **mask)
{
  g_return_if_fail (GTK_IS_PIXMAP (pixmap));

  if (val)
    *val = pixmap->pixmap;
  if (mask)
    *mask = pixmap->mask;
}

static gint
gtk_pixmap_expose (GtkWidget      *widget,
		   GdkEventExpose *event)
{
  GtkPixmap *pixmap;
  GtkMisc *misc;
  gint x, y;
196
  gfloat xalign;
Elliot Lee's avatar
Elliot Lee committed
197 198 199 200 201 202 203 204 205

  g_return_val_if_fail (GTK_IS_PIXMAP (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  if (GTK_WIDGET_DRAWABLE (widget))
    {
      pixmap = GTK_PIXMAP (widget);
      misc = GTK_MISC (widget);

206 207 208 209 210 211 212 213 214 215 216 217
      if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
	xalign = misc->xalign;
      else
	xalign = 1.0 - misc->xalign;
  
      x = floor (widget->allocation.x + misc->xpad
		 + ((widget->allocation.width - widget->requisition.width) * xalign)
		 + 0.5);
      y = floor (widget->allocation.y + misc->ypad 
		 + ((widget->allocation.height - widget->requisition.height) * misc->yalign)
		 + 0.5);
      
Elliot Lee's avatar
Elliot Lee committed
218 219 220 221 222 223
      if (pixmap->mask)
	{
	  gdk_gc_set_clip_mask (widget->style->black_gc, pixmap->mask);
	  gdk_gc_set_clip_origin (widget->style->black_gc, x, y);
	}

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
      if (GTK_WIDGET_STATE (widget) == GTK_STATE_INSENSITIVE
          && pixmap->build_insensitive)
        {
	  if (!pixmap->pixmap_insensitive)
	    build_insensitive_pixmap (pixmap);
          gdk_draw_pixmap (widget->window,
	   	           widget->style->black_gc,
		           pixmap->pixmap_insensitive,
		           0, 0, x, y, -1, -1);
        }
      else
	{
          gdk_draw_pixmap (widget->window,
	   	           widget->style->black_gc,
		           pixmap->pixmap,
		           0, 0, x, y, -1, -1);
	}
Elliot Lee's avatar
Elliot Lee committed
241 242 243 244 245 246 247 248 249

      if (pixmap->mask)
	{
	  gdk_gc_set_clip_mask (widget->style->black_gc, NULL);
	  gdk_gc_set_clip_origin (widget->style->black_gc, 0, 0);
	}
    }
  return FALSE;
}
250 251

void
252
gtk_pixmap_set_build_insensitive (GtkPixmap *pixmap, gboolean build)
253 254 255 256 257 258 259 260 261 262 263 264
{
  g_return_if_fail (GTK_IS_PIXMAP (pixmap));

  pixmap->build_insensitive = build;

  if (GTK_WIDGET_VISIBLE (pixmap))
    {
      gtk_widget_queue_clear (GTK_WIDGET (pixmap));
    }
}

static void
265
build_insensitive_pixmap (GtkPixmap *gtkpixmap)
266 267 268
{
  GdkPixmap *pixmap = gtkpixmap->pixmap;
  GdkPixmap *insensitive;
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  gint w, h;
  GdkPixbuf *pixbuf;
  GdkPixbuf *stated;
  
  gdk_window_get_size (pixmap, &w, &h);

  pixbuf = gdk_pixbuf_get_from_drawable (NULL,
                                         pixmap,
                                         gtk_widget_get_colormap (GTK_WIDGET(gtkpixmap)),
                                         0, 0,
                                         0, 0,
                                         w, h);
  
  stated = gdk_pixbuf_copy (pixbuf);
  
  gdk_pixbuf_saturate_and_pixelate (pixbuf, stated,
                                    0.8, TRUE);
286

287 288 289 290
  g_object_unref (G_OBJECT (pixbuf));
  pixbuf = NULL;
  
  insensitive = gdk_pixmap_new (GTK_WIDGET (gtkpixmap)->window, w, h, -1);
291

292 293 294 295 296 297 298 299
  gdk_draw_pixbuf (insensitive,
		   GTK_WIDGET (gtkpixmap)->style->white_gc,
		   stated,
		   0, 0,
		   0, 0,
		   w, h,
		   GDK_RGB_DITHER_NORMAL,
		   0, 0);
300 301 302

  gtkpixmap->pixmap_insensitive = insensitive;

303
  g_object_unref (G_OBJECT (stated));
304 305 306
}