
global vbi_caption_images

proc skip_args3 { a b c } {}

proc vbi_event_handler_scaled { a b } {
puts "$a $b"
switch -exact -- $a {
	"caption" {
		global vbi_caption_images
		foreach {canvas page} [array get vbi_caption_images] {
			puts "\t$canvas $page"
			if { $page != $b } { continue }
			vbi_draw_cc_page primary $page $canvas.vbi_image
			$canvas configure -width [image width $canvas.vbi_image] -height [image height $canvas.vbi_image]
			}
		}
	}
}

global vbi_window_count
set vbi_window_count 0

proc new_vbi_window {} {
global vbi_window_count vbi_caption_images
incr vbi_window_count
set W .vbi$vbi_window_count
set var caption_var$vbi_window_count
global $var
toplevel $W
label $W.l -text "CC"
canvas $W.c
image create photo $W.c.vbi_image
$W.c create image 1 1 -image $W.c.vbi_image -anchor nw
tk_optionMenu $W.page $var 1 2 3 4 5 6 7 8
grid x $W.l $W.page x -sticky news
grid $W.c - - - -sticky news
grid columnconfig $W 0 -weight 1
grid columnconfig $W 3 -weight 1
grid rowconfig $W 1 -weight 1
set vbi_caption_images($W.c) 1
vbi_draw_cc_page primary 1 $W.c.vbi_image
$W.c configure -width [image width $W.c.vbi_image] -height [image height $W.c.vbi_image]
trace variable $var w "puts \"+\" ; global vbi_caption_images $var; set vbi_caption_images($W.c) \[set $var\] ; skip_args3"
}

proc render_vbi_line { canvas tag width height columns rows line } {
foreach var {x y attr str} value $line {
	set $var $value
	}
set fontsize [expr $height/$rows]
foreach var {style effect size opacity foreground background} value [split $attr -] {
	set $var $value
	}
switch -exact -- $style {
	""  {
		set font [font create -family Helvetica -size -$fontsize -slant roman -underline false -weight normal]
		}
	"u"  {
		set font [font create -family Helvetica -size -$fontsize -slant roman -underline true -weight normal]
		}
	"b"  {
		set font [font create -family Helvetica -size -$fontsize -slant roman -underline false -weight bold]
		}
	"ub"  {
		set font [font create -family Helvetica -size -$fontsize -slant roman -underline true -weight bold]
		}
	"i"  {
		set font [font create -family Helvetica -size -$fontsize -slant italic -underline false -weight normal]
		}
	"ui"  {
		set font [font create -family Helvetica -size -$fontsize -slant italic -underline true -weight normal]
		}
	"bi"  {
		set font [font create -family Helvetica -size -$fontsize -slant italic -underline false -weight bold]
		}
	"ubi"  {
		set font [font create -family Helvetica -size -$fontsize -slant italic -underline true -weight bold]
		}
	}
set a [$canvas create text [expr ($x*$width)/$columns] [expr ($y*$height)/$rows] -font $font -text $str -anchor nw \
	-fill $foreground ]
$canvas addtag $tag withtag $a
set bbox [$canvas bbox $a]
set b [$canvas create rectangle $bbox -fill $background]
$canvas raise $a
$canvas addtag $tag withtag $b
}

proc render_cc_page { canvas page } {
set cc_page [vbi_get_cc_page primary $page]
set width [lindex $cc_page 0]
set height [lindex $cc_page 1]
.video delete cc_text
foreach line [lrange $cc_page 2 end] {
	render_vbi_line $canvas cc_text [winfo width $canvas] [winfo height $canvas] $width $height $line
	}
}

proc vbi_event_handler_on_screen {a b} {
catch {
switch -exact -- $a {
	"caption"  {	
	        # signal that it is likely that we have received
		# data for the particular CC channel
		.video_menu.cc_menu entryconfigure $b -foreground #008000 -label "$b (available)"
		global on_screen_cc_page
		if { $b != $on_screen_cc_page } { 
			continue 
			}
		render_cc_page .video $b
		}
	default {
		puts "vbi event $a $b"
		}
	}
	} err 
if { $err != "" } {
	puts "vbi_err=$err"
	}
}

proc vbi_set_cc_page_on_screen { } {
global on_screen_cc_page
if { $on_screen_cc_page == "Off" } {
	vbi_close_device primary
	.video delete cc_text
	return
	}
global primary_vbi_device on_screen_page
if { ! [info exists primary_vbi_device] } { update_primary_globals } 
if { $primary_vbi_device == "" } { update_primary_globals }
catch {
	vbi_open_device primary $primary_vbi_device	
	vbi_set_event_handler primary vbi_event_handler_on_screen
	render_cc_page .video $on_screen_cc_page
	} err
if { $err != "" } {
	puts "vbi_sec_cc_page_on_screen_err=$err"
	}
}

#vbi_open_device primary /dev/vbi0
#vbi_set_event_handler primary vbi_event_handler_on_screen

global on_screen_cc_page
set on_screen_cc_page "Off"
