function sbhndlunkb(cmd,opt)
%SBHNDLUNKB
%
%  See also

% A. S. Bozin, 10-12-97.
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.6 $ $Date: 2000/06/14 16:08:18 $

   %
   % Action depends on CMD
   %
   switch (cmd)
   case 'scroll'
      %
      %
      %
      hFig = get(0,'CurrentFigure');
      udFig = get(hFig,'UserData');
      udTxt = get(udFig.path,'UserData');
      cind = udTxt.bos;
      switch (opt)
      case 'forward'
	 cind = min(cind,udTxt.eos)+1;
	 if (cind == udTxt.eos+1)
	    set(udFig.forward,'Enable','inactive');
	 elseif (cind == 2)
	    set(udFig.backward,'Enable','on');
	 end
	 udTxt.bos = cind;
	 set(udFig.path,'String',udTxt.string(cind:end),...
			'UserData',udTxt);   	   % Update the display and indices
      case 'backward'
	 cind = max(cind-1,1);
	 if (cind == 1)
	    set(udFig.backward,'Enable','inactive');
	 elseif (cind == udTxt.eos)
	    set(udFig.forward,'Enable','on');
	 end
	 udTxt.bos = cind;
	 set(udFig.path,'String',udTxt.string(cind:end),...
			'UserData',udTxt);   	   % Update the display and indices
      otherwise
      end
   case 'select'
      %
      %
      %
      hFig = get(0,'CurrentFigure');
      ud = get(hFig,'UserData');
      prop = get(ud.unknown,{'UserData';'Value'});
      [fullPath,blkInd] = deal(prop{:});
      nblk = size(fullPath,1);
      %
      % If more then one block selected, pick up the first one
      %
      if (length(blkInd) > 1)
	 sbhndlmsgs('write','Multiple block name selection ignored.');
	 blkInd = blkInd(1);
      end
      if (blkInd > nblk)
	 set(ud.path,'String','','TooltipString','');
	 set([ud.forward;ud.backward],'Enable','inactive');
      else
	 blkName = fullPath{blkInd};
	 set(ud.path,'String',blkName,...
		     'TooltipString',blkName,...
		     'UserData',struct('bos',1,...
				       'eos',length(blkName),...
				       'string',blkName));
	 set([ud.forward;ud.backward],{'Enable'},{'on';'inactive'});
      end
   case 'close'
      %
      %
      %
      hFig = get(0,'CurrentFigure');
      set(hFig,'Visible','off');
   case 'update'
      %
      % Update a list of missing blocks if the window is visible
      %
      shh = get(0,'ShowHiddenHandles');
      set(0,'ShowHiddenHandles','on');
      hFig = findobj(get(0,'Children'),'flat','Tag','hndlunkbTag');
      set(0,'ShowHiddenHandles',shh);
      if (~isempty(hFig))
	 if (strcmp(get(hFig','Visible'),'on'))
	    uBlocks = find_system(opt,'Tag','Unknown');
	    sbhndlunkb('create',uBlocks);
	 end
      end
   case 'create'
      %
      % Create the missing blocks window
      %
      shh = get(0,'ShowHiddenHandles');
      set(0,'ShowHiddenHandles','on');
      kids = get(0,'Children');
      hFig = findobj(kids,'flat','Tag','hndlunkbTag');
      set(0,'ShowHiddenHandles',shh);
      if (length(hFig) > 1)
	 % 
	 % Something wrong (there is another figure with the same tag)
	 % 
	 error('sb2sl: sbhndlunkb detected more than one window.');
      elseif (length(hFig) == 1)
	 % 
	 % Missing blocks 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 470 206];
	 hFig = figure('Units','pixels',...
		       'Position',rect,...
		       'Color',bgc,...
		       'Resize','off',...
		       'Tag','hndlunkbTag',...
		       'IntegerHandle','off',...
		       'HandleVisibility','callback',...
		       'MenuBar','none',...
		       'Name','SB2SL: Unconverted Blocks',...
		       'NumberTitle','off',...
		       'DeleteFcn','sbhndlunkb(''delete'');',...
		       'ResizeFcn','sbhndlunkb(''resize'');');
	 %
	 % Text sliders
	 %
	 rect = [420 15 20 20];
	 hForward = uicontrol('Parent',hFig,...
			      'Style','pushbutton',...
			      'units','pixels',...
			      'Position',rect,...
			      'CallBack','sbhndlunkb(''scroll'',''forward'');',...
			      'Interruptible','on',...
			      'Enable','inactive',...
			      'FontName','Helvetica',...
			      'FontSize',winpref.varSize(1),...
			      'Tag','varLarge',...
			      'HorizontalAlignment','center',...
			      'String',char(187));
	 set(hForward,'Units','normalized');
	 rect = [440 15 20 20];
	 hBackward = uicontrol('Parent',hFig,...
			       'Style','pushbutton',...
			       'units','pixels',...
			       'Position',rect,...
			       'CallBack','sbhndlunkb(''scroll'',''backward'');',...
			       'Interruptible','on',...
			       'Enable','inactive',...
			       'FontName','Helvetica',...
			       'FontSize',winpref.varSize(1),...
			       'Tag','varLarge',...
			       'HorizontalAlignment','center',...
			       'String',char(171));
	 set(hBackward,'Units','normalized');
	 rect = [10 10 400 30];
	 hFrame = uicontrol('Parent',hFig,...
			    'Style','frame',...
			    'Enable','inactive',...
			    'ForeGround',bgc,...
			    'Position',rect);
	 set(hFrame','Units','normalized');
	 hCapt = uicontrol('Parent',hFig,...
			   'Style','text',...
			   'Units','pixels',...
			   'Position',rect,...
			   'Interruptible','off',...
			   'FontName','Helvetica',...
			   'FontSize',winpref.varSize(2),...
			   'Tag','varNormal',...
			   'HorizontalAlignment','left',...
			   'String','Full path name',...
			   'Clipping','on');
	 captExt = get(hCapt,'Extent');
	 rect(:) = [rect(1)+6 rect(2)+rect(4)-captExt(4)/2 captExt(3:4)];
	 set(hCapt,'Position',rect,'Units','normalized');
	 rect = [10 10 400 30];
	 hPath = uicontrol('Parent',hFig,...
			   'Style','text',...
			   'Units','pixels',...
			   'Position',rect,...
			   'Interruptible','off',...
			   'FontName','Courier',...
			   'FontSize',winpref.fixSize,...
			   'Tag','fixNormal',...
			   'HorizontalAlignment','left',...
			   'String','Extent',...
			   'Clipping','on');
	 pathExt = get(hPath,'Extent');
	 rect(:) = [rect(1)+6 rect(2)+(rect(4)-pathExt(4))/2 rect(3)-12 pathExt(4)];
	 set(hPath,'Position',rect,...
		   'String','',...
		   'Units','normalized');
	 %
	 % Missing block list
	 %
	 rect = [10 172 80 20];
	 hText = uicontrol('Parent',hFig,...
			   'Style','text',...
			   'Units','pixels',...
			   'Position',rect,...
			   'Interruptible','off',...
			   'FontName','Helvetica',...
			   'FontSize',winpref.varSize(2),...
			   'Tag','varNormal',...
			   'HorizontalAlignment','left',...
			   'String','Block name');
	 set(hText,'Units','normalized');
	 rect = [118 172 80 20];
	 hText = uicontrol('Parent',hFig,...
			   'Style','text',...
			   'Units','pixels',...
			   'Position',rect,...
			   'Interruptible','off',...
			   'FontName','Helvetica',...
			   'FontSize',winpref.varSize(2),...
			   'Tag','varNormal',...
			   'HorizontalAlignment','left',...
			   'String','Block type');
	 set(hText,'Units','normalized');
	 rect = [10 60 350 112];
	 hUnknown = uicontrol('Parent',hFig,...
			      'Style','listbox',...
			      'Units','pixels',...
			      'Position',rect,...
			      'CallBack','sbhndlunkb(''select'');',...
			      'Interruptible','on',...
			      'Enable','inactive',...
			      'UserData',[],...
			      'FontName','Courier',...
			      'FontSize',winpref.fixSize,...
			      'Tag','fixNormal',...
			      'HorizontalAlignment','left',...
			      'ListboxTop',1,...
			      'Min',0,...
			      'Max',2,...
			      'String','<<end of block list>>');
	 set(hUnknown,'Units','normalized');
	 %
	 % Command buttons
	 %
	 rect = [380 152 80 20];
	 hClose = uicontrol('Parent',hFig,...
			    'Style','pushbutton',...
			    'Units','pixels',...
			    'Position',rect,...
			    'CallBack','sbhndlunkb(''close'');',...
			    'Interruptible','on',...
			    'FontName','Helvetica',...
			    'FontSize',winpref.varSize(2),...
			    'Tag','varNormal',...
			    'HorizontalAlignment','center',...
			    'TooltipString','Close the window',...
			    'String','Cancel');
	 set(hClose,'Units','normalized');
	 %
	 % Save the handles
	 %
	 ud = struct('unknown',hUnknown,...
		     'forward',hForward,...
		     'backward',hBackward,...
		     'path',hPath);
	 set(hFig,'UserData',ud);
      end
      %
      % Update a list of missing blocks
      %
      if (isempty(opt))
	 nblk = 0;
	 blkFull = {};
	 str = {'<<end of block list>>'};
      else
	 CR = char(10);
	 SP = char(32);
	 blkName = cellstr(get_param(opt,'Name'));
	 blkPath = cellstr(get_param(opt,'Parent'));
	 nblk = size(blkName,1);
	 maxLen = 13;
	 truncName = cell(nblk,1);
	 blankLine = char(SP*ones(1,maxLen+2));
	 for (k = 1:nblk)
	    ind = find(blkName{k} == CR);
	    if (~isempty(ind))
	       blkName{k}(ind) = SP;
	    end
	    truncName{k} = blankLine;
	    nameLen = min(maxLen,size(blkName{k},2));
	    truncName{k}(1:nameLen) = blkName{k}(1:nameLen);
	    ind = find(blkPath{k} == CR);
	    if (~isempty(ind))
	       blkPath{k}(ind) = SP;
	    end
	 end
	 blkFull = strcat(blkPath,{'/'},blkName);
	 blkInfo = strcat(truncName,get_param(opt,'MaskType'));
	 str = cell(nblk+1,1);
	 str(1:nblk) = blkInfo;
	 str(end) = {'<<end of block list>>'};
      end
      ud = get(hFig,'UserData');
      set(ud.unknown,'String',str,...
		     'Enable','on',...
		     'Value',nblk+1,...
		     'ListboxTop',1,...
		     'UserData',blkFull);
   case 'resize'
      %
      %
      %
   case 'delete'
      %
      %
      %
   case 'compile'
      % 
      % Compilation message
      % 
      sbhndlmsgs('write','.','append');	   	   % Show progress
   otherwise
      error('sb2sl: sbhndlunkb called with incorrect argument.');
   end
   return                               	   % Last card of SBHNDLUNKB

