DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download
Index: ┃ T a

⟦9b0894908⟧ TextFile

    Length: 10059 (0x274b)
    Types: TextFile
    Names: »appendix«

Derivation

└─⟦a0efdde77⟧ Bits:30001252 EUUGD11 Tape, 1987 Spring Conference Helsinki
    └─ ⟦526ad3590⟧ »EUUGD11/gnu-31mar87/X.V10.R4.tar.Z« 
        └─⟦2109abc41⟧ 
            └─ ⟦this⟧ »./X.V10R4/doc/Xlib/appendix« 

TextFile

.bp
.SH
Appendix A - Example X Programs
.XS
Appendix A - Example X Programs
.XE
.SH
Xrefresh - Refresh the Entire Screen.
.PP
.IN "Example Programs" "xrefresh"
The following program (\fIxrefresh\fP) is the simplest X application to date.
It is useful if your screen has gotten trashed,
either by a program error
(for example, using the \fIRootWindow\fP when you didn't mean to),
or by the system putting messages out underneath you.
.IN "Examples of Use" "XOpenDisplay"
.IN "Examples of Use" "XCreateWindow"
.IN "Examples of Use" "XMapWindow"
.IN "Examples of Use" "XDestroyWindow"
.IN "Examples of Use" "XFlush"
.nf
.DS
.TA .5i 3i
.ta .5i 3i
.cs R36
#include <X/Xlib.h>
#include <stdio.h>
/*
 * Copyright 1985, Massachusetts Institute of Technology.
 * This program just throws up a window over the whole screen, causing
 * exposure events to be generated on all windows.  This may be useful
 * to cause the whole screen to be repainted when it has somehow gotten
 * trashed.
 */

main(argc, argv)
int argc;
char **argv;
{
	Window w;

	if (XOpenDisplay(argc ? argv[1] : "\\0") == NULL) 
		fprintf (stderr, "Could not open Display!\\n");

.IN "Examples of Use" "DisplayWidth"
.IN "Examples of Use" "DisplayHeight"
.IN "Examples of Use" "BlackPixmap" 
	w = XCreateWindow(RootWindow, 0, 0, DisplayWidth(), DisplayHeight(),
		0, (Pixmap) 0, (Pixmap) 0);
	XMapWindow(w);			/* put it up on the screen 	*/
	XDestroyWindow(w);		/* throw it away		*/

	XFlush();			/* and make sure the server sees it*/
}
.cs R
.DE
.fi
.PP
In short, \fIxrefresh\fP connects to the display, creates a window with a black
background and zero width border over the root window, maps it to
the screen, and destroys the window.
The side effects of this will be to send exposure events to the client programs
who have selected exposure events on all mapped unobscured windows.
This causes most clients to repaint their windows.
The call to \fIXFlush\fP is necessary since no input call occurs after the
\fIXDestroyWindow\fP call to flush the output buffer.
Since the background pixmap is 0, the window will be covered with the
background pixmap, for a pleasing effect.
The most common X programming mistake is to forget to flush the
output buffer when first experimenting with X.
.SH
Xfd - Display a Font in a Window
.IN "Example Programs" "xrefresh"
.PP
The following program is more ambitious, as it parses many
arguments and defaults before creating a simple window to paint text in.
.nf
.DS
.TA .5i 1i 1.5i 2i 2.5i 3i 3.5i 4i 4.5i 5i 5.5i 6i 6.5i 7i 7.5i 8i
.ta .5i 1i 1.5i 2i 2.5i 3i 3.5i 4i 4.5i 5i 5.5i 6i 6.5i 7i 7.5i 8i
.cs R36
/* Copyright 1985, Massachusetts Institute of Technology */
#include <Xlib/Xlib.h>
#include <stdio.h>
#include <strings.h>

short gray_bits[16] = {
    0xaaaa, 0x5555, 0xaaaa, 0x5555,
    0xaaaa, 0x5555, 0xaaaa, 0x5555,
    0xaaaa, 0x5555, 0xaaaa, 0x5555,
    0xaaaa, 0x5555, 0xaaaa, 0x5555};


char chars[9];
int last_line;

main(argc, argv) 
    int argc;
    char **argv;
    {
    Window w;				/* window id of the window */
    FontInfo *fontInfo;		/* font to be displayed */
    int width;
    char *fontname = "vtsingle";
    register char *option;
    char *border_color, *back_color, *fore_color;	/* strings */
    int border_width;
    int reverse = 0;
    char *geometry;			/* user supplied geometry spec */
    char def[32];			/* default size */
    int defwidth, defheight;
    char display[128];
    register int i;
    OpaqueFrame window;		/* frame for the window */
    Pixmap border_pixmap;
    int background;	    		/* color of background */
    int foreground;		    	/* color of graph */
    int highlight;			/* color of text, scale */
    Color cdef;				/* color structure */
.IN "Examples of Use" "Color"

.IN "Examples of Use" "XGetDefault"
    if ((option = XGetDefault(argv[0],"ReverseVideo")) != NULL )
    		if (strcmp (option, "on") == 0)
			reverse = 1;
    if ((option = XGetDefault(argv[0],"BorderWidth")) != NULL)
    	border_width = atoi(option);
    if ((border_color = XGetDefault(argv[0],"Border")) == NULL)
	border_color = XGetDefault(argv[0],"BorderColor");
    back_color = XGetDefault(argv[0],"Background");
    fore_color = XGetDefault(argv[0],"Foreground");
    display[0] = '\\0';

    for (i = 1; i < argc; i++) {		/* Parse line */
	if (argv[i][0] == '=') {
	    geometry = argv[i];
	    continue;
	}
	if (index(argv[i], ':') != NULL) {		/* host:display */
	    (void)strncpy(display, argv[i], sizeof(display));
	    continue;
	}
	if (strcmp(argv[i], "-rv") == 0) {		/* black on white */
	    reverse = 1;
	    continue;
	}
	if (strcmp(argv[i], "-bw") == 0) {		/* border width */
	    if (++i >= argc) usage(argv[0]);
	    border_width = atoi(argv[i]);
	    continue;
	}
	if (strcmp(argv[i], "-bd") == 0) {		/* border color */
	   if (++i >= argc) usage(argv[0]);
	   border_color = argv[i];
	   continue;
	}
	if (strcmp(argv[i], "-fg") == 0) {		/* foreground color */
	   if (++i >= argc) usage(argv[0]);
	   fore_color = argv[i];
	   continue;
	}
	if (strcmp(argv[i], "-bg") == 0) {		/* background color */
	   if (++i >= argc) usage(argv[0]);
	   back_color = argv[i];
	   continue;
	}
	if (argv[i][0] == '-') usage (argv[0]);
	fontname = argv[i];
    }
.IN "Examples of Use" "XOpenDisplay"
    if (!XOpenDisplay(display)) {
       	fprintf (stderr, "%s: Could not open display %s!\\n", argv[0], display);
	exit(1);
    }
.IN "Examples of Use" "XOpenFont"
    if (!(fontInfo = XOpenFont (fontname))) {
       	fprintf (stderr, "%s: Could not open font %s!\\n", argv[0], fontname);
	exit(1);
    }
    last_line = (unsigned char)(fontInfo->lastchar)/8;
    width = ComputeWidth (fontInfo);

    /* if DisplayCells are greater than 2, then on color display */
.IN "Examples of Use" "XParseColor"
.IN "Examples of Use" "XGetHardwareColor"
.IN "Examples of Use" "DisplayCells"
    if (border_color && DisplayCells() > 2 &&
	XParseColor(border_color, &cdef) && XGetHardwareColor(&cdef))
.IN "Examples of Use" "XMakeTile"
	border_pixmap = XMakeTile(cdef.pixel);
    else if (border_color && strcmp(border_color, "black") == 0)
.IN "Examples of Use" "BlackPixmap"
	border_pixmap = BlackPixmap;
    else if (border_color && strcmp(border_color, "white") == 0)
.IN "Examples of Use" "WhitePixmap"
	border_pixmap = WhitePixmap;
    else
.IN "Examples of Use" "XStorePixmap"
	border_pixmap = XMakePixmap (XStoreBitmap (16, 16, gray_bits),
					BlackPixel, WhitePixel);
.IN "Examples of Use" "WhitePixel"
.IN "Examples of Use" "BlackPixel"
    if (back_color && DisplayCells() > 2 &&
	XParseColor(back_color, &cdef) && XGetHardwareColor(&cdef)) {
	background = cdef.pixel;
    } else if (back_color && strcmp(back_color, "white") == 0) {
	background = WhitePixel;
	reverse = 0;
    } else if (back_color && strcmp(back_color, "black") == 0) {
	background = BlackPixel;
	reverse = 0;
    } else
	background = BlackPixel;

    if (fore_color && DisplayCells() > 2 &&
	XParseColor(fore_color, &cdef) && XGetHardwareColor(&cdef)) {
	foreground = cdef.pixel;
    } else if (fore_color && strcmp(fore_color, "black") == 0) {
	foreground = BlackPixel;
	reverse = 0;
    } else if (fore_color && strcmp(fore_color, "white") == 0) {
	foreground = WhitePixel;
	reverse = 0;
    } else
	foreground = WhitePixel;

    if (reverse) {
	highlight = background;
	background = foreground;
	foreground = highlight;
    }

.IN "Examples of Use" "XMakeTile"
.IN "Examples of Use" "OpaqueFrame"
    window.bdrwidth = border_width;
    window.border = border_pixmap;
    window.background = XMakeTile(background);

    defwidth = width + 10;
    defheight = fontInfo->height * (last_line + 1) + 10;
    (void) sprintf(def, "=%dx%d+300+300", defwidth, defheight);
.IN "Examples of Use" "XCreate"
    w = XCreate ("Font Display", argv[0], geometry, def, &window,
    		defwidth, defheight);

    if (!w) {
	fprintf (stderr, "XCreateWindow failed\\n");
	exit(1);
    }
.IN "Examples of Use" "XSelectInput"
    XSelectInput (w, ExposeWindow | ButtonPressed);
.IN "Examples of Use" "XMapWindow"
    XMapWindow (w);
    while (1) {
      	XEvent event;
      	int i, j;
.IN "Examples of Use" "XWindowEvent"
      	XWindowEvent (w, ExposeWindow | ButtonPressed, &event);
	if(event.type == ButtonPressed) exit(0);
      	for (i=0;i<=last_line;i++) {
       	    for (j=0;j<8;j++)
            	chars[j] = (char)((8*i)+j);
.IN "Examples of Use" "XText"
       	    XText (w, 5, 5+(i*fontInfo->height), chars, 8,
	      fontInfo->id, foreground, background);
       	    }
      	}
    }    

usage (program)
	char *program;
    {
	fprintf(stderr,
		"usage: %s [host:display] [=geom]
		 [-fw] [-rv] [-bw] [-bd] [-fg] [-bg] fontname\\n", program);
	exit(1);
    }

ComputeWidth (fontInfo)
    FontInfo *fontInfo;
    {
    int maxwidth, i, j;

    /* Horrible hack needed for first line because line starts
    	with \\0, and XStringWidth considers \\0 to terminate string */
    for (j=1;j<8;j++)
    	chars[j] = j;
.IN "Examples of Use" "XStringWidth"
    maxwidth = XStringWidth (&chars[1], fontInfo, 0, 0);
    /* add the width of the '\\0' character, if it has one */
    if (fontInfo->firstchar == '\\0')
    	maxwidth += (fontInfo->fixedwidth ? 
	    fontInfo->width : fontInfo->widths[0]);
    
    /* now measure the width of remaining lines */
    for (i=1;i<=last_line;i++) {
	int this_width;
    	for (j=0;j<8;j++)
	    chars[j] = (char)((8*i)+j);
	this_width = XStringWidth (chars, fontInfo, 0, 0);
	if (this_width > maxwidth)
	    maxwidth = this_width;
	}
    return (maxwidth);
    }
.cs R
.DE
.fi
.PP
After parsing all the arguments,
the program allocates colors if on a color display for the foreground
and background text colors, and the background and border pixmaps.
It then creates the window, making sure that it is the right size,
and then maps it to the screen.
The fact the window was mapped to the screen generates an expose
window event.
The program repaints the window on each expose window event,
and exits if any mouse button event is detected on the window.
.EH ''\fB- ii -\fP''
.OH ''\fB- ii -\fP''
.bp 
.XS
Index
.XE
.PX