GUI

Programming Graphics, The X-Window System

Most of the programs are not complete with some Graphics. Nowadays almost all programs employ a GUI. This section explains how to write a graphics program. You need to recall the X-Window Concepts once again. Let us look at them from a programmers point of view.

Read a Tutorial on X-Window Graphics Programming by Willem Vermin (willem@sara.nl)

Turbo-C Style Graphics

There are some people who require some minimal amount of graphics and managing it using Turbo-C under DOS platform. Here is a program that can help you compiling and running those programs with minor modifications. Turbo-C graphics functions are emulated in a crude manner using X-lib calls. A program draw.c to draw a rectangle is shown below

#include “xwin.c”

void mycode()

{

rectangle(100,100,200,200);

}

int main()

{

makeWindow (600, 500); / creates a window to draw

return 0;

}

You just needs to include the file xwin.c, that is provided. Write all your code inside a function named ‘mycode’ . Compile it using the command

gcc -o draw -I/usr/include/X11 -L/usr/X11R6/lib -lXaw3d -lXt -lX11 draw.c

Here is a demo program demo.c and a Makefile.screenshot of few demo programs running in different windows

The X-Server & Application Programs

Remember that the definition of client and server are reversed in the case of X-windows. For Web server and all the Server is located somewhere far away from the user. The X-server is the program running on the Computer with you are interacting. It controls the bitmapped display, the keyboard and the mouse. Any application program that wants to write anything on the screen or get a mouse or keyboard input has to get it through the X-server. If my program wants to draw a circle on the screen, it sends a request to the X-server to do that job. No application program has direct access to the screen. The communication between the X-Server and the application is over a TCP/IP link, that means they can be on different machines connected over a network. Under this scheme more than one program can share the same display and input devices.

Writing X-Window Programs

When you write an X-application you need to know how to communicate to the X-server. This is done by function calls from the Xlib library. It has functions for establishing a connection with the server, drawing various shapes to a window, getting keyboard and mouse inputs etc. But it is very difficult to program using Xlib function calls. If you want to have push button on screen, you have to draw it using lines, dots etc. and then call a lot of functions to make arrangements to send the mouse clicks to your program. It is too complicated for most of us. The solution is simply not using it. Yes, we have higher level tool kits available to make the job easier.

X-toolkit and Widget Libraries

The X toolkit or Xt library provides you with readymade elements, called Widgets, like buttos, labels, drawing areas etc. that can be directly used by your program. The Athena Widget Library comes with the X-Window distribution. They are better toolkits like Motif, GTK and Qt etc. See the next section for more details.