Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

cr-cascade.c

Go to the documentation of this file.
00001 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
00002 
00003 /*
00004  * This file is part of The Croco Library
00005  *
00006  * Copyright (C) 2002-2003 Dodji Seketeli <dodji@seketeli.org>
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of version 2.1 of the 
00010  * GNU Lesser General Public
00011  * License as published by the Free Software Foundation.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the 
00019  * GNU Lesser General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00022  * USA
00023  */
00024 
00025 /*
00026  *$Id: cr-cascade.c,v 1.6 2004/03/07 13:22:47 dodji Exp $
00027  */
00028 
00029 #include <string.h>
00030 #include "cr-cascade.h"
00031 
00032 #define PRIVATE(a_this) ((a_this)->priv)
00033 
00034 struct _CRCascadePriv {
00035  /**
00036          *the 3 style sheets of the cascade:
00037          *author, user, and useragent sheet.
00038          *Intended to be addressed by
00039          *sheets[ORIGIN_AUTHOR] or sheets[ORIGIN_USER]
00040          *of sheets[ORIGIN_UA] ;
00041          */
00042         CRStyleSheet *sheets[3];
00043         guint ref_count;
00044 };
00045 
00046 /**
00047  *Constructor of the #CRCascade class.
00048  *Note that all three parameters of this
00049  *method are ref counted and their refcount is increased.
00050  *Their refcount will be decreased at the destruction of
00051  *the instance of #CRCascade.
00052  *So the caller should not call their destructor. The caller
00053  *should call their ref/unref method instead if it wants
00054  *@param a_author_sheet the autor origin style sheet
00055  *@param a_user_sheet the user origin style sheet.
00056  *@param a_ua_sheet the user agent origin style sheet.
00057  *@return the newly built instance of CRCascade or NULL if
00058  *an error arose during constrution.
00059  */
00060 CRCascade *
00061 cr_cascade_new (CRStyleSheet * a_author_sheet,
00062                 CRStyleSheet * a_user_sheet, CRStyleSheet * a_ua_sheet)
00063 {
00064         CRCascade *result = NULL;
00065 
00066         result = g_try_malloc (sizeof (CRCascade));
00067         if (!result) {
00068                 cr_utils_trace_info ("Out of memory");
00069                 return NULL;
00070         }
00071         memset (result, 0, sizeof (CRCascade));
00072 
00073         PRIVATE (result) = g_try_malloc (sizeof (CRCascadePriv));
00074         if (!PRIVATE (result)) {
00075                 cr_utils_trace_info ("Out of memory");
00076                 return NULL;
00077         }
00078         memset (PRIVATE (result), 0, sizeof (CRCascadePriv));
00079 
00080         if (a_author_sheet) {
00081                 cr_cascade_set_sheet (result, a_author_sheet, ORIGIN_AUTHOR);
00082         }
00083         if (a_user_sheet) {
00084                 cr_cascade_set_sheet (result, a_user_sheet, ORIGIN_USER);
00085         }
00086         if (a_ua_sheet) {
00087                 cr_cascade_set_sheet (result, a_ua_sheet, ORIGIN_UA);
00088         }
00089 
00090         return result;
00091 }
00092 
00093 /**
00094  *Gets a given origin sheet.
00095  *Note that the returned stylesheet
00096  *is refcounted so if the caller wants
00097  *to manage it's lifecycle, it must use
00098  *cr_stylesheet_ref()/cr_stylesheet_unref() instead
00099  *of the cr_stylesheet_destroy() method.
00100  *@param a_this the current instance of #CRCascade.
00101  *@param a_origin the origin of the style sheet as
00102  *defined in the css2 spec in chapter 6.4.
00103  *@return the style sheet, or NULL if it does not
00104  *exist.
00105  */
00106 CRStyleSheet *
00107 cr_cascade_get_sheet (CRCascade * a_this, enum CRStyleOrigin a_origin)
00108 {
00109         g_return_val_if_fail (a_this
00110                               && a_origin >= ORIGIN_UA
00111                               && a_origin < NB_ORIGINS, NULL);
00112 
00113         return PRIVATE (a_this)->sheets[a_origin];
00114 }
00115 
00116 /**
00117  *Sets a stylesheet in the cascade
00118  *@param a_this the current instance of #CRCascade.
00119  *@param a_sheet the stylesheet to set.
00120  *@param a_origin the origin of the stylesheet.
00121  *@return CR_OK upon successfull completion, an error
00122  *code otherwise.
00123  */
00124 enum CRStatus
00125 cr_cascade_set_sheet (CRCascade * a_this,
00126                       CRStyleSheet * a_sheet, enum CRStyleOrigin a_origin)
00127 {
00128         g_return_val_if_fail (a_this
00129                               && a_sheet
00130                               && a_origin >= ORIGIN_UA
00131                               && a_origin < NB_ORIGINS, CR_BAD_PARAM_ERROR);
00132 
00133         if (PRIVATE (a_this)->sheets[a_origin])
00134                 cr_stylesheet_unref (PRIVATE (a_this)->sheets[a_origin]);
00135         PRIVATE (a_this)->sheets[a_origin] = a_sheet;
00136         cr_stylesheet_ref (a_sheet);
00137         a_sheet->origin = a_origin;
00138         return CR_OK;
00139 }
00140 
00141 /**
00142  *Increases the reference counter of the current instance
00143  *of #CRCascade.
00144  *@param a_this the current instance of #CRCascade
00145  *
00146  */
00147 void
00148 cr_cascade_ref (CRCascade * a_this)
00149 {
00150         g_return_if_fail (a_this && PRIVATE (a_this));
00151 
00152         PRIVATE (a_this)->ref_count++;
00153 }
00154 
00155 /**
00156  *Decrements the reference counter associated
00157  *to this instance of #CRCascade. If the reference
00158  *counter reaches zero, the instance is destroyed 
00159  *using cr_cascade_destroy()
00160  *@param a_this the current instance of 
00161  *#CRCascade.
00162  */
00163 void
00164 cr_cascade_unref (CRCascade * a_this)
00165 {
00166         g_return_if_fail (a_this && PRIVATE (a_this));
00167 
00168         if (PRIVATE (a_this)->ref_count)
00169                 PRIVATE (a_this)->ref_count--;
00170         if (!PRIVATE (a_this)->ref_count) {
00171                 cr_cascade_destroy (a_this);
00172         }
00173 }
00174 
00175 /**
00176  *Destructor of #CRCascade.
00177  */
00178 void
00179 cr_cascade_destroy (CRCascade * a_this)
00180 {
00181         g_return_if_fail (a_this);
00182 
00183         if (PRIVATE (a_this)) {
00184                 gulong i = 0;
00185 
00186                 for (i = 0; PRIVATE (a_this)->sheets && i < NB_ORIGINS; i++) {
00187                         if (PRIVATE (a_this)->sheets[i]) {
00188                                 if (cr_stylesheet_unref
00189                                     (PRIVATE (a_this)->sheets[i])
00190                                     == TRUE) {
00191                                         PRIVATE (a_this)->sheets[i] = NULL;
00192                                 }
00193                         }
00194                 }
00195                 g_free (PRIVATE (a_this));
00196                 PRIVATE (a_this) = NULL;
00197         }
00198         g_free (a_this);
00199 }

Generated on Fri Oct 29 08:29:11 2004 for Libcroco by  doxygen 1.3.9.1