function plotpuic(varargin)
% ------------------------------------------------------------------
% Plotp uicontrols.
%
% SYNOPSIS:
%
%   plotpuic
%  
%      Install or re-initialize the button window.
%
%   plotpuic(cmd)
%
%      Evaluate the comand string 'cmd'. Possible string values are
%      subfunctions in this script file, and are installed as the
%      callback functions for each button.
%
% ------------------------------------------------------------------
%Time:<Last updated on 98/10/06 at 22:50:31 by veins@TORSK>
%File:<c:/codas3/gfi/matlab/matlab50/scan_bin/plotpuic.m>

scanglob;

% Callback functions:
if nargin > 0,
  cmd = varargin{1};
  eval(cmd);
  return;
end

% Re-Initialize the window:
if ~isempty(PLOTP_UIC_),
  quit_button;
end

% Get screen size and calculate button window size:
screen = get(0, 'ScreenSize');
l = screen(1);
r = screen(3);
b = screen(2);
t = screen(4);
h = 50;
w = 550;
l = l + 50;
b = t - 50;
rect = [l, b, w, h];

% Install button window:
PLOTP_UIC_ = figure('WindowStyle', 'normal', 'MenuBar', 'none', ...
	 'Position', rect, 'NumberTitle', 'off', 'Name', 'Plotp Menu');
axis off

% Call back functions:
cbf = { ...
	 {...
	 'plotpuic(''quit_button'');', ...
	 'plotpuic(''data_button'');', ...
	 'plotpuic(''print_button'');', ...
	 'plotpuic(''scroll_button'');', ...
	 'plotpuic(''start_button'');'}, ...
	 { ...
	 'plotpuic(''lllt_button'');', ...
	 'plotpuic(''llt_button'');', ...
	 'plotpuic(''lt_button'');', ...
	 'plotpuic(''gt_button'');', ...
	 'plotpuic(''ggt_button'');', ...
	 'plotpuic(''gggt_button'');' }};

% Button strings:
bstr = { { 'Quit', 'Data', 'Print', 'Scroll is OFF', 'Start'}, ...
	 { '<<<', '<<', '<', '>', '>>', '>>>' }};

if PLOTP_SCROLL,
  bstr{1}{4} = 'Scroll is ON';
end

% Indexes to above cells:
ind = [ 5, 6 ];

% Define button positions, from bottom:
l = 0.0;
r = 1.0;
b = 0.0;
t = 1.0;
n = length(ind);
m = 0.04;
w1 = (r-l)-(2*m);
h = ((t-b) - (n+1)*m)/n;

% Loop through the vertical buttons:
for i=1:n,
  if ind(i) > 0,
	 % Horizontal buttons:
	 nn = ind(i);
	 wn = ((r-l)-(nn+1)*m)/nn;
	 for j=1:nn,
		pos = [m + (wn+m)*(j-1), m + (h+m)*(i-1), wn, h];
		uicontrol('Style', 'Pushbutton', 'Units', 'normalized', ...
			 'FontSize', 10, ...
			 'Position', pos, ...
			 'String', bstr{i}{j}, ...
			 'Callback', cbf{i}{j} );
	 end
  else
	 % One button per line:
	 pos = [m, m + (h+m)*(i-1), w1, h];
	 uicontrol('Style', 'Pushbutton', 'Units', 'normalized', ...
		  'Interruptible', 'on', ...
		  'FontSize', 10, ...
		  'Position', pos, ...
		  'String', bstr{i}, ...
		  'Callback', cbf{i} );
  end
end

return;

% ------------------------------------------------------------------
function quit_button()
scanglob;
if ~isempty(PLOTP_UIC_) & ~PLOTP_SCROLL,
  set(PLOTP_UIC_, 'WindowStyle', 'normal');
  delete(PLOTP_UIC_);
  PLOTP_UIC_ = [];
  fprintf(1, '\n ... quitting plotp ... press return ...\n');
  plotp(0);
end
return;

% ------------------------------------------------------------------
function scroll_button()
scanglob;

% Get scroll button object:
chs = get(PLOTP_UIC_, 'Children');
imax = length(chs);
obj = -1;
i = 1;
while (i <= imax) & (obj < 0),
  if strcmp(get(chs(i), 'Type'), 'uicontrol') ...
		  & (length(findstr(get(chs(i), 'Callback'), 'scroll_button')) > 0),
	 obj = chs(i);
  end
  i = i + 1;
end
if obj < 0,
  error('Could not find ''scroll_button'' object');
end
  
if PLOTP_SCROLL == 0,
  set(obj, 'String', 'Scroll is ON');
  %set(PLOTP_UIC_, 'WindowStyle', 'modal');
  PLOTP_SCROLL = 1;
else
  set(obj, 'String', 'Scroll is OFF');
  %set(PLOTP_UIC_, 'WindowStyle', 'normal');
  PLOTP_SCROLL = 0;
end
return;

% ------------------------------------------------------------------
function data_button()
  scanglob;
  if PLOTP_SCROLL,
	 return;
  end
  CATP_VARS = zeros(1,NVARS);
  ii = 1:length(PLOTP_VARS);
  for i=ii,
	 jj = 1:length(PLOTP_VARS{i});
	 for j=jj,
		CATP_VARS(PLOTP_VARS{i}(j)) = 1;
	 end
  end
  CATP_VARS = find(CATP_VARS);
  CATP_STARTP = PLOTP_STARTP;
  catp;
  return;

% ------------------------------------------------------------------
function print_button()
   scanglob;
	if PLOTP_SCROLL,
	  return;
	end

	%--- Next name for PS file:
	nextps = next_psf(PSFILE);
	%--- Add some additional information on the plot:
	tid = plotp_plot_info(nextps);
	%--- Save the plot:
	plotp_save_plot(nextps);
    
	% Remove annotations:
	imax = length(tid);
	for i=1:imax,
	  delete(tid(i));
	end

	return;

% ------------------------------------------------------------------
function start_button()
   scanglob;
	plotp('-start');
	return;

% ------------------------------------------------------------------
function lllt_button()
   scanglob;
	plotp('-rrrew');
	return;

% ------------------------------------------------------------------
function llt_button()
   scanglob;
	plotp('-rrew');
	return;

% ------------------------------------------------------------------
function lt_button()
   scanglob;
	plotp('-rew');
	return;

% ------------------------------------------------------------------
function gt_button()
   scanglob;
	plotp('-fwd');
	return;

% ------------------------------------------------------------------
function ggt_button()
   scanglob;
	plotp('-ffwd');
	return;

% ------------------------------------------------------------------
function gggt_button()
   scanglob;
	plotp('-fffwd');
	return;

% ------------------------------------------------------------------
% This function must be called by functions allowing interuptions.
function plotp_interrupt()
   scanglob;
	pause(0);
	return;

% ------------------------------------------------------------------
function tid = plotp_plot_info(nextps)

scanglob;

% Activate plot:
figure(PLOTP_FIG);

plot_title = CRUISE_ID;
plot_subtitle = TIME_RANGE;
info = 'PLOTP: Ping Values';
note  = [ 'BIN File: ' BINFILE ' - PS File: ' nextps ' - PINGS: ' ...
		PLOTP_STARTP ' TO ' int2str(PLOTP_STARTP+size(DATA,2)) ];
institute = INSTITUTE;
tid = plotstuf('-title', plot_title, '-subtitle', plot_subtitle, ...
	 '-institute', institute, '-note', note, '-info', info);

return;

% ------------------------------------------------------------------
function plotp_save_plot(nextps)

scanglob;

%--- Save plot:
figure(PLOTP_FIG);
orient tall;
eval([ 'print -dps ' nextps ]);

%--- Add here some info to be output in ASCFILE
str = sprintf('PRINT PLOTP:%s\n', nextps);
str = sprintf('%s\n\n VARIABLESS:\n\n\t ..n ..x ..y', str);
np = length(PLOTP_VARS);
for n=1:np,
  pv = PLOTP_VARS{n};
  nv = length(pv);
  str = sprintf('%s\n\t %3d', str, n);
  for v=1:nv,
	 str = sprintf('%s %3d', str, pv(v));
  end
end
logp(1, str);
fprintf('\n\n File is saved into \n\t%s\n\n', nextps);

return;






