function hout = imoverview(varargin)
%IMOVERVIEW Overview tool for image displayed in scroll panel.
%   IMOVERVIEW(HIMAGE) creates an Overview tool associated with the image
%   specified by the handle HIMAGE, called the target image. HIMAGE must be
%   contained in a scroll panel created by IMSCROLLPANEL.
% 
%   The Overview tool is a navigation aid for images displayed in a scroll
%   panel. IMOVERVIEW creates the tool in a separate figure window that displays
%   the target image in its entirety, scaled to fit. Over this scaled version of
%   image, the tool draws a rectangle, called the detail rectangle, that shows
%   the portion of the target image that is currently visible in the scroll
%   panel.  To view portions of the image that are not currently visible in the
%   scroll panel, move the detail rectangle in the Overview tool.
%    
%   FIG = IMOVERVIEW(...) returns a handle to the Overview tool figure.
% 
%   Note 
%   ----
%   To create an Overview tool that can be embedded in an existing figure or
%   uipanel object, use IMOVERVIEWPANEL.
% 
%   Example
%   -------
%   
%       hFig = figure('Toolbar','none',...
%                     'Menubar','none');
%       hIm = imshow('tape.png');
%       hSP = imscrollpanel(hFig,hIm);
%       api = iptgetapi(hSP);
%       api.setMagnification(2) % 2X = 200%
%       imoverview(hIm)
%  
%   See also IMOVERVIEWPANEL, IMSCROLLPANEL, IMTOOL.

%   Copyright 1993-2004 The MathWorks, Inc.
%   $Revision: 1.1.8.2 $  $Date: 2004/12/18 07:36:06 $

   iptchecknargin(1, 1, nargin, mfilename);
   himage = varargin{1};
   
   iptcheckhandle(himage,{'image'},mfilename,'HIMAGE',1)
   hScrollpanel = getimscrollpanel(himage,mfilename,'HIMAGE');
   apiScrollpanel = iptgetapi(hScrollpanel);
   
   hScrollpanelFig = ancestor(hScrollpanel,'figure');
   hScrollpanelIm  = himage;
   
   hOverviewFig = figure('Menubar','none',...
                         'IntegerHandle','off',...
                         'HandleVisibility','Callback',...
                         'NumberTitle','off',...
                         'Name','Overview',...
                         'Colormap',get(hScrollpanelFig,'Colormap'),...
                         'Position',[100 100 200 200],...
                         'Visible','off');

   % use same renderer as parent
   set(hOverviewFig,'Renderer',get(hScrollpanelFig,'Renderer'));
   
   hOverviewPanel = imoverviewpanel(hOverviewFig,hScrollpanelIm);
   
   % Initialize zoom buttons for function scope
   zoomInButton = [];
   zoomOutButton = [];
   
   toolbarOld = findall(hOverviewFig,'type','uitoolbar');    
   delete(toolbarOld)
   createToolbar
   createMenubar
   
   linkFig = linkprop([hScrollpanelFig hOverviewFig],'Colormap');
   
   setappdata(hOverviewFig, 'OverviewListeners', linkFig);
   
   % Position the overview figure to the upper left and make visible.
   iptwindowalign(hScrollpanelFig, 'left', hOverviewFig, 'right');
   iptwindowalign(hScrollpanelFig, 'top', hOverviewFig, 'top');
   set(hOverviewFig,'Visible','on')

   % Set up wiring so zoom buttons enable/disable according to
   % magnification of main image.
   updateZoomButtons(apiScrollpanel.getMagnification())
   magCallbackID = apiScrollpanel.addNewMagnificationCallback(@updateZoomButtons);
   
   set(hOverviewFig, ...
       'DeleteFcn', ...
       @(src, varargin) apiScrollpanel.removeNewMagnificationCallback(magCallbackID));
   
   %--------------------------------
   function updateZoomButtons(mag)
 
     if ishandle(hOverviewFig)

         fitMag = apiScrollpanel.findFitMag();
         if mag<=fitMag
             set(zoomOutButton,'Enable','off')
         else
             set(zoomOutButton,'Enable','on')
         end
     
         if mag>=1024 % arbritrary big choice, 1024 screen pixels for one
                      % image pixel, same as in imtool.m
             set(zoomInButton,'Enable','off')
         else
             set(zoomInButton,'Enable','on')
         end
     end
     
   end
   
       
   reactToImageChangesInFig(himage,@deleteFcn);

   %--------------------------------
   function deleteFcn(obj,eventData)
 
     if ishandle(hOverviewFig)
         delete(hOverviewFig);
     end
   end
   
   
   if (nargout==1)
       hout = hOverviewFig;
   end

  %--------------------
  function createToolbar
  
    toolbar =  uitoolbar(hOverviewFig);
    
    [iconRoot,iconRootMATLAB] = ipticondir;
     
    zoomInIcon = makeToolbarIconFromPNG(fullfile(iconRoot,...
                                                 'overview_zoom_in.png'));
    zoomInButton = createToolbarPushItem(toolbar,zoomInIcon,...
                                         {@zoomIn},...
                                         'Zoom in');
  
    zoomOutIcon = makeToolbarIconFromPNG(fullfile(iconRoot,...
                                                  'overview_zoom_out.png'));
    zoomOutButton = createToolbarPushItem(toolbar,zoomOutIcon,...
                                          {@zoomOut},...
                                          'Zoom out');
    
    if ~isdeployed
      helpIcon = makeToolbarIconFromGIF(fullfile(iconRootMATLAB, 'helpicon.gif'));
      
      createToolbarPushItem(toolbar,helpIcon,...
                            @(varargin) ipthelp('imtool_overview_help','Overview'),...
                            'Help');
    end
    
  end 
  
  %---------------------
  function createMenubar
    
    filemenu = uimenu(hOverviewFig, 'Label','&File','Tag','file menu');
    editmenu = uimenu(hOverviewFig, 'Label','&Edit','Tag','edit menu');
    
    if isJavaFigure
      uimenu(hOverviewFig, 'Label', '&Window','tag','winmenu', ...
             'Callback', winmenu('callback'));
    end
    
    % File menu
    uimenu(filemenu,'Label','&Print To Figure','Tag','print menu',...
           'Callback',@(varargin) printImageToFigure(hOverviewFig));
    
    uimenu(filemenu,'Label','&Close','Accelerator','W',...
           'Callback',@(varargin) close(hOverviewFig));
    
    % Edit menu    
    uimenu(editmenu, 'Label', '&Copy Position', ...
           'Callback', @(varargin) clipboard('copy', apiScrollpanel.getVisibleImageRect()), ...
           'Tag', 'copyItem');

      
    % Help menu
    if ~isdeployed
      helpmenu = uimenu(hOverviewFig, 'Label','&Help','Tag','help menu');
      uimenu(helpmenu,'Label','&Overview Help','Tag',...
             'imoverview help menu','Callback',...
             @(varargin) ipthelp('imtool_overview_help','Overview'));
      
      iptstandardhelp(helpmenu);
    end
  end

  %-------------------
  function copyToClipboard(src,evt)
  
    clipboard('copy',[apiScrollpanel.getVisibleImageRect()]);
    
  end
  
  %-------------------
  function zoomIn(varargin)
    
    new_mag = findZoomMag('in',apiScrollpanel.getMagnification());
    apiScrollpanel.setMagnification(new_mag)
    
  end
  
  %-------------------
  function zoomOut(varargin)

    newMag = findZoomOutMagConstrained(apiScrollpanel);
    apiScrollpanel.setMagnification(newMag)
    
  end  

end

%-------------------------------------------------------------------
function item = createToolbarPushItem(toolbar,icon,callback,tooltip)

  item = uipushtool(toolbar,...
                    'Cdata',icon,...
                    'TooltipString',tooltip,...
                    'Tag',lower(tooltip),...
                    'ClickedCallback',callback);
  
end
