function hw=idbuildw(number)
%IDBUILDW This function handles the creation of all the ident VIEW windows.
%       Twelve different windows are handled, with names corresponding to
%       the input argument NUMBER according to the table in idlaytab.

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

global  XIDlayout XIDstyleprefs XIDplotw XIDhw XIDstatus
s1='iduipoin(1);';s2='iduipoin(1);iduistat(''Compiling ...'');';
s3='iduipoin(2);';
fz=10; % Fontsize for axes,title and labels

fw='normal'; % Fontweight for ditto  ( 'bold' is an alternative)
map = idlayout('plotcol',number); % The colors associated with the plots
col = map(1,:);
ftcol = map(2,:);
fticol = map(3,:);
AxesColor = map(4,:);

title=iduigetp(number,'name');
iduistat(['Opening ',title,' window ...'])
if figflag(title,0)
   hw=gcf;return
end
posfig=[];
if length(XIDlayout)>=number,if XIDlayout(number,3)
       	posfig=XIDlayout(number,1:4);
end,end
if isempty(posfig)
   pos1=get(0,'Screensize');
   if any(number==[3,9,10,14,15])
      posWH=min(pos1(3:4)*1/2,[360 300]);
   else
      posWH=min(pos1(3:4)*1/2,[330 290]);
   end
   posXY=max(pos1(3:4)-posWH-(number-1)*[40 40],[0 0]);
   posfig=[posXY posWH];
end
tag=['sitb',int2str(number)];

posWH=posfig(3:4);posXY=posfig(1:2);
hw=figure('pos',posfig,'NumberTitle','off','name',title,...
		   'NextPlot','New','Visible','off','tag',tag,...
                    'color',col);
set(hw,'menubar','none');
XIDplotw(number,1)=hw;
if number==8
   idbwtext(hw,posXY),return
end
% Main menu items:
   if isempty(XIDstyleprefs)
      defaults=idlayout('figdefs');defaults=defaults(:,number);
   else
      defaults=XIDstyleprefs(:,number);
   end
   [label,acc]=menulabel('&File');
   hf=uimenu(hw,'Label',label);
   [label,acc]=menulabel('&Options');
   ho=uimenu(hw,'Label',label);
   [label,acc]=menulabel('&Style');
   ha=uimenu(hw,'Label',label);
   if any(number==[1:7,13:15])
      [label,acc]=menulabel('&Channel');
      hch=uimenu(hw,'Label',label);
      XIDplotw(number,3)=hch;
   end
   [label,acc]=menulabel('&Help');
   hh=uimenu(hw,'Label',label);
   [label,acc]=menulabel('&View explanation');
   hhelp=uimenu(hh,'Label',label);
   [label,acc]=menulabel('&General menu help');
   uimenu(hh,'Label',label,'callback',...
           'iduihelp(''idgview.hlp'',''Help: General Menu Items'');');
  if any(number==[2:7]) 
      [label,acc]=menulabel('&Confidence intervals');
      uimenu(hh,'Label',label,'callback',...
           'iduihelp(''iduiconf.hlp'',''Help: Confidence Intervals'');');
  end
   if number==9
      help='iduihelp(''selordax.hlp'',''Help: Choice of ARX Structure'');';
   elseif number==10
      help='iduihelp(''selordss.hlp'',''Help: Choice of Model Order'');';
   elseif number==2
      help='iduihelp(''bodeplot.hlp'',''Help: Frequency Response Plot'');';
      [label,acc]=menulabel('&Special options');
      uimenu(hh,'Label',label,'Callback',...
           'iduihelp(''idbodopt.hlp'',''Help: Options for Frequency Response'');');
   elseif number==3
      help='iduihelp(''compare.hlp'',''Help: Model Output'');';
      [label,acc]=menulabel('&Special options');
      uimenu(hh,'Label',label,'Callback',...
           'iduihelp(''idcompop.hlp'',''Help: Options for Model Output'');');

   elseif number==4
      help='iduihelp(''zpplot.hlp'',''Help: Zeros and Poles'');';
   elseif number==5
      help='iduihelp(''idtrresp.hlp'',''Help: Transient Response'');';
      [label,acc]=menulabel('&Special options');
      uimenu(hh,'Label',label,'Callback',...
           'iduihelp(''idtrropt.hlp'',''Help: Options for Transient Response'');');

   elseif number==6
      help='iduihelp(''resid.hlp'',''Help: Residual Analysis'');';
      [label,acc]=menulabel('&Special options');
      uimenu(hh,'Label',label,'Callback',...
           'iduihelp(''idresopt.hlp'',''Help: Options for Residual Analysis'');');

   elseif number==7
      help='iduihelp(''idspect.hlp'',''Help: Disturbance Spectrum'');';
      [label,acc]=menulabel('&Special options');
      uimenu(hh,'Label',label,'Callback',...
           'iduihelp(''idbodopt.hlp'',''Help: Options for Disturbance Spectrum'');');

   elseif number==11
      help='iduihelp(''selauxo.hlp'',''Help: Choice of Auxiliary Order'');';
   elseif number==1
      help='iduihelp(''idplot.hlp'',''Help: Input Output Time Plot'');';
      [label,acc]=menulabel('&Special options');
   uimenu(hh,'Label',label,'Callback',...
           'iduihelp(''idplotop.hlp'',''Help: Options for Time Plots'');');

   elseif number==13
      help='iduihelp(''iduyspe.hlp'',''Help: Input Output Spectra'');';
      [label,acc]=menulabel('&Special options');
      uimenu(hh,'Label',label,'Callback',...
           'iduihelp(''iduyspop.hlp'',''Help: Options for Data Spectra'');');
   elseif number==14
      help='iduihelp(''idsel.hlp'',''Help: Select Input Output Time Range'');';
   elseif number==15
      help='iduihelp(''idfilt.hlp'',''Help: Prefilter Input Output Data'');';
      [label,acc]=menulabel('&Special options');
      uimenu(hh,'Label',label,'Callback',...
           'iduihelp(''iduyspop.hlp'',''Help: Options for Data Spectra'');');
   end
   set(hhelp,'callback',help);   
   if any(number==[9 10])
      XIDplotw(number,3)=hhelp;
   end

   % Submenues for FILE
   [label,acc]=menulabel('Copy &figure');
   uimenu(hf,'Label',label,'callback',...
         [s1,'idunlink(',int2str(number),');',s3]);
   [label,acc]=menulabel('&Print');
   uimenu(hf,'Label',label,'callback', ...
    [s1,'printdlg;',s3]);
   [label,acc]=menulabel('&Close ^w');
   ht=uimenu(hf,'Label',label,'separator','off','Accelerator',acc,...
            'callback',[s1,'iduiaxes(''close'',',int2str(number),');',s3]);
   if number==14,set(ht,'callback',[s1,'iduisel(''done'');',s3]);end
   % Submenus for OPTIONS
   [label,acc]=menulabel('&Autorange ^a');
   uimenu(ho,'Label',label,'accelerator',acc,...
                 'callback',[s1,'iduiaxes(''auto'');',s3]);
   [label,acc]=menulabel('Set axes &limits... ^m');
   uimenu(ho,'Label',label,'accelerator',acc,...
                 'callback',[s1,'iduiaxis(''open'',gcf);',s3]);
   if any(number==[2:7])
      hco=ho;
      [label,acc]=menulabel('Show 99% confidence &intervals ^i');
      h1=uimenu(hco,'Label',...
          label,'accelerator',acc,'callback',[s1,'iduiconf(''onoff'');',s3],...
           'userdata',2.5758,'tag','confonoff','interruptible','yes',...
           'separator','on');
      [label,acc]=menulabel('Set &confidence level');
      h2=uimenu(hco,'Label',label);
        [label,acc]=menulabel('9&5%');
        uimenu(h2,'label',label,'userdata',1.96,'callback',...
        [s1,'iduiconf(''set'');',s3]);
        [label,acc]=menulabel('9&7%');
        uimenu(h2,'label',label,'userdata',2.1701,'callback',...
        [s1,'iduiconf(''set'');',s3]);
        [label,acc]=menulabel('9&9%');
        uimenu(h2,'label',label,'userdata',2.5758,...
                     'callback',[s1,'iduiconf(''set'');',s3]);
        [label,acc]=menulabel('99&.9%');
        uimenu(h2,'label',label,'userdata',3.2905,...
                     'callback',[s1,'iduiconf(''set'');',s3]);
        [label,acc]=menulabel('&Other...');
        uimenu(h2,'label',label,...
                 'callback',[s1,'iduiconf(''conf'');',s3]);
      if defaults(5)
         set(h1,'checked','on')
      end
   end
   
   idvmenus(number,ho,'options');

   % SUBMENUS for Style
   [label,acc]=menulabel('&Grid ^g');
   h1=uimenu(ha,'Label',label,'accelerator',acc,'separator','off',...
                       'callback',[s1,'iduiaxes(''grid'');',s3],'tag','grid');
   if defaults(4)==0,set(h1,'checked','on'),end % will be toggled later
    hgrid=h1;
%   [label,acc]=menulabel('&Box');
%   h1=uimenu(ha,'Label',label,'vis','off',...
%                        'callback',[s1,'iduiaxes(''box'');',s3]);
%   if defaults(3)==1,set(h1,'checked','on'),end
   [label,acc]=menulabel('&Zoom');
   hz=uimenu(ha,'Label',label,'separator','on','callback',...
        [s1,'iduimbcb(''setzoom'',',int2str(number),');',s3],'tag','zoom');
   if defaults(6)==0
      set(hz,'checked','on') % Will be toggled later
   else
      set(hz,'checked','off')
   end
   idvmenus(number,ha,'style');
   if any(number==[1:3,5:7,13:15])
     [label,acc]=menulabel('S&eparate linestyles');
     h1=uimenu(ha,'Label',label,...
                  'callback',[s1,'iduiaxes(''sep_on'');',s3],...
                  'separator','on','tag','sepls');
     [label,acc]=menulabel('&All Solid Lines');
     h2=uimenu(ha,'Label',label,'callback',...
                [s1,'iduiaxes(''sep_off'');',s3],'userdata',h1,'tag','solls');
     set(h1,'userdata',h2);
     if defaults(2)==1, chn2=h2;else chn2=h1;end
     set(chn2,'checked','on')
   end
   [label,acc]=menulabel('Erasemode &xor');
   h1=uimenu(ha,'Label',label,'callback',...
                   'iduiaxes(''xor'')','separator','on','vis','on','tag','xor');
   [label,acc]=menulabel('Erasemode &normal');
   h2=uimenu(ha,'Label',label,'callback',...
                   'iduiaxes(''ernormal'')','userdata',h1,'vis','on','tag','xnormal');
   set(h1,'userdata',h2)
   if defaults(1)==1, chn1=h2;else chn1=h1;end
   set(chn1,'checked','on')

   % Now follows the basic AXES settings, with the axes handles
   % made userdata (3-- rd item) of the figure

   if any(number==[1 2 6 13]) % Two axes plots
      pos=idlayout('axes',number);
      xax(1,1)=axes('position',pos(1,:),'box','on',...
                  'drawmode','fast','xticklabels',[],'tag','axis1',...
                  'color',AxesColor,'xcolor',fticol,'ycolor',fticol,'fontsize',fz);
      set(get(xax(1,1),'title'),'fontsize',fz,'color',ftcol,'fontweight',fw);
      set(get(xax(1,1),'xlabel'),'fontsize',fz,'color',ftcol,'fontweight',fw);
      set(get(xax(1,1),'ylabel'),'fontsize',fz,'color',ftcol,'fontweight',fw);
      xax(2,1)=axes('position',pos(2,:),'box','on',...
                  'drawmode','fast','tag','axis2','color',AxesColor,...
                  'xcolor',fticol,'ycolor',fticol,'fontsize',fz);
      set(get(xax(2,1),'title'),'fontsize',fz,'color',ftcol,'fontweight',fw);
      set(get(xax(2,1),'xlabel'),'fontsize',fz,'color',ftcol,'fontweight',fw);
      set(get(xax(2,1),'ylabel'),'fontsize',fz,'color',ftcol,'fontweight',fw);

   elseif any(number==[14 15]) % Two axes plots with buttons
      pos = idlayout('axes',number);
      xax(1,1)=axes('position',pos(1,:),'box','on',...
                  'drawmode','fast','xticklabels',[],'tag','axis1',...
                  'color','k','xcolor',fticol,'ycolor',fticol,'fontsize',fz);
      set(get(xax(1,1),'title'),'fontsize',fz,'color',ftcol,'fontweight',fw);
      set(get(xax(1,1),'xlabel'),'fontsize',fz,'color',ftcol,'fontweight',fw);
      set(get(xax(1,1),'ylabel'),'fontsize',fz,'color',ftcol,'fontweight',fw);

      xax(2,1)=axes('position',pos(2,:),'box','on',...
                  'drawmode','fast','tag','axis2','color','k',...
                   'xcolor',fticol,'ycolor',fticol,'fontsize',fz);
      set(get(xax(2,1),'title'),'fontsize',fz,'color',ftcol,'fontweight',fw);
      set(get(xax(2,1),'xlabel'),'fontsize',fz,'color',ftcol,'fontweight',fw);
      set(get(xax(2,1),'ylabel'),'fontsize',fz,'color',ftcol,'fontweight',fw);
 
   elseif any(number==[3,9,10])   % Single plots with buttons
      pos = idlayout('axes',number);
      xax(1,1)=axes('position',pos(1,:),'box','on',...
                  'drawmode','fast','tag','axis1','color','k',...
                  'xcolor',fticol,'ycolor',fticol,'fontsize',fz);
      set(get(xax(1,1),'title'),'fontsize',fz,'color',ftcol,'fontweight',fw);
      set(get(xax(1,1),'xlabel'),'fontsize',fz,'color',ftcol,'fontweight',fw);
      set(get(xax(1,1),'ylabel'),'fontsize',fz,'color',ftcol,'fontweight',fw);
      if number==3
        xtab=axes('position',[0.72 0.15 0.27 0.75],'box','on',...
                  'drawmode','fast','tag','table','color',col,...
              'XColor',col,'YColor',col,'Ydir','reverse',...
              'ytick',[],'xtick',[],'xticklabels',[],'yticklabels',[]);
        axes(xtab)
        text(0.5,0.95,'Best Fits','color',ftcol,'units','norm',...
                 'fontsize',10,'Horizontalalignment','center','tag','leader');
      end
   else                        % Single plots
      pos = idlayout('axes',number);
      xax=axes('position',pos,'box','on',...
                  'drawmode','fast','tag','axis1','color','k',...
                  'xcolor',fticol,'ycolor',fticol,'fontsize',fz);
      set(get(xax,'title'),'fontsize',fz,'color',ftcol,'fontweight',fw);
      set(get(xax,'xlabel'),'fontsize',fz,'color',ftcol,'fontweight',fw);
      set(get(xax,'ylabel'),'fontsize',fz,'color',ftcol,'fontweight',fw);
   end
   set(xax(:,1),'interruptible','yes');
   layout
   pos=[0.9*mStdButtonWidth,3*mStdButtonHeight];
   uicontrol(hw,'pos',[0 0 pos],'vis','off','style','edit',...
      'max',2,'tag','infobox');
%   if any(number==[9 10 14 15])
   XIDstatus(number) = uicontrol(hw,'Style','text','String','', ...
 'Position',[mEdgeToFrame mEdgeToFrame posWH(1)-2*mEdgeToFrame 0.8*mStdButtonHeight]);
%   end

   set(hw,'interruptible','yes','windowbuttonupfcn','1;');
   if number==3,fpos=posfig(4);else fpos=0;end
           %Model output uses userdata to detect resize
   set(hw,'Userdata',[fpos;0;xax]); 
   if any(number==[1 2 4 5 7 13]),iduiiono('set',[],number);end
   if any(number==[3 6]),iduiiono('set',get(XIDhw(4,1),'userdata'),number);end
   if any(number==[14 15]),iduiiono('set',get(XIDhw(3,1),'userdata'),number);end
   iduimbcb('setzoom',number);
   iduiaxes('grid',1,hgrid);
   iduital(number);
   set(hw,'vis','on');
