i
i
i
i
i
i
i
i
19.2. Programming Models 485
The left mouse button down event puts the program into State 1 where, in our
solution from Figure 19.8, LMBDownRoutine() implements this state and defines
the center of the HeroBall, etc. In this case the transition between states is trig-
gered by the mouse events, and we see that it is physically impossible to move
from State 2 back to State 1. However, we do need to handle the case where the
user action causes a transition from State 1 to State 3 directly (mouse button down
and release without any dragging actions). This state diagram helps us analyze
possible combinations of state transitions and perform appropriate initializations.
Event-drivenapplications interface with the user throughphysical (e.g., mouse
clicks) or simulated GUI elements (e.g., quit button, slider bars). An input GUI
element (e.g., the quit button) is an artifact (e.g., an icon) for the user to direct
changes to the application state, while an output GUI element (e.g., the status
bar) is an avenue for the application to present application state information to
the user as feedback. For both types of elements, information only flows in one
direction—either from the user to the application (input) or from the application
to the user (output). When working with GUI elements that serve both input and
output purposes, special care is required. For example, after the user selects or
defines a HeroBall, the slider bars reflects the velocity of the free falling HeroBall
(output), while at any time, the user can manipulate the slider bar to alter the Her-
oBall velocity (input). In this case, the GUI element’s displayed state and the ap-
plication’s internal state are connected. The applicationmust ensure that these two
states are consistent. Notice that in the solution shown in Figure 19.4, this state
consistency is not maintained. When a user clicks the RMB (B2 in Figure 19.4)
to select a HeroBall, the slider bar values are updated properly; however, as the
HeroBall free falls under gravity, the slider bar values are not updated. The so-
lution presented in Figure 19.8 fixes this problem by using the ServiceTimer()
function.
Event service routines are functions defined in our program that cause a call-
back from the MainEventLoop in the presence of relevant events. For this reason,
these service routines are also referred to as callback functions. The application
program registers callback functions with the GUI system by passing the address
of the function to the GUI system. This is the registration mechanism implied in
Figure 19.7 and Figure 19.8. Simple GUI systems (e.g., GLUT or FLTK) usually
support this form of registration mechanism. The advantage of this mechanism is
that it is easy to understand, straightforward to program, and often contributes to
a small memory footprint in the resulting program. The main disadvantage of this
mechanism is the lack of organizational structure for the callback functions.
In commercial GUI systems, there are a large numbers of events with which
user applications must deal, and a structured organization of the service routines
can assist the programmability of the GUI system. Modern commercial GUI sys-