
GnomeTools::Gtk::ListView
Description
A listview is like a listbox where objects can be inserted horizontally or vertically. The listbox is used for simple and short lists while the listview can be used for much longer lists and complex objects. 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. Other entries are available to get the number of selected items for example.
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
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
) {
my @selections = @items[$listview.get-selection];
$dialog.set-status(@selections.join(', '));
}
method selection-changed ( @selections, GnomeTools::Gtk::Dialog :$dialog ) {
$dialog.set-status("Rows '{@selections.join(', ')}' are selected");
}
}
Instantiate the class and setup the GnomeTools::Gtk::ListView. In this example the ListView is placed in a GnomeTools::Gtk::Dialog.
my HelperObject $helper .= new;
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( :object($helper), :$dialog);
$listview.append(|@items);
$dialog.add-content( 'Nice list', $listview);
# Buttons
$dialog.add-button(
$helper, 'show-select', 'Get Selection 2',
:$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 )
$!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).
set-events
Register a series of events. One defined in Gnome::Gtk4::ListView and the rest from Gnome::Gtk4::SignalListItemFactory class.
method set-events ( :$object, *%options )
$object; User object where methods are defined to process the events. There are many events, so the method names are fixed for simplicity. Most events are defined by Gnome::Gtk4::SignalListItemFactory. The info can be looked up here or here. The method is not called when it isn't defined.
*%options: Any user options. The options is given to the method in
$object.
The event defined in nome::Gtk4::ListView is activate and the method called will be activate-list-item. 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.
The user callback interface must be like;
method activate-list-item ( UInt $position, @selections, *%options )
Where;
$position; Position is the last clicked selection.
@selections; The list of selections in the ListView. Any named arguments
*%optionsgiven to.new()are given to the method.
About my projects, references, blog posts, examples and tutorials