gtkstackswitcher.c 15.5 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 24
#include "gtkradiobutton.h"
#include "gtklabel.h"
#include "gtkorientable.h"
Matthias Clasen's avatar
Matthias Clasen committed
25 26 27
#include "gtkprivate.h"
#include "gtkintl.h"

28 29
/**
 * SECTION:gtkstackswitcher
30
 * @Short_description: A controller for GtkStack
31 32 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
typedef struct _GtkStackSwitcherPrivate GtkStackSwitcherPrivate;
Matthias Clasen's avatar
Matthias Clasen committed
48 49 50 51 52 53 54 55 56 57 58 59
struct _GtkStackSwitcherPrivate
{
  GtkStack *stack;
  GHashTable *buttons;
  gboolean in_child_changed;
};

enum {
  PROP_0,
  PROP_STACK
};

60
G_DEFINE_TYPE_WITH_PRIVATE (GtkStackSwitcher, gtk_stack_switcher, GTK_TYPE_BOX)
Matthias Clasen's avatar
Matthias Clasen committed
61 62 63 64 65 66 67

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

68
  priv = gtk_stack_switcher_get_instance_private (switcher);
Matthias Clasen's avatar
Matthias Clasen committed
69 70 71 72 73

  priv->stack = NULL;
  priv->buttons = g_hash_table_new (g_direct_hash, g_direct_equal);

  context = gtk_widget_get_style_context (GTK_WIDGET (switcher));
74
  gtk_style_context_add_class (context, "stack-switcher");
Matthias Clasen's avatar
Matthias Clasen committed
75 76 77 78 79 80 81 82 83 84
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_LINKED);

  gtk_orientable_set_orientation (GTK_ORIENTABLE (switcher), GTK_ORIENTATION_HORIZONTAL);
}

static void
on_button_clicked (GtkWidget        *widget,
                   GtkStackSwitcher *self)
{
  GtkWidget *child;
85 86 87
  GtkStackSwitcherPrivate *priv;

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

89
  if (!priv->in_child_changed)
Matthias Clasen's avatar
Matthias Clasen committed
90 91
    {
      child = g_object_get_data (G_OBJECT (widget), "stack-child");
92
      gtk_stack_set_visible_child (priv->stack, child);
Matthias Clasen's avatar
Matthias Clasen committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    }
}

static void
rebuild_child (GtkWidget   *self,
               const gchar *icon_name,
               const gchar *title)
{
  GtkStyleContext *context;
  GtkWidget *button_child;

  gtk_widget_set_valign (GTK_WIDGET (self), GTK_ALIGN_CENTER);

  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)
    {
      button_child = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
      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);

126 127
      gtk_widget_set_tooltip_text (GTK_WIDGET (self), NULL);

Matthias Clasen's avatar
Matthias Clasen committed
128 129 130 131 132 133
      gtk_style_context_remove_class (context, "image-button");
      gtk_style_context_add_class (context, "text-button");
    }

  if (button_child)
    {
134
      gtk_widget_set_halign (GTK_WIDGET (button_child), GTK_ALIGN_CENTER);
Matthias Clasen's avatar
Matthias Clasen committed
135 136 137 138 139
      gtk_widget_show_all (button_child);
      gtk_container_add (GTK_CONTAINER (self), button_child);
    }
}

140
static void
Matthias Clasen's avatar
Matthias Clasen committed
141
update_needs_attention (GtkWidget *widget, GtkWidget *button, gpointer data)
142 143 144 145 146 147 148 149 150 151 152
{
  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);
153
  if (needs_attention)
154 155 156 157 158
    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
159 160 161 162 163 164 165
static void
update_button (GtkStackSwitcher *self,
               GtkWidget        *widget,
               GtkWidget        *button)
{
  gchar *title;
  gchar *icon_name;
166 167 168
  GtkStackSwitcherPrivate *priv;

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

170
  gtk_container_child_get (GTK_CONTAINER (priv->stack), widget,
Matthias Clasen's avatar
Matthias Clasen committed
171 172 173 174 175 176
                           "title", &title,
                           "icon-name", &icon_name,
                           NULL);

  rebuild_child (button, icon_name, title);

177
  gtk_widget_set_visible (button, gtk_widget_get_visible (widget) && (title != NULL || icon_name != NULL));
Matthias Clasen's avatar
Matthias Clasen committed
178 179 180 181 182 183 184 185

  if (icon_name != NULL)
    gtk_widget_set_size_request (button, -1, -1);
  else
    gtk_widget_set_size_request (button, 100, -1);

  g_free (title);
  g_free (icon_name);
186 187

  update_needs_attention (widget, button, priv->stack);
Matthias Clasen's avatar
Matthias Clasen committed
188 189 190
}

static void
191 192 193
on_title_icon_visible_updated (GtkWidget        *widget,
                               GParamSpec       *pspec,
                               GtkStackSwitcher *self)
Matthias Clasen's avatar
Matthias Clasen committed
194 195
{
  GtkWidget *button;
196 197 198
  GtkStackSwitcherPrivate *priv;

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

200
  button = g_hash_table_lookup (priv->buttons, widget);
Matthias Clasen's avatar
Matthias Clasen committed
201 202 203 204 205 206 207 208 209 210
  update_button (self, widget, button);
}

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

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

215 216 217
  button = g_hash_table_lookup (priv->buttons, widget);

  gtk_container_child_get (GTK_CONTAINER (priv->stack), widget,
Matthias Clasen's avatar
Matthias Clasen committed
218 219 220 221 222 223
                           "position", &position,
                           NULL);

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

224 225 226 227 228 229 230 231 232 233 234 235 236 237
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);
}

Matthias Clasen's avatar
Matthias Clasen committed
238
static void
239 240
add_child (GtkWidget        *widget,
           GtkStackSwitcher *self)
Matthias Clasen's avatar
Matthias Clasen committed
241 242 243
{
  GtkWidget *button;
  GList *group;
244 245 246
  GtkStackSwitcherPrivate *priv;

  priv = gtk_stack_switcher_get_instance_private (self);
Matthias Clasen's avatar
Matthias Clasen committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

  button = gtk_radio_button_new (NULL);
  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);
264 265 266
  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
267
  g_signal_connect (widget, "child-notify::position", G_CALLBACK (on_position_updated), self);
268
  g_signal_connect (widget, "child-notify::needs-attention", G_CALLBACK (on_needs_attention_updated), self);
Matthias Clasen's avatar
Matthias Clasen committed
269

270
  g_hash_table_insert (priv->buttons, widget, button);
Matthias Clasen's avatar
Matthias Clasen committed
271 272 273
}

static void
274 275
remove_child (GtkWidget        *widget,
              GtkStackSwitcher *self)
Matthias Clasen's avatar
Matthias Clasen committed
276
{
277 278 279 280 281 282 283 284 285 286 287 288
  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
289 290 291 292 293
}

static void
populate_switcher (GtkStackSwitcher *self)
{
294
  GtkStackSwitcherPrivate *priv;
295
  GtkWidget *widget, *button;
296 297

  priv = gtk_stack_switcher_get_instance_private (self);
298
  gtk_container_foreach (GTK_CONTAINER (priv->stack), (GtkCallback)add_child, self);
299 300 301 302 303 304 305

  widget = gtk_stack_get_visible_child (priv->stack);
  if (widget)
    {
      button = g_hash_table_lookup (priv->buttons, widget);
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
    }
306 307 308 309 310 311 312 313 314
}

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
315 316 317 318 319 320 321 322 323
}

static void
on_child_changed (GtkWidget        *widget,
                  GParamSpec       *pspec,
                  GtkStackSwitcher *self)
{
  GtkWidget *child;
  GtkWidget *button;
324 325 326
  GtkStackSwitcherPrivate *priv;

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

  child = gtk_stack_get_visible_child (GTK_STACK (widget));
329
  button = g_hash_table_lookup (priv->buttons, child);
Matthias Clasen's avatar
Matthias Clasen committed
330 331
  if (button != NULL)
    {
332
      priv->in_child_changed = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
333
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
334
      priv->in_child_changed = FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
335 336 337 338 339 340 341 342
    }
}

static void
on_stack_child_added (GtkContainer     *container,
                      GtkWidget        *widget,
                      GtkStackSwitcher *self)
{
343
  add_child (widget, self);
Matthias Clasen's avatar
Matthias Clasen committed
344 345 346 347 348 349 350
}

static void
on_stack_child_removed (GtkContainer     *container,
                        GtkWidget        *widget,
                        GtkStackSwitcher *self)
{
351
  remove_child (widget, self);
Matthias Clasen's avatar
Matthias Clasen committed
352 353 354 355 356
}

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

359
  priv = gtk_stack_switcher_get_instance_private (switcher);
Matthias Clasen's avatar
Matthias Clasen committed
360 361 362 363 364 365 366 367 368
  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)
{
369
  GtkStackSwitcherPrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
370

371
  priv = gtk_stack_switcher_get_instance_private (switcher);
Matthias Clasen's avatar
Matthias Clasen committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  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.
388 389
 *
 * Since: 3.10
Matthias Clasen's avatar
Matthias Clasen committed
390 391 392 393 394 395 396 397
 */
void
gtk_stack_switcher_set_stack (GtkStackSwitcher *switcher,
                              GtkStack         *stack)
{
  GtkStackSwitcherPrivate *priv;

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

400
  priv = gtk_stack_switcher_get_instance_private (switcher);
Matthias Clasen's avatar
Matthias Clasen committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426

  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
 *
427 428
 * Retrieves the stack.
 * See gtk_stack_switcher_set_stack().
Matthias Clasen's avatar
Matthias Clasen committed
429
 *
430
 * Returns: (transfer none): the stack, or %NULL if
Matthias Clasen's avatar
Matthias Clasen committed
431
 *    none has been set explicitly.
432 433
 *
 * Since: 3.10
Matthias Clasen's avatar
Matthias Clasen committed
434 435 436 437
 */
GtkStack *
gtk_stack_switcher_get_stack (GtkStackSwitcher *switcher)
{
438
  GtkStackSwitcherPrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
439 440
  g_return_val_if_fail (GTK_IS_STACK_SWITCHER (switcher), NULL);

441 442
  priv = gtk_stack_switcher_get_instance_private (switcher);
  return priv->stack;
Matthias Clasen's avatar
Matthias Clasen committed
443 444 445 446 447 448 449 450 451
}

static void
gtk_stack_switcher_get_property (GObject      *object,
                                 guint         prop_id,
                                 GValue       *value,
                                 GParamSpec   *pspec)
{
  GtkStackSwitcher *switcher = GTK_STACK_SWITCHER (object);
452
  GtkStackSwitcherPrivate *priv;
Matthias Clasen's avatar
Matthias Clasen committed
453

454
  priv = gtk_stack_switcher_get_instance_private (switcher);
Matthias Clasen's avatar
Matthias Clasen committed
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
  switch (prop_id)
    {
    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)
    {
    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);

  gtk_stack_switcher_set_stack (switcher, NULL);

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

497 498 499 500 501 502 503 504 505 506 507 508 509
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
510 511 512 513 514 515 516 517
static void
gtk_stack_switcher_class_init (GtkStackSwitcherClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  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;
518
  object_class->finalize = gtk_stack_switcher_finalize;
Matthias Clasen's avatar
Matthias Clasen committed
519 520 521 522 523 524 525 526 527 528 529

  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));
}

530 531 532 533 534
/**
 * gtk_stack_switcher_new:
 *
 * Create a new #GtkStackSwitcher.
 *
535
 * Returns: a new #GtkStackSwitcher.
536 537 538
 *
 * Since: 3.10
 */
Matthias Clasen's avatar
Matthias Clasen committed
539 540 541
GtkWidget *
gtk_stack_switcher_new (void)
{
542
  return g_object_new (GTK_TYPE_STACK_SWITCHER, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
543
}