Here is a simple "hello world" program written to show how to create a small Reaction UI:
Includes
Include description pending
#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
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.
enum { GID_MAIN=0, GID_STRING_WORLD, GID_BUTTON_BYE, GID_LAST };
Main function
// Login GUI int main(void) {
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.
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;
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.
if ((AppPort = (struct MsgPort *)IExec->CreateMsgPort()) == NULL) return 0;
Create Window object
Code description pending
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; }
Open window
Code description pending
if ((window = (struct Window *) RA_OpenWindow(object)) == NULL) { IIntuition->DisposeObject(object); IExec->DeletePort(AppPort); return 0; }
Signal mask
Code description pending
IIntuition->GetAttr(WINDOW_SigMask, object, &signal);
Main loop
This is the main loop.
while (!done) {
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.
wait = IExec->Wait( signal | SIGBREAKF_CTRL_C ); if ( wait & SIGBREAKF_CTRL_C ) done = TRUE; else {
Inputs from the GUI
Code description pending
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:; } } } }
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.
IIntuition->DisposeObject(object); IExec->DeletePort(AppPort); return 0; }
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:
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 |
---|---|
helloworld.txt | 3.11 KB |
Comments
Submitted by kas1e on
As i complete noob with reaction, i see that need to add a bit more description to source code itself and just a bit of explaining here and here. I.e. something like this:
Something like this will be interesting and helpfull as well imho.
Submitted by Menthos on
Thanks kas1e for your suggestions.
I will try to find the time to split the code into sections and describing each section (what it does and why).
I am also hoping that someone will look at the code and comment on the way it is set up. If the code is good or that it do things the 'wrong' way.
/M.Andersson, Viking Technology
Submitted by YesCop on
Hi Menthos,
As Kas1e, I am not fond of amiga programming so I would like to thank you for your idea.
For me, what I would understand is the connection, the link between all objects in reaction; How is Reaction working to understand better the system ? and so programming it better.
Just another idea, could you end your post with the complete source without the line numbers ?
Thanks
YesCop
Submitted by Menthos on
I am thinking to attach the code as a file to the blog.
/M.Andersson, Viking Technology
Submitted by Menthos on
I have added the code as an attachment to the blog and have added some descriptions.
Descriptions of the trickier parts are still pending.
/M.Andersson, Viking Technology
Submitted by YesCop on
Hi Menthos,
I forgot to say that I like your avatar.
Let's work now.
I tested your program. I used to use C++ so your file has been renamed helloworld.cc.
I had a compliation error. Here are the results.
gcc -o helloworld helloworld.c -lraauto -lauto
helloworld.c: In function 'main':
helloworld.c:64: warning: assignment from incompatible pointer type
gcc -o helloworld helloworld.cc -lraauto -lauto
helloworld.cc: In function 'int main()':
helloworld.cc:64: error: cannot convert 'Object*' to 'Gadget*' in assignment
g++ -o helloworld helloworld.cc -lraauto -lauto
helloworld.cc: In function 'int main()':
helloworld.cc:64: error: cannot convert 'Object*' to 'Gadget*' in assignment
It is strange in the first version, the error became a warning and I could launch your program.
Any ideas to correct that error ? I don't like C much, I prefer C++.
Bye
YesCop
Submitted by Menthos on
I think it can be because the button was not cast to a gadget.
Try this: (struct Gadget *)ButtonObject
/M.Andersson, Viking Technology
Submitted by trixie on
As of the latest AmigaOS4 SDK, BOOPSI gadgets and images are defined as pointers to objects, not structures Gadget and Image, respectively. So your gadget definition above should read
Object *gadgets[GID_LAST];
and not
struct Gadget *gadgets[GID_LAST];
Should you then need to use gadget-specific functions such as SetGadgetAttrs() you will cast the variable to a struct Gadget pointer:
IIntuition->SetGadgetAttrs( (struct Gadget *) gadgets[GID_STRING_WORLD], window, NULL, ... );
AmigaOne X5000-020 / 2GB RAM / Sapphire Pulse Radeon RX 560 / AmigaOS 4.1 Final Edition Update 2
Submitted by Menthos on
Thanks Trixie!
I will do this update too when I find the time (I have not started my Amiga in about a month :( ).
/M.Andersson, Viking Technology
Submitted by salass00 on
You should use DeletePort() only for ports created with CreatePort() and DeleteMsgPort() for ports created with CreateMsgPort().
Better yet since your code is for OS4.x only you should use the new AllocSysObject()/FreeSysObject() functions instead as the other methods are now obsolete.
Submitted by Menthos on
Double post...
/M.Andersson, Viking Technology
Submitted by Menthos on
Thanks!
I will try to update the code this weekend and will update the example after that.
Everybody: Keep the improvement coming so we have a really good and up to date example!
/M.Andersson, Viking Technology