I am trying to create a GUI application using fltk which will have 4 types of input widgets: checkbox, menu choice as well as single and multi-line input text fields:
All these have a function named value() which returns the entered value.
Can I have a vector which can hold these different types of widgets. I want to then iterate through the vector and get values from each of these widgets:
for v in mixed_vector{
let entered_value = v.value();
// do something with entered_value..
}
I see a page on net but not able to apply it to my problem.
Looks to me like the value method comes from the ButtonExt trait. You can create a vector of trait objects where each element implements ButtonExt[1], thus allowing you to call the value method on each element.
Pseudo-code:
let elements: Vec<Box<dyn ButtonExt>> = vec![
Box::new(MultilineInput::new()),
Box::new(Input::new())
];
for v in elements {
v.value();
}
Never mind, ButtonExt requires the WidgetExt trait as supertrait. WidgetExt has methods with generic type parameters, hence it is not object safe. AFAIK that leaves you with implementing an enum wrapper with a variant for each element you want to store in the Vector and forward the value method to the widgets, something along the lines of:
enum Widgets {
MultilineInput(MultilineInput),
Input(Input),
// ...
}
impl Widgets {
fn value(&self) -> bool {
match self {
Self::MultilineInput(mi) => mi.value(),
Self::Input(i) => i.value(),
// ...
}
}
}
let elements: Vec<Widget> = vec![
Widget::MultilineInput(MultilineInput::new()),
Widget::Input(Input::new())
];
for v in elements {
v.value();
}
If ButtonExt is object safe which, on a quick glance, look to me like it is. âŠī¸