function newObj = copyobj(hgObj,HGParent)
%SCRIBEHGOBJ/COPYOBJ

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.12 $  $Date: 1998/07/02 19:09:15 $

% call the get method so overloads (figobj/get) have chance...
HG = get(hgObj,'MyHGHandle');

try
   myUIContextMenu = getappdata(HG,'ScribeUIContextMenu');
   UIContextMenuTag = get(myUIContextMenu,'Tag');
   newHG = copyobj(HG,HGParent);
   % the object SHOULD copied implicitly through the appdata
   % (but at the moment is is not)
   UD = getappdata(HG,'ScribeObjectData');
   setappdata(newHG,'ScribeObjectData',UD);
   
   % but not the contextmenu...
   parentType = get(HGParent,'Type');
   switch parentType
   case 'axes'
      newFig = get(HGParent,'Parent');
   case 'figure'
      newFig = HGParent;
   end
   
   newUIContextMenu = findobj(allchild(newFig),'flat',...
           'Tag',UIContextMenuTag);
   if isempty(newUIContextMenu)
      newUIContextMenu = copyobj(myUIContextMenu,newFig);
   end
   if ~isempty(newUIContextMenu)
      setappdata(newHG,'ScribeUIContextMenu',newUIContextMenu(1));
   end
   % return a copy...
   newObj = set(hgObj,'MyHGHandle',newHG);
   
catch
   newObj = [];
end   

