# Copyright (C) 2004  Melissa K. Schrumpf
# 
# This software may be distributed freely, provided that it is
# distributed in its entirety, without modifications, and with
# the original copyright notice and license included.
# 
# You may distribute your own separate patches together with
# this software package or a modified package if you always
# include a pointer where to get the original unmodified package.
# In this case you must inform the author about the modified package.
# 
# The software may not be sold for profit or as "hidden" part of 
# another software, but it may be included with collections 
# of other free software, such as CD-ROM images of FTP servers
# and similar, provided that this software is not a significant part 
# of that collection.  In this case you must inform the author of the
# method of distribution.
# 
# Pre-compiled binaries of this software may be distributed in the
# same way, provided that this copyright notice and license is
# included without modification.
# 
# This software may be used freely, for personal and non-profit
# use, provided that the original author is always credited.  If you
# intend to use this software as a significant part of business
# (for-profit) activities, the author's permission is required.  Also,
# any usage that is not covered by this license requires the explicit
# permission of the author.
# 
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
# 
# 
# 
set ::msglist {
	{random msg 1}
	{random msg 2}
	{random msg 3}
	{random msg 4}
	{random msg 5}
	{random msg 6}
	{random msg 7}
	{random msg 8}
	{random msg 9}
	{random msg 10}
	{random msg 11}
	{random msg 12}
}

set ::n 0

button .s -text spawn -command spawn
pack .s

proc spawn {} {
	toplevel .nw$::n
	set ::bannermsg($::n) "this is message #$::n"
	set ::bannertext($::n) ""
	set ::scanoff($::n) 0
	label .nw$::n\.pathto -textvariable ::bannertext($::n) -font {Terminal 6}
	pack .nw$::n\.pathto
	after 100 "scan $::n [expr round(10*rand())]"
	incr ::n
}



proc scan {n N} {
	set insr [string range $::bannermsg($n) 0 $::scanoff($n)]
	if {[string length $insr]==[string length $::bannermsg($n)]} {
		for {set i 0} {$i<=[expr $::scanoff($n)-[string length $::bannermsg($n)]]} {incr i} {
			set insr "$insr "
		}
	}
	while {[string length $insr]<=80} {
		set insr " $insr"
	}
	if {[string length $insr]>80} {
		set insr [string range $insr [expr [string length $insr]-79] end]
	}
	set ::bannertext($n) $insr
	incr ::scanoff($n)
	if {$::scanoff($n)>[expr 80+[string length $::bannermsg($n)]]} {
		set ::scanoff($n) 0
		incr N -1
		if {$N<0} {
			set rmi [format %0.0f [expr rand()*[llength $::msglist]]]
			set ::bannermsg($n) [lindex $::msglist $rmi]
			set N [expr round(10*rand())]
		}
	}
	if {[winfo exists .nw$n]} {
		after 100 "scan $n $N"
	}
}

