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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*-
* gtksourcestyle.c
*
* Copyright (C) 2003 - Paolo Maggi <paolo.maggi@polito.it>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "gtksourcestyle-private.h"
#include "gtksourceview-i18n.h"
static void gtk_source_style_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gtk_source_style_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
typedef GObjectClass GtkSourceStyleClass;
G_DEFINE_TYPE (GtkSourceStyle, gtk_source_style, G_TYPE_OBJECT)
enum {
PROP_0,
PROP_LINE_BACKGROUND,
PROP_LINE_BACKGROUND_SET,
PROP_BACKGROUND,
PROP_BACKGROUND_SET,
PROP_FOREGROUND,
PROP_FOREGROUND_SET,
PROP_BOLD,
PROP_BOLD_SET,
PROP_ITALIC,
PROP_ITALIC_SET,
PROP_UNDERLINE,
PROP_UNDERLINE_SET,
PROP_STRIKETHROUGH,
PROP_STRIKETHROUGH_SET
};
static void
gtk_source_style_class_init (GtkSourceStyleClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->set_property = gtk_source_style_set_property;
object_class->get_property = gtk_source_style_get_property;
/* All properties are CONSTRUCT_ONLY so we can safely return references
* from style_scheme_get_style(). But style scheme is of course cheating
* and sets everything after construction (but nobody can notice it). */
g_object_class_install_property (object_class,
PROP_LINE_BACKGROUND,
g_param_spec_string ("line-background",
_("Line background"),
_("Line background color"),
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_BACKGROUND,
g_param_spec_string ("background",
_("Background"),
_("Background color"),
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_FOREGROUND,
g_param_spec_string ("foreground",
_("Foreground"),
_("Foreground color"),
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_BOLD,
g_param_spec_boolean ("bold",
_("Bold"),
_("Bold"),
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_ITALIC,
g_param_spec_boolean ("italic",
_("Italic"),
_("Italic"),
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_UNDERLINE,
g_param_spec_boolean ("underline",
_("Underline"),
_("Underline"),
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_STRIKETHROUGH,
g_param_spec_boolean ("strikethrough",
_("Strikethrough"),
_("Strikethrough"),
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_LINE_BACKGROUND_SET,
g_param_spec_boolean ("line-background-set",
_("Line background set"),
_("Whether line background color is set"),
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_FOREGROUND_SET,
g_param_spec_boolean ("foreground-set",
_("Foreground set"),
_("Whether foreground color is set"),
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_BACKGROUND_SET,
g_param_spec_boolean ("background-set",
_("Background set"),
_("Whether background color is set"),
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_BOLD_SET,
g_param_spec_boolean ("bold-set",
_("Bold set"),
_("Whether bold attribute is set"),
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_ITALIC_SET,
g_param_spec_boolean ("italic-set",
_("Italic set"),
_("Whether italic attribute is set"),
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_UNDERLINE_SET,
g_param_spec_boolean ("underline-set",
_("Underline set"),
_("Whether underline attribute is set"),
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class,
PROP_STRIKETHROUGH_SET,
g_param_spec_boolean ("strikethrough-set",
_("Strikethrough set"),
_("Whether strikethrough attribute is set"),
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
}
static void
gtk_source_style_init (GtkSourceStyle *style)
{
style->foreground = NULL;
style->background = NULL;
style->line_background = NULL;
}
#define SET_MASK(style,name) (style)->mask |= (GTK_SOURCE_STYLE_USE_##name)
#define UNSET_MASK(style,name) (style)->mask &= (GTK_SOURCE_STYLE_USE_##name)
#define MODIFY_MASK(style,value,name) \
G_STMT_START { \
if (g_value_get_boolean (value)) \
SET_MASK (style, name); \
else \
UNSET_MASK (style, name); \
} G_STMT_END
#define GET_MASK(style,value,name) \
g_value_set_boolean (value, ((style)->mask & GTK_SOURCE_STYLE_USE_##name) != 0)
static void
gtk_source_style_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkSourceStyle *style = GTK_SOURCE_STYLE (object);
const gchar *string;
switch (prop_id)
{
case PROP_FOREGROUND:
string = g_value_get_string (value);
if (string != NULL)
{
style->foreground = g_intern_string (string);
SET_MASK (style, FOREGROUND);
}
else
{
style->foreground = NULL;
UNSET_MASK (style, FOREGROUND);
}
break;
case PROP_BACKGROUND:
string = g_value_get_string (value);
if (string != NULL)
{
style->background = g_intern_string (string);
SET_MASK (style, BACKGROUND);
}
else
{
style->background = NULL;
UNSET_MASK (style, BACKGROUND);
}
break;
case PROP_LINE_BACKGROUND:
string = g_value_get_string (value);
if (string != NULL)
{
style->line_background = g_intern_string (string);
SET_MASK (style, LINE_BACKGROUND);
}
else
{
style->line_background = NULL;
UNSET_MASK (style, LINE_BACKGROUND);
}
break;
case PROP_BOLD:
style->bold = g_value_get_boolean (value) != 0;
SET_MASK (style, BOLD);
break;
case PROP_ITALIC:
style->italic = g_value_get_boolean (value) != 0;
SET_MASK (style, ITALIC);
break;
case PROP_UNDERLINE:
style->underline = g_value_get_boolean (value) != 0;
SET_MASK (style, UNDERLINE);
break;
case PROP_STRIKETHROUGH:
style->strikethrough = g_value_get_boolean (value) != 0;
SET_MASK (style, STRIKETHROUGH);
break;
case PROP_FOREGROUND_SET:
MODIFY_MASK (style, value, FOREGROUND);
break;
case PROP_BACKGROUND_SET:
MODIFY_MASK (style, value, BACKGROUND);
break;
case PROP_LINE_BACKGROUND_SET:
MODIFY_MASK (style, value, LINE_BACKGROUND);
break;
case PROP_BOLD_SET:
MODIFY_MASK (style, value, BOLD);
break;
case PROP_ITALIC_SET:
MODIFY_MASK (style, value, ITALIC);
break;
case PROP_UNDERLINE_SET:
MODIFY_MASK (style, value, UNDERLINE);
break;
case PROP_STRIKETHROUGH_SET:
MODIFY_MASK (style, value, STRIKETHROUGH);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_source_style_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkSourceStyle *style = GTK_SOURCE_STYLE (object);
switch (prop_id)
{
case PROP_FOREGROUND:
g_value_set_string (value, style->foreground);
break;
case PROP_BACKGROUND:
g_value_set_string (value, style->background);
break;
case PROP_LINE_BACKGROUND:
g_value_set_string (value, style->line_background);
break;
case PROP_BOLD:
g_value_set_boolean (value, style->bold);
break;
case PROP_ITALIC:
g_value_set_boolean (value, style->italic);
break;
case PROP_UNDERLINE:
g_value_set_boolean (value, style->underline);
break;
case PROP_STRIKETHROUGH:
g_value_set_boolean (value, style->strikethrough);
break;
case PROP_FOREGROUND_SET:
GET_MASK (style, value, FOREGROUND);
break;
case PROP_BACKGROUND_SET:
GET_MASK (style, value, BACKGROUND);
break;
case PROP_LINE_BACKGROUND_SET:
GET_MASK (style, value, LINE_BACKGROUND);
break;
case PROP_BOLD_SET:
GET_MASK (style, value, BOLD);
break;
case PROP_ITALIC_SET:
GET_MASK (style, value, ITALIC);
break;
case PROP_UNDERLINE_SET:
GET_MASK (style, value, UNDERLINE);
break;
case PROP_STRIKETHROUGH_SET:
GET_MASK (style, value, STRIKETHROUGH);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/**
* gtk_source_style_copy:
* @style: a #GtkSourceStyle structure to copy.
*
* Creates a copy of @style, that is a new #GtkSourceStyle instance which
* has the same attributes set.
*
* Returns: copy of @style, call g_object_unref() when you are done with it.
*
* Since: 2.0
*/
GtkSourceStyle *
gtk_source_style_copy (const GtkSourceStyle *style)
{
GtkSourceStyle *copy;
g_return_val_if_fail (style != NULL, NULL);
copy = g_object_new (GTK_TYPE_SOURCE_STYLE, NULL);
copy->foreground = style->foreground;
copy->background = style->background;
copy->line_background = style->line_background;
copy->italic = style->italic;
copy->bold = style->bold;
copy->underline = style->underline;
copy->strikethrough = style->strikethrough;
copy->mask = style->mask;
return copy;
}
/**
* _gtk_source_style_apply:
* @style: a #GtkSourceStyle to apply.
* @tag: a #GtkTextTag to apply styles to.
*
* Applies text styles set in @style if it's not %NULL, or
* unsets style fields in @tag set with _gtk_source_style_apply()
* if @style is %NULL. Note that it does not touch fields which
* are not set in @style. To reset everything use @style == %NULL.
*
* Since: 2.0
*/
void
_gtk_source_style_apply (const 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->mask & GTK_SOURCE_STYLE_USE_BACKGROUND)
g_object_set (tag, "background", style->background, NULL);
if (style->mask & GTK_SOURCE_STYLE_USE_FOREGROUND)
g_object_set (tag, "foreground", style->foreground, NULL);
if (style->mask & GTK_SOURCE_STYLE_USE_LINE_BACKGROUND)
g_object_set (tag, "paragraph-background", style->line_background, NULL);
if (style->mask & GTK_SOURCE_STYLE_USE_ITALIC)
g_object_set (tag, "style", style->italic ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, NULL);
if (style->mask & GTK_SOURCE_STYLE_USE_BOLD)
g_object_set (tag, "weight", style->bold ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL, NULL);
if (style->mask & GTK_SOURCE_STYLE_USE_UNDERLINE)
g_object_set (tag, "underline", style->underline ? PANGO_UNDERLINE_SINGLE : PANGO_UNDERLINE_NONE, NULL);
if (style->mask & GTK_SOURCE_STYLE_USE_STRIKETHROUGH)
g_object_set (tag, "strikethrough", style->strikethrough != 0, NULL);
g_object_thaw_notify (G_OBJECT (tag));
}
else
{
g_object_set (tag,
"background-set", FALSE,
"foreground-set", FALSE,
"paragraph-background-set", FALSE,
"style-set", FALSE,
"weight-set", FALSE,
"underline-set", FALSE,
"strikethrough-set", FALSE,
NULL);
}
}
|