SelectionBox — Scrollable list selection
SelectionBox
( | string label , |
list
items
); |
shrinkable
make the widget very small
immediate
make `notify trigger immediately when the selected item changes
A selection box offers the user to select an item out of a list. Each item has a label and an optional id. When constructing the list of items, you have two way of specifying an item. Either you give a plain string, in which case the string is used both for the id and the label of the item. Or you specify a term `item( term id, string label ) or `item( term id, string label, boolean selected ), where you give an id of the form `id( any v ) where you can store an aribtrary value as id. The third argument controls whether the item is the selected item.
`SelectionBox( `id( `pizza ), "select your Pizza:", [ "Margarita", `item( `id( `na ), "Napoli" ) ] )
{
UI::OpenDialog(
`VBox(
`SelectionBox( "Select your Pizza:",
[
"Napoli",
"Funghi",
"Salami"
] ),
`PushButton("&OK")
)
);
UI::UserInput();
UI::CloseDialog();
}
// Simple SelectionBox example
{
// Create a selection box with three entries.
// All entries have IDs to identify them independent of locale
// (The texts might have to be translated!).
// Entry "Funghi" will be selected by default.
UI::OpenDialog(
`VBox(
`SelectionBox(`id(`pizza),
"Select your Pizza:",
[
`item(`id(`nap), "Napoli" ),
`item(`id(`fun), "Funghi", true ),
`item(`id(`sal), "Salami" )
] ),
`PushButton("&OK")
)
);
UI::UserInput();
// Get the input from the selection box.
//
// Notice: The return value of UI::UserInput() does NOT return this value!
// Rather, it returns the ID of the widget (normally the PushButton)
// that caused UI::UserInput() to return.
any pizza = UI::QueryWidget(`id(`pizza), `CurrentItem);
y2milestone( "Selected pizza: %1", pizza );
// Close the dialog.
// Remember to read values from the dialog's widgets BEFORE closing it!
UI::CloseDialog();
// Evaluate selection
string toppings = "nothing";
if ( pizza == `nap ) toppings = "Tomatoes, Cheese";
else if ( pizza == `fun ) toppings = "Tomatoes, Cheese, Mushrooms";
else if ( pizza == `sal ) toppings = "Tomatoes, Cheese, Sausage";
// Pop up a new dialog to echo the selection.
UI::OpenDialog(
`VBox(
`Label("You will get a pizza with:"),
`Label(toppings),
`PushButton("&OK")
)
);
UI::UserInput();
UI::CloseDialog();
}
{
// Create a selection box with three entries.
// All entries have IDs to identify them independent of locale
// (The texts might have to be translated!).
// Entry "Funghi" will be selected by default.
//
// There are two buttons to select a "Today's special" and a
// "veggie" pizza to demonstrate how to select list entries
// programmatically.
UI::OpenDialog(
`VBox(
`SelectionBox(`id(`pizza),
"Select your Pizza:",
[
`item(`id(`nap), "Napoli" ),
`item(`id(`fun), "Funghi", true ),
`item(`id(`sal), "Salami" )
] ),
`HBox(
`PushButton(`id(`todays_special), `opt(`hstretch), "&Today's special" ),
`PushButton(`id(`veggie), `opt(`hstretch), "&Veggie" ),
`PushButton(`id(`none), `opt(`hstretch), "&Nothing" )
),
`PushButton(`id(`ok), `opt(`default), "&OK")
)
);
any id = nil;
repeat
{
id = UI::UserInput();
if ( id == `todays_special ) UI::ChangeWidget( `pizza, `CurrentItem, `nap );
else if ( id == `veggie ) UI::ChangeWidget( `pizza, `CurrentItem, `fun );
else if ( id == `none ) UI::ChangeWidget( `pizza, `CurrentItem, nil );
} until ( id == `ok );
// Get the input from the selection box.
//
// Notice: The return value of UI::UserInput() does NOT return this value!
// Rather, it returns the ID of the widget (normally the PushButton)
// that caused UI::UserInput() to return.
any pizza = UI::QueryWidget(`id(`pizza), `CurrentItem);
// Close the dialog.
// Remember to read values from the dialog's widgets BEFORE closing it!
UI::CloseDialog();
// Evaluate selection
string toppings = "nothing";
if ( pizza == `nap ) toppings = "Tomatoes, Cheese";
else if ( pizza == `fun ) toppings = "Tomatoes, Cheese, Mushrooms";
else if ( pizza == `sal ) toppings = "Tomatoes, Cheese, Sausage";
// Pop up a new dialog to echo the selection.
UI::OpenDialog(
`VBox(
`Label("You will get a pizza with:"),
`Label(toppings),
`PushButton(`opt(`default), "&OK")
)
);
UI::UserInput();
UI::CloseDialog();
}
{
// Create a selection box with three entries.
// All entries have IDs to identify them independent of locale
// (The texts might have to be translated!).
// Entry "Funghi" will be selected by default.
//
// There are two buttons to select a "Today's special" and a
// "veggie" pizza to demonstrate how to select list entries
// from within a YCP script - even without having to use item IDs.
UI::OpenDialog(
`VBox(
`SelectionBox(`id(`pizza),
"Select your Pizza:",
[
"Napoli",
"Funghi",
"Salami",
"Quattro Stagioni (a pizza which is devided into 4 parts each with a different topping)",
"Caprese",
"Speciale",
"Hawaii"
] ),
`HBox(
`PushButton(`id(`todays_special), `opt(`hstretch), "&Today's special" ),
`PushButton(`id(`veggie), `opt(`hstretch), "&Veggie" )
),
`PushButton(`id(`ok), `opt(`default), "&OK")
)
);
any id = nil;
repeat
{
id = UI::UserInput();
if ( id == `todays_special ) UI::ChangeWidget( `id( `pizza ), `CurrentItem, "Napoli" );
else if ( id == `veggie ) UI::ChangeWidget( `id( `pizza ), `CurrentItem, "Funghi" );
} until ( id == `ok );
// Get the input from the selection box.
//
// Notice: The return value of UI::UserInput() does NOT return this value!
// Rather, it returns the ID of the widget (normally the PushButton)
// that caused UI::UserInput() to return.
string pizza = (string) UI::QueryWidget(`id(`pizza), `CurrentItem);
// Close the dialog.
// Remember to read values from the dialog's widgets BEFORE closing it!
UI::CloseDialog();
// Pop up a new dialog to echo the selection.
UI::OpenDialog(
`VBox(
`Label("Pizza " + pizza + " coming right up"),
`PushButton(`opt(`default), "&OK")
)
);
UI::UserInput();
UI::CloseDialog();
}
// Selection box with icons
{
UI::OpenDialog(
`VBox(
`Heading( "YaST2 Mini Control Center" ),
`SelectionBox(`id(`mod ),
"Modules",
[
`item(`id( "keyboard" ), `icon( "yast-keyboard.png" ), "Keyboard" ),
`item(`id( "mouse" ), `icon( "yast-mouse.png" ), "Mouse" ),
`item(`id( "timezone" ), `icon( "yast-timezone.png" ), "Time zone" ),
`item(`id( "lan" ), `icon( "yast-lan.png" ), "Network" ),
`item(`id( "sw_single" ), `icon( "yast-software.png" ), "Software" )
] ),
`PushButton("&OK")
)
);
UI::UserInput();
// Get the input from the selection box.
//
// Notice: The return value of UI::UserInput() does NOT return this value!
// Rather, it returns the ID of the widget (normally the PushButton)
// that caused UI::UserInput() to return.
string mod = (string) UI::QueryWidget(`id(`mod ), `CurrentItem);
// Close the dialog.
// Remember to read values from the dialog's widgets BEFORE closing it!
UI::CloseDialog();
}
// Example showing how to replace SelectionBox items
{
list pizza_list =
[
"Pizza Napoli",
"Pizza Funghi",
"Pizza Salami",
"Pizza Hawaii"
];
list pasta_list =
[
"Spaghetti",
"Rigatoni",
"Tortellini"
];
UI::OpenDialog( `VBox(
`SelectionBox(`id(`menu), "Daily &Specials:", pizza_list ),
`HBox(
`PushButton(`id(`pizza), "Pi&zza" ),
`PushButton(`id(`pasta), "&Pasta" ),
`PushButton(`id(`diet ), "Strict &Diet" )
),
`PushButton(`id(`ok), "&OK" )
)
);
symbol button = nil;
do
{
button = (symbol) UI::UserInput();
if ( button == `pizza ) UI::ChangeWidget(`menu, `Items, pizza_list );
if ( button == `pasta ) UI::ChangeWidget(`menu, `Items, pasta_list );
if ( button == `diet ) UI::ChangeWidget(`menu, `Items, [] );
} while ( button != `ok );
string order = (string) UI::QueryWidget(`menu, `CurrentItem );
UI::CloseDialog();
//
// Show the result
//
UI::OpenDialog(`VBox(
`Label( sformat( "Your order: %1", order ) ),
`PushButton(`opt(`default), "&OK" )
)
);
UI::UserInput();
UI::CloseDialog();
}
// Example showing how to replace SelectionBox items
{
list pizza_list =
[
`item(`id("Pizza #01"), "Pizza Napoli" ),
`item(`id("Pizza #02"), "Pizza Funghi" ),
`item(`id("Pizza #03"), "Pizza Salami", true ),
`item(`id("Pizza #04"), "Pizza Hawaii" )
];
list pasta_list =
[
`item(`id("Pasta #11"), "Spaghetti" ),
`item(`id("Pasta #12"), "Rigatoni", true ),
`item(`id("Pasta #13"), "Tortellini" )
];
UI::OpenDialog( `VBox(
`SelectionBox(`id(`menu), "Daily &Specials:", pizza_list ),
`HBox(
`PushButton(`id(`pizza), "Pi&zza" ),
`PushButton(`id(`pasta), "&Pasta" )
),
`PushButton(`id(`ok), "&OK" )
)
);
symbol button = nil;
do
{
button = (symbol) UI::UserInput();
if ( button == `pizza ) UI::ChangeWidget(`menu, `Items, pizza_list );
if ( button == `pasta ) UI::ChangeWidget(`menu, `Items, pasta_list );
} while ( button != `ok );
string order = (string) UI::QueryWidget(`menu, `CurrentItem );
UI::CloseDialog();
//
// Show the result
//
UI::OpenDialog(`VBox(
`Label( sformat( "Your order: %1", order ) ),
`PushButton(`opt(`default), "&OK" )
)
);
UI::UserInput();
UI::CloseDialog();
}