
array set ::grid {}

#
# The parent size
#
set pwidth 336
set pheight 400

array set ::requests {
 .w1,width 40
 .w1,height 20

 .w2,width 60
 .w2,height 30

 .w3,width 100
 .w3,height 100
}

proc _grid.insert {path column row _sticky_ sticky} {
 global grid

 #TODO use info exists to see if we should remove $path from grid(list)
 catch {unset grid($path)}

 lappend grid(list) $path
 set grid($path,column) $column
 set grid($path,row) $row
 set grid($path,columnratio) 0
 set grid($path,rowratio) 0
 set grid($path,sticky) $sticky

 _grid.layout
}


#key is generally column or row
proc _grid.layout.insert {p list key} {
 global grid

 set inserted 0

 #insert the item in a sorted fashion into the list
 for {set i 0} {$i < [llength $list]} {incr i} {
  set e [lindex $list $i]
  set ca $grid($e,$key)
  set cb $grid($p,$key)

  puts "$e:$ca $p:$cb"

  if {$cb < $ca} {
   set list [linsert $list $i $p]
   set inserted 1
   break
  } elseif {$cb == $ca} {
   #THIS IS INVALID
   return -code error "2 widgets are in the same grid $key $ca.  These widgets are $e and $p."
  }
 }

 if {!$inserted} {
  lappend list $p
 }
 return $list
}

proc _grid.layout {} {
 global grid requests pwidth pheight

 array set widths {}
 array set heights {}

 foreach p $grid(list) {
  set column $grid($p,column)
  set row $grid($p,row)

  if {![info exists widths($row,list)]} {
   lappend widths($row,list) $p
  } else {
   set widths($row,list) [_grid.layout.insert $p $widths($row,list) column]
  }

  if {![info exists heights($column,list)]} {
   lappend heights($column,list) $p
  } else {
   set heights($column,list) [_grid.layout.insert $p $heights($column,list) row]
  }
 }

 #
 # Set the width and height for the grid widgets.
 #

 foreach p $grid(list) {
  set column $grid($p,column) 
  set row $grid($p,row)
  set grid($p,width) [expr {$pwidth / [llength $widths($row,list)]}]
  set grid($p,height) [expr {$pheight / [llength $heights($column,list)]}]
 }

 parray grid
 parray widths
 parray heights

 #
 # Set the x and y for the grid widgets.
 #
 foreach p $grid(list) {
  #
  # Iterate all of the widgets in the row.
  #
  set x 0
  set row $grid($p,row)

  foreach xp $widths($row,list) {
   set grid($xp,x) $x
   incr x $grid($xp,width)
  }

  #
  # Iterate based on all of the widgets in the column.
  #
  set y 0
  set column $grid($p,column)

  foreach yp $heights($column,list) {
   set grid($yp,y) $y
   incr y $grid($yp,height)
  }
 }

 parray grid

}

if 0 {
 Rules:

  when -sticky [list width] use the maximum space for the slot according to the space ratio
  when -sticky {} use the request unless request > maximium space for the slot
  when -row is equal to another widget's -row and -column is equal to another widget's -column then go by the order of processing

}


#scrollbar (4 is also a test, instead of 3)
_grid.insert .w1 4 0 -sticky [list height]
_grid.insert .w2 1 0 -sticky [list width height]
_grid.insert .w3 2 0 -sticky [list width height]
_grid.insert .wb4 1 1 -sticky [list width height]
