function fig = ltibrowse(varargin);
%LTIBROWSE Open an LTI Browser for the Control System Toolbox GUIs.
%
%   H = LTIBROWSE(Type,ParentFig,ListStr,SystemList)
%   opens an LTI Browser and returns the figure handle in H. Type  
%   indicates the purpose of the LTI Browser, either 1) "import" or 
%   2) "delete". ParentFig is the Handle of the GUI from which the   
%   LTI Browser was opened. ListStr is the string to display in the
%   GUIs list box. ListStr should contain the LTI object names, size, 
%   and class. SystemList is acell array of only the names
%   of the LTI objects listed in ListStr. 
%
%   Based on the LTI Browser Type, the following behaviors are followed:
%   1) Import: The LTI Browser shows all LTI objects in the main
%      MATLAB workspace.
%   2) Delete: The LTI Browser shows all LTI objects in the caller
%      function's workspace.
%
%   When the OK button is pressed, the LTI Browser invokes the Callback
%   function with the following input list:
%      rguifcn('action',ParentFigureHandle,SelectedModels)
%   where 
%      * action = deletesys or importsys
%      * SelectedModels is a cell array containing the names of all 
%        selected models. The Handle for the Parent figure is stored in
%        ud.Parent.

%   Authors: Karen D. Gondoly, 4-22-98
%   Revised: Adam W. DiVergilio
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.15 $  $Date: 2000/06/14 19:59:10 $

error(nargchk(1,5,nargin));
Type = lower(varargin{1});

if nargin>1,
   ParentFig=varargin{2};
   ListStr = varargin{3};
   SystemList = varargin{4};
end

%---Set up data based on Type
switch Type,
case 'import',
   TitleStr = 'Select the systems to import';
   HelpTag = 'viewer_import';
case 'delete',
   TitleStr = 'Select the systems to delete';
   HelpTag = 'viewer_delete';
case {'ok','close','apply'},
   %---Place holders
otherwise,
   error('Invalid Type in call to LTIBROWSE.');
end

switch Type
case {'import','delete'},
   h0 = LocalOpenFig(ListStr,SystemList,TitleStr,ParentFig,HelpTag);
   ud = get(h0,'UserData');
   set(ud.Parent,'Pointer','watch');  % indicates modality
   if nargout
      fig = h0;
   end
   
case {'apply','ok'},
   BrowserFig = gcbf;
   Type2 = get(gcbo,'UserData');
   ud = get(BrowserFig,'UserData');
   
   Vals = get(ud.SystemList,'Value');
   set(ud.SystemList,'Value',[])
   AllNames = get(ud.SystemList,'UserData');
   if length(AllNames) & ~isempty(AllNames{1})
      SelectedNames = AllNames(Vals);
      
      if strcmp(Type2,'delete'),
         SystemStr = get(ud.SystemList,'String');
         SystemStr(Vals,:)=[];
         AllNames(Vals)=[]; 
         if isempty(AllNames),
            AllNames={''};
         end
         set(ud.SystemList,'String',SystemStr, ...
            'UserData',AllNames)
      end
      
      action = sprintf('%ssys',Type2);  % deletesys or importsys
      rguifcn(action,ud.Parent,SelectedNames)
   end
   if strcmp(Type,'ok')
      ltibrowse('close');
   end

case 'close',
   BrowserFig = gcbf;
   ud = get(gcbf,'UserData');
   close(gcbf);
   set(ud.Parent,'Pointer','arrow');

end

%--------------------------Internal Functions--------------------
%%%%%%%%%%%%%%%%%%%%
%%% LocalOpenFig %%%
%%%%%%%%%%%%%%%%%%%%
function fig = LocalOpenFig(ListStr,AllNames,TitleStr,ParentFig,HelpTag);

PointsToPixels = 72/get(0,'ScreenPixelsPerInch');
StdUnit = 'points';
UIColor = get(0,'DefaultUIControlBackground');

ud = struct('Parent',ParentFig,'SystemList',[]);

h0 = figure('Color',UIColor,...
   'IntegerHandle','off',...
   'MenuBar','none',...
   'Name',xlate('LTI Browser'),...
   'NumberTitle','off',...
   'PaperPosition',PointsToPixels*[18 180 576 432],...
   'PaperUnits','points',...
   'Resize','off',...
   'Position',[106 135 320 267],...
   'Tag','LTIBrowserFigure',...
   'Visible','off',...
   'DeleteFcn','ltibrowse(''close'');',...
   'WindowStyle','modal');

%---Position figure within ParentFig bounds
centerfig(h0,ParentFig);

h1 = uicontrol('Parent',h0, ...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'Position',PointsToPixels*[16 243 290 20], ...
   'String',TitleStr, ...
   'Style','text');
h1 = uicontrol('Parent',h0, ...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'Position',PointsToPixels*[21 222 63 20], ...
   'String','Name', ...
   'Style','text');
h1 = uicontrol('Parent',h0, ...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'Position',PointsToPixels*[128.5 222 63 20], ...
   'String','Size', ...
   'Style','text');
h1 = uicontrol('Parent',h0, ...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'Position',PointsToPixels*[236 221.5 63 20], ...
   'String','Class', ...
   'Style','text');

ud.SystemList = uicontrol('Parent',h0, ...
   'Unit',StdUnit,...
   'BackgroundColor',[1 1 1], ...
   'FontName','courier',...
   'Max',2,...
   'Position',PointsToPixels*[20 45 285 176], ...
   'String',ListStr, ...
   'Style','listbox', ...
   'Tag','SystemList', ...
   'UserData',AllNames, ...
   'Value',[]);
h1 = uicontrol('Parent',h0, ...
   'CallBack','ltibrowse(''ok'');',...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'UserData',TitleStr(end-5:end),...
   'Position',PointsToPixels*[20 10 55 25], ...
   'String','OK', ...
   'Tag','OkButton');
h1 = uicontrol('Parent',h0, ...
   'Callback','ltibrowse(''close'');',...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'Position',PointsToPixels*[97 10 55 25], ...
   'String','Cancel', ...
   'Tag','CancelButton');
h1 = uicontrol('Parent',h0, ...
   'Callback','ctrlguihelp(get(gcbo,''Tag''));',...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'Position',PointsToPixels*[174 10 55 25], ...
   'String','Help', ...
   'Tag',HelpTag);
h1 = uicontrol('Parent',h0, ...
   'CallBack','ltibrowse(''apply'');',...
   'Unit',StdUnit,...
   'BackgroundColor',UIColor, ...
   'UserData',TitleStr(end-5:end),...
   'Position',PointsToPixels*[250 10 55 25], ...
   'String','Apply', ...
   'Tag','ApplyButton');

set(h0,'UserData',ud,'Visible','on');

if nargout > 0, fig = h0; end
