summaryrefslogtreecommitdiff
path: root/gtk/gtksymbolicpaintable.c
blob: 057be29742c91fa88b57ad57e9e7ee7a3d6c2776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright © 2021 Benjamin Otte
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Benjamin Otte <otte@gnome.org>
 */

#include "config.h"

#include "gtksymbolicpaintable.h"

#include "gtkenums.h"

/**
 * GtkSymbolicPaintable:
 *
 * `GtkSymbolicPaintable` is an interface that support symbolic colors in
 * paintables.
 *
 * `GdkPaintable`s implementing the interface will have the
 * [vfunc@Gtk.SymbolicPaintable.snapshot_symbolic] function called and
 * have the colors for drawing symbolic icons passed. At least 4 colors are guaranteed
 * to be passed every time.
 *
 * These 4 colors are the foreground color, and the colors to use for errors, warnings
 * and success information in that order.
 *
 * More colors may be added in the future.
 *
 * Since: 4.6
 */

G_DEFINE_INTERFACE (GtkSymbolicPaintable, gtk_symbolic_paintable, GDK_TYPE_PAINTABLE)

static void
gtk_symbolic_paintable_default_snapshot_symbolic (GtkSymbolicPaintable *paintable,
                                                  GdkSnapshot          *snapshot,
                                                  double                width,
                                                  double                height,
                                                  const GdkRGBA        *colors,
                                                  gsize                 n_colors)
{
  gdk_paintable_snapshot (GDK_PAINTABLE (paintable), snapshot, width, height);
}

static void
gtk_symbolic_paintable_default_init (GtkSymbolicPaintableInterface *iface)
{
  iface->snapshot_symbolic = gtk_symbolic_paintable_default_snapshot_symbolic;
}

/**
 * gtk_symbolic_paintable_snapshot_symbolic
 * @paintable: a `GtkSymbolicPaintable`
 * @snapshot: a `GdkSnapshot` to snapshot to
 * @width: width to snapshot in
 * @height: height to snapshot in
 * @colors: (array length=n_colors): a pointer to an array of colors
 * @n_colors: The number of colors
 *
 * Snapshots the paintable with the given colors.
 *
 * If less than 4 colors are provided, GTK will pad the array with default
 * colors.
 *
 * Since: 4.6
 */
void
gtk_symbolic_paintable_snapshot_symbolic (GtkSymbolicPaintable   *paintable,
                                          GdkSnapshot            *snapshot,
                                          double                  width,
                                          double                  height,
                                          const GdkRGBA          *colors,
                                          gsize                   n_colors)
{
  GtkSymbolicPaintableInterface *iface;

  g_return_if_fail (GTK_IS_SYMBOLIC_PAINTABLE (paintable));
  g_return_if_fail (snapshot != NULL);
  g_return_if_fail (colors != NULL || n_colors == 0);

  if (width <= 0.0 || height <= 0.0)
    return;

  iface = GTK_SYMBOLIC_PAINTABLE_GET_IFACE (paintable);

  if (n_colors >= 4)
    {
      iface->snapshot_symbolic (paintable, snapshot, width, height, colors, n_colors);
    }
  else
    {
      /* Taken from GTK3, no idea where it got those from */
      GdkRGBA real_colors[4] = {
        [GTK_SYMBOLIC_COLOR_FOREGROUND] = { 0.7450980392156863, 0.7450980392156863, 0.7450980392156863, 1.0 },
        [GTK_SYMBOLIC_COLOR_ERROR] = { 0.796887159533074, 0, 0, 1.0 },
        [GTK_SYMBOLIC_COLOR_WARNING] = { 0.9570458533607996, 0.47266346227206835, 0.2421911955443656, 1.0 },
        [GTK_SYMBOLIC_COLOR_SUCCESS] = { 0.3046921492332342,0.6015716792553597, 0.023437857633325704, 1.0 }
      };

      memcpy (real_colors, colors, sizeof (GdkRGBA) * n_colors);

      iface->snapshot_symbolic (paintable, snapshot, width, height, real_colors, 4);
    }
}