FILE: BiScrollersImpl.Mesa
Last Edited by: Spreitzer, April 15, 1985 9:51:20 am PST
DIRECTORY BiScrollers, Geom2D, IO, Menus, MessageWindow, Real, RealFns, Rope, ViewerOps, ViewerSpecs, ViewerTools;
BiScrollersImpl: CEDAR MONITOR
IMPORTS Geom2D, IO, Menus, MessageWindow, Real, RealFns, Rope, ViewerOps, ViewerTools
EXPORTS BiScrollers
SHARES Menus =
BEGIN OPEN BiScrollers;
StyleList: TYPE = LIST OF RECORD [name: ROPE, style: BiScrollerStyle];
bsMenu: PUBLIC Menus.Menu ← Menus.CreateMenu[];
styles: StyleList ← NIL;
defaultStyleName: ROPENIL;
GetStyle: PUBLIC PROC [name: ROPENIL] RETURNS [style: BiScrollerStyle] = {
IF name = NIL THEN name ← defaultStyleName;
FOR sl: StyleList ← styles, sl.rest WHILE sl # NIL DO
IF sl.first.name.Equal[name] THEN RETURN [sl.first.style]
ENDLOOP;
style ← NIL;
};
RegisterStyle: PUBLIC ENTRY PROC [name: ROPE, style: BiScrollerStyle] = {
ENABLE UNWIND => {};
styles ← CONS[[name, style], styles];
};
SetDefaultStyle: PUBLIC ENTRY PROC [name: ROPE] RETURNS [old: ROPE] =
{old ← defaultStyleName; defaultStyleName ← name};
IsBiScroller: PUBLIC PROC [ra: REF ANY] RETURNS [BOOLEAN] =
{RETURN [ISTYPE[ra, BiScroller]]};
NarrowToBiScroller: PUBLIC PROC [ra: REF ANY] RETURNS [BiScroller] =
{bs: BiScroller ← NARROW[ra]; RETURN [bs]};
QuaViewer: PUBLIC PROC [bs: BiScroller, inner: BOOLFALSE] RETURNS [Viewer] =
{RETURN [bs.style.QuaViewer[bs, inner]]};
QuaBiScroller: PUBLIC PROC [v: Viewer] RETURNS [BiScroller] =
{bs: BiScroller ← NARROW[v.data]; RETURN [bs]};
ViewerIsABiScroller: PUBLIC PROC [v: Viewer] RETURNS [BOOLEAN] =
{RETURN [ISTYPE[v.data, BiScroller]]};
ClientDataOf: PUBLIC PROC [bs: BiScroller] RETURNS [REF ANY] =
{RETURN [bs.style.ClientDataOf[bs]]};
ClientDataOfViewer: PUBLIC PROC [v: Viewer] RETURNS [REF ANY] =
{bs: BiScroller ← NARROW[v.data]; RETURN [bs.style.ClientDataOf[bs]]};
ViewportExtrema: PUBLIC PROC [bs: BiScroller, direction: Vec] RETURNS [min, max: Vec] =
BEGIN
vl: VecList ← bs.style.ViewportOf[bs];
e: Geom2D.ExtremaRec ←
direction.Extreme[vl.first,
direction.Extreme[vl.rest.first,
direction.Extreme[vl.rest.rest.first,
direction.StartExtreme[vl.rest.rest.rest.first]]]];
RETURN [e.minV, e.maxV];
END;
ViewportBox: PUBLIC PROC [bs: BiScroller] RETURNS [bb: Geom2D.Area] = {
Pt: PROC [v: Vec] RETURNS [a: Geom2D.Area] = INLINE
{a ← [v.x, v.y, v.x, v.y]};
vl: VecList ← bs.style.ViewportOf[bs];
bb ← Geom2D.UpdateBounds[ Geom2D.UpdateBounds[ Geom2D.UpdateBounds[
Pt[vl.first],
Pt[vl.rest.first]],
Pt[vl.rest.rest.first]],
Pt[vl.rest.rest.rest.first]];
};
GenID: PUBLIC PROC [BiScroller] RETURNS [Transform] --TransformGenerator-- =
{RETURN [Geom2D.id]};
ConstantVector: PROC [bs: BiScroller] RETURNS [cv: Vec] = {
v: Viewer ← bs.style.QuaViewer[bs, TRUE];
cv ← [v.cw/2, v.ch/2];
};
ViewLimitsOfImage: PUBLIC PROC [bs: BiScroller, axis: Axis] RETURNS [vmin, vmax: REAL] =
BEGIN
t: Transform ← bs.style.GetTransforms[bs].t;
norm, min, max: Vec;
SELECT axis FROM
X => norm ← [t.a, t.c];
Y => norm ← [t.b, t.d];
ENDCASE => ERROR;
[min, max] ← bs.class.common.extrema[bs.style.ClientDataOf[bs], norm];
min ← t.MapVec[min];
max ← t.MapVec[max];
SELECT axis FROM
X => {vmin ← min.x; vmax ← max.x};
Y => {vmin ← min.y; vmax ← max.y};
ENDCASE => ERROR;
END;
Scale: PUBLIC PROC [bs: BiScroller, op: ScaleOp, paint: BOOLTRUE] = {
cv: Vec ← ConstantVector[bs];
old: Transform ← bs.style.GetTransforms[bs].t;
new: Transform ← old.Translate[-cv.x, -cv.y];
WITH op SELECT FROM
reset => {
v: Transform ← bs.class.common.vanilla[bs];
vd: REAL ← v.a*v.d - v.b*v.c;
od: REAL ← old.a*old.d - old.b*old.c;
new ← new.ScaleT[
RealFns.SqRt[ABS[vd/ZeroProtect[od]]]*SGN[vd]*SGN[od]
];
};
byArg => new ← new.ScaleT[arg];
ENDCASE => ERROR;
new ← new.TranslateV[cv];
bs.style.ChangeTransform[bs, new, paint];
};
SGN: PROC [r: REAL] RETURNS [sgn: [-1 .. 1]] = {
sgn ← SELECT r FROM
<0 => -1,
=0 => 0,
>0 => 1,
ENDCASE => ERROR};
ZeroProtect: PROC [r: REAL] RETURNS [r0: REAL] =
{r0 ← IF r = 0.0 THEN 1.0 ELSE r};
Rotate: PUBLIC PROC [bs: BiScroller, op: RotateOp, paint: BOOLTRUE] = {
cv: Vec ← ConstantVector[bs];
old: Transform ← bs.style.GetTransforms[bs].t;
new: Transform ← old.Translate[-cv.x, -cv.y];
WITH op SELECT FROM
reset => {
v: Transform ← bs.class.common.vanilla[bs];
new ← new.RotateDegrees[
RealFns.ArcTanDeg[y: v.b, x: v.a] -
RealFns.ArcTanDeg[y: old.b, x: old.a]
];
};
byArg => new ← new.RotateDegrees[arg];
ENDCASE => ERROR;
new ← new.TranslateV[cv];
bs.style.ChangeTransform[bs, new, paint];
};
Shift: PUBLIC PROC [bs: BiScroller, dx, dy: REAL, paint: BOOLTRUE] = {
old: Transform ← bs.style.GetTransforms[bs].t;
new: Transform ← old.Translate[dx, dy];
bs.style.ChangeTransform[bs, new, paint];
};
Align: PUBLIC PROC [bs: BiScroller, client, viewer: Location, doX, doY, paint: BOOLTRUE] = {
old: Transform ← bs.style.GetTransforms[bs].t;
new: Transform;
from, to: Vec;
Blend: PROC [a: REAL, b0, b1: REAL] RETURNS [c: REAL] =
{c ← (1-a)*b0 + a*b1};
WITH client SELECT FROM
coord => from ← old.MapVec[[x, y]];
fraction => {
min, max: REAL;
IF doX THEN {
[min, max] ← ViewLimitsOfImage[bs, X];
from.x ← Blend[fx, min, max];
};
IF doY THEN {
[min, max] ← ViewLimitsOfImage[bs, Y];
from.y ← Blend[fy, min, max];
};
};
ENDCASE => ERROR;
WITH viewer SELECT FROM
coord => to ← [x, y];
fraction => {
v: Viewer ← bs.style.QuaViewer[bs, TRUE];
to ← [fx*v.cw, fy*v.ch];
};
ENDCASE => ERROR;
new ← old.Translate[-from.x, -from.y].TranslateV[to];
IF NOT doX THEN {new.a ← old.a; new.c ← old.c; new.e ← old.e};
IF NOT doY THEN {new.b ← old.b; new.d ← old.d; new.f ← old.f};
bs.style.ChangeTransform[bs, new, paint];
};
BoxScale: PUBLIC PROC [bs: BiScroller, from, to: Geom2D.Area --both in viewer coords--, paint: BOOLTRUE] = {
cv: Vec ← ConstantVector[bs];
old: Transform ← bs.style.GetTransforms[bs].t;
new: Transform ← old.Translate[
-(from.xmin + from.xmax) / 2,
-(from.ymin + from.ymax) / 2];
ndx, ndy, odx, ody, sx, sy: REAL;
ndx ← to.xmax - to.xmin;
ndy ← to.ymax - to.ymin;
odx ← from.xmax - from.xmin;
ody ← from.ymax - from.ymin;
sx ← IF ndx=0 OR odx=0 THEN 1 ELSE ndx/odx;
sy ← IF ndy=0 OR ody=0 THEN 1 ELSE ndy/ody;
IF NOT bs.class.common.mayStretch THEN sx ← sy ← MIN[sx, sy];
IF bs.class.common.preferIntegerCoefficients
THEN {sx ← Round[sx]; sy ← Round[sy]};
new ← new.ScaleTXY[sx, sy];
new ← new.Translate[
(to.xmin + to.xmax) / 2,
(to.ymin + to.ymax) / 2];
bs.style.ChangeTransform[bs, new, paint];
};
Round: PROC [r: REAL] RETURNS [rr: REAL] = {
i: INT ← Real.RoundLI[r];
rr ← IF (i=0) # (r=0) THEN r ELSE REAL[i];
};
GetArg: PROC RETURNS [valid: BOOL, arg: REAL] = {
sel: ROPE ← ViewerTools.GetSelectionContents[];
s: IO.STREAM;
IF NOT (valid ← (sel.Length[] > 1)) THEN RETURN;
s ← IO.RIS[sel];
arg ← s.GetReal[!
IO.Error, IO.EndOfStream => {
valid ← FALSE;
MessageWindow.Append[
message: IO.PutFR["Select a number, not %g", IO.refAny[sel]],
clearFirst: TRUE];
MessageWindow.Blink[];
CONTINUE}
];
s.Close[];
};
GetBS: PROC [v: Viewer] RETURNS [bs: BiScroller] = {
FOR v ← v, v.parent WHILE (bs ← NARROW[ViewerOps.FetchProp[v, $SubBiScroller]]) = NIL DO NULL ENDLOOP;
};
SetBS: PUBLIC PROC [v: Viewer, bs: BiScroller] = {
FOR v ← v, v.parent WHILE v # NIL DO
ViewerOps.AddProp[v, $SubBiScroller, bs]
ENDLOOP;
};
ScaleButt: PROC [parent: REF ANY, clientData: REF ANYNIL, mouseButton: Menus.MouseButton ← red, shift, control: BOOLFALSE] --Menus.MenuProc-- = {
v: Viewer ← NARROW[parent];
bs: BiScroller ← GetBS[v];
valid: BOOL;
arg: REAL;
[valid, arg] ← GetArg[];
IF NOT valid THEN arg ← 2.0;
Scale[
bs,
SELECT mouseButton FROM
red => [byArg[arg]],
yellow => [reset[]],
blue => [byArg[1.0/arg]],
ENDCASE => ERROR
];
};
RotateButt: PROC [parent: REF ANY, clientData: REF ANYNIL, mouseButton: Menus.MouseButton ← red, shift, control: BOOLFALSE] --Menus.MenuProc-- = {
v: Viewer ← NARROW[parent];
bs: BiScroller ← GetBS[v];
valid: BOOL;
arg: REAL;
[valid, arg] ← GetArg[];
IF NOT valid THEN arg ← 90;
Rotate[
bs,
SELECT mouseButton FROM
red => [byArg[arg]],
yellow => [reset[]],
blue => [byArg[-arg]],
ENDCASE => ERROR
];
};
FitButt: PROC [parent: REF ANY, clientData: REF ANYNIL, mouseButton: Menus.MouseButton ← red, shift, control: BOOLFALSE] --Menus.MenuProc-- = {
v: Viewer ← NARROW[parent];
bs: BiScroller ← GetBS[v];
iv: Viewer ← bs.style.QuaViewer[bs, TRUE];
limits: Geom2D.Area;
[limits.xmin, limits.xmax] ← ViewLimitsOfImage[bs, X];
[limits.ymin, limits.ymax] ← ViewLimitsOfImage[bs, Y];
BoxScale[bs, limits, [0, 0, iv.cw, iv.ch]];
};
ResetAndCenterButt: PROC [parent: REF ANY, clientData: REF ANYNIL, mouseButton: Menus.MouseButton ← red, shift, control: BOOLFALSE] --Menus.MenuProc-- = {
v: Viewer ← NARROW[parent];
bs: BiScroller ← GetBS[v];
Rotate[bs, [reset[]], FALSE];
Scale[bs, [reset[]], FALSE];
Align[bs, [fraction[0.5, 0.5]], [fraction[0.5, 0.5]]];
};
CenterButt: PROC [parent: REF ANY, clientData: REF ANYNIL, mouseButton: Menus.MouseButton ← red, shift, control: BOOLFALSE] --Menus.MenuProc-- = {
v: Viewer ← NARROW[parent];
bs: BiScroller ← GetBS[v];
Align[bs, [fraction[0.5, 0.5]], [fraction[0.5, 0.5]]];
};
VanillaButt: PROC [parent: REF ANY, clientData: REF ANYNIL, mouseButton: Menus.MouseButton ← red, shift, control: BOOLFALSE] --Menus.MenuProc-- = {
v: Viewer ← NARROW[parent];
bs: BiScroller ← GetBS[v];
bs.style.ChangeTransform[bs, bs.class.common.vanilla[bs], TRUE];
};
LeftButt: PROC [parent: REF ANY, clientData: REF ANYNIL, mouseButton: Menus.MouseButton ← red, shift, control: BOOLFALSE] --Menus.MenuProc-- = {
v: Viewer ← NARROW[parent];
bs: BiScroller ← GetBS[v];
Align[bs: bs, client: [fraction[0.0, 0.0]], viewer: [fraction[0.0, 0.0]], doY: FALSE];
};
RightButt: PROC [parent: REF ANY, clientData: REF ANYNIL, mouseButton: Menus.MouseButton ← red, shift, control: BOOLFALSE] --Menus.MenuProc-- = {
v: Viewer ← NARROW[parent];
bs: BiScroller ← GetBS[v];
Align[bs: bs, client: [fraction[1.0, 0.0]], viewer: [fraction[1.0, 0.0]], doY: FALSE];
};
BottomButt: PROC [parent: REF ANY, clientData: REF ANYNIL, mouseButton: Menus.MouseButton ← red, shift, control: BOOLFALSE] --Menus.MenuProc-- = {
v: Viewer ← NARROW[parent];
bs: BiScroller ← GetBS[v];
Align[bs: bs, client: [fraction[0.0, 0.0]], viewer: [fraction[0.0, 0.0]], doX: FALSE];
};
TopButt: PROC [parent: REF ANY, clientData: REF ANYNIL, mouseButton: Menus.MouseButton ← red, shift, control: BOOLFALSE] --Menus.MenuProc-- = {
v: Viewer ← NARROW[parent];
bs: BiScroller ← GetBS[v];
Align[bs: bs, client: [fraction[0.0, 1.0]], viewer: [fraction[0.0, 1.0]], doX: FALSE];
};
CatenateMenus: PUBLIC PROC [pre, post: Menus.Menu] RETURNS [menu: Menus.Menu] = {
preLines, postLines: INT;
menu ← IF pre = NIL THEN Menus.CreateMenu[0] ELSE pre.CopyMenu[];
preLines ← menu.GetNumberOfLines[];
postLines ← post.GetNumberOfLines[];
menu.ChangeNumberOfLines[preLines + postLines];
FOR i: INT IN [0 .. postLines) DO menu.SetLine[preLines+i, post.GetLine[i]] ENDLOOP;
};
SetViewerPosition: PUBLIC PROC [v: Viewer, x, y, w, h: INTEGER] = {
vbs: INTEGER = IF v.border THEN ViewerSpecs.windowBorderSize ELSE 0;
v.wx ← x; v.wy ← y; v.ww ← w; v.wh ← h;
v.ch ← v.wh - (vbs+vbs) - (IF v.column=static OR v.parent#NIL THEN 0 ELSE ViewerSpecs.captionHeight);
IF v.menu#NIL THEN v.ch ← v.ch - (v.menu.linesUsed*ViewerSpecs.menuHeight);
v.cx ← v.wx + vbs + (IF v.scrollable THEN ViewerSpecs.scrollBarW ELSE 0);
v.cy ← v.wy + vbs;
v.cw ← v.ww - (v.cx-v.wx) - vbs;
};
Start: PROC = {
bsMenu.AppendMenuEntry[Menus.CreateEntry["Scale", ScaleButt], 0];
bsMenu.AppendMenuEntry[Menus.CreateEntry["Rotate", RotateButt], 0];
bsMenu.AppendMenuEntry[Menus.CreateEntry["Fit", FitButt], 0];
bsMenu.AppendMenuEntry[Menus.CreateEntry["ResetAndCenter", ResetAndCenterButt], 0];
bsMenu.AppendMenuEntry[Menus.CreateEntry["Center", CenterButt], 0];
bsMenu.AppendMenuEntry[Menus.CreateEntry["Vanilla", VanillaButt], 0];
bsMenu.AppendMenuEntry[Menus.CreateEntry["Left", LeftButt], 0];
bsMenu.AppendMenuEntry[Menus.CreateEntry["Right", RightButt], 0];
bsMenu.AppendMenuEntry[Menus.CreateEntry["Top", TopButt], 0];
bsMenu.AppendMenuEntry[Menus.CreateEntry["Bottom", BottomButt], 0];
};
Start[];
END.