function [progress_hdl] =  rri_progress_status(varargin),
%
%   progress_hdl = rri_progress_status('Create',fig_title);
%   progress_hdl = rri_progress_status(fig_hdl,'Create',fig_title);
%
%   rri_progress_status(progress_hdl,'Update_bar',amount);
%   rri_progress_status(progress_hdl,'Show_message',message);
%   rri_progress_status(progress_hdl,'Clear_bar');
%   rri_progress_status(progress_hdl,'Clear_message');
%
%   Note: if 'fig_hdl' is specified in 'Create' operation, the figure   
%         with handle 'fig_hdl' will be converted to the progress figure;
%         otherwise, a new figure is created.
%

   %  Create a new figure for the progress figure
   %
   if ischar(varargin{1}) & strcmp(upper(varargin{1}),'CREATE') 
       fig_title = varargin{2};			
       progress_hdl = create_progress_figure([],fig_title);
       return; 
   end;

   if ~ishandle(varargin{1})
       disp('ERROR: Syntax error for calling "rri_progress_status".');
       return; 
   end;

   fig_hdl = varargin{1};

   action = upper(varargin{2});
   switch upper(action)
     case {'CREATE'}		    % make 'fig_hdl' as the progress figure
        fig_title = varargin{3};
        progress_hdl = create_progress_figure(fig_hdl,fig_title);
     case {'UPDATE_BAR'}
        amount = varargin{3};
        show_progress_bar(fig_hdl,amount);
     case {'SHOW_MESSAGE'}
        message = varargin{3};
        show_progress_message(fig_hdl,message);
     case {'CLEAR_BAR'}
        show_progress_bar(fig_hdl,[]);
     case {'CLEAR_MESSAGE'}
        show_progress_message(fig_hdl,'');
   end;  

   return				% rri_progress_status


%-----------------------------------------------------------------------------
function fig_hdl = create_progress_figure(fig_hdl,f_name),

   screen_size = get(0,'ScreenSize');
   fig_w = 420;
   fig_h = 150;

   if ~isempty(fig_hdl),
      child_hdls = get(fig_hdl,'Child');
      delete(child_hdls);
      set(fig_hdl,'Units','pixels');
      fig_pos = get(fig_hdl,'Position');
      fig_pos(3) = fig_w; fig_pos(4) = fig_h;
   else
      fig_x = (screen_size(3) - fig_w)/2;
      fig_y = (screen_size(4) - fig_h)/2;
      fig_pos = [fig_x fig_y fig_w fig_h];
      fig_hdl = figure;
   end;

   if isempty(f_name),
      f_name = 'Progress Status';
   end;

   set(fig_hdl,'ResizeFcn','');

   set(fig_hdl, ...
	'Units','pixels', ...
        'Color',[0.8 0.8 0.8], ...
	'Colormap', jet, ...
	'Doublebuffer','on', ...
	'Menubar', 'none', ...
	'Resize', 'off', ...
	'ResizeFcn', '', ...
        'Position', fig_pos, ...
	'NumberTitle', 'off', ...
	'Name', f_name, ...
	'Windowstyle','modal', ...
        'Tag', 'ProgressFigure');


   ax_pos = [40 65 fig_pos(3)-100 50];
   ax_hdl = axes('Parent',fig_hdl, ...
             'units','pixels', ...
             'Position',ax_pos, ...
             'XLim',[0 1], 'YLim',[0 1], 'ZLim',[0 1], ...
             'YTick',[], 'XTick',[0:0.2:1], ...
             'XTickLabel',{'0%';'20%';'40%';'60%';'80%';'100%'});

   rectangle('Position',[0 0 1 1],'FaceColor','none');		% bar boundary

   progress_bar = rectangle('Position',[0 0 1 1], ...		% progress bar
                            'FaceColor',[0 0 1], ...
                            'Visible', 'off');

   message_line = uicontrol('Parent',fig_hdl, ...               % Message Line
        'Style','text', ...
        'Units','pixels', ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0.0 0.0 0.8], ...
        'FontSize',12, ...
        'HorizontalAlignment','left', ...
        'Position',[5 0 fig_w 20], ...
        'String','', ...
        'Tag','MessageLine');
   
   setappdata(gcf,'ProgressAxes',ax_hdl);
   setappdata(gcf,'ProgressBar',progress_bar);
   setappdata(gcf,'MessageLine',message_line);
   setappdata(gcf,'ProgressStart',0);
   setappdata(gcf,'ProgressScale',1);

   drawnow;
   
   return;				% create_progress_figure;


%-----------------------------------------------------------------------------
function show_progress_bar(fig_hdl,amount),

   progress_axes = getappdata(gcf,'ProgressAxes');
   progress_bar = getappdata(gcf,'ProgressBar');
   progress_start = getappdata(gcf,'ProgressStart');
   progress_scale = getappdata(gcf,'ProgressScale');

   if isempty(amount),				% reset
      set(progress_bar,'Position',[0 0 1 1],'Visible','off');
      drawnow;
      return;
   end;

   curr_progress = amount*progress_scale + progress_start;

   axes(progress_axes);
   set(progress_bar,'Position',[0 0 curr_progress 1],'Visible','on');

   drawnow;

   return;				% show_progress_bar


%-----------------------------------------------------------------------------
function show_progress_message(fig_hdl,message),

   message_line = getappdata(gcf,'MessageLine');
   set(message_line,'String',message);
   drawnow;

   return;				% show_progress_message
