Code

finally getting closer to processing axes and contexts correctly
[inkscape.git] / src / libcroco / cr-cascade.c
1 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
3 /*
4  * This file is part of The Croco Library
5  *
6  * Copyright (C) 2002-2003 Dodji Seketeli <dodji@seketeli.org>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of version 2.1 of the 
10  * GNU Lesser General Public
11  * License as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the 
19  * GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  */
25 /*
26  *$Id: cr-cascade.c,v 1.6 2004/03/07 13:22:47 dodji Exp $
27  */
29 #include <string.h>
30 #include "cr-cascade.h"
32 #define PRIVATE(a_this) ((a_this)->priv)
34 struct _CRCascadePriv {
35  /**
36          *the 3 style sheets of the cascade:
37          *author, user, and useragent sheet.
38          *Intended to be addressed by
39          *sheets[ORIGIN_AUTHOR] or sheets[ORIGIN_USER]
40          *of sheets[ORIGIN_UA] ;
41          */
42         CRStyleSheet *sheets[3];
43         guint ref_count;
44 };
46 /**
47  *Constructor of the #CRCascade class.
48  *Note that all three parameters of this
49  *method are ref counted and their refcount is increased.
50  *Their refcount will be decreased at the destruction of
51  *the instance of #CRCascade.
52  *So the caller should not call their destructor. The caller
53  *should call their ref/unref method instead if it wants
54  *@param a_author_sheet the autor origin style sheet
55  *@param a_user_sheet the user origin style sheet.
56  *@param a_ua_sheet the user agent origin style sheet.
57  *@return the newly built instance of CRCascade or NULL if
58  *an error arose during constrution.
59  */
60 CRCascade *
61 cr_cascade_new (CRStyleSheet * a_author_sheet,
62                 CRStyleSheet * a_user_sheet, CRStyleSheet * a_ua_sheet)
63 {
64         CRCascade *result = NULL;
66         result = g_try_malloc (sizeof (CRCascade));
67         if (!result) {
68                 cr_utils_trace_info ("Out of memory");
69                 return NULL;
70         }
71         memset (result, 0, sizeof (CRCascade));
73         PRIVATE (result) = g_try_malloc (sizeof (CRCascadePriv));
74         if (!PRIVATE (result)) {
75                 cr_utils_trace_info ("Out of memory");
76                 return NULL;
77         }
78         memset (PRIVATE (result), 0, sizeof (CRCascadePriv));
80         if (a_author_sheet) {
81                 cr_cascade_set_sheet (result, a_author_sheet, ORIGIN_AUTHOR);
82         }
83         if (a_user_sheet) {
84                 cr_cascade_set_sheet (result, a_user_sheet, ORIGIN_USER);
85         }
86         if (a_ua_sheet) {
87                 cr_cascade_set_sheet (result, a_ua_sheet, ORIGIN_UA);
88         }
90         return result;
91 }
93 /**
94  *Gets a given origin sheet.
95  *Note that the returned stylesheet
96  *is refcounted so if the caller wants
97  *to manage its lifecycle, it must use
98  *cr_stylesheet_ref()/cr_stylesheet_unref() instead
99  *of the cr_stylesheet_destroy() method.
100  *@param a_this the current instance of #CRCascade.
101  *@param a_origin the origin of the style sheet as
102  *defined in the css2 spec in chapter 6.4.
103  *@return the style sheet, or NULL if it does not
104  *exist.
105  */
106 CRStyleSheet *
107 cr_cascade_get_sheet (CRCascade * a_this, enum CRStyleOrigin a_origin)
109         g_return_val_if_fail (a_this
110                               && (unsigned)a_origin < NB_ORIGINS, NULL);
112         return PRIVATE (a_this)->sheets[a_origin];
115 /**
116  *Sets a stylesheet in the cascade
117  *@param a_this the current instance of #CRCascade.
118  *@param a_sheet the stylesheet to set.
119  *@param a_origin the origin of the stylesheet.
120  *@return CR_OK upon successfull completion, an error
121  *code otherwise.
122  */
123 enum CRStatus
124 cr_cascade_set_sheet (CRCascade * a_this,
125                       CRStyleSheet * a_sheet, enum CRStyleOrigin a_origin)
127         g_return_val_if_fail (a_this
128                               && a_sheet
129                               && (unsigned)a_origin < NB_ORIGINS, CR_BAD_PARAM_ERROR);
131         if (PRIVATE (a_this)->sheets[a_origin])
132                 cr_stylesheet_unref (PRIVATE (a_this)->sheets[a_origin]);
133         PRIVATE (a_this)->sheets[a_origin] = a_sheet;
134         cr_stylesheet_ref (a_sheet);
135         a_sheet->origin = a_origin;
136         return CR_OK;
139 /**
140  *Increases the reference counter of the current instance
141  *of #CRCascade.
142  *@param a_this the current instance of #CRCascade
143  *
144  */
145 void
146 cr_cascade_ref (CRCascade * a_this)
148         g_return_if_fail (a_this && PRIVATE (a_this));
150         PRIVATE (a_this)->ref_count++;
153 /**
154  *Decrements the reference counter associated
155  *to this instance of #CRCascade. If the reference
156  *counter reaches zero, the instance is destroyed 
157  *using cr_cascade_destroy()
158  *@param a_this the current instance of 
159  *#CRCascade.
160  */
161 void
162 cr_cascade_unref (CRCascade * a_this)
164         g_return_if_fail (a_this && PRIVATE (a_this));
166         if (PRIVATE (a_this)->ref_count)
167                 PRIVATE (a_this)->ref_count--;
168         if (!PRIVATE (a_this)->ref_count) {
169                 cr_cascade_destroy (a_this);
170         }
173 /**
174  *Destructor of #CRCascade.
175  */
176 void
177 cr_cascade_destroy (CRCascade * a_this)
179         g_return_if_fail (a_this);
181         if (PRIVATE (a_this)) {
182                 gulong i = 0;
184                 for (i = 0; PRIVATE (a_this)->sheets && i < NB_ORIGINS; i++) {
185                         if (PRIVATE (a_this)->sheets[i]) {
186                                 if (cr_stylesheet_unref
187                                     (PRIVATE (a_this)->sheets[i])
188                                     == TRUE) {
189                                         PRIVATE (a_this)->sheets[i] = NULL;
190                                 }
191                         }
192                 }
193                 g_free (PRIVATE (a_this));
194                 PRIVATE (a_this) = NULL;
195         }
196         g_free (a_this);