GraphBrowsersImpl.Mesa
Last Edited by: Spreitzer, June 7, 1985 5:00:22 pm PDT
DIRECTORY Atom, GraphBrowsers, Graphs, Icons, Imager, ImagerBackdoor, ImagerBox, ImagerFont, Menus, MessageWindow, MJSContainers, Process, Real, TIPUser, VFonts, ViewerClasses, ViewerOps;
GraphBrowsersImpl: CEDAR MONITOR
IMPORTS Atom, Icons, Imager, ImagerBackdoor, ImagerBox, ImagerFont, MessageWindow, MJSContainers, Process, Real, TIPUser, VFonts, ViewerOps
EXPORTS GraphBrowsers =
BEGIN OPEN GraphBrowsers;
Pane: TYPE = REF PaneRep;
PaneRep: PUBLIC TYPE = RECORD [
asViewer: Viewer,
vertex: Graphs.Vertex,
browser: Browser,
parent: Pane,
myIndex: CARDINAL,
height: CARDINAL,
columnWidth: REAL,
rows, columns: CARDINAL,
expanded, expandedV: CARDINAL,
length: CARDINAL,
sel, vSel: CardBox,
maxNameWidth: REAL,
entries: SEQUENCE size: CARDINAL OF PaneEntry];
CardBox: TYPE = RECORD [
startRow, startCol, finishRow, finishCol: CARDINAL];
badBox: CardBox = [badRowCol, badRowCol, badRowCol, badRowCol];
badRowCol: CARDINAL = LAST[CARDINAL] - 1;
PaneEntry: TYPE = RECORD [
name: ROPENIL,
vertex: Graphs.Vertex ← NIL,
drawX, drawY: REAL ← 0];
Browser: TYPE = REF BrowserRep;
BrowserRep: PUBLIC TYPE = RECORD [
container: Viewer ← NIL,
width: INTEGER ← 0,
panes: LIST OF Pane ← NIL,
class: BrowserClass,
browserData: REF ANY,
serial: INTEGER ← 1,
newSerial: CONDITION];
dy: INT ← VFonts.FontHeight[] + 2;
hSep: REAL ← 10;
font: ImagerFont.Font ← VFonts.defaultFont;
paneFlavor: ATOM ← Atom.MakeAtom["Spreitzer January 5, 1984 6:40 pm"];
structureButton: Menus.MouseButton ← red;
icon: Icons.IconFlavor ← Icons.NewIconFromFile["GraphBrowsers.icons", 0];
basicBrowserClass: PUBLIC BrowserClass ← NEW [BrowserClassRep ← [
cmds: ALL[ALL[[[Expand], [Contract]]]] ]];
paneClass: ViewerClasses.ViewerClass ← NEW [ViewerClasses.ViewerClassRec ← [
flavor: paneFlavor,
notify: NotifyPane,
paint: PaintPane,
tipTable: TIPUser.InstantiateNewTIPTable[file: "GraphBrowserPane.TIP"],
topDownCoordSys: FALSE
]];
Browse: PUBLIC PROC [vertex: Graphs.Vertex, browserClass: BrowserClass, browserData: REF ANYNIL, viewerInit: ViewerClasses.ViewerRec ← [], paint: BOOLEANTRUE] RETURNS [browser: Browser] =
BEGIN
browser ← NEW [BrowserRep ← [
class: browserClass,
browserData: browserData]];
viewerInit.data ← browser;
IF viewerInit.icon = unInit THEN viewerInit.icon ← icon;
browser.container ← MJSContainers.Create[viewerFlavor: $GraphBrowser, info: viewerInit, paint: paint];
TRUSTED {Process.InitializeCondition[@browser.newSerial, Process.SecondsToTicks[3]]};
browser.panes ← LIST[MakePane[
browser: browser,
vertex: vertex,
parent: NIL,
myIndex: 0,
paint: paint
]];
END;
BrowserQuaViewer: PUBLIC PROC [browser: Browser] RETURNS [Viewer] =
{RETURN [browser.container]};
MakePane: PROC [browser: Browser, vertex: Graphs.Vertex, parent: Pane, myIndex: CARDINAL, paint: BOOLEAN] RETURNS [pane: Pane] =
BEGIN
SurveyNameWidths: PROC [edgeLabel: ROPE, data: REF ANY] =
BEGIN
xmin,ymin,xmax,ymax: REAL;
[[xmin,ymin,xmax,ymax]] ← ImagerBox.BoxFromExtents[font.RopeBoundingBox[edgeLabel]];
pane.entries[index] ← [name: edgeLabel, drawX: 0, drawY: 0, vertex: NIL];
pane.maxNameWidth ← MAX[pane.maxNameWidth, hSep + xmax - xmin];
index ← index + 1;
END;
SurveyVertexWidths: PROC [edge: Graphs.Edge, data: REF ANY] =
BEGIN
xmin,ymin,xmax,ymax: REAL;
[[xmin,ymin,xmax,ymax]] ← ImagerBox.BoxFromExtents[font.RopeBoundingBox[edge.label]];
pane.entries[index] ← [name: edge.label, drawX: 0, drawY: 0, vertex: edge.otherSide];
pane.maxNameWidth ← MAX[pane.maxNameWidth, hSep + xmax - xmin];
index ← index + 1;
END;
GetList: PROC [edge: Graphs.Edge, data: REF ANY] =
BEGIN
rest: LIST OF Graphs.Edge ← LIST[edge];
IF last = NIL THEN list ← rest ELSE last.rest ← rest;
last ← rest;
size ← size + 1;
END;
list, last: LIST OF Graphs.Edge ← NIL;
index, size: CARDINAL ← 0;
IF vertex.type.GetNeighborCount = NIL
THEN TRUSTED {vertex.type.Expand[vertex: vertex, consume: [GetList]]}
ELSE size ← vertex.type.GetNeighborCount[vertex];
pane ← NEW [PaneRep[size]];
pane.browser ← browser;
pane.parent ← parent;
pane.myIndex ← myIndex;
pane.vertex ← vertex;
pane.height ← 10;
pane.expanded ← pane.expandedV ← pane.size;
pane.sel ← pane.vSel ← badBox;
pane.columnWidth ← 0;
pane.maxNameWidth ← 0;
IF (size = 0) OR list # NIL THEN
BEGIN
FOR list ← list, list.rest WHILE list # NIL DO
SurveyVertexWidths[list.first, NIL];
ENDLOOP;
END
ELSE TRUSTED {vertex.type.LabelEdges[vertex: vertex, consume: [SurveyNameWidths]]};
pane.length ← index;
pane.asViewer ← ViewerOps.CreateViewer[
flavor: paneFlavor,
info: [
parent: browser.container,
name: IF parent # NIL THEN GetChildName[parent, myIndex] ELSE "",
data: pane,
ww: browser.container.cw,
scrollable: FALSE],
paint: FALSE];
LayoutPane[pane, paint];
END;
LayoutPane: PROC [pane: Pane, paint: BOOLEAN] =
BEGIN
index: CARDINAL ← 0;
paneWidth: REAL ← 0;
pane.columns ← MAX[1, Real.FixC[(paneWidth ← MAX[pane.asViewer.cw, 100]) / MAX[pane.maxNameWidth, 1]]];
pane.columnWidth ← paneWidth / pane.columns;
pane.rows ← ((pane.length + pane.columns - 1) / pane.columns);
pane.height ← pane.rows * dy;
FOR index IN [0 .. pane.length) DO
leftX, bottomY: REAL;
xmin,ymin,xmax,ymax: REAL;
leftX ← pane.columnWidth * (index MOD pane.columns);
bottomY ← pane.height + 1 - (1 + index / pane.columns) * dy;
[[xmin,ymin,xmax,ymax]] ← ImagerBox.BoxFromExtents[font.RopeBoundingBox[pane.entries[index].name]];
pane.entries[index].drawX ← leftX - xmin;
pane.entries[index].drawY ← bottomY - ymin;
ENDLOOP;
ViewerOps.MoveViewer[viewer: pane.asViewer, paint: paint,
x: pane.asViewer.wx,
y: IF pane.parent # NIL THEN pane.parent.asViewer.wy + pane.parent.asViewer.wh ELSE 0,
w: pane.asViewer.ww,
h: pane.height + pane.asViewer.wh - pane.asViewer.ch];
END;
LayoutBrowser: PROC [browser: Browser] =
BEGIN
topDown: LIST OF Pane ← NIL;
FOR rest: LIST OF Pane ← browser.panes, rest.rest WHILE rest # NIL DO
topDown ← CONS[rest.first, topDown];
ENDLOOP;
FOR topDown ← topDown, topDown.rest WHILE topDown # NIL DO
ViewerOps.MoveViewer[viewer: topDown.first.asViewer, paint: FALSE,
x: topDown.first.asViewer.wx,
y: topDown.first.asViewer.wy,
w: browser.container.cw,
h: topDown.first.asViewer.wh];
LayoutPane[topDown.first, FALSE];
ENDLOOP;
END;
SizeChange: PROC [self: Viewer] RETURNS [adjusted: BOOLFALSE] --ViewerClasses.AdjustProc-- =
BEGIN
browser: Browser ← NARROW[MJSContainers.GetClientData[self]];
IF browser.container = NIL THEN --we hope it's only because we're in initialization code-- RETURN;
IF (browser.width # browser.container.cw) THEN
BEGIN
browser.width ← browser.container.cw;
LayoutBrowser[browser];
END;
END;
SortBox: PROC [cb: CardBox] RETURNS [sorted: CardBox] =
BEGIN
[sorted.startCol, sorted.finishCol] ← MinMax[cb.startCol, cb.finishCol];
[sorted.startRow, sorted.finishRow] ← MinMax[cb.startRow, cb.finishRow];
END;
MinMax: PROC [a, b: CARDINAL] RETURNS [min, max: CARDINAL] =
{IF a <= b THEN RETURN [a, b] ELSE RETURN [b, a]};
PaintPane: PROC [self: Viewer, context: Imager.Context, whatChanged: REF, clear: BOOL] RETURNS [quit: BOOLFALSE] --ViewerClasses.PaintProc-- =
BEGIN
InvertEntry: PROC [index: CARDINAL] =
BEGIN
IF index IN [firstIndex .. afterLastIndex) THEN
BEGIN
xmin,ymin,xmax,ymax: REAL;
expanded: PaneEntry ← pane.entries[index];
[[xmin,ymin,xmax,ymax]] ← ImagerBox.BoxFromExtents[font.RopeBoundingBox[expanded.name]];
context.MaskRectangle[[
x: xmin + expanded.drawX, w: xmax - xmin,
y: ymin + expanded.drawY, h: ymax - ymin]];
END;
END;
InvertBox: PROC [cb: CardBox] =
BEGIN
context.MaskRectangle[[
x: cb.startCol * pane.columnWidth,
w: (cb.finishCol+1 - cb.startCol) * pane.columnWidth,
y: pane.height - (cb.finishRow+1) * dy,
h: (cb.finishRow+1 - cb.startRow) * dy]];
END;
pane: Pane ← NARROW[self.data];
box: Box ← [xmin: 0, ymin: 0, xmax: self.cw, ymax: self.ch];
firstRow: CARDINAL ← Real.FixC[MAX[pane.height - box.ymax, 0] / dy];
lastRow: CARDINAL ← Real.FixC[MAX[pane.height - box.ymin, 0] / dy];
firstIndex: CARDINAL ← firstRow * pane.columns;
afterLastIndex: CARDINALMIN[pane.length, (lastRow+1) * pane.columns];
IF clear THEN
BEGIN
context.SetFont[font];
FOR index: CARDINAL IN [firstIndex .. afterLastIndex) DO
entry: PaneEntry ← pane.entries[index];
context.SetXY[[x: entry.drawX, y: entry.drawY]];
context.ShowRope[entry.name];
ENDLOOP;
context.SetColor[ImagerBackdoor.invert];
InvertEntry[pane.expandedV ← pane.expanded];
InvertBox[SortBox[pane.vSel ← pane.sel]];
END
ELSE BEGIN
context.SetColor[ImagerBackdoor.invert];
IF pane.expandedV # pane.expanded THEN
{InvertEntry[pane.expandedV]; InvertEntry[pane.expandedV ← pane.expanded]};
IF pane.vSel # pane.sel THEN
{InvertBox[SortBox[pane.vSel]]; InvertBox[SortBox[pane.vSel ← pane.sel]]};
END;
END;
Box: TYPE = RECORD [xmin, ymin, xmax, ymax: REAL];
NotifyPane: PROC [self: Viewer, input: LIST OF REF ANY] --ViewerClasses.NotifyProc-- =
BEGIN
op: ATOMNARROW[input.first];
coords: TIPUser.TIPScreenCoords;
ctled, shifted, paint: BOOLEANFALSE;
button: Menus.MouseButton;
pane: Pane ← NARROW[self.data];
row, col: CARDINAL;
sel: CardBox;
IF op = $Abort THEN
BEGIN
pane.sel ← badBox;
ViewerOps.PaintViewer[viewer: pane.asViewer, hint: client, clearClient: FALSE];
RETURN;
END;
coords ← NARROW[input.rest.first];
row ← Real.FixC[(pane.height - coords.mouseY) / dy];
row ← MIN[row, MAX[1, pane.rows]-1];
col ← Real.FixC[coords.mouseX / pane.columnWidth];
col ← MIN[col, MAX[1, pane.columns]-1];
IF op = $Start THEN
{pane.sel.startRow ← row; pane.sel.startCol ← col; paint ← TRUE};
IF pane.sel.finishRow # row OR pane.sel.finishCol # col THEN
{pane.sel.finishRow ← row; pane.sel.finishCol ← col; paint ← TRUE};
IF paint THEN ViewerOps.PaintViewer[viewer: pane.asViewer, hint: client, clearClient: FALSE];
IF op # $Finish THEN RETURN;
sel ← SortBox[pane.sel];
pane.sel ← badBox;
ctled ← SELECT input.rest.rest.first FROM $Ctl => TRUE, $NotCtl => FALSE, ENDCASE => ERROR;
shifted ← SELECT input.rest.rest.rest.first FROM $Shift => TRUE, $NotShift => FALSE, ENDCASE => ERROR;
button ← SELECT input.rest.rest.rest.rest.first FROM $Red => red, $Yellow => yellow, $Blue => blue, ENDCASE => ERROR;
IF sel.startRow = badRowCol OR sel.startCol = badRowCol OR sel.finishRow = badRowCol OR sel.finishCol = badRowCol THEN MessageWindow.Append[message: "Bad selection", clearFirst: TRUE]
ELSE IF pane.browser.class.cmds[button][ctled][shifted].proc # NIL THEN
BEGIN
MessageWindow.Append[message: NIL, clearFirst: TRUE];
IF pane.browser.class.fork THEN TRUSTED {Process.Detach[FORK Invoke[pane, sel, button, ctled, shifted]]} ELSE Invoke[pane, sel, button, ctled, shifted];
END
ELSE MessageWindow.Append[message: "Undefined click", clearFirst: TRUE];
END;
Invoke: PROC [pane: Pane, sel: CardBox, button: Menus.MouseButton, ctled, shifted: BOOLEAN] =
BEGIN
browser: Browser ← pane.browser;
bc: BrowserCmd ← pane.browser.class.cmds[button][ctled][shifted];
single: BOOLEAN ← sel.startRow = sel.finishRow AND sel.startCol = sel.finishCol;
IF sel = badBox THEN RETURN;
IF browser.class.serialize THEN GetSerial[browser];
BEGIN
ENABLE UNWIND => IF browser.class.serialize THEN ReleaseSerial[browser];
FOR col: CARDINAL IN [sel.startCol .. sel.finishCol] DO
IF col >= pane.columns THEN EXIT;
FOR row: CARDINAL IN [sel.startRow .. sel.finishRow] DO
index: CARDINAL;
IF row >= pane.rows THEN EXIT;
index ← row * pane.columns + col;
IF NOT index IN [0 .. pane.length) THEN
{IF single THEN MessageWindow.Append[message: "Missed", clearFirst: FALSE]}
ELSE BEGIN
bc.proc[pane: pane, index: index, cmdData: bc.cmdData, browserData: browser.browserData, button: button, ctl: ctled, shift: shifted, paint: TRUE];
END
ENDLOOP;
ENDLOOP;
END;
IF browser.class.serialize THEN ReleaseSerial[browser];
END;
GetSerial: ENTRY PROC [browser: Browser] =
BEGIN
WHILE browser.serial < 1 DO
WAIT browser.newSerial;
ENDLOOP;
browser.serial ← browser.serial - 1;
END;
ReleaseSerial: ENTRY PROC [browser: Browser] =
BEGIN
browser.serial ← browser.serial + 1;
BROADCAST browser.newSerial;
END;
GetPaneVertex: PUBLIC PROC [pane: Pane] RETURNS [vertex: Graphs.Vertex] = {RETURN [pane.vertex]};
GetChildName: PUBLIC PROC [pane: Pane, index: CARDINAL--origin 0--] RETURNS [name: ROPE] =
{RETURN [pane.entries[index].name]};
GetChildVertex: PUBLIC PROC [pane: Pane, index: CARDINAL--origin 0--] RETURNS [vertex: Graphs.Vertex] =
BEGIN
IF pane.entries[index].vertex = NIL THEN
BEGIN
IF pane.vertex.type.GetIndexedNeighbor # NIL THEN pane.entries[index].vertex ← pane.vertex.type.GetIndexedNeighbor[vertex: pane.vertex, index: index+1]
ELSE pane.entries[index].vertex ← pane.vertex.type.CrossLabeledEdge[vertex: pane.vertex, edgeLabel: GetChildName[pane, index]]
END;
RETURN [pane.entries[index].vertex];
END;
GetPaneUpEdge: PUBLIC PROC [pane: Pane] RETURNS [parent: Pane, index: CARDINAL--origin 0--] =
{RETURN [pane.parent, pane.myIndex]};
Expand: PUBLIC PROC [pane: Pane, index: CARDINAL--origin 0--, cmdData, browserData: REF ANY, button: Menus.MouseButton, ctl, shift, paint: BOOLEAN] =
BEGIN
childPane: Pane;
childVertex: Graphs.Vertex;
IF pane.expanded = index THEN RETURN;
IF pane.expanded IN [0 .. pane.length) THEN Contract[pane, pane.expanded, NIL, NIL, button, ctl, shift, FALSE];
pane.expanded ← index;
childVertex ← GetChildVertex[pane, index];
IF (childVertex.type.GetNeighborCount = NIL) OR (childVertex.type.GetNeighborCount[childVertex] > 0) THEN
BEGIN
childPane ← MakePane[browser: pane.browser, vertex: childVertex, parent: pane, myIndex: index, paint: paint];
pane.browser.panes ← CONS[childPane, pane.browser.panes];
END
ELSE IF paint THEN ViewerOps.PaintViewer[viewer: pane.browser.container, hint: client];
END;
Contract: PUBLIC PROC [pane: Pane, index: CARDINAL--origin 0--, cmdData, browserData: REF ANY, button: Menus.MouseButton, ctl, shift, paint: BOOLEAN] =
BEGIN
panes: LIST OF Pane;
IF index # pane.expanded THEN RETURN;
FOR panes ← pane.browser.panes, panes.rest WHILE panes.first # pane DO
ViewerOps.DestroyViewer[viewer: panes.first.asViewer, paint: FALSE];
ENDLOOP;
pane.browser.panes ← panes;
pane.expanded ← pane.length;
IF paint THEN ViewerOps.PaintViewer[viewer: pane.browser.container, hint: client];
END;
Setup: PROC =
BEGIN
ViewerOps.RegisterViewerClass[flavor: paneFlavor, class: paneClass];
MJSContainers.RegisterClass[viewerFlavor: $GraphBrowser, class: NEW [MJSContainers.MJSContainerClassRep ← [adjust: SizeChange]]];
END;
Setup[];
END.