Here is a simple "hello world" program written to show how to create a small Reaction UI:
Includes
Include description pending
Enum
We use an enum to help us keep track of our GUI objects/gadgets.
Here we will use GID_MAIN for a vertical group object that will contain the other objects, GID_STRING_WORLD for the string object and GID_BUTTON_BYE for the button object.
We add a GID_LAST at the bottom of the enum to be able to use that enum as the size of our array of gadgets.
Main function
Local variables
To start with we set up the local variables we are going to use for our GUI.
We need a message port (AppPort) to be able to get the events that the UI is going to send when we interact with the window. We also need a window (window) and space for all the gadgets (gadgets) we want to do things with. We also have a variable to store the object (object) we use to create the window.
Then we have som variable for signal handling (wait, signal), somewhere to store results and codes (result, code) and finally a variable for terminating (done) our program loop.
Create Message port
To be able to get signals and communicate with the window we need to set up a message port that we will attach to the window.
Create Window object
Code description pending
Open window
Code description pending
Signal mask
Code description pending
Main loop
This is the main loop.
Wait for signal
The exec library has a function for waiting for signals. Earlier we set up the signal mask variable named signal with the signals that the window will send. Here we also add the SIGBREAKF_CTRL_C to the mask so we can listen for that signal too.
The program will halt at the Wait call until a signal is received.
If the signal we got from the Wait function is a SIGBREAKF_CTRL_C we end the loop by setting done to TRUE.
Inputs from the GUI
Code description pending
Clean up
When we are done with the GUI we need to give back everything we allocated and created. So we dispose the window object. That also closes the window. Then we delete the message port. After that we can end the program.
Compilation
The code is provided in this blog as a .txt file (.c is not allowed to be added to the blogs).
Rename the file to helloworld.c and compile with:
#include <classes/window.h> #include <proto/exec.h> #include <proto/intuition.h> #include <proto/dos.h> #include <proto/window.h> #include <proto/layout.h> #include <proto/label.h> #include <proto/string.h> #include <gadgets/string.h> #include <images/label.h> #include <reaction/reaction_macros.h>
enum { GID_MAIN=0, GID_STRING_WORLD, GID_BUTTON_BYE, GID_LAST };
// Login GUI int main(void) {
struct MsgPort *AppPort = NULL; struct Window *window = NULL; struct Gadget *gadgets[GID_LAST]; Object *object = NULL; ULONG wait, signal; ULONG result; UWORD code; int done = FALSE;
if ((AppPort = (struct MsgPort *)IExec->CreateMsgPort()) == NULL) return 0;
object = (Object *)WindowObject, WA_Title, "Hello world!", WA_Activate, TRUE, WA_DepthGadget, TRUE, WA_DragBar, TRUE, WA_CloseGadget, TRUE, WA_SizeGadget, FALSE, WINDOW_IconifyGadget, TRUE, WINDOW_IconTitle, "Hello world", WINDOW_AppPort, AppPort, WINDOW_Position, WPOS_CENTERMOUSE, WINDOW_ParentGroup, gadgets[GID_MAIN] = (struct Gadget *)VGroupObject, LAYOUT_AddChild, gadgets[GID_STRING_WORLD] = (struct Gadget *)StringObject, GA_ID, GID_STRING_WORLD, STRINGA_TextVal, "world!", GA_TabCycle, TRUE, GA_ReadOnly, TRUE, EndObject, CHILD_Label, LabelObject, LABEL_Text, "Hello", LabelEnd, LAYOUT_AddChild, HGroupObject, LAYOUT_AddChild, gadgets[GID_BUTTON_BYE] = ButtonObject, GA_ID, GID_BUTTON_BYE, GA_Text, "Bye!", GA_RelVerify, TRUE, GA_TabCycle, TRUE, EndObject, EndGroup, EndGroup, EndWindow; if (object == NULL) { IExec->DeletePort(AppPort); return 0; }
if ((window = (struct Window *) RA_OpenWindow(object)) == NULL) { IIntuition->DisposeObject(object); IExec->DeletePort(AppPort); return 0; }
IIntuition->GetAttr(WINDOW_SigMask, object, &signal);
while (!done) {
wait = IExec->Wait( signal | SIGBREAKF_CTRL_C ); if ( wait & SIGBREAKF_CTRL_C ) done = TRUE; else {
while ( (result = RA_HandleInput(object, &code) ) != WMHI_LASTMSG ) { switch (result & WMHI_CLASSMASK) { case WMHI_CLOSEWINDOW: done = TRUE; break; case WMHI_ICONIFY: RA_Iconify(object); window = NULL; break; case WMHI_UNICONIFY: window = (struct Window *) RA_OpenWindow(object); if (window) IIntuition->GetAttr(WINDOW_SigMask, object, &signal); else done = TRUE; // error re-opening window! break; case WMHI_GADGETUP: switch (result & WMHI_GADGETMASK) { case GID_BUTTON_BYE: done = TRUE; break; default:; } break; default:; } } } }
IIntuition->DisposeObject(object); IExec->DeletePort(AppPort); return 0; }
gcc -o helloworld helloworld.c -lraauto -lauto
If compilation went well you can run the helloworld program.
Final notes
Please provide constructive criticism how to improve/correct this example program in a useful way to help beginners getting started with Reaction programming.
There also is a good tutorial here.Tags:
Blog post type:
| Attachment | Size |
|---|---|
| 3.11 KB |


Comments
Submitted by kas1e on
Submitted by Menthos on
Submitted by YesCop on
Submitted by Menthos on
Submitted by Menthos on
Submitted by YesCop on
Submitted by Menthos on
Submitted by trixie on
Submitted by Menthos on
Submitted by salass00 on
Submitted by Menthos on
Submitted by Menthos on