GUI enabling/disabling of controls: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(21 intermediate revisions by 12 users not shown)
Line 52:
 
disabling.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Strings.Fixed;
with Gtk.Main;
with Gtk.Handlers;
Line 220:
 
Gtk.Main.Main;
end Disabling;</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">GUI, Add, Edit, w150 number vValue gEnableDisable, 0 ; Number specifies a numbers-only edit field. g<Subroutine> specifies a subroutine to run when the value of control changes.
GUI, Add, button,, Increment
GUI, Add, button, xp+70 yp, Decrement ; xp+70 and yp are merely positioning options
Line 267:
GuiClose:
ExitApp
; Ensures the script ends when the GUI is closed.</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
==={{header|BaCon}}===
This code requires BaCon 4.0.1 or higher.
<syntaxhighlight lang="bacon">OPTION GUI TRUE
PRAGMA GUI gtk3
 
gui = GUIDEFINE(" \
{ type=WINDOW name=window callback=delete-event title=\"Rosetta Code\" width-request=200 } \
{ type=BOX name=box parent=window orientation=GTK_ORIENTATION_VERTICAL } \
{ type=SPIN_BUTTON name=spin parent=box numeric=TRUE } \
{ type=BUTTON_BOX name=bbox parent=box } \
{ type=BUTTON name=increment parent=bbox callback=clicked label=\"Increment\" } \
{ type=BUTTON name=decrement parent=bbox callback=clicked label=\"Decrement\" } ")
 
CALL GUISET(gui, "spin", "adjustment", gtk_adjustment_new(0, 0, 10, 1, 1, 0))
CALL GUISET(gui, "decrement", "sensitive", FALSE)
 
DECLARE input TYPE FLOATING
 
WHILE TRUE
 
CALL GUIGET(gui, "spin", "value", &input)
SELECT GUIEVENT$(gui)
CASE "increment"
INCR input
CASE "decrement"
DECR input
CASE "window"
BREAK
ENDSELECT
CALL GUISET(gui, "spin", "value", input)
 
CALL GUISET(gui, "decrement", "sensitive", IIF(input <= 0, FALSE, TRUE))
CALL GUISET(gui, "increment", "sensitive", IIF(input > 9, FALSE, TRUE))
CALL GUISET(gui, "spin", "sensitive", IIF(input = 0, TRUE, FALSE))
 
WEND</syntaxhighlight>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"WINLIB2"
INSTALL @lib$+"WINLIB5"
ES_NUMBER = 8192
Line 307 ⟶ 345:
SYS "GetDlgItemInt", !form%, 101, 0, 1 TO number%
SYS "SetDlgItemInt", !form%, 101, number% - 1, 1
ENDPROC</langsyntaxhighlight>
{{out}}
<p>
Line 317 ⟶ 355:
<pre>file main.c</pre>
 
<langsyntaxhighlight lang="c">#include <windows.h>
#include "resource.h"
 
Line 396 ⟶ 434:
EnableWindow( GetDlgItem(hwnd,IDC_INCREMENT), n<MAX_VALUE );
EnableWindow( GetDlgItem(hwnd,IDC_DECREMENT), n>MIN_VALUE );
}</langsyntaxhighlight>
 
<pre>file resource.h</pre>
 
<langsyntaxhighlight lang="c">#define IDD_DLG 101
#define IDC_INPUT 1001
#define IDC_INCREMENT 1002
#define IDC_DECREMENT 1003
#define IDC_QUIT 1004</langsyntaxhighlight>
 
<pre>file resource.rc</pre>
 
<langsyntaxhighlight lang="c">#include <windows.h>
#include "resource.h"
 
Line 421 ⟶ 459:
PUSHBUTTON "Quit", IDC_QUIT, 117, 25, 30, 14
RTEXT "Value:", -1, 10, 8, 20, 8
}</langsyntaxhighlight>
 
=={{header|C sharp}}==
Using Windows Forms; compile with csc -t:winexe Program.cs (on MS.NET) or gmcs -t:winexe Program.cs (on Mono)
<langsyntaxhighlight lang="csharp">using System;
using System.ComponentModel;
using System.Windows.Forms;
Line 506 ⟶ 544:
Application.Run(new RosettaInteractionForm());
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
with Qt 4.4, creating project file with qmake -project, Makefile with qmake -o Makefile <projectfile> and finally make
<pre>file task.h</pre>
<langsyntaxhighlight lang="cpp">#ifndef TASK_H
#define TASK_H
 
Line 540 ⟶ 578:
QHBoxLayout *lowerPart ;
} ;
#endif</langsyntaxhighlight>
 
<pre>file task.cpp</pre>
<langsyntaxhighlight lang="cpp">#include <QtGui>
#include <QString>
#include "task.h"
Line 596 ⟶ 634:
number-- ;
entryField->setText( QString("%1").arg( number )) ;
}</langsyntaxhighlight>
 
<pre>main.cpp</pre>
<langsyntaxhighlight lang="cpp">#include <QApplication>
#include "task.h"
 
Line 607 ⟶ 645:
theWidget.show( ) ;
return app.exec( ) ;
}</langsyntaxhighlight>
 
=={{header|Delphi}}==
<langsyntaxhighlight lang="delphi">type
TForm1 = class(TForm)
MaskEditValue: TMaskEdit; // Set Editmask on: "99;0; "
Line 640 ⟶ 678:
begin
MaskEditValue.Text := IntToStr(Succ(StrToIntDef(Trim(MaskEditValue.Text), 0)));
end;</langsyntaxhighlight>
 
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">using fwt
using gfx
 
Line 711 ⟶ 749:
}.open
}
}</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
<lang FreeBASIC>
#Include "windows.bi"
 
Line 752 ⟶ 790:
 
End
</syntaxhighlight>
</lang>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_window = 1
begin enum 1
_inputFld
_incBtn
_decBtn
end enum
 
void local fn BuildWindow
window _window, @"GUI enabling/disabling of controls", (0,0,350,150), NSWindowStyleMaskTitled
textfield _inputFld,, @"0", (160,78,30,21)
ControlSetAlignment( _inputFld, NSTextAlignmentCenter )
button _incBtn,,, @"Increment", (75,43,101,32)
button _decBtn, NO,, @"Decrement", (174,43,101,32)
end fn
 
void local fn InputAction
long value = fn ControlIntegerValue( _inputFld )
if ( value < 0 or value > 10 ) then value = 0
textfield _inputFld,, str(value)
select ( value )
case 0
TextFieldSetEditable( _inputFld, YES )
button _incBtn, YES
button _decBtn, NO
case 10
TextFieldSetEditable( _inputFld, NO )
WindowMakeFirstResponder( _window, 0 )
button _incBtn, NO
button _decBtn, YES
case else
TextFieldSetEditable( _inputFld, NO )
WindowMakeFirstResponder( _window, 0 )
button _incBtn, YES
button _decBtn, YES
end select
end fn
 
void local fn IncrementAction
long value = fn ControlIntegerValue( _inputFld ) + 1
textfield _inputFld,, str(value)
TextFieldSetEditable( _inputFld, NO )
WindowMakeFirstResponder( _window, 0 )
if ( value >= 10 ) then button _incBtn, NO
if ( value > 0 ) then button _decBtn, YES
end fn
 
void local fn DecrementAction
long value = fn ControlIntegerValue( _inputFld ) - 1
textfield _inputFld,, str(value)
if ( value < 10 ) then button _incBtn, YES
if ( value == 0 )
button _decBtn, NO
TextFieldSetEditable( _inputFld, YES )
end if
end fn
 
void local fn DoDialog( ev as long, tag as long )
select ( ev )
case _btnClick
select ( tag )
case _inputFld : fn InputAction
case _incBtn : fn IncrementAction
case _decBtn : fn DecrementAction
end select
end select
end fn
 
fn BuildWindow
 
on dialog fn DoDialog
 
HandleEvents
</syntaxhighlight>
[[File:GUIEnableDisableControls350FB.png]]
 
=={{header|Gambas}}==
<langsyntaxhighlight lang="gambas">hValueBox As ValueBox 'We need a ValueBox
hButton0 As Button 'We need a button
hButton1 As Button 'We need another button
Line 833 ⟶ 959:
Checks 'Rune the Checks routine
 
End</langsyntaxhighlight>
 
=={{header|Go}}==
{{libheader|Gotk3}}
Loosely based on the Vala entry.
<langsyntaxhighlight lang="go">package main
 
import (
Line 959 ⟶ 1,085:
window.ShowAll()
gtk.Main()
}</langsyntaxhighlight>
 
==Icon and {{header|Unicon}}==
Line 965 ⟶ 1,091:
This uses the Unicon specific graphics library.
 
<syntaxhighlight lang="unicon">
<lang Unicon>
import gui
$include "guih.icn"
Line 1,053 ⟶ 1,179:
w.show_modal ()
end
</syntaxhighlight>
</lang>
 
=={{header|J}}==
'''J 8.x'''
<langsyntaxhighlight lang="j">task_run=: wd bind (noun define)
pc task nosize;
cc decrement button;cn "Decrement";
Line 1,082 ⟶ 1,208:
task_decrement_button=:verb define
update Value=: ": _1 + 0 ". Value
)</langsyntaxhighlight>
 
'''J 6.x'''
<langsyntaxhighlight Jlang="j">task_run=: wd bind (noun define)
pc task nosize;
xywh 6 30 48 12;cc decrement button;cn "-";
Line 1,108 ⟶ 1,234:
task_decrement_button=:verb define
update Value=: ": _1 + 0 ". Value
)</langsyntaxhighlight>
 
Example use:
 
<syntaxhighlight lang="text"> task_run''</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Swing}}
{{works with|AWT}}
<langsyntaxhighlight lang="java">import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Line 1,269 ⟶ 1,395:
new Interact().setVisible(true);
}
}</langsyntaxhighlight>
 
 
{{libheader|JavaFX}}
{{works with|Java|8}}
<langsyntaxhighlight lang="java">
import javafx.application.Application;
import javafx.beans.property.LongProperty;
Line 1,353 ⟶ 1,479:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
using Tk
w = Toplevel("GUI enabling/disabling")
Line 1,406 ⟶ 1,532:
 
 
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{libheader|JavaFX}}
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.2.21
 
import javafx.application.Application
Line 1,480 ⟶ 1,606:
fun main(args: Array<String>) {
Application.launch(InteractFX::class.java, *args)
}</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">nomainwin
textbox #demo.val, 20, 50, 90, 24
button #demo.dec, "Decrement", [btnDecrement], UL, 20, 90, 90, 24
Line 1,562 ⟶ 1,688:
#demo.val "!disable"
end if
End Sub</langsyntaxhighlight>
 
=={{header|LiveCode}}==
Line 1,578 ⟶ 1,704:
 
4. Select Object->Card Script and enter the following to handle enabling the buttons.
<langsyntaxhighlight LiveCodelang="livecode">command enableButtons v
switch
case v <= 0
Line 1,595 ⟶ 1,721:
put v into fld "Field1"
end switch
end enableButtons</langsyntaxhighlight>
 
For the increment button, click on Button 1 (in edit mode) and the select "code" from the toolbar and enter the following to handle clicks<langsyntaxhighlight LiveCodelang="livecode">on mouseUp
put field "Field1" into x
add 1 to x
enableButtons x
end mouseUp</langsyntaxhighlight>
 
Repeat for Button 2 (note we are subtracting here)<langsyntaxhighlight LiveCodelang="livecode">on mouseUp
put field "Field1" into x
subtract 1 from x
enableButtons x
end mouseUp</langsyntaxhighlight>
 
Finally add the code to handle keyboard entry in text field's code. Note this code prevents entering digits outside 0-9 and limits the value between 0 and 10.<langsyntaxhighlight LiveCodelang="livecode">on rawKeyDown k
if numToChar(k) is among the items of "1,2,3,4,5,6,7,8,9,0" then
if (numToChar(k) + the text of me) <= 10 then
Line 1,617 ⟶ 1,743:
pass rawKeyDown
end if
end rawKeyDown</langsyntaxhighlight>
 
Switch back to the form, and enter run mode to execute.
Line 1,626 ⟶ 1,752:
Normally a function can't call other except something global or something defined in function (local), or for member functions in Group object, they can call other members public or private at same level or deeper but then only the public members. Using Call Local we call functions like function's part. When an event service function called (by event), interpreter provide the same name as the module's name, where form declared, so this call is a "local call". So a second local call inside event service function,also provide same name. So global local1 called as local, so code executed as part of CheckIt (but without same "current" stack, and other specific to execution object properties). Modules, functions, threads, events are all executed on "execution objects" which carries the execution code.
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
\\ this is global, but call as local in events, which means with local visibility for identifiers
\\ so thispos and this$ has to exist in caller 's context
Line 1,696 ⟶ 1,822:
Checkit
 
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
For this problem, you will need to open up Maple, go to the Components tab on the left, and insert a Text Area, and 2 Buttons. By right clicking each button, and clicking Component Properties, change one to Increase, and the other to Decrease. Then, click on 'Edit Content Changed Action". In the one labeled increase, type:
 
<syntaxhighlight lang="maple">
<lang Maple>
Increase();
</syntaxhighlight>
</lang>
 
In the one labeled Decrease, type:
 
<syntaxhighlight lang="maple">
<lang Maple>
Decrease();
</syntaxhighlight>
</lang>
 
Then, by clicking the 2 gears and opening up the start-up commands, enter this:
<syntaxhighlight lang="maple">
<lang Maple>
macro(SP=DocumentTools:-SetProperty, GP=DocumentTools:-GetProperty);
with(Maplets[Elements]):
Line 1,742 ⟶ 1,868:
end if;
end proc;
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">Manipulate[Null, {{value, 0},
InputField[Dynamic[value], Number,
Enabled -> Dynamic[value == 0]] &},
Row@{Button["increment", value++, Enabled -> Dynamic[value < 10]],
Button["decrement", value--, Enabled -> Dynamic[value > 0]]}]</langsyntaxhighlight>
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">; file: gui-enable.lsp
; url: http://rosettacode.org/wiki/GUI_enabling/disabling_of_controls
; author: oofoe 2012-02-02
Line 1,799 ⟶ 1,925:
(gs:listen)
 
(exit)</langsyntaxhighlight>
 
Screenshot:
Line 1,806 ⟶ 1,932:
 
=={{header|Nim}}==
==={{libheader|Using Gtk2}}===
{{libheader|Gtk2}}
<lang nim>import
<syntaxhighlight lang="nim">import gtk2, strutils, glib2
 
var valu: int = 0
var chngd_txt_hndler: gulong = 0
proc thisDestroy(widget: PWidget, data: Pgpointer){.cdecl.} =
 
proc thisCheckBtns # forward declaration
 
proc thisDestroy(widget: pWidget, data: pgpointer){.cdecl.} =
main_quit()
 
nim_init()
var window = window_new(gtk2.WINDOW_TOPLEVEL)
var content = vbox_new(TRUEtrue,10)
var hbox1 = hbox_new(TRUEtrue,10)
var entry_fld = entry_new()
entry_fld.set_text("0")
Line 1,829 ⟶ 1,952:
add(hbox1,btn_inc)
add(hbox1,btn_dec)
pack_start(content, entry_fld, TRUEtrue, TRUEtrue, 0)
pack_start(content, hbox1, TRUEtrue, TRUEtrue, 0)
pack_start(content, btn_quit, TRUEtrue, TRUEtrue, 0)
set_border_width(Windowwindow, 5)
add(window, content)
 
proc thisCheckBtns =
proc thisInc(widget: pWidget, data: pgpointer){.cdecl.} =
set_sensitive(btn_inc, valu < 10)
set_sensitive(btn_dec, valu > 0)
set_sensitive(entry_fld, valu == 0)
 
proc thisInc(widget: PWidget, data: Pgpointer){.cdecl.} =
inc(valu)
entry_fld.set_text($valu)
thisCheckBtns()
proc thisDec(widget: pWidgetPWidget, data: pgpointerPgpointer){.cdecl.} =
dec(valu)
entry_fld.set_text($valu)
thisCheckBtns()
 
proc thisTextChanged(widget: pWidgetPWidget, data: pgpointerPgpointer) {.cdecl.} =
#signal_handler_block(entry_fld, chngd_txt_hndler)
try:
valu = parseInt($entry_fld.get_text())
except EInvalidValueValueError:
entry_fld.set_text($valu = 0)
entry_fld.set_text($valu)
#signal_handler_unblock(entry_fld, chngd_txt_hndler)
#signal_emit_stop(entry_fld, signal_lookup("changed",TYPE_EDITABLE()),0)
thisCheckBtns()
 
proc thisCheckBtns =
set_sensitive(btn_inc, valu < 10)
set_sensitive(btn_dec, valu > 0)
set_sensitive(entry_fld, valu == 0)
 
discard signal_connect(window, "destroy",
SIGNAL_FUNC(thisDestroy), nil)
Line 1,869 ⟶ 1,988:
discard signal_connect(btn_dec, "clicked",
SIGNAL_FUNC(thisDec), nil)
chngd_txt_hndler =discard signal_connect(entry_fld, "changed",
SIGNAL_FUNC(thisTextChanged), nil)
 
show_all(window)
thisCheckBtns()
main()</langsyntaxhighlight>
 
==={{libheader|IUP}}===
===Using Gtk3 (gintro)===
<lang nim>import
{{libheader|gintro}}
 
<syntaxhighlight lang="nim">import strutils
import gintro/[glib, gobject, gtk, gio]
 
type Context = ref object
value: int
entry: Entry
btnIncr: Button
btnDecr: Button
 
#---------------------------------------------------------------------------------------------------
 
proc checkButtons(ctx: Context) =
ctx.btnIncr.setSensitive(ctx.value < 10)
ctx.btnDecr.setSensitive(ctx.value > 0)
ctx.entry.setSensitive(ctx.value == 0)
 
#---------------------------------------------------------------------------------------------------
 
proc onQuit(button: Button; window: ApplicationWindow) =
window.destroy()
 
#---------------------------------------------------------------------------------------------------
 
proc onIncr(button: Button; ctx: Context) =
inc ctx.value
ctx.entry.setText($ctx.value)
ctx.checkButtons()
 
#---------------------------------------------------------------------------------------------------
 
proc onDecr(button: Button; ctx: Context) =
dec ctx.value
ctx.entry.setText($ctx.value)
ctx.checkButtons()
 
#---------------------------------------------------------------------------------------------------
 
proc onEntryChange(entry: Entry; ctx: Context) =
try:
ctx.value = entry.text().parseInt()
except ValueError:
entry.setText($ctx.value)
 
#---------------------------------------------------------------------------------------------------
 
proc activate(app: Application) =
## Activate the application.
 
let window = app.newApplicationWindow()
window.setTitle("GUI controls")
 
let content = newBox(Orientation.vertical, 10)
content.setHomogeneous(true)
let hbox1 = newBox(Orientation.horizontal, 10)
hbox1.setHomogeneous(true)
let hbox2 = newBox(Orientation.horizontal, 1)
hbox2.setHomogeneous(false)
let label = newLabel("Value:")
let entry = newEntry()
entry.setText("0")
let btnQuit = newButton("Quit")
let btnIncr = newButton("Increment")
let btnDecr = newButton("Decrement")
 
hbox2.add(label)
hbox2.add(entry)
hbox1.add(btnIncr)
hbox1.add(btnDecr)
 
content.packStart(hbox2, true, true, 0)
content.packStart(hbox1, true, true, 0)
content.packStart(btnQuit, true, true, 0)
 
window.setBorderWidth(5)
window.add(content)
 
let context = Context(value: 0, entry: entry, btnIncr: btnIncr, btnDecr: btnDecr)
 
discard btnQuit.connect("clicked", onQuit, window)
discard btnIncr.connect("clicked", onIncr, context)
discard btnDecr.connect("clicked", onDecr, context)
discard entry.connect("changed", onEntryChange, context)
 
context.checkButtons()
window.showAll()
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
let app = newApplication(Application, "Rosetta.GuiControls")
discard app.connect("activate", activate)
discard app.run()</syntaxhighlight>
 
===Using IUP===
{{libheader|IUP}}
<syntaxhighlight lang="nim">import
iup, strutils, math
 
Line 1,968 ⟶ 2,184:
discard dlg.show()
discard mainloop()
iup.close()</langsyntaxhighlight>
 
=={{header|PhixPerl}}==
<syntaxhighlight lang="perl">#!/usr/bin/perl
Note: this validates each input char separately, and '-' is not "NUMBER": the only way to get numbers <0 or >9 into the input field is to paste them.
{{libheader|pGUI}}
<lang Phix>include pGUI.e
 
use strict;
Ihandle txt, inc, dec, hbx, vbx, dlg
use warnings;
use Tk;
use Tk::Dialog;
use Tk::LabFrame;
 
my $value = 0;
function activate(integer v)
IupSetInt(txt,"VALUE",v)
IupSetAttribute(inc,"ACTIVE",iff(v<10,"YES":"NO"))
IupSetAttribute(dec,"ACTIVE",iff(v>0,"YES":"NO"))
IupSetAttribute(txt,"ACTIVE",iff(v=0,"YES":"NO"))
return IUP_CONTINUE
end function
 
my $mw = MainWindow->new;
function valuechanged_cb(Ihandle /*ih*/)
$mw->title( 'GUI component enable/disable' );
return activate(IupGetInt(txt,"VALUE"))
my $lf = $mw->LabFrame( -label => 'Value' )->pack(-fill => 'x',-expand => 1);
end function
my $entry = $lf->Entry( -width => 10, -textvariable => \$value,
-validate => 'key', -validatecommand => sub {
$_[0] =~ /^\d{1,9}\z/ ? do{setenable($_[0]); 1} : 0
},
)->pack(-fill => 'x', -expand => 1);
my $inc = $mw->Button( -text => 'increment',
-command => sub { $value++; setenable($value) },
)->pack( -side => 'left' );
my $dec = $mw->Button( -text => 'decrement',
-command => sub { $value--; setenable($value) },
)->pack( -side => 'right' );
 
setenable($value);
function inc_cb(Ihandle /*ih*/)
return activate(IupGetInt(txt,"VALUE")+1)
end function
 
MainLoop;
function dec_cb(Ihandle /*ih*/)
return activate(IupGetInt(txt,"VALUE")-1)
end function
 
sub setenable
function esc_close(Ihandle /*ih*/, atom c)
{
return iff(c=K_ESC?IUP_CLOSE:IUP_CONTINUE)
$inc and $inc->configure( -state => $_[0] < 10 ? 'normal' : 'disabled' );
end function
$dec and $dec->configure( -state => $_[0] > 0 ? 'normal' : 'disabled' );
$entry and $entry->configure( -state => $_[0] == 0 ? 'normal' : 'disabled' );
}</syntaxhighlight>
 
=={{header|Phix}}==
IupOpen()
Note: this validates each input char separately, and '-' is not "NUMBER": the only way to get numbers <0 or >9 into the input field is to paste them.
txt = IupText("VALUECHANGED_CB",Icallback("valuechanged_cb"),"FILTER=NUMBER, EXPAND=YES")
{{libheader|Phix/basics}}
inc = IupButton("increment",Icallback("inc_cb"))
{{libheader|Phix/pGUI}}
dec = IupButton("decrement",Icallback("dec_cb"),"ACTIVE=NO")
<!--<syntaxhighlight lang="phix">-->
hbx = IupHbox({inc,dec},"MARGIN=0x10, GAP=20")
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
vbx = IupVbox({txt,hbx},"MARGIN=40x20")
dlg = IupDialog(vbx)
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">txt</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">inc</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dec</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hbx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">vbx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dlg</span>
IupSetCallback(dlg, "K_ANY", Icallback("esc_close"))
IupShow(dlg)
<span style="color: #008080;">function</span> <span style="color: #000000;">activate</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
IupMainLoop()
<span style="color: #7060A8;">IupSetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">txt</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"VALUE"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
IupClose()</lang>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">inc</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ACTIVE"</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;"><</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YES"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"NO"</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dec</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ACTIVE"</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YES"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"NO"</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">txt</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"ACTIVE"</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">v</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YES"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"NO"</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CONTINUE</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">valuechanged_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">activate</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupGetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">txt</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"VALUE"</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">inc_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">activate</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupGetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">txt</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"VALUE"</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">dec_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">activate</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupGetInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">txt</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"VALUE"</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">esc_close</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_ESC</span><span style="color: #0000FF;">?</span><span style="color: #004600;">IUP_CLOSE</span><span style="color: #0000FF;">:</span><span style="color: #004600;">IUP_CONTINUE</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">txt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupText</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"VALUECHANGED_CB"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"valuechanged_cb"</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"FILTER=NUMBER, EXPAND=YES"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">inc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"increment"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"inc_cb"</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">dec</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupButton</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"decrement"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"dec_cb"</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"ACTIVE=NO"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">hbx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupHbox</span><span style="color: #0000FF;">({</span><span style="color: #000000;">inc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dec</span><span style="color: #0000FF;">},</span><span style="color: #008000;">"MARGIN=0x10, GAP=20"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">vbx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupVbox</span><span style="color: #0000FF;">({</span><span style="color: #000000;">txt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hbx</span><span style="color: #0000FF;">},</span><span style="color: #008000;">"MARGIN=40x20"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">vbx</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"K_ANY"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"esc_close"</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
 
=={{header|PicoLisp}}==
The standard PicoLisp GUI is HTTP based. Connect your browser to
http://localhost:8080 after starting the following script.
<langsyntaxhighlight PicoLisplang="picolisp">#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
 
(load "@ext.l" "@lib/http.l" "@lib/xhtml.l" "@lib/form.l")
Line 2,032 ⟶ 2,290:
 
(server 8080 "!start")
(wait)</langsyntaxhighlight>
 
=={{header|Prolog}}==
Works with SWI-Prolog and XPCE.
<langsyntaxhighlight Prologlang="prolog">dialog('GUI_Interaction',
[ object :=
GUI_Interaction,
Line 2,135 ⟶ 2,393:
-> send(Incr, active, @off)).
 
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Enumeration
#TextGadget
#AddButton
Line 2,183 ⟶ 2,441:
EndIf
Until Event=#PB_Event_CloseWindow
EndIf</langsyntaxhighlight>
 
=={{header|Python}}==
<syntaxhighlight lang="python">
<lang Python>
#!/usr/bin/env python3
 
Line 2,263 ⟶ 2,521:
if __name__ == "__main__":
main()
</syntaxhighlight>
</lang>
 
Result: [https://ibb.co/cEmqy5]
 
=={{header|R}}==
<syntaxhighlight lang="r">
<lang R>
library(gWidgets)
options(guiToolkit="RGtk2") ## using gWidgtsRGtk2
Line 2,295 ⟶ 2,553:
addHandlerChanged(down_btn, handler=rement, action=-1)
addHandlerChanged(up_btn, handler=rement, action=1)
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket/gui
Line 2,325 ⟶ 2,583:
 
(send frame show #t)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
Extremely basic implementation using the GTK library.
<syntaxhighlight lang="raku" perl6line>use GTK::Simple;
use GTK::Simple::App;
 
Line 2,357 ⟶ 2,615:
$dec.clicked.tap: { my $val = $value.text; $val -= 1; $value.text = $val.Str }
 
$app.run;</langsyntaxhighlight>
 
=={{header|Red}}==
<syntaxhighlight lang="rebol">Red[]
 
enable: does [
f/enabled?: 0 = n
i/enabled?: 10 > n
d/enabled?: 0 < n
]
 
view [
f: field "0" [either error? try [n: to-integer f/text] [f/text: "0"] [enable]]
i: button "increment" [f/text: mold n: add to-integer f/text 1 enable]
d: button "decrement" disabled [f/text: mold n: subtract to-integer f/text 1 enable]
]</syntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
Load "guilib.ring"
 
Line 2,396 ⟶ 2,669:
lineedit1.settext(num)
if number(num)<0 btn2.setDisabled(True) ok
</syntaxhighlight>
</lang>
Output:
[[File:CalmoSoftGui.jpg]]
Line 2,402 ⟶ 2,675:
=={{header|Ruby}}==
{{libheader|Shoes}}
<langsyntaxhighlight lang="ruby">Shoes.app do
@number = edit_line
@number.change {update_controls}
Line 2,416 ⟶ 2,689:
 
update_controls 0
end</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">import swing.{ BoxPanel, Button, GridPanel, Orientation, Swing, TextField }
import swing.event.{ ButtonClicked, Key, KeyPressed, KeyTyped }
 
Line 2,468 ⟶ 2,741:
centerOnScreen()
} // def top(
}</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
{{works with|Smalltalk/X}}
<langsyntaxhighlight lang="smalltalk">|top input vh incButton decButton|
 
vh := ValueHolder with:0.
Line 2,491 ⟶ 2,764:
decButton enableChannel:(BlockValue with:[:v | v > 0] argument:vh).
 
top open</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{libheader|Tk}}
<langsyntaxhighlight lang="tcl">package require Tk
 
# Model
Line 2,525 ⟶ 2,798:
.dec state [expr {$field > 0 ? "!disabled" : "disabled"}]
}
updateEnables; # Force initial state of buttons</langsyntaxhighlight>
 
=={{header|Vala}}==
{{libheader|Gtk+-3.0}}
 
<langsyntaxhighlight lang="vala">bool validate_input(Gtk.Window window, string str){
int64 val;
bool ret = int64.try_parse(str,out val);;
Line 2,557 ⟶ 2,830:
window.window_position = Gtk.WindowPosition.CENTER;
window.destroy.connect(Gtk.main_quit);
window.set_default_size(0,0);
 
var box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 1);
Line 2,640 ⟶ 2,914:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|Visual Basic}}==
Line 2,650 ⟶ 2,924:
by a "spinner" or "up-down" control, not by buttons.)
 
<langsyntaxhighlight lang="vb">VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
Line 2,720 ⟶ 2,994:
cmdDec.Enabled = True
End Select
End Sub</langsyntaxhighlight>
 
=={{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
 
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
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: "decrement"
bg_color: gx.light_gray
radius: 5
border_color: gx.gray
on_click: app.btn_click_dec
),
]
)
)
ui.run(app.window)
}
 
fn (mut app App) btn_click_inc(mut btn ui.Button) {
if app.counter.int() > 10 {
btn.disabled = true
return
}
app.counter = (app.counter.int() + 1).str()
}
 
fn (mut app App) btn_click_dec(mut btn ui.Button) {
if app.counter.int() < 0 {
btn.disabled = true
return
}
app.counter = (app.counter.int() - 1).str()
}
</syntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
{{libheader|Wren-polygon}}
<syntaxhighlight lang="wren">import "graphics" for Canvas, Color
import "input" for Mouse, Keyboard
import "dome" for Window
import "./polygon" for Polygon
 
class Button {
construct new(x, y, w, h, legend, c, oc, lc) {
var vertices = [[x, y], [x+w, y], [x+w, y+h], [x, y+h]]
_rect = Polygon.quick(vertices)
_x = x
_y = y
_w = w
_h = h
_legend = legend
_c = c
_oc = oc
_lc = lc
_enabled = true
}
 
enabled { _enabled }
enabled=(e) { _enabled = e }
 
draw() {
_rect.drawfill(_c)
_rect.draw(_oc)
var l = Canvas.getPrintArea(_legend)
var lx = ((_w - l.x)/2).truncate
lx = (lx > 0) ? _x + lx : _x + 1
var ly = ((_h - l.y)/2).truncate
ly = (ly > 0) ? _y + ly : _y + 1
var col = _enabled ? _lc : Color.darkgray
Canvas.print(_legend, lx, ly, col)
}
 
justClicked { Mouse["left"].justPressed && _rect.contains(Mouse.position.x, Mouse.position.y) }
}
 
class TextBox {
construct new(x, y, w, h, label, c, oc, lc) {
var vertices = [[x, y], [x+w, y], [x+w, y+h], [x, y+h]]
_rect = Polygon.quick(vertices)
_x = x
_y = y
_w = w
_h = h
_label = label
_c = c
_oc = oc
_lc = lc
_text = ""
_enabled = true
}
 
text { _text }
text=(t) { _text = t }
 
enabled { _enabled }
enabled=(e) { _enabled = e }
 
draw() {
_rect.drawfill(_c)
_rect.draw(_oc)
var l = Canvas.getPrintArea(_label).x
var lx = _x - l - 7
if (lx < 1) {
lx = 1
_label = _label[0..._x]
}
var col = _enabled ? _lc : Color.darkgray
Canvas.print(_label, lx, _y, col)
Canvas.getPrintArea(_label).x
Canvas.print(_text, _x + 3, _y + 1, Color.black)
}
}
 
class GUIControlEnablement {
construct new() {
Window.title = "GUI control enablement"
_btnIncrement = Button.new(60, 40, 80, 80, "Increment", Color.red, Color.blue, Color.white)
_btnDecrement = Button.new(180, 40, 80, 80, "Decrement", Color.green, Color.blue, Color.white)
_txtValue = TextBox.new(140, 160, 80, 8, "Value", Color.white, Color.blue, Color.white)
_txtValue.text = "0"
Keyboard.handleText = true
}
 
init() {
_btnDecrement.enabled = false
drawControls()
}
 
update() {
if (_btnIncrement.enabled && _btnIncrement.justClicked) {
var number = Num.fromString(_txtValue.text) + 1
_btnIncrement.enabled = (number < 10)
_btnDecrement.enabled = true
_txtValue.enabled = (number == 0)
_txtValue.text = number.toString
} else if (_btnDecrement.enabled && _btnDecrement.justClicked) {
var number = Num.fromString(_txtValue.text) - 1
_btnDecrement.enabled = (number > 0)
_btnIncrement.enabled = true
_txtValue.enabled = (number == 0)
_txtValue.text = number.toString
} else if (_txtValue.enabled && "0123456789".any { |d| Keyboard[d].justPressed }) {
_txtValue.text = Keyboard.text
_txtValue.enabled = (_txtValue.text == "0")
_btnIncrement.enabled = true
_btnDecrement.enabled = (_txtValue.text != "0")
}
}
 
draw(alpha) {
drawControls()
}
 
drawControls() {
Canvas.cls()
_btnIncrement.draw()
_btnDecrement.draw()
_txtValue.draw()
}
}
 
var Game = GUIControlEnablement.new()</syntaxhighlight>
9,476

edits