function topicshow(topiclist,inittopic,callbackfcn)
%TOPICSHOW  Creates the GUI for slide show organized by topic.

%   Authors: P. Gahinet
%   Revised: A. DiVergilio
%   Copyright 1986-2004 The MathWorks, Inc.
%   $Revision: 1.4.4.2 $  $Date: 2004/04/10 23:13:59 $

fig=figure( ...
    'Name','Topic Show', ...
    'IntegerHandle','off', ...
    'NumberTitle','off', ...
    'DoubleBuffer','on',...
    'Visible','off',...
    'DockControls', 'off');

pos = get(fig,'position');
set(fig,'position',[pos(1) pos(2)-.15*pos(4) pos(3) 1.15*pos(4)])

axBorder = 0.09;
Data.Axes=axes('Parent',fig, ...
    'Units','normalized', ...
    'Position',[axBorder 0.55 1-axBorder-.04 0.41]);

% =====Set up the text Window==========       
top=0.37;
left=0.012;
bottom=0.012;
spacing=0.005;
frmBorder=0.012;
fontSize = 9+3*isunix;

% First, the Text Window frame 
frmPos=[left bottom 1-2*left 0.45-bottom];  % 45
axm = axes('Parent',fig,...
    'Position',frmPos,...
    'Color',[0.50 0.50 0.50],...
    'XLim',[0 1],...
    'YLim',[0 1],...
    'XTick',[],...
    'YTick',[],...
    'DrawMode','fast',...
    'Box','on',...
    'XColor',[0.30 0.30 0.30],...
    'YColor',[0.30 0.30 0.30],...
    'HitTest','off',...
    'HandleVisibility','off');
Data.Counter = text('Parent',axm, ...
    'Units','data', ...
    'Position',[1 1.008], ...
    'String','', ...
    'HorizontalAlignment','right',...
    'VerticalAlignment','bottom',...
    'FontSize',8,...
    'Color',[0.4 0.4 0.4],...
    'HitTest','off',...
    'HandleVisibility','off');
mcwPos=[left+frmBorder bottom+0.07 frmPos(3)-2*frmBorder frmPos(4)-0.07-frmBorder]; 
ax = axes('Parent',fig,...
    'Position',mcwPos,...
    'XLim',[0 1],...
    'YLim',[0 1],...
    'XTick',[],...
    'YTick',[],...
    'DrawMode','fast',...
    'Box','on',...
    'Color',[1 1 1],...
    'XColor',[1 1 1],...
    'YColor',[1 1 1],...
    'HandleVisibility','off');
Data.Text=text('Parent',ax,...
    'Units','data',...
    'Position',[0.005 0.98],...
    'HorizontalAlignment','left',...
    'VerticalAlignment','top',...
    'FontSize',fontSize,...
    'Clipping','on',...
    'HitTest','off',...
    'HandleVisibility','off');
uic = uicontextmenu('Parent',fig);
uicm(1) = uimenu(uic,'Label','8 pt', 'UserData',8, 'Callback',{@LocalFontSize,Data.Text});
uicm(2) = uimenu(uic,'Label','9 pt', 'UserData',9, 'Callback',{@LocalFontSize,Data.Text});
uicm(3) = uimenu(uic,'Label','10 pt','UserData',10,'Callback',{@LocalFontSize,Data.Text});
uicm(4) = uimenu(uic,'Label','11 pt','UserData',11,'Callback',{@LocalFontSize,Data.Text});
uicm(5) = uimenu(uic,'Label','12 pt','UserData',12,'Callback',{@LocalFontSize,Data.Text});
uicm(6) = uimenu(uic,'Label','14 pt','UserData',14,'Callback',{@LocalFontSize,Data.Text});
set(ax,'UIContextMenu',uic);
set(findobj(uicm,'flat','UserData',fontSize),'Checked','on');

% =======Popup==============    
x0 = left+frmBorder;
y0 = bottom + 0.01;
tw = 0.06;
uicontrol('Parent',fig, ...
    'Style', 'text', ...
    'Units','normalized', ...
    'Position',[x0 y0 tw+.01 0.04], ...
    'String','Topic:', ...
    'FontWeight','normal',...
    'BackgroundColor',[0.50 0.50 0.50],...
    'ForegroundColor',[1 1 1],...
    'HorizontalA','left',...
    'Callback',''); 
Data.Popup = uicontrol('Parent',fig, ...
    'Style', 'popupmenu', ...
    'Units','normalized', ...
    'Position',[x0+tw+.02 y0 .35 0.05], ...
    'String',topiclist, ...
    'Value',inittopic,...
    'Callback',{@LocalChangeTopic callbackfcn}); 

% =======Buttons==============    
bGap = 0.02; 
x0 = mcwPos(1)+mcwPos(3);
bw = 0.12;
uicontrol('Parent',fig, ...
    'Style', 'pushbutton', ...
    'Units','normalized', ...
    'Position',[x0-bw y0 bw 0.05], ...
    'String','Close', ...
    'Callback','close(gcbf)');
x0 = x0-bw-bGap;
Data.Next = uicontrol('Parent',fig, ...
    'Style', 'pushbutton', ...
    'Units','normalized', ...
    'Position',[x0-bw y0 bw 0.05], ...
    'String','Next >>', ...
    'Tag','next',...
    'Callback',{@LocalChangeSlide callbackfcn});
x0 = x0-bw-bGap;
Data.Prev = uicontrol('Parent',fig, ...
    'Style', 'pushbutton', ...
    'Units','normalized', ...
    'Position',[x0-bw y0 bw 0.05], ...
    'String','Prev <<', ...
    'Tag','prev',...
    'Enable','off',...
    'Callback',{@LocalChangeSlide callbackfcn});

% Initialize
Data.Topic = inittopic;
Data.Slide = 1;
Nslides = feval(callbackfcn,Data.Topic,0);
set(Data.Text,'String',feval(callbackfcn,Data.Topic,Data.Slide));
set(Data.Counter,'String',sprintf('Slide %d/%d',Data.Slide,Nslides));

set(fig,'Visible','on','HandleVisibility','callback',...
   'UserData',Data,'CurrentAxes',Data.Axes);



%------------------- Local Functions ----------------------

%%%%%%%%%%%%%%%%%%%%
% LocalChangeSlide %
%%%%%%%%%%%%%%%%%%%%
function LocalChangeSlide(hSrc,event,callbackfcn)
% Callbacks from previous and next button
Fig = get(hSrc,'parent');
setptr(Fig,'watch')
Data = get(Fig,'UserData');
Nslides = feval(callbackfcn,Data.Topic,0);
switch get(hSrc,'Tag')
case 'next'
    Data.Slide = Data.Slide + 1;
    if Data.Slide==2
        set(Data.Prev,'Enable','on')
    end
    if Data.Slide==Nslides
        set(Data.Next,'Enable','off')
    end
case 'prev'
    Data.Slide = Data.Slide - 1;
    if Data.Slide==1
        set(Data.Prev,'Enable','off') 
    end
    if Data.Slide==Nslides-1
        set(Data.Next,'Enable','on')
    end
end
set(Fig,'UserData',Data)
% Update content
set(Data.Text,'String',feval(callbackfcn,Data.Topic,Data.Slide));
set(Data.Counter,'String',sprintf('Slide %d/%d',Data.Slide,Nslides));
setptr(Fig,'arrow')

%%%%%%%%%%%%%%%%%%%%
% LocalChangeTopic %
%%%%%%%%%%%%%%%%%%%%
function LocalChangeTopic(hSrc,event,callbackfcn)
% Callbacks from previous and next button
Fig = get(hSrc,'parent');
setptr(Fig,'watch')
Data = get(Fig,'UserData');
Data.Topic = get(hSrc,'Value');
Data.Slide = 1;
Nslides = feval(callbackfcn,Data.Topic,0);
set(Fig,'UserData',Data)
% initialize
set(Data.Text,'String',feval(callbackfcn,Data.Topic,Data.Slide));
set(Data.Prev,'Enable','off')
set(Data.Next,'Enable','on')
set(Data.Counter,'String',sprintf('Slide %d/%d',Data.Slide,Nslides));
setptr(Fig,'arrow')

%%%%%%%%%%%%%%%%%
% LocalFontSize %
%%%%%%%%%%%%%%%%%
function LocalFontSize(hSrc,event,h,n)
% Change fontsize
set(h,'FontSize',get(hSrc,'UserData'))
set(get(get(hSrc,'Parent'),'Children'),'Checked','off')
set(hSrc,'Checked','on')
