%% pb_delete_call
%    callback function for when user clicks on the delete cell
%    button.
% inputs:
%   object:Grid - current Grid object
%   src:double - source of the callback calling
%   event:eventdata - event that triggered the callback
% outputs:
%   none
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification
function [] = pb_delete_call(object,src,event)
gui = get(src,'userdata');
gui.save_data;
deleted_cell = object.cells(end);



% deleted last cell in grid
if size(object.cells,2) == 1
    if (~isempty(object.parent_cell))
        
        
        if isempty(event) || event ~= 1
            
            undo_data.action = 5;
            undo_data.grid = object;
            undo_data.cell = object.parent_cell;
            undo_data.text = deleted_cell.cond;
            undo_data.subgrid = deleted_cell;
            
            gui.undo_man.new_state(undo_data);
            
        end        
    end        
else
    if isempty(event) || event ~= 1
        
        undo_data.action = 3;
        undo_data.grid = object;
        undo_data.cell = deleted_cell;
        undo_data.text = deleted_cell.cond_text;
        undo_data.subgrid = deleted_cell.cond;
        
        gui.undo_man.new_state(undo_data);
        
    end
    
end

% button could be pressed in the left or top grid so we need to
% try to remove the result cell associated with either case.
object.rGrid.delete_g2s(deleted_cell);
object.rGrid.delete_g1s(deleted_cell);
deleted_cell.delete_Cell;
object.cells(end) = [];

% deleted last cell in grid
if size(object.cells,2) == 0
    if (~isempty(object.parent_cell))
               
        object.parent_cell.subgrid = [];
        object.parent_cell.pb_flag = 1;
        delete(object.new_cell_pb);
        delete(object.delete_cell_pb);
        object.rGrid.refresh;
     
    end
    
end


if size(object.cells,2) == 1 && isempty(object.parent_cell)
    set(object.delete_cell_pb,'Enable','off');
end
gui.reset_wh();
gui.draw_allgrids(0);


gui.update_undoredo;



end

