Code

Use subdirectories with icon sizes.
[inkscape.git] / src / widgets / gradient-image.cpp
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     //TODO: switch to GObject
48     // GtkType and such calls were deprecated a while back with the
49     // introduction of GObject as a separate layer, with GType instead. --JonCruz
51         static GtkType type = 0;
52         if (!type) {
53                 GtkTypeInfo info = {
54                         (gchar*) "SPGradientImage",
55                         sizeof (SPGradientImage),
56                         sizeof (SPGradientImageClass),
57                         (GtkClassInitFunc) sp_gradient_image_class_init,
58                         (GtkObjectInitFunc) sp_gradient_image_init,
59                         NULL, NULL, NULL
60                 };
61                 type = gtk_type_unique (GTK_TYPE_WIDGET, &info);
62         }
63         return type;
64 }
66 static void
67 sp_gradient_image_class_init (SPGradientImageClass *klass)
68 {
69         GtkObjectClass *object_class;
70         GtkWidgetClass *widget_class;
72         object_class = (GtkObjectClass *) klass;
73         widget_class = (GtkWidgetClass *) klass;
75         parent_class = (GtkWidgetClass*)gtk_type_class (GTK_TYPE_WIDGET);
77         object_class->destroy = sp_gradient_image_destroy;
79         widget_class->realize = sp_gradient_image_realize;
80         widget_class->unrealize = sp_gradient_image_unrealize;
81         widget_class->size_request = sp_gradient_image_size_request;
82         widget_class->size_allocate = sp_gradient_image_size_allocate;
83         widget_class->expose_event = sp_gradient_image_expose;
84 }
86 static void
87 sp_gradient_image_init (SPGradientImage *image)
88 {
89         GTK_WIDGET_SET_FLAGS (image, GTK_NO_WINDOW);
91         image->gradient = NULL;
92         image->px = NULL;
94         new (&image->release_connection) sigc::connection();
95         new (&image->modified_connection) sigc::connection();
96 }
98 static void
99 sp_gradient_image_destroy (GtkObject *object)
101         SPGradientImage *image;
103         image = SP_GRADIENT_IMAGE (object);
105         if (image->gradient) {
106                 image->release_connection.disconnect();
107                 image->modified_connection.disconnect();
108                 image->gradient = NULL;
109         }
111         image->release_connection.~connection();
112         image->modified_connection.~connection();
114         if (((GtkObjectClass *) (parent_class))->destroy)
115                 (* ((GtkObjectClass *) (parent_class))->destroy) (object);
118 static void
119 sp_gradient_image_realize (GtkWidget *widget)
121         SPGradientImage *image;
123         image = SP_GRADIENT_IMAGE (widget);
125         if (((GtkWidgetClass *) parent_class)->realize)
126                 (* ((GtkWidgetClass *) parent_class)->realize) (widget);
128         g_assert (!image->px);
129         image->px = g_new (guchar, 3 * VBLOCK * widget->allocation.width);
130         sp_gradient_image_update (image);
133 static void
134 sp_gradient_image_unrealize (GtkWidget *widget)
136         SPGradientImage *image;
138         image = SP_GRADIENT_IMAGE (widget);
140         if (((GtkWidgetClass *) parent_class)->unrealize)
141                 (* ((GtkWidgetClass *) parent_class)->unrealize) (widget);
143         g_assert (image->px);
144         g_free (image->px);
145         image->px = NULL;
148 static void
149 sp_gradient_image_size_request (GtkWidget *widget, GtkRequisition *requisition)
151         SPGradientImage *slider;
153         slider = SP_GRADIENT_IMAGE (widget);
155         requisition->width = 64;
156         requisition->height = 12;
159 static void
160 sp_gradient_image_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
162         SPGradientImage *image;
164         image = SP_GRADIENT_IMAGE (widget);
166         widget->allocation = *allocation;
168         if (GTK_WIDGET_REALIZED (widget)) {
169                 g_free (image->px);
170                 image->px = g_new (guchar, 3 * VBLOCK * allocation->width);
171         }
173         sp_gradient_image_update (image);
176 static gint
177 sp_gradient_image_expose (GtkWidget *widget, GdkEventExpose *event)
179         SPGradientImage *image;
181         image = SP_GRADIENT_IMAGE (widget);
183         if (GTK_WIDGET_DRAWABLE (widget)) {
184                 gint x0, y0, x1, y1;
185                 x0 = MAX (event->area.x, widget->allocation.x);
186                 y0 = MAX (event->area.y, widget->allocation.y);
187                 x1 = MIN (event->area.x + event->area.width, widget->allocation.x + widget->allocation.width);
188                 y1 = MIN (event->area.y + event->area.height, widget->allocation.y + widget->allocation.height);
189                 if ((x1 > x0) && (y1 > y0)) {
190                         if (image->px) {
191                                 if (image->gradient) {
192                                         gint y;
193                                         guchar *p;
194                                         p = image->px + 3 * (x0 - widget->allocation.x);
195                                         for (y = y0; y < y1; y += VBLOCK) {
196                                                 gdk_draw_rgb_image (widget->window, widget->style->black_gc,
197                                                                     x0, y,
198                                                                     (x1 - x0), MIN (VBLOCK, y1 - y),
199                                                                     GDK_RGB_DITHER_MAX,
200                                                                     p, widget->allocation.width * 3);
201                                         }
202                                 } else {
203                                         nr_gdk_draw_gray_garbage (widget->window, widget->style->black_gc,
204                                                                   x0, y0,
205                                                                   x1 - x0, y1 - y0);
206                                 }
207                         } else {
208                                 gdk_draw_rectangle (widget->window, widget->style->black_gc,
209                                                     x0, y0,
210                                                     (x1 - x0), (y1 - x0),
211                                                     TRUE);
212                         }
213                 }
214         }
216         return TRUE;
219 GtkWidget *
220 sp_gradient_image_new (SPGradient *gradient)
222         SPGradientImage *image;
224         image = (SPGradientImage*)gtk_type_new (SP_TYPE_GRADIENT_IMAGE);
226         sp_gradient_image_set_gradient (image, gradient);
228         return (GtkWidget *) image;
231 void
232 sp_gradient_image_set_gradient (SPGradientImage *image, SPGradient *gradient)
234         if (image->gradient) {
235                 image->release_connection.disconnect();
236                 image->modified_connection.disconnect();
237         }
239         image->gradient = gradient;
241         if (gradient) {
242                 image->release_connection = gradient->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_gradient_image_gradient_release), image));
243                 image->modified_connection = gradient->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_gradient_image_gradient_modified), image));
244         }
246         sp_gradient_image_update (image);
249 static void
250 sp_gradient_image_gradient_release (SPObject *, SPGradientImage *image)
252         if (image->gradient) {
253                 image->release_connection.disconnect();
254                 image->modified_connection.disconnect();
255         }
257         image->gradient = NULL;
259         sp_gradient_image_update (image);
262 static void
263 sp_gradient_image_gradient_modified (SPObject *, guint /*flags*/, SPGradientImage *image)
265         sp_gradient_image_update (image);
268 static void
269 sp_gradient_image_update (SPGradientImage *image)
271         GtkAllocation *allocation;
273         if (!image->px) return;
275         allocation = &((GtkWidget *) image)->allocation;
277         if (image->gradient) {
278                 nr_render_checkerboard_rgb (image->px, allocation->width, VBLOCK, 3 * allocation->width, 0, 0);
279                 sp_gradient_render_vector_block_rgb (image->gradient,
280                                                      image->px, allocation->width, VBLOCK, 3 * allocation->width,
281                                                      0, allocation->width, TRUE);
282         } else {
283                 NRPixBlock pb;
284                 nr_pixblock_setup_extern (&pb, NR_PIXBLOCK_MODE_R8G8B8,
285                                           0, 0, allocation->width, VBLOCK,
286                                           image->px, 3 * allocation->width, TRUE, FALSE);
287                 nr_pixblock_render_gray_noise (&pb, NULL);
288                 nr_pixblock_release (&pb);
289         }
291         if (GTK_WIDGET_DRAWABLE (image)) {
292                 gtk_widget_queue_draw (GTK_WIDGET (image));
293         }