function cutcopypaste(currentFig,action)
%CUTCOPYPASTE  for Scribe Plot Editor

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.7 $  $Date: 1998/10/21 21:45:27 $


switch action
case 'cut'
   cutcopypaste(currentFig,'copy');
   LClearSelection(currentFig);
   return
   
case 'copy'
   [copyBufferFig, copyBufferAx] = getcopybuffer;
   % erase current copy buffer
      
   LClearSelection(copyBufferFig);
   selectionList = LGetSelection(currentFig);
   % scan for axes
   axesHandles = [];
   for aObjH = selectionList
      if strcmp(get(aObjH,'Type'),'axes')
         axesHandles(end+1)=get(aObjH,'MyHGHandle');
      end
   end
   % remove children of those axes from the selection list
   if ~isempty(axesHandles)
      for aObjH = selectionList
         if any(get(aObjH,'Parent')==axesHandles)
            set(aObjH,'IsSelected',0);
         end
      end
   end
   
   
   % store new selection
   initialPosition = get(currentFig,'CurrentPoint');
   for aObjH = LGetSelection(currentFig)
      objType = get(aObjH,'Type');
      switch objType
      case 'axes'
         set(aObjH,'Offset',initialPosition);
         newAH = copyobj(aObjH, copyBufferFig);
      case 'text'
         oldUnits = get(aObjH,'Units');
         % offset according to position relative to figure,
         % rather than position relative to parent axis
         set(aObjH,'Units','normalized');
         set(aObjH,'FigureOffset',initialPosition);
         newAH = copyobj(aObjH, copyBufferAx);
         set(aObjH,'Units',oldUnits);
      otherwise
         set(aObjH,'Offset',initialPosition);
         newAH = copyobj(aObjH, copyBufferAx);
         if LIsDataObj(aObjH)
            LMarkAsDataObj(newAH);
         end
      end
   end
   
   return
case 'paste'
   [copyBufferFig, copyBufferAx] = getcopybuffer;   
   % paste into current window from copy buffer
   overlayAx = findobj(allchild(currentFig),'flat',...
           'Tag','ScribeOverlayAxesActive');
   currentAx = get(currentFig,'CurrentAxes');
   fCreateDataAxes = isempty(currentAx);

   if isempty(overlayAx)
      warning('Could not paste into the current figure');
      return
   end
   
   for aObjH = LGetSelection(currentFig)
      set(aObjH,'IsSelected',0);
   end
   
   oldFigUnits = get(currentFig,'Units');
   newPt = get(overlayAx,'CurrentPoint');
   newX = newPt(1,1);
   newY = newPt(1,2);
   for aObjH = LGetSelection(copyBufferFig)
      if LIsAxisObj(aObjH)
         newAH = copyobj(aObjH, currentFig);
         if get(newAH,'Draggable')
            objectUnits = get(newAH,'Units');
            set(currentFig,'Units',objectUnits);
            newFigPt = get(currentFig,'CurrentPoint');
            domove(newAH,newFigPt(1),newFigPt(2),'refresh');
         end
      else
         if LIsMarkedDataObj(aObjH)
            if fCreateDataAxes
               currentAx = axes('Parent',currentFig);
               fCreateDataAxes = 0;
            end
            newAH = copyobj(aObjH, currentAx);
         else
            % annotations
            % offset and copy in
            newAH = copyobj(aObjH, overlayAx);
            domove(newAH,newX,newY,'refresh');
         end
      end
   end
   set(currentFig,'Units',oldFigUnits);

   promoteoverlay(currentFig);

   return
case 'clear'
   % clear current selection
   LClearSelection(currentFig);
end



function val = LIsAxisObj(aObjH)
val = strcmp(get(aObjH,'Type'),'axes');

function val = LIsDataObj(aObjH)
val = ~strcmp(get(get(get(aObjH,'MyHGHandle'),'Parent'),'Tag'),...
           'ScribeOverlayAxesActive');

   
function val = LIsMarkedDataObj(aObjH)
% Temporary test: replace this with an explicit object
% property
fDataObj = getappdata(get(aObjH,'MyHGHandle'),'ScribeDataObj');
if isempty(fDataObj)
   val = 0;
else
   val = fDataObj;
end


function LMarkAsDataObj(aObjH)
setappdata(get(aObjH,'MyHGHandle'),'ScribeDataObj',1);



function LClearSelection(aFig)
% erase currently selected objects
aFigObjH = getobj(aFig);
if ~isempty(aFigObjH)
   dragBinH = aFigObjH.DragObjects;
   for aObjH = dragBinH.Items
      delete(aObjH);
   end
end


function objectVector = LGetSelection(aFig)
objectVector = [];
aFigObjH = getobj(aFig);
if ~isempty(aFigObjH)
   dragBinH = aFigObjH.DragObjects;
   objectVector = dragBinH.Items;
end

