gtklistbox.c 91.1 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"

Matthias Clasen's avatar
Matthias Clasen committed
20 21 22 23 24 25
#include "gtklistbox.h"
#include "gtkwidget.h"
#include "gtkmarshalers.h"
#include "gtkprivate.h"
#include "gtkintl.h"

Alexander Larsson's avatar
Alexander Larsson committed
26 27 28 29 30
#include <float.h>
#include <math.h>
#include <string.h>

#include "a11y/gtklistboxaccessibleprivate.h"
31
#include "a11y/gtklistboxrowaccessible.h"
Alexander Larsson's avatar
Alexander Larsson committed
32 33 34 35 36

/**
 * SECTION:gtklistbox
 * @Short_description: A list container
 * @Title: GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
37
 * @See_also: #GtkScrolledWindow
Alexander Larsson's avatar
Alexander Larsson committed
38
 *
Alexander Larsson's avatar
Alexander Larsson committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 * 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.
 *
 * The GtkListBox widget was added in GTK+ 3.10.
Matthias Clasen's avatar
Matthias Clasen committed
55
 */
Alexander Larsson's avatar
Alexander Larsson committed
56

57
typedef struct
Alexander Larsson's avatar
Alexander Larsson committed
58 59
{
  GSequence *children;
60
  GHashTable *header_hash;
Alexander Larsson's avatar
Alexander Larsson committed
61

62 63
  GtkWidget *placeholder;

Alexander Larsson's avatar
Alexander Larsson committed
64 65 66 67 68 69 70 71
  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;

72 73 74
  GtkListBoxUpdateHeaderFunc update_header_func;
  gpointer update_header_func_target;
  GDestroyNotify update_header_func_target_destroy_notify;
Alexander Larsson's avatar
Alexander Larsson committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

  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;

  /* DnD */
  GtkListBoxRow *drag_highlighted_row;
90 91

  int n_visible_rows;
92
  gboolean in_widget;
93
} GtkListBoxPrivate;
Alexander Larsson's avatar
Alexander Larsson committed
94

95
typedef struct
Alexander Larsson's avatar
Alexander Larsson committed
96 97
{
  GSequenceIter *iter;
98
  GtkWidget *header;
Alexander Larsson's avatar
Alexander Larsson committed
99 100
  gint y;
  gint height;
101
  gboolean visible;
102
} GtkListBoxRowPrivate;
Alexander Larsson's avatar
Alexander Larsson committed
103 104 105 106 107 108 109 110 111 112

enum {
  ROW_SELECTED,
  ROW_ACTIVATED,
  ACTIVATE_CURSOR_ROW,
  TOGGLE_CURSOR_ROW,
  MOVE_CURSOR,
  LAST_SIGNAL
};

113 114 115 116 117
enum {
  ROW__ACTIVATE,
  ROW__LAST_SIGNAL
};

Alexander Larsson's avatar
Alexander Larsson committed
118 119 120 121 122 123 124
enum  {
  PROP_0,
  PROP_SELECTION_MODE,
  PROP_ACTIVATE_ON_SINGLE_CLICK,
  LAST_PROPERTY
};

125 126
G_DEFINE_TYPE_WITH_PRIVATE (GtkListBox, gtk_list_box, GTK_TYPE_CONTAINER)
G_DEFINE_TYPE_WITH_PRIVATE (GtkListBoxRow, gtk_list_box_row, GTK_TYPE_BIN)
Alexander Larsson's avatar
Alexander Larsson committed
127 128 129 130

static void                 gtk_list_box_update_selected              (GtkListBox          *list_box,
                                                                       GtkListBoxRow       *row);
static void                 gtk_list_box_apply_filter_all             (GtkListBox          *list_box);
Matthias Clasen's avatar
Matthias Clasen committed
131
static void                 gtk_list_box_update_header                (GtkListBox          *list_box,
Alexander Larsson's avatar
Alexander Larsson committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
                                                                       GSequenceIter       *iter);
static GSequenceIter *      gtk_list_box_get_next_visible             (GtkListBox          *list_box,
                                                                       GSequenceIter       *_iter);
static void                 gtk_list_box_apply_filter                 (GtkListBox          *list_box,
                                                                       GtkListBoxRow       *row);
static void                 gtk_list_box_add_move_binding             (GtkBindingSet       *binding_set,
                                                                       guint                keyval,
                                                                       GdkModifierType      modmask,
                                                                       GtkMovementStep      step,
                                                                       gint                 count);
static void                 gtk_list_box_update_cursor                (GtkListBox          *list_box,
                                                                       GtkListBoxRow       *row);
static void                 gtk_list_box_select_and_activate          (GtkListBox          *list_box,
                                                                       GtkListBoxRow       *row);
static void                 gtk_list_box_update_prelight              (GtkListBox          *list_box,
                                                                       GtkListBoxRow       *row);
static void                 gtk_list_box_update_active                (GtkListBox          *list_box,
                                                                       GtkListBoxRow       *row);
150
static gboolean             gtk_list_box_enter_notify_event           (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
151
                                                                       GdkEventCrossing    *event);
152
static gboolean             gtk_list_box_leave_notify_event           (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
153
                                                                       GdkEventCrossing    *event);
154
static gboolean             gtk_list_box_motion_notify_event          (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
155
                                                                       GdkEventMotion      *event);
156
static gboolean             gtk_list_box_button_press_event           (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
157
                                                                       GdkEventButton      *event);
158
static gboolean             gtk_list_box_button_release_event         (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
159
                                                                       GdkEventButton      *event);
160 161
static void                 gtk_list_box_show                         (GtkWidget           *widget);
static gboolean             gtk_list_box_focus                        (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
162 163 164
                                                                       GtkDirectionType     direction);
static GSequenceIter*       gtk_list_box_get_previous_visible         (GtkListBox          *list_box,
                                                                       GSequenceIter       *_iter);
165 166
static GtkListBoxRow       *gtk_list_box_get_first_focusable          (GtkListBox          *list_box);
static GtkListBoxRow       *gtk_list_box_get_last_focusable           (GtkListBox          *list_box);
167
static gboolean             gtk_list_box_draw                         (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
168
                                                                       cairo_t             *cr);
169 170
static void                 gtk_list_box_realize                      (GtkWidget           *widget);
static void                 gtk_list_box_add                          (GtkContainer        *container,
Alexander Larsson's avatar
Alexander Larsson committed
171
                                                                       GtkWidget           *widget);
172
static void                 gtk_list_box_remove                       (GtkContainer        *container,
Alexander Larsson's avatar
Alexander Larsson committed
173
                                                                       GtkWidget           *widget);
174
static void                 gtk_list_box_forall_internal              (GtkContainer        *container,
Alexander Larsson's avatar
Alexander Larsson committed
175 176
                                                                       gboolean             include_internals,
                                                                       GtkCallback          callback,
Matthias Clasen's avatar
Matthias Clasen committed
177
                                                                       gpointer             callback_target);
178
static void                 gtk_list_box_compute_expand_internal      (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
179 180
                                                                       gboolean            *hexpand,
                                                                       gboolean            *vexpand);
181 182 183
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
184
                                                                       GtkAllocation       *allocation);
185
static void                 gtk_list_box_drag_leave                   (GtkWidget           *widget,
Alexander Larsson's avatar
Alexander Larsson committed
186 187
                                                                       GdkDragContext      *context,
                                                                       guint                time_);
188 189 190
static void                 gtk_list_box_activate_cursor_row          (GtkListBox          *list_box);
static void                 gtk_list_box_toggle_cursor_row            (GtkListBox          *list_box);
static void                 gtk_list_box_move_cursor                  (GtkListBox          *list_box,
Alexander Larsson's avatar
Alexander Larsson committed
191 192 193
                                                                       GtkMovementStep      step,
                                                                       gint                 count);
static void                 gtk_list_box_finalize                     (GObject             *obj);
194
static void                 gtk_list_box_parent_set                   (GtkWidget           *widget,
195
                                                                       GtkWidget           *prev_parent);
Alexander Larsson's avatar
Alexander Larsson committed
196

197 198 199 200 201 202 203 204 205 206 207 208 209 210
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
211 212 213

static GParamSpec *properties[LAST_PROPERTY] = { NULL, };
static guint signals[LAST_SIGNAL] = { 0 };
214
static guint row_signals[ROW__LAST_SIGNAL] = { 0 };
Alexander Larsson's avatar
Alexander Larsson committed
215

Alexander Larsson's avatar
Alexander Larsson committed
216 217 218 219 220 221 222 223 224
/**
 * gtk_list_box_new:
 *
 * Creates a new #GtkListBox container.
 *
 * Returns: a new #GtkListBox
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
225 226 227 228 229 230 231 232 233
GtkWidget *
gtk_list_box_new (void)
{
  return g_object_new (GTK_TYPE_LIST_BOX, NULL);
}

static void
gtk_list_box_init (GtkListBox *list_box)
{
234
  GtkListBoxPrivate *priv = gtk_list_box_get_instance_private (list_box);
235
  GtkStyleContext *context;
Alexander Larsson's avatar
Alexander Larsson committed
236 237 238 239 240 241 242

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

  priv->children = g_sequence_new (NULL);
243
  priv->header_hash = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, NULL);
244 245 246

  context = gtk_widget_get_style_context (GTK_WIDGET (list_box));
  gtk_style_context_add_class (context, GTK_STYLE_CLASS_LIST);
Alexander Larsson's avatar
Alexander Larsson committed
247 248 249 250 251 252 253 254 255
}

static void
gtk_list_box_get_property (GObject    *obj,
                           guint       property_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
  GtkListBox *list_box = GTK_LIST_BOX (obj);
256
  GtkListBoxPrivate *priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
257 258 259 260

  switch (property_id)
    {
    case PROP_SELECTION_MODE:
261
      g_value_set_enum (value, priv->selection_mode);
Alexander Larsson's avatar
Alexander Larsson committed
262 263
      break;
    case PROP_ACTIVATE_ON_SINGLE_CLICK:
264
      g_value_set_boolean (value, priv->activate_single_click);
Alexander Larsson's avatar
Alexander Larsson committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
      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)
{
  GtkListBox *list_box = GTK_LIST_BOX (obj);

  switch (property_id)
    {
    case PROP_SELECTION_MODE:
      gtk_list_box_set_selection_mode (list_box, g_value_get_enum (value));
      break;
    case PROP_ACTIVATE_ON_SINGLE_CLICK:
      gtk_list_box_set_activate_on_single_click (list_box, g_value_get_boolean (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
      break;
    }
}

static void
gtk_list_box_finalize (GObject *obj)
{
  GtkListBox *list_box = GTK_LIST_BOX (obj);
298
  GtkListBoxPrivate *priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
299 300 301 302 303

  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);
304 305
  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
306 307 308 309 310

  g_clear_object (&priv->adjustment);
  g_clear_object (&priv->drag_highlighted_row);

  g_sequence_free (priv->children);
311
  g_hash_table_unref (priv->header_hash);
Alexander Larsson's avatar
Alexander Larsson committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

  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;
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
  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->button_press_event = gtk_list_box_button_press_event;
  widget_class->button_release_event = gtk_list_box_button_release_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;
  widget_class->compute_expand = gtk_list_box_compute_expand_internal;
  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;
  container_class->forall = gtk_list_box_forall_internal;
  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;
Alexander Larsson's avatar
Alexander Larsson committed
354 355 356

  properties[PROP_SELECTION_MODE] =
    g_param_spec_enum ("selection-mode",
Matthias Clasen's avatar
Matthias Clasen committed
357 358
                       P_("Selection mode"),
                       P_("The selection mode"),
Alexander Larsson's avatar
Alexander Larsson committed
359 360 361 362 363 364
                       GTK_TYPE_SELECTION_MODE,
                       GTK_SELECTION_SINGLE,
                       G_PARAM_READWRITE);

  properties[PROP_ACTIVATE_ON_SINGLE_CLICK] =
    g_param_spec_boolean ("activate-on-single-click",
Matthias Clasen's avatar
Matthias Clasen committed
365 366
                          P_("Activate on Single Click"),
                          P_("Activate row on a single click"),
Alexander Larsson's avatar
Alexander Larsson committed
367 368 369 370 371
                          TRUE,
                          G_PARAM_READWRITE);

  g_object_class_install_properties (object_class, LAST_PROPERTY, properties);

Alexander Larsson's avatar
Alexander Larsson committed
372 373 374 375 376 377 378 379 380 381
  /**
   * GtkListBox::row-selected:
   * @list_box: the #GtkListBox
   * @row: the selected row
   *
   * The ::row-selected signal is emitted when a new row is selected, or
   * (with a %NULL @row) when the selection is cleared.
   *
   * Since: 3.10
   */
Alexander Larsson's avatar
Alexander Larsson committed
382 383 384 385 386 387 388 389
  signals[ROW_SELECTED] =
    g_signal_new ("row-selected",
                  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
390
                  GTK_TYPE_LIST_BOX_ROW);
Matthias Clasen's avatar
Matthias Clasen committed
391

Alexander Larsson's avatar
Alexander Larsson committed
392 393 394 395 396 397 398 399 400 401
  /**
   * GtkListBox::row-activated:
   * @list_box: the #GtkListBox
   * @row: the activated row
   *
   * The ::row-activated signal is emitted when a row has been activated by
   * the user.
   *
   * Since: 3.10
   */
Alexander Larsson's avatar
Alexander Larsson committed
402 403 404 405 406 407 408 409
  signals[ROW_ACTIVATED] =
    g_signal_new ("row-activated",
                  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
410
                  GTK_TYPE_LIST_BOX_ROW);
Alexander Larsson's avatar
Alexander Larsson committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
  signals[ACTIVATE_CURSOR_ROW] =
    g_signal_new ("activate-cursor-row",
                  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] =
    g_signal_new ("toggle-cursor-row",
                  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] =
    g_signal_new ("move-cursor",
                  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);
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK,
                                 GTK_MOVEMENT_DISPLAY_LINES, -1);
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_KP_Up, GDK_CONTROL_MASK,
                                 GTK_MOVEMENT_DISPLAY_LINES, -1);
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK,
                                 GTK_MOVEMENT_DISPLAY_LINES, 1);
  gtk_list_box_add_move_binding (binding_set, GDK_KEY_KP_Down, GDK_CONTROL_MASK,
                                 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);
  gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, GDK_CONTROL_MASK,
                                "toggle-cursor-row", 0, NULL);
}

/**
 * gtk_list_box_get_selected_row:
Matthias Clasen's avatar
Matthias Clasen committed
470
 * @list_box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
471 472 473
 *
 * Gets the selected row.
 *
Matthias Clasen's avatar
Matthias Clasen committed
474
 * Return value: (transfer none): the selected #GtkWidget
Alexander Larsson's avatar
Alexander Larsson committed
475 476
 *
 * Since: 3.10
Matthias Clasen's avatar
Matthias Clasen committed
477
 */
Alexander Larsson's avatar
Alexander Larsson committed
478 479 480
GtkListBoxRow *
gtk_list_box_get_selected_row (GtkListBox *list_box)
{
481 482 483
  GtkListBoxPrivate *priv;

  g_return_val_if_fail (GTK_IS_LIST_BOX (list_box), NULL);
Alexander Larsson's avatar
Alexander Larsson committed
484

485
  priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
486 487 488 489 490 491 492 493 494

  if (priv->selected_row != NULL)
    return priv->selected_row;

  return NULL;
}

/**
 * gtk_list_box_get_row_at_index:
Matthias Clasen's avatar
Matthias Clasen committed
495 496
 * @list_box: a #GtkListBox
 * @index_: the index of the row
Alexander Larsson's avatar
Alexander Larsson committed
497
 *
498
 * Gets the n:th child in the list (not counting headers).
Alexander Larsson's avatar
Alexander Larsson committed
499
 *
Matthias Clasen's avatar
Matthias Clasen committed
500
 * Return value: (transfer none): the child #GtkWidget
Alexander Larsson's avatar
Alexander Larsson committed
501 502
 *
 * Since: 3.10
Matthias Clasen's avatar
Matthias Clasen committed
503
 */
Alexander Larsson's avatar
Alexander Larsson committed
504
GtkListBoxRow *
Matthias Clasen's avatar
Matthias Clasen committed
505 506
gtk_list_box_get_row_at_index (GtkListBox *list_box,
                               gint        index_)
Alexander Larsson's avatar
Alexander Larsson committed
507
{
508
  GtkListBoxPrivate *priv;
Alexander Larsson's avatar
Alexander Larsson committed
509 510
  GSequenceIter *iter;

511 512 513
  g_return_val_if_fail (GTK_IS_LIST_BOX (list_box), NULL);

  priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
514

Matthias Clasen's avatar
Matthias Clasen committed
515
  iter = g_sequence_get_iter_at_pos (priv->children, index_);
Alexander Larsson's avatar
Alexander Larsson committed
516 517 518 519 520 521 522 523
  if (iter)
    return g_sequence_get (iter);

  return NULL;
}

/**
 * gtk_list_box_get_row_at_y:
Matthias Clasen's avatar
Matthias Clasen committed
524
 * @list_box: a #GtkListBox
Alexander Larsson's avatar
Alexander Larsson committed
525 526
 * @y: position
 *
Alexander Larsson's avatar
Alexander Larsson committed
527
 * Gets the row at the @y position.
Alexander Larsson's avatar
Alexander Larsson committed
528
 *
Matthias Clasen's avatar
Matthias Clasen committed
529
 * Return value: (transfer none): the row
Alexander Larsson's avatar
Alexander Larsson committed
530 531
 *
 * Since: 3.10
Matthias Clasen's avatar
Matthias Clasen committed
532
 */
Alexander Larsson's avatar
Alexander Larsson committed
533
GtkListBoxRow *
Matthias Clasen's avatar
Matthias Clasen committed
534 535
gtk_list_box_get_row_at_y (GtkListBox *list_box,
                           gint        y)
Alexander Larsson's avatar
Alexander Larsson committed
536
{
537
  GtkListBoxPrivate *priv;
Alexander Larsson's avatar
Alexander Larsson committed
538 539 540 541
  GtkListBoxRow *row, *found_row;
  GtkListBoxRowPrivate *row_priv;
  GSequenceIter *iter;

542 543 544
  g_return_val_if_fail (GTK_IS_LIST_BOX (list_box), NULL);

  priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
545 546 547 548 549 550 551 552 553

  /* TODO: This should use g_sequence_search */

  found_row = NULL;
  for (iter = g_sequence_get_begin_iter (priv->children);
       !g_sequence_iter_is_end (iter);
       iter = g_sequence_iter_next (iter))
    {
      row = (GtkListBoxRow*) g_sequence_get (iter);
554
      row_priv = gtk_list_box_row_get_instance_private (row);
Alexander Larsson's avatar
Alexander Larsson committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568
      if (y >= row_priv->y && y < (row_priv->y + row_priv->height))
        {
          found_row = row;
          break;
        }
    }

  return found_row;
}

/**
 * gtk_list_box_select_row:
 * @list_box: a #GtkListBox
 * @row: (allow-none): The row to select or %NULL
Alexander Larsson's avatar
Alexander Larsson committed
569 570 571 572
 *
 * Make @row the currently selected row.
 *
 * Since: 3.10
Alexander Larsson's avatar
Alexander Larsson committed
573 574
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
575 576
gtk_list_box_select_row (GtkListBox    *list_box,
                         GtkListBoxRow *row)
Alexander Larsson's avatar
Alexander Larsson committed
577
{
578
  g_return_if_fail (GTK_IS_LIST_BOX (list_box));
Alexander Larsson's avatar
Alexander Larsson committed
579 580 581 582

  gtk_list_box_update_selected (list_box, row);
}

583 584 585 586 587 588 589 590 591 592 593 594

/**
 * gtk_list_box_set_placeholder:
 * @list_box: a #GtkListBox
 * @placeholder: (allow-none): a #GtkWidget or %NULL
 *
 * Sets the placeholder widget that is shown in the list when
 * it doesn't display any visible children.
 *
 * Since: 3.10
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
595 596
gtk_list_box_set_placeholder (GtkListBox *list_box,
                              GtkWidget  *placeholder)
597
{
598 599 600 601 602
  GtkListBoxPrivate *priv;

  g_return_if_fail (GTK_IS_LIST_BOX (list_box));

  priv = gtk_list_box_get_instance_private (list_box);
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620

  if (priv->placeholder)
    {
      gtk_widget_unparent (priv->placeholder);
      gtk_widget_queue_resize (GTK_WIDGET (list_box));
    }

  priv->placeholder = placeholder;

  if (placeholder)
    {
      gtk_widget_set_parent (GTK_WIDGET (placeholder), GTK_WIDGET (list_box));
      gtk_widget_set_child_visible (GTK_WIDGET (placeholder),
                                    priv->n_visible_rows == 0);
    }
}


Alexander Larsson's avatar
Alexander Larsson committed
621 622 623
/**
 * gtk_list_box_set_adjustment:
 * @list_box: a #GtkListBox
Matthias Clasen's avatar
Matthias Clasen committed
624
 * @adjustment: (allow-none): the adjustment, or %NULL
Alexander Larsson's avatar
Alexander Larsson committed
625 626 627 628 629 630 631 632 633 634 635 636
 *
 * 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.
 *
 * In the normal case when the @list_box is packed inside
 * 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
637
void
Matthias Clasen's avatar
Matthias Clasen committed
638
gtk_list_box_set_adjustment (GtkListBox    *list_box,
Alexander Larsson's avatar
Alexander Larsson committed
639 640
                             GtkAdjustment *adjustment)
{
641
  GtkListBoxPrivate *priv;
Alexander Larsson's avatar
Alexander Larsson committed
642

643 644 645
  g_return_if_fail (GTK_IS_LIST_BOX (list_box));

  priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
646

647
  g_object_ref_sink (adjustment);
Alexander Larsson's avatar
Alexander Larsson committed
648 649 650 651 652
  if (priv->adjustment)
    g_object_unref (priv->adjustment);
  priv->adjustment = adjustment;
}

Alexander Larsson's avatar
Alexander Larsson committed
653 654 655 656 657 658 659
/**
 * gtk_list_box_get_adjustment:
 * @list_box: a #GtkListBox
 *
 * Gets the adjustment (if any) that the widget uses to
 * for vertical scrolling.
 *
Matthias Clasen's avatar
Matthias Clasen committed
660
 * Return value: (transfer none): the adjustment
Alexander Larsson's avatar
Alexander Larsson committed
661 662 663
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
664 665 666
GtkAdjustment *
gtk_list_box_get_adjustment (GtkListBox *list_box)
{
667 668 669
  GtkListBoxPrivate *priv;

  g_return_val_if_fail (GTK_IS_LIST_BOX (list_box), NULL);
Alexander Larsson's avatar
Alexander Larsson committed
670

671
  priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
672 673 674 675

  return priv->adjustment;
}

676
static void
677 678
gtk_list_box_parent_set (GtkWidget *widget,
                         GtkWidget *prev_parent)
Alexander Larsson's avatar
Alexander Larsson committed
679
{
680 681
  GtkWidget *parent;
  GtkAdjustment *adjustment;
Alexander Larsson's avatar
Alexander Larsson committed
682

683
  parent = gtk_widget_get_parent (widget);
Alexander Larsson's avatar
Alexander Larsson committed
684

685 686 687 688 689 690
  if (parent && GTK_IS_SCROLLABLE (parent))
    {
      adjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (parent));
      gtk_list_box_set_adjustment (GTK_LIST_BOX (widget), adjustment);
    }
}
Alexander Larsson's avatar
Alexander Larsson committed
691

Alexander Larsson's avatar
Alexander Larsson committed
692 693 694 695 696 697 698 699 700 701 702 703
/**
 * gtk_list_box_set_selection_mode:
 * @list_box: a #GtkListBox
 * @mode: The #GtkSelectionMode
 *
 * Sets how selection works in the listbox.
 * See #GtkSelectionMode for details.
 *
 * Note: #GtkListBox does not support @GTK_SELECTION_MULTIPLE.
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
704
void
Matthias Clasen's avatar
Matthias Clasen committed
705 706
gtk_list_box_set_selection_mode (GtkListBox       *list_box,
                                 GtkSelectionMode  mode)
Alexander Larsson's avatar
Alexander Larsson committed
707
{
708 709 710
  GtkListBoxPrivate *priv;

  g_return_if_fail (GTK_IS_LIST_BOX (list_box));
Alexander Larsson's avatar
Alexander Larsson committed
711

712
  priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729

  if (mode == GTK_SELECTION_MULTIPLE)
    {
      g_warning ("Multiple selections not supported");
      return;
    }

  if (priv->selection_mode == mode)
    return;

  priv->selection_mode = mode;
  if (mode == GTK_SELECTION_NONE)
    gtk_list_box_update_selected (list_box, NULL);

  g_object_notify_by_pspec (G_OBJECT (list_box), properties[PROP_SELECTION_MODE]);
}

Alexander Larsson's avatar
Alexander Larsson committed
730 731 732 733 734 735 736 737 738 739
/**
 * gtk_list_box_get_selection_mode:
 * @list_box: a #GtkListBox
 *
 * Gets the selection mode of the listbox.
 *
 * Returns: a #GtkSelectionMode
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
740 741 742
GtkSelectionMode
gtk_list_box_get_selection_mode (GtkListBox *list_box)
{
743 744 745
  GtkListBoxPrivate *priv;

  g_return_val_if_fail (GTK_IS_LIST_BOX (list_box), GTK_SELECTION_NONE);
Alexander Larsson's avatar
Alexander Larsson committed
746

747
  priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
748 749 750 751 752 753

  return priv->selection_mode;
}

/**
 * gtk_list_box_set_filter_func:
Alexander Larsson's avatar
Alexander Larsson committed
754
 * @list_box: a #GtkListBox
Matthias Clasen's avatar
Matthias Clasen committed
755
 * @filter_func: (closure user_data) (allow-none): callback that lets you filter which rows to show
Alexander Larsson's avatar
Alexander Larsson committed
756 757 758 759 760 761 762 763 764
 * @user_data: user data passed to @filter_func
 * @destroy: destroy notifier for @user_data
 *
 * By setting a filter function on the @list_box one can decide dynamically which
 * 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
765
 * when gtk_list_box_invalidate_filter() is called.
Alexander Larsson's avatar
Alexander Larsson committed
766 767
 *
 * Since: 3.10
Alexander Larsson's avatar
Alexander Larsson committed
768 769
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
770 771 772 773
gtk_list_box_set_filter_func (GtkListBox           *list_box,
                              GtkListBoxFilterFunc  filter_func,
                              gpointer              user_data,
                              GDestroyNotify        destroy)
Alexander Larsson's avatar
Alexander Larsson committed
774
{
775 776 777
  GtkListBoxPrivate *priv;

  g_return_if_fail (GTK_IS_LIST_BOX (list_box));
Alexander Larsson's avatar
Alexander Larsson committed
778

779
  priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
780 781 782 783 784 785 786 787

  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;

788
  gtk_list_box_invalidate_filter (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
789 790 791
}

/**
792
 * gtk_list_box_set_header_func:
Alexander Larsson's avatar
Alexander Larsson committed
793
 * @list_box: a #GtkListBox
Matthias Clasen's avatar
Matthias Clasen committed
794
 * @update_header: (closure user_data) (allow-none): callback that lets you add row headers
795
 * @user_data: user data passed to @update_header
Alexander Larsson's avatar
Alexander Larsson committed
796 797
 * @destroy: destroy notifier for @user_data
 *
798
 * By setting a header function on the @list_box one can dynamically add headers
Alexander Larsson's avatar
Alexander Larsson committed
799 800 801 802
 * 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.
 *
803
 * The @update_header can look at the current header widget using gtk_list_box_row_get_header()
Alexander Larsson's avatar
Alexander Larsson committed
804
 * and either update the state of the widget as needed, or set a new one using
805
 * gtk_list_box_row_set_header(). If no header is needed, set the header to %NULL.
Alexander Larsson's avatar
Alexander Larsson committed
806
 *
807 808 809
 * Note that you may get many calls @update_header to this for a particular row when e.g.
 * changing things that don't affect the header. In this case it is important for performance
 * to not blindly replace an exisiting header widh an identical one.
Alexander Larsson's avatar
Alexander Larsson committed
810
 *
811
 * The @update_header function will be called for each row after the call, and it will
Alexander Larsson's avatar
Alexander Larsson committed
812 813 814
 * 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
815
 * gtk_list_box_invalidate_headers() is called.
Alexander Larsson's avatar
Alexander Larsson committed
816 817
 *
 * Since: 3.10
Alexander Larsson's avatar
Alexander Larsson committed
818 819
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
820 821 822 823
gtk_list_box_set_header_func (GtkListBox                 *list_box,
                              GtkListBoxUpdateHeaderFunc  update_header,
                              gpointer                    user_data,
                              GDestroyNotify              destroy)
Alexander Larsson's avatar
Alexander Larsson committed
824
{
825 826 827
  GtkListBoxPrivate *priv;

  g_return_if_fail (GTK_IS_LIST_BOX (list_box));
Alexander Larsson's avatar
Alexander Larsson committed
828

829
  priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
830

831 832
  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
833

834 835 836
  priv->update_header_func = update_header;
  priv->update_header_func_target = user_data;
  priv->update_header_func_target_destroy_notify = destroy;
837
  gtk_list_box_invalidate_headers (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
838 839
}

Alexander Larsson's avatar
Alexander Larsson committed
840
/**
841
 * gtk_list_box_invalidate_filter:
Alexander Larsson's avatar
Alexander Larsson committed
842 843 844 845 846 847 848 849 850 851
 * @list_box: a #GtkListBox
 *
 * Update the filtering for all rows. Call this when result
 * of the filter function on the @list_box is changed due
 * 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
852
void
853
gtk_list_box_invalidate_filter (GtkListBox *list_box)
Alexander Larsson's avatar
Alexander Larsson committed
854
{
855
  g_return_if_fail (GTK_IS_LIST_BOX (list_box));
Alexander Larsson's avatar
Alexander Larsson committed
856

857
  gtk_list_box_apply_filter_all (list_box);
858
  gtk_list_box_invalidate_headers (list_box);
859
  gtk_widget_queue_resize (GTK_WIDGET (list_box));
Alexander Larsson's avatar
Alexander Larsson committed
860 861 862 863 864 865 866
}

static gint
do_sort (GtkListBoxRow *a,
         GtkListBoxRow *b,
         GtkListBox *list_box)
{
867
  GtkListBoxPrivate *priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
868 869 870 871

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

Alexander Larsson's avatar
Alexander Larsson committed
872
/**
873
 * gtk_list_box_invalidate_sort:
Alexander Larsson's avatar
Alexander Larsson committed
874 875 876 877 878 879 880 881
 * @list_box: a #GtkListBox
 *
 * Update the sorting for all rows. Call this when result
 * of the sort function on the @list_box is changed due
 * to an external factor.
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
882
void
883
gtk_list_box_invalidate_sort (GtkListBox *list_box)
Alexander Larsson's avatar
Alexander Larsson committed
884
{
885 886 887 888 889
  GtkListBoxPrivate *priv;

  g_return_if_fail (GTK_IS_LIST_BOX (list_box));

  priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
890

891
  g_sequence_sort (priv->children, (GCompareDataFunc)do_sort, list_box);
Alexander Larsson's avatar
Alexander Larsson committed
892

893
  gtk_list_box_invalidate_headers (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
894 895 896
  gtk_widget_queue_resize (GTK_WIDGET (list_box));
}

897 898 899
static void
gtk_list_box_do_reseparate (GtkListBox *list_box)
{
900
  GtkListBoxPrivate *priv = gtk_list_box_get_instance_private (list_box);
901 902 903 904 905 906 907 908 909 910 911
  GSequenceIter *iter;

  for (iter = g_sequence_get_begin_iter (priv->children);
       !g_sequence_iter_is_end (iter);
       iter = g_sequence_iter_next (iter))
    gtk_list_box_update_header (list_box, iter);

  gtk_widget_queue_resize (GTK_WIDGET (list_box));
}


Alexander Larsson's avatar
Alexander Larsson committed
912
/**
913
 * gtk_list_box_invalidate_headers:
Alexander Larsson's avatar
Alexander Larsson committed
914 915 916
 * @list_box: a #GtkListBox
 *
 * Update the separators for all rows. Call this when result
917
 * of the header function on the @list_box is changed due
Alexander Larsson's avatar
Alexander Larsson committed
918 919 920 921
 * to an external factor.
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
922
void
923
gtk_list_box_invalidate_headers (GtkListBox *list_box)
Alexander Larsson's avatar
Alexander Larsson committed
924
{
925
  g_return_if_fail (GTK_IS_LIST_BOX (list_box));
Alexander Larsson's avatar
Alexander Larsson committed
926

927 928
  if (!gtk_widget_get_visible (GTK_WIDGET (list_box)))
    return;
Alexander Larsson's avatar
Alexander Larsson committed
929

930
  gtk_list_box_do_reseparate (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
931 932 933 934
}

/**
 * gtk_list_box_set_sort_func:
Alexander Larsson's avatar
Alexander Larsson committed
935
 * @list_box: a #GtkListBox
Matthias Clasen's avatar
Matthias Clasen committed
936
 * @sort_func: (closure user_data) (allow-none): the sort function
Alexander Larsson's avatar
Alexander Larsson committed
937 938 939 940 941 942 943 944
 * @user_data: user data passed to @sort_func
 * @destroy: destroy notifier for @user_data
 *
 * By setting a sort function on the @list_box one can dynamically reorder the rows
 * 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
945
 * gtk_list_box_invalidate_sort() is called.
Alexander Larsson's avatar
Alexander Larsson committed
946 947
 *
 * Since: 3.10
Alexander Larsson's avatar
Alexander Larsson committed
948 949
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
950 951 952 953
gtk_list_box_set_sort_func (GtkListBox         *list_box,
                            GtkListBoxSortFunc  sort_func,
                            gpointer            user_data,
                            GDestroyNotify      destroy)
Alexander Larsson's avatar
Alexander Larsson committed
954
{
955 956 957
  GtkListBoxPrivate *priv;

  g_return_if_fail (GTK_IS_LIST_BOX (list_box));
Alexander Larsson's avatar
Alexander Larsson committed
958

959
  priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
960 961 962 963 964 965 966

  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;
967

968
  gtk_list_box_invalidate_sort (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
969 970 971
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
972 973
gtk_list_box_got_row_changed (GtkListBox    *list_box,
                              GtkListBoxRow *row)
Alexander Larsson's avatar
Alexander Larsson committed
974
{
975 976
  GtkListBoxPrivate *priv;
  GtkListBoxRowPrivate *row_priv;
Alexander Larsson's avatar
Alexander Larsson committed
977 978
  GSequenceIter *prev_next, *next;

979 980 981 982 983
  g_return_if_fail (GTK_IS_LIST_BOX (list_box));
  g_return_if_fail (GTK_IS_LIST_BOX_ROW (row));

  priv = gtk_list_box_get_instance_private (list_box);
  row_priv = gtk_list_box_row_get_instance_private (row);
Alexander Larsson's avatar
Alexander Larsson committed
984

985
  prev_next = gtk_list_box_get_next_visible (list_box, row_priv->iter);
Alexander Larsson's avatar
Alexander Larsson committed
986 987
  if (priv->sort_func != NULL)
    {
988
      g_sequence_sort_changed (row_priv->iter,
Alexander Larsson's avatar
Alexander Larsson committed
989 990 991 992 993 994 995
                               (GCompareDataFunc)do_sort,
                               list_box);
      gtk_widget_queue_resize (GTK_WIDGET (list_box));
    }
  gtk_list_box_apply_filter (list_box, row);
  if (gtk_widget_get_visible (GTK_WIDGET (list_box)))
    {
996 997
      next = gtk_list_box_get_next_visible (list_box, row_priv->iter);
      gtk_list_box_update_header (list_box, row_priv->iter);
998 999
      gtk_list_box_update_header (list_box, next);
      gtk_list_box_update_header (list_box, prev_next);
Alexander Larsson's avatar
Alexander Larsson committed
1000 1001 1002
    }
}

Alexander Larsson's avatar
Alexander Larsson committed
1003 1004 1005 1006 1007
/**
 * gtk_list_box_set_activate_on_single_click:
 * @list_box: a #GtkListBox
 * @single: a boolean
 *
Matthias Clasen's avatar
Matthias Clasen committed
1008 1009
 * If @single is %TRUE, rows will be activated when you click on them,
 * otherwise you need to double-click.
Alexander Larsson's avatar
Alexander Larsson committed
1010 1011 1012
 *
 * Since: 3.10
 */
Alexander Larsson's avatar
Alexander Larsson committed
1013 1014
void
gtk_list_box_set_activate_on_single_click (GtkListBox *list_box,
Matthias Clasen's avatar
Matthias Clasen committed
1015
                                           gboolean    single)
Alexander Larsson's avatar
Alexander Larsson committed
1016
{
1017 1018 1019
  GtkListBoxPrivate *priv;

  g_return_if_fail (GTK_IS_LIST_BOX (list_box));
Alexander Larsson's avatar
Alexander Larsson committed
1020

1021
  priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032

  single = single != FALSE;

  if (priv->activate_single_click == single)
    return;

  priv->activate_single_click = single;

  g_object_notify_by_pspec (G_OBJECT (list_box), properties[PROP_ACTIVATE_ON_SINGLE_CLICK]);
}

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
/**
 * gtk_list_box_get_activate_on_single_click:
 * @list_box: a #GtkListBox
 *
 * Returns whether rows activate on single clicks.
 *
 * Returns: %TRUE if rows are activated on single click, %FALSE otherwise
 *
 * Since: 3.10
 */
gboolean
gtk_list_box_get_activate_on_single_click (GtkListBox *list_box)
{
1046 1047 1048
  GtkListBoxPrivate *priv;

  g_return_val_if_fail (GTK_IS_LIST_BOX (list_box), FALSE);
1049

1050
  priv = gtk_list_box_get_instance_private (list_box);
1051 1052 1053 1054 1055

  return priv->activate_single_click;
}


Alexander Larsson's avatar
Alexander Larsson committed
1056
static void
Matthias Clasen's avatar
Matthias Clasen committed
1057 1058 1059 1060 1061
gtk_list_box_add_move_binding (GtkBindingSet   *binding_set,
                               guint            keyval,
                               GdkModifierType  modmask,
                               GtkMovementStep  step,
                               gint             count)
Alexander Larsson's avatar
Alexander Larsson committed
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
{
  gtk_binding_entry_add_signal (binding_set, keyval, modmask,
                                "move-cursor", (guint) 2, GTK_TYPE_MOVEMENT_STEP, step, G_TYPE_INT, count, NULL);

  if ((modmask & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
    return;

  gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK,
                                "move-cursor", (guint) 2, GTK_TYPE_MOVEMENT_STEP, step, G_TYPE_INT, count, NULL);
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
1074
gtk_list_box_update_cursor (GtkListBox    *list_box,
Alexander Larsson's avatar
Alexander Larsson committed
1075 1076
                            GtkListBoxRow *row)
{
1077
  GtkListBoxPrivate *priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
1078 1079 1080 1081 1082 1083 1084 1085

  priv->cursor_row = row;
  gtk_widget_grab_focus (GTK_WIDGET (row));
  gtk_widget_queue_draw (GTK_WIDGET (row));
  _gtk_list_box_accessible_update_cursor (list_box, row);
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
1086
gtk_list_box_update_selected (GtkListBox    *list_box,
Alexander Larsson's avatar
Alexander Larsson committed
1087 1088
                              GtkListBoxRow *row)
{
1089
  GtkListBoxPrivate *priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111

  if (row != priv->selected_row &&
      (row == NULL || priv->selection_mode != GTK_SELECTION_NONE))
    {
      if (priv->selected_row)
        gtk_widget_unset_state_flags (GTK_WIDGET (priv->selected_row),
                                      GTK_STATE_FLAG_SELECTED);
      priv->selected_row = row;
      if (priv->selected_row)
        gtk_widget_set_state_flags (GTK_WIDGET (priv->selected_row),
                                    GTK_STATE_FLAG_SELECTED,
                                    FALSE);
      g_signal_emit (list_box, signals[ROW_SELECTED], 0,
                     priv->selected_row);
      gtk_widget_queue_draw (GTK_WIDGET (list_box));
    }
  _gtk_list_box_accessible_selection_changed (list_box);
  if (row != NULL)
    gtk_list_box_update_cursor (list_box, row);
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
1112 1113
gtk_list_box_select_and_activate (GtkListBox    *list_box,
                                  GtkListBoxRow *row)
Alexander Larsson's avatar
Alexander Larsson committed
1114 1115 1116 1117 1118 1119 1120 1121
{
  gtk_list_box_update_selected (list_box, row);

  if (row != NULL)
    g_signal_emit (list_box, signals[ROW_ACTIVATED], 0, row);
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
1122 1123
gtk_list_box_update_prelight (GtkListBox    *list_box,
                              GtkListBoxRow *row)
Alexander Larsson's avatar
Alexander Larsson committed
1124
{
1125
  GtkListBoxPrivate *priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
1126 1127 1128 1129 1130 1131

  if (row != priv->prelight_row)
    {
      if (priv->prelight_row)
        gtk_widget_unset_state_flags (GTK_WIDGET (priv->prelight_row),
                                      GTK_STATE_FLAG_PRELIGHT);
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144

      if (row != NULL && gtk_widget_is_sensitive (GTK_WIDGET (row)))
        {
          priv->prelight_row = row;
          gtk_widget_set_state_flags (GTK_WIDGET (priv->prelight_row),
                                      GTK_STATE_FLAG_PRELIGHT,
                                      FALSE);
        }
      else
        {
          priv->prelight_row = NULL;
        }

Alexander Larsson's avatar
Alexander Larsson committed
1145 1146 1147 1148 1149
      gtk_widget_queue_draw (GTK_WIDGET (list_box));
    }
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
1150 1151
gtk_list_box_update_active (GtkListBox    *list_box,
                            GtkListBoxRow *row)
Alexander Larsson's avatar
Alexander Larsson committed
1152
{
1153
  GtkListBoxPrivate *priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
  gboolean val;

  val = priv->active_row == row;
  if (priv->active_row != NULL &&
      val != priv->active_row_active)
    {
      priv->active_row_active = val;
      if (priv->active_row_active)
        gtk_widget_set_state_flags (GTK_WIDGET (priv->active_row),
                                    GTK_STATE_FLAG_ACTIVE,
                                    FALSE);
      else
        gtk_widget_unset_state_flags (GTK_WIDGET (priv->active_row),
                                      GTK_STATE_FLAG_ACTIVE);
      gtk_widget_queue_draw (GTK_WIDGET (list_box));
    }
}

static gboolean
1173 1174
gtk_list_box_enter_notify_event (GtkWidget        *widget,
                                 GdkEventCrossing *event)
Alexander Larsson's avatar
Alexander Larsson committed
1175 1176
{
  GtkListBox *list_box = GTK_LIST_BOX (widget);
1177
  GtkListBoxPrivate *priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
1178 1179 1180 1181 1182
  GtkListBoxRow *row;

  if (event->window != gtk_widget_get_window (GTK_WIDGET (list_box)))
    return FALSE;

1183
  priv->in_widget = TRUE;
1184

Alexander Larsson's avatar
Alexander Larsson committed
1185 1186 1187 1188 1189 1190 1191 1192
  row = gtk_list_box_get_row_at_y (list_box, event->y);
  gtk_list_box_update_prelight (list_box, row);
  gtk_list_box_update_active (list_box, row);

  return FALSE;
}

static gboolean
1193 1194
gtk_list_box_leave_notify_event (GtkWidget        *widget,
                                 GdkEventCrossing *event)
Alexander Larsson's avatar
Alexander Larsson committed
1195 1196
{
  GtkListBox *list_box = GTK_LIST_BOX (widget);
1197
  GtkListBoxPrivate *priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
1198 1199 1200 1201 1202 1203
  GtkListBoxRow *row = NULL;

  if (event->window != gtk_widget_get_window (GTK_WIDGET (list_box)))
    return FALSE;

  if (event->detail != GDK_NOTIFY_INFERIOR)
1204
    {
1205
      priv->in_widget = FALSE;
1206 1207
      row = NULL;
    }
Alexander Larsson's avatar
Alexander Larsson committed
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
  else
    row = gtk_list_box_get_row_at_y (list_box, event->y);

  gtk_list_box_update_prelight (list_box, row);
  gtk_list_box_update_active (list_box, row);

  return FALSE;
}

static gboolean
1218 1219
gtk_list_box_motion_notify_event (GtkWidget      *widget,
                                  GdkEventMotion *event)
Alexander Larsson's avatar
Alexander Larsson committed
1220 1221
{
  GtkListBox *list_box = GTK_LIST_BOX (widget);
1222
  GtkListBoxPrivate *priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
1223 1224 1225 1226 1227
  GtkListBoxRow *row;
  GdkWindow *window, *event_window;
  gint relative_y;
  gdouble parent_y;

1228
  if (!priv->in_widget)
1229 1230
    return FALSE;

Alexander Larsson's avatar
Alexander Larsson committed
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
  window = gtk_widget_get_window (GTK_WIDGET (list_box));
  event_window = event->window;
  relative_y = event->y;

  while ((event_window != NULL) && (event_window != window))
    {
      gdk_window_coords_to_parent (event_window, 0, relative_y, NULL, &parent_y);
      relative_y = parent_y;
      event_window = gdk_window_get_effective_parent (event_window);
    }

  row = gtk_list_box_get_row_at_y (list_box, relative_y);
  gtk_list_box_update_prelight (list_box, row);
  gtk_list_box_update_active (list_box, row);

  return FALSE;
}

static gboolean
1250 1251
gtk_list_box_button_press_event (GtkWidget      *widget,
                                 GdkEventButton *event)
Alexander Larsson's avatar
Alexander Larsson committed
1252 1253
{
  GtkListBox *list_box = GTK_LIST_BOX (widget);
1254
  GtkListBoxPrivate *priv = gtk_list_box_get_instance_private (list_box);
1255
  gboolean ctrl_pressed;
1256 1257
  GdkWindow *window;
  double x, y;
Alexander Larsson's avatar
Alexander Larsson committed
1258 1259 1260 1261

  if (event->button == GDK_BUTTON_PRIMARY)
    {
      GtkListBoxRow *row;
1262 1263 1264 1265 1266

      ctrl_pressed = (event->state & GDK_CONTROL_MASK) != 0;

      priv->active_row = NULL;

1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
      window = event->window;
      x = event->x;
      y = event->y;

      while (window && window != gtk_widget_get_window (widget))
        {
          gdk_window_coords_to_parent (window, x, y, &x, &y);
          window = gdk_window_get_effective_parent (window);
        }

      row = gtk_list_box_get_row_at_y (list_box, y);
1278
      if (row != NULL && gtk_widget_is_sensitive (GTK_WIDGET (row)))
Alexander Larsson's avatar
Alexander Larsson committed
1279
        {
1280 1281 1282 1283 1284 1285 1286
          if (event->type == GDK_2BUTTON_PRESS)
            {
              if (!priv->activate_single_click)
                g_signal_emit (list_box, signals[ROW_ACTIVATED], 0,
                               row);
            }
          else if (ctrl_pressed)
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
            {
              if (priv->selection_mode == GTK_SELECTION_SINGLE &&
                  priv->selected_row == row)
                gtk_list_box_update_selected (list_box, NULL);
              else
                gtk_list_box_update_selected (list_box, row);
            }
          else
            {
              priv->active_row = row;
              priv->active_row_active = TRUE;
              gtk_widget_set_state_flags (GTK_WIDGET (priv->active_row),
                                          GTK_STATE_FLAG_ACTIVE,
                                          FALSE);
              gtk_widget_queue_draw (GTK_WIDGET (list_box));
            }
Alexander Larsson's avatar
Alexander Larsson committed
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313

        }
      /* TODO:
         Should mark as active while down,
         and handle grab breaks */
    }

  return FALSE;
}

static gboolean
1314 1315
gtk_list_box_button_release_event (GtkWidget      *widget,
                                   GdkEventButton *event)
Alexander Larsson's avatar
Alexander Larsson committed
1316 1317
{
  GtkListBox *list_box = GTK_LIST_BOX (widget);
1318
  GtkListBoxPrivate *priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
1319

1320 1321 1322
  /* Take a ref to protect against reentrancy (i.e. the activation may destroy the widget) */
  g_object_ref (list_box);

Alexander Larsson's avatar
Alexander Larsson committed
1323 1324 1325 1326 1327
  if (event->button == GDK_BUTTON_PRIMARY)
    {
      if (priv->active_row != NULL &&
          priv->active_row_active)
        {
1328 1329 1330
          gtk_widget_unset_state_flags (GTK_WIDGET (priv->active_row),
                                        GTK_STATE_FLAG_ACTIVE);

Alexander Larsson's avatar
Alexander Larsson committed
1331 1332 1333 1334
          if (priv->activate_single_click)
            gtk_list_box_select_and_activate (list_box, priv->active_row);
          else
            gtk_list_box_update_selected (list_box, priv->active_row);
Alexander Larsson's avatar
Alexander Larsson committed
1335

Alexander Larsson's avatar
Alexander Larsson committed
1336 1337 1338 1339 1340 1341
        }
      priv->active_row = NULL;
      priv->active_row_active = FALSE;
      gtk_widget_queue_draw (GTK_WIDGET (list_box));
  }

1342 1343
  g_object_unref (list_box);

Alexander Larsson's avatar
Alexander Larsson committed
1344 1345 1346 1347
  return FALSE;
}

static void
1348
gtk_list_box_show (GtkWidget *widget)
Alexander Larsson's avatar
Alexander Larsson committed
1349
{
1350
  GtkListBox *list_box = GTK_LIST_BOX (widget);
Alexander Larsson's avatar
Alexander Larsson committed
1351

1352
  gtk_list_box_do_reseparate (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
1353 1354 1355 1356

  GTK_WIDGET_CLASS (gtk_list_box_parent_class)->show (widget);
}

1357 1358
#define ROW_PRIV(_row) ((GtkListBoxRowPrivate *)gtk_list_box_row_get_instance_private (_row))

Alexander Larsson's avatar
Alexander Larsson committed
1359
static gboolean
1360 1361
gtk_list_box_focus (GtkWidget        *widget,
                    GtkDirectionType  direction)
Alexander Larsson's avatar
Alexander Larsson committed
1362 1363
{
  GtkListBox *list_box = GTK_LIST_BOX (widget);
1364
  GtkListBoxPrivate *priv = gtk_list_box_get_instance_private (list_box);
Alexander Larsson's avatar
Alexander Larsson committed
1365 1366 1367 1368 1369 1370 1371
  GtkWidget *focus_child;
  GtkListBoxRow *next_focus_row;

  focus_child = gtk_container_get_focus_child ((GtkContainer*) list_box);
  next_focus_row = NULL;
  if (focus_child != NULL)
    {
Matthias Clasen's avatar
Matthias Clasen committed
1372
      GSequenceIter *i;
Alexander Larsson's avatar
Alexander Larsson committed
1373 1374 1375 1376 1377 1378

      if (gtk_widget_child_focus (focus_child, direction))
        return TRUE;

      if (direction == GTK_DIR_UP || direction == GTK_DIR_TAB_BACKWARD)
        {
1379
          i = gtk_list_box_get_previous_visible (list_box, ROW_PRIV (GTK_LIST_BOX_ROW (focus_child))->iter);