www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/cgwinlist.m

    function cgwinlist(fig)
%CGWINLIST Create submenu for "Window" menu item.
%  CGWINLIST constructs a submenu under the "Window" menu item
%  of the current figure.  The submenu can then be used to
%  change the current figure.  To use WINLIST, the "Window"
%  menu item must have its 'Tag' property set to a string which is
%  common between all of the windows being gathered together
%
%  CGWINLIST(FIG) constructs the "Window" menu and it's subitems for figure
%  FIG.  If a window menu exists then it is used as the base and it's tag is
%  used for searching for other windows.  The default tag is 'cgWinList'.
%
%
%  Example:
%    fig_handle = figure;
%    cgwinlist(fig_handle);  % Initialize the submenu

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

    

if (nargin == 0)
   fig = gcf;
end

h=findobj(allchild(fig),'flat','Type','uimenu','Tag','cgWinList');
if isempty(h)
   h=uimenu('Parent',fig,'Label','&Window','Callback',@i_createsublist,....
      'Tag','cgWinList'); 
   % this menu prevents the first-time failure to show
   uimenu('Parent',h,'Label','init');
end

return




function i_createsublist(h,~)
HiddenVal = get(0,'ShowHiddenHandles');
set(0,'ShowHiddenHandles','on');
tg=get(h,'Tag');
% only get items from figures with similar window menus
figs=get(findobj('Type','uimenu','Tag',tg,'Label','&Window'),{'Parent'});
figs=[figs{:}];
figs=findobj(figs,'flat','Visible','on');
% figs=-sort(-figs);
set(0,'ShowHiddenHandles',HiddenVal);

% get list of text and filter out blanks etc
figName = get(figs,{'Name'});
intHandle = get(figs,{'IntegerHandle'});
numTitle = get(figs,{'NumberTitle'});

N=length(figs);
lbls=cell(N,1);
for k=1:N
   if (strcmp(numTitle{k},'on'))
      if strcmp(intHandle{k},'on'),    
         if (isempty(figName))
            lbls(k)={sprintf('&%d Figure No. %d',N-k+1,figs(k))};
         else          
            lbls(k)= {sprintf('&%d Figure No. %d: %s',N-k+1,figs(k),i_detokenise(figName{k}))};
         end
      else
         if (isempty(figName))
            lbls(k)={sprintf('&%d Figure No. %.8f',N-k+1,figs(k))};

         else
            lbls(k)= {sprintf('&%d Figure No. %.8f: %s',N-k+1,figs(k),i_detokenise(figName{k}))};
         end
      end      
   else
      % numTitle is off.
      if (isempty(figName{k}))
         lbls(k)={''};
      else
         % Filter names for the string ' - ' and only take everything before this
         lbls(k)={sprintf('&%d %s',N-k+1,i_detokenise(figName{k}))};
      end
   end
end

FigsToDo=find(~cellfun('isempty',lbls));

% Create/delete sub-uimenus as needed
N=length(FigsToDo);
winds=get(h,'Children');
if N>length(winds)
   for k=N:-1:(length(winds)+1)
      uimenu('Parent',h);   
   end
   winds=get(h,'Children');
elseif N<length(winds)
   delete(winds(N+1:end));
   winds=winds(1:N);
end

MyPrnt=get(h,'Parent');
for k=FigsToDo(:)'
   set(winds(k),'Label',lbls{k},'Callback',{@iBringToFront,figs(k)});   
   if figs(k)==MyPrnt
      set(winds(k),'Checked','on');
   else
      set(winds(k),'Checked','off');
   end
end




function str=i_detokenise(str)
tok=strfind(str,' - ');
if ~isempty(tok)
   str= str(1:tok(1)-1);
end

function iBringToFront(~,~,hFig)

figure(hFig)