Code

Adds 'Save as SVG' button / dialog to clarify when saving file, that it
[inkscape.git] / src / widgets / sp-color-preview.cpp
1 #define __SP_COLOR_PREVIEW_C__
3 /*
4  * A simple color preview widget
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 "../display/nr-plain-stuff-gdk.h"
16 #include "sp-color-preview.h"
18 #define SPCP_DEFAULT_WIDTH 32
19 #define SPCP_DEFAULT_HEIGHT 11
21 static void sp_color_preview_class_init (SPColorPreviewClass *klass);
22 static void sp_color_preview_init (SPColorPreview *image);
23 static void sp_color_preview_destroy (GtkObject *object);
25 static void sp_color_preview_size_request (GtkWidget *widget, GtkRequisition *requisition);
26 static void sp_color_preview_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
27 static gint sp_color_preview_expose (GtkWidget *widget, GdkEventExpose *event);
29 static void sp_color_preview_paint (SPColorPreview *cp, GdkRectangle *area);
31 static GtkWidgetClass *parent_class;
33 GtkType
34 sp_color_preview_get_type (void)
35 {
36         static GtkType type = 0;
37         if (!type) {
38                 GtkTypeInfo info = {
39                         "SPColorPreview",
40                         sizeof (SPColorPreview),
41                         sizeof (SPColorPreviewClass),
42                         (GtkClassInitFunc) sp_color_preview_class_init,
43                         (GtkObjectInitFunc) sp_color_preview_init,
44                         NULL, NULL, NULL
45                 };
46                 type = gtk_type_unique (GTK_TYPE_WIDGET, &info);
47         }
48         return type;
49 }
51 static void
52 sp_color_preview_class_init (SPColorPreviewClass *klass)
53 {
54         GtkObjectClass *object_class;
55         GtkWidgetClass *widget_class;
57         object_class = (GtkObjectClass *) klass;
58         widget_class = (GtkWidgetClass *) klass;
60         parent_class = (GtkWidgetClass*)gtk_type_class (GTK_TYPE_WIDGET);
62         object_class->destroy = sp_color_preview_destroy;
64         widget_class->size_request = sp_color_preview_size_request;
65         widget_class->size_allocate = sp_color_preview_size_allocate;
66         widget_class->expose_event = sp_color_preview_expose;
67 }
69 static void
70 sp_color_preview_init (SPColorPreview *image)
71 {
72         GTK_WIDGET_SET_FLAGS (image, GTK_NO_WINDOW);
74         image->rgba = 0xffffffff;
75 }
77 static void
78 sp_color_preview_destroy (GtkObject *object)
79 {
80         SPColorPreview *image;
82         image = SP_COLOR_PREVIEW (object);
84         if (((GtkObjectClass *) (parent_class))->destroy)
85                 (* ((GtkObjectClass *) (parent_class))->destroy) (object);
86 }
88 static void
89 sp_color_preview_size_request (GtkWidget *widget, GtkRequisition *requisition)
90 {
91         SPColorPreview *slider;
93         slider = SP_COLOR_PREVIEW (widget);
95         requisition->width = SPCP_DEFAULT_WIDTH;
96         requisition->height = SPCP_DEFAULT_HEIGHT;
97 }
99 static void
100 sp_color_preview_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
102         SPColorPreview *image;
104         image = SP_COLOR_PREVIEW (widget);
106         widget->allocation = *allocation;
108         if (GTK_WIDGET_DRAWABLE (image)) {
109                 gtk_widget_queue_draw (GTK_WIDGET (image));
110         }
113 static gint
114 sp_color_preview_expose (GtkWidget *widget, GdkEventExpose *event)
116         SPColorPreview *cp;
118         cp = SP_COLOR_PREVIEW (widget);
120         if (GTK_WIDGET_DRAWABLE (widget)) {
121                 sp_color_preview_paint (cp, &event->area);
122         }
124         return TRUE;
127 GtkWidget *
128 sp_color_preview_new (guint32 rgba)
130         SPColorPreview *image;
132         image = (SPColorPreview*)gtk_type_new (SP_TYPE_COLOR_PREVIEW);
134         sp_color_preview_set_rgba32 (image, rgba);
136         return (GtkWidget *) image;
139 void
140 sp_color_preview_set_rgba32 (SPColorPreview *cp, guint32 rgba)
142         cp->rgba = rgba;
144         if (GTK_WIDGET_DRAWABLE (cp)) {
145                 gtk_widget_queue_draw (GTK_WIDGET (cp));
146         }
149 static void
150 sp_color_preview_paint (SPColorPreview *cp, GdkRectangle *area)
152         GtkWidget *widget;
153         GdkRectangle warea, carea;
154         GdkRectangle wpaint, cpaint;
155         gint w2;
157         widget = GTK_WIDGET (cp);
159         warea.x = widget->allocation.x;
160         warea.y = widget->allocation.y;
161         warea.width = widget->allocation.width;
162         warea.height = widget->allocation.height;
164         if (!gdk_rectangle_intersect (area, &warea, &wpaint)) return;
166         /* Transparent area */
168         w2 = warea.width / 2;
170         carea.x = warea.x;
171         carea.y = warea.y;
172         carea.width = w2;
173         carea.height = warea.height;
175         if (gdk_rectangle_intersect (area, &carea, &cpaint)) {
176                 nr_gdk_draw_rgba32_solid (widget->window, widget->style->black_gc,
177                                           cpaint.x, cpaint.y,
178                                           cpaint.width, cpaint.height,
179                                           cp->rgba);
180         }
182         /* Solid area */
184         carea.x = warea.x + w2;
185         carea.y = warea.y;
186         carea.width = warea.width - w2;
187         carea.height = warea.height;
189         if (gdk_rectangle_intersect (area, &carea, &cpaint)) {
190                 nr_gdk_draw_rgba32_solid (widget->window, widget->style->black_gc,
191                                           cpaint.x, cpaint.y,
192                                           cpaint.width, cpaint.height,
193                                           cp->rgba | 0xff);
194         }