|
Next: Further Information Up: Writing An Xlib Previous: Limitations Of The
Example Program Listing
/* win.c --- a very basic Xlib application, Jeff Pitchers, LUT 1993 */ /* The basic set of header files for any Xlib program */ #include <stdio.h> #include <X11/Xlib.h> #include <X11/Xutil.h> #include <X11/Xresource.h> #include <X11/keysym.h> Display *display; /* Server or display to which a connection will be made */ int screen; /* Screen number to use on that display */ Window win; /* The applications one and only window */ XEvent event; /* The event data structure */ GC gc1, gc2; /* Graphics context data structures */ XGCValues gs1,gs2; /* Graphics context settings */ char *message; /* Message printed in window */ static char progDefaultMessage[] = {"A Simple Xlib Program"}; main(argc, argv) int argc; char **argv; { /* Attempt to connect to the server and use the default screen */ if ((display = XOpenDisplay(NULL)) == NULL) { perror("Can't connect to server"); exit(1); } screen = DefaultScreen(display); /* Create a window */ win = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 200, 400, 300, 1, BlackPixel(display, screen), WhitePixel(display, screen)); /* Set up which event types the window will handle */ XSelectInput(display, win, ExposureMask | ButtonPressMask); /* Graphics contexts */ setUpGCs(); /* Attempt to get message from user-defined resource setting */ if ((message = XGetDefault(display, "win", "message")) == NULL) message = progDefaultMessage; /* Map the window in order for it to be displayed */ XMapWindow(display, win); /* Enter an infinite loop, awaiting and responding to the chosen events */ while (1) { XNextEvent(display, &event); switch (event.type) { case Expose: drawInToWindow(); break; /* Draw/re-draw everything */ case ButtonPress: exit(0); break; /* Quit on mouse button press */ default: break; } } } setUpGCs() /* Set up 2 graphics context resources in the display server */ /* gc1 for message and rectangle; gc2 for ellipse */ { /* Define a bitmap for the stipple pattern used to fill the ellipse */ #define stipple_width 3 #define stipple_height 3 static char stipple_bits[] = {0x02, 0x05, 0x02}; Pixmap stipple; /* Create the stipple for ellipse fill pattern of gc2 */ if ((stipple = XCreateBitmapFromData(display, RootWindow(display, screen), stipple_bits, stipple_width, stipple_height)) == 0) { perror("Can't create bitmap"); exit(1); } gc1 = XCreateGC(display, win, 0, &gs1); /* 0 -> use defaults */ gc2 = XCreateGC(display, win, 0, &gs2); /* 0 -> use defaults */ /* Now override some of the defaults */ XSetForeground(display, gc1, BlackPixel(display, screen)); XSetBackground(display, gc1, WhitePixel(display, screen)); XSetLineAttributes(display, gc1, 2, LineSolid, CapRound, JoinRound); XSetStipple(display, gc2, stipple); XSetFillStyle(display, gc2, FillOpaqueStippled); } drawInToWindow() { /* Print message, draw restangle and filled ellipse as defined */ /* by the GC */ XDrawString(display, win, gc1, 130, 20, message, strlen(message)); XDrawRectangle(display, win, gc1, 40, 40, 320, 220); XFillArc(display, win, gc2, 50, 50, 300, 200, 0, 360*64); }
JR Pitchers Wed May 3 16:45:04 BST 1995 |
|||||||||||||||||
With any suggestions or questions please feel free to contact us |