]> Pileus Git - ~andy/gtk/blob - gtk/gtktextattributes.c
textview: Move text attributes code into its own .c file
[~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  **/
66 GtkTextAttributes*
67 gtk_text_attributes_new (void)
68 {
69   GtkTextAttributes *values;
70
71   values = g_new0 (GtkTextAttributes, 1);
72
73   /* 0 is a valid value for most of the struct */
74
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  **/
94 GtkTextAttributes*
95 gtk_text_attributes_copy (GtkTextAttributes *src)
96 {
97   GtkTextAttributes *dest;
98
99   dest = gtk_text_attributes_new ();
100   gtk_text_attributes_copy_values (src, dest);
101
102   return dest;
103 }
104
105 G_DEFINE_BOXED_TYPE (GtkTextAttributes, gtk_text_attributes,
106                      gtk_text_attributes_ref,
107                      gtk_text_attributes_unref)
108
109 /**
110  * gtk_text_attributes_copy_values:
111  * @src: a #GtkTextAttributes
112  * @dest: another #GtkTextAttributes
113  * 
114  * Copies the values from @src to @dest so that @dest has the same values
115  * as @src. Frees existing values in @dest.
116  **/
117 void
118 gtk_text_attributes_copy_values (GtkTextAttributes *src,
119                                  GtkTextAttributes *dest)
120 {
121   guint orig_refcount;
122
123   if (src == dest)
124     return;
125
126   /* Remove refs */
127
128   if (dest->font)
129     pango_font_description_free (dest->font);
130   
131   /* Copy */
132   orig_refcount = dest->refcount;
133
134   *dest = *src;
135
136   if (src->tabs)
137     dest->tabs = pango_tab_array_copy (src->tabs);
138
139   dest->language = src->language;
140
141   if (dest->font)
142     dest->font = pango_font_description_copy (src->font);
143   
144   if (src->pg_bg_color)
145     dest->pg_bg_color = gdk_color_copy (src->pg_bg_color);
146
147   dest->refcount = orig_refcount;
148 }
149
150 /**
151  * gtk_text_attributes_ref:
152  * @values: a #GtkTextAttributes
153  * 
154  * Increments the reference count on @values.
155  *
156  * Returns: the #GtkTextAttributes that were passed in
157  **/
158 GtkTextAttributes *
159 gtk_text_attributes_ref (GtkTextAttributes *values)
160 {
161   g_return_val_if_fail (values != NULL, NULL);
162
163   values->refcount += 1;
164
165   return values;
166 }
167
168 /**
169  * gtk_text_attributes_unref:
170  * @values: a #GtkTextAttributes
171  * 
172  * Decrements the reference count on @values, freeing the structure
173  * if the reference count reaches 0.
174  **/
175 void
176 gtk_text_attributes_unref (GtkTextAttributes *values)
177 {
178   g_return_if_fail (values != NULL);
179   g_return_if_fail (values->refcount > 0);
180
181   values->refcount -= 1;
182
183   if (values->refcount == 0)
184     {
185       if (values->tabs)
186         pango_tab_array_free (values->tabs);
187
188       if (values->font)
189         pango_font_description_free (values->font);
190
191       if (values->pg_bg_color)
192         gdk_color_free (values->pg_bg_color);
193
194       g_free (values);
195     }
196 }
197
198 void
199 _gtk_text_attributes_fill_from_tags (GtkTextAttributes *dest,
200                                      GtkTextTag**       tags,
201                                      guint              n_tags)
202 {
203   guint n = 0;
204
205   guint left_margin_accumulative = 0;
206   guint right_margin_accumulative = 0;
207
208   while (n < n_tags)
209     {
210       GtkTextTag *tag = tags[n];
211       GtkTextAttributes *vals = tag->priv->values;
212
213       g_assert (tag->priv->table != NULL);
214       if (n > 0)
215         g_assert (tags[n]->priv->priority > tags[n-1]->priv->priority);
216
217       if (tag->priv->bg_color_set)
218         {
219           dest->appearance.bg_color = vals->appearance.bg_color;
220
221           dest->appearance.draw_bg = TRUE;
222         }
223       if (tag->priv->fg_color_set)
224         dest->appearance.fg_color = vals->appearance.fg_color;
225
226       if (tag->priv->pg_bg_color_set)
227         {
228           dest->pg_bg_color = gdk_color_copy (vals->pg_bg_color);
229         }
230
231       if (vals->font)
232         {
233           if (dest->font)
234             pango_font_description_merge (dest->font, vals->font, TRUE);
235           else
236             dest->font = pango_font_description_copy (vals->font);
237         }
238
239       /* multiply all the scales together to get a composite */
240       if (tag->priv->scale_set)
241         dest->font_scale *= vals->font_scale;
242
243       if (tag->priv->justification_set)
244         dest->justification = vals->justification;
245
246       if (vals->direction != GTK_TEXT_DIR_NONE)
247         dest->direction = vals->direction;
248
249       if (tag->priv->left_margin_set)
250         {
251           if (tag->priv->accumulative_margin)
252             left_margin_accumulative += vals->left_margin;
253           else
254             dest->left_margin = vals->left_margin;
255         }
256
257       if (tag->priv->indent_set)
258         dest->indent = vals->indent;
259
260       if (tag->priv->rise_set)
261         dest->appearance.rise = vals->appearance.rise;
262
263       if (tag->priv->right_margin_set)
264         {
265           if (tag->priv->accumulative_margin)
266             right_margin_accumulative += vals->right_margin;
267           else
268             dest->right_margin = vals->right_margin;
269         }
270
271       if (tag->priv->pixels_above_lines_set)
272         dest->pixels_above_lines = vals->pixels_above_lines;
273
274       if (tag->priv->pixels_below_lines_set)
275         dest->pixels_below_lines = vals->pixels_below_lines;
276
277       if (tag->priv->pixels_inside_wrap_set)
278         dest->pixels_inside_wrap = vals->pixels_inside_wrap;
279
280       if (tag->priv->tabs_set)
281         {
282           if (dest->tabs)
283             pango_tab_array_free (dest->tabs);
284           dest->tabs = pango_tab_array_copy (vals->tabs);
285         }
286
287       if (tag->priv->wrap_mode_set)
288         dest->wrap_mode = vals->wrap_mode;
289
290       if (tag->priv->underline_set)
291         dest->appearance.underline = vals->appearance.underline;
292
293       if (tag->priv->strikethrough_set)
294         dest->appearance.strikethrough = vals->appearance.strikethrough;
295
296       if (tag->priv->invisible_set)
297         dest->invisible = vals->invisible;
298
299       if (tag->priv->editable_set)
300         dest->editable = vals->editable;
301
302       if (tag->priv->bg_full_height_set)
303         dest->bg_full_height = vals->bg_full_height;
304
305       if (tag->priv->language_set)
306         dest->language = vals->language;
307
308       ++n;
309     }
310
311   dest->left_margin += left_margin_accumulative;
312   dest->right_margin += right_margin_accumulative;
313 }
314
315 gboolean
316 _gtk_text_tag_affects_size (GtkTextTag *tag)
317 {
318   GtkTextTagPrivate *priv = tag->priv;
319
320   return
321     (priv->values->font && pango_font_description_get_set_fields (priv->values->font) != 0) ||
322     priv->scale_set ||
323     priv->justification_set ||
324     priv->left_margin_set ||
325     priv->indent_set ||
326     priv->rise_set ||
327     priv->right_margin_set ||
328     priv->pixels_above_lines_set ||
329     priv->pixels_below_lines_set ||
330     priv->pixels_inside_wrap_set ||
331     priv->tabs_set ||
332     priv->underline_set ||
333     priv->wrap_mode_set ||
334     priv->invisible_set;
335 }
336
337 gboolean
338 _gtk_text_tag_affects_nonsize_appearance (GtkTextTag *tag)
339 {
340   GtkTextTagPrivate *priv = tag->priv;
341
342   return
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;
348 }