annotate scripts/plot/waitbar.m @ 13803:a2e158c3451f

provide the waitbar function * waitbar.m: New file. * plot/module.mk (plot_FCN_FILES): Add it to the list. * NEWS: Add waitbar to the list of new functions.
author John W. Eaton <jwe@octave.org>
date Thu, 03 Nov 2011 05:30:45 -0400
parents
children b3cdef33ac0e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13803
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
1 ## Copyright (C) 2011 John W. Eaton
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2 ##
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
3 ## This file is part of Octave.
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4 ##
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 ## your option) any later version.
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 ##
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 ## General Public License for more details.
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 ##
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 ## -*- texinfo -*-
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20 ## @deftypefn {Function File} {@var{h} =} waitbar (@var{frac}, @var{msg})
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21 ## @deftypefnx {Function File} {@var{h} =} waitbar (@var{frac}, @var{h}, @var{msg})
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22 ## Craete a waitbar and display an optional message.
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23 ## @end deftypefn
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25 ## Author: jwe
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27 function h = waitbar (varargin)
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 msg = " ";
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31 h = 0;
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 if (nargin > 0)
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35 frac = varargin{1};
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 varargin(1) = [];
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 if (! (isnumeric (frac) && isscalar (frac) && frac >= 0 && frac <= 1))
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 error ("waitbar: frac must be in between 0 and 1");
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 endif
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 if (numel (varargin) > 0 && ishandle (varargin{1}))
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 h = varargin{1};
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
44 varargin(1) = [];
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45 ## FIXME -- also check that H is really a waitbar?
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46 if (! isfigure (h))
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 error ("handle must be a waitbar object");
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
48 endif
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
49 endif
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51 if (numel (varargin) > 0)
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 msg = varargin{1};
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53 varargin(1) = [];
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54 if (! ischar (msg))
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55 error ("waitbar: msg must be a character string");
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56 endif
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
57 endif
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
58
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
59 if (rem (numel (varargin), 2) != 0)
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60 error ("waitbar: invalid number of property-value pairs");
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61 endif
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63 if (h)
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64 p = findobj (h, "type", "patch");
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65 if (p)
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
66 delete (p);
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
67 endif
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68 ax = findobj (h, "type", "axes");
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69 else
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
70 h = __go_figure__ (Inf, "position", [250, 500, 400, 100],
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
71 "numbertitle", "off",
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72 "handlevisibility", "callback",
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
73 varargin{:});
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
74
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75 ax = axes ("parent", h, "xtick", [], "ytick", [],
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
76 "xlim", [0, 1], "ylim", [0, 1],
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
77 "xlimmode", "manual", "ylimmode", "manual",
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
78 "position", [0.1, 0.3, 0.8, 0.2]);
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79 endif
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
81 patch (ax, [0, frac, frac, 0], [0, 0, 1, 1], [0, 0.35, 0.75]);
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82 title (ax, msg);
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
83 drawnow ();
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
84
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
85 else
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86 print_usage ();
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
87 endif
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
89 endfunction
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
91 %!demo
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
92 %! h = 0;
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
93 %! for i = 0:0.01:1
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
94 %! if (h)
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
95 %! waitbar (i, h, sprintf ("%.2f%%", 100*i));
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
96 %! else
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
97 %! h = waitbar (i, sprintf ("%.2f%%", 100*i));
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
98 %! endif
a2e158c3451f provide the waitbar function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
99 %! endfor