ltkx

GUI toolkit for X11 (WIP)
git clone git://lumidify.org/ltkx.git
Log | Files | Refs | README | LICENSE

main1.c (2972B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <X11/Xlib.h>
      4 #include <X11/Xutil.h>
      5 #include <X11/Xos.h>
      6 
      7 int main(int argc, char *argv[])
      8 {
      9     Display *display;
     10     int screen;
     11     Window window;
     12     GC gc;
     13 
     14     unsigned long black, white;
     15     XColor green;
     16     Colormap colormap;
     17     display = XOpenDisplay((char *)0);
     18     screen = DefaultScreen(display);
     19     colormap = DefaultColormap(display, screen);
     20     black = BlackPixel(display, screen);
     21     white = WhitePixel(display, screen);
     22     XParseColor(display, colormap, "#00FF00", &green);
     23     XAllocColor(display, colormap, &green);
     24     window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, 200, 300, 0, white, white);
     25     XSetStandardProperties(display, window, "Random Window", NULL, None, NULL, 0, NULL);
     26     XSelectInput(display, window, ExposureMask|ButtonPressMask|KeyPressMask);
     27     gc = XCreateGC(display, window, 0, 0);
     28     XSetBackground(display, gc, black);
     29     XSetForeground(display, gc, black);
     30     XClearWindow(display, window);
     31     XMapRaised(display, window);
     32 
     33     XEvent event;
     34     KeySym key;
     35     char text[255];
     36 
     37 
     38 
     39 /* this pointer will hold the returned font structure. */
     40 XFontStruct* font_info;
     41 
     42 /* try to load the given font. */
     43 char* font_name = "*";
     44 font_info = XLoadQueryFont(display, font_name);
     45 if (!font_info) {
     46     fprintf(stderr, "XLoadQueryFont: failed loading font '%s'\n", font_name);
     47 }
     48 
     49 XSetFont(display, gc, font_info->fid);
     50 /* assume that win_width contains the width of our window, win_height        */
     51 /* contains the height of our window, and 'win' is the handle of our window. */
     52 
     53 /* some temporary variables used for the drawing. */
     54 int x, y;
     55 
     56 /* draw a "hello world" string on the top-left side of our window. */
     57 x = 0;
     58 y = 0;
     59 XDrawString(display, window, gc, x, y, "hello world", strlen("hello world"));
     60 
     61 /* draw a "middle of the road" string in the middle of our window. */
     62 char* text_string = "middle of the road";
     63 /* find the width, in pixels, of the text that will be drawn using */
     64 /* the given font.                                                 */
     65 int string_width = XTextWidth(font_info, text_string, strlen(text_string));
     66 /* find the height of the characters drawn using this font.        */
     67 int font_height = font_info->ascent + font_info->descent;
     68 /*
     69 x = (win_width - string_width) / 2;
     70 y = (win_width - font_height) / 2;
     71 */
     72 x = 0;
     73 y = 0;
     74 XDrawString(display, window, gc, x, y, "hello world", strlen("hello world"));
     75 
     76     while(1)
     77     {
     78         XNextEvent(display, &event);
     79         if (event.type == KeyPress && XLookupString(&event.xkey, text, 255, &key, 0) == 1)
     80         {
     81             if (text[0] == 'q')
     82             {
     83 XDrawString(display, window, gc, x, y, "hello world", strlen("hello world"));
     84 /*
     85                 XFreeGC(display, gc);
     86                 XFreeColormap(display, colormap);
     87                 XDestroyWindow(display, window);
     88                 XCloseDisplay(display);
     89                 exit(0);
     90 		*/
     91             }
     92         }
     93     }
     94 }