/* Chrysalide - Outil d'analyse de fichiers binaires
 * area.c - définition d'un sous-composant de rendu
 *
 * Copyright (C) 2024 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chrysalide is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "area.h"


#include "area-int.h"



/* ------------------------ BASES D'UN NOUVEAU COMPOSANT GTK ------------------------ */


/* Procède à l'initialisation de l'afficheur générique. */
static void gtk_composing_area_class_init(GtkComposingAreaClass *);

/* Procède à l'initialisation de l'afficheur générique. */
static void gtk_composing_area_init(GtkComposingArea *);

/* Supprime toutes les références externes. */
static void gtk_composing_area_dispose(GtkComposingArea *);

/* Procède à la libération totale de la mémoire. */
static void gtk_composing_area_finalize(GtkComposingArea *);


/* --------------------- IMPLEMENTATION DES FONCTIONS DE CLASSE --------------------- */


/* Procède à l'actualisation de l'affichage d'un composant. */
static void gtk_composing_area_snapshot(GtkWidget *, GtkSnapshot *);



/* ---------------------------------------------------------------------------------- */
/*                          BASES D'UN NOUVEAU COMPOSANT GTK                          */
/* ---------------------------------------------------------------------------------- */


/* Détermine le type du composant d'affichage générique. */
G_DEFINE_TYPE(GtkComposingArea, gtk_composing_area, GTK_TYPE_WIDGET);


/******************************************************************************
*                                                                             *
*  Paramètres  : class = classe GTK à initialiser.                            *
*                                                                             *
*  Description : Procède à l'initialisation de l'afficheur générique.         *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void gtk_composing_area_class_init(GtkComposingAreaClass *class)
{
    GObjectClass *object;                   /* Plus haut niveau équivalent */
    GtkWidgetClass *widget;                 /* Classe de haut niveau       */

    object = G_OBJECT_CLASS(class);

    object->dispose = (GObjectFinalizeFunc/* ! */)gtk_composing_area_dispose;
    object->finalize = (GObjectFinalizeFunc)gtk_composing_area_finalize;

    widget = GTK_WIDGET_CLASS(class);

    widget->snapshot = gtk_composing_area_snapshot;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : area = composant GTK à initialiser.                          *
*                                                                             *
*  Description : Procède à l'initialisation de l'afficheur générique.         *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void gtk_composing_area_init(GtkComposingArea *area)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : area = instance d'objet GLib à traiter.                      *
*                                                                             *
*  Description : Supprime toutes les références externes.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void gtk_composing_area_dispose(GtkComposingArea *area)
{
    g_clear_object(&area->snapshot_wgt);

    G_OBJECT_CLASS(gtk_composing_area_parent_class)->dispose(G_OBJECT(area));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : area = instance d'objet GLib à traiter.                      *
*                                                                             *
*  Description : Procède à la libération totale de la mémoire.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void gtk_composing_area_finalize(GtkComposingArea *area)
{
    G_OBJECT_CLASS(gtk_composing_area_parent_class)->finalize(G_OBJECT(area));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Crée un composant de composition pour rendu principalement.  *
*                                                                             *
*  Retour      : Composant graphique pour GTK créé.                           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GtkComposingArea *g_raw_scan_cache_builder_new(void)
{
    GtkComposingArea *result;               /* Nouvelle instance à renvoyer*/

    result = g_object_new(GTK_TYPE_COMPOSING_AREA, NULL);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : area = sous-composant dont la définition est à compléter.    *
*                cb   = procédure de sous-traitance à enregistrer.            *
*                wgt  = contexte d'appel.                                     *
*                                                                             *
*  Description : Enregistre une fonction de rappel pour le rendu du composant.*
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_raw_scan_cache_register_snapshot(GtkComposingArea *area, snapshot_composing_area_cb cb, GtkWidget *wgt)
{
    g_clear_object(&area->snapshot_wgt);

    area->snapshot = cb;

    if (wgt != NULL)
        ref_object(wgt);

    area->snapshot_wgt = wgt;

}



/* ---------------------------------------------------------------------------------- */
/*                       IMPLEMENTATION DES FONCTIONS DE CLASSE                       */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : widget   = composant graphique visé par la procédure.        *
*                snapshot = gestionnaire de noeuds de rendu à solliciter.     *
*                                                                             *
*  Description : Procède à l'actualisation de l'affichage d'un composant.     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void gtk_composing_area_snapshot(GtkWidget *widget, GtkSnapshot *snapshot)
{
    GtkComposingArea *area;                 /* Véritable instance en jeu   */

    area = GTK_COMPOSING_AREA(widget);

    if (area->snapshot != NULL)
        area->snapshot(widget, snapshot, area->snapshot_wgt);

}