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
67 gtk_text_attributes_new (void)
69 GtkTextAttributes *values;
71 values = g_new0 (GtkTextAttributes, 1);
73 /* 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
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 the same values
115 * as @src. Frees existing values in @dest.
118 gtk_text_attributes_copy_values (GtkTextAttributes *src,
119 GtkTextAttributes *dest)
129 pango_font_description_free (dest->font);
132 orig_refcount = dest->refcount;
137 dest->tabs = pango_tab_array_copy (src->tabs);
139 dest->language = src->language;
142 dest->font = pango_font_description_copy (src->font);
144 if (src->pg_bg_color)
145 dest->pg_bg_color = gdk_color_copy (src->pg_bg_color);
147 dest->refcount = orig_refcount;
151 * gtk_text_attributes_ref:
152 * @values: a #GtkTextAttributes
154 * Increments the reference count on @values.
156 * Returns: the #GtkTextAttributes that were passed in
159 gtk_text_attributes_ref (GtkTextAttributes *values)
161 g_return_val_if_fail (values != NULL, NULL);
163 values->refcount += 1;
169 * gtk_text_attributes_unref:
170 * @values: a #GtkTextAttributes
172 * Decrements the reference count on @values, freeing the structure
173 * if the reference count reaches 0.
176 gtk_text_attributes_unref (GtkTextAttributes *values)
178 g_return_if_fail (values != NULL);
179 g_return_if_fail (values->refcount > 0);
181 values->refcount -= 1;
183 if (values->refcount == 0)
186 pango_tab_array_free (values->tabs);
189 pango_font_description_free (values->font);
191 if (values->pg_bg_color)
192 gdk_color_free (values->pg_bg_color);
199 _gtk_text_attributes_fill_from_tags (GtkTextAttributes *dest,
205 guint left_margin_accumulative = 0;
206 guint right_margin_accumulative = 0;
210 GtkTextTag *tag = tags[n];
211 GtkTextAttributes *vals = tag->priv->values;
213 g_assert (tag->priv->table != NULL);
215 g_assert (tags[n]->priv->priority > tags[n-1]->priv->priority);
217 if (tag->priv->bg_color_set)
219 dest->appearance.bg_color = vals->appearance.bg_color;
221 dest->appearance.draw_bg = TRUE;
223 if (tag->priv->fg_color_set)
224 dest->appearance.fg_color = vals->appearance.fg_color;
226 if (tag->priv->pg_bg_color_set)
228 dest->pg_bg_color = gdk_color_copy (vals->pg_bg_color);
234 pango_font_description_merge (dest->font, vals->font, TRUE);
236 dest->font = pango_font_description_copy (vals->font);
239 /* multiply all the scales together to get a composite */
240 if (tag->priv->scale_set)
241 dest->font_scale *= vals->font_scale;
243 if (tag->priv->justification_set)
244 dest->justification = vals->justification;
246 if (vals->direction != GTK_TEXT_DIR_NONE)
247 dest->direction = vals->direction;
249 if (tag->priv->left_margin_set)
251 if (tag->priv->accumulative_margin)
252 left_margin_accumulative += vals->left_margin;
254 dest->left_margin = vals->left_margin;
257 if (tag->priv->indent_set)
258 dest->indent = vals->indent;
260 if (tag->priv->rise_set)
261 dest->appearance.rise = vals->appearance.rise;
263 if (tag->priv->right_margin_set)
265 if (tag->priv->accumulative_margin)
266 right_margin_accumulative += vals->right_margin;
268 dest->right_margin = vals->right_margin;
271 if (tag->priv->pixels_above_lines_set)
272 dest->pixels_above_lines = vals->pixels_above_lines;
274 if (tag->priv->pixels_below_lines_set)
275 dest->pixels_below_lines = vals->pixels_below_lines;
277 if (tag->priv->pixels_inside_wrap_set)
278 dest->pixels_inside_wrap = vals->pixels_inside_wrap;
280 if (tag->priv->tabs_set)
283 pango_tab_array_free (dest->tabs);
284 dest->tabs = pango_tab_array_copy (vals->tabs);
287 if (tag->priv->wrap_mode_set)
288 dest->wrap_mode = vals->wrap_mode;
290 if (tag->priv->underline_set)
291 dest->appearance.underline = vals->appearance.underline;
293 if (tag->priv->strikethrough_set)
294 dest->appearance.strikethrough = vals->appearance.strikethrough;
296 if (tag->priv->invisible_set)
297 dest->invisible = vals->invisible;
299 if (tag->priv->editable_set)
300 dest->editable = vals->editable;
302 if (tag->priv->bg_full_height_set)
303 dest->bg_full_height = vals->bg_full_height;
305 if (tag->priv->language_set)
306 dest->language = vals->language;
311 dest->left_margin += left_margin_accumulative;
312 dest->right_margin += right_margin_accumulative;
316 _gtk_text_tag_affects_size (GtkTextTag *tag)
318 GtkTextTagPrivate *priv = tag->priv;
321 (priv->values->font && pango_font_description_get_set_fields (priv->values->font) != 0) ||
323 priv->justification_set ||
324 priv->left_margin_set ||
327 priv->right_margin_set ||
328 priv->pixels_above_lines_set ||
329 priv->pixels_below_lines_set ||
330 priv->pixels_inside_wrap_set ||
332 priv->underline_set ||
333 priv->wrap_mode_set ||
338 _gtk_text_tag_affects_nonsize_appearance (GtkTextTag *tag)
340 GtkTextTagPrivate *priv = tag->priv;
343 priv->bg_color_set ||
344 priv->fg_color_set ||
345 priv->strikethrough_set ||
346 priv->bg_full_height_set ||
347 priv->pg_bg_color_set;