Annotation of 43BSDTahoe/new/X/doc/Xlib/appendix, revision 1.1.1.1

1.1       root        1: .bp
                      2: .SH
                      3: Appendix A - Example X Programs
                      4: .XS
                      5: Appendix A - Example X Programs
                      6: .XE
                      7: .SH
                      8: Xrefresh - Refresh the Entire Screen.
                      9: .PP
                     10: .IN "Example Programs" "xrefresh"
                     11: The following program (\fIxrefresh\fP) is the simplest X application to date.
                     12: It is useful if your screen has gotten trashed,
                     13: either by a program error
                     14: (for example, using the \fIRootWindow\fP when you didn't mean to),
                     15: or by the system putting messages out underneath you.
                     16: .IN "Examples of Use" "XOpenDisplay"
                     17: .IN "Examples of Use" "XCreateWindow"
                     18: .IN "Examples of Use" "XMapWindow"
                     19: .IN "Examples of Use" "XDestroyWindow"
                     20: .IN "Examples of Use" "XFlush"
                     21: .nf
                     22: .DS
                     23: .TA .5i 3i
                     24: .ta .5i 3i
                     25: .cs R36
                     26: #include <X/Xlib.h>
                     27: #include <stdio.h>
                     28: /*
                     29:  * Copyright 1985, Massachusetts Institute of Technology.
                     30:  * This program just throws up a window over the whole screen, causing
                     31:  * exposure events to be generated on all windows.  This may be useful
                     32:  * to cause the whole screen to be repainted when it has somehow gotten
                     33:  * trashed.
                     34:  */
                     35: 
                     36: main(argc, argv)
                     37: int argc;
                     38: char **argv;
                     39: {
                     40:        Window w;
                     41: 
                     42:        if (XOpenDisplay(argc ? argv[1] : "\\0") == NULL) 
                     43:                fprintf (stderr, "Could not open Display!\\n");
                     44: 
                     45: .IN "Examples of Use" "DisplayWidth"
                     46: .IN "Examples of Use" "DisplayHeight"
                     47: .IN "Examples of Use" "BlackPixmap" 
                     48:        w = XCreateWindow(RootWindow, 0, 0, DisplayWidth(), DisplayHeight(),
                     49:                0, (Pixmap) 0, (Pixmap) 0);
                     50:        XMapWindow(w);                  /* put it up on the screen      */
                     51:        XDestroyWindow(w);              /* throw it away                */
                     52: 
                     53:        XFlush();                       /* and make sure the server sees it*/
                     54: }
                     55: .cs R
                     56: .DE
                     57: .fi
                     58: .PP
                     59: In short, \fIxrefresh\fP connects to the display, creates a window with a black
                     60: background and zero width border over the root window, maps it to
                     61: the screen, and destroys the window.
                     62: The side effects of this will be to send exposure events to the client programs
                     63: who have selected exposure events on all mapped unobscured windows.
                     64: This causes most clients to repaint their windows.
                     65: The call to \fIXFlush\fP is necessary since no input call occurs after the
                     66: \fIXDestroyWindow\fP call to flush the output buffer.
                     67: Since the background pixmap is 0, the window will be covered with the
                     68: background pixmap, for a pleasing effect.
                     69: The most common X programming mistake is to forget to flush the
                     70: output buffer when first experimenting with X.
                     71: .SH
                     72: Xfd - Display a Font in a Window
                     73: .IN "Example Programs" "xrefresh"
                     74: .PP
                     75: The following program is more ambitious, as it parses many
                     76: arguments and defaults before creating a simple window to paint text in.
                     77: .nf
                     78: .DS
                     79: .TA .5i 1i 1.5i 2i 2.5i 3i 3.5i 4i 4.5i 5i 5.5i 6i 6.5i 7i 7.5i 8i
                     80: .ta .5i 1i 1.5i 2i 2.5i 3i 3.5i 4i 4.5i 5i 5.5i 6i 6.5i 7i 7.5i 8i
                     81: .cs R36
                     82: /* Copyright 1985, Massachusetts Institute of Technology */
                     83: #include <Xlib/Xlib.h>
                     84: #include <stdio.h>
                     85: #include <strings.h>
                     86: 
                     87: short gray_bits[16] = {
                     88:     0xaaaa, 0x5555, 0xaaaa, 0x5555,
                     89:     0xaaaa, 0x5555, 0xaaaa, 0x5555,
                     90:     0xaaaa, 0x5555, 0xaaaa, 0x5555,
                     91:     0xaaaa, 0x5555, 0xaaaa, 0x5555};
                     92: 
                     93: 
                     94: char chars[9];
                     95: int last_line;
                     96: 
                     97: main(argc, argv) 
                     98:     int argc;
                     99:     char **argv;
                    100:     {
                    101:     Window w;                          /* window id of the window */
                    102:     FontInfo *fontInfo;                /* font to be displayed */
                    103:     int width;
                    104:     char *fontname = "vtsingle";
                    105:     register char *option;
                    106:     char *border_color, *back_color, *fore_color;      /* strings */
                    107:     int border_width;
                    108:     int reverse = 0;
                    109:     char *geometry;                    /* user supplied geometry spec */
                    110:     char def[32];                      /* default size */
                    111:     int defwidth, defheight;
                    112:     char display[128];
                    113:     register int i;
                    114:     OpaqueFrame window;                /* frame for the window */
                    115:     Pixmap border_pixmap;
                    116:     int background;                    /* color of background */
                    117:     int foreground;                    /* color of graph */
                    118:     int highlight;                     /* color of text, scale */
                    119:     Color cdef;                                /* color structure */
                    120: .IN "Examples of Use" "Color"
                    121: 
                    122: .IN "Examples of Use" "XGetDefault"
                    123:     if ((option = XGetDefault(argv[0],"ReverseVideo")) != NULL )
                    124:                if (strcmp (option, "on") == 0)
                    125:                        reverse = 1;
                    126:     if ((option = XGetDefault(argv[0],"BorderWidth")) != NULL)
                    127:        border_width = atoi(option);
                    128:     if ((border_color = XGetDefault(argv[0],"Border")) == NULL)
                    129:        border_color = XGetDefault(argv[0],"BorderColor");
                    130:     back_color = XGetDefault(argv[0],"Background");
                    131:     fore_color = XGetDefault(argv[0],"Foreground");
                    132:     display[0] = '\\0';
                    133: 
                    134:     for (i = 1; i < argc; i++) {               /* Parse line */
                    135:        if (argv[i][0] == '=') {
                    136:            geometry = argv[i];
                    137:            continue;
                    138:        }
                    139:        if (index(argv[i], ':') != NULL) {              /* host:display */
                    140:            (void)strncpy(display, argv[i], sizeof(display));
                    141:            continue;
                    142:        }
                    143:        if (strcmp(argv[i], "-rv") == 0) {              /* black on white */
                    144:            reverse = 1;
                    145:            continue;
                    146:        }
                    147:        if (strcmp(argv[i], "-bw") == 0) {              /* border width */
                    148:            if (++i >= argc) usage(argv[0]);
                    149:            border_width = atoi(argv[i]);
                    150:            continue;
                    151:        }
                    152:        if (strcmp(argv[i], "-bd") == 0) {              /* border color */
                    153:           if (++i >= argc) usage(argv[0]);
                    154:           border_color = argv[i];
                    155:           continue;
                    156:        }
                    157:        if (strcmp(argv[i], "-fg") == 0) {              /* foreground color */
                    158:           if (++i >= argc) usage(argv[0]);
                    159:           fore_color = argv[i];
                    160:           continue;
                    161:        }
                    162:        if (strcmp(argv[i], "-bg") == 0) {              /* background color */
                    163:           if (++i >= argc) usage(argv[0]);
                    164:           back_color = argv[i];
                    165:           continue;
                    166:        }
                    167:        if (argv[i][0] == '-') usage (argv[0]);
                    168:        fontname = argv[i];
                    169:     }
                    170: .IN "Examples of Use" "XOpenDisplay"
                    171:     if (!XOpenDisplay(display)) {
                    172:                fprintf (stderr, "%s: Could not open display %s!\\n", argv[0], display);
                    173:        exit(1);
                    174:     }
                    175: .IN "Examples of Use" "XOpenFont"
                    176:     if (!(fontInfo = XOpenFont (fontname))) {
                    177:                fprintf (stderr, "%s: Could not open font %s!\\n", argv[0], fontname);
                    178:        exit(1);
                    179:     }
                    180:     last_line = (unsigned char)(fontInfo->lastchar)/8;
                    181:     width = ComputeWidth (fontInfo);
                    182: 
                    183:     /* if DisplayCells are greater than 2, then on color display */
                    184: .IN "Examples of Use" "XParseColor"
                    185: .IN "Examples of Use" "XGetHardwareColor"
                    186: .IN "Examples of Use" "DisplayCells"
                    187:     if (border_color && DisplayCells() > 2 &&
                    188:        XParseColor(border_color, &cdef) && XGetHardwareColor(&cdef))
                    189: .IN "Examples of Use" "XMakeTile"
                    190:        border_pixmap = XMakeTile(cdef.pixel);
                    191:     else if (border_color && strcmp(border_color, "black") == 0)
                    192: .IN "Examples of Use" "BlackPixmap"
                    193:        border_pixmap = BlackPixmap;
                    194:     else if (border_color && strcmp(border_color, "white") == 0)
                    195: .IN "Examples of Use" "WhitePixmap"
                    196:        border_pixmap = WhitePixmap;
                    197:     else
                    198: .IN "Examples of Use" "XStorePixmap"
                    199:        border_pixmap = XMakePixmap (XStoreBitmap (16, 16, gray_bits),
                    200:                                        BlackPixel, WhitePixel);
                    201: .IN "Examples of Use" "WhitePixel"
                    202: .IN "Examples of Use" "BlackPixel"
                    203:     if (back_color && DisplayCells() > 2 &&
                    204:        XParseColor(back_color, &cdef) && XGetHardwareColor(&cdef)) {
                    205:        background = cdef.pixel;
                    206:     } else if (back_color && strcmp(back_color, "white") == 0) {
                    207:        background = WhitePixel;
                    208:        reverse = 0;
                    209:     } else if (back_color && strcmp(back_color, "black") == 0) {
                    210:        background = BlackPixel;
                    211:        reverse = 0;
                    212:     } else
                    213:        background = BlackPixel;
                    214: 
                    215:     if (fore_color && DisplayCells() > 2 &&
                    216:        XParseColor(fore_color, &cdef) && XGetHardwareColor(&cdef)) {
                    217:        foreground = cdef.pixel;
                    218:     } else if (fore_color && strcmp(fore_color, "black") == 0) {
                    219:        foreground = BlackPixel;
                    220:        reverse = 0;
                    221:     } else if (fore_color && strcmp(fore_color, "white") == 0) {
                    222:        foreground = WhitePixel;
                    223:        reverse = 0;
                    224:     } else
                    225:        foreground = WhitePixel;
                    226: 
                    227:     if (reverse) {
                    228:        highlight = background;
                    229:        background = foreground;
                    230:        foreground = highlight;
                    231:     }
                    232: 
                    233: .IN "Examples of Use" "XMakeTile"
                    234: .IN "Examples of Use" "OpaqueFrame"
                    235:     window.bdrwidth = border_width;
                    236:     window.border = border_pixmap;
                    237:     window.background = XMakeTile(background);
                    238: 
                    239:     defwidth = width + 10;
                    240:     defheight = fontInfo->height * (last_line + 1) + 10;
                    241:     (void) sprintf(def, "=%dx%d+300+300", defwidth, defheight);
                    242: .IN "Examples of Use" "XCreate"
                    243:     w = XCreate ("Font Display", argv[0], geometry, def, &window,
                    244:                defwidth, defheight);
                    245: 
                    246:     if (!w) {
                    247:        fprintf (stderr, "XCreateWindow failed\\n");
                    248:        exit(1);
                    249:     }
                    250: .IN "Examples of Use" "XSelectInput"
                    251:     XSelectInput (w, ExposeWindow | ButtonPressed);
                    252: .IN "Examples of Use" "XMapWindow"
                    253:     XMapWindow (w);
                    254:     while (1) {
                    255:        XEvent event;
                    256:        int i, j;
                    257: .IN "Examples of Use" "XWindowEvent"
                    258:        XWindowEvent (w, ExposeWindow | ButtonPressed, &event);
                    259:        if(event.type == ButtonPressed) exit(0);
                    260:        for (i=0;i<=last_line;i++) {
                    261:                    for (j=0;j<8;j++)
                    262:                chars[j] = (char)((8*i)+j);
                    263: .IN "Examples of Use" "XText"
                    264:                    XText (w, 5, 5+(i*fontInfo->height), chars, 8,
                    265:              fontInfo->id, foreground, background);
                    266:                    }
                    267:        }
                    268:     }    
                    269: 
                    270: usage (program)
                    271:        char *program;
                    272:     {
                    273:        fprintf(stderr,
                    274:                "usage: %s [host:display] [=geom]
                    275:                 [-fw] [-rv] [-bw] [-bd] [-fg] [-bg] fontname\\n", program);
                    276:        exit(1);
                    277:     }
                    278: 
                    279: ComputeWidth (fontInfo)
                    280:     FontInfo *fontInfo;
                    281:     {
                    282:     int maxwidth, i, j;
                    283: 
                    284:     /* Horrible hack needed for first line because line starts
                    285:        with \\0, and XStringWidth considers \\0 to terminate string */
                    286:     for (j=1;j<8;j++)
                    287:        chars[j] = j;
                    288: .IN "Examples of Use" "XStringWidth"
                    289:     maxwidth = XStringWidth (&chars[1], fontInfo, 0, 0);
                    290:     /* add the width of the '\\0' character, if it has one */
                    291:     if (fontInfo->firstchar == '\\0')
                    292:        maxwidth += (fontInfo->fixedwidth ? 
                    293:            fontInfo->width : fontInfo->widths[0]);
                    294:     
                    295:     /* now measure the width of remaining lines */
                    296:     for (i=1;i<=last_line;i++) {
                    297:        int this_width;
                    298:        for (j=0;j<8;j++)
                    299:            chars[j] = (char)((8*i)+j);
                    300:        this_width = XStringWidth (chars, fontInfo, 0, 0);
                    301:        if (this_width > maxwidth)
                    302:            maxwidth = this_width;
                    303:        }
                    304:     return (maxwidth);
                    305:     }
                    306: .cs R
                    307: .DE
                    308: .fi
                    309: .PP
                    310: After parsing all the arguments,
                    311: the program allocates colors if on a color display for the foreground
                    312: and background text colors, and the background and border pixmaps.
                    313: It then creates the window, making sure that it is the right size,
                    314: and then maps it to the screen.
                    315: The fact the window was mapped to the screen generates an expose
                    316: window event.
                    317: The program repaints the window on each expose window event,
                    318: and exits if any mouse button event is detected on the window.
                    319: .EH ''\fB- ii -\fP''
                    320: .OH ''\fB- ii -\fP''
                    321: .bp 
                    322: .XS
                    323: Index
                    324: .XE
                    325: .PX

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.