Gtk.SelectionModel

g GObject.GInterface GObject.GInterface Gtk.SelectionModel Gtk.SelectionModel GObject.GInterface->Gtk.SelectionModel

Implementations:

Gtk.MultiSelection, Gtk.NoSelection, Gtk.SingleSelection

Methods

get_selection ()

get_selection_in_range (position, n_items)

is_selected (position)

select_all ()

select_item (position, unselect_rest)

select_range (position, n_items, unselect_rest)

selection_changed (position, n_items)

set_selection (selected, mask)

unselect_all ()

unselect_item (position)

unselect_range (position, n_items)

Virtual Methods

do_get_selection_in_range (position, n_items)

do_is_selected (position)

do_select_all ()

do_select_item (position, unselect_rest)

do_select_range (position, n_items, unselect_rest)

do_set_selection (selected, mask)

do_unselect_all ()

do_unselect_item (position)

do_unselect_range (position, n_items)

Properties

None

Signals

Name

Short Description

selection-changed

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

Fields

None

Class Details

class Gtk.SelectionModel
Bases:

GObject.GInterface

Structure:

Gtk.SelectionModelInterface

GtkSelectionModel is an interface that add support for selection to list models.

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 most common selection modes such as [class`Gtk`.SingleSelection], 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 item indicating if an item is selected or not. This can be queried via [method`Gtk`.SelectionModel.is_selected]. When the selected state of one or more items changes, the model will emit the [signal`Gtk`.SelectionModel::selection-changed] signal by calling the [method`Gtk`.SelectionModel.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 [signal`Gio`.ListModel::items-changed] signal are selected or not is up to the implementation.

Note that items added via [signal`Gio`.ListModel::items-changed] may already be selected and no [signal`Gtk`.SelectionModel::selection-changed] will be emitted for them. So to track which items are selected, it is necessary to listen to both signals.

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, ``GtkSelectionModel``s 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.

get_selection()[source]
Returns:

a GtkBitset containing all the values currently selected in self. If no items are selected, the bitset is empty. The bitset must not be modified.

Return type:

Gtk.Bitset

Gets the set containing all currently selected items in the model.

This function may be slow, so if you are only interested in single item, consider using [method`Gtk`.SelectionModel.is_selected] or if you are only interested in a few, consider [method`Gtk`.SelectionModel.get_selection_in_range].

get_selection_in_range(position, n_items)[source]
Parameters:
  • position (int) – start of the queried range

  • n_items (int) – number of items in the queried range

Returns:

A GtkBitset that matches the selection state for the given range with all other values being undefined. The bitset must not be modified.

Return type:

Gtk.Bitset

Gets the set of selected items in a range.

This function is an optimization for [method`Gtk`.SelectionModel.get_selection] when you are only interested in part of the model’s selected state. A common use case is in response to the [signal`Gtk`.SelectionModel::selection-changed] signal.

is_selected(position)[source]
Parameters:

position (int) – the position of the item to query

Returns:

True if the item is selected

Return type:

bool

Checks if the given item is selected.

select_all()[source]
Returns:

True if this action was supported and no fallback should be tried. This does not mean that all items are now selected.

Return type:

bool

Requests to select all items in the model.

select_item(position, unselect_rest)[source]
Parameters:
  • position (int) – the position of the item to select

  • unselect_rest (bool) – whether previously selected items should be unselected

Returns:

True if this action was supported and no fallback should be tried. This does not mean the item was selected.

Return type:

bool

Requests to select an item in the model.

select_range(position, n_items, unselect_rest)[source]
Parameters:
  • position (int) – the first item to select

  • n_items (int) – the number of items to select

  • unselect_rest (bool) – whether previously selected items should be unselected

Returns:

True if this action was supported and no fallback should be tried. This does not mean the range was selected.

Return type:

bool

Requests to select a range of items in the model.

selection_changed(position, n_items)[source]
Parameters:
  • position (int) – the first changed item

  • n_items (int) – the number of changed items

Helper function for implementations of GtkSelectionModel.

Call this when the selection changes to emit the [signal`Gtk`.SelectionModel::selection-changed] signal.

set_selection(selected, mask)[source]
Parameters:
  • selected (Gtk.Bitset) – bitmask specifying if items should be selected or unselected

  • mask (Gtk.Bitset) – bitmask specifying which items should be updated

Returns:

True if this action was supported and no fallback should be tried. This does not mean that all items were updated according to the inputs.

Return type:

bool

Make selection changes.

This is the most advanced selection updating method that allows the most fine-grained control over selection changes. If you can, you should try the simpler versions, as implementations are more likely to implement support for those.

Requests that the selection state of all positions set in mask be updated to the respective value in the selected bitmask.

In pseudocode, it would look something like this:

```c for (i = 0; i < n_items; i++) { // don’t change values not in the mask if (!:obj:Gtk.Bitset.contains (mask, i)) continue;

if (Gtk.Bitset.contains (selected, i)) select_item (i); else unselect_item (i); }

Gtk.SelectionModel.selection_changed (model, first_changed_item, n_changed_items); ```

mask and selected must not be modified. They may refer to the same bitset, which would mean that every item in the set should be selected.

unselect_all()[source]
Returns:

True if this action was supported and no fallback should be tried. This does not mean that all items are now unselected.

Return type:

bool

Requests to unselect all items in the model.

unselect_item(position)[source]
Parameters:

position (int) – the position of the item to unselect

Returns:

True if this action was supported and no fallback should be tried. This does not mean the item was unselected.

Return type:

bool

Requests to unselect an item in the model.

unselect_range(position, n_items)[source]
Parameters:
  • position (int) – the first item to unselect

  • n_items (int) – the number of items to unselect

Returns:

True if this action was supported and no fallback should be tried. This does not mean the range was unselected.

Return type:

bool

Requests to unselect a range of items in the model.

do_get_selection_in_range(position, n_items) virtual
Parameters:
  • position (int) – start of the queried range

  • n_items (int) – number of items in the queried range

Returns:

A GtkBitset that matches the selection state for the given range with all other values being undefined. The bitset must not be modified.

Return type:

Gtk.Bitset

Gets the set of selected items in a range.

This function is an optimization for [method`Gtk`.SelectionModel.get_selection] when you are only interested in part of the model’s selected state. A common use case is in response to the [signal`Gtk`.SelectionModel::selection-changed] signal.

do_is_selected(position) virtual
Parameters:

position (int) – the position of the item to query

Returns:

True if the item is selected

Return type:

bool

Checks if the given item is selected.

do_select_all() virtual
Returns:

True if this action was supported and no fallback should be tried. This does not mean that all items are now selected.

Return type:

bool

Requests to select all items in the model.

do_select_item(position, unselect_rest) virtual
Parameters:
  • position (int) – the position of the item to select

  • unselect_rest (bool) – whether previously selected items should be unselected

Returns:

True if this action was supported and no fallback should be tried. This does not mean the item was selected.

Return type:

bool

Requests to select an item in the model.

do_select_range(position, n_items, unselect_rest) virtual
Parameters:
  • position (int) – the first item to select

  • n_items (int) – the number of items to select

  • unselect_rest (bool) – whether previously selected items should be unselected

Returns:

True if this action was supported and no fallback should be tried. This does not mean the range was selected.

Return type:

bool

Requests to select a range of items in the model.

do_set_selection(selected, mask) virtual
Parameters:
  • selected (Gtk.Bitset) – bitmask specifying if items should be selected or unselected

  • mask (Gtk.Bitset) – bitmask specifying which items should be updated

Returns:

True if this action was supported and no fallback should be tried. This does not mean that all items were updated according to the inputs.

Return type:

bool

Make selection changes.

This is the most advanced selection updating method that allows the most fine-grained control over selection changes. If you can, you should try the simpler versions, as implementations are more likely to implement support for those.

Requests that the selection state of all positions set in mask be updated to the respective value in the selected bitmask.

In pseudocode, it would look something like this:

```c for (i = 0; i < n_items; i++) { // don’t change values not in the mask if (!:obj:Gtk.Bitset.contains (mask, i)) continue;

if (Gtk.Bitset.contains (selected, i)) select_item (i); else unselect_item (i); }

Gtk.SelectionModel.selection_changed (model, first_changed_item, n_changed_items); ```

mask and selected must not be modified. They may refer to the same bitset, which would mean that every item in the set should be selected.

do_unselect_all() virtual
Returns:

True if this action was supported and no fallback should be tried. This does not mean that all items are now unselected.

Return type:

bool

Requests to unselect all items in the model.

do_unselect_item(position) virtual
Parameters:

position (int) – the position of the item to unselect

Returns:

True if this action was supported and no fallback should be tried. This does not mean the item was unselected.

Return type:

bool

Requests to unselect an item in the model.

do_unselect_range(position, n_items) virtual
Parameters:
  • position (int) – the first item to unselect

  • n_items (int) – the number of items to unselect

Returns:

True if this action was supported and no fallback should be tried. This does not mean the range was unselected.

Return type:

bool

Requests to unselect a range of items in the model.

Signal Details

Gtk.SelectionModel.signals.selection_changed(selection_model, position, n_items)
Signal Name:

selection-changed

Flags:

RUN_LAST

Parameters:
  • selection_model (Gtk.SelectionModel) – The object which received the signal

  • position (int) – The first item that may have changed

  • n_items (int) – number of items with changes

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.