C++ Programming Tutorial/Introduction to the Toolkit

From 68kMLA Wiki
Revision as of 04:46, 22 April 2012 by Chuzzum (talk | contribs) (moved Macintosh Toolkit Programming to C++ Programming Tutorial/Introduction to the Toolkit: namespacing)
Jump to: navigation, search

Work In Progress

SIOW apps 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.

All Macintosh programs can be broken up into 3 rough parts: Initialization, Main Event Loop, and Termination. Initialization starts up whatever managers are needed and loads all the parts of the application into memory. The Main Event Loop cycles through an infinite loop waiting for input from the user. When it gets some, 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:

 1 #include <MacWindows.h>
 2 #include <Events.h>
 4 void Initialize();
 5 void MainLoop();
 6 void Terminate();
 8 void main() {
 9     Initialize();
11     MainLoop();
13     Terminate();
14 }
16  void Initialize() {
17      InitWindows();
18 }
20 void MainLoop() {
22     EventRecord event;
24     while (true) {
25         if (GetNextEvent(everyEvent, &event)) {
26             if (event.what == keyDown)
27                 Terminate();
28         }
29     }
30 }
32 void Terminate() {
33     ExitToShell();
34 }

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 a bool.) 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.

Next: Windows and Menus