ebook img

X Window Programming From Scratch (Jesse Liberty's from Scratch Programming Series) PDF

798 Pages·2000·2.64 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview X Window Programming From Scratch (Jesse Liberty's from Scratch Programming Series)

P R O G R A M M I N G S E R I E S scratch from J E S S E L I B E R T Y ’ S from X Window Programming scratch A Division of Macmillan USA 201 West 103rd Street, Indianapolis, Indiana 46290 J. Robert Brown X Window Programming from Scratch Copyright © 2000 by Que Corporation All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photo- copying, recording, or otherwise, without written permission from the pub- lisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. International Standard Book Number: 0-7897-2372-7 Library of Congress Catalog Card Number: 00-100691 Printed in the United States of America First Printing: August 2000 02 01 00 4 3 2 1 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Que Corporation cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The authors and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages aris- ing from the information contained in this book or from the use of the CD or programs accompanying it. Associate Publisher Tracy Dunkelberger Acquisitions Editor Katie Purdum Development Editor Hugh Vandivier Managing Editor Thomas F. Hayes Senior Editor Susan Ross Moore Copy Editor Cynthia Fields Proofreaders Harvey Stanbrough Megan Wade Technical Editor Ed Petron Team Coordinator Vicki Harding Media Developer Michael Hunter Interior Design Sandra Schroeder Cover Designers Maureen McCarty Anne Jones Copy Writer Eric Borgert Production Darin Crone Steve Geiselman Contents at a Glance Introduction xvi Section One: Starting Points 1 Part I: Absolute Zero 3 Chapter 1 UNIX for Developers 5 2 Programming Constructs 49 3 A Word on C 83 Part II: The Pieces of X 111 Chapter 4 Windowing Concepts 113 5 Widget Sets 125 6 Components of an X Window Application 143 7 Xlib Graphic Primitives 179 Part III: Back to School 195 Chapter 8 Vector Versus Raster Graphics 197 9 Object Bounds Checking 203 10 Trigonometric and Geometric Functions 211 11 Graphic Transformations 225 12 Coordinate Systems 253 Section Two: Graphics Editor Application 257 Part IV: Laying Out the Parts 259 Chapter 13 Application Structure 261 14 Program Flow 299 15 Common Object Definition 305 16 Object Manipulation 319 17 Utilities and Tools 343 18 File Formats 355 19 Save and Restore 363 Part V: Adding Objects to the Editor 377 Chapter 20 Latex Line Object 379 21 Pencil Line Object 405 22 Object Templates 411 23 Arc Object 421 24 Vector Text Object 437 Part VI: Adding a Print Driver 477 Chapter 25 Introduction to PostScript 479 26 Color Versus Black and White 491 27 Working with XImages and Colormaps 495 Part VII: What’s Next? 507 Chapter 28 Extending the Graphics Editor 509 29 Adding Context-Sensitive Help 513 Part VIII: Appendixes 521 A Command Shells and Scripting 523 B Application Layout Code Listing 539 C Additional Vector Font Sets and vector_chars.h 557 Index 741 Table of Contents Introduction xvi Section One: Starting Points 1 Part I: Absolute Zero 3 Chapter 1 UNIX for Developers 5 The man Command ................................................................................................6 Organization and Navigation ................................................................................8 Directories ......................................................................................................10 Permissions ......................................................................................................15 chmod................................................................................................................17 The cd Command ..........................................................................................18 The C Compiler ..................................................................................................18 Object Files ..........................................................................................................19 Source Files ..........................................................................................................21 The vi Editor ......................................................................................................22 The make Utility ..................................................................................................24 The cc Command ..........................................................................................25 Makefile ..........................................................................................................31 System Tools and Useful Commands ..................................................................44 grep, Pipes, Redirection, and more ................................................................44 The find Command ......................................................................................46 Next Steps ............................................................................................................47 Chapter 2 Programming Constructs 49 Decisions ..............................................................................................................50 The if Statement............................................................................................51 The else Statement ........................................................................................52 Types of Conditions ........................................................................................52 The if else Statement ..................................................................................57 The case Statement ........................................................................................58 Loops ....................................................................................................................60 The for Loop..................................................................................................60 The while Loop..............................................................................................62 The do while Loop........................................................................................63 Functions ..............................................................................................................64 Declarations ....................................................................................................64 Return Type ....................................................................................................66 Function Name................................................................................................66 Parameters ......................................................................................................67 Definition ........................................................................................................68 The return Statement ....................................................................................68 Data ......................................................................................................................69 Data Types ......................................................................................................70 Next Steps ............................................................................................................81 Chapter 3 A Word on C 83 Hello World..........................................................................................................84 Comment Tokens ............................................................................................84 The Function main..........................................................................................85 Code Bodies ....................................................................................................86 Variable Scope ................................................................................................88 Built-In Functions ..........................................................................................90 Memory Management ..................................................................................100 Dynamic Memory Allocation........................................................................105 Memory Leaks ..............................................................................................107 Definitions and Macros ................................................................................107 Conclusion ..........................................................................................................109 Next Steps ..........................................................................................................109 Part II: The Pieces of X 111 Chapter 4 Windowing Concepts 113 Origins of the X Window System......................................................................113 The Pieces of X ..................................................................................................114 Client/Server Model......................................................................................114 Window Hierarchy........................................................................................121 Next Steps ..........................................................................................................124 Chapter 5 Widget Sets 125 The Power and Convenience of Using Widget Sets ........................................125 The Athena Widget Set ....................................................................................129 The Core Widget ..........................................................................................131 Widgets That Manage Other Widgets ........................................................135 The Motif Widgets ......................................................................................140 Next Steps ..........................................................................................................142 Chapter 6 Components of an X Window Application 143 Connecting to the X Server ..............................................................................144 Employing Widget Resources Using Variable Argument Lists ..................144 Creating the Application Interface ....................................................................151 Creating Buttons ..........................................................................................159 Creating Pixmap Icons ..................................................................................162 Assigning Actions ..........................................................................................169 vi X Window Programming from Scratch Managing Windows............................................................................................175 Processing Events ..............................................................................................176 Summary ............................................................................................................176 Next Steps ..........................................................................................................177 Chapter 7 Xlib Graphic Primitives 179 The Graphics Context........................................................................................179 The GC Function............................................................................................182 GCForeground and GCBackground ................................................................184 GCLineWidth ..................................................................................................187 GCTile ............................................................................................................188 Graphic Primitive Functions..............................................................................189 XDrawPoint ....................................................................................................190 XDrawLine ......................................................................................................191 XDrawRectangle ............................................................................................191 XDrawArc ........................................................................................................192 Filled Graphics ..............................................................................................193 Next Steps ..........................................................................................................194 Part III: Back To School 195 Chapter 8 Vector Versus Raster Graphics 197 Vector Graphics..................................................................................................199 Raster Graphics ..................................................................................................199 Next Steps ..........................................................................................................201 Chapter 9 Object Bounds Checking 203 Point-Array–Based Object Bounds ....................................................................204 Arc Object Bounds ............................................................................................206 Employing Object Bounds ................................................................................207 Next Steps ..........................................................................................................209 Chapter 10 Trigonometric and Geometric Functions 211 Calculating Point and Line Intersections..........................................................211 Calculating Slope................................................................................................216 Calculating Point and Arc Intersections............................................................218 Next Steps ..........................................................................................................224 Chapter 11 Graphic Transformations 225 Moving................................................................................................................225 Moving a Line ..............................................................................................226 Moving an Arc ..............................................................................................231 Scaling ................................................................................................................232 Scaling a Line ................................................................................................234 Scaling an Arc................................................................................................243 vii Contents Rotating ..............................................................................................................247 Rotating a Line..............................................................................................248 Rotating an Arc..............................................................................................251 Next Steps ..........................................................................................................252 Chapter 12 Coordinate Systems 253 Rectangular Coordinates....................................................................................254 Polar Coordinate System ..................................................................................254 Next Steps ..........................................................................................................256 Section Two: Graphics Editor Application 257 Part IV: Laying Out the Parts 259 Chapter 13 Application Structure 261 Project Structure ................................................................................................262 Laying Out the User Interface......................................................................265 Parsing the Command Line ..............................................................................289 The getopt Function ........................................................................................290 The XtVaGetApplicationResources Function ................................................292 Setting Up a Canvas ..........................................................................................296 Building the Project............................................................................................297 Next Steps ..........................................................................................................298 Chapter 14 Program Flow 299 Processing Events ..............................................................................................300 X Event Hooks ..................................................................................................302 Widget Callbacks ..........................................................................................302 Event Handlers..............................................................................................302 Widget Translation Tables............................................................................303 Next Steps ..........................................................................................................304 Chapter 15 Common Object Definition 305 Line Object Data Structure................................................................................305 Text Object Data Structure................................................................................307 Understanding Vector Fonts ........................................................................309 The GXText Data Structure................................................................................314 Arc Object Data Structure ................................................................................314 Common Object Data Structure........................................................................315 Next Steps ..........................................................................................................318 Chapter 16 Object Manipulation 319 Copying an Object..............................................................................................319 Deleting an Object ............................................................................................321 viii X Window Programming from Scratch Refreshing Objects ............................................................................................325 Parsing for an Object..........................................................................................326 Managing Object Handles ................................................................................327 Managing the Status of an Object ....................................................................331 Processing User Navigation of Objects ............................................................334 Next Steps ..........................................................................................................341 Chapter 17 Utilities and Tools 343 Common Object Creation ................................................................................343 Linked List Management..............................................................................346 Creating a Graphics Context ............................................................................347 Graphics Context Tiling ..............................................................................348 Using the Cursor as State Indicator ..................................................................349 Next Steps ..........................................................................................................354 Chapter 18 File Formats 355 Understanding Files ..........................................................................................357 Binary File Formatting ......................................................................................357 ASCII File Formatting ......................................................................................359 Tagged File Formats......................................................................................359 Position-Specific File Formats......................................................................360 Magic Numbers ..................................................................................................361 Next Steps ..........................................................................................................361 Chapter 19 Save and Restore 363 File Format Strategy ..........................................................................................363 Save and Restore Program Hooks ....................................................................366 Common-Object Save and Restore ..................................................................367 Object-Specific Save and Restore ......................................................................373 Next Steps ..........................................................................................................376 Part V: Adding Objects to the Editor 277 Chapter 20 Latex Line Object 379 Creating a Latex Line Object ..........................................................................380 Drawing and Erasing a Line Object..................................................................388 Finding a Line Object ........................................................................................390 Selecting and Deselecting a Line Object ..........................................................392 Moving a Line Object ........................................................................................395 Scaling a Line Object ........................................................................................398 Copying a Line Object ......................................................................................400 Saving and Restoring a Line Object..................................................................401 Next Steps ..........................................................................................................403 ix Contents Chapter 21 Pencil Line Object 405 Creating a Pencil Object ..................................................................................406 Pencil Object Management ..............................................................................410 Next Steps ..........................................................................................................410 Chapter 22 Object Templates 411 The Box Object ..................................................................................................411 The Arrow Object ..............................................................................................415 Next Steps ..........................................................................................................419 Chapter 23 Arc Object 421 Creating an Arc Object ......................................................................................422 Drawing and Erasing an Arc Object..................................................................427 Finding an Arc Object........................................................................................428 Selecting and Deselecting an Arc Object ..........................................................428 Moving an Arc Object........................................................................................431 Scaling an Arc Object ........................................................................................432 Copying an Arc Object ......................................................................................434 Saving and Restoring an Arc Object..................................................................435 Next Steps ..........................................................................................................436 Chapter 24 Vector Text Object 437 Creating a Text Object ......................................................................................438 Drawing and Erasing a Text Object..................................................................461 Finding a Text Object ........................................................................................463 Selecting and Deselecting a Text Object ..........................................................464 Moving a Text Object ........................................................................................467 Scaling a Text Object ........................................................................................469 Copying a Text Object ......................................................................................472 Saving and Restoring a Text Object..................................................................473 Next Steps ..........................................................................................................475 Part VI: Adding a Print Driver 477 Chapter 25 Introduction to PostScript 479 PostScript............................................................................................................479 Learning PostScript............................................................................................480 Stacks ............................................................................................................481 PostScript Commands........................................................................................482 Comments......................................................................................................482 PostScript Programming....................................................................................482 Viewing PostScript Files ....................................................................................488 Comments Understood by Ghostscript ......................................................489 Next Steps ..........................................................................................................490 x X Window Programming from Scratch Chapter 26 Color Versus Black and White 491 Determining a Printer’s Capability....................................................................491 Defining Color Images for Black and White Printers......................................492 Next Steps ..........................................................................................................494 Chapter 27 Working with XImages and Colormaps 495 Printing the Canvas............................................................................................495 Creating an XImage ..........................................................................................497 Creating a PostScript Prolog ............................................................................499 Parsing the X Colormap ....................................................................................500 Writing the PostScript Page Definition File ....................................................502 Directing the Output to a Printer or File ........................................................504 Next Steps ..........................................................................................................506 Part VII: What’s Next? 507 Chapter 28 Extending the Graphics Editor 509 Attributes ............................................................................................................509 Color..............................................................................................................509 Line Attributes ..............................................................................................510 Arc Angles......................................................................................................511 Rotating Objects ................................................................................................511 Next Steps ..........................................................................................................512 Chapter 29 Adding Context-Sensitive Help 513 Processing Help-Related Events........................................................................514 Widget Paths ......................................................................................................516 Relating Widgets to Text....................................................................................518 Next Steps ..........................................................................................................519 Part VIII: Appendixes 521 Appendix A: Command Shells and Scripting 523 UNIX Command Shells ....................................................................................523 Command Shell Environment......................................................................525 Scripting with the Bourne Shell ........................................................................531 Shell Variables................................................................................................532 Writing a Script with Function Calls ..........................................................536 Debugging Shell Scripts................................................................................538 Appendix B: Application Layout Code Listing 539 make.defines File Contents ........................................................................540 GNUmakefile File Contents ..........................................................................542 gxMain.c File Contents ................................................................................542 xi Contents gxGraphics.c File Contents ........................................................................544 gxGx.c File Contents ....................................................................................547 gxArc.c File Contents ..................................................................................549 gxLine.c File Contents ................................................................................549 gxText.c File Contents ................................................................................550 gxGraphics.h File Contents ........................................................................550 gxIcons.h File Contents ..............................................................................551 gxBitmaps.h File Contents ..........................................................................552 gxProtos.h File Contents ............................................................................555 Appendix C: Additional Vector Font Sets and vector_chars.h 557 Triplex Bold Italic Vector Font Set....................................................................539 The vector_chars.h Header File ....................................................................560 Index 741 xii X Window Programming from Scratch About the Author J. Robert Brown started his path to a career in software development by earning a college scholarship for Performing Arts in his homeland of central Ohio, where he held the misguided belief that he could be a movie star. After years of either sleeping in his car or working three jobs concurrently to fund his way through an Electrical Engineering program, he found himself in Europe in the late 1980s working for the Department of Defense. As a field engineer maintaining the mobile computer systems responsible for collect- ing and processing intelligence data, he realized that the position required too much manual labor. In 1991, he made his way through a Computer Science program at the European Division of the University of Maryland and although he didn’t exactly fin- ish in the top 10% of his class, he believes strongly that he helped those who did to get there. John was invited to join Los Alamos National Laboratory as a Computer Scientist in 1996 where he remained until only recently. He now works for GTE Data Sources near Tampa, Florida. Dedication There are people who exist in the world who, once you’ve encountered them, change you for- ever. Through the strength of their character, depth of their spirit, or simply their presence in the world, they leave a lasting impression. I fear that we have one fewer such individual today due to the loss of Shel Silverstein in May, 1999. I hope for everyone there is someone who touches his or her life as Shel’s works have touched mine. Those without whom my life would mean less and this effort would not have been possible are my dear mother, Cindy Baker; my brother and best friend, Scott Brown; the absolute love of my life, Mikeala Elise; and the person who gave her to me, Kinnina McCray. Acknowledgments X Window Programming from Scratch is the result of efforts by many people. I am filled with awe and gratitude for the level of professionalism and quality the staff at Que publishing brought to this effort: specifically, Hugh “Red” Vandivier, Susan “I need this back by Monday” Moore, Cynthia “Did this change your meaning?” Fields, and Katie “You’d better meet the deadline” Purdum. And although no one likes to be told they have made a mistake, technical editor Ed “Um, you might want to check this” Petron was able to point out oversights in a way that never came close to wounding my ego. With the help of these and many others behind the scenes, this text is much better than I could ever have made it on my own. There are others who contributed indirectly to the project by offering their friend- ship, encouragement, and patience as I tried to keep my head above water: namely, Mike Koscielniak, Cindy Sievers, and Jennifer Brown.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.