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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
*
* Copyright (C) 2008 William Jon McCann
* Copyright (C) 2014 Michal Ratajsky <michal.ratajsky@gmail.com>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <canberra-gtk.h>
#include <libmatemixer/matemixer.h>
#include "gvc-balance-bar.h"
#define BALANCE_BAR_STYLE \
"style \"balance-bar-scale-style\" {\n" \
" GtkScale::trough-side-details = 0\n" \
"}\n" \
"widget \"*.balance-bar-scale\" style : rc \"balance-bar-scale-style\"\n"
#define SCALE_SIZE 128
#define GVC_BALANCE_BAR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GVC_TYPE_BALANCE_BAR, GvcBalanceBarPrivate))
struct _GvcBalanceBarPrivate
{
GvcBalanceType btype;
GtkWidget *scale_box;
GtkWidget *start_box;
GtkWidget *end_box;
GtkWidget *label;
GtkWidget *scale;
GtkAdjustment *adjustment;
GtkSizeGroup *size_group;
gboolean symmetric;
MateMixerStreamControl *control;
gint lfe_channel;
};
enum
{
PROP_0,
PROP_CONTROL,
PROP_BALANCE_TYPE,
N_PROPERTIES
};
static GParamSpec *properties[N_PROPERTIES] = { NULL, };
static void gvc_balance_bar_class_init (GvcBalanceBarClass *klass);
static void gvc_balance_bar_init (GvcBalanceBar *balance_bar);
static void gvc_balance_bar_dispose (GObject *object);
static gboolean on_scale_scroll_event (GtkWidget *widget,
GdkEventScroll *event,
GvcBalanceBar *bar);
static void on_adjustment_value_changed (GtkAdjustment *adjustment,
GvcBalanceBar *bar);
G_DEFINE_TYPE (GvcBalanceBar, gvc_balance_bar, GTK_TYPE_BOX)
static void
create_scale_box (GvcBalanceBar *bar)
{
#if GTK_CHECK_VERSION (3, 0, 0)
bar->priv->scale_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
bar->priv->start_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
bar->priv->end_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
bar->priv->scale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL,
bar->priv->adjustment);
#if GTK_CHECK_VERSION (3, 4, 0)
/* Balance and fade scales do not have an origin */
if (bar->priv->btype != BALANCE_TYPE_LFE)
gtk_scale_set_has_origin (GTK_SCALE (bar->priv->scale), FALSE);
#endif
#else
bar->priv->scale_box = gtk_hbox_new (FALSE, 6);
bar->priv->start_box = gtk_hbox_new (FALSE, 6);
bar->priv->end_box = gtk_hbox_new (FALSE, 6);
bar->priv->scale = gtk_hscale_new (bar->priv->adjustment);
/* GTK2 way to remove the origin */
if (bar->priv->btype != BALANCE_TYPE_LFE) {
gtk_rc_parse_string (BALANCE_BAR_STYLE);
gtk_widget_set_name (bar->priv->scale, "balance-bar-scale");
}
#endif
gtk_widget_set_size_request (bar->priv->scale, SCALE_SIZE, -1);
gtk_box_pack_start (GTK_BOX (bar->priv->scale_box),
bar->priv->start_box,
FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (bar->priv->start_box),
bar->priv->label,
FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (bar->priv->scale_box),
bar->priv->scale,
TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (bar->priv->scale_box),
bar->priv->end_box,
FALSE, FALSE, 0);
ca_gtk_widget_disable_sounds (bar->priv->scale, FALSE);
gtk_widget_add_events (bar->priv->scale, GDK_SCROLL_MASK);
g_signal_connect (G_OBJECT (bar->priv->scale),
"scroll-event",
G_CALLBACK (on_scale_scroll_event),
bar);
if (bar->priv->size_group != NULL) {
gtk_size_group_add_widget (bar->priv->size_group,
bar->priv->start_box);
if (bar->priv->symmetric)
gtk_size_group_add_widget (bar->priv->size_group,
bar->priv->end_box);
}
gtk_scale_set_draw_value (GTK_SCALE (bar->priv->scale), FALSE);
}
static void
update_scale_marks (GvcBalanceBar *bar)
{
gchar *str_lower = NULL,
*str_upper = NULL;
gdouble lower,
upper;
gtk_scale_clear_marks (GTK_SCALE (bar->priv->scale));
switch (bar->priv->btype) {
case BALANCE_TYPE_RL:
str_lower = g_strdup_printf ("<small>%s</small>", C_("balance", "Left"));
str_upper = g_strdup_printf ("<small>%s</small>", C_("balance", "Right"));
break;
case BALANCE_TYPE_FR:
str_lower = g_strdup_printf ("<small>%s</small>", C_("balance", "Rear"));
str_upper = g_strdup_printf ("<small>%s</small>", C_("balance", "Front"));
break;
case BALANCE_TYPE_LFE:
str_lower = g_strdup_printf ("<small>%s</small>", C_("balance", "Minimum"));
str_upper = g_strdup_printf ("<small>%s</small>", C_("balance", "Maximum"));
break;
}
lower = gtk_adjustment_get_lower (bar->priv->adjustment);
gtk_scale_add_mark (GTK_SCALE (bar->priv->scale),
lower,
GTK_POS_BOTTOM,
str_lower);
upper = gtk_adjustment_get_upper (bar->priv->adjustment);
gtk_scale_add_mark (GTK_SCALE (bar->priv->scale),
upper,
GTK_POS_BOTTOM,
str_upper);
g_free (str_lower);
g_free (str_upper);
if (bar->priv->btype != BALANCE_TYPE_LFE)
gtk_scale_add_mark (GTK_SCALE (bar->priv->scale),
(upper - lower) / 2 + lower,
GTK_POS_BOTTOM,
NULL);
}
void
gvc_balance_bar_set_size_group (GvcBalanceBar *bar,
GtkSizeGroup *group,
gboolean symmetric)
{
g_return_if_fail (GVC_IS_BALANCE_BAR (bar));
g_return_if_fail (GTK_IS_SIZE_GROUP (group));
bar->priv->size_group = group;
bar->priv->symmetric = symmetric;
if (bar->priv->size_group != NULL) {
gtk_size_group_add_widget (bar->priv->size_group,
bar->priv->start_box);
if (bar->priv->symmetric)
gtk_size_group_add_widget (bar->priv->size_group,
bar->priv->end_box);
}
gtk_widget_queue_draw (GTK_WIDGET (bar));
}
static void
update_balance_value (GvcBalanceBar *bar)
{
gdouble value = 0;
switch (bar->priv->btype) {
case BALANCE_TYPE_RL:
value = mate_mixer_stream_control_get_balance (bar->priv->control);
g_debug ("Balance value changed to %.2f", value);
break;
case BALANCE_TYPE_FR:
value = mate_mixer_stream_control_get_fade (bar->priv->control);
g_debug ("Fade value changed to %.2f", value);
break;
case BALANCE_TYPE_LFE:
value = mate_mixer_stream_control_get_channel_volume (bar->priv->control,
bar->priv->lfe_channel);
g_debug ("Subwoofer volume changed to %.0f", value);
break;
}
gtk_adjustment_set_value (bar->priv->adjustment, value);
}
static void
on_balance_value_changed (MateMixerStream *stream,
GParamSpec *pspec,
GvcBalanceBar *bar)
{
update_balance_value (bar);
}
static gint
find_stream_lfe_channel (MateMixerStreamControl *control)
{
guint i;
for (i = 0; i < mate_mixer_stream_control_get_num_channels (control); i++) {
MateMixerChannelPosition position;
position = mate_mixer_stream_control_get_channel_position (control, i);
if (position == MATE_MIXER_CHANNEL_LFE)
return i;
}
return -1;
}
static void
gvc_balance_bar_set_control (GvcBalanceBar *bar, MateMixerStreamControl *control)
{
g_return_if_fail (GVC_BALANCE_BAR (bar));
g_return_if_fail (MATE_MIXER_IS_STREAM_CONTROL (control));
if (bar->priv->control != NULL) {
g_signal_handlers_disconnect_by_func (G_OBJECT (bar->priv->control),
on_balance_value_changed,
bar);
g_object_unref (bar->priv->control);
}
bar->priv->control = g_object_ref (control);
if (bar->priv->btype == BALANCE_TYPE_LFE) {
gdouble minimum;
gdouble maximum;
minimum = mate_mixer_stream_control_get_min_volume (bar->priv->control);
maximum = mate_mixer_stream_control_get_normal_volume (bar->priv->control);
/* Configure the adjustment for the volume limits of the current
* stream.
* Only subwoofer scale uses volume, balance and fade use fixed
* limits which do not need to be updated as balance type is
* only set during construction. */
gtk_adjustment_configure (GTK_ADJUSTMENT (bar->priv->adjustment),
gtk_adjustment_get_value (bar->priv->adjustment),
minimum,
maximum,
(maximum - minimum) / 100.0,
(maximum - minimum) / 10.0,
0.0);
bar->priv->lfe_channel = find_stream_lfe_channel (bar->priv->control);
if (G_LIKELY (bar->priv->lfe_channel > -1))
g_debug ("Found LFE channel at position %d", bar->priv->lfe_channel);
else
g_warn_if_reached ();
} else
bar->priv->lfe_channel = -1;
switch (bar->priv->btype) {
case BALANCE_TYPE_RL:
g_signal_connect (G_OBJECT (bar->priv->control),
"notify::balance",
G_CALLBACK (on_balance_value_changed),
bar);
break;
case BALANCE_TYPE_FR:
g_signal_connect (G_OBJECT (bar->priv->control),
"notify::fade",
G_CALLBACK (on_balance_value_changed),
bar);
break;
case BALANCE_TYPE_LFE:
g_signal_connect (G_OBJECT (bar->priv->control),
"notify::volume",
G_CALLBACK (on_balance_value_changed),
bar);
break;
}
update_balance_value (bar);
update_scale_marks (bar);
g_object_notify_by_pspec (G_OBJECT (bar), properties[PROP_CONTROL]);
}
static void
gvc_balance_bar_set_balance_type (GvcBalanceBar *bar, GvcBalanceType btype)
{
GtkWidget *frame;
GtkAdjustment *adjustment;
/* Create adjustment with limits for balance and fade types because
* some limits must be provided.
* If subwoofer type is used instead, the limits will be changed when
* stream is set. */
adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, -1.0, 1.0, 0.05, 0.5, 0.0));
bar->priv->btype = btype;
bar->priv->adjustment = GTK_ADJUSTMENT (g_object_ref_sink (adjustment));
g_signal_connect (G_OBJECT (adjustment),
"value-changed",
G_CALLBACK (on_adjustment_value_changed),
bar);
switch (btype) {
case BALANCE_TYPE_RL:
bar->priv->label = gtk_label_new_with_mnemonic (_("_Balance:"));
break;
case BALANCE_TYPE_FR:
bar->priv->label = gtk_label_new_with_mnemonic (_("_Fade:"));
break;
case BALANCE_TYPE_LFE:
bar->priv->label = gtk_label_new_with_mnemonic (_("_Subwoofer:"));
break;
}
#if GTK_CHECK_VERSION (3, 0, 0)
gtk_label_set_xalign (GTK_LABEL (bar->priv->label), 0.0);
gtk_label_set_yalign (GTK_LABEL (bar->priv->label), 0.0);
#else
gtk_misc_set_alignment (GTK_MISC (bar->priv->label), 0.0, 0.0);
#endif
/* Frame */
frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
gtk_box_pack_start (GTK_BOX (bar), frame, TRUE, TRUE, 0);
/* Box with scale */
create_scale_box (bar);
gtk_container_add (GTK_CONTAINER (frame), bar->priv->scale_box);
gtk_label_set_mnemonic_widget (GTK_LABEL (bar->priv->label),
bar->priv->scale);
gtk_widget_set_direction (bar->priv->scale, GTK_TEXT_DIR_LTR);
gtk_widget_show_all (frame);
g_object_notify_by_pspec (G_OBJECT (bar), properties[PROP_BALANCE_TYPE]);
}
static void
gvc_balance_bar_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GvcBalanceBar *self = GVC_BALANCE_BAR (object);
switch (prop_id) {
case PROP_CONTROL:
gvc_balance_bar_set_control (self, g_value_get_object (value));
break;
case PROP_BALANCE_TYPE:
gvc_balance_bar_set_balance_type (self, g_value_get_int (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gvc_balance_bar_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GvcBalanceBar *self = GVC_BALANCE_BAR (object);
switch (prop_id) {
case PROP_CONTROL:
g_value_set_object (value, self->priv->control);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gvc_balance_bar_class_init (GvcBalanceBarClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = gvc_balance_bar_dispose;
object_class->set_property = gvc_balance_bar_set_property;
object_class->get_property = gvc_balance_bar_get_property;
properties[PROP_CONTROL] =
g_param_spec_object ("control",
"Control",
"MateMixer stream control",
MATE_MIXER_TYPE_STREAM_CONTROL,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS);
properties[PROP_BALANCE_TYPE] =
g_param_spec_int ("balance-type",
"balance type",
"Whether the balance is right-left or front-rear",
BALANCE_TYPE_RL,
NUM_BALANCE_TYPES - 1,
BALANCE_TYPE_RL,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, N_PROPERTIES, properties);
g_type_class_add_private (klass, sizeof (GvcBalanceBarPrivate));
}
static gboolean
on_scale_scroll_event (GtkWidget *widget,
GdkEventScroll *event,
GvcBalanceBar *bar)
{
gdouble value;
gdouble minimum;
gdouble maximum;
gdouble step;
value = gtk_adjustment_get_value (bar->priv->adjustment);
minimum = gtk_adjustment_get_lower (bar->priv->adjustment);
maximum = gtk_adjustment_get_upper (bar->priv->adjustment);
// XXX fix this for GTK3
if (bar->priv->btype == BALANCE_TYPE_LFE)
step = (maximum - minimum) / 100.0;
else
step = 0.05;
if (event->direction == GDK_SCROLL_UP) {
if (value + step > maximum)
value = maximum;
else
value = value + step;
} else if (event->direction == GDK_SCROLL_DOWN) {
if (value - step < minimum)
value = minimum;
else
value = value - step;
}
gtk_adjustment_set_value (bar->priv->adjustment, value);
return TRUE;
}
static void
on_adjustment_value_changed (GtkAdjustment *adjustment, GvcBalanceBar *bar)
{
gdouble value;
if (bar->priv->control == NULL)
return;
value = gtk_adjustment_get_value (adjustment);
switch (bar->priv->btype) {
case BALANCE_TYPE_RL:
mate_mixer_stream_control_set_balance (bar->priv->control, value);
break;
case BALANCE_TYPE_FR:
mate_mixer_stream_control_set_fade (bar->priv->control, value);
break;
case BALANCE_TYPE_LFE:
mate_mixer_stream_control_set_channel_volume (bar->priv->control,
bar->priv->lfe_channel,
value);
break;
}
}
static void
gvc_balance_bar_init (GvcBalanceBar *bar)
{
bar->priv = GVC_BALANCE_BAR_GET_PRIVATE (bar);
}
static void
gvc_balance_bar_dispose (GObject *object)
{
GvcBalanceBar *bar;
bar = GVC_BALANCE_BAR (object);
if (bar->priv->control != NULL) {
g_signal_handlers_disconnect_by_func (G_OBJECT (bar->priv->control),
on_balance_value_changed,
bar);
g_clear_object (&bar->priv->control);
}
G_OBJECT_CLASS (gvc_balance_bar_parent_class)->dispose (object);
}
GtkWidget *
gvc_balance_bar_new (MateMixerStreamControl *control, GvcBalanceType btype)
{
return g_object_new (GVC_TYPE_BALANCE_BAR,
"balance-type", btype,
"control", control,
#if GTK_CHECK_VERSION (3, 0, 0)
"orientation", GTK_ORIENTATION_HORIZONTAL,
#endif
NULL);
}
|