ColorTrixDispatchImpl.mesa
Copyright © 1985 by Xerox Corporation. All rights reserved.
Bloomenthal, January 17, 1987 0:12:51 am PST
DIRECTORY Args, ColorTrixBasics, ColorTrixDispatch, Commander, ImagerPixelMap, IO, Real, Rope;
ColorTrixDispatchImpl: CEDAR PROGRAM
IMPORTS Args, ColorTrixBasics, Commander, ImagerPixelMap, IO, Real, Rope
EXPORTS ColorTrixDispatch
~ BEGIN
OPEN ColorTrixDispatch;
ctOps, cmOps: Ops ← NIL;
Color Trix Registry
AddToOps: PROC [ops: Ops, name: ROPE, proc: CommandProc, usage: ROPE] RETURNS [Ops] ~ {
IF ops = NIL THEN ops ← NEW[OpsSequence[10]];
IF ops.length = ops.maxLength THEN {
old: Ops ~ ops;
ops ← NEW[OpsSequence[Real.RoundI[1.3*old.length]]];
FOR i: NAT IN [0..old.length) DO ops[i] ← old[i]; ENDLOOP;
ops.length ← old.length;
};
ops[ops.length] ← [name, proc, usage];
ops.length ← ops.length+1;
RETURN[ops];
};
RegisterCmOp: PUBLIC PROC [name: ROPE, proc: CommandProc, usage: ROPE] ~ {
cmOps ← AddToOps[cmOps, name, proc, usage];
};
RegisterCtOp: PUBLIC PROC [name: ROPE, proc: CommandProc, usage: ROPE] ~ {
ctOps ← AddToOps[ctOps, name, proc, usage];
};
FindOp: PROC [name: ROPE, ops: Ops] RETURNS [Op] ~ {
FOR n: NAT IN [0..ops.length) DO
IF Rope.Equal[name, ops[n].name, FALSE] THEN RETURN[ops[n]];
ENDLOOP;
RETURN[[NIL, NIL, NIL]];
};
GetCmOp: PUBLIC PROC [name: ROPE] RETURNS [Op] ~ {RETURN[FindOp[name, cmOps]]};
GetCtOp: PUBLIC PROC [name: ROPE] RETURNS [Op] ~ {RETURN[FindOp[name, ctOps]]};
Dispatcher
ColorTrixDispatch: PUBLIC Commander.CommandProc ~ {
CmdIs: PROC [r: ROPE] RETURNS [BOOL] ~ {
RETURN[Rope.Find[cmd.command, r, 0, FALSE] # -1];
};
Equal: PROC [r1, r2: ROPE] RETURNS [BOOL] ~ {RETURN[Rope.Equal[r1, r2, FALSE]]};
GetOp: PROC RETURNS [Op] ~ {
FOR n: NAT IN [0..ops.length) DO
IF Equal[opName, ops[n].name] THEN RETURN[ops[n]];
ENDLOOP;
RETURN[[NIL, NIL, NIL]];
};
opName: ROPE ← Args.GetRope[cmd, 0];
ops: Ops ~ IF CmdIs["Ct"] OR CmdIs["ColorTrix"] THEN ctOps ELSE cmOps;
SELECT TRUE FROM
opName = NIL => RETURN[$Failure, colorTrixUsage];
Rope.Equal[opName, "?"] => {
IO.PutF[cmd.out, "%g commands are:\n", IO.rope[IF ops = ctOps THEN "Ct" ELSE "Cm"]];
FOR n: NAT IN [0..ops.length) DO
IO.PutF[cmd.out, "%g\n", IO.rope[ops[n].name]];
ENDLOOP;
};
ENDCASE => {
op: Op ~ GetOp[];
IF op.proc = NIL THEN RETURN[$Failure, IF ops = ctOps
THEN "No such colortrix option." ELSE "No such colormap option."];
IF Rope.Equal[Args.GetRope[cmd, 1], "?"]
THEN RETURN[$Failure, Rope.Concat["Usage: ", op.usage]];
cmd.command ← opName;
cmd.commandLine ← Rope.Substr[cmd.commandLine,
Rope.Find[cmd.commandLine, opName]+Rope.Length[opName]];
[result, msg] ← op.proc[cmd];
IF result = $Failure AND msg = NIL THEN msg ← Rope.Concat["Usage: ", op.usage];
};
};
Windowing Support
initalWindow: PUBLIC ImagerPixelMap.DeviceRectangle ← [0, 0, 768, 1024];
globalWindow: PUBLIC ImagerPixelMap.DeviceRectangle ← initalWindow;
GetWindow: PUBLIC PROC [cmd: Commander.Handle, pm: PixelMap]
RETURNS [window: DeviceRectangle] ~ {
x, y, w, h: Args.Arg;
window ← globalWindow;
FOR n: NAT IN [0..Args.NArgs[cmd]) DO
IF NOT Rope.Equal[Args.GetRope[cmd, n], "-w"] THEN LOOP;
x ← Args.ArgInt[cmd, n+1];
y ← Args.ArgInt[cmd, n+2];
w ← Args.ArgInt[cmd, n+3];
h ← Args.ArgInt[cmd, n+4];
EXIT;
ENDLOOP;
IF x.ok AND y.ok AND w.ok AND h.ok THEN window ← [y.int, x.int, h.int, w.int];
window ← ImagerPixelMap.Intersect[ImagerPixelMap.BoundedWindow[pm], window];
};
GetWindowedPm: PUBLIC PROC [cmd: Commander.Handle] RETURNS [pm: PixelMap] ~ {
pm ← ColorTrixBasics.GetColorDisplayPm[];
RETURN[ImagerPixelMap.SetWindow[pm, GetWindow[cmd, pm]]];
};
GetWindowedPmMisc: PUBLIC PROC [cmd: Commander.Handle]
RETURNS [pmMisc: PixelMapMisc] ~ {
pmMisc ← ColorTrixBasics.GetColorDisplayPmMisc[];
SELECT pmMisc.bpp FROM
8 => pmMisc.bw ← ImagerPixelMap.SetWindow[pmMisc.bw, GetWindow[cmd, pmMisc.bw]];
24 => {
pmMisc.rg ← ImagerPixelMap.SetWindow[pmMisc.rg, GetWindow[cmd, pmMisc.rg]];
pmMisc.b ← ImagerPixelMap.SetWindow[pmMisc.b, GetWindow[cmd, pmMisc.b]];
};
ENDCASE => NULL;
};
SetGlobalWindow: PUBLIC PROC [window: DeviceRectangle] ~ {globalWindow ← window};
GetGlobalWindow: PUBLIC PROC RETURNS [DeviceRectangle] ~ {RETURN[globalWindow]};
GetInitialWindow: PUBLIC PROC RETURNS [DeviceRectangle] ~ {RETURN[initalWindow]};
Usage Message
colorTrixUsage: ROPE ~
"Ct (or ColorTrix) < colortrix option> [arguments]
Cm (or ColorTrixMap) <colormap option> [arguments]
'Ct ?' or 'Cm ?' will list the set of appropriate options;
'Ct/Cm <option> ?' prints a usage message for the option.";
Start Code
Commander.Register["///Commands/Ct",    ColorTrixDispatch, colorTrixUsage];
Commander.Register["///CommandsColorTrix/",  ColorTrixDispatch, colorTrixUsage];
Commander.Register["///Commands/Cm",    ColorTrixDispatch, colorTrixUsage];
Commander.Register["///Commands/ColorTrixMap", ColorTrixDispatch, colorTrixUsage];
END.