![]() |
![]() |
![]() |
![]() |
Control Flow in a Typical ApplicationsysAppLaunchCmdNormalLaunch. It begins with a startup routine, then goes into an event loop, and finally exits with a stop routine.
appStopEvent (not a launch code) through the event queue. The application must detect this event and terminate.
static void EventLoop (void){    UInt16 error;    EventType event;    do    {    EvtGetEvent (&event, evtWaitForever);        PreprocessEvent (&event);        if (! SysHandleEvent (&event))        if (! MenuHandleEvent (NULL, &event, &error))        if (! ApplicationHandleEvent (&event))    FrmDispatchEvent (&event);     #if EMULATION_LEVEL != EMULATION_NONE    ECApptDBValidate (ApptDB);    #endif    }    while (event.eType != appStopEvent);}PreprocessEvent to allow the datebook event handler to see the command keys before any other event handler gets them. Some of the datebook views display UI that disappears automatically; this UI needs to be dismissed before the system event handler or the menu event handler display any UI objects.
Note that not all applications need a PreprocessEvent function. It may be appropriate to call SysHandleEvent right away.
Call SysHandleEvent to give the system an opportunity to handle the event.
The system handles events like power on/power off, Graffiti input, tapping silk-screened icons, or pressing buttons. During the call to SysHandleEvent, the user may also be informed about low-battery warnings or may find and search another application.
Note that in the process of handling an event, SysHandleEvent may generate new events and put them on the queue. For example, the system handles Graffiti input by translating the pen events to key events. Those, in turn, are put on the event queue and are eventually handled by the application.
SysHandleEvent returns true if the event was completely handled, that is, no further processing of the event is required. The application can then pick up the next event from the queue.
If SysHandleEvent did not completely handle the event, the application calls MenuHandleEvent. MenuHandleEvent handles two types of events:
MenuHandleEvent brings up the menu.
MenuHandleEvent removes the menu from the screen and puts the events that result from the command onto the event queue.
MenuHandleEvent returns TRUE if the event was completely handled.
If MenuHandleEvent did not completely handle the event, the application calls ApplicationHandleEvent, a function your application has to provide itself. ApplicationHandleEvent handles only the frmLoadEvent for that event; it loads and activates application form resources and sets the event handler for the active form.
If ApplicationHandleEvent did not completely handle the event, the application calls FrmDispatchEvent. FrmDispatchEvent first sends the event to the application's event handler for the active form. This is the event handler routine that was established in ApplicationHandleEvent. Thus the application's code is given the first opportunity to process events that pertain to the current form. The application's event handler may completely handle the event and return true to calls from FrmDispatchEvent.In that case, FrmDispatchEvent returns to the application's event loop. Otherwise, FrmDispatchEvent calls FrmHandleEvent to provide the system's default processing for the event.
For example, in the process of handling an event, an application frequently has to first close the current form and then open another one, as follows:
FrmGotoForm to bring up another form. FrmGotoForm queues a frmCloseEvent for the currently active form, then queues frmLoadEvent and frmOpenEvent for the new form.
frmCloseEvent, it closes and erases the currently active form.
frmLoadEvent, it loads and then activates the new form. Normally, the form remains active until it's closed. (Note that this wouldn't work if you preload all forms, but preloading is really discouraged. Applications don't need to be concerned with the overhead of loading forms; loading is so fast that applications can do it when they need it.) The application's event handler for the new form is also established.
frmOpenEvent, it performs any required initialization of the form, then draws the form on the display.
FrmGotoForm has been called, any further events that come through the main event loop and to FrmDispatchEvent are dispatched to the event handler for the form that's currently active. For each dialog box or form, the event handler knows how it should respond to events, for example, it may open, close, highlight, or perform other actions in response to the event. FrmHandleEvent invokes this default UI functionality.
After the system has done all it can to handle the event for the specified form, the application finally calls the active form's own event handling function. For example, in the datebook application, it may call DayViewHandleEvent or WeekViewHandleEvent.
ctlSelectEvent. All the details of the event queue are handled by the system.
frmOpenEvent. Typically, all the application does is draw its own interface, using the functions provided by the system, and then waits for events it can handle to arrive from the queue.
EvtDequeuePenStrokeInfo call must be made to get the next stroke.
EvtDequeuePenStrokeInfo because the event manager calls this function automatically when it detects a complete pen stroke in the pen queue. After calling EvtDequeuePenStrokeInfo, the system event manager stores the stroke bounds into the event record and returns the pen-up event to the application. The application is then free to dequeue the stroke points from the pen queue, or to ignore them altogether. If the points for that stroke are not dequeued by the time EvtGetEvent is called again, the system event manager automatically flushes them.
keyDownEvent to the application through the EvtGetEvent call.
| System Event Manager Functions | |
|---|---|
| Main Event Queue Management | |
| EvtGetEvent | EvtEventAvail |
| EvtSysEventAvail | EvtAddEventToQueue |
| EvtAddUniqueEventToQueue | EvtCopyEvent |
| Pen Queue Management | |
| EvtPenQueueSize | EvtDequeuePenPoint |
| EvtDequeuePenStrokeInfo | EvtFlushNextPenStroke |
| EvtFlushPenQueue | EvtGetPen |
| EvtGetPenBtnList | |
| Key Queue Management | |
| EvtKeyQueueSize | EvtEnqueueKey |
| EvtFlushKeyQueue | EvtKeyQueueEmpty |
| Handling pen strokes and key strokes | |
| EvtEnableGraffiti | EvtProcessSoftKeyStroke |
| Handling power on and off events | |
| EvtResetAutoOffTimer |
EvtWakeup
|
|   | ![]() |
![]() |
![]() |
![]() |
  |