LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-24-2009, 10:26 AM   #1
toplotna
LQ Newbie
 
Registered: Dec 2006
Posts: 4

Rep: Reputation: 0
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);
}
 
Old 07-24-2009, 11:36 AM   #2
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
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').
 
Old 07-24-2009, 04:47 PM   #3
toplotna
LQ Newbie
 
Registered: Dec 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by fantas View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Flash programming in Ubuntu. Unr3a164 Ubuntu 2 10-28-2008 05:15 PM
PHP programming in ubuntu 8.04 umwai Linux - Newbie 2 09-16-2008 10:37 AM
Ubuntu 7.04 or fedora 8 for programming??? sweetynikku Linux - Newbie 16 05-14-2008 06:18 PM
Xwindows programming in Ubuntu 7.04 matrix13 Linux - Software 2 03-15-2008 07:44 AM
C programming on Ubuntu kenneho Ubuntu 13 11-08-2005 03:04 AM


All times are GMT -5. The time now is 12:07 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration