function subGUI4BLA(version,nr,basicfsz)
% The toolbox FACPACK, which includes the function subGUI4BLA, provides software
% to correct the baseline for spectral data.
% Copyright (c) 2013 Mathias Sawall, 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.
% 
% The user has to respect the lapack license, see http://www.netlib.org/lapack/LICENSE.txt, and
% the license for the ACM routine nl2sol, see http://www.acm.org/publications/policies/softwarecrnotice,
% which are used in the associated C-code.

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

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

if ispc
%     basicfsz = 5;
    basichi = 0.04;
else
%     basicfsz = 6.5;
    basichi = 0.04;
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);
bgw = [1, 1, 1];

% The title names
titles(1) = uicontrol('Style','text','String','Raw spectra','HorizontalAlignment','center','Fontsize',3*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.151, 0.9, 0.348, 1.5*basichi]);

titles(2) = uicontrol('Style','text','String','Baseline corrected series of spectra','HorizontalAlignment','center','Fontsize',3*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.61, 0.9, 0.398, 1.5*basichi]);

titles(3) = uicontrol('Style','text','String','filename','HorizontalAlignment','center','Fontsize',1.75*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'Position',[0.01, 0.96, 0.98, .9*basichi]);


pos = [0.01, 0.62, 0.12, basichi];
DataText(1) = uicontrol('Style','text','String','Data info','HorizontalAlignment','center','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',pos-[0, 0.04, 0, 0]);

DataText(2) = uicontrol('Style','text','String',' spectra: ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',pos-[0, 0.09, 0.06, 0]);

DataText(3) = uicontrol('Style','text','String','','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',pos-[-.065, 0.09, 0.065, 0]);

DataText(4) = uicontrol('Style','text','String',' channels: ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',pos-[0, 0.14, 0.06, 0]);

DataText(5) = uicontrol('Style','text','String','','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',pos-[-.065, 0.14, 0.065, 0]);

DataText(6) = uicontrol('Style','text','String','','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor','w',...
    'Units','normalized','FontName',sty,'FontWeight','bold','Position',pos+[0, 0, 0, 0.08]);

ZerolineText(1) = uicontrol('Style','text','String','Type of the baseline','HorizontalAlignment','center','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[.51, 0.79, 0.12, basichi]);

ZerolineText(2) = uicontrol('Style','text','String','Checklist','HorizontalAlignment','center','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[.51, 0.64, 0.12, basichi]);

ZerolineText(3) = uicontrol('Style','text','String',' data:','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[.51, 0.59, 0.07, basichi]);

ZerolineText(4) = uicontrol('Style','text','String','not ok','HorizontalAlignment','center','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'ForegroundColor','r','Units','normalized','FontName',sty,'Position',[.585, 0.59, 0.045, basichi]);

ZerolineText(5) = uicontrol('Style','text','String',' grid points:','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[.51, 0.54, 0.07, basichi]);

ZerolineText(6) = uicontrol('Style','text','String','not ok','HorizontalAlignment','center','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'ForegroundColor','r','Units','normalized','FontName',sty,'Position',[.585, 0.54, 0.045, basichi]);

ZerolineText(7) = uicontrol('Style','text','String',' data wrapping time: ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.51, 0.35, 0.12, 2*basichi],...
    'TooltipString', sprintf('The time matlab needs for the data conversion.'));

ZerolineText(8) = uicontrol('Style','text','String',' computation time: ','HorizontalAlignment','left','Fontsize',2*basicfsz,'BackgroundColor',bgc,...
    'Units','normalized','FontName',sty,'Position',[0.51, 0.26, 0.12, 2*basichi],...
    'TooltipString', sprintf('The time the C-process required for the baseline correction.'));


ZeroFunction = uicontrol('Style','popupmenu','String',...
    {'polynomial of degree 0', 'polynomial of degree 1', 'polynomial of degree 2', 'polynomial of degree 3', 'polynomial of degree 4', 'Gauss curve', 'Lorentz curve'},...
    'Fontsize',2*basicfsz,'BackgroundColor',bgc,'Units','normalized','FontName',sty,'Position',[0.51, 0.74, 0.12, basichi]);


% The axes for the rawdata and the corrected data
AxesRAW = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.165, 0.07, 0.335, 0.8]);
plot(AxesRAW,1,0,'w');

AxesZLF = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.65, 0.07, 0.335, 0.8]);
plot(AxesZLF,1,0,'w');


% The buttons with all UserData in emptybutton
emptybutton = uicontrol('Style','pushbutton','Position',[0.02, 0.87, 0.095, basichi],'Visible','off','Callback',{@emptybutton_CB});

data.isdata = 0;
data.isbd = 0;
data.iscut = 0;
data.iszlf = 0;
data.iszla = 0;
data.nobd = 0;
data.xbd = [];
data.ibd = [];

set(emptybutton,'UserData',data);
Button = [];

Button(7) = uicontrol('Style','pushbutton','UserData',0,'String','Save data','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.51, 0.15, 0.12, basichi+0.005], 'Enable', 'off',...
    'TooltipString', sprintf('Export the pretreated data.'),...
    'Callback',{@buttonsaveBLA, emptybutton, Button, ZerolineText});

Button(6) = uicontrol('Style','pushbutton','UserData',0,'String','Correct baseline','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.51, 0.44, 0.12, basichi+0.005], 'Enable', 'off',...
    'TooltipString', sprintf('Correct the baseline on the selected boundaries.'),...
    'Callback',{@buttonadjustbaselineBLA, emptybutton, Button, AxesRAW, AxesZLF, ZeroFunction, ZerolineText, calcs});

pos = get(DataText(1),'Position')+[0, -0.03, 0, 0.005];
Button(5) = uicontrol('Style','pushbutton','UserData',0,'String','Reset intervals','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',pos-[0, 0.3, 0, 0], 'Enable', 'off',...
    'TooltipString', sprintf('Reset the selected boundaries.'));

Button(4) = uicontrol('Style','pushbutton','UserData',0,'String','Reset cutting','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',pos-[0, 0.25, 0, 0], 'Enable', 'off',...
    'TooltipString', sprintf('Reset the selection.'),...
    'Callback',{@buttonresetcutBLA, emptybutton, Button, AxesRAW, AxesZLF, DataText, ZerolineText});

Button(3) = uicontrol('Style','pushbutton','UserData',0,'String','Cut data','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',pos-[0, 0.2, 0, 0], 'Enable', 'off',...
    'TooltipString', sprintf('Select a frequency subinterval.'),...
    'Callback',{@buttoncutBLA, emptybutton, Button, AxesRAW, AxesZLF, DataText, ZerolineText});

Button(2) = uicontrol('Style','pushbutton','UserData',0,'String','Transpose','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',pos+[0, 0.2, 0, 0], 'Enable', 'off',...
    'TooltipString', sprintf('Transpose data matrix.'),...
    'Callback',{@buttontransposeBLA, emptybutton, Button, AxesRAW, AxesZLF, DataText, ZerolineText});

Button(1) = uicontrol('Style','pushbutton','UserData',0,'String','Load data','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',pos+[0, 0.25, 0, 0],...
    'TooltipString', sprintf('Load mat-file containing data matrix D (spectra  x channels)\nand channels-vector "x".'),...
    'Callback',{@buttonloadBLA, emptybutton, Button, AxesRAW, AxesZLF, DataText, ZerolineText, titles});

set(Button(5), 'Callback',{@buttonresetzerobdsBLA, emptybutton, Button, AxesRAW, AxesZLF, DataText, ZerolineText});
set(ZeroFunction, 'Callback',{@checkbasefcnBLA, emptybutton, Button, AxesZLF, ZerolineText, ZeroFunction});

set(gcf,'WindowButtonDownFcn', {@fcnbdBLA, fig, emptybutton, Button, AxesRAW, AxesZLF})
set(gcf,'WindowButtonUpFcn', {@fcnbuBLA, emptybutton, Button, AxesRAW, ZerolineText, ZeroFunction})
set(gcf,'WindowButtonMotionFcn', {@fcnmoBLA, emptybutton, Button, AxesRAW})

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

function emptybutton_CB(hObject, eventdata)
data.ismatrix = 0;
data.isnmf = 0;
data.isafs = 0;
set(hObject,'UserData',data);
