view scripts/miscellaneous/menu.m @ 21630:b5d9b95d1e1a

Removing Java dialog boxes. * scripts/gui/module.mk: removed message_dialog.m entry. * scripts/gui/private/message_dialog.m: merged code to msgbox. * scripts/gui/errordlg.m: function now redirects to msgbox. New tests. * scripts/gui/helpdlg.m: function now redirects to msgbox. New tests. * scripts/gui/warndlg.m: function now redirects to msgbox. New tests. * scripts/gui/msgbox.m: merged code from message_dialog. Stripped Java dialog boxes. New tests. * scripts/gui/inputdlg.m: stripped Java dialog boxes. Improved doc. New tests. * scripts/gui/listdlg.m: stripped Java dialog boxes. Improved doc. New tests. * scripts/gui/questdlg.m: stripped Java dialog boxes. Improved doc. New tests. * scripts/miscellaneous/menu.m: stripped Java dialog boxes. Improved doc. * scripts/java/module.mk: removed entries of deleted files. * scripts/java/org/octave/DlgListener.java: removed Java dialog box Listener. * scripts/java/org/octave/images/error.png: removed unused image. * scripts/java/org/octave/images/information.png: removed unused image. * scripts/java/org/octave/images/octave.png: removed unused image. * scripts/java/org/octave/images/question.png: removed unused image. * scripts/java/org/octave/images/warning.png: removed unused image. * scripts/java/org/octave/JDialogBox.java: removed Java dialog box class. * scripts/java/org/octave/TeXcode.java: removed class used by Java dialogs. * scripts/java/org/octave/TeXtranslator.java: removed class used by Java dialogs. * doc/module.mk: removed unused java images and java.txi. * doc/interpreter/java.txi: merged content as minor sections to following files. * doc/interpreter/bugs.txi: moved Java distinguish section here. * doc/interpreter/external.txi: moved Java Interface description here. * doc/interpreter/gui.txi: moved dialog box docstrings here. * doc/interpreter/octave.texi: corrected TOC. * doc/interpreter/java-images/image001.png: removed unused image. * doc/interpreter/java-images/image002.png: removed unused image. * doc/interpreter/java-images/image003.png: removed unused image. * doc/interpreter/java-images/image004.png: removed unused image. * doc/interpreter/java-images/image005.png: removed unused image. * doc/interpreter/java-images/image006.png: removed unused image. * doc/interpreter/java-images/image007.png: removed unused image. * doc/interpreter/java-images/image008.png: removed unused image. * doc/interpreter/java-images/image009.png: removed unused image. * NEWS: Announced changes.
author Kai T. Ohlhus <k.ohlhus@gmail.com>
date Tue, 19 Apr 2016 15:27:19 +0200
parents 516bb87ea72e
children dcf8922b724b
line wrap: on
line source

## Copyright (C) 1993-2016 John W. Eaton
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or (at
## your option) any later version.
##
## 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 Octave; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn  {} {@var{choice} =} menu (@var{title}, @var{opt1}, @dots{})
## @deftypefnx {} {@var{choice} =} menu (@var{title}, @{@var{opt1}, @dots{}@})
## Display a menu with heading @var{title} and options @var{opt1}, @dots{},
## and wait for user input.
##
## If the GUI is running, the menu is displayed graphically using
## @code{listdlg}.  Otherwise, the title and menu options are printed on the
## console.
##
## @var{title} is a string and the options may be input as individual strings
## or as a cell array of strings.
##
## The return value @var{choice} is the number of the option selected by the
## user counting from 1 or 0 if the user aborts the dialog or makes an invalid
## selection.
##
## This function is useful for interactive programs.  There is no limit to the
## number of options that may be passed in, but it may be confusing to present
## more than will fit easily on one screen.
## @seealso{input, listdlg}
## @end deftypefn

## Author: jwe

function choice = menu (title, varargin)

  if (nargin < 2)
    print_usage ();
  endif

  if (! ischar (title))
    error ("menu: TITLE must be a string");
  elseif (nargin > 2 && ! iscellstr (varargin))
    error ("menu: All OPTIONS must be strings");
  elseif (! ischar (varargin{1}) && ! iscellstr (varargin{1}))
    error ("menu: OPTIONS must be string or cell array of strings");
  endif

  if (__octave_link_enabled__ ())
    [choice, ok] = listdlg ("Name", "menu", "PromptString", title,
                            "ListString", varargin, "SelectionMode", "Single");
    if (! ok)
      choice = 0;
    endif
  else  # console menu
    ## Force pending output to appear before the menu.
    fflush (stdout);

    ## Don't send the menu through the pager since doing that can cause
    ## major confusion.
    page_screen_output (0, "local");

    if (! isempty (title))
      printf ("%s\n", title);
    endif

    nopt = numel (varargin);
    while (1)
      for i = 1:nopt
        printf ("  [%2d] %s\n", i, varargin{i});
      endfor
      printf ("\n");
      s = input ("Select a number: ", "s");
      choice = sscanf (s, "%d");
      if (! isscalar (choice) || choice < 1 || choice > nopt)
        printf ("\nerror: input invalid or out of range\n\n");
        choice = 0;
      else
        break;
      endif
    endwhile
  endif

endfunction


%!error menu ()
%!error menu ("title")
%!error <TITLE must be a string> menu (1, "opt1")
%!error <All OPTIONS must be strings> menu ("title", "opt1", 1)
%!error <OPTIONS must be string or cell array of strings> menu ("title", 1)