LinuxQuestions.org
Review your favorite Linux distribution.
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
 
LinkBack Search this Thread
Old 07-18-2005, 06:27 AM   #1
pihu
LQ Newbie
 
Registered: Jun 2005
Posts: 11

Rep: Reputation: 0
Xlib Code --- Why not Drawing Rectangle??????


Hi,

I have pasted my code below....I want to draw rectangle in any selected window including desktop(whichever window is selected) in the motion event of my pointer but its not drawing . Can anybody tell me what can be the problem???its showing me the pointer motion but no drawing.....

Please tell me what could be the reason????

regards,
Pihu......

/***********************************************************/


main(int argc, char **argv)
{
register int i;
int tree = 0, stats = 0, bits = 0, events = 0, wm = 0, size = 0, shape = 0;
int frame = 0, children = 0;
Window window;

window = 0;
char *displayname = NULL;

dpy = XOpenDisplay(0);
screen = DefaultScreen(dpy);
/* If no window selected on command line, let user pick one the hard way */
if (!window)
{
printf("\n");
printf("xwininfo: Please select the window about which you\n");
printf(" would like information by clicking the\n");
printf(" mouse in that window.\n");
window = Select_Window(dpy);
printf("The target Window address is 0x%x \n",window);
}

/*
* Routine to let user select a window using the mouse
*/

Window Select_Window(dpy)
Display *dpy;
{
int status;
Cursor cursor;
XEvent event;
Window target_win = None, root = RootWindow(dpy,screen),child;
GC gc; // handle of newly created GC
XGCValues values; // initial values for the GC

unsigned long valuemask = 0; // which values in 'values' to /
// check when creating the GC. /

int buttons = 0;
XPoint p1,p2;
int index = 1;
int pos_x, pos_y;
int prev_x, prev_y;
int flag1 = 0;
int flag = 0;
int x = 0,y = 0;
int root_x, root_y;
unsigned int keys_buttons;


/* Make the target cursor */
cursor = XCreateFontCursor(dpy, XC_crosshair);

/* Grab the pointer using target cursor, letting it room all over */
status = XGrabPointer(dpy, root, False,ButtonPressMask|ButtonReleaseMask|PointerMotionMask|ButtonMotionMask, GrabModeSync,
GrabModeAsync, root, cursor, CurrentTime);

if (status != GrabSuccess) Fatal_Error("Can't grab the mouse.");
gc = XCreateGC(dpy, root, valuemask , &values);
if (gc < 0)
{
fprintf(stderr, "XCreateGC: \n");
}
/* Let the user select a window... */
while ((target_win == None) || (buttons != 0))
{
/* allow one more event */

XAllowEvents(dpy, SyncPointer, CurrentTime);
XWindowEvent(dpy, root, ButtonPressMask|ButtonReleaseMask|ButtonMotionMask|PointerMotionMask, &event);
switch (event.type)
{
case ButtonPress:
if (target_win == None)
{
target_win = event.xbutton.subwindow; /* window selected */
p1.x = event.xbutton.x;
p1.y = event.xbutton.y;
printf("BUTTON PRESSED \n");

if (target_win == None) target_win = root;

if (target_win )
{

int dummyi;
unsigned int dummy;

if (XGetGeometry (dpy, target_win, &root, &dummyi, &dummyi,
&dummy, &dummy, &dummy, &dummy) &&
target_win != root)
target_win = XmuClientWindow (dpy, target_win);
}
printf("The target Window address is 0x%x with in BUTTON PRESS after getting XmuClientWindow\n",target_win);
}
buttons++;
break;
case ButtonRelease:
index++;
p2.x = event.xbutton.x;
p2.y = event.xbutton.y;
flag1 = 1;
printf("BUTTON RELEASED\n");
printf("top left coordinates are X : %d Y : %d\n",p1.x,p1.y);
printf("top right coordinates are X : %d Y : %d \n",p2.x,p1.y);
printf("bottom left coordinates are X : %d Y : %d \n",p1.x,p2.y);
printf("bottom right coordinates are X : %d Y : %d \n",p2.x,p2.y);
if (buttons > 0) /* there may have been some down before we started */
buttons--;
break;
case MotionNotify:
printf("Mouse POINTER Motion\n");
while (XCheckMaskEvent(dpy,
PointerMotionHintMask | ButtonMotionMask, &event));
if (!XQueryPointer(dpy, event.xmotion.window,
&root, &child, &root_x, &root_y,
&pos_x, &pos_y, &keys_buttons))
{

//printf("After motion notify \n");
// printf("motion %d %d \n",pos_x,pos_y);

}
if (flag)
{
XClearWindow(dpy, target_win);
if(pos_y < p1.y && pos_x > p1.x) // fourth quadrant
{
XDrawRectangle(dpy, target_win, gc, p1.x-1 ,pos_y+1 ,p1.x +1 > pos_x ? p1.x - pos_x+1: pos_x -(p1.x+1),p1.y +1 > pos_y ? (p1.y + 1) - pos_y: pos_y -(p1.y +1) );
}
else if(pos_y < p1.y && pos_x < p1.x)//Third quadrant
{
XDrawRectangle(dpy, target_win, gc, pos_x-1 ,pos_y-1 ,p1.x +1 > pos_x ? p1.x - pos_x+1: pos_x -(p1.x+1),p1.y +1
> pos_y ? (p1.y + 1) - pos_y: pos_y -(p1.y +1) );
}
else if(pos_y > p1.y && pos_x < p1.x) //Second quadrant
{
XDrawRectangle(dpy, target_win, gc, pos_x-1 ,p1.y+1 ,p1.x +1 > pos_x ? p1.x - pos_x+1: pos_x -(p1.x+1),p1.y +1 > pos_y ? (p1.y + 1) - pos_y: pos_y -(p1.y +1) );
}
else if(pos_y > p1.y && pos_x > p1.x)//first quadrant
{
XDrawRectangle(dpy, target_win, gc, p1.x +1,p1.y +1,p1.x +1 > pos_x ? p1.x - pos_x+1: pos_x -(p1.x+1),p1.y +1 > pos_y ? (p1.y + 1) - pos_y: pos_y -(p1.y +1) );
}
}
break;

}
}
XUngrabPointer(dpy, CurrentTime); /* Done with pointer */
return(target_win);
}
 
Old 07-18-2005, 01:05 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OEL
Posts: 9,165

Rep: Reputation: 243Reputation: 243Reputation: 243
There are at least two reasons explaining why your program doesn't draw rectangles.

- it doesn't compile, the first part is missing (includes), the main function doesn't ends and the Fatal_error code is missing.
- your are drawing rectangles only if flag is non zero, but flag is initially set to zero and never changes ...

Please repost a full code that compiles, and don't forget to enclose it with code tags.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Xlib: connection to ":0.0" refused by server Xlib: No protocol specified eyalkz Linux - Newbie 9 04-18-2012 03:41 PM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
Recommend me a sprite rectangle clearing algorithm MadCactus Programming 2 10-09-2004 01:11 PM
java, rectangle size issues. exodist Programming 1 03-13-2004 04:48 PM
Xlib: connection to ":0.0" refused by server Xlib: No protocol specified eyalkz Programming 1 03-02-2004 08:22 AM


All times are GMT -5. The time now is 02:28 AM.

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
Open Source Consulting | Domain Registration