
GnomeTools::Gtk::ListView
Description
A listview is like a list where objects can be inserted horizontally or vertically. The listview is filled using a factory and is created in steps. The list is often partly visible and only asks for the objects to be created when they become visible.
There are several events which needs to be captured if complex objects must be created. This class has only one event which is activate and can be set using .set-activate(). Other events which may be needed are defined in GnomeTools::Gtk::R-ListModel using the Gtk class Gnome::Gtk4::SignalListItemFactory.
CSS classes
The GnomeTools::Gtk::ListView class is placed in a Gnome::Gtk4::ScrolledWindow. That object has a classname listview-window and the GnomeTools::Gtk::ListView object has a classname listview-tool. Other CSS nodes are defined for Gnome::Gtk4::ListView.
Example
This example shows the easy way to make use of the class. The objects created are simple Label objects with a text.
First define a helper class.
class HelperObject {
method show-select (
GnomeTools::Gtk::ListView :$listview,
GnomeTools::Gtk::Dialog :$dialog, :@items
) {
$dialog.set-status($listview.get-selection.join(', '));
}
method selection-changed (
UInt $position, @selections, GnomeTools::Gtk::Dialog :$dialog, :@items
) {
my @rows = @selections.map: {@items.first( $_, :k)};
$dialog.set-status("Rows '{@rows.join(', ')}' are selected");
}
}
my HelperObject $helper .= new;
Instantiate the class and setup the GnomeTools::Gtk::ListView. In this example the ListView is placed in a GnomeTools::Gtk::Dialog.
my GnomeTools::Gtk::Dialog $dialog .= new(
:dialog-header('Test Dialog'), :add-statusbar
);
my @items = <class role method sub submethod for else unit package module>;
my GnomeTools::Gtk::ListView $listview .= new(:multi-select);
$listview.set-events;
$listview.set-selection-changed(
:object($helper), :method<selection-changed>, :$dialog, :@items
);
$listview.append(|@items);
$dialog.add-content( 'Nice list', $listview);
# Buttons
$dialog.add-button(
$helper, 'show-select', 'Get Selection',
:$dialog, :@items, :$listview
);
$dialog.add-button( $dialog, 'destroy-dialog', 'Cancel');
$dialog.set-size-request( 400, 300);
$dialog.show-dialog;
Methods
new
Instantiate the GnomeTools::Gtk::ListView class.
submethod BUILD ( Bool :$multi-select = False, :$orientation = GTK_ORIENTATION_VERTICAL )
$multi-select; Selection method. When True, more than one entry can be selected. By default False. Selections can be done a) by holding <CTRL> or <SHIFT> and click on the entries. b) by dragging the pointer over the entries (rubberband select).
$orientation; Orientation of the list. By default shown vertically.
set-activate
Define a callback routine for the activate event. The activate event is emitted when a row has been activated by the user. If an item is activatable, double-clicking on the item, using the Return key or calling .activate() in Gnome::Gtk4::Widget will activate the item. Activating instructs the containing view to handle activation.
method set-activate ( Any:D $object, Str:D $method, *%options )
$object; user object where methods are defined to process the events.
$method; the method called when the
activateevent arrives.*%options: any named arguments are passed to the callback method.
The callback method must have the following API;
method $object."$method" ( UInt $position, @selections, *%options);
$position; The position of the activated entry.
@selections; A list of the currently selected entries.
%options. Any named arguments provided to
.set-activate().
About my projects, examples and tutorials