<commit_before>package provide Tkblt 3.0

namespace eval ::blt::legend {
    variable _private 
    array set _private {
	afterId ""
	scroll 0
	space off
	drag 0
	x 0
	y 0
    }
}

namespace eval ::blt::ZoomStack {
    variable _private
    array set _private {
	afterId ""
	scroll 0
	space off
	drag 0
	x 0
	y 0
    }
}

proc blt::legend::SetSelectionAnchor { w tagOrId } {
    set elem [$w legend get $tagOrId]
    # If the anchor hasn't changed, don't do anything
    if { $elem != [$w legend get anchor] } {
	$w legend selection clearall
	$w legend focus $elem
	$w legend selection set $elem
	$w legend selection anchor $elem
    }
}

# ----------------------------------------------------------------------
#
# MoveFocus --
#
#	Invoked by KeyPress bindings.  Moves the active selection to
#	the entry <where>, which is an index such as "up", "down",
#	"prevsibling", "nextsibling", etc.
#
# ----------------------------------------------------------------------
proc blt::legend::MoveFocus { w elem } {
    catch {$w legend focus $elem} result
    puts stderr "result=$result elem=$elem"
    if { [$w legend cget -selectmode] == "single" } {
        $w legend selection clearall
        $w legend selection set focus
	$w legend selection anchor focus
    }
}


proc Blt_ActiveLegend { g } {
    $g legend bind all <Enter> [list blt::ActivateLegend $g ]
    $g legend bind all <Leave> [list blt::DeactivateLegend $g]
    $g legend bind all <ButtonPress-1> [list blt::HighlightLegend $g]
}

proc Blt_Crosshairs { g } {
    blt::Crosshairs $g 
}

proc Blt_ResetCrosshairs { g state } {
    blt::Crosshairs $g "Any-Motion" $state
}

proc Blt_ZoomStack { g args } {
    array set params {
	-mode click
    }
    array set params $args
    if { $params(-mode) == "click" } {
	blt::ZoomStack::ClickClick $g
    } else {
	blt::ZoomStack::ClickRelease $g
    }	
}

proc Blt_ClosestPoint { g } {
    blt::ClosestPoint $g
}

#
# The following procedures that reside in the "blt" namespace are
# supposed to be private.
#

proc blt::ActivateLegend { g } {
    set elem [$g legend get current]
    $g legend activate $elem
}
proc blt::DeactivateLegend { g } {
    set elem [$g legend get current]
    $g legend deactivate $elem
}

proc blt::HighlightLegend { g } {
    set elem [$g legend get current]
    if { $elem != ""  } {
      set relief [$g element cget $elem -legendrelief]
      if { $relief == "flat" } {
	$g element configure $elem -legendrelief raised
	$g element activate $elem
      } else {
	$g element configure $elem -legendrelief flat
	$g element deactivate $elem
      }
   }
}

proc blt::Crosshairs { g {event "Any-Motion"} {state "on"}} {
    $g crosshairs $state
    bind crosshairs-$g <$event>   {
	%W crosshairs configure -position @%x,%y 
    }
    bind crosshairs-$g <Leave>   {
	%W crosshairs off
    }
    bind crosshairs-$g <Enter>   {
	%W crosshairs on
    }
    $g crosshairs configure -color red
    if { $state == "on" } {
	blt::AddBindTag $g crosshairs-$g
    } elseif { $state == "off" } {
	blt::RemoveBindTag $g crosshairs-$g
    }
}

proc blt::ClosestPoint { g {event "Control-ButtonPress-2"} } {
    bind closest-point-$g <$event>  {
	blt::FindElement %W %x %y
    }
    blt::AddBindTag $g closest-point-$g
}

proc blt::AddBindTag { widget tag } {
    set oldTagList [bindtags $widget]
    if { [lsearch $oldTagList $tag] < 0 } {
	bindtags $widget [linsert $oldTagList 0  $tag]
    }
}

proc blt::RemoveBindTag { widget tag } {
    set oldTagList [bindtags $widget]
    set index [lsearch $oldTagList $tag]
    if { $index >= 0 } {
	bindtags $widget [lreplace $oldTagList $index $index]
    }
}

proc blt::FindElement { g x y } {
    array set info [$g element closest $x $y -interpolate yes]
    if { ![info exists info(name)] } {
	beep
	return
    }
    # --------------------------------------------------------------
    # find(name)		- element Id
    # find(index)		- index of closest point
    # find(x) find(y)		- coordinates of closest point
    #				  or closest point on line segment.
    # find(dist)		- distance from sample coordinate
    # --------------------------------------------------------------
    set markerName "bltClosest_$info(name)"
    catch { $g marker delete $markerName }
    $g marker create text $markerName \
	-coords "$info(x) $info(y)" \
	-text "$info(name): $info(dist)\nindex $info(index)" \
	-anchor center -justify left \
	-yoffset 0 -bg {} 

    set coords [$g invtransform $x $y]
    set nx [lindex $coords 0]
    set ny [lindex $coords 1]

    $g marker create line line.$markerName -coords "$nx $ny $info(x) $info(y)"

    blt::FlashPoint $g $info(name) $info(index) 10
    blt::FlashPoint $g $info(name) [expr $info(index) + 1] 10
}

proc blt::FlashPoint { g name index count } {
    if { $count & 1 } {
        $g element deactivate $name 
    } else {
        $g element activate $name $index
    }
    incr count -1
    if { $count > 0 } {
	after 200 blt::FlashPoint $g $name $index $count
	update
    } else {
	catch {eval $g marker delete [$g marker names "bltClosest_*"]}
    }
}


proc blt::ZoomStack::Init { g } {
    variable _private
    set _private($g,interval) 100
    set _private($g,afterId) 0
    set _private($g,A,x) {}
    set _private($g,A,y) {}
    set _private($g,B,x) {}
    set _private($g,B,y) {}
    set _private($g,stack) {}
    set _private($g,corner) A
}

proc blt::ZoomStack::ClickClick { g {start "ButtonPress-1"} {reset "ButtonPress-3"} } {
    variable _private
    
    Init $g
    
    bind zoom-$g <Enter> "focus %W"
    bind zoom-$g <KeyPress-Escape> { blt::ZoomStack::Reset %W }
    bind zoom-$g <${start}> { blt::ZoomStack::SetPoint %W %x %y }
    bind zoom-$g <${reset}> { 
	if { [%W inside %x %y] } { 
	    blt::ZoomStack::Reset %W 
	}
    }
    blt::AddBindTag $g zoom-$g
}

proc blt::ZoomStack::ClickRelease { g } {
    variable _private
    
    Init $g
    bind zoom-$g <Enter> "focus %W"
    bind zoom-$g <KeyPress-Escape> {blt::ZoomStack::Reset %W}
    bind zoom-$g <ButtonPress-1> {blt::ZoomStack::DragStart %W %x %y}
    bind zoom-$g <B1-Motion> {blt::ZoomStack::DragMotion %W %x %y}
    bind zoom-$g <ButtonRelease-1> {blt::ZoomStack::DragFinish %W %x %y}
    bind zoom-$g <ButtonPress-3> { 
	if { [%W inside %x %y] } { 
	    blt::ZoomStack::Reset %W 
	}
    }
    blt::AddBindTag $g zoom-$g
}

proc blt::ZoomStack::GetCoords { g x y index } {
    variable _private
    if { [$g cget -invertxy] } {
	set _private($g,$index,x) $y
	set _private($g,$index,y) $x
    } else {
	set _private($g,$index,x) $x
	set _private($g,$index,y) $y
    }
}

proc blt::ZoomStack::MarkPoint { g index } {
    variable _private

    if { [llength [$g xaxis use]] > 0 } {
	set x [$g xaxis invtransform $_private($g,$index,x)]<commit_msg>*** empty log message ***<commit_after>package provide Tkblt 3.0

namespace eval ::blt::legend {
    variable _private 
    array set _private {
	afterId ""
	scroll 0
	space off
	drag 0
	x 0
	y 0
    }
}

namespace eval ::blt::ZoomStack {
    variable _private
    array set _private {
	afterId ""
	scroll 0
	space off
	drag 0
	x 0
	y 0
    }
}

proc blt::legend::SetSelectionAnchor { w tagOrId } {
    set elem [$w legend get $tagOrId]
    # If the anchor hasn't changed, don't do anything
    if { $elem != [$w legend get anchor] } {
	$w legend selection clearall
	$w legend focus $elem
	$w legend selection set $elem
	$w legend selection anchor $elem
    }
}

# ----------------------------------------------------------------------
#
# MoveFocus --
#
#	Invoked by KeyPress bindings.  Moves the active selection to
#	the entry <where>, which is an index such as "up", "down",
#	"prevsibling", "nextsibling", etc.
#
# ----------------------------------------------------------------------
proc blt::legend::MoveFocus { w elem } {
    catch {$w legend focus $elem} result
    puts stderr "result=$result elem=$elem"
    if { [$w legend cget -selectmode] == "single" } {
        $w legend selection clearall
        $w legend selection set focus
	$w legend selection anchor focus
    }
}


proc Blt_ActiveLegend { g } {
    $g legend bind all <Enter> [list blt::ActivateLegend $g ]
    $g legend bind all <Leave> [list blt::DeactivateLegend $g]
    $g legend bind all <ButtonPress-1> [list blt::HighlightLegend $g]
}

proc Blt_Crosshairs { g } {
    blt::Crosshairs $g 
}

proc Blt_ResetCrosshairs { g state } {
    blt::Crosshairs $g "Any-Motion" $state
}

proc Blt_ZoomStack { g args } {
    array set params {
	-mode click
	-button "ButtonPress-3"
    }
    array set params $args
    if { $params(-mode) == "click" } {
	blt::ZoomStack::ClickClick $g $params(-button)
    } else {
	blt::ZoomStack::ClickRelease $g $params(-button)
    }	
}

proc Blt_ClosestPoint { g } {
    blt::ClosestPoint $g
}

#
# The following procedures that reside in the "blt" namespace are
# supposed to be private.
#

proc blt::ActivateLegend { g } {
    set elem [$g legend get current]
    $g legend activate $elem
}
proc blt::DeactivateLegend { g } {
    set elem [$g legend get current]
    $g legend deactivate $elem
}

proc blt::HighlightLegend { g } {
    set elem [$g legend get current]
    if { $elem != ""  } {
      set relief [$g element cget $elem -legendrelief]
      if { $relief == "flat" } {
	$g element configure $elem -legendrelief raised
	$g element activate $elem
      } else {
	$g element configure $elem -legendrelief flat
	$g element deactivate $elem
      }
   }
}

proc blt::Crosshairs { g {event "Any-Motion"} {state "on"}} {
    $g crosshairs $state
    bind crosshairs-$g <$event>   {
	%W crosshairs configure -position @%x,%y 
    }
    bind crosshairs-$g <Leave>   {
	%W crosshairs off
    }
    bind crosshairs-$g <Enter>   {
	%W crosshairs on
    }
    $g crosshairs configure -color red
    if { $state == "on" } {
	blt::AddBindTag $g crosshairs-$g
    } elseif { $state == "off" } {
	blt::RemoveBindTag $g crosshairs-$g
    }
}

proc blt::ClosestPoint { g {event "Control-ButtonPress-2"} } {
    bind closest-point-$g <$event>  {
	blt::FindElement %W %x %y
    }
    blt::AddBindTag $g closest-point-$g
}

proc blt::AddBindTag { widget tag } {
    set oldTagList [bindtags $widget]
    if { [lsearch $oldTagList $tag] < 0 } {
	bindtags $widget [linsert $oldTagList 0  $tag]
    }
}

proc blt::RemoveBindTag { widget tag } {
    set oldTagList [bindtags $widget]
    set index [lsearch $oldTagList $tag]
    if { $index >= 0 } {
	bindtags $widget [lreplace $oldTagList $index $index]
    }
}

proc blt::FindElement { g x y } {
    array set info [$g element closest $x $y -interpolate yes]
    if { ![info exists info(name)] } {
	beep
	return
    }
    # --------------------------------------------------------------
    # find(name)		- element Id
    # find(index)		- index of closest point
    # find(x) find(y)		- coordinates of closest point
    #				  or closest point on line segment.
    # find(dist)		- distance from sample coordinate
    # --------------------------------------------------------------
    set markerName "bltClosest_$info(name)"
    catch { $g marker delete $markerName }
    $g marker create text $markerName \
	-coords "$info(x) $info(y)" \
	-text "$info(name): $info(dist)\nindex $info(index)" \
	-anchor center -justify left \
	-yoffset 0 -bg {} 

    set coords [$g invtransform $x $y]
    set nx [lindex $coords 0]
    set ny [lindex $coords 1]

    $g marker create line line.$markerName -coords "$nx $ny $info(x) $info(y)"

    blt::FlashPoint $g $info(name) $info(index) 10
    blt::FlashPoint $g $info(name) [expr $info(index) + 1] 10
}

proc blt::FlashPoint { g name index count } {
    if { $count & 1 } {
        $g element deactivate $name 
    } else {
        $g element activate $name $index
    }
    incr count -1
    if { $count > 0 } {
	after 200 blt::FlashPoint $g $name $index $count
	update
    } else {
	catch {eval $g marker delete [$g marker names "bltClosest_*"]}
    }
}


proc blt::ZoomStack::Init { g } {
    variable _private
    set _private($g,interval) 100
    set _private($g,afterId) 0
    set _private($g,A,x) {}
    set _private($g,A,y) {}
    set _private($g,B,x) {}
    set _private($g,B,y) {}
    set _private($g,stack) {}
    set _private($g,corner) A
}

proc blt::ZoomStack::ClickClick { g reset } {
    variable _private
    
    Init $g
    
    bind zoom-$g <Enter> "focus %W"
    bind zoom-$g <KeyPress-Escape> { blt::ZoomStack::Reset %W }
    bind zoom-$g <ButtonPress-1> { blt::ZoomStack::SetPoint %W %x %y }
    bind zoom-$g <${reset}> { 
	if { [%W inside %x %y] } { 
	    blt::ZoomStack::Reset %W 
	}
    }
    blt::AddBindTag $g zoom-$g
}

proc blt::ZoomStack::ClickRelease { g reset } {
    variable _private
    
    Init $g
    bind zoom-$g <Enter> "focus %W"
    bind zoom-$g <KeyPress-Escape> {blt::ZoomStack::Reset %W}
    bind zoom-$g <ButtonPress-1> {blt::ZoomStack::DragStart %W %x %y}
    bind zoom-$g <B1-Motion> {blt::ZoomStack::DragMotion %W %x %y}
    bind zoom-$g <ButtonRelease-1> {blt::ZoomStack::DragFinish %W %x %y}
    bind zoom-$g <${reset}> { 
	if { [%W inside %x %y] } { 
	    blt::ZoomStack::Reset %W 
	}
    }
    blt::AddBindTag $g zoom-$g
}

proc blt::ZoomStack::GetCoords { g x y index } {
    variable _private
    if { [$g cget -invertxy] } {
	set _private($g,$index,x) $y
	set _private($g,$index,y) $x
    } else {
	set _private($g,$index,x) $x
	set _private($g,$index,y) $y
    }
}

proc blt::ZoomStack::MarkPoint { g index } {
    variable _private

    if { [llength [$g xaxis use]] > 0 } {
	set x [$g xaxis invtransform $_private($g,$index,x)]
{'fraction_non_alphanumeric': 0.17444699267588962, 'fraction_numerical': 0.005844492121032774, 'mean_word_length': 2.511168831168831, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6391285', 'n_tokens_mistral': 4943, 'n_tokens_neox': 4744, 'n_words': 1417}