%% draw_grid1
%     Draws the grid on the top onto the figure. the current
%     assumption is that the top grid does not have any subgrids,
% inputs:
%   obj:GUI - current GUI object
%   grid:Grid - grid to be drawn, should be grid on top.
%   load:tristate - 0 if grid is being refreshed, 1 if grid should reload
%                   data, 2 if the grid is being initialized.
% outputs:
%   none
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification
function [] = draw_grid1(object,grid,load)
% assumption grid 1 has no subgrids
pos = [];
for i=1:size(grid.cells,2)
    % if we are in edit mode we need to shift over the cells to
    % accomidate for the new grid buttons in the left grid.
    if(object.edit == 1)
        pb_space = grid.cells(i).grid_push_width;
    else
        pb_space = 0;
    end
    figpos = get(object.fig,'position');
    
    % check if grid2 is empty, currently grid 2 will never be
    % empty, may change in future though.
    if (isempty(object.Grid2))
        pos = [grid.cells(i).condition_text_x grid.cells(i).condition_text_y grid.cells(i).condition_text_width grid.cells(i).condition_text_height];
    else
        pos = [grid.cells(i).condition_text_x+grid.cells(i).condition_text_width*object.Grid2.max_width(1) figpos(4)-object.header_height-grid.cells(i).condition_text_y-grid.cells(i).condition_text_height grid.cells(i).condition_text_width grid.cells(i).condition_text_height];
    end
    % set the x coordinate of the cell
    pos(1) = pos(1) + (i-1)*grid.cells(i).condition_text_width + grid.cells(i).condition_text_offset + pb_space;
    % if the edit box does not exist create it, if it does
    % adjust it's position.
    if (load == 2 || isempty(grid.cells(i).cond) || ~ishandle(grid.cells(i).cond))
        grid.cells(i).cond = object.create_std_text(object.fig,pos);
        if (load ~= 0)
            set(grid.cells(i).cond,'String',grid.cells(i).cond_text);
        end
    else
        grid.cells(i).set_pos(pos);
    end
    
    if(object.edit == 0)
        string = get(grid.cells(i).cond,'String');
        if(isempty(string))
            set(grid.cells(i).cond,'Visible','off');
        end
    else
        set(grid.cells(i).cond,'Visible','on');
    end
end
% if we are in edit mode, draw the new and delete buttons at
% the right of the last cell
if( object.edit == 1)
    pos(1) = pos(1) + pos(3);
    pos(2) = pos(2) + 20;
    pos(4) = 20;
    pos(3) = 60;
    grid.set_pb(object.fig,pos);
    pos(2) = pos(2) - 20;
    
    grid.set_delete_pb(object.fig,pos);
    set(grid.delete_cell_pb,'userdata',object);
    set(grid.new_cell_pb,'userdata',object);
else
    delete(grid.new_cell_pb);
    grid.new_cell_pb = [];
    delete(grid.delete_cell_pb)
    grid.delete_cell_pb = [];
end
if object.edit ==1 || load ~= 0
    fig_pos = get(object.fig,'position');
    if(pos(1)+pos(3)>fig_pos(3))
        fig_pos(3) = pos(1)+pos(3);
        set(object.fig,'position',fig_pos);
    elseif (pos(1) + pos(3)<object.fig_width)
        fig_pos(3) = object.fig_width;
        set(object.fig,'position',fig_pos);
    elseif (pos(1) + pos(3)<fig_pos(3) && pos(1) +pos(3) >object.fig_width)
        fig_pos(3) = pos(1)+pos(3);
        set(object.fig,'position',fig_pos);
    end
end
end

