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