function sbhndlopts(cmd,opt,varargin)
%SBHNDLOPTS
%
%  See also

% Initial coding by A. S. Bozin, 1-07-98.
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.7 $ $Date: 2000/06/14 16:07:50 $

   %
   % Action depends on CMD
   %
   switch (cmd)
   case 'tabcallbk'
      %
      % Set a new sheet
      %
      hFig = get(0,'CurrentFigure');
      ud = get(hFig,'UserData');
      switch (opt)
      case 'Build'
	 set([ud.hrep;ud.hcon],'Visible','off');
	 set(ud.hbld,'Visible','on');
	 ud.sheet = 1;
      case 'Report Layout'
	 set([ud.hbld;ud.hcon],'Visible','off');
	 set(ud.hrep,'Visible','on');
	 ud.sheet = 2;
      case 'Report Format'
	 set([ud.hbld;ud.hrep],'Visible','off');
	 if (get(ud.hcon(1,1),'Value') == 1)
	    set(ud.hcon,'Visible','on');
	 else
	    set(ud.hcon(:),{'Visible'},{'on';'on';'off';'on';'on';'off'});
	 end
	 ud.sheet = 3;
      otherwise
      end
      set(hFig,'UserData',ud);
   case 'style'
      %
      %
      %
      hFig = get(0,'CurrentFigure');
      ud = get(hFig,'UserData');
      if (get(ud.hcon(1,1),'Value') == 1)
	 optstr = {'Single page web';'Multi page web'};
	 set(ud.hcon(3,:),'Visible','on');
      else
	 optstr = {'Standard print';'Simple print';'Large type print'};
	 set(ud.hcon(3,:),'Visible','off');
      end
      set(ud.hcon(2,1),'String',optstr,'Value',1);
   case 'apply'
      %
      % Set a new working partition
      %
      hFig = get(0,'CurrentFigure');
      ud = get(hFig,'UserData');
      bldopt = get(ud.hbld(:,1),'Value');
      repopt = get(ud.hrep(:,1),'Value');
      conopt = get(ud.hcon(:,1),'Value');
      shh = get(0,'ShowHiddenHandles');
      set(0,'ShowHiddenHandles','on');
      sFig = findobj(get(0,'Children'),'flat','Tag','sb2slTag');
      set(0,'ShowHiddenHandles',shh);
      ud = get(sFig,'UserData');
      ud.bldopt = bldopt;
      if (any([ud.repopt{:}] ~= [repopt{:}]))
	 ud.state = bitand(ud.state,31);
      end
      ud.repopt = repopt;
      ud.conopt = conopt;
      set(sFig,'UserData',ud);		   	   % Update options
      if (strcmp(opt,'off'))
	 set(hFig,'Visible','off');
      end
   case 'close'
      %
      % Close the window
      %
      hFig = get(0,'CurrentFigure');
      set(hFig,'Visible','off');
   case 'create'
      %
      % Create the list of partitions window
      %
      shh = get(0,'ShowHiddenHandles');
      set(0,'ShowHiddenHandles','on');
      kids = get(0,'Children');
      set(0,'ShowHiddenHandles',shh);
      hFig = findobj(kids,'flat','Tag','hndloptsTag');
      if (length(hFig) > 1)			   % There is another figure with the same tag
	 error('sb2sl: sbhndlopts detected more than one window.');
      elseif (length(hFig) == 1)		   % Option window already created
	 set(0,'CurrentFigure',hFig);
	 set(hFig,'Visible','on');
      else
	 %
	 % Get preferences
	 %
	 sFig = findobj(kids,'flat','Tag','sb2slTag');
	 ud = get(sFig,'UserData');
	 winpref = ud.winpref;
	 %
	 % Create a new window
	 %
	 bgc = get(0,'DefaultUIControlBackGroundColor');
	 rect = [490 300 300 232];
	 hFig = figure('Units','pixels',...
		       'Position',rect,...
		       'Color',bgc,...
		       'Resize','off',...
		       'Tag','hndloptsTag',...
		       'IntegerHandle','off',...
		       'Visible','off',...
		       'HandleVisibility','callback',...
		       'MenuBar','none',...
		       'Name','SB2SL: Translator Options',...
		       'NumberTitle','off',...
		       'DeleteFcn','sbhndlopts(''delete'');',...
		       'ResizeFcn','sbhndlopts(''resize'');');
	 %
	 % Create a tabbed dialog box
	 %
	 tabLabs = {'Build','Report Layout','Report Format'};
	 tabDims = {[50 90 90],14};
	 shtDims = [280 160];
	 offDims = [9 10 10 39];
	 [hFig,sheetPos] = tabdlg('create',tabLabs,tabDims,'sbhndlopts',shtDims,offDims,1,...
				  {'Helvetica',winpref.varSize(2)},hFig);
	 hbutts = findobj(get(hFig,'Children'),'flat','Style','pushbutton');
	 set(hbutts,'Tag','varNormal');
	 %
	 % Create objects on the first sheet
	 %
	 labels = {'Fit system to view',...
		   'Add Terminator and Ground blocks',...
		   'Avoid block crossing',...
		   'Ignore output posting for triggered SuperBlocks',...
		   'Convert idle SuperBlocks',...
		   'Optimize translated model'};
	 nopts = size(labels,2);
	 hbld = zeros(nopts,2);
	 rect = [36 166 234 20];
	 for (k = 1:nopts)
	    hbld(k,1) = uicontrol('Parent',hFig,...
				  'Style','togglebutton',...
				  'Units','pixels',...
				  'Position',[rect(1)-16,rect(2)+8,12,12],...
				  'Interruptible','on',...
				  'FontName','Helvetica',...
				  'FontSize',winpref.varSize(2),...
				  'Tag','varNormal',...
				  'HorizontalAlignment','center',...
				  'String','');
	    hbld(k,2) = uicontrol('Parent',hFig,...
				  'Style','text',...
				  'Units','pixels',...
				  'Position',rect,...
				  'Interruptible','off',...
				  'FontName','Helvetica',...
				  'FontSize',winpref.varSize(2),...
				  'Tag','varNormal',...
				  'HorizontalAlignment','left',...
				  'String',labels{k});
	    rect(2) = rect(2)-24;
	 end
	 set(hbld(:),'Units','normalized');
	 %
	 % Create objects on the second sheet
	 %
	 labels = {'Catalog of SuperBlocks',...
		   'Detailed SuperBlock information',...
		   'Partitions and parameters',...
		   'List of Blockscript blocks',...
		   'List of unconverted blocks'};
	 nopts = size(labels,2);
	 hrep = zeros(nopts,2);
	 rect = [36 166 194 20];
	 for (k = 1:nopts)
	    hrep(k,1) = uicontrol('Parent',hFig,...
				  'Style','togglebutton',...
				  'Units','pixels',...
				  'Position',[rect(1)-16,rect(2)+8,12,12],...
				  'Interruptible','on',...
				  'FontName','Helvetica',...
				  'FontSize',winpref.varSize(2),...
				  'Tag','varNormal',...
				  'HorizontalAlignment','center',...
				  'String','');
	    hrep(k,2) = uicontrol('Parent',hFig,...
				  'Style','text',...
				  'Units','pixels',...
				  'Position',rect,...
				  'Interruptible','off',...
				  'FontName','Helvetica',...
				  'FontSize',winpref.varSize(2),...
				  'Tag','varNormal',...
				  'HorizontalAlignment','left',...
				  'String',labels{k});
	    rect(2) = rect(2)-24;
	 end
	 set(hrep(:),'Units','normalized');
	 %
	 % Create objects on the third sheet
	 %
	 labels = {'Format','Stylesheet'};
	 optstr = {{'Web (HTML)';...
		    'Rich Text Format 95 (RTF)';...
		    'Rich Text Format 97 (RTF)';...
		    'LaTeX (TEX)'} ...
		   {'Single page web';
		    'Multi page web'}};
	 nopts = size(labels,2);
	 hcon = zeros(nopts+1,2);
	 rect = [20 166 68 20];
	 for (k = 1:nopts)
	    hcon(k,2) = uicontrol('Parent',hFig,...
				  'Style','text',...
				  'Units','pixels',...
				  'Position',rect,...
				  'Interruptible','off',...
				  'FontName','Helvetica',...
				  'FontSize',winpref.varSize(2),...
				  'Tag','varNormal',...
				  'HorizontalAlignment','left',...
				  'String',labels{k});
	    hcon(k,1) = uicontrol('Parent',hFig,...
				  'Style','popup',...
				  'Units','pixels',...
				  'Position',[rect(1)+70 rect(2)+4 160 20],...
				  'Interruptible','on',...
				  'FontName','Helvetica',...
				  'FontSize',winpref.varSize(2),...
				  'Tag','varNormal',...
				  'HorizontalAlignment','left',...
				  'String',optstr{k},...
				  'Value',1);
	    rect(2) = rect(2)-24;
	 end
	 set(hcon(1,1),'CallBack','sbhndlopts(''style'');');
	 rect = [36 118 194 20];
	 hcon(nopts+1,1) = uicontrol('Parent',hFig,...
				     'Style','togglebutton',...
				     'Units','pixels',...
				     'Position',[rect(1)-16,rect(2)+8,12,12],...
				     'Interruptible','on',...
				     'FontName','Helvetica',...
				     'FontSize',winpref.varSize(2),...
				     'Tag','varNormal',...
				     'HorizontalAlignment','center',...
				     'String','');
	 hcon(nopts+1,2) = uicontrol('Parent',hFig,...
				     'Style','text',...
				     'Units','pixels',...
				     'Position',rect,...
				     'Interruptible','off',...
				     'FontName','Helvetica',...
				     'FontSize',winpref.varSize(2),...
				     'Tag','varNormal',...
				     'HorizontalAlignment','left',...
				     'String','View report after conversion');
	 set(hcon(:),'Units','normalized');
	 %
	 % Command buttons
	 %
	 labels = {'OK','Cancel','Apply'};
	 callBacks = {'sbhndlopts(''apply'',''off'');',...
		      'sbhndlopts(''close'');',...
		      'sbhndlopts(''apply'',''on'');'};
	 toolTips = {'Set new options and close the window',...
		     'Close the window',...
		     'Set new options'};
	 nbutts = size(labels,2);
	 rect = [20 10 80 20];
	 hbutts = zeros(nbutts,1);
	 for (k = 1:nbutts)
	    hbutts(k) = uicontrol('Parent',hFig,...
				  'Style','pushbutton',...
				  'Units','pixels',...
				  'Position',rect,...
				  'CallBack',callBacks{k},...
				  'Interruptible','on',...
				  'FontName','Helvetica',...
				  'FontSize',winpref.varSize(2),...
				  'Tag','varNormal',...
				  'HorizontalAlignment','center',...
				  'TooltipString',toolTips{k},...
				  'String',labels{k});
	    rect(1) = rect(1)+90;
	 end
	 set(hbutts,'Units','normalized');
	 %
	 % Save the handles
	 %
	 ud = struct('sheet',1,'hbld',hbld,'hrep',hrep,'hcon',hcon);
	 set(hFig,'Visible','on','UserData',ud);
      end
      %
      % Update options
      %
      sFig = findobj(kids,'flat','Tag','sb2slTag');
      ud = get(sFig,'UserData');
      bldopt = ud.bldopt;
      repopt = ud.repopt;
      conopt = ud.conopt;
      ud = get(hFig,'UserData');
      set(ud.hbld(:,1),{'Value'},bldopt);	   % Update sheets
      set(ud.hrep(:,1),{'Value'},repopt);
      set(ud.hcon(:,1),{'Value'},conopt);
      switch (ud.sheet)
      case 1
	 set([ud.hrep;ud.hcon],'Visible','off');
	 set(ud.hbld,'Visible','on');
      case 2
	 set([ud.hbld;ud.hcon],'Visible','off');
	 set(ud.hrep,'Visible','on');
      case 3
	 set([ud.hbld;ud.hrep],'Visible','off');
	 if (conopt{1} == 1)
	    set(ud.hcon,'Visible','on');
	 else
	    set(ud.hcon(:),{'Visible'},{'on';'on';'off';'on';'on';'off'});
	 end
      otherwise
      end
   case 'resize'
      %
      %
      %
   case 'delete'
      %
      %
      %
   case 'compile'
      % 
      % Compilation message
      % 
      sbhndlmsgs('write','.','append');	   % Show progress
   otherwise
      error('sb2sl: sbhndlopts called with incorrect argument.');
   end 
   return					   % Last card of SBHNDLOPTS
