function buttonClickedCallback(src, evnt)
% buttonClickedCallback
%   push button clicked callback

%   Project: IMPACT
%   Title of Code: buttonClickedCallback
%   Relevant additional documentation:
%   Developer: Carlos Gonzalez
%   External Dependencies: NA
%
%   Disclaimer
%   CAUTION: It is strongly recommended that changes to this code
%   should be avoided without first consulting with authors.


handles = guidata(src);

C = handles.C;
str = lower(get(src, 'TooltipString'));
switch str
    case 'integrator'
        C2 = zpk([], [0], 1);
        C = series(C, C2);

        handles.C = C;
        handles.sys = series(C, handles.G);
        guidata(handles.hFig, handles);
        updatePlot(handles);
    case 'differentiator'
        C2 = zpk([0], [], 1);
        C = series(C, C2);

        handles.C = C;
        handles.sys = series(C, handles.G);
        guidata(handles.hFig, handles);
        updatePlot(handles);
    case 'delete integrator'
        [z,p,k] = zpkdata(C,'v');
        for m = 1:numel(p)
           if p(m)==0
               p(m) = [];
               break;
           end
        end
        C = zpk(z, p, k);

        handles.C = C;
        handles.sys = series(C, handles.G);
        guidata(handles.hFig, handles);
        updatePlot(handles);
    case 'delete differentiator'
        [z,p,k] = zpkdata(C,'v');
        for m = 1:numel(z)
           if z(m)==0
               z(m) = [];
               break;
           end
        end
        C = zpk(z, p, k);

        handles.C = C;
        handles.sys = series(C, handles.G);
        guidata(handles.hFig, handles);
        updatePlot(handles);
    case 'new analysis plot'
        uiColor = get(0,'defaultUicontrolBackgroundColor');
        
        h=figureMLS( 'Position',[100 200 200 250],'Resize','off', 'Visible', 'off', 'WindowStyle', 'modal',...
            'MenuBar','none','Name','New analysis plots', 'NumberTitle', 'Off', 'color', uiColor ); % open a new figure
        chk_sens = uicontrol('Style','checkbox', 'tag', 'chk_sens',...
            'Position',[10,230,280,15],'String','Sensitivity',...
            'HorizontalAlignment','left' );
        chk_bode = uicontrol('Style','checkbox', 'tag', 'chk_bode',...
            'Position',[10,210,280,15],'String','Bode',...
            'HorizontalAlignment','left' );
        chk_nyquist = uicontrol('Style','checkbox', 'tag', 'chk_nyquist',...
            'Position',[10,190,280,15],'String','Nyquist',...
            'HorizontalAlignment','left' );
        chk_nichols = uicontrol('Style','checkbox', 'tag', 'chk_nichols',...
            'Position',[10,170,280,15],'String','Nichols',...
            'HorizontalAlignment','left' );
        chk_step = uicontrol('Style','checkbox', 'tag', 'chk_step',...
            'Position',[10,150,280,15],'String','Step',...
            'HorizontalAlignment','left' );
        Data.sens = chk_sens;
        Data.bode = chk_bode;
        Data.nyquist = chk_nyquist;
        Data.nichols = chk_nichols;
        Data.step = chk_step;
        pushbutton_okay = uicontrol('Style','pushbutton',...
            'Position',[10,30,50,20],'String','OK',...
            'Callback', {@buttonOK, handles, Data} );
        pushbutton_cancel = uicontrol('Style','pushbutton',...
            'Position',[70,30,50,20],'String','Cancel',...
            'Callback', 'close(gcf);');
        
        movegui(h, 'center');
        set(h, 'Visible', 'on');
        
        handles.C = C;
        handles.sys = series(C, handles.G);
        guidata(handles.hFig, handles);
end


function buttonOK(src, eventdata, handles, Data)

figH = gcf;
if get(Data.sens, 'value')
    h = findobj('type', 'figure', 'tag', 'sensDDV');
    if isempty(h)
        figureMLS('Name', 'Sensitivity - Dynamic design view', 'NumberTitle', 'Off', 'tag', 'sensDDV');
    end
end
if get(Data.bode, 'value')
    h = findobj('type', 'figure', 'tag', 'bodeDDV');
    if isempty(h)
        figureMLS('Name', 'Bode - Dynamic design view', 'NumberTitle', 'Off', 'tag', 'bodeDDV');
    end
end
if get(Data.nyquist, 'value')
    h = findobj('type', 'figure', 'tag', 'nyquistDDV');
    if isempty(h)
        figureMLS('Name', 'Nyquist - Dynamic design view', 'NumberTitle', 'Off', 'tag', 'nyquistDDV');
    end
end
if get(Data.nichols, 'value')
    h = findobj('type', 'figure', 'tag', 'nicholsDDV');
    if isempty(h)
        figureMLS('Name', 'Nichols - Dynamic design view', 'NumberTitle', 'Off', 'tag', 'nicholsDDV');
    end
end
if get(Data.step, 'value')
    h = findobj('type', 'figure', 'tag', 'stepDDV');
    if isempty(h)
        figureMLS('Name', 'Step - Dynamic design view', 'NumberTitle', 'Off', 'tag', 'stepDDV');
    end
end
close(figH);

updateAnalysisPlots(handles);





