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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
/*
* This file is part of GtkSourceView
*
* Copyright (C) 2003 - Paolo Maggi <paolo.maggi@polito.it>
* Copyright (C) 2023 - Sébastien Wilmet <swilmet@gnome.org>
*
* GtkSourceView 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.
*
* GtkSourceView 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 "gtksourcestyle.h"
/**
* SECTION:style
* @Title: GtkSourceStyle
* @Short_description: A style
*
* The #GtkSourceStyle structure contains text attributes.
*/
/* API design:
*
* In a past implementation, GtkSourceStyle was a GObject with a lot of
* properties to get each individual attribute (the properties were read-write
* and construct-only). This was language-bindings friendly, but a bit too
* "heavy" as a solution.
*
* Thus it is now a boxed type.
*
* Reference counting is useful to implement the use-style attribute of the
* <style> tag from the style scheme XML file format.
*/
G_DEFINE_BOXED_TYPE (GtkSourceStyle, gtk_source_style,
gtk_source_style_ref,
gtk_source_style_unref)
/**
* gtk_source_style_new:
*
* Creates a new #GtkSourceStyle with initially all the "`use_`" fields set to
* %FALSE.
*
* Returns: a new #GtkSourceStyle with a reference count of one.
* Since: 299.2
*/
GtkSourceStyle *
gtk_source_style_new (void)
{
GtkSourceStyle *style;
style = g_new0 (GtkSourceStyle, 1);
g_ref_count_init (&style->ref_count);
return style;
}
/**
* gtk_source_style_ref:
* @style: a #GtkSourceStyle.
*
* Increases the reference count of @style by one.
*
* Returns: (transfer full): the passed in @style.
* Since: 299.2
*/
GtkSourceStyle *
gtk_source_style_ref (GtkSourceStyle *style)
{
g_return_val_if_fail (style != NULL, NULL);
g_ref_count_inc (&style->ref_count);
return style;
}
/**
* gtk_source_style_unref:
* @style: a #GtkSourceStyle.
*
* Decreases the reference count of @style by one. If the reference count drops
* to 0, @style is freed.
*
* Since: 299.2
*/
void
gtk_source_style_unref (GtkSourceStyle *style)
{
g_return_if_fail (style != NULL);
if (g_ref_count_dec (&style->ref_count))
{
g_free (style);
}
}
/**
* gtk_source_style_apply:
* @style: (nullable): a #GtkSourceStyle to apply, or %NULL.
* @tag: a #GtkTextTag to apply styles to.
*
* This function modifies the #GtkTextTag properties that are related to the
* #GtkSourceStyle attributes. Other #GtkTextTag properties are left untouched.
*
* If @style is non-%NULL, applies @style to @tag.
*
* If @style is %NULL, the related `*-set` properties of #GtkTextTag are set to
* %FALSE.
*
* Since: 3.22
*/
void
gtk_source_style_apply (GtkSourceStyle *style,
GtkTextTag *tag)
{
g_return_if_fail (GTK_IS_TEXT_TAG (tag));
if (style != NULL)
{
g_object_freeze_notify (G_OBJECT (tag));
if (style->use_foreground_color)
{
g_object_set (tag, "foreground-rgba", &style->foreground_color, NULL);
}
else
{
g_object_set (tag, "foreground-set", FALSE, NULL);
}
if (style->use_background_color)
{
g_object_set (tag, "background-rgba", &style->background_color, NULL);
}
else
{
g_object_set (tag, "background-set", FALSE, NULL);
}
if (style->use_italic)
{
g_object_set (tag, "style", style->italic ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, NULL);
}
else
{
g_object_set (tag, "style-set", FALSE, NULL);
}
if (style->use_bold)
{
g_object_set (tag, "weight", style->bold ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL, NULL);
}
else
{
g_object_set (tag, "weight-set", FALSE, NULL);
}
if (style->use_underline)
{
g_object_set (tag, "underline", style->underline, NULL);
}
else
{
g_object_set (tag, "underline-set", FALSE, NULL);
}
if (style->use_underline_color)
{
g_object_set (tag, "underline-rgba", &style->underline_color, NULL);
}
else
{
g_object_set (tag, "underline-rgba-set", FALSE, NULL);
}
if (style->use_strikethrough)
{
g_object_set (tag, "strikethrough", style->strikethrough, NULL);
}
else
{
g_object_set (tag, "strikethrough-set", FALSE, NULL);
}
if (style->use_scale)
{
g_object_set (tag, "scale", style->scale, NULL);
}
else
{
g_object_set (tag, "scale-set", FALSE, NULL);
}
g_object_thaw_notify (G_OBJECT (tag));
}
else
{
g_object_set (tag,
"foreground-set", FALSE,
"background-set", FALSE,
"style-set", FALSE,
"weight-set", FALSE,
"underline-set", FALSE,
"underline-rgba-set", FALSE,
"strikethrough-set", FALSE,
"scale-set", FALSE,
NULL);
}
}
|