]> Creatis software - CreaPhase.git/blob - octave_packages/zenity-0.5.7/zenity_list.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / zenity-0.5.7 / zenity_list.m
1 ## Copyright (C) 2006 Søren Hauberg
2 ## 
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ## 
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ## 
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn  {Function File} @var{s} = zenity_list(@var{title}, @var{columns}, @var{data}, @var{options1}, ...)
18 ## Displays a graphical list of data.
19 ## The variable @var{title} sets the title of the list. The variable
20 ## @var{columns} must be a cell array of strings of length N containing the headers
21 ## of the list. The variable @var{data} must be cell array of strings of
22 ## length NxM containing the data of the list.
23 ##
24 ## The code
25 ## @example
26 ## zenity_list("Age versus Height", @{"Age", "Height"@}, 
27 ## @{"10", "20"; "120cm", "180cm"@})
28 ## @end example
29 ## produces a list of the data. The user can select a row in the table, and it's
30 ## first value will be returned by the function when the user closes the window.
31 ##
32 ## It's possible to alter the behaviour of the list window by passing more than
33 ## three arguments to the funtion. Theese optional string arguments can be
34 ## @table @samp
35 ## @item checklist
36 ## The first row in the list will be a check box. The first value of each data row
37 ## must be either "TRUE" or "FALSE".
38 ## @item radiolist
39 ## The first row in the list will be a radio list. The first value of each data row
40 ## must be either "TRUE" or "FALSE".
41 ## @item editable
42 ## The values of the list will be editable by the user.
43 ## @item A numeric value
44 ## The value returned by the function will be the value of this column
45 ## of the user selected row.
46 ## @item all
47 ## The value returned by the function will be the entire row selected by the user.
48 ## @end table
49 ##
50 ## @seealso{zenity_calendar, zenity_progress, zenity_entry, zenity_message,
51 ## zenity_text_info, zenity_file_selection, zenity_notification}
52 ## @end deftypefn
53
54 function s = zenity_list(title, columns, data, varargin)
55   if (nargin < 3 || !ischar(title) || !iscellstr(columns) || !iscellstr(data))
56     print_usage();
57   endif
58   
59   checklist = radiolist = editable = "";
60   print_column = "1";
61   for i = 1:length(varargin)
62     option = varargin{i};
63     isc = ischar(option);
64     if (isc && strcmpi(option, "checklist"))
65       checklist = "--checklist";
66     elseif (isc && strcmpi(option, "radiolist"))
67       radiolist = "--radiolist";
68     elseif (isc && strcmpi(option, "editable"))
69       editable = "--editable";
70     elseif (isc && strcmpi(option, "all"))
71       print_column = "all";
72     elseif (isnumeric(option))
73       print_column = num2str(option);
74     else
75       error("zenity_list: unsupported option");
76     endif
77   endfor
78
79   columns = sprintf('--column="%s" ', columns{:});
80   data = sprintf("%s ", data{:});
81
82   cmd = sprintf('zenity --list --title="%s" %s %s %s --print-column="%s" --separator=":" %s %s', ...
83                 title, checklist, radiolist, editable, print_column, columns, data);
84   [status, output] = system(cmd);
85   if (status == 0)
86     if (length(output) > 0 && output(end) == "\n")
87       output = output(1:end-1);
88     endif
89     idx = strfind(output, ":");
90     idx = [0, idx, length(output)+1];
91     l = length(idx);
92     if (l == 2)
93       s = output;
94     else
95       s = cell(1, l-1);
96       for i = 1:l-1
97         s{i} = output((idx(i)+1):(idx(i+1)-1));
98       endfor
99     endif
100   elseif (status == 1)
101     s = "";
102   else
103     error("zenity_list: %s", output);
104   endif
105 endfunction