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

# Experimental code to plot city names.

proc new_city {city lat long} {
   global city_coords
   set city_coords($city) [list $lat $long]
}


proc laddcity {s} { addcity $s 1 }

proc addcity {city {setleft 0}} {
   global user_cities lnamelist

   lappend user_cities $city
   if {$setleft} { lappend lnamelist $city }
}


# rmcities clears the map of city names
proc rmcities {} {
   global cities_loaded
   .c delete withtag cityname
   set cities_loaded 0
}

# TODO:
# * Use airport codes for unique identifiers for each city name plotted.
# * In add/remove cities code, make it a set of radiobuttons for each
#   city name:  left ( )   right ( ) Z do not plot (*)
#   When the radiobuttons change value, the city name is plotted/removed
#   accordingly.
#
# plotcity plots the city name at the specified lat/long (if on map)
proc plotcity {name lat long} {
   global color loadsat lnamelist

   set fill white
   if {$loadsat} { set fill #ffff00 }
   set font {Courier 12 bold}

   if {![valid_coords $lat $long]} { return -1 }

   set y [set_latt $lat]
   set x [set_long $long]
   if {[lsearch -exact $lnamelist $name] < 0} {
      set item [.c create text $x $y -text "* $name" -anchor w \
                              -fill $fill -font $font]
   } else {
      set item [.c create text $x $y -text "$name *" -anchor e \
                              -fill $fill -font $font]
   }
   addtags $item cityname
   return 0
}


# plot_user_cities plots the selected list of city names

proc plot_user_cities {} {
   global user_cities city_coords install_dir cities_loaded lnamelist

   # start with a clean chart/empty variables
   rmcities
   set user_cities [list]
   set lnamelist [list]

   # Now load what the user has asked for
   set cities_loaded 1
   source $install_dir/tracker/config_citydefs.tcl
   source $install_dir/tracker/config_user_citylist.tcl

   set not_valid [list]
   foreach city $user_cities {
      foreach {lat long} $city_coords($city) { break }
      if {[plotcity $city $lat $long] == -1} { lappend not_valid $city }
   }
   if {[llength $not_valid]} {
      set invstr ""
      foreach inv $not_valid { append invstr $inv\n }
      tk_messageBox -type ok -message "The following cities were not plotted:
because they are not valid for this map:

$invstr
      "
   }
}


