2008-07-01 22:57:50 +00:00
|
|
|
|
/* GTK - The GIMP Toolkit
|
2007-05-19 23:35:00 +00:00
|
|
|
|
* Copyright (C) 2007 Red Hat, Inc.
|
|
|
|
|
*
|
|
|
|
|
* Authors:
|
|
|
|
|
* - Bastien Nocera <bnocera@redhat.com>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library 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
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2007-05-19 23:35:00 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Modified by the GTK+ Team and others 2007. See the AUTHORS
|
|
|
|
|
* file for a list of people on the GTK+ Team. See the ChangeLog
|
|
|
|
|
* files for a list of changes. These files are distributed with
|
2008-06-26 17:14:27 +00:00
|
|
|
|
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
|
2007-05-19 23:35:00 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2008-06-22 14:28:52 +00:00
|
|
|
|
#include "config.h"
|
2007-05-19 23:35:00 +00:00
|
|
|
|
|
|
|
|
|
#include "gtkvolumebutton.h"
|
2012-03-03 18:41:55 +00:00
|
|
|
|
|
|
|
|
|
#include "gtkadjustment.h"
|
2008-02-12 22:43:35 +00:00
|
|
|
|
#include "gtkintl.h"
|
2012-03-03 18:41:55 +00:00
|
|
|
|
#include "gtktooltip.h"
|
2017-11-18 13:18:11 +00:00
|
|
|
|
#include "gtkprivate.h"
|
2007-05-19 23:35:00 +00:00
|
|
|
|
|
2007-05-23 17:49:34 +00:00
|
|
|
|
|
2010-03-04 18:05:45 +00:00
|
|
|
|
/**
|
2021-02-22 00:43:00 +00:00
|
|
|
|
* GtkVolumeButton:
|
2010-03-04 18:05:45 +00:00
|
|
|
|
*
|
2021-02-22 00:43:00 +00:00
|
|
|
|
* `GtkVolumeButton` is a `GtkScaleButton` subclass tailored for
|
|
|
|
|
* volume control.
|
|
|
|
|
*
|
|
|
|
|
* ![An example GtkVolumeButton](volumebutton.png)
|
2010-03-04 18:05:45 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2019-05-19 20:34:13 +00:00
|
|
|
|
typedef struct _GtkVolumeButtonClass GtkVolumeButtonClass;
|
|
|
|
|
|
|
|
|
|
struct _GtkVolumeButtonClass
|
|
|
|
|
{
|
|
|
|
|
GtkScaleButtonClass parent_class;
|
|
|
|
|
};
|
|
|
|
|
|
2007-06-28 14:32:58 +00:00
|
|
|
|
#define EPSILON (1e-10)
|
2007-05-23 17:49:34 +00:00
|
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
|
static const char * const icons[] =
|
2010-12-10 15:05:50 +00:00
|
|
|
|
{
|
|
|
|
|
"audio-volume-muted",
|
|
|
|
|
"audio-volume-high",
|
|
|
|
|
"audio-volume-low",
|
|
|
|
|
"audio-volume-medium",
|
|
|
|
|
NULL
|
|
|
|
|
};
|
2010-12-18 02:43:42 +00:00
|
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
|
static const char * const icons_symbolic[] =
|
2010-12-10 15:05:50 +00:00
|
|
|
|
{
|
|
|
|
|
"audio-volume-muted-symbolic",
|
|
|
|
|
"audio-volume-high-symbolic",
|
|
|
|
|
"audio-volume-low-symbolic",
|
|
|
|
|
"audio-volume-medium-symbolic",
|
|
|
|
|
NULL
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
PROP_0,
|
|
|
|
|
PROP_SYMBOLIC
|
|
|
|
|
};
|
2007-05-19 23:35:00 +00:00
|
|
|
|
|
2020-05-07 02:52:20 +00:00
|
|
|
|
static gboolean cb_query_tooltip (GtkWidget *button,
|
2020-07-24 13:54:49 +00:00
|
|
|
|
int x,
|
|
|
|
|
int y,
|
2008-06-26 17:14:27 +00:00
|
|
|
|
gboolean keyboard_mode,
|
|
|
|
|
GtkTooltip *tooltip,
|
|
|
|
|
gpointer user_data);
|
2020-05-07 02:52:20 +00:00
|
|
|
|
static void cb_value_changed (GtkVolumeButton *button,
|
2020-07-24 20:32:16 +00:00
|
|
|
|
double value,
|
2008-06-26 17:14:27 +00:00
|
|
|
|
gpointer user_data);
|
2007-05-19 23:35:00 +00:00
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkVolumeButton, gtk_volume_button, GTK_TYPE_SCALE_BUTTON)
|
|
|
|
|
|
2014-06-09 15:51:12 +00:00
|
|
|
|
static gboolean
|
|
|
|
|
get_symbolic (GtkScaleButton *button)
|
|
|
|
|
{
|
2020-07-24 18:40:36 +00:00
|
|
|
|
char **icon_list;
|
2014-06-09 15:51:12 +00:00
|
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
|
|
g_object_get (button, "icons", &icon_list, NULL);
|
|
|
|
|
if (icon_list != NULL &&
|
|
|
|
|
icon_list[0] != NULL &&
|
|
|
|
|
g_str_equal (icon_list[0], icons_symbolic[0]))
|
|
|
|
|
ret = TRUE;
|
|
|
|
|
else
|
|
|
|
|
ret = FALSE;
|
|
|
|
|
g_strfreev (icon_list);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-10 15:05:50 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_volume_button_set_property (GObject *object,
|
2020-05-07 02:52:20 +00:00
|
|
|
|
guint prop_id,
|
|
|
|
|
const GValue *value,
|
|
|
|
|
GParamSpec *pspec)
|
2010-12-10 15:05:50 +00:00
|
|
|
|
{
|
|
|
|
|
GtkScaleButton *button = GTK_SCALE_BUTTON (object);
|
|
|
|
|
|
|
|
|
|
switch (prop_id)
|
|
|
|
|
{
|
|
|
|
|
case PROP_SYMBOLIC:
|
2014-06-09 15:51:12 +00:00
|
|
|
|
if (get_symbolic (button) != g_value_get_boolean (value))
|
|
|
|
|
{
|
|
|
|
|
if (g_value_get_boolean (value))
|
|
|
|
|
gtk_scale_button_set_icons (button, (const char **) icons_symbolic);
|
|
|
|
|
else
|
2020-05-07 02:52:20 +00:00
|
|
|
|
gtk_scale_button_set_icons (button, (const char **) icons);
|
2014-06-09 15:51:12 +00:00
|
|
|
|
g_object_notify_by_pspec (object, pspec);
|
|
|
|
|
}
|
2010-12-10 15:05:50 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gtk_volume_button_get_property (GObject *object,
|
2020-05-07 02:52:20 +00:00
|
|
|
|
guint prop_id,
|
|
|
|
|
GValue *value,
|
|
|
|
|
GParamSpec *pspec)
|
2010-12-10 15:05:50 +00:00
|
|
|
|
{
|
|
|
|
|
switch (prop_id)
|
|
|
|
|
{
|
2014-06-09 15:51:12 +00:00
|
|
|
|
case PROP_SYMBOLIC:
|
|
|
|
|
g_value_set_boolean (value, get_symbolic (GTK_SCALE_BUTTON (object)));
|
2010-12-10 15:05:50 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-19 23:35:00 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_volume_button_class_init (GtkVolumeButtonClass *klass)
|
|
|
|
|
{
|
2010-12-10 15:05:50 +00:00
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
2013-03-30 09:51:58 +00:00
|
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
2010-12-10 15:05:50 +00:00
|
|
|
|
|
|
|
|
|
gobject_class->set_property = gtk_volume_button_set_property;
|
|
|
|
|
gobject_class->get_property = gtk_volume_button_get_property;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GtkVolumeButton:use-symbolic:
|
|
|
|
|
*
|
2021-02-22 00:43:00 +00:00
|
|
|
|
* Whether to use symbolic icons as the icons.
|
|
|
|
|
*
|
|
|
|
|
* Note that if the symbolic icons are not available in your installed
|
|
|
|
|
* theme, then the normal (potentially colorful) icons will be used.
|
2010-12-10 15:05:50 +00:00
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_SYMBOLIC,
|
|
|
|
|
g_param_spec_boolean ("use-symbolic",
|
|
|
|
|
P_("Use symbolic icons"),
|
|
|
|
|
P_("Whether to use symbolic icons"),
|
2014-02-13 08:54:27 +00:00
|
|
|
|
TRUE,
|
2017-11-18 13:18:11 +00:00
|
|
|
|
GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_EXPLICIT_NOTIFY));
|
2013-03-30 09:51:58 +00:00
|
|
|
|
|
|
|
|
|
/* Bind class to template
|
|
|
|
|
*/
|
2014-01-23 23:59:20 +00:00
|
|
|
|
gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtkvolumebutton.ui");
|
2013-07-26 17:49:49 +00:00
|
|
|
|
gtk_widget_class_bind_template_callback (widget_class, cb_query_tooltip);
|
|
|
|
|
gtk_widget_class_bind_template_callback (widget_class, cb_value_changed);
|
2007-05-19 23:35:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gtk_volume_button_init (GtkVolumeButton *button)
|
|
|
|
|
{
|
2013-03-30 09:51:58 +00:00
|
|
|
|
GtkWidget *widget = GTK_WIDGET (button);
|
|
|
|
|
|
|
|
|
|
gtk_widget_init_template (widget);
|
2007-05-19 23:35:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-04-13 00:07:28 +00:00
|
|
|
|
* gtk_volume_button_new:
|
2007-05-19 23:35:00 +00:00
|
|
|
|
*
|
2021-02-22 00:43:00 +00:00
|
|
|
|
* Creates a `GtkVolumeButton`.
|
|
|
|
|
*
|
|
|
|
|
* The button has a range between 0.0 and 1.0, with a stepping of 0.02.
|
|
|
|
|
* Volume values can be obtained and modified using the functions from
|
|
|
|
|
* [class@Gtk.ScaleButton].
|
2007-05-19 23:35:00 +00:00
|
|
|
|
*
|
2021-02-22 00:43:00 +00:00
|
|
|
|
* Returns: a new `GtkVolumeButton`
|
2007-05-19 23:35:00 +00:00
|
|
|
|
*/
|
|
|
|
|
GtkWidget *
|
|
|
|
|
gtk_volume_button_new (void)
|
|
|
|
|
{
|
|
|
|
|
GObject *button;
|
|
|
|
|
button = g_object_new (GTK_TYPE_VOLUME_BUTTON, NULL);
|
|
|
|
|
return GTK_WIDGET (button);
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-09 20:32:35 +00:00
|
|
|
|
static gboolean
|
|
|
|
|
cb_query_tooltip (GtkWidget *button,
|
2020-07-24 13:54:49 +00:00
|
|
|
|
int x,
|
|
|
|
|
int y,
|
2020-05-07 02:52:20 +00:00
|
|
|
|
gboolean keyboard_mode,
|
|
|
|
|
GtkTooltip *tooltip,
|
|
|
|
|
gpointer user_data)
|
2007-05-19 23:35:00 +00:00
|
|
|
|
{
|
2007-06-28 14:32:58 +00:00
|
|
|
|
GtkScaleButton *scale_button = GTK_SCALE_BUTTON (button);
|
2011-01-05 22:03:09 +00:00
|
|
|
|
GtkAdjustment *adjustment;
|
2020-07-24 20:32:16 +00:00
|
|
|
|
double val;
|
2007-05-19 23:35:00 +00:00
|
|
|
|
char *str;
|
|
|
|
|
|
2011-01-05 22:03:09 +00:00
|
|
|
|
adjustment = gtk_scale_button_get_adjustment (scale_button);
|
2007-06-28 14:32:58 +00:00
|
|
|
|
val = gtk_scale_button_get_value (scale_button);
|
2007-05-19 23:35:00 +00:00
|
|
|
|
|
2011-01-05 22:03:09 +00:00
|
|
|
|
if (val < (gtk_adjustment_get_lower (adjustment) + EPSILON))
|
2007-05-19 23:35:00 +00:00
|
|
|
|
{
|
|
|
|
|
str = g_strdup (_("Muted"));
|
|
|
|
|
}
|
2011-01-05 22:03:09 +00:00
|
|
|
|
else if (val >= (gtk_adjustment_get_upper (adjustment) - EPSILON))
|
2007-05-19 23:35:00 +00:00
|
|
|
|
{
|
|
|
|
|
str = g_strdup (_("Full Volume"));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int percent;
|
|
|
|
|
|
2011-01-05 22:03:09 +00:00
|
|
|
|
percent = (int) (100. * val / (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment)) + .5);
|
2007-06-28 14:32:58 +00:00
|
|
|
|
|
|
|
|
|
/* Translators: this is the percentage of the current volume,
|
|
|
|
|
* as used in the tooltip, eg. "49 %".
|
|
|
|
|
* Translate the "%d" to "%Id" if you want to use localised digits,
|
|
|
|
|
* or otherwise translate the "%d" to "%d".
|
|
|
|
|
*/
|
2014-08-22 15:06:47 +00:00
|
|
|
|
str = g_strdup_printf (C_("volume percentage", "%d %%"), percent);
|
2007-05-19 23:35:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-07-09 20:32:35 +00:00
|
|
|
|
gtk_tooltip_set_text (tooltip, str);
|
2007-05-19 23:35:00 +00:00
|
|
|
|
g_free (str);
|
2007-07-09 20:32:35 +00:00
|
|
|
|
|
|
|
|
|
return TRUE;
|
2007-05-19 23:35:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2020-07-24 20:32:16 +00:00
|
|
|
|
cb_value_changed (GtkVolumeButton *button, double value, gpointer user_data)
|
2007-05-19 23:35:00 +00:00
|
|
|
|
{
|
2007-07-09 20:32:35 +00:00
|
|
|
|
gtk_widget_trigger_tooltip_query (GTK_WIDGET (button));
|
2007-05-19 23:35:00 +00:00
|
|
|
|
}
|