C++ Tutorials‎ > ‎

Introduction to the Macintosh Toolkit

Console driven applications are great for learning about C/C++ and can be very helpful for debugging bits of code, but if you want to write applications that are actually useful to someone you will need to learn about the Macintosh Toolkit. This is an API (application programming interface) created by Apple that is embedded in the memory of every 68k Mac (and most older PPC Macs as well). MPW comes with a set of libraries that allow C/C++ programs to access this interface. 

The Toolkit is divided into a number of Managers, which are each responsible for different aspects of the operating system. The primary managers we're going to be dealing with in this part of the tutorial are the Window manager and the Event manager. To use these managers in your program, add these includes to the top of your cpp file:

#include <MacWindows.h>
#include <Events.h>

These include the Window and Event managers respectively.

Note: If you are using CodeWarrior, all includes needed for the Macintosh Toolkit are generally handled for you. It doesn't hurt to have the include statements there, but don't be surprised when it works without them.

If you have never done GUI programming before, it helps to know that all GUI programs (and by extension all Macintosh programs) can be broken up into 3 general parts: Initialization, Main Event Loop, and Termination. 

During initialization, you starts up whatever managers are needed and load all the parts of the application into memory. 

The Main Event Loop cycles through an infinite loop waiting for input from the user. When an event occurs, it interprets it and takes an action based on that input. Some inputs may cause the loop to break (such as choosing Quit from the menu), in which case the program continues on to Termination.

In the Termination part of the program, all open windows are closed, used memory is freed, and then the program returns control of the system to the Finder.

It is useful to structure our program in a way that reflects this, so here is the first version of the program we will be working with:

#include <MacWindows.h>
#include <Events.h>

void Initialize();
void MainLoop();
void Terminate();

void main() {



void Initialize() {

void MainLoop() {

    EventRecord event;

    while (true) {
        if (GetNextEvent(everyEvent, &event)) {
            if (event.what == keyDown)

void Terminate() {

Here is a brief description of each function. 

The Initialization() function only calls one other function currently, InitWindows(). This function tells the Toolkit that we will be using the Window Manager and that it should allocate memory for it.

MainLoop() has an infinite loop that repeatedly calls the function GetNextEvent(). The first argument in GetNextEvent() is called an event mask and it tells the program what types of events to look for. In this case, we want to know about all the events. We pass it a EventRecord variable for it to store information about the event in. It returns a Boolean, which is an Apple defined data type (I'm not sure how it's different from the C/C++ bool data type, but it acts like one.) The value is true if an event is found, false otherwise. Once we find an event, we can check the value event.what to find out what type of event it is. In this example, if any key is pressed down on the keyboard, we call Terminate() to end the program. 

Terminate() calls the function ExitToShell(), which immediately quits the program.