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

Limbo/Tk Example

The following is the version of Greet program that simply illustrates the use of the Limbo/Tk modules:

Program Listing 3-16 tkgreet.b

  1. implement TkGreet;
  2. include "sys.m";
  3. include "draw.m";
  4. include "tk.m";
  5. include "wmlib.m";
  6. sys: Sys;
  7. tk: Tk;
  8. wmlib: Wmlib;
  9. msg: string;
  10. TkGreet : 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. tk = load Tk Tk->PATH;
  16. wmlib = load Wmlib Wmlib->PATH;
  17. wmlib->init();
  18. if ((tl argv) != nil)
  19. msg = "Hello, " + (hd(tl argv)) + "!";
  20. else
  21. msg = "Hello, World!";
  22. win_cfg := array [] of {
  23. "label .i -bitmap @/icons/logon.bit",
  24. "label .t -text {" + msg + "\n" + "}",
  25. "focus .t",
  26. "button .b -text {OK} -width 10h
    -command {send cmd O}",
  27. "pack .i .t .b -padx 5 -pady 5",
  28. "pack propagate . 0",
  29. "update",
  30. };
  31. (title, menubut) := wmlib->titlebar(ctxt.screen,
    "-x 50 -y 25", "Limbo/Tk Example", wmlib->Hide);
  32. cmd := chan of string;
  33. tk->namechan(title, cmd, "cmd");
  34. wmlib->tkcmds(title, win_cfg);
  35. for(;;) alt {
  36. click := <- menubut =>
  37. if (click[0] == 'e')
  38. return;
  39. wmlib->titlectl(title, click);
  40. if (click[0] == 't')
  41. tk->cmd(title, "focus .t");
  42. s := <- cmd =>
  43. case s[0] {
  44. 'O' => exit;
  45. }
  46. }
  47. }

Line 21 initializes the display in preparation for drawing to it.

Lines 28 through 36 define the window configuration that will be displayed. This is an array of string elements that resemble Tcl commands. Creating the array is a convenient way to compile the widgets that make up a window, or some portion of the display.

Line 38 defines the text and buttons that display in the titlebar, as well as the starting coordinates (as a command string).

The Wmlib.titlebar function returns a tuple that contains a reference to the top-level widget and a channel that is used to notify the program of mouse events on the titlebar.

Lines 40 and 41 set up the channel that is used to communicate between the widgets and the program. This allows Limbo programs to receive event notification.

Line 43 creates the window using the array of string that defined the window as the commands.

Lines 45 through 56 is an infinite loop that processes widget events. The alt statement alternates between two channels: menubut for titlebar button events, and cmd for commands from other widgets (in this case, the OK button).

After compiling this program, you could run it from the Inferno shell in the Window Manager (wm):

	$ tkgreet Inferno



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

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