gtkactionbar.c 17.9 KB
Newer Older
William Jon McCann's avatar
William Jon McCann committed
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
/*
 * Copyright (c) 2013 - 2014 Red Hat, Inc.
 *
 * This program 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 License, or (at your
 * option) any later version.
 *
 * This program 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 program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "config.h"

#include "gtkactionbar.h"
#include "gtkintl.h"
#include "gtkaccessible.h"
#include "gtkbuildable.h"
#include "gtktypebuiltins.h"
27
#include "gtkbox.h"
William Jon McCann's avatar
William Jon McCann committed
28
#include "gtkrevealer.h"
29 30 31
#include "gtkcsscustomgadgetprivate.h"
#include "gtkwidgetprivate.h"
#include "gtkcontainerprivate.h"
William Jon McCann's avatar
William Jon McCann committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

#include <string.h>

/**
 * SECTION:gtkactionbar
 * @Short_description: A full width bar for presenting contextual actions
 * @Title: GtkActionBar
 * @See_also: #GtkBox
 *
 * GtkActionBar is designed to present contextual actions. It is
 * expected to be displayed below the content and expand horizontally
 * to fill the area.
 *
 * It allows placing children at the start or the end. In addition, it
 * contains an internal centered box which is centered with respect to
 * the full width of the box, even if the children at either side take
 * up different amounts of space.
49 50 51 52
 *
 * # CSS nodes
 *
 * GtkActionBar has a single CSS node with name actionbar.
William Jon McCann's avatar
William Jon McCann committed
53 54 55 56
 */

struct _GtkActionBarPrivate
{
57
  GtkWidget *box;
William Jon McCann's avatar
William Jon McCann committed
58
  GtkWidget *revealer;
59
  GtkCssGadget *gadget;
William Jon McCann's avatar
William Jon McCann committed
60 61 62 63 64 65 66 67
};

enum {
  CHILD_PROP_0,
  CHILD_PROP_PACK_TYPE,
  CHILD_PROP_POSITION
};

68 69
static void gtk_action_bar_finalize (GObject *object);
static void gtk_action_bar_buildable_interface_init (GtkBuildableIface *iface);
William Jon McCann's avatar
William Jon McCann committed
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 109 110 111 112

G_DEFINE_TYPE_WITH_CODE (GtkActionBar, gtk_action_bar, GTK_TYPE_BIN,
                         G_ADD_PRIVATE (GtkActionBar)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                gtk_action_bar_buildable_interface_init))

static void
gtk_action_bar_show (GtkWidget *widget)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (widget));

  GTK_WIDGET_CLASS (gtk_action_bar_parent_class)->show (widget);

  gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), TRUE);
}

static void
child_revealed (GObject *object, GParamSpec *pspec, GtkWidget *widget)
{
  GTK_WIDGET_CLASS (gtk_action_bar_parent_class)->hide (widget);
  g_signal_handlers_disconnect_by_func (object, child_revealed, widget);
  g_object_notify (G_OBJECT (widget), "visible");
}

static void
gtk_action_bar_hide (GtkWidget *widget)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (widget));

  g_signal_connect_object (priv->revealer, "notify::child-revealed",
                            G_CALLBACK (child_revealed), widget, 0);
  gtk_revealer_set_reveal_child (GTK_REVEALER (priv->revealer), FALSE);
}

static void
gtk_action_bar_add (GtkContainer *container,
                    GtkWidget    *child)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (container));

  /* When constructing the widget, we want the revealer to be added
   * as the first child of the bar, as an implementation detail.
   * After that, the child added by the application should be added
113
   * to box.
William Jon McCann's avatar
William Jon McCann committed
114 115
   */

116
  if (priv->box == NULL)
William Jon McCann's avatar
William Jon McCann committed
117 118
    GTK_CONTAINER_CLASS (gtk_action_bar_parent_class)->add (container, child);
  else
119
    gtk_container_add (GTK_CONTAINER (priv->box), child);
William Jon McCann's avatar
William Jon McCann committed
120 121 122 123 124 125 126 127 128 129 130
}

static void
gtk_action_bar_remove (GtkContainer *container,
                       GtkWidget    *child)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (container));

  if (child == priv->revealer)
    GTK_CONTAINER_CLASS (gtk_action_bar_parent_class)->remove (container, child);
  else
131
    gtk_container_remove (GTK_CONTAINER (priv->box), child);
William Jon McCann's avatar
William Jon McCann committed
132 133
}

134 135 136 137 138 139 140 141 142 143
static void
gtk_action_bar_forall (GtkContainer *container,
                       gboolean      include_internals,
                       GtkCallback   callback,
                       gpointer      callback_data)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (container));

  if (include_internals)
    (* callback) (priv->revealer, callback_data);
144
 
145 146
  if (priv->box)
    gtk_container_forall (GTK_CONTAINER (priv->box), callback, callback_data);
147 148
}

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
static void
gtk_action_bar_map (GtkWidget *widget)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (widget));

  gtk_widget_set_mapped (widget, TRUE);
  gtk_widget_map (priv->revealer);
}

static void
gtk_action_bar_unmap (GtkWidget *widget)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (widget));

  gtk_widget_set_mapped (widget, FALSE);
  gtk_widget_unmap (priv->revealer);
}

167 168 169 170 171 172 173 174 175 176 177 178 179 180
static void
gtk_action_bar_destroy (GtkWidget *widget)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (widget));

  if (priv->revealer)
    {
      gtk_widget_destroy (priv->revealer);
      priv->revealer = NULL;
    }

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

William Jon McCann's avatar
William Jon McCann committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
static GType
gtk_action_bar_child_type (GtkContainer *container)
{
  return GTK_TYPE_WIDGET;
}

static void
gtk_action_bar_get_child_property (GtkContainer *container,
                                   GtkWidget    *child,
                                   guint         property_id,
                                   GValue       *value,
                                   GParamSpec   *pspec)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (container));

  if (child == priv->revealer)
    g_param_value_set_default (pspec, value);
  else
199
    gtk_container_child_get_property (GTK_CONTAINER (priv->box),
William Jon McCann's avatar
William Jon McCann committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
                                      child,
                                      pspec->name,
                                      value);
}

static void
gtk_action_bar_set_child_property (GtkContainer *container,
                                   GtkWidget    *child,
                                   guint         property_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (container));

  if (child != priv->revealer)
215
    gtk_container_child_set_property (GTK_CONTAINER (priv->box),
William Jon McCann's avatar
William Jon McCann committed
216 217 218 219 220
                                      child,
                                      pspec->name,
                                      value);
}

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
static gboolean
gtk_action_bar_render (GtkCssGadget *gadget,
                       cairo_t      *cr,
                       int           x,
                       int           y,
                       int           width,
                       int           height,
                       gpointer      data)
{
  GTK_WIDGET_CLASS (gtk_action_bar_parent_class)->draw (gtk_css_gadget_get_owner (gadget), cr);

  return FALSE;
}

static gboolean
gtk_action_bar_draw (GtkWidget *widget,
                     cairo_t   *cr)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (widget));

  gtk_css_gadget_draw (priv->gadget, cr);

  return FALSE;
}

static void
gtk_action_bar_allocate (GtkCssGadget        *gadget,
                         const GtkAllocation *allocation,
                         int                  baseline,
                         GtkAllocation       *out_clip,
                         gpointer             data)
{
  GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
Matthias Clasen's avatar
Matthias Clasen committed
254
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (widget));
255

Matthias Clasen's avatar
Matthias Clasen committed
256
  gtk_widget_size_allocate (priv->revealer, (GtkAllocation *)allocation);
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 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 320 321 322 323 324 325

  gtk_container_get_children_clip (GTK_CONTAINER (widget), out_clip);
}

static void
gtk_action_bar_size_allocate (GtkWidget     *widget,
                              GtkAllocation *allocation)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (widget));
  GtkAllocation clip;

  gtk_widget_set_allocation (widget, allocation);

  gtk_css_gadget_allocate (priv->gadget, allocation, gtk_widget_get_allocated_baseline (widget), &clip);

  gtk_widget_set_clip (widget, &clip);
}

static void
gtk_action_bar_measure (GtkCssGadget   *gadget,
                        GtkOrientation  orientation,
                        int             for_size,
                        int            *minimum,
                        int            *natural,
                        int            *minimum_baseline,
                        int            *natural_baseline,
                        gpointer        data)
{
  GtkWidget *widget = gtk_css_gadget_get_owner (gadget);
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (widget));

  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    gtk_widget_get_preferred_width_for_height (priv->revealer, for_size, minimum, natural);
  else
    gtk_widget_get_preferred_height_and_baseline_for_width (priv->revealer, for_size, minimum, natural, minimum_baseline, natural_baseline);
}

static void
gtk_action_bar_get_preferred_width_for_height (GtkWidget *widget,
                                               gint       height,
                                               gint      *minimum,
                                               gint      *natural)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (widget));

  gtk_css_gadget_get_preferred_size (priv->gadget,
                                     GTK_ORIENTATION_HORIZONTAL,
                                     height,
                                     minimum, natural,
                                     NULL, NULL);
}

static void
gtk_action_bar_get_preferred_height_and_baseline_for_width (GtkWidget *widget,
                                                            gint       width,
                                                            gint      *minimum,
                                                            gint      *natural,
                                                            gint      *minimum_baseline,
                                                            gint      *natural_baseline)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (GTK_ACTION_BAR (widget));

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

William Jon McCann's avatar
William Jon McCann committed
326 327 328
static void
gtk_action_bar_class_init (GtkActionBarClass *klass)
{
329
  GObjectClass *object_class;
William Jon McCann's avatar
William Jon McCann committed
330 331 332
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

333
  object_class = G_OBJECT_CLASS (klass);
William Jon McCann's avatar
William Jon McCann committed
334 335 336
  widget_class = GTK_WIDGET_CLASS (klass);
  container_class = GTK_CONTAINER_CLASS (klass);

337 338
  object_class->finalize = gtk_action_bar_finalize;

William Jon McCann's avatar
William Jon McCann committed
339 340
  widget_class->show = gtk_action_bar_show;
  widget_class->hide = gtk_action_bar_hide;
341 342
  widget_class->map = gtk_action_bar_map;
  widget_class->unmap = gtk_action_bar_unmap;
343
  widget_class->destroy = gtk_action_bar_destroy;
344 345 346 347
  widget_class->draw = gtk_action_bar_draw;
  widget_class->size_allocate = gtk_action_bar_size_allocate;
  widget_class->get_preferred_width_for_height = gtk_action_bar_get_preferred_width_for_height;
  widget_class->get_preferred_height_and_baseline_for_width = gtk_action_bar_get_preferred_height_and_baseline_for_width;
William Jon McCann's avatar
William Jon McCann committed
348 349 350

  container_class->add = gtk_action_bar_add;
  container_class->remove = gtk_action_bar_remove;
351
  container_class->forall = gtk_action_bar_forall;
William Jon McCann's avatar
William Jon McCann committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
  container_class->child_type = gtk_action_bar_child_type;
  container_class->set_child_property = gtk_action_bar_set_child_property;
  container_class->get_child_property = gtk_action_bar_get_child_property;

  gtk_container_class_install_child_property (container_class,
                                              CHILD_PROP_PACK_TYPE,
                                              g_param_spec_enum ("pack-type",
                                                                 P_("Pack type"),
                                                                 P_("A GtkPackType indicating whether the child is packed with reference to the start or end of the parent"),
                                                                 GTK_TYPE_PACK_TYPE, GTK_PACK_START,
                                                                 G_PARAM_READWRITE));
  gtk_container_class_install_child_property (container_class,
                                              CHILD_PROP_POSITION,
                                              g_param_spec_int ("position",
                                                                P_("Position"),
                                                                P_("The index of the child in the parent"),
                                                                -1, G_MAXINT, 0,
369
                                                                G_PARAM_READWRITE));
William Jon McCann's avatar
William Jon McCann committed
370 371

  gtk_widget_class_set_template_from_resource (widget_class, "/org/gtk/libgtk/ui/gtkactionbar.ui");
372
  gtk_widget_class_bind_template_child_internal_private (widget_class, GtkActionBar, box);
William Jon McCann's avatar
William Jon McCann committed
373 374 375
  gtk_widget_class_bind_template_child_internal_private (widget_class, GtkActionBar, revealer);

  gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_PANEL);
376
  gtk_widget_class_set_css_name (widget_class, "actionbar");
William Jon McCann's avatar
William Jon McCann committed
377 378 379 380 381 382 383
}

static void
gtk_action_bar_init (GtkActionBar *action_bar)
{
  GtkWidget *widget = GTK_WIDGET (action_bar);
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (action_bar);
384
  GtkCssNode *widget_node;
William Jon McCann's avatar
William Jon McCann committed
385 386 387 388 389 390

  gtk_widget_set_redraw_on_allocate (widget, TRUE);

  gtk_widget_init_template (GTK_WIDGET (action_bar));

  gtk_revealer_set_transition_type (GTK_REVEALER (priv->revealer), GTK_REVEALER_TRANSITION_TYPE_SLIDE_UP);
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

  widget_node = gtk_widget_get_css_node (GTK_WIDGET (action_bar));
  priv->gadget = gtk_css_custom_gadget_new_for_node (widget_node,
                                                     GTK_WIDGET (action_bar),
                                                     gtk_action_bar_measure,
                                                     gtk_action_bar_allocate,
                                                     gtk_action_bar_render,
                                                     NULL,
                                                     NULL);
}

static void
gtk_action_bar_finalize (GObject *object)
{
  GtkActionBar *action_bar = GTK_ACTION_BAR (object);
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (action_bar);

  g_clear_object (&priv->gadget);

  G_OBJECT_CLASS (gtk_action_bar_parent_class)->finalize (object);
William Jon McCann's avatar
William Jon McCann committed
411 412 413 414 415 416 417 418 419 420 421 422
}

static void
gtk_action_bar_buildable_add_child (GtkBuildable *buildable,
                                    GtkBuilder   *builder,
                                    GObject      *child,
                                    const gchar  *type)
{
  GtkActionBar *action_bar = GTK_ACTION_BAR (buildable);
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (action_bar);

  if (type && strcmp (type, "center") == 0)
423
    gtk_box_set_center_widget (GTK_BOX (priv->box), GTK_WIDGET (child));
William Jon McCann's avatar
William Jon McCann committed
424 425 426 427 428 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
  else if (!type)
    gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
  else
    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (action_bar, type);
}

static GtkBuildableIface *parent_buildable_iface;

static void
gtk_action_bar_buildable_interface_init (GtkBuildableIface *iface)
{
  parent_buildable_iface = g_type_interface_peek_parent (iface);
  iface->add_child = gtk_action_bar_buildable_add_child;
}

/**
 * gtk_action_bar_pack_start:
 * @action_bar: A #GtkActionBar
 * @child: the #GtkWidget to be added to @action_bar
 *
 * Adds @child to @action_bar, packed with reference to the
 * start of the @action_bar.
 *
 * Since: 3.12
 */
void
gtk_action_bar_pack_start (GtkActionBar *action_bar,
                           GtkWidget    *child)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (action_bar);

455
  gtk_box_pack_start (GTK_BOX (priv->box), child, FALSE, TRUE, 0);
William Jon McCann's avatar
William Jon McCann committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
}

/**
 * gtk_action_bar_pack_end:
 * @action_bar: A #GtkActionBar
 * @child: the #GtkWidget to be added to @action_bar
 *
 * Adds @child to @action_bar, packed with reference to the
 * end of the @action_bar.
 *
 * Since: 3.12
 */
void
gtk_action_bar_pack_end (GtkActionBar *action_bar,
                         GtkWidget    *child)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (action_bar);

474
  gtk_box_pack_end (GTK_BOX (priv->box), child, FALSE, TRUE, 0);
William Jon McCann's avatar
William Jon McCann committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
}

/**
 * gtk_action_bar_set_center_widget:
 * @action_bar: a #GtkActionBar
 * @center_widget: (allow-none): a widget to use for the center
 *
 * Sets the center widget for the #GtkActionBar.
 *
 * Since: 3.12
 */
void
gtk_action_bar_set_center_widget (GtkActionBar *action_bar,
                                  GtkWidget    *center_widget)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (action_bar);

492
  gtk_box_set_center_widget (GTK_BOX (priv->box), center_widget);
William Jon McCann's avatar
William Jon McCann committed
493 494 495 496 497 498 499 500
}

/**
 * gtk_action_bar_get_center_widget:
 * @action_bar: a #GtkActionBar
 *
 * Retrieves the center bar widget of the bar.
 *
501
 * Returns: (transfer none) (nullable): the center #GtkWidget or %NULL.
William Jon McCann's avatar
William Jon McCann committed
502 503 504 505 506 507 508 509 510 511
 *
 * Since: 3.12
 */
GtkWidget *
gtk_action_bar_get_center_widget (GtkActionBar *action_bar)
{
  GtkActionBarPrivate *priv = gtk_action_bar_get_instance_private (action_bar);

  g_return_val_if_fail (GTK_IS_ACTION_BAR (action_bar), NULL);

512
  return gtk_box_get_center_widget (GTK_BOX (priv->box));
William Jon McCann's avatar
William Jon McCann committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
}

/**
 * gtk_action_bar_new:
 *
 * Creates a new #GtkActionBar widget.
 *
 * Returns: a new #GtkActionBar
 *
 * Since: 3.12
 */
GtkWidget *
gtk_action_bar_new (void)
{
  return GTK_WIDGET (g_object_new (GTK_TYPE_ACTION_BAR, NULL));
}