comparison scripts/ui/inputdlg.m @ 16505:ff061068a66c

move dialog files to separate directory * scripts/ui/errordlg.m, scripts/ui/helpdlg.m, scripts/ui/inputdlg.m, scripts/ui/listdlg.m, scripts/ui/msgbox.m, scripts/ui/questdlg.m, scripts/ui/warndlg.m: Move here from scripts/java. * scripts/java/module.mk (java_FCN_FILES): Update list. * scripts/ui/module.mk: New file. * scripts/Makefile.am: Include it. (ui/PKG_ADD, $(ui_GEN_FCN_FILES), ui/$(octave_dirstamp)): New targets.
author John W. Eaton <jwe@octave.org>
date Fri, 12 Apr 2013 14:51:51 -0400
parents scripts/java/inputdlg.m@921912c92102
children 7f2395651a1c
comparison
equal deleted inserted replaced
16504:49b059bf27c7 16505:ff061068a66c
1 ## Copyright (C) 2010 Martin Hepperle
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
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
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{cstr} =} inputdlg (@var{prompt})
21 ## @deftypefnx {Function File} {@var{cstr} =} inputdlg (@var{prompt}, @var{title})
22 ## @deftypefnx {Function File} {@var{cstr} =} inputdlg (@var{prompt}, @var{title}, @var{rowscols})
23 ## @deftypefnx {Function File} {@var{cstr} =} inputdlg (@var{prompt}, @var{title}, @var{rowscols}, @var{defaults})
24 ## Return user input from a multi-textfield dialog box in a cell array
25 ## of strings, or an empty cell array if the dialog is closed by the
26 ## Cancel button.
27 ##
28 ## Inputs:
29 ##
30 ## @table @var
31 ## @item prompt
32 ## A cell array with strings labeling each text field. This input is required.
33 ##
34 ## @item title
35 ## String to use for the caption of the dialog. The default is "Input Dialog".
36 ##
37 ## @item rowscols
38 ## Specifies the size of the text fields and can take three forms:
39 ##
40 ## @enumerate
41 ## @item a scalar value which defines the number of rows used for each
42 ## text field.
43 ##
44 ## @item a vector which defines the individual number of rows
45 ## used for each text field.
46 ##
47 ## @item a matrix which defines the individual number of rows and
48 ## columns used for each text field. In the matrix each row describes
49 ## a single text field. The first column specifies the number of input
50 ## rows to use and the second column specifies the text field width.
51 ## @end enumerate
52 ##
53 ## @item defaults
54 ## A list of default values to place in each text fields. It must be
55 ## a cell array of strings with the same size as @var{prompt}.
56 ## @end table
57 ## @seealso{errordlg, helpdlg, listdlg, msgbox, questdlg, warndlg}
58 ## @end deftypefn
59
60 function cstr = inputdlg (prompt, title = "Input Dialog", varargin)
61
62 if (nargin < 1 || nargin > 4)
63 print_usage ();
64 endif
65
66 if (iscell (prompt))
67 ## Silently extract only char elements
68 prompt = prompt(cellfun ("ischar", prompt));
69 elseif (ischar (prompt))
70 prompt = {prompt};
71 else
72 error ("inputdlg: PROMPT must be a character string or cellstr array");
73 endif
74
75 if (! ischar (title))
76 error ("inputdlg: TITLE must be a character string");
77 endif
78
79 switch (numel (varargin))
80 case 0
81 linespec = 1;
82 defaults = cellstr (cell (size (prompt)));
83
84 case 1
85 linespec = varargin{1};
86 defaults = cellstr (cell (size (prompt)));
87
88 case 2
89 linespec = varargin{1};
90 defaults = varargin{2};
91 endswitch
92
93 ## specification of text field sizes as in Matlab
94 ## Matlab requires a matrix for linespec, not a cell array...
95 ## rc = [1,10; 2,20; 3,30];
96 ## c1 c2
97 ## r1 1 10 first text field is 1x10
98 ## r2 2 20 second text field is 2x20
99 ## r3 3 30 third text field is 3x30
100 if (isscalar (linespec))
101 ## only scalar value in lineTo, copy from linespec and add defaults
102 rowscols = zeros (columns (prompt), 2);
103 ## cols
104 rowscols(:,2) = 25;
105 rowscols(:,1) = linespec;
106 elseif (isvector (linespec))
107 ## only one column in lineTo, copy from vector linespec and add defaults
108 rowscols = zeros (columns (prompt), 2);
109 ## rows from colum vector linespec, columns are set to default
110 rowscols(:,2) = 25;
111 rowscols(:,1) = linespec(:);
112 elseif (ismatrix (linespec))
113 if (rows (linespec) == columns (prompt) && columns (linespec) == 2)
114 ## (rows x columns) match, copy array linespec
115 rowscols = linespec;
116 else
117 error ("inputdlg: ROWSCOLS matrix does not match size of PROMPT");
118 endif
119
120 else
121 ## dunno
122 error ("inputdlg: unknown form of ROWSCOLS argument");
123 endif
124
125 ## convert numeric values in defaults cell array to strings
126 defs = cellfun (@num2str, defaults, "UniformOutput", false);
127 rc = arrayfun (@num2str, rowscols, "UniformOutput", false);
128
129 user_inputs = javaMethod ("inputdlg", "org.octave.JDialogBox",
130 prompt, title, rc, defs);
131
132 if (isempty (user_inputs))
133 cstr = {};
134 else
135 cstr = cellstr (user_inputs);
136 endif
137
138 endfunction