%DIPZOOM   Interactive image zooming
%   DIPZOOM ON turns on interactive zooming of the current image.
%   DIPZOOM OFF turns it off.
%   DIPZOOM by itself toggles the state.
%
%   When DIPZOOM is turned on, clicking on the image with the left mouse
%   button zooms in, and with the right mouse button zooms out. A double-
%   click will set the zoom factor to 100%. You can also drag a rectangle
%   around the region of interest, which will be zoomed such that it fits
%   the figure window. DIPZOOM never changes the size of the window itself.
%
%   DIPZOOM only works on figure windows created through DIPSHOW.
%
%   Also: DIPZOOM(H,'ON'), DIPZOOM(H,'OFF'), etc. to specify a window handle.
%
%   See also DIPSHOW, DIPTEST, DIPORIEN, DIPTRUESIZE, DIPSTEP, DIPLINK.

% (C) Copyright 1999-2009               Pattern Recognition Group
%     All rights reserved               Faculty of Applied Physics
%                                       Delft University of Technology
%                                       Lorentzweg 1
%                                       2628 CJ Delft
%                                       The Netherlands
%
% Cris Luengo, July-August 2000
% 2 September 2000: Enabled DIPZOOM for color images.
% 9 November 2000: Improved title bar display.
%                  We now display the zoom factor in the titlebar.
% 16-26 August 2001: Changed DIPSHOW. This function changes accordingly.
%                    Zoom callback functions are in DIPSHOW now.
%                    Changed how DIPZOOM reacts to the mouse a bit.
%                    Added 1D support.
% 19 May 2009: Changed help to match changes in DIPSHOW.

function menu_out = dipzoom(arg1,arg2)

% Avoid being in menu
if nargin == 1 & ischar(arg1) & strcmp(arg1,'DIP_GetParamList')
   menu_out = struct('menu','none');
   return
end

if nargin == 0
   fig = get(0,'CurrentFigure');
   if isempty(fig)
      error('No figure window open to do operation on.')
   end
   action = 'toggle';
elseif nargin == 1
   if ischar(arg1)
      fig = get(0,'CurrentFigure');
      if isempty(fig)
         error('No figure window open to do operation on.')
      end
      action = lower(arg1);
   else
      try
         fig = getfigh(arg1);
      catch
         error('Argument must be a valid figure handle.')
      end
      action = 'toggle';
   end
else
   try
      fig = getfigh(arg1);
   catch
      error('Argument must be a valid figure handle.')
   end
   action = lower(arg2);
end
if strncmp(get(fig,'Tag'),'DIP_Image',9)
   udata = get(fig,'UserData');
   switch (action)
   case 'toggle'
      if strcmp(udata.state,'dipzoom')
         dipfig_clear_state(fig,udata);
      else
         makeDIPzoomObj(fig,udata);
      end
   case 'on'
      if ~strcmp(udata.state,'dipzoom')
         makeDIPzoomObj(fig,udata);
      end
   case 'off'
      if strcmp(udata.state,'dipzoom')
         dipfig_clear_state(fig,udata);
      end
   end
end


%
% Enable to dipzoom tool
%
function makeDIPzoomObj(fig,udata)
udata.state = 'dipzoom';
set(fig,'WindowButtonDownFcn','dipshow DIP_callback dipzoomWindowButtonDownFcn',...
        'WindowButtonUpFcn','',...
        'WindowButtonMotionFcn','',...
        'ButtonDownFcn','',...
        'UserData',[]);   % Solve MATLAB bug!
set(fig,'UserData',udata);
dipfig_setpointer(fig,'loupe');
dipfig_set_action_check(fig,udata.state);
