use super::image;
use super::widget;
use super::wish;
#[derive(Clone, Debug, PartialEq)]
pub struct TkMenu {
pub id: String,
}
pub fn make_menu(parent: &impl widget::TkWidget) -> TkMenu {
let id = wish::next_wid(parent.id());
let msg = format!("menu {}", id);
wish::tell_wish(&msg);
TkMenu { id }
}
#[derive(Clone, Debug)]
pub struct TkMenuCascade {
parent: String,
compound: widget::Compound,
font: Option<String>,
image: Option<String>,
label: Option<String>,
menu: Option<String>,
state: widget::State,
underline: Option<u64>,
}
#[derive(Clone, Debug)]
pub struct TkMenuCheck {
parent: String,
accelerator: Option<String>,
command: Option<String>,
command_variable: Option<String>,
compound: widget::Compound,
font: Option<String>,
image: Option<String>,
label: Option<String>,
state: widget::State,
underline: Option<u64>,
}
#[derive(Clone, Debug)]
pub struct TkMenuCommand {
parent: String,
accelerator: Option<String>,
command: Option<String>,
compound: widget::Compound,
font: Option<String>,
image: Option<String>,
label: Option<String>,
state: widget::State,
underline: Option<u64>,
}
#[derive(Clone, Debug)]
pub struct TkMenuRadio {
parent: String,
group: String,
value: String,
accelerator: Option<String>,
command: Option<String>,
command_variable: Option<String>,
compound: widget::Compound,
font: Option<String>,
image: Option<String>,
label: Option<String>,
state: widget::State,
underline: Option<u64>,
}
#[derive(Clone, Debug)]
pub struct TkMenuSeparator {
parent: String,
}
impl widget::TkWidget for TkMenu {
fn id(&self) -> &str {
&self.id
}
}
impl TkMenu {
pub fn cascade(&self) -> TkMenuCascade {
TkMenuCascade {
parent: self.id.clone(),
compound: widget::Compound::None,
font: None,
image: None,
label: None,
menu: None,
state: widget::State::Normal,
underline: None,
}
}
pub fn check_button(&self) -> TkMenuCheck {
TkMenuCheck {
parent: self.id.clone(),
accelerator: None,
command: None,
command_variable: None,
compound: widget::Compound::None,
font: None,
image: None,
label: None,
state: widget::State::Normal,
underline: None,
}
}
pub fn command(&self) -> TkMenuCommand {
TkMenuCommand {
parent: self.id.clone(),
accelerator: None,
command: None,
compound: widget::Compound::None,
font: None,
image: None,
label: None,
state: widget::State::Normal,
underline: None,
}
}
pub fn radio_button(&self, group: &str, value: &str) -> TkMenuRadio {
TkMenuRadio {
parent: self.id.clone(),
group: String::from(group),
value: String::from(value),
accelerator: None,
command: None,
command_variable: None,
compound: widget::Compound::None,
font: None,
image: None,
label: None,
state: widget::State::Normal,
underline: None,
}
}
pub fn radio_button_value_get(&self, group: &str) -> String {
let msg = format!("puts $::mrb_group_{} ; flush stdout", group);
wish::ask_wish(&msg)
}
pub fn radio_button_value(&self, group: &str, value: &str) {
let msg = format!("set ::mrb_group_{} {}", group, value);
wish::tell_wish(&msg);
}
pub fn separator(&self) -> TkMenuSeparator {
TkMenuSeparator {
parent: self.id.clone(),
}
}
pub fn delete(&self, index: u64) {
let msg = format!("{} delete {}", &self.id, index);
wish::tell_wish(&msg);
}
pub fn entry_cget(&self, index: u64, option: &str) -> String {
let msg = format!("{} entrycfig {} {{{}}}", &self.id, index, option);
wish::ask_wish(&msg)
}
pub fn entry_configure(&self, index: u64, option: &str, value: &str) {
let msg = format!(
"{} entryconfigure {} {{{}}} {{{}}}",
&self.id, index, option, value
);
wish::tell_wish(&msg);
}
pub fn invoke(&self, index: u64) {
let msg = format!("{} invoke {}", &self.id, index);
wish::tell_wish(&msg);
}
pub fn popup(&self, x: i64, y: i64) {
let msg = format!("tk_popup {} {} {}", &self.id, x, y);
wish::tell_wish(&msg);
}
}
fn common_option_string(
compound: &widget::Compound,
font: &Option<String>,
image: &Option<String>,
label: &Option<String>,
state: &widget::State,
underline: &Option<u64>,
) -> String {
let mut msg = String::new();
msg.push_str(&format!("-compound {} ", compound));
if let Some(font) = &font {
msg.push_str(&format!("-font {{{}}} ", font));
}
if let Some(image) = &image {
msg.push_str(&format!("-image {{{}}} ", image));
}
if let Some(label) = &label {
msg.push_str(&format!("-label {{{}}} ", label));
}
msg.push_str(&format!("-state {} ", state));
if let Some(underline) = &underline {
msg.push_str(&format!("-underline {} ", underline));
}
msg
}
impl TkMenuCascade {
pub fn compound(&mut self, value: widget::Compound) -> &mut Self {
self.compound = value;
self
}
pub fn font(&mut self, value: &str) -> &mut Self {
self.font = Some(String::from(value));
self
}
pub fn image(&mut self, image: &image::TkImage) -> &mut Self {
self.image = Some(String::from(&image.id));
self
}
pub fn label(&mut self, value: &str) -> &mut Self {
self.label = Some(String::from(value));
self
}
pub fn menu(&mut self, submenu: &TkMenu) -> &mut Self {
self.menu = Some(submenu.id.clone());
self
}
pub fn state(&mut self, value: widget::State) -> &mut Self {
self.state = value;
self
}
pub fn underline(&mut self, value: u64) -> &mut Self {
self.underline = Some(value);
self
}
fn option_string(&self) -> String {
let mut msg = common_option_string(
&self.compound,
&self.font,
&self.image,
&self.label,
&self.state,
&self.underline,
);
if let Some(menu) = &self.menu {
msg.push_str(&format!("-menu {} ", menu));
}
msg
}
pub fn add(&self) {
let msg = format!("{} add cascade {}", &self.parent, &self.option_string());
wish::tell_wish(&msg);
}
pub fn insert(&self, index: u64) {
let msg = format!(
"{} insert cascade {} {}",
&self.parent,
index,
&self.option_string()
);
wish::tell_wish(&msg);
}
}
impl TkMenuCheck {
pub fn accelerator(&mut self, key_definition: &str) -> &mut Self {
self.accelerator = Some(String::from(key_definition));
self
}
pub fn command(&mut self, command: impl Fn(bool) + Send + 'static) -> &mut Self {
let id = wish::next_wid(".");
let var = format!("::mcb{}", wish::current_id());
wish::add_callback1_bool(&id, wish::mk_callback1_bool(command));
self.command = Some(id);
self.command_variable = Some(var);
self
}
pub fn compound(&mut self, value: widget::Compound) -> &mut Self {
self.compound = value;
self
}
pub fn font(&mut self, value: &str) -> &mut Self {
self.font = Some(String::from(value));
self
}
pub fn image(&mut self, image: &image::TkImage) -> &mut Self {
self.image = Some(String::from(&image.id));
self
}
pub fn label(&mut self, value: &str) -> &mut Self {
self.label = Some(String::from(value));
self
}
pub fn state(&mut self, value: widget::State) -> &mut Self {
self.state = value;
self
}
pub fn underline(&mut self, value: u64) -> &mut Self {
self.underline = Some(value);
self
}
fn option_string(&self) -> String {
let mut msg = common_option_string(
&self.compound,
&self.font,
&self.image,
&self.label,
&self.state,
&self.underline,
);
if let Some(accelerator) = &self.accelerator {
msg.push_str(&format!("-accelerator {{{}}} ", &accelerator));
}
if let Some(command) = &self.command {
if let Some(command_variable) = &self.command_variable {
msg.push_str(&format!(
"-command {{ puts cb1b-{}-${} ; flush stdout }} ",
&command, &command_variable
));
msg.push_str(&format!("-variable {} ", &command_variable));
}
}
msg
}
pub fn add(&self) {
let msg = format!("{} add checkbutton {}", &self.parent, &self.option_string());
wish::tell_wish(&msg);
}
pub fn insert(&self, index: u64) {
let msg = format!(
"{} insert checkbutton {} {}",
&self.parent,
index,
&self.option_string()
);
wish::tell_wish(&msg);
}
}
impl TkMenuCommand {
pub fn accelerator(&mut self, key_definition: &str) -> &mut Self {
self.accelerator = Some(String::from(key_definition));
self
}
pub fn command(&mut self, command: impl Fn() + Send + 'static) -> &mut Self {
let id = wish::next_wid(".");
wish::add_callback0(&id, wish::mk_callback0(command));
self.command = Some(id);
self
}
pub fn compound(&mut self, value: widget::Compound) -> &mut Self {
self.compound = value;
self
}
pub fn font(&mut self, value: &str) -> &mut Self {
self.font = Some(String::from(value));
self
}
pub fn image(&mut self, image: &image::TkImage) -> &mut Self {
self.image = Some(String::from(&image.id));
self
}
pub fn label(&mut self, value: &str) -> &mut Self {
self.label = Some(String::from(value));
self
}
pub fn state(&mut self, value: widget::State) -> &mut Self {
self.state = value;
self
}
pub fn underline(&mut self, value: u64) -> &mut Self {
self.underline = Some(value);
self
}
fn option_string(&self) -> String {
let mut msg = common_option_string(
&self.compound,
&self.font,
&self.image,
&self.label,
&self.state,
&self.underline,
);
if let Some(accelerator) = &self.accelerator {
msg.push_str(&format!("-accelerator {{{}}} ", &accelerator));
}
if let Some(command) = &self.command {
msg.push_str(&format!(
"-command {{ puts clicked-{} ; flush stdout }} ",
&command
));
}
msg
}
pub fn add(&self) {
let msg = format!("{} add command {}", &self.parent, &self.option_string());
wish::tell_wish(&msg);
}
pub fn insert(&self, index: u64) {
let msg = format!(
"{} insert command {} {}",
&self.parent,
index,
&self.option_string()
);
wish::tell_wish(&msg);
}
}
impl TkMenuRadio {
pub fn accelerator(&mut self, key_definition: &str) -> &mut Self {
self.accelerator = Some(String::from(key_definition));
self
}
pub fn command(&mut self, command: impl Fn(bool) + Send + 'static) -> &mut Self {
let id = wish::next_wid(".");
let var = format!("::mcb{}", wish::current_id());
wish::add_callback1_bool(&id, wish::mk_callback1_bool(command));
self.command = Some(id);
self.command_variable = Some(var);
self
}
pub fn compound(&mut self, value: widget::Compound) -> &mut Self {
self.compound = value;
self
}
pub fn font(&mut self, value: &str) -> &mut Self {
self.font = Some(String::from(value));
self
}
pub fn image(&mut self, image: &image::TkImage) -> &mut Self {
self.image = Some(String::from(&image.id));
self
}
pub fn label(&mut self, value: &str) -> &mut Self {
self.label = Some(String::from(value));
self
}
pub fn state(&mut self, value: widget::State) -> &mut Self {
self.state = value;
self
}
pub fn underline(&mut self, value: u64) -> &mut Self {
self.underline = Some(value);
self
}
fn option_string(&self) -> String {
let mut msg = common_option_string(
&self.compound,
&self.font,
&self.image,
&self.label,
&self.state,
&self.underline,
);
msg.push_str(&format!(
"-variable ::mrb_group_{} -value {{{}}} ",
&self.group, &self.value
));
if let Some(accelerator) = &self.accelerator {
msg.push_str(&format!("-accelerator {{{}}} ", &accelerator));
}
if let Some(command) = &self.command {
if let Some(command_variable) = &self.command_variable {
msg.push_str(&format!(
"-command {{ puts cb1b-{}-{} ; flush stdout }} ",
&command, &command_variable
));
msg.push_str(&format!("-variable {{{}}} ", &command_variable));
}
}
msg
}
pub fn add(&self) {
let msg = format!("{} add radiobutton {}", &self.parent, &self.option_string());
wish::tell_wish(&msg);
}
pub fn insert(&self, index: u64) {
let msg = format!(
"{} insert radiobutton {} {}",
&self.parent,
index,
&self.option_string()
);
wish::tell_wish(&msg);
}
}
impl TkMenuSeparator {
pub fn add(&self) {
let msg = format!("{} add separator", &self.parent);
wish::tell_wish(&msg);
}
pub fn insert(&self, index: u64) {
let msg = format!("{} insert sepator {}", &self.parent, index);
wish::tell_wish(&msg);
}
}