www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/@mbcgraph/@legend/legend.m

    function obj = legend(varargin)
%LEGEND Constructor for MBC legend object
%
%  OBJ = LEGEND(PROP, VAL, ...) constructs a new instance of an MBC legend.
%  This legend is a wrapper around the standard Matlab legend that
%  correctly sets up the legend capabilities.
%
%  LEGEND(HITEMS, PROP, VAL, ...) creates legend items for each item handle
%  in HITEMS. They are labelled with a set of default strings.
%
%  LEGEND(HITEMS, LABELS, PROP, VAL, ...) creates the legend items with the
%  given set of labels.  LABELS must be the same length as HITEMS.
%
%  LEGEND(HAXES, LABELS, PROP, VAL, ...) creates the legend items with the
%  given set of labels for all of the items that are children of HAXES.

%  Copyright 2000-2013 The MathWorks, Inc. and Ford Global Technologies, Inc.


% Check for the first argument being handle(s)
hParent = [];
hLegItems = [];
sLabels = {};
if ~isempty(varargin) && ~ischar(varargin{1})
    hLegItems = varargin{1};    
    varargin(1) = [];
    
    % Work out whether the input is a pair of axes or a set of items
    if length(hLegItems)==1 && isgraphics(hLegItems, 'axes')
        hParent = hLegItems;
        hLegItems = get(hParent, 'Children');
    else
        hParent = get(hLegItems(1), 'Parent');
    end
end

% Check for labels as second argument
nProps = length(varargin)/2;
if nProps~=floor(nProps)
    % Input includes list of labels
    sLabels = varargin{1};
    varargin(1) = [];
end

obj = mbcgraph.legend;
set(obj, varargin{:});

if isempty(obj.Parent) && isempty(hParent)
    % Grab current axes
    obj.Parent = gca;
elseif ~isempty(hParent)
    % Use parent that is derived from first argument
    obj.Parent = hParent;
end

if obj.LockVisibleToParent || ~obj.Active
    % Force update of visible setting from parent
    obj.Visible = 'off';
end

% Set up property listeners
obj.PropList = [ ...
    handle.listener(obj, obj.findprop('Visible'), 'PropertyPostSet', @i_setvisible); ...
    handle.listener(obj, obj.findprop('Location'), 'PropertyPostSet', @i_mirrorlegendprop); ...
    handle.listener(obj, obj.findprop('Interpreter'), 'PropertyPostSet', @i_mirrorlegendprop); ...
    handle.listener(obj, obj.findprop('LockVisibleToParent'), 'PropertyPostSet', @i_setupvislock); ...
    handle.listener(obj, obj.findprop('MoveMode'), 'PropertyPostSet', @i_setmovemode); ...
    handle.listener(obj, obj.findprop('Active'), 'PropertyPostSet', @i_setactive); ...
    handle.listener(obj, 'ObjectBeingDestroyed', @i_destroylegend); ...
    ];

% Initialise the legend with any input data
obj.pGenLegend(hLegItems, sLabels);

% Set up the link to the parent if required
obj.pSetupVisLink;

% Connect a handle up to the uilatch
obj.connect(xregfigurehook(obj.Parent), 'up');

function i_setvisible(src, evt)
obj = evt.AffectedObject;
obj.pSetLegendProp('Visible', obj.Visible); 

function i_mirrorlegendprop(src, evt)
evt.AffectedObject.pSetLegendProp(src.Name, evt.NewValue); 

function i_setupvislock(src, evt)
% Create a listener to synchronise the visible setting with the parent
evt.AffectedObject.pSetupVisLink;

function i_destroylegend(src, evt)
src.pDestroyObject;

function i_setmovemode(src, evt)
evt.AffectedObject.pSetupMoveMode;

function i_setactive(src, evt)
obj = evt.AffectedObject;
if evt.NewValue
    if obj.LockVisibleToParent && ~isempty(obj.Parent)
        obj.Visible = get(obj.Parent, 'Visible');
    end
else
    obj.Visible = 'off';
end