gtklistbox.c 123 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 "gtkadjustmentprivate.h"
21
#include "gtkcssnodeprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
22 23 24 25 26
#include "gtklistbox.h"
#include "gtkwidget.h"
#include "gtkmarshalers.h"
#include "gtkprivate.h"
#include "gtkintl.h"
Timm Bäder's avatar
Timm Bäder committed
27
#include "gtkwidgetprivate.h"
28
#include "gtkcontainerprivate.h"
29
#include "gtkcsscustomgadgetprivate.h"
Matthias Clasen's avatar
Matthias Clasen committed
30

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

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

/**
 * SECTION:gtklistbox
 * @Short_description: A list container
 * @Title: GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
42
 * @See_also: #GtkScrolledWindow
Alexander Larsson's avatar
Alexander Larsson committed
43
 *
Alexander Larsson's avatar
Alexander Larsson committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
 * 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.
 *
59 60 61 62 63
 * #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
64
 * The GtkListBox widget was added in GTK+ 3.10.
65 66 67 68 69 70
 *
 * # CSS nodes
 *
 * GtkListBox uses a single CSS node with name list. GtkListBoxRow uses
 * a single CSS node with name row. The row nodes get the .activatable
 * style class added when appropriate.
Matthias Clasen's avatar
Matthias Clasen committed
71
 */
Alexander Larsson's avatar
Alexander Larsson committed
72

73
typedef struct
Alexander Larsson's avatar
Alexander Larsson committed
74 75
{
  GSequence *children;
76
  GHashTable *header_hash;
Alexander Larsson's avatar
Alexander Larsson committed
77

78 79
  GtkWidget *placeholder;

80 81
  GtkCssGadget *gadget;

Alexander Larsson's avatar
Alexander Larsson committed
82 83 84 85 86 87 88 89
  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;

90 91 92
  GtkListBoxUpdateHeaderFunc update_header_func;
  gpointer update_header_func_target;
  GDestroyNotify update_header_func_target_destroy_notify;
Alexander Larsson's avatar
Alexander Larsson committed
93 94 95 96 97 98 99 100 101 102 103 104 105

  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;

106 107
  GtkGesture *multipress_gesture;

Alexander Larsson's avatar
Alexander Larsson committed
108 109
  /* DnD */
  GtkListBoxRow *drag_highlighted_row;
110 111

  int n_visible_rows;
112
  gboolean in_widget;
113 114 115 116 117

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

120
typedef struct
Alexander Larsson's avatar
Alexander Larsson committed
121 122
{
  GSequenceIter *iter;
123
  GtkWidget *header;
124
  GtkCssGadget *gadget;
Alexander Larsson's avatar
Alexander Larsson committed
125 126
  gint y;
  gint height;
127 128 129
  guint visible     :1;
  guint selected    :1;
  guint activatable :1;
130
  guint selectable  :1;
131
} GtkListBoxRowPrivate;
Alexander Larsson's avatar
Alexander Larsson committed
132 133 134 135 136 137 138

enum {
  ROW_SELECTED,
  ROW_ACTIVATED,
  ACTIVATE_CURSOR_ROW,
  TOGGLE_CURSOR_ROW,
  MOVE_CURSOR,
139 140 141
  SELECTED_ROWS_CHANGED,
  SELECT_ALL,
  UNSELECT_ALL,
Alexander Larsson's avatar
Alexander Larsson committed
142 143 144
  LAST_SIGNAL
};

145 146 147 148 149
enum {
  ROW__ACTIVATE,
  ROW__LAST_SIGNAL
};

150
enum {
Alexander Larsson's avatar
Alexander Larsson committed
151 152 153 154 155 156
  PROP_0,
  PROP_SELECTION_MODE,
  PROP_ACTIVATE_ON_SINGLE_CLICK,
  LAST_PROPERTY
};

157 158 159
enum {
  ROW_PROP_0,
  ROW_PROP_ACTIVATABLE,
160
  ROW_PROP_SELECTABLE,
161 162 163
  LAST_ROW_PROPERTY
};

164 165 166
#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)))

167 168 169 170 171 172
static void     gtk_list_box_buildable_interface_init     (GtkBuildableIface *iface);

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))
173
G_DEFINE_TYPE_WITH_PRIVATE (GtkListBoxRow, gtk_list_box_row, GTK_TYPE_BIN)
Alexander Larsson's avatar
Alexander Larsson committed
174

175 176
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
177
                                                                       GSequenceIter       *iter);
178 179 180
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
181 182 183 184 185 186
                                                                       GtkListBoxRow       *row);
static void                 gtk_list_box_add_move_binding             (GtkBindingSet       *binding_set,
                                                                       guint                keyval,
                                                                       GdkModifierType      modmask,
                                                                       GtkMovementStep      step,
                                                                       gint                 count);
187
static void                 gtk_list_box_update_cursor                (GtkListBox          *box,
188 189
                                                                       GtkListBoxRow       *row,
                                                                       gboolean             grab_focus);
190
static void                 gtk_list_box_update_prelight              (GtkListBox          *box,
Alexander Larsson's avatar
Alexander Larsson committed
191
                                                                       GtkListBoxRow       *row);
192
static void                 gtk_list_box_update_active                (GtkListBox          *box,
Alexander Larsson's avatar
Alexander Larsson committed
193
                                                                       GtkListBoxRow       *row);
194
static gboolean             gtk_list_box_enter_notify_event           (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
195
                                                                       GdkEventCrossing    *event);
196
static gboolean             gtk_list_box_leave_notify_event           (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
197
                                                                       GdkEventCrossing    *event);
198
static gboolean             gtk_list_box_motion_notify_event          (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
199
                                                                       GdkEventMotion      *event);
200 201
static void                 gtk_list_box_show                         (GtkWidget           *widget);
static gboolean             gtk_list_box_focus                        (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
202
                                                                       GtkDirectionType     direction);
203 204 205 206
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);
207
static gboolean             gtk_list_box_draw                         (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
208
                                                                       cairo_t             *cr);
209 210
static void                 gtk_list_box_realize                      (GtkWidget           *widget);
static void                 gtk_list_box_add                          (GtkContainer        *container,
Alexander Larsson's avatar
Alexander Larsson committed
211
                                                                       GtkWidget           *widget);
212
static void                 gtk_list_box_remove                       (GtkContainer        *container,
Alexander Larsson's avatar
Alexander Larsson committed
213
                                                                       GtkWidget           *widget);
214
static void                 gtk_list_box_forall                       (GtkContainer        *container,
Alexander Larsson's avatar
Alexander Larsson committed
215 216
                                                                       gboolean             include_internals,
                                                                       GtkCallback          callback,
Matthias Clasen's avatar
Matthias Clasen committed
217
                                                                       gpointer             callback_target);
218
static void                 gtk_list_box_compute_expand               (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
219 220
                                                                       gboolean            *hexpand,
                                                                       gboolean            *vexpand);
221 222 223
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
224
                                                                       GtkAllocation       *allocation);
225
static void                 gtk_list_box_drag_leave                   (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
226 227
                                                                       GdkDragContext      *context,
                                                                       guint                time_);
228 229 230
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
231 232 233
                                                                       GtkMovementStep      step,
                                                                       gint                 count);
static void                 gtk_list_box_finalize                     (GObject             *obj);
234
static void                 gtk_list_box_parent_set                   (GtkWidget           *widget,
235
                                                                       GtkWidget           *prev_parent);
Alexander Larsson's avatar
Alexander Larsson committed
236

237 238 239 240 241 242 243 244 245 246 247 248 249 250
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
251

252 253 254 255 256 257 258 259 260
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);
261
static void                 gtk_list_box_selected_rows_changed          (GtkListBox          *box);
262

263 264 265 266 267 268 269 270 271 272 273
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);

274 275 276
static void gtk_list_box_update_row_styles (GtkListBox    *box);
static void gtk_list_box_update_row_style  (GtkListBox    *box,
                                            GtkListBoxRow *row);
277

278 279 280 281 282 283
static void                 gtk_list_box_bound_model_changed            (GListModel          *list,
                                                                         guint                position,
                                                                         guint                removed,
                                                                         guint                added,
                                                                         gpointer             user_data);

284
static void                 gtk_list_box_check_model_compat             (GtkListBox          *box);
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

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
309 310
static GParamSpec *properties[LAST_PROPERTY] = { NULL, };
static guint signals[LAST_SIGNAL] = { 0 };
311
static GParamSpec *row_properties[LAST_ROW_PROPERTY] = { NULL, };
312
static guint row_signals[ROW__LAST_SIGNAL] = { 0 };
Alexander Larsson's avatar
Alexander Larsson committed
313

Alexander Larsson's avatar
Alexander Larsson committed
314 315 316 317 318 319 320 321 322
/**
 * gtk_list_box_new:
 *
 * Creates a new #GtkListBox container.
 *
 * Returns: a new #GtkListBox
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
323 324 325 326 327 328 329 330 331 332 333 334
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)
{
335
  GtkListBoxPrivate *priv = BOX_PRIV (obj);
Alexander Larsson's avatar
Alexander Larsson committed
336 337 338 339

  switch (property_id)
    {
    case PROP_SELECTION_MODE:
340
      g_value_set_enum (value, priv->selection_mode);
Alexander Larsson's avatar
Alexander Larsson committed
341 342
      break;
    case PROP_ACTIVATE_ON_SINGLE_CLICK:
343
      g_value_set_boolean (value, priv->activate_single_click);
Alexander Larsson's avatar
Alexander Larsson committed
344 345 346 347 348 349 350 351 352 353 354 355 356
      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)
{
357
  GtkListBox *box = GTK_LIST_BOX (obj);
Alexander Larsson's avatar
Alexander Larsson committed
358 359 360 361

  switch (property_id)
    {
    case PROP_SELECTION_MODE:
362
      gtk_list_box_set_selection_mode (box, g_value_get_enum (value));
Alexander Larsson's avatar
Alexander Larsson committed
363 364
      break;
    case PROP_ACTIVATE_ON_SINGLE_CLICK:
365
      gtk_list_box_set_activate_on_single_click (box, g_value_get_boolean (value));
Alexander Larsson's avatar
Alexander Larsson committed
366 367 368 369 370 371 372 373 374 375
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
      break;
    }
}

static void
gtk_list_box_finalize (GObject *obj)
{
376
  GtkListBoxPrivate *priv = BOX_PRIV (obj);
Alexander Larsson's avatar
Alexander Larsson committed
377 378 379 380 381

  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);
382 383
  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
384 385 386

  g_clear_object (&priv->adjustment);
  g_clear_object (&priv->drag_highlighted_row);
387
  g_clear_object (&priv->multipress_gesture);
Alexander Larsson's avatar
Alexander Larsson committed
388 389

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

392 393 394 395 396 397 398 399 400
  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);
    }

401 402
  g_clear_object (&priv->gadget);

Alexander Larsson's avatar
Alexander Larsson committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
  G_OBJECT_CLASS (gtk_list_box_parent_class)->finalize (obj);
}

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;
419 420 421 422 423 424 425
  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;
426
  widget_class->compute_expand = gtk_list_box_compute_expand;
427 428 429 430 431 432 433 434 435 436
  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;
437
  container_class->forall = gtk_list_box_forall;
438 439 440 441
  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;
442 443 444
  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
445 446 447

  properties[PROP_SELECTION_MODE] =
    g_param_spec_enum ("selection-mode",
Matthias Clasen's avatar
Matthias Clasen committed
448 449
                       P_("Selection mode"),
                       P_("The selection mode"),
Alexander Larsson's avatar
Alexander Larsson committed
450 451
                       GTK_TYPE_SELECTION_MODE,
                       GTK_SELECTION_SINGLE,
452
                       G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
Alexander Larsson's avatar
Alexander Larsson committed
453 454 455

  properties[PROP_ACTIVATE_ON_SINGLE_CLICK] =
    g_param_spec_boolean ("activate-on-single-click",
Matthias Clasen's avatar
Matthias Clasen committed
456 457
                          P_("Activate on Single Click"),
                          P_("Activate row on a single click"),
Alexander Larsson's avatar
Alexander Larsson committed
458
                          TRUE,
459
                          G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
Alexander Larsson's avatar
Alexander Larsson committed
460 461 462

  g_object_class_install_properties (object_class, LAST_PROPERTY, properties);

Alexander Larsson's avatar
Alexander Larsson committed
463 464
  /**
   * GtkListBox::row-selected:
465
   * @box: the #GtkListBox
466
   * @row: (nullable): the selected row
Alexander Larsson's avatar
Alexander Larsson committed
467 468 469 470
   *
   * The ::row-selected signal is emitted when a new row is selected, or
   * (with a %NULL @row) when the selection is cleared.
   *
471 472 473
   * 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.
474
   *
Alexander Larsson's avatar
Alexander Larsson committed
475 476
   * Since: 3.10
   */
Alexander Larsson's avatar
Alexander Larsson committed
477
  signals[ROW_SELECTED] =
478
    g_signal_new (I_("row-selected"),
Alexander Larsson's avatar
Alexander Larsson committed
479 480 481 482 483 484
                  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
485
                  GTK_TYPE_LIST_BOX_ROW);
Matthias Clasen's avatar
Matthias Clasen committed
486

487 488
  /**
   * GtkListBox::selected-rows-changed:
489
   * @box: the #GtkListBox on wich the signal is emitted
490 491 492 493 494 495
   *
   * The ::selected-rows-changed signal is emitted when the
   * set of selected rows changes.
   *
   * Since: 3.14
   */
496
  signals[SELECTED_ROWS_CHANGED] = g_signal_new (I_("selected-rows-changed"),
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
                                                 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
   */
516
  signals[SELECT_ALL] = g_signal_new (I_("select-all"),
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
                                      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
   */
536 537 538 539 540 541 542
  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);
543

Alexander Larsson's avatar
Alexander Larsson committed
544 545
  /**
   * GtkListBox::row-activated:
546
   * @box: the #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
547 548
   * @row: the activated row
   *
549
   * The ::row-activated signal is emitted when a row has been activated by the user.
Alexander Larsson's avatar
Alexander Larsson committed
550 551 552
   *
   * Since: 3.10
   */
Alexander Larsson's avatar
Alexander Larsson committed
553
  signals[ROW_ACTIVATED] =
554
    g_signal_new (I_("row-activated"),
Alexander Larsson's avatar
Alexander Larsson committed
555 556 557 558 559 560
                  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
561
                  GTK_TYPE_LIST_BOX_ROW);
Alexander Larsson's avatar
Alexander Larsson committed
562
  signals[ACTIVATE_CURSOR_ROW] =
563
    g_signal_new (I_("activate-cursor-row"),
Alexander Larsson's avatar
Alexander Larsson committed
564 565 566 567 568 569 570
                  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] =
571
    g_signal_new (I_("toggle-cursor-row"),
Alexander Larsson's avatar
Alexander Larsson committed
572 573 574 575 576 577 578
                  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] =
579
    g_signal_new (I_("move-cursor"),
Alexander Larsson's avatar
Alexander Larsson committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
                  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);
599
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_Up, 0,
Alexander Larsson's avatar
Alexander Larsson committed
600
                                 GTK_MOVEMENT_DISPLAY_LINES, -1);
601
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_KP_Up, 0,
Alexander Larsson's avatar
Alexander Larsson committed
602
                                 GTK_MOVEMENT_DISPLAY_LINES, -1);
603
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_Down, 0,
Alexander Larsson's avatar
Alexander Larsson committed
604
                                 GTK_MOVEMENT_DISPLAY_LINES, 1);
605
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_KP_Down, 0,
Alexander Larsson's avatar
Alexander Larsson committed
606 607 608 609 610 611 612 613 614
                                 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);
615

Alexander Larsson's avatar
Alexander Larsson committed
616 617
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, GDK_CONTROL_MASK,
                                "toggle-cursor-row", 0, NULL);
618 619 620 621 622 623 624
  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);
625 626

  gtk_widget_class_set_css_name (widget_class, "list");
Alexander Larsson's avatar
Alexander Larsson committed
627 628
}

629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
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
667 668
/**
 * gtk_list_box_get_selected_row:
669
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
670 671 672
 *
 * Gets the selected row.
 *
673 674 675 676 677
 * 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
678 679
 *
 * Since: 3.10
Matthias Clasen's avatar
Matthias Clasen committed
680
 */
Alexander Larsson's avatar
Alexander Larsson committed
681
GtkListBoxRow *
682
gtk_list_box_get_selected_row (GtkListBox *box)
Alexander Larsson's avatar
Alexander Larsson committed
683
{
684
  g_return_val_if_fail (GTK_IS_LIST_BOX (box), NULL);
Alexander Larsson's avatar
Alexander Larsson committed
685

686
  return BOX_PRIV (box)->selected_row;
Alexander Larsson's avatar
Alexander Larsson committed
687 688 689 690
}

/**
 * gtk_list_box_get_row_at_index:
691
 * @box: a #GtkListBox
Matthias Clasen's avatar
Matthias Clasen committed
692
 * @index_: the index of the row
Alexander Larsson's avatar
Alexander Larsson committed
693
 *
694
 * Gets the n-th child in the list (not counting headers).
695 696
 * If @_index is negative or larger than the number of items in the
 * list, %NULL is returned.
Alexander Larsson's avatar
Alexander Larsson committed
697
 *
698
 * Returns: (transfer none) (nullable): the child #GtkWidget or %NULL
Alexander Larsson's avatar
Alexander Larsson committed
699 700
 *
 * Since: 3.10
Matthias Clasen's avatar
Matthias Clasen committed
701
 */
Alexander Larsson's avatar
Alexander Larsson committed
702
GtkListBoxRow *
703
gtk_list_box_get_row_at_index (GtkListBox *box,
Matthias Clasen's avatar
Matthias Clasen committed
704
                               gint        index_)
Alexander Larsson's avatar
Alexander Larsson committed
705 706 707
{
  GSequenceIter *iter;

708
  g_return_val_if_fail (GTK_IS_LIST_BOX (box), NULL);
709

710
  iter = g_sequence_get_iter_at_pos (BOX_PRIV (box)->children, index_);
711
  if (!g_sequence_iter_is_end (iter))
Alexander Larsson's avatar
Alexander Larsson committed
712 713 714 715 716
    return g_sequence_get (iter);

  return NULL;
}

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
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
734 735
/**
 * gtk_list_box_get_row_at_y:
736
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
737 738
 * @y: position
 *
Alexander Larsson's avatar
Alexander Larsson committed
739
 * Gets the row at the @y position.
Alexander Larsson's avatar
Alexander Larsson committed
740
 *
741 742
 * 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
743 744
 *
 * Since: 3.10
Matthias Clasen's avatar
Matthias Clasen committed
745
 */
Alexander Larsson's avatar
Alexander Larsson committed
746
GtkListBoxRow *
747
gtk_list_box_get_row_at_y (GtkListBox *box,
Matthias Clasen's avatar
Matthias Clasen committed
748
                           gint        y)
Alexander Larsson's avatar
Alexander Larsson committed
749 750 751
{
  GSequenceIter *iter;

752
  g_return_val_if_fail (GTK_IS_LIST_BOX (box), NULL);
Alexander Larsson's avatar
Alexander Larsson committed
753

754 755 756 757
  iter = g_sequence_lookup (BOX_PRIV (box)->children,
                            GINT_TO_POINTER (y),
                            row_y_cmp_func,
                            NULL);
Alexander Larsson's avatar
Alexander Larsson committed
758

759 760
  if (iter)
    return GTK_LIST_BOX_ROW (g_sequence_get (iter));
Alexander Larsson's avatar
Alexander Larsson committed
761

762
  return NULL;
Alexander Larsson's avatar
Alexander Larsson committed
763 764 765 766
}

/**
 * gtk_list_box_select_row:
767
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
768
 * @row: (allow-none): The row to select or %NULL
Alexander Larsson's avatar
Alexander Larsson committed
769 770 771 772
 *
 * Make @row the currently selected row.
 *
 * Since: 3.10
Alexander Larsson's avatar
Alexander Larsson committed
773 774
 */
void
775
gtk_list_box_select_row (GtkListBox    *box,
Matthias Clasen's avatar
Matthias Clasen committed
776
                         GtkListBoxRow *row)
Alexander Larsson's avatar
Alexander Larsson committed
777
{
778 779
  gboolean dirty = FALSE;

780
  g_return_if_fail (GTK_IS_LIST_BOX (box));
781
  g_return_if_fail (row == NULL || GTK_IS_LIST_BOX_ROW (row));
Alexander Larsson's avatar
Alexander Larsson committed
782

783
  if (row)
784
    gtk_list_box_select_row_internal (box, row);
785
  else
786 787 788 789 790 791 792
    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
793 794
}

795 796
/**   
 * gtk_list_box_unselect_row:
797
 * @box: a #GtkListBox
798 799
 * @row: the row to unselected
 *
800
 * Unselects a single row of @box, if the selection mode allows it.
801 802 803 804
 *
 * Since: 3.14
 */                       
void
805
gtk_list_box_unselect_row (GtkListBox    *box,
806 807
                           GtkListBoxRow *row)
{
808
  g_return_if_fail (GTK_IS_LIST_BOX (box));
809 810
  g_return_if_fail (GTK_IS_LIST_BOX_ROW (row));
  
811
  gtk_list_box_unselect_row_internal (box, row);
812 813 814 815
} 

/**
 * gtk_list_box_select_all:
816
 * @box: a #GtkListBox
817
 *
818
 * Select all children of @box, if the selection mode allows it.
819 820 821 822
 *
 * Since: 3.14
 */
void
823
gtk_list_box_select_all (GtkListBox *box)
824
{
825
  g_return_if_fail (GTK_IS_LIST_BOX (box));
826

827
  if (BOX_PRIV (box)->selection_mode != GTK_SELECTION_MULTIPLE)
828 829
    return;

830
  if (g_sequence_get_length (BOX_PRIV (box)->children) > 0)
831
    {
832 833
      gtk_list_box_select_all_between (box, NULL, NULL, FALSE);
      g_signal_emit (box, signals[SELECTED_ROWS_CHANGED], 0);
834
    }
835 836 837 838
}

/**
 * gtk_list_box_unselect_all:
839
 * @box: a #GtkListBox
840
 *
841
 * Unselect all children of @box, if the selection mode allows it.
842 843 844 845
 *
 * Since: 3.14
 */
void
846
gtk_list_box_unselect_all (GtkListBox *box)
847
{
848 849
  gboolean dirty = FALSE;

850
  g_return_if_fail (GTK_IS_LIST_BOX (box));
851

852
  if (BOX_PRIV (box)->selection_mode == GTK_SELECTION_BROWSE)
853 854
    return;

855
  dirty = gtk_list_box_unselect_all_internal (box);
856 857

  if (dirty)
858 859 860 861
    {
      g_signal_emit (box, signals[ROW_SELECTED], 0, NULL);
      g_signal_emit (box, signals[SELECTED_ROWS_CHANGED], 0);
    }
862 863 864
}

static void
865
gtk_list_box_selected_rows_changed (GtkListBox *box)
866
{
867
  _gtk_list_box_accessible_selection_changed (box);
868 869 870 871
}

/**
 * GtkListBoxForeachFunc:
872
 * @box: a #GtkListBox
873 874 875 876
 * @row: a #GtkListBoxRow
 * @user_data: (closure): user data
 *
 * A function used by gtk_list_box_selected_foreach().
877
 * It will be called on every selected child of the @box.
878 879 880 881 882 883
 *
 * Since: 3.14
 */

/**
 * gtk_list_box_selected_foreach:
884
 * @box: a #GtkListBox
885 886 887 888 889
 * @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.
 *
890
 * Note that the selection cannot be modified from within this function.
891 892 893 894
 *
 * Since: 3.14
 */
void
895
gtk_list_box_selected_foreach (GtkListBox            *box,
896 897 898 899 900 901
                               GtkListBoxForeachFunc  func,
                               gpointer               data)
{
  GtkListBoxRow *row;
  GSequenceIter *iter;

902
  g_return_if_fail (GTK_IS_LIST_BOX (box));
903

904
  for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
905 906 907 908 909
       !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))
910
        (*func) (box, row, data);
911 912 913 914
    }
}

/**
915
 * gtk_list_box_get_selected_rows:
916
 * @box: a #GtkListBox
917 918 919 920 921 922 923 924 925 926
 *
 * 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 *
927
gtk_list_box_get_selected_rows (GtkListBox *box)
928 929 930 931 932
{
  GtkListBoxRow *row;
  GSequenceIter *iter;
  GList *selected = NULL;

933
  g_return_val_if_fail (GTK_IS_LIST_BOX (box), NULL);
934

935
  for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
936 937 938 939 940 941 942 943 944 945
       !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);
}
946 947 948

/**
 * gtk_list_box_set_placeholder:
949
 * @box: a #GtkListBox
950 951 952
 * @placeholder: (allow-none): a #GtkWidget or %NULL
 *
 * Sets the placeholder widget that is shown in the list when
953
 * it doesn't display any visible children.
954 955 956 957
 *
 * Since: 3.10
 */
void
958
gtk_list_box_set_placeholder (GtkListBox *box,
Matthias Clasen's avatar
Matthias Clasen committed
959
                              GtkWidget  *placeholder)
960
{
961
  GtkListBoxPrivate *priv = BOX_PRIV (box);
962

963
  g_return_if_fail (GTK_IS_LIST_BOX (box));
964 965 966 967

  if (priv->placeholder)
    {
      gtk_widget_unparent (priv->placeholder);
968
      gtk_widget_queue_resize (GTK_WIDGET (box));
969 970 971 972 973 974
    }

  priv->placeholder = placeholder;

  if (placeholder)
    {
975
      gtk_widget_set_parent (GTK_WIDGET (placeholder), GTK_WIDGET (box));
976 977 978 979 980 981
      gtk_widget_set_child_visible (GTK_WIDGET (placeholder),
                                    priv->n_visible_rows == 0);
    }
}


Alexander Larsson's avatar
Alexander Larsson committed
982 983
/**
 * gtk_list_box_set_adjustment:
984
 * @box: a #GtkListBox
Matthias Clasen's avatar
Matthias Clasen committed
985
 * @adjustment: (allow-none): the adjustment, or %NULL
Alexander Larsson's avatar
Alexander Larsson committed
986 987 988 989 990
 *
 * 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.
 *
991
 * In the normal case when the @box is packed inside
Alexander Larsson's avatar
Alexander Larsson committed
992 993 994 995 996 997
 * 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
998
void
999
gtk_list_box_set_adjustment (GtkListBox    *box,
Alexander Larsson's avatar
Alexander Larsson committed
1000 1001
                             GtkAdjustment *adjustment)
{
1002
  GtkListBoxPrivate *priv = BOX_PRIV (box);
1003

1004
  g_return_if_fail (GTK_IS_LIST_BOX (box));
1005
  g_return_if_fail (adjustment == NULL || GTK_IS_ADJUSTMENT (adjustment));
Alexander Larsson's avatar
Alexander Larsson committed
1006

1007 1008
  if (adjustment)
    g_object_ref_sink (adjustment);
Alexander Larsson's avatar
Alexander Larsson committed
1009 1010 1011 1012 1013
  if (priv->adjustment)
    g_object_unref (priv->adjustment);
  priv->adjustment = adjustment;
}

Alexander Larsson's avatar
Alexander Larsson committed
1014 1015
/**
 * gtk_list_box_get_adjustment:
1016
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
1017 1018 1019 1020
 *
 * Gets the adjustment (if any) that the widget uses to
 * for vertical scrolling.
 *
1021
 * Returns: (transfer none): the adjustment
Alexander Larsson's avatar
Alexander Larsson committed
1022 1023 1024
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
1025
GtkAdjustment *
1026
gtk_list_box_get_adjustment (GtkListBox *box)
Alexander Larsson's avatar
Alexander Larsson committed
1027
{
1028
  g_return_val_if_fail (GTK_IS_LIST_BOX (box), NULL);
Alexander Larsson's avatar
Alexander Larsson committed
1029

1030
  return BOX_PRIV (box)->adjustment;
Alexander Larsson's avatar
Alexander Larsson committed
1031 1032
}

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
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);
}

1044
static void
1045 1046
gtk_list_box_parent_set (GtkWidget *widget,
                         GtkWidget *prev_parent)
Alexander Larsson's avatar
Alexander Larsson committed
1047
{
1048
  GtkWidget *parent;
Alexander Larsson's avatar
Alexander Larsson committed
1049

1050
  parent = gtk_widget_get_parent (widget);
Alexander Larsson's avatar
Alexander Larsson committed
1051

1052 1053 1054 1055
  if (prev_parent && GTK_IS_SCROLLABLE (prev_parent))
    g_signal_handlers_disconnect_by_func (prev_parent,
                                          G_CALLBACK (adjustment_changed), widget);

1056 1057
  if (parent && GTK_IS_SCROLLABLE (parent))
    {
1058 1059 1060
      adjustment_changed (G_OBJECT (parent), NULL, widget);
      g_signal_connect (parent, "notify::vadjustment",
                        G_CALLBACK (adjustment_changed), widget);
1061
    }
1062 1063
  else
    gtk_list_box_set_adjustment (GTK_LIST_BOX (widget), NULL);
1064
}
Alexander Larsson's avatar
Alexander Larsson committed
1065

Alexander Larsson's avatar
Alexander Larsson committed
1066 1067
/**
 * gtk_list_box_set_selection_mode:
1068
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
1069 1070 1071 1072 1073 1074 1075
 * @mode: The #GtkSelectionMode
 *
 * Sets how selection works in the listbox.
 * See #GtkSelectionMode for details.
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
1076
void
1077
gtk_list_box_set_selection_mode (GtkListBox       *box,
Matthias Clasen's avatar
Matthias Clasen committed
1078
                                 GtkSelectionMode  mode)
Alexander Larsson's avatar
Alexander Larsson committed
1079
{
1080
  GtkListBoxPrivate *priv = BOX_PRIV (box);
1081
  gboolean dirty = FALSE;
1082

1083
  g_return_if_fail (GTK_IS_LIST_BOX (box));
Alexander Larsson's avatar
Alexander Larsson committed
1084

1085 1086
  if (priv->selection_mode == mode)
    return;
Alexander Larsson's avatar
Alexander Larsson committed
1087

1088 1089
  if (mode == GTK_SELECTION_NONE ||
      priv->selection_mode == GTK_SELECTION_MULTIPLE)
1090
    dirty = gtk_list_box_unselect_all_internal (box);
Alexander Larsson's avatar
Alexander Larsson committed
1091 1092 1093

  priv->selection_mode = mode;

1094 1095
  gtk_list_box_update_row_styles (box);

1096
  g_object_notify_by_pspec (G_OBJECT (box), properties[PROP_SELECTION_MODE]);
1097 1098

  if (dirty)
1099 1100 1101 1102
    {
      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
1103 1104
}

Alexander Larsson's avatar
Alexander Larsson committed
1105 1106
/**
 * gtk_list_box_get_selection_mode:
1107
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
1108 1109 1110 1111 1112 1113 1114
 *
 * Gets the selection mode of the listbox.
 *
 * Returns: a #GtkSelectionMode
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
1115
GtkSelectionMode
1116
gtk_list_box_get_selection_mode (GtkListBox *box)
Alexander Larsson's avatar
Alexander Larsson committed
1117
{
1118
  g_return_val_if_fail (GTK_IS_LIST_BOX (box), GTK_SELECTION_NONE);
1119

1120
  return BOX_PRIV (box)->selection_mode;
Alexander Larsson's avatar
Alexander Larsson committed
1121 1122 1123 1124
}

/**
 * gtk_list_box_set_filter_func:
1125
 * @box: a #GtkListBox
Matthias Clasen's avatar
Matthias Clasen committed
1126
 * @filter_func: (closure user_data) (allow-none): callback that lets you filter which rows to show
Alexander Larsson's avatar
Alexander Larsson committed
1127 1128 1129
 * @user_data: user data passed to @filter_func
 * @destroy: destroy notifier for @user_data
 *
1130
 * By setting a filter function on the @box one can decide dynamically which
Alexander Larsson's avatar
Alexander Larsson committed
1131 1132 1133 1134 1135
 * 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
1136
 * when gtk_list_box_invalidate_filter() is called.
Alexander Larsson's avatar
Alexander Larsson committed
1137
 *
1138 1139 1140
 * 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
1141
 * Since: 3.10
Alexander Larsson's avatar
Alexander Larsson committed
1142 1143
 */
void
1144
gtk_list_box_set_filter_func (GtkListBox           *box,
Matthias Clasen's avatar
Matthias Clasen committed
1145 1146 1147
                              GtkListBoxFilterFunc  filter_func,
                              gpointer              user_data,
                              GDestroyNotify        destroy)
Alexander Larsson's avatar
Alexander Larsson committed
1148
{
1149
  GtkListBoxPrivate *priv = BOX_PRIV (box);
Alexander Larsson's avatar
Alexander Larsson committed
1150

1151
  g_return_if_fail (GTK_IS_LIST_BOX (box));
Alexander Larsson's avatar
Alexander Larsson committed
1152 1153 1154 1155 1156 1157 1158 1159

  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;

1160 1161
  gtk_list_box_check_model_compat (box);

1162
  gtk_list_box_invalidate_filter (box);
Alexander Larsson's avatar
Alexander Larsson committed
1163 1164 1165
}

/**
1166
 * gtk_list_box_set_header_func:
1167
 * @box: a #GtkListBox
Matthias Clasen's avatar
Matthias Clasen committed
1168
 * @update_header: (closure user_data) (allow-none): callback that lets you add row headers
1169
 * @user_data: user data passed to @update_header
Alexander Larsson's avatar
Alexander Larsson committed
1170 1171
 * @destroy: destroy notifier for @user_data
 *
1172
 * By setting a header function on the @box one can dynamically add headers
Alexander Larsson's avatar
Alexander Larsson committed
1173 1174 1175 1176
 * 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.
 *
1177
 * The @update_header can look at the current header widget using gtk_list_box_row_get_header()
Alexander Larsson's avatar
Alexander Larsson committed
1178
 * and either update the state of the widget as needed, or set a new one using
1179
 * gtk_list_box_row_set_header(). If no header is needed, set the header to %NULL.
Alexander Larsson's avatar
Alexander Larsson committed
1180
 *
1181
 * Note that you may get many calls @update_header to this for a particular row when e.g.
1182
 * changing things that don’t affect the header. In this case it is important for performance
Matthias Clasen's avatar
Matthias Clasen committed
1183
 * to not blindly replace an existing header with an identical one.
Alexander Larsson's avatar
Alexander Larsson committed
1184
 *
1185
 * The @update_header function will be called for each row after the call, and it will
Alexander Larsson's avatar
Alexander Larsson committed
1186 1187 1188
 * 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
1189
 * gtk_list_box_invalidate_headers() is called.
Alexander Larsson's avatar
Alexander Larsson committed
1190 1191
 *
 * Since: 3.10
Alexander Larsson's avatar
Alexander Larsson committed
1192 1193
 */
void
1194
gtk_list_box_set_header_func (GtkListBox                 *box,
Matthias Clasen's avatar
Matthias Clasen committed
1195 1196 1197
                              GtkListBoxUpdateHeaderFunc  update_header,
                              gpointer                    user_data,
                              GDestroyNotify              destroy)
Alexander Larsson's avatar
Alexander Larsson committed
1198
{
1199
  GtkListBoxPrivate *priv = BOX_PRIV (box);
Alexander Larsson's avatar
Alexander Larsson committed
1200

1201
  g_return_if_fail (GTK_IS_LIST_BOX (box));
Alexander Larsson's avatar
Alexander Larsson committed
1202

1203 1204
  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
1205

1206 1207 1208
  priv->update_header_func = update_header;
  priv->update_header_func_target = user_data;
  priv->update_header_func_target_destroy_notify = destroy;
1209
  gtk_list_box_invalidate_headers (box);
Alexander Larsson's avatar
Alexander Larsson committed
1210 1211
}

Alexander Larsson's avatar
Alexander Larsson committed
1212
/**
1213
 * gtk_list_box_invalidate_filter:
1214
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
1215 1216
 *
 * Update the filtering for all rows. Call this when result
1217
 * of the filter function on the @box is changed due
Alexander Larsson's avatar
Alexander Larsson committed
1218 1219 1220 1221 1222 1223
 * 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
1224
void
1225
gtk_list_box_invalidate_filter (GtkListBox *box)
Alexander Larsson's avatar
Alexander Larsson committed
1226
{
1227
  g_return_if_fail (GTK_IS_LIST_BOX (box));
Alexander Larsson's avatar
Alexander Larsson committed
1228

1229 1230 1231
  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
1232 1233 1234 1235 1236
}

static gint
do_sort (GtkListBoxRow *a,
         GtkListBoxRow *b,
1237
         GtkListBox    *box)
Alexander Larsson's avatar
Alexander Larsson committed
1238
{
1239
  GtkListBoxPrivate *priv = BOX_PRIV (box);
Alexander Larsson's avatar
Alexander Larsson committed
1240 1241 1242 1243

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

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
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);
1257 1258 1259
      gtk_css_node_insert_after (gtk_css_node_get_parent (row_node),
                                 row_node,
                                 prev_node);
1260 1261 1262 1263 1264
    }

  *previous = row;
}

Alexander Larsson's avatar
Alexander Larsson committed
1265
/**
1266
 * gtk_list_box_invalidate_sort:
1267
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
1268 1269
 *
 * Update the sorting for all rows. Call this when result
1270
 * of the sort function on the @box is changed due
Alexander Larsson's avatar
Alexander Larsson committed
1271 1272 1273 1274
 * to an external factor.
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
1275
void
1276
gtk_list_box_invalidate_sort (GtkListBox *box)
Alexander Larsson's avatar
Alexander Larsson committed
1277
{
1278
  GtkListBoxPrivate *priv = BOX_PRIV (box);
1279
  GtkWidget *previous = NULL;
1280

1281
  g_return_if_fail (GTK_IS_LIST_BOX (box));
Alexander Larsson's avatar
Alexander Larsson committed
1282

1283
  g_sequence_sort (priv->children, (GCompareDataFunc)do_sort, box);
1284
  g_sequence_foreach (priv->children, gtk_list_box_css_node_foreach, &previous);
Alexander Larsson's avatar
Alexander Larsson committed
1285

1286 1287
  gtk_list_box_invalidate_headers (box);
  gtk_widget_queue_resize (GTK_WIDGET (box));
Alexander Larsson's avatar
Alexander Larsson committed
1288 1289
}

1290
static void
1291
gtk_list_box_do_reseparate (GtkListBox *box)
1292 1293 1294
{
  GSequenceIter *iter;

1295
  for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
1296 1297
       !g_sequence_iter_is_end (iter);
       iter = g_sequence_iter_next (iter))
1298
    gtk_list_box_update_header (box, iter);
1299

1300
  gtk_widget_queue_resize (GTK_WIDGET (box));
1301 1302 1303
}


Alexander Larsson's avatar
Alexander Larsson committed
1304
/**
1305
 * gtk_list_box_invalidate_headers:
1306
 * @box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
1307 1308
 *
 * Update the separators for all rows. Call this when result
1309
 * of the header function on the @box is changed due
Alexander Larsson's avatar
Alexander Larsson committed
1310 1311 1312 1313
 * to an external factor.
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
1314
void
1315
gtk_list_box_invalidate_headers (GtkListBox *box)
Alexander Larsson's avatar
Alexander Larsson committed
1316
{
1317
  g_return_if_fail (GTK_IS_LIST_BOX (box));
Alexander Larsson's avatar
Alexander Larsson committed
1318

1319
  if (!gtk_widget_get_visible (GTK_WIDGET (box)))
1320
    return;
Alexander Larsson's avatar
Alexander Larsson committed
1321

1322
  gtk_list_box_do_reseparate (box);
Alexander Larsson's avatar
Alexander Larsson committed
1323 1324 1325 1326
}

/**
 * gtk_list_box_set_sort_func:
1327
 * @box: a #GtkListBox
Matthias Clasen's avatar
Matthias Clasen committed
1328
 * @sort_func: (closure user_data) (allow-none): the sort function
Alexander Larsson's avatar
Alexander Larsson committed
1329 1330 1331
 * @user_data: user data passed to @sort_func
 * @destroy: destroy notifier for @user_data
 *
1332
 * By setting a sort function on the @box one can dynamically reorder the rows
Alexander Larsson's avatar
Alexander Larsson committed
1333 1334 1335 1336
 * of the list, based on the contents of the rows.
 *
 * The @sort_func will be called for each row after the call, and will continue to
 * be called each time a row changes (via gtk_list_box_row_changed()) and when
1337
 * gtk_list_box_invalidate_sort() is called.
Alexander Larsson's avatar
Alexander Larsson committed
1338
 *
1339 1340 1341
 * Note that using a sort function is incompatible with using a model
 * (see gtk_list_box_bind_model()).
 *
Alexander Larsson's avatar
Alexander Larsson committed
1342
 * Since: 3.10
Alexander Larsson's avatar
Alexander Larsson committed
1343 1344
 */
void
1345
gtk_list_box_set_sort_func (GtkListBox         *box,
Matthias Clasen's avatar
Matthias Clasen committed
1346 1347 1348
                            GtkListBoxSortFunc  sort_func,
                            gpointer            user_data,
                            GDestroyNotify      destroy)
Alexander Larsson's avatar
Alexander Larsson committed
1349
{
1350
  GtkListBoxPrivate *priv = BOX_PRIV (box);
Alexander Larsson's avatar
Alexander Larsson committed
1351

1352
  g_return_if_fail (GTK_IS_LIST_BOX (box));
Alexander Larsson's avatar
Alexander Larsson committed
1353 1354 1355 1356 1357 1358 1359

  if (priv->sort_func_target_destroy_notify != NULL)
    priv->sort_func_target_destroy_notify (priv->sort_func_target);

  priv->sort_func = sort_func;
  priv->sort_func_target = user_data;
  priv->sort_func_target_destroy_notify = destroy;
1360

1361 1362
  gtk_list_box_check_model_compat (box);