1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
/*
* Copyright (c) 1987, 1988, 1989, 1990, 1991 Stanford University
* Copyright (c) 1991 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Stanford and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Stanford and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL STANFORD OR SILICON GRAPHICS BE LIABLE FOR
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
/*
* Panner implementation.
*/
#include <InterViews/pattern.h>
#include <IV-2_6/InterViews/adjuster.h>
#include <IV-2_6/InterViews/border.h>
#include <IV-2_6/InterViews/panner.h>
#include <IV-2_6/InterViews/box.h>
#include <IV-2_6/InterViews/glue.h>
#include <IV-2_6/InterViews/painter.h>
#include <IV-2_6/InterViews/perspective.h>
#include <IV-2_6/InterViews/rubrect.h>
#include <IV-2_6/InterViews/sensor.h>
#include <IV-2_6/InterViews/shape.h>
#include <OS/math.h>
#include <string.h>
#include <IV-2_6/_enter.h>
Panner::Panner (Interactor* i, int size) {
Init(i, size);
}
Panner::Panner (const char* name, Interactor* i, int size) {
SetInstance(name);
Init(i, size);
}
Panner::~Panner() { }
/* 0.3 second delay for auto-repeat */
static int DELAY = 3;
void Panner::Init (Interactor* i, int n) {
SetClassName("Panner");
size = n;
adjusters = new HBox(
new HGlue,
new VBox(
new VGlue,
new UpMover(i, DELAY),
new HBox(
new HGlue,
new LeftMover(i, DELAY),
new HGlue,
new RightMover(i, DELAY),
new HGlue
),
new DownMover(i, DELAY),
new VGlue
),
new HGlue,
new VBox(
new VGlue(2),
new Enlarger(i),
new VGlue(4),
new Reducer(i),
new VGlue(2)
),
new HGlue
);
slider = new Slider(i);
Insert(
new VBox(adjusters, new HBorder, slider)
);
}
void Panner::Reconfig () {
MonoScene::Reconfig();
Shape a = *adjusters->GetShape();
if (a.vstretch != 0 || a.vshrink != a.height / 3) {
if (size != 0) {
a.width = size;
a.hshrink = a.hstretch = 0;
}
a.vstretch = 0;
a.vshrink = a.height/3;
adjusters->Reshape(a);
}
Shape* s = slider->GetShape();
if (s->width != a.width) {
slider->Reshape(a);
}
}
static const int MIN_SLIDER_HT = 20;
enum MoveType { MOVE_HORIZ, MOVE_VERT, MOVE_UNDEF };
Slider::Slider (Interactor* i) {
Init(i);
}
Slider::Slider (const char* name, Interactor* i) {
SetInstance(name);
Init(i);
}
void Slider::Init (Interactor* i) {
SetClassName("Slider");
interactor = i;
view = i->GetPerspective();
view->Attach(this);
shown = new Perspective;
constrained = false;
moveType = MOVE_UNDEF;
*shown = *view;
shape->vstretch = shape->vshrink = 0;
prevl = prevb = prevr = prevt = 0;
input_ = new Sensor(updownEvents);
}
void Slider::Reconfig () {
Painter* p = new Painter(output_);
p->Reference();
Unref(output_);
output_ = p;
const char* attrib = GetAttribute("syncScroll");
syncScroll = attrib != nil &&
(strcmp(attrib, "true") == 0 || strcmp(attrib, "on") == 0);
}
void Slider::Reshape (Shape& ns) {
if (shown->width == 0) {
*shape = ns;
} else {
shape->width = (canvas == nil) ? ns.width : xmax + 1;
float aspect = float(shown->height) / float(shown->width);
int h = Math::round(aspect * float(shape->width));
if (h != shape->height) {
shape->height = h;
Scene* p = Parent();
if (p != nil) {
p->Change(this);
}
}
}
}
void Slider::Draw () {
if (canvas != nil) {
output_->SetPattern(new Pattern(Pattern::lightgray));
output_->FillRect(canvas, 0, 0, xmax, ymax);
output_->SetPattern(new Pattern(int(Pattern::clear)));
output_->FillRect(canvas, left, bottom, right, top);
output_->SetPattern(new Pattern(Pattern::solid));
output_->Rect(canvas, left, bottom, right, top);
output_->Line(canvas, left+1, bottom-1, right+1, bottom-1);
output_->Line(canvas, right+1, bottom-1, right+1, top-1);
prevl = left; prevb = bottom;
prevr = right; prevt = top;
}
}
void Slider::Redraw (
IntCoord left, IntCoord bottom, IntCoord right, IntCoord top
) {
output_->Clip(canvas, left, bottom, right, top);
Draw();
output_->NoClip();
}
inline IntCoord Slider::ViewX (IntCoord x) {
return Math::round(float(x) * float(shown->width) / float(xmax));
}
inline IntCoord Slider::ViewY (IntCoord y) {
return Math::round(float(y) * float(shown->height) / float(ymax));
}
inline IntCoord Slider::SliderX (IntCoord x) {
return Math::round(float(x) * float(xmax) / float(shown->width));
}
inline IntCoord Slider::SliderY (IntCoord y) {
return Math::round(float(y) * float(ymax) / float(shown->height));
}
void Slider::Move (IntCoord dx, IntCoord dy) {
shown->curx += dx;
shown->cury += dy;
}
boolean Slider::Inside (Event& e) {
return e.x > left && e.x < right && e.y > bottom && e.y < top;
}
void Slider::CalcLimits (Event& e) {
llim = e.x - Math::max(0, left);
blim = e.y - Math::max(0, bottom);
rlim = e.x + Math::max(0, xmax - right);
tlim = e.y + Math::max(0, ymax - top);
constrained = e.shift;
moveType = MOVE_UNDEF;
origx = e.x;
origy = e.y;
}
static int CONSTRAIN_THRESH = 2;
// difference between x and y movement needed to decide which direction
// is constrained
void Slider::Constrain (Event& e) {
IntCoord dx, dy;
if (constrained && moveType == MOVE_UNDEF) {
dx = Math::abs(e.x - origx);
dy = Math::abs(e.y - origy);
if (Math::abs(dx - dy) < CONSTRAIN_THRESH) {
e.x = origx;
e.y = origy;
} else if (dx > dy) {
moveType = MOVE_HORIZ;
} else {
moveType = MOVE_VERT;
}
}
if (!constrained) {
e.x = Math::min(Math::max(e.x, llim), rlim);
e.y = Math::min(Math::max(e.y, blim), tlim);
} else if (moveType == MOVE_HORIZ) {
e.x = Math::min(Math::max(e.x, llim), rlim);
e.y = origy;
} else if (moveType == MOVE_VERT) {
e.x = origx;
e.y = Math::min(Math::max(e.y, blim), tlim);
}
}
void Slider::Slide (Event& e) {
IntCoord newleft, newbot, dummy;
boolean control = e.control;
Listen(allEvents);
SlidingRect r(output_, canvas, left, bottom, right, top, e.x, e.y);
CalcLimits(e);
do {
switch (e.eventType) {
case MotionEvent:
e.target->GetRelative(e.x, e.y, this);
Constrain(e);
r.Track(e.x, e.y);
if ((syncScroll && !control) || (!syncScroll && control)) {
r.Erase();
r.GetCurrent(newleft, newbot, dummy, dummy);
Move(ViewX(newleft - left), ViewY(newbot - bottom));
interactor->Adjust(*shown);
}
break;
default:
break;
}
Read(e);
} while (e.eventType != UpEvent);
r.GetCurrent(newleft, newbot, dummy, dummy);
Move(ViewX(newleft - left), ViewY(newbot - bottom));
Listen(input_);
}
void Slider::Jump (Event& e) {
register Perspective* s = shown;
IntCoord dx, dy;
if (e.button == RIGHTMOUSE) {
dx = ViewX(e.x) - s->curx - s->curwidth/2;
dy = ViewY(e.y) - s->cury - s->curheight/2;
} else {
if (e.button == LEFTMOUSE) {
dx = s->sx;
dy = s->sy;
} else {
dx = s->lx;
dy = s->ly;
}
if (e.x < left) {
dx = -dx;
} else if (e.x < right) {
dx = 0;
}
if (e.y < bottom) {
dy = -dy;
} else if (e.y < top) {
dy = 0;
}
}
dx = Math::min(
Math::max(s->x0 - s->curx, dx),
s->x0 + s->width - s->curx - s->curwidth
);
dy = Math::min(
Math::max(s->y0 - s->cury, dy),
s->y0 + s->height - s->cury - s->curheight
);
Move(dx, dy);
}
void Slider::Handle (Event& e) {
if (e.eventType == DownEvent) {
if (Inside(e)) {
Slide(e);
} else {
Jump(e);
}
interactor->Adjust(*shown);
}
}
static const int MIN_SIZE = 2;
void Slider::SizeKnob () {
register Perspective* s = shown;
if (canvas != nil) {
left = SliderX(s->curx - s->x0);
bottom = SliderY(s->cury - s->y0);
right = left + Math::max(SliderX(s->curwidth), MIN_SIZE);
top = bottom + Math::max(SliderY(s->curheight), MIN_SIZE);
}
}
void Slider::Update () {
register Perspective* p = shown;
int h, oldwidth, oldheight;
float aspect;
Scene* s;
Shape ns;
oldwidth = p->width;
oldheight = p->height;
*p = *view;
aspect = float(p->height) / float(p->width);
SizeKnob();
if (p->width != oldwidth || p->height != oldheight) {
h = Math::round(aspect * float(shape->width));
if (h == shape->height) {
Draw();
} else {
shape->height = h;
if ((s = Parent()) != nil) {
s->Change(this);
}
}
} else if (
prevl != left || prevb != bottom || prevr != right || prevt != top
) {
Draw();
}
}
void Slider::Resize () {
int w = xmax + 1;
if (shape->width != w) {
Shape ns = *shape;
ns.width = w;
Reshape(ns);
}
SizeKnob();
}
Slider::~Slider () {
view->Detach(this);
Unref(shown);
}
|