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

    function varargout = dataPatch(ax,infoStr,FLAG,emergencyArea)
%DATAPATCH Display a data tooltip
%
% DATAPATCH(axesHandle,infoString,FLAG) sets button down function of axes
% to display a fake "tooltip patch" containing infoString.  If FLAG==1 is
% passed in, buttonDownFcn not set, function handle returned to sort this
% out yourself.

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


if FLAG
    varargout{1}=@i_dataPatch;
else
    set(ax,'ButtonDownFcn',{@i_dataPatch,infoStr,emergencyArea});
    varargout={};
end




%----------------------------------------------------------------------
%  SUBFUNCTION i_dataPatch
%----------------------------------------------------------------------
function i_dataPatch(ax, ~, infoStr,maxA)

hFig = ancestor(ax,'figure');
downtype = get(hFig ,'SelectionType');
switch downtype
    case {'open','alt'}
        % Double click, right mouse button...just return
        return

    case 'normal'
        % Left mouse button...show info patch
        CP = get(hFig,'CurrentPoint');
        
        parent = get(ax,'Parent');
        
        bgb = uicontrol('Style','text',...
            'Parent',parent,...
            'Visible','off',...
            'BackgroundColor','k');
        bgy = uicontrol('Style','text',...
            'Parent',parent,...
            'Visible','off',...
            'BackgroundColor',[1 1 0.8]);

        th = uicontrol('Style','text',...
            'Parent',parent,...
            'String',infoStr,...
            'BackgroundColor',[1 1 0.8],...
            'Visible','off',...
            'Tag','dataPatch',...
            'FontName','Lucida Console',...
            'HorizontalAlignment','left',...
            'Units',get(parent,'Units'),...
            'FontUnits','point',...
            'FontSize',8);

        % try to keep patch within gca
        APos = get(ax,'Position');
        W = APos(3);
        H = APos(4);
        AreaOrigin = APos(1:2);
        ext = get(th,'Extent')+3;

        if ext(3)>W || ext(4)>H
            % text will not fit on this axes alone
            W = maxA(3);
            H = maxA(4);
            AreaOrigin = maxA(1:2);
        end

        % decide where to put the text/patch
        left = CP(1);
        bottom = CP(2);
        if isgraphics(parent,'uipanel')
            ParPos = mbcgui.util.convertLocation(CP, parent);
            % correct for panel
            left = ParPos(1);
            bottom = ParPos(2);
            AreaOrigin = mbcgui.util.convertLocation(AreaOrigin,parent);
        end

        if (left+ext(3))>AreaOrigin(1)+W %% goes off axes right
            if (left-ext(3))<AreaOrigin(1) %% goes off left as well
                left = AreaOrigin(1)+W-ext(3);
            else
                left = left-ext(3);
            end
        end

        if (bottom+ext(4))>AreaOrigin(2)+H %% goes off axes top
            if (bottom-ext(4))<AreaOrigin(2) %% goes off bottom
                bottom = AreaOrigin(2)+H-ext(4);
            else
                bottom = bottom - ext(4);
            end
        end

        set(th,'Position',[left,bottom,ext(3),ext(4)]);
        set(bgy,'Position',[left-2,bottom-2,ext(3)+2,ext(4)+2]);
        set(bgb,'Position',[left-3,bottom-3,ext(3)+4,ext(4)+4]);
        th = [th,bgb,bgy];
        bm = xregGui.ButtonUpManager(hFig);
        bm.getNextEvent({@i_killPatch, th});
        set(th,'Visible','on');
end % switch downtype

%----------------------------------------------------------------------
%  SUBFUNCTION i_killPatch
%----------------------------------------------------------------------
function i_killPatch(hFig, null, th)
% Remove text and patch
delete(th);