www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/+mbcgui/+widget/AxesContainer.m

    classdef AxesContainer < mbcgui.widget.BasicContainer
%AxesContainer Standard container for graph axes (does not use a uipanel)
%   mbcgui.widget.AxesContainer is a component that contains a set of axes and
%   provides the axes handle for use in graph creation.  When the AxesContainer
%   is set to be invisible both the axes and all of its children are set to
%   be invisible.
%
%   AxesContainer properties:
%      AxesHandle       -  Handle of the contained axes.  If no axes are
%                          provided at construction a new set will be
%                          created.
%      Border           -  [W S E N] insets to apply between the component
%                          position and the axes position.
%      BackgroundColor  -  Color of the component background.
%      UIContextMenu    -  Context menu handle to apply to both the panel
%                          and the contained axes.


%   Copyright 2008-2015 The MathWorks, Inc.

properties (Dependent)
    %AXESHANDLE Handle to contained axes
    %   The AxesHandle property contains the handle to the axes that are in
    %   this component.  If you set a new axes handle the existing one will
    %   be deleted.
    AxesHandle = [];
    
end

properties
    %BORDER Border to apply round the axes
    %   The Border property contains a (1x4) vector of border insets to
    %   apply around the axes.  The border is specified as [W S E N], i.e.
    %   an inset from the left, bottom, right and top.
    Border = [0 0 0 0];
    % PositionSetting 
    PositionSetting = 'inner';
end

properties(Dependent)
    %BACKGROUNDCOLOR Background color of the panel
    %   The BackgroundColor property sets the colour of the panel that
    %   contains the axes, but not the colour of the axes themselves.
    BackgroundColor
    
    %UICONTEXTMENU Context menu for the panel
    %   The UIContextMenu property contains a handle to a context menu that
    %   is set on the panel.  If a context menu is set then it is also
    %   forced onto the contained axes.  If no context menu is set on the
    %   panel then the axes may have their own and it will not be cleared.
    UIContextMenu
end

methods
    function set.AxesHandle(obj, hAx)
    if ~isgraphics(hAx, 'axes')
        error(message('mbc:mbcgui:graph:AxesPanel:InvalidPropertyValue'));
    end
    obj.ContentHandle = hAx;
        
    % Delete the current axes
    if ~isempty(obj.AxesHandle) && hAx~=obj.AxesHandle
        delete(obj.AxesHandle);
    end
    
    % Set a context menu if we have one, otherwise don't touch the axes' one.
    if ~isempty(obj.UIContextMenu)
        set(hAx, 'UIContextMenu', obj.UIContextMenu);
    end
    set(hAx, 'Visible', 'on', 'Units', 'pixels');
        
    
    % Update Parent property
    AxParent = get(hAx, 'Parent');
    if isempty(obj.ContentHandle) || AxParent~=obj.ContentHandle
        obj.Parent = AxParent;
    end
        
    % Update axes position
    obj.setPosition(obj.Position);
    end
    
    function hAx = get.AxesHandle(obj)
    hAx = obj.ContentHandle;
    end
    
    function set.Border(obj, Bord)
        if ~isnumeric(Bord) || ~isequal(size(Bord), [1 4])
            error(message('mbc:mbcgui:graph:AxesPanel:InvalidPropertyValue1'));
        end
    
        obj.Border = Bord;
        
        % Update axes position
        obj.setPosition(obj.Position);
    end
    
    function set.PositionSetting(obj,setting)
    setting = validatestring(setting,{'inner','outer'},'PositionSetting');
    obj.PositionSetting = setting;
    obj.setPosition(obj.Position);
    end
    
    function set.BackgroundColor(obj, BGCol)
    obj.forwardProperty(obj.ContentHandle,'Color', BGCol);
    end
    function BGCol = get.BackgroundColor(obj)
    BGCol = obj.retrieveProperty(obj.ContentHandle,'Color', 'none');
    end
    
    function set.UIContextMenu(obj, hUIC)
    obj.forwardProperty(obj.ContentHandle,'UIContextMenu', hUIC);
    if isempty(hUIC)
        ht = 'off';
    else
        ht = 'on';
    end
    obj.forwardProperty(obj.ContentHandle,'HitTest', ht);
    if ~isempty(obj.AxesHandle)
        set(obj.AxesHandle, 'UIContextMenu', hUIC);
    end
    
    end
    
    function T = get.UIContextMenu(obj)
    T = obj.retrieveProperty(obj.ContentHandle,'UIContextMenu', []);
    end
end


methods
    function obj = AxesContainer(varargin)
    %AxesContainer Construct a new AxesContainer object
    %   H = mbcgui.widget.AxesContainer(PROP, VAL, ...) constructs a new
    %   AxesContainer object with its properties set to the values specified
    %   by the input list of property-value pairs.
    
    obj@mbcgui.widget.BasicContainer(varargin{:});
    
    PanelProps = obj.getDepPropPairs({'UIContextMenu', 'HitTest'});
    
    if isempty(obj.AxesHandle)
        % Create a set of axes
        obj.ContentHandle = axes('Parent', obj.Parent, 'Units', 'pixels');
    else
        % Pull the axes into the container
        set(obj.ContentHandle, 'Parent', obj.Parent, 'Visible', obj.Visible);
    end
    
    % The additional properties may include a color so these must be set
    % after the call that enables automatic coloring by default
    if ~isempty(PanelProps)
        set(obj.ContentHandle, PanelProps{:});
    end
    
    
    end
end


methods(Hidden)  
    % Don't want to advertise the presence of this method but it is
    % required for backwards compatibility
    function newobj = copyobj(obj, hParent)
        newobj = copyobj(obj.AxesHandle, hParent);
    end
end


methods(Access=protected)
    function setPosition(obj, NewPos)
    
    if  ~isempty(obj.ContentHandle) ...
            && isgraphics(obj.ContentHandle)
        
        % Re-position the axes
        B = obj.Border;
        
        % Adjust for the pixels lost due to the border decoration
        InnerPos = NewPos;

        
        AxPos= [NewPos(1)+B(1)+1 NewPos(2)+B(2)+1 max(InnerPos(3)-B(1)-B(3),1) max(InnerPos(4)-B(2)-B(4),1)];
        
        switch obj.PositionSetting
            case 'inner'
                PosProp = 'Position';
            case 'outer'
                PosProp = 'OuterPosition';
        end
        
        OldPos = get(obj.AxesHandle, PosProp);
        if ~isequal(AxPos, OldPos)
            set(obj.AxesHandle, PosProp, AxPos);
        end
    end
    end
    
    function setVisible(obj, Vis)
    setVisible@mbcgui.widget.BasicContainer(obj, Vis);
    % set contents visible on all children
    set(obj.ContentHandle,'ContentsVisible',Vis);
    end    
    
    
    function setEnable(obj, En)
    % uipanel does not support the enable property, so do not pass it on.
    end 
end
end