/* Time-stamp: <97/01/10 13:48:04 john> */

/*
# Purpose: Calculate the sizes of things, e.g. width of columns for export.
 */

#include <stdio.h>
#include "attr.h"
#include "grid.h"
#include "sizes.h"

/* #define debug 1 */

void set_initial_sizes(struct entry *entries)
{
  while (entries != NULL)
    {
      char *width = get_attr(entries, "width");
      int iwidth;

      if (width != NULL)
	{
	  if (sscanf(width, "%d", &iwidth) == 1)
	    {
	      entries->width = iwidth;
	    } else {
	      fprintf(stderr, "Bad width: %s\n", width);
	    }
	}

      if ((strcmp(entries->type, "road") == 0) &&
	  entries->width == 1)
	{
	  if (entries->column == -1)
	    {
	      entries->column = -2;
	      entries->width = 2;
	    } else
	      if (entries->column == 1)
		{
		  entries->width = 2;
		}
	}
      entries = entries->next;
    }
}

int total_column_widths(struct grid *grid, int span)
{
  int i;
  int z = grid->max_rank;
  int a = grid->min_rank;
  int *widths = (int*)malloc(((z - a) + 1) * sizeof(int));
  struct row *row = grid->row_list;

  if (widths == NULL)
    return 0;

  grid->row_title_widths = widths;

  for (i = a; i <= z; i++)
    widths[i-a] = 0;

  while (row != NULL)
    {
      int ra = a;
      int rz = z;

      if ((row->min_rank) > ra) ra = row->min_rank;
      if ((row->max_rank) < rz) rz = row->max_rank;
      for (i = ra; i <= rz; i++)
	{
	  struct cell *cell = cell_n(row, i);

	  if (cell != NULL)
	    {
	      int width = (span ?
			   ((cell->entry->name != cell->cell_text) ?
			    cell->title_width :
			    strlen(cell->entry->name) / cell->entry->width) :
			   cell->title_width);

	      if (width > widths[i-a])
		widths[i-a] = width;
	    }
	}


      row = row->next;
    }

  return 1;
}

int set_physical_coordinates(struct grid *grid)
{
  int *xs = grid->phys_xs;
  int i;
  int this_row_index;
  int z = grid->max_rank;
  int a = grid->min_rank;
  int *widths;

#ifdef debug
  printf("calculating sizes on grid\n");
#endif

  if (xs != NULL)
    {
      Free(xs);
    }

#ifdef debug
  printf("a=%d z=%d z-a = %d\n", a, z, z-a);
#endif

  grid->phys_xs = xs = (int*)malloc(((z - a) + 1) * sizeof(int));

  if (xs == NULL)
    {
      return 0;
    }

  widths = (int*)malloc(((z - a) + 1) * sizeof(int));

  if (widths == NULL)
    {
      Free(xs);
      return 0;
    }

  for (i = a; i <= z; i++)
    {
      xs[i] = i;
      widths[i] = 1;
    }

  {
    for (this_row_index = 0;
	 this_row_index < grid->n_rows;
	 this_row_index++)
      {
	struct row *row=grid->rows[this_row_index];

	int this_col_index;
	int tallest_this_row = 0;
#ifdef debug
	printf(" processing row %d\n", this_row_index);
#endif
	if (this_row_index == 0) row->phys_y = 0;
	for (this_col_index = row->min_rank;
	     this_col_index <= row->max_rank;
	     this_col_index++)
	  {
	    struct cell *cell = cell_n(row, this_col_index);
#ifdef debug
	    printf("  processing col %d\n", this_col_index);
#endif
	    if ((cell != NULL) &&
		(cell->entry != NULL))
	      {
		if (cell->phys_height > tallest_this_row)
		  {
		    tallest_this_row = cell->phys_height;
		  }

		if (cell->phys_width > widths[this_col_index])
		  {
		    widths[this_col_index] = cell->phys_width;
		  }
	      }
	  }
#ifdef debug
	printf("tallest_this_row=%d\n", tallest_this_row);
#endif
	if ((this_row_index+1) < grid->n_rows)
	  {
	    grid->rows[this_row_index+1]->phys_y =
	      row->phys_y + tallest_this_row;
#ifdef debug
	    printf("set next row (%d) to y = %d+%d = %d\n",
		   this_row_index+1,
		   row->phys_y, tallest_this_row,
		   row->phys_y + tallest_this_row);
#endif
	    /* now stretch everything that passes through this row */






	  } else {
	    grid->phys_top = row->phys_y + tallest_this_row;
#ifdef debug
	    printf("set grid top to y = %d+%d = %d\n",
		   row->phys_y, tallest_this_row,
		   row->phys_y + tallest_this_row);
#endif
	  }
      }
  }

  for (i = 1; i < z; i++)
    {
      xs[i+1] = xs[i] + widths[i];
    }

  for (i = -1; i > a; i--)
    {
      xs[i-1] = xs[i] + widths[i];
    }

  Free(widths);

  return 1;
}

void widen_roads_to_full_width(struct grid *grid)
{
}

/* end of sizes.c */
