How to simulate window dragging? - Xwindows

This is a discussion on How to simulate window dragging? - Xwindows ; I am trying to write a program that will allow the user to drag a window from any area in that window besides just the title bar. I have not been successful and am experiencing weird behaviour that I do ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: How to simulate window dragging?

  1. How to simulate window dragging?

    I am trying to write a program that will allow the user to drag a
    window from any area in that window besides just the title bar.

    I have not been successful and am experiencing weird behaviour that I
    do not understand. Despite how I move the mouse, the window insists
    on travelling downward toward the bottom of the screen.

    Here is the code relevant to the drag implementation:

    ....

    ::XSelectInput(g_pDisplay, g_window, ExposureMask | ButtonPressMask |
    Button1MotionMask | StructureNotifyMask);

    ....

    XEvent event;
    ::XNextEvent(g_pDisplay, &event);

    static int nMouseX;
    static int nMouseY;

    switch(event.type){
    case ButtonPress:
    if(Button1 == event.xbutton.button){
    nMouseX = event.xbutton.x_root;
    nMouseY = event.xbutton.y_root;
    }
    break;

    case MotionNotify:
    {
    int nDragX = event.xmotion.x_root;
    int nDragY = event.xmotion.y_root;

    int nXChange = nDragX - nMouseX;
    int nYChange = nDragY - nMouseY;

    nMouseX = nDragX;
    nMouseY = nDragY;

    XWindowAttributes attributes;
    ::XGetWindowAttributes(g_pDisplay, g_window, &attributes);

    Window root = RootWindow(g_pDisplay, g_nScreen);
    int x, y;
    Window child;
    ::XTranslateCoordinates(g_pDisplay, g_window, root, attributes.x,
    attributes.y, &x, &y, &child);

    using std::clog;
    clog << "Moving window from " << x << ", " << y << " to ";
    x += nXChange;
    y += nYChange;

    clog << x << ", " << y << ".\n";

    ::XMoveWindow(g_pDisplay, g_window, x , y);

    ::XGetWindowAttributes(g_pDisplay, g_window, &attributes);
    ::XTranslateCoordinates(g_pDisplay, g_window, root,
    attributes.x,
    attributes.y, &x, &y, &child);
    clog << "Window moved to " << x << ", " << y << ".\n";

    break;
    }

    This is the kind of output I get:

    Mouse moved from 347, 156 to 348, 156.
    Moving window from 9, 60 to 10, 60.
    Window moved to 16, 100.
    Mouse moved from 348, 156 to 349, 157.
    Moving window from 16, 100 to 17, 101.
    Window moved to 23, 141.
    Mouse moved from 349, 157 to 350, 157.
    Moving window from 23, 141 to 24, 141.
    Window moved to 30, 181.
    Mouse moved from 350, 157 to 351, 157.
    Moving window from 30, 181 to 31, 181.
    Window moved to 37, 221.
    Mouse moved from 351, 157 to 352, 158.
    Moving window from 37, 221 to 38, 222.
    Window moved to 44, 262.

    Why is this dragging code behaving in this manner?

  2. Re: How to simulate window dragging?

    Tron Thomas wrote:

    > I am trying to write a program that will allow the user to drag a
    > window from any area in that window besides just the title bar.
    >
    > I have not been successful and am experiencing weird behaviour that I
    > do not understand. Despite how I move the mouse, the window insists
    > on travelling downward toward the bottom of the screen.
    >
    > Here is the code relevant to the drag implementation:

    [...]
    > Mouse moved from 351, 157 to 352, 158.
    > Moving window from 37, 221 to 38, 222.
    > Window moved to 44, 262.
    >
    > Why is this dragging code behaving in this manner?


    You toplevel window is managed by the window manager. If it has the
    default window gravity 'NorthWestGravity', the WM will place the upper
    left corner of the window decoration (usually the titlebar) at the
    coordinates you specify by XMoveWindow(). You may change the window
    gravity to 'StaticGravity' (and hope you WM will honour this) or set the
    'override_redirect' attribute (but then your window has no titlebar).

    Regards,
    Daniel

  3. Re: How to simulate window dragging?

    Tron Thomas wrote:
    >I am trying to write a program that will allow the user to drag a
    >window from any area in that window besides just the title bar.
    >
    >I have not been successful and am experiencing weird behaviour that I
    >do not understand. Despite how I move the mouse, the window insists
    >on travelling downward toward the bottom of the screen.
    >
    >Here is the code relevant to the drag implementation:
    >
    >...
    >
    >::XSelectInput(g_pDisplay, g_window, ExposureMask | ButtonPressMask |
    > Button1MotionMask | StructureNotifyMask);
    >
    >...
    >
    >XEvent event;
    >::XNextEvent(g_pDisplay, &event);
    >
    >static int nMouseX;
    >static int nMouseY;
    >
    >switch(event.type){
    > case ButtonPress:
    > if(Button1 == event.xbutton.button){
    > nMouseX = event.xbutton.x_root;
    > nMouseY = event.xbutton.y_root;
    > }
    > break;
    >
    > case MotionNotify:
    > {
    > int nDragX = event.xmotion.x_root;
    > int nDragY = event.xmotion.y_root;
    >
    > int nXChange = nDragX - nMouseX;
    > int nYChange = nDragY - nMouseY;
    >
    > nMouseX = nDragX;
    > nMouseY = nDragY;
    >
    > XWindowAttributes attributes;
    > ::XGetWindowAttributes(g_pDisplay, g_window, &attributes);
    >
    > Window root = RootWindow(g_pDisplay, g_nScreen);
    > int x, y;
    > Window child;
    > ::XTranslateCoordinates(g_pDisplay, g_window, root, attributes.x,
    > attributes.y, &x, &y, &child);


    I guess this should be something like

    ::XTranslateCoordinates(g_pDisplay, g_window, root,
    -attributes.border_width,
    -attributes.border_width, &x, &y, &child);

    >
    > using std::clog;
    > clog << "Moving window from " << x << ", " << y << " to ";
    > x += nXChange;
    > y += nYChange;
    >
    > clog << x << ", " << y << ".\n";
    >
    > ::XMoveWindow(g_pDisplay, g_window, x , y);

    [snip]


+ Reply to Thread