# mapicon.tcl -- By Ray R. Larson
# set icons on a map
# the routines in this file are used to 
# place icons on the map created by geobrowse.tcl and tkmap (or pgtkm)
# The map is currently drawn onto a canvas widget, and each routine
# needs the name of this widget as an argument.
# the icons are presently simple geometric figures

proc placemenuicon {mapwin latdeg latmin londeg lonmin zoommax zoommin xtratag newcmd menulabel} {
global tk_priv
global POSTEDICONLIST
global BITMAPPATH

    if {[winfo exists $mapwin.$xtratag]==0} {
       set xy [concat [getmapxy $mapwin $latdeg $latmin $londeg $lonmin]]
       set x [lindex $xy 0]
       set y [lindex $xy 1]
       if { ($x == -1) && ($y == -1) } { 
           # puts stderr "icon not in display area"
          return 
       }
       set currentzoom [.mp.scales1.zoom get]

       if { ($currentzoom > $zoommax) || ($currentzoom < $zoommin) } { 
          # puts stderr "icon not proper zoom level"
          return 
       }
       menubutton $mapwin.$xtratag -menu $mapwin.$xtratag.m \
          -bitmap @$BITMAPPATH/mapicon.off -borderwidth 0

       menu $mapwin.$xtratag.m 

       bind $mapwin.$xtratag  <Enter> \
          "set tk_priv(inMenuButton) %W ; %W config -bitmap \
          @$BITMAPPATH/mapicon.on "
  
       bind $mapwin.$xtratag  <Leave> \
          "set tk_priv(inMenuButton) {} ; %W config -bitmap \
          @$BITMAPPATH/mapicon.off "

       place $mapwin.$xtratag -in $mapwin -x $x -y $y
    
       lappend POSTEDICONLIST $mapwin.$xtratag
    }

    $mapwin.$xtratag.m add command -command "$newcmd" -label "$menulabel"
}

proc setmapmenuicons {} {
     global MAP_ICONS_ON
     global ICONLIST
     global POSTEDICONLIST

     if {$MAP_ICONS_ON} { # toggle them off 
         set MAP_ICONS_ON 0
	 .mp.buttons.dispicons config -text "Display Icons"
         foreach icon $POSTEDICONLIST {
            catch {destroy $icon}
         }
     } else {
         set MAP_ICONS_ON 1
	 .mp.buttons.dispicons config -text "Hide Icons"
         foreach icon $ICONLIST {
            placemenuicon [lindex $icon 0] [lindex $icon 1] [lindex $icon 2] [lindex $icon 3] [lindex $icon 4] [lindex $icon 5] [lindex $icon 6] [lindex $icon 7] [lindex $icon 8] [lindex $icon 9]
         }
         
     }     
}

proc addmapicon {mapwin latdeg latmin londeg lonmin zoommax zoommin tag command menulabel } {
     global ICONLIST 
     global MAP_ICONS_ON
     lappend ICONLIST [list $mapwin $latdeg $latmin $londeg $lonmin $zoommax $zoommin $tag $command $menulabel]
     if {$MAP_ICONS_ON} {
        placemenuicon  $mapwin $latdeg $latmin $londeg $lonmin $zoommax $zoommin $tag $command $menulabel
     }
}


proc removemapicons {} {
     global MAP_ICONS_ON
     global ICONLIST
     global POSTEDICONLIST

     if {$MAP_ICONS_ON} {
         foreach icon $POSTEDICONLIST {
            catch {destroy $icon}
         }
     set POSTEDICONLIST {}
     } 
}


proc destroymapicons {} {
     global MAP_ICONS_ON
     removemapicons
     set MAP_ICONS_ON 0
}


proc replacemapicons {} {
     global MAP_ICONS_ON
     global ICONLIST

     if {$MAP_ICONS_ON} {
         foreach icon $ICONLIST {
            placemenuicon [lindex $icon 0] [lindex $icon 1] [lindex $icon 2] [lindex $icon 3] [lindex $icon 4] [lindex $icon 5] [lindex $icon 6] [lindex $icon 7] [lindex $icon 8] [lindex $icon 9]
         }
     }
}



