

Almost every application needs to communicate with the user in some way.
Therefore, a substantial part of the code deals with the interaction
of program logic with GUI components.

Typically, the following is needed:

 put values into input fields under program control
 read and check input from the user
 pop up dialogs to query the user for further information


Task

For a minimal "application", write a program that presents a form with three components to the user:

 a numeric input field ("Value") 
 a button ("increment")
 a button ("random")


The field is initialized to zero.

The user may manually enter a new value into the field,
or increment its value with the "increment" button.

Entering a non-numeric value should be either impossible,
or issue an error message.

Pressing the "random" button presents a confirmation dialog,
and resets the field's value to a random value if the answer is "Yes".

(This task may be regarded as an extension of the task Simple windowed application).



@1Rosetta code program.
@aPROGRAM guicomponent CONTEXT VOID USE standard
BEGIN
@<Included declarations@>
@<Modes in the outer reach@>
@<Names in the outer reach@>
@<Callback procedures@>
@<Other routines@>
@<Main logic@>
END
FINISH
 
@ This file contains all the macros for the Xforms library procedures.
Only macros which are called will actually generate code.
 
@iforms.w@>
 
@ Initialise the Xforms library, create the form, set the value in the
input field, show the form and hand control to the Xforms library.
 
@<Main...@>=
open(argf,"",arg channel);
fl initialize(argc,argv,"GUI interact",NIL,0);
main form:=create form main;
fl set input(main input OF main form,float(value,10,5,2));
fl show form(main OF main form,fl place center,fl fullborder,"GUI interact");
fl do forms
 
@ The input value will be stored in !value!.
 
@<Names...@>=
REF FDMAIN main form;
REAL value:=0;
FILE argf;
 
@1The form.
The following section contains declarations for the form. It was output by
the program 'fdtow68' using the file output by the 'fdesign' program.
 
@2Modes.
This is the mode declaration for form !main!.
 
@<Modes...@>=
MODE FDMAIN = STRUCT(
   REF FLFORM main,
   REF FLOBJECT main increment,
   REF FLOBJECT main input,
   REF FLOBJECT main random);
 
@ This procedure creates form !main!.
 
@<Other...@>=
PROC create form main = REF FDMAIN:
BEGIN
   REF FLOBJECT obj;
   REF FDMAIN fdui:=HEAP FDMAIN;
   OP(REF FDMAIN)CBPTR TOCBPTR = BIOP 99;
   main OF fdui:=fl bgn form(fl no box,259,126);
   obj:=fl add box(fl up box,0,0,259,126,"");
      fl set object color(obj,fl col1,fl col1);
   main input OF fdui:=obj:=fl add input(fl float input,78,18,160,35,"Value");
      fl set object lsize(obj,fl normal size);
      fl set object callback(obj,main cb,1);
      fl set object return(obj,fl return end changed);
   main increment OF fdui:=obj:=fl add button(fl normal button,20,70,100,40,"Increment");
      fl set object lsize(obj,fl normal size);
      fl set object callback(obj,main cb,2);
      fl set button mouse buttons(obj,BIN 7);
   main random OF fdui:=obj:=fl add button(fl normal button,140,70,100,40,"Random");
      fl set object lsize(obj,fl normal size);
      fl set object callback(obj,main cb,3);
      fl set button mouse buttons(obj,BIN 7);
   fl end form;
   fl adjust form size(main OF fdui);
   fdui OF main OF fdui:=TOCBPTR fdui;
   fdui
END; #create form main#
 
@2Callback procedures.
There is only one callback procedure.
 
@<Callback...@>=
PROC main cb = (REF FLOBJECT obj,INT data)VOID:
CASE data
IN
	#input#
	BEGIN
		FILE mf;
		open(mf,fl get input(main input OF main form)+blank,mem channel);
		get(mf,value);  #convert the input to binary#
		close(mf)
	END
	,
	#increment#
	(
		value +:= 1;
		fl set input(main input OF main form,float(value,10,5,2))
	)
	,
	#random#
	(
		value:=random;
		fl set input(main input OF main form,float(value,10,5,2))
	)
ESAC; #main cb#
 
@2Macro calls.
Here are all the Xforms macro calls in alphabetical order.
 
@<Include...@>=
macro fl add box;
macro fl add button;
macro fl add input;
macro fl adjust form size;
macro fl bgn form;
macro fl do forms;
macro fl end form;
macro fl get border width;
macro fl get input;
macro fl initialize;
macro fl set border width;
macro fl set button mouse buttons;
macro fl set input;
macro fl set object callback;
macro fl set object color;
macro fl set object lsize;
macro fl set object return;
macro fl show form;
 
@ The end.