2015-12-02 23:33:51 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
* Copyright (C) 2011 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* 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 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2016-10-15 20:22:48 +00:00
|
|
|
#include "gtkcsspalettevalueprivate.h"
|
2015-12-02 23:33:51 +00:00
|
|
|
|
|
|
|
#include "gtkcsscolorvalueprivate.h"
|
2020-01-10 12:02:33 +00:00
|
|
|
#include "gtkcsscolorvalueprivate.h"
|
2019-08-17 16:23:36 +00:00
|
|
|
#include "gtkprivate.h"
|
2015-12-02 23:33:51 +00:00
|
|
|
|
|
|
|
struct _GtkCssValue {
|
|
|
|
GTK_CSS_VALUE_BASE
|
2019-08-17 16:23:36 +00:00
|
|
|
guint n_colors;
|
|
|
|
char **color_names;
|
|
|
|
GtkCssValue **color_values;
|
2015-12-02 23:33:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static GtkCssValue *default_palette;
|
|
|
|
|
|
|
|
static GtkCssValue *gtk_css_palette_value_new_empty (void);
|
2019-08-17 16:23:36 +00:00
|
|
|
static GtkCssValue *gtk_css_palette_value_new_sized (guint size);
|
2015-12-02 23:33:51 +00:00
|
|
|
|
|
|
|
static void
|
2019-08-17 16:23:36 +00:00
|
|
|
gtk_css_palette_value_set_color (GtkCssValue *value,
|
|
|
|
guint i,
|
|
|
|
char *name,
|
2015-12-02 23:33:51 +00:00
|
|
|
GtkCssValue *color)
|
|
|
|
{
|
2019-08-17 16:23:36 +00:00
|
|
|
value->color_names[i] = name; /* No strdup */
|
|
|
|
value->color_values[i] = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_palette_value_sort_colors (GtkCssValue *value)
|
|
|
|
{
|
|
|
|
guint i, j;
|
|
|
|
|
|
|
|
/* Bubble sort. We're mostly talking about 3 elements here. */
|
|
|
|
for (i = 0; i < value->n_colors; i ++)
|
|
|
|
for (j = 0; j < value->n_colors; j ++)
|
|
|
|
{
|
|
|
|
if (strcmp (value->color_names[i], value->color_names[j]) < 0)
|
|
|
|
{
|
|
|
|
char *tmp_name;
|
|
|
|
GtkCssValue *tmp_value;
|
|
|
|
|
|
|
|
tmp_name = value->color_names[i];
|
|
|
|
tmp_value = value->color_values[i];
|
|
|
|
|
|
|
|
value->color_names[i] = value->color_names[j];
|
|
|
|
value->color_values[i] = value->color_values[j];
|
|
|
|
|
|
|
|
value->color_names[j] = tmp_name;
|
|
|
|
value->color_values[j] = tmp_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkCssValue *
|
|
|
|
gtk_css_palette_value_find_color (GtkCssValue *value,
|
|
|
|
const char *color_name)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
for (i = 0; i < value->n_colors; i ++)
|
|
|
|
{
|
|
|
|
if (strcmp (value->color_names[i], color_name) == 0)
|
|
|
|
return value->color_values[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2015-12-02 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_value_palette_free (GtkCssValue *value)
|
|
|
|
{
|
2019-08-17 16:23:36 +00:00
|
|
|
guint i;
|
|
|
|
|
|
|
|
for (i = 0; i < value->n_colors; i ++)
|
|
|
|
{
|
|
|
|
g_free (value->color_names[i]);
|
|
|
|
_gtk_css_value_unref (value->color_values[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (value->color_names);
|
|
|
|
g_free (value->color_values);
|
2015-12-02 23:33:51 +00:00
|
|
|
|
|
|
|
g_slice_free (GtkCssValue, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkCssValue *
|
2017-10-31 03:31:46 +00:00
|
|
|
gtk_css_value_palette_compute (GtkCssValue *specified,
|
|
|
|
guint property_id,
|
|
|
|
GtkStyleProvider *provider,
|
|
|
|
GtkCssStyle *style,
|
|
|
|
GtkCssStyle *parent_style)
|
2015-12-02 23:33:51 +00:00
|
|
|
{
|
|
|
|
GtkCssValue *computed_color;
|
|
|
|
GtkCssValue *result;
|
|
|
|
gboolean changes = FALSE;
|
2019-08-17 16:23:36 +00:00
|
|
|
guint i;
|
2015-12-02 23:33:51 +00:00
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
result = gtk_css_palette_value_new_sized (specified->n_colors);
|
2015-12-02 23:33:51 +00:00
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
for (i = 0; i < specified->n_colors; i ++)
|
2015-12-02 23:33:51 +00:00
|
|
|
{
|
2019-08-17 16:23:36 +00:00
|
|
|
GtkCssValue *value = specified->color_values[i];
|
|
|
|
|
2015-12-02 23:33:51 +00:00
|
|
|
computed_color = _gtk_css_value_compute (value, property_id, provider, style, parent_style);
|
2019-08-17 16:23:36 +00:00
|
|
|
result->color_names[i] = g_strdup (specified->color_names[i]);
|
|
|
|
result->color_values[i] = computed_color;
|
|
|
|
|
2015-12-02 23:33:51 +00:00
|
|
|
changes |= computed_color != value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!changes)
|
|
|
|
{
|
|
|
|
_gtk_css_value_unref (result);
|
|
|
|
result = _gtk_css_value_ref (specified);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_css_value_palette_equal (const GtkCssValue *value1,
|
|
|
|
const GtkCssValue *value2)
|
|
|
|
{
|
2019-08-17 16:23:36 +00:00
|
|
|
guint i;
|
2015-12-02 23:33:51 +00:00
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
if (value1->n_colors != value2->n_colors)
|
2015-12-02 23:33:51 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
for (i = 0; i < value1->n_colors; i ++)
|
2015-12-02 23:33:51 +00:00
|
|
|
{
|
2019-08-17 16:23:36 +00:00
|
|
|
if (strcmp (value1->color_names[i], value2->color_names[i]) != 0)
|
2015-12-02 23:33:51 +00:00
|
|
|
return FALSE;
|
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
if (!_gtk_css_value_equal (value1->color_values[i], value2->color_values[i]))
|
2015-12-02 23:33:51 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkCssValue *
|
|
|
|
gtk_css_value_palette_transition (GtkCssValue *start,
|
|
|
|
GtkCssValue *end,
|
|
|
|
guint property_id,
|
|
|
|
double progress)
|
|
|
|
{
|
|
|
|
GtkCssValue *result, *transition;
|
2019-08-17 16:23:36 +00:00
|
|
|
GtkCssValue *start_color, *end_color;
|
|
|
|
const char *name;
|
|
|
|
guint i;
|
|
|
|
GPtrArray *new_names;
|
|
|
|
GPtrArray *new_values;
|
2015-12-02 23:33:51 +00:00
|
|
|
|
|
|
|
/* XXX: For colors that are only in start or end but not both,
|
|
|
|
* we don't transition but just keep the value.
|
|
|
|
* That causes an abrupt transition to currentColor at the end.
|
|
|
|
*/
|
|
|
|
|
|
|
|
result = gtk_css_palette_value_new_empty ();
|
2019-08-17 16:23:36 +00:00
|
|
|
new_names = g_ptr_array_new ();
|
|
|
|
new_values = g_ptr_array_new ();
|
2015-12-02 23:33:51 +00:00
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
for (i = 0; i < start->n_colors; i ++)
|
2015-12-02 23:33:51 +00:00
|
|
|
{
|
2019-08-17 16:23:36 +00:00
|
|
|
name = start->color_names[i];
|
|
|
|
start_color = start->color_values[i];
|
|
|
|
end_color = gtk_css_palette_value_find_color (end, name);
|
|
|
|
|
2015-12-02 23:33:51 +00:00
|
|
|
if (end_color == NULL)
|
|
|
|
transition = _gtk_css_value_ref (start_color);
|
|
|
|
else
|
|
|
|
transition = _gtk_css_value_transition (start_color, end_color, property_id, progress);
|
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
g_ptr_array_add (new_names, g_strdup (name));
|
|
|
|
g_ptr_array_add (new_values, transition);
|
2015-12-02 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
for (i = 0; i < end->n_colors; i ++)
|
2015-12-02 23:33:51 +00:00
|
|
|
{
|
2019-08-17 16:23:36 +00:00
|
|
|
name = end->color_names[i];
|
|
|
|
end_color = end->color_values[i];
|
|
|
|
start_color = gtk_css_palette_value_find_color (start, name);
|
|
|
|
|
2015-12-02 23:33:51 +00:00
|
|
|
if (start_color != NULL)
|
|
|
|
continue;
|
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
g_ptr_array_add (new_names, g_strdup (name));
|
|
|
|
g_ptr_array_add (new_values, _gtk_css_value_ref (end_color));
|
2015-12-02 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
result->n_colors = new_names->len;
|
|
|
|
result->color_names = (char **)g_ptr_array_free (new_names, FALSE);
|
|
|
|
result->color_values = (GtkCssValue **)g_ptr_array_free (new_values, FALSE);
|
|
|
|
gtk_css_palette_value_sort_colors (result);
|
|
|
|
|
2015-12-02 23:33:51 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_value_palette_print (const GtkCssValue *value,
|
|
|
|
GString *string)
|
|
|
|
{
|
|
|
|
gboolean first = TRUE;
|
2019-08-17 16:23:36 +00:00
|
|
|
guint i;
|
2015-12-02 23:33:51 +00:00
|
|
|
|
|
|
|
if (value == default_palette)
|
|
|
|
{
|
|
|
|
g_string_append (string, "default");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
for (i = 0; i < value->n_colors; i ++)
|
2015-12-02 23:33:51 +00:00
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
first = FALSE;
|
|
|
|
else
|
|
|
|
g_string_append (string, ", ");
|
2019-08-17 16:23:36 +00:00
|
|
|
|
|
|
|
g_string_append (string, value->color_names[i]);
|
2015-12-02 23:33:51 +00:00
|
|
|
g_string_append_c (string, ' ');
|
2019-08-17 16:23:36 +00:00
|
|
|
_gtk_css_value_print (value->color_values[i], string);
|
2015-12-02 23:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const GtkCssValueClass GTK_CSS_VALUE_PALETTE = {
|
2020-01-09 10:57:00 +00:00
|
|
|
"GtkCssPaletteValue",
|
2015-12-02 23:33:51 +00:00
|
|
|
gtk_css_value_palette_free,
|
|
|
|
gtk_css_value_palette_compute,
|
|
|
|
gtk_css_value_palette_equal,
|
|
|
|
gtk_css_value_palette_transition,
|
2018-02-13 04:55:33 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2015-12-02 23:33:51 +00:00
|
|
|
gtk_css_value_palette_print
|
|
|
|
};
|
|
|
|
|
|
|
|
static GtkCssValue *
|
|
|
|
gtk_css_palette_value_new_empty (void)
|
|
|
|
{
|
|
|
|
GtkCssValue *result;
|
|
|
|
|
|
|
|
result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_PALETTE);
|
2019-08-17 16:23:36 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkCssValue *
|
|
|
|
gtk_css_palette_value_new_sized (guint size)
|
|
|
|
{
|
|
|
|
GtkCssValue *result;
|
|
|
|
|
|
|
|
result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_PALETTE);
|
|
|
|
result->n_colors = size;
|
|
|
|
result->color_names = g_malloc (sizeof (char *) * size);
|
|
|
|
result->color_values = g_malloc (sizeof (GtkCssValue *) * size);
|
2015-12-02 23:33:51 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkCssValue *
|
|
|
|
gtk_css_palette_value_new_default (void)
|
|
|
|
{
|
|
|
|
if (default_palette == NULL)
|
|
|
|
{
|
2019-08-17 16:23:36 +00:00
|
|
|
default_palette = gtk_css_palette_value_new_sized (3);
|
|
|
|
gtk_css_palette_value_set_color (default_palette, 0, g_strdup ("error"),
|
|
|
|
_gtk_css_color_value_new_name ("error_color"));
|
|
|
|
gtk_css_palette_value_set_color (default_palette, 1, g_strdup ("success"),
|
|
|
|
_gtk_css_color_value_new_name ("success_color"));
|
|
|
|
gtk_css_palette_value_set_color (default_palette, 2, g_strdup ("warning"),
|
|
|
|
_gtk_css_color_value_new_name ("warning_color"));
|
|
|
|
/* Above is already sorted */
|
2015-12-02 23:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return _gtk_css_value_ref (default_palette);
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkCssValue *
|
|
|
|
gtk_css_palette_value_parse (GtkCssParser *parser)
|
|
|
|
{
|
|
|
|
GtkCssValue *result, *color;
|
2019-08-17 16:23:36 +00:00
|
|
|
GPtrArray *names;
|
|
|
|
GPtrArray *colors;
|
2015-12-02 23:33:51 +00:00
|
|
|
char *ident;
|
|
|
|
|
2019-03-31 10:40:53 +00:00
|
|
|
if (gtk_css_parser_try_ident (parser, "default"))
|
2015-12-02 23:33:51 +00:00
|
|
|
return gtk_css_palette_value_new_default ();
|
2019-08-17 16:23:36 +00:00
|
|
|
|
2015-12-02 23:33:51 +00:00
|
|
|
result = gtk_css_palette_value_new_empty ();
|
2019-08-17 16:23:36 +00:00
|
|
|
names = g_ptr_array_new ();
|
|
|
|
colors = g_ptr_array_new ();
|
2015-12-02 23:33:51 +00:00
|
|
|
|
|
|
|
do {
|
2019-03-31 10:40:53 +00:00
|
|
|
ident = gtk_css_parser_consume_ident (parser);
|
2015-12-02 23:33:51 +00:00
|
|
|
if (ident == NULL)
|
|
|
|
{
|
|
|
|
_gtk_css_value_unref (result);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-08-17 16:23:36 +00:00
|
|
|
|
2015-12-02 23:33:51 +00:00
|
|
|
color = _gtk_css_color_value_parse (parser);
|
|
|
|
if (color == NULL)
|
|
|
|
{
|
|
|
|
g_free (ident);
|
|
|
|
_gtk_css_value_unref (result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-01-11 12:40:13 +00:00
|
|
|
result->is_computed = result->is_computed && gtk_css_value_is_computed (color);
|
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
g_ptr_array_add (names, ident);
|
|
|
|
g_ptr_array_add (colors, color);
|
2019-03-31 10:40:53 +00:00
|
|
|
} while (gtk_css_parser_try_token (parser, GTK_CSS_TOKEN_COMMA));
|
2015-12-02 23:33:51 +00:00
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
result->n_colors = names->len;
|
|
|
|
result->color_names = (char **)g_ptr_array_free (names, FALSE);
|
|
|
|
result->color_values = (GtkCssValue **) g_ptr_array_free (colors, FALSE);
|
|
|
|
gtk_css_palette_value_sort_colors (result);
|
|
|
|
|
2015-12-02 23:33:51 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GdkRGBA *
|
|
|
|
gtk_css_palette_value_get_color (GtkCssValue *value,
|
|
|
|
const char *name)
|
|
|
|
{
|
2019-08-17 16:23:36 +00:00
|
|
|
guint i;
|
2015-12-02 23:33:51 +00:00
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
gtk_internal_return_val_if_fail (value->class == >K_CSS_VALUE_PALETTE, NULL);
|
2015-12-02 23:33:51 +00:00
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
for (i = 0; i < value->n_colors; i ++)
|
|
|
|
{
|
|
|
|
if (strcmp (value->color_names[i], name) == 0)
|
2020-01-10 12:02:33 +00:00
|
|
|
return gtk_css_color_value_get_rgba (value->color_values[i]);
|
2019-08-17 16:23:36 +00:00
|
|
|
}
|
2015-12-02 23:33:51 +00:00
|
|
|
|
2019-08-17 16:23:36 +00:00
|
|
|
return NULL;
|
2015-12-02 23:33:51 +00:00
|
|
|
}
|