function name=idlaytab(arg,number)
%IDLAYTAB Handles closing of ident, and the layout values.
%	The argument ARG takes the following values:
%	'figname' : Returns the name of figure number NUMBER
%	'save': Saving the layout-info in a variable XIDlayout on a file
%	       idprefs
%	'savepf': Saving the preferences in the variables XIDplotprefs and
%                 XIDstyleprefs on a file idprefs.
%	'def': Invoking default values for the layout and preferences.

%	L. Ljung 4-4-94
%	Copyright (c) 1986-95 by the MathWorks, Inc.
%	$Revision: 1.10 $  $Date: 1995/05/10 21:36:59 $


global XIDlayout XIDlaypath XIDsessions XIDplotprefs XIDstyleprefs XIDplotw
if strcmp(arg,'save')
    iduistat('Saving layout info from the windows...')
end
if strcmp(arg,'savepf')
    iduistat('Saving plot preferences from the windows...')
end

figure_names=str2mat(...
'Time Plot',...		      		%1
'Frequency Function',...   		%2
'Model Output',...		        %3
'Zeros and Poles',...			%4
'Transient Response',...		%5
'Residual Analysis',...			%6
'Noise Spectrum',...			%7
'Data/model Information',...		%8
'ARX Model Structure Selection');	%9

figure_names=str2mat(figure_names,...
'Model Order Selection',...		%10
'Choice of Auxiliary Order',...		%11
'',...					%12
'Output and Input Spectra',...		%13
'Select Range',...			%14
'Filter',...				%15
'ident: Untitled',...			%16
'Import Model',...			%17
'Import Data Variable');		%18

figure_names=str2mat(figure_names,...
'Import Data',...			%19
'Parametric Models',...			%20
'Order Editor',...			%21
'Iteration Control',...			%22
'Set Options 1',...			%23
'Set Options 2',...			%24
'Set Options 3',...			%25
'Set Options 4',...			%26
'Set Axis Scaling');			%27

figure_names=str2mat(figure_names,...
'Select Channels',...			%28
'Set Confidence',...			%29
'Resample',...				%30
'Interactive Demo of ident',...	        %31
'Correlation Model',...			%32
'Spectral Model',...			%33
'Trash',...				%34
'');					%35

if strcmp(arg,'figname')
   name=deblank(figure_names(number,:));
elseif strcmp(arg,'save')
   if isempty(XIDlaypath)
      errordlg(str2mat(...
  'No directory where to store the layout information has been specified',...
  'First type HELP MIDPREFS in the command window, and select a directory',...
  'using the m-file midprefs, before saving the layout again.'));
      return
   end
   if isempty(XIDlayout)
      XIDlayout=zeros(35,4);
   end

   figures=get(0,'children');
   for kf=figures(:)'
     eval('tag=get(kf,''tag'');','tag=[];');
     if length(tag)>4
      if tag(1:4)=='sitb'
           number=eval(tag(5:length(tag)));
           XIDlayout(number,:)=get(kf,'pos');
       end
     end
    end
    iduistat(['Layout info saved. '])
   idlaytab('save_file');
elseif strcmp(arg,'savepf')
    pd=zeros(12,15);
    for kw=[1,14]
       usd=get(XIDplotw(kw,2),'userdata');
       pd(12,kw)=eval(usd);
    end
    for kw=[2,7,13,15]
       usd=get(XIDplotw(kw,2),'userdata');
       pd(2,kw)=eval(usd(1,:));
       pd(3,kw)=eval(usd(2,:));
       pd(1,kw)=eval(usd(3,:));
       if kw>10,pd(11,kw)=eval(usd(5,:));end
    end
    usd=get(XIDplotw(3,2),'userdata');
    pd(4,3)=eval(usd(1,:));
    pd(5,3)=eval(usd(2,:));
    pd(6,3)=eval(usd(4,:));
    usd=get(XIDplotw(5,2),'userdata');
    pd(8,5)=eval(usd(1,:));
    pd(9,5)=eval(usd(2,:));
    pd(7,5)=eval(usd(3,:));
    usd=get(XIDplotw(6,2),'userdata');
    pd(10,6)=eval(usd(1,:));
    XIDplotprefs=pd;

    if isempty(XIDstyleprefs),XIDstyleprefs=idlayout('figdefs');end
    wins=[1:7,9,10,13,14,15];
    for kw=wins
      if iduiwok(kw)
        ch=findobj(XIDplotw(kw,1),'tag','confonoff');
        if ~isempty(ch),XIDstyleprefs(5,kw)=strcmp(get(ch,'checked'),'on');end
        ch=findobj(XIDplotw(kw,1),'tag','grid');
        if ~isempty(ch),XIDstyleprefs(4,kw)=strcmp(get(ch,'checked'),'on');end
        ch=findobj(XIDplotw(kw,1),'tag','zoom');
        if ~isempty(ch),XIDstyleprefs(6,kw)=strcmp(get(ch,'checked'),'on');end
        ch=findobj(XIDplotw(kw,1),'tag','solls');
        if ~isempty(ch),XIDstyleprefs(2,kw)=strcmp(get(ch,'checked'),'on');end
        ch=findobj(XIDplotw(kw,1),'tag','xnormal');
        if ~isempty(ch),XIDstyleprefs(1,kw)=strcmp(get(ch,'checked'),'on');end
      end
    end

    idlaytab('save_file')
elseif strcmp(arg,'def')
  XIDlayout=[];XIDplotprefs=[]; XIDstyleprefs=[];
   pd=idlayout('plotdefs');pd=pd(:);
   pd=pd*ones(1,15);
set(XIDplotw(1,2),'userdata',int2str(pd(12,1)));
set(XIDplotw(14,2),'userdata',int2str(pd(12,14)));
set(XIDplotw(2,2),'UserData',...
     str2mat(int2str(pd(2,2)),int2str(pd(3,2)),int2str(pd(1,2)),'[]'));
set(XIDplotw(3,2),'UserData',...
     str2mat(int2str(pd(4,3)),int2str(pd(5,3)),'[]',int2str(pd(6,3))));
set(XIDplotw(5,2),'UserData',...
     str2mat(int2str(pd(8,5)),num2str(pd(9,5)),int2str(pd(7,5))));
set(XIDplotw(6,2),'UserData',num2str(pd(10,6)));
set(XIDplotw(7,2),'UserData',...
     str2mat(int2str(pd(2,7)),int2str(pd(3,7)),int2str(pd(1,7)),'[]'));
set(XIDplotw(13,2),'userdata',...
     str2mat(int2str(pd(2,13)),int2str(pd(3,13)),...
             int2str(pd(1,13)),'[]',int2str(pd(11,13))));
set(XIDplotw(15,2),'userdata',...
     str2mat(int2str(pd(2,15)),int2str(pd(3,15)),int2str(pd(1,15)),'[]',...
            int2str(pd(11,15))));
%   for kwin=1:15
%       if iduiwok(kwin),iduiclpw(kwin);end
%   end

  str=str2mat(...
   'Windows that are created from now on will be given default sizes',...
   'and style and plot preferences. Already created ones are not affected.',...
   'You can Quit windows using the window manager and open them again',...
   'for default values.');
  helpdlg(str,'Default Window Sizes');
  idlaytab('save_file');
elseif strcmp(arg,'save_file')
   if isempty(XIDlaypath)
      return
   else
    err=0;
    eval(['save ',XIDlaypath,...
        'idprefs.mat XIDlaypath XIDsessions XIDlayout ',...
        'XIDplotprefs XIDstyleprefs'],'err=1;')
    if err,
       iduistat('Saving start-up info failed. Type HELP MIDPREFS for your options')
    else
       iduistat([' ',XIDlaypath,'idprefs.mat saved.'],1);
    end
   end
  
end % if save
