click here to go to home page

Introduction to Motif Application Development

Part 1: Introduction and first program

by Marshall Brain ,
Interface Technologies, Inc.
(800) 224-4965
Copyright 1994 by Marshall Brain. All rights reserved.
Version 2.0, 1/16/95

These tutorials are excerpted from the book "Motif Programming: The Essentials and More" , by Marshall Brain.

Motif is a library of routines that make the creation of user interfaces in an X environment fairly easy and straightforward. The Motif libraries handle most of the low-level event handling tasks common to any GUI system for you, so that you are able to create sophisticated interfaces without having to contend with the all of the complexity of X.

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.


Before you can start to learn Motif, there are several prerequisites that you must meet. You must know Unix and C first, and it doesn't hurt if you have experience with Unix system calls. Obviously you will also need to know general Unix programming tools, like cc and make and dbx.

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.]
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.

Some terminology

Before we can start, you need to know a few basic concepts and some vocabulary. This section will give you that information.

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.

The General Plan of these Tutorials

Motif will isolate you from a lot of the minutae that you have to worry about in X. So in these tutorials, I will start talking about purely Motif programs, as best I can. I will add one or two capabilities at a time until you can write fairly involved Motif programs. As we go along however, we'll need to drop down to Xt or Xlib, and when we do I'll explain why and how as best I can.

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"< /A> , by Marshall Brain. All of the source code from it can be found on the ftp site

Your first program

To get a feel for all of this, here is your first program. Go ahead and type it in to a file called "first.c":

#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.