Plan 9 from Bell Labs’s /usr/web/sources/contrib/rog/sh-examples/wmtst2.sh

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


load tk expr

fn x {
	a := $*;
	or {tk $wid $a} {
		echo error on tk cmd $"a':' $status
	}
}

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
}

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}
			)
		}
	} &
}

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 + +}
}

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
}

fn maxwin {
	p := ${getpoint}
	r := `{maxplace $p}
	if {~ $#r 0} {
		echo no space there
	} {
		newwin Max $r
	}
}

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
}

fn findwin {
	args=$*
	or {~ $#args 2} {echo 'usage: findwin minx miny' >[1=2]; raise usage}
	or {~ $#lastrect 4} {lastrect=()}
	lastrect = r := `{winrects | rectfind $args $lastrect}
	if {~ $#r 0} {
		echo 'no space for window' >[1=2]
		status no win
	} {
		newwin '' $r
		echo $r
	}
}

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]
			}
		}
	}
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.