function moveimage(h,event)
%MOVEIMAGE  Allow click-and-drag movement through an image
%
% example usage:
%  h = image(data);
%  moveimage(h)
%
%  Or, even:
%
%  x = 0:0.1:15;
%  h = plot(x,sin(x));
%  moveimage(h);
%
% Then use the toolbar icon to select the new click-and-drag tool!
%
% At some point this will be renamed to "dragplot" I think.
%
% $Id: moveimage.m,v 1.4 2002/07/15 11:11:24 tobin Exp tobin $

% Copyright (c) 2002 by Tobin Fricke <tobin@splorg.org>

MyName = 'moveimage';
Signature = [MyName '$Revision$'];

r = get(gca,'parent');

if nargin == 1,

  % :: Install a button into the toolbar 
  
  % make up a sort of crosshairs icon
  
  icon = ...
      [ 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 ; ...
	0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 ; ...
	0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 ; ...
	0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 ; ...
	0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 ; ...
	0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 ; ...
	0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 ; ...
	1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ; ...
	0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 ; ...
	0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 ; ...
	0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 ; ...
	0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 ; ...
	0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 ; ...
	0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 ; ...
	0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 ];
  
  % Use "NaN" for transparent; 0 for black.
  
  icon(find(icon == 0)) = NaN;
  icon(find(icon == 1)) = 0;
  
  % This follows the technique explained by  Perttu Ranta-aho
  % http://groups.google.com/groups?as_umsgid=lp8z7tf7ds.fsf%40mela16.uku.fi
  
  f = gcf;
  h = findall(f,'tooltip','Rotate 3D');
  pr_h = get(h,'Parent');

  own_h = uitoggletool('Parent',pr_h, 'Separator','off',... 
		       'Tooltip','Pan', ...
		       'Tag', Signature, ...
		       'OnCallback','moveimage(gcf,''enable'')', ...
		       'OffCallback','moveimage(gcf,''disable'')', ...
		       'cdata',repmat(icon,[1 1 3]));
  
   return;
end

state = get(h,'UserData');

cp = get(h,'currentpoint');
point = [cp(1,1) cp(1,2)];

% disp(sprintf('event was %s, point was (%0.2f,%0.2f)',event,cp(1,1),cp(1,2)));

if (strcmp(event,'enable')),
  % Scrolling mode is being enabled -- install our mouse button
  % click callbacks (first saving their original values)
  disp('Moveimage is being enabled.');
  
  % Install the button-press callbacks
  state.WindowButtonDownFcn = get(r,'WindowButtonDownFcn');
  state.DoubleBuffer = get(r,'DoubleBuffer');
  state.UserData = get(h,'UserData');

  toolButton = getappdata(gcf,'ScribeCurrentToolButton');
  set(toolButton, 'State','off');
  setappdata(gcf,'ScribeCurrentToolButton',gcbo);
  	    
  set(r,'WindowButtonDownFcn','moveimage(gca,''down'')');   
  set(r,'DoubleBuffer','on');  % wow, isn't this cool??
  set(h,'UserData',state);
   
elseif (strcmp(event,'disable')),
  % Scrolling mode is being disabled.  We should set the mouse button
  % callbacks to the state in which we originally found them
  disp('Moveimage is being disabled.');

  set(r,'WindowButtonDownFcn',state.WindowButtonDownFcn);
  set(h,'UserData',state.UserData);
  
elseif (strcmp(event,'remove')),
  disp('Moveimage is being removed.');
  
elseif (strcmp(event,'down')),
  
  state.Point = point;
  state.WindowButtonMotionFcn = get(r,'WindowButtonMotionFcn');
  state.WindowButtonUpFcn = get(r,'WindowButtonUpFcn');

  set(h,'UserData',state);
  set(r,'WindowButtonMotionFcn','moveimage(gca,''move'')');
  set(r,'WindowButtonUpFcn','moveimage(gca,''up'')');  

end

if (strcmp(event,'up') | strcmp(event,'move')),
  displacement = state.Point - point;
  set(gca,'xlim',get(gca,'xlim') + displacement(1));
  set(gca,'ylim',get(gca,'ylim') + displacement(2));
end

if (strcmp(event,'up'))
  set(r,'WindowButtonMotionFcn',state.WindowButtonMotionFcn);
  set(r,'WindowButtonUpFcn',state.WindowButtonUpFcn);    
end








   


