HSpacing, VSpacing, HStretch, VStretch — Fixed size empty space for layout
HSpacing
( | void); |
VSpacing
( | void); |
HStretch
( | void); |
VStretch
( | void); |
HSpacing and VSpacing are layout helpers to add empty space between widgets.
VStretch and HStretch act as "rubber bands" in layouts that take excess space. They have a size of zero if there is no excess space.
The size given is measured in units roughly equivalent to the size of a character in the respective UI. Fractional numbers can be used here, but text based UIs may choose to round the number as appropriate - even if this means simply ignoring a spacing when its size becomes zero.
If size is omitted, it defaults to 1. HSpacing() will create a horizontal spacing with default width and zero height. VSpacing() will create a vertical spacing with default height and zero width. HStretch() will create a horizontal stretch with zero width and height. VStretch() will create a vertical stretch with zero width and height.
A HStretch or VStretch with a size specification will take at least the specified amount of space, but it will take more (in that dimension) if there is excess space in the layout.
{
// Build dialog with one input field field, 4 Beatles buttons and an OK button.
UI::OpenDialog(
`VBox(
`VSpacing(),
`HBox(
`Label("Name:"),
`InputField(`id(`name), "")
),
`VSpacing(0.2),
`HBox(
`PushButton(`id(`john), "&John" ), `HSpacing(0.5),
`PushButton(`id(`paul), "&Paul" ), `HSpacing(3),
`PushButton(`id(`george), "&George"), `HSpacing(0.5),
`PushButton(`id(`ringo), "&Ringo" )
),
`VSpacing(0.5),
`PushButton(`id(`ok), "&OK")
)
);
// Wait for user input.
any button = nil;
// Input loop that only the OK button will leave.
// The 4 Beatles buttons will just propose a name.
repeat
{
button = UI::UserInput();
if ( button == `john ) UI::ChangeWidget(`id(`name), `Value, "John Lennon");
else if ( button == `paul ) UI::ChangeWidget(`id(`name), `Value, "Paul McCartney");
else if ( button == `george ) UI::ChangeWidget(`id(`name), `Value, "George Harrison");
else if ( button == `ringo ) UI::ChangeWidget(`id(`name), `Value, "Ringo Starr" );
} until ( button == `ok );
UI::CloseDialog();
}
{
// Layout example:
//
// Build a dialog with three equal sized buttons,
// this time with some spacing in between.
//
// The equal `HWeight()s will make the buttons even sized.
// When resized larger, all buttons will retain their size.
// Excess space will go to the HSpacing() widgets between the
// buttons, i.e. there will be empty space between the buttons.
//
// Notice the importance of `opt(`hstretch) for the `HSpacing()s
// here: This is what makes the HSpacing()s grow. Otherwise, they
// would retain a constant size, and the buttons would grow.
UI::OpenDialog(
`HBox(
`HWeight(1, `PushButton( `opt(`default), "&OK" ) ),
`HSpacing(`opt(`hstretch), 3),
`HWeight(1, `PushButton( "&Cancel everything" ) ),
`HSpacing(`opt(`hstretch), 3),
`HWeight(1, `PushButton( "&Help" ) )
)
);
UI::UserInput();
UI::CloseDialog();
}
{
// Layout example:
//
// Build a dialog with three equal sized buttons.
//
// The equal `HWeight()s will make the buttons equal sized.
// When resized larger, all buttons will retain their size.
// Excess space will go to the HStretch() widgets between the
// buttons, i.e. there will be empty space between the buttons.
UI::OpenDialog(
`HBox(
`HWeight(1, `PushButton( `opt(`default), "&OK" ) ),
`HStretch(),
`HWeight(1, `PushButton( "&Cancel everything" ) ),
`HStretch(),
`HWeight(1, `PushButton( "&Help" ) )
)
);
UI::UserInput();
UI::CloseDialog();
}
{
// Layout example:
//
// Build a dialog with three equal sized buttons.
//
// The equal `HWeight()s will make the buttons equal sized.
// When resized larger, all buttons will retain their size.
// Excess space will go to the HStretch() widgets between the
// buttons, i.e. there will be empty space between the buttons.
UI::OpenDialog(
`HBox(
`HWeight(1, `PushButton( `opt(`default), "&OK" ) ),
`HStretch(),
`HWeight(1, `PushButton( "&Cancel everything" ) ),
`HStretch(),
`HWeight(1, `PushButton( "&Help" ) )
)
);
UI::UserInput();
UI::CloseDialog();
}
// Table example: Exchange complete table content
{
list foodItems =
[
`item(`id(3), "Spaghetti", 8),
`item(`id(4), "Steak Sandwich", 12),
`item(`id(1), "Chili", 6),
`item(`id(2), "Salami Baguette", nil)
];
list carItems =
[
`item(`id(0), "Mercedes", 60000),
`item(`id(1), "Audi", 50000),
`item(`id(2), "VW", 40000),
`item(`id(3), "BMW", 60000),
`item(`id(3), "Porsche", 80000)
];
list itemLists = [ foodItems, carItems ];
integer listNo = 0;
UI::OpenDialog(
`VBox(
`Heading("Prices"),
`MinSize( 30, 10, `Table(`id(`table), `header("Name", "Price"), foodItems ) ),
`Right(
`HBox(
`PushButton(`id(`next), "Change &Table Contents"),
`PushButton(`id(`cancel), "&Close")
)
)
)
);
while (UI::UserInput() != `cancel)
{
// Change table contents
listNo = 1 - listNo;
UI::ChangeWidget(`table, `Items, itemLists[ listNo ]:nil );
// Double check: Retrieve contents and dump to log
y2milestone( "New table content:\n%1", UI::QueryWidget(`table, `Items ) );
}
UI::CloseDialog();
}
// Table example: Exchange complete table content
{
list foodItems =
[
`item(`id(3), "Spaghetti", 8),
`item(`id(4), "Steak Sandwich", 12),
`item(`id(1), "Chili", 6),
`item(`id(2), "Salami Baguette", nil)
];
list carItems =
[
`item(`id(0), "Mercedes", 60000),
`item(`id(1), "Audi", 50000),
`item(`id(2), "VW", 40000),
`item(`id(3), "BMW", 60000),
`item(`id(3), "Porsche", 80000)
];
list itemLists = [ foodItems, carItems ];
integer listNo = 0;
UI::OpenDialog(
`VBox(
`Heading("Prices"),
`MinSize( 30, 10, `Table(`id(`table), `header("Name", "Price"), foodItems ) ),
`Right(
`HBox(
`PushButton(`id(`next), "Change &Table Contents"),
`PushButton(`id(`cancel), "&Close")
)
)
)
);
while (UI::UserInput() != `cancel)
{
// Change table contents
listNo = 1 - listNo;
UI::ChangeWidget(`table, `Items, itemLists[ listNo ]:nil );
// Double check: Retrieve contents and dump to log
y2milestone( "New table content:\n%1", UI::QueryWidget(`table, `Items ) );
}
UI::CloseDialog();
}