function menuedit(varargin)
%MENUEDIT Edit menu.
%   MENUEDIT allows the menus on figures to be edited interactively.
%   MENUEDIT(HandleList) selects all objects in HandleList.
%
%   See also GUIDE, ALIGN, CBEDIT, CTLPANEL, PROPEDIT.

%   Loren Dean
%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.43 $ $Date: 1996/01/04 16:45:53 

MenuFig=findall(0,'Tag','Guide Menu Editor');

Data=[];WatchFlag=1;
if ~isempty(MenuFig),
  Data=get(MenuFig,'UserData');
  if ~isempty(Data),  
    Data=get(Data.All.DataHandle,'UserData');
    if nargin==1 & strcmp(varargin{1},'Resize'),    
      [Data,WatchFlag]=setwatch('on',Data,1);
    else,      
      [Data,WatchFlag]=setwatch('on',Data);
    end      
  end    
end  


% Wrap try catch around the callback area
%try,

switch nargin,
  %%%%%%%%%%%%%%%%%%%%%%%%
  %%% Peform an action %%%
  %%%%%%%%%%%%%%%%%%%%%%%%
  case 1,
    if isstr(varargin{1}),
      Action=varargin{1};
      ComValue=Data.All.Mapping.Menu(get(Data.All.Browser.Menu(1),'Value'));
      Value=Data.All.Mapping.Complete(ComValue);  
          
      switch Action,
        %%%%%%%%%%%%%
        %%% Apply %%%
        %%%%%%%%%%%%%
        case 'Apply',
          LocalApply(Data.All.HandleList,Data,MenuFig);        
          Data=guide('BrowserUpdate',Data);
          
        %%%%%%%%%%%%%%%%%%
        %%% ButtonDown %%%
        %%%%%%%%%%%%%%%%%%
        case 'ButtonDown',
          Data=objbrwser('ButtonDown',Data, ...
                         Data.MenuFig.BrowserListHandle,MenuFig,'Menu');
         
        %%%%%%%%%%%%%
        %%% Close %%%
        %%%%%%%%%%%%%
        case 'Close',
          Data=guide('ToolUpdate',Data,MenuFig,'delete');
          
        %%%%%%%%%%%
        %%% Cut %%%
        %%%%%%%%%%%
        case 'Cut',
          Data=editfun('Cut',Data);
          Data=guide('HandleUpdate',Data);
          
        %%%%%%%%%%%%
        %%% Copy %%%
        %%%%%%%%%%%%
        case 'Copy',
          Data=editfun('Copy',Data);
          
        %%%%%%%%%%%%%%
        %%% Delete %%%
        %%%%%%%%%%%%%%
        case 'Delete',
          Data=editfun('Delete',Data);
          Data=guide('HandleUpdate',Data);
          
        %%%%%%%%%%%%%%%%%%%%
        %%% HandleUpdate %%%
        %%%%%%%%%%%%%%%%%%%%
        case 'HandleUpdate',
          LocalApply(Data.All.CurrentHandles,Data,MenuFig);        
          LocalSetControls(Data,MenuFig);
          Data=guide('BrowserUpdate',Data);
          
        %%%%%%%%%%%%
        %%% Help %%%
        %%%%%%%%%%%%
        case 'Help',
            helpfun('Menu Editor',Data)
          
        %%%%%%%%%%%%%%%%
        %%% MenuCall %%%
        %%%%%%%%%%%%%%%%        
        case 'MenuCall',
          helpfun(get(gcbo,'Label'),Data);
          
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %%% MoveUp MoveDown Promote Demote %%%
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        case {'MoveUp','MoveDown','Promote','Demote'},
          treemove(varargin{1},Data);
          Data=guide('HandleUpdate',Data);
          
        %%%%%%%%%%%
        %%% New %%%
        %%%%%%%%%%%
        case 'New',
          Data.All.HandleList=uimenu('Parent',Data.All.HandleList, ...
                                     'Label' ,'uimenu'             ...
                                    );
          Data=guide('HandleUpdate',Data);
          
        %%%%%%%%%%%%%%%%%%
        %%% NewContext %%%
        %%%%%%%%%%%%%%%%%%
        case 'NewContext',
          Data.All.HandleList=uicontextmenu('Parent',Data.All.HandleList);
          Data=guide('HandleUpdate',Data);
          
        %%%%%%%%%%%%%
        %%% Paste %%%
        %%%%%%%%%%%%%
        case 'Paste',
          Data=editfun('Paste',Data);
          Data=guide('HandleUpdate',Data);
          
        %%%%%%%%%%%%%%
        %%% Resize %%%
        %%%%%%%%%%%%%%
        case 'Resize',
          LocalResize(Data,MenuFig);        
          
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %%% SelectionUpdate / Update %%%
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        case {'SelectionUpdate','Update'}
          LocalApply(Data.All.CurrentHandles,Data,MenuFig);        
          LocalSetControls(Data,MenuFig);
          Data=guide('BrowserUpdate',Data);
          
        %%%%%%%%%%%%%%%%%%%%%
        %%% ToolsMenuCall %%%
        %%%%%%%%%%%%%%%%%%%%%        
        case 'ToolsMenuCall',
          Data=gnrlfun('ToolsMenuCall',Data);        
          
        %%%%%%%%%%%%%%%%%%%%%%%%
        %%% WindowButtonDown %%%
        %%%%%%%%%%%%%%%%%%%%%%%%
        case 'WindowButtonDown',
          %% Do nothing at this time
          
      end % switch Action
      
    %%%%%%%%%%%%%%%%%%
    %%% Initialize %%%
    %%%%%%%%%%%%%%%%%%
    else,
      Data=LocalInitFig(varargin{1},0,MenuFig);
      
    end % if isstr
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %%% Custom Initialization %%%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  case 2,
    Data=LocalInitFig(varargin{1:2},MenuFig);
    
  %%%%%%%%%%%%%%%%%%
  %%% Initialize %%%
  %%%%%%%%%%%%%%%%%%
  case 0,
    HandleList=gnrlfun('InitializeHandleList',Data);    
    Data=LocalInitFig(HandleList,0,MenuFig);
    
  %%%%%%%%%%%%%
  %%% Error %%%
  %%%%%%%%%%%%%
  otherwise,
    Data=setwatch('off',WatchFlag,Data);
    set(Data.All.DataHandle,'UserData',Data);
    error('Wrong number of input arguments for MENUEDIT');

end % switch nargin


%catch,
%  Data=gnrlfun('CatchErrors',Data);  
%end % try catch

Data=setwatch('off',WatchFlag,Data);
set(Data.All.DataHandle,'UserData',Data);


%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalApply %%%%%
%%%%%%%%%%%%%%%%%%%%%%
function LocalApply(Handles,Data,MenuFig);
% Apply the current settings

if length(Handles)==1,
  switch get(Handles,'Type'),
    case {'root','figure'},
      QLoc=2;    
    case 'uicontextmenu',    
      QLoc=2:3;    
    case 'uimenu',    
      QLoc=1:3;    
    otherwise,    
      QLoc=[];    
  end
  TempHandles=Data.All.HandleList;        
  Data.All.HandleList=Handles;

  if ~isempty(Data.All.HandleList)&~isempty(QLoc),

    QuotedStrings=get(Data.MenuFig.EditHandles(QLoc),{'String'})';
    Properties={'Label','Tag','Callback'};

    StatFlag=0;
    for StrLp=1:length(QLoc),
      Property=Properties{QLoc(StrLp)};    
      Value=quoting('DeQuotify',QuotedStrings{StrLp});
      %Value=QuotedStrings{StrLp};
      EditHandle=Data.MenuFig.EditHandles(StrLp);    
      [UpdateType,Data]=gnrlfun('SafeSet',Data,Property,Value, ...
                                'edit',EditHandle,'Menu');
      if isequal(get(EditHandle,'ForegroundColor'),[1 0 0]),
        StatFlag=1;
      end      

    end        
    if StatFlag,
      statmsg(MenuFig,'Invalid value for the given property.',[1 0 0]);
    else,            
      statmsg(MenuFig,' ');
    end

    Strings=get(Handles(1),Properties(QLoc));
    for StrLp=1:length(QLoc),
      QuotedStrings{StrLp}=quoting('Quotify',Strings{StrLp});
    end      
    set(Data.MenuFig.EditHandles(QLoc),{'String'},QuotedStrings');

    set(Data.All.HandleList,Properties(QLoc),Strings);

  end 

  Data.All.HandleList=TempHandles;
end  

%%%%%%%%%%%%%%%%%%% 
%%% LocalResize %%%
%%%%%%%%%%%%%%%%%%%        
function LocalResize(Data,MenuFig);

StatusHandle=findall(MenuFig,'Tag','Status');
set([Data.MenuFig.EditHandles       Data.MenuFig.TextHandles ...
     Data.MenuFig.FrameHandles      Data.MenuFig.ArrowHandles ...
     Data.MenuFig.BrowserListHandle Data.MenuFig.ButtonHandles MenuFig ...
     StatusHandle], ...
    'Units','points');

TempPos=get(Data.MenuFig.FrameHandles(1),'Position');
HOffset=max(1,TempPos(1));
VOffset=max(1,TempPos(2));

FigPos=get(MenuFig,'Position');
BrowserPos=get(Data.MenuFig.BrowserListHandle,'Position');
StatusPos=get(StatusHandle,'Position');

EditPos=get(Data.MenuFig.EditHandles,{'Position'});
TextPos=get(Data.MenuFig.TextHandles,{'Position'});
FramePos=get(Data.MenuFig.FrameHandles,{'Position'});
ArrowPos=get(Data.MenuFig.ArrowHandles,{'Position'});
ButtonPos=get(Data.MenuFig.ButtonHandles,{'Position'});

EditPos=cat(1,EditPos{:});
TextPos=cat(1,TextPos{:});
FramePos=cat(1,FramePos{:});
ArrowPos=cat(1,ArrowPos{:});
ButtonPos=cat(1,ButtonPos{:});

ButtonWidth=Data.MenuFig.ButtonWidth;
ButtonHeight=Data.MenuFig.ButtonHeight;

ArrowWidth=Data.MenuFig.ArrowWidth;
ArrowHeight=Data.MenuFig.ArrowHeight;

FramePos(1,4)=2*ButtonHeight+3*VOffset;
FramePos(2,2)=sum(FramePos(1,[2 4]))+VOffset; 
StatusPos(1,2)=FramePos(1,2)+VOffset;
StatusPos(1,4)=ButtonHeight;
ButtonPos(2,1)=sum(FramePos(2,[1 3]))-HOffset-Data.MenuFig.ButtonWidth;
ButtonPos(1:2,3)=ButtonWidth;
ButtonPos(3:5,3)=Data.MenuFig.TButtonWidth;
ButtonPos(5,1)=sum(FramePos(2,[1 3]))-HOffset-Data.MenuFig.TButtonWidth;
ButtonPos(:,4)=ButtonHeight;
EditPos(:,4)=ButtonHeight;
TextPos(:,4)=ButtonHeight;
ButtonPos(1:2,2)=sum(StatusPos(1,[2 4]))+VOffset;
ButtonPos(3,2)=FramePos(2,2)+VOffset;
ButtonPos(4:5,2)=sum(ButtonPos(3,[2 4]))+VOffset;
TextPos(end:-1:1,2)=sum(ButtonPos(end,[2 4]))+ ...
                   (0:2)'*(ButtonHeight+VOffset)+VOffset;
EditPos(:,2)=TextPos(:,2);
BrowserPos(2)=sum(EditPos(1,[2 4]))+VOffset;
BrowserPos(4)=max(FigPos(4)-2*VOffset-BrowserPos(1,2),4*ArrowHeight+4*VOffset);


FramePos(2,4)=sum(BrowserPos(1,[2 4]))+VOffset-FramePos(2,2);
ArrowPos(:,1)=FramePos(2,1)+HOffset;
ArrowPos(:,2)=BrowserPos(2)+BrowserPos(4)/2+ ...
             [2*VOffset+ArrowHeight;VOffset
             -VOffset-ArrowHeight;-2*VOffset-2*ArrowHeight];
         
ArrowPos(:,3)=ArrowWidth;
ArrowPos(:,4)=ArrowHeight;
BrowserPos(1)=sum(ArrowPos(1,[1 3]))+HOffset;
BrowserPos(3)=sum(FramePos(2,[1 3]))-BrowserPos(1)-HOffset;


ButtonPos=num2cell(ButtonPos,2);
FramePos=num2cell(FramePos,2);
EditPos=num2cell(EditPos,2);
TextPos=num2cell(TextPos,2);
ArrowPos=num2cell(ArrowPos,2);

set(StatusHandle,'Position',StatusPos);
set(Data.MenuFig.EditHandles,{'Position'},EditPos);
set(Data.MenuFig.TextHandles,{'Position'},TextPos);
set(Data.MenuFig.FrameHandles,{'Position'},FramePos);
set(Data.MenuFig.ArrowHandles,{'Position'},ArrowPos);
set(Data.MenuFig.BrowserListHandle,'Position',BrowserPos);
set(Data.MenuFig.ButtonHandles,{'Position'},ButtonPos);
set(StatusHandle,'Position',StatusPos);

set([Data.MenuFig.EditHandles       Data.MenuFig.TextHandles ...
     Data.MenuFig.FrameHandles      Data.MenuFig.ArrowHandles ...
     Data.MenuFig.BrowserListHandle Data.MenuFig.ButtonHandles ...
     StatusHandle], ...
    'Units','normalized');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalSetControls %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function LocalSetControls(Data,MenuFig)
% Set the movement arrows for a selected object

Movement={'on';'off'};
MoveVal=Movement((Data.MenuFig.Movement==0)+1);
set(Data.MenuFig.ArrowHandles,{'Enable'},MoveVal);

QuotedStrings={'''''','''''',''''''};

if Data.All.GuideFlag,
  StatString=['Figures must be activated to view their menus.'];
else,
  StatString=' ';
end

switch length(Data.All.HandleList),
  case 0,
    Apply='off';Add='off';Edit='off';AddC='off';
    StatString='A figure or menu must be selected.';

  case 1,
    if Data.All.HandleList==0,
      Apply='on';Add='off';Edit='TagOnly';AddC='off';  
      QuotedStrings(2)=get(Data.All.HandleList,{'Tag'});
      StatString='To add a menu, select a figure, uicontextmenu or menu.';

    elseif strcmp(get(Data.All.HandleList,'Type'),'figure'),
      Apply='on';Add='on';Edit='TagOnly';AddC='on';
      QuotedStrings(2)=get(Data.All.HandleList,{'Tag'});

    elseif strcmp(get(Data.All.HandleList,'Type'),'uicontextmenu'),
      Apply='on';Add='on';Edit='TagCB';AddC='off';  
      QuotedStrings(2:3)=get(Data.All.HandleList,{'Tag','Callback'});

    elseif strcmp(get(Data.All.HandleList,'Type'),'uimenu'),
      Apply='on';Add='on';Edit='on';AddC='off';
      QuotedStrings=get(Data.All.HandleList,{'Label','Tag','Callback'});

    else,      
      Apply='off';Add='off';Edit='off';AddC='off';
      StatString='A figure or menu must be selected. ';
      
    end % if
    
  otherwise,
    Apply='off';Add='off';Edit='off';AddC='off';
    
end  

statmsg(MenuFig,StatString);

set(Data.MenuFig.ButtonHandles(4),'Enable',Apply);
set(Data.MenuFig.AddHandles,'Enable',Add);
set(Data.MenuFig.AddContextHandles,'Enable',AddC);
if strcmp(Edit,'on'),
  BackgroundColor=[1 1 1];
else,
  BackgroundColor=[.5 .5 .5];
end  

Strings=cell(size(QuotedStrings));  
for StrLp=1:length(QuotedStrings),
  Strings{StrLp}=quoting('DeQuotify',QuotedStrings{StrLp});      
  QuotedStrings{StrLp}=quoting('Quotify',Strings{StrLp});      
end      

switch Edit,
 case  'TagOnly',
   set(Data.MenuFig.EditHandles([1 3]), ...
       'Enable'                 ,'off'          , ...
       'BackgroundColor'        ,BackgroundColor, ...   
       {'String'}               ,QuotedStrings([1 3])' ...
       );
   set(Data.MenuFig.EditHandles(2), ...
       'Enable'                 ,'on'             , ...
       'BackgroundColor'        ,[1 1 1]          , ...   
       {'String'}               ,QuotedStrings(2)'  ...
       );
 case 'TagCB',
   set(Data.MenuFig.EditHandles(1), ...
       'Enable'                 ,'off'          , ...
       'BackgroundColor'        ,BackgroundColor, ...   
       {'String'}               ,QuotedStrings(1)' ...
       );
   set(Data.MenuFig.EditHandles(2:3), ...
       'Enable'                 ,'on'             , ...
       'BackgroundColor'        ,[1 1 1]          , ...   
       {'String'}               ,QuotedStrings(2:3)'  ...
       );
 otherwise,
   set(Data.MenuFig.EditHandles, ...
       'Enable'                 ,Edit           , ...
       'BackgroundColor'        ,BackgroundColor, ...   
       {'String'}               ,QuotedStrings'   ...
       );
end % switch edit

%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalInitFig %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
function Data=LocalInitFig(HandleList,CustomFlag,MenuFig)

[HandleFlag,Data]=gnrlfun('InitializeHandles',MenuFig,HandleList);
if strcmp(studio,'on'),
  warning(['Menu Editor not currently available.']);
  delete(Data.All.HiddenFigure);
  return
end % if strcmp java
if HandleFlag==2,Data=guide('HandleUpdate',Data);return;end

CustomFlag=0;
CustomList={};

% Set up colors
LightGray=[192 192 192]/256 ; White=[1 1 1];
Black=[0 0 0]               ; MediumGray=[128 128 128]/256;

%%% Set up positions
Offset=3;
FigWidth=275; 

FramePos=[Offset Offset FigWidth-2*Offset 0;Offset Offset FigWidth-2*Offset 0];

ButtonWidth=60;ButtonHeight=20;
TButtonWidth=110;
StatusPos=[FramePos(1,1:2)+Offset FramePos(1,3)-2*Offset ButtonHeight]; 

ButtonPos=[StatusPos(1) sum(StatusPos(1,[2 4]))+Offset ...
           ButtonWidth ButtonHeight
           sum(FramePos(1,[1 3]))-Offset-ButtonWidth  ...
          sum(StatusPos(1,[2 4]))+Offset ButtonWidth ButtonHeight
          ];
ButtonPos=[ButtonPos;ButtonPos(1,:);ButtonPos];
ButtonPos(3,2)=sum(ButtonPos(1,[2 4]))+4*Offset;
ButtonPos(4:5,2)=sum(ButtonPos(3,[2 4]))+Offset;
ButtonPos(3:5,3)=TButtonWidth;
ButtonPos(5,1)=ButtonPos(2,1)+ButtonWidth-TButtonWidth;

TextWidth=(FramePos(1,3)-3*Offset)/4;
TextPos=zeros(3,4);
TextPos(:,1)=ButtonPos(1,1);
TextPos(end:-1:1,2)=sum(ButtonPos(end,[2 4]))+ ...
                   (0:2)'*(ButtonHeight+Offset)+Offset;
TextPos(:,3)=TextWidth;
TextPos(:,4)=ButtonHeight;
EditPos=TextPos;EditPos(:,1)=sum(EditPos(1,[1 3]))+Offset;
EditPos(:,3)=EditPos(:,3)*3;

ArrowWidth=20;ArrowHeight=20;
ArrowPos=[FramePos(2,1)+Offset 0 ArrowWidth ArrowHeight];
ArrowPos=ArrowPos(ones(4,1),:);
          
ListWidth=FramePos(1,3)-2*Offset-sum(ArrowPos(1,[1 3]));  
ListHeight=110;
ListPos=[sum(FramePos(1,[1 3]))-Offset-ListWidth ...
         sum(TextPos(1,[2 4]))+Offset ListWidth ListHeight];

ArrowPos(:,2)=ListPos(2)+ListHeight/2+ ...
             [2*Offset+ArrowHeight;Offset
             -Offset-ArrowHeight;-2*Offset-2*ArrowHeight];

FramePos(1,4)=sum(ButtonPos(1,[2 4]))+Offset;
FramePos(2,2)=sum(FramePos(1,[2 4]))+Offset;
FramePos(2,4)=sum(ListPos(1,[2 4]))+Offset-FramePos(2,2);

FigHeight=sum(FramePos(2,[2 4]))+Offset;
[FigHoriz,FigVert]=gnrlfun('GetFigPos',FigHeight,Data);
FigPos=[FigHoriz FigVert-30 FigWidth FigHeight]; % -30 for menu

%%% Other Info

FrameColor=get(0,'DefaultUicontrolBackgroundColor');         
FontSize=get(0,'FactoryUIControlFontSize');
FrameInfo={'Style'         ,'frame'      ;'BackGroundColor' ,FrameColor
           'Units'         ,'points'     ;'HandleVisibility','off'
           'ForeGroundColor',Black       ;'Interruptible'   ,'off'   
           'HorizontalAlignment','center';'BusyAction'      ,'queue'
          };
FrameTags={'BottomFrame';'TopFrame'};
FrameTips={'';''};

StatusInfo=FrameInfo;
StatusInfo{1,2}='text';StatusInfo{2,2}=[1 1 1];
StatusTag='Status';
StatusTip='';

ButtonInfo=FrameInfo;ButtonInfo{1,2}='pushbutton';
ButtonTags={'Help';'Close';'New Menu';'New Context Menu';'Apply'};
ButtonTips={''    ;''     ;''        ;''                ;''     };
ButtonStrings=ButtonTags;
ButtonCall={'menuedit Help';'menuedit Close';
            'menuedit New';'menuedit NewContext';'menuedit Apply'};

EditInfo=ButtonInfo;
EditInfo{1,2}='edit';EditInfo{2,2}=White;EditInfo{end-1,2}='left';
EditTags={'LabelEdit';'TagEdit';'CallbackEdit'};
EditTips={'';'';''};
EditStrings={'';'';''};
EditCall={'menuedit Label'      ;'menuedit Tag';
          'menuedit Callback'
         };


TextInfo=EditInfo;
TextInfo{1,2}='text';TextInfo{2,2}=FrameColor;TextInfo{end-1,2}='right';
TextTags={'Label';'Tag';'Callback'};
TextTips={'';'';''};
TextStrings=TextTags;

ArrowTags={'Up';'Down';'Promote';'Demote'};
ArrowTips={'Move menu(s) up';'Move menu(s) down'
           'Promote menu(s)';'Demote menu(s)'};
ArrowStrings={'/\';'\/';'<';'>'};
ArrowInfo=[ButtonInfo;{'Enable','off';'FontWeight','bold'}];
ArrowCall={'menuedit MoveUp' ;'menuedit MoveDown'
           'menuedit Promote';'menuedit Demote'};

ListTag={'Menu'};
ListTip={''};
ListCall={'menuedit ButtonDown'};
ListInfo=[ButtonInfo;{'Max',2;'Value',[];'FontName','FixedWidth'}];

ListInfo{1,2}='listbox'    ;
ListInfo{end-4,2}='left';

%% Create uimenus
MenuInfo={'HandleVisibility','off' ;'Interruptible' ,'off'    
          'BusyAction'      ,'queue'
         };
         
MenuLabels={'File'   ;'>Close Menu Editor' ;'Edit'    ;'>Undo'  ;'>New Menu';
            '>New Context Menu';'>Cut^x' ;'>Copy^c';'>Paste^v';'>Delete'   
           };
           
MenuCalls={''              ;'menuedit Close' ;''             ;'menuedit Undo';
           'menuedit New'  ;'menuedit NewContext';
           'menuedit Cut'   ;'menuedit Copy';
           'menuedit Paste';'menuedit Delete'
          };
          
MenuTags={'MenuFile'      ;'MenuFileClose';'MenuEdit'     ;'MenuEditUndo' ;
          'MenuEditAdd'   ;'MenuEditAddContext';
          'MenuEditCut'  ;'MenuEditCopy' ;'MenuEditPaste';
          'MenuEditDelete'
         };          
         
[MenuToolsLabels,MenuToolsCalls,MenuToolsTags]= ...
    gnrlfun('AddToolsMenu','Menu Editor');

[MenuHelpLabels,MenuHelpCalls,MenuHelpTags]= ...
    gnrlfun('AddHelpMenu','Menu Editor');

MenuLabels=[MenuLabels ;MenuToolsLabels; MenuHelpLabels];
MenuTags  =[MenuTags   ;MenuToolsTags;MenuHelpTags];
MenuCalls =[MenuCalls  ;MenuToolsCalls;MenuHelpCalls];
             
MenuLabels=char(MenuLabels);
MenuTags=char(MenuTags);
MenuCalls=char(MenuCalls);

% set up tool
MenuFig=figure(                     ...
              'Color'              ,FrameColor                 , ...
              'CreateFcn'          ,''                         , ...
              'IntegerHandle'      ,'off'                      , ...
              'KeyPressFcn'        ,'menuedit KeyPress'        , ...
              'WindowButtonDownFcn','menuedit WindowButtonDown', ...
              'CloseRequestFcn'    ,'menuedit Close'           , ...
              'MenuBar'            ,'none'                     , ...
              'Name'               ,'Guide Menu Editor'        , ...
              'Tag'                ,'Guide Menu Editor'        , ...
              'NumberTitle'        ,'off'                      , ...
              'Units'              ,'points'                   , ...
              'Position'           ,FigPos                     , ...
              'Resize'             ,'on'                       , ...
              'UserData'           ,[]                         , ...
              'Colormap'           ,[]                         , ...
              'Visible'            ,'off'                        ...
              );

MenuHandles=makemenu(MenuFig,MenuLabels,MenuCalls,MenuTags);
set(MenuHandles,MenuInfo(:,1),MenuInfo(:,2)');
set(MenuHandles(4),'Visible','off');
set(MenuHandles(7:8),'Enable','off');
% Tools Menu Editor
set(MenuHandles(end-10),'Enable','off');

for FLp=1:size(FrameTags,1),
  FrameHandles(FLp)=uicontrol(FrameInfo(:,1),FrameInfo(:,2)'    , ...
                              'Parent'      ,MenuFig            , ...
                              'Position'    ,FramePos(FLp,:)    , ...
                              'Tag'         ,FrameTags{FLp,1}     ...
                              );
end % FLp

StatusHandle=uicontrol(StatusInfo(:,1),StatusInfo(:,2)', ...
                      'Parent'        ,MenuFig         , ...
                      'Position'      ,StatusPos       , ...
                      'TooltipString' ,StatusTip       , ...
                      'Tag'           ,StatusTag         ...
                      );

BrowserListHandle=uicontrol(ListInfo(:,1),ListInfo(:,2)', ...
                           'Parent'      ,MenuFig       , ...
                           'Position'    ,ListPos       , ...
                           'TooltipString',ListTip{1}   , ...
                           'Tag'         ,ListTag{1}    , ...
                           'Callback'    ,ListCall{1}   , ...
                           'String'      ,' '             ...
                           );

for ETLp=1:size(EditTags,1),
  TextHandles(ETLp)=uicontrol(TextInfo(:,1),TextInfo(:,2)'     , ...
                              'Parent'     ,MenuFig            , ...
                              'Position'   ,TextPos(ETLp,:)    , ...
                              'Tag'        ,TextTags{ETLp,1}   , ...
                              'String'     ,TextStrings{ETLp,1}  ...
                              );
  EditHandles(ETLp)=uicontrol(EditInfo(:,1),EditInfo(:,2)'     , ...
                              'Parent'     ,MenuFig            , ...
                              'Position'   ,EditPos(ETLp,:)    , ...
                              'TooltipString',EditTips{ETLp,1} , ...
                              'Tag'        ,EditTags{ETLp,1}   , ...
                              'Callback'   ,EditCall{ETLp,1}   , ...
                              'String'     ,EditStrings{ETLp,1}  ...
                              );
end % ETLp
                       
for BLp=1:size(ButtonTags,1),
  ButtonHandles(BLp)=uicontrol(ButtonInfo(:,1),ButtonInfo(:,2)'    , ...
                              'Parent'        ,MenuFig             , ...
                              'Position'      ,ButtonPos(BLp,:)    , ...
                              'Tag'           ,ButtonTags{BLp,1}   , ...
                              'TooltipString' ,ButtonTips{BLp,1}   , ...
                              'Callback'      ,ButtonCall{BLp,1}   , ...
                              'String'        ,ButtonStrings{BLp,1}  ...
                              );
end % BLp

for ALp=1:size(ArrowTags,1),
  ArrowHandles(ALp)=uicontrol(ArrowInfo(:,1),ArrowInfo(:,2)'   , ...
                             'Parent'      ,MenuFig            , ...
                             'Position'    ,ArrowPos(ALp,:)    , ...
                             'Tag'         ,ArrowTags{ALp,1}   , ...
                             'TooltipString',ArrowTips{ALp,1}  , ...
                             'Callback'    ,ArrowCall{ALp,1}   , ...
                             'String'      ,ArrowStrings{ALp,1}  ...
                             );
end % ALp
set(ArrowHandles(3:4),'FontSize',FontSize+2);

set(MenuFig,'HandleVisibility','off');
set([ArrowHandles BrowserListHandle ButtonHandles  ...
     EditHandles TextHandles FrameHandles StatusHandle], ...
   'Units','normalized');

% Common UserData Settings
if isempty(Data.All.DataHandle),
  Data.All.DataHandle=MenuFig;
end  


Data.All.CustomFlag=CustomFlag;
Data.All.CustomList=CustomList;
    
Data.All.UndoHandle=[Data.All.UndoHandle;MenuFig MenuHandles(4)];
Data.All.CutHandle=[Data.All.CutHandle;MenuFig MenuHandles(7)];
Data.All.CopyHandle=[Data.All.CopyHandle;MenuFig MenuHandles(8)];
Data.All.PasteHandle=[Data.All.PasteHandle;MenuFig MenuHandles(9)];
Data.All.DeleteHandle=[Data.All.DeleteHandle;MenuFig MenuHandles(10)];
Data.All.Browser.Handles=[Data.All.Browser.Handles;BrowserListHandle];
Data.All.Browser.Menu=[Data.All.Browser.Menu;BrowserListHandle];

Data.MenuFig.BrowserListHandle=BrowserListHandle;
Data.MenuFig.ArrowHandles=ArrowHandles;
Data.MenuFig.EditHandles=EditHandles;
Data.MenuFig.ButtonHandles=ButtonHandles;
Data.MenuFig.EditHandles=EditHandles;
Data.MenuFig.TextHandles=TextHandles;
Data.MenuFig.FrameHandles=FrameHandles;
Data.MenuFig.ButtonWidth=ButtonWidth;
Data.MenuFig.TButtonWidth=TButtonWidth;
Data.MenuFig.ButtonHeight=ButtonHeight;
Data.MenuFig.ArrowWidth=ArrowWidth;
Data.MenuFig.ArrowHeight=ArrowHeight;
Data.MenuFig.AddHandles=[MenuHandles(5) ButtonHandles(3)];
Data.MenuFig.AddContextHandles=[MenuHandles(6) ButtonHandles(4)];
Data.MenuFig.Movement=[0 0 0 0];
set(MenuFig,'HandleVisibility','off');

Data=guide('ToolUpdate',Data,MenuFig,'add');

if Data.All.DataHandle~=MenuFig,
  DataFig.All.DataHandle=Data.All.DataHandle;
  set(MenuFig,'UserData',DataFig);
  if length(Data.All.Browser.Complete)<2,% No other tool with a complete 
                                          % browser is open
    Data=objbrwser('CreateInitialMapping',Data);
  else,
    Data=objbrwser('SetBrowserString',Data,'Menu');
  end    
else, % This is the first open tool    
  Data=objbrwser('CreateInitialMapping',Data);
  Data=gnrlfun('TakeGeneralControl',Data);

end

LocalSetControls(Data,MenuFig);
set(MenuFig,'Visible','on');
drawnow
set(MenuFig,'ResizeFcn','menuedit Resize');


if HandleFlag,
  Data=guide('SelectionUpdate',Data,[]);
end
