← Macintosh C Programming By Example 1991

Macintosh C Programming By Example 1991

From the originals

13 scans

Machine-transcribed text and image descriptions. Check the linked original for uncertain words or fine print.

These are transcribed pages from the documents, not the full publications. Open the original for the remaining pages.

Image 1

MACINTOSH C PROGRAMMING BY EXAMPLE

MACINTOSH® PROGRAMMING SERIES ONE 3.5" DISK INCLUDED MACINTOSH® C PROGRAMMING BY EXAMPLE A Step-by-Step Guide to Developing Programs with THINK C™ KURT W. G. MATTHIES THOM HOGAN Microsoft PRESS®

A book cover with a peach background, bold blue title text, a green subtitle on the left, and an illustration of concentric orange and purple rings with a glossy sphere, a diagonal beam, binary digits, and a purple building silhouette on a teal panel, plus a Microsoft Press logo and a 3.5-inch disk icon in the top right.

Reading notes: The ® and ™ symbols are small and may be approximated in the transcript. Reading order of the separate text blocks is inferred from layout.

Printed product names
Macintosh C Programming by Example
Languages
English
PDF page 1

ill ACINTOSH® PROGRAMMING SERIES A Step-by-Step Guide to Developing Programs with THINK C™ KURT W. G. MAnHIES THOM HOGAN DISK INCLUDED

Reading notes: Transcript is intentionally empty because exact text is preserved separately; other fields come only from the supplied extracted copy. The headline is assembled from several title lines and may not match the exact line breaks or styling on the cover. The leading fragment 'ill' and the spelling 'MAnHIES' appear to be extraction artifacts and may not match the printed text; the author name may be 'MATHIES' and needs verification against the image. 'KURT W. G. MAnHIES' and 'THOM HOGAN' are author names only as they appear in the supplied copy; reading order between the series banner and title is uncertain. No prices or printed dates appear in the supplied copy.

Printed product names
THINK C™
Languages
English
PDF page 2

The image is a entirely blank white page with no visible artwork, layout elements, or text.

Reading notes: No visible text found; image appears to be a blank white page.

PDF page 3

MACINTOSH® C PROGRAMMING BY EXAMPLE KURT W. G. MATTHIES THOM HOGAN Microsoft PRESS®

A book cover with large black bold shadowed title text at the top, the authors' names in the lower right, and a black Microsoft Press logo box with registered trademark symbol in the bottom right corner on a white background.

Reading notes: Registered trademark symbols after MACINTOSH and Microsoft Press are small and included as printed.

Printed product names
Macintosh
Languages
English
PDF page 4

PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright© 1991 by Kurt W.G. Matthies All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. Library of Congress Cataloging-in-Publication Data Matthies, Kurt W.G., 1954- Macintosh C programming by example : a step by step guide to developing programs with Think CI Kurt W.G. Matthies, Thom Hogan. p. cm. Includes index. ISBN 1-55615-357-0: $34.95 ($44.95 Can.) 1. Macintosh (Computer)--Programming. 2. C (Computer program language) I. Hogan, Thom, 1952- II. Title. QA76.8.M3M37648 1991 005.265--dc20 91-37506 CIP Printed and bound in the United States of America. 123456789 MLML 654321 Distributed to the book trade in Canada by Macmillan of Canada, a division of Canada Publishing Corporation. Distributed to the book trade outside the United States and Canada by Penguin Books Ltd. Penguin Books Ltd., Harmondsworth, Middlesex, England Penguin Books Australia Ltd., Ringwood, Victoria, Australia Penguin Books N.Z. Ltd., 182-190 Wairau Road, Auckland 10, New Zealand British Cataloging-in-Publication Data available. Apple~ Mac~ and Macintosh® are registered trademarks of Apple Computer, Inc. Macreations "': Tycho "': and Tycho Table Maker™ are trademarks of Macreations Publishing Corp. Symantec® and THINK C® are registered trademarks of Symantec Corporation. Acquisitions Editor: Matjorie Schlaikjer Project Editor: Erin O'Connor Technical Editor: Jeff Carey

Reading notes: ISBN and Library of Congress catalog number are OCR-spaced; digits may need verification Some copyright and trademark symbols and quotation marks are garbled in the source text

Printed product names
Macintosh C Programming by Example · THINK C
Printed prices
$34.95 · $44.95 Can.
Printed dates
1991 · 1954 · 1952 · 91-37506
Languages
English
PDF page 5

CONTENTS PREFACE vii I. PROGRAMMING THE MACINTOSH WITH THINK C I 2. USING THINK C 9 3. ACPRIMER 35 4. MACINTOSH APPLICATION FUNDAMENTALS 69 5. MACINTOSH MEMORY MANAGEMENT 87 6. INTRODUCTION TO THE GENERIC APPLICATION 113 7. A SHELL THAT MANAGES MULTIPLE DOCUMENTS 137 8. SCROLLING WINDOWS 163 9. LOSER: A LESSON IN PROGRAM DESIGN 191 10. THE MACINTOSH FILE SYSTEM 217 II. BROWSER: OUR CULMINATING APPLICATION 237 APPENDIX: SYSTEM 7.0 COMPATIBILITY 259 INDEX 270

Reading notes: Headline not clearly identified; source is a table of contents rather than a headline-bearing advertisement

Languages
English
PDF page 6

PRIFACI Why anyone would want to do what I do for a living-sit day and night in front of a stolid, one-eyed deity and wrestle with abstract bits of mind-fluff-continues to be a source of wonder-to me and to my long-suffering family. Is it for the long hours, the backaches, the spreading middle, the chronic tic of eye strain? For all this and more. Sarcastic reflections aside, if you have the right temperament, programming the Macintosh can be one of the most rewarding preoccupations in this wide world. Granted, it can turn ugly as fast as the weather changes here in Colorado's Front Range. To paraphrase Robert Pirsig in Zen and the Art of Motorcycle Maintenance, writing a Macintosh application requires "great peace of mind." One thing is for sure: A Macintosh application's ease-of-use is directly proportional to the effort the programmer puts into writing it. Creating a Macintosh applicaton is tricky under the best conditions, and learning to write a maintainable application that's relatively free of errors takes years of experience. We've written this book to reduce the time it takes you to become an effective Macintosh programmer. You already know how to program? So did we. I came to Macintosh programming in 1986 with a strong background in the UNIX environment, and Thom had spent years in CP/M and MS-DOS programming. In spite of our combined experience, we weren't prepared for the Macintosh's totally different software development require- ments. And, unfortunately for us, back then there weren't many Macintosh applica- tion examples around to teach us Mac ways. I've always found that I learn programming techniques best by example. If I can find a code example that does the kind of thing I want to do, I use that code as a founda- tion and work a "variation on a theme" to solve my particular programming prob- lem. This book provides the foundation common to almost all Macintosh application programming. The book came out of a relationship between Thom and me that's spanned several years and several ventures. Early in 1990, we started up a series of columns for MacUser magazine. The Power Programming series ran from February 1990, through June 1991. Each column focused on skills in a particular area of Macintosh programming: windows, menus, dialog boxes, text files, graphics, file-handling, and so on. What was unique in our approach was that we always demonstrated the spe- cialized skill in the context of whole applications. Writing programs for the Macintosh requires much more than knowing how to use the various Macintosh Toolbox software managers-the utility routines embedded in the Macintosh read-only memory chips that display windows, report the mouse location, and read menu selections. Effective Macintosh application programming also requires that you engineer your program code so that your application uses memory and the Toolbox routines efficiently. vii

Reading notes: Headline 'PRIFACI' may be a preface heading with uncertain spelling. Line-break hyphenation and running text in the source may affect reading order.

Printed product names
Macintosh · Zen and the Art of Motorcycle Maintenance · UNIX · CP/M · MS-DOS · MacUser · Power Programming
Printed dates
1986 · 1990 · February 1990 · June 1991
Languages
English
PDF page 7

MACINTOSH C PROGRAMMING BY EXAMPLE

MACINTOSH C PROGRAMMING BY EXAMPLE The approach we used in the Mac User columns got a lot of positive feedback from readers. Experienced programmers said, "I wish I'd had this when I was learning to write for the Mac." Beginners expressed their gratitude and enthusiastically de- manded more. Last year, Microsoft Press gave us the opportunity to publish the particulars of our approach in book form, a boon that has allowed us to rethink the columns without the magazine's 2500-word limit hanging over our heads. We've rethought, expanded, and rewritten the Power Programming columns. Now you'll find three full applica- tions-Generic Application, Loser, and Browser-each of which describes a Mac- intosh programming paradigm. We develop the applications over the course of the book's chapters, and you'll find fully finished copies on the disk that accompanies the book. And we've added a chapter on the C language that makes a point of going into aspects of the language that require special attention in the Macintosh program- ming context. We thank everyone at Microsoft Press for helping this book come into being espe- cially Marjorie Schlaikjer, the acquisitions editor whose superhuman stamina saw the writing of this book through to completion; our technical editor Jeff Carey, who kept us honest; and our manuscript editor Erin O'Connor, who provided an antidote to 20 years of bad writing habits. Principal proofreader Deborah Long made many valu- able suggestions, and text processors Debbie Kem and Barb Runyan prepared the manuscript for typesetter Carolyn Magruder's ministrations. Kim Eggleston, Lisa Sandburg, and Peggy Herman collaborated on the design, art, and layout. While we're at it, we want to thank Paul Somerson, Jim Bradbury, and Rhoda Sim- mons at MacUser. Their work with us on the Power Programming series was invaluable. I'd like to make special acknowledgment of the best software design~r I ever met, Dennis K. Ward, whose thinking permeates the code examples in this book. Dennis and I have worked so closely together on Macintosh projects that it's no longer clear where his ideas about Macintosh applications end and mine begin. I also want to thank my good friend David J. Hall, who introduced me to the Macintosh way back when. And eternal love and kisses to Debby, Jason, Adam, and Jamie, who let me hang around the house all day in my pajamas. I sincerely hope this book helps you become a better Macintosh programmer. With the new, low-priced Macintoshes, System 7.0's interapplication communications fa- cilities, and the latest agreement between Apple and IBM, the Macintosh seems here to stay. The need for Macintosh software is greater than ever, and the opportunities available to Macintosh programmers have never been better. Even if programming is a pain in the neck, it sure beats working for a living. viii Kurt W.G. Matthies Boulder, Colorado September 13, 1991

Reading notes: Possible OCR spacing artifacts in 'design~r' and 'Peggy Herman' name spans Front-matter page number 'viii' appears mid-sequence, reading order near acknowledgments may be uncertain

Printed product names
Macintosh · Microsoft Press · MacUser · System 7.0 · Apple · IBM · Power Programming · Generic Application · Loser · Browser
Printed dates
September 13, 1991 · 1991 · 20 years · 2500-word
Languages
English
PDF page 8

PROGRAMMING THE MACINTOSH WITHTHINKC

1 PROGRAMMING THE MACINTOSH WITHTHINKC Why would anyone want to become a Macintosh programmer? The answer is more complex than you might think. Several circumstances make a familiarity with pro- gramming the Macintosh helpful if not downright necessary. • Mac users are becoming more sophisticated and are demanding more from their machines. Many state-of-the-art application programs allow user pro- gramming through either macros or a built-in control language such as Basic. • Apple has privately indicated that a scripting language might eventually become part of the Macintosh operating system. • HyperCard and SuperCard give Mac users a taste of programming but lead them on. To accomplish meaningful work, HyperTalk and SuperTalk scripts usually have to resort to external commands (XCMDs) written in a traditional programming language such as C or Pascal. • The way programs are built dictates the way programs are used. A user famil- iar with Mac programming restrictions won't be surprised by the behavior of a Mac application. • Corporate Mac use inevitably leads to custom applications and the need to tie the Macintosh into existing database and entry applications running on other machines. But more to the point, we've seen over time that virtually all Mac users eventually express an interest in learning how the Mac does what it does. Apple's documenta- tion doesn't always explain underlying concepts, and the curious Mac user conse- quently senses a wall between him or her and the inner confines of the Toolbox built into the Mac ROM. We've written this book to help you break through that wall. I

Reading notes: Heading spacing in 'WITHTHINKC' may be a typographic variant

Printed product names
HyperCard · SuperCard · HyperTalk · SuperTalk · Basic · Pascal · C · Macintosh
Languages
English
PDF page 9

MACINTOSH C PROGRAMMING BY EXAMPLE

MACINTOSH C PROGRAMMING BY EXAMPLE No Degree Necessary You won't need a degree in computer science to become a Mac programmer, but you will need some preparation. We know that the main hurdle in writing a first Mac ap- plication is the volume of code it takes to put up a single window. That's where we can help. Our charter for this book is to help the casual programmer explore the wealth of system software that comes with the Mac. How This Book Will Proceed Before we get down to writing applications, we'll take care of a few important preliminaries. We'll introduce you to the THINK C compiler in Chapter 2, and in Chapter 3, we'll take a look at C language fundamentals. Then in Chapter 4, we'll develop a simple application. We'll step back in Chapter 5 to see how the Mac manages memory, and then in Chapters 6 through 8 we'll develop a generic application that will form the basis of all your future programming projects. In later chapters, we'll use this generic application to create applications that explore the file system, that demonstrate the graphics capabilities of the Mac, and that show how the Mac handles text. We'll look at a host of other examples of using the Mac's Toolbox. Every project will be a complete working application. If You're New to C For the projects in this book we'll use the C programming language. If you're new to C, look at the short C primer in Chapter 3. It's only a primer. It is not an introduction to programming. We assume that you already know about fundamental program- ming concepts such as variables, subroutines, and assignments. We don't expect you to know C itself. Know Thy Mac Interface You do need to be familiar with the conventions of the Mac interface. We'll assume that you know what a menu, a dialog box, a button, and a window are, and we'll focus on how to put them together in an application program. If you study a Macintosh application carefully, you'll notice how the menus lead to dialog boxes, how button and command names refer to actions that the user takes, and how well-organized dialog boxes lead the user from the most significant choices to the less important ones. Compare two similar applications-two word processors, say-and notice which elements of the user interface are the same in both programs and which differ. Which interface do you like better? Why? If you can answer these questions, you're well on your way to understanding how to design a user interface for your programs. One advantage of programming the Mac over programming other machines is that much of the Mac's user interface is well defined and directly supported by the 2

Reading notes: Page number '2' and running header positions are uncertain in the extracted order

Printed product names
THINK C
Languages
English
PDF page 10

1: PROGRAMMING THE MACINTOSH WITH THINK C operating system. You don't have to invent a window, a menu, a scroll bar, or any of the other features of the Macintosh interface. The small price you pay for having a standard interface is conformity. All Mac appli- cations have an Apple, a File, and an Edit menu, and these menus have standard sets of basic commands and standard command-key equivalents for their menu com- mands. That's because Mac programmers have agreed to cooperate with Apple and make their applications conform to the Macintosh user interface guidelines. This set of "suggestions" about what a Macintosh application should look like helps users know where things are when they first sit down to use a program. Your application designs should follow the guidelines-unless, of course, you have discovered a much better way of doing things; that's progress, after all. If your modi- fications to the standard interface don't work, if they don't feel right, your users will let you know about it. One of the first applications for the Macintosh was a document outliner very Mac-like in all respects but one: The way the user scrolled text differed from Apple's specification. Users complained directly to the company, in letters to the editor in Macintosh magazines, in online comments on services like Compu- Serve, and in many other ways. The program was redesigned, and later versions worked according to the guidelines. The sample applications in this book follow Apple's guidelines, and if you've used your Mac for some time, you probably have a pretty good idea of how a Macintosh application should look and feel. If you need information about a particular interface issue, you'll find the complete user interface guidelines in two books: The Program- mer's Introduction to the Macintosh and The Macintosh Human Interface Guide- lines, both published by Addison-Wesley. As a casual programmer, you're not likely to need these books, but they do deserve a browse. A professional programmer should have both. Know Thy Toolbox You also need to know something about how Apple divided the Macintosh's resident software into Toolboxes and Managers-roughly, Toolbox routines for the interface goodies and Manager routines for operating system chores like file handling. The six-volume Inside Macintosh, also from Addison-Wesley, is the definitive source for Macintosh programming information. Known tersely as IM by those of us who fre- quent the Apple Macintosh Developer Technical Support electronic mail facility, In- side Macintosh is the basic reference for system routine call syntax and for data structures. You don't need to rush out and buy all six volumes-the whole set repre- sents a sizable investment. Start by buying Volume I. As you progress, you'll know when it's time to get the other books. Volumes I and II contain the original Macintosh programming information. Volume I focuses on the user interface, and Volume II on the behind-the-scenes operating sys- tem activity. You'll use Volume I a lot and Volume II hardly at all. Volume III describes the Macintosh hardware and fills the reader in on changes to the Toolbox and Manager routines between the initial Mac (128K) and the Lisa XL. Volume IV 3

Reading notes: Running header 'PROGRAMMING THE MACINTOSH WITH THINK C' and page number '3' treated as page furniture, not body text. Title of 'Compu-Serve' is hyphenated across a line break and normalized here as CompuServe; original spelling may differ.

Printed product names
Macintosh · Think C · Inside Macintosh · Lisa XL · Mac · Apple
Languages
English
PDF page 11

MACINTOSH C PROGRAMMING BY EXAMPLE

MACINTOSH C PROGRAMMING BY EXAMPLE covers changes that resulted from the introduction of the Plus and 512E models. Vol- ume V describes changes that resulted from the addition of color and other hardware innovations with the Mac II. Volume VI describes changes that accompanied the ad- ditional Mac models introduced in late 1990 and System 7.0, the new Mac operating system introduced in 1991. You might think of the volumes after I and II as "delta documents," documents that describe only changes or additions but that do not recap the original information. This makes IM somewhat hard to use. For casual programming, we recommend that you try a third party reference. A professional programmer will need all the IM vol- umes at some point in his or her career because their detailed information will even- tually come in handy for troubleshooting. A good third party resource is Encyclopedia Mac ROM, by Mathews and Friedland, from Brady Books. A software utility Kurt finds useful is the Inside Mac Desk Ac- cessory, a shareware utility written by Bernard Gallet. This DA, available directly from its author, is first-rate-better than anything available from commercial pub- lishers. It contains a database of the Toolbox calls and the data structures found in IM It really helps to have the information online and quickly accessible. A commer- cial product of this type is available from Addison-Wesley, but we find the shareware utility much more useful. Know Thy Programming Language Picking a programming language is an emotionally charged decision. Everyone has his or her own ideas about which one is best. For the Mac, five general choices were available to us: HyperTalk, Basic, Pascal, C, and assembly language. We ruled out HyperTalk for many reasons. Although it comes free with every Mac, it simply doesn't have the power to control all aspects of the Macintosh by itself. XCMDs are available, but they must be programmed in one of the other languages. Moreover, the disjointed nature of HyperTalk scripts, scattered as they are within HyperCard stacks, makes it difficult to present finished solutions. HyperTalk also brings along a large overhead because it is interpreted by HyperCard, which eats up a lion's share of the available memory in a 1-megabyte Mac. Finally, HyperCard is mostly object oriented, which we feel makes it less suited for procedural operations such as scientific calculations or for carefully controlled sequences of events. Basic has many appealing features. You can run it interactively, so you don't have to wait until compile time to see whether a statement is going to execute the way you think it will. Basic is easy to understand. It's inexpensive and readily available. Un- fortunately, Basic belongs to the old sequential world of computing, in which instruc- tions are always executed in a particular order. (In the original Basic, that order was dictated by line numbers.) The event-driven Mac interface isn't well suited to this sequential control. Moreover, the Macintosh ROM expects to deal with special groupings of data called data structures, and Basic has no way of dealing with data structures directly, which means that Mac programs would have to be more compli- cated than they might otherwise have to be. 4

Reading notes: Possible OCR spacing artifacts in running text (e.g., 'IM' references and hyphenated line breaks).

Printed product names
Plus · 512E · Mac II · System 7.0 · Encyclopedia Mac ROM · HyperTalk · HyperCard
Printed dates
late 1990 · 1991
Languages
English