gtkcenterbox.c 13.3 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/*
 * Copyright (c) 2017 Timm Bäder <mail@baedert.org>
 *
 * 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
 *
 * Author: Timm Bäder <mail@baedert.org>
 */

/**
 * SECTION:gtkcenterbox
 * @Short_description: A centering container
 * @Title: GtkCenterBox
 * @See_also: #GtkBox
 *
 * The GtkCenterBox widget arranges three children in a horizontal
 * or vertical arrangement, keeping the middle child centered as well
 * as possible.
 *
 * To add children to GtkCenterBox, use gtk_center_box_set_start_widget(),
 * gtk_center_box_set_center_widget() and gtk_center_box_set_end_widget().
 *
 * # CSS nodes
 *
 * GtkCenterBox uses a single CSS node with name box.
 *
 * In horizontal orientation, the nodes of the children are always arranged
 * from left to right. So :first-child will always select the leftmost child,
 * regardless of text direction.
 *
 * In vertical orientation, the are arranged from top to bottom.
 */

#include "config.h"
#include "gtkcenterbox.h"
#include "gtkcssnodeprivate.h"
#include "gtkwidgetprivate.h"
49 50
#include "gtkorientable.h"
#include "gtkorientableprivate.h"
51
#include "gtkbuildable.h"
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

struct _GtkCenterBox
{
  GtkWidget parent_instance;

  GtkWidget *start_widget;
  GtkWidget *center_widget;
  GtkWidget *end_widget;
};


struct _GtkCenterBoxClass
{
  GtkWidgetClass parent_class;
};
67 68


69
static void gtk_center_box_buildable_init (GtkBuildableIface *iface);
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
G_DEFINE_TYPE_WITH_CODE (GtkCenterBox, gtk_center_box, GTK_TYPE_WIDGET,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                gtk_center_box_buildable_init))

static void
gtk_center_box_buildable_add_child (GtkBuildable  *buildable,
                                    GtkBuilder    *builder,
                                    GObject       *child,
                                    const gchar   *type)
{
  if (g_strcmp0 (type, "start") == 0)
    gtk_center_box_set_start_widget (GTK_CENTER_BOX (buildable), GTK_WIDGET (child));
  else if (g_strcmp0 (type, "center") == 0)
    gtk_center_box_set_center_widget (GTK_CENTER_BOX (buildable), GTK_WIDGET (child));
  else if (g_strcmp0 (type, "end") == 0)
    gtk_center_box_set_end_widget (GTK_CENTER_BOX (buildable), GTK_WIDGET (child));
  else
    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GTK_CENTER_BOX (buildable), type);
}

static void
gtk_center_box_buildable_init (GtkBuildableIface *iface)
{
  iface->add_child = gtk_center_box_buildable_add_child;
}
96 97 98 99 100 101 102 103 104 105 106

static void
gtk_center_box_measure (GtkWidget      *widget,
                        GtkOrientation  orientation,
                        int             for_size,
                        int            *minimum,
                        int            *natural,
                        int            *minimum_baseline,
                        int            *natural_baseline)
{
  GtkCenterBox *self = GTK_CENTER_BOX (widget);
107 108 109 110 111 112 113
  int min_baseline, nat_baseline;
  int start_min = 0;
  int start_nat = 0;
  int center_min = 0;
  int center_nat = 0;
  int end_min = 0;
  int end_nat = 0;
114

115
  if (self->start_widget)
116 117 118 119 120
    gtk_widget_measure (self->start_widget,
                        orientation,
                        for_size,
                        &start_min, &start_nat,
                        &min_baseline, &nat_baseline);
121

122
  if (self->center_widget)
123 124 125 126 127
    gtk_widget_measure (self->center_widget,
                        orientation,
                        for_size,
                        &center_min, &center_nat,
                        &min_baseline, &nat_baseline);
128 129

  if (self->end_widget)
130 131 132 133 134 135 136 137 138 139 140 141
    gtk_widget_measure (self->end_widget,
                        orientation,
                        for_size,
                        &end_min, &end_nat,
                        &min_baseline, &nat_baseline);

  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    {
      *minimum = start_min + center_min + end_min;
      *natural = center_nat + 2 * MAX (start_nat, end_nat);
    }
  else /* GTK_ORIENTATION_VERTICAL */
142
    {
143 144
      *minimum = MAX (start_min, MAX (center_min, end_min));
      *natural = MAX (start_nat, MAX (center_nat, end_nat));
145
    }
146 147 148 149 150 151 152 153 154 155
}

static void
gtk_center_box_size_allocate (GtkWidget     *widget,
                              GtkAllocation *allocation)
{
  GtkCenterBox *self = GTK_CENTER_BOX (widget);
  GtkAllocation child_allocation;
  GtkAllocation clip = *allocation;
  GtkAllocation child_clip;
156 157 158 159 160 161 162 163 164 165 166 167
  GtkWidget *left;
  GtkWidget *right;
  int left_size = 0;
  int left_min = 0;
  int left_nat = 0;
  int center_size = 0;
  int center_min = 0;
  int center_nat = 0;
  int right_size = 0;
  int right_min = 0;
  int right_nat = 0;
  int avail;
168 169 170
  gboolean left_expand = FALSE;
  gboolean center_expand = FALSE;
  gboolean right_expand = FALSE;
171 172 173 174 175 176 177 178 179 180 181

  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
    {
      left = self->start_widget;
      right = self->end_widget;
    }
  else
    {
      right = self->start_widget;
      left = self->end_widget;
    }
182 183 184

  GTK_WIDGET_CLASS (gtk_center_box_parent_class)->size_allocate (widget, allocation);

185 186 187 188 189 190 191 192 193 194 195 196 197 198
  if (left)
    gtk_widget_measure (left, GTK_ORIENTATION_HORIZONTAL,
                        allocation->height,
                        &left_min, &left_nat, NULL, NULL);

  if (right)
    gtk_widget_measure (right, GTK_ORIENTATION_HORIZONTAL,
                        allocation->height,
                        &right_min, &right_nat, NULL, NULL);

  if (self->center_widget)
    gtk_widget_measure (self->center_widget, GTK_ORIENTATION_HORIZONTAL,
                        allocation->height,
                        &center_min, &center_nat, NULL, NULL);
199

200 201 202 203 204
  if (self->center_widget)
    {
      center_size = CLAMP (allocation->width - (left_min + right_min), center_min, center_nat);
      center_expand = gtk_widget_get_hexpand (self->center_widget);
    }
205 206

  if (left)
207
    {
208
      avail = MIN ((allocation->width - center_size) / 2, allocation->width - (center_size + right_min));
209 210
      left_size = CLAMP (avail, left_min, left_nat);
      left_expand = gtk_widget_get_hexpand (left);
211
    }
212

213
  if (right)
214
    {
215
      avail = MIN ((allocation->width - center_size) / 2, allocation->width - (center_size + left_min));
216 217
      right_size = CLAMP (avail, right_min, right_nat);
      right_expand = gtk_widget_get_hexpand (right);
218
    }
219

220 221 222
  child_allocation.y = allocation->y;
  child_allocation.height = allocation->height;

223 224
  if (self->center_widget)
    {
225 226
      child_allocation.width = center_size;
      child_allocation.x = (allocation->width / 2) - (child_allocation.width / 2);
227 228

      /* Push in from start/end */
229 230 231 232
      if (left_size > child_allocation.x)
        child_allocation.x = left_size;
      else if (allocation->width - right_size < child_allocation.x + child_allocation.width)
        child_allocation.x = allocation->width - child_allocation.width - right_size;
233 234 235 236 237
      else if (center_expand)
        {
          child_allocation.width = allocation->width - 2 * MAX (left_size, right_size);
          child_allocation.x = (allocation->width / 2) - (child_allocation.width / 2);
        }
238 239 240 241 242

      child_allocation.x += allocation->x;
      gtk_widget_size_allocate (self->center_widget, &child_allocation);
      gtk_widget_get_clip (self->center_widget, &child_clip);
      gdk_rectangle_union (&clip, &clip, &child_clip);
243 244 245 246 247 248 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

      if (left_expand)
        left_size = child_allocation.x - allocation->x;

      if (right_expand)
        right_size = allocation->x + allocation->width - (child_allocation.x + child_allocation.width);
    }
  else
    {
      avail = allocation->width - (left_size + right_size);
      if (left_expand && right_expand)
        {
          left_size += avail / 2;
          right_size += avail / 2;
        }
      else if (left_expand)
        {
          left_size += avail;
        }
      else if (right_expand)
        {
          right_size += avail;
        }
    }

  if (left)
    {
      child_allocation.width = left_size;
      child_allocation.x = allocation->x;
      gtk_widget_size_allocate (left, &child_allocation);
      gtk_widget_get_clip (left, &child_clip);
      gdk_rectangle_union (&clip, &clip, &child_clip);
    }

  if (right)
    {
      child_allocation.width = right_size;
      child_allocation.x = allocation->x + allocation->width - child_allocation.width;
      gtk_widget_size_allocate (right, &child_allocation);
      gtk_widget_get_clip (right, &child_clip);
      gdk_rectangle_union (&clip, &clip, &child_clip);
284 285 286 287 288 289 290 291 292 293 294
    }

  gtk_widget_set_clip (widget, &clip);
}

static void
gtk_center_box_snapshot (GtkWidget   *widget,
                         GtkSnapshot *snapshot)
{
  GtkCenterBox *self = GTK_CENTER_BOX (widget);

295 296
  if (self->start_widget)
    gtk_widget_snapshot_child (widget, self->start_widget, snapshot);
297 298 299 300

  if (self->center_widget)
    gtk_widget_snapshot_child (widget, self->center_widget, snapshot);

301 302
  if (self->end_widget)
    gtk_widget_snapshot_child (widget, self->end_widget, snapshot);
303 304
}

305 306 307 308 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 335 336 337
static void
update_css_node_order (GtkCenterBox *self)
{
  GtkCssNode *parent;
  GtkCssNode *first;
  GtkCssNode *last;

  parent = gtk_widget_get_css_node (GTK_WIDGET (self));

  if (gtk_widget_get_direction (GTK_WIDGET (self)) == GTK_TEXT_DIR_LTR)
    {
      first = self->start_widget ? gtk_widget_get_css_node (self->start_widget) : NULL;
      last = self->end_widget ? gtk_widget_get_css_node (self->end_widget) : NULL;
    }
  else
    {
      first = self->end_widget ? gtk_widget_get_css_node (self->end_widget) : NULL;
      last = self->start_widget ? gtk_widget_get_css_node (self->start_widget) : NULL;
    }

  if (first)
    gtk_css_node_insert_after (parent, first, NULL);
  if (last)
    gtk_css_node_insert_before (parent, last, NULL);
}

static void
gtk_center_box_direction_changed (GtkWidget        *widget,
                                  GtkTextDirection  previous_direction)
{
  update_css_node_order (GTK_CENTER_BOX (widget));
}

338 339 340 341 342 343 344 345
static void
gtk_center_box_class_init (GtkCenterBoxClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  widget_class->measure = gtk_center_box_measure;
  widget_class->size_allocate = gtk_center_box_size_allocate;
  widget_class->snapshot = gtk_center_box_snapshot;
346
  widget_class->direction_changed = gtk_center_box_direction_changed;
347 348 349

  gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_FILLER);
  gtk_widget_class_set_css_name (widget_class, "box");
350 351 352 353 354 355 356 357 358 359 360 361
}

static void
gtk_center_box_init (GtkCenterBox *self)
{
  gtk_widget_set_has_window (GTK_WIDGET (self), FALSE);

  self->start_widget = NULL;
  self->center_widget = NULL;
  self->end_widget = NULL;
}

362 363 364 365 366 367 368 369 370
/**
 * gtk_center_box_new:
 *
 * Creates a new #GtkCenterBox.
 *
 * Returns: the new #GtkCenterBox.
 *
 * Since: 3.92
 */
371 372 373 374 375 376
GtkWidget *
gtk_center_box_new (void)
{
  return GTK_WIDGET (g_object_new (GTK_TYPE_CENTER_BOX, NULL));
}

377 378 379 380 381 382 383 384 385
/**
 * gtk_center_box_set_start_widget:
 * @self: a #GtkCenterBox
 * @child: the child
 *
 * Sets the start widget.
 *
 * Since: 3.92
 */
386 387 388 389 390 391 392 393 394 395
void
gtk_center_box_set_start_widget (GtkCenterBox *self,
                                 GtkWidget    *child)
{
  if (self->start_widget)
    gtk_widget_unparent (self->start_widget);

  self->start_widget = child;
  if (child)
    gtk_widget_set_parent (child, GTK_WIDGET (self));
396 397

  update_css_node_order (self);
398 399
}

400 401 402 403 404 405 406 407 408
/**
 * gtk_center_box_set_center_widget:
 * @self: a #GtkCenterBox
 * @child: the child
 *
 * Sets the center widget.
 *
 * Since: 3.92
 */
409 410
void
gtk_center_box_set_center_widget (GtkCenterBox *self,
411
                                 GtkWidget     *child)
412 413 414 415 416 417 418
{
  if (self->center_widget)
    gtk_widget_unparent (self->center_widget);

  self->center_widget = child;
  if (child)
    gtk_widget_set_parent (child, GTK_WIDGET (self));
419 420

  update_css_node_order (self);
421 422
}

423 424 425 426 427 428 429 430 431
/**
 * gtk_center_box_set_end_widget:
 * @self: a #GtkCenterBox
 * @child: the child
 *
 * Sets the end widget.
 *
 * Since: 3.92
 */
432 433 434 435 436 437 438 439 440 441
void
gtk_center_box_set_end_widget (GtkCenterBox *self,
                               GtkWidget    *child)
{
  if (self->end_widget)
    gtk_widget_unparent (self->end_widget);

  self->end_widget = child;
  if (child)
    gtk_widget_set_parent (child, GTK_WIDGET (self));
442 443

  update_css_node_order (self);
444 445
}

446 447 448 449 450 451 452 453 454 455
/**
 * gtk_center_box_get_start_widget:
 * @self: a #GtkCenterBox
 *
 * Gets the start widget.
 *
 * Returns: the start widget.
 *
 * Since: 3.92
 */
456 457 458 459 460 461
GtkWidget *
gtk_center_box_get_start_widget (GtkCenterBox *self)
{
  return self->start_widget;
}

462 463 464 465 466 467 468 469 470 471
/**
 * gtk_center_box_get_center_widget:
 * @self: a #GtkCenterBox
 *
 * Gets the center widget.
 *
 * Returns: the center widget.
 *
 * Since: 3.92
 */
472 473 474 475 476 477
GtkWidget *
gtk_center_box_get_center_widget (GtkCenterBox *self)
{
  return self->center_widget;
}

478 479 480 481 482 483 484 485 486 487
/**
 * gtk_center_box_get_end_widget:
 * @self: a #GtkCenterBox
 *
 * Gets the end widget.
 *
 * Returns: the end widget.
 *
 * Since: 3.92
 */
488 489 490 491 492
GtkWidget *
gtk_center_box_get_end_widget (GtkCenterBox *self)
{
  return self->end_widget;
}