function sbhndlinfo(cmd,varargin) 
%SBHNDLINFO Manages the block info window.
%
%  SBHNDLINFO creates a new figure or opens the existing one which
%  has been made invisible.
%  SBHNDLINFO(CMD) is used internally for managing callback calls.
%
%  See also

% Initial coding by A. S. Bozin, 5-15-97.
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.7 $ $Date: 2000/06/14 16:07:47 $
   % 
   % Handle input arguments, if any
   % 
   if (nargin == 0)
      cmd = 'create';
   end
   %
   % Action depends on CMD argument
   %
   switch (cmd)
   case 'show'
      %
      %
      %
      shh = get(0,'ShowHiddenHandles');
      set(0,'ShowHiddenHandles','on');
      sFig = findobj(get(0,'Children'),'flat','Tag','sb2slTag');
      set(0,'ShowHiddenHandles',shh);
      ud = get(sFig,'UserData');
      catalog = get(ud.catalog,'UserData');
      sFig = get(0,'CurrentFigure');
      sbhndlinfo('create');
      hFig = get(0,'CurrentFigure');
      ud = get(hFig,'UserData');
      node = varargin{1};
      str = {catalog(node).name;...
	     gettype(catalog(node).type,catalog(node).signal);...
	     int2str(catalog(node).nBlocks);...
	     int2str(catalog(node).nInputs);...
	     int2str(catalog(node).nOutputs)};
      set(ud.labels,{'String'},str);
      set([ud.lshift,ud.rshift],{'Enable'},{'inactive';'on'});
      sb = struct('ind',1,...
		  'str',str{1});
      set(ud.labels(1),'UserData',sb);
      set(0,'CurrentFigure',sFig);
   case 'lshift'
      %
      %
      %
      hFig = get(0,'CurrentFigure');
      ud = get(hFig,'UserData');
      sb = get(ud.labels(1),'UserData');
      ccp = max(sb.ind-1,1);
      sb.ind = ccp;
      len = size(sb.str,2);
      if (ccp == 1)
	 set(ud.lshift,'Enable','inactive');
      elseif (ccp == len)
	 set(ud.rshift,'Enable','on');
      end
      set(ud.labels(1),'String',sb.str(ccp:len),...
		       'UserData',sb);
   case 'rshift'
      %
      %
      %
      hFig = get(0,'CurrentFigure');
      ud = get(hFig,'UserData');
      sb = get(ud.labels(1),'UserData');
      len = size(sb.str,2);
      ccp = min(sb.ind,len)+1;
      sb.ind = ccp;
      if (ccp == len+1)
	 set(ud.rshift,'Enable','inactive');
      elseif (ccp == 2)
	 set(ud.lshift,'Enable','on');
      end
      set(ud.labels(1),'String',sb.str(ccp:len),...
		       'UserData',sb);
   case 'resize'
      %
      %
      %
   case 'close'
      %
      % Close the info window
      %
      hFig = get(0,'CurrentFigure');
      set(hFig,'Visible','off');
   case 'create'
      %
      % Create an info window
      %
      shh = get(0,'ShowHiddenHandles');
      set(0,'ShowHiddenHandles','on');
      kids = get(0,'Children');
      hFig = findobj(kids,'flat','Tag','hndlinfoTag');
      set(0,'ShowHiddenHandles',shh);
      if (length(hFig) > 1)
	 % 
	 % Something wrong (there is another figure with the same tag)
	 % 
	 error('sb2sl: sbhndlinfo detected more than one window.');
      elseif (length(hFig) == 1)
	 % 
	 % Info window already created
	 % 
	 set(hFig,'Visible','on');
	 set(0,'CurrentFigure',hFig);
      else
	 %
	 % Get preferences
	 %
	 sFig = findobj(kids,'flat','Tag','sb2slTag');
	 ud = get(sFig,'UserData');
	 winpref = ud.winpref;
	 % 
	 % Create a new tree structure window.
	 %
	 bgc = get(0,'DefaultUIControlBackGroundColor');
	 rect = [490 200 300 160];
	 hFig = figure('Units','pixels',...
		       'Position',rect,...
		       'Color',bgc,...
		       'Resize','off',...
		       'Tag','hndlinfoTag',...
		       'IntegerHandle','off',...
		       'HandleVisibility','callback',...
		       'MenuBar','none',...
		       'Name','SB2SL: SuperBlock Info',...
		       'NumberTitle','off',...
		       'ResizeFcn','sbhndlinfo(''resize'');');
	 % 
	 % Uicontrol object creation
	 %
	 labls = {'SuperBlock:';...
		  'Type:';...
		  'Blocks:';...
		  'Inputs:';...
		  'Outputs:'};
	 m = size(labls,1);
	 htext = zeros(m,1);
	 rect = [10 130 80 20];
	 for (k = 1:m)
	    htext(k) = uicontrol('Parent',hFig,...
				 'Style','text',...
				 'Units','pixels',...
				 'Position',rect,...
				 'Interruptible','off',...
				 'FontName','Helvetica',...
				 'FontSize',winpref.varSize(2),...
				 'Tag','varNormal',...
				 'HorizontalAlignment','left',...
				 'HandleVisibility','off',...
				 'String',labls{k});
	    rect(2) = rect(2)-20;
	 end
	 set(htext,'Units','normalized');
	 rect = [90 130 150 20];
	 for (k = 1:m)
	    htext(k) = uicontrol('Parent',hFig,...
				 'Style','text',...
				 'Units','pixels',...
				 'Position',rect,...
				 'Clipping','on',...
				 'Interruptible','off',...
				 'FontName','Helvetica',...
				 'FontSize',winpref.varSize(2),...
				 'Tag','varNormal',...
				 'HorizontalAlignment','left',...
				 'HandleVisibility','callback',...
				 'String','');
	    rect(2) = rect(2)-20;
	 end
	 set(htext,'Units','normalized');
	 %
	 % Command buttons
	 %
	 rect = [250 130 20 20];
	 hlbtn = uicontrol('Parent',hFig,...
			   'Style','pushbutton',...
			   'Units','pixels',...
			   'Position',rect,...
			   'CallBack','sbhndlinfo(''lshift'');',...
			   'Interruptible','on',...
			   'FontName','Helvetica',...
			   'FontSize',winpref.varSize(1),...
			   'Tag','varLarge',...
			   'HorizontalAlignment','center',...
			   'String',char(171));
	 set(hlbtn,'Units','normalized');
	 rect = [270 130 20 20];
	 hrbtn = uicontrol('Parent',hFig,...
			   'Style','pushbutton',...
			   'Units','pixels',...
			   'Position',rect,...
			   'CallBack','sbhndlinfo(''rshift'');',...
			   'Interruptible','on',...
			   'FontName','Helvetica',...
			   'FontSize',winpref.varSize(1),...
			   'Tag','varLarge',...
			   'HorizontalAlignment','center',...
			   'String',char(187));
	 set(hrbtn,'Units','normalized');
	 rect = [110 10 80 20];
	 hexit = uicontrol('Parent',hFig,...
			   'Style','pushbutton',...
			   'Units','pixels',...
			   'Position',rect,...
			   'CallBack','sbhndlinfo(''close'');',...
			   'Interruptible','on',...
			   'FontName','Helvetica',...
			   'FontSize',winpref.varSize(2),...
			   'Tag','varNormal',...
			   'HorizontalAlignment','center',...
			   'String','Cancel');
	 set(hexit,'Units','normalized');
	 %
	 % Save handles and create the rest of the windows.
	 %
	 ud = struct('lshift',hlbtn,...
		     'rshift',hrbtn,...
		     'labels',htext);
	 set(hFig,'UserData',ud);
	 drawnow;				   % Flush the event queue
      end
   case 'compile'
      % 
      % Compilation message
      % 
      sbhndlmsgs('write','.','append');	   % Show progress
   otherwise
      error('sb2sl: sbhndlinfo called with incorrect argument.');
   end 
   return					   % Last card of SBHNDLINFO

function sbtype = gettype(code,signal)
%GETTYPE Map the superblock number into the string.
%
%

   codes = [0;1;33;73;129;257;513;769;1025;1281;1537];
   types = {'Continuous';...
            'Discrete';...
	    'State Transition Diagram';...
	    'Data Store';...
	    'Triggered';...
	    'Standard Procedure';...
	    'Startup Procedure';...
	    'Background Procedure';...
	    'Interrupt Procedure';...
	    'Macro Procedure';...
	    'Inline Procedure'};
   ind = find(codes == code);
   if (isempty(ind))
      sbtype = 'Unknown';
   else
      switch (code)
      case 1
	 switch (signal)
	 case -1
	    sbtype = cat(2,types{ind},' Free-running');
	 case 0
	    sbtype = cat(2,types{ind},' Enabled (Parent)');
	 otherwise
	    sbtype = cat(2,types{ind},' Enabled (Pin)');
	 end
      case 129
	 posting = [' (ATR)';
		    ' (SAF)';
		    ' (ANT)'];
	 switch (signal(1))
	 case -1
	    sbtype = cat(2,types{ind},' None',posting(signal(2),:));
	 case 0
	    sbtype = cat(2,types{ind},' Parent',posting(signal(2),:));
	 otherwise
	    sbtype = cat(2,types{ind},' Pin',posting(signal(2),:));
	 end
      otherwise
	 sbtype = types{ind};
      end
   end
   return					   % Last card of GETTYPE
