summaryrefslogtreecommitdiff
path: root/src/gtkext/gtkextstatusbar.c
blob: 74c23bd798b130495ab2133b36216e55c5335df2 (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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620

/* Chrysalide - Outil d'analyse de fichiers binaires
 * gtkextstatusbar.h - prototypes pour la barre de statut améliorée
 *
 * Copyright (C) 2009-2013 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  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 "gtkextstatusbar.h"


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



/* ------------------------- GESTION EXTERIEURE DE LA BARRE ------------------------- */


/* Elément de statut */
typedef struct _bar_item
{
    char *message;                          /* Message à afficher          */
    bool is_progressive;                    /* Utilisations de progression */
    double value;                           /* Valeur courante             */

    size_t index;                           /* Valeur pour l'identifiant   */

} bar_item;


/* Abstration d'une gestion de barre de statut (instance) */
struct _GtkExtStatusBar
{
    GtkStatusbar bar;                       /* Présence obligatoire en 1er */

    guint context;                          /* Nouvel identifiant          */
    guint cur_msg;                          /* Message courant             */

    GtkProgressBar *progress;               /* Barre de progression        */

    bar_item *stack;                        /* Pile de statut de la barre  */
    size_t stack_size;                      /* Taille de la pile           */
    GMutex stack_access;                    /* Accès en à la pile          */

};

/* Abstration d'une gestion de barre de statut (classe) */
struct _GtkExtStatusBarClass
{
    GtkStatusbarClass parent_class;         /* Présence obligatoire en 1er */

};


/* Initialise la classe des barres de statut améliorées. */
static void gtk_extended_status_bar_class_init(GtkExtStatusBarClass *);

/* Initialise une instance de barre de statut améliorée. */
static void gtk_extended_status_bar_init(GtkExtStatusBar *);



/* ----------------------- MISES A JOUR EN CONTEXTE PRINCIPAL ----------------------- */


/* Type de commande */
typedef enum _DelayedUpdateCmd
{
    DUC_CHANGE_CONTENT,                     /* Elément ajouté ou retiré    */
    DUC_UPDATE_ACTIVITY                     /* Mise à jour de progression  */

} DelayedUpdateCmd;

/* Transfert des commandes */
typedef struct _bar_update_info
{
    GtkExtStatusBar *bar;                   /* Barre associée              */
    DelayedUpdateCmd cmd;                   /* Type de commande            */

} bar_update_info;


/* Met à jour la partie graphique de la barre de statut. */
static gboolean gtk_extended_status_update(bar_update_info *);

/* Libère la mémoire occupée par la transmission. */
static void free_bar_update_info(bar_update_info *);

/* Place un nouveau message dans la barre de statut. */
static void _gtk_extended_status_bar_change_content(GtkExtStatusBar *);

/* Met à jour la barre de progression de la barre de statut. */
static void _gtk_extended_status_bar_update_activity(GtkExtStatusBar *);



/* -------------------------- GESTION EN VUE MACROSCOPIQUE -------------------------- */


/* Concentré d'informations utiles */
struct _status_blob_info
{
    GtkExtStatusBar *bar;                   /* Barre de statut utilisée    */
    bstatus_id_t id;                        /* Identifiant du message      */

    double current;                         /* Valeur courante représentée */
    double max;                             /* Valeur maximale à atteindre */

};



/* ---------------------------------------------------------------------------------- */
/*                           GESTION EXTERIEURE DE LA BARRE                           */
/* ---------------------------------------------------------------------------------- */


/* Détermine le type de la barre de statut améliorée. */
G_DEFINE_TYPE(GtkExtStatusBar, gtk_extended_status_bar, GTK_TYPE_STATUSBAR)


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe GTK à initialiser.                            *
*                                                                             *
*  Description : Initialise la classe des barres de statut améliorées.        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void gtk_extended_status_bar_class_init(GtkExtStatusBarClass *klass)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bar = instance GTK à initialiser.                            *
*                                                                             *
*  Description : Initialise une instance de barre de statut améliorée.        *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void gtk_extended_status_bar_init(GtkExtStatusBar *bar)
{
    bar->context = gtk_statusbar_get_context_id(GTK_STATUSBAR(bar), "");

    bar->progress = GTK_PROGRESS_BAR(gtk_progress_bar_new());

    gtk_widget_set_size_request(GTK_WIDGET(bar->progress), 200, -1);

    gtk_progress_bar_set_fraction(bar->progress, 0.5);
    gtk_progress_bar_set_text(bar->progress, "50%");

    gtk_box_pack_start(GTK_BOX(bar), GTK_WIDGET(bar->progress), FALSE, FALSE, 4);

    g_mutex_init(&bar->stack_access);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Crée une nouvelle instance de barre de statut.               *
*                                                                             *
*  Retour      : Composant GTK mis en place.                                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GtkWidget *gtk_extended_status_bar_new(void)
{
    return g_object_new(GTK_TYPE_EXT_STATUS_BAR, NULL);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bar         = barre de statut à manipuler.                   *
*                message     = message à afficher pour l'utilisateur.         *
*                progressive = utilisation de la barre de progression.        *
*                                                                             *
*  Description : Place un nouveau message dans la barre de statut.            *
*                                                                             *
*  Retour      : Identifiant du nouveau statut défini.                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bstatus_id_t gtk_extended_status_bar_push(GtkExtStatusBar *bar, const char *message, bool progressive)
{
    bstatus_id_t result;                    /* Identifiant à retourner     */
    size_t index;                           /* Indice du nouvel élément    */
    bar_update_info *info;                  /* Informations à mémoriser    */

    if (bar == NULL) return 0;

    /* Mise à jour de la pile */

    g_mutex_lock(&bar->stack_access);

    bar->stack = (bar_item *)realloc(bar->stack, ++bar->stack_size * sizeof(bar_item));

    index = bar->stack_size - 1;

    bar->stack[index].message = strdup(message);
    bar->stack[index].is_progressive = progressive;
    bar->stack[index].value = 0.0;
    bar->stack[index].index = index;

    result = &bar->stack[index].index;

    g_mutex_unlock(&bar->stack_access);

    /* Mise à jour de l'affichage */

    info = (bar_update_info *)calloc(1, sizeof(bar_update_info));

    info->bar = bar;
    g_object_ref(G_OBJECT(bar));
    info->cmd = DUC_CHANGE_CONTENT;

    gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE, (GSourceFunc)gtk_extended_status_update,
                              info, (GDestroyNotify)free_bar_update_info);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bar   = barre de statut à manipuler.                         *
*                id    = identifiant du message concerné.                     *
*                value = valeur actuelle de la progression.                   *
*                                                                             *
*  Description : Met à jour la barre de progression de la barre de statut.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void gtk_extended_status_bar_update_activity(GtkExtStatusBar *bar, bstatus_id_t id, double value)
{
    bar_update_info *info;                  /* Informations à mémoriser    */

    if (bar == NULL) return;

    /* Mise à jour de la pile */

    g_mutex_lock(&bar->stack_access);

    bar->stack[*id].value = value;

    g_mutex_unlock(&bar->stack_access);

    /* Mise à jour de l'affichage */

    info = (bar_update_info *)calloc(1, sizeof(bar_update_info));

    info->bar = bar;
    g_object_ref(G_OBJECT(bar));
    info->cmd = DUC_UPDATE_ACTIVITY;

    gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE, (GSourceFunc)gtk_extended_status_update,
                              info, (GDestroyNotify)free_bar_update_info);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bar = barre de statut à manipuler.                           *
*                id  = identifiant du statut à supprimer.                     *
*                                                                             *
*  Description : Retire de la barre un statut, visible ou non.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void gtk_extended_status_bar_remove(GtkExtStatusBar *bar, bstatus_id_t id)
{
    size_t index;                           /* Indice de l'élément visé    */
    size_t i;                               /* Boucle de parcours          */
    bar_update_info *info;                  /* Informations à mémoriser    */

    if (bar == NULL) return;

    /* Mise à jour de la pile */

    g_mutex_lock(&bar->stack_access);

    index = *id;

    free(bar->stack[index].message);

    if ((index + 1) < bar->stack_size)
    {
        memmove(&bar->stack[index], &bar->stack[index + 1],
                (bar->stack_size - index - 1) * sizeof(bar_item));

        for (i = index; i < bar->stack_size; i++)
            bar->stack[i].index = i;

    }

    bar->stack = (bar_item *)realloc(bar->stack, --bar->stack_size * sizeof(bar_item));

    g_mutex_unlock(&bar->stack_access);

    /* Mise à jour de l'affichage */

    info = (bar_update_info *)calloc(1, sizeof(bar_update_info));

    info->bar = bar;
    g_object_ref(G_OBJECT(bar));
    info->cmd = DUC_CHANGE_CONTENT;

    gdk_threads_add_idle_full(G_PRIORITY_HIGH_IDLE, (GSourceFunc)gtk_extended_status_update,
                              info, (GDestroyNotify)free_bar_update_info);

}



/* ---------------------------------------------------------------------------------- */
/*                         MISES A JOUR EN CONTEXTE PRINCIPAL                         */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : info = informations à consulter pour l'opération.            *
*                                                                             *
*  Description : Met à jour la partie graphique de la barre de statut.        *
*                                                                             *
*  Retour      : FALSE pour faire disparaître la mise à jour ensuite.         *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static gboolean gtk_extended_status_update(bar_update_info *info)
{
    switch (info->cmd)
    {
        case DUC_CHANGE_CONTENT:
            _gtk_extended_status_bar_change_content(info->bar);
            /*break;*/

        case DUC_UPDATE_ACTIVITY:
            _gtk_extended_status_bar_update_activity(info->bar);
            break;

    }

    return FALSE;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : info = informations à libérer de la mémoire.                 *
*                                                                             *
*  Description : Libère la mémoire occupée par la transmission.               *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void free_bar_update_info(bar_update_info *info)
{
    if (info->bar != NULL)

    g_object_unref(G_OBJECT(info->bar));

    free(info);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bar = barre de statut à manipuler.                           *
*                                                                             *
*  Description : Place un nouveau message dans la barre de statut.            *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void _gtk_extended_status_bar_change_content(GtkExtStatusBar *bar)
{
    size_t top;                             /* Indice de l'élément visé    */

    if (bar == NULL) return;

    g_mutex_lock(&bar->stack_access);

    if (bar->cur_msg > 0)
        gtk_statusbar_remove(GTK_STATUSBAR(bar), bar->context, bar->cur_msg);

    if (bar->stack_size == 0)
        gtk_widget_hide(GTK_WIDGET(bar->progress));

    else
    {
        top = bar->stack_size - 1;

        bar->cur_msg = gtk_statusbar_push(GTK_STATUSBAR(bar), bar->context,
                                          bar->stack[top].message);

        if (bar->stack[top].is_progressive)
            gtk_widget_show(GTK_WIDGET(bar->progress));
        else
            gtk_widget_hide(GTK_WIDGET(bar->progress));

    }

    g_mutex_unlock(&bar->stack_access);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : bar = barre de statut à manipuler.                           *
*                                                                             *
*  Description : Met à jour la barre de progression de la barre de statut.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void _gtk_extended_status_bar_update_activity(GtkExtStatusBar *bar)
{
    size_t top;                             /* Indice de l'élément visé    */
    double value;                           /* Valeur à prendre en compte  */
    gchar percent[5];                       /* Pourcentage en version txt. */

    if (bar == NULL) return;

    g_mutex_lock(&bar->stack_access);

    if (bar->stack_size > 0)
    {
        top = bar->stack_size - 1;

        if (bar->stack[top].is_progressive)
        {
            value = bar->stack[top].value;

            if (value != 1.0 && value - gtk_progress_bar_get_fraction(bar->progress) < 0.01)
                goto gesbua_exit;

            g_snprintf(percent, 5, "%.0f%%", value * 100);

            gtk_progress_bar_set_fraction(bar->progress, value);
            gtk_progress_bar_set_text(bar->progress, percent);

        }

    }

 gesbua_exit:

    g_mutex_unlock(&bar->stack_access);

}



/* ---------------------------------------------------------------------------------- */
/*                            GESTION EN VUE MACROSCOPIQUE                            */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : bar     = barre de statut à manipuler.                       *
*                message = message à afficher pour l'utilisateur.             *
*                val     = valeur de départ à afficher initialement.          *
*                max     = valeur maximale de progression (négative = aucune).*
*                                                                             *
*  Description : Met en place rapidement un message progressiste de statut.   *
*                                                                             *
*  Retour      : Structure opaque contenant le nécessaire pour les opérations *
*                ultérieures.                                                 *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

status_blob_info *init_progessive_status(GtkExtStatusBar *bar, const char *message, double val, double max)
{
    status_blob_info *result;               /* Information à retourner     */

    result = (status_blob_info *)calloc(1, sizeof(status_blob_info));

    g_object_ref(G_OBJECT(bar));
    result->bar = bar;

    result->id = gtk_extended_status_bar_push(bar, message, max > 0.0);

    result->current = 0.0;
    result->max = max;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : info = regroupement d'informations à manipuler.              *
*                                                                             *
*  Description : Fait disparaître la glue d'affichage de progression.         *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void fini_progessive_status(status_blob_info *info)
{
    gtk_extended_status_bar_remove(info->bar, info->id);

    if (info->bar != NULL)

    g_object_unref(G_OBJECT(info->bar));

    free(info);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : info = regroupement d'informations à manipuler.              *
*                                                                             *
*  Description : Indique la valeur courante utilisée pour la progression.     *
*                                                                             *
*  Retour      : Valeur courante.                                             *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

double get_current_progessive_status(const status_blob_info *info)
{
    return info->current;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : info = regroupement d'informations à manipuler.              *
*                inc  = valeur de la progression à prendre en compte.         *
*                                                                             *
*  Description : Augmente la progression visible dans la barre de statut.     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void inc_progessive_status(status_blob_info *info, double inc)
{
    assert((info->current + inc) < info->max);

    info->current += inc;

    gtk_extended_status_bar_update_activity(info->bar, info->id, info->current / info->max);

}