$include "info.icn"

$define Win_Size   600

# set the default for DrawTree_Square_R
procedure drawtree_rectangle_default(fg, bg)

   local draw_record

   draw_record                      := DrawTree_Square_R()

   draw_record.win_width            := Win_Size + 200
   draw_record.win_height           := Win_Size - 200
   if /fg then draw_record.fg       := FG else draw_record.fg := fg
   if /bg then draw_record.bg       := BG else draw_record.bg := bg
   draw_record.color_list           := COLOR_LIST
   draw_record.color_list_u         := COLOR_LIST_U
   draw_record.num_color            := 4                  #  take thins out
   draw_record.win                  := WOpen("canvas=hidden", "size=" || Win_Size + 200 || "," || Win_Size,
                                              "bg=" || draw_record.bg, "fg=" || draw_record.fg)
   draw_record.linewidth            := 10
   draw_record.length               := Win_Size + 200 - 20
   draw_record.space                := 2
   draw_record.move                 := 15
   draw_record.x                    := draw_record.move
   draw_record.y                    := 10
   draw_record.tree                 := &null
   draw_record.generation           := 0
   draw_record.num_children_code    := &null
   draw_record.color_children       := &null

   draw_record.menu := ["background", format_rectangle_cb, "color list", format_rectangle_cb, 
                        "linewidth", format_rectangle_cb, 
                        "space", format_rectangle_cb, "length", format_rectangle_cb, 
                        "generation", format_rectangle_cb, "tree",  format_rectangle_cb,
                        "color format", format_rectangle_cb, "# of children", format_rectangle_cb,
                        "snapshot", format_rectangle_cb]

   return draw_record

end


# draw the tree in a circle seperated with line between each node
procedure drawtree_rectangle(draw_record, children)

    local gen, id, win, size, x, y, num

    win := Clone(draw_record.win)
    EraseArea(win)

    \draw_record.num_children_code & num := children.num_children / *draw_record.color_list

    # draw all the children
    every id := 0 to children.num_children do
    {
      /num & Fg(win, draw_record.color_list_u[(draw_record.color_list[(children.all[id].generation) % 
              draw_record.num_color + 1])])
      \num & Fg(win, draw_record.color_list_u[draw_record.color_list[
                     integer((children.all[id].children_num / num) + 1)]]) |
             Fg(win, draw_record.color_list_u[draw_record.color_list[
                     integer((children.all[id].children_num / num))]]) 
      \draw_record.color_children & draw_record.color_children == *children.all[id].children_id &
             Fg(win, "gray")
      x     := (children.all[id].base * draw_record.length) / (2 * &pi) + 10
      size  := (((children.all[id].bound - children.all[id].base) * draw_record.length) / (2 * &pi)) 
      y   := children.all[id].generation * draw_record.linewidth + 10
      FillRectangle(win, x, y, size, draw_record.linewidth - draw_record.space)
    }

    every id := 0 to children.num_children do
    {
      x     := (children.all[id].base * draw_record.length) / (2 * &pi) + 10
      size  := (((children.all[id].bound - children.all[id].base) * draw_record.length) / (2 * &pi)) 
      y   := children.all[id].generation * draw_record.linewidth + 10
      Fg(win, draw_record.bg)
      DrawLine(win, x, y, x, y + draw_record.linewidth)
    }

   if draw_record.generation > 0 then drawtree_rec_gen_find(draw_record, children)
   \draw_record.tree & drawtree_rectangle_line(draw_record, children, 0)

   return

end
    

# draw the tree by lines
procedure drawtree_rectangle_line(draw_record, children, id)

   local win, new_id, radius, new_radius, y_new, x_new, x, y, size

   size := 2
   win := Clone(draw_record.win)
   Fg("black")

   every new_id := !children.all[id].children_id do {

      x         := (((children.all[id].base + children.all[id].bound)/2) * draw_record.length) / (2 * &pi) + 10
      y         := (children.all[id].generation) * draw_record.linewidth + 10 + draw_record.linewidth/2     
      x_new     := (((children.all[new_id].base + children.all[new_id].bound)/2) * draw_record.length) / (2 * &pi) + 10
      y_new     := (children.all[new_id].generation)* draw_record.linewidth + 10 + draw_record.linewidth/2           

      DrawLine(win, x, y, x_new, y_new)     
      size := 2
      \draw_record.color_children & draw_record.color_children == *children.all[new_id].children_id &
             size := 5 
      FillCircle(win, x, y, size)
      FillCircle(win, x_new, y_new, size)
      drawtree_rectangle_line(draw_record, children, new_id)

   }

  return

end


# color code by number of children
procedure drawtree_rec_gen_find(draw_record, children)

   local num, id, color_n, first, second, third, gen

   gen := draw_record.generation
   num := 0
   every id := 0 to children.num_children do
   {
      if children.all[id].generation == gen then
         num +:= 1
   }

   num := MAX_COL / num
   color_n := BLUE

   every id := 0 to children.num_children do
   {
      if children.all[id].generation == gen then {
         drawtree_rec_gen(draw_record, children, id, color_n)
         color_n ? {
            first := tab(upto(",")); move(1)
            second := tab(upto(",")); move(1)
            third :=  tab(0)
         }
         second := integer(second) + num
         third := integer(third) - num
         color_n := string(first) || "," || string(second) || "," || string(third)
     }            
   }

  Fg("black")

  return

end


# draw the tree 
procedure drawtree_rec_gen(draw_record, children, id, color_n)

    local gen, new_id, win, size, x, y

    win := Clone(draw_record.win)
    Fg(win, color_n)

    # draw all the children
    every new_id := !children.all[id].children_id do 
    {
      x     := (children.all[new_id].base * draw_record.length) / (2 * &pi) + 10
      size  := (((children.all[new_id].bound - children.all[new_id].base) * draw_record.length) / (2 * &pi)) 
      y   := children.all[new_id].generation * draw_record.linewidth + 10
      FillRectangle(win, x, y, size, draw_record.linewidth - draw_record.space)
      drawtree_rec_gen(draw_record, children, new_id, color_n)
    }

    every new_id := !children.all[id].children_id do 
    {
      x     := (children.all[new_id].base * draw_record.length) / (2 * &pi) + 10
      size  := (((children.all[new_id].bound - children.all[new_id].base) * draw_record.length) / (2 * &pi)) 
      y   := children.all[new_id].generation * draw_record.linewidth + 10
      Fg(win, draw_record.bg)
      DrawLine(win, x, y, x, y + draw_record.linewidth)
    }

   return

end
