function evalcb(action);
% Demo button and listboxes's callbacks. This function is
% called from tour 

%    Yama Habibzai
%    Copyright (c) 1984-98 by The MathWorks, Inc. All Rights Reserved
%    $Revision: 1.15 $
%    $Date: 1997/11/20 14:04:47 $

global helpflag;
figs = allchild(0);
figNumber = findobj(figs, 'Name', 'MATLAB Tour');

switch action
case 'showlist',
  % When the category list(left one) is selected
  
  if helpflag == 1;
    listb = findobj(figNumber, 'tag', 'tooldemos');
    set(listb, 'callback', 'evalcb(''showdemo'')');
  end
  
  param=get(figNumber, 'Userdata');
  pageIndex=param.pageIndex;
  listHndl = findobj(figNumber, 'Tag', 'ToolListTag');
  %listHndl=gco;
  catagoryIndex=get(listHndl, 'value');
%%%%%%%%%%%%%%%%%%%  
  liststring = get(listHndl, 'string');
  sizestring = size(liststring);
  stringvalue = sizestring(1);
  if stringvalue < catagoryIndex(1)
     catagoryIndex(1) = stringvalue;
  end
%%%%%%%%%%%%%%%%%%%%%%%%%%
  % in case of multiple selection, only show the first list
  if isempty(catagoryIndex)
    catagoryIndex = 1;
  else
    catagoryIndex=catagoryIndex(1);
  end
  demoList=char(param.pageList{pageIndex}(catagoryIndex).DemoList);
  onelist=findobj(figNumber, 'Tag', 'tooldemos');
  set(onelist, 'String', demoList, 'value', 1, 'foregroundcolor', [0 0 0]); 
  infoHndl=findobj(figNumber, 'Tag', 'lable1'); 
  
  if pageIndex == 6
    btnLabel=[param.pageList{pageIndex}(catagoryIndex).Name]; 
  else
    btnLabel=[param.pageList{pageIndex}(catagoryIndex).Name ' Demos'];
  end
  % set demo list for seleted catagory and set run button
  set(infoHndl, 'String', btnLabel); 
  runHndl=findobj(figNumber, 'Type', 'uicontrol', 'Tag', 'RunDemo'); 
  
  % Turn off the callback for the right list box and the run button
  % for certain catagories 
  
  lista = findobj(figNumber, 'tag', 'ToolListTag'); 
  liststr = get(lista, 'string'); 
  liststr = cellstr(liststr);   
  
  val = get(lista, 'value');
  
  if isempty(val)
    val = 1;
  else  
    val = val(1);
  end
  
  if strcmp(liststr{val}, 'Products installed with demos:') 
    LocalTurnOff(onelist, runHndl);

  elseif strcmp(liststr{val}, 'Could not find demos for:')
    LocalTurnOff(onelist, runHndl);

  elseif strcmp(liststr{val}, 'Resources')
    LocalTurnOff(onelist, runHndl);
 
  elseif strcmp(liststr{val}, 'Contact Information')
    LocalTurnOff(onelist, runHndl);

  elseif strcmp(liststr{val}, 'Information only:')
    LocalTurnOff(onelist, runHndl);

  elseif strcmp(liststr{val}, 'MATLAB')
    LocalTurnOff(onelist, runHndl);
    
  elseif strcmp(liststr{val}, '');
    LocalTurnOff(onelist, runHndl);
    
  elseif strcmp(demoList(1,:), 'No demos')
    btnLabel = demoList(1,:);
    LocalButtonStr(runHndl, btnLabel)
    set(runHndl, 'enable', 'off', 'string', 'Run:');
    
  else
    
    btnLabel = demoList(1,:);
    LocalButtonStr(runHndl, btnLabel);
    set(runHndl, 'enable', 'on'); 
    set(onelist, 'value', 1);
    
  end
  
case 'showdemo',
  %when the demo listbox(right one) is selected
  figs = allchild(0);
  figNumber = findobj(figs, 'Name', 'MATLAB Tour');
  param=get(figNumber, 'Userdata');
  pageIndex=param.pageIndex;
  CategoryHndl=findobj(figNumber, 'Tag', 'ToolListTag');
  catagoryIndex=get(CategoryHndl, 'value');
  
  if isempty(catagoryIndex)
    catagoryIndex = 1; 
  end
  demoList=char(param.pageList{pageIndex}(catagoryIndex(1)).DemoList);
  FcnList=char(param.pageList{pageIndex}(catagoryIndex(1)).FcnList);
  
  listHndl = findobj(figNumber, 'Tag', 'tooldemos'); 
  demoIndex=get(listHndl, 'value');
  
  %in case of multiple selection
  if isempty(demoIndex)
    demoIndex = 1;
  else
    demoIndex=demoIndex(1);
  end 
  param=get(figNumber, 'Userdata');
  runHndl=findobj(figNumber, 'tag', 'RunDemo');
  if strcmp(demoList(demoIndex,:), 'No demos'),
    set(runHndl, 'Enable', 'off');
    btnLabel='Run Demo';
  else
    btnLabel = demoList(demoIndex, :);
    LocalButtonStr(runHndl, btnLabel)
    
    %if the selection is a double click
    if strcmp(get(figNumber, 'selectiontype'), 'open'),
      set(figNumber, 'Pointer', 'watch');
      %clsdemo(FcnList);
      demoname = FcnList(demoIndex,:);
      eval(demoname);
      watchoff(figNumber);
      
      % Based on Demo's figure name determine to close it or not.
      param.prevdemo = demoname;
      figs = allchild(0);
      siz = size(figs);
      for i = 1:siz(1)
       DemoFig = get(figs(i), 'Name');
         if ~strcmp(DemoFig, 'MATLAB Tour')
           param.DemoWin = DemoFig;
           set(figNumber, 'UserData', param);
         end % if ...
      end   % for ...
    end  % if strcmp ...
  end  % if strcmp ...
case 'toolInfo',
  % about this toolbox button is selected
  param=get(figNumber, 'Userdata');
  pageIndex=param.pageIndex;
  CategoryHndl=findobj(figNumber, 'Tag', 'ToolListTag');
  catagoryIndex=get(CategoryHndl, 'value');
  if isempty(catagoryIndex)
    catagoryIndex = 1;
  end 
  Help=param.pageList{pageIndex}(catagoryIndex).Help;
  
  namestr=param.pageList{pageIndex}(catagoryIndex).Name;
  helpwin(Help, namestr);
  
case 'runtool',
  % run ... demo button is selected
  set(figNumber, 'Pointer', 'watch');
  param=get(figNumber, 'Userdata'); 
  pageIndex=param.pageIndex;
  CategoryHndl=findobj(figNumber, 'Tag', 'ToolListTag');
  catagoryIndex=get(CategoryHndl, 'value');
  catagoryIndex=catagoryIndex(1);
  FcnList=char(param.pageList{pageIndex}(catagoryIndex).FcnList);
  runHndl=findobj(figNumber, 'tag', 'tooldemos');
  demoIndex=get(runHndl, 'value');
  
  % In case of multiple selections
  demoIndex = demoIndex(1);
  %clsdemo(FcnList);
  demoname = FcnList(demoIndex,:);
  eval(demoname);
  watchoff(figNumber);
  
  % Based on Demo's figure name determine to close it or not.
  param.prevdemo = demoname;
  % See if another figure has been opened. This is to trap
  % command line demos.
  
  figs = allchild(0);
  siz = size(figs);
  for i = 1:siz(1)
   DemoFig = get(figs(i), 'Name');
     if ~strcmp(DemoFig, 'MATLAB Tour')
        param.DemoWin = DemoFig;
        set(figNumber, 'UserData', param);
     end
  end
  
case 'back',
  figs = allchild(0);
  figNumber = findobj(figs, 'Name', 'MATLAB Tour');
  FcnList = [ ];
  demoIndex = [];
  %clsdemo(FcnList);
  
  % Make the sliders always be on top
  list = findobj(figNumber, 'type', 'uicontrol', 'style', 'listbox');
  set(list, 'listboxtop', 1);
  tour('showmain');
end;    % if switch ...



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Local Functions %%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalButtonStr(runBtn, labelStr);
nMax = 20;
labelStr = deblank(labelStr);
% first, clip
if (length(labelStr)>nMax)
  labelStr = labelStr(1:nMax);
  
  % find the last space before the nMax'th character
  spaces = findstr(' ', labelStr);
  
  if (isempty(spaces))
    labelStr = [labelStr '...'];
  else
    % truncate to last complete word before nMax
    lastSpace = spaces(length(spaces));
    labelStr = [labelStr(1:lastSpace) '...'];
  end
end
% Check if its a web link
firstword = labelStr(1:2);
if strcmp(firstword, 'Go')
  set(runBtn, 'String', labelStr,...
      'Enable', 'on');
else
set(runBtn, ...
    'String', ['Run:  ' labelStr],...
    'Enable','on');
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% LocalTurnOff %%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalTurnOff(listHandle, ButtonHndle);

set(listHandle, 'callback', ' ', 'value', []);
set(ButtonHndle, 'enable', 'off', 'string', 'Run:');

