DragW.c

// NewDragWindow, Copyright © 1990 Ricardo Batista
// This code gets installed during system startup by an INIT, the purpose is to
// replace the old DragWindow routine with our code so that a window is moved
// along with it's contents as supposed to moving around an outline only.
//
// This code is for MPW C 3.1, when compiled into a resource the SysHeap and
// Locked bits need to be set in the resource attributes.
//
// After compiling copy this resource into a file that includes DragW INIT
//
 
#include "Types.h"
#include "Windows.h"
 
// we accept the same parameters as DragWindow, called by all Mac applications
 
pascal void NewDragWindow(WindowPtr w, Point pt, Rect *bounds)
{
    Point OldPoint, NewPoint, OffsetPoint;
    short h, v;
    GrafPtr savePort;
    
    GetPort(&savePort);                             // save the current port
    SetPort(w);                                     // set coordinate system to window
    OldPoint = NewPoint = pt;                       // make local copies of current mouse
    OffsetPoint.h = w->portRect.left;               // stuff top-left of window in point
    OffsetPoint.v = w->portRect.top;
    LocalToGlobal(&OffsetPoint);                    // convert too global coordinates
    OffsetPoint.h = pt.h - OffsetPoint.h;           // this offset is from window to mouse
    OffsetPoint.v = pt.v - OffsetPoint.v;
    while (StillDown()) {                           // is mouse still down ?
        GetMouse(&NewPoint);
        LocalToGlobal(&NewPoint);                   // whe now ?
        if (PtInRect(NewPoint, bounds)) {           // respect application boundaries
            if ((NewPoint.h != OldPoint.h) || (NewPoint.v != OldPoint.v)) {
                h = NewPoint.h - OffsetPoint.h;     // calculate new position of window
                v = NewPoint.v - OffsetPoint.v;     // using the offset of mouse-window
                MoveWindow(w, h, v, false);         // just do it
                OldPoint = NewPoint;                // save new 'old' location
            }
        }
    }
    SetPort(savePort);                              // restore coordinate system
}