ebook img

Programming with Motif™ PDF

668 Pages·1992·12.083 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 Programming with Motif™

Programming with Motir Keith D. Gregory Programming with Motif1M With 112 Illustrations Springer-Verlag New York Berlin Heidelberg London Paris Tokyo Hong Kong Barcelona Budapest Keith D. Gregory Wakefield, MA 01880 U.S.A. Pennission to reprint the portions of OSF/ Motif'I'M copyrighted material appearing in this book has been granted by Open Software Foundation, Incorporated. PostScript is a registered trademark of Adobe Systems, Incorporated. UNIX is a registered trademark of UNIX Systems Laboratories, Incorporated. DECstation, Digital, the Digital logo, MicroVAX. and VAX are trademarks of the Digital Equipment Corporation. X Window System is a trademark of the Massachusetts Institute of Technology. Motif, OSF, and OSF/ Motif are registered trademarks of the Open Software Foundation, Incorporated. IBM is a registered trademark and OS/2 is a trademark of International Business Machines Corporation. Hewlett-Packard (HP) is a trademark of the Hewlett-Packard Company. MS-Windows is a registered trademark of Microsoft Corporation. library of Congress Cataloging-in-Publication Data GregoI)', Keith D. Programming with motif / Keith D. Gregory p. cm. Includes bibliographical references and index. ISBN-13:978-0-387-97877-2 e-ISBN-13:978-1-4612-2954-4 DOl: 10.1007/978-1-4612-2954-4 1. X Window System (Computer System) 2. Motif (Computer program) I. Title. QA76.76.W56G84 1992 005.4'3-dc20 92-17576 Printed on acid-free paper. ©1992 Sprtnger-VerlagNewYork, Inc. All rights reseIVed. This work may not be translated or copied in whole or in part without the written permission of the publisher (Springer-Verlag New York, Inc., 175 Fifth Avenue, New York, NY 10010, USA), except for brief excerpts in connection with reviews or scholarly analysis. Use in connection with any form of information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodol ogy now known or hereafter developed is forbidden. The use of general descriptive names, trade names, trademarks, etc., in this publication, even if the former are not especially identified, is not to be taken as a sign that such names, as understood by the Trade Marks and Merchandise Marks Act, may accordingly be used freely by anyone. Production supervised by Ken Dreyhaupt; manufacturing coordinated by Vincent Scelta. Camera-ready copy prepared from the author's Microsoft Word meso 98765432 ISBN -13 :978-0-387-97877-2 For Jennifer Preface About this Book This book is a detailed introduction to programming with the OSF/ MotifI'M graphical user interface. It is an introduction in that it does not require the reader to have experience programming in the X Window environment. It is detailed in that it teaches you how to use the interface components provided by Motif in a complex application. Although it contains a great deal of reference material, it is not meant as an authoritative reference - that is the job of the OSF/Motif Programmer's Reference, which uses over 900 pages in the process. Instead, this book provides its reference material in a practical, "how to" manner and allows the reader to use the Programmer's Reference effectively. The target reader is an experienced C programmer and user of the X Window System under the UNIX operating system. 'the reader should be familiar with the tools provided by UNIX for the compilation and testing of programs; while this book does examine the process by which a Motif program is compiled, it does not explain that process. It also assumes that the reader is familiar with "x" terms such as 'pointer' and 'display'. How this Book Came to Exist As a programmer learning Motif, I found that Motif books assumed that the reader knew the X toolkit, needed to know a bit about the capabilities of Motif, but really wanted to write widgets. Books that covered the X toolkit only quickly went in a direction away from Motif and ended with the reader writing widgets. I was a programmer who had used Xlib for some small projects and wanted to make the leap to Motif; I didn't know what a widget was, and I certainly didn't want to write one. I have written this book for people in a similar situation, which I believe is the one facing most programmers learning Motif. At one time, a programmer would enter the world of X through Xlib or Xt and eventually move on to writing a new widget set. At the present time, however, I believe that most programmers enter the world of X seeking solutions to business problems and find themselves directly at the door of Motif or Open Look. With this in mind, I start the book with the basics of toolkit programming and end before widget implementation. Throughout, I try to present information that vii viii Preface is needed for an actual application, without the details used to get "the last 10%" from the environment. As source material, I use documentation and code from the XII distribution, with Motif documentation published by the Open Software Foundation. In addition, I have received information from the large group of programmers represented by the comp.windows.xand comp.windows.x.motijnewsgroups. Suggested Reading Plan Looking at this book, you may be intimidated by its length - and become more intimidated when I suggest that you start at the front cover and end at the back cover. I say this because Motif is a complex environment, and I have attempted to structure the book such that each chapter builds upon the ones before it. The chapter descriptions below will give you an idea of this structure. If, however, you are one of those people who likes to see something work right away, there is an abbreviated introduction that gives you "Hello World" in one hour or less. This abbreviated process is also described in the chapter listings. Chapters Chapter I describes Motif: what it is, why it exists, and what benefit it provides to users and programmers. If you are unfamiliar with the purpose and organization of X and Motif - from Xlib to toolkits to the Motif library - I recommend reading this chapter. For those on the "quick start" plan, it may be safely skipped. Chapter 2 describes the widget, the abstract graphical object that is the core of Motif programming. This description is from both the abstract rerspective of object-oriented programming and the programming perspective 0 "what does a widget do." Along the way, it presents the key ideas of derivation (the creation of a new type of widget by building on an existing widget) and parentage (the hierarchical organization of widgets in a program). This chapter proviaes the foundation for the rest of the bOOK and should be read by everyone. Chapter 3 describes resources, which are used to customize the appearance and interaction of a widget. It presents a large amount of material, covering various attributes of resources. This material is invaluable and should be read in detail, but is best read in the context of an actual program - which has not yet been presented. For this reason, I recommend reading the introductory sections (through Sample Resource File) at first and then rereading the whole chapter once you've written your first program. Chapter 4 describes the format of a Motif program and provides you with an understanding of what happens when you press the Return key. It too is invaluable and should be read in detail, but this reading may be postponed until after Chapter 5. I do, however, recommend at least skimming this chapter and reading CompUing A Motij Program in detail. Chapter 5 introduces the first widget: XmLabel, which is used to display static text. It also presents the first program -like most books, this is "Hello, World." Preface ix This program completes the "quick start" plan, and I recommend that skimmed chapters should be reread. Chapter 6 describes manager widgets, which are used to position other widgets and thus control the appearance of a program's interface. The four most common Motif managers: XmBulletinBoard, XmRowColumn, XmPanedWindow, and XmFonn are described, with examples showing the uses of each. Chapter 7 describes the standard Motif button widgets: XmPushButton, XmArrowButton, and XmToggleButton. In this chapter, they are presented as user-interface objects; their link to the program is postponed to Chapter 8. Chapter 8 describes the X event mechanism by which programs receive input. It starts at the lowest level, that of actual events, and describes what events are and how the program uses them. After this, it describes the callback mechanism, which is a high-level link between programs and events. Next are actions and translations, a yet higher level of event processing. Chapter 9 describes methods of keyboard input. It starts with a low-level description of keyboard events and then presents the XmText widget, which is Motifs primary means of keyboard input. This chapter contains three progressively complex examples of XmText usage: as an entry field, as a single screen memo pad, and as a replacement for the more utility. Chapter 10 describes scrollbars, a user-interface object used to control the position and scope of a program's displayed data. This chapter starts at a low level, with the XmScrolLBar widget and its interface. Next is a description of XmScrolledWindow, a manager widget that transparently handles the scrolling of its contents. The chapter finishes with a description of the xmScale widget, a related widget that is used to input and display data values. Chapter 11 describes the program as a whole. This description is from two perspectives: the appearance of a program, as defmed by the OSF/ Motif Style Guide, and those widgets that exist to assist the programmer in implementing a program with the desired appearance. This chapter also presents the beginnings of a text editor implemented with Motif widgets. Chapter 12 describes menus, the primary means by which a user controls the operation of a Motif program. It starts with a description of the menu bar and its pull-down menus and adds these items to the text editor. It also presents pop-up and option menus, which are used to provide context-sensitive input. Chapter 13 describes dialogs, which provide the program with auxiliary windows for user interaction. Examples include a custom dialog that implements a search facility for the text editor, as well as the use of Motifs "canned" message dialogs. Chapter 14 describes lists and the way that the user interacts with them. It starts with the basic list, as implemented by XmList, and follows with widgets built around a list: XmSelectionBox, XmFileSelectionBox, and XmCommand. It also presents examples of uses of lists, ranging from an e-mail address-selection dialog to a file-selection module for the text editor. Chapter 15 is an introduction to the use of Xlib - the low-level X drawing functions - with Motif. It describes the general form of an Xlib call and presents the XmDrawingArea widget, which acts as a "canvas" for such calls. The scope of this chapter is necessarily limited - Xlib could easily occupy an entire book by itself. x Preface Chapter 16 presents advanced resource topics. These range from the way that Motif associates resource specifications with a program's invocation name, to the use and creation of functions that convert resource data from one type to another. Along the way, it describes the X approach to command-line program options and a method by which the resource file may be used to change program variables. Chapter 17, the final chapter, describes interclient communication. This topic involves both the X-specific methods of interclient communications, along with the traditional communication methods such as pipes. This chapter is quite long and presents a variety of communications methods - a subject that I feel other books have omitted. The appendices exist for reference only. Appendix A is simply a character chart, showing the ISO Latin 1 character set - an extension of ASCII used by X and many other modern systems. Appendix B presents a summary of widget information - a "mini Programmer's Reference." Appendix C provides a complete reference for X events - seemingly a requirement for X-related books. Appendix D contains a summary of changes in X and Motif, while Appendix E lists other sources of information, from the official OSF/ Motif documentation to various X user groups. The book fInishes with a glossary of X and Motif terms. Source Code Conventions Like most books on programming, this book contains a lot of source code. I believe that the keys to understanding any program are its naming conventions. Mine generally follow those of the MIT X consortium and are as below: • Function names consist of one or more words, run together, with the fIrst letter of each word capitalized (eg, MyFunction). • Variables consist of one or more words, with all characters in lowercase. Where a variable name consists of several words, underscores separate the words; in cases where the name is a contraction of multiple words (eg, appshell for 'application shell'), no underscores are used. My conventions do not follow those of the X consortium in the naming of variables used for widgets: I do not differentiate between such variables and other program variables. The X standard specifies that widget names consist of one or more words, run together, with the fIrst letter of every word but the fIrst capitalized. • Macro names and symbolic constants consist of one or more words, separated by underscores, with all characters in uppercase (Eg, A_CONSTANT). This follows the X Consortium specification; mixed case is reserved for X "action" macros such as XtSetArg. • Names beginning with a capital 'X are reserved for use by the X libraries and are not used as program variables or functions. Specific prefIxes are: "X" for low-level names (those associated with Xlib) , "Xt" for names associated with the X Toolkit (also known as the Intrinsics), and "Xm" for Motif. Preface xi • I do not use naming techniques to differentiate between global and local variables. All global variables are declared in one location and typically carry the same names from program to program. As my tendency is to use global variables to hold utility data only (and. for Motif programs. some widgets of general interest). I feel that no naming conventions are needed. My source-code structuring rules are as follows: • Indentation is four characters per level. • Braces are aligned with the level they enclose. except for function bodies (where they are aligned at column 1). Braces always occupy their own line: I do not follow the "K&R" style of putting an opening brace at the end of the line above the block it encloses. • Function parameter declarations are indented four characters and appear between function definition and function body; as described below. the ANSI format is not used. Function variables are similarly indented four characters. I do not use block-level variables. • Function parameters are declared using the "old style": parameter names are specified in the header line. with declarations between the header line and the function body. I do this primarily because ANSI C compilers have been slow to arrive in the UNIX world. • When a new function is introduced. it is "prototyped" out of context. In this case only. I align the function's parameters with its name - I believe this approach is less cluttered than standard indentation. Text Conventions The text of this book is set in a proportional typeface. Italics are used to highlight the initial use of glossary terms. for names of Motif widget classes. names of standard programs (eg. a.out). common parenthetical abbreviations (eg. eg). and occasionally. to emphasize a point in the text. Italics are also used for computer names in captions. Boldface is used in the text to highlight user interface items in a specific program (eg. the File menu). Computer text is set in a monospaced font (Letter Gothi c) that resembles lineprinter output. Such usage includes source listings (which are separate from the text). as well as variable and function names that appear in the text. This typeface is italicized to indicate that the italicized word(s) are replaced in actual usage (eg. the name of a prototype function would be italicized). This book uses two types of quotation marks: text and literal. Text quotation marks are used to highlight idiomatic phrases and actual quotations; single quotes (' and .) for "jargon" words. double quotes (" and ") for all else. Literal quotation marks (") are used in the text to delimit literal strings (eg. "MyString"). Sin~e quotes (') are used in the C manner to delimit Single-character strings (eg. '\nT; such strings may contain C "escape" characters. Double quotes (") do not connote C semantics (ie. such strings are not NUL-terminated) unless otherwise indicated. xii Preface A final note about notes. I footnote heavily and use footnotes to provide infonnation tangential to the topic under consideration. I consider such notes important - they often clarify obscure points of a topic - but understand that some people find them distracting. They may be safely skipped, or their reading may be postponed until a more convenient time. Caveats The first and foremost caveat is that this book is written from the perspective of Motif 1.0 and XII Release 3 (XIIR3) - versions that are essentially obsolete. I made this decision after an infonnal poll indicated that many sites are limited to this configuration; I did not want to give such sites a book that they couldn't use. For Motif, this decision was easy to make: Motif 1.1 was released during the writing of this book and did not come into widespread usage until just before it went to press. Where I feel that 1.1 provides a better technique than 1.0, I note this possibility, either in the text or as a footnote. The only place where this rule does not hold is in Chapter 18: the clipboard mechanism did not work properly under 1.0 and thus must be discussed from the perspective of 1.1. For X, this was a more difficult decision: XII Release 4 (XIIR4) was commonly available while I was writing this book, and R5 was released before the fmal draft. However, my poll indicated that many people were still using R3. Moreover, the differences between R3 and R4 have a minimal effect on this book: the biggest impact is on type conversion, as described in Chapter 17. The second caveat is that this book is written from a UNIX perspective, while X and Motif are available on other platfonns. In answer to this, I must answer "I go with what I know" - I use a UNIX system and am not able to test on other platfonns. There is also the question of how much infonnation to include - adding infonnation specific to VMS and other systems would cause the book to grow in size. I believe that the infonnation contained in this book is of use to such people; the examples, however, are most likely not. A third (and final) caveat is that every vendor's system is slightly different. The placement of files, even the names of some files, may differ between your site and this book. I have attempted, in all cases, to use standard names. In some cases, where I know of differences, I present alternatives. Acknowledgments I've been working on this book for apprOximately one and a half years, and I've tried to keep track of all the people that had an fufluence on it; at one time, that list even included the Boston Red Sox. The first influence occurred in 1987, when Art Shane gave me the opportunity to branch out from programming into writing. The idea for this particular book came from my brother, David, who impressed upon me the fact that the Motif market was wide open. For help and support during the writing, I thank my editors at Springer-Verlag, Mark, David, and Andrea. For their comments, I thank both the reviewers that I

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.