Search the Catalog
Learning Carbon

Learning Carbon

By Apple Computer, Inc.
May 2001 (est.)
0-596-00161-4, Order Number: 1614
352 pages, $34.95

Chapter 1
Introduction to Carbon

Contents:

Carbon and Mac OS X
What's in Carbon?
Building Carbon Applications
The Carbon Factory Tour
Recap

The element carbon is an essential building block for life on earth. For C programmers, Apple's Carbon is the essential building block for applications on Mac OS X. With Carbon, you not only gain the benefits of Mac OS X, but you do so while using some of the simplest traditional C interfaces available. This combination makes it easier than ever to create world-class applications for a world-class operating system.

Carbon and Mac OS X

Carbon is the collection of C programming interfaces for Mac OS X, Apple's next-generation operating system.

NOTE: A programming interface is the set of functions and data structures defined by one piece of software, such as an operating system service, for use by client software, such as applications and device drivers. For example, you would access one programming interface to enable your application to print and another to manipulate your application's menus.

Carbon was originally designed to support the evolution of the Mac OS. Apple updated and reimplemented programming interfaces written for earlier versions of the Mac OS to take advantage of new features and enhancements in Mac OS X. Mac OS X brings many modern operating-system features to the Macintosh, including:

NOTE: Carbon is especially useful for developers who need to update older Mac OS code to run on Mac OS X. This book doesn't cover this porting process, but if you're interested, see Inside Carbon: Carbon Porting Guide (listed in Appendix A, "Additional Resources").

In addition to updating older features, Mac OS X adopts some industry-standard practices and introduces brand new technologies. To support these features for C-language programmers, Apple added new programming interfaces to the venerable Mac OS. The collection of updated and new programming interfaces is called Carbon.

As shown in Figure 1-1 Carbon is one of several application environments available on Mac OS X.

Figure 1.1. Carbon in relation to other Mac OS X environments

These other environments include:

These environments depend on the same application and core services for their operation, and the underlying services rely on Darwin (Apple's open-source core operating system) and the Mach kernel.

Each environment (including the optional BSD command-line environment) has advantages and disadvantages, but for C and C++ programmers, Carbon is the best choice.

What's in Carbon?

Carbon contains thousands of functions, data structures, and constants, but don't worry, this book uses only a small number of these. Related functions and data structures are organized into functional groups, usually referred to as managers or services. For example, the Window Manager contains functions and data structures that let you create, remove, and otherwise manipulate application windows.

In this book you'll use functions from only a handful of these managers and services, most of which are commonly used in all Carbon applications. These interfaces cover the following functionality:

You'll probably be surprised to see how few functions you need from each manager or service to implement basic functionality. That's what's great about Carbon on Mac OS X. The operating system and the development tools you'll use--Project Builder and Interface Builder--do most of the grunt work for you. For example, you might think you'd have to write a lot of complicated code to allow a user to drag a window around the screen. In actuality, you get this for free: the operating system automatically makes the appropriate calls for you and makes sure the dragged window behaves properly. Of course, if you need to control every aspect of an action or want to add custom features, Carbon lets you do that too. However, if you're willing to accept the standard behavior--and there is little reason not to--for most user interface elements, you'll have far less code to write.

Building Carbon Applications

Because Carbon is just a collection of C programming interfaces, you can use just about any Macintosh C development environment that supports PowerPC to build Carbon applications. However, there are some limitations. For example, Carbon developers can create applications in two different executable formats:

Older environments, such as Apple's MPW, can produce only PEF executables; while others, such as Metrowerks CodeWarrior, let you build either PEF or Mach-O versions. Which tool you use depends on what platforms you are targeting.

The tutorials in this book will use Project Builder and Interface Builder, which are Apple's development tools for building Mach-O-based applications on Mac OS X. Both tools have been written with Carbon in mind, and they come free with Mac OS X, making them excellent choices for new developers.

Project Builder and Interface Builder work in conjunction with each other to make building applications easier than ever:

The Carbon Factory Tour

Because Carbon contains many, many programming interfaces, it can be quite daunting to a new developer. While this book provides a good start, at some point you will need to begin fending for yourself. What interfaces you will require depends on the type of applications you want to write, but as is often the case, some are more commonly used than others. This section divides up the Carbon interfaces according to usage (from fundamental to esoteric) and gives some useful information about each one. To learn more about a particular interface, you can use the Carbon Help feature integrated into Project Builder (to be described later in this book). You can also consult the technical documentation on the Apple developer web site.

The Starter Kit

This section covers the interfaces most likely to be called by a Macintosh application (and covers those that can be found in this book). These managers and services provide basic user interface functionality as well as fundamental features, such as the ability to save and print files. You can build basic but fully functional Carbon applications using the interfaces described here.

NOTE: If Carbon were an interface to build automobiles, the Starter Kit interfaces would define the engine, wheels, brakes and other essential components, as well as the user interface (dashboard, steering wheel, gas and brake pedals, and so on).

The Toolbox interfaces

The following interfaces are grouped together because they generally work together to create and manage the user interfaces. In most cases, Interface Builder and the Interface Builder Services interface can handle the creation and control of the basic user interface. However, to accomplish more esoteric tasks, you may need to call additional functions in one of these so-called Human Interface Toolbox interfaces:

Aqua

Aqua is not an interface, but you need to follow the Aqua guidelines when designing and laying out your user interface. While the Carbon programming interfaces are flexible enough to let you do just about anything with your user interface, that doesn't mean you should. Just as adherence to common rules and customs when designing steering wheels and dashboards makes driving more pleasant and less confusing, following the Aqua guidelines lets your application provide the best possible experience for your users.

Behind the curtain

The following interfaces work behind the scenes, as it were, to provide the basic functionality that you expect from most applications:

The Expansion Pack

This section lists interfaces that are desirable, but not necessary for most applications. Full-featured commercial applications usually adopt a number of these interfaces.

NOTE: Continuing the automobile metaphor, these interfaces would add useful features that may be essential in some cases, such as power-steering, windshield wipers, and air conditioning.

NOTE: The Apple Event Manager is different from the Carbon Event Manager. While there is some overlap in their capabilities, Carbon events are typically received from the user interface or operating system, while Apple events come from scripts or other applications.

Specialty Interfaces

These are more esoteric interfaces that you generally would not use unless you were interested in creating specific types of applications. Some provide specialized features, while others expand on basic functionality (such as text manipulation). You use these interfaces to create highly sophisticated applications that take full advantage of the system software.

NOTE: For automobiles, these features would be for specific types of cars: turbocharging and tight suspensions for sports cars, flatbeds and towhooks for trucks, leather seats and soft suspensions for luxury cars, and so on.

QuickTime

QuickTime is Apple's multimedia programming interface. You use QuickTime to create and play file-based or streaming movies, virtual reality environments, sounds, and music files. The Carbon developer documentation site groups QuickTime into the following categories:

As you can see, the QuickTime programming interface is huge and somewhat daunting. However, a good place to start exploring is the book Discovering QuickTime, published by Morgan-Kaufmann (see Appendix A, "Additional Resources").

Color, images, and print production

These interfaces are for applications that create and manipulate images, such as a photo retouching program:

3D graphics

For high-quality 3D graphics, the interface of choice is OpenGL. Okay, in actuality this is an industry-standard interface and not part of Carbon, but it is fully compatible with Carbon (just make sure you link to the OpenGL framework when you build). You can use OpenGL's 3D rendering capabilities for everything from medical imaging to virtual reality to incredibly photorealistic games.

HTML

To display HTML text and images, you can use the HTML Rendering Library. Essentially, this interface allows you to render text and images in a window as if it were in a browser. It provides support for such design elements as border and scroll bars, as well as for navigation using URL links.

Speech

These interfaces let your application speak text or recognize speech:

Text and international services

Most of these interfaces are only for developers writing text-intensive applications or those that handle multiple text encodings. For basic text input and display, the Multilingual Text Engine provides a much simpler interface for most of the same functionality.

Internet and networking

If your application uses or enables network access, you will need to call functions from the following managers or services:

Security

If you want your application to provide security measures available on the Macintosh, you should consider these interfaces:

Low-level tweaking

If your application needs to access or control specific low-level functionality, you should consider these interfaces. Note, however, that Mac OS X is designed to shield applications from low-level workings of the system. To that end, you should use one of these interfaces only if you are sure you need its particular functionality:

Utility Interfaces

This section covers utility interfaces that may be useful, depending on the application. These managers and services aren't particularly related to any technology or functionality:

NOTE: If Carbon were for cars, these interfaces would add possibly useful features, such as headlight wipers, cup holders, curb feelers, and GPS units.

Legacy Interfaces

This section covers Carbon interfaces that are mostly of historical interest. These interfaces were included to assist developers porting legacy Mac OS code to Mac OS X. For new developers, these interfaces may be useful only to gain perspective on the evolution of Macintosh system software. In most cases, the functionality of these managers is covered in a newer interface.

NOTE: If Carbon were for cars, these interfaces would add older features such as hubcaps, ashtrays, carburetors, and whitewall tires.

Recap

We introduced Carbon, the C programming interface that lets you take advantage of features in Mac OS X, such as preemptive multitasking and protected memory. Carbon is a synthesis of old and new Mac OS technologies. Although there are many interfaces in Carbon, in most cases only a subset is essential for all applications. Others may be useful only for specialized needs or to support the porting of legacy applications. Of the numerous development environments available to build Carbon applications, this book will use Project Builder and Interface Builder, which are specifically designed for building applications on Mac OS X.

Next, we will design the application, a moon travel planning guide, that we will use to illustrate major Carbon programming concepts and methods.

Back to: Sample Chapter Index

Back to: Learning Carbon


O'Reilly Home | O'Reilly Bookstores | How to Order | O'Reilly Contacts
International | About O'Reilly | Affiliated Companies

© 2001, O'Reilly & Associates, Inc.
webmaster@oreilly.com