# window manager, window placement and menubar testing
load tk expr
# execute a tk command; print a warning if it's failed
fn x {
a := $*;
or {tk $wid $a} {
echo error on tk cmd $"a':' $status >[1=2]
}
}
# print rectangles of all on-screen windows.
fn winrects {
wids := ${tk windows -v}
for i in $wids {
x := ${tk $i . cget -actx}
y := ${tk $i . cget -acty}
w := ${tk $i . cget -width}
h := ${tk $i . cget -height}
bd := ${expr ${tk $i . cget -bd} 2 x}
echo $x $y ${expr $x $w $bd + + $y $h $bd + +}
}
tk del $wids
}
# create a new blank window at the specified rectangle.
fn newwin {
args=$*
or {~ $#args 5} {echo 'usage: newwin title minx miny maxx maxy' >[1=2]; raise usage}
(title minx miny maxx maxy) := $*
flag i -
{
unload tk; load tk
wid:=${tk window ''}
pctl newpgrp
x pack propagate . 0
(x . configure
-x $minx -y $miny
-width ${expr $maxx $minx -}
-height ${expr $maxy $miny - }
)
x frame .f -bd 2 -relief sunken
x pack .f -side top -fill both -expand 1
chan cmd; tk namechan $wid cmd
x bind .f '<Button-2>' {send cmd grow}
tk wintitle $wid $title
x update
while {} {
(c v) := ${alt $wid cmd}
(if {~ $c cmd} {setwinrect `{growplace ${winrect}}}
{tk winctl $wid $v}
)
}
} &
}
# get rectangle of a window; id in $wid
subfn winrect {
or {~ $#wid 1} {echo 'winrect needs wid' >[1=2]; raise usage}
x := ${tk $wid . cget -actx}
y := ${tk $wid . cget -acty}
w := ${tk $wid . cget -width}
h := ${tk $wid . cget -height}
bd := ${expr ${tk $wid . cget -bd} 2 x}
result = $x $y ${expr $x $w $bd + + $y $h $bd + +}
}
# set the rectangle of a window; id in $wid
fn setwinrect {
(minx miny maxx maxy) := $*
or {~ $#maxy 1} {echo 'usage: setwinrect rect' >[1=2]; raise usage}
bd := ${expr ${tk $wid . cget -bd} 2 x}
(x . configure
-x $minx
-y $miny
-width ${expr $maxx $minx - $bd -}
-height ${expr $maxy $miny - $bd -}
)
x update
}
# create a window at the rectangle printed by maxplace
fn maxwin {
p := ${getpoint}
r := `{maxplace $p}
if {~ $#r 0} {
echo no space there
} {
newwin Max $r
}
}
# get the user to click somewhere on screen, and yield
# point clicked on.
subfn getpoint {
wid := ${tk window Hello}
x update
chan xx
tk namechan $wid xx
x . unmap
x 'bind . <Button-1> {send xx %X %Y}'
x grab set .
x cursor -bitmap cursor.drag
result = ${split ' ' ${recv xx}}
x cursor -default
x grab release .
tk del xx $wid
}
# test the menubar app, which has created a file2chan file
# at /chan/wmmenu.
# writes to /chan/wmmenu create menu items.
# reads of /chan/wmmenu return when menu items are activated.
fn startmenu {
<>[7] /chan/wmmenu {
echo Start Up >[1=7]
{
x={echo ${quote $*}}
$x add item .sh Shell 'wm/sh 542 297'
$x add item .ch Charon 'charon 630 470'
$x add item .date Date 'wm/date 160 36'
$x add item .max Max max
$x add item .quit Quit quit
} >[1=7]
lastrect=()
getlines <[0=7] {
(nil app size) := ${split ' ' $line}
if {~ $app quit} {
echo bye bye
raise break
} {~ $app max} {
{unload tk; load tk; maxwin} &
} {
lastrect= `{wplace $size $lastrect}
newwin $app $lastrect >[7=0]
}
}
}
}
|