function varargout = hoverfig(fig,action);
%HOVERFIG  Use HOVERFIG as the figure's WindowButtonMotionFcn to
%          dynamically modify pointer shape and activate data
%          markers where appropriate.  An optional output argument
%          may be specified to obtain a status string related to 
%          the hover object.

%   Author: A. DiVergilio
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.4 $  $Date: 2000/08/17 22:30:21 $

persistent LM;

if nargin<1
   fig = gcbf;

elseif nargin>1
   set(LM(2),'ButtonDownFcn','');
   LM = [];
   WBMF = get(fig,'WindowButtonMotionFcn');
   if ~isempty(WBMF)
      feval(WBMF);
   end
   return;
end

StatusStr = '';

hoverobj = hittest(fig);

if ~isempty(LM)
   if hoverobj==LM(1) | hoverobj==LM(2)
      StatusStr = sprintf('Left-click to make this data marker persistent.');
      if nargout, varargout{1}=StatusStr; end
      return;
   else
      if ishandle(LM(2))
         delete(LM(2));
      end
      LM = [];
      hoverobj = hittest(fig);
   end
end

objtype = get(hoverobj,'type');
objtag  = get(hoverobj,'tag');

switch objtype
case {'figure';'uicontrol'}
   setptr(fig,'arrow');
   if nargout, varargout{1}=StatusStr; end
   return;
case {'axes'}
   hoveraxes = hoverobj;
otherwise
   hoveraxes = get(hoverobj,'Parent');
end

%---If hoveraxes is NOT an LTIdisplayAxes, setptr('arrow') and return
axestag = get(hoveraxes,'Tag');
if ~strcmpi(axestag,'LTIdisplayAxes')
   setptr(fig,'arrow');
   if nargout, varargout{1}=StatusStr; end
   return;
end

%---If response object of hoveraxes is in zoom mode, setptr('crosshair') and return
uic = get(hoveraxes,'UIContextMenu');
robj = get(uic,'UserData');
if any(strcmpi(get(robj,'Zoom'),{'on','xon','yon'}))
   pa = get(robj,'PlotAxes');
   cp = get(pa(:),'currentpoint');
   if iscell(cp)
      cp = cat(1,cp{:});
   end
   %---Only show crosshair if we are within axes bounds
   if any(cp(2:2:end,3)~=0)
      setptr(fig,'crosshair');
      StatusStr = sprintf('Left-click and drag to select region to magnify.');
   else
      setptr(fig,'arrow');
   end
   if nargout, varargout{1}=StatusStr; end
   return;
end

switch objtag
case 'LinescanMarker'
   %---Hack for non-moveable markers
   if isempty(get(hoverobj,'ButtonDownFcn'))
      setptr(fig,'arrow');
   else
      setptr(fig,'hand');
      StatusStr = sprintf('Left-click and drag to move data marker to new location.');
   end
   if nargout, varargout{1}=StatusStr; end
   return;
case 'LinescanText'
   StatusStr = sprintf('Right-click for data marker options.');
case 'LTIresponseLines'
   %---Get Response Data
   ContextMenu = get(hoveraxes,'UIContextMenu');
   RespObj = get(ContextMenu,'UserData');
   AllRespProps = get(RespObj,'Undocumented');
   ResponseType = AllRespProps.ResponseType;
   ResponseHandles = AllRespProps.ResponseHandles;
   SystemNames = AllRespProps.SystemNames;
   udLine = get(hoverobj,'UserData');
   SystemIndex = find(strcmp(udLine.System,SystemNames));
   SystemIndex = SystemIndex(1);
   Handles = ResponseHandles{SystemIndex}{udLine.Output,udLine.Input};
   IOSize = size(AllRespProps.SelectedChannels);
   ArraySize = size(Handles);
   sysstr = rguihelper('labelsystem',udLine.System,...
      udLine.Input,udLine.Output,udLine.Array,IOSize,ArraySize,ResponseType);
   StatusStr = sprintf('%s.  Left-click to add a data marker to this line.',sysstr);
end
setptr(fig,'arrow');

hoverbdf = get(hoverobj,'ButtonDownFcn');
if isstr(hoverbdf) & ~isempty(findstr('plotoptbuttondown',hoverbdf))
   lobj = hoverobj;
   M = linescan(lobj,'x',0,get(lobj,'UserData'),get(lobj,'XData'),get(lobj,'YData'));
   set(M,'ButtonDownFcn',{@localStick,fig});
   LM = [hoverobj;M];
   StatusStr = sprintf('Left-click to make this data marker persistent.');
elseif strcmpi(objtype,'line') & length(get(hoverobj,'XData'))==1
   M = feval(hoverbdf,hoverobj,1);
   set(M,'ButtonDownFcn',{@localStick,fig});
   LM = [hoverobj;M];
   StatusStr = sprintf('Left-click to make this data marker persistent.');
end

if nargout, varargout{1}=StatusStr; end


%%%%%%%%%%%%%%
% localStick %
%%%%%%%%%%%%%%
function localStick(eventSrc,eventData,fig)

hoverfig(fig,'stick');
