set cvs [canvas .c -width 500 -height 500 -background white]
grid $cvs -row 2 -column 1 -columnspan 7

set col 1

foreach type {line rectangle oval text polygon clear} {
  set w [button .b_$type -command "do_$type $cvs" -text "$type"]
  grid $w -row 1 -column $col
  incr col
}

set colors {red orange yellow green blue violet brown gray black}

################################################################
# proc do_clear {cvs}--
#    Delete all items on the canvas
# Arguments
#   cvs		The canvas to delete items from
# 
# Results
#   No side effects
# 
proc do_clear {cvs} {
  $cvs delete all
}


proc do_line {cvs} {
  global colors
  set x1 0
  set y1 0
  set x2 100
  set y2 100
  foreach color $colors {
    puts "    $cvs create line $x1 $y1 $x2 $y2 -fill $color"
    set id [$cvs create line $x1 $y1 $x2 $y2 -fill $color]
    $cvs bind $id <ButtonPress-1> \
        "display $cvs  line $x1 $y1 $x2 $y2 $color"
    set x1 [expr {(($x1+5)*23)%500}]
    set y1 [expr {(($y1+5)*37)%500}]
    set x2 [expr {(($x2+5)*41)%500}]
    set y2 [expr {(($y2+5)*43)%500}]
  }
}

proc do_rectangle {cvs} {
  global colors
  set x1 0
  set y1 0
  set x2 100
  set y2 100
  foreach color $colors {
    puts "    $cvs create rectangle $x1 $y1 $x2 $y2 -fill $color"
    set id [$cvs create rectangle $x1 $y1 $x2 $y2 -fill $color]
    $cvs bind $id <ButtonPress-1> \
        "display $cvs  rectangle $x1 $y1 $x2 $y2 $color"
    set x1 [expr {(($x1+5)*23)%500}]
    set y1 [expr {(($y1+5)*37)%500}]
    set x2 [expr {(($x2+5)*41)%500}]
    set y2 [expr {(($y2+5)*43)%500}]
  }
}

proc do_oval {cvs} {
  global colors
  set x1 0
  set y1 0
  set x2 100
  set y2 100
  foreach color $colors {
    puts "    $cvs create oval $x1 $y1 $x2 $y2 -fill $color"
    set id [$cvs create oval $x1 $y1 $x2 $y2 -fill $color]
    $cvs bind $id <ButtonPress-1> \
        "display $cvs oval $x1 $y1 $x2 $y2 $color"
    set x1 [expr {(($x1+5)*23)%500}]
    set y1 [expr {(($y1+5)*37)%500}]
    set x2 [expr {(($x2+5)*41)%500}]
    set y2 [expr {(($y2+5)*43)%500}]
  }
}


proc do_text {cvs} {
  global colors
  set x1 0
  set y1 0
  foreach color $colors {
    puts "    $cvs create text $x1 $y1 -text $color -fill $color"
    set id [$cvs create text $x1 $y1 -text $color -fill $color]
    $cvs bind $id <ButtonPress-1> \
        "display $cvs  text $x1 $y1 - - $color"
    set x1 [expr {(($x1+5)*23)%500}]
    set y1 [expr {(($y1+5)*37)%500}]
  }
}

proc display {cvs type x1 y1 x2 y2 color} {
  if {$x2 eq "-"} {
    incr y1 10
    $cvs create text $x1 $y1 -text \
      "This is a $color $type at $x1 $y1 "
  } else {
  set x [expr {($x1 + $x2) / 2}]
  set y [expr {($y1 + $y2) / 2}]
  $cvs create text $x $y -text \
      "This is a $color $type from $x1 $y1 to $x2 $y2 "
  }
}


proc do_polygon {cvs} {
  global colors
  
  set pos 0
  for {set i 0} {$i < 6} {incr i} {
    set x($pos) [expr {250 + sin($i)*(50+$i)}]
    set y($pos) [expr {250 + cos($i)*(50+$i)}]
    incr pos
  }
  
  set fillColor [lindex $colors 0]
  set outlineColor [lindex $colors 3]
  puts "  $cvs create polygon $x(0) $y(0) \
      $x(1) $y(1) \
      $x(2) $y(2) \
      $x(3) $y(3) \
      $x(4) $y(4) \
      $x(5) $y(5) \
  -fill $fillColor -outline $outlineColor -width 4"

  $cvs create polygon $x(0) $y(0) \
      $x(1) $y(1) \
      $x(2) $y(2) \
      $x(3) $y(3) \
      $x(4) $y(4) \
      $x(5) $y(5) \
  -fill $fillColor -outline $outlineColor -width 4
}




