function subGUI4BKP(version,nr,basicfsz)
% The function BorgenPlot, provides software 
% for the computation of multi-component factorizations and the area of feasible solutions.
% Copyright (c) 2014 Annekathrin Juerss, University of Rostock, Department of Mathematics.
% 
% The software can be used for academic, research and other similar noncommercial uses. The user
% acknowledges that this software is still in the development stage and that it is provided by the
% copyright holders and contributors "as is" and any express or implied warranties, fitness for a
% particular purpose are disclaimed. In no event shall the copyright owner or contributors be liable
% for any direct, indirect, incidental, special, exemplary, or consequential damages.
% 
% The copyright holders provide no reassurances that the source code provided does not infringe any
% patent, copyright, or any other intellectual property rights of third parties. The copyright
% holders disclaim any liability to any recipient for claims brought against recipient by any third
% party for infringement of that parties intellectual property rights.
% 


fname = mfilename;
calcf = mfilename('fullpath');
calcf = calcf(1:end-length(fname));
calcp = [calcf, 'subfcnsBKP'];
path(path, calcf);
path(path, calcp);

scrsz = get(0,'ScreenSize');
fig = figure(nr);
pause(.1);
set(fig,'Position',[1/20*scrsz(3:4) 9/10*scrsz(3:4)]);
set(fig,'Name',['Borgen Plot- module (version ',num2str(version, '%2.2f'),')'],'NumberTitle','off','MenuBar','none',...
    'Toolbar','none','color','white','Visible','off');

if ispc
%     basicfsz = 5;
    basichi = 0.025;
else
%     basicfsz = 6.5;
    basichi = 0.025;
end
basicfsz = basicfsz*scrsz(4)/1080;

Styles = {'bitstream charter';'clean';'Courier';'courier 10 pitch';'frank ruehl';'Helvetica';'Helvetica-Narrow';'itc bookman';'urw bookman l'};
sty = Styles{6};
bgc = 0.95*ones(3,1);

titles(1) = uicontrol('Style','text','String','AFS scaling options','HorizontalAlignment','center','Fontsize',3*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.13, 0.25, 0.2, 1.8*basichi]);
titles(2) = uicontrol('Style','text','String','Generalized Borgen plot - RS-scaling','HorizontalAlignment','center','Fontsize',3*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.45, 0.925, 0.4, 1.5*basichi]);
titles(3) = uicontrol('Style','text','String',' Factor window','HorizontalAlignment','center','Fontsize',3*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.55, 0.44, 0.2, 1.5*basichi]);
titles(5) = uicontrol('Style','text','String','filename','HorizontalAlignment','center','Fontsize',1.5*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.01, 0.44, 0.45, .9*basichi]);

% The axes for the data, the AFS and the transformed AFS

AxesD = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.06, 0.52, 0.35, 0.38]);
plot(AxesD,1,0,'w');


AxesSOL = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.48, 0.05, 0.35, 0.38]);
plot(AxesSOL,1,0,'w');

AxesAFS = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.48, 0.52, 0.35, 0.38]);
plot(AxesAFS,1,0,'w');

emptybutton = uicontrol('Style','pushbutton','Position',[0.02, 0.9, 0.1, basichi],'Visible','off','Callback',{@emptybutton_CB});

data.ismatrix = 0;
data.isafs = 0;
data.calcp = calcf;
data.isExpert1 = 0;
data.isExpert2 = 0;
data.isLastClickLockMode = 0;
data.scalBorg=1;
set(emptybutton,'UserData',data);

%Polysettings
angleText = uicontrol('Style','text','String','rot. angle:','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.85, 0.84, .06, basichi]);

Polysettings(1) = uicontrol('Style','edit','String',' 1.0e-1','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[0.92, 0.84, .05, basichi],'TooltipString', sprintf('Rotation angle for tangents in the construction of the AFS (in degree)'));

stoppingText = uicontrol('Style','text','String','d-stopping:','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.85, 0.81, .06, basichi]);

Polysettings(2) = uicontrol('Style','edit','String',' 1.0e-3','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[0.92, 0.81, .05, basichi],'TooltipString', sprintf('Distance between parallel lines in the line moving algorithm.'));

%Radiogroup
Radiogroup(1) = uibuttongroup('visible','on','Position',[0.05, 0.15, 0.22, .08],'BackgroundColor',bgc);
borg = uicontrol('Style','radiobutton','String','RS-scaling (Row Sum)','Units','normalized','Position',[0.01, 0.6, 0.98, 0.25],...
    'Fontsize',2*basicfsz,'FontWeight','bold','FontName',sty,'parent',Radiogroup(1),'HandleVisibility','off','Value',1,'BackgroundColor',bgc,...
    'TooltipString', sprintf('Row Sum scaling (RS-scaling) scales the rows of D. Used for classical Borgen plots.'));

one_x = uicontrol('Style','radiobutton','String','FSV-scaling (First Singular Vector)','Units','normalized','Position',[0.01, 0.15, 0.98, 0.25],...
    'Fontsize',2*basicfsz,'FontWeight','bold','FontName',sty,'parent',Radiogroup(1),'HandleVisibility','off','Value',0,'BackgroundColor',bgc,...
    'TooltipString', sprintf('First Singular Vector scaling (FSV-scaling) scales the data so that the expansion coefficient of the first sing. vec. is 1.'));

Radiogroup(2) = uibuttongroup('visible','on','Position',[0.85, 0.21, 0.12, .06],'BackgroundColor',bgc);
usmn = uicontrol('Style','radiobutton','String',' live view on','Units','normalized','Position',[0.01, 0.6, 0.98, 0.25],...
    'Fontsize',2*basicfsz,'FontWeight','bold','FontName',sty,'parent',Radiogroup(2),'HandleVisibility','off','Value',0,'BackgroundColor',bgc,...
    'TooltipString', sprintf('Activate live view mode to represent\nthe factors just in time and lock points.\nMove the mouse pointer through the AFS.'));

usmf = uicontrol('Style','radiobutton','String',' live view off','Units','normalized','Position',[0.01, 0.15, 0.98, 0.25],...
    'Fontsize',2*basicfsz,'FontWeight','bold','FontName',sty,'parent',Radiogroup(2),'HandleVisibility','off','Value',1,'BackgroundColor',bgc,...
    'TooltipString', sprintf('Deactivate live view mode.'));


% The infomation-text-field
Footnote = uicontrol('Style','text','String','User info','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.002, 0.002, 0.97, basichi]);


% The data-information-text-fields

emptybutton = uicontrol('Style','pushbutton','Position',[0.02, 0.9, 0.1, basichi],'Visible','off','Callback',{@emptybutton_CB});

% Button(2) = uicontrol('Style','pushbutton','String',' Transpose ','HorizontalAlignment','right','Fontsize',2*basicfsz,...
%     'Units','normalized','FontName',sty,'Position',[0.08,  0.87, 0.1, basichi]);


Datainfo(1) = uicontrol('Style','text','String','Data info','HorizontalAlignment','center','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.05, 0.41, 0.12, basichi]);

Datainfo(2) = uicontrol('Style','text','String',' spectra: ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.05, 0.38, 0.12, basichi]);

Datainfo(3) = uicontrol('Style','text','String',' channels: ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.05, 0.35, 0.12, basichi]);







% The SVD-information-text-fields
SVDinfo(1) = uicontrol('Style','text','String','SVD info','HorizontalAlignment','center','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.19, 0.41, 0.22, basichi]);

SVDinfo(2) = uicontrol('Style','text','String',' s(1):','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName','symbol','Position',[0.19, 0.38, 0.1, basichi],...
    'TooltipString', sprintf('First singular value, indicates number of components.'));

SVDinfo(3) = uicontrol('Style','text','String',' s(2): ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName','symbol','Position',[0.3, 0.38, 0.11, basichi],...
    'TooltipString', sprintf('Second singular value, indicates number of components.'));

SVDinfo(4) = uicontrol('Style','text','String',' s(3): ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName','symbol','Position',[0.19, 0.35, 0.1, basichi],...
    'TooltipString', sprintf('Third singular value, indicates number of components.'));

SVDinfo(5) = uicontrol('Style','text','String',' s(4): ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName','symbol','Position',[0.3, 0.35, 0.11 , basichi],...
    'TooltipString', sprintf('Fourth singular value, indicates number of components.'));

EPSinfo(1) = uicontrol('Style','text','String',' min(A):','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.29, 0.21, .12, basichi] ,'TooltipString', sprintf('minimal acceptable entry of A'));

EPSinfo(2) = uicontrol('Style','edit','String',' 0.0','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[0.35, 0.18, .06, basichi],'TooltipString', sprintf('absolute value of minimal negative entry of C'));

EPSinfo(3)= uicontrol('Style','edit','String',' 0.0','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[0.35, 0.15, .06, basichi],'TooltipString', sprintf('absolute value of minimal negative entry of A'));


AFSinfo(1) = uicontrol('Style','text','String','AFS info','HorizontalAlignment','center','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.85, 0.63, 0.12, basichi]);

AFSinfo(2) = uicontrol('Style','text','String','pts. outer bnd.: ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.85, 0.6, 0.12, basichi]);

AFSinfo(3) = uicontrol('Style','text','String','pts. inner bnd.: ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.85, 0.57, 0.12, basichi]);

%Factor window information
Plotparameter(1) = uicontrol('Style','text','String',' bnd.-pts.:','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.85, 0.43, .07, basichi]);

Plotparameter(2) = uicontrol('Style','text','String',' stepsize x:','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.85, 0.40, .07, basichi]);

Plotparameter(3) = uicontrol('Style','text','String',' stepsize y:','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.85, 0.37, .07, basichi]);

Plotsettings(1) = uicontrol('Style','edit','String',' 20','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[0.93, 0.43, .04, basichi],'TooltipString', sprintf('Number of points on the border of the AFS segments for which corresponding spectra shall be plotted.'));

Plotsettings(2) = uicontrol('Style','edit','String',' 0.1','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[0.93, 0.40, .04, basichi],'TooltipString', sprintf('horizontal distance between points on the border of the AFS segments for which corresponding spectra shall be plotted.'));

Plotsettings(3) = uicontrol('Style','edit','String',' 0.1','HorizontalAlignment','right','Fontsize',2*basicfsz,...
    'Units','normalized','FontName',sty,'Position',[0.93, 0.37, .04, basichi],'TooltipString', sprintf('vertical distance between points on the border of the AFS segments for which corresponding spectra shall be plotted.'));


AFS(1) = uicontrol('Style','pushbutton','UserData',1,'String','AFS 1','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.85,  0.34, 0.12, basichi],'Enable','on',...
    'Tooltip', 'Show the factors for segment 1 of the AFS (blue or black).',...
    'Callback', {@buttontransformAFS_BKP, fig, emptybutton, Footnote, AxesAFS, AxesSOL, Plotsettings, 1,usmn, usmf});

AFS(2) = uicontrol('Style','pushbutton','UserData',1,'String','AFS 2','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.85,  0.31, 0.12, basichi],'Enable','on',...
    'Tooltip', 'Show the factors for segment 2 of the AFS (green).',...
    'Callback', {@buttontransformAFS_BKP, fig, emptybutton, Footnote, AxesAFS, AxesSOL, Plotsettings, 2,usmn, usmf});

AFS(3) = uicontrol('Style','pushbutton','UserData',1,'String','AFS 3','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.85,  0.28, 0.12, basichi],'Enable','on',...
    'Tooltip', 'Show the factors for segment 3 of the AFS (red).',...
    'Callback', {@buttontransformAFS_BKP, fig, emptybutton, Footnote, AxesAFS, AxesSOL, Plotsettings, 3,usmn, usmf});

LockMode(1) = uicontrol('Style','text','String','Lock-Mode','HorizontalAlignment','center','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.85, 0.18, 0.12, basichi]);

LockMode(5) = uicontrol('Style','pushbutton','UserData',1,'String','esc','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.92,  0.12, 0.05, basichi],'Enable','on',...
    'Tooltip', 'Remove only the second locked point and its reduced AFS.',...
    'Callback', {@esc_BKP, emptybutton,LockMode, AxesAFS,AxesSOL, 2});
LockMode(4) = uicontrol('Style','pushbutton','UserData',1,'String','no. 2','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.85,  0.12, 0.05, basichi],'Enable','on',...
    'Tooltip', 'Compute doubly reduced AFS after locking two points.',...
    'Callback', {@lockmode_BKP, emptybutton,2, Footnote,LockMode, AxesAFS, AxesSOL, Plotsettings,usmn, usmf,data.calcp});
LockMode(3) = uicontrol('Style','pushbutton','UserData',1,'String','esc','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.92,  0.15, 0.05, basichi],'Enable','on',...
    'Tooltip', 'Remove locked point(s) and reduced AFS.',...
    'Callback', {@esc_BKP, emptybutton,LockMode, AxesAFS,AxesSOL, 1});

LockMode(2) = uicontrol('Style','pushbutton','UserData',1,'String','no. 1','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.85,  0.15, 0.05, basichi],'Enable','on',...
    'Tooltip', 'Compute reduced AFS after locking one point.',...
    'Callback', {@lockmode_BKP, emptybutton,1, Footnote,LockMode, AxesAFS, AxesSOL, Plotsettings,usmn, usmf,data.calcp});








Button(4)  = uicontrol('Style','pushbutton','UserData',0,'String','twist x','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.85,  0.73, 0.12, basichi],...
    'TooltipString', sprintf('flip the Borgen plot horizontal'),...
    'Callback',{@buttontwist_x_BKP,fig, emptybutton,LockMode,Footnote, AxesAFS, AxesSOL});
Button(8) = uicontrol('Style','pushbutton','UserData',0,'String','swap to FSV-scal.','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.85,  0.67, 0.12, basichi],...
    'TooltipString', sprintf('transform the AFS into the FSV-scaling'),...
    'Callback',{@buttonswap_scal_BKP,fig, emptybutton,Button,titles,Plotsettings,LockMode,Footnote, AxesAFS});
Button(5)  = uicontrol('Style','pushbutton','UserData',0,'String','twist y','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.85,  0.70, 0.12, basichi],...
    'TooltipString', sprintf('flip the Borgen-plot vertical'),...
    'Callback',{@buttontwist_y_BKP,fig, emptybutton,LockMode,Footnote, AxesAFS,AxesSOL});

 


Button(6) = uicontrol('Style','pushbutton','UserData',0,'String','save RS-scal.','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.85,  0.07, 0.12, basichi],...
    'TooltipString', 'Save all results with RS-scaling in *.mat file.',...
    'Callback',{@button_save_BKP,fig, emptybutton,Footnote,Plotsettings,1});

Button(7) = uicontrol('Style','pushbutton','UserData',0,'String','save FSV-scal.','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.85,  0.04, 0.12, basichi],...
    'TooltipString', 'Save all results with FSV-scaling in *.mat file.',...
    'Callback',{@button_save_BKP,fig, emptybutton,Footnote,Plotsettings,0});

Button(2) = uicontrol('Style','pushbutton','UserData',0,'String','Transpose','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.25, 0.92, 0.15, 1.5*basichi],'Enable','on',...
    'TooltipString', 'Transpose the data matrix.',...
    'Callback', {@buttontranspose_BKP, emptybutton,Button,AFS,LockMode, Footnote, AxesD, AxesAFS, AxesSOL, Datainfo, SVDinfo,EPSinfo, Polysettings, usmn, usmf,borg,one_x});

Button(3)    = uicontrol('Style','pushbutton','UserData',0,'String','AFS','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.85,  0.77, 0.12, basichi],...
    'TooltipString', sprintf('Compute the AFS (area of feasible solutions).'),...
    'Callback',{@buttonAFS_BKP,fig, emptybutton,Button, Footnote,AFS,LockMode, AxesAFS, AxesSOL, AFSinfo,...
    SVDinfo,EPSinfo, Polysettings,Plotsettings, usmn, usmf,data.calcp});
Button(1)    = uicontrol('Style','pushbutton','UserData',0,'String','Load','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.08,  0.92, 0.15, 1.5*basichi],...
    'TooltipString', sprintf('Load *.mat file containing data matrix D (spectra  x channels)\nand channel vector "x" and time vector "t".'),...
    'Callback',{@loadButton_BKP, emptybutton, Button,AFS,LockMode, Footnote, AxesD, AxesAFS, AxesSOL, Datainfo,...
    SVDinfo,EPSinfo,AFSinfo, Polysettings,borg,one_x, usmn, usmf, titles});



% Information on scaled D, eps_C and eps_S


epsCText = uicontrol('Style','text','String','eps_C','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.29, 0.18, .05, basichi]);

epsAText = uicontrol('Style','text','String','eps_S','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.29, 0.15, .05, basichi]);



%The borgen-plot information




data = get(emptybutton,'UserData');
data.calcp = calcf;
if (isfield(data,'ismatrix') == 0) || (data.ismatrix == 0)
    onoffButton_BKP(Button,1,2:8);
    onoffButton_BKP(LockMode,[],2:5);
    onoffButton_BKP(AFS,[],1:3);

end
set(emptybutton,'UserData',data);



set(Radiogroup(1),'SelectionChangeFcn', {@buttonselectnorm_BKP, emptybutton,Button,titles,Footnote,AFS,LockMode,AxesAFS,AxesSOL, EPSinfo, borg, one_x});

set(gcf,'WindowButtonMotionFcn', {@liveview_BKP, emptybutton, Button, AxesD,AxesAFS, AxesSOL, Radiogroup, usmn,usmf, basichi})

set(gcf,'WindowButtonDownFcn', {@extractaxes_BKP, fig, emptybutton, Button, AxesD,  AxesAFS, AxesSOL, usmn, usmf})

set(fig,'Visible','on')

function emptybutton_CB(hObject, eventdata)
data.ismatrix = 0;
data.isafs = 0;
data.polygon = 0;
data.scalBorg = 1;

set(hObject,'UserData',data);
