Mercurial > octave
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 |
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 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
245 | 18 |
3381 | 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 | 22 ## Display a menu with heading @var{title} and options @var{opt1}, @dots{}, |
23 ## and wait for user input. | |
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 | 28 ## |
29 ## @var{title} is a string and the options may be input as individual strings | |
30 ## or as a cell array of strings. | |
31 ## | |
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 | 35 ## |
36 ## This function is useful for interactive programs. There is no limit to the | |
37 ## number of options that may be passed in, but it may be confusing to present | |
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 | 40 ## @end deftypefn |
4 | 41 |
2314 | 42 ## Author: jwe |
43 | |
19267 | 44 function choice = menu (title, varargin) |
4 | 45 |
211 | 46 if (nargin < 2) |
6046 | 47 print_usage (); |
4 | 48 endif |
49 | |
19267 | 50 if (! ischar (title)) |
51 error ("menu: TITLE must be a string"); | |
52 elseif (nargin > 2 && ! iscellstr (varargin)) | |
53 error ("menu: All OPTIONS must be strings"); | |
54 elseif (! ischar (varargin{1}) && ! iscellstr (varargin{1})) | |
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 | 59 [choice, ok] = listdlg ("Name", "menu", "PromptString", title, |
60 "ListString", varargin, "SelectionMode", "Single"); | |
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 | 63 endif |
64 else # console menu | |
65 ## Force pending output to appear before the menu. | |
66 fflush (stdout); | |
67 | |
68 ## Don't send the menu through the pager since doing that can cause | |
69 ## major confusion. | |
70 page_screen_output (0, "local"); | |
4 | 71 |
19267 | 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 | 75 |
76 nopt = numel (varargin); | |
77 while (1) | |
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 | 80 endfor |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
81 printf ("\n"); |
19267 | 82 s = input ("Select a number: ", "s"); |
83 choice = sscanf (s, "%d"); | |
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 | 87 else |
88 break; | |
89 endif | |
90 endwhile | |
91 endif | |
4 | 92 |
93 endfunction | |
13952
acaf33ccc04f
Use "local" option to configuration variables to simplify code.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
94 |
19267 | 95 |
96 %!error menu () | |
97 %!error menu ("title") | |
98 %!error <TITLE must be a string> menu (1, "opt1") | |
99 %!error <All OPTIONS must be strings> menu ("title", "opt1", 1) | |
100 %!error <OPTIONS must be string or cell array of strings> menu ("title", 1) |