www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/@cgsurfview/@lineplot/setCursorMode.m

    function setCursorMode(obj,newstate,cursorcallback)
%LINEPLOT/SETCURSORMODE Sets the cursor mode state and its callback
%
% setCursorMode(obj,newstate,cursorcallback)
%
% The state can be "on", "off" or "inactive".
% The "src" and "evt" parameters of the callback will be
%   the lineplot object
%   the string to be shown

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


if nargin>2
    obj.CursorCallback = cursorcallback;
end

switch newstate
case 'on'
    if isempty(obj.CursorCallback)
        error(message('mbc:cgsurfview:lineplot:InvalidArgument'));
    end
    % Make the axis limits manual, so that dragging the cross-hairs won't cause
    % the limits to change.  This will be undone the next time we call "plot".
    set(obj.hAxes,'XLimMode','manual','YLimMode','manual');
    set(obj.hLines,'ButtonDownFcn',{@i_line_click, obj});
case 'inactive'
    if ~isempty(obj.hCrossHairs) && all(ishandle(obj.hCrossHairs))
        set(obj.hCrossHairs,'Color',[0.8 0.8 0.8]);
    end
case 'off'
    delete(obj.hCrossHairs);
    obj.hCrossHairs = [];
    set(obj.hLines,'ButtonDownFcn',[]);
end

%%%%%%%%%%%%%%%%%%%%%%%
function i_line_click(hline,evt,obj)

if isempty(obj.hCrossHairs) || ~any(ishandle(obj.hCrossHairs))
    obj.hCrossHairs = [...
        line('Parent',obj.hAxes, 'XData',[],'YData',[],'LineWidth',2);...
        line('Parent',obj.hAxes, 'XData',[],'YData',[],'LineWidth',2) ];
else
    set(obj.hCrossHairs,'Color',[0 0 0])
end
set(obj.hCrossHairs,'ButtonDownFcn',...
    {@i_crosshair_click,obj,hline});
i_ShowMousePosition(hline,[],obj);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function i_ShowMousePosition(hline, ignoreevt, obj)

cp = get(obj.hAxes,'CurrentPoint');
x = cp(1,1);

xdata = get(hline,'XData');
ydata = get(hline,'YData');
% Interpolate to find y.  If it is outside the constraints boundary it will
% be NaN (which, conveniently, causes the crosshairs to disappear).  interp1
% issues a warning about "undefined" results if ydata contains NaNs.  However,
% the default interpolation method just returns NaN if the point on either
% side is NaN.
warning('off','MATLAB:interp1:NaNinY');
y = interp1(xdata,ydata,x);
warning('on','MATLAB:interp1:NaNinY');

str = sprintf('%s: %s, x = %f, y = %f',...
    obj.PlotData.getTitle,get(hline,'UserData'),x,y);

xregcallback(obj.CursorCallback,obj,str); % src, evt

xrange = get(obj.hAxes,'XLim');
yrange = get(obj.hAxes,'YLim');
dx = (xrange(2) - xrange(1) ) / 50;
dy = (yrange(2) - yrange(1) ) / 50;

set(obj.hCrossHairs(1),'XData',[x-dx x+dx],'YData',[y-dy y+dy]);
set(obj.hCrossHairs(2),'XData',[x-dx,x+dx],'YData',[y+dy y-dy]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%
function i_crosshair_click(src,evt,obj,hline)

upfcn = get(gcbf,'WindowButtonUpFcn');
motionfcn = get(gcbf,'WindowButtonMotionFcn');
set(gcbf,'WindowButtonMotionFcn',{@i_drag, obj, hline});
set(gcbf,'WindowButtonUpFcn',{@i_release,obj,upfcn,motionfcn,hline});
set(obj.hCrossHairs,'Color',[0 0 0])
i_ShowMousePosition(hline,[],obj);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function i_release(src,evt,obj,upfcn,motionfcn,hline)

set(gcbf,'WindowButtonMotionFcn',[]);
set(gcbf,'WindowButtonUpFcn',[]);
i_ShowMousePosition(hline,[],obj);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function i_drag(src,evt,obj,hline)

i_ShowMousePosition(hline,[],obj);