Introduction
This tutorial looks at writing a simple X-Windows application that displays the text 'Hello World' in the middle of the frame window. When the window is re-sized, the text is re-positioned accordingly.
Throughout this tutorial, I shall be making references to the equivalent Microsoft Windows API routines, for those of you who are more familiar with that environment.
The X-Windows environment
One of the major differences between the Microsoft Windows and X-Windows systems is that X-Windows is client/server based. This has a significant impact on networked machines, as the application may be running on the server, yet displaying windows on a client machine. Seen from the users perspective, the application looks like it is running locally, when in actual fact it isn't. The downside to this approach is that response times from client to server and back to client are limited to the bandwidth of the network connection.
Accordingly, there are some X-Windows calls that may seen a little strange, but I will discuss them as we progress through the tutorial.
[/php]
01 #include <stdio.h>
02 #include <stdlib.h>
03 #include <string.h>
04 #include <X11/Xlib.h>
05
06 int main (int argc, char *argv[])
07 {
08 Display *display;
09 Visual *visual;
10 int depth;
11 int text_x;
12 int text_y;
13 XSetWindowAttributes frame_attributes;
14 Window frame_window;
15 XFontStruct *fontinfo;
16 XGCValues gr_values;
17 GC graphical_context;
18 XKeyEvent event;
19 char hello_string[] = "Hello World";
20 int hello_string_length = strlen(hello_string);
21
22 display = XOpenDisplay(NULL);
23 visual = DefaultVisual(display, 0);
24 depth = DefaultDepth(display, 0);
25
26 frame_attributes.background_pixel = XWhitePixel(display, 0);
27 /* create the application window */
28 frame_window = XCreateWindow(display, XRootWindow(display, 0),
29 0, 0, 400, 400, 5, depth,
30 InputOutput, visual, CWBackPixel,
31 &frame_attributes);
32 XStoreName(display, frame_window, "Hello World Example");
33 XSelectInput(display, frame_window, ExposureMask | StructureNotifyMask);
34
35 fontinfo = XLoadQueryFont(display, "10x20");
36 gr_values.font = fontinfo->fid;
37 gr_values.foreground = XBlackPixel(display, 0);
38 graphical_context = XCreateGC(display, frame_window,
39 GCFont+GCForeground, &gr_values);
40 XMapWindow(display, frame_window);
41
42 while ( 1 ) {
43 XNextEvent(display, (XEvent *)&event);
44 switch ( event.type ) {
45 case Expose:
46 {
47 XWindowAttributes window_attributes;
48 int font_direction, font_ascent, font_descent;
49 XCharStruct text_structure;
50 XTextExtents(fontinfo, hello_string, hello_string_length,
51 &font_direction, &font_ascent, &font_descent,
52 &text_structure);
53 XGetWindowAttributes(display, frame_window, &window_attributes);
54 text_x = (window_attributes.width - text_structure.width)/2;
55 text_y = (window_attributes.height -
56 (text_structure.ascent+text_structure.descent))/2;
57 XDrawString(display, frame_window, graphical_context,
58 text_x, text_y, hello_string, hello_string_length);
59 break;
60 }
61 default:
62 break;
63 }
64 }
65 return(0);
66 }
This tutorial looks at writing a simple X-Windows application that displays the text 'Hello World' in the middle of the frame window. When the window is re-sized, the text is re-positioned accordingly.
Throughout this tutorial, I shall be making references to the equivalent Microsoft Windows API routines, for those of you who are more familiar with that environment.
The X-Windows environment
One of the major differences between the Microsoft Windows and X-Windows systems is that X-Windows is client/server based. This has a significant impact on networked machines, as the application may be running on the server, yet displaying windows on a client machine. Seen from the users perspective, the application looks like it is running locally, when in actual fact it isn't. The downside to this approach is that response times from client to server and back to client are limited to the bandwidth of the network connection.
Accordingly, there are some X-Windows calls that may seen a little strange, but I will discuss them as we progress through the tutorial.
[/php]
01 #include <stdio.h>
02 #include <stdlib.h>
03 #include <string.h>
04 #include <X11/Xlib.h>
05
06 int main (int argc, char *argv[])
07 {
08 Display *display;
09 Visual *visual;
10 int depth;
11 int text_x;
12 int text_y;
13 XSetWindowAttributes frame_attributes;
14 Window frame_window;
15 XFontStruct *fontinfo;
16 XGCValues gr_values;
17 GC graphical_context;
18 XKeyEvent event;
19 char hello_string[] = "Hello World";
20 int hello_string_length = strlen(hello_string);
21
22 display = XOpenDisplay(NULL);
23 visual = DefaultVisual(display, 0);
24 depth = DefaultDepth(display, 0);
25
26 frame_attributes.background_pixel = XWhitePixel(display, 0);
27 /* create the application window */
28 frame_window = XCreateWindow(display, XRootWindow(display, 0),
29 0, 0, 400, 400, 5, depth,
30 InputOutput, visual, CWBackPixel,
31 &frame_attributes);
32 XStoreName(display, frame_window, "Hello World Example");
33 XSelectInput(display, frame_window, ExposureMask | StructureNotifyMask);
34
35 fontinfo = XLoadQueryFont(display, "10x20");
36 gr_values.font = fontinfo->fid;
37 gr_values.foreground = XBlackPixel(display, 0);
38 graphical_context = XCreateGC(display, frame_window,
39 GCFont+GCForeground, &gr_values);
40 XMapWindow(display, frame_window);
41
42 while ( 1 ) {
43 XNextEvent(display, (XEvent *)&event);
44 switch ( event.type ) {
45 case Expose:
46 {
47 XWindowAttributes window_attributes;
48 int font_direction, font_ascent, font_descent;
49 XCharStruct text_structure;
50 XTextExtents(fontinfo, hello_string, hello_string_length,
51 &font_direction, &font_ascent, &font_descent,
52 &text_structure);
53 XGetWindowAttributes(display, frame_window, &window_attributes);
54 text_x = (window_attributes.width - text_structure.width)/2;
55 text_y = (window_attributes.height -
56 (text_structure.ascent+text_structure.descent))/2;
57 XDrawString(display, frame_window, graphical_context,
58 text_x, text_y, hello_string, hello_string_length);
59 break;
60 }
61 default:
62 break;
63 }
64 }
65 return(0);
66 }