2.16 <GtkSelectionModel>

Interface: <GtkSelectionModel>

An interface that adds 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 <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 item indicating if an item is selected or not. This can be queried via selection-model:is-selected?. When the selected state of one or more items changes, the model will emit the selection-model:selection-changed signal by calling the 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 list-model:items-changed signal are selected or not is up to the implementation.

Note that items added via list-model:items-changed may already be selected and no selection-model: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.

Procedures

Method: selection-model:get-selection self ⇒ %return
Method: get-selection self ⇒ %return

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 selection-model:is-selected? or if you are only interested in a few, consider selection-model:get-selection-in-range.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

%return

Returned: a <GtkBitset>.

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.

Method: selection-model:get-selection-in-range self position n-items ⇒ %return
Method: get-selection-in-range self position n-items ⇒ %return

Gets the set of selected items in a range.

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

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

position

Passed: an unsigned 32-bit integer.

start of the queried range

n-items

Passed: an unsigned 32-bit integer.

number of items in the queried range

%return

Returned: a <GtkBitset>.

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

Method: selection-model:is-selected? self position ⇒ %return
Method: is-selected? self position ⇒ %return

Checks if the given item is selected.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

position

Passed: an unsigned 32-bit integer.

the position of the item to query

%return

Returned: a boolean.

#t if the item is selected

Method: selection-model:select-all? self ⇒ %return
Method: select-all? self ⇒ %return

Requests to select all items in the model.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

%return

Returned: a boolean.

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

Method: selection-model:select-item? self position unselect-rest ⇒ %return
Method: select-item? self position unselect-rest ⇒ %return

Requests to select an item in the model.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

position

Passed: an unsigned 32-bit integer.

the position of the item to select

unselect-rest

Passed: a boolean.

whether previously selected items should be unselected

%return

Returned: a boolean.

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

Method: selection-model:select-range? self position n-items unselect-rest ⇒ %return
Method: select-range? self position n-items unselect-rest ⇒ %return

Requests to select a range of items in the model.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

position

Passed: an unsigned 32-bit integer.

the first item to select

n-items

Passed: an unsigned 32-bit integer.

the number of items to select

unselect-rest

Passed: a boolean.

whether previously selected items should be unselected

%return

Returned: a boolean.

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

Method: selection-model:selection-changed self position n-items
Method: selection-changed self position n-items

Helper function for implementations of <GtkSelectionModel>.

Call this when the selection changes to emit the selection-model:selection-changed signal.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

position

Passed: an unsigned 32-bit integer.

the first changed item

n-items

Passed: an unsigned 32-bit integer.

the number of changed items

Method: selection-model:set-selection? self selected mask ⇒ %return
Method: set-selection? self selected mask ⇒ %return

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:

for (i = 0; i < n_items; i++)
  {
    // don't change values not in the mask
    if (!gtk_bitset_contains (mask, i))
      continue;

    if (gtk_bitset_contains (selected, i))
      select_item (i);
    else
      unselect_item (i);
  }

gtk_selection_model_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.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

selected

Passed: a <GtkBitset>.

bitmask specifying if items should be selected or unselected

mask

Passed: a <GtkBitset>.

bitmask specifying which items should be updated

%return

Returned: a boolean.

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

Method: selection-model:unselect-all? self ⇒ %return
Method: unselect-all? self ⇒ %return

Requests to unselect all items in the model.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

%return

Returned: a boolean.

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

Method: selection-model:unselect-item? self position ⇒ %return
Method: unselect-item? self position ⇒ %return

Requests to unselect an item in the model.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

position

Passed: an unsigned 32-bit integer.

the position of the item to unselect

%return

Returned: a boolean.

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

Method: selection-model:unselect-range? self position n-items ⇒ %return
Method: unselect-range? self position n-items ⇒ %return

Requests to unselect a range of items in the model.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

position

Passed: an unsigned 32-bit integer.

the first item to unselect

n-items

Passed: an unsigned 32-bit integer.

the number of items to unselect

%return

Returned: a boolean.

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

Signals

Signal: selection-model:selection-changed

Connect a handler with (connect object selection-model:selection-changed handler). It is called as (handler self position n-items).

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.

self

Passed: a <GtkSelectionModel>.

position

Passed: an unsigned 32-bit integer.

The first item that may have changed

n-items

Passed: an unsigned 32-bit integer.

number of items with changes

Virtual methods

Virtual method: selection-model:do-get-selection-in-range implementor self position n-items ⇒ %return

Gets the set of selected items in a range.

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

To override it in a new class, give register-type #:vfuncs `((get-selection-in-range . ,procedure)). The procedure is called as (procedure self position n-items) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GtkSelectionModel>, or its GType number.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

position

Passed: an unsigned 32-bit integer.

start of the queried range

n-items

Passed: an unsigned 32-bit integer.

number of items in the queried range

%return

Returned: a <GtkBitset>.

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

Virtual method: selection-model:do-is-selected implementor self position ⇒ %return

Checks if the given item is selected.

To override it in a new class, give register-type #:vfuncs `((is-selected . ,procedure)). The procedure is called as (procedure self position) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GtkSelectionModel>, or its GType number.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

position

Passed: an unsigned 32-bit integer.

the position of the item to query

%return

Returned: a boolean.

#t if the item is selected

Virtual method: selection-model:do-select-all implementor self ⇒ %return

Requests to select all items in the model.

To override it in a new class, give register-type #:vfuncs `((select-all . ,procedure)). The procedure is called as (procedure self) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GtkSelectionModel>, or its GType number.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

%return

Returned: a boolean.

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

Virtual method: selection-model:do-select-item implementor self position unselect-rest ⇒ %return

Requests to select an item in the model.

To override it in a new class, give register-type #:vfuncs `((select-item . ,procedure)). The procedure is called as (procedure self position unselect-rest) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GtkSelectionModel>, or its GType number.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

position

Passed: an unsigned 32-bit integer.

the position of the item to select

unselect-rest

Passed: a boolean.

whether previously selected items should be unselected

%return

Returned: a boolean.

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

Virtual method: selection-model:do-select-range implementor self position n-items unselect-rest ⇒ %return

Requests to select a range of items in the model.

To override it in a new class, give register-type #:vfuncs `((select-range . ,procedure)). The procedure is called as (procedure self position n-items unselect-rest) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GtkSelectionModel>, or its GType number.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

position

Passed: an unsigned 32-bit integer.

the first item to select

n-items

Passed: an unsigned 32-bit integer.

the number of items to select

unselect-rest

Passed: a boolean.

whether previously selected items should be unselected

%return

Returned: a boolean.

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

Virtual method: selection-model:do-set-selection implementor self selected mask ⇒ %return

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:

for (i = 0; i < n_items; i++)
  {
    // don't change values not in the mask
    if (!gtk_bitset_contains (mask, i))
      continue;

    if (gtk_bitset_contains (selected, i))
      select_item (i);
    else
      unselect_item (i);
  }

gtk_selection_model_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.

To override it in a new class, give register-type #:vfuncs `((set-selection . ,procedure)). The procedure is called as (procedure self selected mask) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GtkSelectionModel>, or its GType number.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

selected

Passed: a <GtkBitset>.

bitmask specifying if items should be selected or unselected

mask

Passed: a <GtkBitset>.

bitmask specifying which items should be updated

%return

Returned: a boolean.

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

Virtual method: selection-model:do-unselect-all implementor self ⇒ %return

Requests to unselect all items in the model.

To override it in a new class, give register-type #:vfuncs `((unselect-all . ,procedure)). The procedure is called as (procedure self) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GtkSelectionModel>, or its GType number.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

%return

Returned: a boolean.

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

Virtual method: selection-model:do-unselect-item implementor self position ⇒ %return

Requests to unselect an item in the model.

To override it in a new class, give register-type #:vfuncs `((unselect-item . ,procedure)). The procedure is called as (procedure self position) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GtkSelectionModel>, or its GType number.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

position

Passed: an unsigned 32-bit integer.

the position of the item to unselect

%return

Returned: a boolean.

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

Virtual method: selection-model:do-unselect-range implementor self position n-items ⇒ %return

Requests to unselect a range of items in the model.

To override it in a new class, give register-type #:vfuncs `((unselect-range . ,procedure)). The procedure is called as (procedure self position n-items) ⇒ %return.

implementor

Passed: the class whose implementation to call: a class that implements <GtkSelectionModel>, or its GType number.

self

Passed: a <GtkSelectionModel>.

a <GtkSelectionModel>

position

Passed: an unsigned 32-bit integer.

the first item to unselect

n-items

Passed: an unsigned 32-bit integer.

the number of items to unselect

%return

Returned: a boolean.

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