gtkmodelbutton.c 24.5 KB
Newer Older
1 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
/*
 * Copyright © 2014 Red Hat, Inc.
 *
 * 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 licence, 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, see <http://www.gnu.org/licenses/>.
 *
 * Author: Matthias Clasen
 */

#include "config.h"

#include "gtkmodelbutton.h"

#include "gtkbutton.h"
#include "gtkbuttonprivate.h"
#include "gtkwidgetprivate.h"
#include "gtkmenutrackeritem.h"
#include "gtkimage.h"
#include "gtklabel.h"
#include "gtkbox.h"
31
#include "gtkrender.h"
32 33 34 35 36 37 38 39 40 41 42
#include "gtkstylecontext.h"
#include "gtktypebuiltins.h"

struct _GtkModelButton
{
  GtkButton parent_instance;
  GtkWidget *box;
  GtkWidget *image;
  GtkWidget *label;
  gboolean toggled;
  gboolean has_submenu;
43
  gboolean centered;
44
  gboolean inverted;
45
  gboolean iconic;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  GtkMenuTrackerItemRole role;
};

typedef GtkButtonClass GtkModelButtonClass;

G_DEFINE_TYPE (GtkModelButton, gtk_model_button, GTK_TYPE_BUTTON)

enum
{
  PROP_0,
  PROP_ACTION_ROLE,
  PROP_ICON,
  PROP_TEXT,
  PROP_TOGGLED,
  PROP_ACCEL,
  PROP_HAS_SUBMENU,
  PROP_INVERTED,
63 64
  PROP_CENTERED,
  PROP_ICONIC
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 91 92 93 94 95 96 97 98 99 100 101
};

static void
gtk_model_button_set_action_role (GtkModelButton         *button,
                                  GtkMenuTrackerItemRole  role)
{
  AtkObject *accessible;
  AtkRole a11y_role;

  if (role == button->role)
    return;

  button->role = role;
  gtk_widget_queue_draw (GTK_WIDGET (button));

  accessible = gtk_widget_get_accessible (GTK_WIDGET (button));
  switch (role)
    {
    case GTK_MENU_TRACKER_ITEM_ROLE_NORMAL:
      a11y_role = ATK_ROLE_PUSH_BUTTON;
      break;

    case GTK_MENU_TRACKER_ITEM_ROLE_CHECK:
      a11y_role = ATK_ROLE_CHECK_BOX;
      break;

    case GTK_MENU_TRACKER_ITEM_ROLE_RADIO:
      a11y_role = ATK_ROLE_RADIO_BUTTON;
      break;

    default:
      g_assert_not_reached ();
    }

  atk_object_set_role (accessible, a11y_role);
}

102 103 104 105 106 107 108 109 110 111 112 113 114
static void
update_visibility (GtkModelButton *button)
{
  gboolean has_icon;
  gboolean has_text;

  has_icon = gtk_image_get_storage_type (GTK_IMAGE (button->image)) != GTK_IMAGE_EMPTY;
  has_text = gtk_label_get_text (GTK_LABEL (button->label))[0] != '\0';

  gtk_widget_set_visible (button->image, has_icon);
  gtk_widget_set_visible (button->label, has_text && (!button->iconic || !has_icon));
}

115 116 117 118 119
static void
gtk_model_button_set_icon (GtkModelButton *button,
                           GIcon          *icon)
{
  gtk_image_set_from_gicon (GTK_IMAGE (button->image), icon, GTK_ICON_SIZE_MENU);
120
  update_visibility (button);
121 122 123 124 125 126
}

static void
gtk_model_button_set_text (GtkModelButton *button,
                           const gchar    *text)
{
127 128
  gtk_label_set_text_with_mnemonic (GTK_LABEL (button->label), text);
  update_visibility (button);
129 130 131 132 133 134 135 136 137
}

static void
gtk_model_button_set_accel (GtkModelButton *button,
                            const gchar    *accel)
{
  /* ignore */
}

138 139
static void gtk_model_button_update_state (GtkModelButton *button);

140 141 142 143 144
static void
gtk_model_button_set_toggled (GtkModelButton *button,
                              gboolean        toggled)
{
  button->toggled = toggled;
145
  gtk_model_button_update_state (button);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  gtk_widget_queue_draw (GTK_WIDGET (button));
}

static void
gtk_model_button_set_has_submenu (GtkModelButton *button,
                                  gboolean        has_submenu)
{
  button->has_submenu = has_submenu;
  gtk_widget_queue_resize (GTK_WIDGET (button));
}

static void
gtk_model_button_set_inverted (GtkModelButton *button,
                               gboolean        inverted)
{
  button->inverted = inverted;
  gtk_widget_queue_resize (GTK_WIDGET (button));
}

static void
gtk_model_button_set_centered (GtkModelButton *button,
167
                               gboolean        centered)
168
{
169 170
  button->centered = centered;
  gtk_widget_set_halign (button->box, button->centered ? GTK_ALIGN_CENTER : GTK_ALIGN_FILL);
171 172 173
  gtk_widget_queue_draw (GTK_WIDGET (button));
}

174 175 176 177
static void
gtk_model_button_set_iconic (GtkModelButton *button,
                             gboolean        iconic)
{
178 179
  GtkStyleContext *context;

180
  button->iconic = iconic;
181 182

  context = gtk_widget_get_style_context (GTK_WIDGET (button));
183 184
  if (iconic)
    {
185 186
      gtk_style_context_remove_class (context, GTK_STYLE_CLASS_MENUITEM);
      gtk_style_context_add_class (context, "image-button");
187 188 189 190
      gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NORMAL);
    }
  else
    {
191 192
      gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUITEM);
      gtk_style_context_remove_class (context, "image-button");
193 194 195 196 197 198 199
      gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
    }

  update_visibility (button);
  gtk_widget_queue_resize (GTK_WIDGET (button));
}

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
static void
gtk_model_button_set_property (GObject      *object,
                               guint         prop_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  GtkModelButton *button = GTK_MODEL_BUTTON (object);

  switch (prop_id)
    {
    case PROP_ACTION_ROLE:
      gtk_model_button_set_action_role (button, g_value_get_enum (value));
      break;

    case PROP_ICON:
      gtk_model_button_set_icon (button, g_value_get_object (value));
      break;

    case PROP_TEXT:
      gtk_model_button_set_text (button, g_value_get_string (value));
      break;

    case PROP_TOGGLED:
      gtk_model_button_set_toggled (button, g_value_get_boolean (value));
      break;

    case PROP_ACCEL:
      gtk_model_button_set_accel (button, g_value_get_string (value));
      break;

    case PROP_HAS_SUBMENU:
      gtk_model_button_set_has_submenu (button, g_value_get_boolean (value));
      break;

    case PROP_INVERTED:
      gtk_model_button_set_inverted (button, g_value_get_boolean (value));
      break;

    case PROP_CENTERED:
      gtk_model_button_set_centered (button, g_value_get_boolean (value));
      break;

242 243 244 245
    case PROP_ICONIC:
      gtk_model_button_set_iconic (button, g_value_get_boolean (value));
      break;

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    default:
      g_assert_not_reached ();
    }
}

static void
gtk_model_button_get_full_border (GtkModelButton *button,
                                  GtkBorder      *border,
                                  gint           *indicator)
{
  gint indicator_size, indicator_spacing;
  gint border_width;

  border_width = gtk_container_get_border_width (GTK_CONTAINER (button));

  gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &indicator_size, NULL);
  indicator_spacing = indicator_size / 8;

264 265 266 267
  border->left = border_width;
  border->right = border_width;
  border->top = border_width;
  border->bottom = border_width;
268

269 270 271 272
  if (button->iconic)
    *indicator = 0;
  else
    *indicator = indicator_size + 2 * indicator_spacing;
273 274
}

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
static gboolean
has_sibling_with_indicator (GtkWidget *button)
{
  GtkWidget *parent;
  gboolean has_indicator;
  GList *children, *l;
  GtkModelButton *sibling;

  has_indicator = FALSE;

  parent = gtk_widget_get_parent (button);
  children = gtk_container_get_children (GTK_CONTAINER (parent));

  for (l = children; l; l = l->next)
    {
      sibling = l->data;

      if (!GTK_IS_MODEL_BUTTON (sibling))
        continue;

      if (!gtk_widget_is_visible (GTK_WIDGET (sibling)))
        continue;

      if (!sibling->centered &&
          (sibling->has_submenu || sibling->role != GTK_MENU_TRACKER_ITEM_ROLE_NORMAL))
        {
          has_indicator = TRUE;
          break;
        }
    }

  g_list_free (children);

  return has_indicator;
}

static gboolean
needs_indicator (GtkModelButton *button)
{
  if (button->role != GTK_MENU_TRACKER_ITEM_ROLE_NORMAL)
    return TRUE;

  return has_sibling_with_indicator (GTK_WIDGET (button));
}

320 321 322 323 324 325 326 327 328 329 330
static void
gtk_model_button_get_preferred_width_for_height (GtkWidget *widget,
                                                 gint       height,
                                                 gint      *minimum,
                                                 gint      *natural)
{
  GtkModelButton *button = GTK_MODEL_BUTTON (widget);
  GtkWidget *child;
  GtkBorder border;
  gint indicator;

331

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
  gtk_model_button_get_full_border (button, &border, &indicator);

  child = gtk_bin_get_child (GTK_BIN (widget));
  if (child && gtk_widget_get_visible (child))
    {
      if (height > -1)
        height -= border.top + border.bottom;

      _gtk_widget_get_preferred_size_for_size (child,
                                               GTK_ORIENTATION_HORIZONTAL,
                                               height,
                                               minimum, natural,
                                               NULL, NULL);
    }
  else
    {
      *minimum = 0;
      *natural = 0;
    }

352 353 354 355 356 357 358 359 360 361 362 363 364
  *minimum += border.left + border.right;
  *natural += border.left + border.right;

  if (button->centered)
    {
      *minimum += 2 * indicator;
      *natural += 2 * indicator;
    }
  else if (needs_indicator (button))
    {
      *minimum += indicator;
      *natural += indicator;
    }
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
}

static void
gtk_model_button_get_preferred_width (GtkWidget *widget,
                                      gint      *minimum,
                                      gint      *natural)
{
  gtk_model_button_get_preferred_width_for_height (widget, -1, minimum, natural);
}

static void
gtk_model_button_get_preferred_height_and_baseline_for_width (GtkWidget          *widget,
                                                              gint                width,
                                                              gint               *minimum,
                                                              gint               *natural,
                                                              gint               *minimum_baseline,
                                                              gint               *natural_baseline)
{
  GtkModelButton *button = GTK_MODEL_BUTTON (widget);
  GtkWidget *child;
  GtkBorder border;
  gint indicator;

  gtk_model_button_get_full_border (button, &border, &indicator);

  child = gtk_bin_get_child (GTK_BIN (widget));
  if (child && gtk_widget_get_visible (child))
    {
      gint child_min, child_nat;
      gint child_min_baseline = -1, child_nat_baseline = -1;

      if (width > -1)
397 398 399 400 401 402 403
        {
          width -= border.left + border.right;
          if (button->centered)
            width -= 2 * indicator;
          else if (needs_indicator (button))
            width -= indicator;
        }
404

405 406 407
      gtk_widget_get_preferred_height_and_baseline_for_width (child, width,
                                                              &child_min, &child_nat,
                                                              &child_min_baseline, &child_nat_baseline);
408

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
      if (button->centered)
        {
          *minimum = MAX (2 * indicator, child_min);
          *natural = MAX (2 * indicator, child_nat);
        }
      else if (needs_indicator (button))
        {
          *minimum = MAX (indicator, child_min);
          *natural = MAX (indicator, child_nat);
        }
      else
        {
          *minimum = child_min;
          *natural = child_nat;
        }
424

425 426 427 428
      if (minimum_baseline && child_min_baseline >= 0)
        *minimum_baseline = child_min_baseline + border.top + (*minimum - child_min) / 2;
      if (natural_baseline && child_nat_baseline >= 0)
        *natural_baseline = child_nat_baseline + border.top + (*natural - child_nat) / 2;
429 430 431
    }
  else
    {
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
      if (button->centered)
        {
          *minimum = 2 * indicator;
          *natural = 2 * indicator;
        }
      else if (needs_indicator (button))
        {
          *minimum = indicator;
          *natural = indicator;
        }
      else
        {
          *minimum = 0;
          *natural = 0;
        }
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
    }

  *minimum += border.top + border.bottom;
  *natural += border.top + border.bottom;
}

static void
gtk_model_button_get_preferred_height_for_width (GtkWidget *widget,
                                                 gint       width,
                                                 gint      *minimum,
                                                 gint      *natural)
{
  gtk_model_button_get_preferred_height_and_baseline_for_width (widget, width,
                                                                minimum, natural,
                                                                NULL, NULL);
}

static void
gtk_model_button_get_preferred_height (GtkWidget *widget,
                                       gint      *minimum,
                                       gint      *natural)
{
  gtk_model_button_get_preferred_height_and_baseline_for_width (widget, -1,
                                                                minimum, natural,
                                                                NULL, NULL);
}

static gboolean
indicator_is_left (GtkWidget *widget)
{
  GtkModelButton *button = GTK_MODEL_BUTTON (widget);

  return ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL && !button->inverted) ||
          (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR && button->inverted));
   
}

static void
gtk_model_button_size_allocate (GtkWidget     *widget,
                                GtkAllocation *allocation)
{
  GtkModelButton *button = GTK_MODEL_BUTTON (widget);
  PangoContext *pango_context;
  PangoFontMetrics *metrics;
  GtkAllocation child_allocation;
  gint baseline;
  GtkWidget *child;

  gtk_widget_set_allocation (widget, allocation);

  if (gtk_widget_get_realized (widget))
    gdk_window_move_resize (gtk_button_get_event_window (GTK_BUTTON (widget)),
                            allocation->x, allocation->y,
                            allocation->width, allocation->height);

  child = gtk_bin_get_child (GTK_BIN (widget));
  if (child && gtk_widget_get_visible (child))
    {
      GtkBorder border;
      gint indicator;
          
      gtk_model_button_get_full_border (button, &border, &indicator);

510
      if (button->centered)
511 512 513 514
        {
          border.left += indicator;
          border.right += indicator;
        }
515 516 517 518 519 520 521
      else if (needs_indicator (button))
        {
          if (indicator_is_left (widget))
            border.left += indicator;
          else
            border.right += indicator;
        }
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
      child_allocation.x = allocation->x + border.left;
      child_allocation.y = allocation->y + border.top;
      child_allocation.width = allocation->width - border.left - border.right;
      child_allocation.height = allocation->height - border.top - border.bottom;

      baseline = gtk_widget_get_allocated_baseline (widget);
      if (baseline != -1)
        baseline -= border.top;
      gtk_widget_size_allocate_with_baseline (child, &child_allocation, baseline);
    }

  pango_context = gtk_widget_get_pango_context (widget);
  metrics = pango_context_get_metrics (pango_context,
                                       pango_context_get_font_description (pango_context),
                                       pango_context_get_language (pango_context));
  GTK_BUTTON (button)->priv->baseline_align =
    (double)pango_font_metrics_get_ascent (metrics) /
    (pango_font_metrics_get_ascent (metrics) + pango_font_metrics_get_descent (metrics));
  pango_font_metrics_unref (metrics);
}

static GtkStateFlags
get_button_state (GtkModelButton *model_button)
{
  GtkButton *button = GTK_BUTTON (model_button);
  GtkStateFlags state;

  state = gtk_widget_get_state_flags (GTK_WIDGET (model_button));

  state &= ~(GTK_STATE_FLAG_INCONSISTENT |
             GTK_STATE_FLAG_ACTIVE |
553
             GTK_STATE_FLAG_CHECKED |
554 555
             GTK_STATE_FLAG_PRELIGHT);

556 557
  if (model_button->toggled && !model_button->has_submenu)
    state |= GTK_STATE_FLAG_CHECKED;
558 559 560

  if (button->priv->activate_timeout ||
      (button->priv->button_down && button->priv->in_button))
561
    state |= GTK_STATE_FLAG_ACTIVE;
562 563 564 565 566 567 568

  if (button->priv->in_button)
    state |= GTK_STATE_FLAG_PRELIGHT;

  return state;
}

569 570 571 572 573 574
static void
gtk_model_button_update_state (GtkModelButton *button)
{
  gtk_widget_set_state_flags (GTK_WIDGET (button), get_button_state (button), TRUE);
}

575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
static gint
gtk_model_button_draw (GtkWidget *widget,
                       cairo_t   *cr)
{
  GtkModelButton *model_button = GTK_MODEL_BUTTON (widget);
  GtkButton *button = GTK_BUTTON (widget);
  GtkWidget *child;
  GtkStyleContext *context;
  GtkStateFlags state;
  gint border_width;
  gint x, y;
  gint width, height;
  gint indicator_size, indicator_spacing;
  gint baseline;

590 591 592 593 594 595 596 597 598 599 600 601
  state = get_button_state (model_button);

  context = gtk_widget_get_style_context (widget);
  gtk_style_context_save (context);
  gtk_style_context_set_state (context, state);

  if (model_button->iconic)
    {
      GTK_WIDGET_CLASS (gtk_model_button_parent_class)->draw (widget, cr);
      goto out;
    }

602 603 604 605 606 607 608 609
  width = gtk_widget_get_allocated_width (widget);
  height = gtk_widget_get_allocated_height (widget);
  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
  baseline = gtk_widget_get_allocated_baseline (widget);

  gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &indicator_size, NULL);
  indicator_spacing = indicator_size / 8;

610
  x = width - border_width - indicator_spacing - indicator_size;
611 612 613 614 615 616 617 618 619 620 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 649 650 651 652 653 654 655 656 657 658 659

  if (indicator_is_left (widget))
    x = width - (indicator_size + x);

  if (baseline == -1)
    y = (height - indicator_size) / 2;
  else
    y = CLAMP (baseline - indicator_size * button->priv->baseline_align,
               0, height - indicator_size);

  gtk_render_background (context, cr,
                         border_width, border_width,
                         width - 2 * border_width,
                         height - 2 * border_width);
  gtk_render_frame (context, cr,
                    border_width, border_width,
                    width - 2 * border_width,
                    height - 2 * border_width);

  if (model_button->has_submenu)
    {
      state = state & ~(GTK_STATE_FLAG_DIR_LTR|GTK_STATE_FLAG_DIR_RTL);
      if (indicator_is_left (widget))
        state = state | GTK_STATE_FLAG_DIR_RTL;
      else
        state = state | GTK_STATE_FLAG_DIR_LTR;

      gtk_style_context_set_state (context, state);
      gtk_style_context_add_class (context, GTK_STYLE_CLASS_EXPANDER);
      gtk_render_expander (context, cr, x, y, indicator_size, indicator_size);
    }
  else if (model_button->role == GTK_MENU_TRACKER_ITEM_ROLE_CHECK)
    {
      gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
      gtk_render_check (context, cr, x, y, indicator_size, indicator_size);
    }
  else if (model_button->role == GTK_MENU_TRACKER_ITEM_ROLE_RADIO)
    {
      gtk_style_context_add_class (context, GTK_STYLE_CLASS_RADIO);
      gtk_render_option (context, cr, x, y, indicator_size, indicator_size);
    }

  if (gtk_widget_has_visible_focus (widget))
    {
      GtkBorder border;
 
      gtk_style_context_get_border (context, state, &border);

      gtk_render_focus (context, cr,
660 661 662 663
                        border_width + border.left,
                        border_width + border.top,
                        width - 2 * border_width - border.left - border.right,
                        height - 2 * border_width - border.top - border.bottom);
664 665 666 667 668 669
    }

  child = gtk_bin_get_child (GTK_BIN (widget));
  if (child)
    gtk_container_propagate_draw (GTK_CONTAINER (widget), child, cr);

670 671 672
out:
  gtk_style_context_restore (context);

673 674 675
  return FALSE;
}

676 677 678
static void
gtk_model_button_pressed (GtkButton *button)
{
679 680
  GTK_BUTTON_CLASS (gtk_model_button_parent_class)->pressed (button);

681 682 683 684 685 686
  gtk_model_button_update_state (GTK_MODEL_BUTTON (button));
}

static void
gtk_model_button_released (GtkButton *button)
{
687 688 689
  GTK_BUTTON_CLASS (gtk_model_button_parent_class)->released (button);

  gtk_model_button_update_state (GTK_MODEL_BUTTON (button));
690 691 692 693 694 695 696 697 698
}

static void
gtk_model_button_enter_leave (GtkButton *button)
{
  gtk_model_button_update_state (GTK_MODEL_BUTTON (button));
  gtk_widget_queue_draw (GTK_WIDGET (button));
}

699 700 701 702 703
static void
gtk_model_button_class_init (GtkModelButtonClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
704
  GtkButtonClass *button_class = GTK_BUTTON_CLASS (class);
705 706 707 708 709 710 711 712 713 714 715

  object_class->set_property = gtk_model_button_set_property;

  widget_class->get_preferred_width = gtk_model_button_get_preferred_width;
  widget_class->get_preferred_width_for_height = gtk_model_button_get_preferred_width_for_height;
  widget_class->get_preferred_height = gtk_model_button_get_preferred_height;
  widget_class->get_preferred_height_for_width = gtk_model_button_get_preferred_height_for_width;
  widget_class->get_preferred_height_and_baseline_for_width = gtk_model_button_get_preferred_height_and_baseline_for_width;
  widget_class->size_allocate = gtk_model_button_size_allocate;
  widget_class->draw = gtk_model_button_draw;

716 717 718 719 720
  button_class->pressed = gtk_model_button_pressed;
  button_class->released = gtk_model_button_released;
  button_class->enter = gtk_model_button_enter_leave;
  button_class->leave = gtk_model_button_enter_leave;

721
  g_object_class_install_property (object_class, PROP_ACTION_ROLE,
722
                                   g_param_spec_enum ("action-role", "", "",
723 724 725 726
                                                      GTK_TYPE_MENU_TRACKER_ITEM_ROLE,
                                                      GTK_MENU_TRACKER_ITEM_ROLE_NORMAL,
                                                      G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (object_class, PROP_ICON,
727
                                   g_param_spec_object ("icon", "", "", G_TYPE_ICON,
728 729
                                                        G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (object_class, PROP_TEXT,
730
                                   g_param_spec_string ("text", "", "", NULL,
731 732
                                                        G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (object_class, PROP_TOGGLED,
733
                                   g_param_spec_boolean ("toggled", "", "", FALSE,
734 735
                                                         G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (object_class, PROP_ACCEL,
736
                                   g_param_spec_string ("accel", "", "", NULL,
737 738
                                                        G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (object_class, PROP_HAS_SUBMENU,
739
                                   g_param_spec_boolean ("has-submenu", "", "", FALSE,
740 741
                                                         G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (object_class, PROP_INVERTED,
742 743
                                   g_param_spec_boolean ("inverted", "", "", FALSE,

744 745
                                                         G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (object_class, PROP_CENTERED,
746 747 748 749
                                   g_param_spec_boolean ("centered", "", "", FALSE,
                                                         G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (object_class, PROP_ICONIC,
                                   g_param_spec_boolean ("iconic", "", "", TRUE,
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
                                                         G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));

  gtk_widget_class_set_accessible_role (GTK_WIDGET_CLASS (class), ATK_ROLE_PUSH_BUTTON);
}

static void
gtk_model_button_init (GtkModelButton *button)
{
  gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
  button->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
  g_object_set (button->box,
                "margin-start", 12,
                "margin-end", 12,
                "margin-top", 3,
                "margin-bottom", 3,
                NULL);
  gtk_widget_set_halign (button->box, GTK_ALIGN_FILL);
  gtk_widget_show (button->box);
  button->image = gtk_image_new ();
769
  g_object_set (button->image, "margin", 4, NULL);
770 771 772 773
  button->label = gtk_label_new ("");
  gtk_container_add (GTK_CONTAINER (button->box), button->image);
  gtk_container_add (GTK_CONTAINER (button->box), button->label);
  gtk_container_add (GTK_CONTAINER (button), button->box);
774 775
  gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (button)),
                               GTK_STYLE_CLASS_MENUITEM);
776 777 778 779 780 781 782
}

GtkWidget *
gtk_model_button_new (void)
{
  return g_object_new (GTK_TYPE_MODEL_BUTTON, NULL);
}