# <20180406.1216.32>
#
# This is a simple routine to keep track of images such
# that they only need to be build once in the life of
# the program.
#
# The user should call with the image building material
# i.e. the image create info type, name and data or file
# ([list -data string] or [list -file @<filename>]
#
# This code checks if the image exists in any given namespace
# and if found returns it. If not found, it is created
# in namespace "images".

# The return is designed to  be used in a widget setup something
# like:
# button $w.newbut {*}[getImage bitmap imagename @file] ... other options.
#
proc getImage {type name args} {
  foreach image [image names] {
    if {[namespace tail $image] == $name ||\
	    $image == $name} {
      return [list -image $image]
    }
  }
  # does not yet exist.
  # If user is providing a namespace, defer to him
  #
 
  if {[namespace tail $name] == $name} {
    # no namespace, use image
    namespace eval images {}
    set name ::images::$name
  }
  # lets simplify the call to us
  if {[string index $type 0] == "-"} {
    set type [string range $type 1 end]
  }
  if {[llength $args] == 1} {
    set args {*}$args
    if {[string index $args 0] == "@"} {
      set args [list -file [string range $args 1 end]]
    } else {
      set args [list -data $args]
    }
  }
  # puts "$args $type $name"
  image create $type $name {*}$args
  return [list -image $name]
}
