function IsOwner = mouseevent(Editor,EventName,Status,DragModeOn,GlobalModeOn)
%MOUSEEVENT  Processes mouse events.

%   Author(s): P. Gahinet
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.9 $  $Date: 2000/07/26 12:55:15 $

% REVISIT: merge
HG = Editor.HG;
PlotAxes = HG.PlotAxes;
CP = get(PlotAxes,{'CurrentPoint'});
CP = cat(1,CP{:});
CP = CP(1:2:end,:);
isHovered = (CP(:,3)==1 & strcmp(get(PlotAxes,'Visible'),'on'));

% Determine is the mouse is over this editor
% RE: Editors in drag mode automatically own the event
IsOwner = strcmp(Editor.Visible,'on') & (DragModeOn | any(isHovered));
if ~IsOwner | strcmp(Editor.EditMode,'off')
    % Exit if editor does not own the event or is turned off
    return
end

% Process event when editor owns it 
SISOfig = get(PlotAxes(1),'Parent');  % host figure

switch EventName
    
case 'wbd'
    % WindowButtonDown event
    if ~strcmp(get(SISOfig,'SelectionType'),'normal')
        % Right-click should behave as normal
        return
    end
    
    switch Editor.EditMode
    case 'zoom'
        % Start zooming
        % Find selected axes
        set(SISOfig,'CurrentObject',PlotAxes(isHovered));   % to bypass all BD callbacks
        % Initialize zoom
        Editor.ZoomMode = 'zooming';
        [i,j] = find(isHovered);
        zoomsm('start',Editor.ModeData,PlotAxes,i,j)
    end
    
case 'wbm'
    % WindowButtonMotion event
    switch Editor.EditMode
    case 'idle'
        % Hovering over editor in idle mode
        % REVISIT: merge next two
        out = hoverstatus(Editor,PlotAxes(isHovered),CP(isHovered,1:2),Status);
        [PointerType,Status] = deal(out{:});
        
    case 'add'
        % Dragging pole/zero
        PZType = Editor.ModeData{1};
        if any(strcmp(PZType,{'Real','Complex'}))
            PZID = lower(Editor.ModeData{2});  % pole or zero
            PointerType = sprintf('add%s',PZID); % addpole or addzero
            Status = sprintf('Left-click where you want to add this %s.',PZID);
        else
            PointerType = 'addpole';  % default
            Status = sprintf('Left-click where you want to add this %s.',lower(PZType));
        end
        
    case 'delete'
        % Deleting pole/zero
        PointerType = 'eraser';
        Status = sprintf('Left-click on the pole/zero you want to delete.');
        
    case 'drag'
        % Drag mode
        PointerType = 'closedhand';
        % Acquire new gain value and update gain data
        switch Editor.ModeData
        case 'gain'
            Status = trackgain(Editor,'acquire');
        case 'notchwidth'  % bode only
            Status = shapenotch(Editor,'acquire');
        case 'pz'
            Status = trackpz(Editor,'acquire');
        end
        
    case 'zoom'
        % Zooming in: use crosshair
        PointerType = 'crosshair';
        switch Editor.ZoomMode
        case 'waiting'
            Status = sprintf('Left-click and drag the cursor over the region to magnify.');
        case 'zooming'
            Status = sprintf('Release the mouse to zoom into the selected region.');
            % update HG zoom
            zoomsm('update')
        end 
        
    otherwise
        % Default
        PointerType = 'arrow'; 
        
    end  
    
    % Update dynamic status and pointer
    hgset_status(Editor,Status);
    if xor(strcmp(get(SISOfig,'Pointer'),'arrow'),strcmp(PointerType,'arrow'))
        setptr(SISOfig,PointerType)
    end
    
case 'wbu'
    % WindowButtonUp event
    if ~any(strcmp(get(SISOfig,'SelectionType'),{'normal','extend'}))
        % Right-click should behave as normal
        return
    end
    
    % Exit actions
    switch Editor.EditMode
    case 'add'
        % Completing Add Pole/Zero operation
        addpz(Editor);
         
    case 'delete'
        % Completing Delete Pole/Zero operation
        deletepz(Editor);
         
    case 'drag'
        % Complete drag mode
        switch Editor.ModeData
        case 'gain'
            trackgain(Editor,'finish');
        case 'notchwidth' 
            shapenotch(Editor,'finish');
        case 'pz'
            trackpz(Editor,'finish');
        end
        
    case 'zoom'
        % Completing zoom. 
        % Exit zoom and update limit modes
        % REVISIT: collapse
        [X,Y] = zoomsm('finish',Editor.XlimMode,Editor.YlimMode);
        Editor.XlimMode = X;
        Editor.YlimMode = Y;
        % Call UPDATELIMS after zooming in (to update grids)
        updatelims(Editor);
        
        % Update status and zoom mode
        hgset_status(Editor,'Zoom completed.');
        Editor.ZoomMode = 'waiting';
    end
    
    % Exit current mode if local in scope
    if ~GlobalModeOn
        % Revert to idle mode
        Editor.EditMode = 'idle';
        set(SISOfig,'Pointer','arrow');  % reset pointer
    end
    
end



