gtkstackswitcher.c 20.3 KB
Newer Older
Matthias Clasen's avatar
Matthias Clasen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (c) 2013 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "config.h"
#include "gtkstackswitcher.h"
Matthias Clasen's avatar
Matthias Clasen committed
22 23
#include "gtkradiobutton.h"
#include "gtklabel.h"
24
#include "gtkdnd.h"
25
#include "gtkdragdest.h"
Matthias Clasen's avatar
Matthias Clasen committed
26
#include "gtkorientable.h"
Matthias Clasen's avatar
Matthias Clasen committed
27 28 29
#include "gtkprivate.h"
#include "gtkintl.h"

30 31
/**
 * SECTION:gtkstackswitcher
32
 * @Short_description: A controller for GtkStack
33 34 35 36 37 38 39 40 41 42 43 44 45 46
 * @Title: GtkStackSwitcher
 * @See_also: #GtkStack
 *
 * The GtkStackSwitcher widget acts as a controller for a
 * #GtkStack; it shows a row of buttons to switch between
 * the various pages of the associated stack widget.
 *
 * All the content for the buttons comes from the child properties
 * of the #GtkStack.
 *
 * It is possible to associate multiple #GtkStackSwitcher widgets
 * with the same stack widget.
 *
 * The GtkStackSwitcher widget was added in 3.10.
47 48 49
 *
 * # CSS nodes
 *
50 51
 * GtkStackSwitcher has a single CSS node named stackswitcher and
 * style class .stack-switcher.
52 53 54 55
 *
 * When circumstances require it, GtkStackSwitcher adds the
 * .needs-attention style class to the widgets representing the
 * stack pages.
56 57
 */

58 59
#define TIMEOUT_EXPAND 500

60
typedef struct _GtkStackSwitcherPrivate GtkStackSwitcherPrivate;
Matthias Clasen's avatar
Matthias Clasen committed
61 62 63 64
struct _GtkStackSwitcherPrivate
{
  GtkStack *stack;
  GHashTable *buttons;
65
  gint icon_size;
Matthias Clasen's avatar
Matthias Clasen committed
66
  gboolean in_child_changed;
67 68
  GtkWidget *switch_button;
  guint switch_timer;
Matthias Clasen's avatar
Matthias Clasen committed
69 70 71 72
};

enum {
  PROP_0,
73
  PROP_ICON_SIZE,
Matthias Clasen's avatar
Matthias Clasen committed
74 75 76
  PROP_STACK
};

77
G_DEFINE_TYPE_WITH_PRIVATE (GtkStackSwitcher, gtk_stack_switcher, GTK_TYPE_BOX)
Matthias Clasen's avatar
Matthias Clasen committed
78 79 80 81 82 83 84

static void
gtk_stack_switcher_init (GtkStackSwitcher *switcher)
{
  GtkStyleContext *context;
  GtkStackSwitcherPrivate *priv;

85 86
  gtk_widget_set_has_window (GTK_WIDGET (switcher), FALSE);

87
  priv = gtk_stack_switcher_get_instance_private (switcher);
Matthias Clasen's avatar
Matthias Clasen committed
88

89
  priv->icon_size = GTK_ICON_SIZE_MENU;
Matthias Clasen's avatar
Matthias Clasen committed
90 91 92 93
  priv->stack = NULL;
  priv->buttons = g_hash_table_new (g_direct_hash, g_direct_equal);

  context = gtk_widget_get_style_context (GTK_WIDGET (switcher));
94
  gtk_style_context_add_class (context, "stack-switcher");
Matthias Clasen's avatar
Matthias Clasen committed
95 96 97
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_LINKED);

  gtk_orientable_set_orientation (GTK_ORIENTABLE (switcher), GTK_ORIENTATION_HORIZONTAL);
98 99 100

  gtk_drag_dest_set (GTK_WIDGET (switcher), 0, NULL, 0, 0);
  gtk_drag_dest_set_track_motion (GTK_WIDGET (switcher), TRUE);
Matthias Clasen's avatar
Matthias Clasen committed
101 102 103 104 105 106 107
}

static void
on_button_clicked (GtkWidget        *widget,
                   GtkStackSwitcher *self)
{
  GtkWidget *child;
108 109 110
  GtkStackSwitcherPrivate *priv;

  priv = gtk_stack_switcher_get_instance_private (self);
Matthias Clasen's avatar
Matthias Clasen committed
111

112
  if (!priv->in_child_changed)
Matthias Clasen's avatar
Matthias Clasen committed
113 114
    {
      child = g_object_get_data (G_OBJECT (widget), "stack-child");
115
      gtk_stack_set_visible_child (priv->stack, child);
Matthias Clasen's avatar
Matthias Clasen committed
116 117 118 119 120 121
    }
}

static void
rebuild_child (GtkWidget   *self,
               const gchar *icon_name,
122 123
               const gchar *title,
               gint         icon_size)
Matthias Clasen's avatar
Matthias Clasen committed
124 125 126 127 128 129 130 131 132 133 134 135 136
{
  GtkStyleContext *context;
  GtkWidget *button_child;

  button_child = gtk_bin_get_child (GTK_BIN (self));
  if (button_child != NULL)
    gtk_widget_destroy (button_child);

  button_child = NULL;
  context = gtk_widget_get_style_context (GTK_WIDGET (self));

  if (icon_name != NULL)
    {
137
      button_child = gtk_image_new_from_icon_name (icon_name, icon_size);
Matthias Clasen's avatar
Matthias Clasen committed
138 139 140 141 142 143 144 145 146 147
      if (title != NULL)
        gtk_widget_set_tooltip_text (GTK_WIDGET (self), title);

      gtk_style_context_remove_class (context, "text-button");
      gtk_style_context_add_class (context, "image-button");
    }
  else if (title != NULL)
    {
      button_child = gtk_label_new (title);

148 149
      gtk_widget_set_tooltip_text (GTK_WIDGET (self), NULL);

Matthias Clasen's avatar
Matthias Clasen committed
150 151 152 153 154 155
      gtk_style_context_remove_class (context, "image-button");
      gtk_style_context_add_class (context, "text-button");
    }

  if (button_child)
    {
156
      gtk_widget_set_halign (GTK_WIDGET (button_child), GTK_ALIGN_CENTER);
Matthias Clasen's avatar
Matthias Clasen committed
157 158 159 160 161
      gtk_widget_show_all (button_child);
      gtk_container_add (GTK_CONTAINER (self), button_child);
    }
}

162
static void
Matthias Clasen's avatar
Matthias Clasen committed
163
update_needs_attention (GtkWidget *widget, GtkWidget *button, gpointer data)
164 165 166 167 168 169 170 171 172 173 174
{
  GtkContainer *container;
  gboolean needs_attention;
  GtkStyleContext *context;

  container = GTK_CONTAINER (data);
  gtk_container_child_get (container, widget,
                           "needs-attention", &needs_attention,
                           NULL);

  context = gtk_widget_get_style_context (button);
175
  if (needs_attention)
176 177 178 179 180
    gtk_style_context_add_class (context, GTK_STYLE_CLASS_NEEDS_ATTENTION);
  else
    gtk_style_context_remove_class (context, GTK_STYLE_CLASS_NEEDS_ATTENTION);
}

Matthias Clasen's avatar
Matthias Clasen committed
181 182 183 184 185 186 187
static void
update_button (GtkStackSwitcher *self,
               GtkWidget        *widget,
               GtkWidget        *button)
{
  gchar *title;
  gchar *icon_name;
188 189 190
  GtkStackSwitcherPrivate *priv;

  priv = gtk_stack_switcher_get_instance_private (self);
Matthias Clasen's avatar
Matthias Clasen committed
191

192
  gtk_container_child_get (GTK_CONTAINER (priv->stack), widget,
Matthias Clasen's avatar
Matthias Clasen committed
193 194 195 196
                           "title", &title,
                           "icon-name", &icon_name,
                           NULL);

197
  rebuild_child (button, icon_name, title, priv->icon_size);
Matthias Clasen's avatar
Matthias Clasen committed
198

199
  gtk_widget_set_visible (button, gtk_widget_get_visible (widget) && (title != NULL || icon_name != NULL));
Matthias Clasen's avatar
Matthias Clasen committed
200 201 202

  g_free (title);
  g_free (icon_name);
203 204

  update_needs_attention (widget, button, priv->stack);
Matthias Clasen's avatar
Matthias Clasen committed
205 206 207
}

static void
208 209 210
on_title_icon_visible_updated (GtkWidget        *widget,
                               GParamSpec       *pspec,
                               GtkStackSwitcher *self)
Matthias Clasen's avatar
Matthias Clasen committed
211 212
{
  GtkWidget *button;
213 214 215
  GtkStackSwitcherPrivate *priv;

  priv = gtk_stack_switcher_get_instance_private (self);
Matthias Clasen's avatar
Matthias Clasen committed
216

217
  button = g_hash_table_lookup (priv->buttons, widget);
Matthias Clasen's avatar
Matthias Clasen committed
218 219 220 221 222 223 224 225 226 227
  update_button (self, widget, button);
}

static void
on_position_updated (GtkWidget        *widget,
                     GParamSpec       *pspec,
                     GtkStackSwitcher *self)
{
  GtkWidget *button;
  gint position;
228
  GtkStackSwitcherPrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
229

230
  priv = gtk_stack_switcher_get_instance_private (self);
Matthias Clasen's avatar
Matthias Clasen committed
231

232 233 234
  button = g_hash_table_lookup (priv->buttons, widget);

  gtk_container_child_get (GTK_CONTAINER (priv->stack), widget,
Matthias Clasen's avatar
Matthias Clasen committed
235 236 237 238 239 240
                           "position", &position,
                           NULL);

  gtk_box_reorder_child (GTK_BOX (self), button, position);
}

241 242 243 244 245 246 247 248 249 250 251 252 253 254
static void
on_needs_attention_updated (GtkWidget        *widget,
                            GParamSpec       *pspec,
                            GtkStackSwitcher *self)
{
  GtkWidget *button;
  GtkStackSwitcherPrivate *priv;

  priv = gtk_stack_switcher_get_instance_private (self);

  button = g_hash_table_lookup (priv->buttons, widget);
  update_button (self, widget, button);
}

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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
static void
remove_switch_timer (GtkStackSwitcher *self)
{
  GtkStackSwitcherPrivate *priv;

  priv = gtk_stack_switcher_get_instance_private (self);

  if (priv->switch_timer)
    {
      g_source_remove (priv->switch_timer);
      priv->switch_timer = 0;
    }
}

static gboolean
gtk_stack_switcher_switch_timeout (gpointer data)
{
  GtkStackSwitcher *self = data;
  GtkStackSwitcherPrivate *priv;
  GtkWidget *button;

  priv = gtk_stack_switcher_get_instance_private (self);

  priv->switch_timer = 0;

  button = priv->switch_button;
  priv->switch_button = NULL;

  if (button)
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);

  return G_SOURCE_REMOVE;
}

static gboolean
gtk_stack_switcher_drag_motion (GtkWidget      *widget,
                                GdkDragContext *context,
                                gint            x,
                                gint            y,
                                guint           time)
{
  GtkStackSwitcher *self = GTK_STACK_SWITCHER (widget);
  GtkStackSwitcherPrivate *priv;
  GtkAllocation allocation;
  GtkWidget *button;
  GHashTableIter iter;
  gpointer value;
  gboolean retval = FALSE;

  gtk_widget_get_allocation (widget, &allocation);

  priv = gtk_stack_switcher_get_instance_private (self);

  x += allocation.x;
  y += allocation.y;

  button = NULL;
  g_hash_table_iter_init (&iter, priv->buttons);
  while (g_hash_table_iter_next (&iter, NULL, &value))
    {
      gtk_widget_get_allocation (GTK_WIDGET (value), &allocation);
      if (x >= allocation.x && x <= allocation.x + allocation.width &&
          y >= allocation.y && y <= allocation.y + allocation.height)
        {
          button = GTK_WIDGET (value);
          retval = TRUE;
          break;
        }
    }

  if (button != priv->switch_button)
    remove_switch_timer (self);

  priv->switch_button = button;

  if (button && !priv->switch_timer)
    {
      priv->switch_timer = gdk_threads_add_timeout (TIMEOUT_EXPAND,
                                                    gtk_stack_switcher_switch_timeout,
                                                    self);
      g_source_set_name_by_id (priv->switch_timer, "[gtk+] gtk_stack_switcher_switch_timeout");
    }

  return retval;
}

static void
gtk_stack_switcher_drag_leave (GtkWidget      *widget,
                               GdkDragContext *context,
                               guint           time)
{
  GtkStackSwitcher *self = GTK_STACK_SWITCHER (widget);

  remove_switch_timer (self);
}

Matthias Clasen's avatar
Matthias Clasen committed
351
static void
352 353
add_child (GtkWidget        *widget,
           GtkStackSwitcher *self)
Matthias Clasen's avatar
Matthias Clasen committed
354 355 356
{
  GtkWidget *button;
  GList *group;
357 358 359
  GtkStackSwitcherPrivate *priv;

  priv = gtk_stack_switcher_get_instance_private (self);
Matthias Clasen's avatar
Matthias Clasen committed
360 361

  button = gtk_radio_button_new (NULL);
362

363
  gtk_widget_set_focus_on_click (button, FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
  gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE);

  update_button (self, widget, button);

  group = gtk_container_get_children (GTK_CONTAINER (self));
  if (group != NULL)
    {
      gtk_radio_button_join_group (GTK_RADIO_BUTTON (button), GTK_RADIO_BUTTON (group->data));
      g_list_free (group);
    }

  gtk_container_add (GTK_CONTAINER (self), button);

  g_object_set_data (G_OBJECT (button), "stack-child", widget);
  g_signal_connect (button, "clicked", G_CALLBACK (on_button_clicked), self);
379 380 381
  g_signal_connect (widget, "notify::visible", G_CALLBACK (on_title_icon_visible_updated), self);
  g_signal_connect (widget, "child-notify::title", G_CALLBACK (on_title_icon_visible_updated), self);
  g_signal_connect (widget, "child-notify::icon-name", G_CALLBACK (on_title_icon_visible_updated), self);
Matthias Clasen's avatar
Matthias Clasen committed
382
  g_signal_connect (widget, "child-notify::position", G_CALLBACK (on_position_updated), self);
383
  g_signal_connect (widget, "child-notify::needs-attention", G_CALLBACK (on_needs_attention_updated), self);
Matthias Clasen's avatar
Matthias Clasen committed
384

385
  g_hash_table_insert (priv->buttons, widget, button);
Matthias Clasen's avatar
Matthias Clasen committed
386 387 388
}

static void
389 390
remove_child (GtkWidget        *widget,
              GtkStackSwitcher *self)
Matthias Clasen's avatar
Matthias Clasen committed
391
{
392 393 394 395 396 397 398 399 400 401 402 403
  GtkWidget *button;
  GtkStackSwitcherPrivate *priv;

  priv = gtk_stack_switcher_get_instance_private (self);

  g_signal_handlers_disconnect_by_func (widget, on_title_icon_visible_updated, self);
  g_signal_handlers_disconnect_by_func (widget, on_position_updated, self);
  g_signal_handlers_disconnect_by_func (widget, on_needs_attention_updated, self);

  button = g_hash_table_lookup (priv->buttons, widget);
  gtk_container_remove (GTK_CONTAINER (self), button);
  g_hash_table_remove (priv->buttons, widget);
Matthias Clasen's avatar
Matthias Clasen committed
404 405 406 407 408
}

static void
populate_switcher (GtkStackSwitcher *self)
{
409
  GtkStackSwitcherPrivate *priv;
410
  GtkWidget *widget, *button;
411 412

  priv = gtk_stack_switcher_get_instance_private (self);
413
  gtk_container_foreach (GTK_CONTAINER (priv->stack), (GtkCallback)add_child, self);
414 415 416 417 418

  widget = gtk_stack_get_visible_child (priv->stack);
  if (widget)
    {
      button = g_hash_table_lookup (priv->buttons, widget);
419
      priv->in_child_changed = TRUE;
420
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
421
      priv->in_child_changed = FALSE;
422
    }
423 424 425 426 427 428 429 430 431
}

static void
clear_switcher (GtkStackSwitcher *self)
{
  GtkStackSwitcherPrivate *priv;

  priv = gtk_stack_switcher_get_instance_private (self);
  gtk_container_foreach (GTK_CONTAINER (priv->stack), (GtkCallback)remove_child, self);
Matthias Clasen's avatar
Matthias Clasen committed
432 433 434 435 436 437 438 439 440
}

static void
on_child_changed (GtkWidget        *widget,
                  GParamSpec       *pspec,
                  GtkStackSwitcher *self)
{
  GtkWidget *child;
  GtkWidget *button;
441 442 443
  GtkStackSwitcherPrivate *priv;

  priv = gtk_stack_switcher_get_instance_private (self);
Matthias Clasen's avatar
Matthias Clasen committed
444 445

  child = gtk_stack_get_visible_child (GTK_STACK (widget));
446
  button = g_hash_table_lookup (priv->buttons, child);
Matthias Clasen's avatar
Matthias Clasen committed
447 448
  if (button != NULL)
    {
449
      priv->in_child_changed = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
450
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
451
      priv->in_child_changed = FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
452 453 454 455 456 457 458 459
    }
}

static void
on_stack_child_added (GtkContainer     *container,
                      GtkWidget        *widget,
                      GtkStackSwitcher *self)
{
460
  add_child (widget, self);
Matthias Clasen's avatar
Matthias Clasen committed
461 462 463 464 465 466 467
}

static void
on_stack_child_removed (GtkContainer     *container,
                        GtkWidget        *widget,
                        GtkStackSwitcher *self)
{
468
  remove_child (widget, self);
Matthias Clasen's avatar
Matthias Clasen committed
469 470 471 472 473
}

static void
disconnect_stack_signals (GtkStackSwitcher *switcher)
{
474
  GtkStackSwitcherPrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
475

476
  priv = gtk_stack_switcher_get_instance_private (switcher);
Matthias Clasen's avatar
Matthias Clasen committed
477 478 479 480 481 482 483 484 485
  g_signal_handlers_disconnect_by_func (priv->stack, on_stack_child_added, switcher);
  g_signal_handlers_disconnect_by_func (priv->stack, on_stack_child_removed, switcher);
  g_signal_handlers_disconnect_by_func (priv->stack, on_child_changed, switcher);
  g_signal_handlers_disconnect_by_func (priv->stack, disconnect_stack_signals, switcher);
}

static void
connect_stack_signals (GtkStackSwitcher *switcher)
{
486
  GtkStackSwitcherPrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
487

488
  priv = gtk_stack_switcher_get_instance_private (switcher);
Matthias Clasen's avatar
Matthias Clasen committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
  g_signal_connect_after (priv->stack, "add",
                          G_CALLBACK (on_stack_child_added), switcher);
  g_signal_connect_after (priv->stack, "remove",
                          G_CALLBACK (on_stack_child_removed), switcher);
  g_signal_connect (priv->stack, "notify::visible-child",
                    G_CALLBACK (on_child_changed), switcher);
  g_signal_connect_swapped (priv->stack, "destroy",
                            G_CALLBACK (disconnect_stack_signals), switcher);
}

/**
 * gtk_stack_switcher_set_stack:
 * @switcher: a #GtkStackSwitcher
 * @stack: (allow-none): a #GtkStack
 *
 * Sets the stack to control.
505 506
 *
 * Since: 3.10
Matthias Clasen's avatar
Matthias Clasen committed
507 508 509 510 511 512 513 514
 */
void
gtk_stack_switcher_set_stack (GtkStackSwitcher *switcher,
                              GtkStack         *stack)
{
  GtkStackSwitcherPrivate *priv;

  g_return_if_fail (GTK_IS_STACK_SWITCHER (switcher));
515
  g_return_if_fail (GTK_IS_STACK (stack) || stack == NULL);
Matthias Clasen's avatar
Matthias Clasen committed
516

517
  priv = gtk_stack_switcher_get_instance_private (switcher);
Matthias Clasen's avatar
Matthias Clasen committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543

  if (priv->stack == stack)
    return;

  if (priv->stack)
    {
      disconnect_stack_signals (switcher);
      clear_switcher (switcher);
      g_clear_object (&priv->stack);
    }
  if (stack)
    {
      priv->stack = g_object_ref (stack);
      populate_switcher (switcher);
      connect_stack_signals (switcher);
    }

  gtk_widget_queue_resize (GTK_WIDGET (switcher));

  g_object_notify (G_OBJECT (switcher), "stack");
}

/**
 * gtk_stack_switcher_get_stack:
 * @switcher: a #GtkStackSwitcher
 *
544 545
 * Retrieves the stack.
 * See gtk_stack_switcher_set_stack().
Matthias Clasen's avatar
Matthias Clasen committed
546
 *
547
 * Returns: (nullable) (transfer none): the stack, or %NULL if
Matthias Clasen's avatar
Matthias Clasen committed
548
 *    none has been set explicitly.
549 550
 *
 * Since: 3.10
Matthias Clasen's avatar
Matthias Clasen committed
551 552 553 554
 */
GtkStack *
gtk_stack_switcher_get_stack (GtkStackSwitcher *switcher)
{
555
  GtkStackSwitcherPrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
556 557
  g_return_val_if_fail (GTK_IS_STACK_SWITCHER (switcher), NULL);

558 559
  priv = gtk_stack_switcher_get_instance_private (switcher);
  return priv->stack;
Matthias Clasen's avatar
Matthias Clasen committed
560 561
}

562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
static void
gtk_stack_switcher_set_icon_size (GtkStackSwitcher *switcher,
                                  gint              icon_size)
{
  GtkStackSwitcherPrivate *priv;

  g_return_if_fail (GTK_IS_STACK_SWITCHER (switcher));

  priv = gtk_stack_switcher_get_instance_private (switcher);

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

      if (priv->stack != NULL)
        {
          clear_switcher (switcher);
          populate_switcher (switcher);
        }
581 582

      g_object_notify (G_OBJECT (switcher), "icon-size");
583 584 585
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
586 587 588 589 590 591 592
static void
gtk_stack_switcher_get_property (GObject      *object,
                                 guint         prop_id,
                                 GValue       *value,
                                 GParamSpec   *pspec)
{
  GtkStackSwitcher *switcher = GTK_STACK_SWITCHER (object);
593
  GtkStackSwitcherPrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
594

595
  priv = gtk_stack_switcher_get_instance_private (switcher);
Matthias Clasen's avatar
Matthias Clasen committed
596 597
  switch (prop_id)
    {
598 599 600 601
    case PROP_ICON_SIZE:
      g_value_set_int (value, priv->icon_size);
      break;

Matthias Clasen's avatar
Matthias Clasen committed
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
    case PROP_STACK:
      g_value_set_object (value, priv->stack);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_stack_switcher_set_property (GObject      *object,
                                 guint         prop_id,
                                 const GValue *value,
                                 GParamSpec   *pspec)
{
  GtkStackSwitcher *switcher = GTK_STACK_SWITCHER (object);

  switch (prop_id)
    {
622 623 624 625
    case PROP_ICON_SIZE:
      gtk_stack_switcher_set_icon_size (switcher, g_value_get_int (value));
      break;

Matthias Clasen's avatar
Matthias Clasen committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
    case PROP_STACK:
      gtk_stack_switcher_set_stack (switcher, g_value_get_object (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_stack_switcher_dispose (GObject *object)
{
  GtkStackSwitcher *switcher = GTK_STACK_SWITCHER (object);

641
  remove_switch_timer (switcher);
Matthias Clasen's avatar
Matthias Clasen committed
642 643 644 645 646
  gtk_stack_switcher_set_stack (switcher, NULL);

  G_OBJECT_CLASS (gtk_stack_switcher_parent_class)->dispose (object);
}

647 648 649 650 651 652 653 654 655 656 657 658 659
static void
gtk_stack_switcher_finalize (GObject *object)
{
  GtkStackSwitcher *switcher = GTK_STACK_SWITCHER (object);
  GtkStackSwitcherPrivate *priv;

  priv = gtk_stack_switcher_get_instance_private (switcher);

  g_hash_table_destroy (priv->buttons);

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

Matthias Clasen's avatar
Matthias Clasen committed
660 661 662 663
static void
gtk_stack_switcher_class_init (GtkStackSwitcherClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
664
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
Matthias Clasen's avatar
Matthias Clasen committed
665 666 667 668

  object_class->get_property = gtk_stack_switcher_get_property;
  object_class->set_property = gtk_stack_switcher_set_property;
  object_class->dispose = gtk_stack_switcher_dispose;
669
  object_class->finalize = gtk_stack_switcher_finalize;
Matthias Clasen's avatar
Matthias Clasen committed
670

671 672
  widget_class->drag_motion = gtk_stack_switcher_drag_motion;
  widget_class->drag_leave = gtk_stack_switcher_drag_leave;
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
  /**
   * GtkStackSwitcher:icon-size:
   *
   * Use the "icon-size" property to change the size of the image displayed
   * when a #GtkStackSwitcher is displaying icons.
   *
   * Since: 3.20
   */
  g_object_class_install_property (object_class,
                                   PROP_ICON_SIZE,
                                   g_param_spec_int ("icon-size",
                                                     P_("Icon Size"),
                                                     P_("Symbolic size to use for named icon"),
                                                     0, G_MAXINT,
                                                     GTK_ICON_SIZE_MENU,
                                                     G_PARAM_EXPLICIT_NOTIFY | GTK_PARAM_READWRITE));

Matthias Clasen's avatar
Matthias Clasen committed
690 691 692 693 694 695 696 697
  g_object_class_install_property (object_class,
                                   PROP_STACK,
                                   g_param_spec_object ("stack",
                                                        P_("Stack"),
                                                        P_("Stack"),
                                                        GTK_TYPE_STACK,
                                                        GTK_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));
698 699

  gtk_widget_class_set_css_name (widget_class, "stackswitcher");
Matthias Clasen's avatar
Matthias Clasen committed
700 701
}

702 703 704 705 706
/**
 * gtk_stack_switcher_new:
 *
 * Create a new #GtkStackSwitcher.
 *
707
 * Returns: a new #GtkStackSwitcher.
708 709 710
 *
 * Since: 3.10
 */
Matthias Clasen's avatar
Matthias Clasen committed
711 712 713
GtkWidget *
gtk_stack_switcher_new (void)
{
714
  return g_object_new (GTK_TYPE_STACK_SWITCHER, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
715
}