function cbedit(varargin)
%CBEDIT Edit callback.
%   CBEDIT interactively edits callback strings for a selected
%   object. CBEDIT(HandleList) edits the properties for the object(s)
%   in HandleList.  If HandleList is a vector, only similar object 
%   properties are listed.
%
%   See also GUIDE, ALIGN, CTLPANEL, MENUEDIT, PROPEDIT.

%   Loren Dean
%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.69 $ $Date: 1998/07/28 19:54:13 $

CBFig=findall(0,'Tag','Guide Callback Editor');

Data=[];WatchFlag=1;
if ~isempty(CBFig),
  Data=get(CBFig,'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,

%%%%%%%%%%%%%%%%%%%%
%%% nargin Check %%%
%%%%%%%%%%%%%%%%%%%%
switch nargin,
  %%%%%%%%%%%%%%%%%%%%%%%%
  %%% Peform an action %%%
  %%%%%%%%%%%%%%%%%%%%%%%%
  case 1,
    if isstr(varargin{1}),
      Action=varargin{1};
      
      switch Action,
      
        %%%%%%%%%%%%%
        %%% Apply %%%
        %%%%%%%%%%%%%
        case 'Apply',        
          [UpdateType,Data]=LocalSafeApply(Data,0);
          if strcmp(UpdateType,'Property'),          
            Data=guide('PropertyUpdate',Data);
          end
          
        %%%%%%%%%%%%%%%%%%%%%%%%% 
        %%% BrowserButtonDown %%%
        %%%%%%%%%%%%%%%%%%%%%%%%%        
        case 'BrowserButtonDown',
          Data=objbrwser('ButtonDown',Data, ...
                         Data.CBFig.PropTypeHandles(5),CBFig,'Complete');
          
        %%%%%%%%%%%%% 
        %%% Close %%%
        %%%%%%%%%%%%%        
        case 'Close',
          set(Data.All.DataHandle,'UserData',Data);        
          cbedit Apply;        
          Data=get(Data.All.DataHandle,'UserData');          
          Data=guide('ToolUpdate',Data,CBFig,'delete');
          
        %%%%%%%%%%%%%%%%%%%% 
        %%% HandleUpdate %%%
        %%%%%%%%%%%%%%%%%%%%       
        case 'HandleUpdate',
          Data=LocalUpdateInfo(Data);

        %%%%%%%%%%%% 
        %%% Help %%%
        %%%%%%%%%%%%        
        case 'Help',
            helpfun('Callback Editor',Data)
        
        %%%%%%%%%%%%%%%%
        %%% MenuCall %%%
        %%%%%%%%%%%%%%%%        
        case 'MenuCall',
          helpfun(get(gcbo,'Label'),Data);
          
        %%%%%%%%%%%%%%%%%%%%%% 
        %%% PropertyUpdate %%%
        %%%%%%%%%%%%%%%%%%%%%%        
        case 'PropertyUpdate',
          Data=LocalUpdateEdit(Data);
          
        %%%%%%%%%%%%%%
        %%% Resize %%%
        %%%%%%%%%%%%%%
        case 'Resize',
          Data=LocalResize(Data,CBFig);        
          
        %%%%%%%%%%%%%%%%%%%%%%% 
        %%% SelectionUpdate %%%
        %%%%%%%%%%%%%%%%%%%%%%%        
        case 'SelectionUpdate',
          [UpdateType,Data]=LocalSafeApply(Data,1);
          Data=LocalUpdateInfo(Data);

        %%%%%%%%%%%%%%%%%%% 
        %%% ShowBrowser %%%
        %%%%%%%%%%%%%%%%%%%        
        case 'ShowBrowser',
          Data=LocalShowBrowser(Data,CBFig);
          
        %%%%%%%%%%%%%%%%%%%%%
        %%% ToolsMenuCall %%%
        %%%%%%%%%%%%%%%%%%%%%        
        case 'ToolsMenuCall',
          Data=gnrlfun('ToolsMenuCall',Data);        
          
        %%%%%%%%%%%%%% 
        %%% Update %%%
        %%%%%%%%%%%%%%        
        case 'Update',
          Data=LocalUpdateInfo(Data);

        %%%%%%%%%%%%%%%%%%%
        %%% UpdatePopup %%%
        %%%%%%%%%%%%%%%%%%%
        case 'UpdatePopup',
          [UpdateType,Data]=LocalSafeApply(Data,2);
          if strcmp(UpdateType,'Property'),          
            Data=guide('PropertyUpdate',Data);
          end
          Data=LocalUpdateEdit(Data);
          
        %%%%%%%%%%%%%%%%%%%%%%%% 
        %%% WindowButtonDown %%%
        %%%%%%%%%%%%%%%%%%%%%%%%        
        case 'WindowButtonDown',
          % Do nothing at this time
          
      end % switch Action

    %%%%%%%%%%%%%%%%%%
    %%% Initialize %%%
    %%%%%%%%%%%%%%%%%%
    else,
      HandleList=varargin{1};
      Data=LocalInitFig(HandleList,0,CBFig);

    end % if isstr
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %%% Custom Initialization %%%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  case 2,
      Data=LocalInitFig(varargin{1:2},CBFig);
    
  %%%%%%%%%%%%%%%%%%%%%%%%%%
  %%% Initialize to Root %%%
  %%%%%%%%%%%%%%%%%%%%%%%%%%
  case 0,
    HandleList=gnrlfun('InitializeHandleList',Data);    
    Data=LocalInitFig(HandleList,0,CBFig);
    
  %%%%%%%%%%%%%
  %%% Error %%%
  %%%%%%%%%%%%%
  otherwise,
    Data=setwatch('off',WatchFlag,Data);
    set(Data.All.DataHandle,'UserData',Data);
    error('Wrong number of input arguments for CBEDIT');

end % switch nargin  

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

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

%%%%%%%%%%%%%%%%%%% 
%%% LocalResize %%%
%%%%%%%%%%%%%%%%%%%        
function Data=LocalResize(Data,CBFig,varargin);

BrowserHandle=Data.CBFig.ButtonHandles(3);

set([Data.CBFig.PropTypeHandles Data.CBFig.ButtonHandles CBFig], ...
    'Units','points');

FigPos=get(CBFig,'Position');
TempFigPos=FigPos;

TempPos=get(Data.CBFig.ButtonHandles(end),'Position');
HOffset=max(1,TempPos(1));
VOffset=max(1,TempPos(2));

ButtonPos=get(Data.CBFig.ButtonHandles,{'Position'});
ButtonPos=cat(1,ButtonPos{:});

PropTypePos=get(Data.CBFig.PropTypeHandles,{'Position'});
PropTypePos=cat(1,PropTypePos{:});

ButtonWidth=Data.CBFig.ButtonWidth;
ButtonHeight=Data.CBFig.ButtonHeight;
ButtonPos([2 4],1)=ButtonPos(2,1)+ButtonPos(2,3)-ButtonWidth;
ButtonPos(1:4,3)=ButtonWidth;

PropTypeWidth=ButtonWidth;
PropTypePos(3,3)=min(Data.CBFig.ButtonWidth,FigPos(3)-4*HOffset);
PropTypePos(4,3)=PropTypePos(2,3);

ButtonPos(1:4,4)=ButtonHeight;
ButtonPos([3 4],2)=sum(ButtonPos(1,[2 4]))+VOffset;
ButtonPos(5,4)=sum(ButtonPos(4,[2 4]));

PropTypePos(1,2)=sum(ButtonPos(5,[2 4]))+VOffset;
PropTypePos(1,4)=FigPos(4)-VOffset-PropTypePos(1,2);
PropTypePos(2,2)=PropTypePos(1,2)+VOffset;

PropTypePos(3:4,4)=ButtonHeight;
FrameHeight=PropTypePos(1,4);

if get(BrowserHandle,'Value'),
  ListHeight=max((FrameHeight-ButtonHeight-4*VOffset)/2,50);
else,
  ListHeight=max(50,FrameHeight-2*ButtonHeight-4*VOffset);
end  

PropTypePos([2 5],4)=ListHeight;
PropTypePos(3,2)=sum(PropTypePos(2,[2 4]))+VOffset;
PropTypePos(4:5,2)=sum(PropTypePos(3,[2 4]))+VOffset;

if get(BrowserHandle,'Value'),
  PropTypePos(1,4)=sum(PropTypePos(5,[2 4]))+VOffset-PropTypePos(1,2);
else,
  PropTypePos(1,4)=sum(PropTypePos(4,[2 4]))+VOffset-PropTypePos(1,2);
end  


ButtonPos=num2cell(ButtonPos,2);
PropTypePos=num2cell(PropTypePos,2);
 
set(Data.CBFig.ButtonHandles,{'Position'},ButtonPos);
set(Data.CBFig.PropTypeHandles,{'Position'},PropTypePos);

set([Data.CBFig.PropTypeHandles Data.CBFig.ButtonHandles ], ...
    'Units','normalized');

%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalSafeApply %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
function [UpdateType,Data]=LocalSafeApply(Data,PopupFlag)

PropInfo=get(Data.CBFig.PropTypeHandles(3),{'Value';'String'});
if PopupFlag~=2,
  CurrentProp=PropInfo{2}{PropInfo{1}};
else,
  CurrentProp=PropInfo{2}{Data.CBFig.CurrentValue};  
  Data.CBFig.CurrentValue=PropInfo{1};
end  

  
if isempty(deblank(CurrentProp)),
  UpdateType='none';
  
else,  
  CurrentVal=get(Data.CBFig.PropTypeHandles(2),'String');
  if ~iscell(CurrentVal),
    CurrentVal=num2cell(CurrentVal,2);  
  end     
  
  CurrentVal(:,2)={sprintf('\n')};
  CurrentVal{end,2}='';
  CurrentVal=CurrentVal';
  CurrentVal=[CurrentVal{:}];

  % Comma separated list expansion does not preserve data type
  % If CurrentVal is empty then it's equal to [] which is not a legal
  % value to send to a string property.
  if isempty(CurrentVal),
    CurrentVal='';
  end
  
  if isequal(CurrentVal,Data.CBFig.CurrentStringValue),
    UpdateType='none';
    return
  end

  if PopupFlag>=1,
    Temp=Data.All.HandleList;    
    Data.All.HandleList=Data.All.CurrentHandles;
  end

  [UpdateType,Data]= ...
    gnrlfun('SafeSet',Data,CurrentProp,CurrentVal, ...
            'cbedit' ,Data.CBFig.PropTypeHandles(2),'CB');

  if PopupFlag>=1,
    Data.All.HandleList=Temp;
  end
  
end % if ~isempty

%%%%%%%%%%%%%%%%%%%%%%%% 
%%% LocalShowBrowser %%%
%%%%%%%%%%%%%%%%%%%%%%%%        
function Data=LocalShowBrowser(Data,CBFig);
% Toggle object browser display on/off

BrowserHandle=findall(CBFig,'Tag','Show Object Browser');
BrowserMenuHandle=findall(CBFig,'Tag','MenuOptionsShowObjectBrowser');

if isequal(gcbo,BrowserMenuHandle),
  CheckFlag=strcmp(get(BrowserMenuHandle,'Checked'),'off');
else,
  CheckFlag=get(BrowserHandle,'Value');
end


ObjHandle=Data.CBFig.PropTypeHandles(4);

if CheckFlag,
  BrowseFlag=1;  
  Vis='on';  
  ObjTagVis='off';  
  Checked='on';  
else,
  BrowseFlag=0;
  Vis='off';  
  ObjTagVis='on';  
  Checked='off';  
end % if strcmp  

set(BrowserHandle,'Value',BrowseFlag);
set(BrowserMenuHandle,'Checked',Checked);

set(Data.CBFig.PropTypeHandles(5),'Visible',Vis);
set(ObjHandle,'Visible',ObjTagVis);

Data=LocalResize(Data,CBFig,'menu');

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalUpdateEdit %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Data=LocalUpdateEdit(Data)

CurPropInfo=get(Data.CBFig.PropTypeHandles(3),{'String','Value'});
if isempty(deblank(CurPropInfo{1}{1})),
  CurrentVal='';ForeColor=[0 0 0];
else,  
  CurrentProp=CurPropInfo{1}(CurPropInfo{2});
  CurrentVal=gnrlfun('GetSafeVal',Data,CurrentProp,'CB');
  CurFlag=gnrlfun('GetSameValFlag',CurrentVal);
  if CurFlag,ForeColor=[0 0 0];else,ForeColor=[.5 .5 .5];end
  if ~isempty(CurrentVal),  
    CurrentVal=CurrentVal{1};
  end    
end % if isempty  

set(Data.CBFig.PropTypeHandles(2), ...
   'ForegroundColor',ForeColor , ...
   'String'         ,CurrentVal  ...
   );   

Data.CBFig.CurrentStringValue=CurrentVal;

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalUpdateInfo %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Data=LocalUpdateInfo(Data)

Value=get(Data.CBFig.PropTypeHandles(3),'Value');
if isempty(Data.CBFig.PropList),
  CurProp={' '};
else,  
  CurProp=Data.CBFig.PropList(Value);
end  

switch length(Data.All.HandleList),
  case 0,
    Data.CBFig.PropList={};
    NewType='';
    
  case 1,
    % If it's a uicontrol or uimenu make sure that callback is the
    % first property listed    
    NewType=get(Data.All.HandleList,'Type');    
    if strcmp(get(Data.All.HandleList,'Type'),'uicontrol') | ...
       strcmp(get(Data.All.HandleList,'Type'),'uicontextmenu') | ...
       strcmp(get(Data.All.HandleList,'Type'),'uimenu'),
      Data.CBFig.PropList=[
          getfield(Data.CBFig.CBList,NewType)
          Data.CBFig.CBList.all
                          ];
    else,       
      Data.CBFig.PropList=[
          Data.CBFig.CBList.all
          getfield(Data.CBFig.CBList,NewType)
                          ];
    end % if strcmp
    
  otherwise,
    CurrentVal=get(Data.All.HandleList,{'Type'});
    ValFlag=gnrlfun('GetSameValFlag',CurrentVal);
    Data.CBFig.PropList=Data.CBFig.CBList.all;
    NewType='all';    
    if ValFlag,    
      % If it's a uicontrol or uimenu make sure that callback is the
      % first property listed    
      if any(strcmp(get(Data.All.HandleList,'Type'),'uicontrol') | ... 
             strcmp(get(Data.All.HandleList,'Type'),'uicontextmenu')| ...
             strcmp(get(Data.All.HandleList,'Type'),'uimenu')),
        Data.CBFig.PropList=[
            getfield(Data.CBFig.CBList,get(Data.All.HandleList(1),'Type'))
            Data.CBFig.PropList
                            ];
      else,       
        Data.CBFig.PropList=[
            Data.CBFig.PropList
            getfield(Data.CBFig.CBList,get(Data.All.HandleList(1),'Type'))
                            ];
      end % if strcmp
    end % if ValFlag
end % switch  

if isequal(Data.CBFig.OldType,NewType),
  Value=strmatch(CurProp,Data.CBFig.PropList);  
else,  
  Data.CBFig.OldType=NewType;  
  Value=1;  
end

if isempty(Value),Value=1;end
if isempty(Data.CBFig.PropList),
  Value=1;  
  set(Data.CBFig.PropTypeHandles(3), ...
     'String', {' '}, ...
     'Value' ,Value , ...
     'Enable','off'   ...
     );
  set(Data.CBFig.PropTypeHandles(2),'Enable','off');  
  set(Data.CBFig.ButtonHandles(1),'Enable','off');  
else,
  set(Data.CBFig.PropTypeHandles(3), ...
     'String'         ,Data.CBFig.PropList, ...
     'Value'          ,Value              , ...
     'Enable'         ,'on'                 ...
     );
  set(Data.CBFig.PropTypeHandles(2),'Enable','on');  
  set(Data.CBFig.ButtonHandles(1),'Enable','on');  
end

Data.CBFig.CurrentValue=Value;
Data=LocalUpdateEdit(Data);

%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% LocalInitFig %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
function Data=LocalInitFig(HandleList,CustomList,CBFig)

[HandleFlag,Data]=gnrlfun('InitializeHandles',CBFig,HandleList);

if strcmp(studio,'on'),
  %make call to Java
  % Try to open the inspector and object browser
  try
      java('com/mathworks/ide/inspector/Inspector', ...
          'activateInspector','()V');
      java('com/mathworks/ide/objectbrowser/ObjectBrowser', ...
          'activateObjectBrowser','()V');
  catch
    % Failed. Bail
    error('Failed to open Inspector and Object Browser');
  end
  delete(Data.All.HiddenFigure);
  return
end % if strcmp java


if HandleFlag==2,Data=guide('HandleUpdate',Data);return;end

%if ~isstruct(CustomList),
%  CustomFlag=CustomList;
%  CustomList=[];  
%  if CustomFlag,
%    CustomList=getpref('Guide','CustomList');
%  end
%else,
%  CustomFlag=1;  
%end % if ~isstruct
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=300;  FigHeight=200;
[FigHoriz,FigVert]=gnrlfun('GetFigPos',FigHeight,Data);
FigPos=[FigHoriz FigVert FigWidth FigHeight];

ButtonWidth=120;
ButtonHeight=25;
ButtonPos=zeros(5,4);
ButtonPos(5,:)=[0 0 FigWidth-2*Offset 0]; 
ButtonPos(1:2,:)= ...
  [Offset                            Offset 150 ButtonHeight
   ButtonPos(5,3)-Offset-ButtonWidth Offset ButtonWidth ButtonHeight
  ];
ButtonPos(3:4,:)=ButtonPos(1:2,:);
ButtonPos(3:4,2)=ButtonPos(3:4,2)+ButtonHeight+Offset;
ButtonPos(5,4)=sum(ButtonPos(4,[2 4]))+Offset;
ButtonPos(:,1:2)=ButtonPos(:,1:2)+Offset;

TextHeight=ButtonHeight;
PropTypePos=zeros(5,4);
PropTypePos(1,:)=[ButtonPos(5,1) sum(ButtonPos(5,[2 4]))+Offset  ...
                  ButtonPos(5,3) FigHeight-2*Offset-sum(ButtonPos(5,[2 4]))];
PropTypePos(2,:)=[PropTypePos(1,1:2)+Offset PropTypePos(1,3)-2*Offset ...
                  PropTypePos(1,4)-4*Offset-TextHeight-ButtonHeight];
PropTypePos(3,:)=[PropTypePos(2,1) sum(PropTypePos(2,[2 4]))+Offset ...
                  ButtonWidth ButtonHeight];
PropTypePos(4,:)=PropTypePos(2,:);
PropTypePos(5,:)=PropTypePos(2,:); 
PropTypePos(4:5,2)=sum(PropTypePos(3,[2 4]))+Offset;
PropTypePos(4,4)=TextHeight;

%%% Other Info
ButtonTags={'Apply';'Close';'Show Object Browser';'Help';'ButtonFrame'};
ButtonTips={'';'';'';'';''};

ButtonStrings=ButtonTags;ButtonStrings{3}='Show Object Browser';

FontSize=get(0,'FactoryUIControlFontSize');

ButtonInfo={'BackGroundColor',LightGray
            'Units'          ,'points'     ;'HandleVisibility','off'
            'FontSize'       ,FontSize
            'FontUnits'      ,'points'     ;'ForeGroundColor',Black
            'Interruptible'  ,'off'        ;'HorizontalAlignment','center' 
            'BusyAction' ,'queue'            
           };
ButtonCall={'cbedit Apply';'cbedit Close';'cbedit ShowBrowser'
            'cbedit Help';''};
ButtonStyle={'pushbutton';'pushbutton';'checkbox';'pushbutton';'frame'};



PropTypeInfo=[ButtonInfo(2:end,:);{'Max',2}];
PropTypeInfo{end-2,2}='left';
PropTypeTags={'PropTypeFrame';'Edit'     ;'Property'
              'Object Type & Tag Information';'Complete'};
PropTypeTips={'';'';'';'';''};

PropTypeStrings(1:5,1)={' '};
PropTypeCalls={'';'cbedit Apply';'cbedit UpdatePopup';''
               'cbedit BrowserButtonDown'
              };
PropTypeStrings{3}={' '};
PropTypeStyle={'frame';'edit';'popupmenu';'text';'listbox'};

FigColor=get(0,'defaultuicontrolbackgroundcolor');
PropTypeColor=[FigColor;White;FigColor;FigColor;FigColor];

MenuBar='none';
MenuInfo={'HandleVisibility','off'        ;'Interruptible' ,'off'    
          'BusyAction'      ,'queue'
         };
           
%%% Create the menu options
MenuFileLabels={'File'               
               '>Close Callback Editor'
               };
MenuFileTags={'MenuFile'
              'MenuFileCloseTool'
             };
             
MenuFileCalls=[{''};{'cbedit Close'}];

MenuOptionsLabels={'Options'               
               '>Show Object Browser'
               };
MenuOptionsTags={'MenuOptions'
              'MenuOptionsShowObjectBrowser'
             };
             
MenuOptionsCalls={'';'cbedit ShowBrowser'};

[MenuToolsLabels,MenuToolsCalls,MenuToolsTags]= ...
    gnrlfun('AddToolsMenu','Callback Editor');

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

MenuInfoLabels=[MenuFileLabels ;MenuOptionsLabels; 
                MenuToolsLabels;MenuHelpLabels];
              
MenuInfoTags  =[MenuFileTags   ;MenuOptionsTags  ;
                MenuToolsTags  ;MenuHelpTags];
              
MenuInfoCalls =[MenuFileCalls  ;MenuOptionsCalls ; 
                MenuToolsCalls ;MenuHelpCalls];
             
MenuInfoLabels=char(MenuInfoLabels);
MenuInfoTags=char(MenuInfoTags);
MenuInfoCalls=char(MenuInfoCalls);
   
% set up tool
drawnow
CBFig=figure(                     ...
            'Color'              ,FigColor                 , ...
            'CreateFcn'          ,''                       , ...
            'IntegerHandle'      ,'off'                    , ...
            'WindowButtonDownFcn','cbedit WindowButtonDown', ...
            'CloseRequestFcn'    ,'cbedit Close'           , ...
            'MenuBar'            ,MenuBar                  , ...
            'Name'               ,'Guide Callback Editor'  , ...
            'Tag'                ,'Guide Callback Editor'  , ...
            'NumberTitle'        ,'off'                    , ...
            'Units'              ,'points'                 , ...
            'Position'           ,FigPos                   , ...
            'Resize'             ,'on'                     , ...
            'UserData'           ,[]                       , ...
            'Colormap'           ,[]                       , ...
            'Visible'            ,'off'                      ...
            );

MenuHandles=makemenu(CBFig,MenuInfoLabels,MenuInfoCalls,MenuInfoTags);
set(MenuHandles,MenuInfo(:,1),MenuInfo(:,2)');
set(MenuHandles(end-12),'Enable','off');

for BLp=1:size(ButtonTags,1),
  ButtonHandles(BLp)=uicontrol('Parent'        ,CBFig             , ...
                               'Style'         ,ButtonStyle{BLp}  , ...
                               ButtonInfo(:,1) ,ButtonInfo(:,2)'  , ...
                               'Position'      ,ButtonPos(BLp,:)  , ...
                               'Tag'           ,ButtonTags{BLp}   , ...
                               'TooltipString' ,ButtonTips{BLp}   , ...
                               'Callback'      ,ButtonCall{BLp}   , ...
                               'String'        ,ButtonStrings{BLp}  ...
                               );
end % BLp
set(ButtonHandles(3),'HorizontalAlignment','left')
for PLp=1:size(PropTypeTags,1),
  PropTypeHandles(PLp)=uicontrol('Parent'         ,CBFig               , ...
                                 'Style'          ,PropTypeStyle{PLp}  , ...
                                 PropTypeInfo(:,1),PropTypeInfo(:,2)'  , ...
                                 'BackgroundColor',PropTypeColor(PLp,:), ...
                                 'Position'       ,PropTypePos(PLp,:)  , ...
                                 'Tag'            ,PropTypeTags{PLp}   , ...
                                 'TooltipString'  ,PropTypeTips{PLp}   , ...
                                 'Callback'       ,PropTypeCalls{PLp}  , ...
                                 'String'         ,PropTypeStrings{PLp}  ...
                                 );
end % PLp
set([ButtonHandles PropTypeHandles],'Units','normalized');
set(PropTypeHandles(5),'Visible','off','FontName','FixedWidth');

set(CBFig,'Children',[ButtonHandles(1:end-1) PropTypeHandles(2:end) ...
                      ButtonHandles(end)     PropTypeHandles(1)     ...
                      MenuHandles([12 5 3 1])']);

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

Data.All.CustomFlag=CustomFlag;
Data.All.CustomList=CustomList;

Data.All.Browser.Handles=[Data.All.Browser.Handles;PropTypeHandles(5)];
Data.All.Browser.Complete=[Data.All.Browser.Complete;PropTypeHandles(5)];
Data.All.ObjTypeTag=[Data.All.ObjTypeTag PropTypeHandles(4)];

Data.CBFig.ButtonWidth=ButtonWidth;
Data.CBFig.ButtonHeight=ButtonHeight;
Data.CBFig.ButtonHandles=ButtonHandles;
Data.CBFig.CBList.all={'ButtonDownFcn';'CreateFcn';'DeleteFcn'};
Data.CBFig.CBList.root={};
Data.CBFig.CBList.figure={'CloseRequestFcn'      ;'KeyPressFcn'
                          'ResizeFcn'            ;'WindowButtonDownFcn'
                          'WindowButtonMotionFcn';'WindowButtonUpFcn'
                         };
                   
Data.CBFig.CBList.uicontextmenu={'Callback'};
Data.CBFig.CBList.uimenu={'Callback'};
Data.CBFig.CBList.uicontrol={'Callback'};
Data.CBFig.CBList.axes={};    
Data.CBFig.CBList.line={};
Data.CBFig.CBList.text={};
Data.CBFig.CBList.light={};
Data.CBFig.CBList.image={};
Data.CBFig.CBList.patch={};
Data.CBFig.CBList.surface={};
Data.CBFig.CBList.rectangle={};

Data.CBFig.CurrentValue=[];
Data.CBFig.OldType='';
Data.CBFig.PropList={};
Data.CBFig.PropTypeHandles=PropTypeHandles;
Data.CBFig.CurrentStringValue='';

set(CBFig,'HandleVisibility','off');

Data=guide('ToolUpdate',Data,CBFig,'add');
  
if Data.All.DataHandle~=CBFig,
  DataFig.All.DataHandle=Data.All.DataHandle;
  set(CBFig,'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,'Complete');
  end    
  
else, % This is the first open tool  
  Data=objbrwser('CreateInitialMapping',Data);
  Data=gnrlfun('TakeGeneralControl',Data);
  
end

set(CBFig,'Visible','on');
drawnow
set(CBFig,'ResizeFcn','cbedit Resize');


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