DISCOVERING OPENSTEP: a Developer Tutorial
Total Page:16
File Type:pdf, Size:1020Kb
DISCOVERING OPENSTEP: A Developer Tutorial Rhapsody Developer Release Apple Computer, Inc. User Interface Tips copyright © 1997 Apple Computer, Inc. All rights reserved. [6467.00] No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, mechanical, electronic, photocopying, recording, or otherwise, without prior written permission of Apple Computer, Inc., except to make a backup copy of any documentation provided on CD-ROM. Printed in the United States of America. The Apple logo is a trademark of Apple Computer, Inc. Use of the “keyboard” Apple logo (Option-Shift-K) for commercial purposes without the prior written consent of Apple may constitute trademark infringement and unfair competition in violation of federal and state laws. No licenses, express or implied, are granted with respect to any of the technology described in this book. Apple retains all intellectual property rights associated with the technology described in this book. This book is intended to assist application developers to develop applications only for Apple-labeled or Apple-licensed computers. Every effort has been made to ensure that the information in this manual is accurate. Apple is not responsible for printing or clerical errors. Apple Computer, Inc. 1 Infinite Loop Cupertino, CA 95014 408-996-1010 Apple, and the Apple logo are trademarks of Apple Computer, Inc., registered in the United States and other countries. NeXT, the NeXT logo, NEXTSTEP, the NEXTSTEP logo, Digital Librarian, NeXTmail, and Workspace Manager are trademarks of NeXT Computer, Inc. All other trademarks mentioned belong to their respective owners. Written by: Terry Donoghue Tutorial applications by: Terry Donoghue Art and Production management: Terri FitzMaurice Book design: Karin Stroud Publications management: Ron Hayden With help from: Trey Matteson, Ron Hayden, Jean Ostrem, Lynn Cox, Derek Clegg, Mike Ferris, and Frederic Bonnard Cover design: CKS Partners, San Francisco, California Table of Contents Welcome to Rhapsody 7 Extending the To Do Application 170 Overriding Behavior of an Application Kit Class: An Example 174 A Simple Application 18 Creating and Managing an Inspector (ToDoInspector) 176 Creating the Currency Converter Project 22 Overriding and Adding Behavior to a Class: An Example 194 Creating the Currency Converter Interface 24 Setting Up Timers 200 Customizing the Application’s Window 24 Build, Run, and Extend the Application 203 Fields and Buttons 28 Optional Exercises 203 Some Finishing Touches 32 Defining the Classes of Currency Converter 38 Appendices Connecting ConverterController to the Interface 44 Building the Currency Converter Project 54 A Object-Oriented Programming 206 Run Currency Converter 57 Encapsulation 209 Messages 210 A Forms-Based Application 60 An Object-Oriented Program 211 Polymorphism and Dynamic Binding 212 Creating the Travel Advisor Interface 64 Classes 214 Getting Started 64 Object Creation 214 Getting the Table View to Work 97 Inheritance 215 Defining a Class 218 A Multi-Document Application 118 OPENSTEP Frameworks 219 Setting Up the To Do Project 122 Creating the Model Class (ToDoItem) 123 B Programming Tools and Resources 222 Setting Up the Programmatic Interface 123 Other Development Applications 224 Extending an Application Kit Class: An Example 131 Other Installed Frameworks 224 The Basics of a Multi-Document Application 142 Useful Command-Line Tools 225 Managing Documents Through Delegation 153 Other Programming Resources 226 Managing ToDo’s Data and Coordinating its Display 162 iii 1 What You’ll Learn What OpenStep is How OpenStep fits in with Rhapsody Rhapsody technologies Programming in Apple’s development environment Programming with objects 6 Chapter 1 Welcome to Rhapsody Welcome to OpenStep. OpenStep is a new way to make programs that run on Power PC Macintoshes. But OpenStep itself is not new. It is proven technology, and although it poses a learning curve for newcomers, once you learn it, application development will suddenly seem easier and quickened with potential. This book eases your way into the experience of OpenStep programming. It guides you through the creation of several applications. It encourages you to explore, to “kick the tires.” Along the way, it explains important concepts and paradigms, and it uncovers rich lodes in the tools and APIs. When you’ve worked through this book, Apple’s development environment will not only be less mysterious, but will be an environment that you’ll want to program in. This chapter presents a brief overview of OpenStep—the user experience and the developer experience—and places it in the larger context, which is the next-generation Macintosh operating system. 7 Chapter 1 Welcome to Rhapsody Imagine a Macintosh... Imagine a Macintosh: Applications and Components • That doesn’t crash when an application crashes. Application Kit • That can draw a graph, send a fax, and play a movie, all at once. (With Graphics API's) • That seems much faster than any Windows system you’ve seen. Frameworks Foundation • That looks a lot like the Mac OS, only better. (With Distributed Objects) Display PostScript • That features some of the most advanced software technology around. • And that still runs all the Macintosh programs you know and love. Core Operating System That Macintosh will soon be here. And you’re invited to be a part of its genesis. What is OpenStep? Application Kit Consists of classes that generate user-interface objects, that manage and process events, and that offer or assist in application services such The new Macintosh operating system, code-named “Rhapsody,” has as color and font management, printing, text manipulation, and cut-and-paste. actually been around a long time—almost as long as the Mac itself. That’s Display PostScript Provides APIs for direct PostScript drawing and image because it is based on OpenStep, which, as NEXTSTEP, was introduced in composition, as well as for low-level window, cursor, and event management. The 1987. Since then, OpenStep has evolved through many releases, has been Application Kit uses these APIs to draw the objects of the user interface. (New adopted by many companies as their development and deployment Graphics APIs will offer a higher level of abstraction for drawing operations.) environment of choice, and has received glowing reviews in the press. It is solid technology, based on a design that was years ahead of anything else Both the Foundation and Display PostScript frameworks interact directly with the and perfected year after year. core operating system (Display PostScript with the windowing and imaging subsystem); applications can access operating-system services through the APIs So OpenStep is well-regarded and battle-tested. But what is it? OpenStep is of these frameworks. an integrated set of shared object libraries, or frameworks, plus a run time and a development environment that do three principal things: The part of Rhapsody called the Yellow Box augments and enhances the core OpenStep frameworks with many other frameworks. See page 10 for details. • They insulate programs from the internal workings of the core operating system, mediating access to system resources and preventing programs from trashing one another’s address space. OpenStep and Mach • They provide all (or almost all) the functionality that programs typically You can think of OpenStep as a layer of objects that acts as mediator and need. facilitator between programs and the core operating system. The stability, performance, and reliability of these programs therefore depend on whether the • They bring the benefits of object-orientation to program development underlying core operating system has these characteristics. (see page 14). The core operating system for Rhapsody is an enhanced version of Mach and BSD. OpenStep has three core object frameworks: Foundation, Application Kit, Mach—the original foundation of NEXTSTEP—is a mature, robust kernel that and Display PostScript. provides low-level services such as memory management, tasking, synchronization, timing, and event messaging. These services form the basis of Foundation Defines basic object characteristics, and implements advanced operating-system capabilities: preemptive multitasking, memory mechanisms for object allocation, deallocation, introspection, and protection, full symmetric multiprocessing, and high-performance I/O. distribution. Foundation includes classes for common data types, such as strings, numbers, and collections. And it provides APIs for platform- Rhapsody’s core operating system will eventually support a variety of file and independent system services, such as dates and times, multithreading volume formats, including HFS, UFS, DOS FAT, ISO9660, AFP, and HFS+ (an support, task and process management, timers, file management, enhanced version of HFS). notification, and internationalization (based on the Unicode standard), 8 Rhapsody: Where OpenStep Fits In Advanced look and feel. The new Rhapsody operating system sports a new exterior that perfects the Mac OS, itself legendary for ease of use. The Rhapsody is a ensemble of technologies in which OpenStep plays a central Rhapsody user experience combines the best visual elements and usage role. The diagram below depicts Rhapsody on PowerPC-based systems; it models of the Mac OS and of OPENSTEP for Mach, and it incorporates new shows an enhanced and expanded version of OpenStep as the Yellow Box. paradigms in human-interface design. Yet it is still recognizably a The Yellow