1 /* gtktextattributes.c - text attributes
3 * Copyright (c) 1992-1994 The Regents of the University of California.
4 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
5 * Copyright (c) 2000 Red Hat, Inc.
6 * Tk -> Gtk port by Havoc Pennington <hp@redhat.com>
8 * This software is copyrighted by the Regents of the University of
9 * California, Sun Microsystems, Inc., and other parties. The
10 * following terms apply to all files associated with the software
11 * unless explicitly disclaimed in individual files.
13 * The authors hereby grant permission to use, copy, modify,
14 * distribute, and license this software and its documentation for any
15 * purpose, provided that existing copyright notices are retained in
16 * all copies and that this notice is included verbatim in any
17 * distributions. No written agreement, license, or royalty fee is
18 * required for any of the authorized uses. Modifications to this
19 * software may be copyrighted by their authors and need not follow
20 * the licensing terms described here, provided that the new terms are
21 * clearly indicated on the first page of each file where they apply.
23 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY
24 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
25 * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION,
26 * OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
30 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
32 * NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
33 * AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
34 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
36 * GOVERNMENT USE: If you are acquiring this software on behalf of the
37 * U.S. government, the Government shall have only "Restricted Rights"
38 * in the software and related documentation as defined in the Federal
39 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
40 * are acquiring the software on behalf of the Department of Defense,
41 * the software shall be classified as "Commercial Computer Software"
42 * and the Government shall have only "Restricted Rights" as defined
43 * in Clause 252.227-7013 (c) (1) of DFARs. Notwithstanding the
44 * foregoing, the authors grant the U.S. Government and others acting
45 * in its behalf permission to use and distribute the software in
46 * accordance with the terms specified in this license.
52 #include "gtktextattributes.h"
54 #include "gtktexttag.h"
55 #include "gtktexttypes.h"
58 * gtk_text_attributes_new:
60 * Creates a #GtkTextAttributes, which describes
61 * a set of properties on some text.
63 * Return value: a new #GtkTextAttributes,
64 * free with gtk_text_attributes_unref().
67 gtk_text_attributes_new (void)
69 GtkTextAttributes *values;
71 values = g_slice_new0 (GtkTextAttributes);
73 /* 0 is a valid value for most of the struct */
76 values->language = gtk_get_default_language ();
78 values->font_scale = 1.0;
80 values->editable = TRUE;
86 * gtk_text_attributes_copy:
87 * @src: a #GtkTextAttributes to be copied
89 * Copies @src and returns a new #GtkTextAttributes.
91 * Return value: a copy of @src,
92 * free with gtk_text_attributes_unref()
95 gtk_text_attributes_copy (GtkTextAttributes *src)
97 GtkTextAttributes *dest;
99 dest = gtk_text_attributes_new ();
100 gtk_text_attributes_copy_values (src, dest);
105 G_DEFINE_BOXED_TYPE (GtkTextAttributes, gtk_text_attributes,
106 gtk_text_attributes_ref,
107 gtk_text_attributes_unref)
110 * gtk_text_attributes_copy_values:
111 * @src: a #GtkTextAttributes
112 * @dest: another #GtkTextAttributes
114 * Copies the values from @src to @dest so that @dest has
115 * the same values as @src. Frees existing values in @dest.
118 gtk_text_attributes_copy_values (GtkTextAttributes *src,
119 GtkTextAttributes *dest)
128 pango_tab_array_free (dest->tabs);
131 pango_font_description_free (dest->font);
133 if (dest->pg_bg_color)
134 gdk_color_free (dest->pg_bg_color);
136 if (dest->pg_bg_rgba)
137 gdk_rgba_free (dest->pg_bg_rgba);
139 if (dest->appearance.rgba[0])
140 gdk_rgba_free (dest->appearance.rgba[0]);
142 if (dest->appearance.rgba[1])
143 gdk_rgba_free (dest->appearance.rgba[1]);
146 orig_refcount = dest->refcount;
151 dest->tabs = pango_tab_array_copy (src->tabs);
153 dest->language = src->language;
156 dest->font = pango_font_description_copy (src->font);
158 if (src->pg_bg_color)
159 dest->pg_bg_color = gdk_color_copy (src->pg_bg_color);
162 dest->pg_bg_rgba = gdk_rgba_copy (src->pg_bg_rgba);
164 if (src->appearance.rgba[0])
165 dest->appearance.rgba[0] = gdk_rgba_copy (src->appearance.rgba[0]);
167 if (src->appearance.rgba[1])
168 dest->appearance.rgba[1] = gdk_rgba_copy (src->appearance.rgba[1]);
170 dest->refcount = orig_refcount;
174 * gtk_text_attributes_ref:
175 * @values: a #GtkTextAttributes
177 * Increments the reference count on @values.
179 * Returns: the #GtkTextAttributes that were passed in
182 gtk_text_attributes_ref (GtkTextAttributes *values)
184 g_return_val_if_fail (values != NULL, NULL);
186 values->refcount += 1;
192 * gtk_text_attributes_unref:
193 * @values: a #GtkTextAttributes
195 * Decrements the reference count on @values, freeing the structure
196 * if the reference count reaches 0.
199 gtk_text_attributes_unref (GtkTextAttributes *values)
201 g_return_if_fail (values != NULL);
202 g_return_if_fail (values->refcount > 0);
204 values->refcount -= 1;
206 if (values->refcount == 0)
209 pango_tab_array_free (values->tabs);
212 pango_font_description_free (values->font);
214 if (values->pg_bg_color)
215 gdk_color_free (values->pg_bg_color);
217 if (values->pg_bg_rgba)
218 gdk_rgba_free (values->pg_bg_rgba);
220 if (values->appearance.rgba[0])
221 gdk_rgba_free (values->appearance.rgba[0]);
223 if (values->appearance.rgba[1])
224 gdk_rgba_free (values->appearance.rgba[1]);
226 g_slice_free (GtkTextAttributes, values);
231 _gtk_text_attributes_fill_from_tags (GtkTextAttributes *dest,
237 guint left_margin_accumulative = 0;
238 guint right_margin_accumulative = 0;
242 GtkTextTag *tag = tags[n];
243 GtkTextAttributes *vals = tag->priv->values;
245 g_assert (tag->priv->table != NULL);
247 g_assert (tags[n]->priv->priority > tags[n-1]->priv->priority);
249 if (tag->priv->bg_color_set)
251 if (dest->appearance.rgba[0])
253 gdk_rgba_free (dest->appearance.rgba[0]);
254 dest->appearance.rgba[0] = NULL;
257 if (vals->appearance.rgba[0])
258 dest->appearance.rgba[0] = gdk_rgba_copy (vals->appearance.rgba[0]);
260 dest->appearance.draw_bg = TRUE;
263 if (tag->priv->fg_color_set)
265 if (dest->appearance.rgba[1])
267 gdk_rgba_free (dest->appearance.rgba[1]);
268 dest->appearance.rgba[1] = NULL;
271 if (vals->appearance.rgba[1])
272 dest->appearance.rgba[1] = gdk_rgba_copy (vals->appearance.rgba[1]);
275 if (tag->priv->pg_bg_color_set)
277 if (dest->pg_bg_rgba)
279 gdk_rgba_free (dest->pg_bg_rgba);
280 dest->pg_bg_rgba = NULL;
283 if (dest->pg_bg_color)
285 gdk_color_free (dest->pg_bg_color);
286 dest->pg_bg_color = NULL;
289 if (vals->pg_bg_rgba)
290 dest->pg_bg_rgba = gdk_rgba_copy (vals->pg_bg_rgba);
292 if (vals->pg_bg_color)
293 dest->pg_bg_color = gdk_color_copy (vals->pg_bg_color);
299 pango_font_description_merge (dest->font, vals->font, TRUE);
301 dest->font = pango_font_description_copy (vals->font);
304 /* multiply all the scales together to get a composite */
305 if (tag->priv->scale_set)
306 dest->font_scale *= vals->font_scale;
308 if (tag->priv->justification_set)
309 dest->justification = vals->justification;
311 if (vals->direction != GTK_TEXT_DIR_NONE)
312 dest->direction = vals->direction;
314 if (tag->priv->left_margin_set)
316 if (tag->priv->accumulative_margin)
317 left_margin_accumulative += vals->left_margin;
319 dest->left_margin = vals->left_margin;
322 if (tag->priv->indent_set)
323 dest->indent = vals->indent;
325 if (tag->priv->rise_set)
326 dest->appearance.rise = vals->appearance.rise;
328 if (tag->priv->right_margin_set)
330 if (tag->priv->accumulative_margin)
331 right_margin_accumulative += vals->right_margin;
333 dest->right_margin = vals->right_margin;
336 if (tag->priv->pixels_above_lines_set)
337 dest->pixels_above_lines = vals->pixels_above_lines;
339 if (tag->priv->pixels_below_lines_set)
340 dest->pixels_below_lines = vals->pixels_below_lines;
342 if (tag->priv->pixels_inside_wrap_set)
343 dest->pixels_inside_wrap = vals->pixels_inside_wrap;
345 if (tag->priv->tabs_set)
348 pango_tab_array_free (dest->tabs);
349 dest->tabs = pango_tab_array_copy (vals->tabs);
352 if (tag->priv->wrap_mode_set)
353 dest->wrap_mode = vals->wrap_mode;
355 if (tag->priv->underline_set)
356 dest->appearance.underline = vals->appearance.underline;
358 if (tag->priv->strikethrough_set)
359 dest->appearance.strikethrough = vals->appearance.strikethrough;
361 if (tag->priv->invisible_set)
362 dest->invisible = vals->invisible;
364 if (tag->priv->editable_set)
365 dest->editable = vals->editable;
367 if (tag->priv->bg_full_height_set)
368 dest->bg_full_height = vals->bg_full_height;
370 if (tag->priv->language_set)
371 dest->language = vals->language;
376 dest->left_margin += left_margin_accumulative;
377 dest->right_margin += right_margin_accumulative;
381 _gtk_text_tag_affects_size (GtkTextTag *tag)
383 GtkTextTagPrivate *priv = tag->priv;
386 (priv->values->font && pango_font_description_get_set_fields (priv->values->font) != 0) ||
388 priv->justification_set ||
389 priv->left_margin_set ||
392 priv->right_margin_set ||
393 priv->pixels_above_lines_set ||
394 priv->pixels_below_lines_set ||
395 priv->pixels_inside_wrap_set ||
397 priv->underline_set ||
398 priv->wrap_mode_set ||
403 _gtk_text_tag_affects_nonsize_appearance (GtkTextTag *tag)
405 GtkTextTagPrivate *priv = tag->priv;
408 priv->bg_color_set ||
409 priv->fg_color_set ||
410 priv->strikethrough_set ||
411 priv->bg_full_height_set ||
412 priv->pg_bg_color_set;