The following is the version of Greet program that simply illustrates the use of the Prefab module:
Program Listing 3-15 prefabgreet.b
implement PrefabGreet;
include "sys.m";
include "draw.m";
include "prefab.m";
sys: Sys;
draw: Draw;
Display, Font, Rect, Point, Image, Screen: import draw;
prefab: Prefab;
Style, Element, Compound, Environ: import prefab;
PrefabGreet : module {
init: fn(ctxt: ref Draw->Context, argv: list of string);
};
init(ctxt: ref Draw->Context, argv: list of string) {
sys = load Sys Sys->PATH;
draw = load Draw Draw->PATH;
prefab = load Prefab Prefab->PATH;
display := Display.allocate(nil);
disp := display.image;
spawn refresh(display);
white := display.color(Draw->White); #predefined color
yellow := display.color(Draw->Yellow); #predefined color
black := display.color(Draw->Black); #predefined color
grey := display.rgb(160,160,160);
ones := display.ones;
screen := Screen.allocate(disp, white, 1);
textfont := Font.open(display,
"/fonts/lucidasans/unicode.13.font");
titlefont := Font.open(display,
"/fonts/lucidasans/italiclatin1.10.font");
win_style := ref Style(
titlefont, # title font
textfont, # text font
grey, # element color
black, # edge color
yellow, # title color
black, # text color
white); # highlight color
win := ref Environ(screen, win_style);
if ((tl argv) != nil)
msg := "Hello, " + (hd(tl argv)) + "!";
else
msg = "Hello, World!";
icon := display.open("/icons/lucent.bit");
dy := (icon.r.dy()-textfont.height)/2;
wintitle := Element.text(win, "Inferno Prefab Example",
((0,0),(0,0)), Prefab->ETitle);
ie := Element.icon(win, icon.r, icon, ones);
te := Element.text(win, msg, ((0,dy),(0,dy)),
Prefab->EText);
se:= Element.separator(win, ((0,0), (10,0)),
display.zeros, display.zeros);
le := Element.elist(win, ie, Prefab->EHorizontal);
le.append(se); # add space between icon and text
le.append(te);
le.append(se); # add space between text and border
le.adjust(Prefab->Adjpack, Prefab->Adjleft);
c:= Compound.box(win, (20,20), wintitle, le);
c.draw();
sys->sleep(10000); # Prefab elements are gc'd
}
refresh(display: ref Display) {
display.startrefresh();
}
Comparing this example to the Draw example, you see that much of the initialization code is essentially the same.
In addition to the the Display
, Image
, and Font
types, this example uses a Screen
object. Line 30 creates a new Screen object and stores it in screen
. This becomes the foundation upon which elements, such as windows, are drawn.
Lines 34 through 41 defines the font and color information for the window that will be displayed.
Line 43 creates the window environment, based on the screen and style that have been defined.
Line 52 defines the title of the window. Lines 53 and 54 define the icon and text that will be displayed. Line 55 defines the space that will be used to pad the elements.
Line 56 sets layout of the elements to be a horizontal list. Lines 57 through 59 add the elements to the list in the appropriate order. Line 60 formats the elements.
Line 62 defines the box, or the window, that will be drawn. It contains the title and contents. Line 63 actually draws the window.
After compiling this program, you could run it from the Inferno console:
inferno$ prefabgreet Inferno![]()