Commit 59195b36 authored by Daniel Vogel's avatar Daniel Vogel
Browse files

new multiwindow demo, fixed bug in button

parent f829b022
...@@ -84,7 +84,7 @@ public: ...@@ -84,7 +84,7 @@ public:
// the CONTROLLER // the CONTROLLER
void mouseClick(int mx, int my) { void mouseClick(int mx, int my) {
float dist = sqrt(pow(mx - x, 2) + pow(my - y, 2)); float dist = sqrt(pow(mx - x, 2) + pow(my - y, 2));
if (dist < diameter) { if (dist < diameter / 2) {
toggle(); toggle();
} }
} }
......
/* /*
CS 349 Code Examples: X Windows and XLib CS 349 Code Examples: X Windows and XLib
multiwindow Opens and draws into two windows dispatch Opens and draws into two windows
usage: usage:
to create sibling window: to create sibling window:
./muliwindow sib ./muliwindow sib
to create child window: to create child window:
./multiwindow child ./multiwindow child
...@@ -23,6 +23,7 @@ See associated makefile for compiling instructions ...@@ -23,6 +23,7 @@ See associated makefile for compiling instructions
#include <list> #include <list>
#include <cstdlib> #include <cstdlib>
#include <unistd.h> #include <unistd.h>
#include <sstream>
/* /*
* Header files for X functions * Header files for X functions
...@@ -32,7 +33,7 @@ See associated makefile for compiling instructions ...@@ -32,7 +33,7 @@ See associated makefile for compiling instructions
using namespace std; using namespace std;
const int Border = 5; const int Border = 4;
/* /*
...@@ -42,87 +43,31 @@ struct XInfo { ...@@ -42,87 +43,31 @@ struct XInfo {
Display *display; Display *display;
int screen; int screen;
Window window; Window window;
GC gc[3]; GC gc;
}; };
/* /*
* Function to put out a message on error exits. * Function to put out a message on error exits.
*/ */
void error( string str ) { void error( string str ) {
cerr << str << endl; cerr << str << endl;
exit(0); exit(0);
}
void drawPointsInCorners(XInfo &xinfo) {
Display *display = xinfo.display;
Window win = xinfo.window;
GC gc = xinfo.gc[1];
XWindowAttributes windowInfo;
XGetWindowAttributes(display, win, &windowInfo);
unsigned int height = windowInfo.height;
unsigned int width = windowInfo.width;
/* draw one pixel near each corner of the window */
XDrawPoint(display, win, gc, 5, 5);
XDrawPoint(display, win, gc, 5, height-5);
XDrawPoint(display, win, gc, width-5, 5);
XDrawPoint(display, win, gc, width-5, height-5);
} }
void drawStuff(XInfo &xinfo, int gcIndex, int x, int y) {
Display *display = xinfo.display;
Window win = xinfo.window;
GC gc = xinfo.gc[gcIndex];
/* draw two intersecting lines, one horizontal and one vertical, */
/* which intersect at point "x,y". */
XDrawLine(display, win, gc, x, y-30, x, y+200);
XDrawLine(display, win, gc, x-30, y, x+200, y);
/* now use the XDrawArc() function to draw a circle whose diameter */
/* is 30 pixels, and whose center is at location 'x,y'. */
XDrawArc(display, win, gc, x-(30/2), y-(30/2), 30, 30, 0, 360*64);
{
XPoint points[] = {
{x+200, y+50},
{x+250, y+80},
{x+200, y+80},
{x+200, y+50}
};
int npoints = sizeof(points)/sizeof(XPoint);
/* draw a small triangle at the top-left corner of the window. */
/* the triangle is made of a set of consecutive lines, whose */
/* end-point pixels are specified in the 'points' array. */
XDrawLines(display, win, gc, points, npoints, CoordModeOrigin);
}
/* draw a rectangle whose top-left corner is at 'x+120,y+50', its width is */
/* 50 pixels, and height is 60 pixels. */
XDrawRectangle(display, win, gc, x+120, y+50, 50, 60);
/* draw a filled rectangle of the same size as above, to the left of the */
/* previous rectangle. */
XFillRectangle(display, win, gc, x+60, y+50, 50, 60);
}
/* /*
* Initialize X and create a window * Initialize X and create a window
*/ */
void initX(int argc, char *argv[], XInfo &xInfo, Window root, int x, int y, int width, int height) { void initX(int argc, char *argv[], XInfo &xinfo, Window root,
int x, int y, int width, int height, string name) {
XSizeHints hints; XSizeHints hints;
unsigned long white, black; unsigned long white, black;
white = XWhitePixel( xInfo.display, xInfo.screen ); white = XWhitePixel( xinfo.display, xinfo.screen );
black = XBlackPixel( xInfo.display, xInfo.screen ); black = XBlackPixel( xinfo.display, xinfo.screen );
hints.x = x; hints.x = x;
hints.y = y; hints.y = y;
...@@ -130,73 +75,58 @@ void initX(int argc, char *argv[], XInfo &xInfo, Window root, int x, int y, int ...@@ -130,73 +75,58 @@ void initX(int argc, char *argv[], XInfo &xInfo, Window root, int x, int y, int
hints.height = height; hints.height = height;
hints.flags = PPosition | PSize; hints.flags = PPosition | PSize;
xInfo.window = XCreateSimpleWindow( xinfo.window = XCreateSimpleWindow(
xInfo.display, // display where window appears xinfo.display, // display where window appears
root, // window's parent in window tree root, // window's parent in window tree
hints.x, hints.y, // upper left corner location hints.x, hints.y, // upper left corner location
hints.width, hints.height, // size of the window hints.width, hints.height, // size of the window
Border, // width of window's border Border, // width of window's border
black, // window border colour black, // window border colour
white ); // window background colour white ); // window background colour
XSetStandardProperties( XSetStandardProperties(
xInfo.display, // display containing the window xinfo.display, // display containing the window
xInfo.window, // window whose properties are set xinfo.window, // window whose properties are set
"x2_simpleDrawing", // window's title name.c_str(), // window's title
"SD", // icon's title "", // icon's title
None, // pixmap for the icon None, // pixmap for the icon
argv, argc, // applications command line args argv, argc, // applications command line args
&hints ); // size hints for the window &hints ); // size hints for the window
/* /*
* Create Graphics Contexts * Create Graphics Context
*/ */
int i = 0; xinfo.gc = XCreateGC(xinfo.display, xinfo.window, 0, 0);
xInfo.gc[i] = XCreateGC(xInfo.display, xInfo.window, 0, 0); XSetForeground(xinfo.display, xinfo.gc, BlackPixel(xinfo.display, xinfo.screen));
XSetForeground(xInfo.display, xInfo.gc[i], BlackPixel(xInfo.display, xInfo.screen)); XSetBackground(xinfo.display, xinfo.gc, WhitePixel(xinfo.display, xinfo.screen));
XSetBackground(xInfo.display, xInfo.gc[i], WhitePixel(xInfo.display, xInfo.screen)); XSetFillStyle(xinfo.display, xinfo.gc, FillSolid);
XSetFillStyle(xInfo.display, xInfo.gc[i], FillSolid); XSetLineAttributes(xinfo.display, xinfo.gc,
XSetLineAttributes(xInfo.display, xInfo.gc[i], 1, LineSolid, CapButt, JoinRound);
1, LineSolid, CapButt, JoinRound);
XSelectInput(xinfo.display, xinfo.window,
i = 1; ButtonPressMask | KeyPressMask | ExposureMask);
xInfo.gc[i] = XCreateGC(xInfo.display, xInfo.window, 0, 0);
XSetForeground(xInfo.display, xInfo.gc[i], BlackPixel(xInfo.display, xInfo.screen)); // load a larger font
XSetBackground(xInfo.display, xInfo.gc[i], WhitePixel(xInfo.display, xInfo.screen)); XFontStruct * font;
XSetFillStyle(xInfo.display, xInfo.gc[i], FillSolid); font = XLoadQueryFont (xinfo.display, "12x24");
XSetLineAttributes(xInfo.display, xInfo.gc[i], XSetFont (xinfo.display, xinfo.gc, font->fid);
7, LineSolid, CapRound, JoinMiter);
i = 2;
xInfo.gc[i] = XCreateGC(xInfo.display, xInfo.window, 0, 0);
XSetForeground(xInfo.display, xInfo.gc[i], BlackPixel(xInfo.display, xInfo.screen));
XSetBackground(xInfo.display, xInfo.gc[i], WhitePixel(xInfo.display, xInfo.screen));
XSetFillStyle(xInfo.display, xInfo.gc[i], FillSolid);
XSetLineAttributes(xInfo.display, xInfo.gc[i],
7, LineOnOffDash, CapButt, JoinBevel);
XSelectInput(xInfo.display, xInfo.window,
ButtonPressMask | KeyPressMask | ExposureMask);
/* /*
* Put the window on the screen. * Put the window on the screen.
*/ */
XMapRaised( xInfo.display, xInfo.window ); XMapRaised( xinfo.display, xinfo.window );
} }
void repaintWindow1(XInfo xinfo) {
drawPointsInCorners(xinfo); // convert to string
drawStuff(xinfo, 0, 30, 50); string toString(int i) {
drawStuff(xinfo, 1, 180, 200); stringstream ss;
drawStuff(xinfo, 2, 330, 350); ss << i;
return ss.str();
} }
void repaintWindow2(XInfo xinfo) {
drawStuff(xinfo, 0, 30, 50);
}
/* /*
* Start executing here. * Start executing here.
...@@ -205,22 +135,15 @@ void repaintWindow2(XInfo xinfo) { ...@@ -205,22 +135,15 @@ void repaintWindow2(XInfo xinfo) {
* Exit forcing window manager to clean up - cheesy, but easy. * Exit forcing window manager to clean up - cheesy, but easy.
*/ */
int main ( int argc, char *argv[] ) { int main ( int argc, char *argv[] ) {
XInfo xInfo1;
XInfo xInfo2;
bool isSibling = true;
if (argc != 2) {
error("Usage: ./multiwindow [sib|child]");
} else if (strcmp(argv[1], "sib") == 0) {
isSibling = true;
} else if (strcmp(argv[1], "child") == 0) {
isSibling = false;
} else {
error("Usage: ./multiwindow [sib|child]");
}
XInfo xinfo1;
XInfo xinfo2;
// true: window is like heavyweight widget embedded in window
// false: window is just another window
bool isSibling = false;
/* /*
* Display opening uses the DISPLAY environment variable. * Display opening uses the DISPLAY environment variable.
* It can go wrong if DISPLAY isn't set, or you don't have permission. * It can go wrong if DISPLAY isn't set, or you don't have permission.
*/ */
...@@ -229,59 +152,61 @@ int main ( int argc, char *argv[] ) { ...@@ -229,59 +152,61 @@ int main ( int argc, char *argv[] ) {
error( "Can't open display." ); error( "Can't open display." );
} }
/* /*
* Find out some things about the display you're using. * Find out some things about the display you're using.
*/ */
int screen = DefaultScreen( display ); int screen = DefaultScreen( display );
xInfo1.display = display; xinfo1.display = display;
xInfo1.screen = screen; xinfo1.screen = screen;
// create window 1 // create window 1
initX(argc, argv, xInfo1, DefaultRootWindow( display ), 100, 100, 800, 600); initX(argc, argv, xinfo1, DefaultRootWindow(display), 75, 75, 200, 200, "One");
// create window 2 // create window 2
xInfo2.display = display; xinfo2.display = display;
xInfo2.screen = screen; xinfo2.screen = screen;
if (isSibling) if (isSibling)
// make window 2 a sibling of window 1 // make window 2 a sibling of window 1
initX(argc, argv, xInfo2, DefaultRootWindow( display ), 50, 50, 250, 125); initX(argc, argv, xinfo2, DefaultRootWindow( display ), 25, 150, 150, 50, "Two");
else else
// make window 2 a child of window 1 // make window 2 a child of window 1
initX(argc, argv, xInfo2, xInfo1.window, 50, 50, 250, 125); initX(argc, argv, xinfo2, xinfo1.window, 25, 50, 150, 50, "Two");
XFlush(display); XFlush(display);
sleep(1); // let server get set up before sending drawing commands
// need to repaint both windows // need to repaint both windows
repaintWindow1(xInfo1);
repaintWindow2(xInfo2); int click1 = 0;
int click2 = 0;
XEvent event; XEvent event;
while( true ) { while ( true ) {
XNextEvent( display, &event ); XNextEvent( display, &event );
switch( event.type ) { switch ( event.type ) {
case ButtonPress: case ButtonPress:
if (event.xany.window == xInfo1.window) if (event.xany.window == xinfo1.window) {
cout << "Got button press in window 1!\n"; cout << "ButtonPress Window One\n";
else if (event.xany.window == xInfo2.window) click1++;
cout << "Got button press in window 2!\n"; } else if (event.xany.window == xinfo2.window) {
break; cout << "ButtonPress Window Two\n";
case KeyPress: click2++;
if (event.xany.window == xInfo1.window) }
cout << "Got key press in window 1!\n"; break;
else if (event.xany.window == xInfo2.window)
cout << "Got key press in window 2!\n";
break;
case Expose:
// only repaint window which sent the expose
if (event.xany.window == xInfo1.window)
repaintWindow1(xInfo1);
else if (event.xany.window == xInfo2.window)
repaintWindow2(xInfo2);
break;
} }
// draw in the windows
string s;
s = toString(click1);
XDrawImageString( xinfo1.display, xinfo1.window, xinfo1.gc,
25, 40, s.c_str(), s.length() );
s = toString(click2);
XDrawImageString( xinfo2.display, xinfo2.window, xinfo2.gc,
25, 40, s.c_str(), s.length() );
} }
/* flush all pending requests to the X server. */ /* flush all pending requests to the X server. */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment