1 /* -------------------------------------------------------------------------- */
2 /* waitmex functions */
3 /* -------------------------------------------------------------------------- */
4
5 /* This file includes the following functions for handling waitbars in MATLAB:
6
7 waitbar_create: create a waitbar
8 waitbar_update: update a waitbar
9 waitbar_destroy: destroy a waitbar
10 waitbar_return: return a waitbar
11
12 in C MATLAB equivalent
13 ---- -----------------
14 h = waitbar_create (x,msg) h = waitbar (x,msg)
15 waitbar_update (x,h,NULL) waitbar (x,h)
16 waitbar_update (x,h,msg) waitbar (x,h,msg)
17 waitbar_destroy (h) close (h)
18 waitbar_return (h) for returning h from a mexFunction
19
20 Copyright 2007, Timothy A. Davis, http://www.suitesparse.com
21 */
22
23 #include "waitmex.h"
24
25 /* -------------------------------------------------------------------------- */
26 /* waitbar_create: create a waitbar */
27 /* -------------------------------------------------------------------------- */
28
29 /* Create a waitbar and return a point to the new waitbar. Example:
30 * h = waitbar_create (x, "Please wait...") in C is just like
31 * h = waitbar (x, 'Please wait') in MATLAB, where x is a fraction between
32 * 0 and 1.
33 */
34
waitbar_create(double fraction,char * message)35 waitbar *waitbar_create /* return a pointer to the new waitbar */
36 (
37 double fraction, /* fraction from 0 to 1 */
38 char *message /* message to display */
39 )
40 {
41 int error ;
42 waitbar *h ;
43
44 h = mxMalloc (sizeof (waitbar)) ;
45 h->fraction = mxCreateDoubleScalar (fraction) ;
46 h->message = mxCreateString (message) ;
47
48 /* h = waitbar (fraction, message) ; */
49 h->inputs [0] = h->fraction ;
50 h->inputs [1] = h->message ;
51 error = mexCallMATLAB (1, h->outputs, 2, h->inputs, "waitbar") ;
52 if (error)
53 {
54 mexErrMsgTxt ("unable to create waitbar") ;
55 }
56
57 /* save the MATLAB handle h in the waitbar struct */
58 h->handle = h->outputs [0] ;
59
60 return (h) ;
61 }
62
63 /* -------------------------------------------------------------------------- */
64 /* waitbar_update: update a waitbar */
65 /* -------------------------------------------------------------------------- */
66
67 /* Update the length of the bar in an existing waitbar. Example:
68 * waitbar_update (x, h, NULL) in C is just like waitbar (x,h) in MATLAB,
69 * where h is the handle to the existing waitbar. The message is not changed.
70 * To change the message, use waitbar_update (x, h, "new message"), which is
71 * just like waitbar (x, h, 'new message') in MATLAB.
72 */
73
waitbar_update(double fraction,waitbar * h,char * message)74 void waitbar_update
75 (
76 double fraction,
77 waitbar *h,
78 char *message
79 )
80 {
81 int error ;
82 if (h == NULL) return ; /* nothing to do */
83 (* mxGetPr (h->fraction)) = fraction ; /* update the fraction */
84 h->inputs [0] = h->fraction ; /* define the inputs x and h */
85 h->inputs [1] = h->handle ;
86
87 if (message == NULL)
88 {
89 /* use the existing message; waitbar (x,h) in MATLAB */
90 error = mexCallMATLAB (0, h->outputs, 2, h->inputs, "waitbar") ;
91 }
92 else
93 {
94 /* define a new message; waitbar (x,h,message) in MATLAB */
95 mxDestroyArray (h->message) ;
96 h->message = mxCreateString (message) ;
97 h->inputs [2] = h->message ;
98 error = mexCallMATLAB (0, h->outputs, 3, h->inputs, "waitbar") ;
99 }
100 if (error)
101 {
102 mexErrMsgTxt ("unable to update waitbar") ;
103 }
104 }
105
106 /* -------------------------------------------------------------------------- */
107 /* waitbar_destroy: destroy a waitbar */
108 /* -------------------------------------------------------------------------- */
109
110 /* Destroys a waitbar; same as close(h) in MATLAB */
111
waitbar_destroy(waitbar * h)112 void waitbar_destroy
113 (
114 waitbar *h
115 )
116 {
117 int error ;
118 if (h == NULL) return ; /* nothing to do */
119 h->inputs [0] = h->handle ;
120 mxDestroyArray (h->fraction) ; /* free the internal mxArrays */
121 mxDestroyArray (h->message) ;
122 error = mexCallMATLAB (0, h->outputs, 1, h->inputs, "close") ;
123 mxDestroyArray (h->handle) ;
124 mxFree (h) ;
125 if (error)
126 {
127 mexErrMsgTxt ("error closing waitbar") ;
128 }
129 }
130
131 /* -------------------------------------------------------------------------- */
132 /* waitbar_return: return a waitbar handle to the caller */
133 /* -------------------------------------------------------------------------- */
134
135 /* This function frees the space used internally in a mexFunction for managing
136 * the waitbar, and returns the mxArray handle to the caller. The waitbar still
137 * exists in MATLAB. Example: pargout [0] = waitbar_return (h) to return the
138 * MATLAB handle to the caller of your mexFunction.
139 */
140
waitbar_return(waitbar * h)141 mxArray *waitbar_return
142 (
143 waitbar *h
144 )
145 {
146 mxArray *handle ;
147 if (h == NULL) return (NULL) ; /* nothing to do */
148 handle = h->handle ; /* get the MATLAB handle */
149 mxDestroyArray (h->fraction) ; /* free the internal mxArrays */
150 mxDestroyArray (h->message) ;
151 mxFree (h) ;
152 return (handle) ; /* return the MATLAB handle */
153 }
154