MoonTravel Planner: Writing an Event Handler
Now that you know what an event handler is supposed to look like, we’re ready to add one to the Moon Travel Planner application. Here’s what you’ll do:
Identify the events your handler processes.
Write the main window event handler and the compute travel time function called by the handler.
Install the main window event handler.
Call the application event loop function.
Make sure the code works.
Identify Events
As you recall from Chapter 2 a user can select a mode of transportation, then click a button to compute the travel time to the moon. When you created the interface, you assigned a command to the Compute Travel Time button. Your application needs to handle this command when it’s issued—a command class event kEventClassCommand
.
Everything else that happens in the window—moving it, minimizing it, clicking a radio button—can be handled by the default handler for the window. (Recall you selected Standard Handler in Chapter 4.)
Now that you’ve identified the event (a command class event) in which your application is interested, you need to declare an event type specifier to indicate to the Carbon Event Manager the event for which to call the handler.
Open the Moon Travel Planner project. In the main.c file
file, copy the following to the main
function, after the declaration OSStatus err
:
EventTypeSpec mainSpec = {kEventClassCommand, kEventCommandProcess};
Write the Main Window Event Handler
In this section, you’ll write an event handler, ...
Get Learning Carbon now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.