use super::grid;
use super::pack;
use super::widget;
use super::wish;
#[derive(Clone, Debug, PartialEq)]
pub struct TkCheckButton {
pub id: String,
var: String,
}
pub fn make_check_button(parent: &impl widget::TkWidget) -> TkCheckButton {
let id = wish::next_wid(parent.id());
let var = format!("::cb{}", wish::current_id());
let msg = format!("set {} 0 ; ttk::checkbutton {} -variable {}", var, id, var);
wish::tell_wish(&msg);
TkCheckButton { id, var }
}
impl widget::TkWidget for TkCheckButton {
fn id(&self) -> &str {
&self.id
}
}
impl grid::TkGridLayout for TkCheckButton {}
impl pack::TkPackLayout for TkCheckButton {}
impl widget::TkLabelOptions for TkCheckButton {}
impl TkCheckButton {
pub fn command(&self, command: impl Fn(bool) + Send + 'static) {
wish::add_callback1_bool(&self.id, wish::mk_callback1_bool(command));
let msg = format!(
"{} configure -command {{ puts cb1b-{}-${} ; flush stdout }}",
self.id, self.id, self.var
);
wish::tell_wish(&msg);
}
pub fn invoke(&self) {
let msg = format!("{} invoke", self.id);
wish::tell_wish(&msg);
}
pub fn is_selected(&self) -> bool {
let msg = format!("puts ${} ; flush stdout", self.var);
let result = wish::ask_wish(&msg);
result == "1"
}
pub fn selected(&self, value: bool) {
let msg = format!("set {} {}", self.var, if value { "1" } else { "0" });
wish::tell_wish(&msg);
}
pub fn state(&self, value: widget::State) {
widget::configure(&self.id, "state", &value.to_string());
}
}