www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/xregmenutool.m

    function varargout= xregmenutool(flag,varargin)
% XREGMENUTOOL - Vectorised uimenu tool  
% varargout=xregmenutool(flag,varargin)
%
% Description: Creates uimenus  
%                             
% Inputs       Action - string
%              various depending on Action
% Returns      depend on actions
%              returns uimenu handle/s on creation and find
% Actions
%    Create        create uimenus
%    Find          find handle for uimenu based on position
%    Set           set uimenu properties.
% hm= xregmenutool('Create',MenuPos,Prop1,Value1,Prop2,Value2)
% hm= xregmenutool('Find',MenuPos)
% xregmenutool('Set',MenuPos,Prop1,Value1,Prop2,Value2)
%
% MenuPos can be specified by either a handle (Figure or Uimenu) or a menu position 
% index e.g. 1 , 2 , 3 or [1 2 3]. 

%  Copyright 2000-2013 The MathWorks, Inc. and Ford Global Technologies, Inc.



switch lower(flag)
case 'create'
   if  ( ~isgraphics(varargin{1}) )
      error(message('mbc:xreg_menutool:InvalidArgument'));
   end
   h=CreateMenus(varargin{:});
   if nargout==1
      varargout{1}= h;
   end
case 'set'
   if isgraphics(varargin{1}) 
      SetMenu(varargin{:});
   else
      error(message('mbc:xreg_menutool:InvalidArgument1'));
   end
case 'find'
   h=FindMenu(varargin{:});
   if nargout==1
      varargout{1}=h;
   else
      error(message('mbc:xreg_menutool:TooManyOutputs'));
   end
   
end


% Set Menu Properties
function SetMenu(h,varargin)

pos=1;
while isa(varargin{pos},'double')
   pos=pos+1;
end
if pos==1 && ~isgraphics(h,'uimenu')
   error(message('mbc:xreg_menutool:InvalidArgument2'));
end
if pos>1
   h= FindMenu(h(1),varargin{1:pos-1});
end

if (length(varargin)-pos-1)/2 ~= fix((length(varargin)-pos-1)/2)
   error(message('mbc:xreg_menutool:InvalidArgument3'));
end

SetPropertyList = fieldnames(set(h));
Parse_Properties(SetPropertyList,varargin{pos:2:end});
set(h,varargin{pos:end});   


function hm=FindMenu(parent,varargin)

pos= [varargin{:}];
   
for i=pos
   hm=findobj(get(parent,'Children'),'flat','Type','uimenu','Position',i);
   parent=hm;
end

function Parse_Properties(PropertyList,varargin)

% Parse Properties
for i= 1:length(varargin)
   Property= varargin{i};
   I=find( strncmpi( Property,PropertyList,length(Property) ) );
   if isempty(I)
      error(message('mbc:xreg_menutool:InvalidPropertyName', Property));
   elseif numel(I)>1
       error(message('mbc:xreg_menutool:AmbiguousPropertyName', Property));
   end
end


function h=CreateMenus(parent,varargin)

pos=1;
while isa(varargin{pos},'double')
   pos=pos+1;
end
if pos>1
   parent= FindMenu(parent,varargin{1:pos-1});
end
hm=uimenu('Parent',parent);
SetPropertyList = fieldnames(set(hm));
delete(hm);

PropertyList=varargin(pos:2:end);
Values=varargin(pos+1:2:end);

if (length(Values) ~= length(PropertyList))
   error(message('mbc:xreg_menutool:InvalidArgument4'));
end

Parse_Properties(SetPropertyList,PropertyList{:});

NumMenus=1;
for i=1:length(Values)
   NumMenus=max(NumMenus,numel(Values{i}) );
end

PropertyValues=cell(NumMenus,length(Values));
for i=1:length(Values);
   if iscell(Values{i})
      [PropertyValues{:,i}]=deal(Values{i}{:});
   else
      [PropertyValues{:,i}]=deal(Values{i});
   end
end

for i= 1:NumMenus
    pvs = [PropertyList ; PropertyValues(i,:)];
    h(i)=uimenu('Parent',parent,'Position',i,pvs{:});
end