Understanding environments

An environment is a program state that consists of the current definition of the screen, any terminal settings and processing options, and open channels.

When you create (enter) a new environment, UI Toolkit saves the old environment and copies its attributes to the current one. Any change to an environment item supersedes the previous contents. When you delete (exit) the current environment, the previous environment is restored.

The advantage to using environments is that they enable you to easily create and maintain “component” programs. For example, consider an inventory management application as a component. If inventory management enters a new environment upon entry, and exits it when returning to the calling program, the component is completely modular and it doesn’t matter who calls it. You can call the component not only from the main menu but also from the inventory receivings component or any other program.

A program can have more than one environment. An application moves from one environment to another with E_ENTER. Each new environment is at a deeper environment level. The default number of levels is 16, and the maximum is 32. You can control the maximum number of environment levels with the U_START subroutine.

When you exit an environment with the E_EXIT subroutine, Toolkit closes its current state (deletes locally created windows, closes locally opened files, deletes any new menus, and so forth). The current environment is then reset to the previous environment.

The current environment is maintained by Toolkit and owns the header, information, and footer lines. It also owns channels, state flags, windows, menu columns, lists, and possibly toolbars. Only toolbars created with %TB_TKCREATE or logged with TB_TKLOG are under the control of the environment.

The current display owns the menu bar and body of the screen as well as placement of the windows, menu columns, toolbars, and lists. The current display can be shared by more than one environment. A shared display is accessible to multiple environment levels (although it is still owned by the environment that defined it). A new environment whose display is defined as shared can access the previous environment’s menu bar, menu columns, and screen body. If you make changes to a shared display, those changes will be reflected in all environment levels that share the display.

Windows, menu columns, channels, and toolbars are either global or local to an environment. These two statuses primarily determine deletion privileges: a local item is owned by the environment that defines it and can only be deleted by that particular environment, while a global item can be deleted at any environment level. Global items are owned by the environment system rather than a specific environment level. All local elements are automatically deleted when you exit the environment that owns them.