function imzoomin(obj,eventdata)
%IMZOOMIN Interactive zoom in on scrollpanel.

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

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

  hAx = get(obj,'parent');
  hFig = ancestor(obj, 'figure');
  
  start_point = get(hAx,'CurrentPoint');
  x = start_point(1,1);
  y = start_point(1,2);
  
  singleClick = strcmp(get(hFig, 'SelectionType'), 'normal');
  doubleClick = strcmp(get(hFig, 'SelectionType'), 'open');  
  alt_key_pressed = strcmpi(get(hFig,'CurrentModifier'),'alt');

  % initialized for function scope
  finish_point = [];
  final_rect = [];
  prev_button_up_fcn = [];

  if singleClick
    
    handleSingleClick();
    
  elseif doubleClick
    apiScrollpanel.setMagnification(apiScrollpanel.findFitMag())
  end
    
  %----------------------------------------------------------
  function handleSingleClick()
    
    prev_button_up_fcn = get(hFig,'WindowButtonUpFcn');
    
    set(hFig,'WindowButtonUpFcn',@buttonReleased);
    % changing figure units to pixels so that RBBOX returns pixel values
    % and caching the previous units
    previous_units = get(hFig,'Units');
    set(hFig,'Units','pixels');
    final_rect = rbbox;
    
    set(hFig,'Units',previous_units);
    
  end %handleSingleClick
  
  %----------------------------------------------------------  
  function buttonReleased(src,evt)
    
    if alt_key_pressed
      zoomOnAltClick();
    else

      if any(final_rect(3:4) < 1,2) 
        % if any of the rect dimension is less than one pixel
        % we will do a zoom on click.
        zoomOnClick();
      else
       % selection zoom
        
        finish_point = get(hAx,'CurrentPoint');
        
        x1 = start_point(1,1);
        x2 = finish_point(1,1);
        
        y1 = start_point(1,2);
        y2 = finish_point(1,2);
        
      mid_point = [x2+x1, y2+y1] * 0.5;
      zoomOnDragRect(mid_point,final_rect(3),final_rect(4))      
      end
      
    end % if alt_key_pressed
    
    set(hFig,'WindowButtonUpFcn',prev_button_up_fcn);
    
  end
  
  %----------------------------------------------------------
  function zoomOnDragRect(rect_center,rect_width,rect_height)

    current_mag = apiScrollpanel.getMagnification();
    
    rect_width_imcoords = rect_width / current_mag;
    rect_height_imcoords = rect_height / current_mag;
    
    mag = apiScrollpanel.findMagnification(rect_width_imcoords,...
                                           rect_height_imcoords);
    
    apiScrollpanel.setMagnificationAndCenter(mag, rect_center(1),rect_center(2));
    
  end 
  
  %----------------------------------------------------------  
  function zoomOnClick
    new_mag = findZoomMag('in',apiScrollpanel.getMagnification());
    apiScrollpanel.setMagnificationAndCenter(new_mag,x,y)   
  end

  %----------------------------------------------------------
  function zoomOnAltClick
  % If the Alt key is pressed, zoom-out is performed
    newMag = findZoomOutMagConstrained(apiScrollpanel);
    apiScrollpanel.setMagnificationAndCenter(newMag,x,y) 
  end

end %imzoomin
