function Data=treemove(FunctionToCall,varargin)
%TREEMOVE Handle Object movement in tree.
% This function is now only called by menuedit

%   Loren Dean
%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.22 $ $Date: 1997/11/21 23:49:29 $

switch FunctionToCall,
  case 'Demote',
    LocalDemote(varargin{1});  
    
  case 'DetermineAvailMove',
    Data=LocalDetermineAvailMove(varargin{1});  
    
  case 'MoveDown',
    LocalMoveDown(varargin{1});  
    
  case 'MoveUp',
    LocalMoveUp(varargin{1});  
    
  case 'Promote',  
    LocalPromote(varargin{1});  
    
end % switch

%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalDemote %%%%%
%%%%%%%%%%%%%%%%%%%%%%%
function LocalDemote(Data)
% Demote a uimenu

Handle=Data.All.StringInfo(Data.MenuFig.Movement(4),1);
Children=flipud(allchild(Handle));
set(Data.All.HandleList,'Parent',Handle);
Children=flipud([Children;Data.All.HandleList]);
set(Handle,'Children',Children);  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalDetermineAvailMove %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Data=LocalDetermineAvailMove(Data)
% Determine if movement is possible for a selected object

% Take care of case where the menu editor is the only tool open
if isempty(Data.All.Browser.Complete),
  ComValue=Data.All.Mapping.Menu(get(Data.All.Browser.Menu(1),'Value'));
  Value=Data.All.Mapping.Complete(ComValue);  
else,  
  Value=Data.All.Mapping.Complete(get(Data.All.Browser.Complete(1),'Value'));
end      

NumVal=length(Value);
ObjType=0;
if ~isempty(Data.All.StringInfo),
  Level=Data.All.StringInfo(Value,2);
  ObjType=Data.All.StringInfo(Value,6);
end  

Data.MenuFig.Movement=[0 0 0 0];
if (NumVal==1) & isequal(ObjType,abs('M')), % 1 uimenu selected
  Data=LocalMoveUIMenu(Data,Value,Level);
end  
  
%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalMoveDown %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalMoveDown(Data)
% Move a uimenu down
Handle=Data.All.StringInfo(abs(Data.MenuFig.Movement(2)),1);
% Deal with Figures
if Data.MenuFig.Movement(2)<0,
  Parent=Handle;
else,
  Parent=get(Handle,'Parent');
end  
OldParent=get(Data.All.HandleList,'Parent');
Children=flipud(allchild(Parent));

if Parent==OldParent,
  Loc=find(Children==Handle);
  Children([Loc-1 Loc])=Children([Loc Loc-1]);
else,
  set(Data.All.HandleList,'Parent',Parent);
  Children=[Data.All.HandleList;Children];
end  
set(Parent,'Children',flipud(Children));  

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalMoveUIMenu %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Data=LocalMoveUIMenu(Data,OrigValue,Level);
% Move a UIMenu in the Tree

MenuFigLoc=find(Data.All.StringInfo(:,6)==abs('M')| ...
                Data.All.StringInfo(:,6)==abs('U')| ...
                Data.All.StringInfo(:,6)==abs('F')| ...
                Data.All.StringInfo(:,6)==abs('R')  ...
               );
                 
% This takes care of the fact that menus are flipped
% wrt child order and how they appear in the object browser
Mapping=gnrlfun('FlipMenus',MenuFigLoc,Data.All.StringInfo);
TempStringInfo=Data.All.StringInfo(Mapping,:);            
Value=find(OrigValue==Mapping);      
      
%%%%% Moving Up
%%% Look at everything above or below and see if anything is one level less
%%% and is also a uimenu or figure or context Menu
MoveUpLoc=find((TempStringInfo(1:(Value-1),2)==(Level-1) | ...
                TempStringInfo(1:(Value-1),2)==Level) &    ...
               (TempStringInfo(1:(Value-1),6)==abs('F') |  ...
                TempStringInfo(1:(Value-1),6)==abs('U') |  ...
                TempStringInfo(1:(Value-1),6)==abs('M'))   ...
               );

if ~isempty(MoveUpLoc),           
  % Deal with being at top of child list
  if TempStringInfo(MoveUpLoc(end),2)==Level-1,
    MoveUpLoc(end)=[];
  end  
end  

% Reparent
if ~isempty(MoveUpLoc),
  if TempStringInfo(MoveUpLoc(end),2)==Level-1,
    MoveUpLoc=-1*MoveUpLoc;  
  end  
end  

MoveDownLoc=Value+ ...
            find((TempStringInfo((Value+1):end,2)==(Level-1) | ...
                  TempStringInfo((Value+1):end,2)==Level) &    ...
                 (TempStringInfo((Value+1):end,6)==abs('F') |  ...
                  TempStringInfo((Value+1):end,6)==abs('U') |  ...
                  TempStringInfo((Value+1):end,6)==abs('M'))   ...
                );

% Reparent
if ~isempty(MoveDownLoc),
  if TempStringInfo(MoveDownLoc(1),2)==Level-1,
    MoveDownLoc=-1*MoveDownLoc;  
  end  
end  

%%% Look at what is just above and see if we can promote there          
PromoteLoc=find((TempStringInfo(1:(Value-1),2)==(Level-1) & ...
                 TempStringInfo(1:(Value-1),6)==abs('M'))   ...
               );
           
if ~isempty(PromoteLoc),
  PromoteLoc=PromoteLoc(end);
end
         
%%% Look at what is just above and see if we can demote there          
DemoteLoc=find((TempStringInfo(1:(Value-1),2)==Level & ...
                TempStringInfo(1:(Value-1),6)==abs('M'))  ...
              );
if ~isempty(DemoteLoc),
  DemoteLoc=DemoteLoc(end);
end
       
if isempty(MoveUpLoc),
  MoveUpLoc=0;
else,
  MoveUpLoc=sign(MoveUpLoc(end))*Mapping(abs(MoveUpLoc(end)));
end

if isempty(MoveDownLoc),
  MoveDownLoc=0;
else,
  MoveDownLoc=sign(MoveDownLoc(1))*Mapping(abs(MoveDownLoc(1)));  
end

if isempty(PromoteLoc),         
  PromoteLoc=0;
else  
  PromoteLoc=Mapping(PromoteLoc);
end  

if isempty(DemoteLoc),         
  DemoteLoc=0;
else,  
  DemoteLoc=Mapping(DemoteLoc);
end

Data.MenuFig.Movement=[MoveUpLoc MoveDownLoc PromoteLoc DemoteLoc];
%[MoveUpLoc MoveDownLoc PromoteLoc DemoteLoc]

%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalMoveUp %%%%%
%%%%%%%%%%%%%%%%%%%%%%%
function LocalMoveUp(Data)
% Move a uimenu up
Handle=Data.All.StringInfo(abs(Data.MenuFig.Movement(1)),1);

% Deal with Figures
if Data.MenuFig.Movement(1)<0,
  Parent=Handle;
else,
  Parent=get(Handle,'Parent');
end  
OldParent=get(Data.All.HandleList,'Parent');
Children=flipud(allchild(Parent));

if Parent==OldParent,
  Loc=find(Children==Handle);
  Children(Loc:Loc+1)=Children(Loc+1:-1:Loc);
else,
  set(Data.All.HandleList,'Parent',Parent);
  Children=[Children;Data.All.HandleList];
end  
set(Parent,'Children',flipud(Children));  

%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalPromote %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
function LocalPromote(Data)
% Promote a uimenu
Handle=Data.All.StringInfo(Data.MenuFig.Movement(3),1);
Parent=get(Handle,'Parent');
Children=flipud(allchild(Parent));

set(Data.All.HandleList,'Parent',Parent);
Loc=find(Children==Handle);
Children=flipud([Children(1:Loc);Data.All.HandleList;Children((Loc+1:end)')]);
set(Parent,'Children',Children)

