
# JStrack:  copyright 1997/2010 by Jim Graham, N5IAL, all rights reserved.

# Bindings for JStrack ... now moved to this file, where I can find them!

# Setup the correct bindings for Delete.  Tcl/Tk seems to think that the
# delete key is supposed to delete the NEXT char, not the previous char.
# With the following, we re-map DEL to BS, and it works the way one would
# expect it to.  If your backspace key sends BS, you're fine.  If your
# backspace key sends DEL, you're fine...after the following re-mapping.

foreach class {Entry Text} {
   bind $class <Key-Delete> [bind $class <Key-BackSpace>]
   bind $class <Control-h> [bind $class <Key-BackSpace>]
}


.c bind point <1> {
   foreach i [.c gettags current] {
      if {[regexp "(\[A-Z]+)(\[0-9]+/\[0-9]+Z)" $i a nm ts] == 1} {
         point_menu %X %Y $nm $ts
      }
   }
}

# no longer plotting watch/warnings ... just text

### .c bind WATCHWARN <Enter> {
###    foreach i [.c gettags current] {
###       if {[regexp {^IDTAG: (.*)$} $i a id]} {
###          set ptrstormid $id
###          .ptrloc.l3 configure -foreground $color(tsdot) -background black
###       }
###    }
### }
### 
### .c bind WATCHWARN <Leave> {
###    set ptrstormid ""
###    .ptrloc.l3 configure -foreground black -background #d9d9d9
### }


.c bind wind_radii <Enter> {
   foreach i [.c gettags current] {
      if {[regexp "(\[A-Z]+)(\[0-9]+/\[0-9]+Z)(\[0-9]+)" $i a nm ts w] == 1} {
         set ptrstormid "$w kt wind radii for $nm $ts"
         set c $color(tsdot)
# This is totally unreadable...stick to yellow/black
#        if {$w >= 64} { set c $color(hrdot) }

         .ptrloc.l3 configure -foreground $c -background black
      }
   }
}

.c bind wind_radii <Leave> {
   set ptrstormid ""
   .ptrloc.l3 configure -foreground black -background #d9d9d9
}

.c bind fc_radii <Enter> {
   foreach i [.c gettags current] {
      if {[regexp "(\[A-Z]+)(\[0-9]+/\[0-9]+Z)(\[0-9]+/\[0-9]+Z)(\[0-9]+)" \
                  $i a nm ts fcts w] == 1} {
         set ptrstormid "$w kt FORECAST wind radii for $nm $fcts ($ts)"
         set c $color(tsdot)
#        if {$w >= 64} { set c $color(hrdot) }

         .ptrloc.l3 configure -foreground $c -background black
      }
   }
}

.c bind fc_radii <Leave> {
   set ptrstormid ""
   .ptrloc.l3 configure -foreground black -background #d9d9d9
}

.c bind seas  <Enter> {
   foreach i [.c gettags current] {
      if {[regexp "(\[A-Z]+)(\[0-9]+/\[0-9]+Z)" $i a nm ts] == 1} {
         set ptrstormid "12 ft seas for $nm $ts"
         .ptrloc.l3 configure -foreground $color(12ft)
      }
   }
}

.c bind seas <Leave> {
   set ptrstormid ""
   .ptrloc.l3 configure -foreground black
}

bind .c <3> {
   global color
   catch { destroy .ms3 }
   set x [expr {[.c canvasx %X] - [winfo rootx .c]}]
   set y [expr {[.c canvasy %Y] - [winfo rooty .c]}]
   set x1 [expr {$x - 35}]
   set x2 [expr {$x + 35}]
   set y1 [expr {$y + 35}]
   set y2 [expr {$y - 35}]

   catch { .c delete DELETEME }
   .c create rectangle $x1 $y1 $x2 $y2 -tags DELETEME \
      -outline $color(b3box)

   toplevel .ms3
   wm geometry .ms3 +[expr {%X + 20}]+[expr {%Y + 20}]
   wm title .ms3 "Select Storm Position"
   wm attributes .ms3 -topmost true

   frame .ms3.lf
   pack .ms3.lf
   listbox .ms3.lf.list -height 10 -width 40 -yscroll ".ms3.lf.sb set"
   .ms3.lf.list configure -background white -foreground black
   scrollbar .ms3.lf.sb -command ".ms3.lf.list yview" -relief raised
   pack .ms3.lf.list -side left
   pack .ms3.lf.sb -side left -expand y -fill y

   frame .ms3.end
   pack .ms3.end -expand y -fill x
   button .ms3.end.done -text "Dismiss" -command {
      destroy .ms3
      .c delete DELETEME
   }
   button .ms3.end.ok -text "Get Selected Data" -command {
      .c delete DELETEME
      set idx [.ms3.lf.list curselection]
      if {[string compare $idx ""]} {
         set idxlist [.ms3.lf.list get $idx]
         set idxnm [lindex $idxlist 0]
         set idxts [lindex $idxlist 1]
         set xylist [.c coords $idxnm$idxts]

         set xoff [expr {int([.c canvasx %X]) - %X - 6}]
         set yoff [expr {int([.c canvasy %Y]) - %Y - 6}]

         set x [expr {int([lindex $xylist 0]) + int([winfo rootx .c]) - $xoff}]
         set y [expr {int([lindex $xylist 1]) + int([winfo rooty .c]) - $yoff}]
         eval point_menu $x $y $idxnm $idxts
      }
      destroy .ms3
   }
   pack .ms3.end.done .ms3.end.ok -side left

   set ms3list [list ""]
   foreach cidx [.c find overlapping $x1 $y1 $x2 $y2] {
      foreach i [lsort [.c gettags $cidx]] {
         if {[regexp "(\[A-Z]+)(\[0-9]+/\[0-9]+Z)" $i a nm ts] == 1} {
            if {[lsearch -exact $ms3list "$nm $ts"] == -1} {
               set name [string toupper $nm]
               set tmplist [split "$st_times($name)"]
               set lts [lindex $tmplist end]
               if {[string compare $ts $lts]} {
                  .ms3.lf.list insert end "$nm $ts"
               } else {
                  .ms3.lf.list insert end "$nm $ts (last position for $nm)"
               }
               lappend ms3list "$nm $ts"
            }
         }
      }
   }
   # See if we're off-screen, and if so, fix
   ck_win_onscreen .ms3
}


# Mac OS X seems to want reverse buttons 2 and 3
bind .c <2> {
   global color
   catch { destroy .ms3 }
   set x [expr {[.c canvasx %X] - [winfo rootx .c]}]
   set y [expr {[.c canvasy %Y] - [winfo rooty .c]}]
   set x1 [expr {$x - 35}]
   set x2 [expr {$x + 35}]
   set y1 [expr {$y + 35}]
   set y2 [expr {$y - 35}]

   catch { .c delete DELETEME }
   .c create rectangle $x1 $y1 $x2 $y2 -tags DELETEME \
      -outline $color(b3box)

   toplevel .ms3
   wm geometry .ms3 +[expr {%X + 20}]+[expr {%Y + 20}]
   wm title .ms3 "Select Storm Position"
   wm attributes .ms3 -topmost true

   frame .ms3.lf
   pack .ms3.lf
   listbox .ms3.lf.list -height 10 -width 40 -yscroll ".ms3.lf.sb set"
   .ms3.lf.list configure -background white -foreground black
   scrollbar .ms3.lf.sb -command ".ms3.lf.list yview" -relief raised
   pack .ms3.lf.list -side left
   pack .ms3.lf.sb -side left -expand y -fill y

   frame .ms3.end
   pack .ms3.end -expand y -fill x
   button .ms3.end.done -text "Dismiss" -command {
      destroy .ms3
      .c delete DELETEME
   }
   button .ms3.end.ok -text "Get Selected Data" -command {
      .c delete DELETEME
      set idx [.ms3.lf.list curselection]
      if {[string compare $idx ""]} {
         set idxlist [.ms3.lf.list get $idx]
         set idxnm [lindex $idxlist 0]
         set idxts [lindex $idxlist 1]
         set xylist [.c coords $idxnm$idxts]

         set xoff [expr {int([.c canvasx %X]) - %X - 6}]
         set yoff [expr {int([.c canvasy %Y]) - %Y - 6}]

         set x [expr {int([lindex $xylist 0]) + int([winfo rootx .c]) - $xoff}]
         set y [expr {int([lindex $xylist 1]) + int([winfo rooty .c]) - $yoff}]
         eval point_menu $x $y $idxnm $idxts
      }
      destroy .ms3
   }
   pack .ms3.end.done .ms3.end.ok -side left

   set ms3list [list ""]
   foreach cidx [.c find overlapping $x1 $y1 $x2 $y2] {
      foreach i [lsort [.c gettags $cidx]] {
         if {[regexp "(\[A-Z]+)(\[0-9]+/\[0-9]+Z)" $i a nm ts] == 1} {
            if {[lsearch -exact $ms3list "$nm $ts"] == -1} {
               set name [string toupper $nm]
               set tmplist [split "$st_times($name)"]
               set lts [lindex $tmplist end]
               if {[string compare $ts $lts]} {
                  .ms3.lf.list insert end "$nm $ts"
               } else {
                  .ms3.lf.list insert end "$nm $ts (last position for $nm)"
               }
               lappend ms3list "$nm $ts"
            }
         }
      }
   }
   # See if we're off-screen, and if so, fix
   ck_win_onscreen .ms3
}


bind .c <Motion> {
   if {$show_ptrloc} {
      set x [expr {[.c canvasx %X] - [winfo rootx .c]}]
      set y [expr {[.c canvasy %Y] - [winfo rooty .c]}]
      set latt [get_latt $y]
      set long [get_long $x]
      if {$latt < 0 || $long < 0} {
         set ptrlocstr "----- N ----- W"
      } else {
#        set ptrlocstr "$latt N (y=%Y) $long W (x=%X)"
         set ptrlocstr "$latt N $long W"
      }
   }
}


bind .c <Leave> {
   if {$show_ptrloc} { set ptrlocstr "----- N ----- W" }
}


