These tutorials are excerpted from the book "Motif Programming: The Essentials and More" , by Marshall Brain.
Because Motif is built on top of X, however, and because X is a detailed and complicated environment, Motif is for many an intimidating, difficult-to-learn programming system. Many people try to learn it and get extremely frustrated by the complexity. The purpose of these tutorials is to ease you into Motif programming as smoothly and as quickly as possible, despite the complexity associated with the environment. It really isn't that hard to get started, as long as you concentrate only on the essentials at first. Once you know the essentials, then the rest can be added incrementally and very quickly over time.
The next thing you would like to have is a few books. You absolutely cannot program X and Motif without having two or three books by your side. Here are some suggestions, and you will have to go look and decide which ones you prefer. If you have the option to borrow them from someone before you buy, do that. Wait until the second or third tutorial before you invest.
Motif Programming: The Essentials and More
By Marshall Brain
Motif Programmer's Reference Manual
by the Open Software Foundation
An essential Motif reference book. Explains all widgets and their resources. Young's book contains some of this in the appendix, but this book explains it all. From Prentice Hall
X Window System - C library and Protocol Reference
by Scheifler, Gettys and Newman
All X window calls are documented and explained. Good if you are having to get down to the X level. A lot of this stuff can be gotten from on-line man pages. From Digital Press.
X and Motif Quick Reference Guide
by Randi J. Rost.
Contains summaries of all calls into X, Xt and Motif libraries, plus other random info. For X11 Release 4 and Motif 1.1. From Digital Press.
Man Pages, Section 3
Start up Xman and look in section 3. All of the X and Xt procedures we use have man pages. [system dependent - you may or may not have them on your system, or they may be in a different place.]
comp.windows.x.motif
This newsgroup deals in Motif questions and answers. Get on the news reader and check it out. Don't flood it with a lot of super-basic questions, just watch and learn.
Another thing that is nice to have when starting to learn Motif is a guru. If you can find someone on your system who has already cut his/her teeth on Motif, you will be doing yourself a favor if you can strike up some kind of question-answering deal with that person. A knowledgable (and preferrably sympathetic and kind) person can be extremely valuable when you get stuck.
Finally, you need an account on a machine that supports X and Motif, and that has the Motif libraries available. The libary header files, in the ideal case, exist in the directory /usr/include/Xm, while the libraries libXm, libXt and libX11 exist in /usr/lib. Unfortunately they are often in non-standard location and you will have to hunt for them or ask your guru.
Armed with equipment, software, books and knowledge, you are now ready to enter the world of Motif programming.
X code generally runs on Unix machines, although you do occasionally see it running on Windows NT. Unix and the standard C libraries (the normal things like stdio.h, math.h, etc.) can be considered the "bottom level" of code. On top of this foundation runs the X Window system, which is made up of the X libraries (from a programmer's view). These libraries are often referred to as Xlib (pronounced "ecks- libe"). On top of that runs the "Xt intrinsics", which is another group of C functions. Finally, teetering on top of all of this is something called the Motif widget set. Motif tries to be an object oriented set of routines that allows a programmer to build an X user interface in a finite amount of time. Then on top of the pile of libaries you write your application program.
When you build a Motif application, you make calls into the Motif library, the Xt library, Xlib, and the normal Unix system libraries. This is one of the things that makes Motif programming so complicated - you have to know what to call when, and you have to know how things react with one another. On the other hand, you can do useful work by knowing a very small subset of the total, and then incrementally increase your knowledge as the need arises.
Motif itself is described as a "widget set". A widget is simply a user interface object--a pushbutton, a scroll bar, a text area, a list box, etc. You build your applications from collections of widgets. The widgets collect input from the user. Motif is a collection of 40 or so widgets. Once you have learned how to use all of the widgets in the Motif widget set, you "know" motif.
"Why", you might ask, "should I use Motif rather than writing to Xlib directly?" Basically because Motif is a time-saver. Complete, nice-looking user interfaces written in X tend to be extremely long. Motif tends to make the creation of user interface code shorter and much easier.
You also need to know that X, and therefore Motif, is an "event driven programming environment". This means that the user interface is programmed to respond to user "events". When the user hits a key on the keyboard, that generates an "event" that the program must then respond to. When the user moves or clicks the mouse, events are also generated. An "event loop" sits and waits for user events. When events occur, the event loop calls the correct piece of code to handle them. One of the joys of programming in Motif is that the Motif code handles almost all of the low-level user-generated events, updates the screen based on those events, and only then, after doing most of the work, does it call the code you have written.
A lot of this might not make much sense to you at the moment, and if that's the case don't panic. It will make more sense as we go through examples.
You will do yourself a favor if you actually type the code presented. Actually typing the code will help you get used to the variable names, types, procedure calls, etc. Some of the naming conventions will seem extremely arbitrary or long-winded to you initially, but after you've typed a lot of Motif code things will begin to make sense to you. The naming scheme is well organized and fairly consistent.
If you hunger for more or better examples, get the book "Motif Programming: The Essentials and More", by Marshall Brain. All of the source code from it can be found on the ftp site
#include <Xm/Xm.h> #include <Xm/Label.h> XtAppContext context; XmStringCharSet char_set=XmSTRING_DEFAULT_CHARSET; Widget toplevel, label; main(argc,argv) int argc; char *argv[]; { Arg al[10]; int ac; /* create the toplevel shell */ toplevel = XtAppInitialize(&context,"",NULL,0, &argc,argv,NULL,NULL,0); /* create label widget */ ac=0; XtSetArg(al[ac],XmNlabelString, XmStringCreateLtoR("Hello World", char_set)); ac++; label=XmCreateLabel(toplevel,"label",al,ac); XtManageChild(label); XtRealizeWidget(toplevel); XtAppMainLoop(context); }
It looks confusing, but that is the simplest Motif program you can write. It will begin to make sense in the next tutorial. To compile it, at least on a UNIX machine with the libraries in their standard places, type:
cc -o first first.c -lXm -lXt -lX11
If it doesn't compile, you need to find someone who has already compiled Motif code on your system and ask for help, or search out the Motif include files and libraries on your system. They are often in strange places. In many cases you end up typing something like this (or making a makefile that says the same thing):
cc -o first first.c -lXm -lXt -lX11 -D_NO_PROTO -I/usr/xxx/ -I/usr/yyy - L/usr/aaa -L/usr/bbb
To run the program, type "first". You should see a small window containing the word "Hello World". You can resize it, iconify it, etc.
Play around with "first" for awhile, and then go to the second tutorial. In that tutorial you will learn what all of the code in "first.c" does.