[Contents]
[Prev] [Next] [Limbo Basics] [Limbo Programming] [Language Definition]

Prefab Example

The following is the version of Greet program that simply illustrates the use of the Prefab module:

Program Listing 3-15 prefabgreet.b

  1. implement PrefabGreet;
  2. include "sys.m";
  3. include "draw.m";
  4. include "prefab.m";
  5. sys: Sys;
  6. draw: Draw;
  7. Display, Font, Rect, Point, Image, Screen: import draw;
  8. prefab: Prefab;
  9. Style, Element, Compound, Environ: import prefab;
  10. PrefabGreet : module {
  11. init: fn(ctxt: ref Draw->Context, argv: list of string);
  12. };
  13. init(ctxt: ref Draw->Context, argv: list of string) {
  14. sys = load Sys Sys->PATH;
  15. draw = load Draw Draw->PATH;
  16. prefab = load Prefab Prefab->PATH;
  17. display := Display.allocate(nil);
  18. disp := display.image;
  19. spawn refresh(display);
  20. white := display.color(Draw->White); #predefined color
  21. yellow := display.color(Draw->Yellow); #predefined color
  22. black := display.color(Draw->Black); #predefined color
  23. grey := display.rgb(160,160,160);
  24. ones := display.ones;
  25. screen := Screen.allocate(disp, white, 1);
  26. textfont := Font.open(display,
    "/fonts/lucidasans/unicode.13.font");
  27. titlefont := Font.open(display,
    "/fonts/lucidasans/italiclatin1.10.font");
  28. win_style := ref Style(
  29. titlefont, # title font
  30. textfont, # text font
  31. grey, # element color
  32. black, # edge color
  33. yellow, # title color
  34. black, # text color
  35. white); # highlight color
  36. win := ref Environ(screen, win_style);
  37. if ((tl argv) != nil)
  38. msg := "Hello, " + (hd(tl argv)) + "!";
  39. else
  40. msg = "Hello, World!";
  41. icon := display.open("/icons/lucent.bit");
  42. dy := (icon.r.dy()-textfont.height)/2;
  43. wintitle := Element.text(win, "Inferno Prefab Example",
    ((0,0),(0,0)), Prefab->ETitle);
  44. ie := Element.icon(win, icon.r, icon, ones);
  45. te := Element.text(win, msg, ((0,dy),(0,dy)),
    Prefab->EText);
  46. se:= Element.separator(win, ((0,0), (10,0)),
    display.zeros, display.zeros);
  47. le := Element.elist(win, ie, Prefab->EHorizontal);
  48. le.append(se); # add space between icon and text
  49. le.append(te);
  50. le.append(se); # add space between text and border
  51. le.adjust(Prefab->Adjpack, Prefab->Adjleft);
  52. c:= Compound.box(win, (20,20), wintitle, le);
  53. c.draw();
  54. sys->sleep(10000); # Prefab elements are gc'd
  55. }
  56. refresh(display: ref Display) {
  57. display.startrefresh();
  58. }

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



[Contents]
[Prev] [Next] [Limbo Basics] [Limbo Programming] [Language Definition]

Copyright © 1998, Lucent Technologies, Inc. All rights reserved.