♦ sun* Xr microsystems NeWS™ 2.0 Programmer’s Guide TM TM TO TO ™ News , Xll/News , SunView , XView , and OpenWindows are trademarks of Sun Microsystems, Inc. Sun Workstation®, Sun Microsystems®, and the Sun logo $-® are registered trademarks of Sun Microsystems Inc. PostScript® is a registered trademark of Adobe Systems Inc. Adobe owns copyrights related to the PostScript language and the PostScript interpreter. The trademark PostScript is used herein to refer to the material supplied by Adobe or to programs written in the PostScript language as defined by Adobe. The X Window System is a trademark of Massachusetts Institute of Technology. UNIX® is a registered trademark of AT&T. All other products or services mentioned in this document are identified by the trademarks or service marks of their respective companies or organizations. Copyright © 1989 Sun Microsystems, Inc. - Printed in U.S.A. All rights reserved. No part of this work covered by copyright hereon may be reproduced in any form or by any means - graphic, electronic, or mechanical - including photocopying, recording, taping, or storage in an information retrieval system, without the prior written permission of the copyright owner. Restricted rights legend: use, duplication, or disclosure by the U.S. government is subject to restrictions set forth in subparagraph (c)(l)(ii) of the Rights in Technical Data and Computer Software clause at DFARS 52.227-7013 and in similar clauses in the FAR and NASA FAR Supplement. The Sun Graphical User Interface was developed by Sun Microsystems, Inc. for its users and licensees. Sun ack- nowledges the pioneering efforts of Xerox in researching and developing the concept of visual or graphical user inter- faces for the computer industry. Sun holds a non-exclusive license from Xerox to the Xerox Graphical User Interface, which license also covers Sun’s licensees. Contents Chapter 1 Introduction 3 1.1. News Programming: An Overview 3 The PostScript Language 3 NeWS Types 3 News Operators 4 The XI 1 /News Server 4 Client-Server Communication 4 C Client Interface 5 Canvases 5 The News Imaging Model 6 Events 6 Memory Management 7 Color Support 7 Font Support 7 1.2. POSTSCRIPT Language Files Used with the Server 8 The Window Manager 8 Classes 8 Debugging 8 Chapter 2 Canvases 11 The canvastype Extension 12 Canvas Operators 12 2.1. Coordinate System Overview 13 2.2. Creating and Displaying Canvases 13 Contents — Continued Creating Canvases 13 Shaping Canvases 14 Setting the Current Canvas 15 Mapping Canvases to the Screen 15 2.3. Manipulating Canvases 16 Moving Canvases 16 Transparent and Opaque Canvases 17 Retained and Non-Retained Canvases 19 Further Information on Damage 21 The SaveBehind Key 21 2.4. Parent, Child, and Sibling Canvases 22 The Sibling List 22 Establishing a New Parent 24 2.5. Overlay Canvases 25 Drawing on Overlays 25 2.6. Canvas Clipping Operators 26 2.7. Cursors 26 2.8. Canvases, Files, and Imaging Procedures 27 Writing Canvases to Files 27 Reading Canvases from Files 27 Other File-Related Operators 28 Imaging 28 2.9. Other Dictionary Keys 29 Events 29 Color 29 X-Specific Features 29 Grab State 29 File Sharing 30 Chapter 3 Events 33 The eventtype extension 33 Event Operators 34 3.1. Overview of Event Distribution 34 - iv - 1 Contents — Continued 3.2. Creating an Event 35 3.3. Expressing Interests 36 Copying an Event Before Expressing Interest 36 Changing and Reusing Interests 36 3.4. Rules for Matching Events to Interests 37 Rules for Name And Action Key Matching 37 Rules for Process Key Matching 37 Rules for Serial Key Matching 37 3.5. Sending an Event into Distribution 38 3.6. Awaiting Events 38 3.7. Specifying the Name, Action, and Canvas Keys as Dictionaries 39 Non-Executable Dictionary Values 39 Executable Dictionary Values 41 3.8. Using the Canvas Key: Matching Multiple Interests 43 Pre-Child and Post-Child Interest Lists 43 Order of Interest Matching 44 Specifying a Single Canvas 44 Specifying an Array or Dictionary 44 Specifying null 44 Using the canvastype EventsConsumed Key 45 Multiple Post-Child Interest Matching: An Example 45 Multiple Pre-Child Interest Matching: An Example 49 3.9. System-Generated Events 49 Mouse Events 50 Enter and Exit Events 52 Name Key Values 52 Action Key Values 54 Using the postcrossings Operator 55 Using the XLocation and YLocation Keys 56 Using the Coordinates Key 57 Focus Events 57 Keyboard Events 58 — V — Contents — Continued The Repeat Key Dictionary 58 Damage Events 59 Obsolescence Events 59 ProcessDied Events 59 3.10. Using the ClientData Key 60 3.11. U sing the Priority Key 60 3.12. Using the Exclusivity Key 62 Using the redistributeevent Operator 63 3.13. Using the TimeStamp Key 64 Using the recallevent Operator 65 3.14. Using the Process Key 66 3.15. Input Synchronization with Multiple Processes 67 Using blockinputqueue 67 3.16. Event Logging 68 Chapter 4 Classes 71 4.1. Basic Terms and Concepts 71 Classes and Instances 71 Inheritance and the Class Tree 73 Superclasses and Subclasses 73 The Immediate Superclass 73 Inheritance 73 Single Inheritance and Multiple Inheritance 74 The Inheritance Array 74 A Single Inheritance Example 75 Summary of Terms 77 4.2. Creating a New Class 79 The Class Definition 79 classbegin 79 classend 79 redef 79 Initializing a New Class 80 4.3. Sending Messages With the send Operator 80 - vi- Contents — Continued The Usual Form of send 80 The Steps Involved in a send 80 Using send to Invoke a Method 81 A Nested send 82 Using send to Create a New Instance 83 Another Form of send 84 Using send to Change the Value of an Instance Variable 84 Using send to Change the Value of a Class Variable 84 4.4. The Psuedo- Variables self and super 85 The self Psuedo-Variable 87 The super Psuedo-Variable 88 Using super to Send a Message Up the Superclass Chain 90 Restrictions on the Use of self and super 90 4.5. Method Compilation 90 Compiling self send 91 Compiling super send 91 Local Dictionaries 91 Controlling Method Compilation 92 /methodcompile 92 /installmethod 93 /doit 93 SetLocalDicts 94 4.6. Creating a New Instance 96 /new 97 /newobject 97 /newinit 98 /newmagic 99 4.7. Intrinsic Classes 100 /newdefault 101 /defaultclass 102 /SubClassResponsibility 102 4.8. Overriding Class Variables With UserProfile 102 Overriding DefaultClass 103 - vii - Contents — Continued 4.9. Promoting Class Variables to Instance Variables 103 promote 103 unpromote 104 promoted? 104 Avoiding an Accidental Promotion 104 4.10. Destroying Classes and Instances 104 /destroy 104 classdestroy 105 /cleanoutclass 105 4.11. Obsolete Objects in the Class System 105 /obsolete 105 4.12. Multiple Inheritance 106 A Simple Multiple Inheritance Example: a Utility Class 106 A More Complex Multiple Inheritance Example 109 Rules for Valid Inheritance Array Orders 109 Possible Inheritance Arrays for this Example 1 10 Which Order Do You Choose? Ill Constraining the Order of the Inheritance Array 112 super and Multiple Inheritance 1 12 4.13. Utilities for Setting and Retrieving an Object’s Name and Classname 113 /name 113 /setname 113 /classname 1 14 4.14. Utilities for Inquiring About an Object’s Status 1 14 isobject? 1 14 isclass? 1 14 isinstance? 1 14 4.15. Utilities for Inquiring About an Object’s Heritage 1 14 /superclasses 1 14 /subclasses 1 14 /instanceof? 1 14 /descendantof? 114 - viii - Contents — Continued /understands? 1 15 /class 115 4.16. Utilities for Finding Objects on the send Stack 115 /topmostinstance 115 /topmostdescendant 115 /sendtopmost 115 4.17. Class Operators 116 4.18. Class Methods 116 Chapter 5 Client-Server Interface 119 5.1. The CPS Facility 119 5.2. Creating the . cps File 120 Argument Types 122 Sending PostScript Language Code without Returning Values 123 Receiving Synchronous Replies 124 Receiving Asynchronous Replies 125 5.3. Creating the .hFile 126 CPS Utilities 127 5.4. Creating the . c File 127 PostScript Language Communication Files 128 Reading the Client’s Input Queue 128 5.5. Tokens and Tokenization 129 Compiling the . c File 130 Comments 130 5.6. Debugging CPS 131 5.7. Supporting NeWS From Other Languages 131 Contacting the Server 132 5.8. Byte Stream Fonnat 132 Encoding For Compressed Tokens 132 enc_int 132 enc_short_string 133 enc_string 133 -ix- Contents — Continued enc_syscommon 133 enc_syscommon2 133 enc_usercommon 133 encjusercommon 133 enc_eusercommon 133 enc_IEEEfloat 133 enc_IEEEdouble 134 Object Tables 134 Magic Numbers 134 Examples 134 Chapter 6 Debugging 139 6.1. Loading the Debugger 139 6.2. Starting the Debugger 139 6.3. Using the Debugger 139 Multi-Process Debugging 140 6.4. Client Commands 140 6.5. User Commands 141 6.6. Miscellaneous Hints 145 Aliases 145 Use Multiple Debugging Connections 146 Chapter 7 Memory Management 149 7.1. Reference Counting 149 Objects 149 References to Counted Objects 150 Counted References 150 Uncounted References 150 Soft References 150 Obsolescence Events 151 Reference T allies 1 5 1 Object Types 151 7.2. Memory Management Operators 152 Contents — Continued 7.3. Memory Management Debugging Operators 153 Using the debugdict 153 Debugging Operators 153 7.4. The Unused Font Cache 156 Specifying the Size of the Cache 156 Flushing the cache 157 Applications 157 Chapter 8 NeWS Type Extensions 161 8.1. News Objects as Dictionaries 161 8.2. List of NeWS Types 162 PostScript Language Types 162 NeWS Type Extensions 162 8.3. colortype 163 8.4. graphicsstatetype 163 8.5. monitortype 163 8.6. packedarraytype 163 8.7. pathtype 164 8.8. canvastype 164 8.9. colormaptype 170 8.10. color mapentry type 170 8.11. cursortype 171 8.12. environmenttype 172 8.13. eventtype 174 8.14. fonttype 177 8.15. processtype 177 8.16. visualtype 182 Chapter 9 NeWS Operator Extensions 187 Chapter 10 Extensibility through POSTSCRIPT Language Files 221 10.1. Initialization Files 221 init . 221 - xi- Contents — Continued redbook.ps 221 basics.ps 221 colors.ps 221 cursor . ps 221 statdict.ps 221 compat.ps 222 util.ps 222 class .ps 222 rootmenu.ps 222 10.2. User-Created Extension Files 222 . user .ps 222 . startup . ps 222 Other Extension Files 222 debug.ps 222 eventlog.ps 222 journal.ps 222 repeat.ps 222 Extension File Contents 223 10.3. Miscellaneous 223 10.4. Array Operations 226 10.5. Conditional Operators 228 10.6. Input Operators 228 10.7. Rectangle Utilities 231 10.8. Class Operators 231 10.9. Graphics Utilities 231 10.10. File Access Utilities 233 10.11. CID Utilities 234 10.12. Journalling Utilities 235 Journalling Internal Variables 236 10.13. Constants 236 10.14. Key Mapping Utilities 237 10.15. Repeating Keys 238 10.16. Standard Colors 238 - xii - Contents — Continued 10.17. Logging Events 238 UnloggedEvents 239 Appendix A NeWS Operators 243 A.l. News Operators, Alphabetically 243 A. 2. NeWS Operators, by Functionality 246 Canvas Operators 246 Event Operators 247 Mathematical Operators 248 Process Operators 248 Path Operators 248 File Operators 249 Color Operators 249 Keyboard and Mouse Operators 249 Cursor Operators 250 Font Operators 250 Miscellaneous Operators 250 Appendix B The Extended Input System 255 B. l. Building on News Input Facilities 255 B.2. The LiteUI Interface 256 B.3. Keyboard Input 257 Keyboard Input: Simple ASCII Characters 257 Revoking Interest in Keyboard Events 257 Keyboard Input: Function Keys 257 Assigning Function Keys 258 Keyboard Input: Editing and Cursor Control 258 B.4. Selections 259 Selection Data Structures 259 Selection Procedures 261 Selection Events 262 /SetSelectionAt 263 /ExtendSelectionTo 264 - xiii - Contents — Continued /DeSelect 265 /ShelveSelection 265 /SelectionRequest 266 B. 5. Input Focus 266 Appendix C Omissions and Implementation Limits 273 C. 1 . Operator Omissions and Limitations 273 C.2. Imaging Omissions 274 C.3. The statusdict Dictionary 274 C.4. Implementation Limits 275 C.5. Other Differences with the PostScript Language 276 Index 275 - xiv - Tables Table 3-1 Boundary Crossing Events 54 Table 3-2 Input Focus 57 Table 4-1 Summary of Terms 78 Table 5-1 CPS Argument Types 122 Table 5-2 C Utility Routines Provided by CPS 127 Table 5-3 Token Values 134 Table 5-4 Meaning of Bytes in Encoding Example 135 Table 7-1 Uncounted Object Types 152 Table 7-2 Counted Object Types 152 Table 8-1 Standard Object Types in the PostScript Language 162 Table 8-2 Additional News Object Types 163 Table 9-1 Events sent to incanvas and its parents 206 Table 9-2 Events sent to outcanvas and its parents 207 Table 9-3 Rasterop Code Values . 214 Table 10-1 Standard News Cursors .. 231 Table B-l Selection-Diet Keys 260 Table B-2 System -defined Selection Attributes 260 Table B-3 Request-diet Entries 261 - XV - Tables — Continued Table B -4 High-Level Selection-Related Events 261 Table B -5 Input Focus 267 Table C-l Implementation Limits 275 Table C-2 News Versions of Various POSTSCRIPT Language Operators 276 - xvi - Figures Figure 2-1 Canvas Mapped at 0,0 16 Figure 2-2 Canvas Mapped at 25,25 17 Figure 2-3 A Mapped Child Canvas 18 Figure 2-4 Parent Canvas Made Transparent 19 Figure 2-5 Parent Canvas Made Opaque and Repainted 19 Figure 2-6 Damage on Unretained Canvas 20 Figure 2-7 Younger Sibling Obscuring Elder 23 Figure 2-8 Elder Sibling Made to Obscure Younger 24 Figure 2-9 Modified Parenthood Between Canvases 24 Figure 2-10 Results of Canvas Clipping Operation 26 Figure 2-11 Imaged Canvas 28 Figure 2-12 Canvas Imaged with buildimage Operator 29 Figure 3-1 Initial Appearance of Canvases 48 Figure 3-2 Result of Pre-Child Interest Matching 48 Figure 3-3 Result of Multiple Pre-Child Interest Matching 49 Figure 3-4 Initial Appearance of FirstCanvas and SecondCanvas 53 Figure 3-5 First Entry Event, Matched by FirstCanvas 53 Figure 3-6 Second Entry Event, Matched by SecondCanvas 54 Figure 3-7 Third Entry Event, Matched by FirstCanvas 54 Figure 3-8 Result of Mouse-Generated Event 57 Figure 4-1 A simple class tree 73 Figure 4-2 A class tree with multiple inheritance 74 - XVII - Figures — Continued Figure 4-3 A single inheritance example 76 Figure 4-4 Dictionary stack before and during a send to MyScrollBar 82 Figure 4-5 Dictionary stack before and during a nested send 83 Figure 4-6 Class tree for self and super example 86 Figure 4-7 Basic class hierarchy for the multiple inheritance examples 106 Figure 4-8 Class hierarchy with a utility class 107 Figure 4-9 Class tree for LabeledDial example 109 Figure 4-10 A breadth-first order for LabeledDial ’s inheritance array 1 10 Figure 4-1 1 A depth-first order for LabeledDial’s inheritance array Ill - xviii - Preface ™ This manual provides a guide to programmin^in the News language. This language is supported as part of the Xii/News server, which itself forms a part of the OpenWindows distributed window system. The News interpreted programming language is based on the PostScript® language . 1 Developed at Adobe Systems, the PostScript language is a general programming language used primarily for specifying the visual appearance of printed documents. The News language uses PostScript language operators to display text and images on a graphics console. Importantly, the News language also provides operators and types that are extensions to the PostScript language; many of these extensions handle the interactive aspects of window management that the PostScript language does not consider. This manual, which assumes the reader’s familiarity with the PostScript language, describes all the basic concepts of News programming. It also provides a syntactic analysis for each News operator and includes code examples that demonstrate the use of News operator and type extensions. For information about using the Xii/News server, see: □ XlliN&VS Server Guide □ X1UN&VS Release Notes For information about OpenWindows, see: □ OpenWindows User’s Guide □ OpenWindows Installation and Startup Guide For information about the PostScript language, see: □ POSTSCRIPT Language Tutorial and Cookbook 2 □ POSTSCRIPT Language Reference Manual 3 1 PostScript is a registered trademark of Adobe Systems Inc. 2 Adobe Systems, PostScript Language Tutorial and Cookbook, Addison-Wesley, July, 1985. 3 Adobe Systems, PostScript Language Reference Manual, Addison-Wesley, July, 1985. Preface — Continued Notational Conventions This manual uses the following notational conventions: □ bold listing font This font indicates text or code typed at the keyboard. □ listing font This font indicates information displayed by the computer. It it also used in code examples and textual passages to indicate use of the C programming language. □ sans serif font This font is used in code examples to indicate use of the PostScript language or NeWS extensions. □ bold font This font is used in textual passages to indicate names of News operators, NeWS types, and system-defined dictionaries. □ italic font This font is used in code examples and textual passages to indicate user- specified parameters for insertion into programs or command lines. It is also used to indicate special terms or phrases the first time they are used in the text. - XX - Introduction Introduction 3 1.1. News Programming: An Overview 3 The PostScript Language 3 News Types 3 News Operators 4 The XI l/NeWS Server 4 Client-Server Communication 4 C Client Interface 5 Canvases 5 The NeWS Imaging Model 6 Events 6 Memory Management 7 Color Support 7 Font Support 7 1.2. PostScript Language Files Used with the Server 8 The Window Manager 8 Classes 8 Debugging 8 J ! 1.1. NeWS Programming: An Overview The PostScript Language NeWS Types Introduction The Xii/News server can be used either by a single computer or by multiple computers linked across a communication network; thus, it is a distributed win- dow system. When the Xii/News server is used with multiple computers, an application run by one machine can use the windows displayed by another. The News interpreted programming language is based on the PostScript language. Developed at Adobe Systems, the PostScript language is used pri- marily for specifying the visual appearance of printed documents. A PostScript program consists of operations that are sent to a PostScript language interpreter residing within a printer, when interpreted, the operations define text, graphics, and page coordinates. The News language uses POSTSCRIPT language operators to display text and images on a graphics console. Programs are interpreted and executed by the Xll/NeWS server, which is resident on the machine to which the graphics console is attached. Importantly, the News language also provides operators and types that are extensions to the POSTSCRIPT language; many of these extensions deal with the interactive and multi-tasking aspects of a window system, which are not handled by the PostScript language. This section provides an overview of NeWS programming. Detailed information is provided in later chapters. The PostScript language is a high level language designed to describe page appearance to a printer. It possesses a wide range of graphics operators. Nevertheless, only about a third of the language is devoted to graphics; the remainder provides a general purpose programming capability. The PostScript language is extensible and thus allows programmers to use the supplied operators to define their own procedures. This extensibility facilitates the creation of modular code, encourages the design of well-structured and comprehensible programs, and helps keep programs small. The News language implements all the standard types provided by the PostScript language. In addition, the News language provides special types as extensions to the PostScript language. Some of the News type extensions can be accessed as if they were POSTSCRIPT language dictionaries. These objects are known as magic dictionary objects. ©sun 'sr microsystems 3 Revision A, of 25 August 1989 4 News Programmer’s Guide Magic dictionaries have keys with predefined names. The programmer can change the value associated with many of the keys; other keys are read-only. The programmer can add new keys to magic dictionaries. Other NeWS type extensions are opaque and cannot be accessed as dictionaries. A full description of all NeWS type extensions is provided in Chapter 8, N