function fd3_init

% function fd3_init
%
% This function is used in conjunction with FD3.  It creates the FD3 graphics
% window, the uicontrols, and the uimenus.  It also sets all global variables
% to their initial values.  If an FD3 is already running, it issues a warning
% message that two FD3's cannot be run at the same time.
%
% ACP

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

% Establish global variables

global FD3FIGURE FD3AXES FD3MODULES FD3STREAMS FD3MENUS FD3MESSAGE 
global FD3NEXT FD3FUNCLEVEL FD3LASTPOS FD3XYDATA FD3OBJECT FD3TEMP
global FD3SCALE FD3TEXT FD3BOXES FD3PXPERCM FD3SLIDERS
global FD3STATUS FD3HELPFIG FD3TITLE FD3RUNNING FD3VERSION

% Check to see if FD3 is already running.

if FD3RUNNING
   disp(setstr(7))
   disp('*******************************************************************')
   disp(' ')
   disp('It appears that you already have a FROG Draw 3 running.  You')
   disp('should not have two FD3''s running at the same time in the same')
   disp('MATLAB session.  If you do not actually have another FD3 running,')
   disp('type the following line into MATLAB and try to start FD3 again:')
   disp(' ')
   disp('FD3RUNNING=0;')
   disp(' ')
   disp('*******************************************************************')
   disp(' ')
   return
end

% Initialze variables

FD3TITLE='';
FD3SCALE=1;
FD3MENUS=[];
FD3NEXT=[];
FD3MESSAGE=[];
FD3MODULES=zeros(15,1);
FD3STREAMS=zeros(6,1);
FD3FUNCLEVEL=0;
FD3LASTPOS=[];
FD3OBJECT=[];
FD3TEMP=[];
FD3TEXT=[];
FD3BOXES=[0 0]';
FD3STATUS=[1 0 1 0];  % [bumps on, grid on, needs name, needs saving]
FD3HELPFIG=[];
FD3RUNNING=1;

% Set the version.

FD3VERSION=0;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%				Set up Window				    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Create new graphics window in which to run program.  The basic graphics
% window is 6.25" by 4" with a 5" by 3.75" work area.  The ability to
% resize the graphics window is turned off to insure that the
% proper aspect ratio is maintained. The name of the graphics window
% is set to 'FD3: untitled'.  The name setting changes to reflect the
% name of the flow sheet once the sheet is first saved.

FD3FIGURE=figure('Int','yes','PaperOrientation','Landscape');
pospx=get(FD3FIGURE,'Position');

if FD3PXPERCM==[]
   set(FD3FIGURE,'Units','Centimeters');
   poscm=get(FD3FIGURE,'Position');
   FD3PXPERCM=pospx(3:4)./poscm(3:4);
   set(FD3FIGURE,'Units','Pixels');
end

xdims=ceil([.25 5 1.25]*2.54*FD3PXPERCM(1));
ydims=ceil([.25 3.75]*2.54*FD3PXPERCM(2));

set(FD3FIGURE,'Position',[pospx(1:2) sum(xdims)+4 sum(ydims)+2])
set(FD3FIGURE,'Units','pixels','Resize','off','Name',...
   'FROG Draw 3: untitled')

% Create a blocks which cover the right and left hand portions of the
% screen.  This is to insure that no lines are drawn in the message portion
% of the window when the quadrant being viewed is changed.  This should
% be unecessary, but sometimes graphics objects show outside the axes even
% when their 'Clipping' is 'on'.

% Left side (partially under left slide bar).
block1=uicontrol('Style','text','Units','Pixels','Position',...
   [0 0 xdims(1) sum(ydims)+2]);

% Right side (where instructions appear).
block2=uicontrol('Style','text','Units','Pixels','Position',...
   [sum(xdims(1:2))+4 0 xdims(3) sum(ydims)+2]);

% Also cover bottom so that it will look okay when slider is turned off
block3=uicontrol('Style','text','Units','Pixels','Position',...
   [0 0 sum(xdims)+4 ydims(1)]);

% Create sliding controls so that the different workspaced quadrants may
% be viewed when working in the default quadrant view. 

% Bottom Slider
slider1=uicontrol('Style','Slider','Max',44,'Min',22,'Position',...
   [xdims(1) 0 xdims(2)+4 ydims(1)],'Units','Pixels','Value',22);
set(slider1,'Call',sprintf('adj_view(1,%18.14f )',slider1));

% Left Slider
slider2=uicontrol('Style','Slider','Max',33,'Min',16.5,'Position',...
   [0 ydims(1) xdims(1) ydims(2)+3],'Units','Pixels','Value',33);
set(slider2,'Call',sprintf('adj_view(2,%18.14f )',slider2));

FD3SLIDERS=[slider1 slider2];

% If the display is black and white, adjust the colors of the sliders so that
% they are easier to see.

% if get(0,'ScreenDepth')==1
%   set(slider1,'Back',[0 0 0],'Fore',[1 1 1]);
%   set(slider2,'Back',[0 0 0],'Fore',[1 1 1]);
%   set(block1,'Back',[0 0 0],'Fore',[1 1 1]);
%   set(block2,'Back',[0 0 0],'Fore',[1 1 1]);
%   set(block3,'Back',[0 0 0],'Fore',[1 1 1]);
%   cinvert(FD3FIGURE)
%   drawnow
%   cinvert(FD3FIGURE)
% end

% Create the "axis" in which the user will work.  The aspect ratio of the 
% basic work space is 4:3.  There are actually three axes superimposed on
% each other.  Axes1 is given the lowest priority when reading clicks.
% Secondary objects and boxes are drawn on this axis.  Axes2 is given the
% second highest priority when reading clicks.  Texts, labels, and streams
% are drawn on this axis.  Axes1 is given the highest priority when reading
% clicks.  Only primary objects are drawn on this axis.

axes1=axes('Box','on','ColorOrder',[1 1 1],'Units','Pixels','Position',...
   [xdims(1)+3 ydims(1)+1 xdims(2) ydims(2)],'XTickLabels',[],...
   'YTickLabels',[]);

axes2=axes('Box','on','ColorOrder',[1 1 1],'Units','Pixels','Position',...
   [xdims(1)+3 ydims(1)+1 xdims(2) ydims(2)],'XTickLabels',[],...
   'YTickLabels',[],'Visible','off');

axes3=axes('Box','on','ColorOrder',[1 1 1],'Units','Pixels','Position',...
   [xdims(1)+3 ydims(1)+1 xdims(2) ydims(2)],'XTickLabels',[],...
   'YTickLabels',[],'Visible','off');

FD3AXES=[axes1 axes2 axes3];

set(FD3AXES,'Units','normalized','Xtick',FD3SCALE*(0:1:44),'Ytick',...
   FD3SCALE*(0:1:33))

drawnow

for i=1:3
   axes(FD3AXES(i))
   axis(FD3SCALE*[0 22 16.5 33])
   axis('equal')
   hold on
   axis(axis)
end

% Set the colormap so that color 1=white, color 2=black, so that these
% show up right on any display.

set(FD3FIGURE,'Colormap',[1 1 1;0 0 0]);

% Set nextplot to new so that Matlab will create a new figure window
% if the user wants to plot some data.

set(FD3FIGURE,'NextPlot','new')

% Call click_win whenever someone clicks in our figure, and
% set the name of the figure.

set(FD3FIGURE,'WindowButtonDownFcn','clk_win')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%				Set up Menus				     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Set up strings which are used in creating the callbacks for the menus

s1='adj_menu;FD3NEXT=''';
s2=''';FD3MESSAGE=di_mssg(''Point and|click on the|';
s3=''');';

%%%%%%%%%%%%%
% File Menu %
%%%%%%%%%%%%%

hmenu1=uimenu('Lab','File');
uimenu(hmenu1,'Lab','New','Call','fd_new','Int','yes','Acc','n')
uimenu(hmenu1,'Lab','Load','Call','fd_load','Int','yes','Acc','l')
uimenu(hmenu1,'Lab','Save','Call','fd_save(0)','Acc','s','Int','yes')
uimenu(hmenu1,'Lab','Save As','Call','fd_save(1)','Int','yes')
uimenu(hmenu1,'Lab','Print','Call','fd_print(0)','Separator','on','Acc',...
   'p','Int','yes')
uimenu(hmenu1,'Lab','Print To File','Int','yes','Call','fd_print(1)')
uimenu(hmenu1,'Lab','Quit','Separator','on','Int','yes','Call',...
   'fd_new;close([FD3FIGURE FD3HELPFIG]);FD3RUNNING=0;')

%%%%%%%%%%%%%
% Edit Menu %
%%%%%%%%%%%%%

c2b=[s1 'Delete' s2 'item to be|deleted.' s3];
c2c=[s1 'Copy' s2 'module or text|to be copied.' s3];
c2d=[s1 'Move' s2 'module or text|to be moved.' s3];
c2e=[s1 'RotateCW' s2 ['module body,|module label,|stream label,|',...
   'or text to be|rotated.'] s3];
c2f=[s1 'RotateCCW' s2 ['module body,|module label,|stream label,|',...
   'or text to be|rotated.'] s3];
c2g=[s1 'Resize' s2 'module to|be resized.' s3];
c2h=[s1 'Reverse' s2 'stream to|be reversed.' s3];
c2i=[s1 'Raise' s2 'stream to|be raised.' s3];

hmenu2=uimenu('Lab','Edit');
uimenu(hmenu2,'Lab','Cancel','Call','fd_reset(0)','Acc','z')
uimenu(hmenu2,'Lab','Delete','Call',c2b,'Acc','x','Sep','on','Int','yes')
uimenu(hmenu2,'Lab','Copy','Call',c2c,'Acc','c','Int','yes')
uimenu(hmenu2,'Lab','Move','Call',c2d,'Acc','m','Int','yes')
uimenu(hmenu2,'Lab','Rotate CW','Call',c2e,'Sep','on','Int','yes');
uimenu(hmenu2,'Lab','Rotate CCW','Call',c2f,'Int','yes');
uimenu(hmenu2,'Lab','Resize','Call',c2g,'Int','Yes')
uimenu(hmenu2,'Lab','Reverse','Call',c2h,'Sep','on','Int','yes')
uimenu(hmenu2,'Lab','Raise','Call',c2i,'Int','yes')

%%%%%%%%%%%%%%%
% Module Menu %
%%%%%%%%%%%%%%%

s4=[s2 'center of the|new module.' s3];

hmenu3=uimenu('Lab','Modules');

% Generic Modules

hmenu3a=uimenu(hmenu3,'Lab','Basic');
uimenu(hmenu3a,'Lab','Mixer','Call',[s1 'f_mixer' s4])
uimenu(hmenu3a,'Lab','Splitter','Call',[s1 'f_splttr' s4])
uimenu(hmenu3a,'Lab','Reactor','Call',[s1 'f_rctr' s4],'Sep','on')
uimenu(hmenu3a,'Lab','Kiln','Call',[s1 'f_kiln' s4])
uimenu(hmenu3a,'Lab','Furnace','Call',[s1 'f_furn' s4])
uimenu(hmenu3a,'Lab','Separator','Call',[s1 'f_sep' s4],'Sep','on')
uimenu(hmenu3a,'Lab','Distillation','Call',[s1 'f_dist' s4])
uimenu(hmenu3a,'Lab','Settler','Call',[s1 'f_sttlr' s4])
uimenu(hmenu3a,'Lab','Evaporator','Call',[s1 'f_evap' s4])
uimenu(hmenu3a,'Lab','Centrifuge','Call',[s1 'f_centr' s4])
uimenu(hmenu3a,'Lab','Heat Exchanger','Call',[s1 'f_htx' s4],'Sep','on')
uimenu(hmenu3a,'Lab','Compressor','Call',[s1 'f_comp' s4])
uimenu(hmenu3a,'Lab','Turbine','Call',[s1 'f_turb' s4],'Sep','on')
uimenu(hmenu3a,'Lab','Pump','Call',[s1 'f_pump' s4])
uimenu(hmenu3a,'Lab','Valve','Call',[s1 'f_valve' s4])
uimenu(hmenu3a,'Lab','Feed Arrow','Call',[s1 'f_arrf' s4],'Sep','on')
uimenu(hmenu3a,'Lab','Product Arrow','Call',[s1 'f_arrp' s4])

% Generic Modules

hmenu3b=uimenu(hmenu3,'Lab','Generic');
uimenu(hmenu3b,'Lab','Circle','Call',[s1 's_circle' s4])
uimenu(hmenu3b,'Lab','Rectangle','Call',[s1 's_rect' s4])
uimenu(hmenu3b,'Lab','Horizontal Tank','Call',[s1 's_htank' s4])
uimenu(hmenu3b,'Lab','Vertical Tank','Call',[s1 's_vtank' s4])

% User Defined
hmenu3c=uimenu(hmenu3,'Lab','User-Defined','Int','Yes','Call','user_def');

%%%%%%%%%%%%%%%
% Stream Menu %
%%%%%%%%%%%%%%%

c4a=[s1 'HandV' s2 'body of the|module which|feeds the|stream.' s3];
c4b=[s1 'Diag' s2 'body of the|module which|feeds the|stream.' s3];

hmenu4=uimenu('Lab','Streams');
uimenu(hmenu4,'Lab','H/V Line(s)','Call',c4a,'Int','yes','Acc','a')
uimenu(hmenu4,'Lab','Diagonal Line','Call',c4b,'Int','yes','Acc','d')
uimenu(hmenu4,'Lab','Bumps Off','Call','tgl_bmp;fd_reset(1)',...
   'Sep','on','Int','yes')

%%%%%%%%%%%%%%
% Misc Menu %
%%%%%%%%%%%%%%

s6=''';FD3MESSAGE=di_mssg(''Point and|click on one|corner of|the box.';

c5a=[s1 'AddLabel' s2 'module to|which you|wish to add|a label.' s3];
c5b=[s1 'EditLabel' s2 'label you|wish to edit.' s3];
c5c=[s1 'AddText' s2 'window where|you wish to|place the|text.' s3];
c5d=[s1 'EditText' s2 'text you|wish to edit.' s3];
c5e=[s1 'DashedBox' s6 s3];
c5f=[s1 'SolidBox' s6 s3];

hmenu5=uimenu('Lab','Misc');
uimenu(hmenu5,'Lab','Add Label','Call',c5a,'Int','yes')
uimenu(hmenu5,'Lab','Edit Label','Call',c5b,'Int','yes')
uimenu(hmenu5,'Lab','Add Text','Sep','on','Call',c5c,'Int','yes')
uimenu(hmenu5,'Lab','Edit Text','Call',c5d,'Int','yes')
uimenu(hmenu5,'Lab','Dashed Box','Sep','on','Call',c5e,'Int','yes')
uimenu(hmenu5,'Lab','Solid Box','Call',c5f,'Int','yes')

%%%%%%%%%%%%%%%
% Window Menu %
%%%%%%%%%%%%%%%

hmenu6=uimenu('Lab','Window');
uimenu(hmenu6,'Lab','Open Help','Call','hlp_opn','Acc','h')
uimenu(hmenu6,'Lab','Full Page','Call','tgl_view','Acc','v','Sep','on')
uimenu(hmenu6,'Lab','Grid On','Call','tgl_grd','Acc','g')

% Store the handles of the menus in a global variable so that the menus
% may be activated and deactivated at appropriate times during the program.

FD3MENUS=[hmenu1 hmenu2 hmenu3 hmenu4 hmenu5 hmenu6];

% If black and white, change color of menus and remove separators.

if get(0,'ScreenDepth')==1
   for i=1:6
      child=get(FD3MENUS(i),'Children');
      set(child,'Fore',[1 1 1],'Back',[0 0 0])
      for i=1:length(child)
	  grandchild=get(child(i),'Children');
	  set(grandchild,'Fore',[1 1 1],'Back',[0 0 0])
      end
   end
end

% Call the window and object click callbacks.  By introducing these functions
% to matlab now (i.e., before the user makes his first window click and his
% first object click), the performance of the program when it's first used
% will be greatly improved.

clk_obj
clk_win
fd_print(2)

% Properly set the Enable status for the menus headers and menu items (i.e.
% whether or not certain menu items are selectable).

fd_reset(0)
