gtkscalebutton.c 43.3 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 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 * Copyright (C) 2005 Ronald S. Bultje
 * Copyright (C) 2006, 2007 Christian Persch
 * Copyright (C) 2006 Jan Arne Petersen
 * Copyright (C) 2005-2007 Red Hat, Inc.
 *
 * Authors:
 * - Ronald S. Bultje <rbultje@ronald.bitfreak.net>
 * - Bastien Nocera <bnocera@redhat.com>
 * - Jan Arne Petersen <jpetersen@jpetersen.org>
 * - Christian Persch <chpe@svn.gnome.org>
 *
 * 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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * 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
33
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
34 35
 */

36
#include "config.h"
37

38
#ifndef _WIN32
39
#define _GNU_SOURCE
40
#endif
41 42 43 44
#include <math.h>
#include <stdlib.h>
#include <string.h>

45 46 47
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk/gdkkeysyms.h>

48 49
#include "gtkbindings.h"
#include "gtkframe.h"
50
#include "gtkmain.h"
51
#include "gtkmarshalers.h"
52
#include "gtkorientable.h"
53
#include "gtkprivate.h"
54
#include "gtkscale.h"
55
#include "gtkscalebutton.h"
56 57 58
#include "gtkstock.h"
#include "gtkvbox.h"
#include "gtkwindow.h"
59

60
#include "gtkintl.h"
61 62
#include "gtkalias.h"

63 64 65
#define SCALE_SIZE 100
#define CLICK_TIMEOUT 250

66 67
enum
{
68 69 70
  VALUE_CHANGED,
  POPUP,
  POPDOWN,
71

72 73 74
  LAST_SIGNAL
};

75 76
enum
{
77
  PROP_0,
78

79
  PROP_ORIENTATION,
80 81
  PROP_VALUE,
  PROP_SIZE,
82 83
  PROP_ADJUSTMENT,
  PROP_ICONS
84 85
};

86 87 88 89 90
#define GET_PRIVATE(obj)        (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_SCALE_BUTTON, GtkScaleButtonPrivate))

struct _GtkScaleButtonPrivate
{
  GtkWidget *dock;
91
  GtkWidget *box;
92 93 94
  GtkWidget *scale;
  GtkWidget *image;

95
  GtkIconSize size;
96
  GtkOrientation orientation;
97

98
  guint click_id;
99 100
  gint click_timeout;
  guint timeout : 1;
101 102
  gdouble direction;
  guint32 pop_time;
103

104
  gchar **icon_list;
105 106

  GtkAdjustment *adjustment; /* needed because it must be settable in init() */
107
};
108

109 110 111
static GObject* gtk_scale_button_constructor    (GType                  type,
                                                 guint                  n_construct_properties,
                                                 GObjectConstructParam *construct_params);
112
static void	gtk_scale_button_dispose	(GObject             *object);
113
static void     gtk_scale_button_finalize       (GObject             *object);
114 115 116 117 118 119 120 121
static void	gtk_scale_button_set_property	(GObject             *object,
						 guint                prop_id,
						 const GValue        *value,
						 GParamSpec          *pspec);
static void	gtk_scale_button_get_property	(GObject             *object,
						 guint                prop_id,
						 GValue              *value,
						 GParamSpec          *pspec);
122 123
static void gtk_scale_button_set_orientation_private (GtkScaleButton *button,
                                                      GtkOrientation  orientation);
124 125 126 127 128 129 130 131
static gboolean	gtk_scale_button_scroll		(GtkWidget           *widget,
						 GdkEventScroll      *event);
static void gtk_scale_button_screen_changed	(GtkWidget           *widget,
						 GdkScreen           *previous_screen);
static gboolean	gtk_scale_button_press		(GtkWidget           *widget,
						 GdkEventButton      *event);
static gboolean gtk_scale_button_key_release	(GtkWidget           *widget,
    						 GdkEventKey         *event);
132 133
static void     gtk_scale_button_popup          (GtkWidget           *widget);
static void     gtk_scale_button_popdown        (GtkWidget           *widget);
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
static gboolean cb_dock_button_press		(GtkWidget           *widget,
						 GdkEventButton      *event,
						 gpointer             user_data);
static gboolean cb_dock_key_release		(GtkWidget           *widget,
						 GdkEventKey         *event,
						 gpointer             user_data);
static gboolean cb_button_press			(GtkWidget           *widget,
						 GdkEventButton      *event,
						 gpointer             user_data);
static gboolean cb_button_release		(GtkWidget           *widget,
						 GdkEventButton      *event,
						 gpointer             user_data);
static void cb_dock_grab_notify			(GtkWidget           *widget,
						 gboolean             was_grabbed,
						 gpointer             user_data);
static gboolean cb_dock_grab_broken_event	(GtkWidget           *widget,
						 gboolean             was_grabbed,
						 gpointer             user_data);
static void cb_scale_grab_notify		(GtkWidget           *widget,
						 gboolean             was_grabbed,
						 gpointer             user_data);
static void gtk_scale_button_update_icon	(GtkScaleButton      *button);
static void gtk_scale_button_scale_value_changed(GtkRange            *range);

/* see below for scale definitions */
159
static GtkWidget *gtk_scale_button_scale_new    (GtkScaleButton      *button);
160

161 162 163
G_DEFINE_TYPE_WITH_CODE (GtkScaleButton, gtk_scale_button, GTK_TYPE_BUTTON,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
                                                NULL))
164

165
static guint signals[LAST_SIGNAL] = { 0, };
166 167 168 169 170 171

static void
gtk_scale_button_class_init (GtkScaleButtonClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
172
  GtkBindingSet *binding_set;
173

174
  g_type_class_add_private (klass, sizeof (GtkScaleButtonPrivate));
175

176
  gobject_class->constructor = gtk_scale_button_constructor;
177
  gobject_class->finalize = gtk_scale_button_finalize;
178 179 180
  gobject_class->dispose = gtk_scale_button_dispose;
  gobject_class->set_property = gtk_scale_button_set_property;
  gobject_class->get_property = gtk_scale_button_get_property;
181

182 183 184 185 186
  widget_class->button_press_event = gtk_scale_button_press;
  widget_class->key_release_event = gtk_scale_button_key_release;
  widget_class->scroll_event = gtk_scale_button_scroll;
  widget_class->screen_changed = gtk_scale_button_screen_changed;

187 188 189 190 191
  /**
   * GtkScaleButton:orientation:
   *
   * The orientation of the #GtkScaleButton's popup window.
   *
192 193 194 195 196
   * Note that since GTK+ 2.16, #GtkScaleButton implements the
   * #GtkOrientable interface which has its own @orientation
   * property. However we redefine the property here in order to
   * override its default horizontal orientation.
   *
197 198
   * Since: 2.14
   **/
199 200 201
  g_object_class_override_property (gobject_class,
				    PROP_ORIENTATION,
				    "orientation");
202

203 204 205 206 207 208 209 210
  g_object_class_install_property (gobject_class,
				   PROP_VALUE,
				   g_param_spec_double ("value",
							P_("Value"),
							P_("The value of the scale"),
							-G_MAXDOUBLE,
							G_MAXDOUBLE,
							0,
211
							GTK_PARAM_READWRITE));
212 213 214 215 216 217 218 219

  g_object_class_install_property (gobject_class,
				   PROP_SIZE,
				   g_param_spec_enum ("size",
						      P_("Icon size"),
						      P_("The icon size"),
						      GTK_TYPE_ICON_SIZE,
						      GTK_ICON_SIZE_SMALL_TOOLBAR,
220
						      GTK_PARAM_READWRITE));
221 222 223 224 225 226 227

  g_object_class_install_property (gobject_class,
                                   PROP_ADJUSTMENT,
                                   g_param_spec_object ("adjustment",
							P_("Adjustment"),
							P_("The GtkAdjustment that contains the current value of this scale button object"),
                                                        GTK_TYPE_ADJUSTMENT,
228
                                                        GTK_PARAM_READWRITE));
229

230 231 232
  /**
   * GtkScaleButton:icons:
   *
233 234 235 236 237
   * The names of the icons to be used by the scale button.
   * The first item in the array will be used in the button
   * when the current value is the lowest value, the second
   * item for the highest value. All the subsequent icons will
   * be used for all the other values, spread evenly over the
238 239
   * range of values.
   *
240 241 242
   * If there's only one icon name in the @icons array, it will
   * be used for all the values. If only two icon names are in
   * the @icons array, the first one will be used for the bottom
243 244
   * 50% of the scale, and the second one for the top 50%.
   *
245 246
   * It is recommended to use at least 3 icons so that the
   * #GtkScaleButton reflects the current value of the scale
247 248 249 250 251 252 253 254 255 256 257
   * better for the users.
   *
   * Since: 2.12
   */
  g_object_class_install_property (gobject_class,
                                   PROP_ICONS,
                                   g_param_spec_boxed ("icons",
                                                       P_("Icons"),
                                                       P_("List of icon names"),
                                                       G_TYPE_STRV,
                                                       GTK_PARAM_READWRITE));
258

259 260
  /**
   * GtkScaleButton::value-changed:
261 262
   * @button: the object which received the signal
   * @value: the new value
263
   *
264 265
   * The ::value-changed signal is emitted when the value field has
   * changed.
266 267 268 269 270 271 272 273 274 275 276
   *
   * Since: 2.12
   */
  signals[VALUE_CHANGED] =
    g_signal_new (I_("value-changed"),
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkScaleButtonClass, value_changed),
		  NULL, NULL,
		  _gtk_marshal_VOID__DOUBLE,
		  G_TYPE_NONE, 1, G_TYPE_DOUBLE);
277

278 279 280 281
  /**
   * GtkScaleButton::popup:
   * @button: the object which received the signal
   *
282 283
   * The ::popup signal is a
   * <link linkend="keybinding-signals">keybinding signal</link>
284 285 286 287 288 289
   * which gets emitted to popup the scale widget.
   *
   * The default bindings for this signal are Space, Enter and Return.
   *
   * Since: 2.12
   */
290
  signals[POPUP] =
291 292 293 294 295 296 297
    g_signal_new_class_handler (I_("popup"),
                                G_OBJECT_CLASS_TYPE (klass),
                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                                G_CALLBACK (gtk_scale_button_popup),
                                NULL, NULL,
                                g_cclosure_marshal_VOID__VOID,
                                G_TYPE_NONE, 0);
298 299 300 301 302

  /**
   * GtkScaleButton::popdown:
   * @button: the object which received the signal
   *
303 304
   * The ::popdown signal is a
   * <link linkend="keybinding-signals">keybinding signal</link>
305 306 307 308 309 310
   * which gets emitted to popdown the scale widget.
   *
   * The default binding for this signal is Escape.
   *
   * Since: 2.12
   */
311
  signals[POPDOWN] =
312 313 314 315 316 317 318
    g_signal_new_class_handler (I_("popdown"),
                                G_OBJECT_CLASS_TYPE (klass),
                                G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                                G_CALLBACK (gtk_scale_button_popdown),
                                NULL, NULL,
                                g_cclosure_marshal_VOID__VOID,
                                G_TYPE_NONE, 0);
319 320 321 322 323 324

  /* Key bindings */
  binding_set = gtk_binding_set_by_class (widget_class);

  gtk_binding_entry_add_signal (binding_set, GDK_space, 0,
				"popup", 0);
325 326
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, 0,
				"popup", 0);
327 328 329 330 331 332 333 334 335 336 337 338 339 340
  gtk_binding_entry_add_signal (binding_set, GDK_Return, 0,
				"popup", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0,
				"popup", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0,
				"popup", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
				"popdown", 0);
}

static void
gtk_scale_button_init (GtkScaleButton *button)
{
  GtkScaleButtonPrivate *priv;
341
  GtkWidget *frame;
342

343
  button->priv = priv = GET_PRIVATE (button);
344 345 346 347

  priv->timeout = FALSE;
  priv->click_id = 0;
  priv->click_timeout = CLICK_TIMEOUT;
348
  priv->orientation = GTK_ORIENTATION_VERTICAL;
349 350 351 352 353 354 355 356 357 358 359

  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
  gtk_button_set_focus_on_click (GTK_BUTTON (button), FALSE);

  /* image */
  priv->image = gtk_image_new ();
  gtk_container_add (GTK_CONTAINER (button), priv->image);
  gtk_widget_show_all (priv->image);

  /* window */
  priv->dock = gtk_window_new (GTK_WINDOW_POPUP);
360
  gtk_widget_set_name (priv->dock, "gtk-scalebutton-popup-window");
361 362 363 364 365 366 367 368 369 370
  g_signal_connect (priv->dock, "button-press-event",
		    G_CALLBACK (cb_dock_button_press), button);
  g_signal_connect (priv->dock, "key-release-event",
		    G_CALLBACK (cb_dock_key_release), button);
  g_signal_connect (priv->dock, "grab-notify",
		    G_CALLBACK (cb_dock_grab_notify), button);
  g_signal_connect (priv->dock, "grab-broken-event",
		    G_CALLBACK (cb_dock_grab_broken_event), button);
  gtk_window_set_decorated (GTK_WINDOW (priv->dock), FALSE);

371 372 373 374 375 376 377 378 379
  /* frame */
  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_OUT);
  gtk_container_add (GTK_CONTAINER (priv->dock), frame);

  /* box for scale and +/- buttons */
  priv->box = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (frame), priv->box);

380 381 382 383 384 385 386
  /* + */
  button->plus_button = gtk_button_new_with_label ("+");
  gtk_button_set_relief (GTK_BUTTON (button->plus_button), GTK_RELIEF_NONE);
  g_signal_connect (button->plus_button, "button-press-event",
		    G_CALLBACK (cb_button_press), button);
  g_signal_connect (button->plus_button, "button-release-event",
		    G_CALLBACK (cb_button_release), button);
387
  gtk_box_pack_start (GTK_BOX (priv->box), button->plus_button, FALSE, FALSE, 0);
388 389 390 391 392 393 394 395

  /* - */
  button->minus_button = gtk_button_new_with_label ("-");
  gtk_button_set_relief (GTK_BUTTON (button->minus_button), GTK_RELIEF_NONE);
  g_signal_connect (button->minus_button, "button-press-event",
		   G_CALLBACK (cb_button_press), button);
  g_signal_connect (button->minus_button, "button-release-event",
		    G_CALLBACK (cb_button_release), button);
396
  gtk_box_pack_end (GTK_BOX (priv->box), button->minus_button, FALSE, FALSE, 0);
397 398 399

  priv->adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 100.0, 2, 20, 0));
  g_object_ref_sink (priv->adjustment);
400 401 402 403

  /* the scale */
  priv->scale = gtk_scale_button_scale_new (button);
  gtk_container_add (GTK_CONTAINER (priv->box), priv->scale);
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
}

static GObject *
gtk_scale_button_constructor (GType                  type,
                              guint                  n_construct_properties,
                              GObjectConstructParam *construct_params)
{
  GObject *object;
  GtkScaleButton *button;
  GtkScaleButtonPrivate *priv;

  object = G_OBJECT_CLASS (gtk_scale_button_parent_class)->constructor (type, n_construct_properties, construct_params);

  button = GTK_SCALE_BUTTON (object);

  priv = button->priv;

421 422 423
  /* set button text and size */
  priv->size = GTK_ICON_SIZE_SMALL_TOOLBAR;
  gtk_scale_button_update_icon (button);
424 425

  return object;
426 427 428 429 430 431 432 433
}

static void
gtk_scale_button_set_property (GObject       *object,
			       guint          prop_id,
			       const GValue  *value,
			       GParamSpec    *pspec)
{
434
  GtkScaleButton *button = GTK_SCALE_BUTTON (object);
435 436 437

  switch (prop_id)
    {
438
    case PROP_ORIENTATION:
439
      gtk_scale_button_set_orientation_private (button, g_value_get_enum (value));
440
      break;
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
    case PROP_VALUE:
      gtk_scale_button_set_value (button, g_value_get_double (value));
      break;
    case PROP_SIZE:
      {
	GtkIconSize size;
	size = g_value_get_enum (value);
	if (button->priv->size != size)
	  {
	    button->priv->size = size;
	    gtk_scale_button_update_icon (button);
	  }
      }
      break;
    case PROP_ADJUSTMENT:
      gtk_scale_button_set_adjustment (button, g_value_get_object (value));
      break;
458
    case PROP_ICONS:
459
      gtk_scale_button_set_icons (button,
460 461
                                  (const gchar **)g_value_get_boxed (value));
      break;
462 463 464 465 466 467
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

468
static void
469 470 471 472 473
gtk_scale_button_get_property (GObject     *object,
			       guint        prop_id,
			       GValue      *value,
			       GParamSpec  *pspec)
{
474 475
  GtkScaleButton *button = GTK_SCALE_BUTTON (object);
  GtkScaleButtonPrivate *priv = button->priv;
476 477 478

  switch (prop_id)
    {
479 480 481
    case PROP_ORIENTATION:
      g_value_set_enum (value, priv->orientation);
      break;
482
    case PROP_VALUE:
Matthias Clasen's avatar
Matthias Clasen committed
483
      g_value_set_double (value, gtk_scale_button_get_value (button));
484 485 486 487 488 489 490
      break;
    case PROP_SIZE:
      g_value_set_enum (value, priv->size);
      break;
    case PROP_ADJUSTMENT:
      g_value_set_object (value, gtk_scale_button_get_adjustment (button));
      break;
491 492 493
    case PROP_ICONS:
      g_value_set_boxed (value, priv->icon_list);
      break;
494 495 496 497 498 499 500
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
501
gtk_scale_button_finalize (GObject *object)
502
{
503 504
  GtkScaleButton *button = GTK_SCALE_BUTTON (object);
  GtkScaleButtonPrivate *priv = button->priv;
505

506 507 508 509 510
  if (priv->icon_list)
    {
      g_strfreev (priv->icon_list);
      priv->icon_list = NULL;
    }
511

512 513 514 515 516 517
  if (priv->adjustment)
    {
      g_object_unref (priv->adjustment);
      priv->adjustment = NULL;
    }

518 519
  G_OBJECT_CLASS (gtk_scale_button_parent_class)->finalize (object);
}
520

521 522 523 524 525
static void
gtk_scale_button_dispose (GObject *object)
{
  GtkScaleButton *button = GTK_SCALE_BUTTON (object);
  GtkScaleButtonPrivate *priv = button->priv;
526

527
  if (priv->dock)
528 529 530 531 532
    {
      gtk_widget_destroy (priv->dock);
      priv->dock = NULL;
    }

533
  if (priv->click_id != 0)
534 535 536 537 538
    {
      g_source_remove (priv->click_id);
      priv->click_id = 0;
    }

539
  G_OBJECT_CLASS (gtk_scale_button_parent_class)->dispose (object);
540 541 542
}

/**
543
 * gtk_scale_button_new:
544
 * @size: (int): a stock icon size
Matthias Clasen's avatar
Matthias Clasen committed
545 546 547 548
 * @min: the minimum value of the scale (usually 0)
 * @max: the maximum value of the scale (usually 100)
 * @step: the stepping of value when a scroll-wheel event,
 *        or up/down arrow event occurs (usually 2)
549
 * @icons: (allow-none): a %NULL-terminated array of icon names, or %NULL if
Matthias Clasen's avatar
Matthias Clasen committed
550
 *         you want to set the list later with gtk_scale_button_set_icons()
551
 *
Matthias Clasen's avatar
Matthias Clasen committed
552 553
 * Creates a #GtkScaleButton, with a range between @min and @max, with
 * a stepping of @step.
554 555 556 557 558 559 560
 *
 * Return value: a new #GtkScaleButton
 *
 * Since: 2.12
 */
GtkWidget *
gtk_scale_button_new (GtkIconSize   size,
Matthias Clasen's avatar
Matthias Clasen committed
561 562 563
		      gdouble       min,
		      gdouble       max,
		      gdouble       step,
564 565 566
		      const gchar **icons)
{
  GtkScaleButton *button;
567
  GtkObject *adj;
568

569
  adj = gtk_adjustment_new (min, min, max, step, 10 * step, 0);
570 571

  button = g_object_new (GTK_TYPE_SCALE_BUTTON,
572 573 574 575
                         "adjustment", adj,
                         "icons", icons,
                         "size", size,
                         NULL);
576 577 578 579 580

  return GTK_WIDGET (button);
}

/**
581
 * gtk_scale_button_get_value:
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
 * @button: a #GtkScaleButton
 *
 * Gets the current value of the scale button.
 *
 * Return value: current value of the scale button
 *
 * Since: 2.12
 */
gdouble
gtk_scale_button_get_value (GtkScaleButton * button)
{
  GtkScaleButtonPrivate *priv;

  g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), 0);

  priv = button->priv;

599
  return gtk_adjustment_get_value (priv->adjustment);
600 601 602
}

/**
603
 * gtk_scale_button_set_value:
604 605 606
 * @button: a #GtkScaleButton
 * @value: new value of the scale button
 *
607 608 609
 * Sets the current value of the scale; if the value is outside
 * the minimum or maximum range values, it will be clamped to fit
 * inside them. The scale button emits the #GtkScaleButton::value-changed
610
 * signal if the value changes.
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
 *
 * Since: 2.12
 */
void
gtk_scale_button_set_value (GtkScaleButton *button,
			    gdouble         value)
{
  GtkScaleButtonPrivate *priv;

  g_return_if_fail (GTK_IS_SCALE_BUTTON (button));

  priv = button->priv;

  gtk_range_set_value (GTK_RANGE (priv->scale), value);
}

/**
628
 * gtk_scale_button_set_icons:
629 630 631
 * @button: a #GtkScaleButton
 * @icons: a %NULL-terminated array of icon names
 *
632
 * Sets the icons to be used by the scale button.
633
 * For details, see the #GtkScaleButton:icons property.
634
 *
635
 * Since: 2.12
636 637 638 639 640 641
 */
void
gtk_scale_button_set_icons (GtkScaleButton  *button,
			    const gchar    **icons)
{
  GtkScaleButtonPrivate *priv;
642
  gchar **tmp;
643 644 645 646 647

  g_return_if_fail (GTK_IS_SCALE_BUTTON (button));

  priv = button->priv;

648
  tmp = priv->icon_list;
649
  priv->icon_list = g_strdupv ((gchar **) icons);
650
  g_strfreev (tmp);
651
  gtk_scale_button_update_icon (button);
652 653

  g_object_notify (G_OBJECT (button), "icons");
654 655 656
}

/**
657
 * gtk_scale_button_get_adjustment:
658 659 660 661 662
 * @button: a #GtkScaleButton
 *
 * Gets the #GtkAdjustment associated with the #GtkScaleButton's scale.
 * See gtk_range_get_adjustment() for details.
 *
Matthias Clasen's avatar
Matthias Clasen committed
663 664
 * Returns: the adjustment associated with the scale
 *
665 666 667 668 669 670 671
 * Since: 2.12
 */
GtkAdjustment*
gtk_scale_button_get_adjustment	(GtkScaleButton *button)
{
  g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);

672
  return button->priv->adjustment;
673 674 675
}

/**
676
 * gtk_scale_button_set_adjustment:
677 678 679
 * @button: a #GtkScaleButton
 * @adjustment: a #GtkAdjustment
 *
680
 * Sets the #GtkAdjustment to be used as a model
681
 * for the #GtkScaleButton's scale.
682 683 684 685 686 687 688 689 690
 * See gtk_range_set_adjustment() for details.
 *
 * Since: 2.12
 */
void
gtk_scale_button_set_adjustment	(GtkScaleButton *button,
				 GtkAdjustment  *adjustment)
{
  g_return_if_fail (GTK_IS_SCALE_BUTTON (button));
691 692 693 694
  if (!adjustment)
    adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
  else
    g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
695

696 697 698 699 700
  if (button->priv->adjustment != adjustment)
    {
      if (button->priv->adjustment)
        g_object_unref (button->priv->adjustment);
      button->priv->adjustment = g_object_ref_sink (adjustment);
701

702 703
      if (button->priv->scale)
        gtk_range_set_adjustment (GTK_RANGE (button->priv->scale), adjustment);
704

705 706
      g_object_notify (G_OBJECT (button), "adjustment");
    }
707 708
}

709 710 711 712 713 714 715 716 717
/**
 * gtk_scale_button_get_orientation:
 * @button: a #GtkScaleButton
 *
 * Gets the orientation of the #GtkScaleButton's popup window.
 *
 * Returns: the #GtkScaleButton's orientation.
 *
 * Since: 2.14
718 719
 *
 * Deprecated: 2.16: Use gtk_orientable_get_orientation() instead.
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
 **/
GtkOrientation
gtk_scale_button_get_orientation (GtkScaleButton *button)
{
  g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), GTK_ORIENTATION_VERTICAL);

  return button->priv->orientation;
}

/**
 * gtk_scale_button_set_orientation:
 * @button: a #GtkScaleButton
 * @orientation: the new orientation
 *
 * Sets the orientation of the #GtkScaleButton's popup window.
 *
 * Since: 2.14
737 738
 *
 * Deprecated: 2.16: Use gtk_orientable_set_orientation() instead.
739 740 741 742 743 744 745
 **/
void
gtk_scale_button_set_orientation (GtkScaleButton *button,
                                  GtkOrientation  orientation)
{
  g_return_if_fail (GTK_IS_SCALE_BUTTON (button));

746
  gtk_scale_button_set_orientation_private (button, orientation);
747 748
}

749 750 751 752 753 754
/**
 * gtk_scale_button_get_plus_button:
 * @button: a #GtkScaleButton
 *
 * Retrieves the plus button of the #GtkScaleButton.
 *
Matthias Clasen's avatar
Matthias Clasen committed
755 756
 * Returns: the plus button of the #GtkScaleButton.
 *
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
 * Since: 2.14
 */
GtkWidget *
gtk_scale_button_get_plus_button (GtkScaleButton *button)
{
  g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);

  return button->plus_button;
}

/**
 * gtk_scale_button_get_minus_button:
 * @button: a #GtkScaleButton
 *
 * Retrieves the minus button of the #GtkScaleButton.
 *
Matthias Clasen's avatar
Matthias Clasen committed
773 774
 * Returns: the minus button of the #GtkScaleButton.
 *
775 776 777 778 779 780 781 782 783 784
 * Since: 2.14
 */
GtkWidget *
gtk_scale_button_get_minus_button (GtkScaleButton *button)
{
  g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);

  return button->minus_button;
}

785 786 787 788 789 790
/**
 * gtk_scale_button_get_popup:
 * @button: a #GtkScaleButton
 *
 * Retrieves the popup of the #GtkScaleButton.
 *
Matthias Clasen's avatar
Matthias Clasen committed
791 792
 * Returns: the popup of the #GtkScaleButton
 *
793 794
 * Since: 2.14
 */
795
GtkWidget *
796 797
gtk_scale_button_get_popup (GtkScaleButton *button)
{
Matthias Clasen's avatar
Matthias Clasen committed
798
  g_return_val_if_fail (GTK_IS_SCALE_BUTTON (button), NULL);
799 800 801 802

  return button->priv->dock;
}

803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
static void
gtk_scale_button_set_orientation_private (GtkScaleButton *button,
                                          GtkOrientation  orientation)
{
  GtkScaleButtonPrivate *priv = button->priv;

  if (orientation != priv->orientation)
    {
      priv->orientation = orientation;

      gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->box),
                                      orientation);
      gtk_container_child_set (GTK_CONTAINER (priv->box),
                               button->plus_button,
                               "pack-type",
                               orientation == GTK_ORIENTATION_VERTICAL ?
                               GTK_PACK_START : GTK_PACK_END,
                               NULL);
      gtk_container_child_set (GTK_CONTAINER (priv->box),
                               button->minus_button,
                               "pack-type",
                               orientation == GTK_ORIENTATION_VERTICAL ?
                               GTK_PACK_END : GTK_PACK_START,
                               NULL);

      gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->scale),
                                      orientation);

      if (orientation == GTK_ORIENTATION_VERTICAL)
        {
          gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
                                       -1, SCALE_SIZE);
          gtk_range_set_inverted (GTK_RANGE (priv->scale), TRUE);
        }
      else
        {
          gtk_widget_set_size_request (GTK_WIDGET (priv->scale),
                                       SCALE_SIZE, -1);
          gtk_range_set_inverted (GTK_RANGE (priv->scale), FALSE);
        }

      /* FIXME: without this, the popup window appears as a square
       * after changing the orientation
       */
      gtk_window_resize (GTK_WINDOW (priv->dock), 1, 1);

      g_object_notify (G_OBJECT (button), "orientation");
    }
}

853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
/*
 * button callbacks.
 */

static gboolean
gtk_scale_button_scroll (GtkWidget      *widget,
			 GdkEventScroll *event)
{
  GtkScaleButton *button;
  GtkScaleButtonPrivate *priv;
  GtkAdjustment *adj;
  gdouble d;

  button = GTK_SCALE_BUTTON (widget);
  priv = button->priv;
868
  adj = priv->adjustment;
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925

  if (event->type != GDK_SCROLL)
    return FALSE;

  d = gtk_scale_button_get_value (button);
  if (event->direction == GDK_SCROLL_UP)
    {
      d += adj->step_increment;
      if (d > adj->upper)
	d = adj->upper;
    }
  else
    {
      d -= adj->step_increment;
      if (d < adj->lower)
	d = adj->lower;
    }
  gtk_scale_button_set_value (button, d);

  return TRUE;
}

static void
gtk_scale_button_screen_changed (GtkWidget *widget,
				 GdkScreen *previous_screen)
{
  GtkScaleButton *button = (GtkScaleButton *) widget;
  GtkScaleButtonPrivate *priv;
  GdkScreen *screen;
  GValue value = { 0, };

  if (gtk_widget_has_screen (widget) == FALSE)
    return;

  priv = button->priv;

  screen = gtk_widget_get_screen (widget);
  g_value_init (&value, G_TYPE_INT);
  if (gdk_screen_get_setting (screen,
			      "gtk-double-click-time",
			      &value) == FALSE)
    {
      priv->click_timeout = CLICK_TIMEOUT;
      return;
    }

  priv->click_timeout = g_value_get_int (&value);
}

static gboolean
gtk_scale_popup (GtkWidget *widget,
		 GdkEvent  *event,
		 guint32    time)
{
  GtkScaleButton *button;
  GtkScaleButtonPrivate *priv;
  GtkAdjustment *adj;
926
  gint x, y, m, dx, dy, sx, sy, startoff;
927 928 929
  gdouble v;
  GdkDisplay *display;
  GdkScreen *screen;
930
  gboolean is_moved;
931

932
  is_moved = FALSE;
933 934
  button = GTK_SCALE_BUTTON (widget);
  priv = button->priv;
935
  adj = priv->adjustment;
936 937 938 939 940 941 942 943 944 945

  display = gtk_widget_get_display (widget);
  screen = gtk_widget_get_screen (widget);

  /* position roughly */
  gtk_window_set_screen (GTK_WINDOW (priv->dock), screen);

  gdk_window_get_origin (widget->window, &x, &y);
  x += widget->allocation.x;
  y += widget->allocation.y;
946 947 948 949 950 951

  if (priv->orientation == GTK_ORIENTATION_VERTICAL)
    gtk_window_move (GTK_WINDOW (priv->dock), x, y - (SCALE_SIZE / 2));
  else
    gtk_window_move (GTK_WINDOW (priv->dock), x - (SCALE_SIZE / 2), y);

952
  gtk_widget_show_all (priv->dock);
953

954
  gdk_window_get_origin (priv->dock->window, &dx, &dy);
955
  dx += priv->dock->allocation.x;
956
  dy += priv->dock->allocation.y;
957

958
  gdk_window_get_origin (priv->scale->window, &sx, &sy);
959
  sx += priv->scale->allocation.x;
960
  sy += priv->scale->allocation.y;
961

962 963 964 965
  priv->timeout = TRUE;

  /* position (needs widget to be shown already) */
  v = gtk_scale_button_get_value (button) / (adj->upper - adj->lower);
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988

  if (priv->orientation == GTK_ORIENTATION_VERTICAL)
    {
      startoff = sy - dy;

      x += (widget->allocation.width - priv->dock->allocation.width) / 2;
      y -= startoff;
      y -= GTK_RANGE (priv->scale)->min_slider_size / 2;
      m = priv->scale->allocation.height -
          GTK_RANGE (priv->scale)->min_slider_size;
      y -= m * (1.0 - v);
    }
  else
    {
      startoff = sx - dx;

      x -= startoff;
      y += (widget->allocation.height - priv->dock->allocation.height) / 2;
      x -= GTK_RANGE (priv->scale)->min_slider_size / 2;
      m = priv->scale->allocation.width -
          GTK_RANGE (priv->scale)->min_slider_size;
      x -= m * v;
    }
989 990 991 992 993 994 995 996 997 998 999

  /* Make sure the dock stays inside the monitor */
  if (event->type == GDK_BUTTON_PRESS)
    {
      int monitor;
      GdkEventButton *button_event = (GdkEventButton *) event;
      GdkRectangle rect;
      GtkWidget *d;

      d = GTK_WIDGET (priv->dock);
      monitor = gdk_screen_get_monitor_at_point (screen,
1000 1001
						 button_event->x_root,
						 button_event->y_root);
1002 1003
      gdk_screen_get_monitor_geometry (screen, monitor, &rect);

1004 1005 1006 1007 1008
      if (priv->orientation == GTK_ORIENTATION_VERTICAL)
        y += button_event->y;
      else
        x += button_event->x;

1009 1010 1011 1012
      /* Move the dock, but set is_moved so we
       * don't forward the first click later on,
       * as it could make the scale go to the bottom */
      if (y < rect.y) {
1013
	y = rect.y;
1014 1015
	is_moved = TRUE;
      } else if (y + d->allocation.height > rect.height + rect.y) {
1016
	y = rect.y + rect.height - d->allocation.height;
1017 1018
	is_moved = TRUE;
      }
1019

1020
      if (x < rect.x) {
1021
	x = rect.x;
1022 1023
	is_moved = TRUE;
      } else if (x + d->allocation.width > rect.width + rect.x) {
1024
	x = rect.x + rect.width - d->allocation.width;
1025 1026
	is_moved = TRUE;
      }
1027 1028 1029 1030 1031
    }

  gtk_window_move (GTK_WINDOW (priv->dock), x, y);

  if (event->type == GDK_BUTTON_PRESS)
1032
    GTK_WIDGET_CLASS (gtk_scale_button_parent_class)->button_press_event (widget, (GdkEventButton *) event);
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056

  /* grab focus */
  gtk_grab_add (priv->dock);

  if (gdk_pointer_grab (priv->dock->window, TRUE,
			GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
			GDK_POINTER_MOTION_MASK, NULL, NULL, time)
      != GDK_GRAB_SUCCESS)
    {
      gtk_grab_remove (priv->dock);
      gtk_widget_hide (priv->dock);
      return FALSE;
    }

  if (gdk_keyboard_grab (priv->dock->window, TRUE, time) != GDK_GRAB_SUCCESS)
    {
      gdk_display_pointer_ungrab (display, time);
      gtk_grab_remove (priv->dock);
      gtk_widget_hide (priv->dock);
      return FALSE;
    }

  gtk_widget_grab_focus (priv->dock);

1057
  if (event->type == GDK_BUTTON_PRESS && !is_moved)
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
    {
      GdkEventButton *e;
      GdkEventButton *button_event = (GdkEventButton *) event;

      /* forward event to the slider */
      e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
      e->window = priv->scale->window;

      /* position: the X position isn't relevant, halfway will work just fine.
       * The vertical position should be *exactly* in the middle of the slider
       * of the scale; if we don't do that correctly, it'll move from its current
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
       * position, which means a position change on-click, which is bad.
       */
      if (priv->orientation == GTK_ORIENTATION_VERTICAL)
        {
          e->x = priv->scale->allocation.width / 2;
          m = priv->scale->allocation.height -
              GTK_RANGE (priv->scale)->min_slider_size;
          e->y = ((1.0 - v) * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
        }
      else
        {
          e->y = priv->scale->allocation.height / 2;
          m = priv->scale->allocation.width -
              GTK_RANGE (priv->scale)->min_slider_size;
          e->x = (v * m) + GTK_RANGE (priv->scale)->min_slider_size / 2;
        }

1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
      gtk_widget_event (priv->scale, (GdkEvent *) e);
      e->window = button_event->window;
      gdk_event_free ((GdkEvent *) e);
    }

  gtk_widget_grab_focus (priv->scale);

  priv->pop_time = time;

  return TRUE;
}

static gboolean
gtk_scale_button_press (GtkWidget      *widget,
			GdkEventButton *event)
{
  return gtk_scale_popup (widget, (GdkEvent *) event, event->time);
}

static void
1106
gtk_scale_button_popup (GtkWidget *widget)
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
{
  GdkEvent *ev;

  ev = gdk_event_new (GDK_KEY_RELEASE);
  gtk_scale_popup (widget, ev, GDK_CURRENT_TIME);
  gdk_event_free (ev);
}

static gboolean
gtk_scale_button_key_release (GtkWidget   *widget,
			      GdkEventKey *event)
{
  return gtk_bindings_activate_event (GTK_OBJECT (widget), event);
}

/* This is called when the grab is broken for
 * either the dock, or the scale itself */
static void
gtk_scale_button_grab_notify (GtkScaleButton *button,
			      gboolean        was_grabbed)
{
  GdkDisplay *display;
  GtkScaleButtonPrivate *priv;

  if (was_grabbed != FALSE)
    return;

  priv = button->priv;

1136
  if (!gtk_widget_has_grab (priv->dock))
1137 1138
    return;

1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
  if (gtk_widget_is_ancestor (gtk_grab_get_current (), priv->dock))
    return;

  display = gtk_widget_get_display (priv->dock);
  gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
  gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
  gtk_grab_remove (priv->dock);

  /* hide again */
  gtk_widget_hide (priv->dock);
  priv->timeout = FALSE;
}

/*
 * +/- button callbacks.
 */

static gboolean
cb_button_timeout (gpointer user_data)
{
  GtkScaleButton *button;
  GtkScaleButtonPrivate *priv;
  GtkAdjustment *adj;
  gdouble val;
  gboolean res = TRUE;

  button = GTK_SCALE_BUTTON (user_data);
  priv = button->priv;

  if (priv->click_id == 0)
    return FALSE;

1171
  adj = priv->adjustment;
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206

  val = gtk_scale_button_get_value (button);
  val += priv->direction;
  if (val <= adj->lower)
    {
      res = FALSE;
      val = adj->lower;
    }
  else if (val > adj->upper)
    {
      res = FALSE;
      val = adj->upper;
    }
  gtk_scale_button_set_value (button, val);

  if (!res)
    {
      g_source_remove (priv->click_id);
      priv->click_id = 0;
    }

  return res;
}

static gboolean
cb_button_press (GtkWidget      *widget,
		 GdkEventButton *event,
		 gpointer        user_data)
{
  GtkScaleButton *button;
  GtkScaleButtonPrivate *priv;
  GtkAdjustment *adj;

  button = GTK_SCALE_BUTTON (user_data);
  priv = button->priv;
1207
  adj = priv->adjustment;
1208 1209 1210 1211 1212 1213 1214 1215 1216

  if (priv->click_id != 0)
    g_source_remove (priv->click_id);

  if (widget == button->plus_button)
    priv->direction = fabs (adj->page_increment);
  else
    priv->direction = - fabs (adj->page_increment);

1217 1218 1219
  priv->click_id = gdk_threads_add_timeout (priv->click_timeout,
                                            cb_button_timeout,
                                            button);
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
  cb_button_timeout (button);

  return TRUE;
}

static gboolean
cb_button_release (GtkWidget      *widget,
		   GdkEventButton *event,
		   gpointer        user_data)
{
  GtkScaleButton *button;
  GtkScaleButtonPrivate *priv;

  button = GTK_SCALE_BUTTON (user_data);
  priv = button->priv;

  if (priv->click_id != 0)
    {
      g_source_remove (priv->click_id);
      priv->click_id = 0;
    }

  return TRUE;
}

static void
cb_dock_grab_notify (GtkWidget *widget,
		     gboolean   was_grabbed,
		     gpointer   user_data)
{
  GtkScaleButton *button = (GtkScaleButton *) user_data;

  gtk_scale_button_grab_notify (button, was_grabbed);
}

static gboolean
cb_dock_grab_broken_event (GtkWidget *widget,
			   gboolean   was_grabbed,
			   gpointer   user_data)
{
  GtkScaleButton *button = (GtkScaleButton *) user_data;

  gtk_scale_button_grab_notify (button, FALSE);

  return FALSE;
}

/*
 * Scale callbacks.
 */

static void
gtk_scale_button_release_grab (GtkScaleButton *button,
			       GdkEventButton *event)
{
  GdkEventButton *e;
  GdkDisplay *display;
  GtkScaleButtonPrivate *priv;

  priv = button->priv;

  /* ungrab focus */
  display = gtk_widget_get_display (GTK_WIDGET (button));
  gdk_display_keyboard_ungrab (display, event->time);
  gdk_display_pointer_ungrab (display, event->time);
  gtk_grab_remove (priv->dock);

  /* hide again */
  gtk_widget_hide (priv->dock);
  priv->timeout = FALSE;

  e = (GdkEventButton *) gdk_event_copy ((GdkEvent *) event);
  e->window = GTK_WIDGET (button)->window;
  e->type = GDK_BUTTON_RELEASE;
  gtk_widget_event (GTK_WIDGET (button), (GdkEvent *) e);
  e->window = event->window;
  gdk_event_free ((GdkEvent *) e);
}

static gboolean
cb_dock_button_press (GtkWidget      *widget,
		      GdkEventButton *event,
		      gpointer        user_data)
{
  GtkScaleButton *button = GTK_SCALE_BUTTON (user_data);

  if (event->type == GDK_BUTTON_PRESS)
    {
      gtk_scale_button_release_grab (button, event);
      return TRUE;
    }

  return FALSE;
}

static void
1316
gtk_scale_button_popdown (GtkWidget *widget)
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
{
  GtkScaleButton *button;
  GtkScaleButtonPrivate *priv;
  GdkDisplay *display;

  button = GTK_SCALE_BUTTON (widget);
  priv = button->priv;

  /* ungrab focus */
  display = gtk_widget_get_display (widget);
  gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
  gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
  gtk_grab_remove (priv->dock);

  /* hide again */
  gtk_widget_hide (priv->dock);
  priv->timeout = FALSE;
}

static gboolean
cb_dock_key_release (GtkWidget   *widget,
		     GdkEventKey *event,
		     gpointer     user_data)
{
  if (event->keyval == GDK_Escape)
    {
1343
      gtk_scale_button_popdown (GTK_WIDGET (user_data));
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
      return TRUE;
    }

  if (!gtk_bindings_activate_event (GTK_OBJECT (widget), event))
    {
      /* The popup hasn't managed the event, pass onto the button */
      gtk_bindings_activate_event (GTK_OBJECT (user_data), event);
    }

  return TRUE;
}

static void
cb_scale_grab_notify (GtkWidget *widget,
		      gboolean   was_grabbed,
		      gpointer   user_data)
{
  GtkScaleButton *button = (GtkScaleButton *) user_data;

  gtk_scale_button_grab_notify (button, was_grabbed);
}

/*
 * Scale stuff.
 */

1370 1371 1372
#define GTK_TYPE_SCALE_BUTTON_SCALE    (_gtk_scale_button_scale_get_type ())
#define GTK_SCALE_BUTTON_SCALE(obj)    (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_SCALE_BUTTON_SCALE, GtkScaleButtonScale))
#define GTK_IS_SCALE_BUTTON_SCALE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_SCALE_BUTTON_SCALE))
1373

1374 1375 1376
typedef struct _GtkScaleButtonScale
{
  GtkScale parent_instance;
1377
  GtkScaleButton *button;
1378
} GtkScaleButtonScale;
1379

1380 1381 1382 1383
typedef struct _GtkScaleButtonScaleClass
{
  GtkScaleClass parent_class;
} GtkScaleButtonScaleClass;
1384 1385

static gboolean	gtk_scale_button_scale_press   (GtkWidget      *widget,
1386 1387 1388
                                                GdkEventButton *event);
static gboolean gtk_scale_button_scale_release (GtkWidget      *widget,
                                                GdkEventButton *event);
1389

1390
G_DEFINE_TYPE (GtkScaleButtonScale, _gtk_scale_button_scale, GTK_TYPE_SCALE)
1391 1392

static void
1393
_gtk_scale_button_scale_class_init (GtkScaleButtonScaleClass *klass)
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
{
  GtkWidgetClass *gtkwidget_class = GTK_WIDGET_CLASS (klass);
  GtkRangeClass *gtkrange_class = GTK_RANGE_CLASS (klass);

  gtkwidget_class->button_press_event = gtk_scale_button_scale_press;
  gtkwidget_class->button_release_event = gtk_scale_button_scale_release;
  gtkrange_class->value_changed = gtk_scale_button_scale_value_changed;
}

static void
1404
_gtk_scale_button_scale_init (GtkScaleButtonScale *scale)
1405 1406 1407 1408
{
}

static GtkWidget *
1409
gtk_scale_button_scale_new (GtkScaleButton *button)
1410
{
1411
  GtkScaleButtonPrivate *priv = button->priv;
1412
  GtkScaleButtonScale *scale;
1413

1414 1415 1416 1417 1418
  scale = g_object_new (GTK_TYPE_SCALE_BUTTON_SCALE,
                        "orientation", priv->orientation,
                        "adjustment",  priv->adjustment,
                        "draw-value",  FALSE,
                        NULL);
1419

1420
  scale->button = button;
1421

1422 1423
  g_signal_connect (scale, "grab-notify",
                    G_CALLBACK (cb_scale_grab_notify), button);
1424

1425 1426 1427 1428
  if (priv->orientation == GTK_ORIENTATION_VERTICAL)
    {
      gtk_widget_set_size_request (GTK_WIDGET (scale), -1, SCALE_SIZE);
      gtk_range_set_inverted (GTK_RANGE (scale), TRUE);
1429
    }
1430
  else
1431
    {
1432 1433
      gtk_widget_set_size_request (GTK_WIDGET (scale), SCALE_SIZE, -1);
      gtk_range_set_inverted (GTK_RANGE (scale), FALSE);
1434 1435
    }

1436
  return GTK_WIDGET (scale);
1437 1438 1439 1440 1441 1442
}

static gboolean
gtk_scale_button_scale_press (GtkWidget      *widget,
			      GdkEventButton *event)
{
1443
  GtkScaleButtonPrivate *priv = GTK_SCALE_BUTTON_SCALE (widget)->button->priv;
1444 1445

  /* the scale will grab input; if we have input grabbed, all goes
1446 1447
   * horribly wrong, so let's not do that.
   */
1448 1449
  gtk_grab_remove (priv->dock);

1450
  return GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_press_event (widget, event);
1451 1452 1453 1454 1455 1456
}

static gboolean
gtk_scale_button_scale_release (GtkWidget      *widget,
				GdkEventButton *event)
{
1457
  GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (widget)->button;
1458 1459
  gboolean res;

1460
  if (button->priv->timeout)
1461 1462
    {
      /* if we did a quick click, leave the window open; else, hide it */
1463
      if (event->time > button->priv->pop_time + button->priv->click_timeout)
1464
        {
1465

1466
	  gtk_scale_button_release_grab (button, event);
1467
	  GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1468

1469 1470
	  return TRUE;
	}
1471

1472
      button->priv->timeout = FALSE;
1473 1474
    }

1475
  res = GTK_WIDGET_CLASS (_gtk_scale_button_scale_parent_class)->button_release_event (widget, event);
1476 1477 1478 1479 1480

  /* the scale will release input; right after that, we *have to* grab
   * it back so we can catch out-of-scale clicks and hide the popup,
   * so I basically want a g_signal_connect_after_always(), but I can't
   * find that, so we do this complex 'first-call-parent-then-do-actual-
1481 1482 1483
   * action' thingy...
   */
  gtk_grab_add (button->priv->dock);
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499

  return res;
}

static void
gtk_scale_button_update_icon (GtkScaleButton *button)
{
  GtkScaleButtonPrivate *priv;
  GtkRange *range;
  GtkAdjustment *adj;
  gdouble value;
  const gchar *name;
  guint num_icons;

  priv = button->priv;

1500
  if (!priv->icon_list || priv->icon_list[0] == '\0')
1501 1502 1503 1504 1505 1506
    {
      gtk_image_set_from_stock (GTK_IMAGE (priv->image),
				GTK_STOCK_MISSING_IMAGE,
				priv->size);
      return;
    }
1507

1508
  num_icons = g_strv_length (priv->icon_list);
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519

  /* The 1-icon special case */
  if (num_icons == 1)
    {
      gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
				    priv->icon_list[0],
				    priv->size);
      return;
    }

  range = GTK_RANGE (priv->scale);
1520
  adj = priv->adjustment;
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
  value = gtk_scale_button_get_value (button);

  /* The 2-icons special case */
  if (num_icons == 2)
    {
      gdouble limit;
      limit = (adj->upper - adj->lower) / 2 + adj->lower;
      if (value < limit)
	name = priv->icon_list[0];
      else
	name = priv->icon_list[1];

      gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
				    name,
				    priv->size);
      return;
    }

  /* With 3 or more icons */
  if (value == adj->lower)
    {
      name = priv->icon_list[0];
    }
  else if (value == adj->upper)
    {
      name = priv->icon_list[1];
    }
  else
    {
      gdouble step;
      guint i;

      step = (adj->upper - adj->lower) / (num_icons - 2);
1554
      i = (guint) ((value - adj->lower) / step) + 2;
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
      g_assert (i < num_icons);
      name = priv->icon_list[i];
    }

  gtk_image_set_from_icon_name (GTK_IMAGE (priv->image),
				name,
				priv->size);
}

static void
gtk_scale_button_scale_value_changed (GtkRange *range)
{
1567
  GtkScaleButton *button = GTK_SCALE_BUTTON_SCALE (range)->button;
1568 1569 1570 1571 1572 1573 1574 1575 1576
  gdouble value;

  value = gtk_range_get_value (range);

  gtk_scale_button_update_icon (button);

  g_signal_emit (button, signals[VALUE_CHANGED], 0, value);
  g_object_notify (G_OBJECT (button), "value");
}
1577

1578 1579
#define __GTK_SCALE_BUTTON_C__
#include "gtkaliasdef.c"