function cmdlyplay(action);
% Slide show player that will display command line
% demos using listboxes as the text and command
% windows. 

%    Yama Habibzai
%    Copyright (c) 1984-98 by The MathWorks, Inc. All Rights Reserved
%    $Revision: 1.3 $
%    $Date: 1997/12/03 17:29:52 $

if nargin<1,
  disp('You must assign a script, for example: cmdlplay intro');
    break
else
  if ~strcmp(action(1), '#')
    filename=action;
    action='initialize';
  else
    action=action(2:end);
  end;
end;

switch action
case 'initialize',
  
  LocalInit(filename);
  
case 'next',
  % go to next slide
  slideData=get(gcbf, 'UserData');
  autoHndl=findobj(gcf, 'Style', 'checkbox', 'Tag', 'autoPlay');
  if get(autoHndl,'Value'),
    % If the autoHndl flag is set, don't wait for the user, just
    % pause a little and then continue.
    
    cmdlen=length(slideData.slide);
    n=slideData.index;
    while (n<cmdlen)& get(autoHndl,'Value'),
      LocalDoCmd(1);
      drawnow;
      pause(2);
      n=n+1;
    end;  % while ...
  else
    LocalDoCmd(1);
  end; % if ...
  
case 'back',
  LocalDoCmd(-1);
  
case 'reset',
  slideData=get(gcbf,'UserData');
  slideData.index=1;
  set(gcf, 'UserData', slideData);
  LocalDoCmd(0);
  
case 'info',
  ttlStr=get(gcbf,'Name');
  Help= {'This is a slideshow player to show command line demos.'
  'Click on Start, Next, and Prev buttons to see the slides.'
  ' '
  'filename: cmdlplay.m'};
  helpwin(Help, ttlStr);
end;    % if  switch ...
    
% End of function cmdlplay 
    
    
    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%  LocalInit   %%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalInit(filename)
figNumber=figure( ...
    'Name','Slide Player', ...
    'NumberTitle','off', ...
    'Visible','off');

%===================================
top=0.37;
left=0.05;
right=0.75;
bottom=0.05;
spacing=0.005;
frmBorder=0.02;
btnWid=0.15;


%=====Set up the text Window==========
% First, the Text Window frame
frmPos=[left-frmBorder bottom+top+bottom+(5*frmBorder)+.02 ...
      (right-left)+2*frmBorder (top-bottom+2*frmBorder)+.02];

frmHandle=LocalBuildFrmTxt(frmPos, '', 'frame', '', '');

%===== The MATLAB output listbox window frame ====

frmPos =[left-frmBorder bottom-frmBorder...
      (right-left)+2*frmBorder  top+bottom+(5*frmBorder)+.01];
frmHandle2=LocalBuildFrmTxt(frmPos, '', 'frame', '', '');

% ====== The List boxes =========================
hndl = uicontrol('style','listbox','Units',...
    'Normalized', 'position', [left bottom,...
      right-left top+bottom+(frmBorder)],...
    'BackGroundColor', [1 1 1],...
    'Max', 2, 'value', [], 'tag', 'listag');

% List box for Comments
mcwPos=[left .615 (right-left) top-bottom+.01];
brevcomlist = findobj(gcf, 'tag', 'commentag');

Hndl = uicontrol('style', 'list',...
    'BackGroundColor', [1 1 1],...
    'units', 'normal',...
    'ForeGroundColor', [0 0 0],...
    'Position', mcwPos, 'max', 2, 'value', [],...
    'tag', 'commentag');

%===== Button Info ====
left = .8;

%======The CONSOLE frame===============
frmBorder=0.02;
yPos=bottom-frmBorder;
frmPos=[left-frmBorder yPos btnWid+2*frmBorder 0.9+2*frmBorder];
frmHandle=LocalBuildFrmTxt(frmPos, '', 'frame', '', '');

%======The frame ======================
frmBorder=0.02;
btnHt=0.04;
yPos=top+.55;
btnPos=[left yPos btnWid btnHt];
slideHandle=LocalBuildFrmTxt(btnPos, 'Slide 1',...
            'text', 'slide', '');
set(slideHandle, 'HorizontalAlignment','center');

%======The Next button=================
btnNumber=1;
labelStr='Start>>';
callbackStr='cmdlplay #next';
nextHndl=LocalBuildBtn('pushbutton', btnNumber,...
    labelStr, callbackStr, 'next');
%======The back button=================
btnNumber=2;
labelStr='Prev<<';
callbackStr='cmdlplay #back';
backHndl=LocalBuildBtn('pushbutton', btnNumber,...
    labelStr, callbackStr, 'back');

% =========== The Text ===============
uicontrol('style', 'text', 'units', 'normalized',...
    'position', [.15 .5 .5 .03],...
    'string', 'MATLAB Command line output',...
    'ForeGroundColor', [1 1 1],...
    'fontsize', 12, 'BackGroundColor', [.5 .5 .5]);

%=======The reset button===============
btnNumber=3;
labelStr='Reset';
callbackStr='cmdlplay #reset';
resetHndl=LocalBuildBtn('pushbutton', btnNumber,...
    labelStr, callbackStr, 'reset');

%=======The INFO button===============
infoHndl=LocalBuildBtn('pushbutton', 0,...
    'Info', 'cmdlplay #info', 'info');

%=======The CLOSE button==============
closeHndl=LocalBuildBtn('pushbutton', 0,...
    'Close', 'close(gcf)', 'close');

% Now initiate userdata and uncover the figure
LocalInitUserData(nextHndl, backHndl);
set(figNumber,'Visible','on');

%============ open script =============
% the script "filename" creates a structure called slideData

slideData=get(gcf, 'UserData');
slideData.slide=eval(filename);
slideData.index=1;
set(gcf, 'UserData', slideData);
LocalDoCmd(0);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%  LocalInitUserData  %%%%%%%%%%%%%%%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalInitUserData(nextHndl, backHndl)
% Initialize index, slide, param, ...
slideData.index=1;
slideData.nexthandle=nextHndl;
slideData.backhandle=backHndl;
slideData.slide(1).cont={''};
slideData.slide(1).text={''};
slideData.param(1).vars={};
set(gcf, 'Userdata', slideData);
LocalDoCmd(0);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%  LocalDoCmd  %%%%%%%%%%%%%%%%%%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalDoCmd(ichange)
% execute the command in the command window
% when ichange = 1, go to the next slide;
% when ichange = -1, go to the previous slide;
% when ichange = 0, stay with the current slide;

% retrieve variables from saved UserData workspace
slideData=get(gcf,'UserData');
SlideShowi=slideData.index+ichange;
cmdlen=length(slideData.slide);
if SlideShowi>1
  SlideShowVars=slideData.param(SlideShowi-1).vars;
  for SlideShown=1:size(SlideShowVars,1);
    eval([SlideShowVars{SlideShown,1} '=SlideShowVars{SlideShown,2};']);
  end;
end;

%  guarantee the index is always inside the boundary
if SlideShowi<=0,
  SlideShowi=1;
elseif SlideShowi>cmdlen
  SlideShowi=cmdlen;
end
LocalEnableBtns(SlideShowi, slideData);

% get slides
SlideShowcmdS=slideData.slide(SlideShowi).cont;
if length(SlideShowcmdS)>0
  SlideShowcmdS=char(SlideShowcmdS);
else
  SlideShowcmdS='';
end
SlideShowcmdNum=size(SlideShowcmdS, 1);
SlideShowtextStr=slideData.slide(SlideShowi).text;

% consider the empty case
if length(SlideShowtextStr)>0
  SlideShowtextStr=char(SlideShowtextStr);
else
  SlideShowtextStr='';
end
sHndl=findobj(gcf, 'Type', 'uicontrol', 'Tag', 'slide');
set(sHndl, 'String', ['Slide ', num2str(SlideShowi)]);

top=0.37;
left=0.05;
right=0.75;
bottom=0.05;
spacing=0.005;
frmBorder=0.02;
btnWid=0.15;

frmPos =[left-frmBorder (bottom+top)+frmBorder...
      (right-left)+2*frmBorder (top+bottom+5*frmBorder)];

% List box for MATLAB commands
brevlist = findobj(gcf, 'tag', 'listag');

set(brevlist, 'String', SlideShowcmdS,...
    'Max', 2, 'value', []);

brevcomlist = findobj(gcf, 'tag', 'commentag'); 
set(brevcomlist, 'String', SlideShowtextStr,...
    'Max', 2, 'value', []);

slideData.index=SlideShowi;
set(gcf, 'UserData', slideData);

% clear all cmdlplay specific variables
clear SlideShowVars SlideShowcmdS SlideShowNoCmt
clear cmdlen SlideShowi ichange
clear SlideShown mcwHndl SlideShowtextStr slideData SlideShowcmdNum
% put variables into UserData workspace
vars=who;
for SlideShown=1:size(vars,1),
  vars{SlideShown,2}=eval(vars{SlideShown,1});
end
slideData=get(gcf, 'UserData');

slideData.param(slideData.index).vars=vars;
set(gcf, 'UserData', slideData);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%  LocalEnableBtns  %%%%%%%%%%%%%%%%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function LocalEnableBtns(i, slideData)
% control the enable property for Next and Prev. buttons
cmdlen=length(slideData.slide);
nextHndl=slideData.nexthandle;
backHndl=slideData.backhandle;
if i==cmdlen,
  set(nextHndl, 'Enable', 'off');
  set(backHndl, 'Enable', 'on');
elseif i==1,
  set(backHndl, 'Enable', 'off');
  set(nextHndl, 'Enable', 'on', 'String', 'Start>>');
else
  set(nextHndl, 'Enable', 'on', 'String', 'Next>>');
  set(backHndl, 'Enable', 'on');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%  LocalBuildFrmTxt %%%%%%%%%%%%%%%%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function frmHandle=LocalBuildFrmTxt(frmPos, txtStr, uiStyle,...
    uiTag, uiCallback)
% build frame and label
frmHandle=uicontrol( ...
    'Style', uiStyle, ...
    'HorizontalAlignment','left', ...
    'Units','normalized', ...
    'Max', 20, ...
    'Position',frmPos, ...
    'BackgroundColor',[0.50 0.50 0.50], ...
    'ForegroundColor',[1 1 1], ... %generates an edge
    'String', txtStr, ...
    'Tag', uiTag);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%  LocalBuildBtn  %%%%%%%%%%%%%%%%%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function btHandle=LocalBuildBtn(btnStyle, btnNumber,...
    labelStr, callbackStr, uiTag)
% build buttons and check boxes on the right panel
labelColor=[0.8 0.8 0.8];
top=0.9;
left=0.80;
btnWid=0.15;
btnHt=0.06;
bottom=0.05;
% Spacing between the button and the next command's label
spacing=0.03;

yPos=top-(btnNumber-1)*(btnHt+spacing);
if strcmp(labelStr, 'Close')==1
  yPos= bottom;
elseif strcmp(labelStr, 'Info')==1
  yPos= bottom+btnHt+spacing;
else
  yPos=top-(btnNumber-1)*(btnHt+spacing)-btnHt;
end
% ui position
btnPos=[left yPos btnWid btnHt];
btHandle=uicontrol( ...
    'Style', btnStyle, ...
    'Units','normalized', ...
    'Position',btnPos, ...
    'String',labelStr, ...
    'Tag', uiTag, ...
    'Callback',callbackStr);



