2014-10-18 03:45:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2014 Benjamin Otte <otte@gnome.org>
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
#include "gtkcssnodedeclarationprivate.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
struct _GtkCssNodeDeclaration {
|
|
|
|
guint refcount;
|
2020-01-23 23:43:26 +00:00
|
|
|
GQuark name;
|
|
|
|
GQuark id;
|
2014-10-18 03:45:21 +00:00
|
|
|
GtkStateFlags state;
|
|
|
|
guint n_classes;
|
2020-01-26 03:17:17 +00:00
|
|
|
GQuark classes[0];
|
2014-10-18 03:45:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline gsize
|
2016-10-07 23:16:46 +00:00
|
|
|
sizeof_node (guint n_classes)
|
2014-10-18 03:45:21 +00:00
|
|
|
{
|
|
|
|
return sizeof (GtkCssNodeDeclaration)
|
2016-10-07 23:16:46 +00:00
|
|
|
+ sizeof (GQuark) * n_classes;
|
2014-10-18 03:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline gsize
|
|
|
|
sizeof_this_node (GtkCssNodeDeclaration *decl)
|
|
|
|
{
|
2016-10-07 23:16:46 +00:00
|
|
|
return sizeof_node (decl->n_classes);
|
2014-10-18 03:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_node_declaration_make_writable (GtkCssNodeDeclaration **decl)
|
|
|
|
{
|
|
|
|
if ((*decl)->refcount == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
(*decl)->refcount--;
|
|
|
|
|
|
|
|
*decl = g_memdup (*decl, sizeof_this_node (*decl));
|
|
|
|
(*decl)->refcount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_css_node_declaration_make_writable_resize (GtkCssNodeDeclaration **decl,
|
|
|
|
gsize offset,
|
|
|
|
gsize bytes_added,
|
|
|
|
gsize bytes_removed)
|
|
|
|
{
|
|
|
|
gsize old_size = sizeof_this_node (*decl);
|
|
|
|
gsize new_size = old_size + bytes_added - bytes_removed;
|
|
|
|
|
|
|
|
if ((*decl)->refcount == 1)
|
|
|
|
{
|
|
|
|
if (bytes_removed > 0 && old_size - offset - bytes_removed > 0)
|
2014-10-22 01:38:20 +00:00
|
|
|
memmove (((char *) *decl) + offset, ((char *) *decl) + offset + bytes_removed, old_size - offset - bytes_removed);
|
2014-10-18 03:45:21 +00:00
|
|
|
*decl = g_realloc (*decl, new_size);
|
|
|
|
if (bytes_added > 0 && old_size - offset > 0)
|
|
|
|
memmove (((char *) *decl) + offset + bytes_added, ((char *) *decl) + offset, old_size - offset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GtkCssNodeDeclaration *old = *decl;
|
|
|
|
|
|
|
|
old->refcount--;
|
|
|
|
|
|
|
|
*decl = g_malloc (new_size);
|
|
|
|
memcpy (*decl, old, offset);
|
|
|
|
if (old_size - offset - bytes_removed > 0)
|
|
|
|
memcpy (((char *) *decl) + offset + bytes_added, ((char *) old) + offset + bytes_removed, old_size - offset - bytes_removed);
|
|
|
|
(*decl)->refcount = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkCssNodeDeclaration *
|
|
|
|
gtk_css_node_declaration_new (void)
|
|
|
|
{
|
|
|
|
static GtkCssNodeDeclaration empty = {
|
|
|
|
1, /* need to own a ref ourselves so the copy-on-write path kicks in when people change things */
|
2020-01-23 23:43:26 +00:00
|
|
|
0,
|
|
|
|
0,
|
2015-09-04 17:38:50 +00:00
|
|
|
0,
|
2014-10-18 03:45:21 +00:00
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
return gtk_css_node_declaration_ref (&empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkCssNodeDeclaration *
|
|
|
|
gtk_css_node_declaration_ref (GtkCssNodeDeclaration *decl)
|
|
|
|
{
|
|
|
|
decl->refcount++;
|
|
|
|
|
|
|
|
return decl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_css_node_declaration_unref (GtkCssNodeDeclaration *decl)
|
|
|
|
{
|
|
|
|
decl->refcount--;
|
|
|
|
if (decl->refcount > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_free (decl);
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:38:50 +00:00
|
|
|
gboolean
|
2020-01-23 23:43:26 +00:00
|
|
|
gtk_css_node_declaration_set_name (GtkCssNodeDeclaration **decl,
|
|
|
|
GQuark name)
|
2015-09-04 17:38:50 +00:00
|
|
|
{
|
2015-10-23 00:58:52 +00:00
|
|
|
if ((*decl)->name == name)
|
2015-09-04 17:38:50 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
gtk_css_node_declaration_make_writable (decl);
|
|
|
|
(*decl)->name = name;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-01-23 23:43:26 +00:00
|
|
|
GQuark
|
2015-09-04 17:38:50 +00:00
|
|
|
gtk_css_node_declaration_get_name (const GtkCssNodeDeclaration *decl)
|
|
|
|
{
|
|
|
|
return decl->name;
|
|
|
|
}
|
|
|
|
|
2015-01-19 23:33:34 +00:00
|
|
|
gboolean
|
|
|
|
gtk_css_node_declaration_set_id (GtkCssNodeDeclaration **decl,
|
2020-01-23 23:43:26 +00:00
|
|
|
GQuark id)
|
2015-01-19 23:33:34 +00:00
|
|
|
{
|
|
|
|
if ((*decl)->id == id)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
gtk_css_node_declaration_make_writable (decl);
|
|
|
|
(*decl)->id = id;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-01-23 23:43:26 +00:00
|
|
|
GQuark
|
2015-01-19 23:33:34 +00:00
|
|
|
gtk_css_node_declaration_get_id (const GtkCssNodeDeclaration *decl)
|
|
|
|
{
|
|
|
|
return decl->id;
|
|
|
|
}
|
|
|
|
|
2014-10-18 03:45:21 +00:00
|
|
|
gboolean
|
|
|
|
gtk_css_node_declaration_set_state (GtkCssNodeDeclaration **decl,
|
|
|
|
GtkStateFlags state)
|
|
|
|
{
|
|
|
|
if ((*decl)->state == state)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
gtk_css_node_declaration_make_writable (decl);
|
|
|
|
(*decl)->state = state;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkStateFlags
|
|
|
|
gtk_css_node_declaration_get_state (const GtkCssNodeDeclaration *decl)
|
|
|
|
{
|
|
|
|
return decl->state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
find_class (const GtkCssNodeDeclaration *decl,
|
|
|
|
GQuark class_quark,
|
|
|
|
guint *position)
|
|
|
|
{
|
2020-07-24 13:54:49 +00:00
|
|
|
int min, max, mid;
|
2014-10-18 03:45:21 +00:00
|
|
|
gboolean found = FALSE;
|
|
|
|
guint pos;
|
|
|
|
|
2015-09-11 02:04:31 +00:00
|
|
|
*position = 0;
|
2014-10-18 03:45:21 +00:00
|
|
|
|
|
|
|
if (decl->n_classes == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
min = 0;
|
|
|
|
max = decl->n_classes - 1;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
GQuark item;
|
|
|
|
|
|
|
|
mid = (min + max) / 2;
|
2020-01-26 03:17:17 +00:00
|
|
|
item = decl->classes[mid];
|
2014-10-18 03:45:21 +00:00
|
|
|
|
|
|
|
if (class_quark == item)
|
|
|
|
{
|
|
|
|
found = TRUE;
|
|
|
|
pos = mid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (class_quark > item)
|
|
|
|
min = pos = mid + 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
max = mid - 1;
|
|
|
|
pos = mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (min <= max);
|
|
|
|
|
2015-09-11 02:04:31 +00:00
|
|
|
*position = pos;
|
2014-10-18 03:45:21 +00:00
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gtk_css_node_declaration_add_class (GtkCssNodeDeclaration **decl,
|
|
|
|
GQuark class_quark)
|
|
|
|
{
|
|
|
|
guint pos;
|
|
|
|
|
|
|
|
if (find_class (*decl, class_quark, &pos))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
gtk_css_node_declaration_make_writable_resize (decl,
|
2020-01-26 03:17:17 +00:00
|
|
|
(char *) &(*decl)->classes[pos] - (char *) *decl,
|
2014-10-18 03:45:21 +00:00
|
|
|
sizeof (GQuark),
|
|
|
|
0);
|
|
|
|
(*decl)->n_classes++;
|
2020-01-26 03:17:17 +00:00
|
|
|
(*decl)->classes[pos] = class_quark;
|
2014-10-18 03:45:21 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gtk_css_node_declaration_remove_class (GtkCssNodeDeclaration **decl,
|
|
|
|
GQuark class_quark)
|
|
|
|
{
|
|
|
|
guint pos;
|
|
|
|
|
|
|
|
if (!find_class (*decl, class_quark, &pos))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
gtk_css_node_declaration_make_writable_resize (decl,
|
2020-01-26 03:17:17 +00:00
|
|
|
(char *) &(*decl)->classes[pos] - (char *) *decl,
|
2014-10-18 03:45:21 +00:00
|
|
|
0,
|
|
|
|
sizeof (GQuark));
|
|
|
|
(*decl)->n_classes--;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-09-12 00:58:28 +00:00
|
|
|
gboolean
|
|
|
|
gtk_css_node_declaration_clear_classes (GtkCssNodeDeclaration **decl)
|
|
|
|
{
|
|
|
|
if ((*decl)->n_classes == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
gtk_css_node_declaration_make_writable_resize (decl,
|
2020-01-26 03:17:17 +00:00
|
|
|
(char *) (*decl)->classes - (char *) *decl,
|
2015-09-12 00:58:28 +00:00
|
|
|
0,
|
|
|
|
sizeof (GQuark) * (*decl)->n_classes);
|
|
|
|
(*decl)->n_classes = 0;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-10-18 03:45:21 +00:00
|
|
|
gboolean
|
|
|
|
gtk_css_node_declaration_has_class (const GtkCssNodeDeclaration *decl,
|
|
|
|
GQuark class_quark)
|
|
|
|
{
|
2015-09-11 02:04:31 +00:00
|
|
|
guint pos;
|
2015-09-09 18:40:36 +00:00
|
|
|
|
|
|
|
switch (decl->n_classes)
|
|
|
|
{
|
|
|
|
case 3:
|
2020-01-26 03:17:17 +00:00
|
|
|
if (decl->classes[2] == class_quark)
|
2015-09-09 18:40:36 +00:00
|
|
|
return TRUE;
|
2019-01-26 14:09:33 +00:00
|
|
|
G_GNUC_FALLTHROUGH;
|
2015-09-09 18:40:36 +00:00
|
|
|
|
|
|
|
case 2:
|
2020-01-26 03:17:17 +00:00
|
|
|
if (decl->classes[1] == class_quark)
|
2015-09-09 18:40:36 +00:00
|
|
|
return TRUE;
|
2019-01-26 14:09:33 +00:00
|
|
|
G_GNUC_FALLTHROUGH;
|
2015-09-09 18:40:36 +00:00
|
|
|
|
|
|
|
case 1:
|
2020-01-26 03:17:17 +00:00
|
|
|
if (decl->classes[0] == class_quark)
|
2015-09-09 18:40:36 +00:00
|
|
|
return TRUE;
|
2019-01-26 14:09:33 +00:00
|
|
|
G_GNUC_FALLTHROUGH;
|
2015-09-09 18:40:36 +00:00
|
|
|
|
|
|
|
case 0:
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
default:
|
2015-09-11 02:04:31 +00:00
|
|
|
return find_class (decl, class_quark, &pos);
|
2015-09-09 18:40:36 +00:00
|
|
|
}
|
2014-10-18 03:45:21 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 15:49:59 +00:00
|
|
|
const GQuark *
|
|
|
|
gtk_css_node_declaration_get_classes (const GtkCssNodeDeclaration *decl,
|
|
|
|
guint *n_classes)
|
2014-10-18 03:45:21 +00:00
|
|
|
{
|
2015-09-11 15:49:59 +00:00
|
|
|
*n_classes = decl->n_classes;
|
2014-10-18 03:45:21 +00:00
|
|
|
|
2020-01-26 03:17:17 +00:00
|
|
|
return decl->classes;
|
2014-10-18 03:45:21 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 03:37:17 +00:00
|
|
|
void
|
|
|
|
gtk_css_node_declaration_add_bloom_hashes (const GtkCssNodeDeclaration *decl,
|
|
|
|
GtkCountingBloomFilter *filter)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
if (decl->name)
|
|
|
|
gtk_counting_bloom_filter_add (filter, gtk_css_hash_name (decl->name));
|
|
|
|
if (decl->id)
|
|
|
|
gtk_counting_bloom_filter_add (filter, gtk_css_hash_id (decl->id));
|
|
|
|
|
|
|
|
for (i = 0; i < decl->n_classes; i++)
|
|
|
|
{
|
|
|
|
gtk_counting_bloom_filter_add (filter, gtk_css_hash_class (decl->classes[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_css_node_declaration_remove_bloom_hashes (const GtkCssNodeDeclaration *decl,
|
|
|
|
GtkCountingBloomFilter *filter)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
if (decl->name)
|
|
|
|
gtk_counting_bloom_filter_remove (filter, gtk_css_hash_name (decl->name));
|
|
|
|
if (decl->id)
|
|
|
|
gtk_counting_bloom_filter_remove (filter, gtk_css_hash_id (decl->id));
|
|
|
|
|
|
|
|
for (i = 0; i < decl->n_classes; i++)
|
|
|
|
{
|
|
|
|
gtk_counting_bloom_filter_remove (filter, gtk_css_hash_class (decl->classes[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-18 03:45:21 +00:00
|
|
|
guint
|
|
|
|
gtk_css_node_declaration_hash (gconstpointer elem)
|
|
|
|
{
|
|
|
|
const GtkCssNodeDeclaration *decl = elem;
|
|
|
|
guint hash, i;
|
|
|
|
|
2020-01-23 01:22:30 +00:00
|
|
|
hash = GPOINTER_TO_UINT (decl->name);
|
2015-01-19 23:33:34 +00:00
|
|
|
hash <<= 5;
|
|
|
|
hash ^= GPOINTER_TO_UINT (decl->id);
|
2014-10-18 03:45:21 +00:00
|
|
|
|
|
|
|
for (i = 0; i < decl->n_classes; i++)
|
|
|
|
{
|
|
|
|
hash <<= 5;
|
2020-01-26 03:17:17 +00:00
|
|
|
hash += decl->classes[i];
|
2014-10-18 03:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hash ^= decl->state;
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gtk_css_node_declaration_equal (gconstpointer elem1,
|
|
|
|
gconstpointer elem2)
|
|
|
|
{
|
|
|
|
const GtkCssNodeDeclaration *decl1 = elem1;
|
|
|
|
const GtkCssNodeDeclaration *decl2 = elem2;
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
if (decl1 == decl2)
|
|
|
|
return TRUE;
|
|
|
|
|
2015-09-04 17:38:50 +00:00
|
|
|
if (decl1->name != decl2->name)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-10-18 03:45:21 +00:00
|
|
|
if (decl1->state != decl2->state)
|
|
|
|
return FALSE;
|
|
|
|
|
2015-01-19 23:33:34 +00:00
|
|
|
if (decl1->id != decl2->id)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-10-18 03:45:21 +00:00
|
|
|
if (decl1->n_classes != decl2->n_classes)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
for (i = 0; i < decl1->n_classes; i++)
|
|
|
|
{
|
2020-01-26 03:17:17 +00:00
|
|
|
if (decl1->classes[i] != decl2->classes[i])
|
2014-10-18 03:45:21 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-01-17 21:54:38 +00:00
|
|
|
static int
|
|
|
|
cmpstr (gconstpointer a,
|
|
|
|
gconstpointer b,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
char **ap = (char **) a;
|
|
|
|
char **bp = (char **) b;
|
|
|
|
|
|
|
|
return g_ascii_strcasecmp (*ap, *bp);
|
|
|
|
}
|
|
|
|
|
2016-01-03 19:02:00 +00:00
|
|
|
/* Append the declaration to the string, in selector format */
|
|
|
|
void
|
|
|
|
gtk_css_node_declaration_print (const GtkCssNodeDeclaration *decl,
|
|
|
|
GString *string)
|
|
|
|
{
|
|
|
|
guint i;
|
2020-01-17 21:54:38 +00:00
|
|
|
char **classnames;
|
2016-01-03 19:02:00 +00:00
|
|
|
|
|
|
|
if (decl->name)
|
2020-01-23 23:43:26 +00:00
|
|
|
g_string_append (string, g_quark_to_string (decl->name));
|
2016-01-03 19:02:00 +00:00
|
|
|
else
|
2020-01-23 01:22:30 +00:00
|
|
|
g_string_append (string, "*");
|
2016-01-03 19:02:00 +00:00
|
|
|
|
|
|
|
if (decl->id)
|
|
|
|
{
|
|
|
|
g_string_append_c (string, '#');
|
2020-01-23 23:43:26 +00:00
|
|
|
g_string_append (string, g_quark_to_string (decl->id));
|
2016-01-03 19:02:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 21:54:38 +00:00
|
|
|
classnames = g_new (char *, decl->n_classes);
|
|
|
|
for (i = 0; i < decl->n_classes; i++)
|
2020-01-26 03:17:17 +00:00
|
|
|
classnames[i] = (char *)g_quark_to_string (decl->classes[i]);
|
2020-01-17 21:54:38 +00:00
|
|
|
|
|
|
|
g_qsort_with_data (classnames, decl->n_classes, sizeof (char *), cmpstr, NULL);
|
|
|
|
|
2016-01-03 19:02:00 +00:00
|
|
|
for (i = 0; i < decl->n_classes; i++)
|
|
|
|
{
|
|
|
|
g_string_append_c (string, '.');
|
2020-01-17 21:54:38 +00:00
|
|
|
g_string_append (string, classnames[i]);
|
2016-01-03 19:02:00 +00:00
|
|
|
}
|
2020-01-17 21:54:38 +00:00
|
|
|
g_free (classnames);
|
2016-01-03 19:02:00 +00:00
|
|
|
|
2020-01-27 02:44:05 +00:00
|
|
|
for (i = 0; i < sizeof (GtkStateFlags) * 8; i++)
|
2016-01-03 19:02:00 +00:00
|
|
|
{
|
|
|
|
if (decl->state & (1 << i))
|
|
|
|
{
|
2020-01-27 02:44:05 +00:00
|
|
|
const char *name = gtk_css_pseudoclass_name (1 << i);
|
|
|
|
g_assert (name);
|
2016-01-03 19:02:00 +00:00
|
|
|
g_string_append_c (string, ':');
|
2020-01-27 02:44:05 +00:00
|
|
|
g_string_append (string, name);
|
2016-01-03 19:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-10 18:59:57 +00:00
|
|
|
|
|
|
|
char *
|
|
|
|
gtk_css_node_declaration_to_string (const GtkCssNodeDeclaration *decl)
|
|
|
|
{
|
|
|
|
GString *s = g_string_new (NULL);
|
|
|
|
|
|
|
|
gtk_css_node_declaration_print (decl, s);
|
|
|
|
|
|
|
|
return g_string_free (s, FALSE);
|
|
|
|
}
|