Debian Codegraph @

client/gtk3/iconwidget.c

raw
1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /* vim:set et sts=4: */
3 /* ibus - The Input Bus
4 * Copyright (C) 2025 Takao Fujiwara <[email protected]>
5 * Copyright (C) 2025 Red Hat, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 * USA
21 */
22
23 #define GDK_DISABLE_DEPRECATION_WARNINGS
24 #include <gtk/gtk.h>
25 #undef GDK_DISABLE_DEPRECATION_WARNINGS
26
27 #include "iconwidget.h"
28
29 #define IBUS_THEMED_RGBA_GET_PRIVATE(o) \
30 ((IBusThemedRGBAPrivate *)ibus_themed_rgba_get_instance_private (o))
31
32 enum {
33 PROP_0 = 0,
34 PROP_STYLE_CONTEXT,
35 PROP_STYLE,
36 };
37
38 typedef struct _IBusThemedRGBAPrivate IBusThemedRGBAPrivate;
39 struct _IBusThemedRGBAPrivate {
40 #if GTK_CHECK_VERSION (2, 91, 0)
41 GtkStyleContext *style_context;
42 #else
43 GtkStyle *style;
44 #endif
45 };
46
47 static void ibus_themed_rgba_set_property (IBusThemedRGBA *rgba,
48 guint prop_id,
49 const GValue *value,
50 GParamSpec *pspec);
51 static void ibus_themed_rgba_get_property (IBusThemedRGBA *rgba,
52 guint prop_id,
53 GValue *value,
54 GParamSpec *pspec);
55 static void ibus_themed_rgba_destroy (IBusObject *object);
56
57 G_DEFINE_TYPE_WITH_PRIVATE (IBusThemedRGBA, ibus_themed_rgba, IBUS_TYPE_OBJECT)
58
59
60 static void
61 ibus_themed_rgba_class_init (IBusThemedRGBAClass *class)
62 {
63 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
64 IBusObjectClass *ibus_object_class = IBUS_OBJECT_CLASS (class);
65
66 gobject_class->set_property =
67 (GObjectSetPropertyFunc)ibus_themed_rgba_set_property;
68 gobject_class->get_property =
69 (GObjectGetPropertyFunc)ibus_themed_rgba_get_property;
70 ibus_object_class->destroy = ibus_themed_rgba_destroy;
71
72 #if GTK_CHECK_VERSION (2, 91, 0)
73 /**
74 * IBusThemedRGBA:style-context:
75 *
76 * The GtkStyleContext of property
77 */
78 g_object_class_install_property (gobject_class,
79 PROP_STYLE_CONTEXT,
80 g_param_spec_object ("style-context",
81 "style context",
82 "The style context of property",
83 GTK_TYPE_STYLE_CONTEXT,
84 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
85 #else
86 /**
87 * IBusThemedRGBA:style:
88 *
89 * The GtkStyle of property
90 */
91 g_object_class_install_property (gobject_class,
92 PROP_STYLE,
93 g_param_spec_pointer ("style",
94 "style",
95 "The style of property",
96 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
97 #endif
98 }
99
100
101 static void
102 ibus_themed_rgba_init (IBusThemedRGBA *rgba)
103 {
104 }
105
106
107 #if GTK_CHECK_VERSION (2, 91, 0)
108 static IBusRGBA *
109 ibus_rgba_new_with_gdk_rgba (GdkRGBA *gdk_rgba)
110 {
111 IBusRGBA *ibus_rgba;
112
113 g_return_val_if_fail (gdk_rgba, NULL);
114 ibus_rgba = g_slice_new (IBusRGBA);
115 ibus_rgba->red = gdk_rgba->red;
116 ibus_rgba->green = gdk_rgba->green;
117 ibus_rgba->blue = gdk_rgba->blue;
118 ibus_rgba->alpha = gdk_rgba->alpha;
119 return ibus_rgba;
120 }
121
122
123 static IBusRGBA *
124 ibus_rgba_new_with_style_context (GtkStyleContext *style_context,
125 const gchar *theme_name,
126 const gchar *regacy_theme_name,
127 const gchar *fallback_rgba)
128 {
129 GdkRGBA color = { 0, };
130 if (gtk_style_context_lookup_color (style_context, theme_name, &color)) {
131 ;
132 } else if (gtk_style_context_lookup_color (style_context,
133 regacy_theme_name,
134 &color)) {
135 ;
136 } else if (fallback_rgba) {
137 gdk_rgba_parse (&color, fallback_rgba);
138 }
139 return ibus_rgba_new_with_gdk_rgba (&color);
140 }
141
142 #else
143
144 static IBusRGBA *
145 ibus_rgba_new_with_gdk_color (GdkColor *gdk_color)
146 {
147 IBusRGBA *ibus_rgba;
148
149 g_return_val_if_fail (gdk_color, NULL);
150 ibus_rgba = g_slice_new (IBusRGBA);
151 ibus_rgba->red = (float)gdk_color->red / 0xffff;
152 ibus_rgba->green = (float)gdk_color->green / 0xffff;
153 ibus_rgba->blue = (float)gdk_color->blue / 0xffff;
154 ibus_rgba->alpha = 1.; /* (float)gdk_color->pixel / 0xffff */
155 return ibus_rgba;
156 }
157 #endif
158
159
160 static void
161 ibus_themed_rgba_set_property (IBusThemedRGBA *rgba,
162 guint prop_id,
163 const GValue *value,
164 GParamSpec *pspec)
165 {
166 IBusThemedRGBAPrivate *priv;
167
168 g_return_if_fail (IBUS_THEMED_RGBA (rgba));
169 priv = IBUS_THEMED_RGBA_GET_PRIVATE (rgba);
170 switch (prop_id) {
171 #if GTK_CHECK_VERSION (2, 91, 0)
172 case PROP_STYLE_CONTEXT:
173 if (priv->style_context)
174 g_object_unref (priv->style_context);
175 priv->style_context = g_value_get_object (value);
176 if (priv->style_context) {
177 g_object_ref (priv->style_context);
178 ibus_themed_rgba_get_colors (rgba);
179 }
180 break;
181 #else
182 case PROP_STYLE:
183 priv->style = g_value_get_pointer (value);
184 if (priv->style)
185 ibus_themed_rgba_get_colors (rgba);
186 break;
187 #endif
188 default:
189 G_OBJECT_WARN_INVALID_PROPERTY_ID (rgba, prop_id, pspec);
190 }
191 }
192
193
194 static void
195 ibus_themed_rgba_get_property (IBusThemedRGBA *rgba,
196 guint prop_id,
197 GValue *value,
198 GParamSpec *pspec)
199 {
200 IBusThemedRGBAPrivate *priv;
201
202 g_return_if_fail (IBUS_THEMED_RGBA (rgba));
203 priv = IBUS_THEMED_RGBA_GET_PRIVATE (rgba);
204 switch (prop_id) {
205 #if GTK_CHECK_VERSION (2, 91, 0)
206 case PROP_STYLE_CONTEXT:
207 if (priv->style_context) {
208 g_value_set_object (value,
209 g_object_ref (priv->style_context));
210 }
211 break;
212 #else
213 case PROP_STYLE:
214 if (priv->style)
215 g_value_set_pointer (value, priv->style);
216 break;
217 #endif
218 default:
219 G_OBJECT_WARN_INVALID_PROPERTY_ID (rgba, prop_id, pspec);
220 }
221 }
222
223
224 static void
225 ibus_themed_rgba_destroy (IBusObject *object)
226 {
227 IBusThemedRGBA *rgba = (IBusThemedRGBA *)object;
228 IBusThemedRGBAPrivate *priv;
229
230 g_return_if_fail (IBUS_IS_THEMED_RGBA (rgba));
231 priv = IBUS_THEMED_RGBA_GET_PRIVATE (rgba);
232 #if GTK_CHECK_VERSION (2, 91, 0)
233 g_clear_object (&priv->style_context);
234 #else
235 priv->style = NULL;
236 #endif
237
238 if (rgba->normal_fg) {
239 g_slice_free (IBusRGBA, rgba->normal_fg);
240 rgba->normal_fg = NULL;
241 }
242 if (rgba->normal_bg) {
243 g_slice_free (IBusRGBA, rgba->normal_bg);
244 rgba->normal_bg = NULL;
245 }
246 if (rgba->selected_fg) {
247 g_slice_free (IBusRGBA, rgba->selected_fg);
248 rgba->selected_fg = NULL;
249 }
250 if (rgba->selected_bg) {
251 g_slice_free (IBusRGBA, rgba->selected_bg);
252 rgba->selected_bg = NULL;
253 }
254 }
255
256
257 IBusThemedRGBA *
258 #if GTK_CHECK_VERSION (2, 91, 0)
259 ibus_themed_rgba_new (GtkStyleContext *style_context)
260 #else
261 ibus_themed_rgba_new (GtkStyle *style)
262 #endif
263 {
264 return (IBusThemedRGBA *)g_object_new (IBUS_TYPE_THEMED_RGBA,
265 #if GTK_CHECK_VERSION (2, 91, 0)
266 "style-context",
267 style_context,
268 #else
269 "style",
270 style,
271 #endif
272 NULL);
273 }
274
275
276 void
277 ibus_themed_rgba_get_colors (IBusThemedRGBA *rgba)
278 {
279 IBusThemedRGBAPrivate *priv;
280
281 g_return_if_fail (IBUS_IS_THEMED_RGBA (rgba));
282 priv = IBUS_THEMED_RGBA_GET_PRIVATE (rgba);
283 if (rgba->normal_fg) {
284 g_slice_free (IBusRGBA, rgba->normal_fg);
285 rgba->normal_fg = NULL;
286 }
287 if (rgba->normal_bg) {
288 g_slice_free (IBusRGBA, rgba->normal_bg);
289 rgba->normal_bg = NULL;
290 }
291 if (rgba->selected_fg) {
292 g_slice_free (IBusRGBA, rgba->selected_fg);
293 rgba->selected_fg = NULL;
294 }
295 if (rgba->selected_bg) {
296 g_slice_free (IBusRGBA, rgba->selected_bg);
297 rgba->selected_bg = NULL;
298 }
299 #if GTK_CHECK_VERSION (2, 91, 0)
300 rgba->normal_fg = ibus_rgba_new_with_style_context (priv->style_context,
301 "theme_fg_color",
302 "fg_normal",
303 "#2e3436");
304 rgba->normal_bg = ibus_rgba_new_with_style_context (priv->style_context,
305 "theme_bg_color",
306 "base_normal",
307 "#f6f5f4");
308 rgba->selected_fg =
309 ibus_rgba_new_with_style_context (priv->style_context,
310 "theme_selected_fg_color",
311 "fg_selected",
312 "#ffffff");
313 rgba->selected_bg =
314 ibus_rgba_new_with_style_context (priv->style_context,
315 "theme_selected_bg_color",
316 "base_selected",
317 "#3584e4");
318 #else
319 rgba->normal_fg = ibus_rgba_new_with_gdk_color (
320 &priv->style->fg[GTK_STATE_NORMAL]);
321 rgba->normal_bg = ibus_rgba_new_with_gdk_color (
322 &priv->style->base[GTK_STATE_NORMAL]);
323 rgba->selected_fg = ibus_rgba_new_with_gdk_color (
324 &priv->style->fg[GTK_STATE_SELECTED]);
325 rgba->selected_bg = ibus_rgba_new_with_gdk_color (
326 &priv->style->base[GTK_STATE_SELECTED]);
327 #endif
328 }