Blame gnulib-local/lib/libcroco/cr-cascade.c

Packit Bot 06c835
/* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset: 8-*- */
Packit Bot 06c835
Packit Bot 06c835
/*
Packit Bot 06c835
 * This file is part of The Croco Library
Packit Bot 06c835
 *
Packit Bot 06c835
 * Copyright (C) 2002-2003 Dodji Seketeli <dodji@seketeli.org>
Packit Bot 06c835
 *
Packit Bot 06c835
 * This program is free software; you can redistribute it and/or
Packit Bot 06c835
 * modify it under the terms of version 2.1 of the 
Packit Bot 06c835
 * GNU Lesser General Public
Packit Bot 06c835
 * License as published by the Free Software Foundation.
Packit Bot 06c835
 *
Packit Bot 06c835
 * This program is distributed in the hope that it will be useful,
Packit Bot 06c835
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot 06c835
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Bot 06c835
 * GNU General Public License for more details.
Packit Bot 06c835
 *
Packit Bot 06c835
 * You should have received a copy of the 
Packit Bot 06c835
 * GNU Lesser General Public License
Packit Bot 06c835
 * along with this program; if not, write to the Free Software
Packit Bot 06c835
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
Packit Bot 06c835
 * USA
Packit Bot 06c835
 */
Packit Bot 06c835
Packit Bot 06c835
/*
Packit Bot 06c835
 *$Id: cr-cascade.c,v 1.7 2005/05/10 19:48:56 dodji Exp $
Packit Bot 06c835
 */
Packit Bot 06c835
Packit Bot 06c835
#include <config.h>
Packit Bot 06c835
#include <string.h>
Packit Bot 06c835
#include "cr-cascade.h"
Packit Bot 06c835
Packit Bot 06c835
#define PRIVATE(a_this) ((a_this)->priv)
Packit Bot 06c835
Packit Bot 06c835
struct _CRCascadePriv {
Packit Bot 06c835
 /**
Packit Bot 06c835
	 *the 3 style sheets of the cascade:
Packit Bot 06c835
	 *author, user, and useragent sheet.
Packit Bot 06c835
	 *Intended to be addressed by
Packit Bot 06c835
	 *sheets[ORIGIN_AUTHOR] or sheets[ORIGIN_USER]
Packit Bot 06c835
	 *of sheets[ORIGIN_UA] ;
Packit Bot 06c835
	 */
Packit Bot 06c835
        CRStyleSheet *sheets[3];
Packit Bot 06c835
        guint ref_count;
Packit Bot 06c835
};
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * cr_cascade_new:
Packit Bot 06c835
 *@a_author_sheet: the author origin style sheet.  May be NULL.
Packit Bot 06c835
 *@a_user_sheet: the user origin style sheet.  May be NULL.
Packit Bot 06c835
 *@a_ua_sheet: the user agent origin style sheet.  May be NULL.
Packit Bot 06c835
 *
Packit Bot 06c835
 *Constructor of the #CRCascade class.
Packit Bot 06c835
 *Note that all three parameters of this
Packit Bot 06c835
 *method are ref counted and their refcount is increased.
Packit Bot 06c835
 *Their refcount will be decreased at the destruction of
Packit Bot 06c835
 *the instance of #CRCascade.
Packit Bot 06c835
 *So the caller should not call their destructor. The caller
Packit Bot 06c835
 *should call their ref/unref method instead if it wants
Packit Bot 06c835
 *
Packit Bot 06c835
 *Returns the newly built instance of CRCascade or NULL if
Packit Bot 06c835
 *an error arose during constrution.
Packit Bot 06c835
 */
Packit Bot 06c835
CRCascade *
Packit Bot 06c835
cr_cascade_new (CRStyleSheet * a_author_sheet,
Packit Bot 06c835
                CRStyleSheet * a_user_sheet, CRStyleSheet * a_ua_sheet)
Packit Bot 06c835
{
Packit Bot 06c835
        CRCascade *result = NULL;
Packit Bot 06c835
Packit Bot 06c835
        result = g_try_malloc (sizeof (CRCascade));
Packit Bot 06c835
        if (!result) {
Packit Bot 06c835
                cr_utils_trace_info ("Out of memory");
Packit Bot 06c835
                return NULL;
Packit Bot 06c835
        }
Packit Bot 06c835
        memset (result, 0, sizeof (CRCascade));
Packit Bot 06c835
Packit Bot 06c835
        PRIVATE (result) = g_try_malloc (sizeof (CRCascadePriv));
Packit Bot 06c835
        if (!PRIVATE (result)) {
Packit Bot 06c835
                cr_utils_trace_info ("Out of memory");
Packit Bot 06c835
                return NULL;
Packit Bot 06c835
        }
Packit Bot 06c835
        memset (PRIVATE (result), 0, sizeof (CRCascadePriv));
Packit Bot 06c835
Packit Bot 06c835
        if (a_author_sheet) {
Packit Bot 06c835
                cr_cascade_set_sheet (result, a_author_sheet, ORIGIN_AUTHOR);
Packit Bot 06c835
        }
Packit Bot 06c835
        if (a_user_sheet) {
Packit Bot 06c835
                cr_cascade_set_sheet (result, a_user_sheet, ORIGIN_USER);
Packit Bot 06c835
        }
Packit Bot 06c835
        if (a_ua_sheet) {
Packit Bot 06c835
                cr_cascade_set_sheet (result, a_ua_sheet, ORIGIN_UA);
Packit Bot 06c835
        }
Packit Bot 06c835
Packit Bot 06c835
        return result;
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * cr_cascade_get_sheet:
Packit Bot 06c835
 *@a_this: the current instance of #CRCascade.
Packit Bot 06c835
 *@a_origin: the origin of the style sheet as
Packit Bot 06c835
 *defined in the css2 spec in chapter 6.4.
Packit Bot 06c835
 *Gets a given origin sheet.
Packit Bot 06c835
 *
Packit Bot 06c835
 *Gets a sheet, part of the cascade.
Packit Bot 06c835
 *Note that the returned stylesheet
Packit Bot 06c835
 *is refcounted so if the caller wants
Packit Bot 06c835
 *to manage it's lifecycle, it must use
Packit Bot 06c835
 *cr_stylesheet_ref()/cr_stylesheet_unref() instead
Packit Bot 06c835
 *of the cr_stylesheet_destroy() method.
Packit Bot 06c835
 *Returns the style sheet, or NULL if it does not
Packit Bot 06c835
 *exist.
Packit Bot 06c835
 */
Packit Bot 06c835
CRStyleSheet *
Packit Bot 06c835
cr_cascade_get_sheet (CRCascade * a_this, enum CRStyleOrigin a_origin)
Packit Bot 06c835
{
Packit Bot 06c835
        g_return_val_if_fail (a_this
Packit Bot 06c835
                              && a_origin >= ORIGIN_UA
Packit Bot 06c835
                              && a_origin < NB_ORIGINS, NULL);
Packit Bot 06c835
Packit Bot 06c835
        return PRIVATE (a_this)->sheets[a_origin];
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * cr_cascade_set_sheet:
Packit Bot 06c835
 *@a_this: the current instance of #CRCascade.
Packit Bot 06c835
 *@a_sheet: the stylesheet to set.
Packit Bot 06c835
 *@a_origin: the origin of the stylesheet.
Packit Bot 06c835
 *
Packit Bot 06c835
 *Sets a stylesheet in the cascade
Packit Bot 06c835
 *
Packit Bot 06c835
 *Returns CR_OK upon successfull completion, an error
Packit Bot 06c835
 *code otherwise.
Packit Bot 06c835
 */
Packit Bot 06c835
enum CRStatus
Packit Bot 06c835
cr_cascade_set_sheet (CRCascade * a_this,
Packit Bot 06c835
                      CRStyleSheet * a_sheet, enum CRStyleOrigin a_origin)
Packit Bot 06c835
{
Packit Bot 06c835
        g_return_val_if_fail (a_this
Packit Bot 06c835
                              && a_sheet
Packit Bot 06c835
                              && a_origin >= ORIGIN_UA
Packit Bot 06c835
                              && a_origin < NB_ORIGINS, CR_BAD_PARAM_ERROR);
Packit Bot 06c835
Packit Bot 06c835
        if (PRIVATE (a_this)->sheets[a_origin])
Packit Bot 06c835
                cr_stylesheet_unref (PRIVATE (a_this)->sheets[a_origin]);
Packit Bot 06c835
        PRIVATE (a_this)->sheets[a_origin] = a_sheet;
Packit Bot 06c835
        cr_stylesheet_ref (a_sheet);
Packit Bot 06c835
        a_sheet->origin = a_origin;
Packit Bot 06c835
        return CR_OK;
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 *cr_cascade_ref:
Packit Bot 06c835
 *@a_this: the current instance of #CRCascade
Packit Bot 06c835
 *
Packit Bot 06c835
 *Increases the reference counter of the current instance
Packit Bot 06c835
 *of #CRCascade.
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
cr_cascade_ref (CRCascade * a_this)
Packit Bot 06c835
{
Packit Bot 06c835
        g_return_if_fail (a_this && PRIVATE (a_this));
Packit Bot 06c835
Packit Bot 06c835
        PRIVATE (a_this)->ref_count++;
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * cr_cascade_unref:
Packit Bot 06c835
 *@a_this: the current instance of 
Packit Bot 06c835
 *#CRCascade.
Packit Bot 06c835
 *
Packit Bot 06c835
 *Decrements the reference counter associated
Packit Bot 06c835
 *to this instance of #CRCascade. If the reference
Packit Bot 06c835
 *counter reaches zero, the instance is destroyed 
Packit Bot 06c835
 *using cr_cascade_destroy()
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
cr_cascade_unref (CRCascade * a_this)
Packit Bot 06c835
{
Packit Bot 06c835
        g_return_if_fail (a_this && PRIVATE (a_this));
Packit Bot 06c835
Packit Bot 06c835
        if (PRIVATE (a_this)->ref_count)
Packit Bot 06c835
                PRIVATE (a_this)->ref_count--;
Packit Bot 06c835
        if (!PRIVATE (a_this)->ref_count) {
Packit Bot 06c835
                cr_cascade_destroy (a_this);
Packit Bot 06c835
        }
Packit Bot 06c835
}
Packit Bot 06c835
Packit Bot 06c835
/**
Packit Bot 06c835
 * cr_cascade_destroy:
Packit Bot 06c835
 * @a_this: the current instance of #CRCascade
Packit Bot 06c835
 *
Packit Bot 06c835
 * Destructor of #CRCascade.
Packit Bot 06c835
 */
Packit Bot 06c835
void
Packit Bot 06c835
cr_cascade_destroy (CRCascade * a_this)
Packit Bot 06c835
{
Packit Bot 06c835
        g_return_if_fail (a_this);
Packit Bot 06c835
Packit Bot 06c835
        if (PRIVATE (a_this)) {
Packit Bot 06c835
                gulong i = 0;
Packit Bot 06c835
Packit Bot 06c835
                for (i = 0; PRIVATE (a_this)->sheets && i < NB_ORIGINS; i++) {
Packit Bot 06c835
                        if (PRIVATE (a_this)->sheets[i]) {
Packit Bot 06c835
                                if (cr_stylesheet_unref
Packit Bot 06c835
                                    (PRIVATE (a_this)->sheets[i])
Packit Bot 06c835
                                    == TRUE) {
Packit Bot 06c835
                                        PRIVATE (a_this)->sheets[i] = NULL;
Packit Bot 06c835
                                }
Packit Bot 06c835
                        }
Packit Bot 06c835
                }
Packit Bot 06c835
                g_free (PRIVATE (a_this));
Packit Bot 06c835
                PRIVATE (a_this) = NULL;
Packit Bot 06c835
        }
Packit Bot 06c835
        g_free (a_this);
Packit Bot 06c835
}