function out = clipboard(whatToDo, stuff)
%CLIPBOARD Copy and paste strings to and from system clipboard.
%
% CLIPBOARD('copy', STUFF) Sets the clipboard contents to STUFF.  If STUFF
% is not a char array, MAT2STR is used to convert it to a string.
%
% STR = CLIPBOARD('paste') Returns the current contents of the clipboard as
% a string or '' if the current clipboard cannot be converted to a string.
%
% DATA = CLIPBOARD('pastespecial') Returns the current contents of the
% clipboard as an array using UIIMPORT.
%
% Requires an active X display on Unix and Java elsewhere.
% 
% See also LOAD, FILEFORMATS, UIIMPORT

% Copyright 1984-2000 The MathWorks, Inc.
% $Revision: 1.11 $  $Date: 2000/06/12 18:41:58 $

if isunix & isempty(get(0,'DefaultFigureXDisplay'))
    error('There is no X display set.')
end

if ~isunix & ~usejava('awt') 
    error('Java is not currently enabled.  See JAVA for more information.');
end

error(nargchk(1,2,nargin));
error(nargchk(0,1,nargout));

if strcmpi(whatToDo, 'copy')
    if nargin == 1
        error('2 args required for copy')
    end

    if isempty(stuff)
        return;
    end
    
    % this should error out if something is hokey
    if ~ischar(stuff)
        stuff = mat2str(stuff);
    end

    if isunix
        % use system_dependent
        system_dependent('xsetselection',stuff);
    else
        % create ClipboardHandler object
        cpobj = com.mathworks.page.utils.ClipboardHandler;

        % do the copy
        cpobj.copy(stuff);
    end
elseif strcmpi(whatToDo, 'paste')
    if isunix
        % use system_dependent
        out = system_dependent('xgetselection');
    else
        % create ClipboardHandler object
        cpobj = com.mathworks.page.utils.ClipboardHandler;
    
        % do the paste
        out = char(cpobj.paste);
    
        % check for error (possibly false errors???)
        if strcmp(out, '!ERR#')
            % exit quietly and let caller manage empty result
            % error('Unable to convert clipboard contents to string.');
            out = '';
        end
    end
elseif strcmpi(whatToDo, 'pastespecial')
    % given output arg, get results from uiimport and hand off
    % otherwise, let uiimport think it was called from the caller's wksp
    if nargout
        out = uiimport('-pastespecial');
    else
        evalin('caller', 'uiimport(''-pastespecial'');');
    end
else
    error('Usage: CLIPBOARD(''copy'',stuffToCopy) or OUT = CLIPBOARD(''paste'')')
end    
