function A = viewgui(plottype,Parent,SystemData);
%VIEWGUI opens an LTI Viewer object

%   Authors: Karen Gondoly 1-27-98
%   Revised: Adam DiVergilio, 11-99
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.49 $  $Date: 2000/08/24 17:05:18 $

ni = nargin;
if ni==0
   plottype = '';
   Parent = [];
   SystemData = struct('Names',[],'Systems',[],'PlotStyles',[]);
end

%---For the LTI Viewer, only use colors with a valid color plot symbol
ColorTable8 = rguicolors(8);

%---Properties
A = struct(...
   'ActivePlotTypes',     {plottype(:)},...
   'ArrayPlotVariable',   'none',...
   'BackgroundAxes',      zeros(length(plottype),1),...
   'ChannelPlotVariable', 'none',...
   'ColorOrder',          {ColorTable8(1:7,:)},... % Leave white out
   'ConfigurationWindow', [],...
   'FigureMenu',          [],...
   'Handle',              [],... 
   'InitialCondition',    [],...
   'InputPlotVariable',   'none',...
   'InputSignal',         [],...
   'LineStyleOrder',      {{'-';'--';'-.';':'}},...
   'LineStylePreferences','off',...
   'MarkerOrder',         {{'none';'x';'o';'+';'*';'s';'d';'p'}},...
   'OutputPlotVariable',  'none',...
   'PlotStyles',          {SystemData.PlotStyles},...
   'SingularValueType',   0,... 
   'StatusText',          [],...
   'StatusFrame',         [],...
   'Systems',             {SystemData.Systems(:)},...
   'SystemNames',         {SystemData.Names(:)},...
   'SystemPlotVariable',  'color',...
   'UIContextMenu',       [],...
   'Preferences',         []);

if nargin==0
   A = class(A,'viewgui');
   return
end

%---Figure
f = figure(...
   'Visible','off',...
   'Name','LTI Viewer',...
   'Menu','none',...
   'HandleVisibility','callback',...
   'IntegerHandle','off',...
   'NumberTitle','off',...
   'WindowButtonMotionFcn',@localHover,...
   'KeyPressFcn',@localKeyPress,...
   'ResizeFcn',@localResize,...
   'DeleteFcn',@localClose,...
   'Tag','ResponseGUI');
A.Handle = f;

%---Preferences
A.Preferences = cstprefs.viewprefs(f);

%---File Menu
 FigMenu.FileMenu.Main = uimenu(A.Handle,'Label','&File');
 FigMenu.FileMenu.NewViewer = uimenu(FigMenu.FileMenu.Main,...
    'Label','&New Viewer','Accelerator','N','Callback','ltiview;');
 FigMenu.FileMenu.Import = uimenu(FigMenu.FileMenu.Main,'Separator','on',...
    'Label','&Import...','Callback',@localImport);
 FigMenu.FileMenu.Export = uimenu(FigMenu.FileMenu.Main,...
    'Label','&Export...','Callback',@localExport);
 if usejava('MWT')
    FigMenu.FileMenu.ToolboxPreferences = uimenu(FigMenu.FileMenu.Main,'Separator','on',...
       'Label','&Toolbox Preferences...','Callback','ctrlpref');
 end
 FigMenu.FileMenu.PageSetup = uimenu(FigMenu.FileMenu.Main,'Separator','on',...
    'Label','Pa&ge Setup...','Callback','pagesetupdlg(gcbf)');
 FigMenu.FileMenu.Print = uimenu(FigMenu.FileMenu.Main,...
    'Label','&Print...','Accelerator','P','Callback',@localPrint);
 FigMenu.FileMenu.PrintToFigure = uimenu(FigMenu.FileMenu.Main,...
    'Label','Print to &Figure','Callback',@localPrintToFigure);
 FigMenu.FileMenu.Close = uimenu(FigMenu.FileMenu.Main,'Separator','on',...
    'Label','&Close','Accelerator','W','CallBack',@localClose);

%---Edit Menu
 FigMenu.EditMenu.Main = uimenu(A.Handle,'Label','&Edit');
 FigMenu.EditMenu.PlotConfigurations = uimenu(FigMenu.EditMenu.Main,...
    'Label','Plot &Configurations...','Callback',@localPlotConfig);
 FigMenu.EditMenu.Systems = uimenu(FigMenu.EditMenu.Main,'Label','&Systems');
 FigMenu.EditMenu.Refresh = uimenu(FigMenu.EditMenu.Systems,...
    'Label','&Refresh','Callback',@localRefresh);
 FigMenu.EditMenu.Delete = uimenu(FigMenu.EditMenu.Systems,...
    'Label','&Delete...','Callback',@localDelete);
 FigMenu.EditMenu.LineStyles = uimenu(FigMenu.EditMenu.Main,'Separator','on',...
    'Label','&Line Styles...','Callback',@localLineStyle,'UserData',0);
 FigMenu.EditMenu.ViewerPreferences = uimenu(FigMenu.EditMenu.Main,...
    'Label','Viewer &Preferences...','Callback',@localViewPref,'UserData',0);

%---Simulink Menu
 FigMenu.SimulinkMenu.Main = uimenu(A.Handle,'Label','&Simulink','Visible','off');
 FigMenu.SimulinkMenu.LinearizeMenu = uimenu(FigMenu.SimulinkMenu.Main,'Label','Get Linearized &Model');
 FigMenu.SimulinkMenu.ICMenu = uimenu(FigMenu.SimulinkMenu.Main,'Label','Set &Operating Point...','Separator','on');
 FigMenu.SimulinkMenu.DeleteMenu = uimenu(FigMenu.SimulinkMenu.Main,'Label','&Remove Input/Output Points'); 

%---Window Menu
 FigMenu.WinMenu.Main = uimenu(A.Handle,'Tag','winmenu',...
    'Label','&Window','Callback',winmenu('callback'));
 %---Initialize the submenu
 winmenu(A.Handle);

%---Help Menu
 FigMenu.HelpMenu.Main = uimenu(A.Handle,'Label','&Help');
 FigMenu.HelpMenu.ViewerHelp = uimenu(FigMenu.HelpMenu.Main,...
    'Label',sprintf('LTI Viewer &Help'),'Callback','ctrlguihelp(''viewermainhelp'');');
 FigMenu.HelpMenu.ToolboxHelp = uimenu(FigMenu.HelpMenu.Main,...
    'Label',sprintf('Control System &Toolbox Help'),'Callback','doc(''control/'');');
 FigMenu.HelpMenu.IOPointsHelp = uimenu(FigMenu.HelpMenu.Main,'Separator','on',...
    'Label','Specifying Input/Output &Points','Visible','off','Callback','ctrlguihelp(''viewer_iopoints'');');
 FigMenu.HelpMenu.OperatingPointHelp = uimenu(FigMenu.HelpMenu.Main,...
    'Label',sprintf('Setting &Operating Point'),'Visible','off','Callback','ctrlguihelp(''viewer_operatingpoint'');');
 FigMenu.HelpMenu.ImportExportHelp = uimenu(FigMenu.HelpMenu.Main,'Separator','on',...
    'Label',sprintf('&Importing/Exporting Models'),'Callback','ctrlguihelp(''viewer_importexport'');');
 FigMenu.HelpMenu.RespTypeHelp = uimenu(FigMenu.HelpMenu.Main,...
    'Label',sprintf('Selecting &Response Types'),'Callback','ctrlguihelp(''viewer_responsetypes'');');
 FigMenu.HelpMenu.MIMOHelp = uimenu(FigMenu.HelpMenu.Main,...
    'Label',sprintf('Analyzing &MIMO Models'),'Callback','ctrlguihelp(''viewer_mimomodels'');');
 FigMenu.HelpMenu.PropPrefHelp = uimenu(FigMenu.HelpMenu.Main,...
       'Label',sprintf('C&ustomizing the LTI Viewer'),'Callback','ctrlguihelp(''viewer_customizing'');');
 FigMenu.HelpMenu.DemosHelp = uimenu(FigMenu.HelpMenu.Main,'Separator','on',...
    'Label',sprintf('&Demos'),'Callback','demo toolbox control');
 FigMenu.HelpMenu.AboutHelp = uimenu(FigMenu.HelpMenu.Main,'Separator','on',...
    'Label',sprintf('&About the Control System Toolbox'),'Callback','aboutcst');

%---Save menu handle structure in viewgui object
A.FigureMenu = FigMenu;

%---Status Bar
set(A.Handle,'Units','Characters');
FigPos = get(A.Handle,'Position');
A.StatusFrame = uicontrol('Parent',A.Handle,'Style','frame',...
   'Units','Character','Position',[2.5 0.25 FigPos(3)-5 2.4],'Units','pixels');
A.StatusText = uicontrol('Parent',A.Handle,'Style','text',...
   'HorizontalAlignment','left',...
   'TooltipString','Status Bar',...
   'Tag','StatusText',...
   'UserData','',...
   'Units','Character','Position',[3.5 0.45 FigPos(3)-7 2.0],'Units','pixels');
set(A.Handle,'Units','pixels');
if isequal([0 0 0],get(A.Handle,'Color'))
   set([A.StatusFrame A.StatusText],'BackgroundColor',[0 0 0],'ForegroundColor',[1 1 1]);
end

%---Create viewgui object
A = class(A,'viewgui');

%---Position axes optimally within figure
A = plotpos(A);

%---Show figure and store viewgui object in UserData
set(A.Handle,'Visible','on','UserData',A);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Pass these callbacks to class methods %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function localImport(eventSrc,eventData)
 import(get(gcbf,'UserData'));

function localExport(eventSrc,eventData)
 export(get(gcbf,'UserData'));

function localPrint(eventSrc,eventData)
 print(get(gcbf,'UserData'),'printer');

function localPrintToFigure(eventSrc,eventData)
 print(get(gcbf,'UserData'),'figure');

function localClose(eventSrc,eventData)
 close(get(gcbf,'UserData'));

function localRefresh(eventSrc,eventData)
 refresh(get(gcbf,'UserData'));

function localDelete(eventSrc,eventData)
 delete(get(gcbf,'UserData'));

function localPlotConfig(eventSrc,eventData)
 ViewerObj = get(gcbf,'UserData');
 set(ViewerObj,'ConfigurationWindow','on');

function localLineStyle(eventSrc,eventData)
 ViewerObj = get(gcbf,'UserData');
 set(ViewerObj,'LineStylePreferences','on');

function localViewPref(eventSrc,eventData)
 ViewerObj = get(gcbf,'UserData');
 edit(ViewerObj.Preferences);

function localResize(eventSrc,eventData)
 ViewerObj = get(eventSrc,'UserData');
 if isa(ViewerObj,'viewgui')
    resize(ViewerObj);
 end

function localHover(eventSrc,eventData)
 hover(get(gcbf,'UserData'));

function localKeyPress(eventSrc,eventData)
 keypress(get(gcbf,'UserData'));

function localHelp(eventSrc,eventData)
 ;
