use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use crate::desktop::{TheCoordinate, ThePlaceOfTheV1, TheSize, TheStyleOfTheV1};
use crate::runtime::{Runtime, RuntimeError, Value};
fn the_answer_of_an_act(
rt: &mut Runtime<'_>,
name: &str,
answer: Result<Value, RuntimeError>,
) -> Result<Value, RuntimeError> {
match answer {
Ok(value) => Ok(value),
Err(failure) => {
rt.note(format!(
"the command {name} did not do the thing (line {}): {}",
failure.line, failure.message
));
Ok(Value::Text(String::new()))
}
}
}
fn the_answer_that_goes_on(
rt: &mut Runtime<'_>,
name: &str,
values: &[Value],
line: u32,
answer: Result<Value, RuntimeError>,
error_level: bool,
) -> Result<Value, RuntimeError> {
match answer {
Ok(value) => {
crate::v1::commands::write_the_answer(rt, name, values, 0, value.clone(), line)?;
if error_level {
rt.set_the_error_level(Value::Int(0));
}
Ok(value)
}
Err(failure) => {
crate::v1::commands::write_the_answer(
rt,
name,
values,
0,
Value::Text(String::new()),
line,
)?;
if error_level {
rt.set_the_error_level(Value::Int(1));
}
rt.note(format!(
"the command {name} did not do the thing (line {}): {}",
failure.line, failure.message
));
Ok(Value::Text(String::new()))
}
}
}
pub(crate) fn com_obj_create(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let class = values
.first()
.map(|value| value.to_text())
.unwrap_or_default();
let interface = values.get(1).map(|value| value.to_text());
let interface = interface.filter(|text| !text.is_empty());
let object = rt
.desktop()
.com_object_of_the_class(&class, interface.as_deref())
.map_err(|reason| RuntimeError {
message: reason,
line,
})?;
if interface.is_some() {
return Ok(Value::Int(object.pointer as i64));
}
Ok(crate::classes::comvalue::the_value(
object.pointer,
object.kind,
0,
))
}
pub(crate) fn com_object_of_the_v1(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
crate::classes::comvalue::construct(values, rt, line).map_err(|failure| RuntimeError {
message: crate::builtins::the_message_of(failure),
line,
})
}
pub(crate) fn obj_add_ref_of_the_v1(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let pointer = the_pointer_of_the_com(values);
let count = rt
.desktop()
.com_pointer_is_counted(pointer)
.map_err(|reason| RuntimeError {
message: reason,
line,
})?;
Ok(Value::Int(count as i64))
}
pub(crate) fn obj_release_of_the_v1(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let pointer = the_pointer_of_the_com(values);
let count = rt
.desktop()
.com_pointer_is_released(pointer)
.map_err(|reason| RuntimeError {
message: reason,
line,
})?;
Ok(Value::Int(count as i64))
}
fn the_pointer_of_the_com(values: &[Value]) -> u64 {
match values.first() {
Some(value) => crate::classes::comvalue::the_number_of(value)
.map(|number| number as u64)
.unwrap_or_else(|| value.to_number().unwrap_or(0.0) as u64),
None => 0,
}
}
pub(crate) fn com_obj_array(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let kind = values
.first()
.map(|value| value.to_text())
.unwrap_or_default()
.trim()
.parse::<i64>()
.unwrap_or(0);
let count = values
.get(1)
.map(|value| value.to_text())
.unwrap_or_default()
.trim()
.parse::<f64>()
.unwrap_or(0.0);
let object = rt
.desktop()
.com_value_is_made(kind, crate::desktop::ComValue::Number(count))
.map_err(|reason| RuntimeError {
message: reason,
line,
})?;
Ok(crate::classes::comvalue::the_value(
object.pointer,
object.kind,
0,
))
}
pub(crate) fn com_obj_error(
_rt: &mut Runtime<'_>,
values: &[Value],
_line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let before = crate::settings::com_obj_error();
if let Some(setting) = values.first() {
let on = match setting {
Value::Int(number) => *number != 0,
Value::Float(number) => *number != 0.0,
other => !other.to_text().is_empty(),
};
crate::settings::set_com_obj_error(on);
}
Ok(Value::Int(if before { 1 } else { 0 }))
}
pub(crate) fn control(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let sub_command = value_of(values, 0);
let value = value_of(values, 1);
let control = value_of(values, 2);
let title = value_of(values, 3);
let mut rest: Vec<Value> = vec![Value::Text(control.clone()), Value::Text(title.clone())];
rest.extend(values.iter().skip(4).cloned());
let mut with_the_style: Vec<Value> = vec![Value::Text(value.clone())];
with_the_style.extend(rest.iter().cloned());
let answer = match sub_command.to_ascii_lowercase().as_str() {
"enable" => crate::builtins::control_set_enabled(rt, &rest, line, 0),
"disable" => crate::builtins::control_set_enabled(rt, &rest, line, 0),
"hide" => crate::builtins::control_visibility(rt, &rest, line, 0),
"show" => crate::builtins::control_show(rt, &rest, line, 0),
"focus" => crate::builtins::control_act(rt, &rest, line, 1),
"move" => crate::builtins::control_move(rt, &rest, line, 0),
"click" => crate::builtins::control_act(rt, &rest, line, 0),
"send" => crate::builtins::control_send(rt, &rest, line, 0),
"sendtext" => crate::builtins::control_send_text(rt, &rest, line, 0),
"style" => crate::builtins::control_set_style(rt, &with_the_style, line, 0),
"exstyle" => crate::builtins::control_set_style(rt, &with_the_style, line, 1),
"showdropdown" => crate::builtins::control_show_drop_down(rt, &rest, line, 0),
"hidedropdown" => crate::builtins::control_hide_drop_down(rt, &rest, line, 0),
"check" => crate::builtins::control_set_checked(rt, &rest, line, 0),
"uncheck" => crate::builtins::control_set_checked(rt, &rest, line, 0),
_ => Err(RuntimeError {
message: format!("the sub-command {sub_command} of the Control is not known."),
line,
}),
};
let answer = match sub_command.to_ascii_lowercase().as_str() {
"enable" | "disable" | "check" | "uncheck" => {
let mut with_value = rest.clone();
with_value.insert(0, Value::Text(value.clone()));
match sub_command.to_ascii_lowercase().as_str() {
"enable" => crate::builtins::control_set_enabled(rt, &with_value, line, 0),
"disable" => crate::builtins::control_set_enabled(rt, &with_value, line, 0),
"check" => crate::builtins::control_set_checked(rt, &with_value, line, 0),
_ => crate::builtins::control_set_checked(rt, &with_value, line, 0),
}
}
_ => answer,
};
the_answer_of_an_act(rt, "Control", answer)
}
pub(crate) fn drive(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let sub_command = value_of(values, 0);
let value = value_of(values, 1);
let answer = match sub_command.to_ascii_lowercase().as_str() {
"label" => crate::builtins::drive_set_label(rt, &[Value::Text(value)], line, 0),
"lock" => crate::builtins::drive_lock(rt, &[Value::Text(value)], line, 0),
"unlock" => crate::builtins::drive_unlock(rt, &[Value::Text(value)], line, 0),
"eject" => crate::builtins::drive_eject(rt, &[Value::Text(value)], line, 0),
"retract" => crate::builtins::drive_retract(rt, &[Value::Text(value)], line, 0),
_ => Err(RuntimeError {
message: format!("the sub-command {sub_command} of the Drive is not known."),
line,
}),
};
the_answer_of_an_act(rt, "Drive", answer)
}
pub(crate) fn gui(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let sub_command = value_of(values, 0);
let trimmed = sub_command.trim();
let (name, sub) = match trimmed.split_once(':') {
Some((name, rest)) => (name.trim().to_string(), rest.trim().to_string()),
None => (String::new(), trimmed.to_string()),
};
rt.the_script_asked_to_stay(true);
let window = the_window_of_the_gui(rt, &name, line)?;
let key = sub.to_ascii_lowercase();
if the_sub_command_makes_the_window(&key) {
rt.desktop()
.v1_gui_open(&window, "")
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
if !window.is_empty()
&& rt.the_default_window_of_the_v1().is_empty()
&& !rt.desktop().v1_gui_is_open("").unwrap_or(false)
{
rt.remember_the_default_window_of_the_v1(&window);
}
}
let answer = if sub.starts_with('+') || sub.starts_with('-') {
the_options_of_the_window_are_applied(rt, &window, &sub, line)
} else {
match sub.to_ascii_lowercase().as_str() {
"new" => {
let title = value_of(values, 1);
let _ = rt.desktop().v1_gui_destroy(&window);
rt.desktop()
.v1_gui_open(&window, title.trim())
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
rt.remember_the_default_window_of_the_v1(&window);
Ok(Value::Text(String::new()))
}
"add" => {
let kind = the_kind_of_the_machine(&value_of(values, 1));
let options = value_of(values, 2);
let text = value_of(values, 3);
let variable = the_variable_of_the_options(&options);
let style = the_style_of_the_add_options(&options, &kind, text.trim());
let added = rt .desktop()
.v1_gui_add(&window, &kind, &variable, text.trim(), style)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
match kind.as_str() {
"listview" => rt.remember_the_listview_of_the_v1(&window, added.0),
"treeview" => rt.remember_the_treeview_of_the_v1(&window, added.0),
_ => {}
}
let place = the_place_of_the_add_options(&options, &kind);
if place.x.is_some()
|| place.y.is_some()
|| place.width.is_some()
|| place.height.is_some()
|| place.section
|| kind == "picture"
{
let _ = rt.desktop().v1_gui_place(&window, added.0, place);
}
let handle_variable = the_handle_variable_of_the_options(&options);
if !handle_variable.is_empty() {
if let Ok(handle) = rt.desktop().v1_gui_handle(&window, added.0) {
the_handle_variable_is_written(rt, &handle_variable, handle);
}
}
Ok(Value::Text(added.0.to_string()))
}
"listview" => {
let name = value_of(values, 1);
let number = rt
.desktop()
.v1_gui_number_named(&window, name.trim())
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
rt.remember_the_listview_of_the_v1(&window, number as u64);
Ok(Value::Text(String::new()))
}
"treeview" => {
let name = value_of(values, 1);
let number = rt
.desktop()
.v1_gui_number_named(&window, name.trim())
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
rt.remember_the_treeview_of_the_v1(&window, number as u64);
Ok(Value::Text(String::new()))
}
"show" => {
let options = value_of(values, 1);
let title = value_of(values, 2);
let (x, y, width, height) = the_place_of_the_options(&options);
let (centered_x, centered_y) = the_center_of_the_options(&options);
let mode = the_mode_of_the_options(&options);
rt.desktop()
.v1_gui_window_place(&window, x, y, width, height, centered_x, centered_y)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
if the_options_say_auto_size(&options) {
rt.desktop()
.v1_gui_auto_size(&window)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
}
if !title.trim().is_empty() && mode != TheMode::Show {
rt.desktop()
.v1_gui_title(&window, Some(title.trim()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
}
match mode {
TheMode::Show => {
rt.desktop()
.v1_gui_show(&window, title.trim())
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
}
TheMode::ShowWithoutActivating => {
rt.desktop()
.v1_gui_show_without_activating(&window, title.trim())
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
}
TheMode::Hide => {
rt.desktop()
.v1_gui_hide(&window)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
}
TheMode::Maximize | TheMode::Minimize | TheMode::Restore => {
let name = match mode {
TheMode::Maximize => "maximize",
TheMode::Minimize => "minimize",
_ => "restore",
};
let _ = rt.desktop().v1_gui_option(&window, name, None, true);
}
}
Ok(Value::Text(String::new()))
}
"hide" => rt
.desktop()
.v1_gui_hide(&window)
.map(|_| Value::Text(String::new()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line)),
"destroy" => {
let _ = rt.desktop().v1_gui_destroy(&window);
Ok(Value::Text(String::new()))
}
"menu" => {
let name = value_of(values, 1);
let items = crate::v1::menus::the_items_of_the_menu(rt, &name);
let machine = crate::v1::menus::the_machine_menu_of(rt, &name);
rt.desktop()
.v1_gui_menu_bar(&window, &name, &items, machine.unwrap_or(0))
.map(|_| Value::Text(String::new()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
"color" => {
let color = value_of(values, 1);
rt.desktop()
.v1_gui_color(&window, color.trim())
.map(|_| Value::Text(String::new()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
"margin" => {
let x = value_of(values, 1).trim().parse::<i64>().unwrap_or(0);
let y = value_of(values, 2).trim().parse::<i64>().unwrap_or(0);
rt.desktop()
.v1_gui_margin(&window, x, y)
.map(|_| Value::Text(String::new()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
"font" => {
let options = value_of(values, 1);
let name = value_of(values, 2);
rt.desktop()
.v1_gui_font(&window, options.trim(), name.trim())
.map(|_| Value::Text(String::new()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
"submit" => {
let answer = rt
.desktop()
.v1_gui_submit(&window)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
for (name, text) in answer {
rt.assign_the_variable_of_the_pseudo_array(&name, Value::Text(text));
}
let options = value_of(values, 1);
if !options
.split_whitespace()
.any(|word| word.eq_ignore_ascii_case("nohide"))
{
let _ = rt.desktop().v1_gui_hide(&window);
}
Ok(Value::Text(String::new()))
}
"cancel" => rt
.desktop()
.v1_gui_hide(&window)
.map(|_| Value::Text(String::new()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line)),
"default" => {
rt.remember_the_default_window_of_the_v1(&window);
Ok(Value::Text(String::new()))
}
"minimize" => {
let _ = rt.desktop().v1_gui_option(&window, "minimize", None, true);
Ok(Value::Text(String::new()))
}
"maximize" => {
let _ = rt.desktop().v1_gui_option(&window, "maximize", None, true);
Ok(Value::Text(String::new()))
}
"restore" => {
let _ = rt.desktop().v1_gui_option(&window, "restore", None, true);
Ok(Value::Text(String::new()))
}
"flash" => {
Ok(Value::Text(String::new()))
}
"tab" => {
let tab = value_of(values, 1).trim().parse::<i64>().unwrap_or(0);
rt.desktop()
.v1_gui_tab(&window, tab)
.map(|_| Value::Text(String::new()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
_ => Err(RuntimeError {
message: format!("the sub-command {sub_command} of the Gui is not known."),
line,
}),
}
};
the_answer_of_an_act(rt, "Gui", answer)
}
fn the_sub_command_makes_the_window(sub: &str) -> bool {
!matches!(
sub,
"new" | "destroy" | "submit" | "cancel" | "hide" | "minimize" | "maximize" | "restore"
| "flash" | "default"
)
}
fn the_window_of_the_gui(
rt: &mut Runtime<'_>,
name: &str,
_line: u32,
) -> Result<String, RuntimeError> {
if name.is_empty() {
return Ok(rt.the_default_window_of_the_v1().to_string());
}
if rt.desktop().v1_gui_is_open(name).unwrap_or(false) {
return Ok(name.to_string());
}
if let Some(handle) = the_handle_of_the_v1(name) {
if let Ok(found) = rt.desktop().v1_gui_name_of_handle(handle) {
return Ok(found);
}
}
Ok(name.to_string())
}
fn the_handle_of_the_v1(text: &str) -> Option<i64> {
let text = text.trim();
if let Some(hex) = text.strip_prefix("0x").or_else(|| text.strip_prefix("0X")) {
return i64::from_str_radix(hex, 16).ok();
}
if !text.is_empty() && text.chars().all(|character| character.is_ascii_digit()) {
return text.parse::<i64>().ok();
}
None
}
fn the_options_of_the_window_are_applied(
rt: &mut Runtime<'_>,
window: &str,
text: &str,
line: u32,
) -> Result<Value, RuntimeError> {
for word in text.split_whitespace() {
let Some(sign) = word.chars().next() else {
continue;
};
let on = match sign {
'+' => true,
'-' => false,
_ => continue,
};
let rest = &word[sign.len_utf8()..];
if let Some((extended, style)) = the_style_without_a_name(rest) {
rt.desktop()
.v1_gui_style(window, extended, style, on)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
continue;
}
let lower = rest.to_ascii_lowercase();
let Some((name, length)) = the_option_of_the_window(&lower) else {
return Err(RuntimeError {
message: format!("the option {rest} of the window is not one that the v1 knows."),
line,
});
};
let value = &rest[length..];
the_option_of_the_window_is_applied(rt, window, name, value, on, line)?;
}
Ok(Value::Text(String::new()))
}
const THE_OPTIONS_OF_A_WINDOW: &[(&str, bool)] = &[
("lastfoundexist", false),
("alwaysontop", false),
("maximizebox", false),
("minimizebox", false),
("delimiter", true),
("owndialogs", false),
("toolwindow", false),
("lastfound", false),
("disabled", false),
("dpiscale", false),
("minsize", true),
("maxsize", true),
("sysmenu", false),
("caption", false),
("resize", false),
("border", false),
("parent", true),
("owner", true),
("label", true),
("theme", false),
("hwnd", true),
];
fn the_option_of_the_window(text: &str) -> Option<(&'static str, usize)> {
for (name, takes_value) in THE_OPTIONS_OF_A_WINDOW {
if let Some(value) = text.strip_prefix(name) {
if !*takes_value && !value.is_empty() {
continue;
}
return Some((name, name.len()));
}
}
None
}
fn the_style_without_a_name(text: &str) -> Option<(bool, u32)> {
let (extended, digits) = if let Some(digits) = text
.strip_prefix("0x")
.or_else(|| text.strip_prefix("0X"))
{
(false, digits)
} else {
let digits = text
.strip_prefix("E0x")
.or_else(|| text.strip_prefix("E0X"))
.or_else(|| text.strip_prefix("e0x"))
.or_else(|| text.strip_prefix("e0X"))?;
(true, digits)
};
let style = u32::from_str_radix(digits, 16).ok()?;
Some((extended, style))
}
fn the_option_of_the_window_is_applied(
rt: &mut Runtime<'_>,
window: &str,
name: &str,
value: &str,
on: bool,
line: u32,
) -> Result<(), RuntimeError> {
match name {
"hwnd" => {
if value.is_empty() {
return Ok(());
}
let handle = rt
.desktop()
.v1_gui_window_handle(window)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
the_handle_variable_is_written(rt, value, handle);
Ok(())
}
"owner" | "parent" => {
let owner = if on { Some(value) } else { None };
rt.desktop()
.v1_gui_owner(window, owner, name == "parent")
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
"lastfound" => {
let handle = rt
.desktop()
.v1_gui_window_handle(window)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
rt.remember_the_last_window_of_the_v1(&handle.to_string());
Ok(())
}
"lastfoundexist" => {
if rt.desktop().v1_gui_is_open(window).unwrap_or(false) {
let handle = rt
.desktop()
.v1_gui_window_handle(window)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
rt.remember_the_last_window_of_the_v1(&handle.to_string());
}
Ok(())
}
"owndialogs" => Ok(()),
"dpiscale" => Ok(()),
_ => rt
.desktop()
.v1_gui_option(
window,
name,
if value.is_empty() { None } else { Some(value) },
on,
)
.map_err(|reason| crate::builtins::desktop_failure(reason, line)),
}
}
fn the_center_of_the_options(options: &str) -> (bool, bool) {
let mut horizontal = false;
let mut vertical = false;
for word in options.split_whitespace() {
match word.to_ascii_lowercase().as_str() {
"center" => {
horizontal = true;
vertical = true;
}
"xcenter" => horizontal = true,
"ycenter" => vertical = true,
_ => {}
}
}
(horizontal, vertical)
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum TheMode {
Show,
ShowWithoutActivating,
Hide,
Minimize,
Maximize,
Restore,
}
fn the_mode_of_the_options(options: &str) -> TheMode {
let mut mode = TheMode::Show;
for word in options.split_whitespace() {
match word.to_ascii_lowercase().as_str() {
"maximize" => mode = TheMode::Maximize,
"minimize" => mode = TheMode::Minimize,
"restore" => mode = TheMode::Restore,
"hide" => mode = TheMode::Hide,
"noactivate" | "na" => mode = TheMode::ShowWithoutActivating,
_ => {}
}
}
mode
}
fn the_options_say_auto_size(options: &str) -> bool {
options
.split_whitespace()
.any(|word| word.eq_ignore_ascii_case("autosize"))
}
fn the_variable_of_the_options(options: &str) -> String {
for word in options.split_whitespace() {
let Some(name) = word.strip_prefix('v').or_else(|| word.strip_prefix('V')) else {
continue;
};
if !name.is_empty() && !name.starts_with('+') {
return name.to_string();
}
}
String::new()
}
fn the_handle_variable_of_the_options(options: &str) -> String {
for word in options.split_whitespace() {
let word = word
.strip_prefix('+')
.or_else(|| word.strip_prefix('-'))
.unwrap_or(word);
if word.len() <= 4
|| !word
.get(..4)
.is_some_and(|head| head.eq_ignore_ascii_case("hwnd"))
{
continue;
}
return word[4..].to_string();
}
String::new()
}
fn the_handle_variable_is_written(rt: &mut Runtime<'_>, name: &str, handle: i64) {
let global_of_that_name = rt
.variable_names()
.iter()
.any(|(other, _)| other.eq_ignore_ascii_case(name));
if global_of_that_name {
rt.set_global(name, Value::Int(handle));
return;
}
rt.assign_the_variable_of_the_pseudo_array(name, Value::Int(handle));
}
fn the_place_of_the_options(options: &str) -> (Option<i64>, Option<i64>, Option<i64>, Option<i64>) {
let mut x = None;
let mut y = None;
let mut width = None;
let mut height = None;
for word in options.split_whitespace() {
let Some(letter) = word.chars().next() else {
continue;
};
let rest = &word[letter.len_utf8()..];
let Some(value) = the_number_of_an_option(rest) else {
continue;
};
match letter.to_ascii_lowercase() {
'w' => width = Some(value),
'h' => height = Some(value),
'x' => x = Some(value),
'y' => y = Some(value),
_ => {}
}
}
(x, y, width, height)
}
fn the_number_of_an_option(text: &str) -> Option<i64> {
let first = text.chars().next()?;
if !(first.is_ascii_digit() || first == '-' || first == '+') {
return None;
}
Some(the_number_of(text))
}
fn the_place_of_the_add_options(options: &str, kind: &str) -> ThePlaceOfTheV1 {
let mut place = ThePlaceOfTheV1::default();
for word in options.split_whitespace() {
let word = word
.strip_prefix('+')
.or_else(|| word.strip_prefix('-'))
.unwrap_or(word);
let Some(letter) = word.chars().next() else {
continue;
};
if the_option_has_the_name_of_another_option(word, kind) {
continue;
}
let rest = &word[letter.len_utf8()..];
match letter.to_ascii_lowercase() {
'x' => {
if let Some(coordinate) = the_coordinate_of_the_options(rest) {
place.x = Some(coordinate);
}
}
'y' => {
if let Some(coordinate) = the_coordinate_of_the_options(rest) {
place.y = Some(coordinate);
}
}
'w' => {
if let Some(size) = the_size_of_the_options(rest) {
place.width = Some(size);
}
}
'h' => {
if let Some(size) = the_size_of_the_options(rest) {
place.height = Some(size);
}
}
'r' => {
if let Ok(rows) = rest.parse::<i64>() {
if rows > 0 {
place.rows = Some(rows);
}
}
}
's' if rest.eq_ignore_ascii_case("ection") => {
place.section = true;
}
_ => {}
}
}
place
}
fn the_option_has_the_name_of_another_option(word: &str, kind: &str) -> bool {
let word = word.to_ascii_lowercase();
if word.starts_with("hwnd") || word.starts_with("hscroll") || word.starts_with("hidden") {
return true;
}
if word == "wrap" {
return true;
}
if word == "hdr" {
return kind == "listview";
}
if word == "horz" {
return kind == "updown";
}
false
}
const BS_MULTILINE: u32 = 0x2000;
fn the_style_of_the_add_options(options: &str, kind: &str, text: &str) -> TheStyleOfTheV1 {
let mut style = TheStyleOfTheV1::default();
for word in options.split_whitespace() {
let (adding, digits) = match word.as_bytes().first() {
Some(b'+') => (true, &word[1..]),
Some(b'-') => (false, &word[1..]),
_ => (true, word),
};
if digits.len() > 4 && digits[..4].eq_ignore_ascii_case("icon") {
if let Ok(number) = digits[4..].parse::<u32>() {
style.icon = Some(number);
}
continue;
}
if digits.len() >= 7 && digits[..7].eq_ignore_ascii_case("checked") {
let rest = &digits[7..];
if rest.eq_ignore_ascii_case("gray") {
style.checked = Some(if adding { 2 } else { 0 });
} else if rest.is_empty() {
style.checked = Some(if adding { 1 } else { 0 });
} else if let Ok(number) = rest.parse::<i64>() {
style.checked = Some(if number == -1 { 2 } else { number });
}
continue;
}
if digits.len() > 6 && digits[..6].eq_ignore_ascii_case("choose") {
if let Ok(number) = digits[6..].parse::<i64>() {
style.choose = Some(number);
}
continue;
}
if digits.eq_ignore_ascii_case("readonly") && kind == "edit" {
const ES_READONLY: u32 = 0x0800;
if adding {
style.add |= ES_READONLY;
} else {
style.remove |= ES_READONLY;
}
continue;
}
if digits.eq_ignore_ascii_case("hide") || digits.eq_ignore_ascii_case("hidden") {
const WS_VISIBLE: u32 = 0x1000_0000;
if adding {
style.remove |= WS_VISIBLE;
} else {
style.add |= WS_VISIBLE;
}
continue;
}
if digits.eq_ignore_ascii_case("disabled") {
const WS_DISABLED: u32 = 0x0800_0000;
if adding {
style.add |= WS_DISABLED;
} else {
style.remove |= WS_DISABLED;
}
continue;
}
if digits.eq_ignore_ascii_case("tabstop") {
const WS_TABSTOP: u32 = 0x0001_0000;
if adding {
style.add |= WS_TABSTOP;
} else {
style.remove |= WS_TABSTOP;
}
continue;
}
if digits.eq_ignore_ascii_case("border") {
const WS_BORDER: u32 = 0x0080_0000;
if adding {
style.add |= WS_BORDER;
} else {
style.remove |= WS_BORDER;
}
continue;
}
if digits.eq_ignore_ascii_case("default") && kind == "button" {
const BS_DEFPUSHBUTTON: u32 = 0x0001;
if adding {
style.add |= BS_DEFPUSHBUTTON;
} else {
style.remove |= BS_DEFPUSHBUTTON;
}
continue;
}
if kind == "edit" {
if digits.eq_ignore_ascii_case("multi") {
const ES_MULTILINE: u32 = 0x0004;
if adding {
style.add |= ES_MULTILINE;
} else {
style.remove |= ES_MULTILINE;
}
continue;
}
if digits.eq_ignore_ascii_case("number") {
const ES_NUMBER: u32 = 0x2000;
if adding {
style.add |= ES_NUMBER;
} else {
style.remove |= ES_NUMBER;
}
continue;
}
if adding && (digits.starts_with('r') || digits.starts_with('R')) {
if let Ok(rows) = digits[1..].parse::<i64>() {
if rows > 1 {
const ES_MULTILINE: u32 = 0x0004;
const ES_WANTRETURN: u32 = 0x1000;
const WS_VSCROLL: u32 = 0x0020_0000;
style.add |= ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL;
}
continue;
}
}
}
if adding
&& digits.len() > 1
&& (digits.starts_with('g') || digits.starts_with('G'))
&& !digits.eq_ignore_ascii_case("group")
&& !digits.eq_ignore_ascii_case("grid")
{
style.glabel = digits[1..].to_string();
if matches!(kind, "text" | "picture") {
const SS_NOTIFY: u32 = 0x0100;
style.add |= SS_NOTIFY;
}
continue;
}
let Some(bits) = the_number_of_a_style(digits) else {
continue;
};
if adding {
style.add |= bits;
} else {
style.remove |= bits;
}
}
if matches!(kind, "button" | "checkbox" | "radio") {
let place = the_place_of_the_add_options(options, kind);
if place.width.is_some()
|| place.height.is_some()
|| text.contains('\n')
|| text.contains('\r')
{
style.add |= BS_MULTILINE;
}
}
style
}
fn the_number_of_a_style(text: &str) -> Option<u32> {
let text = text.trim();
if text.is_empty() {
return None;
}
if let Some(digits) = text.strip_prefix("0x").or_else(|| text.strip_prefix("0X")) {
if digits.is_empty() || !digits.chars().all(|digit| digit.is_ascii_hexdigit()) {
return None;
}
return u32::from_str_radix(digits, 16).ok();
}
if !text.chars().all(|digit| digit.is_ascii_digit()) {
return None;
}
text.parse::<u32>().ok()
}
fn is_signed_number_prefix(text: &str) -> bool {
let bytes = text.as_bytes();
match bytes.first() {
Some(b) if b.is_ascii_digit() => true,
Some(b'+' | b'-') => bytes.get(1).is_some_and(|b| b.is_ascii_digit()),
_ => false,
}
}
fn the_coordinate_of_the_options(rest: &str) -> Option<TheCoordinate> {
if let Some(after) = rest.strip_prefix('+') {
if after.eq_ignore_ascii_case("m") {
return Some(TheCoordinate::AfterThePreviousWithTheMargin);
}
if !is_signed_number_prefix(after) {
return None;
}
return Some(TheCoordinate::AfterThePrevious(the_number_of(after)));
}
let mut characters = rest.chars();
match characters.next() {
Some('m') | Some('M') => {
let tail = characters.as_str();
if tail.is_empty() || is_signed_number_prefix(tail) {
Some(TheCoordinate::FromTheMargin(the_number_of(tail)))
} else {
None
}
}
Some('p') | Some('P') => {
let tail = characters.as_str();
if tail.is_empty() || is_signed_number_prefix(tail) {
Some(TheCoordinate::FromThePrevious(the_number_of(tail)))
} else {
None
}
}
Some('s') | Some('S') => {
let tail = characters.as_str();
if tail.is_empty() || is_signed_number_prefix(tail) {
Some(TheCoordinate::FromTheSection(the_number_of(tail)))
} else {
None
}
}
_ => {
if is_signed_number_prefix(rest) {
Some(TheCoordinate::Absolute(the_number_of(rest)))
} else {
None
}
}
}
}
fn the_size_of_the_options(rest: &str) -> Option<TheSize> {
let mut characters = rest.chars();
match characters.next() {
Some('p') | Some('P') => {
let tail = characters.as_str();
if tail.is_empty() || is_signed_number_prefix(tail) {
Some(TheSize::FromThePrevious(the_number_of(tail)))
} else {
None
}
}
_ => {
if is_signed_number_prefix(rest) {
Some(TheSize::Absolute(the_number_of(rest)))
} else {
None
}
}
}
}
fn the_number_of(text: &str) -> i64 {
let mut characters = text.chars().peekable();
let mut number = String::new();
if let Some(first) = characters.peek() {
if *first == '-' || *first == '+' {
number.push(*first);
characters.next();
}
}
while let Some(one) = characters.peek() {
if one.is_ascii_digit() {
number.push(*one);
characters.next();
} else {
break;
}
}
number.parse::<i64>().unwrap_or(0)
}
fn the_kind_of_the_machine(kind: &str) -> String {
let kind = kind.trim().to_ascii_lowercase();
match kind.as_str() {
"dropdownlist" => "ddl".to_string(),
"trackbar" => "slider".to_string(),
"tab2" | "tab3" => "tab".to_string(),
"pic" => "picture".to_string(),
_ => kind,
}
}
pub(crate) fn gui_control(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let sub_command = value_of(values, 0);
let control = value_of(values, 1);
let value = value_of(values, 2);
let (name, sub) = the_name_and_the_sub_command(&sub_command);
let window = the_window_of_the_gui(rt, &name, line)?;
let answer = (|| -> Result<Value, RuntimeError> {
match sub.to_ascii_lowercase().as_str() {
"" | "text" => {
let number = the_number_of_a_control(rt, &window, &control, line)?;
rt.desktop()
.v1_gui_value(&window, number as u64, Some(&value))
.map(Value::Text)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
"move" | "movedraw" => {
let number = the_number_of_a_control(rt, &window, &control, line)?;
let place = the_place_of_the_add_options(&value, "");
rt.desktop()
.v1_gui_place(&window, number as u64, place)
.map(|_| Value::Text(String::new()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
"focus" => {
let number = the_number_of_a_control(rt, &window, &control, line)?;
rt.desktop()
.v1_gui_focus(&window, number as u64)
.map(|_| Value::Text(String::new()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
"enable" => the_state_of_a_control(rt, &window, &control, "enabled", true, line),
"disable" => the_state_of_a_control(rt, &window, &control, "enabled", false, line),
"hide" => {
let number = the_number_of_a_control(rt, &window, &control, line)?;
rt.desktop()
.v1_gui_visible(&window, number as u64, false)
.map(|_| Value::Text(String::new()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
"show" => {
let number = the_number_of_a_control(rt, &window, &control, line)?;
rt.desktop()
.v1_gui_visible(&window, number as u64, true)
.map(|_| Value::Text(String::new()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
"choose" => {
let number = the_number_of_a_control(rt, &window, &control, line)?;
rt.desktop()
.v1_gui_choose(&window, number as u64, &value, false)
.map(|index| Value::Text(index.to_string()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
"choosestring" => {
let number = the_number_of_a_control(rt, &window, &control, line)?;
rt.desktop()
.v1_gui_choose(&window, number as u64, &value, true)
.map(|index| Value::Text(index.to_string()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
"+redraw" | "-redraw" => {
Ok(Value::Text(String::new()))
}
"font" => Err(RuntimeError {
message: the_reason_of_a_gui_control_that_the_tree_does_not_carry(sub.trim()),
line,
}),
"delete" => Err(RuntimeError {
message: "the v1 does not have the delete of a control: the CHM of the v1.1.37.02 says \
of the sub-command: \"Delete : Not yet implemented.\""
.to_string(),
line,
}),
_ => Err(RuntimeError {
message: format!("the sub-command {sub_command} of the GuiControl is not known."),
line,
}),
}
})();
the_answer_of_an_act(rt, "GuiControl", answer)
}
fn the_name_and_the_sub_command(text: &str) -> (String, String) {
match text.trim().split_once(':') {
Some((name, rest)) => (name.trim().to_string(), rest.trim().to_string()),
None => (String::new(), text.trim().to_string()),
}
}
fn the_reason_of_a_gui_control_that_the_tree_does_not_carry(sub_command: &str) -> String {
match sub_command.to_ascii_lowercase().as_str() {
"font" => "the tree does not carry the font of a control of the window of the script: the machine of the tree has no font for it.".to_string(),
_ => "the tree does not move a control of the window of the script: the machine of the tree lays the controls out its own way.".to_string(),
}
}
fn the_number_of_a_control(
rt: &mut Runtime<'_>,
window: &str,
control: &str,
line: u32,
) -> Result<i64, RuntimeError> {
rt.desktop()
.v1_gui_number_named(window, control)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
fn the_state_of_a_control(
rt: &mut Runtime<'_>,
window: &str,
control: &str,
what: &str,
on: bool,
line: u32,
) -> Result<Value, RuntimeError> {
let number = the_number_of_a_control(rt, window, control, line)?;
rt.desktop()
.v1_gui_flag(window, number as u64, what, Some(on))
.map(|_| Value::Text(String::new()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
pub(crate) fn gui_control_get(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let output = value_of(values, 0);
let sub_command = value_of(values, 1);
let mut control = value_of(values, 2);
let value = value_of(values, 3);
let (name, sub) = the_name_and_the_sub_command(&sub_command);
let window = the_window_of_the_gui(rt, &name, line)?;
if control.trim().is_empty() {
control = output.clone();
}
let lower = sub.to_ascii_lowercase();
let control_is_named = lower != "focus" && lower != "focusv";
let window_is_there = rt.desktop().v1_gui_is_open(&window).unwrap_or(false);
let control_is_there = !control_is_named
|| rt
.desktop()
.v1_gui_number_named(&window, &control)
.is_ok();
if !window_is_there || !control_is_there {
crate::v1::commands::write_the_answer(
rt,
"GuiControlGet",
values,
0,
Value::Text(String::new()),
line,
)?;
rt.set_the_error_level(Value::Int(1));
return Ok(Value::Text(String::new()));
}
let answer = (|| -> Result<Value, RuntimeError> {
match sub.to_ascii_lowercase().as_str() {
"" => {
let number = the_number_of_a_control(rt, &window, &control, line)?;
if value.trim().eq_ignore_ascii_case("text") {
rt.desktop()
.v1_gui_caption(&window, number as u64)
.map(Value::Text)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
} else {
rt.desktop()
.v1_gui_value(&window, number as u64, None)
.map(Value::Text)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
}
"pos" => {
let number = the_number_of_a_control(rt, &window, &control, line)?;
let (x, y, width, height) = rt
.desktop()
.v1_gui_control_pos(&window, number as u64)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
rt.assign_the_variable(&format!("{output}X"), Value::Int(x));
rt.assign_the_variable(&format!("{output}Y"), Value::Int(y));
rt.assign_the_variable(&format!("{output}W"), Value::Int(width));
rt.assign_the_variable(&format!("{output}H"), Value::Int(height));
Ok(Value::Text(String::new()))
}
"focus" => rt
.desktop()
.v1_gui_focused(&window)
.map(Value::Text)
.map_err(|reason| crate::builtins::desktop_failure(reason, line)),
"focusv" => {
let classnn = rt
.desktop()
.v1_gui_focused(&window)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
if classnn.is_empty() {
return Ok(Value::Text(String::new()));
}
let number = rt
.desktop()
.v1_gui_number_named(&window, &classnn)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
let name = rt
.desktop()
.v1_gui_control_name(&window, number as u64)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
if !name.is_empty() {
return Ok(Value::Text(name));
}
let text = rt
.desktop()
.v1_gui_caption(&window, number as u64)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
Ok(Value::Text(text.chars().take(63).collect()))
}
"enabled" => {
let number = the_number_of_a_control(rt, &window, &control, line)?;
rt.desktop()
.v1_gui_flag(&window, number as u64, "enabled", None)
.map(Value::Int)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
"visible" => {
let number = the_number_of_a_control(rt, &window, &control, line)?;
rt.desktop()
.v1_gui_flag(&window, number as u64, "visible", None)
.map(Value::Int)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
"hwnd" => {
let number = the_number_of_a_control(rt, &window, &control, line)?;
let handle = rt
.desktop()
.v1_gui_handle(&window, number as u64)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
Ok(Value::Text(format!("0x{handle:x}")))
}
"name" => {
let number = the_number_of_a_control(rt, &window, &control, line)?;
rt.desktop()
.v1_gui_control_name(&window, number as u64)
.map(Value::Text)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
_ => Err(RuntimeError {
message: format!(
"the sub-command {sub_command} of the GuiControlGet is not known."
),
line,
}),
}
})();
the_answer_that_goes_on(rt, "GuiControlGet", values, line, answer, true)
}
pub(crate) fn input_hook(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let options = values
.first()
.map(|value| value.to_text())
.unwrap_or_default();
let end_keys = values.get(1).map(|value| value.to_text()).unwrap_or_default();
let match_list = values.get(2).map(|value| value.to_text()).unwrap_or_default();
let mut arguments: Vec<Value> = Vec::new();
if !options.trim().is_empty() {
arguments.push(Value::Text(the_options_of_the_v2(&options, line)?));
}
if !end_keys.trim().is_empty() {
arguments.push(Value::Text(end_keys));
}
if !match_list.trim().is_empty() {
arguments.push(Value::Text(match_list));
}
match crate::classes::construct("InputHook", &arguments, rt, line) {
Some(answer) => answer.map_err(|failure| RuntimeError {
message: crate::builtins::the_message_of(failure),
line,
}),
None => Err(RuntimeError {
message: "the class of the InputHook is not known.".to_string(),
line,
}),
}
}
fn the_options_of_the_v2(options: &str, line: u32) -> Result<String, RuntimeError> {
let mut carried = String::new();
for letter in options.split_whitespace() {
for one in letter.chars() {
if one.is_ascii_digit() || one == '-' || one == '+' || one == '.' {
carried.push(one);
continue;
}
let upper = one.to_ascii_uppercase();
match upper {
'B' | 'C' | 'E' | 'H' | 'I' | 'L' | 'M' | 'N' | 'O' | 'P' | 'R' | 'S' | 'T' | 'V'
| 'W' => carried.push(upper),
_ => {
return Err(RuntimeError {
message: format!("the option {one} of the InputHook is not known."),
line,
})
}
}
}
carried.push(' ');
}
Ok(carried.trim_end().to_string())
}
pub(crate) fn menu(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let name = value_of(values, 0);
let sub_command = value_of(values, 1);
let value = value_of(values, 2);
if name.trim().eq_ignore_ascii_case("tray") {
let answer = the_tray_of_the_script(rt, values, line);
return the_answer_of_an_act(rt, "Menu", answer);
}
let answer = match sub_command.trim().to_ascii_lowercase().as_str() {
"add" => { let callback = value_of(values, 3);
crate::v1::menus::add_item(rt, &name, &value, &callback, line)
}
"insert" => {
let callback = value_of(values, 3);
crate::v1::menus::insert_item(rt, &name, &value, &callback, line)
}
"delete" => {
if value.trim().is_empty() {
crate::v1::menus::delete_the_menu(rt, &name, line)
} else {
crate::v1::menus::delete_item(rt, &name, &value, line)
}
}
"deleteall" => crate::v1::menus::delete_all(rt, &name, line),
"disable" => crate::v1::menus::set_enabled(rt, &name, &value, false, line),
"enable" => crate::v1::menus::set_enabled(rt, &name, &value, true, line),
"toggleenable" => crate::v1::menus::toggle_enabled(rt, &name, &value, line),
"check" => crate::v1::menus::set_checked(rt, &name, &value, true, line),
"uncheck" => crate::v1::menus::set_checked(rt, &name, &value, false, line),
"togglecheck" => crate::v1::menus::toggle_checked(rt, &name, &value, line),
"default" => crate::v1::menus::set_default(rt, &name, &value, line),
"nodefault" => crate::v1::menus::no_default(rt, &name, line),
"rename" => {
let new_name = value_of(values, 2);
let old_name = value_of(values, 3);
crate::v1::menus::rename_item(rt, &name, &old_name, &new_name, line)
}
"icon" => {
let item = value_of(values, 2);
let icon = value_of(values, 3);
let number = value_of(values, 4).trim().parse::<i64>().unwrap_or(1);
crate::v1::menus::set_icon(rt, &name, &item, &icon, number, line)
}
"show" => {
let x = value_of(values, 2);
let y = value_of(values, 3);
crate::v1::menus::show(rt, &name, &x, &y, line)
}
"useerrorlevel" => Ok(Value::Text(String::new())),
"standard" | "nostandard" | "noicon" | "tip" | "color" | "click" | "mainwindow"
| "nomainwindow" => Err(RuntimeError {
message: the_reason_of_a_menu_that_the_tree_does_not_carry(sub_command.trim()),
line,
}),
_ => Err(RuntimeError {
message: format!("the sub-command {sub_command} of the Menu is not known."),
line,
}),
};
let items = crate::v1::menus::the_items_of_the_menu(rt, &name);
let machine = crate::v1::menus::the_machine_menu_of(rt, &name);
let _ = rt
.desktop()
.v1_gui_menu_bar_is_updated(&name, &items, machine.unwrap_or(0));
for holder in crate::v1::menus::the_menus_that_have_the_submenu(rt, &name) {
let items = crate::v1::menus::the_items_of_the_menu(rt, &holder);
let machine = crate::v1::menus::the_machine_menu_of(rt, &holder);
let _ = rt
.desktop()
.v1_gui_menu_bar_is_updated(&holder, &items, machine.unwrap_or(0));
}
the_answer_of_an_act(rt, "Menu", answer)
}
fn the_tray_of_the_script(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
) -> Result<Value, RuntimeError> {
let sub_command = value_of(values, 1);
let file = value_of(values, 2);
match sub_command.trim().to_ascii_lowercase().as_str() {
"icon" => {
if file.trim().is_empty() {
return Err(RuntimeError {
message: "the file of the icon of the tray may not be empty.".to_string(),
line,
});
}
let number = value_of(values, 3).trim().parse::<i64>().unwrap_or(1);
let canonical = rt
.desktop()
.canonical_path(file.trim())
.unwrap_or_else(|_| file.trim().to_string());
if rt.desktop().icon_of_a_file(&canonical, number).is_err() {
return Err(RuntimeError {
message: "Can't load icon.".to_string(),
line,
});
}
rt.set_the_icon_of_the_script(canonical.clone(), number);
if let Some(reason) = rt.desktop().tray_set_icon(&canonical).err() {
rt.note(format!(
"the tray of the session did not take the icon: {reason}"
));
}
Ok(Value::Text(String::new()))
}
_ => Err(RuntimeError {
message: the_reason_of_a_menu_that_the_tree_does_not_carry(sub_command.trim()),
line,
}),
}
}
fn the_reason_of_a_menu_that_the_tree_does_not_carry(sub_command: &str) -> String {
match sub_command.to_ascii_lowercase().as_str() {
"standard" | "nostandard" => "the tree does not carry the standard items of a menu: the menus of the tree have the items that the script puts in them.".to_string(),
_ => "the tree does not carry the menu bar of a window of the machine: the menus of the tree are menus of the script.".to_string(),
}
}
pub(crate) fn menu_get_handle(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let name = values
.first()
.map(|value| value.to_text())
.unwrap_or_default();
crate::v1::menus::the_handle_of(rt, &name, line)
}
pub(crate) fn menu_get_name(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let handle = values
.first()
.map(|value| value.to_text())
.unwrap_or_default();
crate::v1::menus::the_name_of(rt, &handle, line)
}
pub(crate) fn progress(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let percent = value_of(values, 0);
let answer = if percent.trim().eq_ignore_ascii_case("off") {
rt.desktop()
.gui_destroy(0)
.map(|_| Value::Text(String::new()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
} else {
let number = percent.trim().parse::<i64>().unwrap_or(0);
let handle = rt
.desktop()
.gui_open("Progress")
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
let (control, _) = rt
.desktop()
.gui_add(handle, "Progress", &number.to_string())
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
rt.desktop()
.gui_value(control, None)
.map(Value::Text)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
};
the_answer_of_an_act(rt, "Progress", answer)
}
pub(crate) fn splash_text_on(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
let width = values.get(1).map(|value| value.to_text()).unwrap_or_default();
let height = values.get(2).map(|value| value.to_text()).unwrap_or_default();
let title = values.get(3).map(|value| value.to_text()).unwrap_or_default();
let text = values.get(4).map(|value| value.to_text()).unwrap_or_default();
let handle = rt
.desktop()
.gui_open(&title)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
let (control, _) = rt
.desktop()
.gui_add(handle, "Text", &text)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
let width = width.trim().parse::<i64>().unwrap_or(200);
let height = height.trim().parse::<i64>().unwrap_or(0);
rt.desktop()
.gui_size(control, width, height)
.map(|_| Value::Text(String::new()))
.map_err(|reason| crate::builtins::desktop_failure(reason, line))
}
pub(crate) fn register_callback(
rt: &mut Runtime<'_>,
values: &[Value],
line: u32,
_variant: u8,
) -> Result<Value, RuntimeError> {
#[cfg(feature = "std")]
{
let first = values.first().cloned().unwrap_or(Value::Text(String::new()));
if let Value::Object(object) = &first {
let class = object.borrow().class.clone();
if class == "func" || class == "boundfunc" {
let params = match values.get(2).and_then(|value| value.to_number()) {
Some(number) if number >= 0.0 => number as usize,
_ => 4,
};
let address = rt
.desktop()
.create_callback(&first, params)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
return Ok(Value::Int(address as i64));
}
}
let name = value_of(values, 0);
if let Some(method) = rt.the_method_of_the_qualified_name(&name) {
let params = match values.get(2).and_then(|value| value.to_number()) {
Some(number) if number >= 0.0 => number as usize,
_ => 4,
};
let address = rt
.desktop()
.create_callback(&method, params)
.map_err(|reason| crate::builtins::desktop_failure(reason, line))?;
return Ok(Value::Int(address as i64));
}
if name.is_empty() || rt.the_function_named(&name).is_none() {
return Ok(Value::Text(String::new()));
}
let params = match values.get(2).and_then(|value| value.to_number()) {
Some(number) if number >= 0.0 => number as usize,
_ => rt.the_number_of_parameters_of(&name).unwrap_or(0),
};
let event_info = values
.get(3)
.and_then(|value| value.to_number())
.map(|number| number as i64);
let address = crate::callback::register_with_event_info(&name, params, event_info);
if address == 0 {
return Err(RuntimeError {
message: "the pool of the callbacks is full.".to_string(),
line,
});
}
Ok(Value::Int(address as i64))
}
#[cfg(not(feature = "std"))]
{
let _ = (rt, values);
Err(RuntimeError {
message: "the callback of the machine needs the std of the tree.".to_string(),
line,
})
}
}
fn value_of(values: &[Value], at: usize) -> String {
values.get(at).map(|value| value.to_text()).unwrap_or_default()
}