gtkbutton.c 71.2 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
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
22 23 24 25 26
 * 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/. 
 */

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

41
#include "config.h"
42
#include <string.h>
43
#include "gtkalignment.h"
Elliot Lee's avatar
Elliot Lee committed
44 45 46
#include "gtkbutton.h"
#include "gtklabel.h"
#include "gtkmain.h"
47
#include "gtkmarshalers.h"
48 49
#include "gtkimage.h"
#include "gtkhbox.h"
50
#include "gtkvbox.h"
51 52
#include "gtkstock.h"
#include "gtkiconfactory.h"
53
#include "gtkactivatable.h"
54
#include "gtksizerequest.h"
55
#include "gtkprivate.h"
56
#include "gtkintl.h"
57

Elliot Lee's avatar
Elliot Lee committed
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
struct _GtkButtonPrivate
{
  GtkAction             *action;
  GtkPositionType        image_position;
  GtkWidget             *image;

  GdkDevice             *grab_keyboard;
  GdkWindow             *event_window;

  gchar         *label_text;

  gfloat         xalign;
  gfloat         yalign;

  guint          activate_timeout;
  guint32        grab_time;

  guint          align_set             : 1;
  guint          button_down           : 1;
  guint          constructed           : 1;
  guint          depressed             : 1;
  guint          depress_on_activate   : 1;
  guint          focus_on_click        : 1;
  guint          image_is_stock        : 1;
  guint          in_button             : 1;
  guint          relief                : 2;
  guint          use_action_appearance : 1;
  guint          use_stock             : 1;
  guint          use_underline         : 1;
};


91 92
static const GtkBorder default_default_border = { 1, 1, 1, 1 };
static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
93
static const GtkBorder default_inner_border = { 1, 1, 1, 1 };
Elliot Lee's avatar
Elliot Lee committed
94

Matthias Clasen's avatar
Matthias Clasen committed
95
/* Time out before giving up on getting a key release when animating
96 97 98
 * the close button.
 */
#define ACTIVATE_TIMEOUT 250
Elliot Lee's avatar
Elliot Lee committed
99

100

Elliot Lee's avatar
Elliot Lee committed
101 102 103 104 105 106
enum {
  PRESSED,
  RELEASED,
  CLICKED,
  ENTER,
  LEAVE,
107
  ACTIVATE,
Elliot Lee's avatar
Elliot Lee committed
108 109
  LAST_SIGNAL
};
110

111
enum {
112 113
  PROP_0,
  PROP_LABEL,
Matthias Clasen's avatar
Matthias Clasen committed
114
  PROP_IMAGE,
115 116
  PROP_RELIEF,
  PROP_USE_UNDERLINE,
Soeren Sandmann's avatar
Soeren Sandmann committed
117
  PROP_USE_STOCK,
118 119
  PROP_FOCUS_ON_CLICK,
  PROP_XALIGN,
Matthias Clasen's avatar
Matthias Clasen committed
120
  PROP_YALIGN,
121 122 123 124 125
  PROP_IMAGE_POSITION,

  /* activatable properties */
  PROP_ACTIVATABLE_RELATED_ACTION,
  PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
126 127
};

128

129
static void gtk_button_destroy        (GtkWidget          *widget);
130
static void gtk_button_dispose        (GObject            *object);
131 132 133 134 135 136 137 138 139 140
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);
141 142 143 144 145 146 147
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);
static void gtk_button_style_set (GtkWidget * widget, GtkStyle * prev_style);
static void gtk_button_size_allocate (GtkWidget * widget,
				      GtkAllocation * allocation);
Benjamin Otte's avatar
Benjamin Otte committed
148
static gint gtk_button_draw (GtkWidget * widget, cairo_t *cr);
149 150 151 152 153 154 155 156 157 158 159 160 161 162
static gint gtk_button_button_press (GtkWidget * widget,
				     GdkEventButton * event);
static gint gtk_button_button_release (GtkWidget * widget,
				       GdkEventButton * event);
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);
163 164 165 166 167 168 169
static void gtk_real_button_activate  (GtkButton          *button);
static void gtk_button_update_state   (GtkButton          *button);
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
170

171 172 173
static GObject*	gtk_button_constructor (GType                  type,
					guint                  n_construct_properties,
					GObjectConstructParam *construct_params);
Matthias Clasen's avatar
Matthias Clasen committed
174
static void gtk_button_construct_child (GtkButton             *button);
175 176 177 178 179
static void gtk_button_state_changed   (GtkWidget             *widget,
					GtkStateType           previous_state);
static void gtk_button_grab_notify     (GtkWidget             *widget,
					gboolean               was_grabbed);

180

181
static void gtk_button_activatable_interface_init(GtkActivatableIface  *iface);
182 183 184 185 186 187 188 189 190
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);
191

192
static void gtk_button_get_preferred_width       (GtkWidget           *widget,
193 194
						  gint                *minimum_size,
						  gint                *natural_size);
195
static void gtk_button_get_preferred_height      (GtkWidget           *widget,
196 197
						  gint                *minimum_size,
						  gint                *natural_size);
198
  
199
static guint button_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
200

201 202
G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
203
						gtk_button_activatable_interface_init))
Elliot Lee's avatar
Elliot Lee committed
204 205 206 207

static void
gtk_button_class_init (GtkButtonClass *klass)
{
Manish Singh's avatar
Manish Singh committed
208
  GObjectClass *gobject_class;
Elliot Lee's avatar
Elliot Lee committed
209 210 211
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

Manish Singh's avatar
Manish Singh committed
212
  gobject_class = G_OBJECT_CLASS (klass);
Elliot Lee's avatar
Elliot Lee committed
213 214
  widget_class = (GtkWidgetClass*) klass;
  container_class = (GtkContainerClass*) klass;
215
  
216 217
  gobject_class->constructor  = gtk_button_constructor;
  gobject_class->dispose      = gtk_button_dispose;
Manish Singh's avatar
Manish Singh committed
218 219
  gobject_class->set_property = gtk_button_set_property;
  gobject_class->get_property = gtk_button_get_property;
220

221 222
  widget_class->get_preferred_width  = gtk_button_get_preferred_width;
  widget_class->get_preferred_height = gtk_button_get_preferred_height;
223
  widget_class->destroy = gtk_button_destroy;
Matthias Clasen's avatar
Matthias Clasen committed
224
  widget_class->screen_changed = gtk_button_screen_changed;
225
  widget_class->realize = gtk_button_realize;
226
  widget_class->unrealize = gtk_button_unrealize;
227 228
  widget_class->map = gtk_button_map;
  widget_class->unmap = gtk_button_unmap;
229
  widget_class->style_set = gtk_button_style_set;
230
  widget_class->size_allocate = gtk_button_size_allocate;
Benjamin Otte's avatar
Benjamin Otte committed
231
  widget_class->draw = gtk_button_draw;
232 233
  widget_class->button_press_event = gtk_button_button_press;
  widget_class->button_release_event = gtk_button_button_release;
Matthias Clasen's avatar
Matthias Clasen committed
234
  widget_class->grab_broken_event = gtk_button_grab_broken;
235
  widget_class->key_release_event = gtk_button_key_release;
236 237
  widget_class->enter_notify_event = gtk_button_enter_notify;
  widget_class->leave_notify_event = gtk_button_leave_notify;
238 239
  widget_class->state_changed = gtk_button_state_changed;
  widget_class->grab_notify = gtk_button_grab_notify;
240 241

  container_class->child_type = gtk_button_child_type;
242
  container_class->add = gtk_button_add;
243
  gtk_container_class_handle_border_width (container_class);
244 245 246

  klass->pressed = gtk_real_button_pressed;
  klass->released = gtk_real_button_released;
247
  klass->clicked = NULL;
248 249
  klass->enter = gtk_button_update_state;
  klass->leave = gtk_button_update_state;
250
  klass->activate = gtk_real_button_activate;
251

Manish Singh's avatar
Manish Singh committed
252
  g_object_class_install_property (gobject_class,
253 254
                                   PROP_LABEL,
                                   g_param_spec_string ("label",
255 256
                                                        P_("Label"),
                                                        P_("Text of the label widget inside the button, if the button contains a label widget"),
257
                                                        NULL,
258
                                                        GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
259
  
Manish Singh's avatar
Manish Singh committed
260
  g_object_class_install_property (gobject_class,
261
                                   PROP_USE_UNDERLINE,
Matthias Clasen's avatar
x  
Matthias Clasen committed
262
                                   g_param_spec_boolean ("use-underline",
263 264
							 P_("Use underline"),
							 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
265
                                                        FALSE,
266
                                                        GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
267
  
Manish Singh's avatar
Manish Singh committed
268
  g_object_class_install_property (gobject_class,
269
                                   PROP_USE_STOCK,
Matthias Clasen's avatar
x  
Matthias Clasen committed
270
                                   g_param_spec_boolean ("use-stock",
271 272
							 P_("Use stock"),
							 P_("If set, the label is used to pick a stock item instead of being displayed"),
273
                                                        FALSE,
274
                                                        GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
275
  
Soeren Sandmann's avatar
Soeren Sandmann committed
276 277
  g_object_class_install_property (gobject_class,
                                   PROP_FOCUS_ON_CLICK,
Matthias Clasen's avatar
x  
Matthias Clasen committed
278
                                   g_param_spec_boolean ("focus-on-click",
279 280
							 P_("Focus on click"),
							 P_("Whether the button grabs focus when it is clicked with the mouse"),
Soeren Sandmann's avatar
Soeren Sandmann committed
281
							 TRUE,
282
							 GTK_PARAM_READWRITE));
Soeren Sandmann's avatar
Soeren Sandmann committed
283
  
Manish Singh's avatar
Manish Singh committed
284
  g_object_class_install_property (gobject_class,
285 286
                                   PROP_RELIEF,
                                   g_param_spec_enum ("relief",
287 288
                                                      P_("Border relief"),
                                                      P_("The border relief style"),
289 290
                                                      GTK_TYPE_RELIEF_STYLE,
                                                      GTK_RELIEF_NORMAL,
291
                                                      GTK_PARAM_READWRITE));
292 293 294 295 296 297 298
  
  /**
   * GtkButton:xalign:
   *
   * If the child of the button is a #GtkMisc or #GtkAlignment, this property 
   * can be used to control it's horizontal alignment. 0.0 is left aligned, 
   * 1.0 is right aligned.
299
   *
300 301 302 303 304
   * Since: 2.4
   */
  g_object_class_install_property (gobject_class,
                                   PROP_XALIGN,
                                   g_param_spec_float("xalign",
305 306
                                                      P_("Horizontal alignment for child"),
                                                      P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
307 308 309
                                                      0.0,
                                                      1.0,
                                                      0.5,
310
                                                      GTK_PARAM_READWRITE));
311 312 313 314 315 316 317

  /**
   * GtkButton:yalign:
   *
   * If the child of the button is a #GtkMisc or #GtkAlignment, this property 
   * can be used to control it's vertical alignment. 0.0 is top aligned, 
   * 1.0 is bottom aligned.
318
   *
319 320 321 322 323
   * Since: 2.4
   */
  g_object_class_install_property (gobject_class,
                                   PROP_YALIGN,
                                   g_param_spec_float("yalign",
324 325
                                                      P_("Vertical alignment for child"),
                                                      P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
326 327 328
                                                      0.0,
                                                      1.0,
                                                      0.5,
329
                                                      GTK_PARAM_READWRITE));
Elliot Lee's avatar
Elliot Lee committed
330

Matthias Clasen's avatar
Matthias Clasen committed
331 332
  /**
   * GtkButton::image:
333
   *
Matthias Clasen's avatar
Matthias Clasen committed
334
   * The child widget to appear next to the button text.
335
   *
Matthias Clasen's avatar
Matthias Clasen committed
336 337 338 339 340 341 342 343
   * Since: 2.6
   */
  g_object_class_install_property (gobject_class,
                                   PROP_IMAGE,
                                   g_param_spec_object ("image",
                                                        P_("Image widget"),
                                                        P_("Child widget to appear next to the button text"),
                                                        GTK_TYPE_WIDGET,
344
                                                        GTK_PARAM_READWRITE));
Matthias Clasen's avatar
Matthias Clasen committed
345

346 347 348
  /**
   * GtkButton:image-position:
   *
349
   * The position of the image relative to the text inside the button.
350
   *
351 352 353 354 355
   * Since: 2.10
   */
  g_object_class_install_property (gobject_class,
                                   PROP_IMAGE_POSITION,
                                   g_param_spec_enum ("image-position",
356
                                            P_("Image position"),
357 358 359 360 361
                                                      P_("The position of the image relative to the text"),
                                                      GTK_TYPE_POSITION_TYPE,
                                                      GTK_POS_LEFT,
                                                      GTK_PARAM_READWRITE));

362 363 364
  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");

365 366 367 368 369
  /**
   * GtkButton::pressed:
   * @button: the object that received the signal
   *
   * Emitted when the button is pressed.
370
   *
371
   * Deprecated: 2.8: Use the #GtkWidget::button-press-event signal.
372
   */ 
Elliot Lee's avatar
Elliot Lee committed
373
  button_signals[PRESSED] =
Matthias Clasen's avatar
Matthias Clasen committed
374
    g_signal_new (I_("pressed"),
375
		  G_OBJECT_CLASS_TYPE (gobject_class),
Manish Singh's avatar
Manish Singh committed
376 377 378 379 380
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkButtonClass, pressed),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
381 382 383 384 385 386

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

  /**
   * 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
405
  button_signals[CLICKED] =
Matthias Clasen's avatar
Matthias Clasen committed
406
    g_signal_new (I_("clicked"),
407
		  G_OBJECT_CLASS_TYPE (gobject_class),
Manish Singh's avatar
Manish Singh committed
408 409 410 411 412
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkButtonClass, clicked),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
413 414 415 416 417 418

  /**
   * GtkButton::enter:
   * @button: the object that received the signal
   *
   * Emitted when the pointer enters the button.
419
   *
420
   * Deprecated: 2.8: Use the #GtkWidget::enter-notify-event signal.
421
   */ 
Elliot Lee's avatar
Elliot Lee committed
422
  button_signals[ENTER] =
Matthias Clasen's avatar
Matthias Clasen committed
423
    g_signal_new (I_("enter"),
424
		  G_OBJECT_CLASS_TYPE (gobject_class),
Manish Singh's avatar
Manish Singh committed
425 426 427 428 429
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GtkButtonClass, enter),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
430 431 432 433 434 435

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

  /**
   * GtkButton::activate:
Matthias Clasen's avatar
Matthias Clasen committed
450
   * @widget: the object which received the signal.
451
   *
452
   * The ::activate signal on GtkButton is an action signal and
453 454
   * emitting it causes the button to animate press then release. 
   * Applications should never connect to this signal, but use the
455
   * #GtkButton::clicked signal.
456
   */
457
  button_signals[ACTIVATE] =
Matthias Clasen's avatar
Matthias Clasen committed
458
    g_signal_new (I_("activate"),
459
		  G_OBJECT_CLASS_TYPE (gobject_class),
Manish Singh's avatar
Manish Singh committed
460 461 462 463 464
		  G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkButtonClass, activate),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
465
  widget_class->activate_signal = button_signals[ACTIVATE];
466

467 468 469 470 471 472 473 474
  /**
   * 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().
   */

475
  gtk_widget_class_install_style_property (widget_class,
Matthias Clasen's avatar
x  
Matthias Clasen committed
476
					   g_param_spec_boxed ("default-border",
477
							       P_("Default Spacing"),
478
							       P_("Extra space to add for GTK_CAN_DEFAULT buttons"),
479
							       GTK_TYPE_BORDER,
480
							       GTK_PARAM_READABLE));
481

482 483 484 485 486 487 488 489
  /**
   * 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().
   */
490
  gtk_widget_class_install_style_property (widget_class,
Matthias Clasen's avatar
x  
Matthias Clasen committed
491
					   g_param_spec_boxed ("default-outside-border",
492
							       P_("Default Outside Spacing"),
493
							       P_("Extra space to add for GTK_CAN_DEFAULT buttons that is always drawn outside the border"),
494
							       GTK_TYPE_BORDER,
495
							       GTK_PARAM_READABLE));
496
  gtk_widget_class_install_style_property (widget_class,
Matthias Clasen's avatar
x  
Matthias Clasen committed
497
					   g_param_spec_int ("child-displacement-x",
498 499
							     P_("Child X Displacement"),
							     P_("How far in the x direction to move the child when the button is depressed"),
500 501 502
							     G_MININT,
							     G_MAXINT,
							     0,
503
							     GTK_PARAM_READABLE));
504
  gtk_widget_class_install_style_property (widget_class,
Matthias Clasen's avatar
x  
Matthias Clasen committed
505
					   g_param_spec_int ("child-displacement-y",
506 507
							     P_("Child Y Displacement"),
							     P_("How far in the y direction to move the child when the button is depressed"),
508 509 510
							     G_MININT,
							     G_MAXINT,
							     0,
511
							     GTK_PARAM_READABLE));
512

513 514 515
  /**
   * GtkButton:displace-focus:
   *
516 517
   * Whether the child_displacement_x/child_displacement_y properties 
   * should also affect the focus rectangle.
518 519 520 521 522 523 524
   *
   * 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
525 526
								 FALSE,
								 GTK_PARAM_READABLE));
527

528
  /**
529
   * GtkButton:inner-border:
530
   *
531
   * Sets the border between the button edges and child.
532 533 534 535
   *
   * Since: 2.10
   */
  gtk_widget_class_install_style_property (widget_class,
536 537 538 539 540
					   g_param_spec_boxed ("inner-border",
                                                               P_("Inner Border"),
                                                               P_("Border between button edges and child."),
                                                               GTK_TYPE_BORDER,
                                                               GTK_PARAM_READABLE));
541

542 543
  /**
   * GtkButton::image-spacing:
544
   *
545
   * Spacing in pixels between the image and label.
546
   *
547 548 549 550 551 552 553 554 555 556 557
   * 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));

558
  g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
Elliot Lee's avatar
Elliot Lee committed
559 560 561 562 563
}

static void
gtk_button_init (GtkButton *button)
{
564 565 566 567 568 569
  GtkButtonPrivate *priv;

  button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
                                              GTK_TYPE_BUTTON,
                                              GtkButtonPrivate);
  priv = button->priv;
570

571
  gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
572
  gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
573
  gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
Elliot Lee's avatar
Elliot Lee committed
574

575 576 577 578 579 580 581 582 583 584 585
  priv->label_text = NULL;

  priv->constructed = FALSE;
  priv->in_button = FALSE;
  priv->button_down = FALSE;
  priv->relief = GTK_RELIEF_NORMAL;
  priv->use_stock = FALSE;
  priv->use_underline = FALSE;
  priv->depressed = FALSE;
  priv->depress_on_activate = TRUE;
  priv->focus_on_click = TRUE;
586 587 588

  priv->xalign = 0.5;
  priv->yalign = 0.5;
Matthias Clasen's avatar
Matthias Clasen committed
589
  priv->align_set = 0;
590
  priv->image_is_stock = TRUE;
591
  priv->image_position = GTK_POS_LEFT;
592
  priv->use_action_appearance = TRUE;
Elliot Lee's avatar
Elliot Lee committed
593 594
}

595
static void
596
gtk_button_destroy (GtkWidget *widget)
597
{
598
  GtkButton *button = GTK_BUTTON (widget);
599
  GtkButtonPrivate *priv = button->priv;
600

601
  if (priv->label_text)
602
    {
603 604
      g_free (priv->label_text);
      priv->label_text = NULL;
605
    }
606

607
  GTK_WIDGET_CLASS (gtk_button_parent_class)->destroy (widget);
608 609
}

610 611 612 613 614 615 616
static GObject*
gtk_button_constructor (GType                  type,
			guint                  n_construct_properties,
			GObjectConstructParam *construct_params)
{
  GObject *object;
  GtkButton *button;
617
  GtkButtonPrivate *priv;
618

619 620 621
  object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
                                                                  n_construct_properties,
                                                                  construct_params);
622 623

  button = GTK_BUTTON (object);
624 625 626
  priv = button->priv;

  priv->constructed = TRUE;
627

628
  if (priv->label_text != NULL)
629 630 631 632 633 634
    gtk_button_construct_child (button);
  
  return object;
}


Manish Singh's avatar
Manish Singh committed
635
static GType
636 637
gtk_button_child_type  (GtkContainer     *container)
{
Javier Jardón's avatar
Javier Jardón committed
638
  if (!gtk_bin_get_child (GTK_BIN (container)))
639 640
    return GTK_TYPE_WIDGET;
  else
Manish Singh's avatar
Manish Singh committed
641
    return G_TYPE_NONE;
642 643
}

644
static void
Matthias Clasen's avatar
Matthias Clasen committed
645 646
maybe_set_alignment (GtkButton *button,
		     GtkWidget *widget)
647
{
648
  GtkButtonPrivate *priv = button->priv;
Matthias Clasen's avatar
Matthias Clasen committed
649

650 651 652
  if (GTK_IS_MISC (widget))
    {
      GtkMisc *misc = GTK_MISC (widget);
Matthias Clasen's avatar
Matthias Clasen committed
653 654 655
      
      if (priv->align_set)
	gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
656 657 658 659
    }
  else if (GTK_IS_ALIGNMENT (widget))
    {
      GtkAlignment *alignment = GTK_ALIGNMENT (widget);
660 661 662 663 664 665
      gfloat xscale, yscale;

      g_object_get (alignment,
                    "xscale", &xscale,
                    "yscale", &yscale,
                    NULL);
Matthias Clasen's avatar
Matthias Clasen committed
666 667

      if (priv->align_set)
668 669 670
        gtk_alignment_set (alignment,
                           priv->xalign, priv->yalign,
                           xscale, yscale);
671 672 673 674 675 676 677
    }
}

static void
gtk_button_add (GtkContainer *container,
		GtkWidget    *widget)
{
Matthias Clasen's avatar
Matthias Clasen committed
678
  maybe_set_alignment (GTK_BUTTON (container), widget);
679

Matthias Clasen's avatar
Matthias Clasen committed
680
  GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
681 682
}

683 684 685 686
static void 
gtk_button_dispose (GObject *object)
{
  GtkButton *button = GTK_BUTTON (object);
687
  GtkButtonPrivate *priv = button->priv;
688 689 690 691 692 693 694 695 696

  if (priv->action)
    {
      gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
      priv->action = NULL;
    }
  G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
}

Elliot Lee's avatar
Elliot Lee committed
697
static void
698 699 700 701
gtk_button_set_property (GObject         *object,
                         guint            prop_id,
                         const GValue    *value,
                         GParamSpec      *pspec)
Elliot Lee's avatar
Elliot Lee committed
702
{
703
  GtkButton *button = GTK_BUTTON (object);
704
  GtkButtonPrivate *priv = button->priv;
Elliot Lee's avatar
Elliot Lee committed
705

706
  switch (prop_id)
707
    {
708
    case PROP_LABEL:
709
      gtk_button_set_label (button, g_value_get_string (value));
710
      break;
Matthias Clasen's avatar
Matthias Clasen committed
711 712 713
    case PROP_IMAGE:
      gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
      break;
714 715
    case PROP_RELIEF:
      gtk_button_set_relief (button, g_value_get_enum (value));
716
      break;
717 718 719 720 721 722
    case PROP_USE_UNDERLINE:
      gtk_button_set_use_underline (button, g_value_get_boolean (value));
      break;
    case PROP_USE_STOCK:
      gtk_button_set_use_stock (button, g_value_get_boolean (value));
      break;
Soeren Sandmann's avatar
Soeren Sandmann committed
723 724 725
    case PROP_FOCUS_ON_CLICK:
      gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
      break;
726 727 728 729 730 731
    case PROP_XALIGN:
      gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
      break;
    case PROP_YALIGN:
      gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
      break;
732 733 734
    case PROP_IMAGE_POSITION:
      gtk_button_set_image_position (button, g_value_get_enum (value));
      break;
735 736 737 738 739 740
    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;
741
    default:
742
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
743 744 745 746 747
      break;
    }
}

static void
748 749 750 751
gtk_button_get_property (GObject         *object,
                         guint            prop_id,
                         GValue          *value,
                         GParamSpec      *pspec)
752
{
753
  GtkButton *button = GTK_BUTTON (object);
754
  GtkButtonPrivate *priv = button->priv;
755

756
  switch (prop_id)
757
    {
758
    case PROP_LABEL:
759
      g_value_set_string (value, priv->label_text);
760
      break;
Matthias Clasen's avatar
Matthias Clasen committed
761 762 763
    case PROP_IMAGE:
      g_value_set_object (value, (GObject *)priv->image);
      break;
764
    case PROP_RELIEF:
765
      g_value_set_enum (value, priv->relief);
766
      break;
767
    case PROP_USE_UNDERLINE:
768
      g_value_set_boolean (value, priv->use_underline);
769 770
      break;
    case PROP_USE_STOCK:
771
      g_value_set_boolean (value, priv->use_stock);
772
      break;
Soeren Sandmann's avatar
Soeren Sandmann committed
773
    case PROP_FOCUS_ON_CLICK:
774
      g_value_set_boolean (value, priv->focus_on_click);
Soeren Sandmann's avatar
Soeren Sandmann committed
775
      break;
776 777 778 779 780 781
    case PROP_XALIGN:
      g_value_set_float (value, priv->xalign);
      break;
    case PROP_YALIGN:
      g_value_set_float (value, priv->yalign);
      break;
782 783 784
    case PROP_IMAGE_POSITION:
      g_value_set_enum (value, priv->image_position);
      break;
785 786 787 788 789 790
    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;
Tim Janik's avatar
Tim Janik committed
791
    default:
792
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
793
      break;
Elliot Lee's avatar
Elliot Lee committed
794 795 796
    }
}

797 798 799
static void 
gtk_button_activatable_interface_init (GtkActivatableIface  *iface)
{
800 801
  iface->update = gtk_button_update;
  iface->sync_action_properties = gtk_button_sync_action_properties;
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
}

static void
activatable_update_stock_id (GtkButton *button,
			     GtkAction *action)
{
  if (!gtk_button_get_use_stock (button))
    return;

  gtk_button_set_label (button, gtk_action_get_stock_id (action));
}

static void
activatable_update_short_label (GtkButton *button,
				GtkAction *action)
{
Javier Jardón's avatar
Javier Jardón committed
818
  GtkWidget *child;
819 820 821 822 823 824 825 826
  GtkWidget *image;

  if (gtk_button_get_use_stock (button))
    return;

  image = gtk_button_get_image (button);

  /* Dont touch custom child... */
Javier Jardón's avatar
Javier Jardón committed
827
  child = gtk_bin_get_child (GTK_BIN (button));
828
  if (GTK_IS_IMAGE (image) ||
Javier Jardón's avatar
Javier Jardón committed
829 830
      child == NULL ||
      GTK_IS_LABEL (child))
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
    {
      gtk_button_set_label (button, gtk_action_get_short_label (action));
      gtk_button_set_use_underline (button, TRUE);
    }
}

static void
activatable_update_icon_name (GtkButton *button,
			      GtkAction *action)
{
  GtkWidget *image;
	      
  if (gtk_button_get_use_stock (button))
    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))
    gtk_image_set_from_icon_name (GTK_IMAGE (image),
				  gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
}

static void
activatable_update_gicon (GtkButton *button,
			  GtkAction *action)
{
  GtkWidget *image = gtk_button_get_image (button);
  GIcon *icon = gtk_action_get_gicon (action);
  
  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 
869 870 871
gtk_button_update (GtkActivatable *activatable,
		   GtkAction      *action,
	           const gchar    *property_name)
872
{
873 874
  GtkButton *button = GTK_BUTTON (activatable);
  GtkButtonPrivate *priv = button->priv;
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898

  if (strcmp (property_name, "visible") == 0)
    {
      if (gtk_action_is_visible (action))
	gtk_widget_show (GTK_WIDGET (activatable));
      else
	gtk_widget_hide (GTK_WIDGET (activatable));
    }
  else if (strcmp (property_name, "sensitive") == 0)
    gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));

  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);
}

899 900 901
static void
gtk_button_sync_action_properties (GtkActivatable *activatable,
			           GtkAction      *action)
902
{
903 904
  GtkButton *button = GTK_BUTTON (activatable);
  GtkButtonPrivate *priv = button->priv;
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925

  if (!action)
    return;

  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));
  
  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);
    }
}

static void