Building Cocoa Applications: A Step by Step Guide

Book description

Cocoa is an object-oriented development environment available in Apple's Mac OS X environment. Mac OS X, a unified operating system and graphical operating environment, is the fastest growing Unix variant on the market today. Hard-core Unix programmers, developers who cut their teeth on classic Mac operating systems, and developers who cherished NeXTSTEP, the decade-old system on which today's Cocoa is based -- all are flocking to Cocoa, and they need a lot more practical information than is currently available from Apple. There is a lot to learn. Building Cocoa Applications is an ideal book for serious developers who want to write programs for the Mac OS X using Cocoa. It's a no-nonsense, hands-on text that's filled with examples -- not only simple and self-contained examples of individual Cocoa features, but extended examples of complete applications with enough sophistication and complexity that readers can put them to immediate use in their own environments. Building Cocoa Applications takes a step-by-step approach to teaching developers how to build real graphics applications using Cocoa. By showing the basics of an application in one chapter and then layering additional functionality onto that application in subsequent chapters, the book keeps readers interested and motivated. Readers will see immediate results, and then go on to build onto what they've already achieved. The book is divided into four major parts: Part I introduces the Mac OS X graphical user interface (Aqua) from a developer's point of view, Cocoa developer tools (such as the Interface Builder, Project Builder, and gdb debugger), object-oriented concepts, the Objective-C language in which Cocoa is written, and the basics of Cocoa programming itself. Part II focuses on building the first complete application, Calculator, a simple four-function calculator. The chapters in this part of the book extend the application, piece by piece, by introducing such features as nibs, icons, delegation, resizing, events, and responders. Part III focuses on building an application called MathPaper, which is similar to a word processor but which instead solves mathematical expressions the user supplies. The chapters in this part of the book extend MathPaper by developing both the front and back ends using a variety of Cocoa classes and methods. They introduce Cocoa'sdocument-based architecture, tasks, pipes, Rich Text format, handling document files, and using Quartz to draw in windows. Part IV focuses on building the GraphPaper application, a more complex multithreading application that graphs mathematical functions in multiple dimensions and that uses mouse-over capabilities to identify graph points. The chapters in this part of the book add more advanced Mac OS X features such as multithreading, color, mouse events, zoom buttons, pasteboards, services, preferences, and the defaults database. By the end of the book, readers who have built the applications as they have read will have a solid understanding of what it really means to develop complete and incrementally more complex Cocoa applications. The book comes with extensive source code available for download from the O'Reilly web site, along with an appendix listing additional resources for further study.

Publisher resources

Download Example Code

Table of contents

  1. Building Cocoa Applications: A Step-by-Step Guide
    1. Preface
      1. Cocoa and Mac OS X
        1. Mac OS X and Cocoa Components
        2. Object-Oriented Programming
        3. Cocoa Versions
        4. Cocoa, Objective-C, and Java
        5. The Foundation Classes
        6. Drawing with Quartz
        7. Mac OS X and Classic Mode
      2. Organization of This Book
        1. Part I, Cocoa Overview
        2. Part II, Calculator: Building a Simple Application
        3. Part III, MathPaper: A Multiple-Document, Multiprocess Application
        4. Part IV, GraphPaper: A Multithreaded, Mouse-Tracking Application
      3. What You Will Need
      4. Conventions Used in This Book
      5. Comments and Questions
      6. Acknowledgments
    2. I. Cocoa Overview
      1. 1. Understanding theAqua Interface
        1. What Makes Mac OS X So Special?
        2. A Quick Look at the Mac OS X User Interface
        3. Basic Principles of the Aqua Interface
          1. The Toolbox, Carbon, and Cocoa
          2. Consistent Aqua
        4. The Mouse and Cursor
          1. Mouse Action Paradigms
          2. Cursors
        5. Window Types and Behavior
          1. Document Windows
          2. Utility Windows
          3. Dialogs
          4. Alerts
          5. Info Dialogs
          6. Multi-View Windows
          7. Windows with Drawers
          8. Main and Key Windows
          9. Window Order
        6. Menus and the Menu Bar
        7. The Dock
        8. Controls
          1. Buttons
            1. Push buttons
            2. Radio buttons and checkboxes
          2. Pop-up Menus, Command Pop-down Menus, and Combination Boxes
          3. Text Fields and Scrolling Lists
          4. Sliders and Scrollers
          5. Color Wells and Image Wells
          6. Disclosure Triangles
          7. Other Controls
        9. The Finder
          1. The Finder Window
          2. The Toolbar
          3. The Menu Structure
          4. Support Windows and Dialogs
        10. Configuring Your Desktop, Step by Step
          1. Working with the Dock
          2. The Finder’s Viewing Options
          3. Finder Preferences
          4. System Preferences
        11. Menu Guidelines and Keyboard Equivalents
          1. Application Menu
          2. File Menu
          3. Edit Menu
          4. Find Submenu
          5. Format and Font Menus
          6. Window Menu
          7. Services Submenu
        12. Working with the Filesystem,Step by Step
          1. Working with Folders
          2. Working with Files
          3. Forcing an Application to Quit
          4. Moving and Copying Files
          5. Moving Without Activating
          6. Getting Information About Files
          7. Filling and Emptying the Trash
        13. Summary
        14. Exercises
        15. References
      2. 2. Tools for Developing Cocoa Applications
        1. Developer Tools
          1. Project Builder
          2. Interface Builder
          3. ObjectAlloc
          4. PropertyListEditor
          5. IconComposer and the icns Browser
        2. Utilities
        3. Working with the Terminal
          1. History of the Video Display Terminal
          2. The Mac OS X Terminal Application
          3. Working in a Unix Shell, Step by Step
          4. The Defaults System
        4. Debugging Programs with gdb
          1. Using gdb in Project Builder, Step by Step
          2. Using gdb in a Terminal Window
          3. gdb Commands
        5. User Interface Design
        6. Summary
        7. Exercises
      3. 3. Creating a Simple Application with Interface Builder
        1. Getting Started with Interface Builder
        2. Adding Objects to Your Application
          1. Adding a Button Object to Your Window
          2. Giving Your Button a Funky Sound
          3. Inspecting and Changing Your Button’s Attributes
        3. Objects, Messages, and Targets
          1. Objects and Classes
          2. Targets, Actions, and Connections
        4. Summary
        5. Exercise
      4. 4. An Objective-C ApplicationWithout Interface Builder
        1. The Tiny.m Program
        2. An Introduction to Objective-C
          1. Objects and Classes
          2. Methods and Messaging
          3. The id Data Type
          4. A Simple Class Example
          5. Creating and Initializing Objects
          6. The @interface Directive
          7. The #import Directive
          8. Destroying Objects
          9. The @implementation Directive
          10. The +alloc Method and the NSObject Root Class
          11. NSString, NSMutableString, and NSLog
          12. autorelease and the NSAutoreleasePool Class
        3. Tiny.m Revisited
          1. Windows, Views, Delegates, and the setup( ) Function
          3. Drawing with Quartz Inside a View Object
        4. Summary
        5. Exercises
        6. References
    3. II. Calculator: Building a Simple Application
      1. 5. Building a Project: A Four-Function Calculator
        1. Getting Started: Building the Calculator Project
        2. Building the Calculator’s User Interface
          1. Customizing the Main Window
          2. Adding Controls in a Window
          3. NSMatrix Dragging Options in IB
        3. Building the Calculator’s Controller Class
          1. Designing the Controller Class
          2. Creating the Controller Class
          3. Outlets and Connections
          4. Adding Outlets to an Object
          5. Adding Actions to the Controller
          6. Creating the Controller Class Files
          7. Adding Code to Make the Controller Class Work
        4. Customizing Buttons and Making Connections
          1. Instantiating (Creating an Instance of) the Controller Class
          2. Setting Up Tags and Titles for the Keypad Buttons
          3. Making the Connections
        5. Compiling and Running a Program
          1. Compiling and Running a Program from PB
        6. Compiler Error Messages
        7. The enterDigit: Action Method
        8. Adding the Four Calculator Functions
        9. Adding the Unary Minus Function to the Controller Class
          1. Using IB’s Read Files Commandwith a New Action Method
        10. The Files in a Project
          1. The main.m Program File Generated by PB
          2. Other PB-Generated Files
        11. Summary
        12. Exercises
      2. 6. Nibs and Icons
        1. Customizing MainMenu.nib
          1. Changing the Application Menu
          2. Changing the Strings in the About Box
        2. Managing Multiple Nibs
          1. Modifying the Controller Class
          2. Modifying the Main Calculator Nib
          3. Creating the About Box Nib
        3. Adding Icons to Applications
          1. Viewing Icons with the icns Browser Application
          2. Creating Application Icon Files with IconComposer
        4. Changing Calculator’s Application Icon
          1. Making the New Icon Appear in the About Box
        5. Cocoa’s NSImage Class
        6. Summary
        7. Exercises
        8. References
      3. 7. Delegation and Resizing
        1. Handling Different Bases
          1. Modifying the Controller Class
        2. Delegation
          1. Should, Will, and Did Delegates
          2. Specifying an Object’s Delegate
          3. Setting Up a Delegate Outlet in the Nib
          4. Adding the Delegate Method to the Controller
        3. Disabling Buttons for BetterMultiradix Input
          1. Accessing NSMatrix Cells with an NSArray Object
          2. Coherence in Object-Oriented Programming
        4. Resizing Windows Programmatically
          1. Modifying the Calculator’s Interface
          2. Modifying the Controller Class
        5. Two Very Important Classes: NSWindow and NSView
          1. The NSWindow Class
          2. The NSView Class
        6. Summary
        7. Exercises
      4. 8. Events and Responders
        1. Events and the NSResponder Chain
          1. What Is an Event?
          2. The NSEvent Object
        2. Events and the NSApplication Object
          1. Responders and the NSResponder Chain
          2. Mouse Event Handling
          3. Keyboard Event Handling
          4. An Event-Handling Example Using TextEdit
          5. Action Messages and the NSResponder Chain
          6. Other Kinds of Events
        3. The Event Loop
          1. The Main Event Loop
        4. Catching Keyboard Eventsfor Our Calculator
          1. Subclassing the NSWindow Class
          2. Dictionaries
          3. Implementing the CalcWindow Class
            1. Methods for searching for button titles
            2. Finishing off the CalcWindow class implementation
          4. Changes in the Controller Class
        5. Summary
        6. Exercises
        7. References
      5. 9. Darwin and the Window Server
        1. Unix, Mach, and the Mac OS X Environment
          1. Operating Systems
          2. Unix
          3. Mach
            1. The Mach system support environment
            2. Usernames and UIDs
            3. Processes, PIDs, and UIDs
        2. The Window Server and Quartz
          1. The Application Kit and the Window Server
        3. Seeing All the Processes
        4. Summary
        5. Exercises
        6. References
    4. III. MathPaper: A Multiple-Document, Multiprocess Application
      1. 10. MathPaper and Cocoa’sDocument-Based Architecture
        1. The MathPaper Application
        2. The Evaluator Back End
          1. lex and yacc
          2. Building the Back End
        3. Cocoa’s Document-Based Architecture
        4. Building MathPaper’s Front End
          1. Setting Up the MathPaper Project
          2. Changing the Names of MathPaper Project Files
          3. The MathDocument Class
          4. The MainMenu.nib File
          5. Customizing the Document-Based Project Information in PB
          6. Setting Up PaperWindow.nib
          7. Testing MathPaper’s Document-Based Architecture
        5. Summary
        6. Exercises
        7. References
      2. 11. Tasks, Pipes, and NSTextView
        1. Processes, Pipes, and Resources
        2. Making Evaluator a MathPaper Auxiliary Executable
        3. MathDocument Class Modifications
        4. Creating PaperController, a Subclass of NSWindowController
        5. The NSScrollView and NSTextView Classes
          1. NSTextView Class Basics
        6. PaperController Class Modifications
          1. Creating the NSTextView Delegate
          2. Getting Data from Evaluator
          3. Adding a Method Using a Category
          4. Killing the Evaluator Processes
          5. Giving Proper Titles to MathPaper Windows
        7. Summary
        8. Exercises
      3. 12. Rich Text Format and NSText
        1. Rich Text Format
          1. Formatting NSTextView Output
          2. Rich Text Syntax
          3. Using TextEdit to Explore RTF
          4. RTF Control Words and Symbols
        2. Creating an RTF Class
        3. Integrating Our RTF Class into MathPaper
        4. Summary
        5. Exercises
      4. 13. Saving, Loading, and Printing
        1. Data Management with NSDocument
          1. Memory to Disk and Back
          2. Rethinking MathDocument and PaperController
        2. Saving to a File
          1. Icons for MathPaper
          2. Synchronizing PaperController with MathDocument
          3. Archiving MathPaper Documents
          4. Writing the Save Methods
          5. Testing the Save Features
          6. Advanced Save Panel Options
        3. Loading from a File
        4. Marking a Document Window as Edited
        5. Adding Printing Capability
        6. Summary
        7. Exercises
      5. 14. Drawing with Quartz
        1. Animation in an About Panel
        2. The Quartz Window Server
          1. Buffered, Retained, and Nonretained Windows
          2. Drawing in an NSView with Quartz
        3. Implementing the About Panel in MathPaper
          1. Creating the MathAnimation View
        4. Quartz Graphics Data Types
          1. Setting Colors, Drawing Rectangles, and Drawing Lines
          2. Drawing Text with Quartz
          3. Drawing Images with Quartz
        5. Timers
          1. Adding and Removing Timers
        6. Putting It All Together
          1. Adding an Easter Egg
        7. Summary
        8. Exercises
        9. References
      6. 15. Drawing in a Rectangle: More Fun with Cocoa Views
        1. The Advantages of NSView’s drawRect: Method
        2. BlackView: An NSView That Paints Itself Black
        3. A Closer Look at the NSView Class
          1. NSView Coordinate Systems
          2. Moving and Resizing Views
          3. Flipping
          4. The NSView Hierarchy
          5. Opaque and Nonrectangular Views
          6. Controlling Display and Redisplay
          7. Resizing
        4. BarView: An NSView with a Scaled Coordinate System
        5. PolygonView: A Non-Opaque NSView
          1. Changing the PolygonView’s Size
          2. Placing an NSView Inside an NSScrollView
        6. Responding to Events in an NSView
          1. Getting a Mouse-Down Event
        7. Autosizing Multiple Views in a Window
        8. Summary
        9. Exercises
    5. IV. GraphPaper: A Multithreaded, Mouse-Tracking Application
      1. 16. GraphPaper: A Multithreaded Application with a Display List
        1. GraphPaper’s Design
          1. The Interface
          2. Connecting to the Back End
          3. Why Use a Display List?
        2. Working with Multiple Threads
          1. Unix Pipes and Evaluator
          2. Threads
          3. Locking with NSLock
          4. Launching Threads with NSThread
        3. Building the GraphPaper Application
          1. Changes to the Evaluator Back End
          2. Building GraphPaper’s Interface
          3. The GraphView Class Interface File
          4. The GraphView Class Implementation File
          5. The initWithFrame: Method
          6. Implementing the Display List
          7. Scaling the GraphView and the drawRect: Method
          8. The Data Stuffer Methods
          9. Stopping a Running Graph
          10. The Graph Displayer
          11. The Segment Class
          12. Testing GraphPaper
        4. Extending the Display List
          1. Adding Axes
          2. Adding Labeling
          3. Using the Label Class
        5. Summary
        6. Exercises
        7. References
      2. 17. Color
        1. Colors and Color Objects
          1. Colors from a Programmer’s Point of View
          2. Colors from a User’s Point of View
          3. Programming with Color
        2. Adding Color to GraphPaper
          1. Creating a Preferences Panel
          2. The Controller Class
          3. Creating the Preferences Nib, Panel, and PrefController
          4. PrefController Class Implementation
          5. ColorGraphView
          6. The ColorGraphView Class Implementation
          7. Setting the Colors
          8. Setting the Initial Color
          9. The GraphView, Segment, and Label Classes
          10. Testing GraphPaper’s Color
        3. Summary
        4. Exercises
      3. 18. Tracking the Mouse
        1. Tracking the Mouse
          1. Tracking Rectangles
          2. Accepting Mouse-Moved Events
        2. Adding Mouse Tracking to GraphPaper
          1. Changes to the GraphPaper Interface
          2. Changes to the TrackingGraphView Class Files
        3. Summary
        4. Exercises
        5. References
      4. 19. Zooming and Saving Graphics Files
        1. Adding a Zoom Button to GraphPaper
          1. The NSScrollView Class Revisited
          2. Changes to MainMenu.nib
          3. Changes to ZoomScrollView
          4. Testing the Zoom Button
          5. Autosizing in GraphPaper
        2. Saving to PDF
          1. Producing PDFs from NSView
          2. Changes to MainMenu.nib
          3. Changes to the Controller Class
          4. Testing the Save Graph Menu Command
        3. Saving to TIFF
        4. Creating an Accessory NSView
          1. Changes to the Controller Class
          2. Testing the PDF and TIFF Save Feature
        5. Summary
        6. Exercises
      5. 20. Pasteboards, Services, Modal Sessions, and Drag-and-Drop
        1. Cut, Copy, and Paste with the Pasteboard
          1. Types of Pasteboards
          2. Users and Pasteboards
          3. Providing Data to the Pasteboard
        2. Using the Pasteboard in GraphPaper
          1. Providing Data Through Lazy Evaluation
          2. Implementing the Cut Command
          3. Testing GraphPaper’s Copy and Cut Commands
        3. Services
          1. How Services Work
        4. Creating Your Own Service
          1. Modifications Required for GraphPaper to Implement Services
          2. Creating the Services Advertisement
          3. Modification of GraphView
          4. Changes to Controller
          5. Testing GraphPaper’s Service
        5. Drag-and-Drop
          1. Being a Drag-and-Drop Source
          2. Being a Drag-and-Drop Receiver
        6. Summary
        7. Exercises
      6. 21. Preferences and Defaults
        1. Preferences and the Defaults Database System
          1. Accessing the Defaults Database with PropertyListEditor
          2. Accessing the Defaults Database in a Terminal
          3. Defaults Domains
            1. Persistent versus volatile defaults
            2. Standard defaults domains
          4. The NSUserDefaults Class
        2. Adding Defaults to GraphPaper
          1. Registering the Default Values
          2. Reading Values from the Defaults Database
        3. Making the Preferences Panel Work with Defaults
          1. Modifying the Preferences Panel
          2. Changes to the PrefController Class
          3. Testing the Updated Preferences Panel
        4. Setting Up a Multi-View Panel
          1. Tab Views
          2. Loading and Saving the Default Values
        5. Summary
        6. Exercises
    6. A. Cocoa Resources
      1. Apple Resources
        1. Online Documentation
        2. Apple Developer Connection
        3. Sample Code
        4. Worldwide Developers Conference
        5. Registering Creator Codes
        6. Bug Reporting
      2. Third-Party Resources
        1. Mailing Lists
        2. Web Sites
        3. Printed Resources
    7. Index
    8. Colophon

Product information

  • Title: Building Cocoa Applications: A Step by Step Guide
  • Author(s):
  • Release date: May 2002
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596002350