Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Tests / X11 / HelloWorldX11.cxx
blob5bbc19a4aa3a61d495ef0ac67d13c02449df6f71
2 /*** START MAIN.H ***/
3 /* http://www.geocities.com/jeff_louie/x11/helloworld.htm* */
4 /*
5 * main.h
6 * TestX
8 * Created by Jeff Louie on Tue Feb 03 2004.
9 * Copyright (c) 2004 __MyCompanyName__. All rights reserved.
14 #ifndef MAIN_H
15 #define MAIN_H 1
17 #include <iostream>
19 /* include the X library headers */
20 #include <X11/Xlib.h>
21 #include <X11/Xutil.h>
22 #include <X11/Xos.h>
24 class Main {
26 public:
27 // constructor
28 Main(int argc, char * const argv[]);
29 //virtual ~Main();
32 private:
35 /* here are our X variables */
36 Display *dis;
37 int screen;
38 Window win;
39 GC gc;
41 /* here are our X routines declared! */
42 void init_x();
43 void close_x();
44 void redraw();
45 int delay(int i);
49 #endif
51 /*** END MAIN.H ***/
53 /*** START MAIN.CPP ***/
55 // modified from Brian Hammond's Howdy program at
56 // http://www.insanityengine.com/doc/x11/xintro.html
57 // jeff louie 02.05.2004
61 int main (int argc, char * const argv[]) {
62 Main m(argc, argv);
63 return 0;
66 //Main::~Main() {;};
67 Main::Main (int argc, char * const argv[]) {
68 XEvent event; // XEvent declaration
69 KeySym key; // KeyPress Events
70 char text[255]; // char buffer for KeyPress Events
72 init_x();
74 // event loop
75 while(1) {
76 // get the next event and stuff it into our event variable.
77 // Note: only events we set the mask for are detected!
78 XNextEvent(dis, &event);
81 switch (event.type) {
82 int x;
83 int y;
84 case Expose:
85 if (event.xexpose.count==0) {
86 redraw();
88 break;
89 case KeyPress:
90 if (XLookupString(&event.xkey,text,255,&key,0)==1) {
91 // use the XLookupString routine to convert the invent
92 // KeyPress data into regular text. Weird but necessary...
93 if ((text[0]=='q') || (text[0]=='Q')) {
94 close_x();
96 else {
97 // echo key press
98 printf("You pressed the %c key!\n",text[0]);
101 break;
102 case ButtonPress:
103 // get cursor position
104 x= event.xbutton.x;
105 y= event.xbutton.y;
106 strcpy(text,"X is FUN!");
107 XSetForeground(dis,gc,rand()%event.xbutton.x%255);
108 // draw text at cursor
109 XDrawString(dis,win,gc,x,y, text, strlen(text));
110 break;
111 default:
112 printf("Unhandled event.\n");
117 void Main::init_x() {
118 unsigned long black,white;
120 dis=XOpenDisplay(NULL);
121 screen=DefaultScreen(dis);
122 black=BlackPixel(dis,screen),
123 white=WhitePixel(dis, screen);
124 win=XCreateSimpleWindow(dis,DefaultRootWindow(dis),0,0,
125 300, 300, 5,black, white);
126 XSetStandardProperties(dis,win,"Hello World","Hi",None,NULL,0,NULL);
127 XSelectInput(dis, win, ExposureMask|ButtonPressMask|KeyPressMask);
128 // get Graphics Context
129 gc=XCreateGC(dis, win, 0,0);
130 XSetBackground(dis,gc,white);
131 XSetForeground(dis,gc,black);
132 XClearWindow(dis, win);
133 XMapRaised(dis, win);
136 void Main::close_x() {
137 XFreeGC(dis, gc);
138 XDestroyWindow(dis,win);
139 XCloseDisplay(dis);
140 exit(1);
143 void Main::redraw() {
144 XClearWindow(dis, win);