← C Programming Techniques for the Macintosh 1989

C Programming Techniques for the Macintosh 1989

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

C Programming Techniques for the Macintosh

HOWARD W. SAMS & COMPANY 22461 C Programming Techniques for the Macintosh® Zigurd R. Mednieks Terry M. Mednieks DialogPtr pos_dial int item got_m Handle GrafPtr Rect b LightspeedC™ Compatible

A dark navy book cover with white and lilac title type, a large stylized lilac letter C enclosing a blue-tinted glowing head sculpture with handwritten-style code overlaid, and a lilac banner at the bottom.

Reading notes: Background code fragments such as 'DialogPtr pos_dial', 'int item', 'Handle', 'Rect b', and 'GrafPtr' are partially obscured and mirrored, so their exact text is uncertain and they were only partially transcribed. The mirrored code text in the lower half of the image is not reliably readable and was omitted. Reading order of the title block versus the author names is inferred from layout.

Printed product names
C Programming Techniques for the Macintosh
Languages
English
PDF page 1

C Programming Techniques for the Macintosh

HOWARD W. SAMS & COMPANY 22461 C Programming Techniques for the Macintosh® Zigurd R. Mednieks Terry M. Mednieks DialogPtr pos_dlel int item got_m Handle ... em Rect b ... GrafPtr Handle ... ... int item ... got_m Dialog ... pos_dlel LightspeedC™ Compatible

A dark navy cover with bold white title text at top, a large lavender letter C graphic framing a blue-tinted glass head sculpture overlaid on handwritten-style code fragments, and a lighter bar at the bottom reading LightspeedC Compatible.

Reading notes: Code fragments overlaid on the artwork are partially obscured by the sculpture and are only partly legible; the middle and lower lines are uncertain and may be inverted or mirrored. The code lines in the image are rendered in a stylized, partly reversed script, so exact characters are uncertain. Title text uses ® symbol after Macintosh, transcribed as printed. Reading order of the artwork text is ambiguous.

Languages
English
PDF page 2

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

Reading notes: No visible text found; the page appears blank white.

PDF page 3

HOWARD W SAMS &.. COMPANY HAYDEN BOOKS Related Titles Macintosh® Revealed Volume One: Unlocking the Toolbox Second ~dition Stephen Chernicoff Volume Two: Programming with the Toolbox Second Edition Stephen Chernicoff Volume Three: Mastering the Toolbox Stephen Chernicoff Volume Four: Pro&p"amming the Macmtosh® D (forthcoming) Stephen Chernicoff and Geri Younggren Bow to Write Macintosh® Software Second Edition Scott Knaster MacAccess: Information in Motion Dean Gengle and Steven Smith MPW and Assembly Language Programming Scott Kronick The Macintosh® Advisor (Updated for Multifinder) Cynthia Harriman and Bencion Calica Object-Oriented Programming for the Macintosh® Kurt J. Schmucker Programming the 68000 Edwin Rosenzweig and Harland Harrison The Waite Group's Tricks of the HyperTalk"' Masters The Waite Group The Waite GrouP-'s HyperTalk '" Bitile The Waite Group IBM® PC and Macintosh® Networkin1: Featuring TOPS and Apple hare"' Steve Michel Macintosh® Hard Disk Management Bencion J. Calica and Charles Rubin Artificial Intelligence Pro&p"amming on the Macmtosh® Dan Shafer Understanding HyperTalk"' Dan Shafer For the retailer nearest you, or to order directly from the publisher, call 800-428-SAMS. In Indiana, Alaska, and Hawaii call 317-298-5699.

Reading notes: Some title text is garbled by OCR (e.g., 'Pro&p"amming', 'Macmtosh®', 'Bow to Write'), so product names may be imperfect

Printed product names
Macintosh® Revealed · Macintosh® Software · MacAccess: Information in Motion · MPW and Assembly Language Programming · The Macintosh® Advisor · Object-Oriented Programming for the Macintosh® · Programming the 68000 · Macintosh® Hard Disk Management
Printed dates
Second Edition
Languages
English
PDF page 4

C Programming Techniques for the Macintosh

C Programming Techniques for the Macintosh® Zigurd R. Mednieks Terry M. Mednieks HOWARD W. SAMS ~COMPANY A Division of Macmlllan, Inc. 4300 West 62nd Street Indianapolis, Indiana 46268 USA

Printed product names
Macintosh
Languages
English
PDF page 5

©1986 by Zigurd Mednieks FIRST EDITION THIRD PRINTING -1989 All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of in- formation contained herein. While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility from er- rors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. International Standard Book Number: 0-672-22461-5 Library of Congress Catalog Card Number: 86-61450 Acquiring Editor: James S. Hill Cover Art: Gregg Butler Composition: MUSRU Corporation Printed in the United States of America Trademark Acknowledgments All terms mentioned in this book that are known to be trademarks or ser- vice marks are listed below. Howard W. Sams & Co. cannot attest to the accuracy of this information. Use of a term in this book should not be re- garded as affecting the validity of any trademark or service mark. Apple][ is a trademark of Apple Computer, Inc. AppleTalk is a trademark of Apple Computer, Inc. DEC is a trademark of Digital Equipment Corporation IBM PC is a trademark of International Business Machines, Inc. Lisa is a trademark of Apple Computer, Inc. MacPaint and MacWrite are trademarks of Claris Corporation PDP-10 and PDP-11 are trademarks of Digital Equipment Corporation Unix is a trademark of AT&T and Bell Laboratories VAX is a trademark of Digital Equipment Corporation

Printed product names
Apple][ · AppleTalk · DEC · IBM PC · Lisa · MacPaint · MacWrite · PDP-10 · PDP-11 · Unix · VAX
Printed dates
1986 · 1989
Languages
English
PDF page 6

CoNrENTS v Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Part 1: Tutorial C~apter 1: Beginnings. . . . . . . . . . . . . . . . . . . . . . . . . 1 Origins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Implementations and implementation languages. . . . . . . . . 2 Capabilities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Wherever C goes, there it is. . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 C and microcomputer software......................... 4 Breaking down barriers................................ 5 Where to go now. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Chapter 2: C and Other Languages. . . . . . . . . . . . . . 9 Where you are going. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Where you are. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 What you will need to get there......................... 9 Familiar tools and new tools. . . . . . . . . . . . . . . . . . . . . . . . . . 10 Abstraction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 The cost of the more-general approach. . . . . . . . . . . . . . . . . 11 Hiding data......................................... 11 Program structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Structuring data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Number crunching................................... 14 Extensibility. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Macros. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Why Macintosh programming really ought to be done in C........................................... 16 Choosing your tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Chapter 3: Knowing C, Thinking C. . . . . . . . . . . . . 21 Ready?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Which C............................................ 22 The preprocessor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Simple preprocessor statements. . . . . . . . . . . . . . . . . . . . . . . 22 Preprocessor statements with parameters. . . . . . . . . . . . . . . 23

Reading notes: Primary heading is not clearly identified; the copy is a table of contents without a distinct headline. OCR-style errors appear in places (e.g., 'CoNrENTS', 'C~apter', 'Wherever C goes, there it is'), which may affect exact spelling.

Languages
English
PDF page 7

VI C PROGRAMMING TECHNIQUES FOR THE MACTNTOSH

VI C PROGRAMMING TECHNIQUES FOR THE MACTNTOSH The opposite of a macro definition. . . . . . . . . . . . . . . . . . . . . 23 Including files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Conditional compilation.............................. 23 Support for program generators....................... 24 Preprocessor example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 The syntax of C- what the C parser sees. . . . . . . . . . . . . . . . 25 Simple variables - storage classes and data types. . . . . . . . 25 An example: simple declarations....................... 27 Data structures and arrays............................ 28 Unions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Defined data types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Enumerated constants................................ 30 Aggregate declarations: an example.................... 30 Pointer declarations.................................. 31 Opera tors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Using unary operators: an example. . . . . . .. . . . . . . . . . . . . . 33 Binary operators..................................... 34 Control flow statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 An example program................................. 39 The meaning and use of style.......................... 41 Chapter 4: QuickDraw and Windows. . . . . . . . . . . 45 The obvious and subtle parts of QuickDraw............. 45 QuickDraw coordinates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 The GrafPort: an environment for drawing. . . . . . . . . . . . . . 48 Bit maps............................................ 49 GrafPort regions: support for windows. . . . . . . . . . . . . . . . . 50 The Window Manager................................ 51 The window record structure.......................... 51 Creating the desktop metaphor........................ 52 Stepping through an update event. . . . . . . . . . . . . . . . . . . . . 53 Drawing from applications............................ 53 The active window................................... 54 What windows are not. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Chapter 5: Revolutionary Software, Classical :rvficrocoinputerfiardware ..................... 57 The Macintosh and the Apple ][. . . . . . . . . . . . . . . . . . . . . . . 58 You can count on Macintosh features ................... 59 The major players on the Macintosh logic board. . . . . . . . . 59 The Motorola M68000. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

Reading notes: Heading spelled 'MACTNTOSH' as printed; OCR-like damage in some characters such as 'C- what', ':rvficrocoinputerfiardware' and 'Opera tors'. Table-of-contents layout with dot leaders; page numbers are not dates and are not extracted.

Languages
English
PDF page 8

CONTEN'I'S vn The Synertek SY6522 Versatile Interface Controller....... 62 The Zilog Z8530 Serial Communications Controller...... 63 The Integrated Woz Machine.......................... 63 The NCR SCSI host adapter........................... 63 How closed is it?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Chapter 6: The Resource Compiler.............. 67 A consistent way of initializing Macintosh data structures. .......................................... 67 Resources and Toolbox managers...................... 68 Your own resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Example: designing a dialog. . . . . . . . . . . . . . . . . . . . . . . . . . 69 Resource compiler syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 The header of a resource compiler input file............. 70 The general format of resource specifications............ 71 Window resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Dialogs and item lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Icon resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Cursors............................................. 74 Patterns............................................ 74 Strings ............................................. 75 Menus............................................. 76 Control resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Finder resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Including resources from other files .................... 79 Defining new kinds of resources....................... 80 Resource editors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Benefits. ................ ·. . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Chapter 7: The Internal Structure of a Macintosh Application.................................. 85 The evolution of an example. . . . . . . . . . . . . . . . . . . . . . . . . . 85 Step 1: Getting started. . .. . . . . . . . . . . . . . . . . . . . . . . . . . .. . 85 C code for the hello world program. . . . . . . . . . . . . . . . . . . . 86 Resources for the hello world program. . . . . . . . . . . . . . . . . 87 The edit/compile/run cycle........................... 88 Initializing Toolbox managers......................... 88 Creating a window.................................. 89 The event loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Step 2: Building up................................... 91 C code for the second version of our hello world program............................................ 91

Reading notes: Table of contents with dotted leaders; page numbers and some headings may be truncated or garbled in extraction

Printed product names
Synertek SY6522 Versatile Interface Controller · Zilog Z8530 Serial Communications Controller · Integrated Woz Machine · NCR SCSI host adapter
Languages
English
PDF page 9

VIII C PROGRAMMING TECHNIQUES FOR THE MAONTOSH

VIII C PROGRAMMING TECHNIQUES FOR THE MAONTOSH Beefing up the event loop............................. 93 It's event driven. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Step 3: Growth and movement. . . . . . . . . . . . . . . . . . . . . . . . 94 Strengthening the framework. . . . . . . . . . . . . . . . . . . . . . . . . 98 Drawing the grow icon............................... 99 Updating and activating.............................. 99 Responding to the mouse. . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Support for activating, updating, growing, and dragging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 What to do about invalid areas. . . . . . . . . . . . . . . . . . . . . . . 101 An invitation to tinker............................... 101 Step 4: Menus and scroll bars......................... 102 Setting up menus and scroll bars...................... 110 Interacting with menus. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Interacting with controls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Controls and activation.............................. 114 Use them! .......................................... 115 Chapter 8: Exploring the Mandelbrot Set. ...... 119 A shift in presentation............................... 120 Stating the problem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Goals and desired results. . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Visual goals: Presentation ............................ 122 Enumerating the parameters. . . . . . . . . . . . . . . . . . . . . . . . . 122 Designing the data structures. . . . . . . . . . . . . . . . . . . . . . . . 123 Solving the problem - computing the results. . . . . . . . . . . 124 Graphic display and graphics update.................. 125 Creating a user interface............................. 128 Designing with dialogs.............................. 128 Source listing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Chapter 9: Extending Our Grasp. . . . . . . . . . . . . . 161 Completing the application, . . . . . . . . . . . . . . . . . . . . . . . . . 161 Choices for filing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Why two file systems?............................... 162 Filing in the example program........................ 162 Choices for cut and paste. . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Choices for printing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Printing in our application........................... 163 The last listing...................................... 163

Reading notes: Heading spelled 'MAONTOSH' in source; likely OCR or typesetting error for 'MACINTOSH' Table of contents layout makes reading order of entries approximate

Languages
English
PDF page 10

CONTENTS IX Chapter 10: Debugging....................... 199 How to prepare for debugging. . . . . . . . . . . . . . . . . . . . . . . 199 Two major Macintosh debuggers. . . . . . . . . . . . . . . . . . . . . 200 Basic concepts in debugging. . . . . . . . . . . . . . . . . . . . . . . . . 201 Deeps_......................................... 202 The most common bugs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Part 2: Reference QuickDraw................................. 209 Event Manager.............................. 245 Window Manager. . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Dialog Manager. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 MellloryManager ........................... 273 Menu Manager.............................. 285 Control Manager............................ 295 TextEdit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 Standard File Package........................ 311 File Manager................................ 315 Font Manager............................... 343 Printing Manager............................ 347 Resource Manager........................... 355 Toolbox Utilities............................. 367 Desk Manager. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 Scrap Manager. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 Segment Loader. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383

Reading notes: Garbled entries 'Deeps_' and 'MellloryManager' appear to be OCR errors Page numbers appear uncertain due to OCR damage in several entries

Languages
English
PDF page 11

Preface Do you need this book? This book is designed to break down barriers for a new breed of Macintosh user: the power user. Many power users reach a point at which it is no longer satisfying to be an expert user. If you are familiar with the Macintosh as a tool, and want to go beyond using the Macintosh to programming the Macintosh, you have reached that critical threshold. You maybe: • A corporate MIS analyst considering using the Macintosh to pro- vide better integration and ease-of-use for your user community. You need to provide your programmers with a detailed study of a complete Macintosh application. • A vertical-market developer. You need to contain development costs. You need to know how to use the Macintosh Toolbox to give your products the same level of sophistication that companies like Lotus and Microsoft can provide in mass-market applications. • A student in a course involving Macintosh programming. You need a practical guide to programming, so that you can devote more at- tention to computer-science issues. • A software developer considering developing versions of your soft- ware for the Macintosh. You have heard that the Macintosh is hard to program. You need to know where the difficulties lie - and where benefits like ease-of-use come from. What this book will tell you If you want to start programming your Macintosh, this book will get you started faster and improve the chances that your first program will work. The Macintosh has earned a reputation as a challenging machine to program. This is due, in part, to the Macintosh Toolbox. While it provides powerful routines for creating user interfaces, it also requires the pro- grammer to be knowledgeable of and sensitive to the way Macintosh pro- grams are written. Where other machines are vehicles for programs of al-

Reading notes: Source text ends mid-sentence at 'Where other machines are vehicles for programs of al-'

Printed product names
Macintosh · Macintosh Toolbox
Languages
English