summaryrefslogtreecommitdiff
path: root/src/gtkext/hexdisplay.c
blob: 685fc81ee1fdbcd458032f908cad6e1d8b846636 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332

/* Chrysalide - Outil d'analyse de fichiers binaires
 * hexdisplay.c - affichage d'un contenu binaire sous forme hexadécimale
 *
 * Copyright (C) 2019 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "hexdisplay.h"


#include "gtkbufferdisplay-int.h"
#include "../format/format.h"
#include "../glibext/generators/hex.h"



/* Composant d'affichage de contenu sous forme hexadécimale (instance) */
struct _GtkHexDisplay
{
    GtkBufferDisplay parent;                /* A laisser en premier        */

    GBufferCache *cache;                    /* Cache pour l'affichage      */
    GHexGenerator *generator;               /* Générateur à la volée       */

};

/* Composant d'affichage de contenu sous forme hexadécimale (classe) */
struct _GtkHexDisplayClass
{
    GtkBufferDisplayClass parent;           /* A laisser en premier        */

};


/* Procède à l'initialisation des afficheurs sous forme hexa. */
static void gtk_hex_display_class_init(GtkHexDisplayClass *);

/* Procède à l'initialisation de l'afficheur sous forme hexa. */
static void gtk_hex_display_init(GtkHexDisplay *);

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

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

/* S'adapte à la surface concédée par le composant parent. */
static void gtk_hex_display_size_allocate(GtkWidget *, GtkAllocation *);

/* Indique les dimensions de travail du composant d'affichage. */
static void gtk_hex_display_compute_requested_size(GtkHexDisplay *, gint *, gint *);

/* Adapte le cache de lignes hexadécimales à la taille courante. */
static void gtk_hex_display_populate_cache(GtkHexDisplay *);



/* Détermine le type du composant d'affichage sous forme hexadécimale. */
G_DEFINE_TYPE(GtkHexDisplay, gtk_hex_display, GTK_TYPE_BUFFER_DISPLAY)


/******************************************************************************
*                                                                             *
*  Paramètres  : class = classe GTK à initialiser.                            *
*                                                                             *
*  Description : Procède à l'initialisation des afficheurs sous forme hexa.   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void gtk_hex_display_class_init(GtkHexDisplayClass *class)
{
    GObjectClass *object;                   /* Autre version de la classe  */
    GtkWidgetClass *widget_class;           /* Classe de haut niveau       */
    GtkDisplayPanelClass *panel_class;      /* Classe parente              */

    object = G_OBJECT_CLASS(class);

    object->dispose = (GObjectFinalizeFunc/* ! */)g_hex_display_dispose;
    object->finalize = (GObjectFinalizeFunc)g_hex_display_finalize;

    widget_class = GTK_WIDGET_CLASS(class);

    widget_class->size_allocate = gtk_hex_display_size_allocate;

    panel_class = GTK_DISPLAY_PANEL_CLASS(class);

    panel_class->compute_size = (compute_requested_size_fc)gtk_hex_display_compute_requested_size;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : view = composant GTK à initialiser.                          *
*                                                                             *
*  Description : Procède à l'initialisation de l'afficheur sous forme hexa.   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void gtk_hex_display_init(GtkHexDisplay *view)
{

}


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

static void g_hex_display_dispose(GtkHexDisplay *display)
{
    g_clear_object(&display->cache);

    g_clear_object(&display->generator);

    G_OBJECT_CLASS(gtk_hex_display_parent_class)->dispose(G_OBJECT(display));

}


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

static void g_hex_display_finalize(GtkHexDisplay *display)
{
    G_OBJECT_CLASS(gtk_hex_display_parent_class)->finalize(G_OBJECT(display));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : content = contenu brut à représenter.                        *
*                                                                             *
*  Description : Crée un nouveau composant pour l'affichage sous forme hexa.  *
*                                                                             *
*  Retour      : Composant GTK créé.                                          *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GtkWidget *gtk_hex_display_new(GBinContent *content)
{
    GtkHexDisplay *result;                  /* Composant à retourner       */
    GBufferView *view;                      /* Vue pointée sur un tampon   */

    result = g_object_new(GTK_TYPE_HEX_DISPLAY, NULL);

    result->cache = g_buffer_cache_new(content);
    g_object_ref_sink(G_OBJECT(result->cache));

    result->generator = g_hex_generator_new(content);

    gtk_hex_display_populate_cache(result);

    view = g_buffer_view_new(result->cache, NULL);

    GTK_BUFFER_DISPLAY(result)->view = view;

    return GTK_WIDGET(result);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : widget     = composant GTK à mettre à jour.                  *
*                allocation = étendue accordée à la vue.                      *
*                                                                             *
*  Description : S'adapte à la surface concédée par le composant parent.      *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void gtk_hex_display_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
{
    GtkHexDisplay *display;                 /* Autre version du composant  */
    GBufferCache *cache;                    /* Contenu représenté          */
    gint text_pos;                          /* Abscisse minimale du texte  */
    bool show_pos;                          /* Affichage des positions ?   */
    bool changed;                           /* Note toute variation        */

    display = GTK_HEX_DISPLAY(widget);

    cache = g_buffer_view_get_cache(GTK_BUFFER_DISPLAY(display)->view);

    text_pos = g_buffer_cache_get_text_position(cache);

    g_object_unref(G_OBJECT(cache));

    show_pos = g_display_options_get(GTK_DISPLAY_PANEL(widget)->options, 0);

    changed = g_hex_generator_auto_fit(display->generator, text_pos, show_pos, allocation->width);

    if (changed)
        gtk_hex_display_populate_cache(display);

    /**
     * On fait appel au parent en dernier pour bénéficier des besoins
     * en espace actualisés avec les nouvelles dispositions.
     */

    GTK_WIDGET_CLASS(gtk_hex_display_parent_class)->size_allocate(widget, allocation);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : display = composant GTK à consulter.                         *
*                width   = largeur requise à renseigner ou NULL. [OUT]        *
*                height  = hauteur requise à renseigner ou NULL. [OUT]        *
*                                                                             *
*  Description : Indique les dimensions de travail du composant d'affichage.  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void gtk_hex_display_compute_requested_size(GtkHexDisplay *display, gint *width, gint *height)
{
    GtkDisplayPanel *pdisplay;              /* Version parente             */

    pdisplay = GTK_DISPLAY_PANEL(display);

    GTK_DISPLAY_PANEL_CLASS(gtk_hex_display_parent_class)->compute_size(pdisplay, width, height);

    if (width != NULL && *width != 0)
        *width = 1;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : display = composant GTK à mettre à jour.                     *
*                                                                             *
*  Description : Adapte le cache de lignes hexadécimales à la taille courante.*
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void gtk_hex_display_populate_cache(GtkHexDisplay *display)
{
    GBinContent *content;                   /* Contenu binaire affiché     */
    phys_t full;                            /* Taille totale à représenter */
    phys_t line;                            /* Taille représentée par ligne*/
    size_t needed;                          /* Nombre de lignes nécessaires*/
    size_t count;                           /* Nombre actuel de lignes     */

    /* Détermination du besoin */

    content = g_hex_generator_get_content(display->generator);

    full = g_binary_content_compute_size(content);

    g_object_unref(G_OBJECT(content));

    line = g_hex_generator_get_bytes_per_line(display->generator);

    needed = full / line;

    if (full % line > 0)
        needed++;

    /* Adaptation du tampon interne */

    count = g_buffer_cache_count_lines(display->cache);

    if (needed < count)
        g_buffer_cache_truncate(display->cache, needed);

    else if (needed > count)
    {
        g_object_ref(G_OBJECT(display->generator));
        g_buffer_cache_extend_with(display->cache, needed, G_LINE_GENERATOR(display->generator));
    }

    if (needed != count)
        gtk_widget_queue_resize(GTK_WIDGET(display));

}