hdy-header-bar.c 95.8 KB
Newer Older
Adrien Plazas's avatar
Adrien Plazas 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 Red Hat, Inc.
 * Copyright (C) 2019 Purism SPC
 *
 * 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
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

#include "config.h"
#include <glib/gi18n-lib.h>

#include "hdy-header-bar.h"

Adrien Plazas's avatar
Adrien Plazas committed
27
#include "hdy-animation-private.h"
28
#include "hdy-cairo-private.h"
Adrien Plazas's avatar
Adrien Plazas committed
29
#include "hdy-enums.h"
30
#include "hdy-window-handle-controller-private.h"
Adrien Plazas's avatar
Adrien Plazas committed
31 32 33 34 35
#include "gtkprogresstrackerprivate.h"
#include "gtk-window-private.h"

/**
 * SECTION:hdy-header-bar
36
 * @short_description: A box with a centered child.
Adrien Plazas's avatar
Adrien Plazas committed
37
 * @Title: HdyHeaderBar
38
 * @See_also: #GtkHeaderBar, #HdyApplicationWindow, #HdyTitleBar, #HdyViewSwitcher, #HdyWindow
Adrien Plazas's avatar
Adrien Plazas committed
39 40 41 42 43 44 45
 *
 * HdyHeaderBar is similar to #GtkHeaderBar but is designed to fix some of its
 * shortcomings for adaptive applications.
 *
 * HdyHeaderBar doesn't force the custom title widget to be vertically centered,
 * hence allowing it to fill up the whole height, which is e.g. needed for
 * #HdyViewSwitcher.
46
 *
Adrien Plazas's avatar
Adrien Plazas committed
47 48 49 50
 * When used in a mobile dialog, HdyHeaderBar will replace its window
 * decorations by a back button allowing to close it. It doesn't have to be its
 * direct child and you can use any complex contraption you like as the dialog's
 * titlebar.
51
 *
52 53 54 55 56
 * #HdyHeaderBar can be used in window's content area rather than titlebar, and
 * will still be draggable and will handle right click, middle click and double
 * click as expected from a titlebar. This is particularly useful with
 * #HdyWindow or #HdyApplicationWindow.
 *
57 58 59
 * # CSS nodes
 *
 * #HdyHeaderBar has a single CSS node with name headerbar.
Adrien Plazas's avatar
Adrien Plazas committed
60 61 62 63 64 65 66 67 68 69 70
 */

/**
 * HdyCenteringPolicy:
 * @HDY_CENTERING_POLICY_LOOSE: Keep the title centered when possible
 * @HDY_CENTERING_POLICY_STRICT: Keep the title centered at all cost
 */

#define DEFAULT_SPACING 6
#define MIN_TITLE_CHARS 5

71 72 73
#define MOBILE_WINDOW_WIDTH  400
#define MOBILE_WINDOW_HEIGHT 800

Adrien Plazas's avatar
Adrien Plazas committed
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
typedef struct {
  gchar *title;
  gchar *subtitle;
  GtkWidget *title_label;
  GtkWidget *subtitle_label;
  GtkWidget *label_box;
  GtkWidget *label_sizing_box;
  GtkWidget *subtitle_sizing_label;
  GtkWidget *custom_title;
  gint spacing;
  gboolean has_subtitle;

  GList *children;

  gboolean shows_wm_decorations;
  gchar *decoration_layout;
  gboolean decoration_layout_set;

  GtkWidget *titlebar_start_box;
  GtkWidget *titlebar_end_box;

  GtkWidget *titlebar_start_separator;
  GtkWidget *titlebar_end_separator;

  GtkWidget *titlebar_icon;

  guint tick_id;
  GtkProgressTracker tracker;
  gboolean first_frame_skipped;

  HdyCenteringPolicy centering_policy;
  guint transition_duration;
  gboolean interpolate_size;
107

108 109 110
  gboolean is_mobile_window;

  gulong window_size_allocated_id;
111 112

  HdyWindowHandleController *controller;
Adrien Plazas's avatar
Adrien Plazas committed
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 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 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 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 326 327 328 329
} HdyHeaderBarPrivate;

typedef struct _Child Child;
struct _Child
{
  GtkWidget *widget;
  GtkPackType pack_type;
};

enum {
  PROP_0,
  PROP_TITLE,
  PROP_SUBTITLE,
  PROP_HAS_SUBTITLE,
  PROP_CUSTOM_TITLE,
  PROP_SPACING,
  PROP_SHOW_CLOSE_BUTTON,
  PROP_DECORATION_LAYOUT,
  PROP_DECORATION_LAYOUT_SET,
  PROP_CENTERING_POLICY,
  PROP_TRANSITION_DURATION,
  PROP_TRANSITION_RUNNING,
  PROP_INTERPOLATE_SIZE,
  LAST_PROP
};

enum {
  CHILD_PROP_0,
  CHILD_PROP_PACK_TYPE,
  CHILD_PROP_POSITION
};

static GParamSpec *props[LAST_PROP] = { NULL, };

static void hdy_header_bar_buildable_init (GtkBuildableIface *iface);

G_DEFINE_TYPE_WITH_CODE (HdyHeaderBar, hdy_header_bar, GTK_TYPE_CONTAINER,
                         G_ADD_PRIVATE (HdyHeaderBar)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                hdy_header_bar_buildable_init));

static gboolean
hdy_header_bar_transition_cb (GtkWidget     *widget,
                              GdkFrameClock *frame_clock,
                              gpointer       user_data)
{
  HdyHeaderBar *self = HDY_HEADER_BAR (widget);
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);

  if (priv->first_frame_skipped)
    gtk_progress_tracker_advance_frame (&priv->tracker,
                                        gdk_frame_clock_get_frame_time (frame_clock));
  else
    priv->first_frame_skipped = TRUE;

  /* Finish the animation early if the widget isn't mapped anymore. */
  if (!gtk_widget_get_mapped (widget))
    gtk_progress_tracker_finish (&priv->tracker);

  gtk_widget_queue_resize (widget);

  if (gtk_progress_tracker_get_state (&priv->tracker) == GTK_PROGRESS_STATE_AFTER) {
    priv->tick_id = 0;
    g_object_notify_by_pspec (G_OBJECT (self), props[PROP_TRANSITION_RUNNING]);

    return FALSE;
  }

  return TRUE;
}

static void
hdy_header_bar_schedule_ticks (HdyHeaderBar *self)
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);

  if (priv->tick_id == 0) {
    priv->tick_id =
      gtk_widget_add_tick_callback (GTK_WIDGET (self), hdy_header_bar_transition_cb, self, NULL);
    g_object_notify_by_pspec (G_OBJECT (self), props[PROP_TRANSITION_RUNNING]);
  }
}

static void
hdy_header_bar_unschedule_ticks (HdyHeaderBar *self)
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);

  if (priv->tick_id != 0) {
    gtk_widget_remove_tick_callback (GTK_WIDGET (self), priv->tick_id);
    priv->tick_id = 0;
    g_object_notify_by_pspec (G_OBJECT (self), props[PROP_TRANSITION_RUNNING]);
  }
}

static void
hdy_header_bar_start_transition (HdyHeaderBar *self,
                                 guint         transition_duration)
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);
  GtkWidget *widget = GTK_WIDGET (self);

  if (gtk_widget_get_mapped (widget) &&
      priv->interpolate_size &&
      transition_duration != 0) {
    priv->first_frame_skipped = FALSE;
    hdy_header_bar_schedule_ticks (self);
    gtk_progress_tracker_start (&priv->tracker,
                                priv->transition_duration * 1000,
                                0,
                                1.0);
  } else {
    hdy_header_bar_unschedule_ticks (self);
    gtk_progress_tracker_finish (&priv->tracker);
  }

  gtk_widget_queue_resize (widget);
}

static void
init_sizing_box (HdyHeaderBar *self)
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);
  GtkWidget *w;
  GtkStyleContext *context;

  /* We use this box to always request size for the two labels (title
   * and subtitle) as if they were always visible, but then allocate
   * the real label box with its actual size, to keep it center-aligned
   * in case we have only the title.
   */
  w = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_widget_show (w);
  priv->label_sizing_box = g_object_ref_sink (w);

  w = gtk_label_new (NULL);
  gtk_widget_show (w);
  context = gtk_widget_get_style_context (w);
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_TITLE);
  gtk_box_pack_start (GTK_BOX (priv->label_sizing_box), w, FALSE, FALSE, 0);
  gtk_label_set_line_wrap (GTK_LABEL (w), FALSE);
  gtk_label_set_single_line_mode (GTK_LABEL (w), TRUE);
  gtk_label_set_ellipsize (GTK_LABEL (w), PANGO_ELLIPSIZE_END);
  gtk_label_set_width_chars (GTK_LABEL (w), MIN_TITLE_CHARS);

  w = gtk_label_new (NULL);
  context = gtk_widget_get_style_context (w);
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_SUBTITLE);
  gtk_box_pack_start (GTK_BOX (priv->label_sizing_box), w, FALSE, FALSE, 0);
  gtk_label_set_line_wrap (GTK_LABEL (w), FALSE);
  gtk_label_set_single_line_mode (GTK_LABEL (w), TRUE);
  gtk_label_set_ellipsize (GTK_LABEL (w), PANGO_ELLIPSIZE_END);
  gtk_widget_set_visible (w, priv->has_subtitle || (priv->subtitle && priv->subtitle[0]));
  priv->subtitle_sizing_label = w;
}

static GtkWidget *
create_title_box (const char *title,
                  const char *subtitle,
                  GtkWidget **ret_title_label,
                  GtkWidget **ret_subtitle_label)
{
  GtkWidget *label_box;
  GtkWidget *title_label;
  GtkWidget *subtitle_label;
  GtkStyleContext *context;

  label_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_widget_set_valign (label_box, GTK_ALIGN_CENTER);
  gtk_widget_show (label_box);

  title_label = gtk_label_new (title);
  context = gtk_widget_get_style_context (title_label);
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_TITLE);
  gtk_label_set_line_wrap (GTK_LABEL (title_label), FALSE);
  gtk_label_set_single_line_mode (GTK_LABEL (title_label), TRUE);
  gtk_label_set_ellipsize (GTK_LABEL (title_label), PANGO_ELLIPSIZE_END);
  gtk_box_pack_start (GTK_BOX (label_box), title_label, FALSE, FALSE, 0);
  gtk_widget_show (title_label);
  gtk_label_set_width_chars (GTK_LABEL (title_label), MIN_TITLE_CHARS);

  subtitle_label = gtk_label_new (subtitle);
  context = gtk_widget_get_style_context (subtitle_label);
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_SUBTITLE);
  gtk_label_set_line_wrap (GTK_LABEL (subtitle_label), FALSE);
  gtk_label_set_single_line_mode (GTK_LABEL (subtitle_label), TRUE);
  gtk_label_set_ellipsize (GTK_LABEL (subtitle_label), PANGO_ELLIPSIZE_END);
  gtk_box_pack_start (GTK_BOX (label_box), subtitle_label, FALSE, FALSE, 0);
  gtk_widget_set_no_show_all (subtitle_label, TRUE);
  gtk_widget_set_visible (subtitle_label, subtitle && subtitle[0]);

  if (ret_title_label)
    *ret_title_label = title_label;
  if (ret_subtitle_label)
    *ret_subtitle_label = subtitle_label;

  return label_box;
}

static gboolean
hdy_header_bar_update_window_icon (HdyHeaderBar *self,
                                   GtkWindow    *window)
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);
  GdkPixbuf *pixbuf;
  gint scale;

  if (priv->titlebar_icon == NULL)
    return FALSE;

  scale = gtk_widget_get_scale_factor (priv->titlebar_icon);
  if (GTK_IS_BUTTON (gtk_widget_get_parent (priv->titlebar_icon)))
    pixbuf = hdy_gtk_window_get_icon_for_size (window, scale * 16);
  else
    pixbuf = hdy_gtk_window_get_icon_for_size (window, scale * 20);

  if (pixbuf) {
330 331
    g_autoptr (cairo_surface_t) surface =
      gdk_cairo_surface_create_from_pixbuf (pixbuf, scale, gtk_widget_get_window (priv->titlebar_icon));
Adrien Plazas's avatar
Adrien Plazas committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

    gtk_image_set_from_surface (GTK_IMAGE (priv->titlebar_icon), surface);
    g_object_unref (pixbuf);
    gtk_widget_show (priv->titlebar_icon);

    return TRUE;
  }

  return FALSE;
}

static void
_hdy_header_bar_update_separator_visibility (HdyHeaderBar *self)
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);
  gboolean have_visible_at_start = FALSE;
  gboolean have_visible_at_end = FALSE;
  GList *l;

  for (l = priv->children; l != NULL; l = l->next) {
    Child *child = l->data;

    if (gtk_widget_get_visible (child->widget)) {
      if (child->pack_type == GTK_PACK_START)
        have_visible_at_start = TRUE;
      else
        have_visible_at_end = TRUE;
    }
  }

  if (priv->titlebar_start_separator != NULL)
    gtk_widget_set_visible (priv->titlebar_start_separator, have_visible_at_start);

  if (priv->titlebar_end_separator != NULL)
    gtk_widget_set_visible (priv->titlebar_end_separator, have_visible_at_end);
}

static void
hdy_header_bar_update_window_buttons (HdyHeaderBar *self)
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);
  GtkWidget *widget = GTK_WIDGET (self), *toplevel;
  GtkWindow *window;
  GtkTextDirection direction;
  gchar *layout_desc;
  gchar **tokens, **t;
  gint i, j;
  GMenuModel *menu;
  gboolean shown_by_shell;
  gboolean is_sovereign_window;
382
  gboolean is_mobile_dialog;
Adrien Plazas's avatar
Adrien Plazas committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

  toplevel = gtk_widget_get_toplevel (widget);
  if (!gtk_widget_is_toplevel (toplevel))
    return;

  if (priv->titlebar_start_box) {
    gtk_widget_unparent (priv->titlebar_start_box);
    priv->titlebar_start_box = NULL;
    priv->titlebar_start_separator = NULL;
  }
  if (priv->titlebar_end_box) {
    gtk_widget_unparent (priv->titlebar_end_box);
    priv->titlebar_end_box = NULL;
    priv->titlebar_end_separator = NULL;
  }

  priv->titlebar_icon = NULL;

  if (!priv->shows_wm_decorations)
    return;

  direction = gtk_widget_get_direction (widget);

  g_object_get (gtk_widget_get_settings (widget),
                "gtk-shell-shows-app-menu", &shown_by_shell,
                "gtk-decoration-layout", &layout_desc,
                NULL);

411
  if (priv->decoration_layout_set) {
Adrien Plazas's avatar
Adrien Plazas committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    g_free (layout_desc);
    layout_desc = g_strdup (priv->decoration_layout);
  }

  window = GTK_WINDOW (toplevel);

  if (!shown_by_shell && gtk_window_get_application (window))
    menu = gtk_application_get_app_menu (gtk_window_get_application (window));
  else
    menu = NULL;

  is_sovereign_window = (!gtk_window_get_modal (window) &&
                          gtk_window_get_transient_for (window) == NULL &&
                          gtk_window_get_type_hint (window) == GDK_WINDOW_TYPE_HINT_NORMAL);

427 428
  is_mobile_dialog= (priv->is_mobile_window && !is_sovereign_window);

Adrien Plazas's avatar
Adrien Plazas committed
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 455 456 457 458 459 460 461 462 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 526 527 528 529
  tokens = g_strsplit (layout_desc, ":", 2);
  if (tokens)   {
    for (i = 0; i < 2; i++) {
      GtkWidget *box;
      GtkWidget *separator;
      int n_children = 0;

      if (tokens[i] == NULL)
        break;

      t = g_strsplit (tokens[i], ",", -1);

      separator = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
      gtk_widget_set_no_show_all (separator, TRUE);
      gtk_style_context_add_class (gtk_widget_get_style_context (separator), "titlebutton");

      box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, priv->spacing);

      for (j = 0; t[j]; j++) {
        GtkWidget *button = NULL;
        GtkWidget *image = NULL;
        AtkObject *accessible;

        if (strcmp (t[j], "icon") == 0 &&
            is_sovereign_window) {
            button = gtk_image_new ();
            gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
            priv->titlebar_icon = button;
            gtk_style_context_add_class (gtk_widget_get_style_context (button), "titlebutton");
            gtk_style_context_add_class (gtk_widget_get_style_context (button), "icon");
            gtk_widget_set_size_request (button, 20, 20);
            gtk_widget_show (button);

            if (!hdy_header_bar_update_window_icon (self, window))
              {
                gtk_widget_destroy (button);
                priv->titlebar_icon = NULL;
                button = NULL;
              }
        } else if (strcmp (t[j], "menu") == 0 &&
                 menu != NULL &&
                 is_sovereign_window) {
            button = gtk_menu_button_new ();
            gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
            gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (button), menu);
            gtk_menu_button_set_use_popover (GTK_MENU_BUTTON (button), TRUE);
            gtk_style_context_add_class (gtk_widget_get_style_context (button), "titlebutton");
            gtk_style_context_add_class (gtk_widget_get_style_context (button), "appmenu");
            image = gtk_image_new ();
            gtk_container_add (GTK_CONTAINER (button), image);
            gtk_widget_set_can_focus (button, FALSE);
            gtk_widget_show_all (button);

            accessible = gtk_widget_get_accessible (button);
            if (GTK_IS_ACCESSIBLE (accessible))
              atk_object_set_name (accessible, _("Application menu"));

            priv->titlebar_icon = image;
            if (!hdy_header_bar_update_window_icon (self, window))
              gtk_image_set_from_icon_name (GTK_IMAGE (priv->titlebar_icon),
                                            "application-x-executable-symbolic", GTK_ICON_SIZE_MENU);
        } else if (strcmp (t[j], "minimize") == 0 &&
                 is_sovereign_window) {
            button = gtk_button_new ();
            gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
            gtk_style_context_add_class (gtk_widget_get_style_context (button), "titlebutton");
            gtk_style_context_add_class (gtk_widget_get_style_context (button), "minimize");
            image = gtk_image_new_from_icon_name ("window-minimize-symbolic", GTK_ICON_SIZE_MENU);
            g_object_set (image, "use-fallback", TRUE, NULL);
            gtk_container_add (GTK_CONTAINER (button), image);
            gtk_widget_set_can_focus (button, FALSE);
            gtk_widget_show_all (button);
            g_signal_connect_swapped (button, "clicked",
                                      G_CALLBACK (gtk_window_iconify), window);

            accessible = gtk_widget_get_accessible (button);
            if (GTK_IS_ACCESSIBLE (accessible))
              atk_object_set_name (accessible, _("Minimize"));
        } else if (strcmp (t[j], "maximize") == 0 &&
                 gtk_window_get_resizable (window) &&
                 is_sovereign_window) {
            const gchar *icon_name;
            gboolean maximized = gtk_window_is_maximized (window);

            icon_name = maximized ? "window-restore-symbolic" : "window-maximize-symbolic";
            button = gtk_button_new ();
            gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
            gtk_style_context_add_class (gtk_widget_get_style_context (button), "titlebutton");
            gtk_style_context_add_class (gtk_widget_get_style_context (button), "maximize");
            image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
            g_object_set (image, "use-fallback", TRUE, NULL);
            gtk_container_add (GTK_CONTAINER (button), image);
            gtk_widget_set_can_focus (button, FALSE);
            gtk_widget_show_all (button);
            g_signal_connect_swapped (button, "clicked",
                                      G_CALLBACK (hdy_gtk_window_toggle_maximized), window);

            accessible = gtk_widget_get_accessible (button);
            if (GTK_IS_ACCESSIBLE (accessible))
              atk_object_set_name (accessible, maximized ? _("Restore") : _("Maximize"));
        } else if (strcmp (t[j], "close") == 0 &&
530 531
                   gtk_window_get_deletable (window) &&
                   !is_mobile_dialog) {
Adrien Plazas's avatar
Adrien Plazas committed
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
          button = gtk_button_new ();
          gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
          image = gtk_image_new_from_icon_name ("window-close-symbolic", GTK_ICON_SIZE_MENU);
          gtk_style_context_add_class (gtk_widget_get_style_context (button), "titlebutton");
          gtk_style_context_add_class (gtk_widget_get_style_context (button), "close");
          g_object_set (image, "use-fallback", TRUE, NULL);
          gtk_container_add (GTK_CONTAINER (button), image);
          gtk_widget_set_can_focus (button, FALSE);
          gtk_widget_show_all (button);
          g_signal_connect_swapped (button, "clicked",
                                    G_CALLBACK (gtk_window_close), window);

          accessible = gtk_widget_get_accessible (button);
          if (GTK_IS_ACCESSIBLE (accessible))
            atk_object_set_name (accessible, _("Close"));
547 548 549
        } else if (i == 0 && /* Only at the start. */
                   gtk_window_get_deletable (window) &&
                   is_mobile_dialog) {
550 551 552 553 554 555 556 557 558 559 560 561 562
          button = gtk_button_new ();
          gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
          image = gtk_image_new_from_icon_name ("go-previous-symbolic", GTK_ICON_SIZE_BUTTON);
          g_object_set (image, "use-fallback", TRUE, NULL);
          gtk_container_add (GTK_CONTAINER (button), image);
          gtk_widget_set_can_focus (button, TRUE);
          gtk_widget_show_all (button);
          g_signal_connect_swapped (button, "clicked",
                                    G_CALLBACK (gtk_window_close), window);

          accessible = gtk_widget_get_accessible (button);
          if (GTK_IS_ACCESSIBLE (accessible))
            atk_object_set_name (accessible, _("Back"));
Adrien Plazas's avatar
Adrien Plazas committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
        }

        if (button) {
          gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
          n_children ++;
        }
      }
      g_strfreev (t);

      if (n_children == 0) {
        g_object_ref_sink (box);
        g_object_unref (box);
        g_object_ref_sink (separator);
        g_object_unref (separator);
        continue;
      }

      gtk_box_pack_start (GTK_BOX (box), separator, FALSE, FALSE, 0);
      if (i == 1)
        gtk_box_reorder_child (GTK_BOX (box), separator, 0);

      if ((direction == GTK_TEXT_DIR_LTR && i == 0) ||
          (direction == GTK_TEXT_DIR_RTL && i == 1))
        gtk_style_context_add_class (gtk_widget_get_style_context (box), GTK_STYLE_CLASS_LEFT);
      else
        gtk_style_context_add_class (gtk_widget_get_style_context (box), GTK_STYLE_CLASS_RIGHT);

      gtk_widget_show (box);
      gtk_widget_set_parent (box, GTK_WIDGET (self));

      if (i == 0) {
        priv->titlebar_start_box = box;
        priv->titlebar_start_separator = separator;
      } else {
        priv->titlebar_end_box = box;
        priv->titlebar_end_separator = separator;
      }
    }
    g_strfreev (tokens);
  }
  g_free (layout_desc);

  _hdy_header_bar_update_separator_visibility (self);
}

608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
static gboolean
compute_is_mobile_window (GtkWindow *window)
{
  gint window_width, window_height;

  gtk_window_get_size (window, &window_width, &window_height);

  if (window_width <= MOBILE_WINDOW_WIDTH &&
      window_height <= MOBILE_WINDOW_HEIGHT)
    return TRUE;

  /* Mobile landscape mode. */
  if (window_width <= MOBILE_WINDOW_HEIGHT &&
      window_height <= MOBILE_WINDOW_WIDTH &&
      gtk_window_is_maximized (window))
    return TRUE;

  return FALSE;
}

static void
update_is_mobile_window (HdyHeaderBar *self)
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);
  GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
  gboolean was_mobile_window = priv->is_mobile_window;

  if (!gtk_widget_is_toplevel (toplevel))
    return;

  priv->is_mobile_window = compute_is_mobile_window (GTK_WINDOW (toplevel));

  if (priv->is_mobile_window != was_mobile_window)
    hdy_header_bar_update_window_buttons (self);
}

Adrien Plazas's avatar
Adrien Plazas committed
644 645 646 647 648 649 650 651 652 653 654 655 656 657 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 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
static void
construct_label_box (HdyHeaderBar *self)
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);

  g_assert (priv->label_box == NULL);

  priv->label_box = create_title_box (priv->title,
                                      priv->subtitle,
                                      &priv->title_label,
                                      &priv->subtitle_label);
  gtk_widget_set_parent (priv->label_box, GTK_WIDGET (self));
}

static gint
count_visible_children (HdyHeaderBar *self)
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);
  GList *l;
  Child *child;
  gint n;

  n = 0;
  for (l = priv->children; l; l = l->next) {
    child = l->data;
    if (gtk_widget_get_visible (child->widget))
      n++;
  }

  return n;
}

static gint
count_visible_children_for_pack_type (HdyHeaderBar *self, GtkPackType pack_type)
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);
  GList *l;
  Child *child;
  gint n;

  n = 0;
  for (l = priv->children; l; l = l->next) {
    child = l->data;
    if (gtk_widget_get_visible (child->widget) && child->pack_type == pack_type)
      n++;
  }

  return n;
}

static gboolean
add_child_size (GtkWidget      *child,
                GtkOrientation  orientation,
                gint           *minimum,
                gint           *natural)
{
  gint child_minimum, child_natural;

  if (!gtk_widget_get_visible (child))
    return FALSE;

  if (orientation == GTK_ORIENTATION_HORIZONTAL)
    gtk_widget_get_preferred_width (child, &child_minimum, &child_natural);
  else
    gtk_widget_get_preferred_height (child, &child_minimum, &child_natural);

  if (GTK_ORIENTATION_HORIZONTAL == orientation) {
    *minimum += child_minimum;
    *natural += child_natural;
  } else {
    *minimum = MAX (*minimum, child_minimum);
    *natural = MAX (*natural, child_natural);
  }

  return TRUE;
}

static void
hdy_header_bar_get_size (GtkWidget      *widget,
                         GtkOrientation  orientation,
                         gint           *minimum,
                         gint           *natural)
{
  HdyHeaderBar *self = HDY_HEADER_BAR (widget);
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);
  GList *l;
730
  gint n_start_children = 0, n_end_children = 0;
Adrien Plazas's avatar
Adrien Plazas committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
  gint start_min = 0, start_nat = 0;
  gint end_min = 0, end_nat = 0;
  gint center_min = 0, center_nat = 0;

  for (l = priv->children; l; l = l->next) {
    Child *child = l->data;

    if (child->pack_type == GTK_PACK_START) {
      if (add_child_size (child->widget, orientation, &start_min, &start_nat))
        n_start_children += 1;
    } else {
      if (add_child_size (child->widget, orientation, &end_min, &end_nat))
        n_end_children += 1;
    }
  }

  if (priv->label_box != NULL) {
    if (orientation == GTK_ORIENTATION_HORIZONTAL)
      add_child_size (priv->label_box, orientation, &center_min, &center_nat);
    else
      add_child_size (priv->label_sizing_box, orientation, &center_min, &center_nat);
  }

754 755
  if (priv->custom_title != NULL)
    add_child_size (priv->custom_title, orientation, &center_min, &center_nat);
Adrien Plazas's avatar
Adrien Plazas committed
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781

  if (priv->titlebar_start_box != NULL) {
    if (add_child_size (priv->titlebar_start_box, orientation, &start_min, &start_nat))
      n_start_children += 1;
  }

  if (priv->titlebar_end_box != NULL) {
    if (add_child_size (priv->titlebar_end_box, orientation, &end_min, &end_nat))
      n_end_children += 1;
  }

  if (orientation == GTK_ORIENTATION_HORIZONTAL) {
    gdouble strict_centering_t;
    gint start_min_spaced = start_min + n_start_children * priv->spacing;
    gint end_min_spaced = end_min + n_end_children * priv->spacing;
    gint start_nat_spaced = start_nat + n_start_children * priv->spacing;
    gint end_nat_spaced = end_nat + n_end_children * priv->spacing;

    if (gtk_progress_tracker_get_state (&priv->tracker) != GTK_PROGRESS_STATE_AFTER) {
      strict_centering_t = gtk_progress_tracker_get_ease_out_cubic (&priv->tracker, FALSE);
      if (priv->centering_policy != HDY_CENTERING_POLICY_STRICT)
        strict_centering_t = 1.0 - strict_centering_t;
    } else
      strict_centering_t = priv->centering_policy == HDY_CENTERING_POLICY_STRICT ? 1.0 : 0.0;

    *minimum = center_min + n_start_children * priv->spacing +
Adrien Plazas's avatar
Adrien Plazas committed
782 783 784
               hdy_lerp (2 * MAX (start_min_spaced, end_min_spaced),
                         start_min_spaced + end_min_spaced,
                         strict_centering_t);
Adrien Plazas's avatar
Adrien Plazas committed
785
    *natural = center_nat + n_start_children * priv->spacing +
Adrien Plazas's avatar
Adrien Plazas committed
786 787 788
               hdy_lerp (2 * MAX (start_nat_spaced, end_nat_spaced),
                         start_nat_spaced + end_nat_spaced,
                         strict_centering_t);
Adrien Plazas's avatar
Adrien Plazas committed
789 790 791 792 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 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
  } else {
    *minimum = MAX (MAX (start_min, end_min), center_min);
    *natural = MAX (MAX (start_nat, end_nat), center_nat);
  }
}

static void
hdy_header_bar_compute_size_for_orientation (GtkWidget *widget,
                                             gint       avail_size,
                                             gint      *minimum_size,
                                             gint      *natural_size)
{
  HdyHeaderBar *self = HDY_HEADER_BAR (widget);
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);
  GList *children;
  gint required_size = 0;
  gint required_natural = 0;
  gint child_size;
  gint child_natural;
  gint nvis_children = 0;

  for (children = priv->children; children != NULL; children = children->next) {
    Child *child = children->data;

    if (gtk_widget_get_visible (child->widget)) {
      gtk_widget_get_preferred_width_for_height (child->widget,
                                                 avail_size, &child_size, &child_natural);

      required_size += child_size;
      required_natural += child_natural;

      nvis_children += 1;
    }
  }

  if (priv->label_box != NULL) {
    gtk_widget_get_preferred_width (priv->label_sizing_box,
                                    &child_size, &child_natural);
    required_size += child_size;
    required_natural += child_natural;
  }

  if (priv->custom_title != NULL &&
      gtk_widget_get_visible (priv->custom_title)) {
    gtk_widget_get_preferred_width (priv->custom_title,
                                    &child_size, &child_natural);
    required_size += child_size;
    required_natural += child_natural;
  }

  if (priv->titlebar_start_box != NULL) {
    gtk_widget_get_preferred_width (priv->titlebar_start_box,
                                    &child_size, &child_natural);
    required_size += child_size;
    required_natural += child_natural;
    nvis_children += 1;
  }

  if (priv->titlebar_end_box != NULL) {
    gtk_widget_get_preferred_width (priv->titlebar_end_box,
                                    &child_size, &child_natural);
    required_size += child_size;
    required_natural += child_natural;
    nvis_children += 1;
  }

  required_size += nvis_children * priv->spacing;
  required_natural += nvis_children * priv->spacing;

  *minimum_size = required_size;
  *natural_size = required_natural;
}

static void
hdy_header_bar_compute_size_for_opposing_orientation (GtkWidget *widget,
                                                      gint       avail_size,
                                                      gint      *minimum_size,
                                                      gint      *natural_size)
{
  HdyHeaderBar *self = HDY_HEADER_BAR (widget);
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);
  Child *child;
  GList *children;
  gint nvis_children;
  gint computed_minimum = 0;
  gint computed_natural = 0;
  GtkRequestedSize *sizes;
  GtkPackType packing;
  gint i;
  gint child_size;
  gint child_minimum;
  gint child_natural;
  gint center_min, center_nat;

  nvis_children = count_visible_children (self);

  if (nvis_children <= 0)
    return;

  sizes = g_newa (GtkRequestedSize, nvis_children);

  /* Retrieve desired size for visible children */
  for (i = 0, children = priv->children; children; children = children->next) {
    child = children->data;

    if (gtk_widget_get_visible (child->widget)) {
      gtk_widget_get_preferred_width (child->widget,
                                      &sizes[i].minimum_size,
                                      &sizes[i].natural_size);

      sizes[i].data = child;
      i += 1;
    }
  }

  /* Bring children up to size first */
905
  gtk_distribute_natural_allocation (MAX (0, avail_size), nvis_children, sizes);
Adrien Plazas's avatar
Adrien Plazas committed
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973

  /* Allocate child positions. */
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing) {
    for (i = 0, children = priv->children; children; children = children->next) {
      child = children->data;

      /* If widget is not visible, skip it. */
      if (!gtk_widget_get_visible (child->widget))
        continue;

      /* If widget is packed differently skip it, but still increment i,
       * since widget is visible and will be handled in next loop
       * iteration.
       */
      if (child->pack_type != packing) {
        i++;
        continue;
      }

      child_size = sizes[i].minimum_size;

      gtk_widget_get_preferred_height_for_width (child->widget,
                                                 child_size, &child_minimum, &child_natural);

      computed_minimum = MAX (computed_minimum, child_minimum);
      computed_natural = MAX (computed_natural, child_natural);
    }
  }

  center_min = center_nat = 0;
  if (priv->label_box != NULL) {
    gtk_widget_get_preferred_height (priv->label_sizing_box,
                                     &center_min, &center_nat);
  }

  if (priv->custom_title != NULL &&
      gtk_widget_get_visible (priv->custom_title)) {
    gtk_widget_get_preferred_height (priv->custom_title,
                                     &center_min, &center_nat);
  }

  if (priv->titlebar_start_box != NULL) {
    gtk_widget_get_preferred_height (priv->titlebar_start_box,
                                     &child_minimum, &child_natural);
    computed_minimum = MAX (computed_minimum, child_minimum);
    computed_natural = MAX (computed_natural, child_natural);
  }

  if (priv->titlebar_end_box != NULL) {
    gtk_widget_get_preferred_height (priv->titlebar_end_box,
                                     &child_minimum, &child_natural);
    computed_minimum = MAX (computed_minimum, child_minimum);
    computed_natural = MAX (computed_natural, child_natural);
  }

  *minimum_size = computed_minimum;
  *natural_size = computed_natural;
}

static void
hdy_header_bar_measure (GtkWidget      *widget,
                        GtkOrientation  orientation,
                        gint            for_size,
                        gint           *minimum,
                        gint           *natural,
                        gint           *minimum_baseline,
                        gint           *natural_baseline)
{
974 975 976
  GtkStyleContext *style_context;
  GtkStateFlags state_flags;
  GtkBorder border, margin, padding;
977 978 979 980 981 982 983
  gint css_width, css_height;

  gtk_style_context_get (gtk_widget_get_style_context (widget),
                         gtk_widget_get_state_flags (widget),
                         "min-width", &css_width,
                         "min-height", &css_height,
                         NULL);
Adrien Plazas's avatar
Adrien Plazas committed
984 985 986 987

  if (for_size < 0)
    hdy_header_bar_get_size (widget, orientation, minimum, natural);
  else if (orientation == GTK_ORIENTATION_HORIZONTAL)
988
    hdy_header_bar_compute_size_for_orientation (widget, MAX (for_size, css_height), minimum, natural);
Adrien Plazas's avatar
Adrien Plazas committed
989
  else
990
    hdy_header_bar_compute_size_for_opposing_orientation (widget, MAX (for_size, css_width), minimum, natural);
Adrien Plazas's avatar
Adrien Plazas committed
991

992 993 994 995 996 997 998 999
  /* Manually apply the border, the padding and the margin as we can't use the
   * private GtkGagdet.
   */
  style_context = gtk_widget_get_style_context (widget);
  state_flags = gtk_widget_get_state_flags (widget);
  gtk_style_context_get_border (style_context, state_flags, &border);
  gtk_style_context_get_margin (style_context, state_flags, &margin);
  gtk_style_context_get_padding (style_context, state_flags, &padding);
Adrien Plazas's avatar
Adrien Plazas committed
1000
  if (orientation == GTK_ORIENTATION_VERTICAL) {
1001 1002 1003 1004
    *minimum += border.top + margin.top + padding.top +
                border.bottom + margin.bottom + padding.bottom;
    *natural += border.top + margin.top + padding.top +
                border.bottom + margin.bottom + padding.bottom;
1005 1006 1007

    *minimum = MAX (*minimum, css_height);
    *natural = MAX (*natural, css_height);
Adrien Plazas's avatar
Adrien Plazas committed
1008
  } else {
1009 1010 1011 1012
    *minimum += border.left + margin.left + padding.left +
                border.right + margin.right + padding.right;
    *natural += border.left + margin.left + padding.left +
                border.right + margin.right + padding.right;
1013 1014 1015

    *minimum = MAX (*minimum, css_width);
    *natural = MAX (*natural, css_width);
Adrien Plazas's avatar
Adrien Plazas committed
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
  }
}

static void
hdy_header_bar_get_preferred_width (GtkWidget *widget,
                                    gint      *minimum,
                                    gint      *natural)
{
  hdy_header_bar_measure (widget, GTK_ORIENTATION_HORIZONTAL, -1,
                          minimum, natural,
                          NULL, NULL);
}

static void
hdy_header_bar_get_preferred_height (GtkWidget *widget,
                                     gint      *minimum,
                                     gint      *natural)
{
  hdy_header_bar_measure (widget, GTK_ORIENTATION_VERTICAL, -1,
                          minimum, natural,
                          NULL, NULL);
}

static void
hdy_header_bar_get_preferred_width_for_height (GtkWidget *widget,
                                               gint       height,
                                               gint      *minimum,
                                               gint      *natural)
{
  hdy_header_bar_measure (widget, GTK_ORIENTATION_HORIZONTAL, height,
                          minimum, natural,
                          NULL, NULL);
}

static void
hdy_header_bar_get_preferred_height_for_width (GtkWidget *widget,
                                               gint       width,
                                               gint      *minimum,
                                               gint      *natural)
{
  hdy_header_bar_measure (widget, GTK_ORIENTATION_VERTICAL, width,
                          minimum, natural,
                          NULL, NULL);
}

static GtkWidget *
get_title_size (HdyHeaderBar     *self,
                gint              for_height,
                GtkRequestedSize *size,
                gint             *expanded)
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);
  GtkWidget *title_widget;

  if (priv->custom_title != NULL &&
      gtk_widget_get_visible (priv->custom_title))
    title_widget = priv->custom_title;
  else if (priv->label_box != NULL)
    title_widget = priv->label_box;
  else
    return NULL;

  gtk_widget_get_preferred_width_for_height (title_widget,
                                             for_height,
                                             &(size->minimum_size),
                                             &(size->natural_size));

  *expanded = gtk_widget_compute_expand (title_widget, GTK_ORIENTATION_HORIZONTAL);

  return title_widget;
}

static void
children_allocate (HdyHeaderBar      *self,
                   GtkAllocation     *allocation,
                   GtkAllocation    **allocations,
                   GtkRequestedSize  *sizes,
                   gint               decoration_width[2],
                   gint               uniform_expand_bonus[2],
                   gint               leftover_expand_bonus[2])
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);
  GtkPackType packing;
  GtkAllocation child_allocation;
  gint x;
  gint i;
  GList *l;
  Child *child;
  gint child_size;
  /* GtkTextDirection direction; */

  /* Allocate the children on both sides of the title. */
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; packing++) {
    child_allocation.y = allocation->y;
    child_allocation.height = allocation->height;
    if (packing == GTK_PACK_START)
      x = allocation->x + decoration_width[0];
    else
      x = allocation->x + allocation->width - decoration_width[1];

    i = 0;
    for (l = priv->children; l != NULL; l = l->next) {
      child = l->data;
      if (!gtk_widget_get_visible (child->widget))
        continue;

      if (child->pack_type != packing)
        goto next;

      child_size = sizes[i].minimum_size;

      /* If this child is expanded, give it extra space from the reserves. */
      if (gtk_widget_compute_expand (child->widget, GTK_ORIENTATION_HORIZONTAL)) {
        gint expand_bonus;

        expand_bonus = uniform_expand_bonus[packing];

        if (leftover_expand_bonus[packing] > 0) {
          expand_bonus++;
          leftover_expand_bonus[packing]--;
        }

        child_size += expand_bonus;
      }

      child_allocation.width = child_size;

      if (packing == GTK_PACK_START) {
        child_allocation.x = x;
        x += child_size;
        x += priv->spacing;
      } else {
        x -= child_size;
        child_allocation.x = x;
        x -= priv->spacing;
      }

      if (gtk_widget_get_direction (GTK_WIDGET (self)) == GTK_TEXT_DIR_RTL)
        child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;

      (*allocations)[i] = child_allocation;

      next:
        i++;
    }
  }
}

static void
get_loose_centering_allocations (HdyHeaderBar   *self,
                                 GtkAllocation  *allocation,
                                 GtkAllocation **allocations,
                                 GtkAllocation  *title_allocation,
                                 gint            decoration_width[2])
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);
  GtkRequestedSize *sizes;
  gint width;
  gint nvis_children;
  GtkRequestedSize title_size = { 0 };
  gboolean title_expands = FALSE;
  gint side[2] = { 0 };
  gint uniform_expand_bonus[2] = { 0 };
  gint leftover_expand_bonus[2] = { 0 };
  gint side_free_space[2] = { 0 };
  gint center_free_space[2] = { 0 };
  gint nexpand_children[2] = { 0 };
  gint center_free_space_min;
  GList *l;
  gint i;
  Child *child;
  GtkPackType packing;

  nvis_children = count_visible_children (self);
  sizes = g_newa (GtkRequestedSize, nvis_children);

  width = allocation->width - nvis_children * priv->spacing;

  i = 0;
  for (l = priv->children; l; l = l->next) {
    child = l->data;
    if (!gtk_widget_get_visible (child->widget))
      continue;

    if (gtk_widget_compute_expand (child->widget, GTK_ORIENTATION_HORIZONTAL))
      nexpand_children[child->pack_type]++;

    gtk_widget_get_preferred_width_for_height (child->widget,
                                               allocation->height,
                                               &sizes[i].minimum_size,
                                               &sizes[i].natural_size);
    width -= sizes[i].minimum_size;
    i++;
  }

  get_title_size (self, allocation->height, &title_size, &title_expands);
  width -= title_size.minimum_size;

  /* Compute the nominal size of the children filling up each side of the title
   * in titlebar.
   */
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; packing++) {
    i = 0;
    for (l = priv->children; l != NULL; l = l->next) {
      child = l->data;
      if (!gtk_widget_get_visible (child->widget))
        continue;

      if (child->pack_type == packing)
        side[packing] += sizes[i].minimum_size + priv->spacing;

      i++;
    }
  }

  /* Distribute the available space for natural expansion of the children. */
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; packing++)
    width -= decoration_width[packing];
  width = gtk_distribute_natural_allocation (MAX (0, width), 1, &title_size);
  width = gtk_distribute_natural_allocation (MAX (0, width), nvis_children, sizes);

  /* Compute the nominal size of the children filling up each side of the title
   * in titlebar.
   */
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; packing++) {
    i = 0;
    side[packing] = 0;
    for (l = priv->children; l != NULL; l = l->next) {
      child = l->data;
      if (!gtk_widget_get_visible (child->widget))
        continue;

      if (child->pack_type == packing)
        side[packing] += sizes[i].minimum_size + priv->spacing;

      i++;
    }
  }

  /* Figure out how much space is left on each side of the title, and earkmark
   * that space for the expanded children.
   *
   * If the title itself is expanded, then it gets half the spoils from each
   * side.
   */
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; packing++) {
    side_free_space[packing] = MIN (MAX (allocation->width / 2 - title_size.natural_size / 2 - decoration_width[packing] - side[packing], 0), width);
    if (title_expands)
      center_free_space[packing] = nexpand_children[packing] > 0 ?
        side_free_space[packing] / 2 :
        side_free_space[packing];
  }
  center_free_space_min = MIN (center_free_space[0], center_free_space[1]);
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; packing++) {
    center_free_space[packing] = center_free_space_min;
    side_free_space[packing] -= center_free_space[packing];
    width -= side_free_space[packing];

    if (nexpand_children[packing] == 0)
      continue;

    uniform_expand_bonus[packing] = (side_free_space[packing]) / nexpand_children[packing];
    leftover_expand_bonus[packing] = (side_free_space[packing]) % nexpand_children[packing];
  }

  children_allocate (self, allocation, allocations, sizes, decoration_width, uniform_expand_bonus, leftover_expand_bonus);

  /* We don't enforce css borders on the center widget, to make title/subtitle
   * combinations fit without growing the header.
   */
  title_allocation->y = allocation->y;
  title_allocation->height = allocation->height;

  title_allocation->width = MIN (allocation->width - decoration_width[0] - side[0] - decoration_width[1] - side[1],
                                 title_size.natural_size);
  title_allocation->x = allocation->x + (allocation->width - title_allocation->width) / 2;

  /* If the title widget is expanded, then grow it by all the available free
   * space, and recenter it.
   */
  if (title_expands && width > 0) {
    title_allocation->width += width;
    title_allocation->x -= width / 2;
  }

  if (allocation->x + decoration_width[0] + side[0] > title_allocation->x)
    title_allocation->x = allocation->x + decoration_width[0] + side[0];
  else if (allocation->x + allocation->width - decoration_width[1] - side[1] < title_allocation->x + title_allocation->width)
    title_allocation->x = allocation->x + allocation->width - decoration_width[1] - side[1] - title_allocation->width;

  if (gtk_widget_get_direction (GTK_WIDGET (self)) == GTK_TEXT_DIR_RTL)
    title_allocation->x = allocation->x + allocation->width - (title_allocation->x - allocation->x) - title_allocation->width;
}

static void
get_strict_centering_allocations (HdyHeaderBar   *self,
                                  GtkAllocation  *allocation,
                                  GtkAllocation **allocations,
                                  GtkAllocation  *title_allocation,
                                  gint            decoration_width[2])
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);

  GtkRequestedSize *children_sizes = { 0 };
  GtkRequestedSize *children_sizes_for_side[2] = { 0 };
  GtkRequestedSize side_size[2] = { 0 }; /* The size requested by each side. */
  GtkRequestedSize title_size = { 0 }; /* The size requested by the title. */
  GtkRequestedSize side_request = { 0 }; /* The maximum size requested by each side, decoration included. */
  gint side_max; /* The maximum space allocatable to each side, decoration included. */
  gint title_leftover; /* The or 0px or 1px leftover from ensuring each side is allocated the same size. */
  /* The space available for expansion on each side, including for the title. */
  gint free_space[2] = { 0 };
  /* The space the title will take from the free space of each side for its expansion. */
  gint title_expand_bonus = 0;
  gint uniform_expand_bonus[2] = { 0 };
  gint leftover_expand_bonus[2] = { 0 };

  gint nvis_children, n_side_vis_children[2] = { 0 };
  gint nexpand_children[2] = { 0 };
  gboolean title_expands = FALSE;
  GList *l;
  gint i;
  Child *child;
  GtkPackType packing;

  get_title_size (self, allocation->height, &title_size, &title_expands);

  nvis_children = count_visible_children (self);
  children_sizes = g_newa (GtkRequestedSize, nvis_children);
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; packing++) {
    n_side_vis_children[packing] = count_visible_children_for_pack_type (self, packing);
    children_sizes_for_side[packing] = packing == 0 ? children_sizes : children_sizes + n_side_vis_children[packing - 1];
    free_space[packing] = (allocation->width - title_size.minimum_size) / 2 - decoration_width[packing];
  }

  /* Compute the nominal size of the children filling up each side of the title
   * in titlebar.
   */
  i = 0;
  for (l = priv->children; l; l = l->next) {
    child = l->data;
    if (!gtk_widget_get_visible (child->widget))
      continue;

    if (gtk_widget_compute_expand (child->widget, GTK_ORIENTATION_HORIZONTAL))
      nexpand_children[child->pack_type]++;

    gtk_widget_get_preferred_width_for_height (child->widget,
                                               allocation->height,
                                               &children_sizes[i].minimum_size,
                                               &children_sizes[i].natural_size);
    side_size[child->pack_type].minimum_size += children_sizes[i].minimum_size + priv->spacing;
    side_size[child->pack_type].natural_size += children_sizes[i].natural_size + priv->spacing;
    free_space[child->pack_type] -= children_sizes[i].minimum_size + priv->spacing;

    i++;
  }

  /* Figure out the space maximum size requests from each side to help centering
   * the title.
   */
  side_request.minimum_size = MAX (side_size[GTK_PACK_START].minimum_size + decoration_width[GTK_PACK_START],
                                   side_size[GTK_PACK_END].minimum_size + decoration_width[GTK_PACK_END]);
  side_request.natural_size = MAX (side_size[GTK_PACK_START].natural_size + decoration_width[GTK_PACK_START],
                                   side_size[GTK_PACK_END].natural_size + decoration_width[GTK_PACK_END]);
  title_leftover = (allocation->width - title_size.natural_size) % 2;
  side_max = MAX ((allocation->width - title_size.natural_size) / 2, side_request.minimum_size);

  /* Distribute the available space for natural expansion of the children and
   * figure out how much space is left on each side of the title, free to be
   * used for expansion.
   */
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; packing++) {
    gint leftovers = side_max - side_size[packing].minimum_size - decoration_width[packing];
    free_space[packing] = gtk_distribute_natural_allocation (leftovers, n_side_vis_children[packing], children_sizes_for_side[packing]);
  }

  /* Compute how much of each side's free space should be distributed to the
   * title for its expansion.
   */
  title_expand_bonus = !title_expands ? 0 :
    MIN (nexpand_children[GTK_PACK_START] > 0 ? free_space[GTK_PACK_START] / 2 :
                                                free_space[GTK_PACK_START],
         nexpand_children[GTK_PACK_END] > 0 ? free_space[GTK_PACK_END] / 2 :
                                              free_space[GTK_PACK_END]);

  /* Remove the space the title takes from each side for its expansion. */
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; packing++)
    free_space[packing] -= title_expand_bonus;

  /* Distribute the free space for expansion of the children. */
  for (packing = GTK_PACK_START; packing <= GTK_PACK_END; packing++) {
    if (nexpand_children[packing] == 0)
      continue;

    uniform_expand_bonus[packing] = free_space[packing] / nexpand_children[packing];
    leftover_expand_bonus[packing] = free_space[packing] % nexpand_children[packing];
  }

  children_allocate (self, allocation, allocations, children_sizes, decoration_width, uniform_expand_bonus, leftover_expand_bonus);

  /* We don't enforce css borders on the center widget, to make title/subtitle
   * combinations fit without growing the header.
   */
  title_allocation->y = allocation->y;
  title_allocation->height = allocation->height;

  title_allocation->width = MIN (allocation->width - 2 * side_max + title_leftover,
                                title_size.natural_size);
  title_allocation->x = allocation->x + (allocation->width - title_allocation->width) / 2;

  /* If the title widget is expanded, then grow it by the free space available
   * for it.
   */
  if (title_expands) {
    title_allocation->width += 2 * title_expand_bonus;
    title_allocation->x -= title_expand_bonus;
  }

  if (gtk_widget_get_direction (GTK_WIDGET (self)) == GTK_TEXT_DIR_RTL)
    title_allocation->x = allocation->x + allocation->width - (title_allocation->x - allocation->x) - title_allocation->width;
}

static void
hdy_header_bar_size_allocate (GtkWidget     *widget,
                              GtkAllocation *allocation)
{
  HdyHeaderBar *self = HDY_HEADER_BAR (widget);
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (self);
  GtkAllocation *allocations;
  GtkAllocation title_allocation;
1447
  GtkAllocation clip;
Adrien Plazas's avatar
Adrien Plazas committed
1448 1449 1450 1451 1452 1453
  gint nvis_children;
  GList *l;
  gint i;
  Child *child;
  GtkAllocation child_allocation;
  GtkTextDirection direction;
1454 1455 1456
  GtkStyleContext *style_context;
  GtkStateFlags state_flags;
  GtkBorder border, margin, padding;
Adrien Plazas's avatar
Adrien Plazas committed
1457 1458 1459
  GtkWidget *decoration_box[2] = { priv->titlebar_start_box, priv->titlebar_end_box };
  gint decoration_width[2] = { 0 };

1460 1461 1462 1463 1464 1465 1466
  gtk_render_background_get_clip (gtk_widget_get_style_context (widget),
                                  allocation->x,
                                  allocation->y,
                                  allocation->width,
                                  allocation->height,
                                  &clip);

Adrien Plazas's avatar
Adrien Plazas committed
1467 1468
  gtk_widget_set_allocation (widget, allocation);

1469 1470 1471 1472 1473 1474 1475
  if (gtk_widget_get_realized (widget))
    gdk_window_move_resize (gtk_widget_get_window (widget),
                            allocation->x,
                            allocation->y,
                            allocation->width,
                            allocation->height);

1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
  /* Manually apply the border, the padding and the margin as we can't use the
   * private GtkGagdet.
   */
  style_context = gtk_widget_get_style_context (widget);
  state_flags = gtk_widget_get_state_flags (widget);
  gtk_style_context_get_border (style_context, state_flags, &border);
  gtk_style_context_get_margin (style_context, state_flags, &margin);
  gtk_style_context_get_padding (style_context, state_flags, &padding);
  allocation->width -= border.left + border.right +
                       margin.left + margin.right +
                       padding.left + padding.right;
  allocation->height -= border.top + border.bottom +
                        margin.top + margin.bottom +
                        padding.top + padding.bottom;
1490 1491
  allocation->x = border.left + margin.left + padding.left;
  allocation->y = border.top + margin.top + padding.top;
Adrien Plazas's avatar
Adrien Plazas committed
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553

  direction = gtk_widget_get_direction (widget);
  nvis_children = count_visible_children (self);
  allocations = g_newa (GtkAllocation, nvis_children);

  /* Get the decoration width. */
  for (GtkPackType packing = GTK_PACK_START; packing <= GTK_PACK_END; packing++) {
    gint min, nat;

    if (decoration_box[packing] == NULL)
      continue;

    gtk_widget_get_preferred_width_for_height (decoration_box[packing],
                                               allocation->height,
                                               &min, &nat);
    decoration_width[packing] = nat + priv->spacing;
  }

  /* Allocate the decoration widgets. */
  child_allocation.y = allocation->y;
  child_allocation.height = allocation->height;

  if (priv->titlebar_start_box) {
    if (direction == GTK_TEXT_DIR_LTR)
      child_allocation.x = allocation->x;
    else
      child_allocation.x = allocation->x + allocation->width - decoration_width[GTK_PACK_START] + priv->spacing;
    child_allocation.width = decoration_width[GTK_PACK_START] - priv->spacing;
    gtk_widget_size_allocate (priv->titlebar_start_box, &child_allocation);
  }

  if (priv->titlebar_end_box) {
    if (direction != GTK_TEXT_DIR_LTR)
      child_allocation.x = allocation->x;
    else
      child_allocation.x = allocation->x + allocation->width - decoration_width[GTK_PACK_END] + priv->spacing;
    child_allocation.width = decoration_width[GTK_PACK_END] - priv->spacing;
    gtk_widget_size_allocate (priv->titlebar_end_box, &child_allocation);
  }

  /* Get the allocation for widgets on both side of the title. */
  if (gtk_progress_tracker_get_state (&priv->tracker) == GTK_PROGRESS_STATE_AFTER) {
    if (priv->centering_policy == HDY_CENTERING_POLICY_STRICT)
      get_strict_centering_allocations (self, allocation, &allocations, &title_allocation, decoration_width);
    else
      get_loose_centering_allocations (self, allocation, &allocations, &title_allocation, decoration_width);
  } else {
    /* For memory usage optimisation's sake, we will use the allocations
     * variable to store the loose centering allocations and the
     * title_allocation variable to store the loose title allocation.
     */
    GtkAllocation *strict_allocations = g_newa (GtkAllocation, nvis_children);
    GtkAllocation strict_title_allocation;
    gdouble strict_centering_t = gtk_progress_tracker_get_ease_out_cubic (&priv->tracker, FALSE);

    if (priv->centering_policy != HDY_CENTERING_POLICY_STRICT)
      strict_centering_t = 1.0 - strict_centering_t;

    get_loose_centering_allocations (self, allocation, &allocations, &title_allocation, decoration_width);
    get_strict_centering_allocations (self, allocation, &strict_allocations, &strict_title_allocation, decoration_width);

    for (i = 0; i < nvis_children; i++) {
Adrien Plazas's avatar
Adrien Plazas committed
1554 1555 1556 1557
      allocations[i].x = hdy_lerp (strict_allocations[i].x, allocations[i].x, strict_centering_t);
      allocations[i].y = hdy_lerp (strict_allocations[i].y, allocations[i].y, strict_centering_t);
      allocations[i].width = hdy_lerp (strict_allocations[i].width, allocations[i].width, strict_centering_t);
      allocations[i].height = hdy_lerp (strict_allocations[i].height, allocations[i].height, strict_centering_t);
Adrien Plazas's avatar
Adrien Plazas committed
1558
    }
Adrien Plazas's avatar
Adrien Plazas committed
1559 1560 1561 1562
    title_allocation.x = hdy_lerp (strict_title_allocation.x, title_allocation.x, strict_centering_t);
    title_allocation.y = hdy_lerp (strict_title_allocation.y, title_allocation.y, strict_centering_t);
    title_allocation.width = hdy_lerp (strict_title_allocation.width, title_allocation.width, strict_centering_t);
    title_allocation.height = hdy_lerp (strict_title_allocation.height, title_allocation.height, strict_centering_t);
Adrien Plazas's avatar
Adrien Plazas committed
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
  }

  /* Allocate the children on both sides of the title. */
  i = 0;
  for (l = priv->children; l != NULL; l = l->next) {
    child = l->data;
    if (!gtk_widget_get_visible (child->widget))
      continue;

    gtk_widget_size_allocate (child->widget, &allocations[i]);
    i++;
  }

  /* Allocate the title widget. */
  if (priv->custom_title != NULL && gtk_widget_get_visible (priv->custom_title))
    gtk_widget_size_allocate (priv->custom_title, &title_allocation);
  else if (priv->label_box != NULL)
    gtk_widget_size_allocate (priv->label_box, &title_allocation);
1581 1582

  gtk_widget_set_clip (widget, &clip);
Adrien Plazas's avatar
Adrien Plazas committed
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
}

static void
hdy_header_bar_destroy (GtkWidget *widget)
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (HDY_HEADER_BAR (widget));

  if (priv->label_sizing_box) {
    gtk_widget_destroy (priv->label_sizing_box);
    g_clear_object (&priv->label_sizing_box);
  }

  if (priv->custom_title) {
    gtk_widget_unparent (priv->custom_title);
    priv->custom_title = NULL;
  }

  if (priv->label_box) {
    gtk_widget_unparent (priv->label_box);
    priv->label_box = NULL;
  }

  if (priv->titlebar_start_box) {
    gtk_widget_unparent (priv->titlebar_start_box);
    priv->titlebar_start_box = NULL;
    priv->titlebar_start_separator = NULL;
  }

  if (priv->titlebar_end_box) {
    gtk_widget_unparent (priv->titlebar_end_box);
    priv->titlebar_end_box = NULL;
    priv->titlebar_end_separator = NULL;
  }

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

static void
hdy_header_bar_finalize (GObject *object)
{
  HdyHeaderBarPrivate *priv = hdy_header_bar_get_instance_private (HDY_HEADER_BAR (object));

  g_clear_pointer (&priv->title, g_free);
  g_clear_pointer (&priv->subtitle, g_free);
  g_clear_pointer (&priv->decoration_layout, g_free);
1628
  g_clear_object (&priv->controller);
Adrien Plazas's avatar
Adrien Plazas committed
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910