Another Hello World with Reaction

  • up
    45%
  • down
    55%

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.

  1. (*########################*)
  2. MODULE HelloWorldReaction; (* $VER: HelloWorldReaction.mod 0.0 (30.5.2011) *)
  3. (*########################*)
  4.  
  5. FROM Break IMPORT TstBreak;
  6. FROM SimpleGUI IMPORT AddGadg, CloseWndo, DirectionSet, DisposeWndo, EvntInfo, Gadg,
  7. GetWndoEvnt, Modifiers, ModifierSet, NewButton, NewLabel, NewWndo,
  8. NullWndo, OpenWndo, Orientations, SizeToFit, TwoD, WaitWndoEvnt,
  9. 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.

  1. VAR MyWndo :Wndo;
  2. HelloLabel,
  3. ByeButton :Gadg;
  4. winSize,
  5. winPos :TwoD;
  6. evnt :EvntInfo;
  7. done :BOOLEAN;

Initializations

  1. BEGIN
  2.  
  3. 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.

  1. HelloLabel := NewLabel("Hello World!", ModifierSet{Static, Emphasized});
  2. 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.

  1. winSize.x := 150;
  2. winSize.y := 1;
  3. 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.

  1. AddGadg(WndoContainer(MyWndo), HelloLabel);
  2. AddGadg(WndoContainer(MyWndo), ByeButton);

Open the Window

  1. winPos.x := 100;
  2. winPos.y := 100;
  3. 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.

  1. done := FALSE;
  2. WHILE NOT done DO
  3.  
  4. WaitWndoEvnt(MyWndo);
  5. WHILE GetWndoEvnt(MyWndo, evnt) DO
  6.  
  7. IF TstBreak() THEN
  8.  
  9. done := TRUE;
  10.  
  11. ELSE
  12.  
  13. CASE evnt.ev OF
  14. CloseEv: done := TRUE; |
  15. SelectEv: IF evnt.g = ByeButton THEN
  16. done := TRUE;
  17. END; |
  18. ELSE
  19. END;
  20.  
  21. END;
  22.  
  23. END;
  24.  
  25. END;

Termination

DisposeWndo() also disposes of any Gadgs associated with the Wndo.

  1. (*-----*)
  2. FINALLY
  3. (*-----*)
  4.  
  5. CloseWndo(MyWndo);
  6. DisposeWndo(MyWndo);
  7.  
  8. END HelloWorldReaction.

Tags: 

Blog post type: 

AttachmentSize
File m2_helloworld.lha128.15 KB

Comments