GtkSelectionModel

GtkSelectionModel — An extension of the list model interface that handles selections

Functions

Signals

void selection-changed Run Last

Types and Values

Object Hierarchy

    GInterface
    ╰── GtkSelectionModel

Prerequisites

GtkSelectionModel requires GListModel and GObject.

Known Implementations

GtkSelectionModel is implemented by GtkSingleSelection.

Includes

#include <gtk/gtk.h>

Description

GtkSelectionModel is an interface that extends the GListModel interface by adding support for selections. This support is then used by widgets using list models to add the ability to select and unselect various items.

GTK provides default implementations of the mode common selection modes such as GtkSingleSelection, so you will only need to implement this interface if you want detailed control about how selections should be handled.

A GtkSelectionModel supports a single boolean per row indicating if a row is selected or not. This can be queried via gtk_selection_model_is_selected(). When the selected state of one or more rows changes, the model will emit the “selection-changed” signal by calling the gtk_selection_model_selection_changed() function. The positions given in that signal may have their selection state changed, though that is not a requirement. If new items added to the model via the “items-changed” signal are selected or not is up to the implementation.

Additionally, the interface can expose functionality to select and unselect items. If these functions are implemented, GTK's list widgets will allow users to select and unselect items. However, GtkSelectionModels are free to only implement them partially or not at all. In that case the widgets will not support the unimplemented operations.

When selecting or unselecting is supported by a model, the return values of the selection functions do NOT indicate if selection or unselection happened. They are only meant to indicate complete failure, like when this mode of selecting is not supported by the model.

Selections may happen asynchronously, so the only reliable way to find out when an item was selected is to listen to the signals that indicate selection.

Functions

gtk_selection_model_is_selected ()

gboolean
gtk_selection_model_is_selected (GtkSelectionModel *model,
                                 guint position);

Checks if the given item is selected.

Parameters

model

a GtkSelectionModel

 

position

the position of the item to query

 

Returns

TRUE if the item is selected


gtk_selection_model_select_item ()

gboolean
gtk_selection_model_select_item (GtkSelectionModel *model,
                                 guint position,
                                 gboolean exclusive);

Requests to select an item in the model.

Parameters

model

a GtkSelectionModel

 

position

the position of the item to select

 

exclusive

whether previously selected items should be unselected

 

gtk_selection_model_unselect_item ()

gboolean
gtk_selection_model_unselect_item (GtkSelectionModel *model,
                                   guint position);

Requests to unselect an item in the model.

Parameters

model

a GtkSelectionModel

 

position

the position of the item to unselect

 

gtk_selection_model_select_range ()

gboolean
gtk_selection_model_select_range (GtkSelectionModel *model,
                                  guint position,
                                  guint n_items,
                                  gboolean exclusive);

Requests to select a range of items in the model.

Parameters

model

a GtkSelectionModel

 

position

the first item to select

 

n_items

the number of items to select

 

exclusive

whether previously selected items should be unselected

 

gtk_selection_model_unselect_range ()

gboolean
gtk_selection_model_unselect_range (GtkSelectionModel *model,
                                    guint position,
                                    guint n_items);

Requests to unselect a range of items in the model.

Parameters

model

a GtkSelectionModel

 

position

the first item to unselect

 

n_items

the number of items to unselect

 

gtk_selection_model_select_all ()

gboolean
gtk_selection_model_select_all (GtkSelectionModel *model);

Requests to select all items in the model.

Parameters

model

a GtkSelectionModel

 

gtk_selection_model_unselect_all ()

gboolean
gtk_selection_model_unselect_all (GtkSelectionModel *model);

Requests to unselect all items in the model.

Parameters

model

a GtkSelectionModel

 

gtk_selection_model_query_range ()

void
gtk_selection_model_query_range (GtkSelectionModel *model,
                                 guint position,
                                 guint *start_range,
                                 guint *n_items,
                                 gboolean *selected);

This function allows to query the selection status of multiple elements at once. It is passed a position and returns a range of elements of uniform selection status.

If position is greater than the number of items in model , n_items is set to 0. Otherwise the returned range is guaranteed to include the passed-in position, so n_items will be >= 1.

Positions directly adjacent to the returned range may have the same selection status as the returned range.

This is an optimization function to make iterating over a model faster when few items are selected. However, it is valid behavior for implementations to use a naive implementation that only ever returns a single element.

Parameters

model

a GtkSelectionModel

 

position

the position inside the range

 

start_range

returns the position of the first element of the range.

[out]

n_items

returns the size of the range.

[out]

selected

returns whether items in range are selected.

[out]

gtk_selection_model_selection_changed ()

void
gtk_selection_model_selection_changed (GtkSelectionModel *model,
                                       guint position,
                                       guint n_items);

Helper function for implementations of GtkSelectionModel. Call this when a the selection changes to emit the ::selection-changed signal.

Parameters

model

a GtkSelectionModel

 

position

the first changed item

 

n_items

the number of changed items

 

Types and Values

GtkSelectionModel

typedef struct _GtkSelectionModel GtkSelectionModel;

Signal Details

The “selection-changed” signal

void
user_function (GtkSelectionModel *model,
               guint              position,
               guint              n_items,
               gpointer           user_data)

Emitted when the selection state of some of the items in model changes.

Note that this signal does not specify the new selection state of the items, they need to be queried manually. It is also not necessary for a model to change the selection state of any of the items in the selection model, though it would be rather useless to emit such a signal.

Parameters

model

a GtkSelectionModel

 

position

The first item that may have changed

 

n_items

number of items with changes

 

user_data

user data set when the signal handler was connected.

 

Flags: Run Last

See Also

GListModel, GtkSingleSelection