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.
implement WmAltX;
include "sys.m";
include "draw.m";
include "tk.m";
include "wmlib.m";
sys: Sys;
draw: Draw;
Display, Image: import draw;
tk: Tk;
wmlib: Wmlib;
WmAltX: module {
init: fn(ctxt: ref Draw->Context, argv: list of string);
};
win_cfg := array[] of {
"frame .ft",
"text .ft.t -yscrollcommand {.ft.s set} -width 40w
-height 15h",
"scrollbar .ft.s -command {.ft.t yview}",
"focus .ft.t",
"pack .ft.s -side right -fill y",
"frame .fb.a -bd 2 -relief sunken",
"pack .ft.t -side left -fill y",
"button .fb.a.b -height 2h -text {Button 1}
-command {send cmd 1}",
"pack .fb.a.b -padx 1 -pady 1",
"button .fb.c -height 2h -text {Button 2}
-command {send cmd 2}",
"button .fb.q -height 2h -text {Quit}
-command {send cmd Q}",
"frame .fb",
"pack .fb.a .fb.c .fb.q -padx 5 -pady 5 -side left",
"pack .ft .fb",
"pack propagate . 0",
"update",
};
init(ctxt: ref Draw->Context, argv: list of string) {
sys = load Sys Sys->PATH;
draw = load Draw Draw->PATH;
tk = load Tk Tk->PATH;
wmlib = load Wmlib Wmlib->PATH;
wmlib->init();
(win, menubut) := wmlib->titlebar(ctxt.screen,
"-x 5 -y 5", "WmAltX", 0);
cmd := chan of string;
tk->namechan(win, cmd, "cmd");
wmlib->tkcmds(win, win_cfg);
for(;;) alt {
menu := <-menubut =>
if(menu[0] == 'e') {
tk->cmd(win, ".ft.t insert end '"+
"Close button pressed.\n");
tk->cmd(win, "update");
wmlib->dialog(win, "warning -fg red",
"Close Button", "Close Button pressed!", 0,
"Close"::nil);
exit;
}
wmlib->titlectl(win, menu);
s := <-cmd =>
case s[0] {
'1' =>
tk->cmd(win, ".ft.t insert end '"+
"Button 1 pressed.\n");
tk->cmd(win, "update");
break;
'2' =>
tk->cmd(win, ".ft.t insert end '"+
"Button 2 pressed.\n");
tk->cmd(win, "update");
break;
'Q' =>
tk->cmd(win, ".ft.t insert end '"+
"Quit button pressed.\n");
tk->cmd(win, "update");
wmlib->dialog(win, "info", "Quit Button",
"Quit button pressed!", 0,
"Quit"::nil);
exit;
}
}
}
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.