gtkvolumebutton.c 7.4 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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
Javier Jardón's avatar
Javier Jardón committed
18
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 20 21 22 23 24
 */

/*
 * 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
25
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26 27
 */

28
#include "config.h"
29 30

#include "gtkvolumebutton.h"
31 32

#include "gtkadjustment.h"
33
#include "gtkintl.h"
34
#include "gtktooltip.h"
35

36

37 38 39 40 41 42 43 44 45 46
/**
 * SECTION:gtkvolumebutton
 * @Short_description: A button which pops up a volume control
 * @Title: GtkVolumeButton
 *
 * #GtkVolumeButton is a subclass of #GtkScaleButton that has
 * been tailored for use as a volume control widget with suitable
 * icons, tooltips and accessible labels.
 */

47
#define EPSILON (1e-10)
48

49
static const gchar * const icons[] =
50 51 52 53 54 55 56
{
  "audio-volume-muted",
  "audio-volume-high",
  "audio-volume-low",
  "audio-volume-medium",
  NULL
};
57 58

static const gchar * const icons_symbolic[] =
59 60 61 62 63 64 65 66 67 68 69 70 71
{
  "audio-volume-muted-symbolic",
  "audio-volume-high-symbolic",
  "audio-volume-low-symbolic",
  "audio-volume-medium-symbolic",
  NULL
};

enum
{
  PROP_0,
  PROP_SYMBOLIC
};
72

73 74 75 76 77 78 79 80 81
static gboolean	cb_query_tooltip (GtkWidget       *button,
                                  gint             x,
                                  gint             y,
                                  gboolean         keyboard_mode,
                                  GtkTooltip      *tooltip,
                                  gpointer         user_data);
static void	cb_value_changed (GtkVolumeButton *button,
                                  gdouble          value,
                                  gpointer         user_data);
82 83 84

G_DEFINE_TYPE (GtkVolumeButton, gtk_volume_button, GTK_TYPE_SCALE_BUTTON)

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
static gboolean
get_symbolic (GtkScaleButton *button)
{
  gchar **icon_list;
  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;
}

103 104 105 106 107 108 109 110 111 112 113
static void
gtk_volume_button_set_property (GObject       *object,
				guint          prop_id,
				const GValue  *value,
				GParamSpec    *pspec)
{
  GtkScaleButton *button = GTK_SCALE_BUTTON (object);

  switch (prop_id)
    {
    case PROP_SYMBOLIC:
114 115 116 117 118 119 120 121
      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
	    gtk_scale_button_set_icons (button, (const char **) icons);
          g_object_notify_by_pspec (object, pspec);
        }
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_volume_button_get_property (GObject     *object,
			        guint        prop_id,
			        GValue      *value,
			        GParamSpec  *pspec)
{
  switch (prop_id)
    {
137 138
    case PROP_SYMBOLIC:
      g_value_set_boolean (value, get_symbolic (GTK_SCALE_BUTTON (object)));
139 140 141 142 143 144 145
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

146 147 148
static void
gtk_volume_button_class_init (GtkVolumeButtonClass *klass)
{
149
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
150
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

  gobject_class->set_property = gtk_volume_button_set_property;
  gobject_class->get_property = gtk_volume_button_get_property;

  /**
   * GtkVolumeButton:use-symbolic:
   *
   * 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.
   *
   * Since: 3.0
   */
  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"),
170
                                                         TRUE,
171
                                                         G_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_EXPLICIT_NOTIFY));
172 173 174

  /* Bind class to template
   */
175
  gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtkvolumebutton.ui");
176 177
  gtk_widget_class_bind_template_callback (widget_class, cb_query_tooltip);
  gtk_widget_class_bind_template_callback (widget_class, cb_value_changed);
178 179 180 181 182
}

static void
gtk_volume_button_init (GtkVolumeButton *button)
{
183 184 185 186 187 188 189
  GtkWidget *widget = GTK_WIDGET (button);

  gtk_widget_init_template (widget);

  /* The atk action description is not supported by GtkBuilder */
  atk_action_set_description (ATK_ACTION (gtk_widget_get_accessible (GTK_WIDGET (widget))),
			      1, _("Adjusts the volume"));
190 191 192
}

/**
193
 * gtk_volume_button_new:
194
 *
Matthias Clasen's avatar
Matthias Clasen committed
195 196
 * Creates a #GtkVolumeButton, with a range between 0.0 and 1.0, with
 * a stepping of 0.02. Volume values can be obtained and modified using
197 198
 * the functions from #GtkScaleButton.
 *
199
 * Returns: a new #GtkVolumeButton
200 201 202 203 204 205 206 207 208 209 210
 *
 * Since: 2.12
 */
GtkWidget *
gtk_volume_button_new (void)
{
  GObject *button;
  button = g_object_new (GTK_TYPE_VOLUME_BUTTON, NULL);
  return GTK_WIDGET (button);
}

211 212 213 214 215 216 217
static gboolean
cb_query_tooltip (GtkWidget  *button,
		  gint        x,
		  gint        y,
		  gboolean    keyboard_mode,
		  GtkTooltip *tooltip,
		  gpointer    user_data)
218
{
219
  GtkScaleButton *scale_button = GTK_SCALE_BUTTON (button);
220
  GtkAdjustment *adjustment;
221 222
  gdouble val;
  char *str;
223 224 225
  AtkImage *image;

  image = ATK_IMAGE (gtk_widget_get_accessible (button));
226

227
  adjustment = gtk_scale_button_get_adjustment (scale_button);
228
  val = gtk_scale_button_get_value (scale_button);
229

230
  if (val < (gtk_adjustment_get_lower (adjustment) + EPSILON))
231 232 233
    {
      str = g_strdup (_("Muted"));
    }
234
  else if (val >= (gtk_adjustment_get_upper (adjustment) - EPSILON))
235 236 237 238 239 240 241
    {
      str = g_strdup (_("Full Volume"));
    }
  else
    {
      int percent;

242
      percent = (int) (100. * val / (gtk_adjustment_get_upper (adjustment) - gtk_adjustment_get_lower (adjustment)) + .5);
243 244 245 246 247 248

      /* 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".
       */
249
      str = g_strdup_printf (C_("volume percentage", "%d %%"), percent);
250 251
    }

252
  gtk_tooltip_set_text (tooltip, str);
253
  atk_image_set_image_description (image, str);
254
  g_free (str);
255 256

  return TRUE;
257 258 259 260 261
}

static void
cb_value_changed (GtkVolumeButton *button, gdouble value, gpointer user_data)
{
262
  gtk_widget_trigger_tooltip_query (GTK_WIDGET (button));
263
}