Graphics Reference
In-Depth Information
user-input actions). Furthermore, to integrate interactive applications in sophis-
ticated multiprogramming window environments, it is important that our system
support automatically takes care of mundane and standard user actions. This is
the basic functionality of a modern GUI system.
1.2
Event-Driven Programming
Event-driven programs are typically implemented based on an existing GUI sys-
tem. GUI systems remedy the efficiency and complexity concerns by defining a
default
MainEventLoop()
function. For event-driven programs, the
MainEvent
Loop()
replaces the
main()
function, because all programs start and end in this
function. Just as in the case of the
main()
function for control-driven program-
ming, when the
MainEventLoop()
function returns, all work should have been
completed, and the program terminates. The
MainEventLoop()
function defines
the central control structure for all event-driven programming solutions and typ-
ically cannot be changed by a user application. In this way, the overall control
of an application is actually external to the user's program code. For this rea-
son, event-driven programming is also referred to as the external control model.
Listing 1.3 shows a typical
MainEventLoop()
implementation. In this case, our
program is the user application that is based on the
MainEventLoop()
function.
Structurally, the
MainEventLoop()
is very similar to the
main()
function of List-
ing 1.2, with a continuous loop (B) containing a central parsing switch statement
(D). The important differences between the two functions are the following.
•
(A) SystemInitialization().
Recall that event-driven programs start and
endinthe
MainEventLoop()
function.
SystemInitialization()
is a
mechanism defined to invoke the user program from within the
MainEvent
Loop()
. It is expected that user programs implement the function
System
Initialization()
to initialize the application state and to register
event
service routines
(refer to the discussion in (D)).
•
(B) Continuous outer loop.
Because this is a general control structure to
be shared by all event-driven programs, there is no way to determine the
termination condition. User programs are expected to override appropriate
event service routines and terminate the program from within the service
routine.
•
(C) Stop and wait.
Instead of actively polling the user for actions (wast-
ing machine resources), the
MainEventLoop()
typically stops the entire
Search WWH ::
Custom Search