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 "gtkmainprivate.h"
55 #include "gtktexttag.h"
56 #include "gtktexttypes.h"
59 * gtk_text_attributes_new:
61 * Creates a #GtkTextAttributes, which describes
62 * a set of properties on some text.
64 * Return value: a new #GtkTextAttributes,
65 * free with gtk_text_attributes_unref().
68 gtk_text_attributes_new (void)
70 GtkTextAttributes *values;
72 values = g_slice_new0 (GtkTextAttributes);
74 /* 0 is a valid value for most of the struct */
77 values->language = gtk_get_default_language ();
79 values->font_scale = 1.0;
81 values->editable = TRUE;
87 * gtk_text_attributes_copy:
88 * @src: a #GtkTextAttributes to be copied
90 * Copies @src and returns a new #GtkTextAttributes.
92 * Return value: a copy of @src,
93 * free with gtk_text_attributes_unref()
96 gtk_text_attributes_copy (GtkTextAttributes *src)
98 GtkTextAttributes *dest;
100 dest = gtk_text_attributes_new ();
101 gtk_text_attributes_copy_values (src, dest);
106 G_DEFINE_BOXED_TYPE (GtkTextAttributes, gtk_text_attributes,
107 gtk_text_attributes_ref,
108 gtk_text_attributes_unref)
111 * gtk_text_attributes_copy_values:
112 * @src: a #GtkTextAttributes
113 * @dest: another #GtkTextAttributes
115 * Copies the values from @src to @dest so that @dest has
116 * the same values as @src. Frees existing values in @dest.
119 gtk_text_attributes_copy_values (GtkTextAttributes *src,
120 GtkTextAttributes *dest)
129 pango_tab_array_free (dest->tabs);
132 pango_font_description_free (dest->font);
134 if (dest->pg_bg_color)
135 gdk_color_free (dest->pg_bg_color);
137 if (dest->pg_bg_rgba)
138 gdk_rgba_free (dest->pg_bg_rgba);
140 if (dest->appearance.rgba[0])
141 gdk_rgba_free (dest->appearance.rgba[0]);
143 if (dest->appearance.rgba[1])
144 gdk_rgba_free (dest->appearance.rgba[1]);
147 orig_refcount = dest->refcount;
152 dest->tabs = pango_tab_array_copy (src->tabs);
154 dest->language = src->language;
157 dest->font = pango_font_description_copy (src->font);
159 if (src->pg_bg_color)
160 dest->pg_bg_color = gdk_color_copy (src->pg_bg_color);
163 dest->pg_bg_rgba = gdk_rgba_copy (src->pg_bg_rgba);
165 if (src->appearance.rgba[0])
166 dest->appearance.rgba[0] = gdk_rgba_copy (src->appearance.rgba[0]);
168 if (src->appearance.rgba[1])
169 dest->appearance.rgba[1] = gdk_rgba_copy (src->appearance.rgba[1]);
171 dest->refcount = orig_refcount;
175 * gtk_text_attributes_ref:
176 * @values: a #GtkTextAttributes
178 * Increments the reference count on @values.
180 * Returns: the #GtkTextAttributes that were passed in
183 gtk_text_attributes_ref (GtkTextAttributes *values)
185 g_return_val_if_fail (values != NULL, NULL);
187 values->refcount += 1;
193 * gtk_text_attributes_unref:
194 * @values: a #GtkTextAttributes
196 * Decrements the reference count on @values, freeing the structure
197 * if the reference count reaches 0.
200 gtk_text_attributes_unref (GtkTextAttributes *values)
202 g_return_if_fail (values != NULL);
203 g_return_if_fail (values->refcount > 0);
205 values->refcount -= 1;
207 if (values->refcount == 0)
210 pango_tab_array_free (values->tabs);
213 pango_font_description_free (values->font);
215 if (values->pg_bg_color)
216 gdk_color_free (values->pg_bg_color);
218 if (values->pg_bg_rgba)
219 gdk_rgba_free (values->pg_bg_rgba);
221 if (values->appearance.rgba[0])
222 gdk_rgba_free (values->appearance.rgba[0]);
224 if (values->appearance.rgba[1])
225 gdk_rgba_free (values->appearance.rgba[1]);
227 g_slice_free (GtkTextAttributes, values);
232 _gtk_text_attributes_fill_from_tags (GtkTextAttributes *dest,
238 guint left_margin_accumulative = 0;
239 guint right_margin_accumulative = 0;
243 GtkTextTag *tag = tags[n];
244 GtkTextAttributes *vals = tag->priv->values;
246 g_assert (tag->priv->table != NULL);
248 g_assert (tags[n]->priv->priority > tags[n-1]->priv->priority);
250 if (tag->priv->bg_color_set)
252 if (dest->appearance.rgba[0])
254 gdk_rgba_free (dest->appearance.rgba[0]);
255 dest->appearance.rgba[0] = NULL;
258 if (vals->appearance.rgba[0])
259 dest->appearance.rgba[0] = gdk_rgba_copy (vals->appearance.rgba[0]);
261 dest->appearance.draw_bg = TRUE;
264 if (tag->priv->fg_color_set)
266 if (dest->appearance.rgba[1])
268 gdk_rgba_free (dest->appearance.rgba[1]);
269 dest->appearance.rgba[1] = NULL;
272 if (vals->appearance.rgba[1])
273 dest->appearance.rgba[1] = gdk_rgba_copy (vals->appearance.rgba[1]);
276 if (tag->priv->pg_bg_color_set)
278 if (dest->pg_bg_rgba)
280 gdk_rgba_free (dest->pg_bg_rgba);
281 dest->pg_bg_rgba = NULL;
284 if (dest->pg_bg_color)
286 gdk_color_free (dest->pg_bg_color);
287 dest->pg_bg_color = NULL;
290 if (vals->pg_bg_rgba)
291 dest->pg_bg_rgba = gdk_rgba_copy (vals->pg_bg_rgba);
293 if (vals->pg_bg_color)
294 dest->pg_bg_color = gdk_color_copy (vals->pg_bg_color);
300 pango_font_description_merge (dest->font, vals->font, TRUE);
302 dest->font = pango_font_description_copy (vals->font);
305 /* multiply all the scales together to get a composite */
306 if (tag->priv->scale_set)
307 dest->font_scale *= vals->font_scale;
309 if (tag->priv->justification_set)
310 dest->justification = vals->justification;
312 if (vals->direction != GTK_TEXT_DIR_NONE)
313 dest->direction = vals->direction;
315 if (tag->priv->left_margin_set)
317 if (tag->priv->accumulative_margin)
318 left_margin_accumulative += vals->left_margin;
320 dest->left_margin = vals->left_margin;
323 if (tag->priv->indent_set)
324 dest->indent = vals->indent;
326 if (tag->priv->rise_set)
327 dest->appearance.rise = vals->appearance.rise;
329 if (tag->priv->right_margin_set)
331 if (tag->priv->accumulative_margin)
332 right_margin_accumulative += vals->right_margin;
334 dest->right_margin = vals->right_margin;
337 if (tag->priv->pixels_above_lines_set)
338 dest->pixels_above_lines = vals->pixels_above_lines;
340 if (tag->priv->pixels_below_lines_set)
341 dest->pixels_below_lines = vals->pixels_below_lines;
343 if (tag->priv->pixels_inside_wrap_set)
344 dest->pixels_inside_wrap = vals->pixels_inside_wrap;
346 if (tag->priv->tabs_set)
349 pango_tab_array_free (dest->tabs);
350 dest->tabs = pango_tab_array_copy (vals->tabs);
353 if (tag->priv->wrap_mode_set)
354 dest->wrap_mode = vals->wrap_mode;
356 if (tag->priv->underline_set)
357 dest->appearance.underline = vals->appearance.underline;
359 if (tag->priv->strikethrough_set)
360 dest->appearance.strikethrough = vals->appearance.strikethrough;
362 if (tag->priv->invisible_set)
363 dest->invisible = vals->invisible;
365 if (tag->priv->editable_set)
366 dest->editable = vals->editable;
368 if (tag->priv->bg_full_height_set)
369 dest->bg_full_height = vals->bg_full_height;
371 if (tag->priv->language_set)
372 dest->language = vals->language;
377 dest->left_margin += left_margin_accumulative;
378 dest->right_margin += right_margin_accumulative;
382 _gtk_text_tag_affects_size (GtkTextTag *tag)
384 GtkTextTagPrivate *priv = tag->priv;
387 (priv->values->font && pango_font_description_get_set_fields (priv->values->font) != 0) ||
389 priv->justification_set ||
390 priv->left_margin_set ||
393 priv->right_margin_set ||
394 priv->pixels_above_lines_set ||
395 priv->pixels_below_lines_set ||
396 priv->pixels_inside_wrap_set ||
398 priv->underline_set ||
399 priv->wrap_mode_set ||
404 _gtk_text_tag_affects_nonsize_appearance (GtkTextTag *tag)
406 GtkTextTagPrivate *priv = tag->priv;
409 priv->bg_color_set ||
410 priv->fg_color_set ||
411 priv->strikethrough_set ||
412 priv->bg_full_height_set ||
413 priv->pg_bg_color_set;