function Data=optnfun(FunctionToCall,Data,varargin)
%OPTNFUN Figure Options menu callback

%   Loren Dean
%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.9 $ $Date: 1998/02/03 14:13:22 $


switch FunctionToCall,
  case 'Bring To Front',
    Data=LocalBringToFront(Data);  
    
  case 'Send To Back',
    Data=LocalSendToBack(Data);
        
  case 'Snap Objects To Grid',
    Data=LocalSnapObjects(Data);
    
  case 'Display Grid',   
    Data=LocalShowGrid(Data,varargin{:});
    
  case 'Set Grid Spacing...',
    Data=LocalSetGridSpacing(Data);
    
end % switch



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalBringToFront %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Data=LocalBringToFront(Data)

Handles=Data.All.HandleList;
Parent=get(Data.All.HandleList,{'Parent'});Parent=[Parent{:}];
[Parent,Index]=sort(Parent);
Handles=Handles(Index);
[UParent,UIndex,UVal]=unique(Parent);

for DLp=1:length(UParent),
  Children=allchild(UParent(DLp));
  MoveChildren=Handles(UVal==DLp);  
  HandleLoc=find(ismember(Children,MoveChildren));
  Children(HandleLoc)=[];  
  set(UParent(DLp),'Children',[MoveChildren;Children]);    
end
   
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalSendToBack %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Data=LocalSendToBack(Data)

Handles=Data.All.HandleList;
Parent=get(Data.All.HandleList,{'Parent'});Parent=[Parent{:}];
[Parent,Index]=sort(Parent);
Handles=Handles(Index);
[UParent,UIndex,UVal]=unique(Parent);

for DLp=1:length(UParent),
  Children=allchild(UParent(DLp));
  MoveChildren=Handles(UVal==DLp);  
  HandleLoc=find(ismember(Children,MoveChildren));
  Children(HandleLoc)=[];  
  NewOrder=[Children;MoveChildren];    
  if strcmp(get(UParent(DLp),'Type'),'figure'),
    FigureLoc=Data.All.ControlInfo.Figures.Handle==UParent(DLp);
    AxisHandle=Data.All.ControlInfo.Figures.Misc(FigureLoc).AxisHandle;
    NewOrder(NewOrder==AxisHandle)=[];
    NewOrder=[NewOrder;AxisHandle];
  end      
  set(UParent(DLp),'Children',NewOrder);    
  
end
   

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalSnapObjects %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Data=LocalSnapObjects(Data)
  SnapHandle=gcbo;
  if strcmp(get(SnapHandle,'Checked'),'on'),
    set(SnapHandle,'Checked','off');
  else
    set(SnapHandle,'Checked','on');
  end  
  CurFig=get(get(gcbo,'Parent'),'Parent');
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalShowGrid %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Data=LocalShowGrid(Data,CurrentFig)

% This function is also called by LocalSnapObjects and
% LocalSetGridSpacing so we have can't rely on gcbo being the display
% grid handle

% It's being called by the menu
if nargin==1,
 CurrentFig=get(get(gcbo,'Parent'),'parent');
end

DispHandle=findall(CurrentFig,'Tag','FigMenuOptionsDisplayGrid');

if nargin==1,
 if strcmp(get(DispHandle,'Checked'),'on'),
   set(DispHandle,'Checked','off');
 else
   set(DispHandle,'Checked','on');
 end
end

FigLoc=find(Data.All.ControlInfo.Figures.Handle==CurrentFig);
AxisHandle=Data.All.ControlInfo.Figures.Misc(FigLoc).AxisHandle;

GridSizeHandle=findall(CurrentFig,'Tag','FigMenuOptionsSetGrid');
GridSize=get(GridSizeHandle,'UserData');
GridSize=[GridSize{:}];

AxUnits=get(AxisHandle,'Units');
set(AxisHandle,'Units','pixels');
AxPos=get(AxisHandle,'Position');
set(AxisHandle,'Units',AxUnits);

AxData.XTick=[0:GridSize(1):AxPos(3)];
if AxData.XTick(end)~=AxPos(3),
  AxData.XTick=[AxData.XTick AxPos(3)];
end

%AxData.YTick=[0:GridSize(2):AxPos(4)];
AxData.YTick=[0:GridSize(1):AxPos(4)];
if AxData.YTick(end)~=AxPos(4),
  AxData.YTick=[AxData.YTick AxPos(4)];
end

AxData.XLim=AxData.XTick([1 end]);
AxData.YLim=AxData.YTick([1 end]);

if strcmp(get(DispHandle,'Checked'),'on'),
  AxData.XGrid='on';
  AxData.YGrid='on';
  AxData.ZGrid='off';
else,
  AxData.XGrid='off';
  AxData.YGrid='off';
  AxData.ZGrid='off';
end

set(AxisHandle,AxData);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalSetGridSpacing %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Data=LocalSetGridSpacing(Data)
%Prompt={'Enter the XGrid Spacing (pixels)'
%        'Enter the YGrid Spacing (pixels)'};
Prompt={'Enter the grid spacing (pixels)'};
        
SnapHandle=gcbo;
CurValues=get(SnapHandle,'UserData');
%DefaultVal={num2str(CurValues{1}),num2str(CurValues{2})};
DefaultVal={num2str(CurValues{1})};
Title='Snap Grid Spacing';
lineNo=[1 20];
Answer=inputdlg(Prompt,Title,lineNo,DefaultVal);

if ~isempty(Answer),
  if isempty(Answer{1}),
    Answer{1}='0';
  end
  CurValues{1}=str2num(Answer{1});
  
  %if isempty(Answer{2}),
  %  Answer{2}='0';
  %end
  %CurValues{2}=str2num(Answer{2});
  
  set(SnapHandle,'UserData',CurValues);
  CurFig=get(get(gcbo,'Parent'),'Parent');
  Data=LocalShowGrid(Data,CurFig);
end
