GUIdebook: Graphical User Interface galleryHome > Articles > “The X Window System” > Program listing
GUIsTimelinesScreenshotsIconsSoundsSplashesApplicationsAdsVideosArticlesBooksTutorialsExtras
Go backListing

A listing accompanying the article “The X Window System,” published in Byte, issue 1/89, pp. 359.

Listing 1: An X Window program for a window that displays Hello, World. If you press a mouse button, it responds Hi!.


/*********** x include files **********/
#include  
#include 

/*********** declarations ************/ 
char hello[] = {"Hello, World."}; 
char hi[] = {"Hi!"};

/************ main *****************/
main(argc, argv)
int argc;
char *argv[];
{
Display *mydisplay;
Window mywindow;
GC mygc;
XEvent myevent;
KeySym mykey;
XSizeHints myhint;
int i, done;
char text[10];

/* initialization */
mydisplay = XOpenDisplay("");
myhint.x = 200;
myhint.y = 200;
myhint.width = 350;
myhint.height = 250;
myhint.flags = PPosition | PSize;
mywindow = XCreateSimpleWindow(mydisplay, DefaultRootWindow(mydisplay),
        myhint.x, myhint.y, myhint.width, myhint.height, 5, 0, 1); 
XSetStandardProperties(mydisplay, mywindow, hello, hello, None, argv,
        argc, &myhint);
mygc = XCreateGC(mydisplay, mywindow, 0, 0);
XSelectInput(mydisplay, mywindow, ButtonPressMask|KeyPressMask|ExposureMask); 
XMapWindow(mydisplay, mywindow);

/* main event-reading loop */ 
done = 0; 
while( done == 0) 
        {
        XNextEvent(mydisplay, &myevent); 
        switch(myevent.type); 
                {
                case Expose: /* repaint window on expose events */ 
                        if(myevent.xexpose.count == 0)
                                XDrawImageString( mydisplay, mywindow, mygc,
                                        50, 50, hello, strlen(hello) ); 
                        break;
                case MappingNotify: /* process keyboard mapping changes */ 
                        XRefreshKeyboardMapping(&myevent); 
                        break; 
                case ButtonPress:
                        XDrawImageString(mydisplay, mywindow, mygc,
                                myevent.xbutton.x, myevent.xbutton.y, hi, strlen (hi) ); 
                        break; 
                case KeyPress:
                        i = XLookupString(&myevent, text, 10, &mykey, 0); 
                        if( i==1 && text[0] == 'q')
                                done = 1; 
                        break;
                } /* switch (myevent.type) */ 
        } /* while (done == 0) */

/* Termination */
XUnmapWindow(mydisplay, mywindow);
XFreeGC(mydisplay, mygc);
XDestroyWindow(mydisplay, mywindow);
XCloseDisplay(mydisplay);
exit(1);
}

Page added on 25th June 2005.

Copyright © 2002-2006 Marcin Wichary, unless stated otherwise.