annotate scripts/miscellaneous/menu.m @ 21634:96518f623c91

Backed out changeset dcf8922b724b
author Mike Miller <mtmiller@octave.org>
date Wed, 20 Apr 2016 11:06:03 -0700
parents dcf8922b724b
children ca9e09417e1f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21630
b5d9b95d1e1a Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
1 ## Copyright (C) 1993-2016 John W. Eaton
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
2 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
3 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6521
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6521
diff changeset
8 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
9 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
13 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6521
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6521
diff changeset
17 ## <http://www.gnu.org/licenses/>.
245
16a24e76d6e0 [project @ 1993-12-03 02:00:15 by jwe]
jwe
parents: 211
diff changeset
18
3381
69b167451491 [project @ 1999-12-15 20:48:10 by jwe]
jwe
parents: 3372
diff changeset
19 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20497
diff changeset
20 ## @deftypefn {} {@var{choice} =} menu (@var{title}, @var{opt1}, @dots{})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20497
diff changeset
21 ## @deftypefnx {} {@var{choice} =} menu (@var{title}, @{@var{opt1}, @dots{}@})
19267
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
22 ## Display a menu with heading @var{title} and options @var{opt1}, @dots{},
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
23 ## and wait for user input.
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
24 ##
21630
b5d9b95d1e1a Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
25 ## If the GUI is running, the menu is displayed graphically using
b5d9b95d1e1a Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
26 ## @code{listdlg}. Otherwise, the title and menu options are printed on the
b5d9b95d1e1a Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
27 ## console.
19267
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
28 ##
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
29 ## @var{title} is a string and the options may be input as individual strings
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
30 ## or as a cell array of strings.
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
31 ##
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
32 ## The return value @var{choice} is the number of the option selected by the
20497
bb09279e5c99 menu.m: Fix bug #45988, return 0 if the dialog is aborted or invalid selection
Andreas Weber <andy.weber.aw@gmail.com>
parents: 19697
diff changeset
33 ## user counting from 1 or 0 if the user aborts the dialog or makes an invalid
bb09279e5c99 menu.m: Fix bug #45988, return 0 if the dialog is aborted or invalid selection
Andreas Weber <andy.weber.aw@gmail.com>
parents: 19697
diff changeset
34 ## selection.
19267
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
35 ##
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
36 ## This function is useful for interactive programs. There is no limit to the
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
37 ## number of options that may be passed in, but it may be confusing to present
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
38 ## more than will fit easily on one screen.
18674
7bccc182e2f7 doc: Update pause documentation and add seealso links between related functions.
Rik <rik@octave.org>
parents: 17744
diff changeset
39 ## @seealso{input, listdlg}
3372
f16c2ce14886 [project @ 1999-11-23 19:07:09 by jwe]
jwe
parents: 3180
diff changeset
40 ## @end deftypefn
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
41
2314
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
42 ## Author: jwe
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
43
19267
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
44 function choice = menu (title, varargin)
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
45
211
4adbab9c31cd [project @ 1993-11-12 14:12:22 by jwe]
jwe
parents: 4
diff changeset
46 if (nargin < 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
47 print_usage ();
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
48 endif
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
49
19267
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
50 if (! ischar (title))
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
51 error ("menu: TITLE must be a string");
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
52 elseif (nargin > 2 && ! iscellstr (varargin))
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
53 error ("menu: All OPTIONS must be strings");
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
54 elseif (! ischar (varargin{1}) && ! iscellstr (varargin{1}))
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
55 error ("menu: OPTIONS must be string or cell array of strings");
14503
ff4143d9fc05 menu.m: Accept cell array input choices (patch #7712)
Ryan Starret <brannigan@starret.ca>
parents: 14138
diff changeset
56 endif
ff4143d9fc05 menu.m: Accept cell array input choices (patch #7712)
Ryan Starret <brannigan@starret.ca>
parents: 14138
diff changeset
57
21630
b5d9b95d1e1a Removing Java dialog boxes.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20852
diff changeset
58 if (__octave_link_enabled__ ())
19267
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
59 [choice, ok] = listdlg ("Name", "menu", "PromptString", title,
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
60 "ListString", varargin, "SelectionMode", "Single");
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
61 if (! ok)
20497
bb09279e5c99 menu.m: Fix bug #45988, return 0 if the dialog is aborted or invalid selection
Andreas Weber <andy.weber.aw@gmail.com>
parents: 19697
diff changeset
62 choice = 0;
19267
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
63 endif
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
64 else # console menu
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
65 ## Force pending output to appear before the menu.
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
66 fflush (stdout);
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
67
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
68 ## Don't send the menu through the pager since doing that can cause
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
69 ## major confusion.
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
70 page_screen_output (0, "local");
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
71
19267
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
72 if (! isempty (title))
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
73 printf ("%s\n", title);
13952
acaf33ccc04f Use "local" option to configuration variables to simplify code.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
74 endif
19267
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
75
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
76 nopt = numel (varargin);
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
77 while (1)
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
78 for i = 1:nopt
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
79 printf (" [%2d] %s\n", i, varargin{i});
19267
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
80 endfor
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
81 printf ("\n");
19267
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
82 s = input ("Select a number: ", "s");
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
83 choice = sscanf (s, "%d");
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
84 if (! isscalar (choice) || choice < 1 || choice > nopt)
21634
96518f623c91 Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents: 21633
diff changeset
85 printf ("\nerror: input invalid or out of range\n\n");
20497
bb09279e5c99 menu.m: Fix bug #45988, return 0 if the dialog is aborted or invalid selection
Andreas Weber <andy.weber.aw@gmail.com>
parents: 19697
diff changeset
86 choice = 0;
19267
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
87 else
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
88 break;
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
89 endif
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
90 endwhile
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
91 endif
4
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
92
b4df021f796c [project @ 1993-08-08 01:26:08 by jwe]
jwe
parents:
diff changeset
93 endfunction
13952
acaf33ccc04f Use "local" option to configuration variables to simplify code.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
94
19267
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
95
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
96 %!error menu ()
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
97 %!error menu ("title")
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
98 %!error <TITLE must be a string> menu (1, "opt1")
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
99 %!error <All OPTIONS must be strings> menu ("title", "opt1", 1)
f32fb4d3fb9e menu.m: Overhaul function.
Rik <rik@octave.org>
parents: 18674
diff changeset
100 %!error <OPTIONS must be string or cell array of strings> menu ("title", 1)