function ToggleHandles = toolbar(sisodb)
%TOOLBAR  Builds SISO Tool toolbar and manages global edit modes.

%   Author(s): P. Gahinet
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.8 $ $Date: 2000/08/25 21:35:17 $

% Load icon images
load sisoicons
uicolor = get(0,'defaultuicontrolbackground');
uicolor = uicolor(1);
rpole(isinf(rpole)) = uicolor;
cpole(isinf(cpole)) = uicolor;
rzero(isinf(rzero)) = uicolor;
czero(isinf(czero)) = uicolor;
erase(isinf(erase)) = uicolor;

% Create toolbar
t = uitoolbar(sisodb.Figure,'HandleVisibility','off');

% Arrow icon (default mode)
a = uitoggletool('Parent',t,...
    'Tooltip','Default mode',...
    'State','on',...
    'CData',arrow);

% Create toggle buttons for Pole/Zero editing
b(1) = uitoggletool('Parent',t,...
    'Tooltip','Add real pole',...
    'CData',rpole);
b(2) = uitoggletool('Parent',t,...
    'Tooltip','Add real zero',...
    'CData',rzero);
b(3) = uitoggletool('Parent',t,...
    'Tooltip','Add complex pole',...
    'CData',cpole);
b(4) = uitoggletool('Parent',t,...
    'Tooltip','Add complex zero',...
    'CData',czero);
b(5) = uitoggletool('Parent',t,...
    'Tooltip','Delete pole/zero',...
    'CData',erase);

% Create toggle buttons for zooming
z(1) = uitoggletool('Parent',t,...
    'Separator','on',...
    'Tooltip','Mouse zoom',...
    'CData',mousezoom);
z(2) = uitoggletool('Parent',t,...
    'Tooltip','Zoom in X',...
    'CData',zoominx);
z(3) = uitoggletool('Parent',t,...
    'Tooltip','Zoom in Y',...
    'CData',zoominy);
z(4) = uitoggletool('Parent',t,...
    'Tooltip','Zoom out (full view)',...
    'CData',fullview);

% Create CS help button
h = uitoggletool('Parent',t,...
    'Separator','on',...
    'Tooltip','Context-sensitive help',...
    'CData',whatsthis);

% Disable toolbar at start (except help if doc installed)
set([b,z,h],'HandleVisibility','off','Enable','off')
if ~isempty(docroot)
    set(h,'Enable','on')
end

% Default mode
ToggleHandles = [a,b,z,h];
set(a,'ClickedCallback',{@LocalReset ToggleHandles}); 

% Add and delete callbacks
set(b(1),'OnCallback',{@LocalEnterMode sisodb 'add' {'Real' 'Pole'} ToggleHandles})
set(b(2),'OnCallback',{@LocalEnterMode sisodb 'add' {'Real' 'Zero'} ToggleHandles})
set(b(3),'OnCallback',{@LocalEnterMode sisodb 'add' {'Complex' 'Pole'} ToggleHandles})
set(b(4),'OnCallback',{@LocalEnterMode sisodb 'add' {'Complex' 'Zero'} ToggleHandles})
set(b(5),'OnCallback',{@LocalEnterMode sisodb 'delete' [] ToggleHandles})

% Zoom callbacks
set(z(1),'OnCallback',{@LocalEnterMode sisodb 'zoom' 'x-y' ToggleHandles})
set(z(2),'OnCallback',{@LocalEnterMode sisodb 'zoom' 'in-x' ToggleHandles})
set(z(3),'OnCallback',{@LocalEnterMode sisodb 'zoom' 'in-y' ToggleHandles})
set(z(4),'ClickedCallback',{@LocalZoomOut sisodb z});

% Off callbacks
set([b,z(1:3)],'OffCallback',{@LocalExitMode sisodb a})

% Help callbacks
set(h,'OnCallback',{@LocalEnterHelp sisodb [a,b,z]},...
    'OffCallback',{@LocalExitHelp sisodb a})

% Listen to changes in editor mode 
lsnr = handle.listener(sisodb,...
    'LocalModeChange',{@LocalReset ToggleHandles});
set(a,'UserData',lsnr);



%----------------------------- Callback actions ----------------------------
   

%%%%%%%%%%%%%%%%%%%%%%
%%% LocalEnterMode %%%
%%%%%%%%%%%%%%%%%%%%%%
function LocalEnterMode(hSrc,event,sisodb,NewMode,TypeData,hToggle)
% Callback when engaging global edit mode

% Exit current global mode and revert to idle mode
% RE: Reverting to idle makes sure Add/Zoom submenus get correctly set (listener
%     may not be triggered if global mode = current mode)
sisodb.GlobalMode = 'off';
set(sisodb.PlotEditors,'EditMode','idle');
set(hToggle(hToggle~=hSrc),'State','off')   % no side effect (global mode off)

% Enter new mode and notify editors of change of mode/scope
sisodb.GlobalMode = 'on';
set(sisodb.PlotEditors,'ModeData',TypeData,'EditMode',NewMode);


%%%%%%%%%%%%%%%%%%%%%
%%% LocalExitMode %%%
%%%%%%%%%%%%%%%%%%%%%
function LocalExitMode(hSrc,event,sisodb,hArrow)
% Callback from exiting global mode (icon state off)
if strcmp(sisodb.GlobalMode,'on')
    % Exit mode
    sisodb.GlobalMode = 'off';
    set(sisodb.PlotEditors,'EditMode','idle');
    set(hArrow,'State','on');
end


%%%%%%%%%%%%%%%%%%
%%% LocalReset %%%
%%%%%%%%%%%%%%%%%%
function LocalReset(hSrc,event,hToggle)
% Callback when changing edit mode locally, through ESC, or by pressing arrow
set(hToggle(1),'State','on')
set(hToggle(2:end),'State','off')


%%%%%%%%%%%%%%%%%%%%
%%% LocalZoomOut %%%
%%%%%%%%%%%%%%%%%%%%
function LocalZoomOut(hSrc,event,sisodb,hZoom)
% Zoom out callback
idxvis = find(strcmp(get(sisodb.PlotEditors,'Visible'),'on'));
for ct=idxvis'    
    % Zoom out in each editor
    zoomout(sisodb.PlotEditors(ct));
end
% Release zoom icons
set(hZoom,'State','off')


%%%%%%%%%%%%%%%%%%%%%%
%%% LocalEnterHelp %%%
%%%%%%%%%%%%%%%%%%%%%%
function LocalEnterHelp(hSrc,event,sisodb,hABZ)
% Enter CS help mode
set(hABZ,'State','off')
% Make sure can't open Import (can interfer badly with CS help by changing plots)
set(sisodb.HG.Menus.File.Import,'enable','off')
sisodb.Figure.CSHelpMode = 'on';


%%%%%%%%%%%%%%%%%%%%%
%%% LocalExitHelp %%%
%%%%%%%%%%%%%%%%%%%%%
function LocalExitHelp(hSrc,event,sisodb,hArrow)
% Exit CS help mode
sisodb.Figure.CSHelpMode = 'off';
set(sisodb.HG.Menus.File.Import,'enable','on')
set(hArrow,'State','on')
