function varargout = putdowntext(varargin)
%PUTDOWNTEXT  Plot Editor helper function
%
%   See also PLOTEDIT
 
%   Copyright (c) 1984-98 by The MathWorks, Inc.  
%   $Revision: 1.40 $  $Date: 1998/11/21 00:29:55 $



if ischar(varargin{1})
   fig = gcbf;
   if isempty(fig) | ~ishandle(fig), return, end   
   action = varargin{1};
   toolButton = getappdata(fig,'ScribeCurrentToolButton');

   if nargin>1
      if ~isempty(toolButton)  % aborting one operation and starting another
         if ishandle(toolButton) ...
            & strcmp(get(toolButton,'Type'),'uitoggletool') ...
            & toolButton ~= gcbo  % not the same button
            set(toolButton,'State','off');
         else
            toolButton = [];
         end
      end
      toolButton = varargin{2};
   end
else
   fig = varargin{1}(1);
   if isempty(fig) | ~ishandle(fig), return, end
   action = varargin{2};
   toolButton = getappdata(fig,'ScribeCurrentToolButton');
end

if isempty(fig) | ~ishandle(fig), return, end
setappdata(fig,'ScribeCurrentToolButton',toolButton);

% black by factory default.  Can be changed by the user
defaultAnnotationColor = get(fig,'DefaultTextColor');

stateData = getappdata(fig,'ScribeAddAnnotationStateData');
if isempty(stateData)
   stateData = LInitStateData(fig);
   setappdata(fig,'ScribeAddAnnotationStateData', stateData);
end


switch action

case 'select'
   switch get(toolButton,'State')
   case 'off'
         plotedit(fig,'off');         
   case 'on'
      dontActivate = ctlpanel('Activate',fig);
      if ~isempty(dontActivate)
         set(toolButton,'State','off');
      else
         plotedit(fig,'on');
      end
   end
      
case 'start'
   % if guide is active give users a chance to cancel   
   dontActivate = ctlpanel('Activate',fig);
   if ~isempty(dontActivate)
      % abort
      set(toolButton,'State','off');
      varargout{1} = 0;
      return
   else 
      % continue
      varargout{1} = 1;
   end
   
   LSetSelect(fig,'off');  % plotedit off first
   
   LMaskAll(fig,'off');    % this saves windowXXXFcn settings
   set(toolButton,'State','on');      

   set(fig,'Pointer',stateData.oldPointer);
   if ~isempty(stateData.myline) & ishandle(stateData.myline)
      delete(stateData.myline);
   end
   stateData = LInitStateData(fig);
   setappdata(fig,'ScribeAddAnnotationStateData', stateData);

case 'axesstart'
   if putdowntext('start')
      set(fig,'Pointer','crosshair',...
              'WindowButtonDownFcn','putdowntext hitaxes');
   end
case 'hitaxes'
   rect = rbbox;  % returns a rectangle in figure units
   units = get(fig,'Units');
   if rect(3:4)>[0 0]
      newAx = axes('Parent',fig,...
              'Units',units,...
              'Position',rect);
      set(newAx,'Units','normalized');
      axH = scribehandle(axisobj(newAx));
      set(axH,'Draggable',1);
   end
   putdowntext reset;
   % end add with plotedit on always
   LSetSelect(fig,'on'); % do this last

case 'textstart'
   if putdowntext('start')
      set(fig,'Pointer','ibeam',...
              'WindowButtonDownFcn','putdowntext hittext')   
   end
case 'hittext'
   set(fig,'WindowButtonDownFcn','')   
   
   ax = LGetOverlayAxis(fig);
   if isempty(ax), return, end
   
   set(fig,'CurrentObject',ax);
   
   pt = get(ax,'CurrentPoint');
   th = newtext(pt(1,1),pt(1,2),' ','Parent',ax);

   putdowntext reset;
   % end add with plotedit on always
   LSetSelect(fig,'on'); % do this last

case 'arrowstart'
   if putdowntext('start')
      stateData.isarrow = 1;
      setappdata(fig,'ScribeAddAnnotationStateData', stateData);   
      set(fig,'Pointer','crosshair',...
              'WindowButtonDownFcn','putdowntext linego');
   end
case 'linestart'
   if putdowntext('start')
      set(fig,'Pointer','crosshair',...
              'WindowButtonDownFcn','putdowntext linego');
   end
case 'linego'
   oldUnits = get(fig,'Units');
   set(fig,'WindowButtonDownFcn','',...
           'Units','pixels');

   stateData.figPt = get(fig,'CurrentPoint');
   set(fig,'Units',oldUnits);
   
   ax = LGetOverlayAxis(fig);
   set(fig,'CurrentObject',ax);

   pt = get(ax,'CurrentPoint');
   stateData.x = pt(1);
   stateData.y = pt(3);

   stateData.myline = line(stateData.x,stateData.y,'EraseMode','xor',...
           'Parent',ax,...
           'Color',defaultAnnotationColor,...
           'CreateFcn','');

   set(fig,'WindowButtonMotionFcn','putdowntext linedrag',...
           'WindowButtonUpFcn','putdowntext linepoint1');
   setappdata(fig,'ScribeAddAnnotationStateData', stateData);

case 'linepoint1' 
   xUp = get(stateData.myline,'XData');
   yUp = get(stateData.myline,'YData');
   if length(xUp)==1 & length(yUp)==1 % clicked once without dragging
      % continue
      set(fig,'WindowButtonDownFcn','putdowntext reset',...
              'WindowButtonUpFcn','',...
              'WindowButtonMotionFcn','putdowntext linepoint2');
   else % end dragging
      putdowntext lineend;
   end

case 'linepoint2'
   ax = LGetOverlayAxis(fig);   
   set(fig,'CurrentObject',ax);
   
   set(fig,'WindowButtonMotionFcn','putdowntext linedrag',...
           'WindowButtonDownFcn','putdowntext lineend');   
   
case 'linedrag'
   ax = LGetOverlayAxis(fig);
   pt = get(ax,'CurrentPoint');
   set(stateData.myline,'XData', [stateData.x pt(1)], 'YData', [stateData.y pt(3)]);

case 'lineend'
   ax = LGetOverlayAxis(fig);   
   set(fig,'CurrentObject',ax);

   lineobj=[];
   
   oldUnits = get(fig,'Units');
   set(fig,'Units','pixels');
   pt = get(fig,'CurrentPoint');
   set(fig,'Units',oldUnits);
   % minimum size: 5 pixel
   if sum(abs(pt-stateData.figPt).^2)>=25
      
      if stateData.isarrow
         % we really have a line
         % redundant check
         x = get(stateData.myline,'XData');
         y = get(stateData.myline,'YData');
         if length(x)>1 & length(y)>1
            % at least two points
            ax = LGetOverlayAxis(fig);
            lineobj = scribehandle(arrowline(x,y,...
                    'Color',defaultAnnotationColor,...
                    'Parent',ax));
         end
         delete(stateData.myline);
         stateData.myline = [];
      else
         lineobj = scribehandle(editline(stateData.myline));
         stateData.myline = [];
      end % if stateData.isarrow
      
      if ~isempty(lineobj)
         set(lineobj,'EraseMode','normal');
         set(lineobj,'IsSelected',1);   
         set(lineobj,'DragConstraint','');
      end

   end % if sum(

   setappdata(fig,'ScribeAddAnnotationStateData',stateData);
   putdowntext reset;
   % end add with plotedit on always
   LSetSelect(fig,'on'); % do this last

   if ~isempty(lineobj)
      HG = get(lineobj,'MyHGHandle');
      set(fig,'CurrentObject',HG);
      setappdata(fig,'ScribeHGCurrentObject',HG);
   end
   
case 'reset'
   
   try
      if ~isempty(stateData.myline) & ishandle(stateData.myline)
         delete(stateData.myline);
         stateData.myline = [];      
      end
      
      if ishandle(fig)
         % set(fig,'Pointer',stateData.oldPointer,...
         %         'WindowButtonDownFcn','',...
         %         'WindowButtonMotionFcn','',...
         %         'WindowButtonUpFcn','');
      
         if ~isempty(toolButton)
            set(toolButton,'State','off');
         end
         
         stateData = LInitStateData(fig);
         setappdata(fig,'ScribeAddAnnotationStateData', stateData);
         
         LMaskAll(fig,'on');  % restore
      end
   catch
      % state may have changed while we were finishing
      % up. e.g. window closed etc.
   end
case 'zoomin'
   fixtoolbar(fig);
   switch zoom(fig,'getmode')
      case {'off' 'out'}
         zoom(fig,'inmode');
      case {'in' 'on'}
         zoom(fig,'off');
   end

case 'zoomout'
   fixtoolbar(fig);
   switch zoom(fig,'getmode')
      case {'on' 'off' 'in'}
         zoom(fig,'outmode');
      case 'out'
         zoom(fig,'off');
   end

case 'rotate3d'
   fixtoolbar(fig);
   if ishandle(toolButton)
      rotate3d(get(toolButton,'State'));
   else
      rotate3d;
   end
end   



function overlayAxis = LGetOverlayAxis(fig)

overlayAxis = findall(fig,'Tag','ScribeOverlayAxesActive');
if isempty(overlayAxis)
   alreadyOn = plotedit(fig,'isactive');
   plotedit(fig,'on','silent');
   if ~alreadyOn
      plotedit(fig,'off','silent');
   end
   overlayAxis = findall(fig,'Tag','ScribeOverlayAxesActive');
end


function LMaskAll(fig,  setting)

WindowFcnList = {...
        'Pointer',...
        'WindowButtonDownFcn', ...
        'WindowButtonMotionFcn',...
        'WindowButtonUpFcn'};

savedSettings = getappdata(fig,'ScribeWindowMaskSettings');

switch setting
case 'on'  % restore
   if ~isempty(savedSettings) & isstruct(savedSettings)
      set(fig, WindowFcnList, savedSettings.WindowFcns);
      savedSettings = [];
   end
case 'off' % save
   promoteoverlay(fig);
   savedSettings.WindowFcns = get(fig, WindowFcnList);
   set(fig, WindowFcnList(2:4), {'' '' ''});
end

setappdata(fig,'ScribeWindowMaskSettings',savedSettings);

function LSetSelect(fig,state)
if ishandle(fig)
   switch state
      case 'off'
         scribeclearmode(fig,'putdowntext',fig,'reset');
      case 'on'
         plotedit(fig,'on');
   end
end
   
function stateData = LInitStateData(fig)
stateData = struct(...
        'x',[], ...
        'y', [], ...
        'myline', [], ...
        'isarrow', 0, ...
        'oldPointer', get(fig,'Pointer'));
     

