Mercurial > octave-nkf
comparison scripts/miscellaneous/menu.m @ 20532:bb09279e5c99
menu.m: Fix bug #45988, return 0 if the dialog is aborted or invalid selection
author | Andreas Weber <andy.weber.aw@gmail.com> |
---|---|
date | Tue, 22 Sep 2015 11:25:04 +0200 |
parents | 4197fc428c7d |
children |
comparison
equal
deleted
inserted
replaced
20530:bf6c4433ed5f | 20532:bb09279e5c99 |
---|---|
28 ## | 28 ## |
29 ## @var{title} is a string and the options may be input as individual strings | 29 ## @var{title} is a string and the options may be input as individual strings |
30 ## or as a cell array of strings. | 30 ## or as a cell array of strings. |
31 ## | 31 ## |
32 ## The return value @var{choice} is the number of the option selected by the | 32 ## The return value @var{choice} is the number of the option selected by the |
33 ## user counting from 1. | 33 ## user counting from 1 or 0 if the user aborts the dialog or makes an invalid |
34 ## selection. | |
34 ## | 35 ## |
35 ## This function is useful for interactive programs. There is no limit to the | 36 ## This function is useful for interactive programs. There is no limit to the |
36 ## number of options that may be passed in, but it may be confusing to present | 37 ## number of options that may be passed in, but it may be confusing to present |
37 ## more than will fit easily on one screen. | 38 ## more than will fit easily on one screen. |
38 ## @seealso{input, listdlg} | 39 ## @seealso{input, listdlg} |
56 | 57 |
57 if (isguirunning () || usejava ("awt")) | 58 if (isguirunning () || usejava ("awt")) |
58 [choice, ok] = listdlg ("Name", "menu", "PromptString", title, | 59 [choice, ok] = listdlg ("Name", "menu", "PromptString", title, |
59 "ListString", varargin, "SelectionMode", "Single"); | 60 "ListString", varargin, "SelectionMode", "Single"); |
60 if (! ok) | 61 if (! ok) |
61 choice = 1; | 62 choice = 0; |
62 endif | 63 endif |
63 else # console menu | 64 else # console menu |
64 ## Force pending output to appear before the menu. | 65 ## Force pending output to appear before the menu. |
65 fflush (stdout); | 66 fflush (stdout); |
66 | 67 |
80 printf ("\n"); | 81 printf ("\n"); |
81 s = input ("Select a number: ", "s"); | 82 s = input ("Select a number: ", "s"); |
82 choice = sscanf (s, "%d"); | 83 choice = sscanf (s, "%d"); |
83 if (! isscalar (choice) || choice < 1 || choice > nopt) | 84 if (! isscalar (choice) || choice < 1 || choice > nopt) |
84 printf ("\nerror: input invalid or out of range\n\n"); | 85 printf ("\nerror: input invalid or out of range\n\n"); |
86 choice = 0; | |
85 else | 87 else |
86 break; | 88 break; |
87 endif | 89 endif |
88 endwhile | 90 endwhile |
89 endif | 91 endif |