Libcroco
cr-cascade.c
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
2 
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  */
24 
25 /*
26  *$Id$
27  */
28 
29 #include <string.h>
30 #include "cr-cascade.h"
31 
32 #define PRIVATE(a_this) ((a_this)->priv)
33 
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  */
43  guint ref_count;
44 };
45 
46 /**
47  * cr_cascade_new:
48  *@a_author_sheet: the author origin style sheet. May be NULL.
49  *@a_user_sheet: the user origin style sheet. May be NULL.
50  *@a_ua_sheet: the user agent origin style sheet. May be NULL.
51  *
52  *Constructor of the #CRCascade class.
53  *Note that all three parameters of this
54  *method are ref counted and their refcount is increased.
55  *Their refcount will be decreased at the destruction of
56  *the instance of #CRCascade.
57  *So the caller should not call their destructor. The caller
58  *should call their ref/unref method instead if it wants
59  *
60  *Returns the newly built instance of CRCascade or NULL if
61  *an error arose during constrution.
62  */
63 CRCascade *
64 cr_cascade_new (CRStyleSheet * a_author_sheet,
65  CRStyleSheet * a_user_sheet, CRStyleSheet * a_ua_sheet)
66 {
67  CRCascade *result = NULL;
68 
69  result = g_try_malloc (sizeof (CRCascade));
70  if (!result) {
71  cr_utils_trace_info ("Out of memory");
72  return NULL;
73  }
74  memset (result, 0, sizeof (CRCascade));
75 
76  PRIVATE (result) = g_try_malloc (sizeof (CRCascadePriv));
77  if (!PRIVATE (result)) {
78  cr_utils_trace_info ("Out of memory");
79  g_free (result);
80  return NULL;
81  }
82  memset (PRIVATE (result), 0, sizeof (CRCascadePriv));
83 
84  if (a_author_sheet) {
85  cr_cascade_set_sheet (result, a_author_sheet, ORIGIN_AUTHOR);
86  }
87  if (a_user_sheet) {
88  cr_cascade_set_sheet (result, a_user_sheet, ORIGIN_USER);
89  }
90  if (a_ua_sheet) {
91  cr_cascade_set_sheet (result, a_ua_sheet, ORIGIN_UA);
92  }
93 
94  return result;
95 }
96 
97 /**
98  * cr_cascade_get_sheet:
99  *@a_this: the current instance of #CRCascade.
100  *@a_origin: the origin of the style sheet as
101  *defined in the css2 spec in chapter 6.4.
102  *Gets a given origin sheet.
103  *
104  *Gets a sheet, part of the cascade.
105  *Note that the returned stylesheet
106  *is refcounted so if the caller wants
107  *to manage it's lifecycle, it must use
108  *cr_stylesheet_ref()/cr_stylesheet_unref() instead
109  *of the cr_stylesheet_destroy() method.
110  *Returns the style sheet, or NULL if it does not
111  *exist.
112  */
113 CRStyleSheet *
115 {
116  g_return_val_if_fail (a_this
117  && a_origin >= ORIGIN_UA
118  && a_origin < NB_ORIGINS, NULL);
119 
120  return PRIVATE (a_this)->sheets[a_origin];
121 }
122 
123 /**
124  * cr_cascade_set_sheet:
125  *@a_this: the current instance of #CRCascade.
126  *@a_sheet: the stylesheet to set.
127  *@a_origin: the origin of the stylesheet.
128  *
129  *Sets a stylesheet in the cascade
130  *
131  *Returns CR_OK upon successfull completion, an error
132  *code otherwise.
133  */
134 enum CRStatus
136  CRStyleSheet * a_sheet, enum CRStyleOrigin a_origin)
137 {
138  g_return_val_if_fail (a_this
139  && a_sheet
140  && a_origin >= ORIGIN_UA
141  && a_origin < NB_ORIGINS, CR_BAD_PARAM_ERROR);
142 
143  if (PRIVATE (a_this)->sheets[a_origin])
144  cr_stylesheet_unref (PRIVATE (a_this)->sheets[a_origin]);
145  PRIVATE (a_this)->sheets[a_origin] = a_sheet;
146  cr_stylesheet_ref (a_sheet);
147  a_sheet->origin = a_origin;
148  return CR_OK;
149 }
150 
151 /**
152  *cr_cascade_ref:
153  *@a_this: the current instance of #CRCascade
154  *
155  *Increases the reference counter of the current instance
156  *of #CRCascade.
157  */
158 void
160 {
161  g_return_if_fail (a_this && PRIVATE (a_this));
162 
163  PRIVATE (a_this)->ref_count++;
164 }
165 
166 /**
167  * cr_cascade_unref:
168  *@a_this: the current instance of
169  *#CRCascade.
170  *
171  *Decrements the reference counter associated
172  *to this instance of #CRCascade. If the reference
173  *counter reaches zero, the instance is destroyed
174  *using cr_cascade_destroy()
175  */
176 void
178 {
179  g_return_if_fail (a_this && PRIVATE (a_this));
180 
181  if (PRIVATE (a_this)->ref_count)
182  PRIVATE (a_this)->ref_count--;
183  if (!PRIVATE (a_this)->ref_count) {
184  cr_cascade_destroy (a_this);
185  }
186 }
187 
188 /**
189  * cr_cascade_destroy:
190  * @a_this: the current instance of #CRCascade
191  *
192  * Destructor of #CRCascade.
193  */
194 void
196 {
197  g_return_if_fail (a_this);
198 
199  if (PRIVATE (a_this)) {
200  gulong i = 0;
201 
202  for (i = 0; PRIVATE (a_this)->sheets && i < NB_ORIGINS; i++) {
203  if (PRIVATE (a_this)->sheets[i]) {
205  (PRIVATE (a_this)->sheets[i])
206  == TRUE) {
207  PRIVATE (a_this)->sheets[i] = NULL;
208  }
209  }
210  }
211  g_free (PRIVATE (a_this));
212  PRIVATE (a_this) = NULL;
213  }
214  g_free (a_this);
215 }
_CRCascadePriv::ref_count
guint ref_count
Definition: cr-cascade.c:43
ORIGIN_USER
@ ORIGIN_USER
Definition: cr-stylesheet.h:46
cr_cascade_set_sheet
enum CRStatus cr_cascade_set_sheet(CRCascade *a_this, CRStyleSheet *a_sheet, enum CRStyleOrigin a_origin)
cr_cascade_set_sheet: @a_this: the current instance of CRCascade.
Definition: cr-cascade.c:135
_CRStyleSheet
An abstraction of a css stylesheet as defined by the css2 spec in chapter 4.
Definition: cr-stylesheet.h:57
CR_BAD_PARAM_ERROR
@ CR_BAD_PARAM_ERROR
Definition: cr-utils.h:45
CRCascadePriv
typedefG_BEGIN_DECLS struct _CRCascadePriv CRCascadePriv
Definition: cr-cascade.h:41
cr_stylesheet_ref
void cr_stylesheet_ref(CRStyleSheet *a_this)
Definition: cr-stylesheet.c:141
_CRCascadePriv::sheets
CRStyleSheet * sheets[3]
the 3 style sheets of the cascade: author, user, and useragent sheet.
Definition: cr-cascade.c:42
ORIGIN_AUTHOR
@ ORIGIN_AUTHOR
Definition: cr-stylesheet.h:47
CRStyleOrigin
CRStyleOrigin
Definition: cr-stylesheet.h:38
CR_OK
@ CR_OK
Definition: cr-utils.h:44
cr-cascade.h
cr_cascade_new
CRCascade * cr_cascade_new(CRStyleSheet *a_author_sheet, CRStyleSheet *a_user_sheet, CRStyleSheet *a_ua_sheet)
cr_cascade_new: @a_author_sheet: the author origin style sheet.
Definition: cr-cascade.c:64
cr_cascade_destroy
void cr_cascade_destroy(CRCascade *a_this)
cr_cascade_destroy: @a_this: the current instance of CRCascade
Definition: cr-cascade.c:195
_CRCascade
Definition: cr-cascade.h:49
cr_stylesheet_unref
gboolean cr_stylesheet_unref(CRStyleSheet *a_this)
Definition: cr-stylesheet.c:149
PRIVATE
#define PRIVATE(a_this)
Definition: cr-cascade.c:32
ORIGIN_UA
@ ORIGIN_UA
Definition: cr-stylesheet.h:45
cr_cascade_ref
void cr_cascade_ref(CRCascade *a_this)
cr_cascade_ref: @a_this: the current instance of CRCascade
Definition: cr-cascade.c:159
cr_cascade_get_sheet
CRStyleSheet * cr_cascade_get_sheet(CRCascade *a_this, enum CRStyleOrigin a_origin)
cr_cascade_get_sheet: @a_this: the current instance of CRCascade.
Definition: cr-cascade.c:114
CRStatus
CRStatus
The status type returned by the methods of the croco library.
Definition: cr-utils.h:43
_CRCascadePriv
Definition: cr-cascade.c:34
cr_cascade_unref
void cr_cascade_unref(CRCascade *a_this)
cr_cascade_unref: @a_this: the current instance of CRCascade.
Definition: cr-cascade.c:177
cr_utils_trace_info
#define cr_utils_trace_info(a_msg)
Traces an info message.
Definition: cr-utils.h:127
NB_ORIGINS
@ NB_ORIGINS
Definition: cr-stylesheet.h:50
_CRStyleSheet::origin
enum CRStyleOrigin origin
Definition: cr-stylesheet.h:62