gtkbutton.c 80.4 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * 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
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17 18

/*
19
 * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
20 21 22 23 24
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

25 26
/**
 * SECTION:gtkbutton
27
 * @Short_description: A widget that emits a signal when clicked on
28 29
 * @Title: GtkButton
 *
30
 * The #GtkButton widget is generally used to trigger a callback function that is
31 32 33
 * called when the button is pressed.  The various signals and how to use them
 * are outlined below.
 *
34 35
 * The #GtkButton widget can hold any valid child widget.  That is, it can hold
 * almost any other standard #GtkWidget.  The most commonly used child is the
36 37 38
 * #GtkLabel.
 */

39
#include "config.h"
40 41 42 43

#include "gtkbutton.h"
#include "gtkbuttonprivate.h"

44
#include <string.h>
Matthias Clasen's avatar
Matthias Clasen committed
45
#include "deprecated/gtkalignment.h"
Elliot Lee's avatar
Elliot Lee committed
46 47
#include "gtklabel.h"
#include "gtkmain.h"
48
#include "gtkmarshalers.h"
49
#include "gtkimage.h"
50
#include "gtkbox.h"
51 52
#include "deprecated/gtkstock.h"
#include "deprecated/gtkactivatable.h"
53
#include "gtksizerequest.h"
54
#include "gtktypebuiltins.h"
55
#include "gtkwidgetprivate.h"
56
#include "gtkprivate.h"
57
#include "gtkintl.h"
58
#include "a11y/gtkbuttonaccessible.h"
59
#include "gtkapplicationprivate.h"
60
#include "gtkactionhelper.h"
Elliot Lee's avatar
Elliot Lee committed
61

62
static const GtkBorder default_default_border = { 0, 0, 0, 0 };
63
static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
Elliot Lee's avatar
Elliot Lee committed
64

Matthias Clasen's avatar
Matthias Clasen committed
65
/* Time out before giving up on getting a key release when animating
66 67 68
 * the close button.
 */
#define ACTIVATE_TIMEOUT 250
Elliot Lee's avatar
Elliot Lee committed
69

70

Elliot Lee's avatar
Elliot Lee committed
71 72 73 74 75 76
enum {
  PRESSED,
  RELEASED,
  CLICKED,
  ENTER,
  LEAVE,
77
  ACTIVATE,
Elliot Lee's avatar
Elliot Lee committed
78 79
  LAST_SIGNAL
};
80

81
enum {
82 83
  PROP_0,
  PROP_LABEL,
84
  PROP_IMAGE,
85 86
  PROP_RELIEF,
  PROP_USE_UNDERLINE,
87
  PROP_USE_STOCK,
88 89
  PROP_FOCUS_ON_CLICK,
  PROP_XALIGN,
90
  PROP_YALIGN,
91
  PROP_IMAGE_POSITION,
92 93 94
  PROP_ALWAYS_SHOW_IMAGE,

  /* actionable properties */
95 96
  PROP_ACTION_NAME,
  PROP_ACTION_TARGET,
97 98 99

  /* activatable properties */
  PROP_ACTIVATABLE_RELATED_ACTION,
100 101
  PROP_ACTIVATABLE_USE_ACTION_APPEARANCE,
  LAST_PROP = PROP_ACTION_NAME
102 103
};

104

105
static void gtk_button_destroy        (GtkWidget          *widget);
106
static void gtk_button_dispose        (GObject            *object);
107 108 109 110 111 112 113 114 115 116
static void gtk_button_set_property   (GObject            *object,
                                       guint               prop_id,
                                       const GValue       *value,
                                       GParamSpec         *pspec);
static void gtk_button_get_property   (GObject            *object,
                                       guint               prop_id,
                                       GValue             *value,
                                       GParamSpec         *pspec);
static void gtk_button_screen_changed (GtkWidget          *widget,
				       GdkScreen          *previous_screen);
117 118 119 120
static void gtk_button_realize (GtkWidget * widget);
static void gtk_button_unrealize (GtkWidget * widget);
static void gtk_button_map (GtkWidget * widget);
static void gtk_button_unmap (GtkWidget * widget);
121
static void gtk_button_style_updated (GtkWidget * widget);
122 123
static void gtk_button_size_allocate (GtkWidget * widget,
				      GtkAllocation * allocation);
124
static gint gtk_button_draw (GtkWidget * widget, cairo_t *cr);
125 126 127 128 129 130 131 132 133 134
static gint gtk_button_grab_broken (GtkWidget * widget,
				    GdkEventGrabBroken * event);
static gint gtk_button_key_release (GtkWidget * widget, GdkEventKey * event);
static gint gtk_button_enter_notify (GtkWidget * widget,
				     GdkEventCrossing * event);
static gint gtk_button_leave_notify (GtkWidget * widget,
				     GdkEventCrossing * event);
static void gtk_real_button_pressed (GtkButton * button);
static void gtk_real_button_released (GtkButton * button);
static void gtk_real_button_clicked (GtkButton * button);
135 136
static void gtk_real_button_activate  (GtkButton          *button);
static void gtk_button_update_state   (GtkButton          *button);
137
static void gtk_button_enter_leave    (GtkButton          *button);
138 139 140 141 142
static void gtk_button_add            (GtkContainer       *container,
			               GtkWidget          *widget);
static GType gtk_button_child_type    (GtkContainer       *container);
static void gtk_button_finish_activate (GtkButton         *button,
					gboolean           do_it);
Elliot Lee's avatar
Elliot Lee committed
143

144
static void gtk_button_constructed (GObject *object);
Matthias Clasen's avatar
Matthias Clasen committed
145
static void gtk_button_construct_child (GtkButton             *button);
146 147 148 149
static void gtk_button_state_changed   (GtkWidget             *widget,
					GtkStateType           previous_state);
static void gtk_button_grab_notify     (GtkWidget             *widget,
					gboolean               was_grabbed);
150 151
static void gtk_button_do_release      (GtkButton             *button,
                                        gboolean               emit_clicked);
152

153
static void gtk_button_actionable_iface_init     (GtkActionableInterface *iface);
154
static void gtk_button_activatable_interface_init(GtkActivatableIface  *iface);
155 156 157 158 159 160 161 162 163
static void gtk_button_update                    (GtkActivatable       *activatable,
				                  GtkAction            *action,
			                          const gchar          *property_name);
static void gtk_button_sync_action_properties    (GtkActivatable       *activatable,
                                                  GtkAction            *action);
static void gtk_button_set_related_action        (GtkButton            *button,
					          GtkAction            *action);
static void gtk_button_set_use_action_appearance (GtkButton            *button,
						  gboolean              use_appearance);
164

165 166 167 168 169 170 171 172 173 174 175 176 177 178
static void gtk_button_get_preferred_width             (GtkWidget           *widget,
                                                        gint                *minimum_size,
                                                        gint                *natural_size);
static void gtk_button_get_preferred_height            (GtkWidget           *widget,
                                                        gint                *minimum_size,
                                                        gint                *natural_size);
static void gtk_button_get_preferred_width_for_height  (GtkWidget           *widget,
                                                        gint                 for_size,
                                                        gint                *minimum_size,
                                                        gint                *natural_size);
static void gtk_button_get_preferred_height_for_width  (GtkWidget           *widget,
                                                        gint                 for_size,
                                                        gint                *minimum_size,
                                                        gint                *natural_size);
179 180 181 182 183 184
static void gtk_button_get_preferred_height_and_baseline_for_width (GtkWidget *widget,
								    gint       width,
								    gint      *minimum_size,
								    gint      *natural_size,
								    gint      *minimum_baseline,
								    gint      *natural_baseline);
185
  
186
static GParamSpec *props[LAST_PROP] = { NULL, };
187
static guint button_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
188

189
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
190
G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
191
                         G_ADD_PRIVATE (GtkButton)
192
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIONABLE, gtk_button_actionable_iface_init)
193
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
194
						gtk_button_activatable_interface_init))
195
G_GNUC_END_IGNORE_DEPRECATIONS;
Elliot Lee's avatar
Elliot Lee committed
196 197 198 199

static void
gtk_button_class_init (GtkButtonClass *klass)
{
Manish Singh's avatar
Manish Singh committed
200
  GObjectClass *gobject_class;
Elliot Lee's avatar
Elliot Lee committed
201 202 203
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

Manish Singh's avatar
Manish Singh committed
204
  gobject_class = G_OBJECT_CLASS (klass);
Elliot Lee's avatar
Elliot Lee committed
205 206
  widget_class = (GtkWidgetClass*) klass;
  container_class = (GtkContainerClass*) klass;
207
  
208
  gobject_class->constructed  = gtk_button_constructed;
209
  gobject_class->dispose      = gtk_button_dispose;
Manish Singh's avatar
Manish Singh committed
210 211
  gobject_class->set_property = gtk_button_set_property;
  gobject_class->get_property = gtk_button_get_property;
212

213
  widget_class->get_preferred_width = gtk_button_get_preferred_width;
214
  widget_class->get_preferred_height = gtk_button_get_preferred_height;
215 216
  widget_class->get_preferred_width_for_height = gtk_button_get_preferred_width_for_height;
  widget_class->get_preferred_height_for_width = gtk_button_get_preferred_height_for_width;
217
  widget_class->get_preferred_height_and_baseline_for_width = gtk_button_get_preferred_height_and_baseline_for_width;
218
  widget_class->destroy = gtk_button_destroy;
Matthias Clasen's avatar
Matthias Clasen committed
219
  widget_class->screen_changed = gtk_button_screen_changed;
220
  widget_class->realize = gtk_button_realize;
221
  widget_class->unrealize = gtk_button_unrealize;
222 223
  widget_class->map = gtk_button_map;
  widget_class->unmap = gtk_button_unmap;
224
  widget_class->style_updated = gtk_button_style_updated;
225
  widget_class->size_allocate = gtk_button_size_allocate;
226
  widget_class->draw = gtk_button_draw;
Matthias Clasen's avatar
Matthias Clasen committed
227
  widget_class->grab_broken_event = gtk_button_grab_broken;
228
  widget_class->key_release_event = gtk_button_key_release;
229 230
  widget_class->enter_notify_event = gtk_button_enter_notify;
  widget_class->leave_notify_event = gtk_button_leave_notify;
231 232
  widget_class->state_changed = gtk_button_state_changed;
  widget_class->grab_notify = gtk_button_grab_notify;
233 234

  container_class->child_type = gtk_button_child_type;
235
  container_class->add = gtk_button_add;
236
  gtk_container_class_handle_border_width (container_class);
237 238 239

  klass->pressed = gtk_real_button_pressed;
  klass->released = gtk_real_button_released;
240
  klass->clicked = NULL;
241 242
  klass->enter = gtk_button_enter_leave;
  klass->leave = gtk_button_enter_leave;
243
  klass->activate = gtk_real_button_activate;
244

245 246 247 248 249
  props[PROP_LABEL] =
    g_param_spec_string ("label",
                         P_("Label"),
                         P_("Text of the label widget inside the button, if the button contains a label widget"),
                         NULL,
250
                         GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_EXPLICIT_NOTIFY);
251
  
252 253 254 255 256
  props[PROP_USE_UNDERLINE] =
    g_param_spec_boolean ("use-underline",
                          P_("Use underline"),
                          P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
                          FALSE,
257
                          GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_EXPLICIT_NOTIFY);
258
  
259 260 261 262 263
  /**
   * GtkButton:use-stock:
   *
   * Deprecated: 3.10
   */
264 265 266 267 268
  props[PROP_USE_STOCK] =
    g_param_spec_boolean ("use-stock",
                          P_("Use stock"),
                          P_("If set, the label is used to pick a stock item instead of being displayed"),
                          FALSE,
269
                          GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_EXPLICIT_NOTIFY|G_PARAM_DEPRECATED);
270
  
271 272 273 274 275
  props[PROP_FOCUS_ON_CLICK] =
    g_param_spec_boolean ("focus-on-click",
                          P_("Focus on click"),
                          P_("Whether the button grabs focus when it is clicked with the mouse"),
                          TRUE,
276
                          GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
277
  
278 279 280 281 282 283
  props[PROP_RELIEF] =
    g_param_spec_enum ("relief",
                       P_("Border relief"),
                       P_("The border relief style"),
                       GTK_TYPE_RELIEF_STYLE,
                       GTK_RELIEF_NORMAL,
284
                       GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
285 286 287 288 289
  
  /**
   * GtkButton:xalign:
   *
   * If the child of the button is a #GtkMisc or #GtkAlignment, this property 
290
   * can be used to control its horizontal alignment. 0.0 is left aligned, 
291
   * 1.0 is right aligned.
292
   *
293
   * Since: 2.4
294 295 296
   *
   * Deprecated: 3.14: Access the child widget directly if you need to control
   * its alignment.
297
   */
298 299 300 301 302
  props[PROP_XALIGN] =
    g_param_spec_float ("xalign",
                        P_("Horizontal alignment for child"),
                        P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
                        0.0, 1.0, 0.5,
303
                        GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY|G_PARAM_DEPRECATED);
304 305 306 307 308

  /**
   * GtkButton:yalign:
   *
   * If the child of the button is a #GtkMisc or #GtkAlignment, this property 
309
   * can be used to control its vertical alignment. 0.0 is top aligned, 
310
   * 1.0 is bottom aligned.
311
   *
312
   * Since: 2.4
313 314 315
   *
   * Deprecated: 3.14: Access the child widget directly if you need to control
   * its alignment.
316
   */
317 318 319 320 321
  props[PROP_YALIGN] =
    g_param_spec_float ("yalign",
                        P_("Vertical alignment for child"),
                        P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
                        0.0, 1.0, 0.5,
322
                        GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY|G_PARAM_DEPRECATED);
Elliot Lee's avatar
Elliot Lee committed
323

324
  /**
325
   * GtkButton:image:
326
   *
327
   * The child widget to appear next to the button text.
328
   *
329 330
   * Since: 2.6
   */
331 332 333 334 335
  props[PROP_IMAGE] =
    g_param_spec_object ("image",
                         P_("Image widget"),
                         P_("Child widget to appear next to the button text"),
                         GTK_TYPE_WIDGET,
336
                         GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
337

338 339 340
  /**
   * GtkButton:image-position:
   *
341
   * The position of the image relative to the text inside the button.
342
   *
343 344
   * Since: 2.10
   */
345 346 347 348 349 350
  props[PROP_IMAGE_POSITION] =
    g_param_spec_enum ("image-position",
                       P_("Image position"),
                       P_("The position of the image relative to the text"),
                       GTK_TYPE_POSITION_TYPE,
                       GTK_POS_LEFT,
351
                       GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
352

353 354 355
  /**
   * GtkButton:always-show-image:
   *
356 357
   * If %TRUE, the button will ignore the #GtkSettings:gtk-button-images
   * setting and always show the image, if available.
358 359 360 361 362 363
   *
   * Use this property if the button would be useless or hard to use
   * without the image.
   *
   * Since: 3.6
   */
364 365 366 367 368
  props[PROP_ALWAYS_SHOW_IMAGE] =
     g_param_spec_boolean ("always-show-image",
                           P_("Always show image"),
                           P_("Whether the image will always be shown"),
                           FALSE,
369
                           GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT|G_PARAM_EXPLICIT_NOTIFY);
370 371

  g_object_class_install_properties (gobject_class, LAST_PROP, props);
372

373 374 375
  g_object_class_override_property (gobject_class, PROP_ACTION_NAME, "action-name");
  g_object_class_override_property (gobject_class, PROP_ACTION_TARGET, "action-target");

376
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
377 378
  g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
  g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
379
  G_GNUC_END_IGNORE_DEPRECATIONS;
380

381 382 383 384 385
  /**
   * GtkButton::pressed:
   * @button: the object that received the signal
   *
   * Emitted when the button is pressed.
386
   *
387
   * Deprecated: 2.8: Use the #GtkWidget::button-press-event signal.
388
   */ 
Elliot Lee's avatar
Elliot Lee committed
389
  button_signals[PRESSED] =
390
    g_signal_new (I_("pressed"),
391
		  G_OBJECT_CLASS_TYPE (gobject_class),
Manish Singh's avatar
Manish Singh committed
392 393 394 395 396
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkButtonClass, pressed),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
397 398 399 400 401 402

  /**
   * GtkButton::released:
   * @button: the object that received the signal
   *
   * Emitted when the button is released.
403
   *
404
   * Deprecated: 2.8: Use the #GtkWidget::button-release-event signal.
405
   */ 
Elliot Lee's avatar
Elliot Lee committed
406
  button_signals[RELEASED] =
407
    g_signal_new (I_("released"),
408
		  G_OBJECT_CLASS_TYPE (gobject_class),
Manish Singh's avatar
Manish Singh committed
409 410 411 412 413
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkButtonClass, released),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
414 415 416 417 418 419 420

  /**
   * GtkButton::clicked:
   * @button: the object that received the signal
   *
   * Emitted when the button has been activated (pressed and released).
   */ 
Elliot Lee's avatar
Elliot Lee committed
421
  button_signals[CLICKED] =
422
    g_signal_new (I_("clicked"),
423
		  G_OBJECT_CLASS_TYPE (gobject_class),
Manish Singh's avatar
Manish Singh committed
424 425 426 427 428
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkButtonClass, clicked),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
429 430 431 432 433 434

  /**
   * GtkButton::enter:
   * @button: the object that received the signal
   *
   * Emitted when the pointer enters the button.
435
   *
436
   * Deprecated: 2.8: Use the #GtkWidget::enter-notify-event signal.
437
   */ 
Elliot Lee's avatar
Elliot Lee committed
438
  button_signals[ENTER] =
439
    g_signal_new (I_("enter"),
440
		  G_OBJECT_CLASS_TYPE (gobject_class),
Manish Singh's avatar
Manish Singh committed
441 442 443 444 445
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkButtonClass, enter),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
446 447 448 449 450 451

  /**
   * GtkButton::leave:
   * @button: the object that received the signal
   *
   * Emitted when the pointer leaves the button.
452
   *
453
   * Deprecated: 2.8: Use the #GtkWidget::leave-notify-event signal.
454
   */ 
Elliot Lee's avatar
Elliot Lee committed
455
  button_signals[LEAVE] =
456
    g_signal_new (I_("leave"),
457
		  G_OBJECT_CLASS_TYPE (gobject_class),
Manish Singh's avatar
Manish Singh committed
458 459 460 461 462
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkButtonClass, leave),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
463 464 465

  /**
   * GtkButton::activate:
Matthias Clasen's avatar
Matthias Clasen committed
466
   * @widget: the object which received the signal.
467
   *
468
   * The ::activate signal on GtkButton is an action signal and
469 470
   * emitting it causes the button to animate press then release. 
   * Applications should never connect to this signal, but use the
471
   * #GtkButton::clicked signal.
472
   */
473
  button_signals[ACTIVATE] =
474
    g_signal_new (I_("activate"),
475
		  G_OBJECT_CLASS_TYPE (gobject_class),
Manish Singh's avatar
Manish Singh committed
476 477 478 479 480
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkButtonClass, activate),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
481
  widget_class->activate_signal = button_signals[ACTIVATE];
482

483 484 485 486 487 488
  /**
   * GtkButton:default-border:
   *
   * The "default-border" style property defines the extra space to add
   * around a button that can become the default widget of its window.
   * For more information about default widgets, see gtk_widget_grab_default().
489 490
   *
   * Deprecated: 3.14: use CSS margins and padding instead.
491 492
   */

493
  gtk_widget_class_install_style_property (widget_class,
Matthias Clasen's avatar
x  
Matthias Clasen committed
494
					   g_param_spec_boxed ("default-border",
495
							       P_("Default Spacing"),
496
							       P_("Extra space to add for GTK_CAN_DEFAULT buttons"),
497
							       GTK_TYPE_BORDER,
498
							       GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
499

500 501 502 503 504 505 506
  /**
   * GtkButton:default-outside-border:
   *
   * The "default-outside-border" style property defines the extra outside
   * space to add around a button that can become the default widget of its
   * window. Extra outside space is always drawn outside the button border.
   * For more information about default widgets, see gtk_widget_grab_default().
507 508
   *
   * Deprecated: 3.14: use CSS margins and padding instead.
509
   */
510
  gtk_widget_class_install_style_property (widget_class,
Matthias Clasen's avatar
x  
Matthias Clasen committed
511
					   g_param_spec_boxed ("default-outside-border",
512
							       P_("Default Outside Spacing"),
513
							       P_("Extra space to add for GTK_CAN_DEFAULT buttons that is always drawn outside the border"),
514
							       GTK_TYPE_BORDER,
515
							       GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
516
  gtk_widget_class_install_style_property (widget_class,
Matthias Clasen's avatar
x  
Matthias Clasen committed
517
					   g_param_spec_int ("child-displacement-x",
518 519
							     P_("Child X Displacement"),
							     P_("How far in the x direction to move the child when the button is depressed"),
520 521 522
							     G_MININT,
							     G_MAXINT,
							     0,
523
							     GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
524
  gtk_widget_class_install_style_property (widget_class,
Matthias Clasen's avatar
x  
Matthias Clasen committed
525
					   g_param_spec_int ("child-displacement-y",
526 527
							     P_("Child Y Displacement"),
							     P_("How far in the y direction to move the child when the button is depressed"),
528 529 530
							     G_MININT,
							     G_MAXINT,
							     0,
531
							     GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
532

533 534 535
  /**
   * GtkButton:displace-focus:
   *
536 537
   * Whether the child_displacement_x/child_displacement_y properties 
   * should also affect the focus rectangle.
538 539 540 541 542 543 544
   *
   * Since: 2.6
   */
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_boolean ("displace-focus",
								 P_("Displace focus"),
								 P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
Johan Dahlin's avatar
Johan Dahlin committed
545
								 FALSE,
546
								 GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
547

548
  /**
549
   * GtkButton:inner-border:
550
   *
551
   * Sets the border between the button edges and child.
552 553
   *
   * Since: 2.10
554 555 556
   *
   * Deprecated: 3.4: Use the standard border and padding CSS properties;
   *   the value of this style property is ignored.
557 558
   */
  gtk_widget_class_install_style_property (widget_class,
559 560 561 562
					   g_param_spec_boxed ("inner-border",
                                                               P_("Inner Border"),
                                                               P_("Border between button edges and child."),
                                                               GTK_TYPE_BORDER,
563
                                                               GTK_PARAM_READABLE | G_PARAM_DEPRECATED));
564

565 566
  /**
   * GtkButton::image-spacing:
567
   *
568
   * Spacing in pixels between the image and label.
569
   *
570 571 572 573 574 575 576 577 578 579 580
   * Since: 2.10
   */
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("image-spacing",
							     P_("Image spacing"),
							     P_("Spacing in pixels between the image and label"),
							     0,
							     G_MAXINT,
							     2,
							     GTK_PARAM_READABLE));

581
  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_BUTTON_ACCESSIBLE);
Elliot Lee's avatar
Elliot Lee committed
582 583
}

584
static void
585 586 587 588 589
multipress_pressed_cb (GtkGestureMultiPress *gesture,
                       guint                 n_press,
                       gdouble               x,
                       gdouble               y,
                       GtkWidget            *widget)
590 591 592 593 594 595
{
  GtkButton *button = GTK_BUTTON (widget);
  GtkButtonPrivate *priv = button->priv;

  if (priv->focus_on_click && !gtk_widget_has_focus (widget))
    gtk_widget_grab_focus (widget);
596

597
  priv->in_button = TRUE;
598
  g_signal_emit (button, button_signals[PRESSED], 0);
599
  gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
600 601 602
}

static void
603 604 605 606 607
multipress_released_cb (GtkGestureMultiPress *gesture,
                        guint                 n_press,
                        gdouble               x,
                        gdouble               y,
                        GtkWidget            *widget)
608 609
{
  GtkButton *button = GTK_BUTTON (widget);
610 611
  GtkButtonPrivate *priv = button->priv;
  GdkEventSequence *sequence;
612 613

  g_signal_emit (button, button_signals[RELEASED], 0);
614 615

  sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
616 617

  if (sequence)
618
    {
619 620
      priv->in_button = FALSE;
      gtk_button_update_state (button);
621
    }
622 623
}

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
static void
multipress_gesture_update_cb (GtkGesture       *gesture,
                              GdkEventSequence *sequence,
                              GtkButton        *button)
{
  GtkButtonPrivate *priv = button->priv;
  GtkAllocation allocation;
  gboolean in_button;
  gdouble x, y;

  if (sequence != gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture)))
    return;

  gtk_widget_get_allocation (GTK_WIDGET (button), &allocation);
  gtk_gesture_get_point (gesture, sequence, &x, &y);

  in_button = (x >= 0 && y >= 0 && x < allocation.width && y < allocation.height);

  if (priv->in_button != in_button)
    {
      priv->in_button = in_button;
      gtk_button_update_state (button);
    }
}

649 650 651 652 653 654 655 656
static void
multipress_gesture_cancel_cb (GtkGesture       *gesture,
                              GdkEventSequence *sequence,
                              GtkButton        *button)
{
  gtk_button_do_release (button, FALSE);
}

Elliot Lee's avatar
Elliot Lee committed
657 658 659
static void
gtk_button_init (GtkButton *button)
{
660
  GtkButtonPrivate *priv;
661
  GtkStyleContext *context;
662

663
  button->priv = gtk_button_get_instance_private (button);
664
  priv = button->priv;
665

666
  gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
667
  gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
668
  gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
Elliot Lee's avatar
Elliot Lee committed
669

670 671 672 673 674 675 676 677
  priv->label_text = NULL;

  priv->constructed = FALSE;
  priv->in_button = FALSE;
  priv->button_down = FALSE;
  priv->use_stock = FALSE;
  priv->use_underline = FALSE;
  priv->focus_on_click = TRUE;
678 679 680

  priv->xalign = 0.5;
  priv->yalign = 0.5;
681
  priv->align_set = 0;
682
  priv->image_is_stock = TRUE;
683
  priv->image_position = GTK_POS_LEFT;
684
  priv->use_action_appearance = TRUE;
685 686 687

  context = gtk_widget_get_style_context (GTK_WIDGET (button));
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
688

689
  priv->gesture = gtk_gesture_multi_press_new (GTK_WIDGET (button));
690
  gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (priv->gesture), FALSE);
691
  gtk_gesture_single_set_exclusive (GTK_GESTURE_SINGLE (priv->gesture), TRUE);
692
  gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (priv->gesture), GDK_BUTTON_PRIMARY);
693 694
  g_signal_connect (priv->gesture, "pressed", G_CALLBACK (multipress_pressed_cb), button);
  g_signal_connect (priv->gesture, "released", G_CALLBACK (multipress_released_cb), button);
695
  g_signal_connect (priv->gesture, "update", G_CALLBACK (multipress_gesture_update_cb), button);
696
  g_signal_connect (priv->gesture, "cancel", G_CALLBACK (multipress_gesture_cancel_cb), button);
697
  gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (priv->gesture), GTK_PHASE_BUBBLE);
Elliot Lee's avatar
Elliot Lee committed
698 699
}

700
static void
701
gtk_button_destroy (GtkWidget *widget)
702
{
703
  GtkButton *button = GTK_BUTTON (widget);
704
  GtkButtonPrivate *priv = button->priv;
705

706
  g_clear_pointer (&priv->label_text, g_free);
707

708
  g_clear_object (&priv->gesture);
709

710
  GTK_WIDGET_CLASS (gtk_button_parent_class)->destroy (widget);
711 712
}

713 714
static void
gtk_button_constructed (GObject *object)
715
{
716 717
  GtkButton *button = GTK_BUTTON (object);
  GtkButtonPrivate *priv = button->priv;
718

719
  G_OBJECT_CLASS (gtk_button_parent_class)->constructed (object);
720 721

  priv->constructed = TRUE;
722

723
  if (priv->label_text != NULL)
724 725 726 727
    gtk_button_construct_child (button);
}


Manish Singh's avatar
Manish Singh committed
728
static GType
729 730
gtk_button_child_type  (GtkContainer     *container)
{
Javier Jardón's avatar
Javier Jardón committed
731
  if (!gtk_bin_get_child (GTK_BIN (container)))
732 733
    return GTK_TYPE_WIDGET;
  else
Manish Singh's avatar
Manish Singh committed
734
    return G_TYPE_NONE;
735 736
}

737
static void
738 739
maybe_set_alignment (GtkButton *button,
		     GtkWidget *widget)
740
{
741
  GtkButtonPrivate *priv = button->priv;
742

743 744
  if (!priv->align_set)
    return;
745

746
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
747 748 749
  if (GTK_IS_LABEL (widget))
    g_object_set (widget, "xalign", priv->xalign, "yalign", priv->yalign, NULL);
  else if (GTK_IS_MISC (widget))
750 751
    gtk_misc_set_alignment (GTK_MISC (widget), priv->xalign, priv->yalign);
  else if (GTK_IS_ALIGNMENT (widget))
752
    g_object_set (widget, "xalign", priv->xalign, "yalign", priv->yalign, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
753
G_GNUC_END_IGNORE_DEPRECATIONS
754 755 756 757 758 759
}

static void
gtk_button_add (GtkContainer *container,
		GtkWidget    *widget)
{
760
  maybe_set_alignment (GTK_BUTTON (container), widget);
761

Matthias Clasen's avatar
Matthias Clasen committed
762
  GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
763 764
}

765 766 767 768
static void 
gtk_button_dispose (GObject *object)
{
  GtkButton *button = GTK_BUTTON (object);
769
  GtkButtonPrivate *priv = button->priv;
770

771
  g_clear_object (&priv->action_helper);
772

773 774
  if (priv->action)
    {
775
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
776
      gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
777
      G_GNUC_END_IGNORE_DEPRECATIONS;
778 779 780 781 782
      priv->action = NULL;
    }
  G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
}

783 784 785 786 787 788
static void
gtk_button_set_action_name (GtkActionable *actionable,
                            const gchar   *action_name)
{
  GtkButton *button = GTK_BUTTON (actionable);

789
  g_return_if_fail (button->priv->action == NULL);
790

791 792
  if (!button->priv->action_helper)
    button->priv->action_helper = gtk_action_helper_new (actionable);
793

794 795 796
  g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
  if (action_name)
    g_signal_connect_after (button, "clicked", G_CALLBACK (gtk_real_button_clicked), NULL);
797

798
  gtk_action_helper_set_action_name (button->priv->action_helper, action_name);
799 800 801 802 803 804 805 806
}

static void
gtk_button_set_action_target_value (GtkActionable *actionable,
                                    GVariant      *action_target)
{
  GtkButton *button = GTK_BUTTON (actionable);

807 808
  if (!button->priv->action_helper)
    button->priv->action_helper = gtk_action_helper_new (actionable);
809

810
  gtk_action_helper_set_action_target_value (button->priv->action_helper, action_target);
811 812
}

Elliot Lee's avatar
Elliot Lee committed
813
static void
814 815 816 817
gtk_button_set_property (GObject         *object,
                         guint            prop_id,
                         const GValue    *value,
                         GParamSpec      *pspec)
Elliot Lee's avatar
Elliot Lee committed
818
{
819
  GtkButton *button = GTK_BUTTON (object);
820
  GtkButtonPrivate *priv = button->priv;
Elliot Lee's avatar
Elliot Lee committed
821

822
  switch (prop_id)
823
    {
824
    case PROP_LABEL:
825
      gtk_button_set_label (button, g_value_get_string (value));
826
      break;
827 828 829
    case PROP_IMAGE:
      gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
      break;
830 831 832
    case PROP_ALWAYS_SHOW_IMAGE:
      gtk_button_set_always_show_image (button, g_value_get_boolean (value));
      break;
833 834
    case PROP_RELIEF:
      gtk_button_set_relief (button, g_value_get_enum (value));
835
      break;
836 837 838 839
    case PROP_USE_UNDERLINE:
      gtk_button_set_use_underline (button, g_value_get_boolean (value));
      break;
    case PROP_USE_STOCK:
840
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
841
      gtk_button_set_use_stock (button, g_value_get_boolean (value));
842
      G_GNUC_END_IGNORE_DEPRECATIONS;
843
      break;
844 845 846
    case PROP_FOCUS_ON_CLICK:
      gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
      break;
847
    case PROP_XALIGN:
848
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
849
      gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
850
G_GNUC_END_IGNORE_DEPRECATIONS
851 852
      break;
    case PROP_YALIGN:
853
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
854
      gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
855
G_GNUC_END_IGNORE_DEPRECATIONS
856
      break;
857 858 859
    case PROP_IMAGE_POSITION:
      gtk_button_set_image_position (button, g_value_get_enum (value));
      break;
860 861 862 863 864 865
    case PROP_ACTIVATABLE_RELATED_ACTION:
      gtk_button_set_related_action (button, g_value_get_object (value));
      break;
    case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
      gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
      break;
866 867 868 869 870 871
    case PROP_ACTION_NAME:
      gtk_button_set_action_name (GTK_ACTIONABLE (button), g_value_get_string (value));
      break;
    case PROP_ACTION_TARGET:
      gtk_button_set_action_target_value (GTK_ACTIONABLE (button), g_value_get_variant (value));
      break;
872
    default:
873
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
874 875 876 877 878
      break;
    }
}

static void
879 880 881 882
gtk_button_get_property (GObject         *object,
                         guint            prop_id,
                         GValue          *value,
                         GParamSpec      *pspec)
883
{
884
  GtkButton *button = GTK_BUTTON (object);
885
  GtkButtonPrivate *priv = button->priv;
886

887
  switch (prop_id)
888
    {
889
    case PROP_LABEL:
890
      g_value_set_string (value, priv->label_text);
891
      break;
892 893 894
    case PROP_IMAGE:
      g_value_set_object (value, (GObject *)priv->image);
      break;
895 896 897
    case PROP_ALWAYS_SHOW_IMAGE:
      g_value_set_boolean (value, gtk_button_get_always_show_image (button));
      break;
898
    case PROP_RELIEF:
899
      g_value_set_enum (value, gtk_button_get_relief (button));
900
      break;
901
    case PROP_USE_UNDERLINE:
902
      g_value_set_boolean (value, priv->use_underline);
903 904
      break;
    case PROP_USE_STOCK:
905
      g_value_set_boolean (value, priv->use_stock);
906
      break;
907
    case PROP_FOCUS_ON_CLICK:
908
      g_value_set_boolean (value, priv->focus_on_click);
909
      break;
910 911 912 913 914 915
    case PROP_XALIGN:
      g_value_set_float (value, priv->xalign);
      break;
    case PROP_YALIGN:
      g_value_set_float (value, priv->yalign);
      break;
916 917 918
    case PROP_IMAGE_POSITION:
      g_value_set_enum (value, priv->image_position);
      break;
919 920 921 922 923 924
    case PROP_ACTIVATABLE_RELATED_ACTION:
      g_value_set_object (value, priv->action);
      break;
    case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
      g_value_set_boolean (value, priv->use_action_appearance);
      break;
925
    case PROP_ACTION_NAME:
926
      g_value_set_string (value, gtk_action_helper_get_action_name (priv->action_helper));
927 928
      break;
    case PROP_ACTION_TARGET:
929
      g_value_set_variant (value, gtk_action_helper_get_action_target_value (priv->action_helper));
930
      break;
Tim Janik's avatar
Tim Janik committed
931
    default:
932
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
933
      break;
Elliot Lee's avatar
Elliot Lee committed
934 935 936
    }
}

937 938 939 940 941
static const gchar *
gtk_button_get_action_name (GtkActionable *actionable)
{
  GtkButton *button = GTK_BUTTON (actionable);

942
  return gtk_action_helper_get_action_name (button->priv->action_helper);
943 944 945 946 947 948 949
}

static GVariant *
gtk_button_get_action_target_value (GtkActionable *actionable)
{
  GtkButton *button = GTK_BUTTON (actionable);

950
  return gtk_action_helper_get_action_target_value (button->priv->action_helper);
951 952 953 954 955 956 957 958 959 960 961
}

static void
gtk_button_actionable_iface_init (GtkActionableInterface *iface)
{
  iface->get_action_name = gtk_button_get_action_name;
  iface->set_action_name = gtk_button_set_action_name;
  iface->get_action_target_value = gtk_button_get_action_target_value;
  iface->set_action_target_value = gtk_button_set_action_target_value;
}

962 963 964
static void 
gtk_button_activatable_interface_init (GtkActivatableIface  *iface)
{
965 966
  iface->update = gtk_button_update;
  iface->sync_action_properties = gtk_button_sync_action_properties;
967 968 969 970 971 972
}

static void
activatable_update_stock_id (GtkButton *button,
			     GtkAction *action)
{
973 974 975 976 977 978 979
  gboolean use_stock;

  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
  use_stock = gtk_button_get_use_stock (button);
  G_GNUC_END_IGNORE_DEPRECATIONS;

  if (!use_stock)
980 981
    return;

982
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
983
  gtk_button_set_label (button, gtk_action_get_stock_id (action));
984
  G_GNUC_END_IGNORE_DEPRECATIONS;
985 986 987 988 989 990
}

static void
activatable_update_short_label (GtkButton *button,
				GtkAction *action)
{
Javier Jardón's avatar
Javier Jardón committed
991
  GtkWidget *child;
992
  GtkWidget *image;
993 994 995 996 997
  gboolean use_stock;

  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
  use_stock = gtk_button_get_use_stock (button);
  G_GNUC_END_IGNORE_DEPRECATIONS;
998

999
  if (use_stock)
1000 1001 1002 1003 1004
    return;

  image = gtk_button_get_image (button);

  /* Dont touch custom child... */
Javier Jardón's avatar
Javier Jardón committed
1005
  child = gtk_bin_get_child (GTK_BIN (button));
1006
  if (GTK_IS_IMAGE (image) ||
Javier Jardón's avatar
Javier Jardón committed
1007 1008
      child == NULL ||
      GTK_IS_LABEL (child))
1009
    {
1010
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1011
      gtk_button_set_label (button, gtk_action_get_short_label (action));
1012
      G_GNUC_END_IGNORE_DEPRECATIONS;
1013 1014 1015 1016 1017 1018 1019 1020 1021
      gtk_button_set_use_underline (button, TRUE);
    }
}

static void
activatable_update_icon_name (GtkButton *button,
			      GtkAction *action)
{
  GtkWidget *image;
1022 1023 1024 1025 1026 1027 1028
  gboolean use_stock;

  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
  use_stock = gtk_button_get_use_stock (button);
  G_GNUC_END_IGNORE_DEPRECATIONS;

  if (use_stock)
1029 1030 1031 1032 1033 1034 1035
    return;

  image = gtk_button_get_image (button);

  if (GTK_IS_IMAGE (image) &&
      (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
       gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
1036 1037 1038 1039 1040 1041
    {
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
      gtk_image_set_from_icon_name (GTK_IMAGE (image),
                                    gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
      G_GNUC_END_IGNORE_DEPRECATIONS;
    }
1042 1043 1044 1045 1046 1047 1048
}

static void
activatable_update_gicon (GtkButton *button,
			  GtkAction *action)
{
  GtkWidget *image = gtk_button_get_image (button);
1049 1050 1051 1052 1053 1054
  GIcon *icon;

  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
  icon = gtk_action_get_gicon (action);
  G_GNUC_END_IGNORE_DEPRECATIONS;

1055 1056 1057 1058 1059 1060 1061
  if (GTK_IS_IMAGE (image) &&
      (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
       gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
    gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
}

static void 
1062 1063 1064
gtk_button_update (GtkActivatable *activatable,
		   GtkAction      *action,
	           const gchar    *property_name)
1065
{
1066 1067
  GtkButton *button = GTK_BUTTON (activatable);
  GtkButtonPrivate *priv = button->priv;
1068 1069 1070

  if (strcmp (property_name, "visible") == 0)
    {
1071
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1072 1073 1074 1075
      if (gtk_action_is_visible (action))
	gtk_widget_show (GTK_WIDGET (activatable));
      else
	gtk_widget_hide (GTK_WIDGET (activatable));
1076
      G_GNUC_END_IGNORE_DEPRECATIONS;
1077 1078
    }
  else if (strcmp (property_name, "sensitive") == 0)
1079 1080 1081 1082 1083
    {
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
      gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
      G_GNUC_END_IGNORE_DEPRECATIONS;
    }
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097

  if (!priv->use_action_appearance)
    return;

  if (strcmp (property_name, "stock-id") == 0)
    activatable_update_stock_id (GTK_BUTTON (activatable), action);
  else if (strcmp (property_name, "gicon") == 0)
    activatable_update_gicon (GTK_BUTTON (activatable), action);
  else if (strcmp (property_name, "short-label") == 0)
    activatable_update_short_label (GTK_BUTTON (activatable), action);
  else if (strcmp (property_name, "icon-name") == 0)
    activatable_update_icon_name (GTK_BUTTON (activatable), action);
}

1098 1099 1100
static void
gtk_button_sync_action_properties (GtkActivatable *activatable,
			           GtkAction      *action)
1101
{
1102 1103
  GtkButton *button = GTK_BUTTON (activatable);
  GtkButtonPrivate *priv = button->priv;
1104
  gboolean always_show_image;
1105 1106 1107 1108

  if (!action)
    return;

1109 1110
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS

1111 1112 1113 1114 1115 1116
  if (gtk_action_is_visible (action))
    gtk_widget_show (GTK_WIDGET (activatable));
  else
    gtk_widget_hide (GTK_WIDGET (activatable));
  
  gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
1117 1118 1119
  always_show_image = gtk_action_get_always_show_image (action);
  G_GNUC_END_IGNORE_DEPRECATIONS

1120 1121 1122 1123 1124 1125 1126
  if (priv->use_action_appearance)
    {
      activatable_update_stock_id (GTK_BUTTON (activatable), action);
      activatable_update_short_label (GTK_BUTTON (activatable), action);
      activatable_update_gicon (GTK_BUTTON (activatable), action);
      activatable_update_icon_name (GTK_BUTTON (activatable), action);
    }
1127

1128
  gtk_button_set_always_show_image (button, always_show_image);
1129 1130 1131
}

static void
1132 1133
gtk_button_set_related_action (GtkButton *button,
			       GtkAction *action)
1134
{
1135
  GtkButtonPrivate *priv = button->priv;
1136

1137
  g_return_if_fail (gtk_action_helper_get_action_name (button->priv->action_helper) == NULL);
1138

1139 1140 1141
  if (priv->action == action)
    return;

1142 1143 1144 1145 1146 1147
  /* This should be a default handler, but for compatibility reasons
   * we need to support derived classes that don't chain up their
   * clicked handler.
   */
  g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
  if (action)
1148
    g_signal_connect_after (button, "clicked",
1149 1150
                            G_CALLBACK (gtk_real_button_clicked), NULL);

1151
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS
1152
  gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
1153
  G_GNUC_END_IGNORE_DEPRECATIONS
1154 1155 1156 1157 1158

  priv->action = action;
}

static void
1159 1160
gtk_button_set_use_action_appearance (GtkButton *button,
				      gboolean   use_appearance)
1161
{
1162
  GtkButtonPrivate *priv = button->priv;
1163 1164 1165 1166

  if (priv->use_action_appearance != use_appearance)
    {
      priv->use_action_appearance = use_appearance;
1167

1168
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS
1169
      gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
1170 1171
      G_GNUC_BEGIN_IGNORE_DEPRECATIONS

1172 1173 1174
    }
}

1175 1176 1177 1178 1179 1180 1181 1182
/**
 * gtk_button_new:
 *
 * Creates a new #GtkButton widget. To add a child widget to the button,
 * use gtk_container_add().
 *
 * Returns: The newly created #GtkButton widget.
 */
Elliot Lee's avatar
Elliot Lee committed
1183
GtkWidget*
1184
gtk_button_new (void)
Elliot Lee's avatar
Elliot Lee committed
1185
{
Manish Singh's avatar
Manish Singh committed
1186
  return g_object_new (GTK_TYPE_BUTTON, NULL);
Elliot Lee's avatar
Elliot Lee committed
1187 1188
}

Matthias Clasen's avatar
Matthias Clasen committed
1189 1190 1191
static gboolean
show_image (GtkButton *button)
{
1192
  GtkButtonPrivate *priv = button->priv;
Matthias Clasen's avatar
Matthias Clasen committed
1193
  gboolean show;