function pj = validate( pj )
%VALIDATE Method to check state of PrintJob object.
%   Values of PrintJob object class variables are checked for consistency.
%   Errors out if it finds bad combinations. Fills in missing data with
%   defaults.
%
%   Ex:
%      pj = VALIDATE( pj );
%
%   See also PRINT, PRINTOPT, INPUTCHECK.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.11 $  $Date: 1998/10/22 22:23:06 $

if ~(nargin >= 1) | ~isa(pj,'printjob')
    error('Requires PrintJob object.')
end

%If no window requested, and none to act as default, error out.
if isempty( pj.Handles )
    %Get current Figure; but don't create one, like gcf would, if none yet.
    h = findobj(get(0,'children'),'flat','type','figure');
    if isempty( h )
       error( 'No Figure to print.' )
    else
        pj.Handles = { h(1) };
    end
else
    if ~iscell( pj.Handles )
        error( 'Handles must be in a cell array.' )
    end
    
    if length(pj.Handles) > 1 
        if feature('NewPrintAPI')
            if strcmp(pj.DriverClass,'EP')
                error( 'EPS supports single page output only.' )
            end
            
            if pj.PostScriptPreview
                error( 'Can not have preview of a multi-page output.' )
            end
        else
            error( 'Can only print one page at a time.' )
        end
    end
end

if pj.PostScriptPreview & ~strcmp(pj.DriverClass,'EP')
    error( 'TIFF previews allowed only with EPS output.' )
end

%If no device given, use default from PRINTOPT
if isempty( pj.Driver )
    %Use method to validate default and set related class variables
    wasError = 0;
    try
        pj = inputcheck( pj, pj.DefaultDevice );
        pj.DriverColorSet = 0;
    catch
        wasError = 1;
    end
    if wasError | isempty( pj.Driver )
        error(['PRINTOPT specifies an unknown device type '''...
                pj.DefaultDevice '''.'])
    end
end

if strcmp(pj.DriverClass, 'MW' ) 
    if ~strcmp( computer, 'PCWIN' )
        error( ['Can o['Can only use Windows driver ''' pj.Driver ''' on a Windows machine.'])
    end
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%  HACK    HACK   HACK   HACK   HACK   HACK   %%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % If user specifies a filename while device is -dwin
    % or -dwinc, either because the user gave that device or, more
    % likely, it's the default, and since the filename is useless
    % with Windows driver anyway, we'll assume the user really wants
    % a PostScript file. This is because 'print foo' is easier
    % to type then 'print -dps foo' and probably more commonly
    % meant if a filename is given. Unless of course the user asked
    % for the Print Dialog with the -v flag, then s/he really meant it.
    if (~isempty(pj.FileName) & ~pj.Verbose ) ...
            & ( strcmp(pj.Driver, 'win') | strcmp(pj.Driver, 'winc') )
        if pj.DriverColor
            pj.Driver = 'psc';
        else
            pj.Driver = 'ps';
        end
        pj.DriverExt = 'ps';
        pj.DriverClass = 'PS';
    end
    %%%%%%%%%%%%%%%%%%%%%%%%     END HACK     %%%%%%%%%%%%%%%%%%%%%%%%%
end

if pj.XTerminalMode 
    %Can't produce TIFF or JPEG files in terminal mode because we can't do ZBuffer.
    if strcmp(pj.DriverClass, 'IM' )
        error( ['Cannot produce ' upper(pj.Driver) ' files in terminal emulation mode.'] )
    end
end

if strcmp( pj.Driver, 'mfile' ) & any( isslhandle( [pj.Handles{:} ] ) )
    error( 'The -dmfile device option cannot be used with Simulink systems.' )
end

if strcmp( pj.Driver, 'mfile' ) & isempty( pj.FileName )
    error( 'Filename required for saving Figure to M-file.' )
end

if strcmp( pj.Driver, 'mfile' )
    locMakeSafeForDmfile( pj.Handles )
end

%GhostScript produced image formats needs -loose PS files
if ( strcmp(pj.DriverClass, 'GS') & pj.DriverExport )
    pj.PostScriptTightBBox = 0;
end

%TIFF previews imply -loose, historicly because ZBuffer TIFF was always "loose".
if pj.PostScriptPreview == pj.TiffPreview
    pj.PostScriptTightBBox = 0;
    
    %We have to produce a 72dpi EPS file first, have GS convert it to
    %TIFF, and then we combine a second high res EPS and the TIFF together.
    %Already checked above that for TIFF preview we have only one page.
    pj.GhostDriver = 'tiffpack';
    pj.GhostName = [tempname '.tif'];
    pj.PostScriptPreview = pj.TiffPreview;
end

% end validate

function locMakeSafeForDmfile( handles )

% if the figure has app data it was likely put there by scribe
% warn the user and turn off scribe before printing

figh = handles{1}(1);
usingAppData = ~ isempty( fieldnames(getappdata(figh)) );
if ( usingAppData )
    % turn off plotedit
    if plotedit(figh,'isactive')
        plotedit(figh, 'off');
    end

    % turn off interactive modes (e.g. zoom, pixval)
    uiclearmode(figh, '');

    % save warning state
    savedState = warning;
    warning('on');
    warning( ['print -dmfile does not fully support annotated plots.  Use' ...
            ' hgsave.']);
    warning(savedState);
end
% end locMakeSafeForDmfile
