Using Event-Action Mapping

In the previous example, there were three events that could trigger ending the application (click on Close in the system menu, Alt+F4, or Ctrl-F4). We can simplify our code by making use of the Framework's ability to map many events to one single action. In order to use this facility, we need to replace FORM INPUT in the event loop with a call to pGetAction. Lines [10] to [15] in MainEventLoop are replaced with

[10] while $IsWinOpen ("wMainWindow") = $true
[12a]    pGetAction (vlStatus, "", vlAction, \
[13a]           vlWindowName)

The first parameter is the usual status variable that passes back any return information. The second parameter refers to a form defined within the Framework. This form is used to define the characteristics of the process (i.e. window) that is currently active. In this case, we are interested in the window name. The Framework knows the window name because it is one of the parameters passed to pRegisterProcess. The third parameter is an output parameter in which the Framework returns the action. Finally, the last parameter is also an output parameter in which the name of the window in which the event occurred is returned.

The Framework has a number of predefined event to action mappings. The three predefined mappings of interest to us are



Accelerator Ctrl-F4 pressed in any window.


Accelerator Alt-F4 pressed in any window.


Closed event occurs in any window.


Note that, since the Framework contains these predefined mappings, it tends to enforce certain standard characteristics of all applications.

Now, we can simplify MainWindowProg by replacing lines [81] to [91] by the following.

[81a]when vlAction in ("EXIT", "EXITAPPLICATION")
[82a]    plClose (vlStatus)
[83a]    pUnRegisterProcess (vlStatus,\
[84a]           MainState.stProcessId, "")

The action we react to is no longer the specific event tag but is the mapped action name. We need to catch both EXIT and EXITAPPLICATION since they both mean the same thing in this case. Note that this code doesn't care how the action was requested (i.e. Alt-F4, Ctrl-F4, etc.). In fact, we could later add a new mapping that would also generate one of these actions. For example, we might have an Exit menu item in the menu displayed in the main application window. If we make the tag for this menu item "EXIT", then clicking on that menu item also causes an EXIT action and is handled by the above lines of code.