function h = constantline(val, varargin)
% CONSTANTLINE Create a line from a constant.
%    H = CONSTANTLINE(VAL) where VAL is a scalar creates a 
%    horizontal line at VAL. If VAL is a vector, it creates
%    a one line that looks like a set of lines, one at each value, 
%    returning just one handle.  To create vertical lines, use 
%    CHANGEDEPENDVAR method.
%    H = CONSTANTLINE(VAL,PARAM1,VALUE1,PARAM2,VALUE2) creates a line
%    with properties specified by the param value pairs.
%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.6 $  $Date: 2000/09/21 01:41:50 $

pkg = findpackage('graph2d'); % get handle to graph2d package
hgpkg = findpackage('hg');    % get handle to hg package
if (nargout == 0)
    % Create a new subclass of the HG line object
    c = schema.class(pkg, 'constantline', hgpkg.findclass('line'));
    % Add hidden property 'Constant'
    % Must be a scalar constant value.
    p = schema.prop(c, 'Value', 'NReals');  % could be a vector  
    p.AccessFlags.PublicSet = 'off';            % No listener since not settable
    % Add hidden property DependVar to name dependent variable
    % if 'y' then ydata = fun(xdata)  (default horizontal)
    % if 'x' then xdata = fun(ydata)  (vertical)
    p = schema.prop(c, 'DependVar', 'String');
    p.AccessFlags.PublicSet = 'off';            
    p.AccessFlags.Init = 'on';
    p.FactoryValue = 'y';
    % Listeners for Public Properties:
    % Add a private property called 'listenerAxes' to hold onto the
    % axes listener.  Listener will automatically get destroyed
    % when the object is destroyed.
    p = schema.prop(c, 'listenerAxes', 'handle');
    p.AccessFlags.PublicGet = 'off';
    p.AccessFlags.PublicSet = 'off';
    p.AccessFlags.Serialize = 'off';

    % Add a private property called 'listenerValue' to hold onto the
    % Value listener.  Listener will automatically get destroyed
    % when the object is destroyed.
    p = schema.prop(c, 'listenerValue', 'handle');
    p.AccessFlags.PublicGet = 'off';
    p.AccessFlags.PublicSet = 'off';
    p.AccessFlags.Serialize = 'off';

elseif (nargout == 1) 
    if isequal(val, 'parent')
        % This condition should only happen when we are being
	% called by the FIG-file loader which always passes
	% our constructor ('parent', parent) arguments.
        val = 0;
	curraxes = varargin{1};
	varargin = {};
    else
        parent = find(strncmpi('parent',varargin,6));
	if ~isempty(parent)
            curraxes = varargin{parent+1};
	else
            curraxes = gca;
	end
    end
    
    % Construct the line
    if ~isempty(varargin)
        h = graph2d.constantline('xdata',get(curraxes,'xlim'),'ydata',get(curraxes,'ylim'),varargin{:});  % calls built-in constructor
    else
        h = graph2d.constantline('xdata',get(curraxes,'xlim'),'ydata',get(curraxes,'ylim'));
    end
    % Initialize
    h.value = val;       % Constant
    % Install a listener for the axes limits
    axesC = hgpkg.findclass('axes');
    xlimP = axesC.findprop('xlim');
    ylimP = axesC.findprop('ylim');
    h.listenerAxes = handle.listener(get(h,'parent'), [xlimP ylimP], 'PropertyPostSet', {@localUpdateLine, h});
    
    % Install a listener for the constantline Value and DependVar property
    h.listenerValue = handle.listener(h, [h.findprop('Value') h.findprop('DependVar')], 'PropertyPostSet', {@localUpdateLine, h});
    
    % Draw the line
    update(h);
end
%-----------------------------------------------------------
function localUpdateLine(src, eventData, hCline)
% LOCALUPDATELINE Callback to update constantline when axes limits change.
update(hCline);


