2022-06-30 10:40:33 +00:00
|
|
|
# RadioButton.nas : radio button, and group helper
|
|
|
|
# to manage updating checked state conherently
|
2023-01-21 22:48:44 +00:00
|
|
|
# SPDX-FileCopyrightText: (C) 2022 James Turner <james@flightgear.org>, Frederic Croix <thefgfseagle@gmail.com>
|
2022-06-30 10:40:33 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
gui.widgets.RadioButton = {
|
2023-01-21 22:48:44 +00:00
|
|
|
new: func(parent, style = nil, cfg = nil) {
|
|
|
|
var m = gui.Widget.new(gui.widgets.RadioButton);
|
|
|
|
style = style or canvas.style;
|
|
|
|
m._cfg = Config.new(cfg or {});
|
|
|
|
m._focus_policy = m.StrongFocus;
|
|
|
|
m._checked = 0;
|
|
|
|
m.radioGroup = nil;
|
2022-06-30 10:40:33 +00:00
|
|
|
|
2023-01-21 22:48:44 +00:00
|
|
|
m._setView( style.createWidget(parent, m._cfg.get("type", "radio-button"), m._cfg) );
|
2022-06-30 10:40:33 +00:00
|
|
|
|
2023-01-21 22:48:44 +00:00
|
|
|
var parentRadio = m._cfg.get("parentRadio", nil);
|
|
|
|
if (parentRadio != nil) {
|
|
|
|
m.radioGroup = parentRadio.radioGroup;
|
|
|
|
} else {
|
|
|
|
m.radioGroup = gui.widgets.RadioButtonsGroup.new();
|
2022-06-30 10:40:33 +00:00
|
|
|
}
|
2023-01-21 22:48:44 +00:00
|
|
|
m.radioGroup.addRadio(m);
|
2022-06-30 10:40:33 +00:00
|
|
|
|
|
|
|
return m;
|
|
|
|
},
|
|
|
|
|
2023-01-21 22:48:44 +00:00
|
|
|
setText: func(text) {
|
|
|
|
me._view.setText(me, text);
|
|
|
|
return me;
|
2022-06-30 10:40:33 +00:00
|
|
|
},
|
|
|
|
|
2023-01-21 22:48:44 +00:00
|
|
|
setChecked: func(checked = 1) {
|
|
|
|
if (me._checked == checked) {
|
|
|
|
return me;
|
2022-06-30 10:40:33 +00:00
|
|
|
}
|
2023-01-21 22:48:44 +00:00
|
|
|
|
|
|
|
me._setRadioGroupSiblingsUnchecked();
|
|
|
|
me._trigger("toggled", {checked: checked});
|
|
|
|
me._checked = checked;
|
|
|
|
me._onStateChange();
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
|
|
|
|
_setRadioGroupSiblingsUnchecked: func {
|
|
|
|
me.radioGroup._updateChecked(me);
|
|
|
|
},
|
|
|
|
|
|
|
|
toggle: func {
|
|
|
|
me.setChecked(!me._checked);
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
|
|
|
|
_setView: func(view) {
|
|
|
|
call(gui.Widget._setView, [view], me);
|
|
|
|
|
|
|
|
var el = view._root;
|
|
|
|
el.addEventListener("click", func {
|
|
|
|
if (me._enabled) {
|
|
|
|
me.setChecked()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
el.addEventListener("drag", func(e) e.stopPropagation());
|
2022-06-30 10:40:33 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
# auto manage radio-button checked state
|
2023-01-21 22:48:44 +00:00
|
|
|
gui.widgets.RadioButtonsGroup = {
|
|
|
|
new: func(name = "unnamed")
|
2022-06-30 10:40:33 +00:00
|
|
|
{
|
2023-01-21 22:48:44 +00:00
|
|
|
var m = {
|
|
|
|
parents: [gui.widgets.RadioButtonsGroup],
|
|
|
|
name: name,
|
|
|
|
radios: [],
|
|
|
|
};
|
2022-06-30 10:40:33 +00:00
|
|
|
return m;
|
|
|
|
},
|
|
|
|
|
|
|
|
addRadio: func(r)
|
|
|
|
{
|
2023-01-21 22:48:44 +00:00
|
|
|
r.listen("toggled", func(e) {
|
|
|
|
me._updateChecked(r);
|
|
|
|
});
|
2022-06-30 10:40:33 +00:00
|
|
|
append(me.radios, r);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeRadio: func(r)
|
|
|
|
{
|
2023-01-21 22:48:44 +00:00
|
|
|
# XXX should we update some other item to be checked ?
|
|
|
|
if (contains(me.radios, r)) {
|
|
|
|
var index = find(r, me.radios);
|
|
|
|
me.radios = subvec(me.radios, 0, index) ~ subvec(me.radios, index + 1);
|
2022-06-30 10:40:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-01-21 22:48:44 +00:00
|
|
|
setEnabled: func(enabled = 1)
|
2022-06-30 10:40:33 +00:00
|
|
|
{
|
2023-01-21 22:48:44 +00:00
|
|
|
foreach (var r; me.radios) {
|
|
|
|
r.setEnabled(enabled);
|
2022-06-30 10:40:33 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
# update check state of all radios in the group
|
|
|
|
_updateChecked: func(active)
|
|
|
|
{
|
2023-01-21 22:48:44 +00:00
|
|
|
foreach (var r; me.radios) {
|
|
|
|
if (r != active) {
|
|
|
|
r.setChecked(0);
|
|
|
|
}
|
2022-06-30 10:40:33 +00:00
|
|
|
}
|
2023-01-21 22:48:44 +00:00
|
|
|
},
|
|
|
|
};
|