function scribetoolbar(varargin)
%SCRIBETOOLBAR  Plot Editor helper function
%
%   See also PLOTEDIT

%   SCRIBETOOLBAR called with no arguments toggles state of plot
%     activation:
%     1. inactive figures with no overlay are activated
%     2. inactive figures with an overlay layer are activated
%     3. active figures are deactivated
%

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.28 $  $Date: 1998/06/10 23:28:12 $
%   j. H. Roh   

switch nargin
case {0 1}
   if nargin==0
      f = gcf;
   else
      f = varargin{1};
   end
   % get current status
   [aOverlay, fActive] = LTestActive(f);
   fActive = plotedit(f,'isactive');
   
   if fActive
      action = 'deactivate';
   elseif isempty(aOverlay)
      action = 'activate';
   else
      action = 'reactivate';
   end
case 2   % scribetoolbar(f, <resize | active | inactive> )
   
   f = varargin{1};
   newstate = varargin{2};
   % get current status
   [aOverlay, fActive] = LTestActive(f);
   fActive = plotedit(f,'isactive');
   
   switch newstate
   case 'resize'
      action = 'resize';
   case 'active'
      if fActive
         return
      elseif isempty(aOverlay)
         action = 'activate';
      else
         action = 'reactivate';
      end
   case 'inactive'
      if fActive
         action = 'deactivate';
      else
         return
      end
   end

end

tbSelectPlotEdit = findall(f,'Tag','ScribeSelectToolBtn');

switch action
case 'resize'
   return
case 'activate'
   dontActivate = ctlpanel('Activate',f);
   if ~isempty(dontActivate)
      return
   end

   if isempty(tbSelectPlotEdit), return, end

   %set(tbSelectPlotEdit,'State','on');

case 'reactivate'
   dontActivate = ctlpanel('Activate',f);
   % turn off GUIDE
   if ~isempty(dontActivate)
      return
   end

   if isempty(tbSelectPlotEdit), return, end

   %set(tbSelectPlotEdit,'State','on');

   %objV = findall(f);
   %LReactivate(objV);

   set(aOverlay,...
           'HitTest','off',...
           'Tag','ScribeOverlayAxesActive');

case 'deactivate'

   if isempty(tbSelectPlotEdit), return, end

   %set(tbSelectPlotEdit,'State','off');

   % set(aOverlay,'Tag','ScribeOverlayAxesInactive');
   %LDeactivate(findall(f));

end


function LDeactivate(hV)
for h = hV'
   savedFcns = getappdata(h,'ScribeSaveFcns');
   scribeFcns = get(h,{'ButtonDownFcn' 'UIContextMenu'});  % and 'Selected'?
   setappdata(h,'ScribePlotEditFcns',scribeFcns);
   if ~isempty(savedFcns)
      set(h,{'ButtonDownFcn' 'UIContextMenu'},savedFcns);
   end
   if ~isempty(scribeFcns)
      setappdata(h,'ScribeSaveFcns',savedFcns);
   end
end

function LSaveFcns(hV)
for h = hV'
   savedFcns = get(h,{'ButtonDownFcn' 'UIContextMenu'});  % and 'Selected'?
   setappdata(h,'ScribeSaveFcns',savedFcns);
end

function LReactivate(hV)
LSaveFcns(hV)
for h = hV'
   scribeFcns = getappdata(h,'ScribePlotEditFcns');
   savedFcns = get(h,{'ButtonDownFcn' 'UIContextMenu'});  % and 'Selected'?
   if ~isempty(scribeFcns)
      set(h,{'ButtonDownFcn' 'UIContextMenu'},scribeFcns);
   end
   if ~isempty(savedFcns)
      setappdata(h,'ScribeSaveFcns',savedFcns);
   end
end


function oldUnits = LSetUnits(objects, newSetting)
oldUnits = get(objects,'Units');
set(objects,'Units',newSetting);


function LRestoreUnits(objects, oldUnits)
iValid = ishandle(objects);
set(objects(iValid),{'Units'},oldUnits(iValid));
if length(iValid)~=length(objects)
   warning('Resize may have corrupted object units.');
end


function [overlayAx, fActive] = LTestActive(f)
aActive = findall(f,'Type','axes','Tag','ScribeOverlayAxesActive');
aInactive = findall(f,'Type','axes','Tag','ScribeOverlayAxesInactive');

if ~isempty(aActive)
   overlayAx = aActive;
   fActive = 1;
elseif ~isempty(aInactive)
   overlayAx = aInactive;
   fActive = 0;
else
   overlayAx = [];
   fActive = 0;
end



