1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/* 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 <http://www.gnu.org/licenses/>.
*/
#include "helpers-ui.h"
#include <assert.h>
#include <pygobject.h>
#include <gtk/gtk.h>
#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;
}
|