Code

Unify stat type for older glib.
[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 = (CRCascade *)g_try_malloc (sizeof (CRCascade));
65         if (!result) {
66                 cr_utils_trace_info ("Out of memory");
67                 return NULL;
68         }
69         memset (result, 0, sizeof (CRCascade));
71         PRIVATE (result) = (CRCascadePriv *)g_try_malloc (sizeof (CRCascadePriv));
72         if (!PRIVATE (result)) {
73                 g_free(result);
74                 cr_utils_trace_info ("Out of memory");
75                 return NULL;
76         }
77         memset (PRIVATE (result), 0, sizeof (CRCascadePriv));
79         if (a_author_sheet) {
80                 cr_cascade_set_sheet (result, a_author_sheet, ORIGIN_AUTHOR);
81         }
82         if (a_user_sheet) {
83                 cr_cascade_set_sheet (result, a_user_sheet, ORIGIN_USER);
84         }
85         if (a_ua_sheet) {
86                 cr_cascade_set_sheet (result, a_ua_sheet, ORIGIN_UA);
87         }
89         return result;
90 }
92 /**
93  *Gets a given origin sheet.
94  *Note that the returned stylesheet
95  *is refcounted so if the caller wants
96  *to manage its lifecycle, it must use
97  *cr_stylesheet_ref()/cr_stylesheet_unref() instead
98  *of the cr_stylesheet_destroy() method.
99  *@param a_this the current instance of #CRCascade.
100  *@param a_origin the origin of the style sheet as
101  *defined in the css2 spec in chapter 6.4.
102  *@return the style sheet, or NULL if it does not
103  *exist.
104  */
105 CRStyleSheet *
106 cr_cascade_get_sheet (CRCascade * a_this, enum CRStyleOrigin a_origin)
108         g_return_val_if_fail (a_this
109                               && (unsigned)a_origin < NB_ORIGINS, NULL);
111         return PRIVATE (a_this)->sheets[a_origin];
114 /**
115  *Sets a stylesheet in the cascade
116  *@param a_this the current instance of #CRCascade.
117  *@param a_sheet the stylesheet to set.
118  *@param a_origin the origin of the stylesheet.
119  *@return CR_OK upon successfull completion, an error
120  *code otherwise.
121  */
122 enum CRStatus
123 cr_cascade_set_sheet (CRCascade * a_this,
124                       CRStyleSheet * a_sheet, enum CRStyleOrigin a_origin)
126         g_return_val_if_fail (a_this
127                               && a_sheet
128                               && (unsigned)a_origin < NB_ORIGINS, CR_BAD_PARAM_ERROR);
130         if (PRIVATE (a_this)->sheets[a_origin])
131                 cr_stylesheet_unref (PRIVATE (a_this)->sheets[a_origin]);
132         PRIVATE (a_this)->sheets[a_origin] = a_sheet;
133         cr_stylesheet_ref (a_sheet);
134         a_sheet->origin = a_origin;
135         return CR_OK;
138 /**
139  *Increases the reference counter of the current instance
140  *of #CRCascade.
141  *@param a_this the current instance of #CRCascade
142  *
143  */
144 void
145 cr_cascade_ref (CRCascade * a_this)
147         g_return_if_fail (a_this && PRIVATE (a_this));
149         PRIVATE (a_this)->ref_count++;
152 /**
153  *Decrements the reference counter associated
154  *to this instance of #CRCascade. If the reference
155  *counter reaches zero, the instance is destroyed 
156  *using cr_cascade_destroy()
157  *@param a_this the current instance of 
158  *#CRCascade.
159  */
160 void
161 cr_cascade_unref (CRCascade * a_this)
163         g_return_if_fail (a_this && PRIVATE (a_this));
165         if (PRIVATE (a_this)->ref_count)
166                 PRIVATE (a_this)->ref_count--;
167         if (!PRIVATE (a_this)->ref_count) {
168                 cr_cascade_destroy (a_this);
169         }
172 /**
173  *Destructor of #CRCascade.
174  */
175 void
176 cr_cascade_destroy (CRCascade * a_this)
178         g_return_if_fail (a_this);
180         if (PRIVATE (a_this)) {
181                 gulong i = 0;
183                 for (i = 0; PRIVATE (a_this)->sheets && i < NB_ORIGINS; i++) {
184                         if (PRIVATE (a_this)->sheets[i]) {
185                                 if (cr_stylesheet_unref
186                                     (PRIVATE (a_this)->sheets[i])
187                                     == TRUE) {
188                                         PRIVATE (a_this)->sheets[i] = NULL;
189                                 }
190                         }
191                 }
192                 g_free (PRIVATE (a_this));
193                 PRIVATE (a_this) = NULL;
194         }
195         g_free (a_this);