Jump to content

GUI component interaction: Difference between revisions

m
(FutureBasic solution added)
imported>Arakov
 
(10 intermediate revisions by 5 users not shown)
Line 13:
{{omit from|Lotus 123}}
{{omit from|Maxima}}
{{omit from|Minimal BASIC|It does not handle GUI}}
{{omit from|Nascom BASIC|It does not handle GUI}}
{{omit from|PARI/GP}}
{{omit from|PostScript}}
{{omit from|Retro}}
{{omit from|SQL PL|It does not handle GUI}}
{{omit from|Tiny BASIC|It does not handle GUI}}
 
{{omit from|Palo Alto Tiny BASIC|It does not handle GUI}}
 
Almost every application needs to communicate with the user in some way.
Line 332 ⟶ 335:
; a timer.</syntaxhighlight>
 
=={{header|BaConBASIC}}==
==={{header|BaCon}}===
Requires BaCon version 4.0.1 or higher, using GTK3.
<syntaxhighlight lang="bacon">OPTION GUI TRUE
Line 371 ⟶ 375:
WEND</syntaxhighlight>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> INSTALL @lib$+"WINLIB2"
Line 892 ⟶ 896:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import forms;
import extensions;
Line 898 ⟶ 902:
public class MainWindow : SDIDialog
{
Button btmIncrement;
Button btmRandom;
Edit txtNumber;
constructor new()
<= super new()
{
btmIncrement := Button.new();
btmRandom := Button.new();
txtNumber := Edit.new();
self
.appendControl:(btmIncrement)
.appendControl:(btmRandom)
.appendControl:(txtNumber);
 
self.Caption := "Rosseta Code";
self.setRegion(100, 100, 160180, 120140);
txtNumber.setRegion(720, 7, 140, 25);
txtNumber.Caption := "0";
btmIncrement.setRegion(720, 35, 140, 25);
btmIncrement.Caption := "Increment";
btmIncrement.onClick := (args){ self.onButtonIncrementClick() };
btmRandom.setRegion(720, 65, 140, 25);
btmRandom.Caption := "Random";
btmRandom.onClick := (args){ self.onButtonRandomClick() };
}
private onButtonIncrementClick()
{
var number := txtNumber.Value.toInt();
number := number + 1;
self.changeTextBoxValue(number)
}
private onButtonRandomClick()
{
if(messageDialog.showQuestion("Inf", "Really reset to random value?"))
{
self.changeTextBoxValue(randomGenerator.evalnextInt(99999999))
}
}
private changeTextBoxValue(number)
{
txtNumber.Caption := number.toString()
}
}</syntaxhighlight>
 
=== Alternative version using xforms script ===
 
form layout:
<syntaxhighlight lang="elena"><Form X="100" Y="100" Width="160" Height="120" Caption="Rosseta Code">
<Edit ID="txtNumber" X="7" Y="7" Width="140" Height="25" Caption="0">
</Edit>
<Button ID="btmIncrement" X="7" Y="35" Width="140" Height="25" Caption="Increment" onClick="onButtonIncrementClick">
</Button>
<Button ID="btmRandom" X="7" Y="65" Width="140" Height="25" Caption="Random" onClick="onButtonRandomClick">
</Button>
</Form></syntaxhighlight>
 
main code:
<syntaxhighlight lang="elena">import xforms;
import forms;
import extensions;
public class MainWindow
{
SDIForm form;
Button btmIncrement;
Button btmRandom;
Edit txtNumber;
constructor new()
{
form := xforms.executePath("main.xs", self);
btmIncrement := form.Controls.btmIncrement;
btmRandom := form.Controls.btmRandom;
txtNumber := form.Controls.txtNumber;
}
onButtonIncrementClick(sender)
{
var number := txtNumber.Value.toInt();
number := number + 1;
self.changeTextBoxValue(number)
}
onButtonRandomClick(sender)
{
if(messageDialog.showQuestion("Inf", "Really reset to random value?"))
{
self.changeTextBoxValue(randomGenerator.eval(99999999))
}
}
private changeTextBoxValue(number)
{
txtNumber.Caption := number.toString()
}
dispatch() => form;
}</syntaxhighlight>
 
 
=={{header|Euphoria}}==
Line 3,147 ⟶ 3,092:
end</syntaxhighlight>
[[File:GuiComponentRunBasic.png]]
 
== {{Header|Rust}} ==
{{libheader|egui}}
<syntaxhighlight lang="rust">
use eframe::egui;
use rand::Rng;
 
fn main() -> Result<(), eframe::Error> {
let options = eframe::NativeOptions {
initial_window_size: Some(egui::vec2(214.0, 100.0)),
..Default::default()
};
 
// Our application state:
let mut value = "0".to_owned();
 
eframe::run_simple_native("GUI component interaction", options, move |ctx, _frame| {
egui::CentralPanel::default().show(ctx, |ui| {
ui.horizontal(|ui| {
let name_label = ui.label("Value: ");
ui.text_edit_singleline(&mut value)
.labelled_by(name_label.id);
});
ui.horizontal(|ui| {
if ui.button("Increment").clicked() {
if let Ok(v) = value.parse::<usize>() {
value = (v + 1).to_string()
}
}
if ui.button("Random").clicked() {
value = (rand::thread_rng().gen_range(1..=10000)).to_string();
}
});
});
})
}</syntaxhighlight>
[[File:Rust GUI component interaction.png|none|thumb]]
 
=={{header|Scala}}==
Line 3,410 ⟶ 3,392:
End Select
End Sub</syntaxhighlight>
 
=={{header|V (Vlang)}}==
Graphical
 
Notes:
 
1) "v install ui" to get, also to locally check source and examples.
 
2) For alternative UI toolkits, check github.com/vlang/awesome-v (Awesome V).
<syntaxhighlight lang="Zig">
import ui
import gx
import rand
 
const (
win_width = 400
win_height = 40
)
 
[heap]
struct App {
mut:
window &ui.Window = unsafe {nil}
counter string = "0"
}
 
fn main() {
mut app := &App{}
app.window = ui.window(
width: win_width
height: win_height
title: "Counter"
mode: .resizable
layout: ui.row(
spacing: 5
margin_: 10
widths: ui.stretch
heights: ui.stretch
children: [
ui.textbox(
max_len: 20
read_only: false
// is_numeric: true // Can enforce only number input
text: &app.counter
),
ui.button(
text: "increment"
bg_color: gx.light_gray
radius: 5
border_color: gx.gray
on_click: app.btn_click_inc
),
ui.button(
text: "random"
bg_color: gx.light_gray
radius: 5
border_color: gx.gray
on_click: app.btn_click_ran
),
]
)
)
ui.run(app.window)
}
 
fn (mut app App) btn_click_inc(mut btn ui.Button) {
for value in app.counter {
if value.is_digit() == false {
ui.message_box("Only numbers allowed!")
return
}
}
if app.counter.int() < 0 || app.counter.int() > 100 {
ui.message_box("Only numbers between 0 to 100 accepted!\nResetting to 1.")
app.counter = "0"
}
app.counter = (app.counter.int() + 1).str()
}
 
fn (mut app App) btn_click_ran(mut btn ui.Button) {
ui.message_box("Will jump to random number between 1 and 100.")
app.counter = rand.int_in_range(1, 100) or {0}.str()
}
</syntaxhighlight>
 
=={{header|Web 68}}==
Line 3,547 ⟶ 3,613:
{{libheader|DOME}}
{{libheader|Wren-polygon}}
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color
import "input" for Mouse, Keyboard
import "dome" for Window
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.