function sbhndlpref(cmd,opt)
%SBHNDLPREF
%
%  See also

% Initial coding by A. S. Bozin, 25-09-98.
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.4 $ $Date: 2000/06/14 16:08:12 $

   %
   % Action depends on CMD
   %
   switch (cmd)
   case 'update'
      %
      %
      %
      hFig = get(0,'CurrentFigure');
      shh = get(0,'ShowHiddenHandles');
      set(0,'ShowHiddenHandles','on');
      kids = get(0,'Children');
      set(0,'ShowHiddenHandles',shh);
      %
      % Get the values
      %
      ud = get(hFig,'UserData');
      vffont = cell(1,3);
      vffont(:) = get(ud.vfsizes,'Value');
      fffont = get(ud.ffsizes,'Value');
      lenstr = get(ud.bufflen,'String');
      [linstr,bufstr] = deal(lenstr{:});
      [msg,nlines] = edt2num(linstr,4);
      if (isempty(msg))
	 [msg,buflen] = edt2num(bufstr,nlines);
	 if (isempty(msg))
	    sFig = findobj(kids,'flat','Tag','sb2slTag');
	    ud = get(sFig,'UserData');
	    winpref = ud.winpref;
	    vfsize = winpref.points([vffont{:}]);
	    ffsize = winpref.points(fffont);
	    if (strcmp(opt,'save'))
	       winpref.varFont = vffont;
	       winpref.varSize = vfsize;
	       winpref.fixFont = fffont;
	       winpref.fixSize = ffsize;
	       winpref.nlines = nlines;
	       winpref.buflen = buflen;
	       rcfile = 'sb2slusr.rc';
	       cookie = 'user';
	       sbhndlmsgs('write',sprintf('Saving new preferences to %s ...',rcfile),'append');
	       save(rcfile,'winpref','cookie');
	       sbhndlmsgs('write',' Done.','newline');
	    else
	       %
	       % If any option changed, apply changes
	       %
	       changed = find([(vfsize ~= winpref.varSize) (ffsize ~= winpref.fixSize) ...
			       ((nlines ~= winpref.nlines) | (buflen ~= winpref.buflen))]);
	       if (isempty(changed))
		  sbhndlmsgs('write','Nothing changed. Command ignored.');
	       else
		  sbhndlmsgs('write','Setting new preferences ...','append');
		  winpref.varFont = vffont;
		  winpref.varSize = vfsize;
		  winpref.fixFont = fffont;
		  winpref.fixSize = ffsize;
		  winpref.nlines = nlines;
		  winpref.buflen = buflen;
		  ud.winpref = winpref;
		  set(sFig,'UserData',ud);
		  onscreen = get(kids,'Tag');
		  tags = {'boxerrorTag';...
			  'boxqueryTag';...
			  'hndltreeTag';...
			  'hndlinfoTag';...
			  'hndlunkbTag';...
			  'hndlcompTag';...
			  'hndlpartTag';...
			  'hndloptsTag';...
			  'hndlprefTag';...
			  'hndlmsgsTag';...
			  'sb2slTag'};
		  m = size(onscreen,1);
		  ind = zeros(m,1);
		  for (k = 1:m)
		     if (all(strcmp(onscreen{k},tags) == 0))
			ind(k) = 1;
		     end
		  end
		  kids(logical(ind)) = []; % Remove handles
		  for (ind = changed)
		     switch (ind)
		     case 1	   	   % Large variable width fonts
			set(findobj(kids,'Tag','varLarge'),'FontSize',vfsize(1));
		     case 2	   	   % Normal variable width fonts
			set(findobj(kids,'Tag','varNormal'),'FontSize',vfsize(2));
		     case 3	   	   % Small variable width fonts
			set(findobj(kids,'Tag','varSmall'),'FontSize',vfsize(3));
		     case 4	   	   % Normal fixed width fonts
			set(findobj(kids,'Tag','fixNormal'),'FontSize',ffsize);
		     case 5	   	   % Number of lines or the buffer length
			sFig = findobj(kids,'flat','Tag','hndlmsgsTag');
			if (~isempty(sFig))
			   ud = get(sFig,'UserData');
			   if (nlines ~= ud.nlines)
			      vskip = 16;
			      height = vskip*nlines;
			      rect = get(sFig,'Position');
			      rect([2 4]) = [rect(2)+vskip*(ud.nlines-nlines) height+20];
			      set(sFig,'Units','pixels',...
				       'Position',rect);
			      rect = [6 10 16 height];
			      set(ud.slider,'Units','pixels',...
					    'Position',rect);
			      linstr = get(ud.txtlin,'String');
			      delete(ud.txtlin);
			      txtlin = zeros(nlines,1);
			      rect = [30 height-8 340 vskip];
			      for (k = 1:nlines)
				 txtlin(k) = uicontrol('Parent',sFig,...
						       'Style','text',...
						       'FontName','Helvetica',...
						       'FontSize',winpref.varSize(2),...
						       'Tag','varNormal',...
						       'HorizontalAlignment','left',...
						       'Units','pixels',...
						       'Position',rect,...
						       'String','',...
						       'Clipping','on',...
						       'HandleVisibility','callback',...
						       'ForeGroundColor',[0 0 0]);
				 rect(2) = rect(2)-vskip;
			      end
			      ud.txtlin = txtlin;
			      if (nlines >= ud.nlines)
				 set(txtlin(1:ud.nlines),{'String'},linstr);
			      else
				 top = max(nlines,ud.ind);
				 set(txtlin,{'String'},linstr(top-nlines+1:top));
			      end
			   end
			   if (buflen ~= ud.buflen)
			      txtbuf = cell(buflen,1);
			      oldbuf = get(ud.slider,'UserData');
			      if (buflen >= ud.buflen)
				 txtbuf(buflen-ud.buflen+1:end) = oldbuf(:);
			      else
				 txtbuf(:) = oldbuf(ud.buflen-buflen+1:end);
			      end
			   else
			      txtbuf = get(ud.slider,'UserData');
			   end
			   scrlLen = buflen-nlines;
			   sldval = min(scrlLen,get(ud.slider,'Value'));
			   set(ud.slider,'Max',scrlLen,...
					 'Value',sldval,...
					 'SliderStep',[1 2]/scrlLen,...
					 'UserData',txtbuf);
			   ud.nlines = nlines;
			   ud.buflen = buflen;
			   ud.ind = min(ud.ind,nlines);
			   ud.ptr = min(ud.ptr,buflen);
			   ud.value = sldval;
			   set(sFig,'UserData',ud);
			end
		     otherwise
		     end
		  end
		  sbhndlmsgs('write',' Done.','newline');
	       end
	       if (strcmp(opt,'off'))
		  set(hFig,'Visible','off');
	       end
	    end
	 end
      end
      if (~isempty(msg))
	 sbboxerror('write',msg);
      end
   case 'close'
      %
      % Close the window
      %
      hFig = get(0,'CurrentFigure');
      set(hFig,'Visible','off');
   case 'create'
      %
      % Create the preferences window
      %
      shh = get(0,'ShowHiddenHandles');
      set(0,'ShowHiddenHandles','on');
      kids = get(0,'Children');
      hFig = findobj(kids,'flat','Tag','hndlprefTag');
      set(0,'ShowHiddenHandles',shh);
      if (length(hFig) > 1)			   % There is another figure with the same tag
	 error('sb2sl: sbhndlpref 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 280 234];
	 hFig = figure('Units','pixels',...
		       'Position',rect,...
		       'Color',bgc,...
		       'Resize','off',...
		       'Tag','hndlprefTag',...
		       'IntegerHandle','off',...
		       'Visible','off',...
		       'HandleVisibility','callback',...
		       'MenuBar','none',...
		       'Name','SB2SL: Window Preferences',...
		       'NumberTitle','off',...
		       'DeleteFcn','sbhndlpref(''delete'');',...
		       'ResizeFcn','sbhndlpref(''resize'');');
	 %
	 % Variable width fonts
	 %
	 rect = [10 134 170 80];
	 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','Variable width font',...
			   'Clipping','on');
	 captExt = get(hCapt,'Extent');
	 rect(:) = [rect(1)+6 rect(2)+rect(4)-captExt(4)/2 captExt(3)+1 captExt(4)];
	 set(hCapt,'Position',rect,'Units','normalized');
	 labels = {'Large font size','Normal font size','Small font size'};
	 nopts = size(labels,2);
	 hfvar = zeros(nopts,1);
	 rect = [16 184 100 20];
	 for (k = 1:nopts)
	    hCapt = uicontrol('Parent',hFig,...
			      'Style','text',...
			      'Units','pixels',...
			      'Position',rect,...
			      'Interruptible','off',...
			      'FontName','Helvetica',...
			      'FontSize',winpref.varSize(2),...
			      'Tag','varNormal',...
			      'HorizontalAlignment','left',...
			      'String',labels{k});
	    set(hCapt,'Units','normalized');
	    hfvar(k) = uicontrol('Parent',hFig,...
				 'Style','popupmenu',...
				 'Units','pixels',...
				 'Position',[rect(1)+104,rect(2)+4 50 20],...
				 'Interruptible','on',...
				 'FontName','Helvetica',...
				 'FontSize',winpref.varSize(2),...
				 'Tag','varNormal',...
				 'HorizontalAlignment','left',...
				 'String',winpref.sizes,...
				 'Value',winpref.varFont{k});
	    rect(2) = rect(2)-24;
	 end
	 set(hfvar,'Units','normalize');
	 %
	 % Fixed width fonts
	 %
	 rect = [10 84 170 32];
	 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','Fixed width font',...
			   'Clipping','on');
	 captExt = get(hCapt,'Extent');
	 rect(:) = [rect(1)+6 rect(2)+rect(4)-captExt(4)/2 captExt(3)+1 captExt(4)];
	 set(hCapt,'Position',rect,'Units','normalized');
	 rect = [16 86 100 20];
	 hCapt = uicontrol('Parent',hFig,...
			   'Style','text',...
			   'Units','pixels',...
			   'Position',rect,...
			   'Interruptible','off',...
			   'FontName','Helvetica',...
			   'FontSize',winpref.varSize(2),...
			   'Tag','varNormal',...
			   'HorizontalAlignment','left',...
			   'String','Normal font size');
	 set(hCapt,'Units','normalized');
	 rect = [120 90 50 20];
	 hffix = uicontrol('Parent',hFig,...
			   'Style','popupmenu',...
			   'Units','pixels',...
			   'Position',rect,...
			   'Interruptible','on',...
			   'FontName','Helvetica',...
			   'FontSize',winpref.varSize(2),...
			   'Tag','varNormal',...
			   'HorizontalAlignment','left',...
			   'String',winpref.sizes,...
			   'Value',winpref.fixFont);
	 set(hffix,'Units','normalize');
	 %
	 % Message window buffer length
	 %
	 rect = [10 10 170 56];
	 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','Message window',...
			   'Clipping','on');
	 captExt = get(hCapt,'Extent');
	 rect(:) = [rect(1)+6 rect(2)+rect(4)-captExt(4)/2 captExt(3)+1 captExt(4)];
	 set(hCapt,'Position',rect,'Units','normalized');
	 labels = {'Number of lines','Buffer length'};
	 values = [winpref.nlines winpref.buflen];
	 nopts = size(labels,2);
	 hbuff = zeros(nopts,1);
	 rect = [16 36 100 20];
	 for (k = 1:nopts)
	    hCapt = uicontrol('Parent',hFig,...
			      'Style','text',...
			      'Units','pixels',...
			      'Position',rect,...
			      'Interruptible','off',...
			      'FontName','Helvetica',...
			      'FontSize',winpref.varSize(2),...
			      'Tag','varNormal',...
			      'HorizontalAlignment','left',...
			      'String',labels{k});
	    set(hCapt,'Units','normalized');
	    hbuff(k) = uicontrol('Parent',hFig,...
				 'Style','edit',...
				 'BackgroundCOlor',[1 1 1],...
				 'Units','pixels',...
				 'Position',[rect(1)+104 rect(2)+4 50 20],...
				 'Interruptible','on',...
				 'FontName','Helvetica',...
				 'FontSize',winpref.varSize(2),...
				 'Tag','varNormal',...
				 'HorizontalAlignment','right',...
				 'String',sprintf('%d',values(k)));
	    rect(2) = rect(2)-24;
	 end
	 set(hbuff,'Units','normalize');
	 %
	 % Command buttons
	 %
	 rect = [190 194 80 20];
	 hSave = uicontrol('Parent',hFig,...
			   'Style','pushbutton',...
			   'Units','pixels',...
			   'Position',rect,...
			   'CallBack','sbhndlpref(''update'',''save'');',...
			   'Interruptible','on',...
			   'FontName','Helvetica',...
			   'FontSize',winpref.varSize(2),...
			   'Tag','varNormal',...
			   'HorizontalAlignment','center',...
			   'TooltipString','Save the preferences',...
			   'String','Save');
	 set(hSave,'Units','normalized');
	 labels = {'OK','Cancel','Apply'};
	 callBacks = {'sbhndlpref(''update'',''off'');',...
		      'sbhndlpref(''close'');',...
		      'sbhndlpref(''update'',''on'');'};
	 toolTips = {'Set new preferences and close the window',...
		     'Close the window',...
		     'Set new preferences'};
	 nbutts = size(labels,2);
	 hbutts = zeros(nbutts,1);
	 rect = [190 70 80 20];
	 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(2) = rect(2)-30;
	 end
	 set(hbutts,'Units','normalized');
	 %
	 % Save the handles
	 %
	 ud = struct('vfsizes',hfvar,'ffsizes',hffix,'bufflen',hbuff);
	 set(hFig,'Visible','on','UserData',ud);
      end
   case 'resize'
      %
      %
      %
   case 'delete'
      %
      %
      %
   case 'compile'
      % 
      % Compilation message
      % 
      sbhndlmsgs('write','.','append');	   	   % Show progress
   otherwise
      error('sb2sl: sbhndlpref called with incorrect argument.');
   end 
   return					   % Last card of SBHNDLOPT

function [msg,num] = edt2num(str,lb)
%EDT2NUM
%
%
   
   ind = find((str == ' ') | (str == char(9)));
   if (~isempty(ind))
      str(ind) = '';
   end
   dbl = double(str);
   errflag = 0;
   msg = '';
   if (isempty(dbl) | any((dbl < 48) | (dbl > 57)))
      errflag = 1;
      num = [];
   else
      num = sscanf(str,'%d');
      if (num <= lb)
	 errflag = 1;
      end
   end
   if (errflag)
      msg = {'Buffer length not specified correctly. It must be';...
	     sprintf('a positive integer number greater than %d.',lb)};
   end
   return					   % Last card of EDT2NUM

   

