function uistate = uiclearmode(fig, varargin)
%UICLEARMODE clears the currently active interactive mode
%   UISTATE=UICLEARMODE(FIG) suspends the interactive properties of a 
%   figure window and returns the previous state in the structure
%   UISTATE.  This structure contains information about the figure's
%   WindowButton* functions and the cursor.  It also contains the 
%   ButtonDownFcn's for all children of the figure.
%
%   UISTATE=UICLEARMODE(FIG, FUNCTION [, ARGS]) suspends the
%   interactive properties of the figure FIG in two ways.
%   First, uiclearmode notifies the currently active mode, if
%   any are active, that the current mode should deinstall its
%   event handlers, such as its Figure WindowButtonDown
%   callbacks.  Next, UICLEARMODE installs the function
%   FUNCTION as the deinstaller for the new mode.  Finally, 
%   UICLEARMODE, like UISUSPEND, resets the WindowButton* 
%   functions and returns that information in a struct which
%   can be saved and passed back to UIRESTORE.
%
%   Example:
%      
%   The following function defines a new interactive mode that
%   cooperates with other modes such as plotedit and rotate3d.
%
%   That is, before myinteractivemode is activated, plot
%   editing or rotate3d is turned off.  If myinteractivemode
%   is active, then activating plot editing calls
%   myinteractive(fig,'off').  The calling syntax for
%   myinteractivemode is:
%      myinteractivemode(gcf,'on')   % display figure current
%                                    % point on mouse down
%   
%   function myinteractivemode(fig,newstate)
%   %MYINTERACTIVEMODE.M
%   persistent uistate;
%      switch newstate
%      case 'on'
%         disp('myinteractivemode: on');
%         uistate = uiclearmode(fig,'myinteractivemode',fig,'off');
%         set(fig,'UserData',uistate,...
%                 'WindowButtonDownFcn',...
%                 'get(gcbf,''CurrentPoint'')',...
%                 'Pointer','crosshair');
%      case 'off'
%         disp('myinteractivemode: off');
%         if ~isempty(uistate)
%            uirestore(uistate);
%            uistate = [];
%         end
%      end
%
%   See also UISUSPEND, UIRESTORE, SCRIBECLEARMODE.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.3 $ $Date: 1998/10/22 21:20:19 $

scribeclearmode(fig, varargin{:});

uistate = struct(...
        'ploteditEnable', [], ...  % supress restoration
        'figureHandle', fig, ...
        'children', findobj(fig), ...
        'WindowButtonMotionFcn', get(fig, 'WindowButtonMotionFcn'), ...
        'WindowButtonDownFcn', get(fig, 'WindowButtonDownFcn'), ...
        'WindowButtonUpFcn', get(fig, 'WindowButtonUpFcn'), ...
        'KeyPressFcn', get(fig, 'KeyPressFcn'), ...
        'Pointer', get(fig, 'Pointer'), ...
        'PointerCdata', get(fig, 'PointerShapeCData'), ...
        'PointerHotSpot', get(fig, 'PointerShapeHotSpot'));

set(fig, 'pointer', get(0, 'DefaultFigurePointer'),...
   'WindowButtonMotionFcn', get(0, 'DefaultFigureWindowButtonMotionFcn'),...
   'WindowButtonDownFcn', get(0, 'DefaultFigureWindowButtonDownFcn'),...
   'WindowButtonUpFcn', get(0, 'DefaultFigureWindowButtonUpFcn'));

uistate.ButtonDownFcns = get(uistate.children, {'buttondownfcn'});
uistate.Interruptible = get(uistate.children, {'Interruptible'});
uistate.BusyAction = get(uistate.children, {'BusyAction'});
set(uistate.children, 'buttondownfcn', '', 'BusyAction', 'Queue', ...
   'Interruptible', 'on');
