1/*
2 * Copyright © 2021 Benjamin Otte
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors: Benjamin Otte <otte@gnome.org>
18 */
19
20#include "config.h"
21
22#include "gtksymbolicpaintable.h"
23
24#include "gtkenums.h"
25
26/**
27 * GtkSymbolicPaintable:
28 *
29 * `GtkSymbolicPaintable` is an interface that support symbolic colors in
30 * paintables.
31 *
32 * `GdkPaintable`s implementing the interface will have the
33 * [vfunc@Gtk.SymbolicPaintable.snapshot_symbolic] function called and
34 * have the colors for drawing symbolic icons passed. At least 4 colors are guaranteed
35 * to be passed every time.
36 *
37 * These 4 colors are the foreground color, and the colors to use for errors, warnings
38 * and success information in that order.
39 *
40 * More colors may be added in the future.
41 *
42 * Since: 4.6
43 */
44
45G_DEFINE_INTERFACE (GtkSymbolicPaintable, gtk_symbolic_paintable, GDK_TYPE_PAINTABLE)
46
47static void
48gtk_symbolic_paintable_default_snapshot_symbolic (GtkSymbolicPaintable *paintable,
49 GdkSnapshot *snapshot,
50 double width,
51 double height,
52 const GdkRGBA *colors,
53 gsize n_colors)
54{
55 gdk_paintable_snapshot (paintable: GDK_PAINTABLE (ptr: paintable), snapshot, width, height);
56}
57
58static void
59gtk_symbolic_paintable_default_init (GtkSymbolicPaintableInterface *iface)
60{
61 iface->snapshot_symbolic = gtk_symbolic_paintable_default_snapshot_symbolic;
62}
63
64/**
65 * gtk_symbolic_paintable_snapshot_symbolic
66 * @paintable: a `GtkSymbolicPaintable`
67 * @snapshot: a `GdkSnapshot` to snapshot to
68 * @width: width to snapshot in
69 * @height: height to snapshot in
70 * @colors: (array length=n_colors): a pointer to an array of colors
71 * @n_colors: The number of colors
72 *
73 * Snapshots the paintable with the given colors.
74 *
75 * If less than 4 colors are provided, GTK will pad the array with default
76 * colors.
77 *
78 * Since: 4.6
79 */
80void
81gtk_symbolic_paintable_snapshot_symbolic (GtkSymbolicPaintable *paintable,
82 GdkSnapshot *snapshot,
83 double width,
84 double height,
85 const GdkRGBA *colors,
86 gsize n_colors)
87{
88 GtkSymbolicPaintableInterface *iface;
89
90 g_return_if_fail (GTK_IS_SYMBOLIC_PAINTABLE (paintable));
91 g_return_if_fail (snapshot != NULL);
92 g_return_if_fail (colors != NULL || n_colors == 0);
93
94 if (width <= 0.0 || height <= 0.0)
95 return;
96
97 iface = GTK_SYMBOLIC_PAINTABLE_GET_IFACE (ptr: paintable);
98
99 if (n_colors >= 4)
100 {
101 iface->snapshot_symbolic (paintable, snapshot, width, height, colors, n_colors);
102 }
103 else
104 {
105 /* Taken from GTK3, no idea where it got those from */
106 GdkRGBA real_colors[4] = {
107 [GTK_SYMBOLIC_COLOR_FOREGROUND] = { 0.7450980392156863, 0.7450980392156863, 0.7450980392156863, 1.0 },
108 [GTK_SYMBOLIC_COLOR_ERROR] = { .red: 0.796887159533074, .green: 0, .blue: 0, .alpha: 1.0 },
109 [GTK_SYMBOLIC_COLOR_WARNING] = { .red: 0.9570458533607996, .green: 0.47266346227206835, .blue: 0.2421911955443656, .alpha: 1.0 },
110 [GTK_SYMBOLIC_COLOR_SUCCESS] = { .red: 0.3046921492332342,.green: 0.6015716792553597, .blue: 0.023437857633325704, .alpha: 1.0 }
111 };
112
113 memcpy (dest: real_colors, src: colors, n: sizeof (GdkRGBA) * n_colors);
114
115 iface->snapshot_symbolic (paintable, snapshot, width, height, real_colors, 4);
116 }
117}
118

source code of gtk/gtk/gtksymbolicpaintable.c