function sbhndlcomp(cmd,opt)
%SBHNDLCOMP Support for the compilation window.
%
%  See also SBHNDLINFO, SBHNDLOPTS, SBHNDLPART, SBHNDLTREE, SBHNDLUNKB

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

   %
   % Action depends on CMD
   %
   switch (cmd)
   case 'script'
      %
      % Generate a script file for the batch compilation
      %
      [mFile,mPath] = uiputfile('*.m','Script File');
      if (mFile == 0)
      else
	 ind = find(mFile == '.');		   % Look for extension part
	 if (isempty(ind))
	    mFile = cat(2,mFile,'.m');
	 end
	 fid = fopen(fullfile(mPath,mFile),'wt');
	 if (fid ~= -1)
	    hFig = get(0,'CurrentFigure');
	    ud = get(hFig,'UserData');
	    fileName = get(ud,'String');
	    nsrc = size(fileName,1)-1;		   % Less the end field
	    sbhndlmsgs('write',sprintf('Creating %s ...',mFile),'append');
	    fprintf(fid,'%%\n');
	    fprintf(fid,'%%\n');
	    fprintf(fid,'%%\n');
	    for (k = 1:nsrc)
	       srcFile = cat(2,fileName{k},'.c');
	       fprintf(fid,'fprintf(''Compiling %s ...'');\n',srcFile);
	       fprintf(fid,'st = mex(''-O'',''%s'');\n',srcFile);
	       fprintf(fid,'if (st == 0)\n');
	       fprintf(fid,'   fprintf('' Done.\\n'');\n');
	       fprintf(fid,'else\n');
	       fprintf(fid,'   fprintf('' Failed.\\n'');\n');
	       fprintf(fid,'end\n');
	    end
	    if (fclose(fid) ~= -1)
	       sbhndlmsgs('write',' Done.','newline');
	    else
	       sbhndlmsgs('write',' Failed.','newline');
	    end
	 end
      end
   case 'convert'
      %
      % Compile selected files
      %
      hFig = get(0,'CurrentFigure');
      ud = get(hFig,'UserData');
      prop = get(ud,{'String';'Value'});
      [fileName,srcInd] = deal(prop{:});
      nsrc = size(fileName,1)-1;		   % Less the end field
      if (srcInd(end) > nsrc)		   	   % Remove the end of source field
	 srcInd(end) = [];
      end
      if (~isempty(srcInd))
	 %
	 % Compile files
	 %
	 for (k = srcInd)
	    srcFile = cat(2,fileName{k},'.c');
	    sbhndlmsgs('write',sprintf('Compiling %s ...',srcFile),'append');
	    errCode = mex('-O',srcFile);
	    if (errCode)
	       sbhndlmsgs('write',' Failed.','newline');
	    else
	       sbhndlmsgs('write',' Done.','newline');
	    end
	 end
      else
	 sbhndlmsgs('write','Nothing to be compiled.');
      end
   case 'close'
      %
      % Close the window
      %
      hFig = get(0,'CurrentFigure');
      set(hFig,'Visible','off');
   case 'edit'
      %
      % Load selected files into the default editor
      %
      hFig = get(0,'CurrentFigure');
      ud = get(hFig,'UserData');
      prop = get(ud,{'String';'Value'});
      [fileName,srcInd] = deal(prop{:});
      nsrc = size(fileName,1)-1;		   % Less the end field
      if (srcInd(end) > nsrc)		   	   % Remove the end of source field
	 srcInd(end) = [];
      end
      if (~isempty(srcInd))
	 %
	 % Invoke the editor
	 %
	 for (k = srcInd)
	    srcFile = cat(2,fileName{k},'.c');
	    sbhndlmsgs('write',sprintf('Loading %s ...',srcFile),'append');
	    try
	       edit(srcFile);
	       stat = 0;
	    catch
	       fprintf('\n%s\n',lasterr);
	       msg = {'Internal error encountered.';...
		      'Please check the Matlab command window for';...
		      'more information.'};
	       sbboxerror('write',msg);
	       stat = 1;
	    end
	    if (stat)
	       sbhndlmsgs('write',' Failed.','newline');
	    else
	       sbhndlmsgs('write',' Done.','newline');
	    end
	 end
      else
	 sbhndlmsgs('write','Nothing to edit.');
      end
   case 'update'
      %
      % Update a list of source files if the compile window is opened
      %
      shh = get(0,'ShowHiddenHandles');
      set(0,'ShowHiddenHandles','on');
      hFig = findobj(get(0,'Children'),'flat','Tag','hndlcompTag');
      set(0,'ShowHiddenHandles',shh);
      if (~isempty(hFig))
	 if (strcmp(get(hFig','Visible'),'on'))
	    sBlocks = find_system(opt,'Tag','BlockScript');
	    sbhndlcomp('create',sBlocks);
	 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','hndlcompTag');
      set(0,'ShowHiddenHandles',shh);
      if (length(hFig) > 1)
	 % 
	 % Something wrong (there is another figure with the same tag)
	 % 
	 error('sb2sl: sbhndlcomp 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 380 156];
	 hFig = figure('Units','pixels',...
		       'Position',rect,...
		       'Color',bgc,...
		       'Resize','off',...
		       'Tag','hndlcompTag',...
		       'IntegerHandle','off',...
		       'HandleVisibility','callback',...
		       'MenuBar','none',...
		       'Name','SB2SL: Source Files',...
		       'NumberTitle','off',...
		       'DeleteFcn','sbhndlcomp(''delete'');',...
		       'ResizeFcn','sbhndlcomp(''resize'');');
	 %
	 % Source file list
	 %
	 rect = [10 122 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','File name');
	 set(hText,'Units','normalized');
	 rect = [10 10 260 112];
	 hSource = uicontrol('Parent',hFig,...
			     'Style','listbox',...
			     'Units','pixels',...
			     'Position',rect,...
			     'Interruptible','on',...
			     'Enable','inactive',...
			     'UserData',[],...
			     'FontName','Courier',...
			     'FontSize',winpref.fixSize,...
			     'Tag','fixNormal',...
			     'HorizontalAlignment','left',...
			     'ListboxTop',1,...
			     'Min',0,...
			     'Max',2,...
			     'String','<<end of source list>>');
	 set(hSource,'Units','normalized');
	 %
	 % Command buttons
	 %
	 rect = [290 102 80 20];
	 hPrint = uicontrol('Parent',hFig,...
			    'Style','pushbutton',...
			    'Units','pixels',...
			    'Position',rect,...
			    'CallBack','sbhndlcomp(''edit'');',...
			    'Interruptible','on',...
			    'FontName','Helvetica',...
			    'FontSize',winpref.varSize(2),...
			    'Tag','varNormal',...
			    'HorizontalAlignment','center',...
			    'TooltipString','Edit selected files',...
			    'String','Edit');
	 set(hPrint,'Units','normalized');
	 rect = [290 72 80 20];
	 hBatch = uicontrol('Parent',hFig,...
			    'Style','pushbutton',...
			    'Units','pixels',...
			    'Position',rect,...
			    'CallBack','sbhndlcomp(''script'');',...
			    'Interruptible','on',...
			    'FontName','Helvetica',...
			    'FontSize',winpref.varSize(2),...
			    'Tag','varNormal',...
			    'HorizontalAlignment','center',...
			    'TooltipString','Generate a script file',...
			    'String','Script');
	 set(hBatch,'Units','normalized');
	 rect = [290 42 80 20];
	 hCompile = uicontrol('Parent',hFig,...
			      'Style','pushbutton',...
			      'Units','pixels',...
			      'Position',rect,...
			      'CallBack','sbhndlcomp(''convert'');',...
			      'Interruptible','on',...
			      'FontName','Helvetica',...
			      'FontSize',winpref.varSize(2),...
			      'Tag','varNormal',...
			      'HorizontalAlignment','center',...
			      'TooltipString','Compile selected files',...
			      'String','Compile');
	 set(hBatch,'Units','normalized');
	 rect = [290 12 80 20];
	 hClose = uicontrol('Parent',hFig,...
			    'Style','pushbutton',...
			    'Units','pixels',...
			    'Position',rect,...
			    'CallBack','sbhndlcomp(''close'');',...
			    'Interruptible','on',...
			    'FontName','Helvetica',...
			    'FontSize',winpref.varSize(2),...
			    'Tag','varNormal',...
			    'HorizontalAlignment','center',...
			    'TooltipString','Close window',...
			    'String','Cancel');
	 set(hClose,'Units','normalized');
	 %
	 % Save handles
	 %
	 set(hFig,'UserData',hSource);
      end
      %
      % Update a list of source files
      %
      if (isempty(opt))
	 nsrc = 0;
	 blkFull = {};
	 str = {'<<end of source list>>'};
      else
	 blkName = cellstr(get_param(opt,'Name'));
	 blkPath = cellstr(get_param(opt,'Parent'));
	 blkFull = strcat(blkPath,{'/'},blkName);
	 fileName = cellstr(get_param(opt,'BSNAME'));
	 nsrc = size(fileName,1);
	 str = cell(nsrc+1,1);
	 str(1:nsrc) = fileName;
	 str(end) = {'<<end of source list>>'};
      end
      ud = get(hFig,'UserData');
      set(ud,'String',str,...
	     'Enable','on',...
	     'Value',nsrc+1,...
	     'ListboxTop',1,...
	     'UserData',blkFull);
   case 'resize'
      %
      %
      %
   case 'delete'
      %
      %
      %
   case 'compile'
      % 
      % Compilation message
      % 
      sbhndlmsgs('write','.','append');	   	   % Show progress
   otherwise
      error('sb2sl: sbhndlcomp called with incorrect argument.');
   end
   return                               	   % Last card of SBHNDLCOMP
