gtkmodelbutton.c 30.2 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
#include "gtkstylecontext.h"
#include "gtktypebuiltins.h"
34 35
#include "gtkstack.h"
#include "gtkpopover.h"
36
#include "gtkintl.h"
37

Matthias Clasen's avatar
Matthias Clasen committed
38 39 40 41 42 43 44
/**
 * SECTION:gtkmodelbutton
 * @Short_description: A button that uses a GAction as model
 * @Title: GtkModelButton
 *
 * GtkModelButton is a button class that can use a #GAction as its model.
 * In contrast to #GtkToggleButton or #GtkRadioButton, which can also
45
 * be backed by a #GAction via the #GtkActionable:action-name property,
Matthias Clasen's avatar
Matthias Clasen committed
46 47 48 49 50 51 52 53 54 55 56 57 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
 * GtkModelButton will adapt its appearance according to the kind of
 * action it is backed by, and appear either as a plain, check or
 * radio button.
 *
 * Model buttons are used when popovers from a menu model with
 * gtk_popover_new_from_model(); they can also be used manually in
 * a #GtkPopoverMenu.
 *
 * When the action is specified via the #GtkActionable:action-name
 * and #GtkActionable:action-target properties, the role of the button
 * (i.e. whether it is a plain, check or radio button) is determined by
 * the type of the action and doesn't have to be explicitly specified
 * with the #GtkModelButton:role property.
 *
 * The content of the button is specified by the #GtkModelButton:text
 * and #GtkModelButton:icon properties.
 *
 * The appearance of model buttons can be influenced with the
 * #GtkModelButton:centered and #GtkModelButton:iconic properties.
 *
 * Model buttons have built-in support for submenus in #GtkPopoverMenu.
 * To make a GtkModelButton that opens a submenu when activated, set
 * the #GtkModelButton:menu-name property. To make a button that goes
 * back to the parent menu, you should set the #GtkModelButton:inverted
 * property to place the submenu indicator at the opposite side.
 *
 * # Example
 *
 * |[
 * <object class="GtkPopoverMenu">
 *   <child>
 *     <object class="GtkBox">
 *       <property name="visible">True</property>
 *       <property name="margin">10</property>
 *       <child>
 *         <object class="GtkModelButton">
 *           <property name="visible">True</property>
 *           <property name="action-name">view.cut</property>
 *           <property name="text" translatable="yes">Cut</property>
 *         </object>
 *       </child>
 *       <child>
 *         <object class="GtkModelButton">
 *           <property name="visible">True</property>
 *           <property name="action-name">view.copy</property>
 *           <property name="text" translatable="yes">Copy</property>
 *         </object>
 *       </child>
 *       <child>
 *         <object class="GtkModelButton">
 *           <property name="visible">True</property>
 *           <property name="action-name">view.paste</property>
 *           <property name="text" translatable="yes">Paste</property>
 *         </object>
 *       </child>
 *     </object>
 *   </child>
 * </object>
 * ]|
 */

107 108 109
struct _GtkModelButton
{
  GtkButton parent_instance;
Matthias Clasen's avatar
Matthias Clasen committed
110

111 112 113
  GtkWidget *box;
  GtkWidget *image;
  GtkWidget *label;
114
  gboolean active;
115
  gboolean centered;
116
  gboolean inverted;
117
  gboolean iconic;
118
  gchar *menu_name;
119
  GtkButtonRole role;
120 121 122 123 124 125 126 127 128
};

typedef GtkButtonClass GtkModelButtonClass;

G_DEFINE_TYPE (GtkModelButton, gtk_model_button, GTK_TYPE_BUTTON)

enum
{
  PROP_0,
129
  PROP_ROLE,
130 131
  PROP_ICON,
  PROP_TEXT,
132
  PROP_ACTIVE,
133
  PROP_MENU_NAME,
134
  PROP_INVERTED,
135
  PROP_CENTERED,
136 137
  PROP_ICONIC,
  LAST_PROPERTY
138 139
};

140 141
static GParamSpec *properties[LAST_PROPERTY] = { NULL, };

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
static void
gtk_model_button_update_state (GtkModelButton *button)
{
  GtkStateFlags state;

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

  state = gtk_widget_get_state_flags (GTK_WIDGET (button));

  state &= ~GTK_STATE_FLAG_CHECKED;

  if (button->active && !button->menu_name)
    state |= GTK_STATE_FLAG_CHECKED;

  gtk_widget_set_state_flags (GTK_WIDGET (button), state, TRUE);
}


161
static void
162 163
gtk_model_button_set_role (GtkModelButton *button,
                           GtkButtonRole   role)
164 165 166 167 168 169 170 171 172 173 174 175
{
  AtkObject *accessible;
  AtkRole a11y_role;

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

  button->role = role;

  accessible = gtk_widget_get_accessible (GTK_WIDGET (button));
  switch (role)
    {
176
    case GTK_BUTTON_ROLE_NORMAL:
177 178 179
      a11y_role = ATK_ROLE_PUSH_BUTTON;
      break;

180
    case GTK_BUTTON_ROLE_CHECK:
181 182 183
      a11y_role = ATK_ROLE_CHECK_BOX;
      break;

184
    case GTK_BUTTON_ROLE_RADIO:
185 186 187 188 189 190 191 192
      a11y_role = ATK_ROLE_RADIO_BUTTON;
      break;

    default:
      g_assert_not_reached ();
    }

  atk_object_set_role (accessible, a11y_role);
193 194 195 196

  gtk_model_button_update_state (button);
  gtk_widget_queue_draw (GTK_WIDGET (button));
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_ROLE]);
197 198
}

199 200 201 202 203 204 205 206 207
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';

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

212 213 214 215 216
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);
217
  update_visibility (button);
218
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_ICON]);
219 220 221 222 223 224
}

static void
gtk_model_button_set_text (GtkModelButton *button,
                           const gchar    *text)
{
225 226
  gtk_label_set_text_with_mnemonic (GTK_LABEL (button->label), text);
  update_visibility (button);
227
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_TEXT]);
228 229 230
}

static void
231 232
gtk_model_button_set_active (GtkModelButton *button,
                             gboolean        active)
233
{
234 235 236
  if (button->active == active)
    return;

237
  button->active = active;
238
  gtk_model_button_update_state (button);
239
  gtk_widget_queue_draw (GTK_WIDGET (button));
240
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_ACTIVE]);
241 242
}

243 244 245 246 247 248 249 250
static void
gtk_model_button_set_menu_name (GtkModelButton *button,
                                const gchar    *menu_name)
{
  g_free (button->menu_name);
  button->menu_name = g_strdup (menu_name);
  gtk_model_button_update_state (button);
  gtk_widget_queue_resize (GTK_WIDGET (button));
251
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_MENU_NAME]);
252 253
}

254 255 256 257
static void
gtk_model_button_set_inverted (GtkModelButton *button,
                               gboolean        inverted)
{
258 259 260
  if (button->inverted == inverted)
    return;

261 262
  button->inverted = inverted;
  gtk_widget_queue_resize (GTK_WIDGET (button));
263
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_INVERTED]);
264 265 266 267
}

static void
gtk_model_button_set_centered (GtkModelButton *button,
268
                               gboolean        centered)
269
{
270 271 272
  if (button->centered == centered)
    return;

273 274
  button->centered = centered;
  gtk_widget_set_halign (button->box, button->centered ? GTK_ALIGN_CENTER : GTK_ALIGN_FILL);
275
  gtk_widget_queue_draw (GTK_WIDGET (button));
276
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_CENTERED]);
277 278
}

279 280 281 282
static void
gtk_model_button_set_iconic (GtkModelButton *button,
                             gboolean        iconic)
{
283 284
  GtkStyleContext *context;

285 286 287
  if (button->iconic == iconic)
    return;

288
  button->iconic = iconic;
289 290

  context = gtk_widget_get_style_context (GTK_WIDGET (button));
291 292
  if (iconic)
    {
293 294
      gtk_style_context_remove_class (context, GTK_STYLE_CLASS_MENUITEM);
      gtk_style_context_add_class (context, "image-button");
295 296 297 298
      gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NORMAL);
    }
  else
    {
299 300
      gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUITEM);
      gtk_style_context_remove_class (context, "image-button");
301 302 303 304 305
      gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
    }

  update_visibility (button);
  gtk_widget_queue_resize (GTK_WIDGET (button));
306
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_ICONIC]);
307 308
}

309 310 311 312 313 314 315 316 317 318
static void
gtk_model_button_get_property (GObject    *object,
                               guint       prop_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  GtkModelButton *button = GTK_MODEL_BUTTON (object);

  switch (prop_id)
    {
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    case PROP_ROLE:
      g_value_set_enum (value, button->role);
      break;

    case PROP_ICON:
      {
        GIcon *icon;
        gtk_image_get_gicon (GTK_IMAGE (button->image), &icon, NULL);
        g_value_set_object (value, icon);
      }
      break;

    case PROP_TEXT:
      g_value_set_string (value, gtk_label_get_text (GTK_LABEL (button->label)));
      break;

335 336 337 338
    case PROP_ACTIVE:
      g_value_set_boolean (value, button->active);
      break;

339 340 341 342
    case PROP_MENU_NAME:
      g_value_set_string (value, button->menu_name);
      break;

343 344 345 346 347 348 349 350 351 352 353 354
    case PROP_INVERTED:
      g_value_set_boolean (value, button->inverted);
      break;

    case PROP_CENTERED:
      g_value_set_boolean (value, button->centered);
      break;

    case PROP_ICONIC:
      g_value_set_boolean (value, button->iconic);
      break;

355 356 357 358 359
    default:
      g_assert_not_reached ();
    }
}

360 361 362 363 364 365 366 367 368 369
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)
    {
370 371
    case PROP_ROLE:
      gtk_model_button_set_role (button, g_value_get_enum (value));
372 373 374 375 376 377 378 379 380 381
      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;

382 383
    case PROP_ACTIVE:
      gtk_model_button_set_active (button, g_value_get_boolean (value));
384 385
      break;

386 387 388 389
    case PROP_MENU_NAME:
      gtk_model_button_set_menu_name (button, g_value_get_string (value));
      break;

390 391 392 393 394 395 396 397
    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;

398 399 400 401
    case PROP_ICONIC:
      gtk_model_button_set_iconic (button, g_value_get_boolean (value));
      break;

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    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;

420 421 422 423
  border->left = border_width;
  border->right = border_width;
  border->top = border_width;
  border->bottom = border_width;
424

425 426 427 428
  if (button->iconic)
    *indicator = 0;
  else
    *indicator = indicator_size + 2 * indicator_spacing;
429 430
}

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
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 &&
455
          (sibling->menu_name || sibling->role != GTK_BUTTON_ROLE_NORMAL))
456 457 458 459 460 461 462 463 464 465 466 467 468 469
        {
          has_indicator = TRUE;
          break;
        }
    }

  g_list_free (children);

  return has_indicator;
}

static gboolean
needs_indicator (GtkModelButton *button)
{
470
  if (button->role != GTK_BUTTON_ROLE_NORMAL)
471 472 473 474 475
    return TRUE;

  return has_sibling_with_indicator (GTK_WIDGET (button));
}

476 477 478 479 480 481 482 483 484 485 486
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;

487

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
  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;
    }

508 509 510 511 512 513 514 515 516 517 518 519 520
  *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;
    }
521 522 523 524 525 526 527 528 529 530 531
}

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
Matthias Clasen's avatar
Matthias Clasen committed
532 533 534 535 536 537
gtk_model_button_get_preferred_height_and_baseline_for_width (GtkWidget *widget,
                                                              gint       width,
                                                              gint      *minimum,
                                                              gint      *natural,
                                                              gint      *minimum_baseline,
                                                              gint      *natural_baseline)
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
{
  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)
553 554 555 556 557 558 559
        {
          width -= border.left + border.right;
          if (button->centered)
            width -= 2 * indicator;
          else if (needs_indicator (button))
            width -= indicator;
        }
560

561 562 563
      gtk_widget_get_preferred_height_and_baseline_for_width (child, width,
                                                              &child_min, &child_nat,
                                                              &child_min_baseline, &child_nat_baseline);
564

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
      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;
        }
580

581 582 583 584
      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;
585 586 587
    }
  else
    {
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
      if (button->centered)
        {
          *minimum = 2 * indicator;
          *natural = 2 * indicator;
        }
      else if (needs_indicator (button))
        {
          *minimum = indicator;
          *natural = indicator;
        }
      else
        {
          *minimum = 0;
          *natural = 0;
        }
603 604 605 606 607 608 609 610 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 660 661 662 663 664 665
    }

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

666
      if (button->centered)
667 668 669 670
        {
          border.left += indicator;
          border.right += indicator;
        }
671 672 673 674 675 676 677
      else if (needs_indicator (button))
        {
          if (indicator_is_left (widget))
            border.left += indicator;
          else
            border.right += indicator;
        }
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
      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 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;
  gint border_width;
  gint x, y;
  gint width, height;
  gint indicator_size, indicator_spacing;
  gint baseline;

713
  if (model_button->iconic)
Matthias Clasen's avatar
Matthias Clasen committed
714
    return GTK_WIDGET_CLASS (gtk_model_button_parent_class)->draw (widget, cr);
715

716
  context = gtk_widget_get_style_context (widget);
717 718 719 720 721 722 723 724
  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;

725
  x = width - border_width - indicator_spacing - indicator_size;
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744

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

745
  if (model_button->menu_name)
746
    {
747 748 749 750
      GtkStateFlags state;

      gtk_style_context_save (context);
      state = gtk_style_context_get_state (context);
751 752 753 754 755 756 757 758 759
      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);
760
      gtk_style_context_restore (context);
761
    }
762
  else if (model_button->role == GTK_BUTTON_ROLE_CHECK)
763
    {
764
      gtk_style_context_save (context);
765 766
      gtk_style_context_add_class (context, GTK_STYLE_CLASS_CHECK);
      gtk_render_check (context, cr, x, y, indicator_size, indicator_size);
767
      gtk_style_context_restore (context);
768
    }
769
  else if (model_button->role == GTK_BUTTON_ROLE_RADIO)
770
    {
771
      gtk_style_context_save (context);
772 773
      gtk_style_context_add_class (context, GTK_STYLE_CLASS_RADIO);
      gtk_render_option (context, cr, x, y, indicator_size, indicator_size);
774
      gtk_style_context_restore (context);
775 776 777 778 779 780
    }

  if (gtk_widget_has_visible_focus (widget))
    {
      GtkBorder border;
 
781
      gtk_style_context_get_border (context, gtk_style_context_get_state (context), &border);
782 783

      gtk_render_focus (context, cr,
784 785 786 787
                        border_width + border.left,
                        border_width + border.top,
                        width - 2 * border_width - border.left - border.right,
                        height - 2 * border_width - border.top - border.bottom);
788 789 790 791 792 793 794 795 796
    }

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

  return FALSE;
}

797 798 799 800 801 802 803 804 805 806 807 808
static void
gtk_model_button_clicked (GtkButton *button)
{
  GtkModelButton *model_button = GTK_MODEL_BUTTON (button);
  if (model_button->menu_name != NULL)
    {
      GtkWidget *stack;

      stack = gtk_widget_get_ancestor (GTK_WIDGET (button), GTK_TYPE_STACK);
      if (stack != NULL)
        gtk_stack_set_visible_child_name (GTK_STACK (stack), model_button->menu_name);
    }
809
  else if (model_button->role == GTK_BUTTON_ROLE_NORMAL)
810 811 812 813 814 815 816 817 818
    {
      GtkWidget *popover;

      popover = gtk_widget_get_ancestor (GTK_WIDGET (button), GTK_TYPE_POPOVER);
      if (popover != NULL)
        gtk_widget_hide (popover);
    }
}

819 820 821 822 823
static void
gtk_model_button_class_init (GtkModelButtonClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
824
  GtkButtonClass *button_class = GTK_BUTTON_CLASS (class);
825

826
  object_class->get_property = gtk_model_button_get_property;
827 828 829 830 831 832 833 834 835 836
  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;

837 838
  button_class->clicked = gtk_model_button_clicked;

Matthias Clasen's avatar
Matthias Clasen committed
839 840 841 842 843 844 845 846 847
  /**
   * GtkModelButton:role:
   *
   * Specifies whether the button is a plain, check or radio button.
   * When #GtkActionable:action-name is set, the role will be determined
   * from the action and does not have to be set explicitly.
   *
   * Since: 3.16
   */
848
  properties[PROP_ROLE] =
Matthias Clasen's avatar
Matthias Clasen committed
849 850 851
    g_param_spec_enum ("role",
                       P_("Role"),
                       P_("The role of this button"),
852 853 854
                       GTK_TYPE_BUTTON_ROLE,
                       GTK_BUTTON_ROLE_NORMAL,
                       G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
Matthias Clasen's avatar
Matthias Clasen committed
855 856 857 858 859 860 861 862 863

  /**
   * GtkModelButton:icon:
   *
   * A #GIcon that will be used if iconic appearance for the button is
   * desired.
   *
   * Since: 3.16
   */
864
  properties[PROP_ICON] = 
Matthias Clasen's avatar
Matthias Clasen committed
865 866 867
    g_param_spec_object ("icon",
                         P_("Icon"),
                         P_("The icon"),
868 869
                         G_TYPE_ICON,
                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
Matthias Clasen's avatar
Matthias Clasen committed
870 871 872 873 874 875 876 877

  /**
   * GtkModelButton:text:
   *
   * The label for the button.
   *
   * Since: 3.16
   */
878
  properties[PROP_TEXT] =
Matthias Clasen's avatar
Matthias Clasen committed
879 880 881
    g_param_spec_string ("text",
                         P_("Text"),
                         P_("The text"),
882
                         "",
883
                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
Matthias Clasen's avatar
Matthias Clasen committed
884 885 886 887 888 889 890 891 892

  /**
   * GtkModelButton:active:
   *
   * The state of the button. This is reflecting the state of the associated
   * #GAction.
   *
   * Since: 3.16
   */
893
  properties[PROP_ACTIVE] =
Matthias Clasen's avatar
Matthias Clasen committed
894 895 896
    g_param_spec_boolean ("active",
                          P_("Active"),
                          P_("Active"),
897 898
                          FALSE,
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
Matthias Clasen's avatar
Matthias Clasen committed
899 900 901 902 903 904 905 906 907

  /**
   * GtkModelButton:menu-name:
   *
   * The name of a submenu to open when the button is activated.
   * If this is set, the button should not have an action associated with it.
   *
   * Since: 3.16
   */
908
  properties[PROP_MENU_NAME] =
Matthias Clasen's avatar
Matthias Clasen committed
909 910 911
    g_param_spec_string ("menu-name",
                         P_("Menu name"),
                         P_("The name of the menu to open"),
912 913
                         NULL,
                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
Matthias Clasen's avatar
Matthias Clasen committed
914 915 916 917 918 919 920 921 922 923

  /**
   * GtkModelButton:inverted:
   *
   * Whether to show the submenu indicator at the opposite side than normal.
   * This property should be set for model buttons that 'go back' to a parent
   * menu.
   *
   * Since: 3.16
   */
924
  properties[PROP_INVERTED] =
Matthias Clasen's avatar
Matthias Clasen committed
925 926 927
    g_param_spec_boolean ("inverted",
                          P_("Inverted"),
                          P_("Whether the menu is a parent"),
928 929
                          FALSE,
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
Matthias Clasen's avatar
Matthias Clasen committed
930 931 932 933 934 935 936 937 938

  /**
   * GtkModelButton:centered:
   *
   * Wether to render the button contents centered instead of left-aligned.
   * This property should be set for title-like items.
   *
   * Since: 3.16
   */
939
  properties[PROP_CENTERED] =
Matthias Clasen's avatar
Matthias Clasen committed
940 941 942
    g_param_spec_boolean ("centered",
                          P_("Centered"),
                          P_("Whether to center the contents"),
943 944
                          FALSE,
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
Matthias Clasen's avatar
Matthias Clasen committed
945 946 947 948 949 950 951 952 953 954

  /**
   * GtkModelButton:iconic:
   *
   * If this property is set, the button will show an icon if one is set.
   * If no icon is set, the text will be used. This is typically used for
   * horizontal sections of linked buttons.
   *
   * Since: 3.16
   */
955
  properties[PROP_ICONIC] =
Matthias Clasen's avatar
Matthias Clasen committed
956 957 958
    g_param_spec_boolean ("iconic",
                          P_("Iconic"),
                          P_("Whether to prefer the icon over text"),
959
                          FALSE,
960 961
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_properties (object_class, LAST_PROPERTY, properties);
962 963 964 965 966 967 968 969 970

  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);
Matthias Clasen's avatar
Matthias Clasen committed
971 972 973 974
  gtk_widget_set_margin_start (button->box, 12);
  gtk_widget_set_margin_end (button->box, 12);
  gtk_widget_set_margin_top (button->box, 3);
  gtk_widget_set_margin_bottom (button->box, 3);
975 976 977
  gtk_widget_set_halign (button->box, GTK_ALIGN_FILL);
  gtk_widget_show (button->box);
  button->image = gtk_image_new ();
978
  gtk_widget_set_no_show_all (button->image, TRUE);
979
  g_object_set (button->image, "margin", 4, NULL);
980
  button->label = gtk_label_new ("");
981
  gtk_widget_set_no_show_all (button->label, TRUE);
982 983 984
  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);
985 986
  gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (button)),
                               GTK_STYLE_CLASS_MENUITEM);
987 988
}

989 990 991 992 993 994 995 996 997
/**
 * gtk_model_button_new:
 *
 * Creates a new GtkModelButton.
 *
 * Returns: the newly created #GtkModelButton widget
 *
 * Since: 3.16
 */
998 999 1000 1001 1002
GtkWidget *
gtk_model_button_new (void)
{
  return g_object_new (GTK_TYPE_MODEL_BUTTON, NULL);
}