#
# QCad
# (c) Q Solutions 1998
#
#
#
#
# Help processing
#
#

proc procHelpKey { name {gbl 0}} {
	upvar #0 $name data

	set c .help_$name.canvas
	Log "help win $c"
	$c delete all

	set i 1
	set x 10
	set y 10
	set item $data(edit,objItem)
	set type $data(edit,objType)
	set mode [lindex $data(Mode) end]
	if { $mode == "" } {set mode "-" }
	if { $item == "" } {set item "-" }
	if { $type == "" } {set type "N/a" }
	set anymode "*"
	set anyitem "*"
	if { $mode == "-" } {set anymode "?"}
	if { $item == "-" } {set anyitem "?" }
	set elist ""
	foreach {ev proc} [array get data event,*] {
		lappend elist [concat  [split $ev ,] [list $proc]] 
		#Log "Ev $ev: $proc"
	}
	Log "[join $elist \n]"
	set elist [lsort -index 3 $elist]
	foreach {xx evmode evitem event proc} [join $elist] {
		set found 0
		if { $gbl } {
			if { ($evmode == "#") } { set found 1}
		} else {
			if { ($mode == $evmode) && ($item == $evitem)} { set found 1}
			if { ($mode == $evmode) && ($evitem == "*")} { set found 1}
			if { ($evmode == "*") && ($item != "-" )} { set found 1}
		}

		if { ! $found } {continue}
		set help [dbGetHelp $proc]
		set proc [lindex $help 0]
		set help [lindex $help 1]
		$c create text $x $y -anc nw -text $event -tags "Text Row$i Col1"
		$c create text $x $y -anc nw -text $proc -tags "Text Row$i Col2"
		set tid [$c create text $x $y -anc nw -text  $help -width 15c -tags "Text Row$i Col3"]
		#eval $c create rectangle [$c bbox $tid] -tags Col3
		scan [$c bbox Row$i] "%f %f %f %f" x1 y1 x2 y2
		$c create line 0 $y2 2000 $y2 
		set y [expr ceil($y2 + 3)]
	}
	if { [$c bbox Col1] == "" } { return "" }
	scan [$c bbox Col1] "%f %f %f %f" x1 y1 x2 y2
	$c create line [expr $x2 + 5] 0 [expr $x2 +5] 2000
	set x [expr ceil($x2 + 1 + 5)]
	$c move Col2 $x 0
	scan [$c bbox Col2] "%f %f %f %f" x1 y1 x2 y2
	$c create line [expr $x2 + 5] 0 [expr $x2 + 5] 2000
	set x [expr ceil($x2 + 1 + 5)]
	$c move Col3 $x 0
	scan [$c bbox Text] "%f %f %f %f" x1 y1 x2 y2
	$c yview moveto 0
	$c xview moveto 0
	$c config -width [expr $x2 + 5]
	$c config -confine 1 -scrollregion [list 0 0 $x2 $y2]
}

proc procHelp { name } {
	upvar #0 $name data

	if { ! [winfo exists .help_$name] } {
		helpMakeWin $name .help_$name
		procHelpKey $name 0
		scan [wm geometry [winfo toplevel $data(Win)]] "%dx%d+%d+%d" w h x y
		wm geometry .help_$name +$x+$y
	}
	wm deiconify .help_$name
	raise .help_$name
}

proc helpMakeWin { name win } {

	toplevel $win
	pack [frame $win.top -bd 1 -relief raised] -side top -anc nw -fill x
	pack [label $win.top.title -text "Key Help for $name"] -anc c 
	pack [frame $win.f1 -bd 1 -relief raised] -side left -anc nw -fill x
	pack [frame $win.f2 -bd 1 -relief ridge] -side left -anc nw -fill both -expand 1
	pack [scrollbar $win.sc -command "$win.canvas yview"] -in $win.f2 -side left -anc nw -fill y
	pack [canvas $win.canvas -width 400 -height 500 -yscrollcommand "$win.sc set"] -in $win.f2 -side left -anc nw -fill both -expand 1
	pack [button $win.f1.b1 -text "Global" -command "procHelpKey $name 1"]	-fill x -anc nw
	pack [button $win.f1.b2 -text "Modal" -command "procHelpKey $name 0"]	-fill x -anc nw
	pack [button $win.f1.b3 -text "Hide" -command "wm withdraw $win"]	-fill x -anc nw
	bind $win.canvas <1> "wm withdraw $win"
	bind $win.canvas <Key> "wm withdraw $win"
	bind $win.canvas <Key-Return> "wm withdraw $win"
	focus $win
	focus $win.canvas
}

proc helpText {name topic} {

	set win .helptext
	if { ! [winfo exists $win] } {
		toplevel $win
		wm withdraw $win
		pack [frame $win.top -bd 1 -relief raised] -side top -anc nw -fill x
		#pack [label $win.top.title -text "Help on $topic"] -anc c 
		pack [frame $win.f1 -bd 1 -relief raised] -side left -anc nw -fill x
		pack [frame $win.f2 -bd 1 -relief ridge] -side left -anc nw -fill both -expand 1
		pack [scrollbar $win.sc -command "$win.help yview"] -in $win.f2 -side left -anc nw -fill y
		pack [text $win.help -width 40 -height 30 -yscrollcommand "$win.sc set" -wrap word] -in $win.f2 -side left -anc nw -fill both -expand 1
		pack [button $win.f1.b3 -text "Hide" -command "destroy $win"]	-fill x -anc nw
		#scan [wm geometry [winfo toplevel $data(Win)]] "%dx%d+%d+%d" w h x y
		#wm geometry $win +$x+$y
		$win.help tag configure Heading -foreground Yellow -font {Time 14 bold} -spacing1 .2c -spacing3 .3c
		$win.help tag configure Para -foreground Blue -font {Time 12 bold} -spacing1 .2c -spacing3 .3c
		$win.help tag configure Glo -foreground Red -lmargin1 0.2c -spacing1 .1c
		$win.help tag configure Dfn -lmargin1 0.7c -lmargin2 .7c -font {Time 12 italic}
		$win.help tag configure Text -lmargin1 0.2c -lmargin2 0.2c 
		$win.help tag configure End -foreground White -background White
		$win.help tag configure Site -foreground Red -lmargin1 0.2c -lmargin2 0.2c 
		$win.help tag configure A -foreground Blue
		bind $win.help <Prior> "$win.help yview scroll 5 units"
		bind $win.help <Next> "$win.help yview scroll 5 units"
		$win.help tag bind A <1> "helpTextMove $win %x %y"
		$win.help tag bind A <Enter> "$win.help configure -cursor hand2"
		$win.help tag bind A <Leave> "$win.help configure -cursor {}"

	}
	helpTextFill $win $topic
	wm deiconify $win
	raise $win
	focus $win.help
}

proc helpTextFill {$win topic} {

	global Help

	set win .helptext
	$win.help configure -state normal
	if { [info exists Help(HelpText,$topic)] } {
		$win.help delete 1.0 end
		foreach {tag text} $Help(HelpText,$topic) {
			$win.help insert end $text $tag
		}
	}
	$win.help configure -state disabled
	wm title $win "\[ $topic ]"
}

proc helpTextMove {win x y} {

	set topic [eval $win.help get [$win.help tag prevrange A [$win.help index @$x,$y]]]
	helpTextFill $win $topic
}


