function impan(obj,eventdata)
%IMPAN Interactively pan scrollpanel.

%   Copyright 1993-2004 The MathWorks, Inc.
%   $Revision: 1.1.8.1 $  $Date: 2004/08/10 01:50:17 $

  hIm = obj;
  hScrollpanel = getimscrollpanel(hIm,mfilename,'HIMAGE');
  apiScrollpanel = iptgetapi(hScrollpanel);

  hAx = get(obj,'parent');
  hFig = ancestor(obj, 'figure');

  singleClick = strcmp(get(hFig, 'SelectionType'), 'normal');
  
  if singleClick      
      % Note: we must use the figure CurrentPoint property to make sure
      % the calculation of the deltas is invarient to the image being dragged.
      start_point = get(hFig, 'CurrentPoint');
      start_x = start_point(1,1,1);
      start_y = start_point(1,2,1);

      start_pos_im = apiScrollpanel.getVisibleImageRect();
      start_pos_im(3:4) = [];
      
      screenPerImagePixels = apiScrollpanel.getMagnification();
      
      drag_motion_callback_id = iptaddcallback(hFig, ...
                                              'WindowButtonMotionFcn', ...
                                              @dragMotion);
        
      drag_up_callback_id = iptaddcallback(hFig, ...
                                          'WindowButtonUpFcn', ...
                                          @stopDrag);
  end
    
  %----------------------------
  function dragMotion(varargin)

    setptr(hFig,'closedhand')  

    % Note: we must use the figure CurrentPoint property to make sure
    % the calculation of the deltas is invarient to the image being dragged.
    new_point = get(hFig, 'CurrentPoint');
    cp = new_point(1,1:2);
    delta_x_screen = cp(1) - start_x;
    delta_y_screen = cp(2) - start_y;

    new_pos_im =  start_pos_im - ...
        [delta_x_screen -delta_y_screen]/screenPerImagePixels;
    
    new_pos_im = constrainDrag(new_pos_im);
    apiScrollpanel.setVisibleLocation(new_pos_im(1),new_pos_im(2))

  end

    
  %--------------------------
  function stopDrag(varargin)
    dragMotion();
    %is_set_position_enabled = true;
    iptremovecallback(hFig, 'WindowButtonMotionFcn', ...
                     drag_motion_callback_id);
    iptremovecallback(hFig, 'WindowButtonUpFcn', ...
                     drag_up_callback_id);
    
    setptr(hFig,'hand')    
  end

  
   %------------------------------------------
   function pos = constrainDrag(proposedPos)

     x = proposedPos(1);
     y = proposedPos(2);
     
     r = apiScrollpanel.getVisibleImageRect();
     w = r(3);
     h = r(4);
     
     imModel = getimagemodel(hIm);

     imW = getImageWidth(imModel);
     imH = getImageHeight(imModel);     
     
     x = min( imW-w, max(x,1) );
     y = min( imH-h, max(y,1) );     
     
     pos = [x y];
     
   end
  

end
  
