| 1 | /* |
| 2 | Copyright (C) 1996 Scott W. Sadler |
| 3 | All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | XsResizeOutline.C |
| 8 | |
| 9 | History |
| 10 | 03-Mar-96 1.0; Scott W. Sadler (ssadler@cisco.com) |
| 11 | Created |
| 12 | */ |
| 13 | |
| 14 | // Includes |
| 15 | |
| 16 | #include <assert.h> |
| 17 | #include <X11/cursorfont.h> |
| 18 | #include "XsResizeOutline.h" |
| 19 | |
| 20 | // Static definitions |
| 21 | |
| 22 | Cursor XsResizeOutline::_cursors[XsResizeOutline::NumCursors]; |
| 23 | int XsResizeOutline::_mutex = 0; |
| 24 | |
| 25 | // Constructor |
| 26 | |
| 27 | XsResizeOutline::XsResizeOutline (Widget w, int direction) : XsOutline (w) |
| 28 | { |
| 29 | |
| 30 | // Initialize |
| 31 | |
| 32 | _minWidth = 5; |
| 33 | _minHeight = 5; |
| 34 | _direction = direction; |
| 35 | |
| 36 | // Translate current window location to root coordinates |
| 37 | |
| 38 | Position xpos, ypos; |
| 39 | |
| 40 | XtTranslateCoords (XtParent (_w), (Position)_x, (Position)_y, &xpos, &ypos); |
| 41 | |
| 42 | _rootX = (int)xpos; |
| 43 | _rootY = (int)ypos; |
| 44 | |
| 45 | // Save the original window position and size |
| 46 | |
| 47 | _origX = _x; |
| 48 | _origY = _y; |
| 49 | _origW = _width; |
| 50 | _origH = _height; |
| 51 | |
| 52 | _origRootX = _rootX; |
| 53 | _origRootY = _rootY; |
| 54 | |
| 55 | // Create the cursors (if necessary) |
| 56 | |
| 57 | if (_mutex == 0) |
| 58 | { |
| 59 | Display *dpy = XtDisplay (_w); |
| 60 | |
| 61 | // Create cursors |
| 62 | |
| 63 | _cursors[TopCursor] = XCreateFontCursor (dpy, XC_top_side); |
| 64 | _cursors[BottomCursor] = XCreateFontCursor (dpy, XC_bottom_side); |
| 65 | _cursors[LeftCursor] = XCreateFontCursor (dpy, XC_left_side); |
| 66 | _cursors[RightCursor] = XCreateFontCursor (dpy, XC_right_side); |
| 67 | _cursors[TopLeftCursor] = XCreateFontCursor (dpy, XC_top_left_corner); |
| 68 | _cursors[TopRightCursor] = XCreateFontCursor (dpy, XC_top_right_corner); |
| 69 | _cursors[BottomLeftCursor] = XCreateFontCursor (dpy, XC_bottom_left_corner); |
| 70 | _cursors[BottomRightCursor] = XCreateFontCursor (dpy, XC_bottom_right_corner); |
| 71 | _cursors[Fleur] = XCreateFontCursor (dpy, XC_fleur); |
| 72 | _mutex = 1; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Destructor |
| 77 | |
| 78 | XsResizeOutline::~XsResizeOutline ( ) |
| 79 | { |
| 80 | // Empty |
| 81 | } |
| 82 | |
| 83 | // setMinSize |
| 84 | |
| 85 | void XsResizeOutline::setMinSize (int minWidth, int minHeight) |
| 86 | { |
| 87 | assert (minWidth > 0); |
| 88 | assert (minHeight > 0); |
| 89 | |
| 90 | _minWidth = minWidth; |
| 91 | _minHeight = minHeight; |
| 92 | } |
| 93 | |
| 94 | // _motionHandler |
| 95 | |
| 96 | void XsResizeOutline::_motionHandler (XEvent *event) |
| 97 | { |
| 98 | int curX, curY; |
| 99 | int diff; |
| 100 | int x, y, w, h; |
| 101 | |
| 102 | // Get current mouse position |
| 103 | |
| 104 | curX = event->xbutton.x_root; |
| 105 | curY = event->xbutton.y_root; |
| 106 | |
| 107 | if (_direction & Undetermined) |
| 108 | { |
| 109 | |
| 110 | /* |
| 111 | Just let the mouse roam around until it crosses the outline. |
| 112 | When it does so, lock in the direction, change the cursor, and let |
| 113 | it rip. |
| 114 | */ |
| 115 | |
| 116 | if (((curX <= _origRootX) || (curX >= (_origRootX + _origW))) |
| 117 | || ((curY <= _origRootY) || (curY >= (_origRootY + _origH)))) |
| 118 | { |
| 119 | |
| 120 | const int CornerOffset = 30; |
| 121 | |
| 122 | // Crossed box. Figure out where and set direction |
| 123 | |
| 124 | _direction = 0; |
| 125 | |
| 126 | if (curY <= (_origRootY + CornerOffset)) |
| 127 | _direction |= Up; |
| 128 | else if (curY >= (_origRootY + _origH - CornerOffset)) |
| 129 | _direction |= Down; |
| 130 | |
| 131 | if (curX <= (_origRootX + CornerOffset)) |
| 132 | _direction |= Left; |
| 133 | else if (curX >= (_origRootX + _origW - CornerOffset)) |
| 134 | _direction |= Right; |
| 135 | |
| 136 | // Get the new cursor |
| 137 | |
| 138 | XChangeActivePointerGrab (XtDisplay (_w), GrabEventMask, |
| 139 | _getCursor ( ), CurrentTime); |
| 140 | } |
| 141 | else |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | // Get the current values |
| 146 | |
| 147 | x = _x; y = _y; w = _width; h = _height; |
| 148 | |
| 149 | // Process the motion |
| 150 | |
| 151 | if (_direction & Up) |
| 152 | { |
| 153 | if (curY < (_origRootY + _origH - 1 - _minHeight)) |
| 154 | { |
| 155 | diff = _rootY - curY; |
| 156 | _rootY = curY; |
| 157 | _y -= diff; |
| 158 | _height = _origRootY + _origH - curY; |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | _rootY = _origRootY + _origH - 1 - _minHeight; |
| 163 | _y = _origY + _origH - 1 - _minHeight; |
| 164 | _height = _minHeight; |
| 165 | } |
| 166 | } |
| 167 | else if (_direction & Down) |
| 168 | { |
| 169 | if (curY > (_origRootY + _minHeight)) |
| 170 | _height = curY - _origRootY - 1; |
| 171 | else |
| 172 | _height = _minHeight; |
| 173 | } |
| 174 | |
| 175 | if (_direction & Left) |
| 176 | { |
| 177 | if (curX < (_origRootX + _origW - 1 - _minWidth)) |
| 178 | { |
| 179 | diff = _rootX - curX; |
| 180 | _rootX = curX; |
| 181 | _x -= diff; |
| 182 | _width = _origRootX + _origW - curX; |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | _rootX = _origRootX + _origW - 1 - _minWidth; |
| 187 | _x = _origX + _origW - 1 - _minWidth; |
| 188 | _width = _minWidth; |
| 189 | } |
| 190 | } |
| 191 | else if (_direction & Right) |
| 192 | { |
| 193 | if (curX > (_origRootX + _minWidth)) |
| 194 | _width = curX - _origRootX - 1; |
| 195 | else |
| 196 | _width = _minWidth; |
| 197 | } |
| 198 | |
| 199 | // Check if the values have "really" changed |
| 200 | |
| 201 | if ((w != _width) || (h != _height) || (x != _x) || (y != _y)) |
| 202 | _drawOutline (False); |
| 203 | } |
| 204 | |
| 205 | // _getCursor |
| 206 | |
| 207 | Cursor XsResizeOutline::_getCursor ( ) const |
| 208 | { |
| 209 | if (_direction == Up) |
| 210 | return (_cursors[TopCursor]); |
| 211 | if (_direction == Down) |
| 212 | return (_cursors[BottomCursor]); |
| 213 | if (_direction == Left) |
| 214 | return (_cursors[LeftCursor]); |
| 215 | if (_direction == Right) |
| 216 | return (_cursors[RightCursor]); |
| 217 | if (_direction == (Up | Left)) |
| 218 | return (_cursors[TopLeftCursor]); |
| 219 | if (_direction == (Up | Right)) |
| 220 | return (_cursors[TopRightCursor]); |
| 221 | if (_direction == (Down | Left)) |
| 222 | return (_cursors[BottomLeftCursor]); |
| 223 | if (_direction == (Down | Right)) |
| 224 | return (_cursors[BottomRightCursor]); |
| 225 | if (_direction == Undetermined) |
| 226 | return (_cursors[Fleur]); |
| 227 | |
| 228 | assert (0); |
| 229 | return (None); |
| 230 | } |
| 231 | |