Debian Codegraph @

libcolord-gtk/cd-sample-widget-gtk4.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2021 Georges Basile Stavracas Neto <[email protected]>
4 * 2012 Richard Hughes <[email protected]>
5 *
6 * Licensed under the GNU Lesser General Public License Version 2.1
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "config.h"
24
25 #include <gtk/gtk.h>
26 #include <colord.h>
27
28 #include "cd-sample-widget.h"
29
30 struct CdSampleWidgetPrivate
31 {
32 cairo_t *cr;
33 CdColorRGB color;
34 };
35
36 G_DEFINE_TYPE_WITH_CODE (CdSampleWidget, cd_sample_widget, GTK_TYPE_DRAWING_AREA,
37 G_ADD_PRIVATE (CdSampleWidget));
38
39 enum
40 {
41 PROP_0,
42 PROP_COLOR,
43 };
44
45 /**
46 * up_sample_get_property:
47 **/
48 static void
49 up_sample_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
50 {
51 CdSampleWidget *sample = CD_SAMPLE_WIDGET (object);
52 CdSampleWidgetPrivate *priv = cd_sample_widget_get_instance_private (sample);
53 switch (prop_id) {
54 case PROP_COLOR:
55 g_value_set_boxed (value, &priv->color);
56 break;
57 default:
58 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
59 break;
60 }
61 }
62
63 /**
64 * up_sample_set_property:
65 **/
66 static void
67 up_sample_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
68 {
69 CdColorRGB *tmp;
70 CdSampleWidget *sample = CD_SAMPLE_WIDGET (object);
71 CdSampleWidgetPrivate *priv = cd_sample_widget_get_instance_private (sample);
72
73 switch (prop_id) {
74 case PROP_COLOR:
75 tmp = g_value_get_boxed (value);
76 cd_color_rgb_copy (tmp, &priv->color);
77 break;
78 default:
79 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
80 break;
81 }
82
83 /* refresh widget */
84 gtk_widget_hide (GTK_WIDGET (sample));
85 gtk_widget_show (GTK_WIDGET (sample));
86 }
87
88 /**
89 * cd_sample_widget_draw_rounded_rectangle:
90 **/
91 static void
92 cd_sample_widget_draw_rounded_rectangle (cairo_t *cr,
93 gdouble x,
94 gdouble y,
95 gdouble width,
96 gdouble height,
97 gdouble corner_radius)
98 {
99 gdouble aspect = width / height;
100 gdouble radius = corner_radius / aspect;
101 gdouble degrees = G_PI / 180.0;
102
103 cairo_new_sub_path (cr);
104 cairo_arc (cr, x + width - radius, y + radius,
105 radius, -90 * degrees, 0 * degrees);
106 cairo_arc (cr, x + width - radius, y + height - radius,
107 radius, 0 * degrees, 90 * degrees);
108 cairo_arc (cr, x + radius, y + height - radius,
109 radius, 90 * degrees, 180 * degrees);
110 cairo_arc (cr, x + radius, y + radius,
111 radius, 180 * degrees, 270 * degrees);
112 cairo_close_path (cr);
113 }
114
115 /**
116 * cd_sample_widget_draw:
117 **/
118 static void
119 cd_sample_widget_draw (GtkDrawingArea *drawing_area,
120 cairo_t *cr,
121 int width,
122 int height,
123 gpointer user_data)
124 {
125 CdSampleWidget *sample = CD_SAMPLE_WIDGET (drawing_area);
126 CdSampleWidgetPrivate *priv = cd_sample_widget_get_instance_private (sample);
127 CdColorRGB *color;
128
129 color = &priv->color;
130 cairo_save (cr);
131 cairo_set_source_rgb (cr, color->R, color->G, color->B);
132 cd_sample_widget_draw_rounded_rectangle (cr, 0, 0, width, height, 10.5);
133 cairo_fill_preserve (cr);
134 cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 1.0);
135 cairo_set_line_width (cr, 1.0);
136 cairo_stroke (cr);
137 cairo_restore (cr);
138 }
139
140 /**
141 * cd_sample_widget_class_init:
142 **/
143 static void
144 cd_sample_widget_class_init (CdSampleWidgetClass *class)
145 {
146 GObjectClass *object_class = G_OBJECT_CLASS (class);
147
148 object_class->get_property = up_sample_get_property;
149 object_class->set_property = up_sample_set_property;
150
151 /* properties */
152 g_object_class_install_property (object_class,
153 PROP_COLOR,
154 g_param_spec_boxed ("color", NULL, NULL,
155 CD_TYPE_COLOR_RGB,
156 G_PARAM_READWRITE));
157 }
158
159 /**
160 * cd_sample_widget_init:
161 **/
162 static void
163 cd_sample_widget_init (CdSampleWidget *sample)
164 {
165 CdSampleWidgetPrivate *priv = cd_sample_widget_get_instance_private (sample);
166
167 gtk_widget_set_cursor (GTK_WIDGET (sample), gdk_cursor_new_from_name ("blank", NULL));
168 cd_color_rgb_set (&priv->color, 1.0, 1.0, 1.0);
169 gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (sample),
170 cd_sample_widget_draw,
171 sample, NULL);
172 }
173
174 /**
175 * cd_sample_widget_set_color:
176 * @sample: This class instance
177 * @color: A color
178 *
179 * Sets the color for the sample widget
180 *
181 * Since: 0.1.24
182 **/
183 void
184 cd_sample_widget_set_color (CdSampleWidget *sample, const CdColorRGB *color)
185 {
186 CdSampleWidgetPrivate *priv;
187
188 g_return_if_fail (color != NULL);
189 g_return_if_fail (CD_IS_SAMPLE_WIDGET (sample));
190
191 priv = cd_sample_widget_get_instance_private (sample);
192
193 /* set new color and refresh */
194 cd_color_rgb_copy (color, &priv->color);
195 gtk_widget_queue_draw (GTK_WIDGET (sample));
196 }
197
198 /**
199 * cd_sample_widget_new:
200 *
201 * Return value: A new #CdSampleWidget object.
202 *
203 * Since: 0.1.24
204 **/
205 GtkWidget *
206 cd_sample_widget_new (void)
207 {
208 return g_object_new (CD_TYPE_SAMPLE_WIDGET, NULL);
209 }