gtkhidingbox.c 16.5 KB
Newer Older
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez 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 27 28 29 30 31
/*
 * Copyright (C) 2015 Rafał Lużyński <digitalfreak@lingonborough.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include "gtkhidingboxprivate.h"
#include "gtkintl.h"
#include "gtksizerequest.h"
#include "gtkbuildable.h"

struct _GtkHidingBoxPrivate
{
  GList *children;
  gint16 spacing;
32
  gint inverted :1;
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
};

static void
gtk_hiding_box_buildable_add_child (GtkBuildable *buildable,
                                    GtkBuilder   *builder,
                                    GObject      *child,
                                    const gchar  *type)
{
  if (!type)
    gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
  else
    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GTK_HIDING_BOX (buildable), type);
}

static void
gtk_hiding_box_buildable_init (GtkBuildableIface *iface)
{
  iface->add_child = gtk_hiding_box_buildable_add_child;
}

G_DEFINE_TYPE_WITH_CODE (GtkHidingBox, gtk_hiding_box, GTK_TYPE_CONTAINER,
                         G_ADD_PRIVATE (GtkHidingBox)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, gtk_hiding_box_buildable_init))

enum {
  PROP_0,
  PROP_SPACING,
60
  PROP_INVERTED,
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
61 62 63
  LAST_PROP
};

64 65
static GParamSpec *hiding_box_properties[LAST_PROP] = { NULL, };

Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
66 67 68 69 70 71 72 73 74 75 76 77 78
static void
gtk_hiding_box_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GtkHidingBox *box = GTK_HIDING_BOX (object);

  switch (prop_id)
    {
    case PROP_SPACING:
      gtk_hiding_box_set_spacing (box, g_value_get_int (value));
      break;
79 80 81
    case PROP_INVERTED:
      gtk_hiding_box_set_inverted (box, g_value_get_boolean (value));
      break;
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_hiding_box_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  GtkHidingBox *box = GTK_HIDING_BOX (object);
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);

  switch (prop_id)
    {
    case PROP_SPACING:
      g_value_set_int (value, priv->spacing);
      break;
102 103 104
    case PROP_INVERTED:
      g_value_set_boolean (value, priv->inverted);
      break;
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_hiding_box_add (GtkContainer *container,
                    GtkWidget    *widget)
{
  GtkHidingBox *box = GTK_HIDING_BOX (container);
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);

  priv->children = g_list_append (priv->children, widget);
  gtk_widget_set_parent (widget, GTK_WIDGET (box));
}

static void
gtk_hiding_box_remove (GtkContainer *container,
                       GtkWidget    *widget)
{
  GList *child;
  GtkHidingBox *box = GTK_HIDING_BOX (container);
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);

  for (child = priv->children; child != NULL; child = child->next)
    {
      if (child->data == widget)
        {
          gboolean was_visible = gtk_widget_get_visible (widget) &&
                                 gtk_widget_get_child_visible (widget);

          gtk_widget_unparent (widget);
          priv->children = g_list_delete_link (priv->children, child);

          if (was_visible)
            gtk_widget_queue_resize (GTK_WIDGET (container));

          break;
        }
    }
}

static void
gtk_hiding_box_forall (GtkContainer *container,
                       gboolean      include_internals,
                       GtkCallback   callback,
                       gpointer      callback_data)
{
  GtkHidingBox *box = GTK_HIDING_BOX (container);
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
  GtkWidget *child;
  GList *children;

  children = priv->children;
  while (children)
    {
      child = children->data;
      children = children->next;
      (* callback) (child, callback_data);
    }
}

static void
169 170 171 172 173
update_children_visibility (GtkHidingBox     *box,
                            GtkAllocation    *allocation,
                            GtkRequestedSize *sizes,
                            gint             *n_visible_children,
                            gint             *n_visible_children_expanding)
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
174 175 176
{
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
  GtkWidget *child_widget;
177
  GList *child;
178
  GtkRequestedSize *sizes_temp;
179 180
  gint i;
  gint children_size = -priv->spacing;
181
  GList *children;
182
  gboolean allocate_more_children = TRUE;
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
183

184 185
  *n_visible_children = 0;
  *n_visible_children_expanding = 0;
186 187 188 189
  children = g_list_copy (priv->children);
  sizes_temp = g_newa (GtkRequestedSize, g_list_length (priv->children));
  if (priv->inverted)
    children = g_list_reverse (children);
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
190 191

  /* Retrieve desired size for visible children. */
192
  for (i = 0, child = children; child != NULL; i++, child = child->next)
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
193 194
    {
      child_widget = GTK_WIDGET (child->data);
195 196 197
      if (!gtk_widget_get_visible (child_widget) || !allocate_more_children)
        {
          gtk_widget_set_child_visible (child_widget, FALSE);
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
198
          continue;
199
        }
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
200 201 202

      gtk_widget_get_preferred_width_for_height (child_widget,
                                                 allocation->height,
203 204
                                                 &sizes_temp[i].minimum_size,
                                                 &sizes_temp[i].natural_size);
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
205
      /* Assert the api is working properly */
206
      if (sizes_temp[i].minimum_size < 0)
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
207 208
        g_error ("GtkHidingBox child %s minimum width: %d < 0 for height %d",
                 gtk_widget_get_name (child_widget),
209
                 sizes_temp[i].minimum_size, allocation->height);
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
210

211
      if (sizes_temp[i].natural_size < sizes_temp[i].minimum_size)
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
212 213
        g_error ("GtkHidingBox child %s natural width: %d < minimum %d for height %d",
                 gtk_widget_get_name (child_widget),
214
                 sizes_temp[i].natural_size, sizes_temp[i].minimum_size,
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
215
                 allocation->height);
216

217 218
      children_size += sizes_temp[i].minimum_size + priv->spacing;
      sizes_temp[i].data = child_widget;
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
219

220 221 222 223 224 225 226
      if (children_size > allocation->width)
        {
          gtk_widget_set_child_visible (child_widget, FALSE);
          allocate_more_children = FALSE;
          continue;
        }

227
      if (gtk_widget_get_hexpand (child_widget))
228 229 230
        (*n_visible_children_expanding)++;
      (*n_visible_children)++;
      gtk_widget_set_child_visible (child_widget, TRUE);
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
231
    }
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

  for (i = 0; i < *n_visible_children; i++)
    {
      if (priv->inverted)
        {
          sizes[*n_visible_children - i - 1].minimum_size = sizes_temp[i].minimum_size;
          sizes[*n_visible_children - i - 1].natural_size = sizes_temp[i].natural_size;
        }
      else
        {
          sizes[i].minimum_size = sizes_temp[i].minimum_size;
          sizes[i].natural_size = sizes_temp[i].natural_size;
        }
    }

  g_list_free (children);
248
}
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
249

250 251 252 253 254 255 256 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
static void
gtk_hiding_box_size_allocate (GtkWidget     *widget,
                              GtkAllocation *allocation)
{
  GtkHidingBox *box = GTK_HIDING_BOX (widget);
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
  GtkTextDirection direction;
  GtkAllocation child_allocation;
  GtkRequestedSize *sizes;
  gint size = 0;
  gint extra = 0;
  gint n_extra_widgets = 0; /* Number of widgets that receive 1 extra px */
  gint x = 0;
  gint i;
  GList *child;
  GtkWidget *child_widget;
  gint spacing = priv->spacing;
  gint n_visible_children = 0;
  gint n_visible_children_expanding = 0;
  GtkAllocation clip, child_clip;

  gtk_widget_set_allocation (widget, allocation);

  sizes = g_newa (GtkRequestedSize, g_list_length (priv->children));
  update_children_visibility (box, allocation, sizes, &n_visible_children,
                              &n_visible_children_expanding);

  /* If there is no visible child, simply return. */
  if (n_visible_children == 0)
    return;

  direction = gtk_widget_get_direction (widget);

  /* Bring children up to allocation width first */
  size = gtk_distribute_natural_allocation (MAX (0, allocation->width), n_visible_children, sizes);
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
285
  /* Only now we can subtract the spacings */
286
  size -= (n_visible_children - 1) * spacing;
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
287

288
  if (n_visible_children > 1)
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
289
    {
290
      extra = size / MAX (1, n_visible_children_expanding);
291
      n_extra_widgets = size % n_visible_children;
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
292 293 294 295 296 297 298
    }

  x = allocation->x;
  for (i = 0, child = priv->children; child != NULL; child = child->next)
    {

      child_widget = GTK_WIDGET (child->data);
299
      if (!gtk_widget_get_child_visible (child_widget))
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
300 301 302 303
        continue;

      child_allocation.x = x;
      child_allocation.y = allocation->y;
304 305 306 307 308
      if (gtk_widget_get_hexpand (child_widget))
        child_allocation.width = sizes[i].minimum_size + extra;
      else
        child_allocation.width = sizes[i].minimum_size;

Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
      child_allocation.height = allocation->height;
      if (n_extra_widgets)
        {
          ++child_allocation.width;
          --n_extra_widgets;
        }
      if (direction == GTK_TEXT_DIR_RTL)
        child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;

      /* Let this child be visible */
      gtk_widget_size_allocate (child_widget, &child_allocation);
      x += child_allocation.width + spacing;
      ++i;
    }

  /*
   * Note: Here we ignore the "box-shadow" CSS property of the
   * hiding box because we don't use it.
   */
  clip = *allocation;
  if (gtk_widget_get_has_window (widget))
    clip.x = clip.y = 0;

  for (i = 0, child = priv->children; child != NULL; child = child->next)
    {
      child_widget = GTK_WIDGET (child->data);
335
      if (gtk_widget_get_child_visible (child_widget))
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
        {
          gtk_widget_get_clip (child_widget, &child_clip);
          gdk_rectangle_union (&child_clip, &clip, &clip);
        }
    }

  if (gtk_widget_get_has_window (widget))
    {
      clip.x += allocation->x;
      clip.y += allocation->y;
    }
  gtk_widget_set_clip (widget, &clip);
}

static void
gtk_hiding_box_get_preferred_width (GtkWidget *widget,
352 353
                                    gint      *minimum_width,
                                    gint      *natural_width)
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
354 355 356
{
  GtkHidingBox *box = GTK_HIDING_BOX (widget);
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
357 358
  gint child_minimum_width;
  gint child_natural_width;
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
359
  GList *child;
360
  gint n_visible_children;
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
361
  gboolean have_min = FALSE;
362
  GList *children;
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
363

364 365
  *minimum_width = 0;
  *natural_width = 0;
366

367 368 369 370
  children = g_list_copy (priv->children);
  if (priv->inverted)
    children = g_list_reverse (children);

371
  n_visible_children = 0;
372
  for (child = children; child != NULL; child = child->next)
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
373 374 375 376
    {
      if (!gtk_widget_is_visible (child->data))
        continue;

377
      ++n_visible_children;
378
      gtk_widget_get_preferred_width (child->data, &child_minimum_width, &child_natural_width);
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
379 380 381
      /* Minimum is a minimum of the first visible child */
      if (!have_min)
        {
382
          *minimum_width = child_minimum_width;
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
383 384 385
          have_min = TRUE;
        }
      /* Natural is a sum of all visible children */
386
      *natural_width += child_natural_width;
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
387 388 389
    }

  /* Natural must also include the spacing */
390
  if (priv->spacing && n_visible_children > 1)
391
    *natural_width += priv->spacing * (n_visible_children - 1);
392 393

  g_list_free (children);
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
394 395 396 397
}

static void
gtk_hiding_box_get_preferred_height (GtkWidget *widget,
398 399
                                     gint      *minimum_height,
                                     gint      *natural_height)
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
400 401 402
{
  GtkHidingBox *box = GTK_HIDING_BOX (widget);
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
403 404
  gint child_minimum_height;
  gint child_natural_height;
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
405 406
  GList *child;

407 408
  *minimum_height = 0;
  *natural_height = 0;
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
409 410 411 412 413
  for (child = priv->children; child != NULL; child = child->next)
    {
      if (!gtk_widget_is_visible (child->data))
        continue;

414 415 416
      gtk_widget_get_preferred_height (child->data, &child_minimum_height, &child_natural_height);
      *minimum_height = MAX (*minimum_height, child_minimum_height);
      *natural_height = MAX (*natural_height, child_natural_height);
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
417 418 419 420 421 422 423 424 425 426
    }
}

static void
gtk_hiding_box_init (GtkHidingBox *box)
{
  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);

  gtk_widget_set_has_window (GTK_WIDGET (box), FALSE);
  priv->spacing = 0;
427
  priv->inverted = FALSE;
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
}

static void
gtk_hiding_box_class_init (GtkHidingBoxClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);

  object_class->set_property = gtk_hiding_box_set_property;
  object_class->get_property = gtk_hiding_box_get_property;

  widget_class->size_allocate = gtk_hiding_box_size_allocate;
  widget_class->get_preferred_width = gtk_hiding_box_get_preferred_width;
  widget_class->get_preferred_height = gtk_hiding_box_get_preferred_height;

  container_class->add = gtk_hiding_box_add;
  container_class->remove = gtk_hiding_box_remove;
  container_class->forall = gtk_hiding_box_forall;

448 449 450 451 452 453 454
  hiding_box_properties[PROP_SPACING] =
           g_param_spec_int ("spacing",
                             _("Spacing"),
                             _("The amount of space between children"),
                             0, G_MAXINT, 0,
                             G_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);

455 456 457 458 459 460 461
  hiding_box_properties[PROP_INVERTED] =
           g_param_spec_int ("inverted",
                             _("Direction of hiding children inverted"),
                             P_("If false the container will start hiding widgets from the end when there is not enough space, and the oposite in case inverted is true."),
                             0, G_MAXINT, 0,
                             G_PARAM_READWRITE);

462
  g_object_class_install_properties (object_class, LAST_PROP, hiding_box_properties);
Carlos Soriano Sánchez's avatar
Carlos Soriano Sánchez committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
}

/**
 * gtk_hiding_box_new:
 *
 * Creates a new #GtkHidingBox.
 *
 * Returns: a new #GtkHidingBox.
 **/
GtkWidget *
gtk_hiding_box_new (void)
{
  return g_object_new (GTK_TYPE_HIDING_BOX, NULL);
}

/**
 * gtk_hiding_box_set_spacing:
 * @box: a #GtkHidingBox
 * @spacing: the number of pixels to put between children
 *
 * Sets the #GtkHidingBox:spacing property of @box, which is the
 * number of pixels to place between children of @box.
 */
void
gtk_hiding_box_set_spacing (GtkHidingBox *box,
                            gint          spacing)
{
  GtkHidingBoxPrivate *priv ;

  g_return_if_fail (GTK_IS_HIDING_BOX (box));

  priv = gtk_hiding_box_get_instance_private (box);

  if (priv->spacing != spacing)
    {
      priv->spacing = spacing;

      g_object_notify (G_OBJECT (box), "spacing");

      gtk_widget_queue_resize (GTK_WIDGET (box));
    }
}

/**
 * gtk_hiding_box_get_spacing:
 * @box: a #GtkHidingBox
 *
 * Gets the value set by gtk_hiding_box_set_spacing().
 *
 * Returns: spacing between children
 **/
gint
gtk_hiding_box_get_spacing (GtkHidingBox *box)
{
  GtkHidingBoxPrivate *priv ;

  g_return_val_if_fail (GTK_IS_HIDING_BOX (box), 0);

  priv = gtk_hiding_box_get_instance_private (box);

  return priv->spacing;
}

526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557

void
gtk_hiding_box_set_inverted (GtkHidingBox *box,
                             gboolean      inverted)
{
  GtkHidingBoxPrivate *priv ;

  g_return_if_fail (GTK_IS_HIDING_BOX (box));

  priv = gtk_hiding_box_get_instance_private (box);

  if (priv->inverted != inverted)
    {
      priv->inverted = inverted != FALSE;

      g_object_notify (G_OBJECT (box), "inverted");

      gtk_widget_queue_resize (GTK_WIDGET (box));
    }
}

gboolean
gtk_hiding_box_get_inverted (GtkHidingBox *box)
{
  GtkHidingBoxPrivate *priv ;

  g_return_val_if_fail (GTK_IS_HIDING_BOX (box), 0);

  priv = gtk_hiding_box_get_instance_private (box);

  return priv->inverted;
}