1 #define __SP_GRADIENT_IMAGE_C__
3 /*
4 * A simple gradient preview
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 2001-2002 Lauris Kaplinski
10 * Copyright (C) 2001 Ximian, Inc.
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #include <libnr/nr-pixblock-pattern.h>
16 #include "macros.h"
17 #include "../display/nr-plain-stuff.h"
18 #include "../display/nr-plain-stuff-gdk.h"
19 #include "gradient-image.h"
20 #include "sp-gradient.h"
21 #include "sp-gradient-fns.h"
23 #include <sigc++/functors/ptr_fun.h>
24 #include <sigc++/adaptors/bind.h>
26 #define VBLOCK 16
28 static void sp_gradient_image_class_init (SPGradientImageClass *klass);
29 static void sp_gradient_image_init (SPGradientImage *image);
30 static void sp_gradient_image_destroy (GtkObject *object);
32 static void sp_gradient_image_realize (GtkWidget *widget);
33 static void sp_gradient_image_unrealize (GtkWidget *widget);
34 static void sp_gradient_image_size_request (GtkWidget *widget, GtkRequisition *requisition);
35 static void sp_gradient_image_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
36 static gint sp_gradient_image_expose (GtkWidget *widget, GdkEventExpose *event);
38 static void sp_gradient_image_gradient_release (SPObject *, SPGradientImage *im);
39 static void sp_gradient_image_gradient_modified (SPObject *, guint flags, SPGradientImage *im);
40 static void sp_gradient_image_update (SPGradientImage *img);
42 static GtkWidgetClass *parent_class;
44 GtkType
45 sp_gradient_image_get_type (void)
46 {
47 static GtkType type = 0;
48 if (!type) {
49 GtkTypeInfo info = {
50 "SPGradientImage",
51 sizeof (SPGradientImage),
52 sizeof (SPGradientImageClass),
53 (GtkClassInitFunc) sp_gradient_image_class_init,
54 (GtkObjectInitFunc) sp_gradient_image_init,
55 NULL, NULL, NULL
56 };
57 type = gtk_type_unique (GTK_TYPE_WIDGET, &info);
58 }
59 return type;
60 }
62 static void
63 sp_gradient_image_class_init (SPGradientImageClass *klass)
64 {
65 GtkObjectClass *object_class;
66 GtkWidgetClass *widget_class;
68 object_class = (GtkObjectClass *) klass;
69 widget_class = (GtkWidgetClass *) klass;
71 parent_class = (GtkWidgetClass*)gtk_type_class (GTK_TYPE_WIDGET);
73 object_class->destroy = sp_gradient_image_destroy;
75 widget_class->realize = sp_gradient_image_realize;
76 widget_class->unrealize = sp_gradient_image_unrealize;
77 widget_class->size_request = sp_gradient_image_size_request;
78 widget_class->size_allocate = sp_gradient_image_size_allocate;
79 widget_class->expose_event = sp_gradient_image_expose;
80 }
82 static void
83 sp_gradient_image_init (SPGradientImage *image)
84 {
85 GTK_WIDGET_SET_FLAGS (image, GTK_NO_WINDOW);
87 image->gradient = NULL;
88 image->px = NULL;
90 new (&image->release_connection) sigc::connection();
91 new (&image->modified_connection) sigc::connection();
92 }
94 static void
95 sp_gradient_image_destroy (GtkObject *object)
96 {
97 SPGradientImage *image;
99 image = SP_GRADIENT_IMAGE (object);
101 if (image->gradient) {
102 image->release_connection.disconnect();
103 image->modified_connection.disconnect();
104 image->gradient = NULL;
105 }
107 image->release_connection.~connection();
108 image->modified_connection.~connection();
110 if (((GtkObjectClass *) (parent_class))->destroy)
111 (* ((GtkObjectClass *) (parent_class))->destroy) (object);
112 }
114 static void
115 sp_gradient_image_realize (GtkWidget *widget)
116 {
117 SPGradientImage *image;
119 image = SP_GRADIENT_IMAGE (widget);
121 if (((GtkWidgetClass *) parent_class)->realize)
122 (* ((GtkWidgetClass *) parent_class)->realize) (widget);
124 g_assert (!image->px);
125 image->px = g_new (guchar, 3 * VBLOCK * widget->allocation.width);
126 sp_gradient_image_update (image);
127 }
129 static void
130 sp_gradient_image_unrealize (GtkWidget *widget)
131 {
132 SPGradientImage *image;
134 image = SP_GRADIENT_IMAGE (widget);
136 if (((GtkWidgetClass *) parent_class)->unrealize)
137 (* ((GtkWidgetClass *) parent_class)->unrealize) (widget);
139 g_assert (image->px);
140 g_free (image->px);
141 image->px = NULL;
142 }
144 static void
145 sp_gradient_image_size_request (GtkWidget *widget, GtkRequisition *requisition)
146 {
147 SPGradientImage *slider;
149 slider = SP_GRADIENT_IMAGE (widget);
151 requisition->width = 64;
152 requisition->height = 12;
153 }
155 static void
156 sp_gradient_image_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
157 {
158 SPGradientImage *image;
160 image = SP_GRADIENT_IMAGE (widget);
162 widget->allocation = *allocation;
164 if (GTK_WIDGET_REALIZED (widget)) {
165 g_free (image->px);
166 image->px = g_new (guchar, 3 * VBLOCK * allocation->width);
167 }
169 sp_gradient_image_update (image);
170 }
172 static gint
173 sp_gradient_image_expose (GtkWidget *widget, GdkEventExpose *event)
174 {
175 SPGradientImage *image;
177 image = SP_GRADIENT_IMAGE (widget);
179 if (GTK_WIDGET_DRAWABLE (widget)) {
180 gint x0, y0, x1, y1;
181 x0 = MAX (event->area.x, widget->allocation.x);
182 y0 = MAX (event->area.y, widget->allocation.y);
183 x1 = MIN (event->area.x + event->area.width, widget->allocation.x + widget->allocation.width);
184 y1 = MIN (event->area.y + event->area.height, widget->allocation.y + widget->allocation.height);
185 if ((x1 > x0) && (y1 > y0)) {
186 if (image->px) {
187 if (image->gradient) {
188 gint y;
189 guchar *p;
190 p = image->px + 3 * (x0 - widget->allocation.x);
191 for (y = y0; y < y1; y += VBLOCK) {
192 gdk_draw_rgb_image (widget->window, widget->style->black_gc,
193 x0, y,
194 (x1 - x0), MIN (VBLOCK, y1 - y),
195 GDK_RGB_DITHER_MAX,
196 p, widget->allocation.width * 3);
197 }
198 } else {
199 nr_gdk_draw_gray_garbage (widget->window, widget->style->black_gc,
200 x0, y0,
201 x1 - x0, y1 - y0);
202 }
203 } else {
204 gdk_draw_rectangle (widget->window, widget->style->black_gc,
205 x0, y0,
206 (x1 - x0), (y1 - x0),
207 TRUE);
208 }
209 }
210 }
212 return TRUE;
213 }
215 GtkWidget *
216 sp_gradient_image_new (SPGradient *gradient)
217 {
218 SPGradientImage *image;
220 image = (SPGradientImage*)gtk_type_new (SP_TYPE_GRADIENT_IMAGE);
222 sp_gradient_image_set_gradient (image, gradient);
224 return (GtkWidget *) image;
225 }
227 void
228 sp_gradient_image_set_gradient (SPGradientImage *image, SPGradient *gradient)
229 {
230 if (image->gradient) {
231 image->release_connection.disconnect();
232 image->modified_connection.disconnect();
233 }
235 image->gradient = gradient;
237 if (gradient) {
238 image->release_connection = gradient->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_gradient_image_gradient_release), image));
239 image->modified_connection = gradient->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_gradient_image_gradient_modified), image));
240 }
242 sp_gradient_image_update (image);
243 }
245 static void
246 sp_gradient_image_gradient_release (SPObject *, SPGradientImage *image)
247 {
248 if (image->gradient) {
249 image->release_connection.disconnect();
250 image->modified_connection.disconnect();
251 }
253 image->gradient = NULL;
255 sp_gradient_image_update (image);
256 }
258 static void
259 sp_gradient_image_gradient_modified (SPObject *, guint flags, SPGradientImage *image)
260 {
261 sp_gradient_image_update (image);
262 }
264 static void
265 sp_gradient_image_update (SPGradientImage *image)
266 {
267 GtkAllocation *allocation;
269 if (!image->px) return;
271 allocation = &((GtkWidget *) image)->allocation;
273 if (image->gradient) {
274 nr_render_checkerboard_rgb (image->px, allocation->width, VBLOCK, 3 * allocation->width, 0, 0);
275 sp_gradient_render_vector_block_rgb (image->gradient,
276 image->px, allocation->width, VBLOCK, 3 * allocation->width,
277 0, allocation->width, TRUE);
278 } else {
279 NRPixBlock pb;
280 nr_pixblock_setup_extern (&pb, NR_PIXBLOCK_MODE_R8G8B8,
281 0, 0, allocation->width, VBLOCK,
282 image->px, 3 * allocation->width, TRUE, FALSE);
283 nr_pixblock_render_gray_noise (&pb, NULL);
284 nr_pixblock_release (&pb);
285 }
287 if (GTK_WIDGET_DRAWABLE (image)) {
288 gtk_widget_queue_draw (GTK_WIDGET (image));
289 }
290 }