## Copyright (C) 2009, 2010 Thomas Weidenfeller
## 
## This file is part of gds2000tools/gds-octave.
## 
## gds2000tools/gds-octave is free software: you can redistribute it
## and/or modify it under the terms of the GNU General Public License
## version 3 as published by the Free Software Foundation.
## 
## gds2000tools/gds-octave is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with gds2000tools/gds-octave.  If not,
## see <http://www.gnu.org/licenses/>.
##

## usage: SELECTION = __gds_menu2(TITLE, OPT1, ...)
##
## Displays a two columns ASCII "pick a number" menu.
##
## See also: __gds_fmenu2, menu

## Author: Thomas Weidenfeller
## Created: 2009-09-09
## Version: $Id: __gds_menu2.m,v 1.2 2010/06/02 23:08:30 tw Exp $
## Keywords: Oscilloscope, GDS-2000, gds2000tools, plot

function selection = __gds_menu2(title, varargin)
	fflush(stdout);
	rows = ceil((nargin - 1) / 2);

	old_psc = page_screen_output();
	unwind_protect
	page_screen_output(0);

	printf("%s\n\n", title);

	while(rows > 0)
		i = 0;
		for i = 1:(rows - 1)
			printf("  [%2d] %-30.30s  [%2d] %s\n",
				i, limits(varargin{i}, 30),
				i + rows, limits(varargin{i + rows}, 30));
		endfor
		i++;
		if(rem(nargin - 1, 2))
			printf("  [%2d] %s\n\n", i , limits(varargin{i},30));
		else
			printf("  [%2d] %-30.30s  [%2d] %s\n\n",
				i, limits(varargin{i}, 30),
				i + rows, limits(varargin{i + rows}, 30));
		endif
		selection = str2num( input("pick a number: ", "s"));
		if(isnan(selection)
		    || isempty(selection)
		    || selection < 1 || selection >= nargin)

			printf("\nInput error.\n\n");
		else
			break;
		endif
	endwhile
	unwind_protect_cleanup
	page_screen_output(old_psc);
	end_unwind_protect
endfunction

##
## Limit string length
##
function s = limits(s, l)
	ls = length(s);
	if(ls <= l)
		return;
	endif
	s = [ "..." s(ls - l + 4:ls) ];
endfunction

