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

    function newobj=copyobj(obj,fig)
% COPYOBJ  Create a copy of an object in a new figure
%
%   NEWOBJ=COPYOBJ(OBJ,FIG) creates a replica of the object
%   OBJ in the figure FIG.
%

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


% copy data structure
newobj=obj;
fud=get(obj.frame.handle,'UserData');
newfud=fud;

%copy background and assorted extra ui elements
newobj.frame.handle=copyobj(obj.frame.handle,fig);
newfud.frame.handle=newobj.frame.handle;
newobj.hslider.handle=copyobj(obj.hslider.handle,fig);
newfud.hslider.handle=newobj.hslider.handle;
newobj.vslider.handle=copyobj(obj.vslider.handle,fig);
newfud.vslider.handle=newobj.vslider.handle;
newobj.dslider.handle=copyobj(obj.dslider.handle,fig);
newfud.dslider.handle=newobj.dslider.handle;
newfud.objecthandle=copyobj(fud.objecthandle,fig);

newobj.parent=fig;
newfud.parent=fig;

% link to new object
builtin('set',newfud.objecthandle,'userdata',newobj);

% set slider callbacks
set(newobj.vslider.handle,'Callback',...
    {@mbcgui.util.legacycallback,@vsliderscroll,newfud.objecthandle});
set(newobj.hslider.handle,'Callback',...
    {@mbcgui.util.legacycallback,@hsliderscroll,newfud.objecthandle});

% move all cell handles across:
% need to move, change callback strings, userdata parent field,
% and uiprops structure

% set up big array of handles
hndls=zeros(fud.rows.number,fud.cols.number);
hndls(1:fud.rows.fixed,1:fud.cols.fixed)=fud.cells.fcornerhandles;
hndls(fud.rows.fixed+1:end,1:fud.cols.fixed)=fud.cells.flefthandles;
hndls(1:fud.rows.fixed,fud.cols.fixed+1:end)=fud.cells.ftophandles;
sz=size(fud.cells.shandles);
hndls(fud.rows.fixed+1:fud.rows.fixed+sz(1),fud.cols.fixed+1:fud.cols.fixed+sz(2))=fud.cells.shandles;

% filter zeros and text items: these indexes are useful later though
hind=(hndls~=0);
hindtxt=false(size(hind));
if ~isempty(fud.cells.ctype)
   hindtxt(1:size(fud.cells.ctype,1),1:size(fud.cells.ctype,2))=(fud.cells.ctype==1);
end
h=hndls(hind & ~hindtxt);
if ~isempty(h)
   h = mbcgui.hgclassesutil.toHandle(h);
   hg=copyobj(h,fig);
   % redo callbacks
   cbcells=get(hg,{'Callback'});
   cbcells = cellfun(@(c) iUpdateCallback(c, newfud.objecthandle), cbcells, 'UniformOutput', false);
   set(hg,{'Callback'},cbcells);
   % userdata
   uds=get(hg,'UserData');
   for n=length(uds):-1:1
      uds{n}.parent=newobj.frame.handle;
   end
   set(hg,{'UserData'},uds);
   % put h back into hndls
   hndls(hind & ~hindtxt)=hg;
end

% sort out text object references
txth=findobj(fud.objecthandle,'Type','text');
newtxth=findobj(newfud.objecthandle,'Type','text');

% assume child object handles maintain the same order in the copy
h=hndls(hindtxt);
% find index vector linking txth to h, then apply to newtxth
[tmp,i]=unique(txth);
[tmp,i2]=unique(h);
[tmp,i2]=sort(i2);
newtxth=newtxth(i(i2));
hndls(hindtxt)=newtxth;

% resplit hndls into new object
newfud.cells.fcornerhandles=hndls(1:fud.rows.fixed,1:fud.cols.fixed);
newfud.cells.flefthandles=hndls(fud.rows.fixed+1:end,1:fud.cols.fixed);
newfud.cells.ftophandles=hndls(1:fud.rows.fixed,fud.cols.fixed+1:end);

newfud.cells.shandles=hndls(fud.rows.fixed+1:fud.rows.fixed+sz(1),fud.cols.fixed+1:fud.cols.fixed+sz(2));

% do uiprops callback strings
fud.cells.defaultuip.callback = iUpdateCallback(fud.cells.defaultuip.callback, newfud.objecthandle);

for n=1:length(newfud.cells.uiprops)
   if isfield(newfud.cells.uiprops{n},'callback')
      newfud.cells.uiprops{n}.callback=iUpdateCallback(newfud.cells.uiprops{n}.callback, newfud.objecthandle);
   end
end

set(newobj.frame.handle,'UserData',newfud);


% Insert a new object handle into a legacy callback
function c = iUpdateCallback(c, NewHandle)
c{3} = NewHandle;