function table_draw(table)
% table_draw(table)
%
% Initializes table ui handles.
% Also called during resizing

if ~isempty(table.ui)
  table.size = table_size(table);
end

table.rows = size(table.handles,1);
table.cols = size(table.handles,2);

if ~isfield(table,'width_margin')
  for row = 1:table.rows
    for col = 1:table.cols
      table.width_margin(row,col) = NaN;
    end
  end
end
if ~isfield(table,'height_margin')
  for row = 1:table.rows
    for col = 1:table.cols
      table.height_margin(row,col) = NaN;
    end
  end
end
if ~isfield(table,'false_height')
  for row = 1:table.rows
    for col = 1:table.cols
      table.false_height(row,col) = NaN;
    end
  end
end
if ~isfield(table,'false_width')
  for row = 1:table.rows
    for col = 1:table.cols
      table.false_width(row,col) = NaN;
    end
  end
end
default_height_margin = 3;
default_width_margin = 3;
default_false_height = 3;
default_false_width  = 3;
if size(table.width_margin,1) < table.rows || size(table.width_margin,2) < table.cols
  table.width_margin(table.rows,table.cols) = default_width_margin;
end
if size(table.height_margin,1) < table.rows || size(table.height_margin,2) < table.cols
  table.height_margin(table.rows,table.cols) = default_height_margin;
end
if size(table.false_height,1) < table.rows || size(table.false_height,2) < table.cols
  table.false_height(table.rows,table.cols) = default_false_height;
end
if size(table.false_width,1) < table.rows || size(table.false_width,2) < table.cols
  table.false_width(table.rows,table.cols) = default_false_width;
end
for row = 1:table.rows
  for col = 1:table.cols
    if isnan(table.width_margin(row,col))
      table.width_margin(row,col) = default_width_margin;
    end
    if isnan(table.height_margin(row,col))
      table.height_margin(row,col) = default_height_margin;
    end
    if isnan(table.false_height(row,col))
      table.false_height(row,col) = 0;
    end
    if isnan(table.false_width(row,col))
      table.false_width(row,col) = 0;
    end
  end
end

if ~isfield(table,'offset')
  table.offset = [0 0];
end

if ~isfield(table,'origin')
  table.origin = [0 0];
end

if ~isempty(table.ui)
  set(table.ui,'UserData',table);
  set(table.ui,'ResizeFcn',@table_resize);
end

curPos = [0 0];
for row = 1:table.rows
  curPos(1) = 0;
  for col = 1:table.cols
    % Check to see if widge fills whole vertical space (inf size)
    if isinf(table.height(row,col))
      nonInfMask = ~isinf(table.height(:, col));
      % Sum up the margins
      margins = 2*sum(table.height_margin(nonInfMask, col));
      % Sum up the other entries
      entries = sum(table.height(nonInfMask, col));

      height = (table.size(2) - margins - entries) / sum(~nonInfMask);
    else
      height = table.height(row,col);
    end

    % Check to see if widge fills whole horizontal space (inf size)
    if isinf(table.width(row,col))
      nonInfMask = ~isinf(table.width(row,:));
      % Sum up the margins
      margins = 2*sum(table.width_margin(row, nonInfMask));
      % Sum up the other entries
      entries = sum(table.width(row, nonInfMask));

      width = (table.size(1) - margins - entries) / sum(~nonInfMask);
    else
      width = table.width(row,col);
    end

    % Compute the size
    if row == table.rows
      % Last row is always on the bottom
      newPos = [table_pos(table,row,col,curPos,height) width-2*table.width_margin(row,col)-table.false_width(row,col) height-2*table.height_margin(row,col)-table.false_height(row,col)];
      newPos(2) = table.offset(2) + table.height_margin(row,col);
    else
      newPos = [table_pos(table,row,col,curPos,height) width-2*table.width_margin(row,col)-table.false_width(row,col) height-2*table.height_margin(row,col)-table.false_height(row,col)];
    end
    newPos(1:2) = newPos(1:2) + table.origin;
    try
      %table.false_width(row,col)
      %table.handles{row,col}
      %table.size
      %newPos
      if ishandle(table.handles{row,col})
        % This is a Matlab graphics object
        %try
        %  get(table.handles{row,col},'String')
        %end
        set(table.handles{row,col},'Units','Points');
        set(table.handles{row,col},'Position', newPos);
      elseif isstruct(table.handles{row,col})
        % This is a table container
        table.handles{row,col}.size = newPos(3:4);
        table.handles{row,col}.origin = newPos(1:2);
        table_draw(table.handles{row,col});
      end
    catch ME
      ME
      %keyboard
    end
    curPos(1) = curPos(1) + width;
  end
  curPos(2) = curPos(2) + height;
end

return;
