TweetFollow Us on Twitter

MACINTOSH C CARBON
MACINTOSH C CARBON: A Hobbyist's Guide To Programming the Mac OS in C
Version 1.0
© 2001 K. J. Bricknell
Go to Contents

PREFACE

The Carbon Application Programming Interface

Apple announced the intended introduction of a new operating system, to be known as Mac OS X, at the 1998 World Wide Developers Conference. Mac OS X, the first version of which was released on 24 March 2001, is not just another Mac OS update; it is a completely new operating system complete with "modern" operating system features such as pre-emptive multitasking and protected memory. It features a completely new user interface, called Aqua, whose appearance and behaviour differs significantly from that of the original Mac OS (represented in its latest, and no doubt last, incarnation by Mac OS 9).

Mac OS X runs on G3 and G4 PowerPC machines only, meaning that machines based on PowerPC 604 and 603 microprocessors must necessarily remain with Mac OS 9 and earlier. A large installed base of these latter machines will no doubt remain for many years to come. In addition, it is likely that many owners of machines capable of running Mac OS X will nonetheless remain with Mac OS 9 and earlier. In these circumstances, it was perceived as all but essential that programs written to take advantage of Mac OS X's advanced features also be capable of running on Mac OS 8 and 9 without modification. Fortunately, Apple has devised the means whereby this can be achieved, namely, the Carbon Application Programming Interface (API).

An API is basically a collection of system software routines, two examples being GetNewCWindow (which programmers call to create windows) and FSpOpenDF (which programmers call to open a file). Mac OS 9 includes a collection of more than 8,000 such system software routines, which are now known collectively as the Classic API. Apple determined that about 2000 of these APIs were incompatible with a modern operating system like Mac OS X. The remaining 6000 or so "clean" APIs were isolated and, together with many additions, included in the Carbon API.

It turns out, therefore, that experienced Macintosh programmers will not need to learn a completely new API in order to program for the completely new operating system that is Mac OS X. Any programmer familiar with the Classic API will be able to transition to the Carbon API with comparatively little effort. In addition, newcomers to Macintosh programming will not be required to learn two APIs in order to write applications intended to run native on both Mac OS X and Mac OS 8 and 9.

That said, there is another API, called Cocoa, that may be used to write programs for Mac OS X. Cocoa's advantage is that it allows applications to be written more quickly than is the case using the Carbon API; however, such applications will not run on Mac OS 8 and 9.

A further advantage of Carbon is that existing applications written using the Classic API can be "carbonized" with but a fraction of the effort that would be required to completely rewrite them for Mac OS X using the Cocoa API.

This book, then, is for those who wish to learn to create applications that will, firstly, run on both Mac OS X and Mac OS 8 and 9 and, secondly, take advantage of Mac OS X's unique features when run on that system. It is intended as a reasonably comprehensive entry point to programming the Macintosh using the Carbon API.

The First Task - Learn the C Language and CodeWarrior IDE

The main assumption made by this book is that you have already learned both the C language and the Metrowerks CodeWarrior IDE (Integrated Development Environment).

CodeWarriorU conducts on-line courses in the C language and the CodeWarrior IDE.

The Macintosh C Carbon Phase

When you have learned the C language and the fundamentals of the CodeWarrior IDE, you are ready to open this book. As you move through this second phase of the journey, you will quickly discover that learning C and the IDE was by far the easiest part!

Essentially, this book covers all of the territory which, in the author's judgement, needs to be covered before you write your first serious application. This includes, for example, how to create and manage all elements of the user interface (menus, windows, controls, dialogs, alerts, lists, drag and drop, etc.), how to perform file input/output, how to print documents, how to draw text and graphics, and so on.

General Structure of Macintosh C Carbon

The general structure of most chapters in this book is the same: first comes the information, then a list of constants, data types and functions relevant to the subject of that chapter, then the source code listing of one or more demonstration programs related to the subject of that chapter, and, finally, line-by-line comments that explain the workings of the source code.

The book itself is supported by the demonstration program package, which contains the CodeWarrior project and source code files, and Resorcerer resource files, for all demonstration programs. The demonstration programs associated with Chapter 18 to 25 exist in two versions:

  • The primary version, which utilises the Carbon event model introduced at Chapter 17, is the version whose source code listing and associated comments are included in the book itself.

  • The secondary version utilises the Classic event model (see Chapter 2). The explanatory comments for this version are contained in PDF files included in the relevant demonstration program (Classic event model version) folders.

What You Will Need

System Software

For Mac OS 8 and 9, Macintosh C Carbon requires:

  • CarbonLib 1.4 or later. (CarbonLib is the extension, located in the Extensions folder in the System folder, that implements the Carbon API on Mac OS 8 and 9.)

  • Mac OS 8.6 or later. (CarbonLib 1.4 and later are not compatible with versions of Mac OS 8 earlier than 8.6.)

For Mac OS X, Version 10.0.3 or later is required. (The so-called frameworks that implement the Carbon API on Mac OS X are an integral part of Mac OS X and are installed when you install that system. Frameworks are like shared libraries on Mac OS 8/9.)

Development System

As previously stated, it is assumed that your development system will be Metrowerks CodeWarrior. CodeWarrior Pro 6 (or later) is itself a Carbon application, and is much to be preferred.

Resource Editor

A resource editor allows you to create resources for programs and files. The resource editor you will need is Resorcerer, a product of Mathemaesthetics.

References and Other Sources of Information

References enable you to access information relating to the system software, and are quite indispensable. The Carbon Developer Documentation section and the Mac OS 8 and 9 Developer Documentation Function Index at Apple's WWW site are the most up-to-date sources of information available. You might also wish to subscribe to Apple's Carbon development mailing list .

Programmer's Calculator

You will find a programmer's calculator very useful for converting between decimal, hexadecimal and binary values, the shareware program CalcWorks being ideal for that purpose.

Icon Editor

Versions of Resorcerer earlier than Version 2.4 did not facilitate the creation of 32-bit icons or the large (128 by 128 pixel) thumbnail icons utilised by Mac OS X. If your version of Resorcerer is earlier than Version 2.4, the shareware program Iconographer is possibly the best option for the creation of these icons.

Universal Headers, CarbonLib, and Stub Libraries

The demonstration programs assume the use of Version 3.4.1 or later of the Universal Headers and, as previously stated, CarbonLib 1.4 or later.

The Universal Headers were introduced at the same time as the Power Macintosh. Amongst other things, they enabled programmers to write (Classic API) source code capable of being compiled as either 680x0 code or PowerPC code - hence the term "Universal".

If the version of CodeWarrior you are using includes an earlier version of the Universal Headers and/or the version of CarbonLib in your Mac OS 8/9 Extensions folder is earlier than 1.4 (or does not exist), you should download the CarbonLib SDK (Software Development Kit) Version 1.4GM (or later) and:

  • Replace the existing folder CIncludes in Metrowerks CodeWarrior / Mac OS Support / Universal / Interfaces with the one in the SDK.

  • Copy the extension CarbonLib from the SDK to the Mac OS 8/9 Extensions folder, replacing the existing version if it exists.

  • Copy the two stub libraries CarbonLib and CarbonFrameWorkLib from the SDK to Metrowerks CodeWarrior / Mac OS Support / Universal / Libraries / StubLibraries, replacing the existing versions if they exist.

Human Interface Guidelines - Mac OS 8/9

Useful additions to your library when you get further down the track would be the publications:

Demonstration Programs

For Mac OS 8/9, you should simply copy the folder Mac C Carbon Demos from the CD-ROM to a convenient location on your hard drive. For Mac OS X, you should copy the folder titled Mac C Carbon Demos to the Applications folder and the folder titled Demo Documents to the relevant user's Documents folder.

Before running a demonstration program, you should read the top section of the source code comments in the relevant chapter (or, for the Chapters 18-15 Classic event model versions, in the previously-mentioned PDF files). For most programs, this explains what to do, what to expect, and what to note. Note that the comments sections occasionally contain additional information incidental to the main purpose of the demonstration.

As far as is possible, each demonstration program avoids making calls to system software routines that are only explained in a later chapter. However, achieving that ideal has not been possible in the demonstration programs associated with the earlier chapters. For example, the demonstration program associated with Chapter 1 must, of necessity, make calls to system software routines relating to windows (the subject of Chapter 4) and drawing in a graphics port (the subject of Chapter 12). Where this occurs, you should simply accept, on faith, that the associated source code does as is stated in the demonstration program comments section. The important thing is to concentrate on that part of the source code pertaining to the subject of the chapter with which the program is associated.

Terminology

All but the later volumes of the official Mac OS reference work (Inside Macintosh) are Pascal-oriented, reflecting the fact that system software routines were originally designed to be called from a Pascal program. Because of this historically cosy relationship between Pascal and the Mac OS 8/9 system software, the C programmer will often be confronted by Pascal terminology. For example:

  • The Pascal terms "procedure" and "record" are sometimes used in C-oriented programming books and other publications in a context where the C programmer would ordinarily expect the terms "function" and "structure".

    In C, a routine which returns a value and a routine which does not return anything are both called functions. In Pascal, a routine which returns a value is also called a function; however, a routine which does not return anything is called a procedure.

  • The names of many system software data structures end in Rec (for record) rather than, say, Struc (for structure).

  • The names of certain fields in many system software data structures end in Proc (for procedure) rather than, say, Func (for function).

As a reflection of the fact that the later additions to Apple's Inside Macintosh series of publications are C-oriented rather than Pascal-oriented, this book uses C terminology exclusively. Hence "structure" is used rather than "record" and "function" is used rather than "procedure".

Early Days

This version of Macintosh C Carbon was finalised during the early days of both Mac OS X and the Mac OS 8/9 Carbon implementation. At that time, both systems contained bugs which impacted on some of the demonstration programs. Known issues in this regard, for which the author begs the reader's indulgence, are listed in the ReadMe file in the demonstration programs folder.

K. J. Bricknell, AM
Canberra
Australia
August 2001
brick@asiaonline.net.au

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »

Price Scanner via MacPrices.net

Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer new Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more
Sunday Sale: 13-inch M3 MacBook Air for $999,...
Several Apple retailers have the new 13″ MacBook Air with an M3 CPU in stock and on sale today for only $999 in Midnight. These are the lowest prices currently available for new 13″ M3 MacBook Airs... Read more
Multiple Apple retailers are offering 13-inch...
Several Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices... Read more

Jobs Board

Relationship Banker - *Apple* Valley Financ...
Relationship Banker - Apple Valley Financial Center APPLE VALLEY, Minnesota **Job Description:** At Bank of America, we are guided by a common purpose to help Read more
IN6728 Optometrist- *Apple* Valley, CA- Tar...
Date: Apr 9, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 824398 At Target Optical, we help people see and look great - and Read more
Medical Assistant - Orthopedics *Apple* Hil...
Medical Assistant - Orthopedics Apple Hill York Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.