function sys = sbboxerror(cmd,msg) 
%SBBOXERROR Manages the error dialog window.
%
%  If no arguments are used, the GUI is initialized.
%  SBBOXERROR(CMD,MSG) is used to perform some 
%
%  See also BOXQUERY.

% Initial coding by A S Bozin, 5-13-97.
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.6 $ $Date: 2000/06/14 16:07:40 $

   if nargin == 0				   % Handle input arguments
      cmd = 'create';
   end
   button = 'OK';				   % Make the label available
   %
   % Acrion depends on CMD
   %
   switch cmd
   case 'write'
      %
      %
      %
      sFig = get(0,'CurrentFigure');		   % Save the current figure handle
      shh = get(0,'ShowHiddenHandles');
      set(0,'ShowHiddenHandles','on');
      hFig = findobj(get(0,'Children'),'flat','Tag','boxerrorTag');
      set(0,'ShowHiddenHandles',shh);
      if isempty(hFig)
	 sbboxerror('create');
	 shh = get(0,'ShowHiddenHandles');
	 set(0,'ShowHiddenHandles','on');
	 hFig = findobj(get(0,'Children'),'flat','Tag','boxerrorTag');
	 set(0,'ShowHiddenHandles',shh);
      end
      hmsgs = get(hFig,'UserData');		   % Get handles
      nl = min(size(msg,1),3);
      i = 1:nl;
      set(hmsgs(i),{'String'},msg(i));
      set(hFig,'Visible','on');		   	   % Make a window visible
      uiwait(hFig);				   % Wait for button
      if ishandle(hFig)
	 set(hFig,'Visible','off');	   	   % Clear flag
	 set(hmsgs,'String','');		   % Remove messages
	 sys = button;
      else
	 sys = 'Aborted';
      end
      set(0,'CurrentFigure',sFig);		   % Restore the current figure
   case 'pressed'
      %
      % Button pressed
      % 
      hFig = get(0,'CurrentFigure');
      uiresume(hFig);			   	   % Resume executions
   case 'delete'
      %
      %
      %
      hFig = get(0,'CallBackObject');
      uiresume(hFig);			   	   % Resume execution
   case 'resize'
      %
      %
      %
   case 'create'
      %
      %
      %
      shh = get(0,'ShowHiddenHandles');
      set(0,'ShowHiddenHandles','on');
      kids = get(0,'Children');
      set(0,'ShowHiddenHandles',shh);
      hFig = findobj(kids,'flat','Tag','boxerrorTag');
      if length(hFig) > 1
	 % 
	 % Something wrong
	 % 
	 error('sb2sl: sbboxerror detected more than one window.');
      elseif length(hFig) == 1
	 % 
	 % Error dialog box already created.
	 % 
	 set(0,'CurrentFigure',hFig);
      else
	 %
	 % Get preferences
	 %
	 sFig = findobj(kids,'flat','Tag','sb2slTag');
	 ud = get(sFig,'UserData');
	 winpref = ud.winpref;
	 % 
	 % Create a new error dialog box
	 %
	 bgc = get(0,'DefaultUIControlBackGroundColor');
	 rect = [400 400 360 100];
	 hFig = figure('Units','pixels',...
		       'Position',rect,...
		       'Resize','off',...
		       'Tag','boxerrorTag',...
		       'IntegerHandle','off',...
		       'MenuBar','none',...
		       'Color',bgc,...
		       'Name','sb2sl: Error Window',...
		       'NumberTitle','off',...
		       'Visible','off',...
		       'HandleVisibility','callback',...
		       'WindowStyle','modal',...
		       'DeleteFcn','sbboxerror(''delete'');',...
		       'ResizeFcn','sbboxerror(''resize'');');
	 % 
	 % Uicontrol object creation
	 %
	 rect = [140 10 80 20];
	 hdone = uicontrol('Parent',hFig,...
			   'Style','pushbutton',...
			   'CallBack','sbboxerror(''pressed'');',...
			   'Units','pixels',...
			   'Position',rect,...
			   'FontName','Helvetica',...
			   'FontSize',winpref.varSize(2),...
			   'Tag','varNormal',...
			   'String',button,...
			   'HorizontalAlignment','center',...
			   'HandleVisibility','callback',...
			   'Visible','on');
	 set(hdone,'Units','normalized');
	 nmsgs = 3;
	 hmsgs = zeros(nmsgs,1);
	 rect = [60 78 290 14];
	 for (k = 1:nmsgs)
	    hmsgs(k) = uicontrol('Parent',hFig,...
				 'Style','text',...
				 'HorizontalAlignment','left',...
				 'Units','pixels',...
				 'Position',rect,...
				 'FontName','Helvetica',...
				 'FontSize',winpref.varSize(2),...
				 'Tag','varNormal',...
				 'String','',...
				 'Clipping','off',...
				 'HandleVisibility','callback',...
				 'ForeGroundColor',[0 0 0]);
	    rect(2) = rect(2)-14;
	 end
	 set(hmsgs,'Units','normalized');
	 % 
	 % Icon object creation
	 % 
	 rect = [10 50 40 40];
	 hAxes = axes('Parent',hFig,...
		      'Units','pixels', ...
		      'Position',rect,...
		      'Xgrid','off',...
		      'Ygrid','off',...
		      'XTick',[],...
		      'YTick',[],...
		      'XLimMode','manual',...
		      'YLimMode','manual',...
		      'XLim',[0 1],...
		      'YLim',[0 1],...
		      'Clipping','on',...
		      'Visible','off',...
		      'HandleVisibility','callback',...
		      'NextPlot','add');
	 set(hAxes,'Units','normalized');
	 drawicon(hAxes,'error');
	 set(hFig,'UserData',hmsgs);	   	   % Save handles
	 drawnow;
      end
   case 'compile'
      % 
      % Compilation message
      % 
      sbhndlmsgs('write','.','append');	   % Show progress
   otherwise
      error('sb2sl: sbboxerror called with incorrect argument.') 
   end
   return					   % Last card of SBBOXERROR

