function dummy = initialize(Editor,Anchors)
%INITIALIZE  Initializes Bode Diagram Editor.

%   Author(s): P. Gahinet
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.11 $ $Date: 2000/08/02 16:06:22 $

% REVISIT
dummy = [];

% Render editor
Editor.HG = LocalRender(Editor,Anchors);

% Set (and draw) labels
% REVISIT: Simplify when h.Title.String = xxx works
Title = Editor.Title;
Title.String = 'Open-Loop Bode Diagram';
Title.Visible = 'off';
Editor.Title = Title;
Xlabel = Editor.Xlabel;
Xlabel.String = sprintf('Frequency (%s)',Editor.FrequencyUnits);
Xlabel.Visible = 'off';
Editor.Xlabel = Xlabel;
Ylabel = Editor.Ylabel;
Ylabel.String = {sprintf('Magnitude (%s)',Editor.MagnitudeUnits);...
        sprintf('Phase (%s)',Editor.PhaseUnits)};
Ylabel.Visible = 'off';
Editor.Ylabel = Ylabel;


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

%%%%%%%%%%%%%%%%%%%
%%% LocalRender %%%
%%%%%%%%%%%%%%%%%%%
function HG = LocalRender(Editor,Anchors)

SISOfig = Anchors.Figure; % host figure

% Plot axes
MagAxes = axes('Parent',SISOfig, ...
    'Units','norm', ...
    'DrawMode','fast', ...
    'Box','on',...
    'Visible','off', ...
    'XColor',Editor.AxesForegroundColor, ...
    'Xlim',[0.1,10], ...
    'Xscale',Editor.FrequencyScale, ...
    'XtickLabel',[], ...
    'YColor',Editor.AxesForegroundColor, ...
    'Ylim',[-20,20],...
    'Yscale',Editor.MagnitudeScale,...
    'HelpTopicKey','sisomagnitudeplot',...
    'ButtonDownFcn',{@LocalButtonDown Editor});

PhaseAxes = axes('Parent',SISOfig, ...
    'Units','norm', ...
    'DrawMode','fast', ...
    'Box','on',...
    'Visible','off', ...
    'XColor',Editor.AxesForegroundColor, ...
    'Xlim',[0.1 10], ...
    'Xscale',Editor.FrequencyScale, ...
    'YColor',Editor.AxesForegroundColor, ...
    'Ylim',[-90 90],...
    'HelpTopicKey','sisophaseplot',...
    'ButtonDownFcn',{@LocalButtonDown Editor});

PlotAxes = [MagAxes;PhaseAxes];

% Initialize grid
set(PlotAxes,'Xgrid',Editor.Grid,'Ygrid',Editor.Grid);

% Add context menus
U = uicontextmenu('Parent',SISOfig); 
set(PlotAxes,'UIContextMenu',U)  % anchor to plot axes
LocalCreateMenus(Editor,U,1);
set(get(U,'children'),'Enable','off')

% Replicate menus in main figure
LocalCreateMenus(Editor,Anchors.Menu,0);

% Build structure of HG handles
HG = struct(...
    'BodePlot',zeros(2,1),...
    'Compensator',struct('Magnitude',[],'Phase',[]),...
    'GainMargin',[],...
    'NyquistLine',zeros(2,0),...
    'PhaseMargin',[],...
    'PlotAxes',PlotAxes,...
    'StatusText',Anchors.Status,...
    'System',[]);

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

%%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalCreateMenus %%%
%%%%%%%%%%%%%%%%%%%%%%%%
function LocalCreateMenus(Editor,MenuAnchor,EditFlag)
% Builds right-click menus

% Edit pole/zero group
addmenu(Editor,MenuAnchor,'add');
addmenu(Editor,MenuAnchor,'delete');
if EditFlag
    addmenu(Editor,MenuAnchor,'edit');
end

% Show menu (Bode-specific)
h = bodemenu(Editor,MenuAnchor,'show');
set(h,'Separator','on')

% Grid/Zoom
h = addmenu(Editor,MenuAnchor,'grid');
set(h,'Separator','on','Checked',Editor.Grid)
addmenu(Editor,MenuAnchor,'zoom');

% Properties
if usejava('MWT')
    h = addmenu(Editor,MenuAnchor,'property');
    set(h,'Separator','on')
end


%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalButtonDown %%%
%%%%%%%%%%%%%%%%%%%%%%%
function LocalButtonDown(hSrc,event,Editor)
% Button down callbacks
if strcmp(Editor.EditMode,'idle')
	switch get(get(hSrc,'Parent'),'SelectionType')
	case 'normal'
		if isopen(cstprefs.sisoprops)
			cstprefs.sisoprops(Editor);
		end
	case 'open'
		cstprefs.sisoprops(Editor);
	end
end
