~audio-recorder/audio-recorder/trunk

« back to all changes in this revision

Viewing changes to src/gtklevelbar.c

  • Committer: Osmo Antero
  • Date: 2012-09-29 18:12:44 UTC
  • Revision ID: osmoma@gmail.com-20120929181244-gmrxd5xww9pua60a
Support new systems; Ubuntu 12.10, Fedora 18. Improved timer and VAD-modules. Better gst-pipeline.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
// GtkWidget *lb = gtk_level_bar_new();
9
9
// gtk_widget_show(lb);
10
10
//
11
 
// Set value [0.0, 1.0] and optional text. Text is disabled if NULL.
12
 
// gtk_level_bar_set_fraction(GTK_LEVEL_BAR(lb), 0.8, "80%");
 
11
// Set value [0.0, 1.0].
 
12
// gtk_level_bar_set_fraction(GTK_LEVEL_BAR(lb), 0.8);
 
13
// gtk_level_bar_set_scale(GTK_LEVEL_BAR(lb), SCALE_PRECENT);
13
14
 
14
15
struct _GtkLevelBarPrivate {
15
16
    gdouble fraction;
16
17
    guint bar_height;
17
 
    gchar *text;
 
18
    enum SCALE_TYPE scale_type;
18
19
};
19
20
 
20
21
static void gtk_level_bar_get_preferred_width(GtkWidget *widget, gint *minimum, gint *natural);
53
54
 
54
55
    priv->fraction = 0.0;
55
56
    priv->bar_height = 8;
56
 
    priv->text = NULL;
 
57
    priv->scale_type = SCALE_VALUE;
57
58
 
58
59
    gtk_widget_set_has_window(GTK_WIDGET (pbar), FALSE);
59
60
}
77
78
}
78
79
 
79
80
static void gtk_level_bar_finalize (GObject *object) {
80
 
    GtkLevelBar *pbar = GTK_LEVEL_BAR (object);
81
 
 
82
 
    GtkLevelBarPrivate *priv = pbar->priv;
83
 
    g_free(priv->text);
84
 
    priv->text = NULL;
85
 
 
86
81
    G_OBJECT_CLASS(gtk_level_bar_parent_class)->finalize (object);
87
82
}
88
83
 
103
98
    GtkStyleContext *context;
104
99
    int width, height;
105
100
 
106
 
    context = gtk_widget_get_style_context (widget);
 
101
    context = gtk_widget_get_style_context(widget);
107
102
 
108
103
    width = gtk_widget_get_allocated_width(widget);
109
104
    height = gtk_widget_get_allocated_height(widget);
132
127
 
133
128
    gtk_style_context_restore(context);
134
129
 
 
130
    cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
 
131
    cairo_set_font_size(cr, height*0.3);
 
132
 
 
133
    GdkRGBA color;
 
134
    gtk_style_context_get_border_color(context, GTK_STATE_NORMAL, &color);
 
135
    gdk_cairo_set_source_rgba(cr, &color);
 
136
    color.alpha = 0.9;
 
137
 
 
138
    // Calculate total width of scale
 
139
    cairo_text_extents_t extents;
 
140
    cairo_text_extents(cr, "0.0", &extents);
 
141
    gint total_w = 9 * (extents.x_advance + extents.width);
 
142
 
 
143
    // Debug:
 
144
    // g_print("Bar width=%d  total_w=%d  bearing=%3.1f advance=%3.1f char.width=%3.1f\n", width, total_w, extents.x_bearing, extents.x_advance, extents.width);
 
145
 
 
146
    // Draw all or only each second value?
 
147
    gboolean draw_all = (total_w - extents.width) < width; 
 
148
 
 
149
    // Draw scale?
 
150
    if (priv->scale_type == SCALE_VALUE) {
 
151
        // Draw scale: 0.1  0.2  0.3  0.4...0.9
 
152
        gint i = 0;
 
153
        for (i=0; i < 10; i++) {
 
154
            gchar *s = NULL;
 
155
 
 
156
            if (draw_all || (i % 2 == 0))
 
157
                s = g_strdup_printf("%2.1f", (gdouble)i/10.0);
 
158
 
 
159
            if (!s) continue;
 
160
 
 
161
            cairo_text_extents_t extents;
 
162
            cairo_text_extents(cr, s, &extents);
 
163
 
 
164
            gdouble xx = (width/10) * i;
 
165
            gdouble yy = height/2-(extents.height/2 + extents.y_bearing);
 
166
 
 
167
            cairo_move_to(cr, xx, yy);
 
168
            cairo_show_text(cr, s);
 
169
 
 
170
            g_free(s);        
 
171
        }
 
172
 
 
173
    } else if (priv->scale_type == SCALE_PERCENT) {
 
174
        // Draw scale: 10% . 20% . 30% . 40% . 50%...90%
 
175
        gint i = 0;
 
176
        for (i=0; i < 10; i++) {
 
177
            gchar *s = NULL;
 
178
            if (i % 2 == 0)
 
179
                s = g_strdup_printf("%2.0f%%", (gdouble)i*10.0);
 
180
            else
 
181
                s = g_strdup_printf("%3s", ".");
 
182
 
 
183
            cairo_text_extents_t extents;
 
184
            cairo_text_extents(cr, s, &extents);
 
185
 
 
186
            gdouble xx = (width/10) * i;
 
187
            gdouble yy = height/2-(extents.height/2 + extents.y_bearing);
 
188
 
 
189
            cairo_move_to(cr, xx, yy);
 
190
            cairo_show_text(cr, s);
 
191
 
 
192
            g_free(s);
 
193
        }
 
194
    }
 
195
 
 
196
    #if 0 
135
197
    // Set text
136
198
    if (priv->text) {
137
199
        cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
152
214
        cairo_move_to(cr, xx, yy);
153
215
        cairo_show_text(cr, priv->text);
154
216
    }
 
217
    #endif
155
218
 
156
219
    return FALSE;
157
220
}
158
221
 
159
 
void gtk_level_bar_set_fraction(GtkLevelBar *pbar, gdouble fraction, const gchar *text) {
160
 
    // Set fraction [0.0, 1.0] and optional text.
161
 
    // Text is disabled/hidden if it's NULL.
 
222
void gtk_level_bar_set_fraction(GtkLevelBar *pbar, gdouble fraction) {
 
223
    // Set fraction [0.0, 1.0]
162
224
    GtkLevelBarPrivate* priv;
163
225
    g_return_if_fail (GTK_IS_LEVEL_BAR (pbar));
164
226
    priv = pbar->priv;
165
227
 
166
 
    g_free(priv->text);
167
 
    priv->text= NULL;
168
 
 
169
 
    if (text) {
170
 
        priv->text = g_strdup(text);
171
 
    }
172
 
 
173
228
    priv->fraction = CLAMP(fraction, 0.0, 1.0);
174
229
    gtk_level_bar_real_update (pbar);
175
230
}
176
231
 
 
232
gdouble gtk_level_bar_get_fraction(GtkLevelBar *pbar) {
 
233
    // Get fraction
 
234
    g_return_val_if_fail(GTK_IS_LEVEL_BAR (pbar), 0);
 
235
    return pbar->priv->fraction;
 
236
}
 
237
 
177
238
void gtk_level_bar_set_bar_height(GtkLevelBar *pbar, guint height) {
178
239
    // Set bar height (thickness). Normally 8 pixels.
179
 
    GtkLevelBarPrivate* priv;
180
 
    g_return_if_fail (GTK_IS_LEVEL_BAR (pbar));
181
 
    priv = pbar->priv;
182
 
 
 
240
    g_return_if_fail(GTK_IS_LEVEL_BAR (pbar));
 
241
    GtkLevelBarPrivate* priv = pbar->priv;
183
242
    priv->bar_height = height;
184
 
    gtk_level_bar_real_update (pbar);
185
 
}
186
 
 
187
 
gdouble gtk_level_bar_get_fraction (GtkLevelBar *pbar) {
188
 
    // Get fraction
189
 
    g_return_val_if_fail (GTK_IS_LEVEL_BAR (pbar), 0);
190
 
    return pbar->priv->fraction;
 
243
    // Redraw
 
244
    gtk_level_bar_real_update(pbar);
191
245
}
192
246
 
193
247
guint gtk_level_bar_get_bar_height(GtkLevelBar *pbar) {
194
248
    // Get bar thickness
195
 
    g_return_val_if_fail (GTK_IS_LEVEL_BAR (pbar), 0);
 
249
    g_return_val_if_fail(GTK_IS_LEVEL_BAR(pbar), 0);
196
250
    return pbar->priv->bar_height;
197
251
}
198
252
 
199
 
 
200
 
 
 
253
void gtk_level_bar_set_scale(GtkLevelBar *pbar, enum SCALE_TYPE scale_type) {
 
254
    // Set SCALE_TYPE
 
255
    g_return_if_fail(GTK_IS_LEVEL_BAR(pbar));
 
256
    GtkLevelBarPrivate* priv = pbar->priv;
 
257
    priv->scale_type = scale_type;
 
258
    // Redraw
 
259
    gtk_level_bar_real_update(pbar);
 
260
}
 
261
 
 
262
enum SCALE_TYPE gtk_level_bar_get_scale(GtkLevelBar *pbar) {
 
263
    // Get SCALE_TYPE
 
264
    g_return_val_if_fail(GTK_IS_LEVEL_BAR(pbar), SCALE_NONE);
 
265
    GtkLevelBarPrivate* priv = pbar->priv;
 
266
    return priv->scale_type;
 
267
}
201
268