/* Chrysalide - Outil d'analyse de fichiers binaires
 * status.c - affichage d'informations de statut dans la fenêtre principale
 *
 * Copyright (C) 2013-2017 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "status.h"


#include <assert.h>
#include <ctype.h>
#include <string.h>


#include <i18n.h>


#include "editem-int.h"
#include "core/global.h"
#include "../common/extstr.h"
#include "../gtkext/gtkbufferdisplay.h"
#include "../gtkext/gtkstatusstack.h"



/* Barre de statut de la fenêtre principale (instance) */
struct _GStatusInfo
{
    GEditorItem parent;                     /* A laisser en premier        */

};


/* Barre de statut de la fenêtre principale (classe) */
struct _GStatusInfoClass
{
    GEditorItemClass parent;                /* A laisser en premier        */

};


/* Initialise la classe de la barre de statut de l'éditeur. */
static void g_status_info_class_init(GStatusInfoClass *);

/* Initialise une instance de la barre de statut pour l'éditeur. */
static void g_status_info_init(GStatusInfo *);

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

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

/* Imprime la position du parcours courant dans le statut. */
static void track_caret_address_for_status_info(GStatusInfo *, GtkBufferDisplay *, const vmpa2t *);

/* Concentre l'attention de l'ensemble sur une adresse donnée. */
static void focus_address_in_status_info(GStatusInfo *, GLoadedBinary *, const vmpa2t *);



/* Indique le type défini pour la barre de statut de la fenêtre principale. */
G_DEFINE_TYPE(GStatusInfo, g_status_info, G_TYPE_EDITOR_ITEM);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe de la barre de statut de l'éditeur.     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_status_info_class_init(GStatusInfoClass *klass)
{
    GObjectClass *object;                   /* Autre version de la classe  */
    GEditorItemClass *editem;               /* Encore une autre vision...  */

    object = G_OBJECT_CLASS(klass);

    object->dispose = (GObjectFinalizeFunc/* ! */)g_status_info_dispose;
    object->finalize = (GObjectFinalizeFunc)g_status_info_finalize;

    editem = G_EDITOR_ITEM_CLASS(klass);

    editem->track_caret = (track_caret_in_view_fc)track_caret_address_for_status_info;
    editem->focus_addr = (focus_addr_fc)focus_address_in_status_info;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bar = instance à initialiser.                                *
*                                                                             *
*  Description : Initialise une instance de la barre de statut pour l'éditeur.*
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_status_info_init(GStatusInfo *bar)
{
    GEditorItem *item;                      /* Autre version de l'élément  */

    item = G_EDITOR_ITEM(bar);

    item->name = "status";

    item->widget = gtk_status_stack_new();
    gtk_widget_show(item->widget);

}


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

static void g_status_info_dispose(GStatusInfo *info)
{
    G_OBJECT_CLASS(g_status_info_parent_class)->dispose(G_OBJECT(info));

}


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

static void g_status_info_finalize(GStatusInfo *info)
{
    G_OBJECT_CLASS(g_status_info_parent_class)->finalize(G_OBJECT(info));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : ref = espace de référencement global.                        *
*                                                                             *
*  Description : Compose la barre de statut principale.                       *
*                                                                             *
*  Retour      : Adresse de la structure mise en place.                       *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GEditorItem *g_status_info_new(GObject *ref)
{
    GStatusInfo *result;                    /* Structure à retourner       */
    GEditorItem *item;                      /* Autre version de l'élément  */

    result = g_object_new(G_TYPE_STATUS_INFO, NULL);

    item = G_EDITOR_ITEM(result);

    g_object_ref(ref);
    item->ref = ref;

    set_global_status(GTK_STATUS_STACK(item->widget));

    return G_EDITOR_ITEM(result);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : info    = barre de statut présentant les informations.       *
*                display = composant d'affichage parcouru.                    *
*                addr    = nouvelle adresse du curseur courant.               *
*                                                                             *
*  Description : Imprime la position du parcours courant dans le statut.      *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void track_caret_address_for_status_info(GStatusInfo *info, GtkBufferDisplay *display, const vmpa2t *addr)
{
    GLoadedBinary *binary;                  /* Binaire courant             */

    binary = get_current_binary();

    focus_address_in_status_info(info, binary, addr);

    g_object_unref(G_OBJECT(binary));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : info   = composant réactif à mettre à jour.                  *
*                binary = binaire contenant l'adresse à représenter.          *
*                addr   = adresse mémoire à mettre en avant.                  *
*                                                                             *
*  Description : Concentre l'attention de l'ensemble sur une adresse donnée.  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void focus_address_in_status_info(GStatusInfo *info, GLoadedBinary *binary, const vmpa2t *addr)
{
    GEditorItem *item;                      /* Autre version de l'élément  */
    GArchProcessor *proc;                   /* Architecture du binaire     */
    GArchInstruction *instr;                /* Instruction présente        */

    item = G_EDITOR_ITEM(info);

    if (is_invalid_vmpa(addr))
        gtk_status_stack_reset_current_instruction(GTK_STATUS_STACK(item->widget));

    else
    {
        proc = g_loaded_binary_get_processor(binary);

        instr = _g_arch_processor_find_instr_by_address(proc, addr, true);
        assert(instr != NULL);

        gtk_status_stack_update_current_instruction(GTK_STATUS_STACK(item->widget), binary, instr);

        g_object_unref(G_OBJECT(instr));

        g_object_unref(G_OBJECT(proc));

    }

}