gtkmodelbutton.c 40.4 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
#include "gtkcssnodeprivate.h"
38
#include "gtkcsscustomgadgetprivate.h"
39
#include "gtkstylecontextprivate.h"
40
#include "gtkcontainerprivate.h"
41

Matthias Clasen's avatar
Matthias Clasen committed
42 43 44 45 46 47 48
/**
 * 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
49
 * be backed by a #GAction via the #GtkActionable:action-name property,
Matthias Clasen's avatar
Matthias Clasen committed
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 107 108
 * 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>
 * ]|
109 110 111 112 113
 *
 * # CSS nodes
 *
 * |[<!-- language="plain" -->
 * modelbutton
114
 * ├── <child>
115 116 117 118 119
 * ╰── check
 * ]|
 *
 * |[<!-- language="plain" -->
 * modelbutton
120
 * ├── <child>
121 122 123 124 125
 * ╰── radio
 * ]|
 *
 * |[<!-- language="plain" -->
 * modelbutton
126
 * ├── <child>
127 128 129 130 131 132 133 134 135
 * ╰── arrow
 * ]|
 *
 * GtkModelButton has a main CSS node with name modelbutton, and a subnode,
 * which will have the name check, radio or arrow, depending on the role
 * of the button and whether it has a menu name set.
 *
 * The subnode is positioned before or after the content nodes and gets the
 * .left or .right style class, depending on where it is located.
136 137 138 139 140 141 142 143 144 145
 *
 * |[<!-- language="plain" -->
 * button.model
 * ├── <child>
 * ╰── check
 * ]|
 *
 * Iconic model buttons (see #GtkModelButton::iconic) change the name of
 * their main node to button and add a .model style class to it. The indicator
 * subnode is invisible in this case.
Matthias Clasen's avatar
Matthias Clasen committed
146 147
 */

148 149 150
struct _GtkModelButton
{
  GtkButton parent_instance;
Matthias Clasen's avatar
Matthias Clasen committed
151

152 153 154
  GtkWidget *box;
  GtkWidget *image;
  GtkWidget *label;
155 156
  GtkCssGadget *gadget;
  GtkCssGadget *indicator_gadget;
157
  gboolean active;
158
  gboolean centered;
159
  gboolean inverted;
160
  gboolean iconic;
161
  gchar *menu_name;
162
  GtkButtonRole role;
163 164 165 166 167 168 169 170 171
};

typedef GtkButtonClass GtkModelButtonClass;

G_DEFINE_TYPE (GtkModelButton, gtk_model_button, GTK_TYPE_BUTTON)

enum
{
  PROP_0,
172
  PROP_ROLE,
173 174
  PROP_ICON,
  PROP_TEXT,
175
  PROP_ACTIVE,
176
  PROP_MENU_NAME,
177
  PROP_INVERTED,
178
  PROP_CENTERED,
179 180
  PROP_ICONIC,
  LAST_PROPERTY
181 182
};

183 184
static GParamSpec *properties[LAST_PROPERTY] = { NULL, };

185 186 187 188 189 190 191 192 193
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));
}

194 195 196 197 198 199 200
static void
gtk_model_button_update_state (GtkModelButton *button)
{
  GtkStateFlags state;

  state = gtk_widget_get_state_flags (GTK_WIDGET (button));

201 202
  gtk_css_node_set_state (gtk_css_gadget_get_node (button->gadget), state);

203 204 205 206
  if (button->role == GTK_BUTTON_ROLE_CHECK ||
      button->role == GTK_BUTTON_ROLE_RADIO)
    {
      state &= ~GTK_STATE_FLAG_CHECKED;
207

208 209 210 211 212 213 214 215 216 217 218 219
      if (button->active && !button->menu_name)
        state |= GTK_STATE_FLAG_CHECKED;
    }

  if (button->menu_name)
    {
      state = state & ~(GTK_STATE_FLAG_DIR_LTR|GTK_STATE_FLAG_DIR_RTL);
      if (indicator_is_left (GTK_WIDGET (button)))
        state = state | GTK_STATE_FLAG_DIR_RTL;
      else
        state = state | GTK_STATE_FLAG_DIR_LTR;
    }
220

221
  gtk_css_node_set_state (gtk_css_gadget_get_node (button->indicator_gadget), state);
222 223
}

224 225 226
static void
update_node_ordering (GtkModelButton *button)
{
227
  GtkCssNode *widget_node, *indicator_node, *node;
228 229

  widget_node = gtk_widget_get_css_node (GTK_WIDGET (button));
230
  indicator_node = gtk_css_gadget_get_node (button->indicator_gadget);
231 232 233

  if (indicator_is_left (GTK_WIDGET (button)))
    {
234 235
      gtk_css_gadget_add_class (button->indicator_gadget, GTK_STYLE_CLASS_LEFT);
      gtk_css_gadget_remove_class (button->indicator_gadget, GTK_STYLE_CLASS_RIGHT);
236 237

      node = gtk_css_node_get_first_child (widget_node);
238 239
      if (node != indicator_node)
        gtk_css_node_insert_before (widget_node, indicator_node, node);
240 241 242
    }
  else
    {
243 244
      gtk_css_gadget_remove_class (button->indicator_gadget, GTK_STYLE_CLASS_LEFT);
      gtk_css_gadget_add_class (button->indicator_gadget, GTK_STYLE_CLASS_RIGHT);
245 246

      node = gtk_css_node_get_last_child (widget_node);
247 248
      if (node != indicator_node)
        gtk_css_node_insert_after (widget_node, indicator_node, node);
249 250 251
    }
}

252 253 254 255 256
static void
gtk_model_button_state_flags_changed (GtkWidget     *widget,
                                      GtkStateFlags  previous_flags)
{
  gtk_model_button_update_state (GTK_MODEL_BUTTON (widget));
257 258

  GTK_WIDGET_CLASS (gtk_model_button_parent_class)->state_flags_changed (widget, previous_flags);
259
}
260

261
static void
262 263 264 265 266 267 268 269 270 271 272 273 274
gtk_model_button_direction_changed (GtkWidget        *widget,
                                    GtkTextDirection  previous_dir)
{
  GtkModelButton *button = GTK_MODEL_BUTTON (widget);

  gtk_model_button_update_state (button);
  update_node_ordering (button);

  GTK_WIDGET_CLASS (gtk_model_button_parent_class)->direction_changed (widget, previous_dir);
}

static void
update_node_name (GtkModelButton *button)
275 276 277
{
  AtkObject *accessible;
  AtkRole a11y_role;
278 279 280
  const gchar *indicator_name;
  gboolean indicator_visible;
  GtkCssNode *indicator_node;
281 282

  accessible = gtk_widget_get_accessible (GTK_WIDGET (button));
283
  switch (button->role)
284
    {
285
    case GTK_BUTTON_ROLE_NORMAL:
286
      a11y_role = ATK_ROLE_PUSH_BUTTON;
287 288
      if (button->menu_name)
        {
289 290
          indicator_name = I_("arrow");
          indicator_visible = TRUE;
291 292 293
        }
      else
        {
294 295
          indicator_name = I_("check");
          indicator_visible = FALSE;
296
        }
297 298
      break;

299
    case GTK_BUTTON_ROLE_CHECK:
300
      a11y_role = ATK_ROLE_CHECK_BOX;
301 302
      indicator_name = I_("check");
      indicator_visible = TRUE;
303 304
      break;

305
    case GTK_BUTTON_ROLE_RADIO:
306
      a11y_role = ATK_ROLE_RADIO_BUTTON;
307 308
      indicator_name = I_("radio");
      indicator_visible = TRUE;
309 310 311 312 313 314
      break;

    default:
      g_assert_not_reached ();
    }

315 316 317
  if (button->iconic)
    indicator_visible = FALSE;

318
  atk_object_set_role (accessible, a11y_role);
319 320 321 322

  indicator_node = gtk_css_gadget_get_node (button->indicator_gadget);
  gtk_css_node_set_name (indicator_node, indicator_name);
  gtk_css_node_set_visible (indicator_node, indicator_visible);
323 324 325 326 327 328 329 330 331 332 333 334
}

static void
gtk_model_button_set_role (GtkModelButton *button,
                           GtkButtonRole   role)
{
  if (role == button->role)
    return;

  button->role = role;

  update_node_name (button);
335 336 337 338

  gtk_model_button_update_state (button);
  gtk_widget_queue_draw (GTK_WIDGET (button));
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_ROLE]);
339 340
}

341 342 343 344 345 346 347 348 349
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';

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

354 355 356 357 358
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);
359
  update_visibility (button);
360
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_ICON]);
361 362 363 364 365 366
}

static void
gtk_model_button_set_text (GtkModelButton *button,
                           const gchar    *text)
{
367 368
  gtk_label_set_text_with_mnemonic (GTK_LABEL (button->label), text);
  update_visibility (button);
369
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_TEXT]);
370 371 372
}

static void
373 374
gtk_model_button_set_active (GtkModelButton *button,
                             gboolean        active)
375
{
376 377 378
  if (button->active == active)
    return;

379
  button->active = active;
380
  gtk_model_button_update_state (button);
381
  gtk_widget_queue_draw (GTK_WIDGET (button));
382
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_ACTIVE]);
383 384
}

385 386 387 388 389 390
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);
391 392

  update_node_name (button);
393
  gtk_model_button_update_state (button);
394 395

  if (menu_name)
396
    gtk_css_gadget_add_class (button->indicator_gadget, GTK_STYLE_CLASS_EXPANDER);
397
  else
398
    gtk_css_gadget_remove_class (button->indicator_gadget, GTK_STYLE_CLASS_EXPANDER);
399

400
  gtk_widget_queue_resize (GTK_WIDGET (button));
401
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_MENU_NAME]);
402 403
}

404 405 406 407
static void
gtk_model_button_set_inverted (GtkModelButton *button,
                               gboolean        inverted)
{
408 409 410
  if (button->inverted == inverted)
    return;

411
  button->inverted = inverted;
412 413
  gtk_model_button_update_state (button);
  update_node_ordering (button);
414
  gtk_widget_queue_resize (GTK_WIDGET (button));
415
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_INVERTED]);
416 417 418 419
}

static void
gtk_model_button_set_centered (GtkModelButton *button,
420
                               gboolean        centered)
421
{
422 423 424
  if (button->centered == centered)
    return;

425 426
  button->centered = centered;
  gtk_widget_set_halign (button->box, button->centered ? GTK_ALIGN_CENTER : GTK_ALIGN_FILL);
427
  gtk_widget_queue_draw (GTK_WIDGET (button));
428
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_CENTERED]);
429 430
}

431 432 433 434
static void
gtk_model_button_set_iconic (GtkModelButton *button,
                             gboolean        iconic)
{
435
  GtkCssNode *widget_node;
436
  GtkCssNode *indicator_node;
437

438 439 440
  if (button->iconic == iconic)
    return;

441
  button->iconic = iconic;
442

443
  widget_node = gtk_widget_get_css_node (GTK_WIDGET (button));
444
  indicator_node = gtk_css_gadget_get_node (button->indicator_gadget);
445 446
  if (iconic)
    {
447
      gtk_css_node_set_name (widget_node, I_("button"));
448
      gtk_css_gadget_add_class (button->gadget, "model");
449
      gtk_css_gadget_add_class (button->gadget, "image-button");
450
      gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NORMAL);
451
      gtk_css_node_set_visible (indicator_node, FALSE);
452 453 454
    }
  else
    {
455
      gtk_css_node_set_name (widget_node, I_("modelbutton"));
456
      gtk_css_gadget_remove_class (button->gadget, "model");
457
      gtk_css_gadget_remove_class (button->gadget, "image-button");
458
      gtk_button_set_relief (GTK_BUTTON (button), GTK_RELIEF_NONE);
459 460 461
      gtk_css_node_set_visible (indicator_node,
                                button->role != GTK_BUTTON_ROLE_NORMAL ||
                                button->menu_name == NULL);
462 463 464 465
    }

  update_visibility (button);
  gtk_widget_queue_resize (GTK_WIDGET (button));
466
  g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_ICONIC]);
467 468
}

469 470 471 472 473 474 475 476 477 478
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)
    {
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
    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;

495 496 497 498
    case PROP_ACTIVE:
      g_value_set_boolean (value, button->active);
      break;

499 500 501 502
    case PROP_MENU_NAME:
      g_value_set_string (value, button->menu_name);
      break;

503 504 505 506 507 508 509 510 511 512 513 514
    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;

515 516 517 518 519
    default:
      g_assert_not_reached ();
    }
}

520 521 522 523 524 525 526 527 528 529
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)
    {
530 531
    case PROP_ROLE:
      gtk_model_button_set_role (button, g_value_get_enum (value));
532 533 534 535 536 537 538 539 540 541
      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;

542 543
    case PROP_ACTIVE:
      gtk_model_button_set_active (button, g_value_get_boolean (value));
544 545
      break;

546 547 548 549
    case PROP_MENU_NAME:
      gtk_model_button_set_menu_name (button, g_value_get_string (value));
      break;

550 551 552 553 554 555 556 557
    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;

558 559 560 561
    case PROP_ICONIC:
      gtk_model_button_set_iconic (button, g_value_get_boolean (value));
      break;

562 563 564 565 566
    default:
      g_assert_not_reached ();
    }
}

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
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 &&
591
          (sibling->menu_name || sibling->role != GTK_BUTTON_ROLE_NORMAL))
592 593 594 595 596 597 598 599 600 601 602 603 604 605
        {
          has_indicator = TRUE;
          break;
        }
    }

  g_list_free (children);

  return has_indicator;
}

static gboolean
needs_indicator (GtkModelButton *button)
{
606
  if (button->role != GTK_BUTTON_ROLE_NORMAL)
607 608 609 610 611
    return TRUE;

  return has_sibling_with_indicator (GTK_WIDGET (button));
}

612 613 614 615 616 617
static void
gtk_model_button_get_preferred_width_for_height (GtkWidget *widget,
                                                 gint       height,
                                                 gint      *minimum,
                                                 gint      *natural)
{
618
  GtkCssGadget *gadget;
619

620 621
  if (GTK_MODEL_BUTTON (widget)->iconic)
    gadget = GTK_BUTTON (widget)->priv->gadget;
622
  else
623
    gadget = GTK_MODEL_BUTTON (widget)->gadget;
624

625 626 627 628 629
  gtk_css_gadget_get_preferred_size (gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     height,
                                     minimum, natural,
                                     NULL, NULL);
630 631 632 633 634 635 636
}

static void
gtk_model_button_get_preferred_width (GtkWidget *widget,
                                      gint      *minimum,
                                      gint      *natural)
{
637 638 639 640 641 642 643 644 645 646 647 648
  GtkCssGadget *gadget;

  if (GTK_MODEL_BUTTON (widget)->iconic)
    gadget = GTK_BUTTON (widget)->priv->gadget;
  else
    gadget = GTK_MODEL_BUTTON (widget)->gadget;

  gtk_css_gadget_get_preferred_size (gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     -1,
                                     minimum, natural,
                                     NULL, NULL);
649 650 651
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
652 653 654 655 656 657
gtk_model_button_get_preferred_height_and_baseline_for_width (GtkWidget *widget,
                                                              gint       width,
                                                              gint      *minimum,
                                                              gint      *natural,
                                                              gint      *minimum_baseline,
                                                              gint      *natural_baseline)
658
{
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
  GtkCssGadget *gadget;

  if (GTK_MODEL_BUTTON (widget)->iconic)
    gadget = GTK_BUTTON (widget)->priv->gadget;
  else
    gadget = GTK_MODEL_BUTTON (widget)->gadget;

  gtk_css_gadget_get_preferred_size (gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     width,
                                     minimum, natural,
                                     minimum_baseline, natural_baseline);
}

static void
gtk_model_button_get_preferred_height_for_width (GtkWidget *widget,
                                                 gint       width,
                                                 gint      *minimum,
                                                 gint      *natural)
{
  GtkCssGadget *gadget;

  if (GTK_MODEL_BUTTON (widget)->iconic)
    gadget = GTK_BUTTON (widget)->priv->gadget;
  else
    gadget = GTK_MODEL_BUTTON (widget)->gadget;

  gtk_css_gadget_get_preferred_size (gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     width,
                                     minimum, natural,
                                     NULL, NULL);
}

static void
gtk_model_button_get_preferred_height (GtkWidget *widget,
                                       gint      *minimum,
                                       gint      *natural)
{
  GtkCssGadget *gadget;
699

700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
  if (GTK_MODEL_BUTTON (widget)->iconic)
    gadget = GTK_BUTTON (widget)->priv->gadget;
  else
    gadget = GTK_MODEL_BUTTON (widget)->gadget;

  gtk_css_gadget_get_preferred_size (gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     -1,
                                     minimum, natural,
                                     NULL, NULL);
}

static void
gtk_model_button_measure_indicator (GtkCssGadget   *gadget,
                                    GtkOrientation  orientation,
                                    int             for_size,
                                    int            *minimum,
                                    int            *natural,
                                    int            *minimum_baseline,
                                    int            *natural_baseline,
                                    gpointer        data)
{
  gint indicator_size;
723

724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
  gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &indicator_size, NULL);

  *minimum = *natural = indicator_size;
}

static void
gtk_model_button_measure (GtkCssGadget   *gadget,
                          GtkOrientation  orientation,
                          int             for_size,
                          int            *minimum,
                          int            *natural,
                          int            *minimum_baseline,
                          int            *natural_baseline,
                          gpointer        data)
{
  GtkWidget *widget;
  GtkModelButton *button;
  GtkWidget *child;

  widget = gtk_css_gadget_get_owner (gadget);
  button = GTK_MODEL_BUTTON (widget);
745
  child = gtk_bin_get_child (GTK_BIN (widget));
746 747

  if (orientation == GTK_ORIENTATION_HORIZONTAL)
748
    {
749 750 751 752 753 754 755
      gint check_min, check_nat;

      gtk_css_gadget_get_preferred_size (button->indicator_gadget,
                                         GTK_ORIENTATION_HORIZONTAL,
                                         -1,
                                         &check_min, &check_nat,
                                         NULL, NULL);
756

757
      if (child && gtk_widget_get_visible (child))
758
        {
759 760 761 762 763 764 765 766 767 768
          _gtk_widget_get_preferred_size_for_size (child,
                                                   GTK_ORIENTATION_HORIZONTAL,
                                                   for_size,
                                                   minimum, natural,
                                                   minimum_baseline, natural_baseline);
        }
      else
        {
          *minimum = 0;
          *natural = 0;
769
        }
770

771 772
      if (button->centered)
        {
773 774
          *minimum += 2 * check_min;
          *natural += 2 * check_nat;
775 776 777
        }
      else if (needs_indicator (button))
        {
778 779
          *minimum += check_min;
          *natural += check_nat;
780
        }
781 782 783
    }
  else
    {
784 785 786 787 788 789 790 791 792
      gint check_min, check_nat;

      gtk_css_gadget_get_preferred_size (button->indicator_gadget,
                                         GTK_ORIENTATION_VERTICAL,
                                         -1,
                                         &check_min, &check_nat,
                                         NULL, NULL);

      if (child && gtk_widget_get_visible (child))
793
        {
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
          gint child_min, child_nat;
          gint child_min_baseline = -1, child_nat_baseline = -1;

          if (for_size > -1)
            {
              if (button->centered)
                for_size -= 2 * check_nat;
              else if (needs_indicator (button))
                for_size -= check_nat;
            }

          gtk_widget_get_preferred_height_and_baseline_for_width (child, for_size,
                                                                  &child_min, &child_nat,
                                                                  &child_min_baseline, &child_nat_baseline);

          if (button->centered)
            {
              *minimum = MAX (2 * check_min, child_min);
              *natural = MAX (2 * check_nat, child_nat);
            }
          else if (needs_indicator (button))
            {
              *minimum = MAX (check_min, child_min);
              *natural = MAX (check_nat, child_nat);
            }
          else
            {
              *minimum = child_min;
              *natural = child_nat;
            }

          if (minimum_baseline && child_min_baseline >= 0)
            *minimum_baseline = child_min_baseline + (*minimum - child_min) / 2;
          if (natural_baseline && child_nat_baseline >= 0)
            *natural_baseline = child_nat_baseline + (*natural - child_nat) / 2;
829 830 831
        }
      else
        {
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
          if (button->centered)
            {
              *minimum = 2 * check_min;
              *natural = 2 * check_nat;
            }
          else if (needs_indicator (button))
            {
              *minimum = check_min;
              *natural = check_nat;
            }
          else
            {
              *minimum = 0;
              *natural = 0;
            }
847
        }
848 849 850 851
    }
}

static void
852 853
gtk_model_button_size_allocate (GtkWidget     *widget,
                                GtkAllocation *allocation)
854
{
855 856
  GtkCssGadget *gadget;
  GdkRectangle clip;
857

858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
  if (GTK_MODEL_BUTTON (widget)->iconic)
    gadget = GTK_BUTTON (widget)->priv->gadget;
  else
    gadget = GTK_MODEL_BUTTON (widget)->gadget;

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

  gtk_css_gadget_allocate (gadget,
                           allocation,
                           gtk_widget_get_allocated_baseline (widget),
                           &clip);

  gtk_widget_set_clip (widget, &clip);
876 877 878
}

static void
879 880 881 882 883
gtk_model_button_allocate (GtkCssGadget        *gadget,
                           const GtkAllocation *allocation,
                           int                  baseline,
                           GtkAllocation       *out_clip,
                           gpointer             unused)
884
{
885 886
  GtkWidget *widget;
  GtkModelButton *button;
887 888 889 890
  PangoContext *pango_context;
  PangoFontMetrics *metrics;
  GtkAllocation child_allocation;
  GtkWidget *child;
891 892 893 894
  gint spacing;
  gint check_min_width, check_nat_width;
  gint check_min_height, check_nat_height;
  GdkRectangle check_clip;
895

896 897 898
  widget = gtk_css_gadget_get_owner (gadget);
  button = GTK_MODEL_BUTTON (widget);
  child = gtk_bin_get_child (GTK_BIN (widget));
899 900


901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
  spacing = 0;

  gtk_css_gadget_get_preferred_size (button->indicator_gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     -1,
                                     &check_min_width, &check_nat_width,
                                     NULL, NULL);
  gtk_css_gadget_get_preferred_size (button->indicator_gadget,
                                     GTK_ORIENTATION_VERTICAL,
                                     -1,
                                     &check_min_height, &check_nat_height,
                                     NULL, NULL);

  if (indicator_is_left (widget))
    child_allocation.x = allocation->x + spacing;
  else
    child_allocation.x = allocation->x + allocation->width - check_nat_width - spacing;
  child_allocation.y = allocation->y + (allocation->height - check_nat_height) / 2;
  child_allocation.width = check_nat_width;
  child_allocation.height = check_nat_height;

  gtk_css_gadget_allocate (button->indicator_gadget,
                           &child_allocation,
                           baseline,
                           &check_clip);

927 928
  if (child && gtk_widget_get_visible (child))
    {
929
      GtkBorder border = { 0, };
930

931
      if (button->centered)
932
        {
933 934
          border.left = check_nat_width;
          border.right = check_nat_width;
935
        }
936 937 938
      else if (needs_indicator (button))
        {
          if (indicator_is_left (widget))
939
            border.left += check_nat_width;
940
          else
941
            border.right += check_nat_width;
942
        }
943

944 945 946 947 948 949 950 951
      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;
952

953 954 955 956 957 958 959 960 961 962 963
      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);
964 965 966

  gtk_container_get_children_clip (GTK_CONTAINER (widget), out_clip);
  gdk_rectangle_union (out_clip, &check_clip, out_clip);
967 968 969 970 971 972
}

static gint
gtk_model_button_draw (GtkWidget *widget,
                       cairo_t   *cr)
{
973
  GtkCssGadget *gadget;
974

975 976 977 978
  if (GTK_MODEL_BUTTON (widget)->iconic)
    gadget = GTK_BUTTON (widget)->priv->gadget;
  else
    gadget = GTK_MODEL_BUTTON (widget)->gadget;
979

980
  gtk_css_gadget_draw (gadget, cr);
981

982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
  return FALSE;
}

static gboolean
gtk_model_button_render_indicator (GtkCssGadget *gadget,
                                   cairo_t      *cr,
                                   int           x,
                                   int           y,
                                   int           width,
                                   int           height,
                                   gpointer      data)
{
  GtkWidget *widget;
  GtkModelButton *button;
  GtkCssNode *indicator_node;
  GtkStyleContext *context;
998

999 1000 1001
  widget = gtk_css_gadget_get_owner (gadget);
  button = GTK_MODEL_BUTTON (widget);
  context = gtk_widget_get_style_context (widget);
1002

1003
  indicator_node = gtk_css_gadget_get_node (button->indicator_gadget);
1004

1005
  if (button->menu_name)
1006
    {
1007 1008
      gtk_style_context_save_to_node (context, indicator_node);
      gtk_render_expander (context, cr, x, y, width, height);
1009
      gtk_style_context_restore (context);
1010
    }
1011
  else if (button->role == GTK_BUTTON_ROLE_CHECK)
1012
    {
1013 1014
      gtk_style_context_save_to_node (context, indicator_node);
      gtk_render_check (context, cr, x, y, width, height);
1015
      gtk_style_context_restore (context);
1016
    }
1017
  else if (button->role == GTK_BUTTON_ROLE_RADIO)
1018
    {
1019 1020
      gtk_style_context_save_to_node (context, indicator_node);
      gtk_render_option (context, cr, x, y, width, height);
1021
      gtk_style_context_restore (context);
1022 1023
    }

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
  return FALSE;
}

static gboolean
gtk_model_button_render (GtkCssGadget *gadget,
                         cairo_t      *cr,
                         int           x,
                         int           y,
                         int           width,
                         int           height,
                         gpointer      data)
{
  GtkWidget *widget;
  GtkModelButton *button;
  GtkWidget *child;

  widget = gtk_css_gadget_get_owner (gadget);
  button = GTK_MODEL_BUTTON (widget);

  gtk_css_gadget_draw (button->indicator_gadget, cr);
1044 1045 1046 1047 1048

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

1049
  return gtk_widget_has_visible_focus (widget);
1050 1051
}

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
static void
gtk_model_button_destroy (GtkWidget *widget)
{
  GtkModelButton *model_button = GTK_MODEL_BUTTON (widget);

  g_clear_pointer (&model_button->menu_name, g_free);

  GTK_WIDGET_CLASS (gtk_model_button_parent_class)->destroy (widget);
}

1062 1063 1064 1065
static void
gtk_model_button_clicked (GtkButton *button)
{
  GtkModelButton *model_button = GTK_MODEL_BUTTON (button);
1066

1067 1068 1069 1070 1071 1072 1073 1074
  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);
    }
1075
  else if (model_button->role == GTK_BUTTON_ROLE_NORMAL)
1076 1077 1078 1079 1080 1081 1082 1083 1084
    {
      GtkWidget *popover;

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

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
static void
gtk_model_button_finalize (GObject *object)
{
  GtkModelButton *button = GTK_MODEL_BUTTON (object);

  g_clear_object (&button->indicator_gadget);
  g_clear_object (&button->gadget);

  G_OBJECT_CLASS (gtk_model_button_parent_class)->finalize (object);
}

1096 1097 1098 1099 1100
static void
gtk_model_button_class_init (GtkModelButtonClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
1101
  GtkButtonClass *button_class = GTK_BUTTON_CLASS (class);
1102

1103
  object_class->finalize = gtk_model_button_finalize;
1104
  object_class->get_property = gtk_model_button_get_property;
1105 1106 1107 1108 1109 1110 1111 1112 1113
  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;
1114
  widget_class->destroy = gtk_model_button_destroy;
1115
  widget_class->state_flags_changed = gtk_model_button_state_flags_changed;
1116
  widget_class->direction_changed = gtk_model_button_direction_changed;
1117

1118 1119
  button_class->clicked = gtk_model_button_clicked;

Matthias Clasen's avatar
Matthias Clasen committed
1120 1121 1122 1123 1124 1125 1126 1127 1128
  /**
   * 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
   */
1129
  properties[PROP_ROLE] =
Matthias Clasen's avatar
Matthias Clasen committed
1130 1131 1132
    g_param_spec_enum ("role",
                       P_("Role"),
                       P_("The role of this button"),
1133 1134 1135
                       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
1136 1137 1138 1139 1140 1141 1142 1143 1144

  /**
   * GtkModelButton:icon:
   *
   * A #GIcon that will be used if iconic appearance for the button is
   * desired.
   *
   * Since: 3.16
   */
1145
  properties[PROP_ICON] = 
Matthias Clasen's avatar
Matthias Clasen committed
1146 1147 1148
    g_param_spec_object ("icon",
                         P_("Icon"),
                         P_("The icon"),
1149 1150
                         G_TYPE_ICON,
                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
Matthias Clasen's avatar
Matthias Clasen committed
1151 1152 1153 1154 1155 1156 1157 1158

  /**
   * GtkModelButton:text:
   *
   * The label for the button.
   *
   * Since: 3.16
   */
1159
  properties[PROP_TEXT] =
Matthias Clasen's avatar
Matthias Clasen committed
1160 1161 1162
    g_param_spec_string ("text",
                         P_("Text"),
                         P_("The text"),
1163
                         "",
1164
                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
Matthias Clasen's avatar
Matthias Clasen committed
1165 1166 1167 1168 1169 1170 1171 1172 1173

  /**
   * GtkModelButton:active:
   *
   * The state of the button. This is reflecting the state of the associated
   * #GAction.
   *
   * Since: 3.16
   */
1174
  properties[PROP_ACTIVE] =
Matthias Clasen's avatar
Matthias Clasen committed
1175 1176 1177
    g_param_spec_boolean ("active",
                          P_("Active"),
                          P_("Active"),
1178 1179
                          FALSE,
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
Matthias Clasen's avatar
Matthias Clasen committed
1180 1181 1182 1183 1184 1185 1186 1187 1188

  /**
   * 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
   */
1189
  properties[PROP_MENU_NAME] =
Matthias Clasen's avatar
Matthias Clasen committed
1190 1191 1192
    g_param_spec_string ("menu-name",
                         P_("Menu name"),
                         P_("The name of the menu to open"),
1193 1194
                         NULL,
                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
Matthias Clasen's avatar
Matthias Clasen committed
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

  /**
   * 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
   */
1205
  properties[PROP_INVERTED] =
Matthias Clasen's avatar
Matthias Clasen committed
1206 1207 1208
    g_param_spec_boolean ("inverted",
                          P_("Inverted"),
                          P_("Whether the menu is a parent"),
1209 1210
                          FALSE,
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
Matthias Clasen's avatar
Matthias Clasen committed
1211 1212 1213 1214 1215 1216 1217 1218 1219

  /**
   * 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
   */
1220
  properties[PROP_CENTERED] =
Matthias Clasen's avatar
Matthias Clasen committed
1221 1222 1223
    g_param_spec_boolean ("centered",
                          P_("Centered"),
                          P_("Whether to center the contents"),
1224 1225
                          FALSE,
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
Matthias Clasen's avatar
Matthias Clasen committed
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235

  /**
   * 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
   */
1236
  properties[PROP_ICONIC] =
Matthias Clasen's avatar
Matthias Clasen committed
1237 1238 1239
    g_param_spec_boolean ("iconic",
                          P_("Iconic"),
                          P_("Whether to prefer the icon over text"),
1240
                          FALSE,
1241 1242
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_properties (object_class, LAST_PROPERTY, properties);
1243 1244

  gtk_widget_class_set_accessible_role (GTK_WIDGET_CLASS (class), ATK_ROLE_PUSH_BUTTON);
1245 1246 1247
  gtk_widget_class_set_css_name (GTK_WIDGET_CLASS (class), "modelbutton");
}

1248 1249 1250
static void
gtk_model_button_init (GtkModelButton *button)
{
1251 1252
  GtkCssNode *widget_node;

1253 1254
  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
1255 1256 1257 1258
  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);
1259 1260 1261
  gtk_widget_set_halign (button->box, GTK_ALIGN_FILL);
  gtk_widget_show (button->box);
  button->image = gtk_image_new ();
1262
  gtk_widget_set_no_show_all (button->image, TRUE);
1263
  g_object_set (button->image, "margin", 4, NULL);
1264
  button->label = gtk_label_new ("");
1265
  gtk_widget_set_no_show_all (button->label, TRUE);
1266 1267 1268
  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);
1269 1270

  widget_node = gtk_widget_get_css_node (GTK_WIDGET (button));
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
  button->gadget = gtk_css_custom_gadget_new_for_node (widget_node,
                                                       GTK_WIDGET (button),
                                                       gtk_model_button_measure,
                                                       gtk_model_button_allocate,
                                                       gtk_model_button_render,
                                                       NULL,
                                                       NULL);

  button->indicator_gadget = gtk_css_custom_gadget_new ("check",
                                                        GTK_WIDGET (button),
                                                        button->gadget,
                                                        NULL,
                                                        gtk_model_button_measure_indicator,
                                                        NULL,
                                                        gtk_model_button_render_indicator,
                                                        NULL,
                                                        NULL);
1288 1289
}

1290 1291 1292 1293 1294 1295 1296 1297 1298
/**
 * gtk_model_button_new:
 *
 * Creates a new GtkModelButton.
 *
 * Returns: the newly created #GtkModelButton widget
 *
 * Since: 3.16
 */
1299 1300 1301 1302 1303
GtkWidget *
gtk_model_button_new (void)
{
  return g_object_new (GTK_TYPE_MODEL_BUTTON, NULL);
}