/* Chrysalide - Outil d'analyse de fichiers binaires
 * launcher.c - lanceur de panneau majeur
 *
 * Copyright (C) 2025 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 "launcher.h"


#include <malloc.h>
#include <string.h>


#include "helpers.h"
#include "launcher-int.h"
#include "../common/extstr.h"



/* Initialise la classe des sections d'éléments paramétrables. */
static void gtk_panel_launcher_class_init(GtkPanelLauncherClass *);

/* Initialise une instance de lanceur de panneau majeur. */
static void gtk_panel_launcher_init(GtkPanelLauncher *);

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

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



/* Détermine le type du composant d'affichage générique. */
G_DEFINE_TYPE(GtkPanelLauncher, gtk_panel_launcher, GTK_TYPE_LIST_BOX_ROW);


/******************************************************************************
*                                                                             *
*  Paramètres  : class = classe GTK à initialiser.                            *
*                                                                             *
*  Description : Initialise la classe des sections d'éléments paramétrables.  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

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

    object = G_OBJECT_CLASS(class);

    object->dispose = gtk_panel_launcher_dispose;
    object->finalize = gtk_panel_launcher_finalize;

    widget = GTK_WIDGET_CLASS(class);

    gtk_widget_class_set_template_from_resource(widget, "/re/chrysalide/framework/gtkext/launcher.ui");

    gtk_widget_class_bind_template_child(widget, GtkPanelLauncher, icon);
    gtk_widget_class_bind_template_child(widget, GtkPanelLauncher, title);
    gtk_widget_class_bind_template_child(widget, GtkPanelLauncher, desc);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : launcher = composant GTK à initialiser.                      *
*                                                                             *
*  Description : Initialise une instance de lanceur de panneau majeur.        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void gtk_panel_launcher_init(GtkPanelLauncher *launcher)
{
    gtk_widget_init_template(GTK_WIDGET(launcher));

}


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

static void gtk_panel_launcher_dispose(GObject *object)
{
    gtk_widget_dispose_template(GTK_WIDGET(object), GTK_TYPE_PANEL_LAUNCHER);

    G_OBJECT_CLASS(gtk_panel_launcher_parent_class)->dispose(object);

}


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

static void gtk_panel_launcher_finalize(GObject *object)
{
    G_OBJECT_CLASS(gtk_panel_launcher_parent_class)->finalize(object);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : icon  = désignation de l'image de représentation.            *
*                title = titre principal à afficher.                          *
*                desc  = description du panneau ciblé.                        *
*                                                                             *
*  Description : Crée un nouveau lanceur de panneau majeur.                   *
*                                                                             *
*  Retour      : Composant GTK mis en place.                                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GtkPanelLauncher *gtk_panel_launcher_new(const char *icon, const char *title, const char *desc)
{
    GtkPanelLauncher *result;               /* Instance à retourner        */

    result = g_object_new(GTK_TYPE_PANEL_LAUNCHER, NULL);

    if (!gtk_panel_launcher_create(result, icon, title, desc))
        g_clear_object(&result);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : launcher = lanceur à initialiser pleinement.                 *
*                icon     = désignation de l'image de représentation.         *
*                title    = titre principal à afficher.                       *
*                desc     = description du panneau ciblé.                     *
*                                                                             *
*  Description : Met en place un nouveau lanceur de panneau majeur.           *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool gtk_panel_launcher_create(GtkPanelLauncher *launcher, const char *icon, const char *title, const char *desc)
{
    bool result;                            /* Bilan à retourner           */
    char *bold;                             /* Titre sublimé               */

    result = true;

    gtk_image_set_from_icon_name(launcher->icon, icon);

    bold = strdup(title);
    bold = strprep(bold, "<b>");
    bold = stradd(bold, "</b>");

    gtk_label_set_label(launcher->title, bold);

    free(bold);

    gtk_label_set_label(launcher->desc, desc);

    return result;

}