function opamp(varargin)
%OPAMP  Draw an op amp

%   Author(s): A. DiVergilio
%   Copyright 1986-2002 The MathWorks, Inc.
%   $Revision: 1.4 $  $Date: 2002/04/10 06:41:57 $

%---NOTE:  Ideal W/H aspect ratio is 3

%---Default properties
p = struct(...
   'Parent',[],...
   'Position',[1 1 3 1],...
   'LineWidth',2,...
   'EdgeColor','k',...
   'FaceColor','none',...
   'FontSize',12,...
   'FontWeight','normal',...
   'Name','',...
   'ShowTerminals',0,...
   'Label',{{'','',''}},...
   'LabelPosition',{{'left','left','right'}},...
   'Info1','',...
   'Info2','',...
   'MarkerSize',6,...
   'MarkerFaceColor','w',...
   'Tag','',...
   'Clipping','off');
plist = fieldnames(p);

%---Merge user-specified properties
for i=1:2:nargin-1
   Property = pnmatch(varargin{i},plist);
   Value = varargin{i+1};
   p.(Property) = Value;
end
if isempty(p.Parent), p.Parent = gca; end

hu = p.Position(3)/6;
vu = p.Position(4)/2;
px = p.Position(1)+hu*[1 1 5];
py = p.Position(2)+vu*[-1 3 1];
lx = p.Position(1)+hu*[0 1 NaN 0 1 NaN 5 6 NaN 1.3 1.8 NaN 1.3 1.8 NaN 1.55 1.55];
ly = p.Position(2)+vu*[0 0 NaN 2 2 NaN 1 1 NaN 0.0 0.0 NaN 2.0 2.0 NaN 1.75 2.25];
tx = p.Position(1)+hu*[2.5];
ty = p.Position(2)+vu*[1];
mx = p.Position(1)+hu*[0 0 6];
my = p.Position(2)+vu*[2 0 1];

%---Op amp symbol
hp = patch(...
   'Parent',p.Parent,...
   'LineWidth',p.LineWidth,...
   'EdgeColor',p.EdgeColor,...
   'FaceColor',p.FaceColor,...
   'XData',px,...
   'YData',py,...
   'Tag',p.Tag,...
   'Clipping',p.Clipping);
hl = line(...
   'Parent',p.Parent,...
   'LineWidth',p.LineWidth,...
   'Color',p.EdgeColor,...
   'XData',lx,...
   'YData',ly,...
   'Tag',p.Tag,...
   'Clipping',p.Clipping);
ht = text(...
   'Parent',p.Parent,...
   'String',p.Name,...
   'Position',[tx ty],...
   'Color',p.EdgeColor,...
   'FontSize',p.FontSize,...
   'FontWeight',p.FontWeight,...
   'HorizontalAlignment','center',...
   'VerticalAlignment','middle',...
   'Tag',p.Tag,...
   'Clipping',p.Clipping);

%---Ports
if p.ShowTerminals
   line(...
      'Parent',p.Parent,...
      'LineStyle','none',...
      'LineWidth',p.LineWidth,...
      'Marker','o',...
      'MarkerSize',p.MarkerSize,...
      'MarkerFaceColor',p.MarkerFaceColor,...
      'Color',p.EdgeColor,...
      'XData',mx,...
      'YData',my,...
      'Tag',p.Tag,...
      'Clipping',p.Clipping);
end

%---Port labels
for n=1:3
   if ~isempty(p.Label{n})
      switch p.LabelPosition{n}
      case 'left'
         hor = 'right';
         ver = 'middle';
         dy = 0;
      case 'right'
         hor = 'left';
         ver = 'middle';
         dy = 0;
      case 'top'
         hor = 'center';
         ver = 'bottom';
         dy = 0.5*vu;
      case 'bottom'
         hor = 'center';
         ver = 'top';
         dy = -0.5*vu;
      end
      text(...
         'Parent',p.Parent,...
         'String',['  ' p.Label{n} '  '],...
         'Position',[mx(n) my(n)+dy],...
         'Color',p.EdgeColor,...
         'FontSize',p.FontSize,...
         'FontWeight',p.FontWeight,...
         'HorizontalAlignment',hor,...
         'VerticalAlignment',ver,...
         'Tag',p.Tag,...
         'Clipping',p.Clipping);
   end
end

%---Upper info text
if ~isempty(p.Info1)
   text(...
      'Parent',p.Parent,...
      'String',p.Info1,...
      'Position',[p.Position(1)+hu*3 p.Position(2)+vu*2.5],...
      'Color',p.EdgeColor,...
      'FontSize',p.FontSize,...
      'FontWeight',p.FontWeight,...
      'HorizontalAlignment','left',...
      'VerticalAlignment','bottom',...
      'Tag',p.Tag,...
      'Clipping',p.Clipping);
end

%---Lower info text
if ~isempty(p.Info2)
   text(...
      'Parent',p.Parent,...
      'String',p.Info2,...
      'Position',[p.Position(1)+hu*3 p.Position(2)-vu*0.5],...
      'Color',p.EdgeColor,...
      'FontSize',p.FontSize,...
      'FontWeight',p.FontWeight,...
      'HorizontalAlignment','left',...
      'VerticalAlignment','top',...
      'Tag',p.Tag,...
      'Clipping',p.Clipping);
end
