gtklistbox.c 128 KB
Newer Older
Alexander Larsson's avatar
Alexander Larsson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2012 Alexander Larsson <alexl@redhat.com>
 *
 * This library 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 library 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 library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

20
#include "gtkactionhelper.h"
21
#include "gtkadjustmentprivate.h"
22
#include "gtkcssnodeprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
23 24 25 26 27
#include "gtklistbox.h"
#include "gtkwidget.h"
#include "gtkmarshalers.h"
#include "gtkprivate.h"
#include "gtkintl.h"
Timm Bäder's avatar
Timm Bäder committed
28
#include "gtkwidgetprivate.h"
29
#include "gtkcontainerprivate.h"
30
#include "gtkcsscustomgadgetprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
31

Alexander Larsson's avatar
Alexander Larsson committed
32 33 34 35 36
#include <float.h>
#include <math.h>
#include <string.h>

#include "a11y/gtklistboxaccessibleprivate.h"
37
#include "a11y/gtklistboxrowaccessible.h"
Alexander Larsson's avatar
Alexander Larsson committed
38 39 40 41 42

/**
 * SECTION:gtklistbox
 * @Short_description: A list container
 * @Title: GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
43
 * @See_also: #GtkScrolledWindow
Alexander Larsson's avatar
Alexander Larsson committed
44
 *
Alexander Larsson's avatar
Alexander Larsson committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
 * A GtkListBox is a vertical container that contains GtkListBoxRow
 * children. These rows can by dynamically sorted and filtered, and
 * headers can be added dynamically depending on the row content.
 * It also allows keyboard and mouse navigation and selection like
 * a typical list.
 *
 * Using GtkListBox is often an alternative to #GtkTreeView, especially
 * when the list contents has a more complicated layout than what is allowed
 * by a #GtkCellRenderer, or when the contents is interactive (i.e. has a
 * button in it).
 *
 * Although a #GtkListBox must have only #GtkListBoxRow children you can
 * add any kind of widget to it via gtk_container_add(), and a #GtkListBoxRow
 * widget will automatically be inserted between the list and the widget.
 *
60 61 62 63 64
 * #GtkListBoxRows can be marked as activatable or selectable. If a row
 * is activatable, #GtkListBox::row-activated will be emitted for it when
 * the user tries to activate it. If it is selectable, the row will be marked
 * as selected when the user tries to select it.
 *
Alexander Larsson's avatar
Alexander Larsson committed
65
 * The GtkListBox widget was added in GTK+ 3.10.
66 67 68
 *
 * # CSS nodes
 *
69 70 71 72 73 74 75
 * |[<!-- language="plain" -->
 * list
 * ╰── row[.activatable]
 * ]|
 *
 * GtkListBox uses a single CSS node named list. Each GtkListBoxRow uses
 * a single CSS node named row. The row nodes get the .activatable
76
 * style class added when appropriate.
Matthias Clasen's avatar
Matthias Clasen committed
77
 */
Alexander Larsson's avatar
Alexander Larsson committed
78

79
typedef struct
Alexander Larsson's avatar
Alexander Larsson committed
80 81
{
  GSequence *children;
82
  GHashTable *header_hash;
Alexander Larsson's avatar
Alexander Larsson committed
83

84 85
  GtkWidget *placeholder;

86 87
  GtkCssGadget *gadget;

Alexander Larsson's avatar
Alexander Larsson committed
88 89 90 91 92 93 94 95
  GtkListBoxSortFunc sort_func;
  gpointer sort_func_target;
  GDestroyNotify sort_func_target_destroy_notify;

  GtkListBoxFilterFunc filter_func;
  gpointer filter_func_target;
  GDestroyNotify filter_func_target_destroy_notify;

96 97 98
  GtkListBoxUpdateHeaderFunc update_header_func;
  gpointer update_header_func_target;
  GDestroyNotify update_header_func_target_destroy_notify;
Alexander Larsson's avatar
Alexander Larsson committed
99 100 101 102 103 104 105 106 107 108 109 110 111

  GtkListBoxRow *selected_row;
  GtkListBoxRow *prelight_row;
  GtkListBoxRow *cursor_row;

  gboolean active_row_active;
  GtkListBoxRow *active_row;

  GtkSelectionMode selection_mode;

  GtkAdjustment *adjustment;
  gboolean activate_single_click;

112 113
  GtkGesture *multipress_gesture;

Alexander Larsson's avatar
Alexander Larsson committed
114 115
  /* DnD */
  GtkListBoxRow *drag_highlighted_row;
116 117

  int n_visible_rows;
118
  gboolean in_widget;
119 120 121 122 123

  GListModel *bound_model;
  GtkListBoxCreateWidgetFunc create_widget_func;
  gpointer create_widget_func_data;
  GDestroyNotify create_widget_func_data_destroy;
124
} GtkListBoxPrivate;
Alexander Larsson's avatar
Alexander Larsson committed
125

126
typedef struct
Alexander Larsson's avatar
Alexander Larsson committed
127 128
{
  GSequenceIter *iter;
129
  GtkWidget *header;
130
  GtkCssGadget *gadget;
131
  GtkActionHelper *action_helper;
Alexander Larsson's avatar
Alexander Larsson committed
132 133
  gint y;
  gint height;
134 135 136
  guint visible     :1;
  guint selected    :1;
  guint activatable :1;
137
  guint selectable  :1;
138
} GtkListBoxRowPrivate;
Alexander Larsson's avatar
Alexander Larsson committed
139 140 141 142 143 144 145

enum {
  ROW_SELECTED,
  ROW_ACTIVATED,
  ACTIVATE_CURSOR_ROW,
  TOGGLE_CURSOR_ROW,
  MOVE_CURSOR,
146 147 148
  SELECTED_ROWS_CHANGED,
  SELECT_ALL,
  UNSELECT_ALL,
Alexander Larsson's avatar
Alexander Larsson committed
149 150 151
  LAST_SIGNAL
};

152 153 154 155 156
enum {
  ROW__ACTIVATE,
  ROW__LAST_SIGNAL
};

157
enum {
Alexander Larsson's avatar
Alexander Larsson committed
158 159 160 161 162 163
  PROP_0,
  PROP_SELECTION_MODE,
  PROP_ACTIVATE_ON_SINGLE_CLICK,
  LAST_PROPERTY
};

164 165 166
enum {
  ROW_PROP_0,
  ROW_PROP_ACTIVATABLE,
167
  ROW_PROP_SELECTABLE,
168 169 170 171 172 173

  /* actionable properties */
  ROW_PROP_ACTION_NAME,
  ROW_PROP_ACTION_TARGET,

  LAST_ROW_PROPERTY = ROW_PROP_ACTION_NAME
174 175
};

176 177 178
#define BOX_PRIV(box) ((GtkListBoxPrivate*)gtk_list_box_get_instance_private ((GtkListBox*)(box)))
#define ROW_PRIV(row) ((GtkListBoxRowPrivate*)gtk_list_box_row_get_instance_private ((GtkListBoxRow*)(row)))

179 180 181
static void     gtk_list_box_buildable_interface_init   (GtkBuildableIface *iface);

static void     gtk_list_box_row_actionable_iface_init  (GtkActionableInterface *iface);
182 183 184 185 186

G_DEFINE_TYPE_WITH_CODE (GtkListBox, gtk_list_box, GTK_TYPE_CONTAINER,
                         G_ADD_PRIVATE (GtkListBox)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                gtk_list_box_buildable_interface_init))
187 188 189
G_DEFINE_TYPE_WITH_CODE (GtkListBoxRow, gtk_list_box_row, GTK_TYPE_BIN,
                         G_ADD_PRIVATE (GtkListBoxRow)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIONABLE, gtk_list_box_row_actionable_iface_init))
Alexander Larsson's avatar
Alexander Larsson committed
190

191 192
static void                 gtk_list_box_apply_filter_all             (GtkListBox          *box);
static void                 gtk_list_box_update_header                (GtkListBox          *box,
Alexander Larsson's avatar
Alexander Larsson committed
193
                                                                       GSequenceIter       *iter);
194 195 196
static GSequenceIter *      gtk_list_box_get_next_visible             (GtkListBox          *box,
                                                                       GSequenceIter       *iter);
static void                 gtk_list_box_apply_filter                 (GtkListBox          *box,
Alexander Larsson's avatar
Alexander Larsson committed
197 198 199 200 201 202
                                                                       GtkListBoxRow       *row);
static void                 gtk_list_box_add_move_binding             (GtkBindingSet       *binding_set,
                                                                       guint                keyval,
                                                                       GdkModifierType      modmask,
                                                                       GtkMovementStep      step,
                                                                       gint                 count);
203
static void                 gtk_list_box_update_cursor                (GtkListBox          *box,
204 205
                                                                       GtkListBoxRow       *row,
                                                                       gboolean             grab_focus);
206
static void                 gtk_list_box_update_prelight              (GtkListBox          *box,
Alexander Larsson's avatar
Alexander Larsson committed
207
                                                                       GtkListBoxRow       *row);
208
static void                 gtk_list_box_update_active                (GtkListBox          *box,
Alexander Larsson's avatar
Alexander Larsson committed
209
                                                                       GtkListBoxRow       *row);
210
static gboolean             gtk_list_box_enter_notify_event           (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
211
                                                                       GdkEventCrossing    *event);
212
static gboolean             gtk_list_box_leave_notify_event           (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
213
                                                                       GdkEventCrossing    *event);
214
static gboolean             gtk_list_box_motion_notify_event          (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
215
                                                                       GdkEventMotion      *event);
216 217
static void                 gtk_list_box_show                         (GtkWidget           *widget);
static gboolean             gtk_list_box_focus                        (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
218
                                                                       GtkDirectionType     direction);
219 220 221 222
static GSequenceIter*       gtk_list_box_get_previous_visible         (GtkListBox          *box,
                                                                       GSequenceIter       *iter);
static GtkListBoxRow       *gtk_list_box_get_first_focusable          (GtkListBox          *box);
static GtkListBoxRow       *gtk_list_box_get_last_focusable           (GtkListBox          *box);
223
static gboolean             gtk_list_box_draw                         (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
224
                                                                       cairo_t             *cr);
225 226
static void                 gtk_list_box_realize                      (GtkWidget           *widget);
static void                 gtk_list_box_add                          (GtkContainer        *container,
Alexander Larsson's avatar
Alexander Larsson committed
227
                                                                       GtkWidget           *widget);
228
static void                 gtk_list_box_remove                       (GtkContainer        *container,
Alexander Larsson's avatar
Alexander Larsson committed
229
                                                                       GtkWidget           *widget);
230
static void                 gtk_list_box_forall                       (GtkContainer        *container,
Alexander Larsson's avatar
Alexander Larsson committed
231 232
                                                                       gboolean             include_internals,
                                                                       GtkCallback          callback,
Matthias Clasen's avatar
Matthias Clasen committed
233
                                                                       gpointer             callback_target);
234
static void                 gtk_list_box_compute_expand               (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
235 236
                                                                       gboolean            *hexpand,
                                                                       gboolean            *vexpand);
237 238 239
static GType                gtk_list_box_child_type                   (GtkContainer        *container);
static GtkSizeRequestMode   gtk_list_box_get_request_mode             (GtkWidget           *widget);
static void                 gtk_list_box_size_allocate                (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
240
                                                                       GtkAllocation       *allocation);
241
static void                 gtk_list_box_drag_leave                   (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
242 243
                                                                       GdkDragContext      *context,
                                                                       guint                time_);
244 245 246
static void                 gtk_list_box_activate_cursor_row          (GtkListBox          *box);
static void                 gtk_list_box_toggle_cursor_row            (GtkListBox          *box);
static void                 gtk_list_box_move_cursor                  (GtkListBox          *box,
Alexander Larsson's avatar
Alexander Larsson committed
247 248 249
                                                                       GtkMovementStep      step,
                                                                       gint                 count);
static void                 gtk_list_box_finalize                     (GObject             *obj);
250
static void                 gtk_list_box_parent_set                   (GtkWidget           *widget,
251
                                                                       GtkWidget           *prev_parent);
Alexander Larsson's avatar
Alexander Larsson committed
252

253 254 255 256 257 258 259 260 261 262 263 264 265 266
static void                 gtk_list_box_get_preferred_height           (GtkWidget           *widget,
                                                                         gint                *minimum_height,
                                                                         gint                *natural_height);
static void                 gtk_list_box_get_preferred_height_for_width (GtkWidget           *widget,
                                                                         gint                 width,
                                                                         gint                *minimum_height,
                                                                         gint                *natural_height);
static void                 gtk_list_box_get_preferred_width            (GtkWidget           *widget,
                                                                         gint                *minimum_width,
                                                                         gint                *natural_width);
static void                 gtk_list_box_get_preferred_width_for_height (GtkWidget           *widget,
                                                                         gint                 height,
                                                                         gint                *minimum_width,
                                                                         gint                *natural_width);
Alexander Larsson's avatar
Alexander Larsson committed
267

268 269 270 271 272 273 274 275 276
static void                 gtk_list_box_select_row_internal            (GtkListBox          *box,
                                                                         GtkListBoxRow       *row);
static void                 gtk_list_box_unselect_row_internal          (GtkListBox          *box,
                                                                         GtkListBoxRow       *row);
static void                 gtk_list_box_select_all_between             (GtkListBox          *box,
                                                                         GtkListBoxRow       *row1,
                                                                         GtkListBoxRow       *row2,
                                                                         gboolean             modify);
static gboolean             gtk_list_box_unselect_all_internal          (GtkListBox          *box);
277
static void                 gtk_list_box_selected_rows_changed          (GtkListBox          *box);
278

279 280 281 282 283 284 285 286 287 288 289
static void gtk_list_box_multipress_gesture_pressed  (GtkGestureMultiPress *gesture,
                                                      guint                 n_press,
                                                      gdouble               x,
                                                      gdouble               y,
                                                      GtkListBox           *box);
static void gtk_list_box_multipress_gesture_released (GtkGestureMultiPress *gesture,
                                                      guint                 n_press,
                                                      gdouble               x,
                                                      gdouble               y,
                                                      GtkListBox           *box);

290 291 292
static void gtk_list_box_update_row_styles (GtkListBox    *box);
static void gtk_list_box_update_row_style  (GtkListBox    *box,
                                            GtkListBoxRow *row);
293

294 295 296 297 298 299
static void                 gtk_list_box_bound_model_changed            (GListModel          *list,
                                                                         guint                position,
                                                                         guint                removed,
                                                                         guint                added,
                                                                         gpointer             user_data);

300
static void                 gtk_list_box_check_model_compat             (GtkListBox          *box);
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

static void     gtk_list_box_measure    (GtkCssGadget        *gadget,
                                          GtkOrientation       orientation,
                                          gint                 for_size,
                                          gint                *minimum_size,
                                          gint                *natural_size,
                                          gint                *minimum_baseline,
                                          gint                *natural_baseline,
                                          gpointer             data);
static void     gtk_list_box_allocate    (GtkCssGadget        *gadget,
                                          const GtkAllocation *allocation,
                                          int                  baseline,
                                          GtkAllocation       *out_clip,
                                          gpointer             data);
static gboolean gtk_list_box_render      (GtkCssGadget        *gadget,
                                          cairo_t             *cr,
                                          int                  x,
                                          int                  y,
                                          int                  width,
                                          int                  height,
                                          gpointer             data);



Alexander Larsson's avatar
Alexander Larsson committed
325 326
static GParamSpec *properties[LAST_PROPERTY] = { NULL, };
static guint signals[LAST_SIGNAL] = { 0 };
327
static GParamSpec *row_properties[LAST_ROW_PROPERTY] = { NULL, };
328
static guint row_signals[ROW__LAST_SIGNAL] = { 0 };
Alexander Larsson's avatar
Alexander Larsson committed
329

Alexander Larsson's avatar
Alexander Larsson committed
330 331 332 333 334 335 336 337 338
/**
 * gtk_list_box_new:
 *
 * Creates a new #GtkListBox container.
 *
 * Returns: a new #GtkListBox
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
339 340 341 342 343 344 345 346 347 348 349 350
GtkWidget *
gtk_list_box_new (void)
{
  return g_object_new (GTK_TYPE_LIST_BOX, NULL);
}

static void
gtk_list_box_get_property (GObject    *obj,
                           guint       property_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
351
  GtkListBoxPrivate *priv = BOX_PRIV (obj);
Alexander Larsson's avatar
Alexander Larsson committed
352 353 354 355

  switch (property_id)
    {
    case PROP_SELECTION_MODE:
356
      g_value_set_enum (value, priv->selection_mode);
Alexander Larsson's avatar
Alexander Larsson committed
357 358
      break;
    case PROP_ACTIVATE_ON_SINGLE_CLICK:
359
      g_value_set_boolean (value, priv->activate_single_click);
Alexander Larsson's avatar
Alexander Larsson committed
360 361 362 363 364 365 366 367 368 369 370 371 372
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
      break;
    }
}

static void
gtk_list_box_set_property (GObject      *obj,
                           guint         property_id,
                           const GValue *value,
                           GParamSpec   *pspec)
{
373
  GtkListBox *box = GTK_LIST_BOX (obj);
Alexander Larsson's avatar
Alexander Larsson committed
374 375 376 377

  switch (property_id)
    {
    case PROP_SELECTION_MODE:
378
      gtk_list_box_set_selection_mode (box, g_value_get_enum (value));
Alexander Larsson's avatar
Alexander Larsson committed
379 380
      break;
    case PROP_ACTIVATE_ON_SINGLE_CLICK:
381
      gtk_list_box_set_activate_on_single_click (box, g_value_get_boolean (value));
Alexander Larsson's avatar
Alexander Larsson committed
382 383 384 385 386 387 388 389 390 391
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
      break;
    }
}

static void
gtk_list_box_finalize (GObject *obj)
{
392
  GtkListBoxPrivate *priv = BOX_PRIV (obj);
Alexander Larsson's avatar
Alexander Larsson committed
393 394 395 396 397

  if (priv->sort_func_target_destroy_notify != NULL)
    priv->sort_func_target_destroy_notify (priv->sort_func_target);
  if (priv->filter_func_target_destroy_notify != NULL)
    priv->filter_func_target_destroy_notify (priv->filter_func_target);
398 399
  if (priv->update_header_func_target_destroy_notify != NULL)
    priv->update_header_func_target_destroy_notify (priv->update_header_func_target);
Alexander Larsson's avatar
Alexander Larsson committed
400 401 402

  g_clear_object (&priv->adjustment);
  g_clear_object (&priv->drag_highlighted_row);
403
  g_clear_object (&priv->multipress_gesture);
Alexander Larsson's avatar
Alexander Larsson committed
404 405

  g_sequence_free (priv->children);
406
  g_hash_table_unref (priv->header_hash);
Alexander Larsson's avatar
Alexander Larsson committed
407

408 409 410 411 412 413 414 415 416
  if (priv->bound_model)
    {
      if (priv->create_widget_func_data_destroy)
        priv->create_widget_func_data_destroy (priv->create_widget_func_data);

      g_signal_handlers_disconnect_by_func (priv->bound_model, gtk_list_box_bound_model_changed, obj);
      g_clear_object (&priv->bound_model);
    }

417 418
  g_clear_object (&priv->gadget);

Alexander Larsson's avatar
Alexander Larsson committed
419 420 421
  G_OBJECT_CLASS (gtk_list_box_parent_class)->finalize (obj);
}

422 423 424 425 426 427 428 429 430 431 432 433 434 435
static void
gtk_list_box_dispose (GObject *object)
{
  GtkListBoxPrivate *priv = BOX_PRIV (object);

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

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

Alexander Larsson's avatar
Alexander Larsson committed
436 437 438 439 440 441 442 443 444 445 446 447 448
static void
gtk_list_box_class_init (GtkListBoxClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
  GtkBindingSet *binding_set;

  gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_LIST_BOX_ACCESSIBLE);

  object_class->get_property = gtk_list_box_get_property;
  object_class->set_property = gtk_list_box_set_property;
  object_class->finalize = gtk_list_box_finalize;
449
  object_class->dispose = gtk_list_box_dispose;
450 451 452 453 454 455 456
  widget_class->enter_notify_event = gtk_list_box_enter_notify_event;
  widget_class->leave_notify_event = gtk_list_box_leave_notify_event;
  widget_class->motion_notify_event = gtk_list_box_motion_notify_event;
  widget_class->show = gtk_list_box_show;
  widget_class->focus = gtk_list_box_focus;
  widget_class->draw = gtk_list_box_draw;
  widget_class->realize = gtk_list_box_realize;
457
  widget_class->compute_expand = gtk_list_box_compute_expand;
458 459 460 461 462 463 464 465 466 467
  widget_class->get_request_mode = gtk_list_box_get_request_mode;
  widget_class->get_preferred_height = gtk_list_box_get_preferred_height;
  widget_class->get_preferred_height_for_width = gtk_list_box_get_preferred_height_for_width;
  widget_class->get_preferred_width = gtk_list_box_get_preferred_width;
  widget_class->get_preferred_width_for_height = gtk_list_box_get_preferred_width_for_height;
  widget_class->size_allocate = gtk_list_box_size_allocate;
  widget_class->drag_leave = gtk_list_box_drag_leave;
  widget_class->parent_set = gtk_list_box_parent_set;
  container_class->add = gtk_list_box_add;
  container_class->remove = gtk_list_box_remove;
468
  container_class->forall = gtk_list_box_forall;
469 470 471 472
  container_class->child_type = gtk_list_box_child_type;
  klass->activate_cursor_row = gtk_list_box_activate_cursor_row;
  klass->toggle_cursor_row = gtk_list_box_toggle_cursor_row;
  klass->move_cursor = gtk_list_box_move_cursor;
473 474 475
  klass->select_all = gtk_list_box_select_all;
  klass->unselect_all = gtk_list_box_unselect_all;
  klass->selected_rows_changed = gtk_list_box_selected_rows_changed;
Alexander Larsson's avatar
Alexander Larsson committed
476 477 478

  properties[PROP_SELECTION_MODE] =
    g_param_spec_enum ("selection-mode",
Matthias Clasen's avatar
Matthias Clasen committed
479 480
                       P_("Selection mode"),
                       P_("The selection mode"),
Alexander Larsson's avatar
Alexander Larsson committed
481 482
                       GTK_TYPE_SELECTION_MODE,
                       GTK_SELECTION_SINGLE,
483
                       G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
Alexander Larsson's avatar
Alexander Larsson committed
484 485 486

  properties[PROP_ACTIVATE_ON_SINGLE_CLICK] =
    g_param_spec_boolean ("activate-on-single-click",
Matthias Clasen's avatar
Matthias Clasen committed
487 488
                          P_("Activate on Single Click"),
                          P_("Activate row on a single click"),
Alexander Larsson's avatar
Alexander Larsson committed
489
                          TRUE,
490
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
Alexander Larsson's avatar
Alexander Larsson committed
491 492 493

  g_object_class_install_properties (object_class, LAST_PROPERTY, properties);

Alexander Larsson's avatar
Alexander Larsson committed
494 495
  /**
   * GtkListBox::row-selected:
496
   * @box: the #GtkListBox
497
   * @row: (nullable): the selected row
Alexander Larsson's avatar
Alexander Larsson committed
498 499 500 501
   *
   * The ::row-selected signal is emitted when a new row is selected, or
   * (with a %NULL @row) when the selection is cleared.
   *
502 503 504
   * When the @box is using #GTK_SELECTION_MULTIPLE, this signal will not
   * give you the full picture of selection changes, and you should use
   * the #GtkListBox::selected-rows-changed signal instead.
505
   *
Alexander Larsson's avatar
Alexander Larsson committed
506 507
   * Since: 3.10
   */
Alexander Larsson's avatar
Alexander Larsson committed
508
  signals[ROW_SELECTED] =
509
    g_signal_new (I_("row-selected"),
Alexander Larsson's avatar
Alexander Larsson committed
510 511 512 513 514 515
                  GTK_TYPE_LIST_BOX,
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkListBoxClass, row_selected),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
                  G_TYPE_NONE, 1,
Alexander Larsson's avatar
Alexander Larsson committed
516
                  GTK_TYPE_LIST_BOX_ROW);
Matthias Clasen's avatar
Matthias Clasen committed
517

518 519
  /**
   * GtkListBox::selected-rows-changed:
520
   * @box: the #GtkListBox on wich the signal is emitted
521 522 523 524 525 526
   *
   * The ::selected-rows-changed signal is emitted when the
   * set of selected rows changes.
   *
   * Since: 3.14
   */
527
  signals[SELECTED_ROWS_CHANGED] = g_signal_new (I_("selected-rows-changed"),
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
                                                 GTK_TYPE_LIST_BOX,
                                                 G_SIGNAL_RUN_FIRST,
                                                 G_STRUCT_OFFSET (GtkListBoxClass, selected_rows_changed),
                                                 NULL, NULL,
                                                 g_cclosure_marshal_VOID__VOID,
                                                 G_TYPE_NONE, 0);

  /**
   * GtkListBox::select-all:
   * @box: the #GtkListBox on which the signal is emitted
   *
   * The ::select-all signal is a [keybinding signal][GtkBindingSignal]
   * which gets emitted to select all children of the box, if the selection
   * mode permits it.
   *
   * The default bindings for this signal is Ctrl-a.
   *
   * Since: 3.14
   */
547
  signals[SELECT_ALL] = g_signal_new (I_("select-all"),
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
                                      GTK_TYPE_LIST_BOX,
                                      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                                      G_STRUCT_OFFSET (GtkListBoxClass, select_all),
                                      NULL, NULL,
                                      g_cclosure_marshal_VOID__VOID,
                                      G_TYPE_NONE, 0);

  /**
   * GtkListBox::unselect-all:
   * @box: the #GtkListBox on which the signal is emitted
   * 
   * The ::unselect-all signal is a [keybinding signal][GtkBindingSignal]
   * which gets emitted to unselect all children of the box, if the selection
   * mode permits it.
   *
   * The default bindings for this signal is Ctrl-Shift-a.
   *
   * Since: 3.14
   */
567 568 569 570 571 572 573
  signals[UNSELECT_ALL] = g_signal_new (I_("unselect-all"),
                                        GTK_TYPE_LIST_BOX,
                                        G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                                        G_STRUCT_OFFSET (GtkListBoxClass, unselect_all),
                                        NULL, NULL,
                                        g_cclosure_marshal_VOID__VOID,
                                        G_TYPE_NONE, 0);
574

Alexander Larsson's avatar
Alexander Larsson committed
575 576
  /**
   * GtkListBox::row-activated:
577
   * @box: the #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
578 579
   * @row: the activated row
   *
580
   * The ::row-activated signal is emitted when a row has been activated by the user.
Alexander Larsson's avatar
Alexander Larsson committed
581 582 583
   *
   * Since: 3.10
   */
Alexander Larsson's avatar
Alexander Larsson committed
584
  signals[ROW_ACTIVATED] =
585
    g_signal_new (I_("row-activated"),
Alexander Larsson's avatar
Alexander Larsson committed
586 587 588 589 590 591
                  GTK_TYPE_LIST_BOX,
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkListBoxClass, row_activated),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
                  G_TYPE_NONE, 1,
Alexander Larsson's avatar
Alexander Larsson committed
592
                  GTK_TYPE_LIST_BOX_ROW);
Alexander Larsson's avatar
Alexander Larsson committed
593
  signals[ACTIVATE_CURSOR_ROW] =
594
    g_signal_new (I_("activate-cursor-row"),
Alexander Larsson's avatar
Alexander Larsson committed
595 596 597 598 599 600 601
                  GTK_TYPE_LIST_BOX,
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkListBoxClass, activate_cursor_row),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
  signals[TOGGLE_CURSOR_ROW] =
602
    g_signal_new (I_("toggle-cursor-row"),
Alexander Larsson's avatar
Alexander Larsson committed
603 604 605 606 607 608 609
                  GTK_TYPE_LIST_BOX,
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkListBoxClass, toggle_cursor_row),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
  signals[MOVE_CURSOR] =
610
    g_signal_new (I_("move-cursor"),
Alexander Larsson's avatar
Alexander Larsson committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
                  GTK_TYPE_LIST_BOX,
                  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
                  G_STRUCT_OFFSET (GtkListBoxClass, move_cursor),
                  NULL, NULL,
                  _gtk_marshal_VOID__ENUM_INT,
                  G_TYPE_NONE, 2,
                  GTK_TYPE_MOVEMENT_STEP, G_TYPE_INT);

  widget_class->activate_signal = signals[ACTIVATE_CURSOR_ROW];

  binding_set = gtk_binding_set_by_class (klass);
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_Home, 0,
                                 GTK_MOVEMENT_BUFFER_ENDS, -1);
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
                                 GTK_MOVEMENT_BUFFER_ENDS, -1);
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_End, 0,
                                 GTK_MOVEMENT_BUFFER_ENDS, 1);
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_KP_End, 0,
                                 GTK_MOVEMENT_BUFFER_ENDS, 1);
630
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_Up, 0,
Alexander Larsson's avatar
Alexander Larsson committed
631
                                 GTK_MOVEMENT_DISPLAY_LINES, -1);
632
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_KP_Up, 0,
Alexander Larsson's avatar
Alexander Larsson committed
633
                                 GTK_MOVEMENT_DISPLAY_LINES, -1);
634
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_Down, 0,
Alexander Larsson's avatar
Alexander Larsson committed
635
                                 GTK_MOVEMENT_DISPLAY_LINES, 1);
636
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_KP_Down, 0,
Alexander Larsson's avatar
Alexander Larsson committed
637 638 639 640 641 642 643 644 645
                                 GTK_MOVEMENT_DISPLAY_LINES, 1);
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_Page_Up, 0,
                                 GTK_MOVEMENT_PAGES, -1);
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
                                 GTK_MOVEMENT_PAGES, -1);
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_Page_Down, 0,
                                 GTK_MOVEMENT_PAGES, 1);
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
                                 GTK_MOVEMENT_PAGES, 1);
646

Alexander Larsson's avatar
Alexander Larsson committed
647 648
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, GDK_CONTROL_MASK,
                                "toggle-cursor-row", 0, NULL);
649 650 651 652 653 654 655
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, GDK_CONTROL_MASK,
                                "toggle-cursor-row", 0, NULL);

  gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
                                "select-all", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
                                "unselect-all", 0);
656 657

  gtk_widget_class_set_css_name (widget_class, "list");
Alexander Larsson's avatar
Alexander Larsson committed
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
static void
gtk_list_box_init (GtkListBox *box)
{
  GtkListBoxPrivate *priv = BOX_PRIV (box);
  GtkWidget *widget = GTK_WIDGET (box);
  GtkCssNode *widget_node;

  gtk_widget_set_has_window (widget, TRUE);
  gtk_widget_set_redraw_on_allocate (widget, TRUE);
  priv->selection_mode = GTK_SELECTION_SINGLE;
  priv->activate_single_click = TRUE;

  priv->children = g_sequence_new (NULL);
  priv->header_hash = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, NULL);

  priv->multipress_gesture = gtk_gesture_multi_press_new (widget);
  gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (priv->multipress_gesture),
                                              GTK_PHASE_BUBBLE);
  gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (priv->multipress_gesture),
                                     FALSE);
  gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (priv->multipress_gesture),
                                 GDK_BUTTON_PRIMARY);
  g_signal_connect (priv->multipress_gesture, "pressed",
                    G_CALLBACK (gtk_list_box_multipress_gesture_pressed), box);
  g_signal_connect (priv->multipress_gesture, "released",
                    G_CALLBACK (gtk_list_box_multipress_gesture_released), box);

  widget_node = gtk_widget_get_css_node (GTK_WIDGET (box));
  priv->gadget = gtk_css_custom_gadget_new_for_node (widget_node,
                                                     GTK_WIDGET (box),
                                                     gtk_list_box_measure,
                                                     gtk_list_box_allocate,
                                                     gtk_list_box_render,
                                                     NULL,
                                                     NULL);

}

Alexander Larsson's avatar
Alexander Larsson committed
698 699
/**
 * gtk_list_box_get_selected_row:
700
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
701 702 703
 *
 * Gets the selected row.
 *
704 705 706 707 708
 * Note that the box may allow multiple selection, in which
 * case you should use gtk_list_box_selected_foreach() to
 * find all selected rows.
 *
 * Returns: (transfer none): the selected row
Alexander Larsson's avatar
Alexander Larsson committed
709 710
 *
 * Since: 3.10
Matthias Clasen's avatar
Matthias Clasen committed
711
 */
Alexander Larsson's avatar
Alexander Larsson committed
712
GtkListBoxRow *
713
gtk_list_box_get_selected_row (GtkListBox *box)
Alexander Larsson's avatar
Alexander Larsson committed
714
{
715
  g_return_val_if_fail (GTK_IS_LIST_BOX (box), NULL);
Alexander Larsson's avatar
Alexander Larsson committed
716

717
  return BOX_PRIV (box)->selected_row;
Alexander Larsson's avatar
Alexander Larsson committed
718 719 720 721
}

/**
 * gtk_list_box_get_row_at_index:
722
 * @box: a #GtkListBox
Matthias Clasen's avatar
Matthias Clasen committed
723
 * @index_: the index of the row
Alexander Larsson's avatar
Alexander Larsson committed
724
 *
725
 * Gets the n-th child in the list (not counting headers).
726 727
 * If @_index is negative or larger than the number of items in the
 * list, %NULL is returned.
Alexander Larsson's avatar
Alexander Larsson committed
728
 *
729
 * Returns: (transfer none) (nullable): the child #GtkWidget or %NULL
Alexander Larsson's avatar
Alexander Larsson committed
730 731
 *
 * Since: 3.10
Matthias Clasen's avatar
Matthias Clasen committed
732
 */
Alexander Larsson's avatar
Alexander Larsson committed
733
GtkListBoxRow *
734
gtk_list_box_get_row_at_index (GtkListBox *box,
Matthias Clasen's avatar
Matthias Clasen committed
735
                               gint        index_)
Alexander Larsson's avatar
Alexander Larsson committed
736 737 738
{
  GSequenceIter *iter;

739
  g_return_val_if_fail (GTK_IS_LIST_BOX (box), NULL);
740

741
  iter = g_sequence_get_iter_at_pos (BOX_PRIV (box)->children, index_);
742
  if (!g_sequence_iter_is_end (iter))
Alexander Larsson's avatar
Alexander Larsson committed
743 744 745 746 747
    return g_sequence_get (iter);

  return NULL;
}

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
static int
row_y_cmp_func (gconstpointer a,
                gconstpointer b,
                gpointer      user_data)
{
  int y = GPOINTER_TO_INT (b);
  GtkListBoxRowPrivate *row_priv = ROW_PRIV (a);


  if (y < row_priv->y)
    return 1;
  else if (y >= row_priv->y + row_priv->height)
    return -1;

  return 0;
}

Alexander Larsson's avatar
Alexander Larsson committed
765 766
/**
 * gtk_list_box_get_row_at_y:
767
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
768 769
 * @y: position
 *
Alexander Larsson's avatar
Alexander Larsson committed
770
 * Gets the row at the @y position.
Alexander Larsson's avatar
Alexander Larsson committed
771
 *
772 773
 * Returns: (transfer none) (nullable): the row or %NULL
 *   in case no row exists for the given y coordinate.
Alexander Larsson's avatar
Alexander Larsson committed
774 775
 *
 * Since: 3.10
Matthias Clasen's avatar
Matthias Clasen committed
776
 */
Alexander Larsson's avatar
Alexander Larsson committed
777
GtkListBoxRow *
778
gtk_list_box_get_row_at_y (GtkListBox *box,
Matthias Clasen's avatar
Matthias Clasen committed
779
                           gint        y)
Alexander Larsson's avatar
Alexander Larsson committed
780 781 782
{
  GSequenceIter *iter;

783
  g_return_val_if_fail (GTK_IS_LIST_BOX (box), NULL);
Alexander Larsson's avatar
Alexander Larsson committed
784

785 786 787 788
  iter = g_sequence_lookup (BOX_PRIV (box)->children,
                            GINT_TO_POINTER (y),
                            row_y_cmp_func,
                            NULL);
Alexander Larsson's avatar
Alexander Larsson committed
789

790 791
  if (iter)
    return GTK_LIST_BOX_ROW (g_sequence_get (iter));
Alexander Larsson's avatar
Alexander Larsson committed
792

793
  return NULL;
Alexander Larsson's avatar
Alexander Larsson committed
794 795 796 797
}

/**
 * gtk_list_box_select_row:
798
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
799
 * @row: (allow-none): The row to select or %NULL
Alexander Larsson's avatar
Alexander Larsson committed
800 801 802 803
 *
 * Make @row the currently selected row.
 *
 * Since: 3.10
Alexander Larsson's avatar
Alexander Larsson committed
804 805
 */
void
806
gtk_list_box_select_row (GtkListBox    *box,
Matthias Clasen's avatar
Matthias Clasen committed
807
                         GtkListBoxRow *row)
Alexander Larsson's avatar
Alexander Larsson committed
808
{
809 810
  gboolean dirty = FALSE;

811
  g_return_if_fail (GTK_IS_LIST_BOX (box));
812
  g_return_if_fail (row == NULL || GTK_IS_LIST_BOX_ROW (row));
Alexander Larsson's avatar
Alexander Larsson committed
813

814
  if (row)
815
    gtk_list_box_select_row_internal (box, row);
816
  else
817 818 819 820 821 822 823
    dirty = gtk_list_box_unselect_all_internal (box);

  if (dirty)
    {
      g_signal_emit (box, signals[ROW_SELECTED], 0, NULL);
      g_signal_emit (box, signals[SELECTED_ROWS_CHANGED], 0);
    }
Alexander Larsson's avatar
Alexander Larsson committed
824 825
}

826 827
/**   
 * gtk_list_box_unselect_row:
828
 * @box: a #GtkListBox
829 830
 * @row: the row to unselected
 *
831
 * Unselects a single row of @box, if the selection mode allows it.
832 833 834 835
 *
 * Since: 3.14
 */                       
void
836
gtk_list_box_unselect_row (GtkListBox    *box,
837 838
                           GtkListBoxRow *row)
{
839
  g_return_if_fail (GTK_IS_LIST_BOX (box));
840 841
  g_return_if_fail (GTK_IS_LIST_BOX_ROW (row));
  
842
  gtk_list_box_unselect_row_internal (box, row);
843 844 845 846
} 

/**
 * gtk_list_box_select_all:
847
 * @box: a #GtkListBox
848
 *
849
 * Select all children of @box, if the selection mode allows it.
850 851 852 853
 *
 * Since: 3.14
 */
void
854
gtk_list_box_select_all (GtkListBox *box)
855
{
856
  g_return_if_fail (GTK_IS_LIST_BOX (box));
857

858
  if (BOX_PRIV (box)->selection_mode != GTK_SELECTION_MULTIPLE)
859 860
    return;

861
  if (g_sequence_get_length (BOX_PRIV (box)->children) > 0)
862
    {
863 864
      gtk_list_box_select_all_between (box, NULL, NULL, FALSE);
      g_signal_emit (box, signals[SELECTED_ROWS_CHANGED], 0);
865
    }
866 867 868 869
}

/**
 * gtk_list_box_unselect_all:
870
 * @box: a #GtkListBox
871
 *
872
 * Unselect all children of @box, if the selection mode allows it.
873 874 875 876
 *
 * Since: 3.14
 */
void
877
gtk_list_box_unselect_all (GtkListBox *box)
878
{
879 880
  gboolean dirty = FALSE;

881
  g_return_if_fail (GTK_IS_LIST_BOX (box));
882

883
  if (BOX_PRIV (box)->selection_mode == GTK_SELECTION_BROWSE)
884 885
    return;

886
  dirty = gtk_list_box_unselect_all_internal (box);
887 888

  if (dirty)
889 890 891 892
    {
      g_signal_emit (box, signals[ROW_SELECTED], 0, NULL);
      g_signal_emit (box, signals[SELECTED_ROWS_CHANGED], 0);
    }
893 894 895
}

static void
896
gtk_list_box_selected_rows_changed (GtkListBox *box)
897
{
898
  _gtk_list_box_accessible_selection_changed (box);
899 900 901 902
}

/**
 * GtkListBoxForeachFunc:
903
 * @box: a #GtkListBox
904 905 906 907
 * @row: a #GtkListBoxRow
 * @user_data: (closure): user data
 *
 * A function used by gtk_list_box_selected_foreach().
908
 * It will be called on every selected child of the @box.
909 910 911 912 913 914
 *
 * Since: 3.14
 */

/**
 * gtk_list_box_selected_foreach:
915
 * @box: a #GtkListBox
916 917 918 919 920
 * @func: (scope call): the function to call for each selected child
 * @data: user data to pass to the function
 *
 * Calls a function for each selected child.
 *
921
 * Note that the selection cannot be modified from within this function.
922 923 924 925
 *
 * Since: 3.14
 */
void
926
gtk_list_box_selected_foreach (GtkListBox            *box,
927 928 929 930 931 932
                               GtkListBoxForeachFunc  func,
                               gpointer               data)
{
  GtkListBoxRow *row;
  GSequenceIter *iter;

933
  g_return_if_fail (GTK_IS_LIST_BOX (box));
934

935
  for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
936 937 938 939 940
       !g_sequence_iter_is_end (iter);
       iter = g_sequence_iter_next (iter))
    {
      row = g_sequence_get (iter);
      if (gtk_list_box_row_is_selected (row))
941
        (*func) (box, row, data);
942 943 944 945
    }
}

/**
946
 * gtk_list_box_get_selected_rows:
947
 * @box: a #GtkListBox
948 949 950 951 952 953 954 955 956 957
 *
 * Creates a list of all selected children.
 *
 * Returns: (element-type GtkListBoxRow) (transfer container):
 *     A #GList containing the #GtkWidget for each selected child.
 *     Free with g_list_free() when done.
 *
 * Since: 3.14
 */
GList *
958
gtk_list_box_get_selected_rows (GtkListBox *box)
959 960 961 962 963
{
  GtkListBoxRow *row;
  GSequenceIter *iter;
  GList *selected = NULL;

964
  g_return_val_if_fail (GTK_IS_LIST_BOX (box), NULL);
965

966
  for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
967 968 969 970 971 972 973 974 975 976
       !g_sequence_iter_is_end (iter);
       iter = g_sequence_iter_next (iter))
    {
      row = g_sequence_get (iter);
      if (gtk_list_box_row_is_selected (row))
        selected = g_list_prepend (selected, row);
    }

  return g_list_reverse (selected);
}
977 978 979

/**
 * gtk_list_box_set_placeholder:
980
 * @box: a #GtkListBox
981 982 983
 * @placeholder: (allow-none): a #GtkWidget or %NULL
 *
 * Sets the placeholder widget that is shown in the list when
984
 * it doesn't display any visible children.
985 986 987 988
 *
 * Since: 3.10
 */
void
989
gtk_list_box_set_placeholder (GtkListBox *box,
Matthias Clasen's avatar
Matthias Clasen committed
990
                              GtkWidget  *placeholder)
991
{
992
  GtkListBoxPrivate *priv = BOX_PRIV (box);
993

994
  g_return_if_fail (GTK_IS_LIST_BOX (box));
995 996 997 998

  if (priv->placeholder)
    {
      gtk_widget_unparent (priv->placeholder);
999
      gtk_widget_queue_resize (GTK_WIDGET (box));
1000 1001 1002 1003 1004 1005
    }

  priv->placeholder = placeholder;

  if (placeholder)
    {
1006
      gtk_widget_set_parent (GTK_WIDGET (placeholder), GTK_WIDGET (box));
1007 1008 1009 1010 1011 1012
      gtk_widget_set_child_visible (GTK_WIDGET (placeholder),
                                    priv->n_visible_rows == 0);
    }
}


Alexander Larsson's avatar
Alexander Larsson committed
1013 1014
/**
 * gtk_list_box_set_adjustment:
1015
 * @box: a #GtkListBox
Matthias Clasen's avatar
Matthias Clasen committed
1016
 * @adjustment: (allow-none): the adjustment, or %NULL
Alexander Larsson's avatar
Alexander Larsson committed
1017 1018 1019 1020 1021
 *
 * Sets the adjustment (if any) that the widget uses to
 * for vertical scrolling. For instance, this is used
 * to get the page size for PageUp/Down key handling.
 *
1022
 * In the normal case when the @box is packed inside
Alexander Larsson's avatar
Alexander Larsson committed
1023 1024 1025 1026 1027 1028
 * a #GtkScrolledWindow the adjustment from that will
 * be picked up automatically, so there is no need
 * to manually do that.
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
1029
void
1030
gtk_list_box_set_adjustment (GtkListBox    *box,
Alexander Larsson's avatar
Alexander Larsson committed
1031 1032
                             GtkAdjustment *adjustment)
{
1033
  GtkListBoxPrivate *priv = BOX_PRIV (box);
1034

1035
  g_return_if_fail (GTK_IS_LIST_BOX (box));
1036
  g_return_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment));
Alexander Larsson's avatar
Alexander Larsson committed
1037

1038 1039
  if (adjustment)
    g_object_ref_sink (adjustment);
Alexander Larsson's avatar
Alexander Larsson committed
1040 1041 1042 1043 1044
  if (priv->adjustment)
    g_object_unref (priv->adjustment);
  priv->adjustment = adjustment;
}

Alexander Larsson's avatar
Alexander Larsson committed
1045 1046
/**
 * gtk_list_box_get_adjustment:
1047
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
1048 1049 1050 1051
 *
 * Gets the adjustment (if any) that the widget uses to
 * for vertical scrolling.
 *
1052
 * Returns: (transfer none): the adjustment
Alexander Larsson's avatar
Alexander Larsson committed
1053 1054 1055
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
1056
GtkAdjustment *
1057
gtk_list_box_get_adjustment (GtkListBox *box)
Alexander Larsson's avatar
Alexander Larsson committed
1058
{
1059
  g_return_val_if_fail (GTK_IS_LIST_BOX (box), NULL);
Alexander Larsson's avatar
Alexander Larsson committed
1060

1061
  return BOX_PRIV (box)->adjustment;
Alexander Larsson's avatar
Alexander Larsson committed
1062 1063
}

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
static void
adjustment_changed (GObject    *object,
                    GParamSpec *pspec,
                    gpointer    data)
{
  GtkAdjustment *adjustment;

  adjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (object));
  gtk_list_box_set_adjustment (GTK_LIST_BOX (data), adjustment);
}

1075
static void
1076 1077
gtk_list_box_parent_set (GtkWidget *widget,
                         GtkWidget *prev_parent)
Alexander Larsson's avatar
Alexander Larsson committed
1078
{
1079
  GtkWidget *parent;
Alexander Larsson's avatar
Alexander Larsson committed
1080

1081
  parent = gtk_widget_get_parent (widget);
Alexander Larsson's avatar
Alexander Larsson committed
1082

1083 1084 1085 1086
  if (prev_parent && GTK_IS_SCROLLABLE (prev_parent))
    g_signal_handlers_disconnect_by_func (prev_parent,
                                          G_CALLBACK (adjustment_changed), widget);

1087 1088
  if (parent && GTK_IS_SCROLLABLE (parent))
    {
1089 1090 1091
      adjustment_changed (G_OBJECT (parent), NULL, widget);
      g_signal_connect (parent, "notify::vadjustment",
                        G_CALLBACK (adjustment_changed), widget);
1092
    }
1093 1094
  else
    gtk_list_box_set_adjustment (GTK_LIST_BOX (widget), NULL);
1095
}
Alexander Larsson's avatar
Alexander Larsson committed
1096

Alexander Larsson's avatar
Alexander Larsson committed
1097 1098
/**
 * gtk_list_box_set_selection_mode:
1099
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
1100 1101 1102 1103 1104 1105 1106
 * @mode: The #GtkSelectionMode
 *
 * Sets how selection works in the listbox.
 * See #GtkSelectionMode for details.
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
1107
void
1108
gtk_list_box_set_selection_mode (GtkListBox       *box,
Matthias Clasen's avatar
Matthias Clasen committed
1109
                                 GtkSelectionMode  mode)
Alexander Larsson's avatar
Alexander Larsson committed
1110
{
1111
  GtkListBoxPrivate *priv = BOX_PRIV (box);
1112
  gboolean dirty = FALSE;
1113

1114
  g_return_if_fail (GTK_IS_LIST_BOX (box));
Alexander Larsson's avatar
Alexander Larsson committed
1115

1116 1117
  if (priv->selection_mode == mode)
    return;
Alexander Larsson's avatar
Alexander Larsson committed
1118

1119 1120
  if (mode == GTK_SELECTION_NONE ||
      priv->selection_mode == GTK_SELECTION_MULTIPLE)
1121
    dirty = gtk_list_box_unselect_all_internal (box);
Alexander Larsson's avatar
Alexander Larsson committed
1122 1123 1124

  priv->selection_mode = mode;

1125 1126
  gtk_list_box_update_row_styles (box);

1127
  g_object_notify_by_pspec (G_OBJECT (box), properties[PROP_SELECTION_MODE]);
1128 1129

  if (dirty)
1130 1131 1132 1133
    {
      g_signal_emit (box, signals[ROW_SELECTED], 0, NULL);
      g_signal_emit (box, signals[SELECTED_ROWS_CHANGED], 0);
    }
Alexander Larsson's avatar
Alexander Larsson committed
1134 1135
}

Alexander Larsson's avatar
Alexander Larsson committed
1136 1137
/**
 * gtk_list_box_get_selection_mode:
1138
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
1139 1140 1141 1142 1143 1144 1145
 *
 * Gets the selection mode of the listbox.
 *
 * Returns: a #GtkSelectionMode
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
1146
GtkSelectionMode
1147
gtk_list_box_get_selection_mode (GtkListBox *box)
Alexander Larsson's avatar
Alexander Larsson committed
1148
{
1149
  g_return_val_if_fail (GTK_IS_LIST_BOX (box), GTK_SELECTION_NONE);
1150

1151
  return BOX_PRIV (box)->selection_mode;
Alexander Larsson's avatar
Alexander Larsson committed
1152 1153 1154 1155
}

/**
 * gtk_list_box_set_filter_func:
1156
 * @box: a #GtkListBox
Matthias Clasen's avatar
Matthias Clasen committed
1157
 * @filter_func: (closure user_data) (allow-none): callback that lets you filter which rows to show
Alexander Larsson's avatar
Alexander Larsson committed
1158 1159 1160
 * @user_data: user data passed to @filter_func
 * @destroy: destroy notifier for @user_data
 *
1161
 * By setting a filter function on the @box one can decide dynamically which
Alexander Larsson's avatar
Alexander Larsson committed
1162 1163 1164 1165 1166
 * of the rows to show. For instance, to implement a search function on a list that
 * filters the original list to only show the matching rows.
 *
 * The @filter_func will be called for each row after the call, and it will
 * continue to be called each time a row changes (via gtk_list_box_row_changed()) or
1167
 * when gtk_list_box_invalidate_filter() is called.
Alexander Larsson's avatar
Alexander Larsson committed
1168
 *
1169 1170 1171
 * Note that using a filter function is incompatible with using a model
 * (see gtk_list_box_bind_model()).
 *
Alexander Larsson's avatar
Alexander Larsson committed
1172
 * Since: 3.10
Alexander Larsson's avatar
Alexander Larsson committed
1173 1174
 */
void
1175
gtk_list_box_set_filter_func (GtkListBox           *box,
Matthias Clasen's avatar
Matthias Clasen committed
1176 1177 1178
                              GtkListBoxFilterFunc  filter_func,
                              gpointer              user_data,
                              GDestroyNotify        destroy)
Alexander Larsson's avatar
Alexander Larsson committed
1179
{
1180
  GtkListBoxPrivate *priv = BOX_PRIV (box);
Alexander Larsson's avatar
Alexander Larsson committed
1181

1182
  g_return_if_fail (GTK_IS_LIST_BOX (box));
Alexander Larsson's avatar
Alexander Larsson committed
1183 1184 1185 1186 1187 1188 1189 1190

  if (priv->filter_func_target_destroy_notify != NULL)
    priv->filter_func_target_destroy_notify (priv->filter_func_target);

  priv->filter_func = filter_func;
  priv->filter_func_target = user_data;
  priv->filter_func_target_destroy_notify = destroy;

1191 1192
  gtk_list_box_check_model_compat (box);

1193
  gtk_list_box_invalidate_filter (box);
Alexander Larsson's avatar
Alexander Larsson committed
1194 1195 1196
}

/**
1197
 * gtk_list_box_set_header_func:
1198
 * @box: a #GtkListBox
Matthias Clasen's avatar
Matthias Clasen committed
1199
 * @update_header: (closure user_data) (allow-none): callback that lets you add row headers
1200
 * @user_data: user data passed to @update_header
Alexander Larsson's avatar
Alexander Larsson committed
1201 1202
 * @destroy: destroy notifier for @user_data
 *
1203
 * By setting a header function on the @box one can dynamically add headers
Alexander Larsson's avatar
Alexander Larsson committed
1204 1205 1206 1207
 * in front of rows, depending on the contents of the row and its position in the list.
 * For instance, one could use it to add headers in front of the first item of a
 * new kind, in a list sorted by the kind.
 *
1208
 * The @update_header can look at the current header widget using gtk_list_box_row_get_header()
Alexander Larsson's avatar
Alexander Larsson committed
1209
 * and either update the state of the widget as needed, or set a new one using
1210
 * gtk_list_box_row_set_header(). If no header is needed, set the header to %NULL.
Alexander Larsson's avatar
Alexander Larsson committed
1211
 *
1212
 * Note that you may get many calls @update_header to this for a particular row when e.g.
1213
 * changing things that don’t affect the header. In this case it is important for performance
Matthias Clasen's avatar
Matthias Clasen committed
1214
 * to not blindly replace an existing header with an identical one.
Alexander Larsson's avatar
Alexander Larsson committed
1215
 *
1216
 * The @update_header function will be called for each row after the call, and it will
Alexander Larsson's avatar
Alexander Larsson committed
1217 1218 1219
 * continue to be called each time a row changes (via gtk_list_box_row_changed()) and when
 * the row before changes (either by gtk_list_box_row_changed() on the previous row, or when
 * the previous row becomes a different row). It is also called for all rows when
1220
 * gtk_list_box_invalidate_headers() is called.
Alexander Larsson's avatar
Alexander Larsson committed
1221 1222
 *
 * Since: 3.10
Alexander Larsson's avatar
Alexander Larsson committed
1223 1224
 */
void
1225
gtk_list_box_set_header_func (GtkListBox                 *box,
Matthias Clasen's avatar
Matthias Clasen committed
1226 1227 1228
                              GtkListBoxUpdateHeaderFunc  update_header,
                              gpointer                    user_data,
                              GDestroyNotify              destroy)
Alexander Larsson's avatar
Alexander Larsson committed
1229
{
1230
  GtkListBoxPrivate *priv = BOX_PRIV (box);
Alexander Larsson's avatar
Alexander Larsson committed
1231

1232
  g_return_if_fail (GTK_IS_LIST_BOX (box));
Alexander Larsson's avatar
Alexander Larsson committed
1233

1234 1235
  if (priv->update_header_func_target_destroy_notify != NULL)
    priv->update_header_func_target_destroy_notify (priv->update_header_func_target);
Alexander Larsson's avatar
Alexander Larsson committed
1236

1237 1238 1239
  priv->update_header_func = update_header;
  priv->update_header_func_target = user_data;
  priv->update_header_func_target_destroy_notify = destroy;
1240
  gtk_list_box_invalidate_headers (box);
Alexander Larsson's avatar
Alexander Larsson committed
1241 1242
}

Alexander Larsson's avatar
Alexander Larsson committed
1243
/**
1244
 * gtk_list_box_invalidate_filter:
1245
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
1246 1247
 *
 * Update the filtering for all rows. Call this when result
1248
 * of the filter function on the @box is changed due
Alexander Larsson's avatar
Alexander Larsson committed
1249 1250 1251 1252 1253 1254
 * to an external factor. For instance, this would be used
 * if the filter function just looked for a specific search
 * string and the entry with the search string has changed.
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
1255
void
1256
gtk_list_box_invalidate_filter (GtkListBox *box)
Alexander Larsson's avatar
Alexander Larsson committed
1257
{
1258
  g_return_if_fail (GTK_IS_LIST_BOX (box));
Alexander Larsson's avatar
Alexander Larsson committed
1259

1260 1261 1262
  gtk_list_box_apply_filter_all (box);
  gtk_list_box_invalidate_headers (box);
  gtk_widget_queue_resize (GTK_WIDGET (box));
Alexander Larsson's avatar
Alexander Larsson committed
1263 1264 1265 1266 1267
}

static gint
do_sort (GtkListBoxRow *a,
         GtkListBoxRow *b,
1268
         GtkListBox    *box)
Alexander Larsson's avatar
Alexander Larsson committed
1269
{
1270
  GtkListBoxPrivate *priv = BOX_PRIV (box);
Alexander Larsson's avatar
Alexander Larsson committed
1271 1272 1273 1274

  return priv->sort_func (a, b, priv->sort_func_target);
}

1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
static void
gtk_list_box_css_node_foreach (gpointer data,
                               gpointer user_data)
{
  GtkWidget **previous = user_data;
  GtkWidget *row = data;
  GtkCssNode *row_node;
  GtkCssNode *prev_node;

  if (*previous)
    {
      prev_node = gtk_widget_get_css_node (*previous);
      row_node = gtk_widget_get_css_node (row);
1288 1289 1290
      gtk_css_node_insert_after (gtk_css_node_get_parent (row_node),
                                 row_node,
                                 prev_node);
1291 1292 1293 1294 1295
    }

  *previous = row;
}

Alexander Larsson's avatar
Alexander Larsson committed
1296
/**
1297
 * gtk_list_box_invalidate_sort:
1298
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
1299 1300
 *
 * Update the sorting for all rows. Call this when result
1301
 * of the sort function on the @box is changed due
Alexander Larsson's avatar
Alexander Larsson committed
1302 1303 1304 1305
 * to an external factor.
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
1306
void
1307
gtk_list_box_invalidate_sort (GtkListBox *box)
Alexander Larsson's avatar
Alexander Larsson committed
1308
{
1309
  GtkListBoxPrivate *priv = BOX_PRIV (box);
1310
  GtkWidget *previous = NULL;
1311

1312
  g_return_if_fail (GTK_IS_LIST_BOX (box));
Alexander Larsson's avatar
Alexander Larsson committed
1313

1314 1315 1316
  if (priv->sort_func == NULL)
    return;

1317
  g_sequence_sort (priv->children, (GCompareDataFunc)do_sort, box);
1318
  g_sequence_foreach (priv->children, gtk_list_box_css_node_foreach, &previous);
Alexander Larsson's avatar
Alexander Larsson committed
1319

1320 1321
  gtk_list_box_invalidate_headers (box);
  gtk_widget_queue_resize (GTK_WIDGET (box));
Alexander Larsson's avatar
Alexander Larsson committed
1322 1323
}

1324
static void
1325
gtk_list_box_do_reseparate (GtkListBox *box)
1326 1327 1328
{
  GSequenceIter *iter;

1329
  for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
1330 1331
       !g_sequence_iter_is_end (iter);
       iter = g_sequence_iter_next (iter))
1332
    gtk_list_box_update_header (box, iter);
1333

1334
  gtk_widget_queue_resize (GTK_WIDGET (box));
1335 1336 1337
}


Alexander Larsson's avatar
Alexander Larsson committed
1338
/**
1339
 * gtk_list_box_invalidate_headers:
1340
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
1341 1342
 *
 * Update the separators for all rows. Call this when result