Menthos example inspired me to post an alternative HelloWorld Reaction example, similar but different.
First, it is in Modula-2, proof that there is Amiga programming other than C (and E and Hollywood).
Second, I get swimmy headed looking at the big C macros that set up a window and all its gadgets in one fell swoop. I'm more linear minded, so I go for separate procedures to create a gadget, create a window, and to link the gadgets to the window.
Third, my Modula-2 bent leads me to want, even for fairly simple things, to create re-usable code modules so I don't have to go thru the Reaction nitty gritty stuff every time. So, this example uses previously built modules rather than call the Reaction library directly. Module SimpleGUI is a relatively thin layer on Reaction, not a one-to-one but giving you quick ability to code examples like HelloWorldReaction as well as a good deal more.
Doing it this way has a lot of advantages. Primarily it means that, one you get the Reaction right this time, getting it right in the next program should be much less of a burden. Information hiding also means that you can tweak the way you think about the GUI a bit - make it fit a little better the way you want to use Reaction. And it makes it much easier to update the way you implement a GUI idea in not just one program, but a whole set of programs that were written to SimpleGUI.
There are disadvantages, of course. It is much harder to create the useful software layer in the first place, though it does not have to be done all at once. There is a tendency to put in capabilities this specific program does not need, and may never be used. The Modula-2 example is much easier to follow (for me), but the executable is much larger because the entire support module is linked in. A better pre-linking usage analysis (which I don't yet have) will help with that problem.
At any rate, here is the example. The executable and code plus SimpleGUI definition are attached. The SimpleGUI implementation is fairly big - ask and I will provide.
IMPORT all used code
Support module SimpleGUI insulates us a bit from Reaction.
Another support module is used for ^C testing.
(*########################*) MODULE HelloWorldReaction; (* $VER: HelloWorldReaction.mod 0.0 (30.5.2011) *) (*########################*) FROM Break IMPORT TstBreak; FROM SimpleGUI IMPORT AddGadg, CloseWndo, DirectionSet, DisposeWndo, EvntInfo, Gadg, GetWndoEvnt, Modifiers, ModifierSet, NewButton, NewLabel, NewWndo, NullWndo, OpenWndo, Orientations, SizeToFit, TwoD, WaitWndoEvnt, Wndo, WndoContainer, WndoEvnts, WndoModifiers, WndoModifierSet;
Variable Declarations
The Wndo type essentially encapsulates a Reaction Window, and Gadg type a Reaction Gadget. SimpleGUI handles all the RA_HandleInput() processing, queues it, and makes it available to us in the EventInfo record.
VAR MyWndo :Wndo; HelloLabel, ByeButton :Gadg; winSize, winPos :TwoD; evnt :EvntInfo; done :BOOLEAN;
Initializations
BEGIN MyWndo := NullWndo();
Create the Gadgets
Each Gadg supported by SimpleGUI has a create procedure, which can be modified with some of a shared set of characteristics. eg, "SizeToFit+FixedH" specifies that this button will not expand beyond the horizontal space needed for the text.
HelloLabel := NewLabel("Hello World!", ModifierSet{Static, Emphasized}); ByeButton := NewButton("Bye!", SizeToFit+ModifierSet{FixedH});
Create the Window
"Vertical" indicates that added Gadgs added to the window's container will be added one on top of the next.
In this case, the y size specified is dummy, and insures that the window vertical size will shrink down to just enclose the space for stacking the two Gadgs.
winSize.x := 150; winSize.y := 1; MyWndo := NewWndo(winSize, Vertical, DirectionSet{}, WndoModifierSet{NoSizeGad});
Put the gadgets in
SimpleGUI automatically creates a top level container (layout) along with a Wndo, and the function WndoContainer() gives you access to it.
AddGadg(WndoContainer(MyWndo), HelloLabel); AddGadg(WndoContainer(MyWndo), ByeButton);
Open the Window
winPos.x := 100; winPos.y := 100; OpenWndo(MyWndo, winPos, "Hello World!");
The Loop
Note that SimpleGUI queues up incoming events for you. Be careful only to Wait after you have retrieved all the pending events.
In this case, there is only one button, so the IF test on the select event would not really be necessary.
done := FALSE; WHILE NOT done DO WaitWndoEvnt(MyWndo); WHILE GetWndoEvnt(MyWndo, evnt) DO IF TstBreak() THEN done := TRUE; ELSE CASE evnt.ev OF CloseEv: done := TRUE; | SelectEv: IF evnt.g = ByeButton THEN done := TRUE; END; | ELSE END; END; END; END;
Termination
DisposeWndo() also disposes of any Gadgs associated with the Wndo.
(*-----*) FINALLY (*-----*) CloseWndo(MyWndo); DisposeWndo(MyWndo); END HelloWorldReaction.
Tags:
Blog post type:
Attachment | Size |
---|---|
m2_helloworld.lha | 128.15 KB |
Comments
Submitted by Menthos on
Nice! Keep them coming! :)
/M.Andersson, Viking Technology