function scribeeventhandler
%SCRIBEEVENTHANDLER  Plot Editor helper function

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.8 $  $Date: 1998/10/22 22:23:06 $

fig = gcbf;
co = gcoall(fig);
cbo = co;

scribeButtonDownFcn = 'doclick';
primaryObject = getappdata(co,'ScribeButtonDownHGObj');
if ~isempty(primaryObject)
   co = primaryObject;
end
setappdata(fig,'ScribeHGCurrentObject',co);

ud = getappdata(co,'ScribeObjectData');
if isempty(ud)
   obj = [];
   coTag = get(co,'Tag');
   if strmatch('ScribeArrowline',coTag)
      % this is an arrow whos data was blown away
      cbo = co;
      obj = set(arrowline(co),'Draggable',1);
      obj = set(obj,'DragConstraint','');
      co = get(obj,'MyHGHandle');
   else
      switch get(co,'Type')
      case 'axes'
         if ~strcmp(get(co,'Tag'),'legend')
            obj = set(axisobj(co),'Draggable',0);
         end
      case 'line'
         axisTag = get(get(co,'Parent'),'Tag');
         switch axisTag
         case 'legend'
            % NOOP      
         case 'ScribeOverlayAxesActive'
            obj = set(editline(co),'Draggable',1);
            obj = set(obj,'DragConstraint','');
         otherwise
            obj = set(editline(co),'Draggable',0);
         end
      case 'text'
         if ~strcmp(get(get(co,'Parent'),'Tag'),'legend')      
            obj = set(axistext(co),'Draggable',1);
         end
      otherwise % patch, line, surface, ... NOT figure...
         coParent = get(co,'Parent');
         if strcmp('axes',get(coParent,'Type')) ...  % child of an axis
            & ~strcmp('legend',get(coParent,'Tag'))  % not part
                                                     % of a legend
            % redirect to the parent axes
            cbo = co;
            co = coParent;
            objH = getobj(co);
            if isempty(objH)
               obj = set(axisobj(co),'Draggable',0);
            else
               obj = objH.Object;
            end
         end
      end
   end
   if ~isempty(obj)
      p = scribehandle(obj);
      ud = getappdata(co,'ScribeObjectData');
      MLObj = ud.ObjectStore;
   else
      return
   end
else
   MLObj = ud.ObjectStore;
end
   
selType = get(fig,'SelectionType');
switch selType
case {'normal' 'open' 'extend'}  % left click
   setappdata(cbo,'ScribeSaveFcns',get(cbo,'ButtonDownFcn'));
   set(cbo,'ButtonDownFcn','scriberestoresavefcns');
   switch get(co,'Type')
      case 'axes'
         if get(MLObj,'IsSelected') & get(MLObj,'Draggable')
            H = ud.HandleStore;
            domethod(H,'resize');
         else
            if ~isempty(scribeButtonDownFcn)
               feval(scribeButtonDownFcn,MLObj);
            end
         end
      otherwise
         if ~isempty(scribeButtonDownFcn)
            feval(scribeButtonDownFcn,MLObj);
         end
   end
case 'alt'     % right click
   contextMenu = getappdata(co,'ScribeUIContextMenu');
   if ~isempty(contextMenu)
      saveFcns = get(co,{'UIContextMenu' 'ButtonDownFcn'});
      setappdata(contextMenu,'ScribeSaveFcns', saveFcns);
      set(fig,'CurrentObject',co);
      set(co,{'UIContextMenu' 'ButtonDownFcn'}, ...
              {contextMenu 'doclick(gcbo)'});
      % fire the context menu if there is one
      % the original is restored by domymenu update
      setappdata(contextMenu,'ScribeOneShotContextMenuFlag',co);
   end
end


