1 /*
2 * A simple color preview widget
3 *
4 * Author:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 *
7 * Copyright (C) 2001-2002 Lauris Kaplinski
8 * Copyright (C) 2001 Ximian, Inc.
9 *
10 * Released under GNU GPL, read the file 'COPYING' for more information
11 */
13 #include "../display/nr-plain-stuff-gdk.h"
14 #include "sp-color-preview.h"
16 #define SPCP_DEFAULT_WIDTH 32
17 #define SPCP_DEFAULT_HEIGHT 11
19 static void sp_color_preview_class_init (SPColorPreviewClass *klass);
20 static void sp_color_preview_init (SPColorPreview *image);
21 static void sp_color_preview_destroy (GtkObject *object);
23 static void sp_color_preview_size_request (GtkWidget *widget, GtkRequisition *requisition);
24 static void sp_color_preview_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
25 static gint sp_color_preview_expose (GtkWidget *widget, GdkEventExpose *event);
27 static void sp_color_preview_paint (SPColorPreview *cp, GdkRectangle *area);
29 static GtkWidgetClass *parent_class;
31 GType sp_color_preview_get_type(void)
32 {
33 static GType type = 0;
34 if (!type) {
35 static const GTypeInfo info = {
36 sizeof(SPColorPreviewClass),
37 NULL, /* base_init */
38 NULL, /* base_finalize */
39 (GClassInitFunc) sp_color_preview_class_init,
40 NULL, /* class_finalize */
41 NULL, /* class_data */
42 sizeof(SPColorPreview),
43 0, /* n_preallocs */
44 (GInstanceInitFunc) sp_color_preview_init,
45 0, /* value_table */
46 };
48 type = g_type_register_static( GTK_TYPE_WIDGET,
49 "SPColorPreview",
50 &info,
51 static_cast< GTypeFlags > (0) );
52 }
53 return type;
54 }
56 static void
57 sp_color_preview_class_init (SPColorPreviewClass *klass)
58 {
59 GtkObjectClass *object_class;
60 GtkWidgetClass *widget_class;
62 object_class = (GtkObjectClass *) klass;
63 widget_class = (GtkWidgetClass *) klass;
65 parent_class = (GtkWidgetClass*)gtk_type_class (GTK_TYPE_WIDGET);
67 object_class->destroy = sp_color_preview_destroy;
69 widget_class->size_request = sp_color_preview_size_request;
70 widget_class->size_allocate = sp_color_preview_size_allocate;
71 widget_class->expose_event = sp_color_preview_expose;
72 }
74 static void
75 sp_color_preview_init (SPColorPreview *image)
76 {
77 GTK_WIDGET_SET_FLAGS (image, GTK_NO_WINDOW);
79 image->rgba = 0xffffffff;
80 }
82 static void
83 sp_color_preview_destroy (GtkObject *object)
84 {
85 SPColorPreview *image;
87 image = SP_COLOR_PREVIEW (object);
89 if (((GtkObjectClass *) (parent_class))->destroy)
90 (* ((GtkObjectClass *) (parent_class))->destroy) (object);
91 }
93 static void
94 sp_color_preview_size_request (GtkWidget *widget, GtkRequisition *requisition)
95 {
96 SPColorPreview *slider;
98 slider = SP_COLOR_PREVIEW (widget);
100 requisition->width = SPCP_DEFAULT_WIDTH;
101 requisition->height = SPCP_DEFAULT_HEIGHT;
102 }
104 static void
105 sp_color_preview_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
106 {
107 SPColorPreview *image;
109 image = SP_COLOR_PREVIEW (widget);
111 widget->allocation = *allocation;
113 if (GTK_WIDGET_DRAWABLE (image)) {
114 gtk_widget_queue_draw (GTK_WIDGET (image));
115 }
116 }
118 static gint
119 sp_color_preview_expose (GtkWidget *widget, GdkEventExpose *event)
120 {
121 SPColorPreview *cp;
123 cp = SP_COLOR_PREVIEW (widget);
125 if (GTK_WIDGET_DRAWABLE (widget)) {
126 sp_color_preview_paint (cp, &event->area);
127 }
129 return TRUE;
130 }
132 GtkWidget *
133 sp_color_preview_new (guint32 rgba)
134 {
135 SPColorPreview *image;
137 image = (SPColorPreview*)gtk_type_new (SP_TYPE_COLOR_PREVIEW);
139 sp_color_preview_set_rgba32 (image, rgba);
141 return (GtkWidget *) image;
142 }
144 void
145 sp_color_preview_set_rgba32 (SPColorPreview *cp, guint32 rgba)
146 {
147 cp->rgba = rgba;
149 if (GTK_WIDGET_DRAWABLE (cp)) {
150 gtk_widget_queue_draw (GTK_WIDGET (cp));
151 }
152 }
154 static void
155 sp_color_preview_paint (SPColorPreview *cp, GdkRectangle *area)
156 {
157 GtkWidget *widget;
158 GdkRectangle warea, carea;
159 GdkRectangle wpaint, cpaint;
160 gint w2;
162 widget = GTK_WIDGET (cp);
164 warea.x = widget->allocation.x;
165 warea.y = widget->allocation.y;
166 warea.width = widget->allocation.width;
167 warea.height = widget->allocation.height;
169 if (!gdk_rectangle_intersect (area, &warea, &wpaint)) return;
171 /* Transparent area */
173 w2 = warea.width / 2;
175 carea.x = warea.x;
176 carea.y = warea.y;
177 carea.width = w2;
178 carea.height = warea.height;
180 if (gdk_rectangle_intersect (area, &carea, &cpaint)) {
181 nr_gdk_draw_rgba32_solid (widget->window, widget->style->black_gc,
182 cpaint.x, cpaint.y,
183 cpaint.width, cpaint.height,
184 cp->rgba);
185 }
187 /* Solid area */
189 carea.x = warea.x + w2;
190 carea.y = warea.y;
191 carea.width = warea.width - w2;
192 carea.height = warea.height;
194 if (gdk_rectangle_intersect (area, &carea, &cpaint)) {
195 nr_gdk_draw_rgba32_solid (widget->window, widget->style->black_gc,
196 cpaint.x, cpaint.y,
197 cpaint.width, cpaint.height,
198 cp->rgba | 0xff);
199 }
200 }
202 /*
203 Local Variables:
204 mode:c++
205 c-file-style:"stroustrup"
206 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
207 indent-tabs-mode:nil
208 fill-column:99
209 End:
210 */
211 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :