6406
|
1 /* |
|
2 |
|
3 Copyright (C) 2007 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
6406
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
6406
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cctype> |
7222
|
28 #include <cfloat> |
7286
|
29 #include <cstdlib> |
6406
|
30 |
|
31 #include <algorithm> |
|
32 #include <list> |
|
33 #include <map> |
|
34 #include <set> |
|
35 #include <string> |
|
36 |
7409
|
37 #include "file-ops.h" |
|
38 #include "file-stat.h" |
|
39 |
6705
|
40 #include "defun.h" |
|
41 #include "error.h" |
6595
|
42 #include "graphics.h" |
7409
|
43 #include "input.h" |
6705
|
44 #include "ov.h" |
|
45 #include "oct-obj.h" |
|
46 #include "oct-map.h" |
|
47 #include "ov-fcn-handle.h" |
|
48 #include "parse.h" |
7409
|
49 #include "toplev.h" |
7222
|
50 #include "unwind-prot.h" |
6595
|
51 |
6406
|
52 static void |
|
53 gripe_set_invalid (const std::string& pname) |
|
54 { |
|
55 error ("set: invalid value for %s property", pname.c_str ()); |
|
56 } |
|
57 |
7363
|
58 static Matrix |
|
59 jet_colormap (void) |
|
60 { |
|
61 Matrix cmap (64, 3, 0.0); |
|
62 |
|
63 for (octave_idx_type i = 0; i < 64; i++) |
|
64 { |
|
65 // This is the jet colormap. It would be nice to be able |
|
66 // to feval the jet function but since there is a static |
|
67 // property object that includes a colormap_property |
|
68 // object, we need to initialize this before main is even |
|
69 // called, so calling an interpreted function is not |
|
70 // possible. |
|
71 |
|
72 double x = i / 63.0; |
|
73 |
|
74 if (x >= 3.0/8.0 && x < 5.0/8.0) |
|
75 cmap(i,0) = 4.0 * x - 3.0/2.0; |
|
76 else if (x >= 5.0/8.0 && x < 7.0/8.0) |
|
77 cmap(i,0) = 1.0; |
|
78 else if (x >= 7.0/8.0) |
|
79 cmap(i,0) = -4.0 * x + 9.0/2.0; |
|
80 |
|
81 if (x >= 1.0/8.0 && x < 3.0/8.0) |
|
82 cmap(i,1) = 4.0 * x - 1.0/2.0; |
|
83 else if (x >= 3.0/8.0 && x < 5.0/8.0) |
|
84 cmap(i,1) = 1.0; |
|
85 else if (x >= 5.0/8.0 && x < 7.0/8.0) |
|
86 cmap(i,1) = -4.0 * x + 7.0/2.0; |
|
87 |
|
88 if (x < 1.0/8.0) |
|
89 cmap(i,2) = 4.0 * x + 1.0/2.0; |
|
90 else if (x >= 1.0/8.0 && x < 3.0/8.0) |
|
91 cmap(i,2) = 1.0; |
|
92 else if (x >= 3.0/8.0 && x < 5.0/8.0) |
|
93 cmap(i,2) = -4.0 * x + 5.0/2.0; |
|
94 } |
|
95 |
|
96 return cmap; |
|
97 } |
|
98 |
|
99 static Matrix |
|
100 default_colororder (void) |
|
101 { |
|
102 Matrix retval (7, 3, 0.0); |
|
103 |
|
104 retval(0,2) = 1.0; |
|
105 |
|
106 retval(1,1) = 0.5; |
|
107 |
|
108 retval(2,0) = 1.0; |
|
109 |
|
110 retval(3,1) = 0.75; |
|
111 retval(3,2) = 0.75; |
|
112 |
|
113 retval(4,0) = 0.75; |
|
114 retval(4,2) = 0.75; |
|
115 |
|
116 retval(5,0) = 0.75; |
|
117 retval(5,1) = 0.75; |
|
118 |
|
119 retval(6,0) = 0.25; |
|
120 retval(6,1) = 0.25; |
|
121 retval(6,2) = 0.25; |
|
122 |
|
123 return retval; |
|
124 } |
|
125 |
|
126 static Matrix |
|
127 default_lim (void) |
|
128 { |
|
129 Matrix m (1, 2, 0); |
|
130 m(1) = 1; |
|
131 return m; |
|
132 } |
|
133 |
|
134 static Matrix |
|
135 default_data (void) |
|
136 { |
|
137 Matrix retval (1, 2); |
|
138 |
|
139 retval(0) = 0; |
|
140 retval(1) = 1; |
|
141 |
|
142 return retval; |
|
143 } |
|
144 |
7367
|
145 // NOTE: "cb" is passed by value, because "function_value" method |
|
146 // is non-const; passing "cb" by const-reference is not |
|
147 // possible |
|
148 |
|
149 static void |
|
150 execute_callback (octave_value cb, const graphics_handle& h, |
|
151 const octave_value& data) |
|
152 { |
|
153 octave_value_list args; |
|
154 octave_function *fcn = 0; |
|
155 |
|
156 args(0) = h.as_octave_value (); |
|
157 args(1) = data; |
|
158 |
|
159 BEGIN_INTERRUPT_WITH_EXCEPTIONS; |
|
160 |
|
161 if (cb.is_function_handle ()) |
|
162 fcn = cb.function_value (); |
|
163 else if (cb.is_string ()) |
|
164 { |
|
165 std::string s = cb.string_value (); |
|
166 octave_value f = symbol_table::find_function (s); |
|
167 int status; |
|
168 |
|
169 if (f.is_defined ()) |
|
170 fcn = f.function_value (); |
|
171 else |
|
172 { |
|
173 eval_string (s, false, status); |
|
174 return; |
|
175 } |
|
176 } |
|
177 else if (cb.is_cell () && cb.length () > 0 |
|
178 && (cb.rows () == 1 || cb.columns () == 1) |
|
179 && cb.cell_value ()(0).is_function_handle ()) |
|
180 { |
|
181 Cell c = cb.cell_value (); |
|
182 |
|
183 fcn = c(0).function_value (); |
|
184 if (! error_state) |
|
185 { |
|
186 for (int i = 0; i < c.length () ; i++) |
|
187 args(2+i) = c(i); |
|
188 } |
|
189 } |
|
190 else |
7373
|
191 { |
|
192 std::string nm = cb.class_name (); |
|
193 error ("trying to execute non-executable object (class = %s)", |
|
194 nm.c_str ()); |
|
195 } |
7367
|
196 |
|
197 if (! error_state) |
|
198 feval (fcn, args); |
|
199 |
|
200 END_INTERRUPT_WITH_EXCEPTIONS; |
|
201 } |
|
202 |
6406
|
203 // --------------------------------------------------------------------- |
|
204 |
6705
|
205 radio_values::radio_values (const std::string& opt_string) |
6406
|
206 { |
6705
|
207 size_t beg = 0; |
|
208 size_t len = opt_string.length (); |
|
209 bool done = len == 0; |
6681
|
210 |
6705
|
211 while (! done) |
|
212 { |
|
213 size_t end = opt_string.find ('|', beg); |
6681
|
214 |
6705
|
215 if (end == std::string::npos) |
|
216 { |
|
217 end = len; |
|
218 done = true; |
|
219 } |
6681
|
220 |
6705
|
221 std::string t = opt_string.substr (beg, end-beg); |
6681
|
222 |
6705
|
223 // Might want more error checking here... |
|
224 if (t[0] == '{') |
|
225 { |
|
226 t = t.substr (1, t.length () - 2); |
|
227 default_val = t; |
|
228 } |
|
229 else if (beg == 0) // ensure default value |
|
230 default_val = t; |
6681
|
231 |
6705
|
232 possible_vals.insert (t); |
6681
|
233 |
6705
|
234 beg = end + 1; |
|
235 } |
|
236 } |
6681
|
237 |
6705
|
238 bool |
6761
|
239 color_values::str2rgb (std::string str) |
6681
|
240 { |
6705
|
241 double tmp_rgb[3] = {0, 0, 0}; |
|
242 bool retval = true; |
6761
|
243 unsigned int len = str.length(); |
6681
|
244 |
6925
|
245 std::transform (str.begin (), str.end (), str.begin (), tolower); |
|
246 |
6761
|
247 if (str.compare(0, len, "blue", 0, len) == 0) |
|
248 tmp_rgb[2] = 1; |
6925
|
249 else if (str.compare(0, len, "black", 0, len) == 0 || |
|
250 str.compare(0, len, "k", 0, len) == 0) |
6761
|
251 tmp_rgb[0] = tmp_rgb[1] = tmp_rgb[2] = 0; |
|
252 else if (str.compare(0, len, "red", 0, len) == 0) |
|
253 tmp_rgb[0] = 1; |
|
254 else if (str.compare(0, len, "green", 0, len) == 0) |
|
255 tmp_rgb[1] = 1; |
|
256 else if (str.compare(0, len, "yellow", 0, len) == 0) |
|
257 tmp_rgb[0] = tmp_rgb[1] = 1; |
|
258 else if (str.compare(0, len, "magenta", 0, len) == 0) |
|
259 tmp_rgb[0] = tmp_rgb[2] = 1; |
|
260 else if (str.compare(0, len, "cyan", 0, len) == 0) |
|
261 tmp_rgb[1] = tmp_rgb[2] = 1; |
6925
|
262 else if (str.compare(0, len, "white", 0, len) == 0 || |
|
263 str.compare(0, len, "w", 0, len) == 0) |
6761
|
264 tmp_rgb[0] = tmp_rgb[1] = tmp_rgb[2] = 1; |
|
265 else |
|
266 retval = false; |
6406
|
267 |
6705
|
268 if (retval) |
|
269 { |
|
270 for (int i = 0; i < 3; i++) |
7363
|
271 xrgb(i) = tmp_rgb[i]; |
6705
|
272 } |
6563
|
273 |
6705
|
274 return retval; |
|
275 } |
6681
|
276 |
7363
|
277 void |
|
278 color_property::set (const octave_value& val) |
6790
|
279 { |
|
280 if (val.is_string ()) |
|
281 { |
|
282 std::string s = val.string_value (); |
|
283 |
|
284 if (! s.empty ()) |
|
285 { |
6898
|
286 if (radio_val.contains (s)) |
6790
|
287 { |
|
288 current_val = s; |
|
289 current_type = radio_t; |
|
290 } |
|
291 else |
|
292 { |
|
293 color_values col (s); |
|
294 if (! error_state) |
|
295 { |
|
296 color_val = col; |
|
297 current_type = color_t; |
|
298 } |
|
299 else |
7363
|
300 error ("invalid value for color property \"%s\" (value = %s)", |
|
301 get_name ().c_str (), s.c_str ()); |
6790
|
302 } |
|
303 } |
|
304 else |
7363
|
305 error ("invalid value for color property \"%s\"", |
|
306 get_name ().c_str ()); |
6790
|
307 } |
|
308 else if (val.is_real_matrix ()) |
|
309 { |
|
310 Matrix m = val.matrix_value (); |
|
311 |
|
312 if (m.numel () == 3) |
|
313 { |
|
314 color_values col (m (0), m (1), m(2)); |
|
315 if (! error_state) |
|
316 { |
|
317 color_val = col; |
|
318 current_type = color_t; |
|
319 } |
|
320 } |
|
321 else |
7363
|
322 error ("invalid value for color property \"%s\"", |
|
323 get_name ().c_str ()); |
6790
|
324 } |
|
325 else |
7363
|
326 error ("invalid value for color property \"%s\"", |
|
327 get_name ().c_str ()); |
6790
|
328 } |
|
329 |
7363
|
330 bool |
|
331 array_property::validate (const octave_value& v) |
|
332 { |
7364
|
333 bool xok = false; |
7363
|
334 |
|
335 // FIXME: should we always support []? |
|
336 if (v.is_empty () && v.is_double_type ()) |
|
337 return true; |
|
338 |
|
339 // check value type |
|
340 if (type_constraints.size () > 0) |
|
341 { |
|
342 for (std::list<std::string>::const_iterator it = type_constraints.begin (); |
7364
|
343 ! xok && it != type_constraints.end (); ++it) |
7363
|
344 if ((*it) == v.type_name ()) |
7364
|
345 xok = true; |
7363
|
346 } |
|
347 else |
7364
|
348 xok = v.is_double_type (); |
7363
|
349 |
7364
|
350 if (xok) |
7363
|
351 { |
|
352 dim_vector vdims = v.dims (); |
|
353 int vlen = vdims.length (); |
|
354 |
7364
|
355 xok = false; |
7363
|
356 |
|
357 // check value size |
|
358 if (size_constraints.size () > 0) |
|
359 for (std::list<dim_vector>::const_iterator it = size_constraints.begin (); |
7364
|
360 ! xok && it != size_constraints.end (); ++it) |
7363
|
361 { |
|
362 dim_vector itdims = (*it); |
|
363 |
|
364 if (itdims.length () == vlen) |
|
365 { |
7364
|
366 xok = true; |
7363
|
367 |
7364
|
368 for (int i = 0; xok && i < vlen; i++) |
7363
|
369 if (itdims(i) >= 0 && itdims(i) != vdims(i)) |
7364
|
370 xok = false; |
7363
|
371 } |
|
372 } |
|
373 else |
|
374 return true; |
|
375 } |
|
376 |
7364
|
377 return xok; |
7363
|
378 } |
|
379 |
|
380 void |
|
381 handle_property::set (const octave_value& v) |
|
382 { |
|
383 double dv = v.double_value (); |
|
384 |
|
385 if (! error_state) |
|
386 { |
|
387 graphics_handle gh = gh_manager::lookup (dv); |
|
388 |
|
389 if (xisnan (gh.value ()) || gh.ok ()) |
|
390 current_val = gh; |
|
391 else |
|
392 error ("set: invalid graphics handle (= %g) for property \"%s\"", |
|
393 dv, get_name ().c_str ()); |
|
394 } |
|
395 else |
|
396 error ("set: invalid graphics handle for property \"%s\"", |
|
397 get_name ().c_str ()); |
|
398 } |
|
399 |
|
400 bool |
7367
|
401 callback_property::validate (const octave_value& v) const |
7363
|
402 { |
7367
|
403 // case 1: function handle |
|
404 // case 2: cell array with first element being a function handle |
|
405 // case 3: string corresponding to known function name |
|
406 // case 4: evaluatable string |
|
407 // case 5: empty matrix |
|
408 |
|
409 if (v.is_function_handle ()) |
|
410 return true; |
|
411 else if (v.is_string ()) |
|
412 // complete validation will be done at execution-time |
|
413 return true; |
|
414 else if (v.is_cell () && v.length () > 0 |
|
415 && (v.rows() == 1 || v.columns () == 1) |
|
416 && v.cell_value ()(0).is_function_handle ()) |
|
417 return true; |
|
418 else if (v.is_empty ()) |
|
419 return true; |
|
420 |
|
421 return false; |
7363
|
422 } |
|
423 |
|
424 void |
7367
|
425 callback_property::execute (const octave_value& data) const |
7363
|
426 { |
7367
|
427 if (callback.is_defined () && ! callback.is_empty ()) |
|
428 execute_callback (callback, get_parent (), data); |
7363
|
429 } |
|
430 |
|
431 // --------------------------------------------------------------------- |
6681
|
432 |
6705
|
433 void |
7189
|
434 property_list::set (const caseless_str& name, const octave_value& val) |
6681
|
435 { |
6705
|
436 size_t offset = 0; |
6681
|
437 |
6705
|
438 size_t len = name.length (); |
6681
|
439 |
6705
|
440 if (len > 4) |
|
441 { |
7189
|
442 caseless_str pfx = name.substr (0, 4); |
6681
|
443 |
6705
|
444 if (pfx.compare ("axes") || pfx.compare ("line") |
|
445 || pfx.compare ("text")) |
|
446 offset = 4; |
|
447 else if (len > 5) |
|
448 { |
|
449 pfx = name.substr (0, 5); |
6681
|
450 |
6807
|
451 if (pfx.compare ("image") || pfx.compare ("patch")) |
6705
|
452 offset = 5; |
|
453 else if (len > 6) |
|
454 { |
|
455 pfx = name.substr (0, 6); |
6681
|
456 |
6705
|
457 if (pfx.compare ("figure")) |
|
458 offset = 6; |
|
459 else if (len > 7) |
|
460 { |
|
461 pfx = name.substr (0, 7); |
6681
|
462 |
6705
|
463 if (pfx.compare ("surface")) |
|
464 offset = 7; |
|
465 } |
|
466 } |
|
467 } |
6681
|
468 |
6705
|
469 if (offset > 0) |
|
470 { |
|
471 // FIXME -- should we validate property names and values here? |
6406
|
472 |
6705
|
473 std::string pname = name.substr (offset); |
6406
|
474 |
6705
|
475 std::transform (pfx.begin (), pfx.end (), pfx.begin (), tolower); |
|
476 std::transform (pname.begin (), pname.end (), pname.begin (), tolower); |
6406
|
477 |
6705
|
478 bool remove = false; |
|
479 if (val.is_string ()) |
|
480 { |
7189
|
481 caseless_str tval = val.string_value (); |
6406
|
482 |
6705
|
483 remove = tval.compare ("remove"); |
|
484 } |
6406
|
485 |
6705
|
486 pval_map_type& pval_map = plist_map[pfx]; |
6406
|
487 |
6705
|
488 if (remove) |
|
489 { |
|
490 pval_map_iterator p = pval_map.find (pname); |
6406
|
491 |
6705
|
492 if (p != pval_map.end ()) |
|
493 pval_map.erase (p); |
|
494 } |
|
495 else |
|
496 pval_map[pname] = val; |
|
497 } |
|
498 } |
6406
|
499 |
6705
|
500 if (offset == 0) |
|
501 error ("invalid default property specification"); |
|
502 } |
6406
|
503 |
6705
|
504 octave_value |
7189
|
505 property_list::lookup (const caseless_str& name) const |
6705
|
506 { |
|
507 octave_value retval; |
6406
|
508 |
6705
|
509 size_t offset = 0; |
6406
|
510 |
6705
|
511 size_t len = name.length (); |
6406
|
512 |
6705
|
513 if (len > 4) |
|
514 { |
7189
|
515 caseless_str pfx = name.substr (0, 4); |
6406
|
516 |
6705
|
517 if (pfx.compare ("axes") || pfx.compare ("line") |
|
518 || pfx.compare ("text")) |
|
519 offset = 4; |
|
520 else if (len > 5) |
|
521 { |
|
522 pfx = name.substr (0, 5); |
6406
|
523 |
6807
|
524 if (pfx.compare ("image") || pfx.compare ("patch")) |
6705
|
525 offset = 5; |
|
526 else if (len > 6) |
|
527 { |
|
528 pfx = name.substr (0, 6); |
6406
|
529 |
6705
|
530 if (pfx.compare ("figure")) |
|
531 offset = 6; |
|
532 else if (len > 7) |
|
533 { |
|
534 pfx = name.substr (0, 7); |
6406
|
535 |
6705
|
536 if (pfx.compare ("surface")) |
|
537 offset = 7; |
|
538 } |
|
539 } |
|
540 } |
6406
|
541 |
6705
|
542 if (offset > 0) |
|
543 { |
|
544 std::string pname = name.substr (offset); |
6406
|
545 |
6705
|
546 std::transform (pfx.begin (), pfx.end (), pfx.begin (), tolower); |
|
547 std::transform (pname.begin (), pname.end (), pname.begin (), tolower); |
6406
|
548 |
6705
|
549 plist_map_const_iterator p = find (pfx); |
6432
|
550 |
6705
|
551 if (p != end ()) |
|
552 { |
|
553 const pval_map_type& pval_map = p->second; |
6406
|
554 |
6705
|
555 pval_map_const_iterator q = pval_map.find (pname); |
6406
|
556 |
6705
|
557 if (q != pval_map.end ()) |
|
558 retval = q->second; |
|
559 } |
|
560 } |
|
561 } |
6406
|
562 |
6705
|
563 return retval; |
|
564 } |
6406
|
565 |
6705
|
566 Octave_map |
|
567 property_list::as_struct (const std::string& prefix_arg) const |
|
568 { |
|
569 Octave_map m; |
6406
|
570 |
6705
|
571 for (plist_map_const_iterator p = begin (); p != end (); p++) |
|
572 { |
|
573 std::string prefix = prefix_arg + p->first; |
6406
|
574 |
6705
|
575 const pval_map_type pval_map = p->second; |
6406
|
576 |
6705
|
577 for (pval_map_const_iterator q = pval_map.begin (); |
|
578 q != pval_map.end (); |
|
579 q++) |
|
580 m.assign (prefix + q->first, q->second); |
|
581 } |
6406
|
582 |
6705
|
583 return m; |
|
584 } |
6432
|
585 |
6874
|
586 graphics_handle::graphics_handle (const octave_value& a) |
|
587 : val (octave_NaN) |
|
588 { |
|
589 if (a.is_empty ()) |
|
590 /* do nothing */; |
|
591 else |
|
592 { |
|
593 double tval = a.double_value (); |
|
594 |
|
595 if (! error_state) |
|
596 val = tval; |
|
597 else |
|
598 error ("invalid graphics handle"); |
|
599 } |
|
600 } |
|
601 |
6705
|
602 void |
|
603 graphics_object::set (const octave_value_list& args) |
|
604 { |
|
605 int nargin = args.length (); |
6406
|
606 |
6705
|
607 if (nargin == 0) |
|
608 rep->defaults (); |
|
609 else if (nargin % 2 == 0) |
|
610 { |
|
611 for (int i = 0; i < nargin; i += 2) |
|
612 { |
7189
|
613 caseless_str name = args(i).string_value (); |
6406
|
614 |
6705
|
615 if (! error_state) |
|
616 { |
|
617 octave_value val = args(i+1); |
6406
|
618 |
6705
|
619 if (val.is_string ()) |
|
620 { |
7189
|
621 caseless_str tval = val.string_value (); |
6406
|
622 |
6705
|
623 if (tval.compare ("default")) |
|
624 val = get_default (name); |
|
625 else if (tval.compare ("factory")) |
|
626 val = get_factory_default (name); |
|
627 } |
6406
|
628 |
6705
|
629 if (error_state) |
|
630 break; |
6406
|
631 |
6705
|
632 rep->set (name, val); |
|
633 } |
|
634 else |
|
635 error ("set: expecting argument %d to be a property name", i); |
|
636 } |
|
637 } |
|
638 else |
|
639 error ("set: invalid number of arguments"); |
|
640 } |
6406
|
641 |
|
642 |
6705
|
643 graphics_handle |
|
644 gh_manager::get_handle (const std::string& go_name) |
|
645 { |
|
646 graphics_handle retval; |
6406
|
647 |
6705
|
648 if (go_name == "figure") |
|
649 { |
|
650 // We always want the lowest unused figure number. |
6406
|
651 |
6705
|
652 retval = 1; |
6425
|
653 |
6705
|
654 while (handle_map.find (retval) != handle_map.end ()) |
|
655 retval++; |
|
656 } |
|
657 else |
|
658 { |
|
659 free_list_iterator p = handle_free_list.begin (); |
6406
|
660 |
6705
|
661 if (p != handle_free_list.end ()) |
|
662 { |
|
663 retval = *p; |
|
664 handle_free_list.erase (p); |
|
665 } |
|
666 else |
7286
|
667 { |
|
668 static double maxrand = RAND_MAX + 2.0; |
|
669 |
|
670 retval = graphics_handle (next_handle); |
|
671 |
7304
|
672 next_handle = ceil (next_handle) - 1.0 - (rand () + 1.0) / maxrand; |
7286
|
673 } |
6705
|
674 } |
6406
|
675 |
6705
|
676 return retval; |
|
677 } |
6406
|
678 |
6705
|
679 void |
|
680 gh_manager::do_free (const graphics_handle& h) |
|
681 { |
7056
|
682 if (h.ok ()) |
6705
|
683 { |
6874
|
684 if (h.value () != 0) |
6705
|
685 { |
6874
|
686 iterator p = handle_map.find (h); |
|
687 |
|
688 if (p != handle_map.end ()) |
|
689 { |
7403
|
690 p->second.get_properties ().set_beingdeleted (true); |
|
691 p->second.get_properties ().execute_deletefcn (); |
7367
|
692 |
6874
|
693 handle_map.erase (p); |
|
694 |
|
695 if (h.value () < 0) |
|
696 handle_free_list.insert (h); |
|
697 } |
|
698 else |
|
699 error ("graphics_handle::free: invalid object %g", h.value ()); |
6705
|
700 } |
|
701 else |
6874
|
702 error ("graphics_handle::free: can't delete root figure"); |
6705
|
703 } |
|
704 } |
6406
|
705 |
|
706 gh_manager *gh_manager::instance = 0; |
|
707 |
|
708 static void |
7189
|
709 xset (const graphics_handle& h, const caseless_str& name, |
6406
|
710 const octave_value& val) |
|
711 { |
|
712 graphics_object obj = gh_manager::get_object (h); |
|
713 obj.set (name, val); |
|
714 } |
|
715 |
|
716 static void |
|
717 xset (const graphics_handle& h, const octave_value_list& args) |
|
718 { |
|
719 if (args.length () > 0) |
|
720 { |
|
721 graphics_object obj = gh_manager::get_object (h); |
|
722 obj.set (args); |
|
723 } |
|
724 } |
|
725 |
|
726 |
|
727 static octave_value |
7189
|
728 xget (const graphics_handle& h, const caseless_str& name) |
6406
|
729 { |
|
730 graphics_object obj = gh_manager::get_object (h); |
|
731 return obj.get (name); |
|
732 } |
|
733 |
|
734 static graphics_handle |
|
735 reparent (const octave_value& ov, const std::string& who, |
|
736 const std::string& property, const graphics_handle& new_parent, |
|
737 bool adopt = true) |
|
738 { |
|
739 graphics_handle h = octave_NaN; |
|
740 |
|
741 double val = ov.double_value (); |
|
742 |
|
743 if (! error_state) |
|
744 { |
|
745 h = gh_manager::lookup (val); |
|
746 |
7056
|
747 if (h.ok ()) |
6406
|
748 { |
|
749 graphics_object obj = gh_manager::get_object (h); |
|
750 |
|
751 graphics_handle parent_h = obj.get_parent (); |
|
752 |
|
753 graphics_object parent_obj = gh_manager::get_object (parent_h); |
|
754 |
|
755 parent_obj.remove_child (h); |
|
756 |
|
757 if (adopt) |
6874
|
758 obj.set ("parent", new_parent.value ()); |
6406
|
759 else |
|
760 obj.reparent (new_parent); |
|
761 } |
|
762 else |
|
763 error ("%s: invalid graphics handle (= %g) for %s", |
|
764 who.c_str (), val, property.c_str ()); |
|
765 } |
|
766 else |
|
767 error ("%s: expecting %s to be a graphics handle", |
|
768 who.c_str (), property.c_str ()); |
|
769 |
|
770 return h; |
|
771 } |
|
772 |
|
773 // This function is NOT equivalent to the scripting language function gcf. |
|
774 graphics_handle |
|
775 gcf (void) |
|
776 { |
|
777 octave_value val = xget (0, "currentfigure"); |
|
778 |
|
779 return val.is_empty () ? octave_NaN : val.double_value (); |
|
780 } |
|
781 |
|
782 // This function is NOT equivalent to the scripting language function gca. |
|
783 graphics_handle |
|
784 gca (void) |
|
785 { |
|
786 octave_value val = xget (gcf (), "currentaxes"); |
|
787 |
|
788 return val.is_empty () ? octave_NaN : val.double_value (); |
|
789 } |
|
790 |
|
791 static void |
|
792 adopt (const graphics_handle& p, const graphics_handle& h) |
|
793 { |
|
794 graphics_object parent_obj = gh_manager::get_object (p); |
|
795 |
|
796 parent_obj.adopt (h); |
|
797 } |
|
798 |
|
799 static bool |
7056
|
800 is_handle (const graphics_handle& h) |
|
801 { |
|
802 return h.ok (); |
|
803 } |
|
804 |
|
805 static bool |
6406
|
806 is_handle (double val) |
|
807 { |
6874
|
808 graphics_handle h = gh_manager::lookup (val); |
|
809 |
|
810 return h.ok (); |
6406
|
811 } |
|
812 |
|
813 static bool |
|
814 is_handle (const octave_value& val) |
|
815 { |
7142
|
816 return val.is_real_scalar () && is_handle (val.double_value ()); |
6406
|
817 } |
|
818 |
|
819 static bool |
|
820 is_figure (double val) |
|
821 { |
|
822 graphics_object obj = gh_manager::get_object (val); |
|
823 |
|
824 return obj && obj.isa ("figure"); |
|
825 } |
|
826 |
7370
|
827 static void |
|
828 xcreatefcn (const graphics_handle& h) |
|
829 { |
|
830 graphics_object obj = gh_manager::get_object (h); |
|
831 obj.get_properties ().execute_createfcn (); |
|
832 } |
|
833 |
6406
|
834 // --------------------------------------------------------------------- |
|
835 |
|
836 static int |
|
837 compare (const void *a_arg, const void *b_arg) |
|
838 { |
|
839 double a = *(static_cast<const double *> (a_arg)); |
|
840 double b = *(static_cast<const double *> (b_arg)); |
|
841 |
|
842 return a > b ? 1 : (a < b) ? -1 : 0; |
|
843 } |
|
844 |
|
845 static Matrix |
|
846 maybe_set_children (const Matrix& kids, const octave_value& val) |
|
847 { |
|
848 const Matrix new_kids = val.matrix_value (); |
|
849 |
|
850 bool ok = true; |
|
851 |
|
852 if (! error_state) |
|
853 { |
|
854 if (kids.numel () == new_kids.numel ()) |
|
855 { |
|
856 Matrix t1 = kids; |
|
857 Matrix t2 = new_kids; |
|
858 |
|
859 t1.qsort (compare); |
|
860 t2.qsort (compare); |
|
861 |
|
862 if (t1 != t2) |
|
863 ok = false; |
|
864 } else |
|
865 ok = false; |
|
866 |
|
867 if (! ok) |
|
868 error ("set: new children must be a permutation of existing children"); |
|
869 } |
|
870 else |
|
871 { |
|
872 ok = false; |
|
873 error ("set: expecting children to be array of graphics handles"); |
|
874 } |
|
875 |
|
876 return ok ? new_kids : kids; |
|
877 } |
|
878 |
6705
|
879 void |
|
880 base_properties::set_from_list (base_graphics_object& obj, |
|
881 property_list& defaults) |
6406
|
882 { |
6705
|
883 std::string go_name = graphics_object_name (); |
6406
|
884 |
6705
|
885 property_list::plist_map_const_iterator p = defaults.find (go_name); |
6406
|
886 |
6705
|
887 if (p != defaults.end ()) |
|
888 { |
|
889 const property_list::pval_map_type pval_map = p->second; |
6406
|
890 |
6705
|
891 for (property_list::pval_map_const_iterator q = pval_map.begin (); |
|
892 q != pval_map.end (); |
|
893 q++) |
|
894 { |
|
895 std::string pname = q->first; |
6406
|
896 |
6705
|
897 obj.set (pname, q->second); |
6406
|
898 |
6705
|
899 if (error_state) |
|
900 { |
|
901 error ("error setting default property %s", pname.c_str ()); |
|
902 break; |
|
903 } |
|
904 } |
|
905 } |
|
906 } |
6406
|
907 |
7363
|
908 octave_value |
|
909 base_properties::get (const caseless_str& name) const |
|
910 { |
|
911 octave_value retval; |
|
912 |
|
913 if (name.compare ("tag")) |
|
914 retval = get_tag (); |
|
915 else if (name.compare ("type")) |
|
916 retval = get_type (); |
|
917 else if (name.compare ("__modified__")) |
|
918 retval = is_modified (); |
|
919 else if (name.compare ("parent")) |
|
920 retval = get_parent ().as_octave_value (); |
|
921 else if (name.compare ("children")) |
|
922 retval = children; |
7366
|
923 else if (name.compare ("busyaction")) |
|
924 retval = get_busyaction (); |
|
925 else if (name.compare ("buttondownfcn")) |
|
926 retval = get_buttondownfcn (); |
|
927 else if (name.compare ("clipping")) |
|
928 retval = get_clipping (); |
|
929 else if (name.compare ("createfcn")) |
|
930 retval = get_createfcn (); |
|
931 else if (name.compare ("deletefcn")) |
|
932 retval = get_deletefcn (); |
|
933 else if (name.compare ("handlevisibility")) |
|
934 retval = get_handlevisibility (); |
|
935 else if (name.compare ("hittest")) |
|
936 retval = get_hittest (); |
|
937 else if (name.compare ("interruptible")) |
|
938 retval = get_interruptible (); |
|
939 else if (name.compare ("selected")) |
|
940 retval = get_selected (); |
|
941 else if (name.compare ("selectionhighlight")) |
|
942 retval = get_selectionhighlight (); |
|
943 else if (name.compare ("uicontextmenu")) |
|
944 retval = get_uicontextmenu (); |
|
945 else if (name.compare ("userdata")) |
|
946 retval = get_userdata (); |
|
947 else if (name.compare ("visible")) |
|
948 retval = get_visible (); |
7403
|
949 else if (name.compare ("beingdeleted")) |
|
950 retval = get_beingdeleted (); |
7363
|
951 else |
|
952 { |
|
953 std::map<caseless_str, property>::const_iterator it = all_props.find (name); |
|
954 |
|
955 if (it != all_props.end ()) |
|
956 retval = it->second.get (); |
|
957 else |
|
958 error ("get: unknown property \"%s\"", name.c_str ()); |
|
959 } |
|
960 |
|
961 return retval; |
|
962 } |
|
963 |
|
964 octave_value |
7379
|
965 base_properties::get (bool all) const |
7363
|
966 { |
|
967 Octave_map m; |
|
968 |
|
969 for (std::map<caseless_str, property>::const_iterator it = all_props.begin (); |
|
970 it != all_props.end (); ++it) |
7379
|
971 if (all || ! it->second.is_hidden ()) |
|
972 m.assign (it->second.get_name (), it->second.get ()); |
7363
|
973 |
|
974 m.assign ("tag", get_tag ()); |
|
975 m.assign ("type", get_type ()); |
7379
|
976 if (all) |
|
977 m.assign ("__modified__", is_modified ()); |
7363
|
978 m.assign ("parent", get_parent ().as_octave_value ()); |
|
979 m.assign ("children", children); |
7366
|
980 m.assign ("busyaction", get_busyaction ()); |
|
981 m.assign ("buttondownfcn", get_buttondownfcn ()); |
|
982 m.assign ("clipping", get_clipping ()); |
|
983 m.assign ("createfcn", get_createfcn ()); |
|
984 m.assign ("deletefcn", get_deletefcn ()); |
|
985 m.assign ("handlevisibility", get_handlevisibility ()); |
|
986 m.assign ("hittest", get_hittest ()); |
|
987 m.assign ("interruptible", get_interruptible ()); |
|
988 m.assign ("selected", get_selected ()); |
|
989 m.assign ("selectionhighlight", get_selectionhighlight ()); |
|
990 m.assign ("uicontextmenu", get_uicontextmenu ()); |
|
991 m.assign ("userdata", get_userdata ()); |
|
992 m.assign ("visible", get_visible ()); |
7403
|
993 m.assign ("beingdeleted", get_beingdeleted ()); |
7363
|
994 |
|
995 return m; |
|
996 } |
|
997 |
|
998 void |
|
999 base_properties::set (const caseless_str& name, const octave_value& val) |
|
1000 { |
|
1001 if (name.compare ("tag")) |
|
1002 set_tag (val); |
|
1003 else if (name.compare ("__modified__")) |
|
1004 __modified__ = val; |
|
1005 else if (name.compare ("parent")) |
|
1006 set_parent (val); |
|
1007 else if (name.compare ("children")) |
|
1008 maybe_set_children (children, val); |
7366
|
1009 else if (name.compare ("busyaction")) |
|
1010 set_busyaction (val); |
|
1011 else if (name.compare ("buttondownfcn")) |
|
1012 set_buttondownfcn (val); |
|
1013 else if (name.compare ("clipping")) |
|
1014 set_clipping (val); |
|
1015 else if (name.compare ("createfcn")) |
|
1016 set_createfcn (val); |
|
1017 else if (name.compare ("deletefcn")) |
|
1018 set_deletefcn (val); |
|
1019 else if (name.compare ("handlevisibility")) |
|
1020 set_handlevisibility (val); |
|
1021 else if (name.compare ("hittest")) |
|
1022 set_hittest (val); |
|
1023 else if (name.compare ("interruptible")) |
|
1024 set_interruptible (val); |
|
1025 else if (name.compare ("selected")) |
|
1026 set_selected (val); |
|
1027 else if (name.compare ("selectionhighlight")) |
|
1028 set_selectionhighlight (val); |
|
1029 else if (name.compare ("uicontextmenu")) |
|
1030 set_uicontextmenu (val); |
|
1031 else if (name.compare ("userdata")) |
|
1032 set_userdata (val); |
|
1033 else if (name.compare ("visible")) |
|
1034 set_visible (val); |
7363
|
1035 else |
|
1036 { |
|
1037 std::map<caseless_str, property>::iterator it = all_props.find (name); |
|
1038 |
|
1039 if (it != all_props.end ()) |
|
1040 it->second.set (val); |
|
1041 else |
|
1042 error ("set: unknown property \"%s\"", name.c_str ()); |
|
1043 } |
|
1044 |
|
1045 if (! error_state && ! name.compare ("__modified__")) |
|
1046 mark_modified (); |
|
1047 } |
|
1048 |
|
1049 property |
|
1050 base_properties::get_property (const caseless_str& name) const |
|
1051 { |
|
1052 std::map<caseless_str, property>::const_iterator it = all_props.find (name); |
|
1053 |
|
1054 if (it == all_props.end ()) |
|
1055 return property (); |
|
1056 else |
|
1057 return it->second; |
|
1058 } |
|
1059 |
6705
|
1060 void |
|
1061 base_properties::remove_child (const graphics_handle& h) |
6406
|
1062 { |
6705
|
1063 octave_idx_type k = -1; |
|
1064 octave_idx_type n = children.numel (); |
|
1065 for (octave_idx_type i = 0; i < n; i++) |
6406
|
1066 { |
6874
|
1067 if (h.value () == children(i)) |
6406
|
1068 { |
6705
|
1069 k = i; |
|
1070 break; |
6406
|
1071 } |
|
1072 } |
|
1073 |
6705
|
1074 if (k >= 0) |
6406
|
1075 { |
6705
|
1076 Matrix new_kids (1, n-1); |
|
1077 octave_idx_type j = 0; |
|
1078 for (octave_idx_type i = 0; i < n; i++) |
|
1079 { |
|
1080 if (i != k) |
|
1081 new_kids(j++) = children(i); |
|
1082 } |
|
1083 children = new_kids; |
7378
|
1084 mark_modified (); |
6406
|
1085 } |
6705
|
1086 } |
6406
|
1087 |
6836
|
1088 void |
|
1089 base_properties::set_parent (const octave_value& val) |
6705
|
1090 { |
|
1091 double tmp = val.double_value (); |
6406
|
1092 |
6705
|
1093 graphics_handle new_parent = octave_NaN; |
6406
|
1094 |
6705
|
1095 if (! error_state) |
|
1096 { |
|
1097 new_parent = gh_manager::lookup (tmp); |
6406
|
1098 |
7056
|
1099 if (new_parent.ok ()) |
6705
|
1100 { |
7363
|
1101 graphics_object parent_obj = gh_manager::get_object (get_parent ()); |
6406
|
1102 |
6705
|
1103 parent_obj.remove_child (__myhandle__); |
6406
|
1104 |
7363
|
1105 parent = new_parent.as_octave_value (); |
6406
|
1106 |
7363
|
1107 ::adopt (parent.handle_value (), __myhandle__); |
6705
|
1108 } |
|
1109 else |
|
1110 error ("set: invalid graphics handle (= %g) for parent", tmp); |
|
1111 } |
|
1112 else |
|
1113 error ("set: expecting parent to be a graphics handle"); |
|
1114 } |
6432
|
1115 |
6705
|
1116 void |
6836
|
1117 base_properties::mark_modified (void) |
|
1118 { |
7363
|
1119 __modified__ = "on"; |
|
1120 graphics_object parent_obj = gh_manager::get_object (get_parent ()); |
7379
|
1121 if (parent_obj) |
|
1122 parent_obj.mark_modified (); |
6836
|
1123 } |
|
1124 |
|
1125 void |
|
1126 base_properties::override_defaults (base_graphics_object& obj) |
|
1127 { |
7363
|
1128 graphics_object parent_obj = gh_manager::get_object (get_parent ()); |
6836
|
1129 parent_obj.override_defaults (obj); |
|
1130 } |
|
1131 |
|
1132 void |
7214
|
1133 base_properties::update_axis_limits (const std::string& axis_type) const |
|
1134 { |
7363
|
1135 graphics_handle h = (get_type () == "axes") ? __myhandle__ : get_parent (); |
7214
|
1136 |
|
1137 graphics_object obj = gh_manager::get_object (h); |
|
1138 |
|
1139 if (obj.isa ("axes")) |
|
1140 obj.update_axis_limits (axis_type); |
|
1141 } |
|
1142 |
|
1143 void |
6836
|
1144 base_properties::delete_children (void) |
|
1145 { |
|
1146 octave_idx_type n = children.numel (); |
|
1147 |
|
1148 for (octave_idx_type i = 0; i < n; i++) |
|
1149 gh_manager::free (children(i)); |
|
1150 } |
|
1151 |
7363
|
1152 // --------------------------------------------------------------------- |
|
1153 |
7408
|
1154 class gnuplot_backend : public base_graphics_backend |
|
1155 { |
|
1156 public: |
|
1157 gnuplot_backend (void) |
|
1158 : base_graphics_backend ("gnuplot") { } |
|
1159 |
|
1160 ~gnuplot_backend (void) { } |
|
1161 |
|
1162 bool is_valid (void) const { return true; } |
|
1163 |
|
1164 void close_figure (const octave_value& pstream) const |
|
1165 { |
|
1166 if (! pstream.is_empty()) |
|
1167 { |
|
1168 octave_value_list args; |
|
1169 args(1) = "\nquit;\n"; |
|
1170 args(0) = pstream; |
|
1171 feval ("fputs", args); |
|
1172 args.resize (1); |
|
1173 feval ("fflush", args); |
|
1174 feval ("pclose", args); |
|
1175 } |
|
1176 } |
|
1177 |
|
1178 void redraw_figure (const graphics_handle& fh) const |
|
1179 { |
|
1180 octave_value_list args; |
|
1181 args(0) = fh.as_octave_value (); |
|
1182 feval ("gnuplot_drawnow", args); |
|
1183 } |
|
1184 |
|
1185 void print_figure (const graphics_handle& fh, const std::string& term, |
|
1186 const std::string& file, bool mono, |
|
1187 const std::string& debug_file) const |
|
1188 { |
|
1189 octave_value_list args; |
|
1190 if (! debug_file.empty ()) |
|
1191 args(4) = debug_file; |
7409
|
1192 args(3) = mono; |
|
1193 args(2) = file; |
|
1194 args(1) = term; |
|
1195 args(0) = fh.as_octave_value (); |
7408
|
1196 feval ("gnuplot_drawnow", args); |
|
1197 } |
|
1198 |
7409
|
1199 Matrix get_canvas_size (const graphics_handle&) const |
7408
|
1200 { return Matrix (1, 2, 0.0); } |
|
1201 }; |
|
1202 |
|
1203 graphics_backend |
|
1204 graphics_backend::default_backend (void) |
|
1205 { |
|
1206 if (available_backends.size () == 0) |
|
1207 register_backend (new gnuplot_backend ()); |
|
1208 |
|
1209 return available_backends["gnuplot"]; |
|
1210 } |
|
1211 |
|
1212 std::map<std::string, graphics_backend> graphics_backend::available_backends; |
|
1213 |
|
1214 // --------------------------------------------------------------------- |
|
1215 |
7363
|
1216 #include "graphics-props.cc" |
|
1217 |
|
1218 // --------------------------------------------------------------------- |
|
1219 |
6836
|
1220 void |
7363
|
1221 root_figure::properties::set_currentfigure (const octave_value& v) |
6874
|
1222 { |
7378
|
1223 graphics_handle val (v); |
7363
|
1224 |
6874
|
1225 if (error_state) |
|
1226 return; |
|
1227 |
7059
|
1228 if (xisnan (val.value ()) || is_handle (val)) |
6874
|
1229 { |
|
1230 currentfigure = val; |
|
1231 |
7363
|
1232 gh_manager::push_figure (val); |
6874
|
1233 } |
|
1234 else |
|
1235 gripe_set_invalid ("currentfigure"); |
|
1236 } |
|
1237 |
6406
|
1238 property_list |
|
1239 root_figure::factory_properties = root_figure::init_factory_properties (); |
|
1240 |
|
1241 // --------------------------------------------------------------------- |
|
1242 |
7363
|
1243 void |
|
1244 figure::properties::set_currentaxes (const octave_value& v) |
|
1245 { |
7378
|
1246 graphics_handle val (v); |
6705
|
1247 |
6874
|
1248 if (error_state) |
|
1249 return; |
|
1250 |
7070
|
1251 if (xisnan (val.value ()) || is_handle (val)) |
6874
|
1252 currentaxes = val; |
|
1253 else |
|
1254 gripe_set_invalid ("currentaxes"); |
|
1255 } |
|
1256 |
|
1257 void |
|
1258 figure::properties::set_visible (const octave_value& val) |
|
1259 { |
|
1260 std::string s = val.string_value (); |
|
1261 |
|
1262 if (! error_state) |
|
1263 { |
|
1264 if (s == "on") |
|
1265 xset (0, "currentfigure", __myhandle__.value ()); |
|
1266 |
|
1267 visible = val; |
|
1268 } |
|
1269 } |
|
1270 |
|
1271 void |
7408
|
1272 figure::properties::close (bool pop) |
6705
|
1273 { |
7408
|
1274 if (backend) |
|
1275 backend.close_figure (get___plot_stream__ ()); |
|
1276 |
|
1277 if (pop) |
6406
|
1278 { |
7408
|
1279 gh_manager::pop_figure (__myhandle__); |
|
1280 |
|
1281 graphics_handle cf = gh_manager::current_figure (); |
|
1282 |
|
1283 xset (0, "currentfigure", cf.value ()); |
6406
|
1284 } |
6705
|
1285 } |
6432
|
1286 |
6836
|
1287 octave_value |
7189
|
1288 figure::get_default (const caseless_str& name) const |
6836
|
1289 { |
|
1290 octave_value retval = default_properties.lookup (name); |
|
1291 |
|
1292 if (retval.is_undefined ()) |
|
1293 { |
|
1294 graphics_handle parent = get_parent (); |
|
1295 graphics_object parent_obj = gh_manager::get_object (parent); |
|
1296 |
|
1297 retval = parent_obj.get_default (name); |
|
1298 } |
|
1299 |
|
1300 return retval; |
|
1301 } |
|
1302 |
6406
|
1303 // --------------------------------------------------------------------- |
|
1304 |
7363
|
1305 void |
|
1306 axes::properties::set_title (const octave_value& v) |
6962
|
1307 { |
7363
|
1308 graphics_handle val = ::reparent (v, "set", "title", __myhandle__, false); |
6962
|
1309 |
6874
|
1310 if (! error_state) |
|
1311 { |
7363
|
1312 gh_manager::free (title.handle_value ()); |
6874
|
1313 title = val; |
|
1314 } |
|
1315 } |
|
1316 |
|
1317 void |
7363
|
1318 axes::properties::set_xlabel (const octave_value& v) |
6874
|
1319 { |
7363
|
1320 graphics_handle val = ::reparent (v, "set", "xlabel", __myhandle__, false); |
6874
|
1321 |
|
1322 if (! error_state) |
|
1323 { |
7363
|
1324 gh_manager::free (xlabel.handle_value ()); |
6874
|
1325 xlabel = val; |
|
1326 } |
|
1327 } |
|
1328 |
|
1329 void |
7363
|
1330 axes::properties::set_ylabel (const octave_value& v) |
6874
|
1331 { |
7363
|
1332 graphics_handle val = ::reparent (v, "set", "ylabel", __myhandle__, false); |
6874
|
1333 |
|
1334 if (! error_state) |
|
1335 { |
7363
|
1336 gh_manager::free (ylabel.handle_value ()); |
6874
|
1337 ylabel = val; |
|
1338 } |
|
1339 } |
|
1340 |
|
1341 void |
7363
|
1342 axes::properties::set_zlabel (const octave_value& v) |
6874
|
1343 { |
7363
|
1344 graphics_handle val = ::reparent (v, "set", "zlabel", __myhandle__, false); |
6874
|
1345 |
|
1346 if (! error_state) |
|
1347 { |
7363
|
1348 gh_manager::free (zlabel.handle_value ()); |
6874
|
1349 zlabel = val; |
|
1350 } |
|
1351 } |
|
1352 |
|
1353 void |
6844
|
1354 axes::properties::set_defaults (base_graphics_object& obj, |
6890
|
1355 const std::string& mode) |
6705
|
1356 { |
|
1357 position = Matrix (); |
7363
|
1358 title = graphics_handle (); |
6705
|
1359 box = "on"; |
|
1360 key = "off"; |
|
1361 keybox = "off"; |
7363
|
1362 keypos = 1.0; |
6962
|
1363 colororder = default_colororder (); |
6705
|
1364 dataaspectratio = Matrix (1, 3, 1.0); |
|
1365 dataaspectratiomode = "auto"; |
7363
|
1366 layer = "bottom"; |
6705
|
1367 |
|
1368 Matrix tlim (1, 2, 0.0); |
|
1369 tlim(1) = 1; |
|
1370 xlim = tlim; |
|
1371 ylim = tlim; |
|
1372 zlim = tlim; |
6807
|
1373 |
|
1374 Matrix cl (1, 2, 0); |
|
1375 cl(1) = 1; |
|
1376 clim = cl; |
|
1377 |
7363
|
1378 xlimmode = "auto"; |
|
1379 ylimmode = "auto"; |
|
1380 zlimmode = "auto"; |
|
1381 climmode = "auto"; |
|
1382 xlabel = graphics_handle (); |
|
1383 ylabel = graphics_handle (); |
|
1384 zlabel = graphics_handle (); |
6705
|
1385 xgrid = "off"; |
|
1386 ygrid = "off"; |
|
1387 zgrid = "off"; |
|
1388 xminorgrid = "off"; |
|
1389 yminorgrid = "off"; |
|
1390 zminorgrid = "off"; |
|
1391 xtick = Matrix (); |
|
1392 ytick = Matrix (); |
|
1393 ztick = Matrix (); |
|
1394 xtickmode = "auto"; |
|
1395 ytickmode = "auto"; |
|
1396 ztickmode = "auto"; |
|
1397 xticklabel = ""; |
|
1398 yticklabel = ""; |
|
1399 zticklabel = ""; |
|
1400 xticklabelmode = "auto"; |
|
1401 yticklabelmode = "auto"; |
|
1402 zticklabelmode = "auto"; |
7364
|
1403 color = color_values (0, 0, 0); |
|
1404 xcolor = color_values ("black"); |
|
1405 ycolor = color_values ("black"); |
|
1406 zcolor = color_values ("black"); |
7363
|
1407 xscale = "linear"; |
|
1408 yscale = "linear"; |
|
1409 zscale = "linear"; |
6705
|
1410 xdir = "normal"; |
|
1411 ydir = "normal"; |
|
1412 zdir = "normal"; |
7363
|
1413 yaxislocation = "left"; |
|
1414 xaxislocation = "bottom"; |
6705
|
1415 |
|
1416 Matrix tview (1, 2, 0.0); |
|
1417 tview(1) = 90; |
|
1418 view = tview; |
|
1419 |
6765
|
1420 visible = "on"; |
6705
|
1421 nextplot = "replace"; |
|
1422 |
|
1423 // FIXME -- this is not quite right; we should preserve |
|
1424 // "position" and "units". |
|
1425 |
|
1426 if (mode != "replace") |
|
1427 { |
6406
|
1428 Matrix touterposition (1, 4, 0.0); |
|
1429 touterposition(2) = 1; |
|
1430 touterposition(3) = 1; |
|
1431 outerposition = touterposition; |
|
1432 } |
|
1433 |
7363
|
1434 activepositionproperty = "outerposition"; |
|
1435 __colorbar__ = "none"; |
7189
|
1436 |
6705
|
1437 delete_children (); |
6406
|
1438 |
6705
|
1439 children = Matrix (); |
6432
|
1440 |
6705
|
1441 override_defaults (obj); |
|
1442 } |
|
1443 |
6874
|
1444 graphics_handle |
|
1445 axes::properties::get_title (void) const |
|
1446 { |
7363
|
1447 if (! title.handle_value ().ok ()) |
6874
|
1448 title = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1449 |
7363
|
1450 return title.handle_value (); |
6874
|
1451 } |
|
1452 |
|
1453 graphics_handle |
|
1454 axes::properties::get_xlabel (void) const |
|
1455 { |
7363
|
1456 if (! xlabel.handle_value ().ok ()) |
6874
|
1457 xlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1458 |
7363
|
1459 return xlabel.handle_value (); |
6874
|
1460 } |
|
1461 |
|
1462 graphics_handle |
|
1463 axes::properties::get_ylabel (void) const |
|
1464 { |
7363
|
1465 if (! ylabel.handle_value ().ok ()) |
6874
|
1466 ylabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1467 |
7363
|
1468 return ylabel.handle_value (); |
6874
|
1469 } |
|
1470 |
|
1471 graphics_handle |
|
1472 axes::properties::get_zlabel (void) const |
|
1473 { |
7363
|
1474 if (! zlabel.handle_value ().ok ()) |
6874
|
1475 zlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1476 |
7363
|
1477 return zlabel.handle_value (); |
6705
|
1478 } |
6406
|
1479 |
6705
|
1480 void |
6844
|
1481 axes::properties::remove_child (const graphics_handle& h) |
6705
|
1482 { |
7363
|
1483 if (title.handle_value ().ok () && h == title.handle_value ()) |
6705
|
1484 title = gh_manager::make_graphics_handle ("text", __myhandle__); |
7363
|
1485 else if (xlabel.handle_value ().ok () && h == xlabel.handle_value ()) |
6705
|
1486 xlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
7363
|
1487 else if (ylabel.handle_value ().ok () && h == ylabel.handle_value ()) |
6705
|
1488 ylabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
7363
|
1489 else if (zlabel.handle_value ().ok () && h == zlabel.handle_value ()) |
6705
|
1490 zlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1491 else |
|
1492 base_properties::remove_child (h); |
|
1493 } |
6406
|
1494 |
6705
|
1495 void |
6844
|
1496 axes::properties::delete_children (void) |
6705
|
1497 { |
|
1498 base_properties::delete_children (); |
|
1499 |
7363
|
1500 gh_manager::free (title.handle_value ()); |
|
1501 gh_manager::free (xlabel.handle_value ()); |
|
1502 gh_manager::free (ylabel.handle_value ()); |
|
1503 gh_manager::free (zlabel.handle_value ()); |
6705
|
1504 } |
6406
|
1505 |
6836
|
1506 octave_value |
7189
|
1507 axes::get_default (const caseless_str& name) const |
6836
|
1508 { |
|
1509 octave_value retval = default_properties.lookup (name); |
|
1510 |
|
1511 if (retval.is_undefined ()) |
|
1512 { |
|
1513 graphics_handle parent = get_parent (); |
|
1514 graphics_object parent_obj = gh_manager::get_object (parent); |
|
1515 |
|
1516 retval = parent_obj.get_default (name); |
|
1517 } |
|
1518 |
|
1519 return retval; |
|
1520 } |
|
1521 |
7222
|
1522 static void |
|
1523 check_limit_vals (double& min_val, double& max_val, double& min_pos, |
|
1524 const data_property& data) |
|
1525 { |
7267
|
1526 double val = data.min_val (); |
|
1527 if (! (xisinf (val) || xisnan (val)) && val < min_val) |
|
1528 min_val = val; |
|
1529 val = data.max_val (); |
|
1530 if (! (xisinf (val) || xisnan (val)) && val > max_val) |
|
1531 max_val = val; |
|
1532 val = data.min_pos (); |
|
1533 if (! (xisinf (val) || xisnan (val)) && val > 0 && val < min_pos) |
|
1534 min_pos = val; |
7222
|
1535 } |
|
1536 |
|
1537 // Attempt to make "nice" limits from the actual max and min of the |
|
1538 // data. For log plots, we will also use the smallest strictly positive |
|
1539 // value. |
|
1540 |
|
1541 static Matrix |
|
1542 get_axis_limits (double xmin, double xmax, double min_pos, bool logscale) |
|
1543 { |
|
1544 Matrix retval; |
|
1545 |
|
1546 double min_val = xmin; |
|
1547 double max_val = xmax; |
|
1548 |
|
1549 if (! (xisinf (min_val) || xisinf (max_val))) |
|
1550 { |
|
1551 if (logscale) |
|
1552 { |
|
1553 if (xisinf (min_pos)) |
|
1554 { |
|
1555 // warning ("axis: logscale with no positive values to plot"); |
|
1556 return retval; |
|
1557 } |
|
1558 |
|
1559 if (min_val <= 0) |
|
1560 { |
|
1561 warning ("axis: omitting nonpositive data in log plot"); |
|
1562 min_val = min_pos; |
|
1563 } |
|
1564 // FIXME -- maybe this test should also be relative? |
|
1565 if (std::abs (min_val - max_val) < sqrt (DBL_EPSILON)) |
|
1566 { |
|
1567 min_val *= 0.9; |
|
1568 max_val *= 1.1; |
|
1569 } |
|
1570 min_val = pow (10, floor (log10 (min_val))); |
|
1571 max_val = pow (10, ceil (log10 (max_val))); |
|
1572 } |
|
1573 else |
|
1574 { |
|
1575 if (min_val == 0 && max_val == 0) |
|
1576 { |
|
1577 min_val = -1; |
|
1578 max_val = 1; |
|
1579 } |
|
1580 // FIXME -- maybe this test should also be relative? |
|
1581 else if (std::abs (min_val - max_val) < sqrt (DBL_EPSILON)) |
|
1582 { |
|
1583 min_val -= 0.1 * std::abs (min_val); |
|
1584 max_val += 0.1 * std::abs (max_val); |
|
1585 } |
|
1586 // FIXME -- to do a better job, we should consider the tic spacing. |
|
1587 double scale = pow (10, floor (log10 (max_val - min_val) - 1)); |
|
1588 min_val = scale * floor (min_val / scale); |
|
1589 max_val = scale * ceil (max_val / scale); |
|
1590 } |
|
1591 } |
|
1592 |
|
1593 retval.resize (1, 2); |
|
1594 |
|
1595 retval(0) = min_val; |
|
1596 retval(1) = max_val; |
|
1597 |
|
1598 return retval; |
|
1599 } |
|
1600 |
|
1601 static bool updating_axis_limits = false; |
|
1602 |
7214
|
1603 void |
7222
|
1604 axes::update_axis_limits (const std::string& axis_type) |
7214
|
1605 { |
7222
|
1606 if (updating_axis_limits) |
|
1607 return; |
|
1608 |
|
1609 Matrix kids = xproperties.get_children (); |
|
1610 |
|
1611 octave_idx_type n = kids.numel (); |
|
1612 |
|
1613 double min_val = octave_Inf; |
|
1614 double max_val = -octave_Inf; |
|
1615 double min_pos = octave_Inf; |
|
1616 |
|
1617 char update_type = 0; |
|
1618 |
|
1619 Matrix limits; |
|
1620 |
|
1621 if (axis_type == "xdata" || axis_type == "xscale" |
|
1622 || axis_type == "xldata" || axis_type == "xudata" |
|
1623 || axis_type == "xlimmode") |
|
1624 { |
7363
|
1625 if (xproperties.xlimmode_is ("auto")) |
7222
|
1626 { |
|
1627 for (octave_idx_type i = 0; i < n; i++) |
|
1628 { |
|
1629 graphics_object obj = gh_manager::get_object (kids(i)); |
|
1630 |
|
1631 if (obj.isa ("line") || obj.isa ("image") |
|
1632 || obj.isa ("patch") || obj.isa ("surface")) |
|
1633 { |
7363
|
1634 data_property xdata = obj.get_xdata_property (); |
7222
|
1635 |
|
1636 check_limit_vals (min_val, max_val, min_pos, xdata); |
|
1637 |
|
1638 if (obj.isa ("line")) |
|
1639 { |
7363
|
1640 data_property xldata = obj.get_xldata_property (); |
|
1641 data_property xudata = obj.get_xudata_property (); |
7222
|
1642 |
|
1643 check_limit_vals (min_val, max_val, min_pos, xldata); |
|
1644 check_limit_vals (min_val, max_val, min_pos, xudata); |
|
1645 } |
|
1646 } |
|
1647 } |
|
1648 |
|
1649 limits = get_axis_limits (min_val, max_val, min_pos, |
7363
|
1650 xproperties.xscale_is ("log")); |
7222
|
1651 |
|
1652 update_type = 'x'; |
|
1653 } |
|
1654 } |
|
1655 else if (axis_type == "ydata" || axis_type == "yscale" |
|
1656 || axis_type == "ldata" || axis_type == "udata" |
|
1657 || axis_type == "ylimmode") |
|
1658 { |
7363
|
1659 if (xproperties.ylimmode_is ("auto")) |
7222
|
1660 { |
|
1661 for (octave_idx_type i = 0; i < n; i++) |
|
1662 { |
|
1663 graphics_object obj = gh_manager::get_object (kids(i)); |
|
1664 |
|
1665 if (obj.isa ("line") || obj.isa ("image") |
|
1666 || obj.isa ("patch") || obj.isa ("surface")) |
|
1667 { |
7363
|
1668 data_property ydata = obj.get_ydata_property (); |
7222
|
1669 |
|
1670 check_limit_vals (min_val, max_val, min_pos, ydata); |
|
1671 |
|
1672 if (obj.isa ("line")) |
|
1673 { |
7363
|
1674 data_property ldata = obj.get_ldata_property (); |
|
1675 data_property udata = obj.get_udata_property (); |
7222
|
1676 |
|
1677 check_limit_vals (min_val, max_val, min_pos, ldata); |
|
1678 check_limit_vals (min_val, max_val, min_pos, udata); |
|
1679 } |
|
1680 } |
|
1681 } |
|
1682 |
|
1683 limits = get_axis_limits (min_val, max_val, min_pos, |
7363
|
1684 xproperties.yscale_is ("log")); |
7222
|
1685 |
|
1686 update_type = 'y'; |
|
1687 } |
|
1688 } |
|
1689 else if (axis_type == "zdata" || axis_type == "zscale" |
|
1690 || axis_type == "zlimmode") |
|
1691 { |
7363
|
1692 if (xproperties.zlimmode_is ("auto")) |
7222
|
1693 { |
|
1694 for (octave_idx_type i = 0; i < n; i++) |
|
1695 { |
|
1696 graphics_object obj = gh_manager::get_object (kids(i)); |
|
1697 |
|
1698 if (obj.isa ("line") || obj.isa ("patch") || obj.isa ("surface")) |
|
1699 { |
7363
|
1700 data_property zdata = obj.get_zdata_property (); |
7222
|
1701 |
|
1702 check_limit_vals (min_val, max_val, min_pos, zdata); |
|
1703 } |
|
1704 } |
|
1705 |
|
1706 limits = get_axis_limits (min_val, max_val, min_pos, |
7363
|
1707 xproperties.zscale_is ("log")); |
7222
|
1708 |
|
1709 update_type = 'z'; |
|
1710 } |
|
1711 } |
|
1712 else if (axis_type == "cdata" || axis_type == "climmode") |
|
1713 { |
7363
|
1714 if (xproperties.climmode_is ("auto")) |
7222
|
1715 { |
|
1716 for (octave_idx_type i = 0; i < n; i++) |
|
1717 { |
|
1718 graphics_object obj = gh_manager::get_object (kids(i)); |
|
1719 |
|
1720 if (obj.isa ("image") || obj.isa ("patch") || obj.isa ("surface")) |
|
1721 { |
7363
|
1722 data_property cdata = obj.get_cdata_property (); |
7222
|
1723 |
|
1724 check_limit_vals (min_val, max_val, min_pos, cdata); |
|
1725 } |
|
1726 } |
|
1727 |
|
1728 if (min_val == max_val) |
|
1729 max_val = min_val + 1; |
|
1730 |
|
1731 limits.resize (1, 2); |
|
1732 |
|
1733 limits(0) = min_val; |
|
1734 limits(1) = max_val; |
|
1735 |
|
1736 update_type = 'c'; |
|
1737 } |
|
1738 |
|
1739 } |
|
1740 |
|
1741 unwind_protect_bool (updating_axis_limits); |
|
1742 updating_axis_limits = true; |
|
1743 |
|
1744 switch (update_type) |
|
1745 { |
|
1746 case 'x': |
|
1747 xproperties.set_xlim (limits); |
|
1748 xproperties.set_xlimmode ("auto"); |
|
1749 break; |
|
1750 |
|
1751 case 'y': |
|
1752 xproperties.set_ylim (limits); |
|
1753 xproperties.set_ylimmode ("auto"); |
|
1754 break; |
|
1755 |
|
1756 case 'z': |
|
1757 xproperties.set_zlim (limits); |
|
1758 xproperties.set_zlimmode ("auto"); |
|
1759 break; |
|
1760 |
|
1761 case 'c': |
|
1762 xproperties.set_clim (limits); |
|
1763 xproperties.set_climmode ("auto"); |
|
1764 break; |
|
1765 |
|
1766 default: |
|
1767 break; |
|
1768 } |
|
1769 |
|
1770 unwind_protect::run (); |
7214
|
1771 } |
|
1772 |
7363
|
1773 // --------------------------------------------------------------------- |
|
1774 |
|
1775 // Note: "line" code is entirely auto-generated |
6406
|
1776 |
|
1777 // --------------------------------------------------------------------- |
|
1778 |
7363
|
1779 // Note: "text" code is entirely auto-generated |
6406
|
1780 |
|
1781 // --------------------------------------------------------------------- |
|
1782 |
7363
|
1783 // Note: "image" code is entirely auto-generated |
6406
|
1784 |
|
1785 // --------------------------------------------------------------------- |
|
1786 |
7363
|
1787 // Note: "patch" code is entirely auto-generated |
6807
|
1788 |
|
1789 // --------------------------------------------------------------------- |
|
1790 |
7363
|
1791 // Note: "surface" code is entirely auto-generated |
6406
|
1792 |
|
1793 // --------------------------------------------------------------------- |
|
1794 |
|
1795 octave_value |
7189
|
1796 base_graphics_object::get_default (const caseless_str& name) const |
6406
|
1797 { |
|
1798 graphics_handle parent = get_parent (); |
|
1799 graphics_object parent_obj = gh_manager::get_object (parent); |
|
1800 |
|
1801 return parent_obj.get_default (type () + name); |
|
1802 } |
|
1803 |
|
1804 octave_value |
7189
|
1805 base_graphics_object::get_factory_default (const caseless_str& name) const |
6406
|
1806 { |
|
1807 graphics_object parent_obj = gh_manager::get_object (0); |
|
1808 |
|
1809 return parent_obj.get_factory_default (type () + name); |
|
1810 } |
|
1811 |
7286
|
1812 // We use a random value for the handle to avoid issues with plots and |
|
1813 // scalar values for the first argument. |
6406
|
1814 gh_manager::gh_manager (void) |
7286
|
1815 : handle_map (), handle_free_list (), |
|
1816 next_handle (-1.0 - (rand () + 1.0) / (RAND_MAX + 2.0)) |
6406
|
1817 { |
|
1818 handle_map[0] = graphics_object (new root_figure ()); |
|
1819 } |
|
1820 |
|
1821 graphics_handle |
|
1822 gh_manager::do_make_graphics_handle (const std::string& go_name, |
7370
|
1823 const graphics_handle& p, bool do_createfcn) |
6406
|
1824 { |
|
1825 graphics_handle h = get_handle (go_name); |
|
1826 |
|
1827 base_graphics_object *go = 0; |
|
1828 |
|
1829 if (go_name == "figure") |
|
1830 go = new figure (h, p); |
|
1831 else if (go_name == "axes") |
|
1832 go = new axes (h, p); |
|
1833 else if (go_name == "line") |
|
1834 go = new line (h, p); |
|
1835 else if (go_name == "text") |
|
1836 go = new text (h, p); |
|
1837 else if (go_name == "image") |
|
1838 go = new image (h, p); |
6807
|
1839 else if (go_name == "patch") |
|
1840 go = new patch (h, p); |
6406
|
1841 else if (go_name == "surface") |
|
1842 go = new surface (h, p); |
|
1843 if (go) |
7370
|
1844 { |
|
1845 handle_map[h] = graphics_object (go); |
|
1846 if (do_createfcn) |
|
1847 go->get_properties ().execute_createfcn (); |
|
1848 } |
6406
|
1849 else |
|
1850 error ("gh_manager::do_make_graphics_handle: invalid object type `%s'", |
|
1851 go_name.c_str ()); |
|
1852 |
|
1853 return h; |
|
1854 } |
|
1855 |
|
1856 graphics_handle |
|
1857 gh_manager::do_make_figure_handle (double val) |
|
1858 { |
|
1859 graphics_handle h = val; |
|
1860 |
|
1861 handle_map[h] = graphics_object (new figure (h, 0)); |
|
1862 |
|
1863 return h; |
|
1864 } |
|
1865 |
|
1866 void |
|
1867 gh_manager::do_push_figure (const graphics_handle& h) |
|
1868 { |
|
1869 do_pop_figure (h); |
|
1870 |
|
1871 figure_list.push_front (h); |
|
1872 } |
|
1873 |
|
1874 void |
|
1875 gh_manager::do_pop_figure (const graphics_handle& h) |
|
1876 { |
|
1877 for (figure_list_iterator p = figure_list.begin (); |
|
1878 p != figure_list.end (); |
|
1879 p++) |
|
1880 { |
|
1881 if (*p == h) |
|
1882 { |
|
1883 figure_list.erase (p); |
|
1884 break; |
|
1885 } |
|
1886 } |
|
1887 } |
|
1888 |
|
1889 property_list::plist_map_type |
|
1890 root_figure::init_factory_properties (void) |
|
1891 { |
|
1892 property_list::plist_map_type plist_map; |
|
1893 |
6844
|
1894 plist_map["figure"] = figure::properties::factory_defaults (); |
|
1895 plist_map["axes"] = axes::properties::factory_defaults (); |
|
1896 plist_map["line"] = line::properties::factory_defaults (); |
|
1897 plist_map["text"] = text::properties::factory_defaults (); |
|
1898 plist_map["image"] = image::properties::factory_defaults (); |
|
1899 plist_map["patch"] = patch::properties::factory_defaults (); |
|
1900 plist_map["surface"] = surface::properties::factory_defaults (); |
6406
|
1901 |
|
1902 return plist_map; |
|
1903 } |
|
1904 |
|
1905 // --------------------------------------------------------------------- |
|
1906 |
|
1907 DEFUN (ishandle, args, , |
|
1908 "-*- texinfo -*-\n\ |
6678
|
1909 @deftypefn {Built-in Function} {} ishandle (@var{h})\n\ |
6406
|
1910 Return true if @var{h} is a graphics handle and false otherwise.\n\ |
|
1911 @end deftypefn") |
|
1912 { |
|
1913 octave_value retval; |
|
1914 |
|
1915 if (args.length () == 1) |
|
1916 retval = is_handle (args(0)); |
|
1917 else |
|
1918 print_usage (); |
|
1919 |
|
1920 return retval; |
|
1921 } |
|
1922 |
|
1923 DEFUN (set, args, , |
|
1924 "-*- texinfo -*-\n\ |
6678
|
1925 @deftypefn {Built-in Function} {} set (@var{h}, @var{p}, @var{v}, @dots{})\n\ |
6732
|
1926 Set the named property value or vector @var{p} to the value @var{v}\n\ |
6894
|
1927 for the graphics handle @var{h}.\n\ |
6406
|
1928 @end deftypefn") |
|
1929 { |
|
1930 octave_value retval; |
|
1931 |
|
1932 int nargin = args.length (); |
|
1933 |
|
1934 if (nargin > 0) |
|
1935 { |
6732
|
1936 ColumnVector hcv (args(0).vector_value ()); |
6406
|
1937 |
|
1938 if (! error_state) |
6732
|
1939 { |
6733
|
1940 bool request_drawnow = false; |
|
1941 |
6732
|
1942 for (octave_idx_type n = 0; n < hcv.length (); n++) |
|
1943 { |
|
1944 graphics_object obj = gh_manager::get_object (hcv(n)); |
6406
|
1945 |
6732
|
1946 if (obj) |
|
1947 { |
|
1948 obj.set (args.splice (0, 1)); |
6406
|
1949 |
6733
|
1950 request_drawnow = true; |
6732
|
1951 } |
|
1952 else |
6733
|
1953 { |
|
1954 error ("set: invalid handle (= %g)", hcv(n)); |
|
1955 break; |
|
1956 } |
6732
|
1957 } |
6733
|
1958 |
|
1959 if (! error_state && request_drawnow) |
7409
|
1960 Vdrawnow_requested = true; |
6732
|
1961 } |
6406
|
1962 else |
6732
|
1963 error ("set: expecting graphics handle as first argument"); |
6406
|
1964 } |
|
1965 else |
|
1966 print_usage (); |
|
1967 |
|
1968 return retval; |
|
1969 } |
|
1970 |
|
1971 DEFUN (get, args, , |
|
1972 "-*- texinfo -*-\n\ |
6678
|
1973 @deftypefn {Built-in Function} {} get (@var{h}, @var{p})\n\ |
6406
|
1974 Return the named property @var{p} from the graphics handle @var{h}.\n\ |
|
1975 If @var{p} is omitted, return the complete property list for @var{h}.\n\ |
6732
|
1976 If @var{h} is a vector, return a cell array including the property\n\ |
|
1977 values or lists respectively.\n\ |
6406
|
1978 @end deftypefn") |
|
1979 { |
|
1980 octave_value retval; |
6732
|
1981 octave_value_list vlist; |
6406
|
1982 |
|
1983 int nargin = args.length (); |
|
1984 |
|
1985 if (nargin == 1 || nargin == 2) |
|
1986 { |
6732
|
1987 ColumnVector hcv (args(0).vector_value ()); |
6406
|
1988 |
|
1989 if (! error_state) |
6732
|
1990 { |
6733
|
1991 octave_idx_type len = hcv.length (); |
|
1992 |
|
1993 vlist.resize (len); |
|
1994 |
|
1995 for (octave_idx_type n = 0; n < len; n++) |
6732
|
1996 { |
|
1997 graphics_object obj = gh_manager::get_object (hcv(n)); |
6406
|
1998 |
6732
|
1999 if (obj) |
|
2000 { |
|
2001 if (nargin == 1) |
|
2002 vlist(n) = obj.get (); |
|
2003 else |
|
2004 { |
7189
|
2005 caseless_str property = args(1).string_value (); |
6406
|
2006 |
6732
|
2007 if (! error_state) |
|
2008 vlist(n) = obj.get (property); |
|
2009 else |
6733
|
2010 { |
|
2011 error ("get: expecting property name as second argument"); |
|
2012 break; |
|
2013 } |
6732
|
2014 } |
|
2015 } |
|
2016 else |
6733
|
2017 { |
|
2018 error ("get: invalid handle (= %g)", hcv(n)); |
|
2019 break; |
|
2020 } |
6732
|
2021 } |
|
2022 } |
6406
|
2023 else |
6732
|
2024 error ("get: expecting graphics handle as first argument"); |
6406
|
2025 } |
|
2026 else |
|
2027 print_usage (); |
|
2028 |
6733
|
2029 if (! error_state) |
6732
|
2030 { |
6733
|
2031 octave_idx_type len = vlist.length (); |
|
2032 |
|
2033 if (len > 1) |
|
2034 retval = Cell (vlist); |
|
2035 else if (len == 1) |
|
2036 retval = vlist(0); |
6732
|
2037 } |
|
2038 |
6406
|
2039 return retval; |
|
2040 } |
|
2041 |
7379
|
2042 DEFUN (__get__, args, , |
|
2043 "-*- texinfo -*-\n\ |
|
2044 @deftypefn {Built-in Function} {} __get__ (@var{h})\n\ |
|
2045 Return all properties from the graphics handle @var{h}.\n\ |
|
2046 If @var{h} is a vector, return a cell array including the property\n\ |
|
2047 values or lists respectively.\n\ |
|
2048 @end deftypefn") |
|
2049 { |
|
2050 octave_value retval; |
|
2051 octave_value_list vlist; |
|
2052 |
|
2053 int nargin = args.length (); |
|
2054 |
|
2055 if (nargin == 1) |
|
2056 { |
|
2057 ColumnVector hcv (args(0).vector_value ()); |
|
2058 |
|
2059 if (! error_state) |
|
2060 { |
|
2061 octave_idx_type len = hcv.length (); |
|
2062 |
|
2063 vlist.resize (len); |
|
2064 |
|
2065 for (octave_idx_type n = 0; n < len; n++) |
|
2066 { |
|
2067 graphics_object obj = gh_manager::get_object (hcv(n)); |
|
2068 |
|
2069 if (obj) |
|
2070 vlist(n) = obj.get (true); |
|
2071 else |
|
2072 { |
|
2073 error ("get: invalid handle (= %g)", hcv(n)); |
|
2074 break; |
|
2075 } |
|
2076 } |
|
2077 } |
|
2078 else |
|
2079 error ("get: expecting graphics handle as first argument"); |
|
2080 } |
|
2081 else |
|
2082 print_usage (); |
|
2083 |
|
2084 if (! error_state) |
|
2085 { |
|
2086 octave_idx_type len = vlist.length (); |
|
2087 |
|
2088 if (len > 1) |
|
2089 retval = Cell (vlist); |
|
2090 else if (len == 1) |
|
2091 retval = vlist(0); |
|
2092 } |
|
2093 |
|
2094 return retval; |
|
2095 } |
|
2096 |
6406
|
2097 static octave_value |
|
2098 make_graphics_object (const std::string& go_name, |
6874
|
2099 const octave_value_list& args) |
6406
|
2100 { |
|
2101 octave_value retval; |
|
2102 |
|
2103 double val = args(0).double_value (); |
|
2104 |
|
2105 if (! error_state) |
|
2106 { |
|
2107 graphics_handle parent = gh_manager::lookup (val); |
|
2108 |
7056
|
2109 if (parent.ok ()) |
6406
|
2110 { |
|
2111 graphics_handle h |
7370
|
2112 = gh_manager::make_graphics_handle (go_name, parent, false); |
6406
|
2113 |
|
2114 if (! error_state) |
|
2115 { |
|
2116 adopt (parent, h); |
|
2117 |
|
2118 xset (h, args.splice (0, 1)); |
7370
|
2119 xcreatefcn (h); |
6406
|
2120 |
6874
|
2121 retval = h.value (); |
7296
|
2122 |
|
2123 if (! error_state) |
7409
|
2124 Vdrawnow_requested = true; |
6406
|
2125 } |
|
2126 else |
|
2127 error ("__go%s__: unable to create graphics handle", |
|
2128 go_name.c_str ()); |
|
2129 } |
|
2130 else |
|
2131 error ("__go_%s__: invalid parent", go_name.c_str ()); |
|
2132 } |
|
2133 else |
|
2134 error ("__go_%s__: invalid parent", go_name.c_str ()); |
|
2135 |
|
2136 return retval; |
|
2137 } |
|
2138 |
|
2139 DEFUN (__go_figure__, args, , |
|
2140 "-*- texinfo -*-\n\ |
|
2141 @deftypefn {Built-in Function} {} __go_figure__ (@var{fignum})\n\ |
6945
|
2142 Undocumented internal function.\n\ |
6406
|
2143 @end deftypefn") |
|
2144 { |
|
2145 octave_value retval; |
|
2146 |
|
2147 if (args.length () > 0) |
|
2148 { |
|
2149 double val = args(0).double_value (); |
|
2150 |
|
2151 if (! error_state) |
|
2152 { |
|
2153 if (is_figure (val)) |
|
2154 { |
|
2155 graphics_handle h = gh_manager::lookup (val); |
|
2156 |
|
2157 xset (h, args.splice (0, 1)); |
|
2158 |
6874
|
2159 retval = h.value (); |
6406
|
2160 } |
|
2161 else |
|
2162 { |
|
2163 graphics_handle h = octave_NaN; |
|
2164 |
|
2165 if (xisnan (val)) |
7370
|
2166 h = gh_manager::make_graphics_handle ("figure", 0, false); |
6406
|
2167 else if (val > 0 && D_NINT (val) == val) |
|
2168 h = gh_manager::make_figure_handle (val); |
|
2169 else |
|
2170 error ("__go_figure__: invalid figure number"); |
|
2171 |
7056
|
2172 if (! error_state && h.ok ()) |
6406
|
2173 { |
|
2174 adopt (0, h); |
|
2175 |
|
2176 xset (h, args.splice (0, 1)); |
7370
|
2177 xcreatefcn (h); |
6406
|
2178 |
6874
|
2179 retval = h.value (); |
6406
|
2180 } |
|
2181 else |
|
2182 error ("__go_figure__: failed to create figure handle"); |
|
2183 } |
|
2184 } |
|
2185 else |
|
2186 error ("__go_figure__: expecting figure number to be double value"); |
|
2187 } |
|
2188 else |
|
2189 print_usage (); |
|
2190 |
|
2191 return retval; |
|
2192 } |
|
2193 |
|
2194 #define GO_BODY(TYPE) \ |
|
2195 octave_value retval; \ |
|
2196 \ |
|
2197 if (args.length () > 0) \ |
|
2198 retval = make_graphics_object (#TYPE, args); \ |
|
2199 else \ |
|
2200 print_usage (); \ |
|
2201 \ |
|
2202 return retval |
|
2203 |
|
2204 DEFUN (__go_axes__, args, , |
|
2205 "-*- texinfo -*-\n\ |
|
2206 @deftypefn {Built-in Function} {} __go_axes__ (@var{parent})\n\ |
6945
|
2207 Undocumented internal function.\n\ |
6406
|
2208 @end deftypefn") |
|
2209 { |
|
2210 GO_BODY (axes); |
|
2211 } |
|
2212 |
|
2213 DEFUN (__go_line__, args, , |
|
2214 "-*- texinfo -*-\n\ |
|
2215 @deftypefn {Built-in Function} {} __go_line__ (@var{parent})\n\ |
6945
|
2216 Undocumented internal function.\n\ |
6406
|
2217 @end deftypefn") |
|
2218 { |
|
2219 GO_BODY (line); |
|
2220 } |
|
2221 |
|
2222 DEFUN (__go_text__, args, , |
|
2223 "-*- texinfo -*-\n\ |
|
2224 @deftypefn {Built-in Function} {} __go_text__ (@var{parent})\n\ |
6945
|
2225 Undocumented internal function.\n\ |
6406
|
2226 @end deftypefn") |
|
2227 { |
|
2228 GO_BODY (text); |
|
2229 } |
|
2230 |
|
2231 DEFUN (__go_image__, args, , |
|
2232 "-*- texinfo -*-\n\ |
|
2233 @deftypefn {Built-in Function} {} __go_image__ (@var{parent})\n\ |
6945
|
2234 Undocumented internal function.\n\ |
6406
|
2235 @end deftypefn") |
|
2236 { |
|
2237 GO_BODY (image); |
|
2238 } |
|
2239 |
|
2240 DEFUN (__go_surface__, args, , |
|
2241 "-*- texinfo -*-\n\ |
|
2242 @deftypefn {Built-in Function} {} __go_surface__ (@var{parent})\n\ |
6945
|
2243 Undocumented internal function.\n\ |
6406
|
2244 @end deftypefn") |
|
2245 { |
|
2246 GO_BODY (surface); |
|
2247 } |
|
2248 |
6807
|
2249 DEFUN (__go_patch__, args, , |
|
2250 "-*- texinfo -*-\n\ |
|
2251 @deftypefn {Built-in Function} {} __go_patch__ (@var{parent})\n\ |
6945
|
2252 Undocumented internal function.\n\ |
6807
|
2253 @end deftypefn") |
|
2254 { |
|
2255 GO_BODY (patch); |
|
2256 } |
|
2257 |
6406
|
2258 DEFUN (__go_delete__, args, , |
|
2259 "-*- texinfo -*-\n\ |
|
2260 @deftypefn {Built-in Function} {} __go_delete__ (@var{h})\n\ |
6945
|
2261 Undocumented internal function.\n\ |
6406
|
2262 @end deftypefn") |
|
2263 { |
|
2264 octave_value_list retval; |
|
2265 |
|
2266 if (args.length () == 1) |
|
2267 { |
|
2268 graphics_handle h = octave_NaN; |
|
2269 |
|
2270 double val = args(0).double_value (); |
|
2271 |
|
2272 if (! error_state) |
|
2273 { |
|
2274 h = gh_manager::lookup (val); |
|
2275 |
7056
|
2276 if (h.ok ()) |
6406
|
2277 { |
|
2278 graphics_object obj = gh_manager::get_object (h); |
|
2279 |
|
2280 graphics_handle parent_h = obj.get_parent (); |
|
2281 |
|
2282 graphics_object parent_obj = gh_manager::get_object (parent_h); |
|
2283 |
7367
|
2284 // NOTE: free the handle before removing it from its parent's |
|
2285 // children, such that the object's state is correct when |
|
2286 // the deletefcn callback is executed |
6406
|
2287 |
|
2288 gh_manager::free (h); |
7367
|
2289 |
|
2290 parent_obj.remove_child (h); |
6406
|
2291 } |
|
2292 else |
|
2293 error ("delete: invalid graphics object (= %g)", val); |
|
2294 } |
|
2295 else |
|
2296 error ("delete: invalid graphics object"); |
|
2297 } |
|
2298 else |
|
2299 print_usage (); |
|
2300 |
|
2301 return retval; |
|
2302 } |
|
2303 |
|
2304 DEFUN (__go_axes_init__, args, , |
|
2305 "-*- texinfo -*-\n\ |
|
2306 @deftypefn {Built-in Function} {} __go_axes_init__ (@var{h}, @var{mode})\n\ |
6945
|
2307 Undocumented internal function.\n\ |
6406
|
2308 @end deftypefn") |
|
2309 { |
|
2310 octave_value retval; |
|
2311 |
|
2312 int nargin = args.length (); |
|
2313 |
|
2314 std::string mode = ""; |
|
2315 |
|
2316 if (nargin == 2) |
|
2317 { |
|
2318 mode = args(1).string_value (); |
|
2319 |
|
2320 if (error_state) |
|
2321 return retval; |
|
2322 } |
|
2323 |
|
2324 if (nargin == 1 || nargin == 2) |
|
2325 { |
|
2326 graphics_handle h = octave_NaN; |
|
2327 |
|
2328 double val = args(0).double_value (); |
|
2329 |
|
2330 if (! error_state) |
|
2331 { |
|
2332 h = gh_manager::lookup (val); |
|
2333 |
7056
|
2334 if (h.ok ()) |
6406
|
2335 { |
|
2336 graphics_object obj = gh_manager::get_object (h); |
|
2337 |
|
2338 obj.set_defaults (mode); |
|
2339 } |
|
2340 else |
|
2341 error ("__go_axes_init__: invalid graphics object (= %g)", val); |
|
2342 } |
|
2343 else |
|
2344 error ("__go_axes_init__: invalid graphics object"); |
|
2345 } |
|
2346 else |
|
2347 print_usage (); |
|
2348 |
|
2349 return retval; |
|
2350 } |
|
2351 |
|
2352 DEFUN (__go_handles__, , , |
|
2353 "-*- texinfo -*-\n\ |
|
2354 @deftypefn {Built-in Function} {} __go_handles__ ()\n\ |
6945
|
2355 Undocumented internal function.\n\ |
6406
|
2356 @end deftypefn") |
|
2357 { |
6425
|
2358 return octave_value (gh_manager::handle_list ()); |
|
2359 } |
|
2360 |
|
2361 DEFUN (__go_figure_handles__, , , |
|
2362 "-*- texinfo -*-\n\ |
|
2363 @deftypefn {Built-in Function} {} __go_figure_handles__ ()\n\ |
6945
|
2364 Undocumented internal function.\n\ |
6425
|
2365 @end deftypefn") |
|
2366 { |
|
2367 return octave_value (gh_manager::figure_handle_list ()); |
6406
|
2368 } |
|
2369 |
7409
|
2370 static void |
|
2371 clear_drawnow_request (void *) |
|
2372 { |
|
2373 Vdrawnow_requested = false; |
|
2374 } |
|
2375 |
7408
|
2376 DEFUN (drawnow, args, , |
|
2377 "-*- texinfo -*-\n\ |
|
2378 @deftypefn {Built-in Function} {} __go_drawnow__ ()\n\ |
|
2379 @deftypefnx {Built-in Function} {} __go_drawnow__ (@var{term}, @var{file}, @var{mono}, @var{debug_file})\n\ |
|
2380 Undocumented internal function.\n\ |
|
2381 @end deftypefn") |
|
2382 { |
|
2383 static int drawnow_executing = 0; |
|
2384 static bool __go_close_all_registered__ = false; |
|
2385 |
|
2386 octave_value retval; |
|
2387 |
7409
|
2388 unwind_protect::begin_frame ("Fdrawnow"); |
|
2389 unwind_protect::add (clear_drawnow_request); |
|
2390 |
|
2391 unwind_protect_int (drawnow_executing); |
|
2392 |
|
2393 if (++drawnow_executing <= 1) |
7408
|
2394 { |
7409
|
2395 if (! __go_close_all_registered__) |
|
2396 { |
|
2397 octave_add_atexit_function ("__go_close_all__"); |
|
2398 |
|
2399 __go_close_all_registered__ = true; |
|
2400 } |
|
2401 |
|
2402 if (args.length () == 0) |
7408
|
2403 { |
7409
|
2404 Matrix hlist = gh_manager::figure_handle_list (); |
|
2405 |
|
2406 for (int i = 0; ! error_state && i < hlist.length (); i++) |
7408
|
2407 { |
7409
|
2408 graphics_handle h = gh_manager::lookup (hlist(i)); |
|
2409 |
|
2410 if (h.ok () && h != 0) |
7408
|
2411 { |
7409
|
2412 graphics_object go = gh_manager::get_object (h); |
|
2413 figure::properties& fprops = dynamic_cast <figure::properties&> (go.get_properties ()); |
|
2414 |
|
2415 if (fprops.is_modified ()) |
7408
|
2416 { |
7409
|
2417 if (fprops.is_visible ()) |
|
2418 fprops.get_backend ().redraw_figure (h); |
|
2419 else if (! fprops.get___plot_stream__ ().is_empty ()) |
|
2420 { |
|
2421 fprops.close (false); |
|
2422 fprops.set___plot_stream__ (Matrix ()); |
|
2423 fprops.set___enhanced__ (false); |
|
2424 } |
|
2425 fprops.set_modified (false); |
7408
|
2426 } |
|
2427 } |
|
2428 } |
|
2429 } |
7409
|
2430 else if (args.length () >= 2 && args.length () <= 4) |
7408
|
2431 { |
7409
|
2432 std::string term, file, debug_file; |
|
2433 bool mono; |
|
2434 |
|
2435 term = args(0).string_value (); |
7408
|
2436 |
|
2437 if (! error_state) |
|
2438 { |
7409
|
2439 file = args(1).string_value (); |
7408
|
2440 |
|
2441 if (! error_state) |
|
2442 { |
7409
|
2443 size_t pos = file.find_last_of (file_ops::dir_sep_chars); |
|
2444 |
|
2445 if (pos != NPOS) |
|
2446 { |
|
2447 file_stat fs (file.substr (0, pos)); |
|
2448 |
|
2449 if (! (fs && fs.is_dir ())) |
|
2450 error ("drawnow: nonexistent directory `%s'", |
|
2451 file.substr (0, pos).c_str ()); |
|
2452 } |
|
2453 |
|
2454 mono = (args.length () >= 3 ? args(2).bool_value () : false); |
7408
|
2455 |
|
2456 if (! error_state) |
|
2457 { |
7409
|
2458 debug_file = (args.length () > 3 ? args(3).string_value () |
|
2459 : ""); |
|
2460 |
|
2461 if (! error_state) |
7408
|
2462 { |
7409
|
2463 graphics_handle h = gcf (); |
|
2464 |
|
2465 if (h.ok ()) |
|
2466 { |
|
2467 graphics_object go = gh_manager::get_object (h); |
|
2468 |
|
2469 figure::properties& fprops = dynamic_cast<figure::properties&> (go.get_properties ()); |
|
2470 |
|
2471 fprops.get_backend () |
|
2472 .print_figure (h, term, file, mono, debug_file); |
|
2473 } |
|
2474 else |
|
2475 error ("drawnow: nothing to draw"); |
7408
|
2476 } |
|
2477 else |
7409
|
2478 error ("drawnow: invalid debug_file, expected a string value"); |
7408
|
2479 } |
|
2480 else |
7409
|
2481 error ("drawnow: invalid colormode, expected a boolean value"); |
7408
|
2482 } |
|
2483 else |
7409
|
2484 error ("drawnow: invalid file, expected a string value"); |
7408
|
2485 } |
|
2486 else |
7409
|
2487 error ("drawnow: invalid terminal, expected a string value"); |
7408
|
2488 } |
|
2489 else |
7409
|
2490 print_usage (); |
7408
|
2491 } |
7409
|
2492 |
|
2493 unwind_protect::run_frame ("Fdrawnow"); |
7408
|
2494 |
|
2495 return retval; |
|
2496 } |
|
2497 |
6595
|
2498 octave_value |
|
2499 get_property_from_handle (double handle, const std::string &property, |
|
2500 const std::string &func) |
|
2501 { |
|
2502 graphics_object obj = gh_manager::get_object (handle); |
|
2503 octave_value retval; |
|
2504 |
|
2505 if (obj) |
|
2506 { |
7189
|
2507 caseless_str p = std::string (property); |
6595
|
2508 retval = obj.get (p); |
|
2509 } |
|
2510 else |
|
2511 error ("%s: invalid handle (= %g)", func.c_str(), handle); |
|
2512 |
|
2513 return retval; |
|
2514 } |
|
2515 |
|
2516 bool |
|
2517 set_property_in_handle (double handle, const std::string &property, |
|
2518 const octave_value &arg, const std::string &func) |
|
2519 { |
|
2520 graphics_object obj = gh_manager::get_object (handle); |
|
2521 int ret = false; |
|
2522 |
|
2523 if (obj) |
|
2524 { |
7189
|
2525 caseless_str p = std::string (property); |
6595
|
2526 obj.set (p, arg); |
|
2527 if (!error_state) |
|
2528 ret = true; |
|
2529 } |
|
2530 else |
|
2531 error ("%s: invalid handle (= %g)", func.c_str(), handle); |
|
2532 |
|
2533 return ret; |
|
2534 } |
|
2535 |
6406
|
2536 /* |
|
2537 ;;; Local Variables: *** |
|
2538 ;;; mode: C++ *** |
|
2539 ;;; End: *** |
|
2540 */ |