use super::font;
use super::toplevel;
use super::widget;
use super::wish;
#[derive(Clone, Debug)]
pub struct TkMessageBox {
default: Option<String>,
detail: Option<String>,
icon: widget::IconImage,
message: Option<String>,
parent: Option<String>,
title: Option<String>,
type_buttons: widget::DialogType,
}
pub fn message_box() -> TkMessageBox {
TkMessageBox {
default: None,
detail: None,
icon: widget::IconImage::Error,
message: None,
parent: None,
title: None,
type_buttons: widget::DialogType::Ok,
}
}
impl TkMessageBox {
pub fn default(&mut self, name: &str) -> &mut Self {
self.default = Some(String::from(name));
self
}
pub fn detail(&mut self, text: &str) -> &mut Self {
self.detail = Some(String::from(text));
self
}
pub fn icon(&mut self, value: widget::IconImage) -> &mut Self {
self.icon = value;
self
}
pub fn message(&mut self, text: &str) -> &mut Self {
self.message = Some(String::from(text));
self
}
pub fn parent(&mut self, value: &toplevel::TkTopLevel) -> &mut Self {
self.parent = Some(String::from(&value.id));
self
}
pub fn title(&mut self, text: &str) -> &mut Self {
self.title = Some(String::from(text));
self
}
pub fn type_buttons(&mut self, value: widget::DialogType) -> &mut Self {
self.type_buttons = value;
self
}
pub fn show(&self) -> String {
let mut msg = String::from("puts [tk_messageBox ");
if let Some(default) = &self.default {
msg.push_str(&format!("-default {{{}}} ", default));
}
if let Some(detail) = &self.detail {
msg.push_str(&format!("-detail {{{}}} ", detail));
}
msg.push_str(&format!("-icon {} ", self.icon));
if let Some(message) = &self.message {
msg.push_str(&format!("-message {{{}}} ", message));
}
if let Some(parent) = &self.parent {
msg.push_str(&format!("-parent {} ", parent));
}
if let Some(title) = &self.title {
msg.push_str(&format!("-title {{{}}} ", title));
}
msg.push_str(&format!("-type {} ", self.type_buttons));
msg.push_str("] ; flush stdout");
wish::ask_wish(&msg)
}
}
#[derive(Clone, Debug)]
pub struct TkColourChooser {
parent: Option<String>,
title: Option<String>,
initial: Option<String>,
}
pub fn colour_chooser() -> TkColourChooser {
TkColourChooser {
parent: None,
title: None,
initial: None,
}
}
impl TkColourChooser {
pub fn parent(&mut self, value: &toplevel::TkTopLevel) -> &mut Self {
self.parent = Some(String::from(&value.id));
self
}
pub fn title(&mut self, text: &str) -> &mut Self {
self.title = Some(String::from(text));
self
}
pub fn initial_color(&mut self, value: &str) -> &mut Self {
self.initial_colour(value)
}
pub fn initial_colour(&mut self, value: &str) -> &mut Self {
self.initial = Some(String::from(value));
self
}
pub fn show(&self) -> Option<String> {
let mut msg = String::from("puts [tk_chooseColor ");
if let Some(parent) = &self.parent {
msg.push_str(&format!("-parent {} ", parent));
}
if let Some(title) = &self.title {
msg.push_str(&format!("-title {{{}}} ", title));
}
if let Some(initial) = &self.initial {
msg.push_str(&format!("-initialcolor {{{}}} ", initial));
}
msg.push_str("] ; flush stdout");
let result = wish::ask_wish(&msg);
if result.is_empty() {
None
} else {
Some(result)
}
}
}
#[derive(Clone, Debug)]
pub struct TkDirectoryChooser {
parent: Option<String>,
title: Option<String>,
initial: Option<String>,
must_exist: bool,
}
pub fn directory_chooser() -> TkDirectoryChooser {
TkDirectoryChooser {
parent: None,
title: None,
initial: None,
must_exist: false,
}
}
impl TkDirectoryChooser {
pub fn parent(&mut self, value: &toplevel::TkTopLevel) -> &mut Self {
self.parent = Some(String::from(&value.id));
self
}
pub fn title(&mut self, text: &str) -> &mut Self {
self.title = Some(String::from(text));
self
}
pub fn initial_directory(&mut self, value: &str) -> &mut Self {
self.initial = Some(String::from(value));
self
}
pub fn must_exist(&mut self, value: bool) -> &mut Self {
self.must_exist = value;
self
}
pub fn show(&self) -> Option<String> {
let mut msg = String::from("puts [tk_chooseDirectory ");
if let Some(parent) = &self.parent {
msg.push_str(&format!("-parent {} ", parent));
}
if let Some(title) = &self.title {
msg.push_str(&format!("-title {{{}}} ", title));
}
if let Some(initial) = &self.initial {
msg.push_str(&format!("-initialdir {{{}}} ", initial));
}
if self.must_exist {
msg.push_str("-mustexist 1 ");
}
msg.push_str("] ; flush stdout");
let result = wish::ask_wish(&msg);
if result.is_empty() {
None
} else {
Some(result)
}
}
}
#[derive(Clone, Debug)]
pub struct TkOpenFileChooser {
parent: Option<String>,
title: Option<String>,
file_types: Option<Vec<(String, String)>>,
initial_directory: Option<String>,
initial_filename: Option<String>,
}
pub fn open_file_chooser() -> TkOpenFileChooser {
TkOpenFileChooser {
parent: None,
title: None,
file_types: None,
initial_directory: None,
initial_filename: None,
}
}
impl TkOpenFileChooser {
pub fn parent(&mut self, value: &toplevel::TkTopLevel) -> &mut Self {
self.parent = Some(String::from(&value.id));
self
}
pub fn title(&mut self, text: &str) -> &mut Self {
self.title = Some(String::from(text));
self
}
pub fn file_types(&mut self, file_types: &[(&str, &str)]) -> &mut Self {
let mut types: Vec<(String, String)> = vec![];
for (txt, pat) in file_types {
types.push((String::from(*txt), String::from(*pat)));
}
self.file_types = Some(types);
self
}
pub fn initial_directory(&mut self, value: &str) -> &mut Self {
self.initial_directory = Some(String::from(value));
self
}
pub fn initial_filename(&mut self, value: &str) -> &mut Self {
self.initial_filename = Some(String::from(value));
self
}
pub fn show(&self) -> Option<String> {
let mut msg = String::from("puts [tk_getOpenFile ");
if let Some(parent) = &self.parent {
msg.push_str(&format!("-parent {} ", parent));
}
if let Some(title) = &self.title {
msg.push_str(&format!("-title {{{}}} ", title));
}
if let Some(types) = &self.file_types {
if !types.is_empty() {
msg.push_str("-filetypes {");
for (txt, pat) in types {
msg.push_str(&format!("{{{{{}}} {{{}}}}} ", *txt, *pat));
}
msg.push_str("} ");
}
}
if let Some(initial) = &self.initial_directory {
msg.push_str(&format!("-initialdir {{{}}} ", initial));
}
if let Some(initial) = &self.initial_filename {
msg.push_str(&format!("-initialfile {{{}}} ", initial));
}
msg.push_str("] ; flush stdout");
let result = wish::ask_wish(&msg);
if result.is_empty() {
None
} else {
Some(result)
}
}
}
#[derive(Clone, Debug)]
pub struct TkSaveFileChooser {
parent: Option<String>,
title: Option<String>,
confirm_overwrite: bool,
file_types: Option<Vec<(String, String)>>,
initial_directory: Option<String>,
initial_filename: Option<String>,
}
pub fn save_file_chooser() -> TkSaveFileChooser {
TkSaveFileChooser {
parent: None,
title: None,
confirm_overwrite: true,
file_types: None,
initial_directory: None,
initial_filename: None,
}
}
impl TkSaveFileChooser {
pub fn parent(&mut self, value: &toplevel::TkTopLevel) -> &mut Self {
self.parent = Some(String::from(&value.id));
self
}
pub fn title(&mut self, text: &str) -> &mut Self {
self.title = Some(String::from(text));
self
}
pub fn confirm_overwrite(&mut self, value: bool) -> &mut Self {
self.confirm_overwrite = value;
self
}
pub fn file_types(&mut self, file_types: &[(&str, &str)]) -> &mut Self {
let mut types: Vec<(String, String)> = vec![];
for (txt, pat) in file_types {
types.push((String::from(*txt), String::from(*pat)));
}
self.file_types = Some(types);
self
}
pub fn initial_directory(&mut self, value: &str) -> &mut Self {
self.initial_directory = Some(String::from(value));
self
}
pub fn initial_filename(&mut self, value: &str) -> &mut Self {
self.initial_filename = Some(String::from(value));
self
}
pub fn show(&self) -> Option<String> {
let mut msg = String::from("puts [tk_getSaveFile ");
if let Some(parent) = &self.parent {
msg.push_str(&format!("-parent {} ", parent));
}
if let Some(title) = &self.title {
msg.push_str(&format!("-title {{{}}} ", title));
}
msg.push_str(&format!(
"-confirmoverwrite {} ",
if self.confirm_overwrite { "1" } else { "0" }
));
if let Some(types) = &self.file_types {
if !types.is_empty() {
msg.push_str("-filetypes {");
for (txt, pat) in types {
msg.push_str(&format!("{{{{{}}} {{{}}}}} ", *txt, *pat));
}
msg.push_str("} ");
}
}
if let Some(initial) = &self.initial_directory {
msg.push_str(&format!("-initialdir {{{}}} ", initial));
}
if let Some(initial) = &self.initial_filename {
msg.push_str(&format!("-initialfile {{{}}} ", initial));
}
msg.push_str("] ; flush stdout");
let result = wish::ask_wish(&msg);
if result.is_empty() {
None
} else {
Some(result)
}
}
}
pub fn font_chooser_parent(parent: &impl widget::TkWidget) {
let msg = format!("tk fontchooser configure -parent {}", parent.id());
wish::tell_wish(&msg);
}
pub fn font_chooser_title(title: &str) {
let msg = format!("tk fontchooser configure -title {}", title);
wish::tell_wish(&msg);
}
pub fn font_chooser_command(command: impl Fn(font::TkFont) + Send + 'static) {
wish::add_callback1_font("font", wish::mk_callback1_font(command));
let msg = "tk fontchooser configure -command [list font_choice font]";
wish::tell_wish(msg);
}
pub fn font_chooser_font_get() -> String {
let msg = "tk fontchooser configure -font ";
wish::ask_wish(msg)
}
pub fn font_chooser_font_set(font: &str) {
let msg = format!("tk fontchooser configure -font {}", font);
wish::tell_wish(&msg);
}
pub fn font_chooser_hide() {
let msg = "tk fontchooser hide";
wish::tell_wish(msg);
}
pub fn font_chooser_show() {
let msg = "tk fontchooser show";
wish::tell_wish(msg);
}
pub fn font_chooser_visible() -> bool {
let msg = "tk fontchooser configure -visible ";
let result = wish::ask_wish(msg);
result == "1"
}