]> Pileus Git - ~andy/gtk/blob - gtk/gtktexttag.c
Add a way to specify accumulative margins. (#344499, Nate Nielsen)
[~andy/gtk] / gtk / gtktexttag.c
1 /* gtktexttag.c - text tag object
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 #include "gtkmain.h"
52 #include "gtktexttag.h"
53 #include "gtktexttypes.h"
54 #include "gtktexttagtable.h"
55 #include "gtkintl.h"
56 #include "gtkmarshalers.h"
57 #include "gtkprivate.h"
58 #include "gtkalias.h"
59
60 #include <stdlib.h>
61 #include <string.h>
62
63 enum {
64   EVENT,
65   LAST_SIGNAL
66 };
67
68 enum {
69   PROP_0,
70   /* Construct args */
71   PROP_NAME,
72
73   /* Style args */
74   PROP_BACKGROUND,
75   PROP_FOREGROUND,
76   PROP_BACKGROUND_GDK,
77   PROP_FOREGROUND_GDK,
78   PROP_BACKGROUND_STIPPLE,
79   PROP_FOREGROUND_STIPPLE,
80   PROP_FONT,
81   PROP_FONT_DESC,
82   PROP_FAMILY,
83   PROP_STYLE,
84   PROP_VARIANT,
85   PROP_WEIGHT,
86   PROP_STRETCH,
87   PROP_SIZE,
88   PROP_SIZE_POINTS,
89   PROP_SCALE,
90   PROP_PIXELS_ABOVE_LINES,
91   PROP_PIXELS_BELOW_LINES,
92   PROP_PIXELS_INSIDE_WRAP,
93   PROP_EDITABLE,
94   PROP_WRAP_MODE,
95   PROP_JUSTIFICATION,
96   PROP_DIRECTION,
97   PROP_LEFT_MARGIN,
98   PROP_INDENT,
99   PROP_STRIKETHROUGH,
100   PROP_RIGHT_MARGIN,
101   PROP_UNDERLINE,
102   PROP_RISE,
103   PROP_BACKGROUND_FULL_HEIGHT,
104   PROP_LANGUAGE,
105   PROP_TABS,
106   PROP_INVISIBLE,
107   PROP_PARAGRAPH_BACKGROUND,
108   PROP_PARAGRAPH_BACKGROUND_GDK,
109
110   /* Behavior args */
111   PROP_ACCUMULATIVE_MARGIN,
112   
113   /* Whether-a-style-arg-is-set args */
114   PROP_BACKGROUND_SET,
115   PROP_FOREGROUND_SET,
116   PROP_BACKGROUND_STIPPLE_SET,
117   PROP_FOREGROUND_STIPPLE_SET,
118   PROP_FAMILY_SET,
119   PROP_STYLE_SET,
120   PROP_VARIANT_SET,
121   PROP_WEIGHT_SET,
122   PROP_STRETCH_SET,
123   PROP_SIZE_SET,
124   PROP_SCALE_SET,
125   PROP_PIXELS_ABOVE_LINES_SET,
126   PROP_PIXELS_BELOW_LINES_SET,
127   PROP_PIXELS_INSIDE_WRAP_SET,
128   PROP_EDITABLE_SET,
129   PROP_WRAP_MODE_SET,
130   PROP_JUSTIFICATION_SET,
131   PROP_LEFT_MARGIN_SET,
132   PROP_INDENT_SET,
133   PROP_STRIKETHROUGH_SET,
134   PROP_RIGHT_MARGIN_SET,
135   PROP_UNDERLINE_SET,
136   PROP_RISE_SET,
137   PROP_BACKGROUND_FULL_HEIGHT_SET,
138   PROP_LANGUAGE_SET,
139   PROP_TABS_SET,
140   PROP_INVISIBLE_SET,
141   PROP_PARAGRAPH_BACKGROUND_SET,
142
143   LAST_ARG
144 };
145 static void gtk_text_tag_finalize     (GObject         *object);
146 static void gtk_text_tag_set_property (GObject         *object,
147                                        guint            prop_id,
148                                        const GValue    *value,
149                                        GParamSpec      *pspec);
150 static void gtk_text_tag_get_property (GObject         *object,
151                                        guint            prop_id,
152                                        GValue          *value,
153                                        GParamSpec      *pspec);
154
155 static guint signals[LAST_SIGNAL] = { 0 };
156
157 G_DEFINE_TYPE (GtkTextTag, gtk_text_tag, G_TYPE_OBJECT)
158
159 static void
160 gtk_text_tag_class_init (GtkTextTagClass *klass)
161 {
162   GObjectClass *object_class = G_OBJECT_CLASS (klass);
163
164   object_class->set_property = gtk_text_tag_set_property;
165   object_class->get_property = gtk_text_tag_get_property;
166   
167   object_class->finalize = gtk_text_tag_finalize;
168
169   /* Construct */
170   g_object_class_install_property (object_class,
171                                    PROP_NAME,
172                                    g_param_spec_string ("name",
173                                                         P_("Tag name"),
174                                                         P_("Name used to refer to the text tag. NULL for anonymous tags"),
175                                                         NULL,
176                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
177
178   /* Style args */
179
180   g_object_class_install_property (object_class,
181                                    PROP_BACKGROUND,
182                                    g_param_spec_string ("background",
183                                                         P_("Background color name"),
184                                                         P_("Background color as a string"),
185                                                         NULL,
186                                                         GTK_PARAM_WRITABLE));
187
188   g_object_class_install_property (object_class,
189                                    PROP_BACKGROUND_GDK,
190                                    g_param_spec_boxed ("background-gdk",
191                                                        P_("Background color"),
192                                                        P_("Background color as a (possibly unallocated) GdkColor"),
193                                                        GDK_TYPE_COLOR,
194                                                        GTK_PARAM_READWRITE));
195
196   g_object_class_install_property (object_class,
197                                    PROP_BACKGROUND_FULL_HEIGHT,
198                                    g_param_spec_boolean ("background-full-height",
199                                                          P_("Background full height"),
200                                                          P_("Whether the background color fills the entire line height or only the height of the tagged characters"),
201                                                          FALSE,
202                                                          GTK_PARAM_READWRITE));
203
204   
205   g_object_class_install_property (object_class,
206                                    PROP_BACKGROUND_STIPPLE,
207                                    g_param_spec_object ("background-stipple",
208                                                         P_("Background stipple mask"),
209                                                         P_("Bitmap to use as a mask when drawing the text background"),
210                                                         GDK_TYPE_PIXMAP,
211                                                         GTK_PARAM_READWRITE));  
212
213
214   g_object_class_install_property (object_class,
215                                    PROP_FOREGROUND,
216                                    g_param_spec_string ("foreground",
217                                                         P_("Foreground color name"),
218                                                         P_("Foreground color as a string"),
219                                                         NULL,
220                                                         GTK_PARAM_WRITABLE));
221
222   g_object_class_install_property (object_class,
223                                    PROP_FOREGROUND_GDK,
224                                    g_param_spec_boxed ("foreground-gdk",
225                                                        P_("Foreground color"),
226                                                        P_("Foreground color as a (possibly unallocated) GdkColor"),
227                                                        GDK_TYPE_COLOR,
228                                                        GTK_PARAM_READWRITE));
229
230   
231   g_object_class_install_property (object_class,
232                                    PROP_FOREGROUND_STIPPLE,
233                                    g_param_spec_object ("foreground-stipple",
234                                                         P_("Foreground stipple mask"),
235                                                         P_("Bitmap to use as a mask when drawing the text foreground"),
236                                                         GDK_TYPE_PIXMAP,
237                                                         GTK_PARAM_READWRITE));  
238   
239   g_object_class_install_property (object_class,
240                                    PROP_DIRECTION,
241                                    g_param_spec_enum ("direction",
242                                                       P_("Text direction"),
243                                                       P_("Text direction, e.g. right-to-left or left-to-right"),
244                                                       GTK_TYPE_TEXT_DIRECTION,
245                                                       GTK_TEXT_DIR_NONE,
246                                                       GTK_PARAM_READWRITE));
247
248   g_object_class_install_property (object_class,
249                                    PROP_EDITABLE,
250                                    g_param_spec_boolean ("editable",
251                                                          P_("Editable"),
252                                                          P_("Whether the text can be modified by the user"),
253                                                          TRUE,
254                                                          GTK_PARAM_READWRITE));
255
256   /**
257    * GtkTextTag:font:
258    *
259    * Font description as string, e.g. \"Sans Italic 12\". 
260    *
261    * Note that the initial value of this property depends on
262    * the internals of #PangoFontDescription.
263    */
264   g_object_class_install_property (object_class,
265                                    PROP_FONT,
266                                    g_param_spec_string ("font",
267                                                         P_("Font"),
268                                                         P_("Font description as a string, e.g. \"Sans Italic 12\""),
269                                                         NULL,
270                                                         GTK_PARAM_READWRITE));
271
272   g_object_class_install_property (object_class,
273                                    PROP_FONT_DESC,
274                                    g_param_spec_boxed ("font-desc",
275                                                        P_("Font"),
276                                                        P_("Font description as a PangoFontDescription struct"),
277                                                        PANGO_TYPE_FONT_DESCRIPTION,
278                                                        GTK_PARAM_READWRITE));
279   
280   g_object_class_install_property (object_class,
281                                    PROP_FAMILY,
282                                    g_param_spec_string ("family",
283                                                         P_("Font family"),
284                                                         P_("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
285                                                         NULL,
286                                                         GTK_PARAM_READWRITE));
287
288   g_object_class_install_property (object_class,
289                                    PROP_STYLE,
290                                    g_param_spec_enum ("style",
291                                                       P_("Font style"),
292                                                       P_("Font style as a PangoStyle, e.g. PANGO_STYLE_ITALIC"),
293                                                       PANGO_TYPE_STYLE,
294                                                       PANGO_STYLE_NORMAL,
295                                                       GTK_PARAM_READWRITE));
296
297   g_object_class_install_property (object_class,
298                                    PROP_VARIANT,
299                                    g_param_spec_enum ("variant",
300                                                      P_("Font variant"),
301                                                      P_("Font variant as a PangoVariant, e.g. PANGO_VARIANT_SMALL_CAPS"),
302                                                       PANGO_TYPE_VARIANT,
303                                                       PANGO_VARIANT_NORMAL,
304                                                       GTK_PARAM_READWRITE));
305   
306   g_object_class_install_property (object_class,
307                                    PROP_WEIGHT,
308                                    g_param_spec_int ("weight",
309                                                      P_("Font weight"),
310                                                      P_("Font weight as an integer, see predefined values in PangoWeight; for example, PANGO_WEIGHT_BOLD"),
311                                                      0,
312                                                      G_MAXINT,
313                                                      PANGO_WEIGHT_NORMAL,
314                                                      GTK_PARAM_READWRITE));
315   
316
317   g_object_class_install_property (object_class,
318                                    PROP_STRETCH,
319                                    g_param_spec_enum ("stretch",
320                                                       P_("Font stretch"),
321                                                       P_("Font stretch as a PangoStretch, e.g. PANGO_STRETCH_CONDENSED"),
322                                                       PANGO_TYPE_STRETCH,
323                                                       PANGO_STRETCH_NORMAL,
324                                                       GTK_PARAM_READWRITE));
325   
326   g_object_class_install_property (object_class,
327                                    PROP_SIZE,
328                                    g_param_spec_int ("size",
329                                                      P_("Font size"),
330                                                      P_("Font size in Pango units"),
331                                                      0,
332                                                      G_MAXINT,
333                                                      0,
334                                                      GTK_PARAM_READWRITE));
335
336   g_object_class_install_property (object_class,
337                                    PROP_SCALE,
338                                    g_param_spec_double ("scale",
339                                                         P_("Font scale"),
340                                                         P_("Font size as a scale factor relative to the default font size. This properly adapts to theme changes etc. so is recommended. Pango predefines some scales such as PANGO_SCALE_X_LARGE"),
341                                                         0.0,
342                                                         G_MAXDOUBLE,
343                                                         1.0,
344                                                         GTK_PARAM_READWRITE));
345   
346   g_object_class_install_property (object_class,
347                                    PROP_SIZE_POINTS,
348                                    g_param_spec_double ("size-points",
349                                                         P_("Font points"),
350                                                         P_("Font size in points"),
351                                                         0.0,
352                                                         G_MAXDOUBLE,
353                                                         0.0,
354                                                         GTK_PARAM_READWRITE));  
355
356   g_object_class_install_property (object_class,
357                                    PROP_JUSTIFICATION,
358                                    g_param_spec_enum ("justification",
359                                                       P_("Justification"),
360                                                       P_("Left, right, or center justification"),
361                                                       GTK_TYPE_JUSTIFICATION,
362                                                       GTK_JUSTIFY_LEFT,
363                                                       GTK_PARAM_READWRITE));
364
365   /**
366    * GtkTextTag:language:
367    *
368    * The language this text is in, as an ISO code. Pango can use this as a 
369    * hint when rendering the text. If not set, an appropriate default will be 
370    * used.
371    *
372    * Note that the initial value of this property depends on the current
373    * locale, see also gtk_get_default_language().
374    */
375   g_object_class_install_property (object_class,
376                                    PROP_LANGUAGE,
377                                    g_param_spec_string ("language",
378                                                         P_("Language"),
379                                                         P_("The language this text is in, as an ISO code. Pango can use this as a hint when rendering the text. If not set, an appropriate default will be used."),
380                                                         NULL,
381                                                         GTK_PARAM_READWRITE));  
382
383   g_object_class_install_property (object_class,
384                                    PROP_LEFT_MARGIN,
385                                    g_param_spec_int ("left-margin",
386                                                      P_("Left margin"),
387                                                      P_("Width of the left margin in pixels"),
388                                                      0,
389                                                      G_MAXINT,
390                                                      0,
391                                                      GTK_PARAM_READWRITE));
392
393   g_object_class_install_property (object_class,
394                                    PROP_RIGHT_MARGIN,
395                                    g_param_spec_int ("right-margin",
396                                                      P_("Right margin"),
397                                                      P_("Width of the right margin in pixels"),
398                                                      0,
399                                                      G_MAXINT,
400                                                      0,
401                                                      GTK_PARAM_READWRITE));
402
403   
404   g_object_class_install_property (object_class,
405                                    PROP_INDENT,
406                                    g_param_spec_int ("indent",
407                                                      P_("Indent"),
408                                                      P_("Amount to indent the paragraph, in pixels"),
409                                                      G_MININT,
410                                                      G_MAXINT,
411                                                      0,
412                                                      GTK_PARAM_READWRITE));
413
414   
415   g_object_class_install_property (object_class,
416                                    PROP_RISE,
417                                    g_param_spec_int ("rise",
418                                                      P_("Rise"),
419                                                      P_("Offset of text above the baseline (below the baseline if rise is negative) in Pango units"),
420                                                      G_MININT,
421                                                      G_MAXINT,
422                                                      0,
423                                                      GTK_PARAM_READWRITE));
424
425   g_object_class_install_property (object_class,
426                                    PROP_PIXELS_ABOVE_LINES,
427                                    g_param_spec_int ("pixels-above-lines",
428                                                      P_("Pixels above lines"),
429                                                      P_("Pixels of blank space above paragraphs"),
430                                                      0,
431                                                      G_MAXINT,
432                                                      0,
433                                                      GTK_PARAM_READWRITE));
434   
435   g_object_class_install_property (object_class,
436                                    PROP_PIXELS_BELOW_LINES,
437                                    g_param_spec_int ("pixels-below-lines",
438                                                      P_("Pixels below lines"),
439                                                      P_("Pixels of blank space below paragraphs"),
440                                                      0,
441                                                      G_MAXINT,
442                                                      0,
443                                                      GTK_PARAM_READWRITE));
444
445   g_object_class_install_property (object_class,
446                                    PROP_PIXELS_INSIDE_WRAP,
447                                    g_param_spec_int ("pixels-inside-wrap",
448                                                      P_("Pixels inside wrap"),
449                                                      P_("Pixels of blank space between wrapped lines in a paragraph"),
450                                                      0,
451                                                      G_MAXINT,
452                                                      0,
453                                                      GTK_PARAM_READWRITE));
454
455   g_object_class_install_property (object_class,
456                                    PROP_STRIKETHROUGH,
457                                    g_param_spec_boolean ("strikethrough",
458                                                          P_("Strikethrough"),
459                                                          P_("Whether to strike through the text"),
460                                                          FALSE,
461                                                          GTK_PARAM_READWRITE));
462   
463   g_object_class_install_property (object_class,
464                                    PROP_UNDERLINE,
465                                    g_param_spec_enum ("underline",
466                                                       P_("Underline"),
467                                                       P_("Style of underline for this text"),
468                                                       PANGO_TYPE_UNDERLINE,
469                                                       PANGO_UNDERLINE_NONE,
470                                                       GTK_PARAM_READWRITE));
471   
472   g_object_class_install_property (object_class,
473                                    PROP_WRAP_MODE,
474                                    g_param_spec_enum ("wrap-mode",
475                                                      P_("Wrap mode"),
476                                                      P_("Whether to wrap lines never, at word boundaries, or at character boundaries"),
477                                                       GTK_TYPE_WRAP_MODE,
478                                                       GTK_WRAP_NONE,
479                                                       GTK_PARAM_READWRITE));
480   
481
482   g_object_class_install_property (object_class,
483                                    PROP_TABS,
484                                    g_param_spec_boxed ("tabs",
485                                                        P_("Tabs"),
486                                                        P_("Custom tabs for this text"),
487                                                        PANGO_TYPE_TAB_ARRAY,
488                                                        GTK_PARAM_READWRITE));
489   
490   /**
491    * GtkTextTag:invisible:
492    *
493    * Whether this text is hidden.
494    *
495    * Note that there may still be problems with the support for invisible 
496    * text, in particular when navigating programmatically inside a buffer
497    * containing invisible segments. 
498    *
499    * Since: 2.8
500    */
501   g_object_class_install_property (object_class,
502                                    PROP_INVISIBLE,
503                                    g_param_spec_boolean ("invisible",
504                                                          P_("Invisible"),
505                                                          P_("Whether this text is hidden."),
506                                                          FALSE,
507                                                          GTK_PARAM_READWRITE));
508
509   /**
510    * GtkTextTag:paragraph-background:
511    *
512    * The paragraph background color as a string.
513    *
514    * Since: 2.8
515    */
516   g_object_class_install_property (object_class,
517                                    PROP_PARAGRAPH_BACKGROUND,
518                                    g_param_spec_string ("paragraph-background",
519                                                         P_("Paragraph background color name"),
520                                                         P_("Paragraph background color as a string"),
521                                                         NULL,
522                                                         GTK_PARAM_WRITABLE));
523
524   /**
525    * GtkTextTag:paragraph-background-gdk:
526    *
527    * The paragraph background color as a as a (possibly unallocated) 
528    * #GdkColor.
529    *
530    * Since: 2.8
531    */
532   g_object_class_install_property (object_class,
533                                    PROP_PARAGRAPH_BACKGROUND_GDK,
534                                    g_param_spec_boxed ("paragraph-background-gdk",
535                                                        P_("Paragraph background color"),
536                                                        P_("Paragraph background color as a (possibly unallocated) GdkColor"),
537                                                        GDK_TYPE_COLOR,
538                                                        GTK_PARAM_READWRITE));
539
540   /**
541    * GtkTextTag:accumulative-margin:
542    *
543    * Whether the margins accumulate or override each other.
544    *
545    * When set to %TRUE the margins of this tag are added to the margins 
546    * of any other non-accumulative margins present. When set to %FALSE 
547    * the margins override one another (the default).
548    *
549    * Since: 2.12
550    */
551   g_object_class_install_property (object_class,
552                                    PROP_ACCUMULATIVE_MARGIN,
553                                    g_param_spec_boolean ("accumulative-margin",
554                                                          P_("Margin Accumulates"),
555                                                          P_("Whether left and right margins accumulate."),
556                                                          FALSE,
557                                                          GTK_PARAM_READWRITE));
558
559   /* Style props are set or not */
560
561 #define ADD_SET_PROP(propname, propval, nick, blurb) g_object_class_install_property (object_class, propval, g_param_spec_boolean (propname, nick, blurb, FALSE, GTK_PARAM_READWRITE))
562
563   ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
564                 P_("Background set"),
565                 P_("Whether this tag affects the background color"));
566   
567   ADD_SET_PROP ("background-full-height-set", PROP_BACKGROUND_FULL_HEIGHT_SET,
568                 P_("Background full height set"),
569                 P_("Whether this tag affects background height"));
570
571   ADD_SET_PROP ("background-stipple-set", PROP_BACKGROUND_STIPPLE_SET,
572                 P_("Background stipple set"),
573                 P_("Whether this tag affects the background stipple"));  
574
575   ADD_SET_PROP ("foreground-set", PROP_FOREGROUND_SET,
576                 P_("Foreground set"),
577                 P_("Whether this tag affects the foreground color"));
578
579   ADD_SET_PROP ("foreground-stipple-set", PROP_FOREGROUND_STIPPLE_SET,
580                 P_("Foreground stipple set"),
581                 P_("Whether this tag affects the foreground stipple"));
582   
583   ADD_SET_PROP ("editable-set", PROP_EDITABLE_SET,
584                 P_("Editability set"),
585                 P_("Whether this tag affects text editability"));
586
587   ADD_SET_PROP ("family-set", PROP_FAMILY_SET,
588                 P_("Font family set"),
589                 P_("Whether this tag affects the font family"));  
590
591   ADD_SET_PROP ("style-set", PROP_STYLE_SET,
592                 P_("Font style set"),
593                 P_("Whether this tag affects the font style"));
594
595   ADD_SET_PROP ("variant-set", PROP_VARIANT_SET,
596                 P_("Font variant set"),
597                 P_("Whether this tag affects the font variant"));
598
599   ADD_SET_PROP ("weight-set", PROP_WEIGHT_SET,
600                 P_("Font weight set"),
601                 P_("Whether this tag affects the font weight"));
602
603   ADD_SET_PROP ("stretch-set", PROP_STRETCH_SET,
604                 P_("Font stretch set"),
605                 P_("Whether this tag affects the font stretch"));
606
607   ADD_SET_PROP ("size-set", PROP_SIZE_SET,
608                 P_("Font size set"),
609                 P_("Whether this tag affects the font size"));
610
611   ADD_SET_PROP ("scale-set", PROP_SCALE_SET,
612                 P_("Font scale set"),
613                 P_("Whether this tag scales the font size by a factor"));
614   
615   ADD_SET_PROP ("justification-set", PROP_JUSTIFICATION_SET,
616                 P_("Justification set"),
617                 P_("Whether this tag affects paragraph justification"));
618   
619   ADD_SET_PROP ("language-set", PROP_LANGUAGE_SET,
620                 P_("Language set"),
621                 P_("Whether this tag affects the language the text is rendered as"));
622
623   ADD_SET_PROP ("left-margin-set", PROP_LEFT_MARGIN_SET,
624                 P_("Left margin set"),
625                 P_("Whether this tag affects the left margin"));
626
627   ADD_SET_PROP ("indent-set", PROP_INDENT_SET,
628                 P_("Indent set"),
629                 P_("Whether this tag affects indentation"));
630
631   ADD_SET_PROP ("rise-set", PROP_RISE_SET,
632                 P_("Rise set"),
633                 P_("Whether this tag affects the rise"));
634
635   ADD_SET_PROP ("pixels-above-lines-set", PROP_PIXELS_ABOVE_LINES_SET,
636                 P_("Pixels above lines set"),
637                 P_("Whether this tag affects the number of pixels above lines"));
638
639   ADD_SET_PROP ("pixels-below-lines-set", PROP_PIXELS_BELOW_LINES_SET,
640                 P_("Pixels below lines set"),
641                 P_("Whether this tag affects the number of pixels above lines"));
642
643   ADD_SET_PROP ("pixels-inside-wrap-set", PROP_PIXELS_INSIDE_WRAP_SET,
644                 P_("Pixels inside wrap set"),
645                 P_("Whether this tag affects the number of pixels between wrapped lines"));
646
647   ADD_SET_PROP ("strikethrough-set", PROP_STRIKETHROUGH_SET,
648                 P_("Strikethrough set"),
649                 P_("Whether this tag affects strikethrough"));
650   
651   ADD_SET_PROP ("right-margin-set", PROP_RIGHT_MARGIN_SET,
652                 P_("Right margin set"),
653                 P_("Whether this tag affects the right margin"));
654
655   ADD_SET_PROP ("underline-set", PROP_UNDERLINE_SET,
656                 P_("Underline set"),
657                 P_("Whether this tag affects underlining"));
658
659   ADD_SET_PROP ("wrap-mode-set", PROP_WRAP_MODE_SET,
660                 P_("Wrap mode set"),
661                 P_("Whether this tag affects line wrap mode"));
662
663   ADD_SET_PROP ("tabs-set", PROP_TABS_SET,
664                 P_("Tabs set"),
665                 P_("Whether this tag affects tabs"));
666
667   ADD_SET_PROP ("invisible-set", PROP_INVISIBLE_SET,
668                 P_("Invisible set"),
669                 P_("Whether this tag affects text visibility"));
670
671   ADD_SET_PROP ("paragraph-background-set", PROP_PARAGRAPH_BACKGROUND_SET,
672                 P_("Paragraph background set"),
673                 P_("Whether this tag affects the paragraph background color"));
674
675   /**
676    * GtkTextTag::event:
677    * @tag: the #GtkTextTag on which the signal is emitted
678    * @object: the object the event was fired from (typically a #GtkTextView)
679    * @event: the event which triggered the signal
680    * @iter: a #GtkTextIter pointing at the location the event occured
681    *
682    * The ::event signal is emitted when an event occurs on a region of the
683    * buffer marked with this tag.
684    *
685    * Returns: %TRUE to stop other handlers from being invoked for the
686    * event. %FALSE to propagate the event further.
687    */
688   signals[EVENT] =
689     g_signal_new (I_("event"),
690                   G_OBJECT_CLASS_TYPE (object_class),
691                   G_SIGNAL_RUN_LAST,
692                   G_STRUCT_OFFSET (GtkTextTagClass, event),
693                   _gtk_boolean_handled_accumulator, NULL,
694                   _gtk_marshal_BOOLEAN__OBJECT_BOXED_BOXED,
695                   G_TYPE_BOOLEAN,
696                   3,
697                   G_TYPE_OBJECT,
698                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE,
699                   GTK_TYPE_TEXT_ITER);
700 }
701
702 static void
703 gtk_text_tag_init (GtkTextTag *text_tag)
704 {
705   text_tag->values = gtk_text_attributes_new ();
706 }
707
708 /**
709  * gtk_text_tag_new:
710  * @name: tag name, or %NULL
711  * 
712  * Creates a #GtkTextTag. Configure the tag using object arguments,
713  * i.e. using g_object_set().
714  * 
715  * Return value: a new #GtkTextTag
716  **/
717 GtkTextTag*
718 gtk_text_tag_new (const gchar *name)
719 {
720   GtkTextTag *tag;
721
722   tag = g_object_new (GTK_TYPE_TEXT_TAG, "name", name, NULL);
723
724   return tag;
725 }
726
727 static void
728 gtk_text_tag_finalize (GObject *object)
729 {
730   GtkTextTag *text_tag;
731
732   text_tag = GTK_TEXT_TAG (object);
733
734   g_assert (!text_tag->values->realized);
735
736   if (text_tag->table)
737     gtk_text_tag_table_remove (text_tag->table, text_tag);
738
739   g_assert (text_tag->table == NULL);
740
741   gtk_text_attributes_unref (text_tag->values);
742   text_tag->values = NULL;
743   
744   g_free (text_tag->name);
745   text_tag->name = NULL;
746
747   (* G_OBJECT_CLASS (gtk_text_tag_parent_class)->finalize) (object);
748 }
749
750 static void
751 set_bg_color (GtkTextTag *tag, GdkColor *color)
752 {
753   if (color)
754     {
755       if (!tag->bg_color_set)
756         {
757           tag->bg_color_set = TRUE;
758           g_object_notify (G_OBJECT (tag), "background-set");
759         }
760       
761       tag->values->appearance.bg_color = *color;
762     }
763   else
764     {
765       if (tag->bg_color_set)
766         {
767           tag->bg_color_set = FALSE;
768           g_object_notify (G_OBJECT (tag), "background-set");
769         }
770     }
771 }
772
773 static void
774 set_fg_color (GtkTextTag *tag, GdkColor *color)
775 {
776   if (color)
777     {
778       if (!tag->fg_color_set)
779         {
780           tag->fg_color_set = TRUE;
781           g_object_notify (G_OBJECT (tag), "foreground-set");
782         }
783       tag->values->appearance.fg_color = *color;
784     }
785   else
786     {
787       if (tag->fg_color_set)
788         {
789           tag->fg_color_set = FALSE;
790           g_object_notify (G_OBJECT (tag), "foreground-set");
791         }
792     }
793 }
794
795 static void
796 set_pg_bg_color (GtkTextTag *tag, GdkColor *color)
797 {
798   if (color)
799     {
800       if (!tag->pg_bg_color_set)
801         {
802           tag->pg_bg_color_set = TRUE;
803           g_object_notify (G_OBJECT (tag), "paragraph-background-set");
804         }
805       else
806         gdk_color_free (tag->values->pg_bg_color);
807
808       tag->values->pg_bg_color = gdk_color_copy (color);
809     }
810   else
811     {
812       if (tag->pg_bg_color_set)
813         {
814           tag->pg_bg_color_set = FALSE;
815           g_object_notify (G_OBJECT (tag), "paragraph-background-set");
816           gdk_color_free (tag->values->pg_bg_color);
817         }
818
819       tag->values->pg_bg_color = NULL;
820     }
821 }
822
823 static PangoFontMask
824 get_property_font_set_mask (guint prop_id)
825 {
826   switch (prop_id)
827     {
828     case PROP_FAMILY_SET:
829       return PANGO_FONT_MASK_FAMILY;
830     case PROP_STYLE_SET:
831       return PANGO_FONT_MASK_STYLE;
832     case PROP_VARIANT_SET:
833       return PANGO_FONT_MASK_VARIANT;
834     case PROP_WEIGHT_SET:
835       return PANGO_FONT_MASK_WEIGHT;
836     case PROP_STRETCH_SET:
837       return PANGO_FONT_MASK_STRETCH;
838     case PROP_SIZE_SET:
839       return PANGO_FONT_MASK_SIZE;
840     }
841
842   return 0;
843 }
844
845 static PangoFontMask
846 set_font_desc_fields (PangoFontDescription *desc,
847                       PangoFontMask         to_set)
848 {
849   PangoFontMask changed_mask = 0;
850   
851   if (to_set & PANGO_FONT_MASK_FAMILY)
852     {
853       const char *family = pango_font_description_get_family (desc);
854       if (!family)
855         {
856           family = "sans";
857           changed_mask |= PANGO_FONT_MASK_FAMILY;
858         }
859
860       pango_font_description_set_family (desc, family);
861     }
862   if (to_set & PANGO_FONT_MASK_STYLE)
863     pango_font_description_set_style (desc, pango_font_description_get_style (desc));
864   if (to_set & PANGO_FONT_MASK_VARIANT)
865     pango_font_description_set_variant (desc, pango_font_description_get_variant (desc));
866   if (to_set & PANGO_FONT_MASK_WEIGHT)
867     pango_font_description_set_weight (desc, pango_font_description_get_weight (desc));
868   if (to_set & PANGO_FONT_MASK_STRETCH)
869     pango_font_description_set_stretch (desc, pango_font_description_get_stretch (desc));
870   if (to_set & PANGO_FONT_MASK_SIZE)
871     {
872       gint size = pango_font_description_get_size (desc);
873       if (size <= 0)
874         {
875           size = 10 * PANGO_SCALE;
876           changed_mask |= PANGO_FONT_MASK_SIZE;
877         }
878       
879       pango_font_description_set_size (desc, size);
880     }
881
882   return changed_mask;
883 }
884
885 static void
886 notify_set_changed (GObject       *object,
887                     PangoFontMask  changed_mask)
888 {
889   if (changed_mask & PANGO_FONT_MASK_FAMILY)
890     g_object_notify (object, "family-set");
891   if (changed_mask & PANGO_FONT_MASK_STYLE)
892     g_object_notify (object, "style-set");
893   if (changed_mask & PANGO_FONT_MASK_VARIANT)
894     g_object_notify (object, "variant-set");
895   if (changed_mask & PANGO_FONT_MASK_WEIGHT)
896     g_object_notify (object, "weight-set");
897   if (changed_mask & PANGO_FONT_MASK_STRETCH)
898     g_object_notify (object, "stretch-set");
899   if (changed_mask & PANGO_FONT_MASK_SIZE)
900     g_object_notify (object, "size-set");
901 }
902
903 static void
904 notify_fields_changed (GObject       *object,
905                        PangoFontMask  changed_mask)
906 {
907   if (changed_mask & PANGO_FONT_MASK_FAMILY)
908     g_object_notify (object, "family");
909   if (changed_mask & PANGO_FONT_MASK_STYLE)
910     g_object_notify (object, "style");
911   if (changed_mask & PANGO_FONT_MASK_VARIANT)
912     g_object_notify (object, "variant");
913   if (changed_mask & PANGO_FONT_MASK_WEIGHT)
914     g_object_notify (object, "weight");
915   if (changed_mask & PANGO_FONT_MASK_STRETCH)
916     g_object_notify (object, "stretch");
917   if (changed_mask & PANGO_FONT_MASK_SIZE)
918     g_object_notify (object, "size");
919 }
920
921 static void
922 set_font_description (GtkTextTag           *text_tag,
923                       PangoFontDescription *font_desc)
924 {
925   GObject *object = G_OBJECT (text_tag);
926   PangoFontDescription *new_font_desc;
927   PangoFontMask old_mask, new_mask, changed_mask, set_changed_mask;
928   
929   if (font_desc)
930     new_font_desc = pango_font_description_copy (font_desc);
931   else
932     new_font_desc = pango_font_description_new ();
933
934   if (text_tag->values->font)
935     old_mask = pango_font_description_get_set_fields (text_tag->values->font);
936   else
937     old_mask = 0;
938   
939   new_mask = pango_font_description_get_set_fields (new_font_desc);
940
941   changed_mask = old_mask | new_mask;
942   set_changed_mask = old_mask ^ new_mask;
943
944   if (text_tag->values->font)
945     pango_font_description_free (text_tag->values->font);
946   text_tag->values->font = new_font_desc;
947   
948   g_object_freeze_notify (object);
949
950   g_object_notify (object, "font-desc");
951   g_object_notify (object, "font");
952   
953   if (changed_mask & PANGO_FONT_MASK_FAMILY)
954     g_object_notify (object, "family");
955   if (changed_mask & PANGO_FONT_MASK_STYLE)
956     g_object_notify (object, "style");
957   if (changed_mask & PANGO_FONT_MASK_VARIANT)
958     g_object_notify (object, "variant");
959   if (changed_mask & PANGO_FONT_MASK_WEIGHT)
960     g_object_notify (object, "weight");
961   if (changed_mask & PANGO_FONT_MASK_STRETCH)
962     g_object_notify (object, "stretch");
963   if (changed_mask & PANGO_FONT_MASK_SIZE)
964     {
965       g_object_notify (object, "size");
966       g_object_notify (object, "size-points");
967     }
968
969   notify_set_changed (object, set_changed_mask);
970   
971   g_object_thaw_notify (object);
972 }
973
974 static void
975 gtk_text_tag_ensure_font (GtkTextTag *text_tag)
976 {
977   if (!text_tag->values->font)
978     text_tag->values->font = pango_font_description_new ();
979 }
980
981 static void
982 gtk_text_tag_set_property (GObject      *object,
983                            guint         prop_id,
984                            const GValue *value,
985                            GParamSpec   *pspec)
986 {
987   GtkTextTag *text_tag;
988   gboolean size_changed = FALSE;
989
990   text_tag = GTK_TEXT_TAG (object);
991
992   g_return_if_fail (!text_tag->values->realized);
993
994   switch (prop_id)
995     {
996     case PROP_NAME:
997       g_return_if_fail (text_tag->name == NULL);
998       text_tag->name = g_strdup (g_value_get_string (value));
999       break;
1000
1001     case PROP_BACKGROUND:
1002       {
1003         GdkColor color;
1004
1005         if (gdk_color_parse (g_value_get_string (value), &color))
1006           set_bg_color (text_tag, &color);
1007         else
1008           g_warning ("Don't know color `%s'", g_value_get_string (value));
1009
1010         g_object_notify (object, "background-gdk");
1011       }
1012       break;
1013
1014     case PROP_FOREGROUND:
1015       {
1016         GdkColor color;
1017
1018         if (gdk_color_parse (g_value_get_string (value), &color))
1019           set_fg_color (text_tag, &color);
1020         else
1021           g_warning ("Don't know color `%s'", g_value_get_string (value));
1022
1023         g_object_notify (object, "foreground-gdk");
1024       }
1025       break;
1026
1027     case PROP_BACKGROUND_GDK:
1028       {
1029         GdkColor *color = g_value_get_boxed (value);
1030
1031         set_bg_color (text_tag, color);
1032       }
1033       break;
1034
1035     case PROP_FOREGROUND_GDK:
1036       {
1037         GdkColor *color = g_value_get_boxed (value);
1038
1039         set_fg_color (text_tag, color);
1040       }
1041       break;
1042
1043     case PROP_BACKGROUND_STIPPLE:
1044       {
1045         GdkBitmap *bitmap = g_value_get_object (value);
1046
1047         text_tag->bg_stipple_set = TRUE;
1048         g_object_notify (object, "background-stipple-set");
1049         
1050         if (text_tag->values->appearance.bg_stipple != bitmap)
1051           {
1052             if (bitmap != NULL)
1053               g_object_ref (bitmap);
1054
1055             if (text_tag->values->appearance.bg_stipple)
1056               g_object_unref (text_tag->values->appearance.bg_stipple);
1057
1058             text_tag->values->appearance.bg_stipple = bitmap;
1059           }
1060       }
1061       break;
1062
1063     case PROP_FOREGROUND_STIPPLE:
1064       {
1065         GdkBitmap *bitmap = g_value_get_object (value);
1066
1067         text_tag->fg_stipple_set = TRUE;
1068         g_object_notify (object, "foreground-stipple-set");
1069
1070         if (text_tag->values->appearance.fg_stipple != bitmap)
1071           {
1072             if (bitmap != NULL)
1073               g_object_ref (bitmap);
1074
1075             if (text_tag->values->appearance.fg_stipple)
1076               g_object_unref (text_tag->values->appearance.fg_stipple);
1077
1078             text_tag->values->appearance.fg_stipple = bitmap;
1079           }
1080       }
1081       break;
1082
1083     case PROP_FONT:
1084       {
1085         PangoFontDescription *font_desc = NULL;
1086         const gchar *name;
1087
1088         name = g_value_get_string (value);
1089
1090         if (name)
1091           font_desc = pango_font_description_from_string (name);
1092
1093         set_font_description (text_tag, font_desc);
1094         if (font_desc)
1095           pango_font_description_free (font_desc);
1096         
1097         size_changed = TRUE;
1098       }
1099       break;
1100
1101     case PROP_FONT_DESC:
1102       {
1103         PangoFontDescription *font_desc;
1104
1105         font_desc = g_value_get_boxed (value);
1106
1107         set_font_description (text_tag, font_desc);
1108
1109         size_changed = TRUE;
1110       }
1111       break;
1112
1113     case PROP_FAMILY:
1114     case PROP_STYLE:
1115     case PROP_VARIANT:
1116     case PROP_WEIGHT:
1117     case PROP_STRETCH:
1118     case PROP_SIZE:
1119     case PROP_SIZE_POINTS:
1120       {
1121         PangoFontMask old_set_mask;
1122
1123         gtk_text_tag_ensure_font (text_tag);
1124         old_set_mask = pango_font_description_get_set_fields (text_tag->values->font);
1125  
1126         switch (prop_id)
1127           {
1128           case PROP_FAMILY:
1129             pango_font_description_set_family (text_tag->values->font,
1130                                                g_value_get_string (value));
1131             break;
1132           case PROP_STYLE:
1133             pango_font_description_set_style (text_tag->values->font,
1134                                               g_value_get_enum (value));
1135             break;
1136           case PROP_VARIANT:
1137             pango_font_description_set_variant (text_tag->values->font,
1138                                                 g_value_get_enum (value));
1139             break;
1140           case PROP_WEIGHT:
1141             pango_font_description_set_weight (text_tag->values->font,
1142                                                g_value_get_int (value));
1143             break;
1144           case PROP_STRETCH:
1145             pango_font_description_set_stretch (text_tag->values->font,
1146                                                 g_value_get_enum (value));
1147             break;
1148           case PROP_SIZE:
1149             pango_font_description_set_size (text_tag->values->font,
1150                                              g_value_get_int (value));
1151             g_object_notify (object, "size-points");
1152             break;
1153           case PROP_SIZE_POINTS:
1154             pango_font_description_set_size (text_tag->values->font,
1155                                              g_value_get_double (value) * PANGO_SCALE);
1156             g_object_notify (object, "size");
1157             break;
1158           }
1159
1160         size_changed = TRUE;
1161         notify_set_changed (object, old_set_mask & pango_font_description_get_set_fields (text_tag->values->font));
1162         g_object_notify (object, "font-desc");
1163         g_object_notify (object, "font");
1164
1165         break;
1166       }
1167       
1168     case PROP_SCALE:
1169       text_tag->values->font_scale = g_value_get_double (value);
1170       text_tag->scale_set = TRUE;
1171       g_object_notify (object, "scale-set");
1172       size_changed = TRUE;
1173       break;
1174       
1175     case PROP_PIXELS_ABOVE_LINES:
1176       text_tag->pixels_above_lines_set = TRUE;
1177       text_tag->values->pixels_above_lines = g_value_get_int (value);
1178       g_object_notify (object, "pixels-above-lines-set");
1179       size_changed = TRUE;
1180       break;
1181
1182     case PROP_PIXELS_BELOW_LINES:
1183       text_tag->pixels_below_lines_set = TRUE;
1184       text_tag->values->pixels_below_lines = g_value_get_int (value);
1185       g_object_notify (object, "pixels-below-lines-set");
1186       size_changed = TRUE;
1187       break;
1188
1189     case PROP_PIXELS_INSIDE_WRAP:
1190       text_tag->pixels_inside_wrap_set = TRUE;
1191       text_tag->values->pixels_inside_wrap = g_value_get_int (value);
1192       g_object_notify (object, "pixels-inside-wrap-set");
1193       size_changed = TRUE;
1194       break;
1195
1196     case PROP_EDITABLE:
1197       text_tag->editable_set = TRUE;
1198       text_tag->values->editable = g_value_get_boolean (value);
1199       g_object_notify (object, "editable-set");
1200       break;
1201
1202     case PROP_WRAP_MODE:
1203       text_tag->wrap_mode_set = TRUE;
1204       text_tag->values->wrap_mode = g_value_get_enum (value);
1205       g_object_notify (object, "wrap-mode-set");
1206       size_changed = TRUE;
1207       break;
1208
1209     case PROP_JUSTIFICATION:
1210       text_tag->justification_set = TRUE;
1211       text_tag->values->justification = g_value_get_enum (value);
1212       g_object_notify (object, "justification-set");
1213       size_changed = TRUE;
1214       break;
1215
1216     case PROP_DIRECTION:
1217       text_tag->values->direction = g_value_get_enum (value);
1218       break;
1219
1220     case PROP_LEFT_MARGIN:
1221       text_tag->left_margin_set = TRUE;
1222       text_tag->values->left_margin = g_value_get_int (value);
1223       g_object_notify (object, "left-margin-set");
1224       size_changed = TRUE;
1225       break;
1226
1227     case PROP_INDENT:
1228       text_tag->indent_set = TRUE;
1229       text_tag->values->indent = g_value_get_int (value);
1230       g_object_notify (object, "indent-set");
1231       size_changed = TRUE;
1232       break;
1233
1234     case PROP_STRIKETHROUGH:
1235       text_tag->strikethrough_set = TRUE;
1236       text_tag->values->appearance.strikethrough = g_value_get_boolean (value);
1237       g_object_notify (object, "strikethrough-set");
1238       break;
1239
1240     case PROP_RIGHT_MARGIN:
1241       text_tag->right_margin_set = TRUE;
1242       text_tag->values->right_margin = g_value_get_int (value);
1243       g_object_notify (object, "right-margin-set");
1244       size_changed = TRUE;
1245       break;
1246
1247     case PROP_UNDERLINE:
1248       text_tag->underline_set = TRUE;
1249       text_tag->values->appearance.underline = g_value_get_enum (value);
1250       g_object_notify (object, "underline-set");
1251       break;
1252
1253     case PROP_RISE:
1254       text_tag->rise_set = TRUE;
1255       text_tag->values->appearance.rise = g_value_get_int (value);
1256       g_object_notify (object, "rise-set");
1257       size_changed = TRUE;      
1258       break;
1259
1260     case PROP_BACKGROUND_FULL_HEIGHT:
1261       text_tag->bg_full_height_set = TRUE;
1262       text_tag->values->bg_full_height = g_value_get_boolean (value);
1263       g_object_notify (object, "background-full-height-set");
1264       break;
1265
1266     case PROP_LANGUAGE:
1267       text_tag->language_set = TRUE;
1268       text_tag->values->language = pango_language_from_string (g_value_get_string (value));
1269       g_object_notify (object, "language-set");
1270       break;
1271
1272     case PROP_TABS:
1273       text_tag->tabs_set = TRUE;
1274
1275       if (text_tag->values->tabs)
1276         pango_tab_array_free (text_tag->values->tabs);
1277
1278       /* FIXME I'm not sure if this is a memleak or not */
1279       text_tag->values->tabs =
1280         pango_tab_array_copy (g_value_get_boxed (value));
1281
1282       g_object_notify (object, "tabs-set");
1283       
1284       size_changed = TRUE;
1285       break;
1286
1287     case PROP_INVISIBLE:
1288       text_tag->invisible_set = TRUE;
1289       text_tag->values->invisible = g_value_get_boolean (value);
1290       g_object_notify (object, "invisible-set");
1291       size_changed = TRUE;
1292       break;
1293       
1294     case PROP_PARAGRAPH_BACKGROUND:
1295       {
1296         GdkColor color;
1297
1298         if (gdk_color_parse (g_value_get_string (value), &color))
1299           set_pg_bg_color (text_tag, &color);
1300         else
1301           g_warning ("Don't know color `%s'", g_value_get_string (value));
1302
1303         g_object_notify (object, "paragraph-background-gdk");
1304       }
1305       break;
1306
1307     case PROP_PARAGRAPH_BACKGROUND_GDK:
1308       {
1309         GdkColor *color = g_value_get_boxed (value);
1310
1311         set_pg_bg_color (text_tag, color);
1312       }
1313       break;
1314
1315     case PROP_ACCUMULATIVE_MARGIN:
1316       text_tag->accumulative_margin = g_value_get_boolean (value);
1317       g_object_notify (object, "accumulative-margin");
1318       size_changed = TRUE;
1319       break;
1320
1321       /* Whether the value should be used... */
1322
1323     case PROP_BACKGROUND_SET:
1324       text_tag->bg_color_set = g_value_get_boolean (value);
1325       break;
1326
1327     case PROP_FOREGROUND_SET:
1328       text_tag->fg_color_set = g_value_get_boolean (value);
1329       break;
1330
1331     case PROP_BACKGROUND_STIPPLE_SET:
1332       text_tag->bg_stipple_set = g_value_get_boolean (value);
1333       if (!text_tag->bg_stipple_set &&
1334           text_tag->values->appearance.bg_stipple)
1335         {
1336           g_object_unref (text_tag->values->appearance.bg_stipple);
1337           text_tag->values->appearance.bg_stipple = NULL;
1338         }
1339       break;
1340
1341     case PROP_FOREGROUND_STIPPLE_SET:
1342       text_tag->fg_stipple_set = g_value_get_boolean (value);
1343       if (!text_tag->fg_stipple_set &&
1344           text_tag->values->appearance.fg_stipple)
1345         {
1346           g_object_unref (text_tag->values->appearance.fg_stipple);
1347           text_tag->values->appearance.fg_stipple = NULL;
1348         }
1349       break;
1350
1351     case PROP_FAMILY_SET:
1352     case PROP_STYLE_SET:
1353     case PROP_VARIANT_SET:
1354     case PROP_WEIGHT_SET:
1355     case PROP_STRETCH_SET:
1356     case PROP_SIZE_SET:
1357       if (!g_value_get_boolean (value))
1358         {
1359           if (text_tag->values->font)
1360             pango_font_description_unset_fields (text_tag->values->font,
1361                                                  get_property_font_set_mask (prop_id));
1362         }
1363       else
1364         {
1365           PangoFontMask changed_mask;
1366           
1367           gtk_text_tag_ensure_font (text_tag);
1368           changed_mask = set_font_desc_fields (text_tag->values->font,
1369                                                get_property_font_set_mask (prop_id));
1370           notify_fields_changed (G_OBJECT (text_tag), changed_mask);
1371         }
1372       break;
1373
1374     case PROP_SCALE_SET:
1375       text_tag->scale_set = g_value_get_boolean (value);
1376       size_changed = TRUE;
1377       break;
1378       
1379     case PROP_PIXELS_ABOVE_LINES_SET:
1380       text_tag->pixels_above_lines_set = g_value_get_boolean (value);
1381       size_changed = TRUE;
1382       break;
1383
1384     case PROP_PIXELS_BELOW_LINES_SET:
1385       text_tag->pixels_below_lines_set = g_value_get_boolean (value);
1386       size_changed = TRUE;
1387       break;
1388
1389     case PROP_PIXELS_INSIDE_WRAP_SET:
1390       text_tag->pixels_inside_wrap_set = g_value_get_boolean (value);
1391       size_changed = TRUE;
1392       break;
1393
1394     case PROP_EDITABLE_SET:
1395       text_tag->editable_set = g_value_get_boolean (value);
1396       break;
1397
1398     case PROP_WRAP_MODE_SET:
1399       text_tag->wrap_mode_set = g_value_get_boolean (value);
1400       size_changed = TRUE;
1401       break;
1402
1403     case PROP_JUSTIFICATION_SET:
1404       text_tag->justification_set = g_value_get_boolean (value);
1405       size_changed = TRUE;
1406       break;
1407       
1408     case PROP_LEFT_MARGIN_SET:
1409       text_tag->left_margin_set = g_value_get_boolean (value);
1410       size_changed = TRUE;
1411       break;
1412
1413     case PROP_INDENT_SET:
1414       text_tag->indent_set = g_value_get_boolean (value);
1415       size_changed = TRUE;
1416       break;
1417
1418     case PROP_STRIKETHROUGH_SET:
1419       text_tag->strikethrough_set = g_value_get_boolean (value);
1420       break;
1421
1422     case PROP_RIGHT_MARGIN_SET:
1423       text_tag->right_margin_set = g_value_get_boolean (value);
1424       size_changed = TRUE;
1425       break;
1426
1427     case PROP_UNDERLINE_SET:
1428       text_tag->underline_set = g_value_get_boolean (value);
1429       break;
1430
1431     case PROP_RISE_SET:
1432       text_tag->rise_set = g_value_get_boolean (value);
1433       size_changed = TRUE;
1434       break;
1435
1436     case PROP_BACKGROUND_FULL_HEIGHT_SET:
1437       text_tag->bg_full_height_set = g_value_get_boolean (value);
1438       break;
1439
1440     case PROP_LANGUAGE_SET:
1441       text_tag->language_set = g_value_get_boolean (value);
1442       size_changed = TRUE;
1443       break;
1444
1445     case PROP_TABS_SET:
1446       text_tag->tabs_set = g_value_get_boolean (value);
1447       size_changed = TRUE;
1448       break;
1449
1450     case PROP_INVISIBLE_SET:
1451       text_tag->invisible_set = g_value_get_boolean (value);
1452       size_changed = TRUE;
1453       break;
1454       
1455     case PROP_PARAGRAPH_BACKGROUND_SET:
1456       text_tag->pg_bg_color_set = g_value_get_boolean (value);
1457       break;
1458
1459     default:
1460       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1461       break;
1462     }
1463
1464   /* FIXME I would like to do this after all set_property in a single
1465    * g_object_set () have been called. But an idle function won't
1466    * work; we need to emit when the tag is changed, not when we get
1467    * around to the event loop. So blah, we eat some inefficiency.
1468    */
1469
1470   /* This is also somewhat weird since we emit another object's
1471    * signal here, but the two objects are already tightly bound.
1472    */
1473
1474   if (text_tag->table)
1475     g_signal_emit_by_name (text_tag->table,
1476                            "tag_changed",
1477                            text_tag, size_changed);
1478 }
1479
1480 static void
1481 gtk_text_tag_get_property (GObject      *object,
1482                            guint         prop_id,
1483                            GValue       *value,
1484                            GParamSpec   *pspec)
1485 {
1486   GtkTextTag *tag;
1487
1488   tag = GTK_TEXT_TAG (object);
1489
1490   switch (prop_id)
1491     {
1492     case PROP_NAME:
1493       g_value_set_string (value, tag->name);
1494       break;
1495
1496     case PROP_BACKGROUND_GDK:
1497       g_value_set_boxed (value, &tag->values->appearance.bg_color);
1498       break;
1499
1500     case PROP_FOREGROUND_GDK:
1501       g_value_set_boxed (value, &tag->values->appearance.fg_color);
1502       break;
1503
1504     case PROP_BACKGROUND_STIPPLE:
1505       if (tag->bg_stipple_set)
1506         g_value_set_object (value, tag->values->appearance.bg_stipple);
1507       break;
1508
1509     case PROP_FOREGROUND_STIPPLE:
1510       if (tag->fg_stipple_set)
1511         g_value_set_object (value, tag->values->appearance.fg_stipple);
1512       break;
1513
1514     case PROP_FONT:
1515         {
1516           gchar *str;
1517
1518           gtk_text_tag_ensure_font (tag);
1519           
1520           str = pango_font_description_to_string (tag->values->font);
1521           g_value_take_string (value, str);
1522         }
1523       break;
1524
1525     case PROP_FONT_DESC:
1526       gtk_text_tag_ensure_font (tag);
1527       g_value_set_boxed (value, tag->values->font);
1528       break;
1529
1530     case PROP_FAMILY:
1531     case PROP_STYLE:
1532     case PROP_VARIANT:
1533     case PROP_WEIGHT:
1534     case PROP_STRETCH:
1535     case PROP_SIZE:
1536     case PROP_SIZE_POINTS:
1537       gtk_text_tag_ensure_font (tag);
1538       switch (prop_id)
1539         {
1540         case PROP_FAMILY:
1541           g_value_set_string (value, pango_font_description_get_family (tag->values->font));
1542           break;
1543           
1544         case PROP_STYLE:
1545           g_value_set_enum (value, pango_font_description_get_style (tag->values->font));
1546           break;
1547           
1548         case PROP_VARIANT:
1549           g_value_set_enum (value, pango_font_description_get_variant (tag->values->font));
1550           break;
1551           
1552         case PROP_WEIGHT:
1553           g_value_set_int (value, pango_font_description_get_weight (tag->values->font));
1554           break;
1555           
1556         case PROP_STRETCH:
1557           g_value_set_enum (value, pango_font_description_get_stretch (tag->values->font));
1558           break;
1559           
1560         case PROP_SIZE:
1561           g_value_set_int (value, pango_font_description_get_size (tag->values->font));
1562           break;
1563           
1564         case PROP_SIZE_POINTS:
1565           g_value_set_double (value, ((double)pango_font_description_get_size (tag->values->font)) / (double)PANGO_SCALE);
1566           break;
1567         }
1568       break;
1569       
1570     case PROP_SCALE:
1571       g_value_set_double (value, tag->values->font_scale);
1572       break;
1573       
1574     case PROP_PIXELS_ABOVE_LINES:
1575       g_value_set_int (value,  tag->values->pixels_above_lines);
1576       break;
1577
1578     case PROP_PIXELS_BELOW_LINES:
1579       g_value_set_int (value,  tag->values->pixels_below_lines);
1580       break;
1581
1582     case PROP_PIXELS_INSIDE_WRAP:
1583       g_value_set_int (value,  tag->values->pixels_inside_wrap);
1584       break;
1585
1586     case PROP_EDITABLE:
1587       g_value_set_boolean (value, tag->values->editable);
1588       break;
1589
1590     case PROP_WRAP_MODE:
1591       g_value_set_enum (value, tag->values->wrap_mode);
1592       break;
1593
1594     case PROP_JUSTIFICATION:
1595       g_value_set_enum (value, tag->values->justification);
1596       break;
1597
1598     case PROP_DIRECTION:
1599       g_value_set_enum (value, tag->values->direction);
1600       break;
1601       
1602     case PROP_LEFT_MARGIN:
1603       g_value_set_int (value,  tag->values->left_margin);
1604       break;
1605
1606     case PROP_INDENT:
1607       g_value_set_int (value,  tag->values->indent);
1608       break;
1609
1610     case PROP_STRIKETHROUGH:
1611       g_value_set_boolean (value, tag->values->appearance.strikethrough);
1612       break;
1613
1614     case PROP_RIGHT_MARGIN:
1615       g_value_set_int (value, tag->values->right_margin);
1616       break;
1617
1618     case PROP_UNDERLINE:
1619       g_value_set_enum (value, tag->values->appearance.underline);
1620       break;
1621
1622     case PROP_RISE:
1623       g_value_set_int (value, tag->values->appearance.rise);
1624       break;
1625
1626     case PROP_BACKGROUND_FULL_HEIGHT:
1627       g_value_set_boolean (value, tag->values->bg_full_height);
1628       break;
1629
1630     case PROP_LANGUAGE:
1631       g_value_set_string (value, pango_language_to_string (tag->values->language));
1632       break;
1633
1634     case PROP_TABS:
1635       if (tag->values->tabs)
1636         g_value_set_boxed (value, tag->values->tabs);
1637       break;
1638
1639     case PROP_INVISIBLE:
1640       g_value_set_boolean (value, tag->values->invisible);
1641       break;
1642       
1643     case PROP_PARAGRAPH_BACKGROUND_GDK:
1644       g_value_set_boxed (value, tag->values->pg_bg_color);
1645       break;
1646
1647     case PROP_ACCUMULATIVE_MARGIN:
1648       g_value_set_boolean (value, tag->accumulative_margin);
1649       break;
1650
1651     case PROP_BACKGROUND_SET:
1652       g_value_set_boolean (value, tag->bg_color_set);
1653       break;
1654
1655     case PROP_FOREGROUND_SET:
1656       g_value_set_boolean (value, tag->fg_color_set);
1657       break;
1658
1659     case PROP_BACKGROUND_STIPPLE_SET:
1660       g_value_set_boolean (value, tag->bg_stipple_set);
1661       break;
1662
1663     case PROP_FOREGROUND_STIPPLE_SET:
1664       g_value_set_boolean (value, tag->fg_stipple_set);
1665       break;
1666
1667     case PROP_FAMILY_SET:
1668     case PROP_STYLE_SET:
1669     case PROP_VARIANT_SET:
1670     case PROP_WEIGHT_SET:
1671     case PROP_STRETCH_SET:
1672     case PROP_SIZE_SET:
1673       {
1674         PangoFontMask set_mask = tag->values->font ? pango_font_description_get_set_fields (tag->values->font) : 0;
1675         PangoFontMask test_mask = get_property_font_set_mask (prop_id);
1676         g_value_set_boolean (value, (set_mask & test_mask) != 0);
1677
1678         break;
1679       }
1680
1681     case PROP_SCALE_SET:
1682       g_value_set_boolean (value, tag->scale_set);
1683       break;
1684       
1685     case PROP_PIXELS_ABOVE_LINES_SET:
1686       g_value_set_boolean (value, tag->pixels_above_lines_set);
1687       break;
1688
1689     case PROP_PIXELS_BELOW_LINES_SET:
1690       g_value_set_boolean (value, tag->pixels_below_lines_set);
1691       break;
1692
1693     case PROP_PIXELS_INSIDE_WRAP_SET:
1694       g_value_set_boolean (value, tag->pixels_inside_wrap_set);
1695       break;
1696
1697     case PROP_EDITABLE_SET:
1698       g_value_set_boolean (value, tag->editable_set);
1699       break;
1700
1701     case PROP_WRAP_MODE_SET:
1702       g_value_set_boolean (value, tag->wrap_mode_set);
1703       break;
1704
1705     case PROP_JUSTIFICATION_SET:
1706       g_value_set_boolean (value, tag->justification_set);
1707       break;
1708       
1709     case PROP_LEFT_MARGIN_SET:
1710       g_value_set_boolean (value, tag->left_margin_set);
1711       break;
1712
1713     case PROP_INDENT_SET:
1714       g_value_set_boolean (value, tag->indent_set);
1715       break;
1716
1717     case PROP_STRIKETHROUGH_SET:
1718       g_value_set_boolean (value, tag->strikethrough_set);
1719       break;
1720
1721     case PROP_RIGHT_MARGIN_SET:
1722       g_value_set_boolean (value, tag->right_margin_set);
1723       break;
1724
1725     case PROP_UNDERLINE_SET:
1726       g_value_set_boolean (value, tag->underline_set);
1727       break;
1728
1729     case PROP_RISE_SET:
1730       g_value_set_boolean (value, tag->rise_set);
1731       break;
1732
1733     case PROP_BACKGROUND_FULL_HEIGHT_SET:
1734       g_value_set_boolean (value, tag->bg_full_height_set);
1735       break;
1736
1737     case PROP_LANGUAGE_SET:
1738       g_value_set_boolean (value, tag->language_set);
1739       break;
1740
1741     case PROP_TABS_SET:
1742       g_value_set_boolean (value, tag->tabs_set);
1743       break;
1744
1745     case PROP_INVISIBLE_SET:
1746       g_value_set_boolean (value, tag->invisible_set);
1747       break;
1748       
1749     case PROP_PARAGRAPH_BACKGROUND_SET:
1750       g_value_set_boolean (value, tag->pg_bg_color_set);
1751       break;
1752
1753     case PROP_BACKGROUND:
1754     case PROP_FOREGROUND:
1755     case PROP_PARAGRAPH_BACKGROUND:
1756       g_warning ("'foreground', 'background' and 'paragraph_background' properties are not readable, use 'foreground_gdk', 'background_gdk' and 'paragraph_background_gdk'");
1757     default:
1758       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1759       break;
1760     }
1761 }
1762
1763 /*
1764  * Tag operations
1765  */
1766
1767 typedef struct {
1768   gint high;
1769   gint low;
1770   gint delta;
1771 } DeltaData;
1772
1773 static void
1774 delta_priority_foreach (GtkTextTag *tag, gpointer user_data)
1775 {
1776   DeltaData *dd = user_data;
1777
1778   if (tag->priority >= dd->low && tag->priority <= dd->high)
1779     tag->priority += dd->delta;
1780 }
1781
1782 /**
1783  * gtk_text_tag_get_priority:
1784  * @tag: a #GtkTextTag
1785  * 
1786  * Get the tag priority.
1787  * 
1788  * Return value: The tag's priority.
1789  **/
1790 gint
1791 gtk_text_tag_get_priority (GtkTextTag *tag)
1792 {
1793   g_return_val_if_fail (GTK_IS_TEXT_TAG (tag), 0);
1794
1795   return tag->priority;
1796 }
1797
1798 /**
1799  * gtk_text_tag_set_priority:
1800  * @tag: a #GtkTextTag
1801  * @priority: the new priority
1802  * 
1803  * Sets the priority of a #GtkTextTag. Valid priorities are
1804  * start at 0 and go to one less than gtk_text_tag_table_get_size().
1805  * Each tag in a table has a unique priority; setting the priority
1806  * of one tag shifts the priorities of all the other tags in the
1807  * table to maintain a unique priority for each tag. Higher priority
1808  * tags "win" if two tags both set the same text attribute. When adding
1809  * a tag to a tag table, it will be assigned the highest priority in
1810  * the table by default; so normally the precedence of a set of tags
1811  * is the order in which they were added to the table, or created with
1812  * gtk_text_buffer_create_tag(), which adds the tag to the buffer's table
1813  * automatically.
1814  **/
1815 void
1816 gtk_text_tag_set_priority (GtkTextTag *tag,
1817                            gint        priority)
1818 {
1819   DeltaData dd;
1820
1821   g_return_if_fail (GTK_IS_TEXT_TAG (tag));
1822   g_return_if_fail (tag->table != NULL);
1823   g_return_if_fail (priority >= 0);
1824   g_return_if_fail (priority < gtk_text_tag_table_get_size (tag->table));
1825
1826   if (priority == tag->priority)
1827     return;
1828
1829   if (priority < tag->priority)
1830     {
1831       dd.low = priority;
1832       dd.high = tag->priority - 1;
1833       dd.delta = 1;
1834     }
1835   else
1836     {
1837       dd.low = tag->priority + 1;
1838       dd.high = priority;
1839       dd.delta = -1;
1840     }
1841
1842   gtk_text_tag_table_foreach (tag->table,
1843                               delta_priority_foreach,
1844                               &dd);
1845
1846   tag->priority = priority;
1847 }
1848
1849 /**
1850  * gtk_text_tag_event:
1851  * @tag: a #GtkTextTag
1852  * @event_object: object that received the event, such as a widget
1853  * @event: the event
1854  * @iter: location where the event was received
1855  * 
1856  * Emits the "event" signal on the #GtkTextTag.
1857  * 
1858  * Return value: result of signal emission (whether the event was handled)
1859  **/
1860 gboolean
1861 gtk_text_tag_event (GtkTextTag        *tag,
1862                     GObject           *event_object,
1863                     GdkEvent          *event,
1864                     const GtkTextIter *iter)
1865 {
1866   gboolean retval = FALSE;
1867
1868   g_return_val_if_fail (GTK_IS_TEXT_TAG (tag), FALSE);
1869   g_return_val_if_fail (G_IS_OBJECT (event_object), FALSE);
1870   g_return_val_if_fail (event != NULL, FALSE);
1871
1872   g_signal_emit (tag,
1873                  signals[EVENT],
1874                  0,
1875                  event_object,
1876                  event,
1877                  iter,
1878                  &retval);
1879
1880   return retval;
1881 }
1882
1883 static int
1884 tag_sort_func (gconstpointer first, gconstpointer second)
1885 {
1886   GtkTextTag *tag1, *tag2;
1887
1888   tag1 = * (GtkTextTag **) first;
1889   tag2 = * (GtkTextTag **) second;
1890   return tag1->priority - tag2->priority;
1891 }
1892
1893 void
1894 _gtk_text_tag_array_sort (GtkTextTag** tag_array_p,
1895                           guint len)
1896 {
1897   int i, j, prio;
1898   GtkTextTag **tag;
1899   GtkTextTag **maxPtrPtr, *tmp;
1900
1901   g_return_if_fail (tag_array_p != NULL);
1902   g_return_if_fail (len > 0);
1903
1904   if (len < 2) {
1905     return;
1906   }
1907   if (len < 20) {
1908     GtkTextTag **iter = tag_array_p;
1909
1910     for (i = len-1; i > 0; i--, iter++) {
1911       maxPtrPtr = tag = iter;
1912       prio = tag[0]->priority;
1913       for (j = i, tag++; j > 0; j--, tag++) {
1914         if (tag[0]->priority < prio) {
1915           prio = tag[0]->priority;
1916           maxPtrPtr = tag;
1917         }
1918       }
1919       tmp = *maxPtrPtr;
1920       *maxPtrPtr = *iter;
1921       *iter = tmp;
1922     }
1923   } else {
1924     qsort ((void *) tag_array_p, (unsigned) len, sizeof (GtkTextTag *),
1925            tag_sort_func);
1926   }
1927
1928 #if 0
1929   {
1930     printf ("Sorted tag array: \n");
1931     i = 0;
1932     while (i < len)
1933       {
1934         GtkTextTag *t = tag_array_p[i];
1935         printf ("  %s priority %d\n", t->name, t->priority);
1936
1937         ++i;
1938       }
1939   }
1940 #endif
1941 }
1942
1943 /*
1944  * Attributes
1945  */
1946
1947 /**
1948  * gtk_text_attributes_new:
1949  * 
1950  * Creates a #GtkTextAttributes, which describes
1951  * a set of properties on some text.
1952  * 
1953  * Return value: a new #GtkTextAttributes
1954  **/
1955 GtkTextAttributes*
1956 gtk_text_attributes_new (void)
1957 {
1958   GtkTextAttributes *values;
1959
1960   values = g_new0 (GtkTextAttributes, 1);
1961
1962   /* 0 is a valid value for most of the struct */
1963
1964   values->refcount = 1;
1965
1966   values->language = gtk_get_default_language ();
1967
1968   values->font_scale = 1.0;
1969
1970   values->editable = TRUE;
1971       
1972   return values;
1973 }
1974
1975 /**
1976  * gtk_text_attributes_copy:
1977  * @src: a #GtkTextAttributes to be copied
1978  * 
1979  * Copies @src and returns a new #GtkTextAttributes.
1980  * 
1981  * Return value: a copy of @src
1982  **/
1983 GtkTextAttributes*
1984 gtk_text_attributes_copy (GtkTextAttributes *src)
1985 {
1986   GtkTextAttributes *dest;
1987
1988   dest = gtk_text_attributes_new ();
1989   gtk_text_attributes_copy_values (src, dest);
1990
1991   return dest;
1992 }
1993
1994 GType
1995 gtk_text_attributes_get_type (void)
1996 {
1997   static GType our_type = 0;
1998   
1999   if (our_type == 0)
2000     our_type = g_boxed_type_register_static (I_("GtkTextAttributes"),
2001                                              (GBoxedCopyFunc) gtk_text_attributes_ref,
2002                                              (GBoxedFreeFunc) gtk_text_attributes_unref);
2003
2004   return our_type;
2005 }
2006
2007 /**
2008  * gtk_text_attributes_copy_values:
2009  * @src: a #GtkTextAttributes
2010  * @dest: another #GtkTextAttributes
2011  * 
2012  * Copies the values from @src to @dest so that @dest has the same values
2013  * as @src. Frees existing values in @dest.
2014  **/
2015 void
2016 gtk_text_attributes_copy_values (GtkTextAttributes *src,
2017                                  GtkTextAttributes *dest)
2018 {
2019   guint orig_refcount;
2020
2021   g_return_if_fail (!dest->realized);
2022
2023   if (src == dest)
2024     return;
2025
2026   /* Add refs */
2027
2028   if (src->appearance.bg_stipple)
2029     g_object_ref (src->appearance.bg_stipple);
2030
2031   if (src->appearance.fg_stipple)
2032     g_object_ref (src->appearance.fg_stipple);
2033
2034   /* Remove refs */
2035
2036   if (dest->appearance.bg_stipple)
2037     g_object_unref (dest->appearance.bg_stipple);
2038
2039   if (dest->appearance.fg_stipple)
2040     g_object_unref (dest->appearance.fg_stipple);
2041
2042   if (dest->font)
2043     pango_font_description_free (dest->font);
2044   
2045   /* Copy */
2046   orig_refcount = dest->refcount;
2047
2048   *dest = *src;
2049
2050   if (src->tabs)
2051     dest->tabs = pango_tab_array_copy (src->tabs);
2052
2053   dest->language = src->language;
2054
2055   if (dest->font)
2056     dest->font = pango_font_description_copy (src->font);
2057   
2058   if (src->pg_bg_color)
2059     dest->pg_bg_color = gdk_color_copy (src->pg_bg_color);
2060
2061   dest->refcount = orig_refcount;
2062   dest->realized = FALSE;
2063 }
2064
2065 /**
2066  * gtk_text_attributes_ref:
2067  * @values: a #GtkTextAttributes
2068  * 
2069  * Increments the reference count on @values.
2070  *
2071  * Returns: the #GtkTextAttributes that were passed in
2072  **/
2073 GtkTextAttributes *
2074 gtk_text_attributes_ref (GtkTextAttributes *values)
2075 {
2076   g_return_val_if_fail (values != NULL, NULL);
2077
2078   values->refcount += 1;
2079
2080   return values;
2081 }
2082
2083 /**
2084  * gtk_text_attributes_unref:
2085  * @values: a #GtkTextAttributes
2086  * 
2087  * Decrements the reference count on @values, freeing the structure
2088  * if the reference count reaches 0.
2089  **/
2090 void
2091 gtk_text_attributes_unref (GtkTextAttributes *values)
2092 {
2093   g_return_if_fail (values != NULL);
2094   g_return_if_fail (values->refcount > 0);
2095
2096   values->refcount -= 1;
2097
2098   if (values->refcount == 0)
2099     {
2100       g_assert (!values->realized);
2101
2102       if (values->appearance.bg_stipple)
2103         g_object_unref (values->appearance.bg_stipple);
2104
2105       if (values->appearance.fg_stipple)
2106         g_object_unref (values->appearance.fg_stipple);
2107
2108       if (values->tabs)
2109         pango_tab_array_free (values->tabs);
2110
2111       if (values->font)
2112         pango_font_description_free (values->font);
2113
2114       if (values->pg_bg_color)
2115         gdk_color_free (values->pg_bg_color);
2116
2117       g_free (values);
2118     }
2119 }
2120
2121 void
2122 _gtk_text_attributes_realize (GtkTextAttributes *values,
2123                               GdkColormap *cmap,
2124                               GdkVisual *visual)
2125 {
2126   g_return_if_fail (values != NULL);
2127   g_return_if_fail (values->refcount > 0);
2128   g_return_if_fail (!values->realized);
2129
2130   /* It is wrong to use this colormap, FIXME */
2131   gdk_colormap_alloc_color (cmap,
2132                             &values->appearance.fg_color,
2133                             FALSE, TRUE);
2134
2135   gdk_colormap_alloc_color (cmap,
2136                             &values->appearance.bg_color,
2137                             FALSE, TRUE);
2138
2139   if (values->pg_bg_color)
2140     gdk_colormap_alloc_color (cmap,
2141                               values->pg_bg_color,
2142                               FALSE, TRUE);
2143
2144   values->realized = TRUE;
2145 }
2146
2147 void
2148 _gtk_text_attributes_unrealize (GtkTextAttributes *values,
2149                                 GdkColormap *cmap,
2150                                 GdkVisual *visual)
2151 {
2152   g_return_if_fail (values != NULL);
2153   g_return_if_fail (values->refcount > 0);
2154   g_return_if_fail (values->realized);
2155
2156   gdk_colormap_free_colors (cmap,
2157                             &values->appearance.fg_color, 1);
2158
2159
2160   gdk_colormap_free_colors (cmap,
2161                             &values->appearance.bg_color, 1);
2162
2163   values->appearance.fg_color.pixel = 0;
2164   values->appearance.bg_color.pixel = 0;
2165
2166   if (values->pg_bg_color)
2167     {
2168       gdk_colormap_free_colors (cmap, values->pg_bg_color, 1);
2169       
2170       values->pg_bg_color->pixel = 0;
2171     }
2172
2173   values->realized = FALSE;
2174 }
2175
2176 void
2177 _gtk_text_attributes_fill_from_tags (GtkTextAttributes *dest,
2178                                      GtkTextTag**       tags,
2179                                      guint              n_tags)
2180 {
2181   guint n = 0;
2182
2183   guint left_margin_accumulative = 0;
2184   guint right_margin_accumulative = 0;
2185
2186   g_return_if_fail (!dest->realized);
2187
2188   while (n < n_tags)
2189     {
2190       GtkTextTag *tag = tags[n];
2191       GtkTextAttributes *vals = tag->values;
2192
2193       g_assert (tag->table != NULL);
2194       if (n > 0)
2195         g_assert (tags[n]->priority > tags[n-1]->priority);
2196
2197       if (tag->bg_color_set)
2198         {
2199           dest->appearance.bg_color = vals->appearance.bg_color;
2200
2201           dest->appearance.draw_bg = TRUE;
2202         }
2203       if (tag->fg_color_set)
2204         dest->appearance.fg_color = vals->appearance.fg_color;
2205       
2206       if (tag->pg_bg_color_set)
2207         {
2208           dest->pg_bg_color = gdk_color_copy (vals->pg_bg_color);
2209         }
2210
2211       if (tag->bg_stipple_set)
2212         {
2213           g_object_ref (vals->appearance.bg_stipple);
2214           if (dest->appearance.bg_stipple)
2215             g_object_unref (dest->appearance.bg_stipple);
2216           dest->appearance.bg_stipple = vals->appearance.bg_stipple;
2217
2218           dest->appearance.draw_bg = TRUE;
2219         }
2220
2221       if (tag->fg_stipple_set)
2222         {
2223           g_object_ref (vals->appearance.fg_stipple);
2224           if (dest->appearance.fg_stipple)
2225             g_object_unref (dest->appearance.fg_stipple);
2226           dest->appearance.fg_stipple = vals->appearance.fg_stipple;
2227         }
2228
2229       if (vals->font)
2230         {
2231           if (dest->font)
2232             pango_font_description_merge (dest->font, vals->font, TRUE);
2233           else
2234             dest->font = pango_font_description_copy (vals->font);
2235         }
2236
2237       /* multiply all the scales together to get a composite */
2238       if (tag->scale_set)
2239         dest->font_scale *= vals->font_scale;
2240       
2241       if (tag->justification_set)
2242         dest->justification = vals->justification;
2243
2244       if (vals->direction != GTK_TEXT_DIR_NONE)
2245         dest->direction = vals->direction;
2246
2247       if (tag->left_margin_set) 
2248         {
2249           if (tag->accumulative_margin)
2250             left_margin_accumulative += vals->left_margin;
2251           else
2252             dest->left_margin = vals->left_margin;
2253         }
2254
2255       if (tag->indent_set)
2256         dest->indent = vals->indent;
2257
2258       if (tag->rise_set)
2259         dest->appearance.rise = vals->appearance.rise;
2260
2261       if (tag->right_margin_set) 
2262         {
2263           if (tag->accumulative_margin)
2264             right_margin_accumulative += vals->right_margin;
2265           else
2266             dest->right_margin = vals->right_margin;
2267         }
2268
2269       if (tag->pixels_above_lines_set)
2270         dest->pixels_above_lines = vals->pixels_above_lines;
2271
2272       if (tag->pixels_below_lines_set)
2273         dest->pixels_below_lines = vals->pixels_below_lines;
2274
2275       if (tag->pixels_inside_wrap_set)
2276         dest->pixels_inside_wrap = vals->pixels_inside_wrap;
2277
2278       if (tag->tabs_set)
2279         {
2280           if (dest->tabs)
2281             pango_tab_array_free (dest->tabs);
2282           dest->tabs = pango_tab_array_copy (vals->tabs);
2283         }
2284
2285       if (tag->wrap_mode_set)
2286         dest->wrap_mode = vals->wrap_mode;
2287
2288       if (tag->underline_set)
2289         dest->appearance.underline = vals->appearance.underline;
2290
2291       if (tag->strikethrough_set)
2292         dest->appearance.strikethrough = vals->appearance.strikethrough;
2293
2294       if (tag->invisible_set)
2295         dest->invisible = vals->invisible;
2296
2297       if (tag->editable_set)
2298         dest->editable = vals->editable;
2299
2300       if (tag->bg_full_height_set)
2301         dest->bg_full_height = vals->bg_full_height;
2302
2303       if (tag->language_set)
2304         dest->language = vals->language;
2305
2306       ++n;
2307     }
2308
2309   dest->left_margin += left_margin_accumulative;
2310   dest->right_margin += right_margin_accumulative;
2311 }
2312
2313 gboolean
2314 _gtk_text_tag_affects_size (GtkTextTag *tag)
2315 {
2316   g_return_val_if_fail (GTK_IS_TEXT_TAG (tag), FALSE);
2317
2318   return
2319     (tag->values->font && pango_font_description_get_set_fields (tag->values->font) != 0) ||
2320     tag->scale_set ||
2321     tag->justification_set ||
2322     tag->left_margin_set ||
2323     tag->indent_set ||
2324     tag->rise_set ||
2325     tag->right_margin_set ||
2326     tag->pixels_above_lines_set ||
2327     tag->pixels_below_lines_set ||
2328     tag->pixels_inside_wrap_set ||
2329     tag->tabs_set ||
2330     tag->underline_set ||
2331     tag->wrap_mode_set ||
2332     tag->invisible_set;
2333 }
2334
2335 gboolean
2336 _gtk_text_tag_affects_nonsize_appearance (GtkTextTag *tag)
2337 {
2338   g_return_val_if_fail (GTK_IS_TEXT_TAG (tag), FALSE);
2339
2340   return
2341     tag->bg_color_set ||
2342     tag->bg_stipple_set ||
2343     tag->fg_color_set ||
2344     tag->fg_stipple_set ||
2345     tag->strikethrough_set ||
2346     tag->bg_full_height_set ||
2347     tag->pg_bg_color_set;
2348 }
2349
2350 #define __GTK_TEXT_TAG_C__
2351 #include "gtkaliasdef.c"