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

User Interface Events Example

This example illustrates a common use of the alt statement. In the event model of the Limbo/Tk graphics environment, channels are used to send commands from graphic elements (widgets). Using the alt statement enables a program to respond to commands from multiple elements. (For more information about graphics programming in Limbo, see Graphics Programming starting on page 3-45.)

Although Program Listing 3-13 is longer than most examples, the use of the alt statement starts at Line 50.

Program Listing 3-13 wmaltx.b

  1. implement WmAltX;
  2. include "sys.m";
  3. include "draw.m";
  4. include "tk.m";
  5. include "wmlib.m";
  6. sys: Sys;
  7. draw: Draw;
  8. Display, Image: import draw;
  9. tk: Tk;
  10. wmlib: Wmlib;
  11. WmAltX: module {
  12. init: fn(ctxt: ref Draw->Context, argv: list of string);
  13. };
  14. win_cfg := array[] of {
  15. "frame .ft",
  16. "text .ft.t -yscrollcommand {.ft.s set} -width 40w
    -height 15h",
  17. "scrollbar .ft.s -command {.ft.t yview}",
  18. "focus .ft.t",
  19. "pack .ft.s -side right -fill y",
  20. "frame .fb.a -bd 2 -relief sunken",
  21. "pack .ft.t -side left -fill y",
  22. "button .fb.a.b -height 2h -text {Button 1}
    -command {send cmd 1}",
  23. "pack .fb.a.b -padx 1 -pady 1",
  24. "button .fb.c -height 2h -text {Button 2}
    -command {send cmd 2}",
  25. "button .fb.q -height 2h -text {Quit}
    -command {send cmd Q}",
  26. "frame .fb",
  27. "pack .fb.a .fb.c .fb.q -padx 5 -pady 5 -side left",
  28. "pack .ft .fb",
  29. "pack propagate . 0",
  30. "update",
  31. };
  32. init(ctxt: ref Draw->Context, argv: list of string) {
  33. sys = load Sys Sys->PATH;
  34. draw = load Draw Draw->PATH;
  35. tk = load Tk Tk->PATH;
  36. wmlib = load Wmlib Wmlib->PATH;
  37. wmlib->init();
  38. (win, menubut) := wmlib->titlebar(ctxt.screen,
    "-x 5 -y 5", "WmAltX", 0);
  39. cmd := chan of string;
  40. tk->namechan(win, cmd, "cmd");
  41. wmlib->tkcmds(win, win_cfg);
  42. for(;;) alt {
  43. menu := <-menubut =>
  44. if(menu[0] == 'e') {
  45. tk->cmd(win, ".ft.t insert end '"+
    "Close button pressed.\n");
  46. tk->cmd(win, "update");
  47. wmlib->dialog(win, "warning -fg red",
    "Close Button", "Close Button pressed!", 0,
    "Close"::nil);
  48. exit;
  49. }
  50. wmlib->titlectl(win, menu);
  51. s := <-cmd =>
  52. case s[0] {
  53. '1' =>
  54. tk->cmd(win, ".ft.t insert end '"+
    "Button 1 pressed.\n");
  55. tk->cmd(win, "update");
  56. break;
  57. '2' =>
  58. tk->cmd(win, ".ft.t insert end '"+
    "Button 2 pressed.\n");
  59. tk->cmd(win, "update");
  60. break;
  61. 'Q' =>
  62. tk->cmd(win, ".ft.t insert end '"+
    "Quit button pressed.\n");
  63. tk->cmd(win, "update");
  64. wmlib->dialog(win, "info", "Quit Button",
    "Quit button pressed!", 0,
    "Quit"::nil);
  65. exit;
  66. }
  67. }
  68. }

Line 50 starts the alt statement. This alternates between two channels: menubut and cmd. The menubut channel is used to notify the application of events from the titlebar, such as move and close. The cmd channel notifies the application of events from other widgets, such as the buttons at the bottom, Button 1, Button 2, and Quit.



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

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