~audio-recorder/audio-recorder/trunk

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
#include "gtklevelbar.h"
#include <math.h> // round()

// A simple level bar widget.
// By Osmo Antero.
//
// Sample call:
// GtkWidget *lb = gtk_level_bar_new();
// gtk_widget_show(lb);
//
// Set value [0.0, 1.0] and optional text. Text is disabled if NULL.
// gtk_level_bar_set_fraction(GTK_LEVEL_BAR(lb), 0.8, "80%");

struct _GtkLevelBarPrivate {
    gdouble fraction;
    guint bar_height;
    gchar *text;
};

static void gtk_level_bar_get_preferred_width(GtkWidget *widget, gint *minimum, gint *natural);
static void gtk_level_bar_get_preferred_height (GtkWidget *widget,gint *minimum, gint *natural);

static void gtk_level_bar_real_update(GtkLevelBar *progress);
static gboolean gtk_level_bar_draw(GtkWidget *widget, cairo_t *cr);

static void gtk_level_bar_finalize(GObject *object);

G_DEFINE_TYPE(GtkLevelBar, gtk_level_bar, GTK_TYPE_WIDGET);

static void gtk_level_bar_class_init (GtkLevelBarClass *class) {
    GObjectClass *gobject_class;
    GtkWidgetClass *widget_class;

    gobject_class = G_OBJECT_CLASS (class);
    widget_class = (GtkWidgetClass *)class;

    gobject_class->set_property = NULL;
    gobject_class->get_property = NULL;
    gobject_class->finalize = gtk_level_bar_finalize;

    widget_class->draw = gtk_level_bar_draw;
    widget_class->get_preferred_width = gtk_level_bar_get_preferred_width;
    widget_class->get_preferred_height = gtk_level_bar_get_preferred_height;

    g_type_class_add_private (class, sizeof (GtkLevelBarPrivate));
}

static void gtk_level_bar_init(GtkLevelBar *pbar) {
    GtkLevelBarPrivate *priv;

    pbar->priv = G_TYPE_INSTANCE_GET_PRIVATE(pbar, GTK_TYPE_LEVEL_BAR, GtkLevelBarPrivate);
    priv = pbar->priv;

    priv->fraction = 0.0;
    priv->bar_height = 8;
    priv->text = NULL;

    gtk_widget_set_has_window(GTK_WIDGET (pbar), FALSE);
}

GtkWidget *gtk_level_bar_new(void) {
    GtkWidget *pbar;
    pbar = g_object_new(GTK_TYPE_LEVEL_BAR, NULL);
    return pbar;
}

static void gtk_level_bar_real_update (GtkLevelBar *pbar) {
    GtkWidget *widget;

    g_return_if_fail (GTK_IS_LEVEL_BAR (pbar));

    GtkLevelBarPrivate __attribute__ ((unused)) *priv = pbar->priv;

    widget = GTK_WIDGET(pbar);

    gtk_widget_queue_draw(widget);
}

static void gtk_level_bar_finalize (GObject *object) {
    GtkLevelBar *pbar = GTK_LEVEL_BAR (object);

    GtkLevelBarPrivate *priv = pbar->priv;
    g_free(priv->text);
    priv->text = NULL;

    G_OBJECT_CLASS(gtk_level_bar_parent_class)->finalize (object);
}

static void gtk_level_bar_get_preferred_width (GtkWidget *widget,gint *minimum, gint *natural) {
    *minimum = 50;
    *natural = 160;
}

static void gtk_level_bar_get_preferred_height (GtkWidget *widget, gint      *minimum, gint      *natural) {
    *minimum = 6;
    *natural = 8;
}

static gboolean gtk_level_bar_draw(GtkWidget *widget, cairo_t *cr) {
    // Draw level bar and optional text
    GtkLevelBar *pbar = GTK_LEVEL_BAR (widget);
    GtkLevelBarPrivate *priv = pbar->priv;
    GtkStyleContext *context;
    int width, height;

    context = gtk_widget_get_style_context (widget);

    width = gtk_widget_get_allocated_width(widget);
    height = gtk_widget_get_allocated_height(widget);

    // Bar thickness
    gdouble bar_height = MIN(height , priv->bar_height);

    // Vertical pos
    gdouble y = (height - bar_height)/2;

    // Pulse width
    gdouble w = priv->fraction/(1.00/width);

    gtk_style_context_save(context);
    // gtk_style_context_add_class(context, GTK_STYLE_CLASS_TROUGH);
    gtk_render_background(context, cr, 0, 0, width, height);
    gtk_render_frame(context, cr, 0, 0, width, height);

    // Progressbar style
    gtk_style_context_add_class(context, GTK_STYLE_CLASS_PROGRESSBAR);

    if (priv->fraction > 0.001) {
        // Render activity bar with current theme and colors
        gtk_render_activity(context, cr, 0, y, w, bar_height);
    }

    gtk_style_context_restore(context);

    // Set text
    if (priv->text) {
        cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);

        cairo_set_font_size(cr, 0.5*height);
        cairo_text_extents_t extents;
        cairo_text_extents(cr, priv->text, &extents);

        // Ref: http://cairographics.org/manual/cairo-cairo-scaled-font-t.html#cairo-text-extents-t
        gdouble xx = width-(extents.width + extents.x_bearing)-2;
        gdouble yy = height/2-(extents.height/2 + extents.y_bearing);

        GdkRGBA color;
        gtk_style_context_get_border_color(context, GTK_STATE_NORMAL, &color);
        gdk_cairo_set_source_rgba(cr, &color);
        color.alpha = 0.9;

        cairo_move_to(cr, xx, yy);
        cairo_show_text(cr, priv->text);
    }

    return FALSE;
}

void gtk_level_bar_set_fraction(GtkLevelBar *pbar, gdouble fraction, const gchar *text) {
    // Set fraction [0.0, 1.0] and optional text.
    // Text is disabled/hidden if it's NULL.
    GtkLevelBarPrivate* priv;
    g_return_if_fail (GTK_IS_LEVEL_BAR (pbar));
    priv = pbar->priv;

    g_free(priv->text);
    priv->text= NULL;

    if (text) {
        priv->text = g_strdup(text);
    }

    priv->fraction = CLAMP(fraction, 0.0, 1.0);
    gtk_level_bar_real_update (pbar);
}

void gtk_level_bar_set_bar_height(GtkLevelBar *pbar, guint height) {
    // Set bar height (thickness). Normally 8 pixels.
    GtkLevelBarPrivate* priv;
    g_return_if_fail (GTK_IS_LEVEL_BAR (pbar));
    priv = pbar->priv;

    priv->bar_height = height;
    gtk_level_bar_real_update (pbar);
}

gdouble gtk_level_bar_get_fraction (GtkLevelBar *pbar) {
    // Get fraction
    g_return_val_if_fail (GTK_IS_LEVEL_BAR (pbar), 0);
    return pbar->priv->fraction;
}

guint gtk_level_bar_get_bar_height(GtkLevelBar *pbar) {
    // Get bar thickness
    g_return_val_if_fail (GTK_IS_LEVEL_BAR (pbar), 0);
    return pbar->priv->bar_height;
}