function fd_print(flag)

% function fd_print(flag)
%
% This function is used in conjunction with FD3.  It prints the entire flow
% diagram workspace.  If flag==0, the print output is sent to the printer.
% If flag==1, the user is given a choice of sending the output to either a
% postscript or encapsulated postscript file.  If flag==2, the function does
% nothing (this setting is used when fd_print is compiled during fd3_init).
% The program also asks the user for a flow diagram title.  The title is
% centered at the top of the flow diagram.  Flow diagrams always print in
% landscape orientation.
%
% ACP

% Last update:  July 26, 1993
% Author:       Adrian C. Penisson

global FD3FIGURE FD3AXES FD3SCALE FD3TITLE

if flag==2
   return
end

% Turn all the menus off so the user doen't do anything funny during the print
% job.

set_menu(1:6,'off')

if flag

   % This is for a print-to-file.  First get name.

   [file,iflag]=di_inp_c('Print to what file?','',0);

   if iflag

      % If the user hits cancel, cancel the print job.

      di_warn('','Print To File Cancelled','CONTINUE');
      fd_reset(0)
      return

   end

   % Find out if the user wishes the file saved as PS or EPS.

   choice=di_choic('','Which format?','PS','EPS');

   if choice==1
      ftype='-dps ';
   else
      ftype='-deps ';
   end

end

% Get the title

[newtitle,iflag]=di_inp_c('What is the flow diagram''s title?',FD3TITLE,1);

if iflag
   if flag
      di_warn('','Print To File Cancelled','CONTINUE');
   else
      di_warn('','Print Cancelled','CONTINUE');
   end
   fd_reset(0)
   return
end

% Switch from the pointer to the watch since the print job can take a while

set(FD3FIGURE,'Pointer','watch')

% Tell matlab to hold its horses on the drawing so the user doesn't think
% the flow diagram window is having some sort of fit.

% Set all the axes and units appropriately.

set(FD3AXES(1),'Visible','off')
axes_units=get(FD3AXES(1),'Units');
set(FD3AXES,'Units','normalized');
axes_pos=get(FD3AXES(1),'Position');
set(FD3AXES,'Position',[0 0 1 1])
axes_Xlim=get(FD3AXES(1),'Xlim');
axes_Ylim=get(FD3AXES(1),'Ylim');

% Make sure the page is oriented correctly.  If there is a title, extra space
% has to be allocated.

if length(newtitle)==0
   set(FD3FIGURE,'PaperPosition',[.5 .5 10 7.5])
   set(FD3AXES,'Xlim',[0 FD3SCALE*44],'Ylim',[0 FD3SCALE*33])
else
   set(FD3FIGURE,'PaperPosition',[.5 .25 10 7.75])
   set(FD3AXES,'Xlim',[0 FD3SCALE*44],'Ylim',[0 FD3SCALE*35.2])
   th=text('Position',[22 34.10 0]*FD3SCALE,'String',newtitle,'Horizontal',...
      'Center','Vert','Middle');
end

% Stop the window from having convulsions during the print job.

drawnow discard

% Finally, print the flow diagram.

if flag
   eval(['print ' ftype file])
else
   print
end

% Put everything back like it was.

set(FD3FIGURE,'Pointer','arrow')
set(FD3AXES(1),'Visible','on')
set(FD3AXES,'Xlim',axes_Xlim,'Ylim',axes_Ylim)
set(FD3AXES,'Position',axes_pos)
set(FD3AXES,'Units',axes_units)

% Delete title since we don't really need it sticking around.

delete(th)

% Let the user know all is well.

di_warn('','Print Is Complete.','CONTINUE')
drawnow

% If the user changed the title, do a hard reset.

if newtitle==FD3TITLE
   fd_reset(0)
else
   FD3TITLE=newtitle;
   fd_reset(1)
end
