Root/eeshow/gui/progress.c

1/*
2 * gui/progress.c - Progress bar
3 *
4 * Written 2016 by Werner Almesberger
5 * Copyright 2016 by Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13/*
14 * Resources:
15 *
16 * http://zetcode.com/gfx/cairo/cairobackends/
17 * https://developer.gnome.org/gtk3/stable/gtk-migrating-2-to-3.html
18 */
19
20#include <stdbool.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <math.h>
25
26#include <cairo/cairo.h>
27#include <gtk/gtk.h>
28
29#include "gui/common.h"
30
31
32#define PROGRESS_BAR_HEIGHT 10
33
34
35static void progress_draw_event(GtkWidget *widget, cairo_t *cr,
36    gpointer user_data)
37{
38    GtkAllocation alloc;
39    struct gui_ctx *ctx = user_data;
40    unsigned w, x;
41
42    x = ctx->progress >> ctx->progress_scale;
43    if (!x) {
44        /* @@@ needed ? Gtk seems to always clear the the surface. */
45        cairo_set_source_rgb(cr, 1, 1, 1);
46        cairo_paint(cr);
47    }
48
49    gtk_widget_get_allocation(ctx->da, &alloc);
50    w = ctx->hist_size >> ctx->progress_scale;
51
52    cairo_save(cr);
53    cairo_translate(cr,
54        (alloc.width - w) / 2, (alloc.height - PROGRESS_BAR_HEIGHT) / 2);
55
56    cairo_set_source_rgb(cr, 0, 0.7, 0);
57    cairo_set_line_width(cr, 0);
58    cairo_rectangle(cr, 0, 0, x, PROGRESS_BAR_HEIGHT);
59    cairo_fill(cr);
60
61    cairo_set_source_rgb(cr, 0, 0, 0);
62    cairo_set_line_width(cr, 2);
63    cairo_rectangle(cr, 0, 0, w, PROGRESS_BAR_HEIGHT);
64    cairo_stroke(cr);
65
66    cairo_restore(cr);
67}
68
69
70void setup_progress_bar(struct gui_ctx *ctx, GtkWidget *window)
71{
72    GtkAllocation alloc;
73
74    gtk_widget_get_allocation(ctx->da, &alloc);
75
76    ctx->progress_scale = 0;
77    while ((ctx->hist_size >> ctx->progress_scale) > alloc.width)
78        ctx->progress_scale++;
79    ctx->progress = 0;
80
81    g_signal_connect(G_OBJECT(ctx->da), "draw",
82        G_CALLBACK(progress_draw_event), ctx);
83
84    redraw(ctx);
85    gtk_main_iteration_do(0);
86}
87
88
89void progress_update(struct gui_ctx *ctx)
90{
91    unsigned mask = (1 << ctx->progress_scale) - 1;
92
93    ctx->progress++;
94    if ((ctx->progress & mask) != mask)
95        return;
96
97    redraw(ctx);
98    gtk_main_iteration_do(0);
99}
100

Archive Download this file

Branches:
master



interactive