summaryrefslogtreecommitdiff
path: root/src/gtkext/graph/node.c
blob: 0d746e2d152b2b811ba4debd01f589fe23563a6b (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

/* OpenIDA - Outil d'analyse de fichiers binaires
 * node.c - éléments de graphiques chez dot
 *
 * Copyright (C) 2009-2012 Cyrille Bagard
 *
 *  This file is part of OpenIDA.
 *
 *  OpenIDA 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.
 *
 *  OpenIDA 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 "node.h"


#include <malloc.h>
#include <stdio.h>
#include <string.h>


#include "../../common/extstr.h"



/* -------------------------- GESTION DES NOEUDS A L'UNITE -------------------------- */


/* Intermédiaire entre le noeud dot et la bribe de code (instance) */
struct _GGraphNode
{
    GObject parent;                         /* A laisser en premier        */

    GtkWidget *view;                        /* Morceau de code représenté  */
    char *name;                             /* Adresse sous forme humaine  */

    GtkAllocation alloc;                    /* Emplacement du bloc rattaché*/

};


/* Intermédiaire entre le noeud dot et la bribe de code (classe) */
struct _GGraphNodeClass
{
    GObjectClass parent;                    /* A laisser en premier        */

    double dpi_x;                           /* Résolution en abscisse      */
    double dpi_y;                           /* Résolution en ordonnée      */

};


/* Initialise la classe des intermédiaires avec les noeuds dot. */
static void g_graph_node_class_init(GGraphNodeClass *);

/* Initialise la classe des intermédiaires avec les noeuds dot. */
static void g_graph_node_init(GGraphNode *);



/* ---------------------------------------------------------------------------------- */
/*                            GESTION DES NOEUDS A L'UNITE                            */
/* ---------------------------------------------------------------------------------- */


/* Indique le type définit par la GLib pour le noeud. */
G_DEFINE_TYPE(GGraphNode, g_graph_node, G_TYPE_OBJECT);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des intermédiaires avec les noeuds dot. *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_graph_node_class_init(GGraphNodeClass *klass)
{
    GdkScreen *screen;                      /* Ecran par défaut            */
    gint width;                             /* Largeur d'écran en pixels   */
    gint height;                            /* Hauteur d'écran en pixels   */
    gint width_mm;                          /* Largeur d'écran en mm.      */
    gint height_mm;                         /* Hauteur d'écran en mm.      */

    screen = gdk_screen_get_default();

    width = gdk_screen_get_width(screen);
    height = gdk_screen_get_height(screen);

    width_mm = gdk_screen_get_width_mm(screen);
    height_mm = gdk_screen_get_height_mm(screen);

    /**
     * Il y a 2.54 centimètres, soit 25.4 millimètres, dans un pouce.
     * On a donc :
     *
     *   dpi = N pixels / (M millimètres / (25.4 millimètres / 1 pouce))
     *       = N pixels / (M pouces / 25.4)
     *       = N * 25.4 pixels / M pouces
     *
     */
 
    if (width_mm > 0 && height_mm > 0)
    {
        klass->dpi_x = (width  * 25.4) / (double)width_mm;
        klass->dpi_y = (height * 25.4) / (double)height_mm;
    }
    else
    {
        klass->dpi_x = 96;
        klass->dpi_y = 96;
    }


    klass->dpi_x = 72;
    klass->dpi_y = 72;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : node = instance à initialiser.                               *
*                                                                             *
*  Description : Initialise la classe des intermédiaires avec les noeuds dot. *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_graph_node_init(GGraphNode *node)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : view = morceau d'affichage à représenter.                    *
*                                                                             *
*  Description : Constitue un intermédiaire entre un noeud dot et du code.    *
*                                                                             *
*  Retour      : Adresse de la structure mise en place.                       *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GGraphNode *g_graph_node_new(GtkWidget *view)
{
    GGraphNode *result;                     /* Structure à retourner       */
    size_t len;                             /* Taille du nom               */

    result = g_object_new(G_TYPE_GRAPH_NODE, NULL);

    result->view = view;

    len = 3 + sizeof(GtkWidget *) * 2 + 1;

    result->name = (char *)calloc(len, sizeof(char));
    snprintf(result->name, len, "_%p", result->view);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : node = intermédiaire à consulter.                            *
*                cmds = description pour dot à compléter.                     *
*                                                                             *
*  Description : Déclare l'intermédiaire en tant que noeud pour dot.          *
*                                                                             *
*  Retour      : Description dûment complétée.                                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

char *g_graph_node_register_for_dot(const GGraphNode *node, char *cmds)
{
    GtkRequisition requisition;             /* Taille à l'écran requise    */
    char buffer[128];

    gtk_widget_size_request(node->view, &requisition);

    snprintf(buffer, 128, "  subgraph cluster%s {\n", node->name);
    cmds = stradd(cmds, buffer);

    cmds = stradd(cmds, "    style=invisible;\n");

    cmds = stradd(cmds, "    ");
    cmds = stradd(cmds, node->name);
    cmds = stradd(cmds, " [shape=box, fixedsize ");

    snprintf(buffer, 128, ", width=\"%g\"",
             requisition.width / G_GRAPH_NODE_GET_CLASS(node)->dpi_x);
    cmds = stradd(cmds, buffer);

    snprintf(buffer, 128, ", height=\"%g\"",
             requisition.height / G_GRAPH_NODE_GET_CLASS(node)->dpi_y);
    cmds = stradd(cmds, buffer);

    cmds = stradd(cmds, "];\n");

    cmds = stradd(cmds, "  }\n");

    return cmds;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : node = intermédiaire à consulter.                            *
*                view = support de destination.                               *
*                x    = abscisse du point d'intégration.                      *
*                y    = ordonnée du point d'intégration.                      *
*                                                                             *
*  Description : Place le morceau de code de l'intermédiaire à l'écran.       *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_graph_node_place(GGraphNode *node, GtkGraphView *view, gint x, gint y)
{
    GtkRequisition requisition;             /* Taille à l'écran actuelle   */

    gtk_widget_size_request(node->view, &requisition);

    x -= requisition.width / 2;
    y -= requisition.height / 2;

    node->alloc.x = x;
    node->alloc.y = y;
    node->alloc.width = requisition.width;
    node->alloc.height = requisition.height;

    gtk_graph_view_put(view, node->view, &node->alloc);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : node   = intermédiaire à consulter.                          *
*                x      = abscisse du point à relier.                         *
*                y      = ordonnée du point à relier.                         *
*                points = liste de points à mettre à jour. [OUT]              *
*                count  = taille de cette même liste. [OUT]                   *
*                                                                             *
*  Description : Etablit une jonction ferme avec un noeud.                    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_graph_node_connect(const GGraphNode *node, gint x, gint y, GdkPoint **points, size_t *count)
{
    const GtkAllocation *alloc;             /* Emplacement du bloc rattaché*/

    alloc = &node->alloc;

    *points = (GdkPoint *)realloc(*points, ++(*count) * sizeof(GdkPoint));

    /* Si le point est sur la gauche... */
    if (alloc->y <= y && y < (alloc->y + alloc->height) && x < alloc->x)
    {
        (*points)[*count - 1].x = alloc->x;
        (*points)[*count - 1].y = y;
    }

    /* Si le point est sur la droite... */
    else if (alloc->y <= y && y < (alloc->y + alloc->height) && x > (alloc->x + alloc->width))
    {
        (*points)[*count - 1].x = alloc->x + alloc->width;
        (*points)[*count - 1].y = y;
    }

    /* Si le point est au dessus... */
    else if (alloc->x <= x && x < (alloc->x + alloc->width) && y < alloc->y)
    {
        (*points)[*count - 1].x = x;
        (*points)[*count - 1].y = alloc->y;
    }

    /* Si le point est en dessous... */
    else if (alloc->x <= x && x < (alloc->x + alloc->width) && y > (alloc->y + alloc->height))
    {
        (*points)[*count - 1].x = x;
        (*points)[*count - 1].y = alloc->y + alloc->height;
    }

    else (*count)--;

}



/* ---------------------------------------------------------------------------------- */
/*                         MANIPULATION D'ENSEMBLES DE NOEUDS                         */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : nodes  = liste de noeuds à parcourir.                        *
*                count  = taille de la liste.                                 *
*                target = nom du noeud recherché.                             *
*                                                                             *
*  Description : Recherche un noeud donné dans une série de noeuds.           *
*                                                                             *
*  Retour      : Noeud trouvé ou NULL si aucun.                               *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GGraphNode *find_graph_node_by_name(GGraphNode **nodes, size_t count, const char *target)
{
    GGraphNode *result;                     /* Trouvaille à remonter       */
    size_t i;                               /* Boucle de parcours          */

    result = NULL;

    for (i = 0; i < count && result == NULL; i++)
        if (strcmp(nodes[i]->name, target) == 0)
            result = nodes[i];

    return result;

}