2011-06-17 00:02:24 +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
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2011-06-17 00:02:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2019-04-10 15:23:14 +00:00
|
|
|
#include "gtkcsssection.h"
|
2011-06-17 00:02:24 +00:00
|
|
|
|
|
|
|
#include "gtkcssparserprivate.h"
|
|
|
|
|
|
|
|
struct _GtkCssSection
|
|
|
|
{
|
2020-07-24 13:54:49 +00:00
|
|
|
int ref_count;
|
2011-06-17 00:02:24 +00:00
|
|
|
GtkCssSection *parent;
|
|
|
|
GFile *file;
|
2019-04-10 00:59:15 +00:00
|
|
|
GtkCssLocation start_location;
|
|
|
|
GtkCssLocation end_location; /* end location if parser is %NULL */
|
2011-06-17 00:02:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_BOXED_TYPE (GtkCssSection, gtk_css_section, gtk_css_section_ref, gtk_css_section_unref)
|
|
|
|
|
2019-04-10 14:37:52 +00:00
|
|
|
/**
|
|
|
|
* gtk_css_section_new: (constructor)
|
|
|
|
* @file: (nullable) (transfer none): The file this section refers to
|
|
|
|
* @start: The start location
|
|
|
|
* @end: The end location
|
|
|
|
*
|
2021-02-22 17:19:37 +00:00
|
|
|
* Creates a new `GtkCssSection` referring to the section
|
|
|
|
* in the given `file` from the `start` location to the
|
|
|
|
* `end` location.
|
2019-04-10 14:37:52 +00:00
|
|
|
*
|
2021-02-22 17:19:37 +00:00
|
|
|
* Returns: (transfer full): a new `GtkCssSection`
|
2019-04-10 14:37:52 +00:00
|
|
|
**/
|
2011-06-17 00:02:24 +00:00
|
|
|
GtkCssSection *
|
2019-04-10 14:37:52 +00:00
|
|
|
gtk_css_section_new (GFile *file,
|
|
|
|
const GtkCssLocation *start,
|
|
|
|
const GtkCssLocation *end)
|
2011-06-17 00:02:24 +00:00
|
|
|
{
|
2019-04-10 14:37:52 +00:00
|
|
|
GtkCssSection *result;
|
2011-06-17 00:02:24 +00:00
|
|
|
|
2019-04-10 16:40:50 +00:00
|
|
|
g_return_val_if_fail (file == NULL || G_IS_FILE (file), NULL);
|
|
|
|
g_return_val_if_fail (start != NULL, NULL);
|
|
|
|
g_return_val_if_fail (end != NULL, NULL);
|
2011-06-17 00:02:24 +00:00
|
|
|
|
2019-04-10 14:37:52 +00:00
|
|
|
result = g_slice_new0 (GtkCssSection);
|
2011-06-17 00:02:24 +00:00
|
|
|
|
2019-04-10 14:37:52 +00:00
|
|
|
result->ref_count = 1;
|
|
|
|
if (file)
|
|
|
|
result->file = g_object_ref (file);
|
|
|
|
result->start_location = *start;
|
|
|
|
result->end_location = *end;
|
2011-06-17 00:02:24 +00:00
|
|
|
|
2019-04-10 14:37:52 +00:00
|
|
|
return result;
|
2011-06-17 00:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_css_section_ref:
|
2021-02-22 17:19:37 +00:00
|
|
|
* @section: a `GtkCssSection`
|
2011-06-17 00:02:24 +00:00
|
|
|
*
|
2021-02-22 17:19:37 +00:00
|
|
|
* Increments the reference count on `section`.
|
2011-06-17 00:02:24 +00:00
|
|
|
*
|
2021-02-22 17:19:37 +00:00
|
|
|
* Returns: (transfer full): the CSS section itself.
|
2011-06-17 00:02:24 +00:00
|
|
|
**/
|
|
|
|
GtkCssSection *
|
|
|
|
gtk_css_section_ref (GtkCssSection *section)
|
|
|
|
{
|
2019-04-10 16:40:50 +00:00
|
|
|
g_return_val_if_fail (section != NULL, NULL);
|
2011-06-17 00:02:24 +00:00
|
|
|
|
2015-09-11 23:04:07 +00:00
|
|
|
section->ref_count += 1;
|
2011-06-17 00:02:24 +00:00
|
|
|
|
|
|
|
return section;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_css_section_unref:
|
2021-02-22 17:19:37 +00:00
|
|
|
* @section: (transfer full): a `GtkCssSection`
|
2011-06-17 00:02:24 +00:00
|
|
|
*
|
2021-02-22 17:19:37 +00:00
|
|
|
* Decrements the reference count on `section`, freeing the
|
2011-06-17 00:02:24 +00:00
|
|
|
* structure if the reference count reaches 0.
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
gtk_css_section_unref (GtkCssSection *section)
|
|
|
|
{
|
2019-04-10 16:40:50 +00:00
|
|
|
g_return_if_fail (section != NULL);
|
2011-06-17 00:02:24 +00:00
|
|
|
|
2015-09-11 23:04:07 +00:00
|
|
|
section->ref_count -= 1;
|
|
|
|
if (section->ref_count > 0)
|
2011-06-17 00:02:24 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (section->parent)
|
|
|
|
gtk_css_section_unref (section->parent);
|
|
|
|
if (section->file)
|
|
|
|
g_object_unref (section->file);
|
|
|
|
|
|
|
|
g_slice_free (GtkCssSection, section);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_css_section_get_parent:
|
|
|
|
* @section: the section
|
|
|
|
*
|
2021-02-22 17:19:37 +00:00
|
|
|
* Gets the parent section for the given `section`.
|
|
|
|
*
|
|
|
|
* The parent section is the section that contains this `section`. A special
|
|
|
|
* case are sections of type `GTK_CSS_SECTION_DOCUMEN`T. Their parent will
|
|
|
|
* either be `NULL` if they are the original CSS document that was loaded by
|
|
|
|
* [method@Gtk.CssProvider.load_from_file] or a section of type
|
|
|
|
* `GTK_CSS_SECTION_IMPORT` if it was loaded with an `@import` rule from
|
2011-06-17 00:02:24 +00:00
|
|
|
* a different file.
|
|
|
|
*
|
2021-02-22 17:19:37 +00:00
|
|
|
* Returns: (nullable) (transfer none): the parent section
|
2011-06-17 00:02:24 +00:00
|
|
|
**/
|
|
|
|
GtkCssSection *
|
|
|
|
gtk_css_section_get_parent (const GtkCssSection *section)
|
|
|
|
{
|
2019-04-10 16:40:50 +00:00
|
|
|
g_return_val_if_fail (section != NULL, NULL);
|
2011-06-17 00:02:24 +00:00
|
|
|
|
|
|
|
return section->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_css_section_get_file:
|
|
|
|
* @section: the section
|
|
|
|
*
|
2021-02-22 17:19:37 +00:00
|
|
|
* Gets the file that @section was parsed from.
|
2011-06-17 00:02:24 +00:00
|
|
|
*
|
2021-02-22 17:19:37 +00:00
|
|
|
* If no such file exists, for example because the CSS was loaded via
|
|
|
|
* [method@Gtk.CssProvider.load_from_data], then `NULL` is returned.
|
|
|
|
*
|
2022-01-01 16:31:38 +00:00
|
|
|
* Returns: (transfer none) (nullable): the `GFile` from which the `section`
|
2021-02-22 17:19:37 +00:00
|
|
|
* was parsed
|
2011-06-17 00:02:24 +00:00
|
|
|
**/
|
|
|
|
GFile *
|
|
|
|
gtk_css_section_get_file (const GtkCssSection *section)
|
|
|
|
{
|
2019-04-10 16:40:50 +00:00
|
|
|
g_return_val_if_fail (section != NULL, NULL);
|
2011-06-17 00:02:24 +00:00
|
|
|
|
|
|
|
return section->file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-10 03:18:14 +00:00
|
|
|
* gtk_css_section_get_start_location:
|
2011-06-17 00:02:24 +00:00
|
|
|
* @section: the section
|
|
|
|
*
|
2019-04-10 03:18:14 +00:00
|
|
|
* Returns the location in the CSS document where this section starts.
|
2011-06-17 00:02:24 +00:00
|
|
|
*
|
2019-06-22 05:13:08 +00:00
|
|
|
* Returns: (transfer none) (not nullable): The start location of
|
2021-05-18 21:05:26 +00:00
|
|
|
* this section
|
|
|
|
*/
|
2019-04-10 03:18:14 +00:00
|
|
|
const GtkCssLocation *
|
|
|
|
gtk_css_section_get_start_location (const GtkCssSection *section)
|
2011-06-17 00:02:24 +00:00
|
|
|
{
|
2019-04-10 16:40:50 +00:00
|
|
|
g_return_val_if_fail (section != NULL, NULL);
|
2019-04-09 02:47:00 +00:00
|
|
|
|
2019-04-10 03:18:14 +00:00
|
|
|
return §ion->start_location;
|
2011-06-17 00:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-10 03:18:14 +00:00
|
|
|
* gtk_css_section_get_end_location:
|
2011-06-17 00:02:24 +00:00
|
|
|
* @section: the section
|
|
|
|
*
|
2019-04-10 03:18:14 +00:00
|
|
|
* Returns the location in the CSS document where this section ends.
|
2011-06-17 00:02:24 +00:00
|
|
|
*
|
2019-06-22 05:13:08 +00:00
|
|
|
* Returns: (transfer none) (not nullable): The end location of
|
2021-05-18 21:05:26 +00:00
|
|
|
* this section
|
|
|
|
*/
|
2019-04-10 03:18:14 +00:00
|
|
|
const GtkCssLocation *
|
|
|
|
gtk_css_section_get_end_location (const GtkCssSection *section)
|
2011-06-17 00:02:24 +00:00
|
|
|
{
|
2019-04-10 16:40:50 +00:00
|
|
|
g_return_val_if_fail (section != NULL, NULL);
|
2011-06-17 00:02:24 +00:00
|
|
|
|
2019-04-10 03:18:14 +00:00
|
|
|
return §ion->end_location;
|
2011-06-17 00:02:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 15:23:14 +00:00
|
|
|
/**
|
|
|
|
* gtk_css_section_print:
|
|
|
|
* @section: a section
|
2021-05-20 13:17:04 +00:00
|
|
|
* @string: a `GString` to print to
|
2019-04-10 15:23:14 +00:00
|
|
|
*
|
2021-02-22 17:19:37 +00:00
|
|
|
* Prints the `section` into `string` in a human-readable form.
|
|
|
|
*
|
|
|
|
* This is a form like `gtk.css:32:1-23` to denote line 32, characters
|
|
|
|
* 1 to 23 in the file `gtk.css`.
|
2019-04-10 15:23:14 +00:00
|
|
|
**/
|
2012-09-27 13:10:52 +00:00
|
|
|
void
|
2019-04-10 15:23:14 +00:00
|
|
|
gtk_css_section_print (const GtkCssSection *section,
|
|
|
|
GString *string)
|
2012-09-27 13:10:52 +00:00
|
|
|
{
|
|
|
|
if (section->file)
|
|
|
|
{
|
|
|
|
GFileInfo *info;
|
|
|
|
|
|
|
|
info = g_file_query_info (section->file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);
|
|
|
|
|
|
|
|
if (info)
|
|
|
|
{
|
|
|
|
g_string_append (string, g_file_info_get_display_name (info));
|
|
|
|
g_object_unref (info);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_string_append (string, "<broken file>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_string_append (string, "<data>");
|
|
|
|
}
|
|
|
|
|
2019-04-10 03:18:14 +00:00
|
|
|
g_string_append_printf (string, ":%zu:%zu",
|
2019-04-10 15:23:14 +00:00
|
|
|
section->start_location.lines + 1,
|
|
|
|
section->start_location.line_chars + 1);
|
|
|
|
if (section->start_location.lines != section->end_location.lines ||
|
|
|
|
section->start_location.line_chars != section->end_location.line_chars)
|
|
|
|
{
|
|
|
|
g_string_append (string, "-");
|
|
|
|
if (section->start_location.lines != section->end_location.lines)
|
|
|
|
g_string_append_printf (string, "%zu:", section->end_location.lines + 1);
|
|
|
|
g_string_append_printf (string, "%zu", section->end_location.line_chars + 1);
|
|
|
|
}
|
2012-09-27 13:10:52 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 15:23:14 +00:00
|
|
|
/**
|
|
|
|
* gtk_css_section_to_string:
|
2021-05-20 13:17:04 +00:00
|
|
|
* @section: a `GtkCssSection`
|
2019-04-10 15:23:14 +00:00
|
|
|
*
|
|
|
|
* Prints the section into a human-readable text form using
|
2021-02-22 17:19:37 +00:00
|
|
|
* [method@Gtk.CssSection.print].
|
2019-04-10 15:23:14 +00:00
|
|
|
*
|
|
|
|
* Returns: (transfer full): A new string.
|
|
|
|
**/
|
2012-09-27 13:10:52 +00:00
|
|
|
char *
|
2019-04-10 15:23:14 +00:00
|
|
|
gtk_css_section_to_string (const GtkCssSection *section)
|
2012-09-27 13:10:52 +00:00
|
|
|
{
|
|
|
|
GString *string;
|
|
|
|
|
2019-04-10 16:40:50 +00:00
|
|
|
g_return_val_if_fail (section != NULL, NULL);
|
2012-09-27 13:10:52 +00:00
|
|
|
|
|
|
|
string = g_string_new (NULL);
|
2019-04-10 15:23:14 +00:00
|
|
|
gtk_css_section_print (section, string);
|
2012-09-27 13:10:52 +00:00
|
|
|
|
|
|
|
return g_string_free (string, FALSE);
|
|
|
|
}
|