function sys = sbboxquery(cmd,msg) 
%SBBOXQUERY Manages the query dialog window.
%
%  If no arguments are used, the GUI is initialized.
%  SBBOXQUERY(CMD,MSG) is used to perform some 
%
%  See also BOXERROR.

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

   if nargin == 0				   % Handle input arguments
      cmd = 'create';
   end
   buttons = {'Yes','No','Cancel'};	   	   % Make the labels always available
   %
   % Action 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','boxqueryTag');
      set(0,'ShowHiddenHandles',shh);
      if isempty(hFig)
	 sbboxquery('create');
	 shh = get(0,'ShowHiddenHandles');
	 set(0,'ShowHiddenHandles','on');
	 hFig = findobj(get(0,'Children'),'flat','Tag','boxqueryTag');
	 set(0,'ShowHiddenHandles',shh);
      end
      hmsgs = get(hFig,'UserData');
      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);
      if ishandle(hFig)
	 pressed = get(get(hFig,'CurrentObject'),'UserData');
	 set(hFig,'Visible','off');
	 set(hmsgs,'String','');		   % Remove messages
	 sys = buttons{pressed};		   % Return button label
      else
	 sys = 'Cancel';
      end
      set(0,'CurrentFigure',sFig);		   % Restore the current figure
   case 'pressed'
      %
      % Button pressed
      %
      hFig = get(0,'CurrentFigure');
      set(get(hFig,'CurrentObject'),'UserData',msg);
      uiresume(hFig);			   	   % Resume execution
   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');
      hFig = findobj(kids,'flat','Tag','boxqueryTag');
      set(0,'ShowHiddenHandles',shh);
      if length(hFig) > 1
	 % 
	 % Something wrong
	 % 
	 error('sb2sl: sbboxquery detected more than one window.');
      elseif length(hFig) == 1
	 % 
	 % Query 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 query dialog box
	 %
	 bgc = get(0,'DefaultUIControlBackGroundColor');
	 rect = [400 400 360 100];
	 hFig = figure('Units','pixels',...
		       'Position',rect,...
		       'Resize','on',...
		       'Tag','boxqueryTag',...
		       'IntegerHandle','off',...
		       'MenuBar','none',...
		       'Color',bgc,...
		       'Name','sb2sl: Query Window',...
		       'NumberTitle','off',...
		       'Visible','off',...
		       'HandleVisibility','callback',...
		       'WindowStyle','modal',...
		       'UserData',0,...
		       'DeleteFcn','sbboxquery(''delete'');',...
		       'ResizeFcn','sbboxquery(''resize'');');
	 % 
	 % Uicontrol object creation
	 %
	 nbutts = size(buttons,2);
	 hbutts = zeros(nbutts,1);
	 rect = [50 10 80 20];
	 for (k = 1:nbutts)
	    hbutts(k) = uicontrol('Parent',hFig,...
				  'Style','pushbutton',...
				  'CallBack',sprintf('sbboxquery(''pressed'',%d);',k),...
				  'Units','pixels',...
				  'Position',rect,...
				  'FontName','Helvetica',...
				  'FontSize',winpref.varSize(2),...
				  'Tag','varNormal',...
				  'String',buttons{k},...
				  'HorizontalAlignment','center',...
				  'HandleVisibility','callback',...
				  'Visible','on');
	    rect(1) = rect(1)+90;
	 end
	 set(hbutts,'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,'query');
	 set(hFig,'UserData',hmsgs);		   % Save handles
	 drawnow;
      end
   case 'compile'
      % 
      % Compilation message
      % 
      sbhndlmsgs('write','.','append');	   	   % Show progress
   otherwise
      error('sb2sl: sbboxquery called with incorrect argument.') 
   end
   return					   % Last card of SBBOXQUERY
