function position = getpixelposition(h,recursive)
% GETPIXELPOSITION Get position HG object in pixels.

% if recursive is true, the returned position is relative to the parent
% figure of H. Otherwise, it is relative to the parent of H.

% Copyright 1984-2004 The MathWorks, Inc.
% $Revision: 1.1.6.3 $ $Date: 2004/08/16 01:52:23 $

  if nargin < 2
      recursive = false;
  end

  old_u = get(h,'Units');
  set(h,'Units','pixels');
  wasError = false;
  try
      position = get(h,'Position');
  catch
      wasError = true;
  end
  set(h,'Units',old_u);  
  if wasError
      rethrow(lasterror);
  end

  parent = get(h,'Parent');
  if recursive && ~isa(handle(parent),'figure')
     parentPos = getpixelposition(parent, recursive); 
     position = position + [parentPos(1) parentPos(2) 0 0];
  end
  
end

