Advertisement
Guest User

drag

a guest
Apr 3rd, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <X11/X.h>
  3. #include <X11/Xlib.h>
  4.  
  5. int main ()
  6. {
  7.   Display* d = XOpenDisplay(0);
  8.   Window w = XCreateSimpleWindow (d,  RootWindow(d, 0), 300, 300, 300, 300, 0,
  9.       BlackPixel (d, 0), BlackPixel(d, 0));
  10.   XMapWindow(d, w);
  11.   XMoveResizeWindow(d, w, 300, 300, 300, 300);
  12.  
  13.   XSelectInput (d, w, ButtonPressMask|ButtonReleaseMask|PointerMotionMask);
  14.  
  15.   printf ("Display %x Window %x\n", d, (void*)w);
  16.  
  17.   int dragging=0;
  18.   int ptr_x, ptr_y;
  19.   int win_x = 300, win_y = 300;
  20.   int win_h = 300, win_w = 300;
  21.   int win_new_x, win_new_y;
  22.   int win_new_h, win_new_w;
  23.   int ptr_new_x, ptr_new_y;
  24.   XWindowAttributes attr;
  25.   Window child;
  26.  
  27.   while (1)
  28.   {
  29.     XEvent xev;
  30.     XNextEvent(d, &xev);
  31.     switch (xev.type)
  32.     {
  33.       case ButtonPress:
  34.         printf ("Dragging!\n");
  35.         dragging = 1;
  36.         ptr_x = xev.xbutton.x;
  37.         ptr_y = xev.xbutton.y;
  38.  
  39.         XGetWindowAttributes (d, w, &attr);
  40.         XTranslateCoordinates(d, w, RootWindow(d, 0), 0, 0, &win_x, &win_y, &child);
  41.         XTranslateCoordinates(d, w, RootWindow(d, 0), xev.xbutton.x, xev.xbutton.y, &ptr_x, &ptr_y, &child);
  42.         win_w = attr.width;
  43.         win_h = attr.height;
  44.  
  45.         printf ("Starting with x=%d y=%d w=%d h=%d\n", win_x, win_y, win_w, win_h);
  46.  
  47.         break;
  48.       case ButtonRelease:
  49.         printf ("Not Dragging!\n");
  50.         dragging = 0;
  51.         break;
  52.       case MotionNotify:
  53.         if (dragging)
  54.         {
  55.           int dx, dy;
  56.           XTranslateCoordinates(d, w, RootWindow(d, 0), xev.xmotion.x, xev.xmotion.y, &ptr_new_x, &ptr_new_y, &child);
  57.           dx = ptr_new_x - ptr_x;
  58.           dy = ptr_new_y - ptr_y;
  59.           printf ("dx = %d dy = %d\n", dx, dy);
  60.           win_new_h = win_h - dy;
  61.           win_new_w = win_w - dx;
  62.           if (win_new_h < 100) win_new_h = 100;
  63.           if (win_new_w < 100) win_new_w = 100;
  64.           win_new_y = win_y + (win_h - win_new_h);
  65.           win_new_x = win_x + (win_w - win_new_w);
  66.  
  67.           XMoveResizeWindow(d, w, win_new_x, win_new_y, win_new_w, win_new_h);
  68.         }
  69.     }
  70.   }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement