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

scanglob;

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

% Re-Initialize the window:
if ~isempty(CATP_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 = 100;
w = 100;
l = l + 5*w/10;
b = t - h - 5*h/10;
rect = [l, b, w, h];

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

% Call back functions:
cbf = { ...
	 'catp_uic(''quit_button'');', ...
	 'catp_uic(''scroll_button'');', ...
	 'catp_uic(''start_button'');', ...
	 { ...
	 'catp_uic(''lt_button'');', ...
	 'catp_uic(''llt_button'');', ...
	 'catp_uic(''ggt_button'');', ...
	 'catp_uic(''gt_button'');' }};

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

if CATP_SCROLL,
  bstr{2} = 'Scroll is ON';
end

% Indexes to above cells:
ind = [ 0, 0, 0, 4 ];

% Define button positions, from bottom:
l = 0.0;
r = 1.0;
b = 0.0;
t = 1.0;
n = 4;
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(CATP_UIC_) & ~CATP_SCROLL,
	 set(CATP_UIC_, 'WindowStyle', 'normal');
	 delete(CATP_UIC_);
	 CATP_UIC_ = [];
	 fprintf(1, '\n ... quitting catp ... press return ...\n');
	 catp(0);
  end
  return;
  
% ------------------------------------------------------------------
function scroll_button()
  scanglob;
  catp('-scroll');
  if CATP_SCROLL == 1,
	 set(gco, 'String', 'Scroll is ON');
  else
	 set(gco, 'String', 'Scroll is OFF');
  end
  
  return;

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

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

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

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

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

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





