function out = playbiodemo(action,caller)
%PLAYBIODEMO Blocks the direct executuion of a Bioinformatics Demo.
%
%  Demonstrations in the Bioinformatics Toolbox should only be executed
%  from the MATLAB editor/debugger. PLAYBIODEMO detects if the demo is
%  being called as a script file and prompts for the user to decide if
%  he/she wants either to see the HTML demo using the help browser or to
%  run the demo with the MATLAB editor/debugger to be able to step through
%  the demo and interact with the demo variables in the MATLAB command
%  window. 
%
%  Example:
%
%        if playbiodemo return; end

%   Copyright 2003-2004 The MathWorks, Inc.
%   $Revision: 1.1.6.7 $  $Date: 2004/12/24 20:41:11 $
%

dbs = dbstack;               % stack of functions
filename = dbs(end).name;    % caller program

% First detect all the cases in which we should let the script continue:

% 1) if there are breakpoints in the file, it means it is being debugged
if numel(dbstatus(filename))     
    out = false;  % let the demo continue
    return
end

% 2) if the MATLAB interactive desktop is not running, can not edit or open
if ~usejava('desktop')
    out = false;  % let the demo continue
    return
end

% 3) if there are not 2 functions in the stack, playbiodemo was called in
% cell mode or when publishing 
if numel(dbs) ~= 2     
    out = false;  % let the demo continue
    return
end

% Now query the user:

switch questbiodemo
    case 'Codepad'
        edit(filename) % open the file
        out = true;    % and stop the demo
    case 'DemoBrowser'
        indexhelper(fileparts(which(filename)),filename,...
                    'Bioinformatics','',['html' filesep filename '.html'])
        out = true;    % and stop the demo
    case 'ContinueScript'
        out = false;   % let the demo continue
    otherwise % got here by canceling
        out = true;    % just stop the demo
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function selection = questbiodemo
% quest window to select the method to run a demonstration

% set the strings
CBString = 'uiresume(gcbf)';

IntroStr = 'Demonstrations in Bioinformatics Toolbox are not intended to be run directly as scripts.';
Option1Str = 'Open the demo code in the MATLAB Editor and run it step by step using the "Cell" tools or the advanced debugging options.';
Option2Str = 'View the HTML version of the demo in the Help Browser.';    
Option3Str = 'Continue with the script demo without pausing.';    
WindowTitlte = 'Bioinformatics Toolbox Demonstration';
BkgColor = [0.831373 0.815686 0.784314];
% setup the quest box
c = get(0,'ScreenSize')*[1 0;0 1;.5 0;0 .5];
QuestFig = figure('WindowStyle','modal','Color',BkgColor,...
             'Position',[c-[180 100] 360 222],'Resize','off','NumberTitle','off',...
             'KeyPressFcn',@doFigureKeyPress,'IntegerHandle','off',...
             'Name',WindowTitlte);
uicontrol(QuestFig,'style','text','Position',[21 175 320 30],'HorizontalAlignment','left','string',IntroStr,'BackgroundColor',BkgColor)
h1 = uibuttongroup('Position',[.04 .20 .92 .50],'BorderType','none','BackgroundColor',BkgColor);
uicontrol(h1,'style','radiobutton','Position',[5 98 20 20],'BackgroundColor',BkgColor,'Tag','Codepad','value',1);
uicontrol(h1,'style','radiobutton','Position',[5 50 20 20],'BackgroundColor',BkgColor,'Tag','DemoBrowser');
uicontrol(h1,'style','radiobutton','Position',[5 20 20 20],'BackgroundColor',BkgColor,'Tag','ContinueScript');
uicontrol(h1,'style','text','Position',[25 65 310 50],'HorizontalAlignment','left','string',Option1Str,'BackgroundColor',BkgColor)
uicontrol(h1,'style','text','Position',[25 37 310 30],'HorizontalAlignment','left','string',Option2Str,'BackgroundColor',BkgColor)
uicontrol(h1,'style','text','Position',[25 5 310 30],'HorizontalAlignment','left','string',Option3Str,'BackgroundColor',BkgColor)
uicontrol(QuestFig,'style','pushbutton','Position',[80 15 60 30],'string','OK','Callback',CBString,'KeyPressFcn',@doControlKeyPress,'BackgroundColor',BkgColor);
uicontrol(QuestFig,'style','pushbutton','Position',[195 15 60 30],'string','Cancel','Callback',CBString,'KeyPressFcn',@doControlKeyPress,'BackgroundColor',BkgColor);


% make sure we are on screen
movegui(QuestFig)
set(QuestFig,'Visible','on');
drawnow;

% wait for an event (figure is modal)
uiwait(QuestFig);

% probably the figure was closed by other method, check if the handle still
% exists
if ishandle(QuestFig) 
    % check is a default shorcut was pressed ('return' or 'space' without
    % selecting an object)
    if isappdata(QuestFig,'DefaultPressed') && getappdata(QuestFig,'DefaultPressed')
        action = 'OK';
    else %if not, get the current value
        action = get(get(QuestFig,'CurrentObject'),'String');
    end
    switch action
        case 'OK'
            selection = get(findobj(QuestFig,'style','radiobutton','value',1),'tag');
        otherwise
            selection = 'Cancel';
    end
    delete(QuestFig);
else
    selection = 'Cancel';
end

function doFigureKeyPress(obj, evd)
switch(evd.Key)
    case {'return','space'}
        setappdata(gcbf,'DefaultPressed',true)
        uiresume(gcbf);
    case 'escape'
        delete(gcbf)
end

function doControlKeyPress(obj, evd)
switch(evd.Key)
    case 'return'
        uiresume(gcbf);
    case 'escape'
        delete(gcbf) 
end



