#!/bin/sh

# Alert: display a message and options for to reply
# this fancy line allows the "-g" option to bypass wish (-geom) \
exec wish8.5 "$0" -name alert -- "$@" 

# Steven.a's first tk script
# Modelled on beos' Alert 
# No , I don't really know what I'm doing  ;->
# 13/5/02 : patched up a bit.
#    todo : Try to set X window properties and identify (bash as) parent 
# 20/2/03 : centred window, use "for" not "while", bit of a tidy
# 21/2/03 : grab option, usage command

switch -regexp -- [lindex $argv 0] {
	 ^-g$ {
	   set grab 1
	   # shift args
	   incr argc -1
	   set argv [lreplace $argv 0 0]
	 }
	 ^- {
	   puts {Usage: alert [-g] message response(s)..} 
	   exit 0
	 }
	 default {
	   set grab 0
	   if {$argc < 2} { puts "Alert: too few args"; exit 1 }
	 }
}

# don't draw window till we have its geometry
wm state . withdrawn
# wm transient . .
### wtf ??? wm protocol . WM_DELETE_WINDOW { }

# init message and bitmap
label	.title	-justify left -text [lindex $argv 0]
label	.bitmap	-bitmap info -fg orange1
pack	.bitmap	-side left -pady .1c -anchor n
pack	.title	-side top 

# setup buttons
for {set j 1} {$j < $argc} {incr j} {
	set i [lindex $argv $j]
	# set i [string map {. _} $i]
	radiobutton .b$j -text $i -variable response -value $i \
	  -command { puts $response ; after 1000 {exit} } -activebackground lavender
	# the "after" stops the mouse click going throguh to a lower window
	pack .b$j -side left -pady .1c -padx .1c 
}

# centrally position window
update
set x [expr ([winfo screenwidth .] - [winfo reqwidth .])/2]
set y [expr ([winfo screenheight .] - [winfo reqheight .])/2]
if { $x < 0 } {set x 0}

# wm geom . +$x+$y
wm state . normal
update

bind . <Destroy> {destroy .}
if {$grab} {grab -global .}
