From 49af9c6449eb48d0ab1fb0893ad59b415f41d3cd Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sun, 16 Aug 2020 21:56:00 +0200
Subject: Created some extra features for the GTK API.

---
 plugins/pychrysalide/analysis/db/certs.c |   4 +-
 plugins/pychrysalide/analysis/db/certs.h |   2 +-
 plugins/pychrysalide/common/fnv1a.c      |   2 +-
 plugins/pychrysalide/gtkext/Makefile.am  |   1 +
 plugins/pychrysalide/gtkext/easygtk.c    | 233 +++++++++++++++++++++++++++++++
 plugins/pychrysalide/gtkext/easygtk.h    |  42 ++++++
 plugins/pychrysalide/gtkext/module.c     |   2 +
 plugins/pychrysalide/helpers.c           |  55 ++++++++
 plugins/pychrysalide/helpers.h           |   3 +
 src/gtkext/easygtk.c                     |  80 +++++++++++
 src/gtkext/easygtk.h                     |   6 +
 11 files changed, 426 insertions(+), 4 deletions(-)
 create mode 100644 plugins/pychrysalide/gtkext/easygtk.c
 create mode 100644 plugins/pychrysalide/gtkext/easygtk.h

diff --git a/plugins/pychrysalide/analysis/db/certs.c b/plugins/pychrysalide/analysis/db/certs.c
index dc46877..729c9a4 100644
--- a/plugins/pychrysalide/analysis/db/certs.c
+++ b/plugins/pychrysalide/analysis/db/certs.c
@@ -289,6 +289,8 @@ PyTypeObject *get_python_certs_type(void)
         .tp_methods     = py_certs_methods,
         .tp_getset      = py_certs_getseters,
 
+        .tp_new         = no_python_constructor_allowed,
+
     };
 
     return &py_certs_type;
@@ -317,8 +319,6 @@ bool ensure_python_certs_is_registered(void)
 
     if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
     {
-        type->tp_new = PyType_GenericNew;
-
         if (PyType_Ready(type) != 0)
             return false;
 
diff --git a/plugins/pychrysalide/analysis/db/certs.h b/plugins/pychrysalide/analysis/db/certs.h
index 5c1da5d..bafa2ae 100644
--- a/plugins/pychrysalide/analysis/db/certs.h
+++ b/plugins/pychrysalide/analysis/db/certs.h
@@ -39,4 +39,4 @@ bool ensure_python_certs_is_registered(void);
 
 
 
-#endif  /* _PLUGINS_PYCHRYSALIDE_ANALYSIS_DB_CERTSS_H */
+#endif  /* _PLUGINS_PYCHRYSALIDE_ANALYSIS_DB_CERTS_H */
diff --git a/plugins/pychrysalide/common/fnv1a.c b/plugins/pychrysalide/common/fnv1a.c
index 381af78..510b531 100644
--- a/plugins/pychrysalide/common/fnv1a.c
+++ b/plugins/pychrysalide/common/fnv1a.c
@@ -118,7 +118,7 @@ PyTypeObject *get_python_fnv1a_type(void)
 
         .tp_doc         = FNV1A_DOC,
 
-        .tp_methods     =  py_fnv1a_methods,
+        .tp_methods     = py_fnv1a_methods,
 
         .tp_new         = no_python_constructor_allowed,
 
diff --git a/plugins/pychrysalide/gtkext/Makefile.am b/plugins/pychrysalide/gtkext/Makefile.am
index 6e1a259..ab62e93 100644
--- a/plugins/pychrysalide/gtkext/Makefile.am
+++ b/plugins/pychrysalide/gtkext/Makefile.am
@@ -6,6 +6,7 @@ libpychrysagtkext_la_SOURCES =			\
 	bufferdisplay.h bufferdisplay.c		\
 	displaypanel.h displaypanel.c		\
 	dockable.h dockable.c				\
+	easygtk.h easygtk.c					\
 	module.h module.c					\
 	named.h named.c
 
diff --git a/plugins/pychrysalide/gtkext/easygtk.c b/plugins/pychrysalide/gtkext/easygtk.c
new file mode 100644
index 0000000..fe57c7e
--- /dev/null
+++ b/plugins/pychrysalide/gtkext/easygtk.c
@@ -0,0 +1,233 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * easygtk.c - équivalent Python du fichier "gtkext/easygtk.c"
+ *
+ * Copyright (C) 2020 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 "easygtk.h"
+
+
+#include <pygobject.h>
+
+
+#include <gtkext/easygtk.h>
+
+
+#include "../access.h"
+#include "../helpers.h"
+
+
+
+#define EASYGTK_DOC                                         \
+    "The EasyGtk class is a kind of toolbox gathering some" \
+    " useful features GTK is missing."
+
+
+/* Détermine l'indice d'un composant dans un conteneur GTK. */
+static PyObject *py_easygtk_find_contained_child_index(PyObject *, PyObject *);
+
+/* Récupère le nième composant d'un conteneur GTK. */
+static PyObject *py_easygtk_get_nth_contained_child(PyObject *, PyObject *);
+
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : self = NULL car méthode statique.                            *
+*                args = paramètres à transmettre à l'appel natif.             *
+*                                                                             *
+*  Description : Détermine l'indice d'un composant dans un conteneur GTK.     *
+*                                                                             *
+*  Retour      : Indice du composant dans le conteneur ou -1 si non trouvé.   *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static PyObject *py_easygtk_find_contained_child_index(PyObject *self, PyObject *args)
+{
+    PyObject *result;                       /* Désignation à retourner     */
+    GtkContainer *container;                /* Composant GTK à analyser    */
+    GtkWidget *child;                       /* Composant GTK à retrouver   */
+    int ret;                                /* Bilan de lecture des args.  */
+    gint index;                             /* Indice obtenu ou -1         */
+
+#define EASYGTK_FIND_CONTAINED_CHILD_INDEX_METHOD PYTHON_METHOD_DEF     \
+(                                                                       \
+    find_contained_child_index, "container, child, /",                  \
+    METH_VARARGS | METH_STATIC, py_easygtk,                             \
+    "Find the index of a given child widget inside a GTK container"     \
+    " children.\n"                                                      \
+    "\n"                                                                \
+    "The *containter* argument must be a Gtk.Container instance and"    \
+    " *child* a Gtk.Widget instance.\n"                                 \
+    "\n"                                                                \
+    "The result is the found index or -1 in case of error."             \
+)
+
+    ret = PyArg_ParseTuple(args, "O&O&", &convert_to_gtk_container, &container, convert_to_gtk_widget, &child);
+    if (!ret) return NULL;
+
+    index = find_contained_child_index(container, child);
+
+    result = PyLong_FromLong(index);
+
+    return result;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : self = NULL car méthode statique.                            *
+*                args = paramètres à transmettre à l'appel natif.             *
+*                                                                             *
+*  Description : Récupère le nième composant d'un conteneur GTK.              *
+*                                                                             *
+*  Retour      : Composant à la position donnée ou NULL en cas d'absence.     *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+static PyObject *py_easygtk_get_nth_contained_child(PyObject *self, PyObject *args)
+{
+    PyObject *result;                       /* Désignation à retourner     */
+    GtkContainer *container;                /* Composant GTK à analyser    */
+    unsigned int n;                         /* Indice du composant GTK visé*/
+    int ret;                                /* Bilan de lecture des args.  */
+    GtkWidget *child;                       /* Composant GTK retrouvé      */
+
+#define EASYGTK_GET_NTH_CONTAINED_CHILD_METHOD PYTHON_METHOD_DEF    \
+(                                                                   \
+    get_nth_contained_child, "container, n, /",                     \
+    METH_VARARGS | METH_STATIC, py_easygtk,                         \
+    "Find the widget contained inside a GTK container at the n-th"  \
+    " position.\n"                                                  \
+    "\n"                                                            \
+    "The *containter* argument must be a Gtk.Container instance"    \
+    " and *n* an integer.\n"                                        \
+    "\n"                                                            \
+    "The result is a Gtk.Widget instance, or Nonein case of error." \
+)
+
+    ret = PyArg_ParseTuple(args, "O&I", &convert_to_gtk_container, &container, &n);
+    if (!ret) return NULL;
+
+    child = get_nth_contained_child(container, n);
+
+    if (child == NULL)
+    {
+        result = Py_None;
+        Py_INCREF(result);
+    }
+    else
+    {
+        result = pygobject_new(G_OBJECT(child));
+        g_object_unref(G_OBJECT(child));
+    }
+
+    return result;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : -                                                            *
+*                                                                             *
+*  Description : Fournit un accès à une définition de type à diffuser.        *
+*                                                                             *
+*  Retour      : Définition d'objet pour Python.                              *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+PyTypeObject *get_python_easygtk_type(void)
+{
+    static PyMethodDef py_easygtk_methods[] = {
+        EASYGTK_FIND_CONTAINED_CHILD_INDEX_METHOD,
+        EASYGTK_GET_NTH_CONTAINED_CHILD_METHOD,
+        { NULL }
+    };
+
+    static PyGetSetDef py_easygtk_getseters[] = {
+        { NULL }
+    };
+
+    static PyTypeObject py_easygtk_type = {
+
+        PyVarObject_HEAD_INIT(NULL, 0)
+
+        .tp_name        = "pychrysalide.gtkext.EasyGtk",
+        .tp_basicsize   = sizeof(PyGObject),
+
+        .tp_flags       = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+
+        .tp_doc         = EASYGTK_DOC,
+
+        .tp_methods     = py_easygtk_methods,
+        .tp_getset      = py_easygtk_getseters,
+
+        .tp_new         = no_python_constructor_allowed,
+
+    };
+
+    return &py_easygtk_type;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : module = module dont la définition est à compléter.          *
+*                                                                             *
+*  Description : Prend en charge l'objet 'pychrysalide.gtkext.EasyGtk'.       *
+*                                                                             *
+*  Retour      : Bilan de l'opération.                                        *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+bool ensure_python_easygtk_is_registered(void)
+{
+    PyTypeObject *type;                     /* Type Python pour 'EasyGtk'  */
+    PyObject *module;                       /* Module à recompléter        */
+
+    type = get_python_easygtk_type();
+
+    if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
+    {
+        if (PyType_Ready(type) != 0)
+            return false;
+
+        module = get_access_to_python_module("pychrysalide.gtkext");
+
+        if (!register_python_module_object(module, type))
+            return false;
+
+    }
+
+    return true;
+
+}
diff --git a/plugins/pychrysalide/gtkext/easygtk.h b/plugins/pychrysalide/gtkext/easygtk.h
new file mode 100644
index 0000000..d7e484b
--- /dev/null
+++ b/plugins/pychrysalide/gtkext/easygtk.h
@@ -0,0 +1,42 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * easygtk.h - prototypes pour l'équivalent Python du fichier "gtkext/easygtk.h"
+ *
+ * Copyright (C) 2020 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
+ */
+
+
+#ifndef _PLUGINS_PYCHRYSALIDE_GTKEXT_EASYGTK_H
+#define _PLUGINS_PYCHRYSALIDE_GTKEXT_EASYGTK_H
+
+
+#include <Python.h>
+#include <stdbool.h>
+
+
+
+/* Fournit un accès à une définition de type à diffuser. */
+PyTypeObject *get_python_easygtk_type(void);
+
+/* Prend en charge l'objet 'pychrysalide.gtkext.EasyGtk'. */
+bool ensure_python_easygtk_is_registered(void);
+
+
+
+#endif  /* _PLUGINS_PYCHRYSALIDE_GTKEXT_EASYGTK_H */
diff --git a/plugins/pychrysalide/gtkext/module.c b/plugins/pychrysalide/gtkext/module.c
index 245dc3c..f8264af 100644
--- a/plugins/pychrysalide/gtkext/module.c
+++ b/plugins/pychrysalide/gtkext/module.c
@@ -32,6 +32,7 @@
 #include "bufferdisplay.h"
 #include "displaypanel.h"
 #include "dockable.h"
+#include "easygtk.h"
 #include "named.h"
 #include "graph/module.h"
 #include "../helpers.h"
@@ -102,6 +103,7 @@ bool populate_gtkext_module(void)
     if (result) result = ensure_python_buffer_display_is_registered();
     if (result) result = ensure_python_display_panel_is_registered();
     if (result) result = ensure_python_dockable_is_registered();
+    if (result) result = ensure_python_easygtk_is_registered();
     if (result) result = ensure_python_built_named_widget_is_registered();
 
     if (result) result = populate_gtkext_graph_module();
diff --git a/plugins/pychrysalide/helpers.c b/plugins/pychrysalide/helpers.c
index e9f65a9..25ce772 100644
--- a/plugins/pychrysalide/helpers.c
+++ b/plugins/pychrysalide/helpers.c
@@ -1174,6 +1174,61 @@ int convert_to_gtk_widget(PyObject *arg, void *dst)
 }
 
 
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : arg = argument quelconque à tenter de convertir.             *
+*                dst = destination des valeurs récupérées en cas de succès.   *
+*                                                                             *
+*  Description : Tente de convertir en instance de conteneur GTK.             *
+*                                                                             *
+*  Retour      : Bilan de l'opération, voire indications supplémentaires.     *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+int convert_to_gtk_container(PyObject *arg, void *dst)
+{
+    int result;                             /* Bilan à retourner           */
+    PyObject *gtk_mod;                      /* Module Python Gtk           */
+    PyObject *container_type;               /* Module "GtkContainer"          */
+    int ret;                                /* Bilan d'une conversion      */
+
+    result = 0;
+
+    gtk_mod = PyImport_ImportModule("gi.repository.Gtk");
+
+    if (gtk_mod == NULL)
+    {
+        PyErr_SetString(PyExc_TypeError, "unable to find the Gtk Python module");
+        goto done;
+    }
+
+    container_type = PyObject_GetAttrString(gtk_mod, "Container");
+
+    Py_DECREF(gtk_mod);
+
+    ret = PyObject_TypeCheck(arg, (PyTypeObject *)container_type);
+
+    Py_DECREF(container_type);
+
+    if (!ret)
+    {
+        PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to GTK container");
+        goto done;
+    }
+
+    *((GtkContainer **)dst) = GTK_CONTAINER(pygobject_get(arg));
+
+    result = 1;
+
+ done:
+
+    return result;
+
+}
+
+
 
 /* ---------------------------------------------------------------------------------- */
 /*                         TRANSFERT DES VALEURS CONSTANTES                           */
diff --git a/plugins/pychrysalide/helpers.h b/plugins/pychrysalide/helpers.h
index f2a9a1d..f5b5781 100644
--- a/plugins/pychrysalide/helpers.h
+++ b/plugins/pychrysalide/helpers.h
@@ -198,6 +198,9 @@ int convert_to_gobject(PyObject *, void *);
 /* Tente de convertir en instance de composant GTK. */
 int convert_to_gtk_widget(PyObject *, void *);
 
+/* Tente de convertir en instance de conteneur GTK. */
+int convert_to_gtk_container(PyObject *, void *);
+
 
 
 /* ----------------------- TRANSFERT DES VALEURS CONSTANTES ------------------------- */
diff --git a/src/gtkext/easygtk.c b/src/gtkext/easygtk.c
index e97b2fd..7021b87 100644
--- a/src/gtkext/easygtk.c
+++ b/src/gtkext/easygtk.c
@@ -801,6 +801,86 @@ GtkWidget *qck_create_menu_separator(void)
 
 
 
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : container = composant GTK contenant d'autres composants.     *
+*                child     = composant GTK à retrouver.                       *
+*                                                                             *
+*  Description : Détermine l'indice d'un composant dans un conteneur GTK.     *
+*                                                                             *
+*  Retour      : Indice du composant dans le conteneur ou -1 si non trouvé.   *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+gint find_contained_child_index(GtkContainer *container, GtkWidget *child)
+{
+    gint result;                            /* Indice à retourner          */
+    GList *list;                            /* Liste de composants contenus*/
+    gint counter;                           /* Décompte progressif         */
+    GList *iter;                            /* Boucle de parcours          */
+
+    result = -1;
+
+    list = gtk_container_get_children(container);
+
+    counter = 0;
+
+    for (iter = g_list_first(list); iter != NULL; iter = g_list_next(iter))
+    {
+        if (iter->data == child)
+        {
+            result = counter;
+            break;
+        }
+
+        counter++;
+
+    }
+
+    g_list_free(list);
+
+    return result;
+
+}
+
+
+/******************************************************************************
+*                                                                             *
+*  Paramètres  : container = composant GTK contenant d'autres composants.     *
+*                n         = indice du composant à retrouver.                 *
+*                                                                             *
+*  Description : Récupère le nième composant d'un conteneur GTK.              *
+*                                                                             *
+*  Retour      : Composant à la position donnée ou NULL en cas d'absence.     *
+*                                                                             *
+*  Remarques   : -                                                            *
+*                                                                             *
+******************************************************************************/
+
+GtkWidget *get_nth_contained_child(GtkContainer *container, guint n)
+{
+    GtkWidget *result;                      /* Résultat à retourner        */
+    GList *list;                            /* Liste de composants contenus*/
+
+    result = NULL;
+
+    list = gtk_container_get_children(container);
+
+    result = g_list_nth_data(list, n);
+
+    if (result != NULL)
+        g_object_ref(G_OBJECT(result));
+
+    g_list_free(list);
+
+    return result;
+
+}
+
+
 /******************************************************************************
 *                                                                             *
 *  Paramètres  : parent   = fenêtre parente pour la modalité d'affichage.     *
diff --git a/src/gtkext/easygtk.h b/src/gtkext/easygtk.h
index 86cba5f..58f1bca 100644
--- a/src/gtkext/easygtk.h
+++ b/src/gtkext/easygtk.h
@@ -92,6 +92,12 @@ GtkWidget *qck_create_menu_separator(void);
 
 
 
+/* Détermine l'indice d'un composant dans un conteneur GTK. */
+gint find_contained_child_index(GtkContainer *, GtkWidget *);
+
+/* Récupère le nième composant d'un conteneur GTK. */
+GtkWidget *get_nth_contained_child(GtkContainer *, guint);
+
 /* Affiche une boîte de dialogue offrant un choix "Oui/Non". */
 gint qck_show_question(GtkWindow *, const char *, const char *);
 
-- 
cgit v0.11.2-87-g4458