Main Page | Alphabetical List | Data Structures | 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.3 2003/05/24 10:42:42 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         /**
00037          *the 3 style sheets of the cascade:
00038          *author, user, and useragent sheet.
00039          *Intended to be addressed by
00040          *sheets[ORIGIN_AUTHOR] or sheets[ORIGIN_USER]
00041          *of sheets[ORIGIN_UA] ;
00042          */
00043         CRStyleSheet *sheets[3] ;
00044 } ;
00045 
00046 
00047 /**
00048  *Constructor of the #CRCascade class.
00049  *Note that all three parameters of this
00050  *method are ref counted and their refcount is increased.
00051  *Their refcount will be decreased at the destruction of
00052  *the instance of #CRCascade.
00053  *So the caller should not call their destructor. The caller
00054  *should call their ref/unref method instead if it wants
00055  *@param a_author_sheet the autor origin style sheet
00056  *@param a_user_sheet the user origin style sheet.
00057  *@param a_ua_sheet the user agent origin style sheet.
00058  *@return the newly built instance of CRCascade or NULL if
00059  *an error arose during constrution.
00060  */
00061 CRCascade *
00062 cr_cascade_new (CRStyleSheet *a_author_sheet,
00063                 CRStyleSheet *a_user_sheet,
00064                 CRStyleSheet *a_ua_sheet)
00065 {
00066         CRCascade *result = NULL ;
00067 
00068         result = g_try_malloc (sizeof (CRCascade)) ;
00069         if (!result)
00070         {
00071                 cr_utils_trace_info ("Out of memory") ;
00072                 return NULL ;
00073         }
00074         memset (result, 0, sizeof (CRCascade)) ;
00075 
00076         PRIVATE (result) = g_try_malloc (sizeof (CRCascadePriv)) ;
00077         if (!PRIVATE (result))
00078         {
00079                 cr_utils_trace_info ("Out of memory") ;
00080                 return NULL ;
00081         }
00082         memset (PRIVATE (result), 0, sizeof (CRCascadePriv)) ;
00083 
00084         if (a_author_sheet)
00085         {
00086                 cr_cascade_set_sheet (result, a_author_sheet,
00087                                       ORIGIN_AUTHOR) ;
00088         }
00089         if (a_user_sheet)
00090         {
00091                 cr_cascade_set_sheet (result, a_user_sheet,
00092                                       ORIGIN_USER) ;
00093         }
00094         if (a_ua_sheet)
00095         {
00096                 cr_cascade_set_sheet (result, a_user_sheet,
00097                                       ORIGIN_UA) ;
00098         }
00099 
00100         return result ;
00101 }
00102 
00103 /**
00104  *Gets a given origin sheet.
00105  *Note that the returned stylesheet
00106  *is refcounted so if the caller wants
00107  *to manage it's lifecycle, it must use
00108  *cr_stylesheet_ref()/cr_stylesheet_unref() instead
00109  *of the cr_stylesheet_destroy() method.
00110  *@param a_this the current instance of #CRCascade.
00111  *@param a_origin the origin of the style sheet as
00112  *defined in the css2 spec in chapter 6.4.
00113  *@return the style sheet, or NULL if it does not
00114  *exist.
00115  */
00116 CRStyleSheet *
00117 cr_cascade_get_sheet (CRCascade *a_this,
00118                       enum CRStyleOrigin a_origin)
00119 {
00120         g_return_val_if_fail (a_this
00121                               && a_origin >= ORIGIN_UA
00122                               && a_origin < NB_ORIGINS,
00123                               NULL) ;
00124 
00125         return PRIVATE (a_this)->sheets[a_origin] ;
00126 }
00127 
00128 
00129 /**
00130  *Sets a stylesheet in the cascade
00131  *@param a_this the current instance of #CRCascade.
00132  *@param a_sheet the stylesheet to set.
00133  *@param a_origin the origin of the stylesheet.
00134  *@return CR_OK upon successfull completion, an error
00135  *code otherwise.
00136  */
00137 enum CRStatus
00138 cr_cascade_set_sheet (CRCascade *a_this,
00139                       CRStyleSheet *a_sheet,
00140                       enum CRStyleOrigin a_origin)
00141 {
00142         g_return_val_if_fail (a_this
00143                               && a_sheet
00144                               && a_origin >= ORIGIN_UA
00145                               && a_origin < NB_ORIGINS,
00146                               CR_BAD_PARAM_ERROR) ;
00147 
00148         if (PRIVATE (a_this)->sheets[a_origin])
00149                 cr_stylesheet_unref 
00150                         (PRIVATE (a_this)->sheets[a_origin]) ;
00151         PRIVATE (a_this)->sheets[a_origin] = a_sheet ;
00152         cr_stylesheet_ref (a_sheet) ;
00153         a_sheet->origin = a_origin ;
00154         return CR_OK ;
00155 }
00156 
00157 
00158 /**
00159  *Destructor of #CRCascade.
00160  */
00161 void
00162 cr_cascade_destroy (CRCascade *a_this)
00163 {
00164         g_return_if_fail (a_this) ;
00165 
00166         if (PRIVATE (a_this))
00167         {
00168                 gulong i = 0 ;
00169 
00170                 for (i = 0 ; 
00171                      PRIVATE (a_this)->sheets
00172                              && i < NB_ORIGINS ; 
00173                      i++)
00174                 {
00175                         if (PRIVATE (a_this)->sheets[i])
00176                         {
00177                                 if (cr_stylesheet_unref 
00178                                     (PRIVATE (a_this)->sheets[i]) 
00179                                     == TRUE)
00180                                 {
00181                                         PRIVATE (a_this)->sheets[i] = 
00182                                                 NULL ;
00183                                 }
00184                         }
00185                 }
00186                 g_free (PRIVATE (a_this)) ;
00187                 PRIVATE (a_this) = NULL ;
00188         }
00189         g_free (a_this) ;
00190 }

Generated on Wed Oct 1 01:36:45 2003 for Libcroco by doxygen 1.3.3