function subGUI4LIB(version,nr,basicfsz)
% The toolbox FACPACK, which includes the function subGUI4LIB, provides software 
% for a Peak group analysis and for the computation of multi-component factorizations.
% Copyright (c) 2015 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, 'subfcnsLIB'];
path(path, calcp);
path(path, calcs);

scrsz = get(0,'ScreenSize');
fig = figure(nr);
pause(.1);
set(fig,'Position',[[1/8 1/4].*scrsz(3:4) [3/4 1/2].*scrsz(3:4)]);
set(fig,'Name',['FACPACK (Library of computed pure component spectra, version ',num2str(version,'%2.2f'), ')'],...
    'NumberTitle','off','MenuBar','none','Toolbar','figure','color','white','Visible','off');

if ispc
%     basicfsz = 5;
    basichi = 0.022;
else
%     basicfsz = 6.5;
    basichi = 0.022;
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];
% lcs = [0 0 1;0 0.5 0;1 0 0;0 .8 .8;.8 0 .8;.8 .8 0;0 0 0];
lcs = [0 0.4470 0.7410; 0.8500 0.3250 0.0980; 0.9290 0.6940 0.1250; 0.4940 0.1840 0.5560; 0.4660 0.6740 0.1880; 0.3010 0.7450 0.9330];
LCS = [0.85 0.9 1; 0.995 0.8250 0.75; 1 0.9 0.75; 0.9 0.6 1; 0.7 1 0.5; 0.7 0.9 1];

Axes = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.55, 0.1, 0.425, 0.85]);
plot(Axes,1,0,'w');

pos = [0.02, 0.39, 0.15, 1.9*basichi];
InfoText(1) = uicontrol('Style','text','String','Information on the spectrum:','HorizontalAlignment','left','Fontsize',2*basicfsz,...
    'BackgroundColor',bgw,'Units','normalized','FontName',sty,'Position',pos+[0, 0, 0.35, 0]);

InfoText(2) = uicontrol('Style','text','String','Name:','HorizontalAlignment','left','Fontsize',2*basicfsz,...
    'BackgroundColor',bgw,'Units','normalized','FontName',sty,'Position',pos-[0, 0.05, 0, 0]);

InfoText(3) = uicontrol('Style','text','String','Size:','HorizontalAlignment','left','Fontsize',2*basicfsz,...
    'BackgroundColor',bgw,'Units','normalized','FontName',sty,'Position',pos-[0, 0.09, 0, 0]);

InfoText(4) = uicontrol('Style','text','String','Generated:','HorizontalAlignment','left','Fontsize',2*basicfsz,...
    'BackgroundColor',bgw,'Units','normalized','FontName',sty,'Position',pos-[0, 0.13, 0, 0]);

InfoText(5) = uicontrol('Style','text','String','Original data set:','HorizontalAlignment','left','Fontsize',2*basicfsz,...
    'BackgroundColor',bgw,'Units','normalized','FontName',sty,'Position',pos-[0, 0.17, 0, 0]);

InfoText(6) = uicontrol('Style','text','String','Frequencies:','HorizontalAlignment','left','Fontsize',2*basicfsz,...
    'BackgroundColor',bgw,'Units','normalized','FontName',sty,'Position',pos-[0, 0.21, 0, 0]);

InfoText(7) = uicontrol('Style','text','String','FACPACK module:','HorizontalAlignment','left','Fontsize',2*basicfsz,...
    'BackgroundColor',bgw,'Units','normalized','FontName',sty,'Position',pos-[0, 0.25, 0, 0]);

pos = [0.125, 0.345, 0.395, 1.7*basichi];
Info(2) = uicontrol('Style','text','String','','HorizontalAlignment','left','Fontsize',2*basicfsz,...
    'BackgroundColor',bgw,'Units','normalized','FontName',sty,'Position',pos);

Info(3) = uicontrol('Style','text','String','','HorizontalAlignment','left','Fontsize',2*basicfsz,...
    'BackgroundColor',bgw,'Units','normalized','FontName',sty,'Position',pos-[0, 0.04, 0, 0]);

Info(4) = uicontrol('Style','text','String','','HorizontalAlignment','left','Fontsize',2*basicfsz,...
    'BackgroundColor',bgw,'Units','normalized','FontName',sty,'Position',pos-[0, 0.08, 0, 0]);

Info(5) = uicontrol('Style','text','String','','HorizontalAlignment','left','Fontsize',2*basicfsz,...
    'BackgroundColor',bgw,'Units','normalized','FontName',sty,'Position',pos-[0, 0.12, 0, 0]);

Info(6) = uicontrol('Style','text','String','','HorizontalAlignment','left','Fontsize',2*basicfsz,...
    'BackgroundColor',bgw,'Units','normalized','FontName',sty,'Position',pos-[0, 0.16, 0, 0]);

Info(7) = uicontrol('Style','text','String','','HorizontalAlignment','left','Fontsize',2*basicfsz,...
    'BackgroundColor',bgw,'Units','normalized','FontName',sty,'Position',pos-[0, 0.2, 0, 0]);


columnname =   {'Plot', 'Name', 'Size', 'Generated', 'Module', 'Normalization'};
columnformat = {'logical', 'char', 'char', 'char', 'char', {'original' 'normalized'}};
columneditable =  [true false false false false true]; 
t = uitable('Units','normalized','Position',[0.02 0.44 0.5 0.51],'ColumnName', columnname,'ColumnFormat', columnformat,...
            'ColumnEditable', columneditable,'RowName', [], 'Units','normalized',...
            'Fonts', 2*basicfsz, 'FontName', sty, 'BackgroundColor', ones(2,3));

if exist([calcs,'/FACPACKlibrary.mat'])
    load([calcs,'/FACPACKlibrary.mat']);
else
    lib = {};
end

dat = [];
for i=1:length(lib)
    dat{i,1} = false;
    dat{i,2} = lib(i).name;
    dat{i,3} = num2str(length(lib(i).a),'%d');
    dat{i,4} = lib(i).date;
    dat{i,5} = lib(i).module;
    dat{i,6} = 'original';
end


px = get(fig,'Position');
pos = get(t,'Position');

columnwidth = num2cell(([0.075, 0.4, 0.1, 0.175, 0.085, 0.145]*px(3)*pos(3)));
set(t,'ColumnWidth', columnwidth,'Units','normalized', 'Data', dat);

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

data.isselected = 0;
data.lib = lib;

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

Button(1) = uicontrol('Style','pushbutton','UserData',0,'String','Delete from library','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.02, 0.08, 0.245, 2.5*basichi],'Enable','off',...
    'TooltipString', sprintf('Delete the selected spectrum, so it does not exist in the library furthermore.'),...
    'Callback',{@buttondeleteLIB, emptybutton, Button, Axes, t, Info, fig, basicfsz, sty, calcs});

Button(2) = uicontrol('Style','pushbutton','UserData',0,'String','Add a new spectrum to library','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.02, 0.02, 0.245, 2.5*basichi],...
    'TooltipString', sprintf('Import a new spectrum from workspace.'));

Button(3) = uicontrol('Style','pushbutton','UserData',0,'String','Export as *.txt','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.275, 0.08, 0.245, 2.5*basichi],'Enable','off',...
    'TooltipString', sprintf('Export the selected spectrum as *.txt file.'),...
    'Callback',{@buttonexportLIB, emptybutton, Button, Axes, t, Info, fig, 1});

Button(4) = uicontrol('Style','pushbutton','UserData',1,'String','Export as *.mat','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.275, 0.02, 0.245, 2.5*basichi],'Enable','off',...
    'TooltipString', sprintf('Export the selected spectrum as *.mat file.'),...
    'Callback',{@buttonexportLIB, emptybutton, Button, Axes, t, Info, fig, 2});

Button(5) = uicontrol('Style','pushbutton','UserData',1,'String','Reload library','HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
    'Units','normalized','FontName',sty,'Position',[0.35, 0.38, 0.17, 2.5*basichi],'Enable','on',...
    'TooltipString', sprintf('Reload the entries in the library.'),...
    'Callback',{@buttonreloadLIB, emptybutton, Button, Axes, t, Info, calcs});

set(Button(2), 'Callback',{@buttonimportLIB, emptybutton, Button, t, Info, Axes, fig, basicfsz, sty, calcs, basichi});
set(t,'CellSelectionCallback',{@selectitemLIB, emptybutton, Button, Axes, t, Info});

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

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