]> Pileus Git - ~andy/gtk/blob - gtk/gtktextattributes.c
Added internal GdkRGBA support for GtkTextTag::paragraph-background-rgba
[~andy/gtk] / gtk / gtktextattributes.c
1 /* gtktextattributes.c - text attributes
2  *
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>
7  *
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.
12  *
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.
22  *
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.
28  *
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.
35  *
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.
47  *
48  */
49
50 #include "config.h"
51
52 #include "gtktextattributes.h"
53
54 #include "gtkmainprivate.h"
55 #include "gtktexttag.h"
56 #include "gtktexttypes.h"
57
58 /**
59  * gtk_text_attributes_new:
60  * 
61  * Creates a #GtkTextAttributes, which describes
62  * a set of properties on some text.
63  * 
64  * Return value: a new #GtkTextAttributes,
65  *     free with gtk_text_attributes_unref().
66  */
67 GtkTextAttributes*
68 gtk_text_attributes_new (void)
69 {
70   GtkTextAttributes *values;
71
72   values = g_slice_new0 (GtkTextAttributes);
73
74   /* 0 is a valid value for most of the struct */
75   values->refcount = 1;
76
77   values->language = gtk_get_default_language ();
78
79   values->font_scale = 1.0;
80
81   values->editable = TRUE;
82
83   return values;
84 }
85
86 /**
87  * gtk_text_attributes_copy:
88  * @src: a #GtkTextAttributes to be copied
89  *
90  * Copies @src and returns a new #GtkTextAttributes.
91  *
92  * Return value: a copy of @src,
93  *     free with gtk_text_attributes_unref()
94  */
95 GtkTextAttributes*
96 gtk_text_attributes_copy (GtkTextAttributes *src)
97 {
98   GtkTextAttributes *dest;
99
100   dest = gtk_text_attributes_new ();
101   gtk_text_attributes_copy_values (src, dest);
102
103   return dest;
104 }
105
106 G_DEFINE_BOXED_TYPE (GtkTextAttributes, gtk_text_attributes,
107                      gtk_text_attributes_ref,
108                      gtk_text_attributes_unref)
109
110 /**
111  * gtk_text_attributes_copy_values:
112  * @src: a #GtkTextAttributes
113  * @dest: another #GtkTextAttributes
114  *
115  * Copies the values from @src to @dest so that @dest has
116  * the same values as @src. Frees existing values in @dest.
117  */
118 void
119 gtk_text_attributes_copy_values (GtkTextAttributes *src,
120                                  GtkTextAttributes *dest)
121 {
122   guint orig_refcount;
123
124   if (src == dest)
125     return;
126
127   /* Remove refs */
128   if (dest->tabs)
129     pango_tab_array_free (dest->tabs);
130
131   if (dest->font)
132     pango_font_description_free (dest->font);
133
134   if (dest->pg_bg_color)
135     gdk_color_free (dest->pg_bg_color);
136
137   if (dest->pg_bg_rgba)
138     gdk_rgba_free (dest->pg_bg_rgba);
139
140   if (dest->appearance.rgba[0])
141     gdk_rgba_free (dest->appearance.rgba[0]);
142
143   if (dest->appearance.rgba[1])
144     gdk_rgba_free (dest->appearance.rgba[1]);
145
146   /* Copy */
147   orig_refcount = dest->refcount;
148
149   *dest = *src;
150
151   if (src->tabs)
152     dest->tabs = pango_tab_array_copy (src->tabs);
153
154   dest->language = src->language;
155
156   if (src->font)
157     dest->font = pango_font_description_copy (src->font);
158
159   if (src->pg_bg_color)
160     dest->pg_bg_color = gdk_color_copy (src->pg_bg_color);
161
162   if (src->pg_bg_rgba)
163     dest->pg_bg_rgba = gdk_rgba_copy (src->pg_bg_rgba);
164
165   if (src->appearance.rgba[0])
166     dest->appearance.rgba[0] = gdk_rgba_copy (src->appearance.rgba[0]);
167
168   if (src->appearance.rgba[1])
169     dest->appearance.rgba[1] = gdk_rgba_copy (src->appearance.rgba[1]);
170
171   dest->refcount = orig_refcount;
172 }
173
174 /**
175  * gtk_text_attributes_ref:
176  * @values: a #GtkTextAttributes
177  * 
178  * Increments the reference count on @values.
179  *
180  * Returns: the #GtkTextAttributes that were passed in
181  **/
182 GtkTextAttributes *
183 gtk_text_attributes_ref (GtkTextAttributes *values)
184 {
185   g_return_val_if_fail (values != NULL, NULL);
186
187   values->refcount += 1;
188
189   return values;
190 }
191
192 /**
193  * gtk_text_attributes_unref:
194  * @values: a #GtkTextAttributes
195  * 
196  * Decrements the reference count on @values, freeing the structure
197  * if the reference count reaches 0.
198  **/
199 void
200 gtk_text_attributes_unref (GtkTextAttributes *values)
201 {
202   g_return_if_fail (values != NULL);
203   g_return_if_fail (values->refcount > 0);
204
205   values->refcount -= 1;
206
207   if (values->refcount == 0)
208     {
209       if (values->tabs)
210         pango_tab_array_free (values->tabs);
211
212       if (values->font)
213         pango_font_description_free (values->font);
214
215       if (values->pg_bg_color)
216         gdk_color_free (values->pg_bg_color);
217
218       if (values->pg_bg_rgba)
219         gdk_rgba_free (values->pg_bg_rgba);
220
221       if (values->appearance.rgba[0])
222         gdk_rgba_free (values->appearance.rgba[0]);
223
224       if (values->appearance.rgba[1])
225         gdk_rgba_free (values->appearance.rgba[1]);
226
227       g_slice_free (GtkTextAttributes, values);
228     }
229 }
230
231 void
232 _gtk_text_attributes_fill_from_tags (GtkTextAttributes *dest,
233                                      GtkTextTag**       tags,
234                                      guint              n_tags)
235 {
236   guint n = 0;
237
238   guint left_margin_accumulative = 0;
239   guint right_margin_accumulative = 0;
240
241   while (n < n_tags)
242     {
243       GtkTextTag *tag = tags[n];
244       GtkTextAttributes *vals = tag->priv->values;
245
246       g_assert (tag->priv->table != NULL);
247       if (n > 0)
248         g_assert (tags[n]->priv->priority > tags[n-1]->priv->priority);
249
250       if (tag->priv->bg_color_set)
251         {
252           if (dest->appearance.rgba[0])
253             {
254               gdk_rgba_free (dest->appearance.rgba[0]);
255               dest->appearance.rgba[0] = NULL;
256             }
257
258           if (vals->appearance.rgba[0])
259             dest->appearance.rgba[0] = gdk_rgba_copy (vals->appearance.rgba[0]);
260
261           dest->appearance.draw_bg = TRUE;
262         }
263
264       if (tag->priv->fg_color_set)
265         {
266           if (dest->appearance.rgba[1])
267             {
268               gdk_rgba_free (dest->appearance.rgba[1]);
269               dest->appearance.rgba[1] = NULL;
270             }
271
272           if (vals->appearance.rgba[1])
273             dest->appearance.rgba[1] = gdk_rgba_copy (vals->appearance.rgba[1]);
274         }
275
276       if (tag->priv->pg_bg_color_set)
277         {
278           if (dest->pg_bg_rgba)
279             {
280               gdk_rgba_free (dest->pg_bg_rgba);
281               dest->pg_bg_rgba = NULL;
282             }
283
284           if (dest->pg_bg_color)
285             {
286               gdk_color_free (dest->pg_bg_color);
287               dest->pg_bg_color = NULL;
288             }
289
290           if (vals->pg_bg_rgba)
291             dest->pg_bg_rgba = gdk_rgba_copy (vals->pg_bg_rgba);
292
293           if (vals->pg_bg_color)
294             dest->pg_bg_color = gdk_color_copy (vals->pg_bg_color);
295         }
296
297       if (vals->font)
298         {
299           if (dest->font)
300             pango_font_description_merge (dest->font, vals->font, TRUE);
301           else
302             dest->font = pango_font_description_copy (vals->font);
303         }
304
305       /* multiply all the scales together to get a composite */
306       if (tag->priv->scale_set)
307         dest->font_scale *= vals->font_scale;
308
309       if (tag->priv->justification_set)
310         dest->justification = vals->justification;
311
312       if (vals->direction != GTK_TEXT_DIR_NONE)
313         dest->direction = vals->direction;
314
315       if (tag->priv->left_margin_set)
316         {
317           if (tag->priv->accumulative_margin)
318             left_margin_accumulative += vals->left_margin;
319           else
320             dest->left_margin = vals->left_margin;
321         }
322
323       if (tag->priv->indent_set)
324         dest->indent = vals->indent;
325
326       if (tag->priv->rise_set)
327         dest->appearance.rise = vals->appearance.rise;
328
329       if (tag->priv->right_margin_set)
330         {
331           if (tag->priv->accumulative_margin)
332             right_margin_accumulative += vals->right_margin;
333           else
334             dest->right_margin = vals->right_margin;
335         }
336
337       if (tag->priv->pixels_above_lines_set)
338         dest->pixels_above_lines = vals->pixels_above_lines;
339
340       if (tag->priv->pixels_below_lines_set)
341         dest->pixels_below_lines = vals->pixels_below_lines;
342
343       if (tag->priv->pixels_inside_wrap_set)
344         dest->pixels_inside_wrap = vals->pixels_inside_wrap;
345
346       if (tag->priv->tabs_set)
347         {
348           if (dest->tabs)
349             pango_tab_array_free (dest->tabs);
350           dest->tabs = pango_tab_array_copy (vals->tabs);
351         }
352
353       if (tag->priv->wrap_mode_set)
354         dest->wrap_mode = vals->wrap_mode;
355
356       if (tag->priv->underline_set)
357         dest->appearance.underline = vals->appearance.underline;
358
359       if (tag->priv->strikethrough_set)
360         dest->appearance.strikethrough = vals->appearance.strikethrough;
361
362       if (tag->priv->invisible_set)
363         dest->invisible = vals->invisible;
364
365       if (tag->priv->editable_set)
366         dest->editable = vals->editable;
367
368       if (tag->priv->bg_full_height_set)
369         dest->bg_full_height = vals->bg_full_height;
370
371       if (tag->priv->language_set)
372         dest->language = vals->language;
373
374       ++n;
375     }
376
377   dest->left_margin += left_margin_accumulative;
378   dest->right_margin += right_margin_accumulative;
379 }
380
381 gboolean
382 _gtk_text_tag_affects_size (GtkTextTag *tag)
383 {
384   GtkTextTagPrivate *priv = tag->priv;
385
386   return
387     (priv->values->font && pango_font_description_get_set_fields (priv->values->font) != 0) ||
388     priv->scale_set ||
389     priv->justification_set ||
390     priv->left_margin_set ||
391     priv->indent_set ||
392     priv->rise_set ||
393     priv->right_margin_set ||
394     priv->pixels_above_lines_set ||
395     priv->pixels_below_lines_set ||
396     priv->pixels_inside_wrap_set ||
397     priv->tabs_set ||
398     priv->underline_set ||
399     priv->wrap_mode_set ||
400     priv->invisible_set;
401 }
402
403 gboolean
404 _gtk_text_tag_affects_nonsize_appearance (GtkTextTag *tag)
405 {
406   GtkTextTagPrivate *priv = tag->priv;
407
408   return
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;
414 }