gtksidebar.c 14.3 KB
Newer Older
Ikey Doherty's avatar
Ikey Doherty 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
/*
 * Copyright (c) 2014 Intel Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author:
 *      Ikey Doherty <michael.i.doherty@intel.com>
 */

#include "config.h"

#include "gtksidebar.h"
25 26

#include "gtklabel.h"
Ikey Doherty's avatar
Ikey Doherty committed
27
#include "gtklistbox.h"
28
#include "gtkscrolledwindow.h"
Ikey Doherty's avatar
Ikey Doherty committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include "gtkseparator.h"
#include "gtkstylecontext.h"
#include "gtkprivate.h"
#include "gtkintl.h"

/**
 * SECTION:gtk-sidebar
 * @short_description: An automatic sidebar widget
 * @title: GtkSidebar
 *
 * A GtkSidebarWindow enables you to quickly and easily provide a consistent
 * "sidebar" object for your user interface.
 *
 * In order to use a GtkSidebar, you simply use a GtkStack to organise
 * your UI flow, and add the sidebar to your sidebar area. You can use
 * gtk_sidebar_set_stack() to connect the #GtkSidebar to the #GtkStack.
 *
 * Since: 3.16
 */
 
struct _GtkSidebarPrivate
{
51
  GtkListBox *list;
Ikey Doherty's avatar
Ikey Doherty committed
52 53 54 55 56
  GtkStack *stack;
  GHashTable *rows;
  gboolean in_child_changed;
};

57
G_DEFINE_TYPE_WITH_PRIVATE (GtkSidebar, gtk_sidebar, GTK_TYPE_BIN)
Ikey Doherty's avatar
Ikey Doherty committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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 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

enum
{
  PROP_0,
  PROP_STACK,
  N_PROPERTIES
};
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };

static void
gtk_sidebar_set_property (GObject    *object,
                          guint       prop_id,
                          const       GValue *value,
                          GParamSpec *pspec)
{
  switch (prop_id)
    {
    case PROP_STACK:
      gtk_sidebar_set_stack (GTK_SIDEBAR (object), g_value_get_object (value));
      break;

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

static void
gtk_sidebar_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  GtkSidebarPrivate *priv = gtk_sidebar_get_instance_private (GTK_SIDEBAR (object));

  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
update_header (GtkListBoxRow *row,
               GtkListBoxRow *before,
               gpointer       userdata)
{
  GtkWidget *ret = NULL;

  if (before && !gtk_list_box_row_get_header (row))
    {
      ret = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
      gtk_list_box_row_set_header (row, ret);
    }
}

static gint
sort_list (GtkListBoxRow *row1,
           GtkListBoxRow *row2,
           gpointer       userdata)
{
  GtkSidebar *sidebar = GTK_SIDEBAR (userdata);
  GtkSidebarPrivate *priv = gtk_sidebar_get_instance_private (sidebar);
  GtkWidget *item;
  GtkWidget *widget;
  gint left = 0; gint right = 0;


  if (row1)
    {
      item = gtk_bin_get_child (GTK_BIN (row1));
      widget = g_object_get_data (G_OBJECT (item), "stack-child");
      gtk_container_child_get (GTK_CONTAINER (priv->stack), widget,
                               "position", &left,
                               NULL);
    }

  if (row2)
    {
      item = gtk_bin_get_child (GTK_BIN (row2));
      widget = g_object_get_data (G_OBJECT (item), "stack-child");
      gtk_container_child_get (GTK_CONTAINER (priv->stack), widget,
                               "position", &right,
                               NULL);
    }

  if (left < right)
    return  -1;

  if (left == right)
    return 0;

  return 1;
}

static void
gtk_sidebar_row_selected (GtkListBox    *box,
160 161
                          GtkListBoxRow *row,
                          gpointer       userdata)
Ikey Doherty's avatar
Ikey Doherty committed
162
{
163
  GtkSidebar *sidebar = GTK_SIDEBAR (userdata);
Ikey Doherty's avatar
Ikey Doherty committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  GtkSidebarPrivate *priv = gtk_sidebar_get_instance_private (sidebar);
  GtkWidget *item;
  GtkWidget *widget;

  if (priv->in_child_changed)
    return;

  if (!row)
    return;

  item = gtk_bin_get_child (GTK_BIN (row));
  widget = g_object_get_data (G_OBJECT (item), "stack-child");
  gtk_stack_set_visible_child (priv->stack, widget);
}

static void
gtk_sidebar_init (GtkSidebar *sidebar)
{
  GtkStyleContext *style;
  GtkSidebarPrivate *priv;
184
  GtkWidget *sw;
Ikey Doherty's avatar
Ikey Doherty committed
185 186 187

  priv = gtk_sidebar_get_instance_private (sidebar);

188 189 190 191 192 193
  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_widget_show (sw);
  gtk_widget_set_no_show_all (sw, TRUE);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
                                  GTK_POLICY_NEVER,
                                  GTK_POLICY_AUTOMATIC);
194

195
  gtk_container_add (GTK_CONTAINER (sidebar), sw);
196 197

  priv->list = GTK_LIST_BOX (gtk_list_box_new ());
198
  gtk_widget_show (GTK_WIDGET (priv->list));
199 200

  gtk_container_add (GTK_CONTAINER (sw), GTK_WIDGET (priv->list));
201 202 203 204 205 206

  gtk_list_box_set_header_func (priv->list, update_header, sidebar, NULL);
  gtk_list_box_set_sort_func (priv->list, sort_list, sidebar, NULL);

  g_signal_connect (priv->list, "row-selected",
                    G_CALLBACK (gtk_sidebar_row_selected), sidebar);
Ikey Doherty's avatar
Ikey Doherty committed
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

  style = gtk_widget_get_style_context (GTK_WIDGET (sidebar));
  gtk_style_context_add_class (style, "sidebar");

  priv->rows = g_hash_table_new (NULL, NULL);
}

static void
update_row (GtkSidebar *sidebar,
            GtkWidget  *widget,
            GtkWidget  *row)
{
  GtkSidebarPrivate *priv = gtk_sidebar_get_instance_private (sidebar);
  GtkWidget *item;
  gchar *title;
  gboolean needs_attention;
  GtkStyleContext *context;

  gtk_container_child_get (GTK_CONTAINER (priv->stack), widget,
                           "title", &title,
                           "needs-attention", &needs_attention,
                           NULL);

  item = gtk_bin_get_child (GTK_BIN (row));
  gtk_label_set_text (GTK_LABEL (item), title);

  gtk_widget_set_visible (row, gtk_widget_get_visible (widget) && title != NULL);

  context = gtk_widget_get_style_context (row);
  if (needs_attention)
     gtk_style_context_add_class (context, GTK_STYLE_CLASS_NEEDS_ATTENTION);
  else
    gtk_style_context_remove_class (context, GTK_STYLE_CLASS_NEEDS_ATTENTION);

  g_free (title);
}

static void
on_position_updated (GtkWidget  *widget,
                     GParamSpec *pspec,
                     GtkSidebar *sidebar)
{
249 250 251
  GtkSidebarPrivate *priv = gtk_sidebar_get_instance_private (sidebar);

  gtk_list_box_invalidate_sort (priv->list);
Ikey Doherty's avatar
Ikey Doherty committed
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
}

static void
on_child_updated (GtkWidget  *widget,
                  GParamSpec *pspec,
                  GtkSidebar *sidebar)
{
  GtkSidebarPrivate *priv = gtk_sidebar_get_instance_private (sidebar);
  GtkWidget *row;

  row = g_hash_table_lookup (priv->rows, widget);
  update_row (sidebar, widget, row);
}

static void
add_child (GtkWidget  *widget,
           GtkSidebar *sidebar)
{
  GtkSidebarPrivate *priv = gtk_sidebar_get_instance_private (sidebar);
  GtkStyleContext *style;
  GtkWidget *item;
  GtkWidget *row;

  /* Check we don't actually already know about this widget */
  if (g_hash_table_lookup (priv->rows, widget))
    return;

  /* Make a pretty item when we add kids */
  item = gtk_label_new ("");
  gtk_widget_set_halign (item, GTK_ALIGN_START);
  gtk_widget_set_valign (item, GTK_ALIGN_CENTER);
  row = gtk_list_box_row_new ();
  gtk_container_add (GTK_CONTAINER (row), item);
  gtk_widget_show (item);

  update_row (sidebar, widget, row);

  /* Fix up styling */
  style = gtk_widget_get_style_context (row);
  gtk_style_context_add_class (style, "sidebar-item");

  /* Hook up for events */
  g_signal_connect (widget, "child-notify::title",
                    G_CALLBACK (on_child_updated), sidebar);
  g_signal_connect (widget, "child-notify::needs-attention",
                    G_CALLBACK (on_child_updated), sidebar);
  g_signal_connect (widget, "notify::visible",
                    G_CALLBACK (on_child_updated), sidebar);
  g_signal_connect (widget, "child-notify::position",
                    G_CALLBACK (on_position_updated), sidebar);

  g_object_set_data (G_OBJECT (item), "stack-child", widget);
  g_hash_table_insert (priv->rows, widget, row);
305
  gtk_container_add (GTK_CONTAINER (priv->list), row);
Ikey Doherty's avatar
Ikey Doherty committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
}

static void
remove_child (GtkWidget  *widget,
              GtkSidebar *sidebar)
{
  GtkSidebarPrivate *priv = gtk_sidebar_get_instance_private (sidebar);
  GtkWidget *row;

  row = g_hash_table_lookup (priv->rows, widget);
  if (!row)
    return;

  g_signal_handlers_disconnect_by_func (widget, on_child_updated, sidebar);
  g_signal_handlers_disconnect_by_func (widget, on_position_updated, sidebar);

322
  gtk_container_remove (GTK_CONTAINER (priv->list), row);
Ikey Doherty's avatar
Ikey Doherty committed
323 324 325 326 327 328 329
  g_hash_table_remove (priv->rows, widget);
}

static void
populate_sidebar (GtkSidebar *sidebar)
{
  GtkSidebarPrivate *priv = gtk_sidebar_get_instance_private (sidebar);
330
  GtkWidget *widget, *row;
Ikey Doherty's avatar
Ikey Doherty committed
331 332

  gtk_container_foreach (GTK_CONTAINER (priv->stack), (GtkCallback)add_child, sidebar);
333 334 335 336 337 338 339

  widget = gtk_stack_get_visible_child (priv->stack);
  if (widget)
    {
      row = g_hash_table_lookup (priv->rows, widget);
      gtk_list_box_select_row (priv->list, GTK_LIST_BOX_ROW (row));
    }
Ikey Doherty's avatar
Ikey Doherty committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
}

static void
clear_sidebar (GtkSidebar *sidebar)
{
  GtkSidebarPrivate *priv = gtk_sidebar_get_instance_private (sidebar);

  gtk_container_foreach (GTK_CONTAINER (priv->stack), (GtkCallback)remove_child, sidebar);
}

static void
on_child_changed (GtkWidget  *widget,
                  GParamSpec *pspec,
                  GtkSidebar *sidebar)
{
  GtkSidebarPrivate *priv = gtk_sidebar_get_instance_private (sidebar);
  GtkWidget *child;
  GtkWidget *row;

  child = gtk_stack_get_visible_child (GTK_STACK (widget));
  row = g_hash_table_lookup (priv->rows, child);
  if (row != NULL)
    {
      priv->in_child_changed = TRUE;
364
      gtk_list_box_select_row (priv->list, GTK_LIST_BOX_ROW (row));
Ikey Doherty's avatar
Ikey Doherty committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 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 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
      priv->in_child_changed = FALSE;
    }
}

static void
on_stack_child_added (GtkContainer *container,
                      GtkWidget    *widget,
                      GtkSidebar   *sidebar)
{
  add_child (widget, sidebar);
}

static void
on_stack_child_removed (GtkContainer *container,
                        GtkWidget    *widget,
                        GtkSidebar   *sidebar)
{
  remove_child (widget, sidebar);
}

static void
disconnect_stack_signals (GtkSidebar *sidebar)
{
  GtkSidebarPrivate *priv = gtk_sidebar_get_instance_private (sidebar);

  g_signal_handlers_disconnect_by_func (priv->stack, on_stack_child_added, sidebar);
  g_signal_handlers_disconnect_by_func (priv->stack, on_stack_child_removed, sidebar);
  g_signal_handlers_disconnect_by_func (priv->stack, on_child_changed, sidebar);
  g_signal_handlers_disconnect_by_func (priv->stack, disconnect_stack_signals, sidebar);
}

static void
connect_stack_signals (GtkSidebar *sidebar)
{
  GtkSidebarPrivate *priv = gtk_sidebar_get_instance_private (sidebar);

  g_signal_connect_after (priv->stack, "add",
                          G_CALLBACK (on_stack_child_added), sidebar);
  g_signal_connect_after (priv->stack, "remove",
                          G_CALLBACK (on_stack_child_removed), sidebar);
  g_signal_connect (priv->stack, "notify::visible-child",
                    G_CALLBACK (on_child_changed), sidebar);
  g_signal_connect_swapped (priv->stack, "destroy",
                            G_CALLBACK (disconnect_stack_signals), sidebar);
}

static void
gtk_sidebar_dispose (GObject *object)
{
  GtkSidebar *sidebar = GTK_SIDEBAR (object);

  gtk_sidebar_set_stack (sidebar, NULL);

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

static void
gtk_sidebar_finalize (GObject *object)
{
  GtkSidebar *sidebar = GTK_SIDEBAR (object);
  GtkSidebarPrivate *priv = gtk_sidebar_get_instance_private (sidebar);

  g_hash_table_destroy (priv->rows);

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

static void
gtk_sidebar_class_init (GtkSidebarClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose = gtk_sidebar_dispose;
  object_class->finalize = gtk_sidebar_finalize;
  object_class->set_property = gtk_sidebar_set_property;
  object_class->get_property = gtk_sidebar_get_property;

  obj_properties[PROP_STACK] =
443 444 445 446
      g_param_spec_object (I_("stack"), P_("Stack"),
                           P_("Associated stack for this GtkSidebar"),
                           GTK_TYPE_STACK,
                           G_PARAM_READWRITE|G_PARAM_STATIC_STRINGS|G_PARAM_EXPLICIT_NOTIFY);
Ikey Doherty's avatar
Ikey Doherty committed
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

  g_object_class_install_properties (object_class, N_PROPERTIES, obj_properties);
}

/**
 * gtk_sidebar_new:
 *
 * Creates a new sidebar.
 *
 * Returns: the new #GtkSidebar
 *
 * Since: 3.16
 */
GtkWidget *
gtk_sidebar_new (void)
{
  return GTK_WIDGET (g_object_new (GTK_TYPE_SIDEBAR, NULL));
}

/**
 * gtk_sidebar_set_stack:
 * @sidebar: a #GtkSidebar
 * @stack: a #GtkStack
 *
 * Set the #GtkStack associated with this #GtkSidebar.
 *
 * The sidebar widget will automatically update according to the order
 * (packing) and items within the given #GtkStack.
 *
 * Since: 3.16
 */
void
gtk_sidebar_set_stack (GtkSidebar *sidebar,
                       GtkStack   *stack)
{
  GtkSidebarPrivate *priv;

  g_return_if_fail (GTK_IS_SIDEBAR (sidebar));
  g_return_if_fail (GTK_IS_STACK (stack) || stack == NULL);

  priv = gtk_sidebar_get_instance_private (sidebar);

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

  if (priv->stack)
    {
      disconnect_stack_signals (sidebar);
      clear_sidebar (sidebar);
      g_clear_object (&priv->stack);
    }
  if (stack)
    {
      priv->stack = g_object_ref (stack);
      populate_sidebar (sidebar);
      connect_stack_signals (sidebar);
    }

  gtk_widget_queue_resize (GTK_WIDGET (sidebar));

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

/**
 * gtk_sidebar_get_stack:
 * @sidebar: a #GtkSidebar
 *
 * Returns: (transfer full): the associated #GtkStack
 *
 * Since: 3.16
 */
GtkStack *
gtk_sidebar_get_stack (GtkSidebar *sidebar)
{
  GtkSidebarPrivate *priv;

Benjamin Otte's avatar
Benjamin Otte committed
523
  g_return_val_if_fail (GTK_IS_SIDEBAR (sidebar), NULL);
Ikey Doherty's avatar
Ikey Doherty committed
524 525 526 527 528

  priv = gtk_sidebar_get_instance_private (sidebar);

  return GTK_STACK (priv->stack);
}