ListStore
open class ListStore : GLibObject.Object, ListStoreProtocol
A list-like data structure that can be used with the GtkTreeView
The GtkListStore
object is a list model for use with a GtkTreeView
widget. It implements the GtkTreeModel
interface, and consequentialy,
can use all of the methods available there. It also implements the
GtkTreeSortable
interface so it can be sorted by the view.
Finally, it also implements the tree
drag and drop
interfaces.
The GtkListStore
can accept most GObject types as a column type, though
it can’t accept all custom types. Internally, it will keep a copy of
data passed in (such as a string or a boxed pointer). Columns that
accept GObject
s are handled a little differently. The
GtkListStore
will keep a reference to the object instead of copying the
value. As a result, if the object is modified, it is up to the
application writer to call gtk_tree_model_row_changed()
to emit the
GtkTreeModel
row_changed
signal. This most commonly affects lists with
GdkTexture
s stored.
An example for creating a simple list store: (C Language Example):
enum {
COLUMN_STRING,
COLUMN_INT,
COLUMN_BOOLEAN,
N_COLUMNS
};
{
GtkListStore *list_store;
GtkTreePath *path;
GtkTreeIter iter;
int i;
list_store = gtk_list_store_new (N_COLUMNS,
G_TYPE_STRING,
G_TYPE_INT,
G_TYPE_BOOLEAN);
for (i = 0; i < 10; i++)
{
char *some_data;
some_data = get_some_data (i);
// Add a new row to the model
gtk_list_store_append (list_store, &iter);
gtk_list_store_set (list_store, &iter,
COLUMN_STRING, some_data,
COLUMN_INT, i,
COLUMN_BOOLEAN, FALSE,
-1);
// As the store will keep a copy of the string internally,
// we free some_data.
g_free (some_data);
}
// Modify a particular row
path = gtk_tree_path_new_from_string ("4");
gtk_tree_model_get_iter (GTK_TREE_MODEL (list_store),
&iter,
path);
gtk_tree_path_free (path);
gtk_list_store_set (list_store, &iter,
COLUMN_BOOLEAN, TRUE,
-1);
}
Performance Considerations
Internally, the GtkListStore
was originally implemented with a linked list
with a tail pointer. As a result, it was fast at data insertion and deletion,
and not fast at random data access. The GtkListStore
sets the
GTK_TREE_MODEL_ITERS_PERSIST
flag, which means that GtkTreeIter
s can be
cached while the row exists. Thus, if access to a particular row is needed
often and your code is expected to run on older versions of GTK, it is worth
keeping the iter around.
Atomic Operations
It is important to note that only the methods
gtk_list_store_insert_with_values()
and gtk_list_store_insert_with_valuesv()
are atomic, in the sense that the row is being appended to the store and the
values filled in in a single operation with regard to GtkTreeModel
signaling.
In contrast, using e.g. gtk_list_store_append()
and then gtk_list_store_set()
will first create a row, which triggers the GtkTreeModel
row-inserted
signal
on GtkListStore
. The row, however, is still empty, and any signal handler
connecting to GtkTreeModel
row-inserted
on this particular store should be prepared
for the situation that the row might be empty. This is especially important
if you are wrapping the GtkListStore
inside a GtkTreeModel
Filter and are
using a GtkTreeModel
FilterVisibleFunc. Using any of the non-atomic operations
to append rows to the GtkListStore
will cause the
GtkTreeModel
FilterVisibleFunc to be visited with an empty row first; the
function must be prepared for that.
GtkListStore as GtkBuildable
The GtkListStore implementation of the GtkBuildable interface allows to specify the model columns with a <columns> element that may contain multiple <column> elements, each specifying one model column. The “type” attribute specifies the data type for the column.
Additionally, it is possible to specify content for the list store in the UI definition, with the <data> element. It can contain multiple <row> elements, each specifying to content for one row of the list model. Inside a <row>, the <col> elements specify the content for individual cells.
Note that it is probably more common to define your models in the code, and one might consider it a layering violation to specify the content of a list store in a UI definition, data, not presentation, and common wisdom is to separate the two, as far as possible.
An example of a UI Definition fragment for a list store: (C Language Example):
<object class="GtkListStore">
<columns>
<column type="gchararray"/>
<column type="gchararray"/>
<column type="gint"/>
</columns>
<data>
<row>
<col id="0">John</col>
<col id="1">Doe</col>
<col id="2">25</col>
</row>
<row>
<col id="0">Johan</col>
<col id="1">Dahlin</col>
<col id="2">50</col>
</row>
</data>
</object>
The ListStore
type acts as a reference-counted owner of an underlying GtkListStore
instance.
It provides the methods that can operate on this data type through ListStoreProtocol
conformance.
Use ListStore
as a strong reference or owner of a GtkListStore
instance.
-
Designated initialiser from the underlying `C` data type.
This creates an instance without performing an unbalanced retain i.e., ownership is transferred to the
ListStore
instance.Declaration
Swift
@inlinable public init(_ op: UnsafeMutablePointer<GtkListStore>)
Parameters
op
pointer to the underlying object
-
Designated initialiser from a constant pointer to the underlying
C
data type. This creates an instance without performing an unbalanced retain i.e., ownership is transferred to theListStore
instance.Declaration
Swift
@inlinable public init(_ op: UnsafePointer<GtkListStore>)
Parameters
op
pointer to the underlying object
-
Optional initialiser from a non-mutating
gpointer
to the underlyingC
data type. This creates an instance without performing an unbalanced retain i.e., ownership is transferred to theListStore
instance.Declaration
Swift
@inlinable override public init!(gpointer op: gpointer?)
Parameters
op
gpointer to the underlying object
-
Optional initialiser from a non-mutating
gconstpointer
to the underlyingC
data type. This creates an instance without performing an unbalanced retain i.e., ownership is transferred to theListStore
instance.Declaration
Swift
@inlinable override public init!(gconstpointer op: gconstpointer?)
Parameters
op
pointer to the underlying object
-
Optional initialiser from a constant pointer to the underlying
C
data type. This creates an instance without performing an unbalanced retain i.e., ownership is transferred to theListStore
instance.Declaration
Swift
@inlinable public init!(_ op: UnsafePointer<GtkListStore>?)
Parameters
op
pointer to the underlying object
-
Optional initialiser from the underlying
C
data type. This creates an instance without performing an unbalanced retain i.e., ownership is transferred to theListStore
instance.Declaration
Swift
@inlinable public init!(_ op: UnsafeMutablePointer<GtkListStore>?)
Parameters
op
pointer to the underlying object
-
Designated initialiser from the underlying
C
data type. Will retainGtkListStore
. i.e., ownership is transferred to theListStore
instance.Declaration
Swift
@inlinable public init(retaining op: UnsafeMutablePointer<GtkListStore>)
Parameters
op
pointer to the underlying object
-
Reference intialiser for a related type that implements
ListStoreProtocol
Will retainGtkListStore
.Declaration
Swift
@inlinable public init<T>(listStore other: T) where T : ListStoreProtocol
Parameters
other
an instance of a related type that implements
ListStoreProtocol
-
Unsafe typed initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ListStoreProtocol
.Declaration
Swift
@inlinable override public init<T>(cPointer p: UnsafeMutablePointer<T>)
Parameters
cPointer
pointer to the underlying object
-
Unsafe typed, retaining initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ListStoreProtocol
.Declaration
Swift
@inlinable override public init<T>(retainingCPointer cPointer: UnsafeMutablePointer<T>)
Parameters
cPointer
pointer to the underlying object
-
Unsafe untyped initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ListStoreProtocol
.Declaration
Swift
@inlinable override public init(raw p: UnsafeRawPointer)
Parameters
p
raw pointer to the underlying object
-
Unsafe untyped, retaining initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ListStoreProtocol
.Declaration
Swift
@inlinable override public init(retainingRaw raw: UnsafeRawPointer)
-
Unsafe untyped initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ListStoreProtocol
.Declaration
Swift
@inlinable public required init(raw p: UnsafeMutableRawPointer)
Parameters
p
mutable raw pointer to the underlying object
-
Unsafe untyped, retaining initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ListStoreProtocol
.Declaration
Swift
@inlinable required public init(retainingRaw raw: UnsafeMutableRawPointer)
Parameters
raw
mutable raw pointer to the underlying object
-
Unsafe untyped initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ListStoreProtocol
.Declaration
Swift
@inlinable override public init(opaquePointer p: OpaquePointer)
Parameters
p
opaque pointer to the underlying object
-
Unsafe untyped, retaining initialiser. Do not use unless you know the underlying data type the pointer points to conforms to
ListStoreProtocol
.Declaration
Swift
@inlinable override public init(retainingOpaquePointer p: OpaquePointer)
Parameters
p
opaque pointer to the underlying object
-
Non-vararg creation function. Used primarily by language bindings.
Declaration
Swift
@inlinable public init(nColumns: Int, types: UnsafeMutablePointer<GType>!)
-
Non-vararg creation function. Used primarily by language bindings.
Declaration
Swift
@inlinable public static func listStoreNewv(nColumns: Int, types: UnsafeMutablePointer<GType>!) -> ListStore!
-
Return a tree model reference for the list store
Declaration
Swift
@inlinable var treeModel: TreeModelRef { get }
-
Convenience constructor specifying the column types
Declaration
Swift
@inlinable convenience init(types: [GType])
Parameters
types
array of column types for this list model
-
Convenience constructor specifying the list column types
Declaration
Swift
@inlinable convenience init(_ types: GType...)
Parameters
types
column types for this list model
-
Set the given values for the current row
Declaration
Swift
@inlinable func set<I, V>(iter i: I, values: [V], startColumn: Int = 0) where I : TreeIterProtocol, V : ValueProtocol
Parameters
i
iterator representing the current row
values
array of values to add
startColumn
column to start from (defaults to
0
) -
Append the given values to the next row
Declaration
Swift
@inlinable func append<I, V>(asNextRow i: I, values v: [V], startColumn s: Int = 0) where I : TreeIterProtocol, V : ValueProtocol
Parameters
i
iterator representing the current row (updated to next row)
v
array of values to add
startColumn
column to start from (defaults to
0
) -
Append the given values to the next row
Declaration
Swift
@inlinable func append<I>(asNextRow i: I, startColumn s: Int = 0, _ values: Value...) where I : TreeIterProtocol
Parameters
i
tree iterator representing the current row (updated to next row)
values
array of values to add
startColumn
column to start from (defaults to
0
)