/* Chrysalide - Outil d'analyse de fichiers binaires * helpers-ui.c - simplification des interactions UI de base avec Python * * 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 . */ #include "helpers-ui.h" #include #include #include #include "bindings.h" /* ---------------------------------------------------------------------------------- */ /* CONFORTS CIBLANT PYGOBJECT */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : - * * * * Description : Assure une prise en charge de l'objet Gtk.WIdget. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool ensure_gtk_widget_is_registered(void) { bool result; /* Bilan à retourner */ PyObject *gtk_mod; /* Module Python Gtk */ PyObject *widget_type; /* Module "GtkWidget" */ /** * Afin d'éviter le message d'avertissement suivant, la version attendue * est demandée : * * PyGIWarning: Gtk was imported without specifying a version first. * Use gi.require_version('Gtk', '4.0') before import to ensure that the right version gets loaded. * */ result = import_namespace_from_gi_repository("Gtk", "4.0"); if (result) { gtk_mod = PyImport_ImportModule("gi.repository.Gtk"); assert(gtk_mod != NULL); widget_type = PyObject_GetAttrString(gtk_mod, "Widget"); result = (widget_type != NULL); Py_DECREF(gtk_mod); Py_XDECREF(widget_type); } return result; } /****************************************************************************** * * * 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 composant GTK. * * * * Retour : Bilan de l'opération, voire indications supplémentaires. * * * * Remarques : - * * * ******************************************************************************/ int convert_to_gtk_widget(PyObject *arg, void *dst) { int result; /* Bilan à retourner */ PyObject *gtk_mod; /* Module Python Gtk */ PyObject *widget_type; /* Module "GtkWidget" */ 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; } widget_type = PyObject_GetAttrString(gtk_mod, "Widget"); Py_DECREF(gtk_mod); ret = PyObject_TypeCheck(arg, (PyTypeObject *)widget_type); Py_DECREF(widget_type); if (!ret) { PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to GTK widget"); goto done; } *((GtkWidget **)dst) = GTK_WIDGET(pygobject_get(arg)); result = 1; done: return result; }