LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   X programming in Ubuntu (http://www.linuxquestions.org/questions/programming-9/x-programming-in-ubuntu-742489/)

toplotna 07-24-2009 11:26 AM

X programming in Ubuntu
 
I tried the attached program to draw lines and rectangles in Ubuntu 8.04
but it won't. I get only the window. Can somebody please explain what is
missing?

/*Program to draw lines and rectangles--draw.c
*On Linux compile as
gcc -o draw draw.c -L/usr/X11R6/lib -lX11
*/


#include<X11/Xlib.h>
#include<X11/Xutil.h>
#include<stdio.h>
#define BORDER_WIDTH 2


/* Program wide globals */
Display *theDisplay;
int theScreen;
int theDepth;
unsigned long theBlackPixel;
unsigned long theWhitePixel;


void initX(void){
}


void drawLine(Window theWindow,GC theGC,int x1,int y1,int x2,int y2){
XDrawLine(theDisplay,theWindow,theGC,x1,y1,x2,y2);


}


void drawRectangle(Window theWindow,GC theGC,int x,int y,int width,int height){
XDrawRectangle(theDisplay,theWindow,theGC,x,y,width,height);
}


int createGC(Window theNewWindow,GC *theNewGC){
XGCValues theGCValues;


*theNewGC = XCreateGC(theDisplay,theNewWindow,(unsigned long) 0,&theGCValues);
if(*theNewGC == 0)
return 0;
else{
XSetForeground(theDisplay,*theNewGC,theWhitePixel);
XSetBackground(theDisplay,*theNewGC,theBlackPixel);
return 1;
}
}


Window OpenWindow(int x, int y, int width, int height, int flag,GC *theNewGC){
XSetWindowAttributes theWindowAttributes;
unsigned long theWindowMask;
XSizeHints theSizeHints;
XWMHints theWMHints;
Window theNewWindow;


/*Setting the attributes*/
theWindowAttributes.border_pixel
=BlackPixel(theDisplay,theScreen);
theWindowAttributes.background_pixel
= WhitePixel(theDisplay,theScreen);
theWindowAttributes.override_redirect = False;



theWindowMask = CWBackPixel|CWBorderPixel|CWOverrideRedirect;


theNewWindow = XCreateWindow( theDisplay,
RootWindow(theDisplay,theScreen),
x,y,width,height,
BORDER_WIDTH,theDepth,
InputOutput,
CopyFromParent,
theWindowMask,
&theWindowAttributes);


theWMHints.initial_state = NormalState;
theWMHints.flags = StateHint;


XSetWMHints(theDisplay,theNewWindow,&theWMHints);

theSizeHints.flags = PPosition | PSize;
theSizeHints.x = x;
theSizeHints.y = y;
theSizeHints.width = width;
theSizeHints.height = height;


XSetNormalHints(theDisplay,theNewWindow,&theSizeHints);


if( createGC(theNewWindow,theNewGC) == 0){
XDestroyWindow(theDisplay,theNewWindow);
return( (Window) 0);
}


XMapWindow(theDisplay,theNewWindow);
XFlush(theDisplay);


return theNewWindow;
}


int main(void){
Window theWindow;
GC theGC;

theDisplay = XOpenDisplay(NULL);

theScreen = DefaultScreen(theDisplay);
theDepth = DefaultDepth(theDisplay,theScreen);
theBlackPixel = WhitePixel(theDisplay,theScreen);
theWhitePixel = BlackPixel(theDisplay,theScreen);

theWindow = OpenWindow(100,100,600,600,0,&theGC);
drawLine(theWindow,theGC,10,10,100,100);
drawRectangle(theWindow,theGC,100,100,100,100);
XFlush(theDisplay);

sleep(7);
XDestroyWindow(theDisplay,theWindow);
}

fantas 07-24-2009 12:36 PM

So you're seeing a white empty window right now, if I got this right ?

In that case it's likely that you need to set the "pen" to a different colour than white, as it is right now (using 'XSetForeground').

toplotna 07-24-2009 05:47 PM

Quote:

Originally Posted by fantas (Post 3619067)
So you're seeing a white empty window right now, if I got this right ?

In that case it's likely that you need to set the "pen" to a different colour than white, as it is right now (using 'XSetForeground').

Yes, I see only white empty window.

Thank you for hint. I did the following:

.....
/*Setting the attributes*/
theWindowAttributes.border_pixel
=BlackPixel(theDisplay,theScreen);
theWindowAttributes.background_pixel
= BlackPixel(theDisplay,theScreen);
theWindowAttributes.override_redirect = True;

instead of

/*Setting the attributes*/
theWindowAttributes.border_pixel
=BlackPixel(theDisplay,theScreen);
theWindowAttributes.background_pixel
= WhitePixel(theDisplay,theScreen);
theWindowAttributes.override_redirect = False;

Now, I get black window with white rectangle and white line.


All times are GMT -5. The time now is 04:29 AM.