Mercurial > octave-nkf
annotate src/graphics.cc @ 7824:adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
author | Michael Goffioul <michael.goffioul@gmail.com> |
---|---|
date | Fri, 08 Feb 2008 16:46:34 +0100 |
parents | edbaa13397ee |
children | 3584f37eac69 |
rev | line source |
---|---|
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 | |
7427 | 145 static Matrix |
146 default_axes_position (void) | |
147 { | |
148 Matrix m (1, 4, 0.0); | |
149 m(0) = 0.13; | |
150 m(1) = 0.11; | |
151 m(2) = 0.775; | |
152 m(3) = 0.815; | |
153 return m; | |
154 } | |
155 | |
156 static Matrix | |
157 default_axes_outerposition (void) | |
158 { | |
159 Matrix m (1, 4, 0.0); | |
160 m(2) = m(3) = 1.0; | |
161 return m; | |
162 } | |
163 | |
7445 | 164 static Matrix |
165 default_figure_position (void) | |
166 { | |
167 Matrix m (1, 4, 0.0); | |
168 m(0) = 300; | |
169 m(1) = 200; | |
170 m(2) = 560; | |
171 m(3) = 420; | |
172 return m; | |
173 } | |
174 | |
7822
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
175 static void |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
176 xset_gcbo (const graphics_handle& h) |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
177 { |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
178 graphics_object go = gh_manager::get_object (0); |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
179 root_figure::properties& props = |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
180 dynamic_cast<root_figure::properties&> (go.get_properties ()); |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
181 |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
182 props.set_callbackobject (h.as_octave_value ()); |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
183 } |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
184 |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
185 static void |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
186 xreset_gcbo (void *) |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
187 { |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
188 xset_gcbo (graphics_handle ()); |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
189 } |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
190 |
7367 | 191 // NOTE: "cb" is passed by value, because "function_value" method |
192 // is non-const; passing "cb" by const-reference is not | |
193 // possible | |
194 | |
195 static void | |
196 execute_callback (octave_value cb, const graphics_handle& h, | |
197 const octave_value& data) | |
198 { | |
199 octave_value_list args; | |
200 octave_function *fcn = 0; | |
201 | |
202 args(0) = h.as_octave_value (); | |
7824
adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7822
diff
changeset
|
203 if (data.is_defined ()) |
adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7822
diff
changeset
|
204 args(1) = data; |
adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7822
diff
changeset
|
205 else |
adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7822
diff
changeset
|
206 args(1) = Matrix (); |
7367 | 207 |
7822
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
208 unwind_protect::begin_frame ("execute_callback"); |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
209 unwind_protect::add (xreset_gcbo); |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
210 |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
211 xset_gcbo (h); |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
212 |
7367 | 213 BEGIN_INTERRUPT_WITH_EXCEPTIONS; |
214 | |
215 if (cb.is_function_handle ()) | |
216 fcn = cb.function_value (); | |
217 else if (cb.is_string ()) | |
218 { | |
7824
adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7822
diff
changeset
|
219 int status; |
7367 | 220 std::string s = cb.string_value (); |
7824
adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7822
diff
changeset
|
221 |
adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7822
diff
changeset
|
222 eval_string (s, false, status); |
7367 | 223 } |
224 else if (cb.is_cell () && cb.length () > 0 | |
225 && (cb.rows () == 1 || cb.columns () == 1) | |
226 && cb.cell_value ()(0).is_function_handle ()) | |
227 { | |
228 Cell c = cb.cell_value (); | |
229 | |
230 fcn = c(0).function_value (); | |
231 if (! error_state) | |
232 { | |
233 for (int i = 0; i < c.length () ; i++) | |
234 args(2+i) = c(i); | |
235 } | |
236 } | |
237 else | |
7373 | 238 { |
239 std::string nm = cb.class_name (); | |
240 error ("trying to execute non-executable object (class = %s)", | |
241 nm.c_str ()); | |
242 } | |
7367 | 243 |
7822
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
244 if (fcn && ! error_state) |
7367 | 245 feval (fcn, args); |
246 | |
247 END_INTERRUPT_WITH_EXCEPTIONS; | |
7822
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
248 |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
249 unwind_protect::run_frame ("execute_callback"); |
7367 | 250 } |
251 | |
7427 | 252 static Matrix |
253 convert_position (const Matrix& pos, const caseless_str& from_units, | |
254 const caseless_str& to_units, | |
255 const Matrix& parent_dim = Matrix (1, 2, 0.0), | |
256 const graphics_backend& backend = graphics_backend ()) | |
257 { | |
258 Matrix retval (1, 4); | |
259 double res = 0; | |
260 | |
261 if (from_units.compare ("pixels")) | |
262 retval = pos; | |
263 else if (from_units.compare ("normalized")) | |
264 { | |
265 retval(0) = pos(0) * parent_dim(0) + 1; | |
266 retval(1) = pos(1) * parent_dim(1) + 1; | |
267 retval(2) = pos(2) * parent_dim(0); | |
268 retval(3) = pos(3) * parent_dim(1); | |
269 } | |
270 else if (from_units.compare ("characters")) | |
271 { | |
272 // FIXME: implement this | |
273 } | |
274 else | |
275 { | |
276 res = backend.get_screen_resolution (); | |
277 | |
278 double f = 0.0; | |
279 | |
280 if (from_units.compare ("points")) | |
281 f = res / 72.0; | |
282 else if (from_units.compare ("inches")) | |
283 f = res; | |
284 else if (from_units.compare ("centimeters")) | |
285 f = res / 2.54; | |
286 | |
287 if (f > 0) | |
288 { | |
289 retval(0) = pos(0) * f + 1; | |
290 retval(1) = pos(1) * f + 1; | |
291 retval(2) = pos(2) * f; | |
292 retval(3) = pos(3) * f; | |
293 } | |
294 } | |
295 | |
296 if (! to_units.compare ("pixels")) | |
297 { | |
298 if (to_units.compare ("normalized")) | |
299 { | |
300 retval(0) = (retval(0) - 1) / parent_dim(0); | |
301 retval(1) = (retval(1) - 1) / parent_dim(1); | |
302 retval(2) /= parent_dim(0); | |
303 retval(3) /= parent_dim(1); | |
304 } | |
305 else if (to_units.compare ("characters")) | |
306 { | |
307 // FIXME: implement this | |
308 } | |
309 else | |
310 { | |
311 if (res <= 0) | |
312 res = backend.get_screen_resolution (); | |
313 | |
314 double f = 0.0; | |
315 | |
316 if (to_units.compare ("points")) | |
317 f = res / 72.0; | |
318 else if (to_units.compare ("inches")) | |
319 f = res; | |
320 else if (to_units.compare ("centimeters")) | |
321 f = res / 2.54; | |
322 | |
323 if (f > 0) | |
324 { | |
325 retval(0) = (retval(0) - 1) / f; | |
326 retval(1) = (retval(1) - 1) / f; | |
327 retval(2) /= f; | |
328 retval(3) /= f; | |
329 } | |
330 } | |
331 } | |
332 | |
333 return retval; | |
334 } | |
335 | |
6406 | 336 // --------------------------------------------------------------------- |
337 | |
6705 | 338 radio_values::radio_values (const std::string& opt_string) |
6406 | 339 { |
6705 | 340 size_t beg = 0; |
341 size_t len = opt_string.length (); | |
342 bool done = len == 0; | |
6681 | 343 |
6705 | 344 while (! done) |
345 { | |
346 size_t end = opt_string.find ('|', beg); | |
6681 | 347 |
6705 | 348 if (end == std::string::npos) |
349 { | |
350 end = len; | |
351 done = true; | |
352 } | |
6681 | 353 |
6705 | 354 std::string t = opt_string.substr (beg, end-beg); |
6681 | 355 |
6705 | 356 // Might want more error checking here... |
357 if (t[0] == '{') | |
358 { | |
359 t = t.substr (1, t.length () - 2); | |
360 default_val = t; | |
361 } | |
362 else if (beg == 0) // ensure default value | |
363 default_val = t; | |
6681 | 364 |
6705 | 365 possible_vals.insert (t); |
6681 | 366 |
6705 | 367 beg = end + 1; |
368 } | |
369 } | |
6681 | 370 |
6705 | 371 bool |
6761 | 372 color_values::str2rgb (std::string str) |
6681 | 373 { |
6705 | 374 double tmp_rgb[3] = {0, 0, 0}; |
375 bool retval = true; | |
6761 | 376 unsigned int len = str.length(); |
6681 | 377 |
6925 | 378 std::transform (str.begin (), str.end (), str.begin (), tolower); |
379 | |
6761 | 380 if (str.compare(0, len, "blue", 0, len) == 0) |
381 tmp_rgb[2] = 1; | |
6925 | 382 else if (str.compare(0, len, "black", 0, len) == 0 || |
383 str.compare(0, len, "k", 0, len) == 0) | |
6761 | 384 tmp_rgb[0] = tmp_rgb[1] = tmp_rgb[2] = 0; |
385 else if (str.compare(0, len, "red", 0, len) == 0) | |
386 tmp_rgb[0] = 1; | |
387 else if (str.compare(0, len, "green", 0, len) == 0) | |
388 tmp_rgb[1] = 1; | |
389 else if (str.compare(0, len, "yellow", 0, len) == 0) | |
390 tmp_rgb[0] = tmp_rgb[1] = 1; | |
391 else if (str.compare(0, len, "magenta", 0, len) == 0) | |
392 tmp_rgb[0] = tmp_rgb[2] = 1; | |
393 else if (str.compare(0, len, "cyan", 0, len) == 0) | |
394 tmp_rgb[1] = tmp_rgb[2] = 1; | |
6925 | 395 else if (str.compare(0, len, "white", 0, len) == 0 || |
396 str.compare(0, len, "w", 0, len) == 0) | |
6761 | 397 tmp_rgb[0] = tmp_rgb[1] = tmp_rgb[2] = 1; |
398 else | |
399 retval = false; | |
6406 | 400 |
6705 | 401 if (retval) |
402 { | |
403 for (int i = 0; i < 3; i++) | |
7363 | 404 xrgb(i) = tmp_rgb[i]; |
6705 | 405 } |
6563 | 406 |
6705 | 407 return retval; |
408 } | |
6681 | 409 |
7363 | 410 void |
411 color_property::set (const octave_value& val) | |
6790 | 412 { |
413 if (val.is_string ()) | |
414 { | |
415 std::string s = val.string_value (); | |
416 | |
417 if (! s.empty ()) | |
418 { | |
6898 | 419 if (radio_val.contains (s)) |
6790 | 420 { |
421 current_val = s; | |
422 current_type = radio_t; | |
423 } | |
424 else | |
425 { | |
426 color_values col (s); | |
427 if (! error_state) | |
428 { | |
429 color_val = col; | |
430 current_type = color_t; | |
431 } | |
432 else | |
7363 | 433 error ("invalid value for color property \"%s\" (value = %s)", |
434 get_name ().c_str (), s.c_str ()); | |
6790 | 435 } |
436 } | |
437 else | |
7363 | 438 error ("invalid value for color property \"%s\"", |
439 get_name ().c_str ()); | |
6790 | 440 } |
441 else if (val.is_real_matrix ()) | |
442 { | |
443 Matrix m = val.matrix_value (); | |
444 | |
445 if (m.numel () == 3) | |
446 { | |
447 color_values col (m (0), m (1), m(2)); | |
448 if (! error_state) | |
449 { | |
450 color_val = col; | |
451 current_type = color_t; | |
452 } | |
453 } | |
454 else | |
7363 | 455 error ("invalid value for color property \"%s\"", |
456 get_name ().c_str ()); | |
6790 | 457 } |
458 else | |
7363 | 459 error ("invalid value for color property \"%s\"", |
460 get_name ().c_str ()); | |
6790 | 461 } |
462 | |
7363 | 463 bool |
464 array_property::validate (const octave_value& v) | |
465 { | |
7364 | 466 bool xok = false; |
7363 | 467 |
468 // FIXME: should we always support []? | |
469 if (v.is_empty () && v.is_double_type ()) | |
470 return true; | |
471 | |
472 // check value type | |
473 if (type_constraints.size () > 0) | |
474 { | |
475 for (std::list<std::string>::const_iterator it = type_constraints.begin (); | |
7364 | 476 ! xok && it != type_constraints.end (); ++it) |
7363 | 477 if ((*it) == v.type_name ()) |
7364 | 478 xok = true; |
7363 | 479 } |
480 else | |
7364 | 481 xok = v.is_double_type (); |
7363 | 482 |
7364 | 483 if (xok) |
7363 | 484 { |
485 dim_vector vdims = v.dims (); | |
486 int vlen = vdims.length (); | |
487 | |
7364 | 488 xok = false; |
7363 | 489 |
490 // check value size | |
491 if (size_constraints.size () > 0) | |
492 for (std::list<dim_vector>::const_iterator it = size_constraints.begin (); | |
7364 | 493 ! xok && it != size_constraints.end (); ++it) |
7363 | 494 { |
495 dim_vector itdims = (*it); | |
496 | |
497 if (itdims.length () == vlen) | |
498 { | |
7364 | 499 xok = true; |
7363 | 500 |
7364 | 501 for (int i = 0; xok && i < vlen; i++) |
7363 | 502 if (itdims(i) >= 0 && itdims(i) != vdims(i)) |
7364 | 503 xok = false; |
7363 | 504 } |
505 } | |
506 else | |
507 return true; | |
508 } | |
509 | |
7364 | 510 return xok; |
7363 | 511 } |
512 | |
513 void | |
514 handle_property::set (const octave_value& v) | |
515 { | |
516 double dv = v.double_value (); | |
517 | |
518 if (! error_state) | |
519 { | |
520 graphics_handle gh = gh_manager::lookup (dv); | |
521 | |
522 if (xisnan (gh.value ()) || gh.ok ()) | |
523 current_val = gh; | |
524 else | |
525 error ("set: invalid graphics handle (= %g) for property \"%s\"", | |
526 dv, get_name ().c_str ()); | |
527 } | |
528 else | |
529 error ("set: invalid graphics handle for property \"%s\"", | |
530 get_name ().c_str ()); | |
531 } | |
532 | |
533 bool | |
7367 | 534 callback_property::validate (const octave_value& v) const |
7363 | 535 { |
7367 | 536 // case 1: function handle |
537 // case 2: cell array with first element being a function handle | |
538 // case 3: string corresponding to known function name | |
539 // case 4: evaluatable string | |
540 // case 5: empty matrix | |
541 | |
542 if (v.is_function_handle ()) | |
543 return true; | |
544 else if (v.is_string ()) | |
545 // complete validation will be done at execution-time | |
546 return true; | |
547 else if (v.is_cell () && v.length () > 0 | |
548 && (v.rows() == 1 || v.columns () == 1) | |
549 && v.cell_value ()(0).is_function_handle ()) | |
550 return true; | |
551 else if (v.is_empty ()) | |
552 return true; | |
553 | |
554 return false; | |
7363 | 555 } |
556 | |
557 void | |
7367 | 558 callback_property::execute (const octave_value& data) const |
7363 | 559 { |
7367 | 560 if (callback.is_defined () && ! callback.is_empty ()) |
561 execute_callback (callback, get_parent (), data); | |
7363 | 562 } |
563 | |
7824
adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7822
diff
changeset
|
564 void |
adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7822
diff
changeset
|
565 callback_property::execute (const octave_value& cb, const graphics_handle& h, |
adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7822
diff
changeset
|
566 const octave_value& data) |
adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7822
diff
changeset
|
567 { |
adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7822
diff
changeset
|
568 if (cb.is_defined () && ! cb.is_empty ()) |
adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7822
diff
changeset
|
569 execute_callback (cb, h, data); |
adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7822
diff
changeset
|
570 } |
adb520646d7e
Fix execution of callback strings and allow execution of callback by name.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7822
diff
changeset
|
571 |
7363 | 572 // --------------------------------------------------------------------- |
6681 | 573 |
6705 | 574 void |
7189 | 575 property_list::set (const caseless_str& name, const octave_value& val) |
6681 | 576 { |
6705 | 577 size_t offset = 0; |
6681 | 578 |
6705 | 579 size_t len = name.length (); |
6681 | 580 |
6705 | 581 if (len > 4) |
582 { | |
7189 | 583 caseless_str pfx = name.substr (0, 4); |
6681 | 584 |
6705 | 585 if (pfx.compare ("axes") || pfx.compare ("line") |
586 || pfx.compare ("text")) | |
587 offset = 4; | |
588 else if (len > 5) | |
589 { | |
590 pfx = name.substr (0, 5); | |
6681 | 591 |
6807 | 592 if (pfx.compare ("image") || pfx.compare ("patch")) |
6705 | 593 offset = 5; |
594 else if (len > 6) | |
595 { | |
596 pfx = name.substr (0, 6); | |
6681 | 597 |
6705 | 598 if (pfx.compare ("figure")) |
599 offset = 6; | |
600 else if (len > 7) | |
601 { | |
602 pfx = name.substr (0, 7); | |
6681 | 603 |
6705 | 604 if (pfx.compare ("surface")) |
605 offset = 7; | |
606 } | |
607 } | |
608 } | |
6681 | 609 |
6705 | 610 if (offset > 0) |
611 { | |
612 // FIXME -- should we validate property names and values here? | |
6406 | 613 |
6705 | 614 std::string pname = name.substr (offset); |
6406 | 615 |
6705 | 616 std::transform (pfx.begin (), pfx.end (), pfx.begin (), tolower); |
617 std::transform (pname.begin (), pname.end (), pname.begin (), tolower); | |
6406 | 618 |
6705 | 619 bool remove = false; |
620 if (val.is_string ()) | |
621 { | |
7189 | 622 caseless_str tval = val.string_value (); |
6406 | 623 |
6705 | 624 remove = tval.compare ("remove"); |
625 } | |
6406 | 626 |
6705 | 627 pval_map_type& pval_map = plist_map[pfx]; |
6406 | 628 |
6705 | 629 if (remove) |
630 { | |
631 pval_map_iterator p = pval_map.find (pname); | |
6406 | 632 |
6705 | 633 if (p != pval_map.end ()) |
634 pval_map.erase (p); | |
635 } | |
636 else | |
637 pval_map[pname] = val; | |
638 } | |
639 } | |
6406 | 640 |
6705 | 641 if (offset == 0) |
642 error ("invalid default property specification"); | |
643 } | |
6406 | 644 |
6705 | 645 octave_value |
7189 | 646 property_list::lookup (const caseless_str& name) const |
6705 | 647 { |
648 octave_value retval; | |
6406 | 649 |
6705 | 650 size_t offset = 0; |
6406 | 651 |
6705 | 652 size_t len = name.length (); |
6406 | 653 |
6705 | 654 if (len > 4) |
655 { | |
7189 | 656 caseless_str pfx = name.substr (0, 4); |
6406 | 657 |
6705 | 658 if (pfx.compare ("axes") || pfx.compare ("line") |
659 || pfx.compare ("text")) | |
660 offset = 4; | |
661 else if (len > 5) | |
662 { | |
663 pfx = name.substr (0, 5); | |
6406 | 664 |
6807 | 665 if (pfx.compare ("image") || pfx.compare ("patch")) |
6705 | 666 offset = 5; |
667 else if (len > 6) | |
668 { | |
669 pfx = name.substr (0, 6); | |
6406 | 670 |
6705 | 671 if (pfx.compare ("figure")) |
672 offset = 6; | |
673 else if (len > 7) | |
674 { | |
675 pfx = name.substr (0, 7); | |
6406 | 676 |
6705 | 677 if (pfx.compare ("surface")) |
678 offset = 7; | |
679 } | |
680 } | |
681 } | |
6406 | 682 |
6705 | 683 if (offset > 0) |
684 { | |
685 std::string pname = name.substr (offset); | |
6406 | 686 |
6705 | 687 std::transform (pfx.begin (), pfx.end (), pfx.begin (), tolower); |
688 std::transform (pname.begin (), pname.end (), pname.begin (), tolower); | |
6406 | 689 |
6705 | 690 plist_map_const_iterator p = find (pfx); |
6432 | 691 |
6705 | 692 if (p != end ()) |
693 { | |
694 const pval_map_type& pval_map = p->second; | |
6406 | 695 |
6705 | 696 pval_map_const_iterator q = pval_map.find (pname); |
6406 | 697 |
6705 | 698 if (q != pval_map.end ()) |
699 retval = q->second; | |
700 } | |
701 } | |
702 } | |
6406 | 703 |
6705 | 704 return retval; |
705 } | |
6406 | 706 |
6705 | 707 Octave_map |
708 property_list::as_struct (const std::string& prefix_arg) const | |
709 { | |
710 Octave_map m; | |
6406 | 711 |
6705 | 712 for (plist_map_const_iterator p = begin (); p != end (); p++) |
713 { | |
714 std::string prefix = prefix_arg + p->first; | |
6406 | 715 |
6705 | 716 const pval_map_type pval_map = p->second; |
6406 | 717 |
6705 | 718 for (pval_map_const_iterator q = pval_map.begin (); |
719 q != pval_map.end (); | |
720 q++) | |
721 m.assign (prefix + q->first, q->second); | |
722 } | |
6406 | 723 |
6705 | 724 return m; |
725 } | |
6432 | 726 |
6874 | 727 graphics_handle::graphics_handle (const octave_value& a) |
728 : val (octave_NaN) | |
729 { | |
730 if (a.is_empty ()) | |
731 /* do nothing */; | |
732 else | |
733 { | |
734 double tval = a.double_value (); | |
735 | |
736 if (! error_state) | |
737 val = tval; | |
738 else | |
739 error ("invalid graphics handle"); | |
740 } | |
741 } | |
742 | |
6705 | 743 void |
744 graphics_object::set (const octave_value_list& args) | |
745 { | |
746 int nargin = args.length (); | |
6406 | 747 |
6705 | 748 if (nargin == 0) |
749 rep->defaults (); | |
750 else if (nargin % 2 == 0) | |
751 { | |
752 for (int i = 0; i < nargin; i += 2) | |
753 { | |
7189 | 754 caseless_str name = args(i).string_value (); |
6406 | 755 |
6705 | 756 if (! error_state) |
757 { | |
758 octave_value val = args(i+1); | |
6406 | 759 |
6705 | 760 if (val.is_string ()) |
761 { | |
7189 | 762 caseless_str tval = val.string_value (); |
6406 | 763 |
6705 | 764 if (tval.compare ("default")) |
765 val = get_default (name); | |
766 else if (tval.compare ("factory")) | |
767 val = get_factory_default (name); | |
768 } | |
6406 | 769 |
6705 | 770 if (error_state) |
771 break; | |
6406 | 772 |
6705 | 773 rep->set (name, val); |
774 } | |
775 else | |
776 error ("set: expecting argument %d to be a property name", i); | |
777 } | |
778 } | |
779 else | |
780 error ("set: invalid number of arguments"); | |
781 } | |
6406 | 782 |
783 | |
6705 | 784 graphics_handle |
785 gh_manager::get_handle (const std::string& go_name) | |
786 { | |
787 graphics_handle retval; | |
6406 | 788 |
6705 | 789 if (go_name == "figure") |
790 { | |
791 // We always want the lowest unused figure number. | |
6406 | 792 |
6705 | 793 retval = 1; |
6425 | 794 |
6705 | 795 while (handle_map.find (retval) != handle_map.end ()) |
796 retval++; | |
797 } | |
798 else | |
799 { | |
800 free_list_iterator p = handle_free_list.begin (); | |
6406 | 801 |
6705 | 802 if (p != handle_free_list.end ()) |
803 { | |
804 retval = *p; | |
805 handle_free_list.erase (p); | |
806 } | |
807 else | |
7286 | 808 { |
809 static double maxrand = RAND_MAX + 2.0; | |
810 | |
811 retval = graphics_handle (next_handle); | |
812 | |
7304 | 813 next_handle = ceil (next_handle) - 1.0 - (rand () + 1.0) / maxrand; |
7286 | 814 } |
6705 | 815 } |
6406 | 816 |
6705 | 817 return retval; |
818 } | |
6406 | 819 |
6705 | 820 void |
821 gh_manager::do_free (const graphics_handle& h) | |
822 { | |
7056 | 823 if (h.ok ()) |
6705 | 824 { |
6874 | 825 if (h.value () != 0) |
6705 | 826 { |
6874 | 827 iterator p = handle_map.find (h); |
828 | |
829 if (p != handle_map.end ()) | |
830 { | |
7403 | 831 p->second.get_properties ().set_beingdeleted (true); |
832 p->second.get_properties ().execute_deletefcn (); | |
7367 | 833 |
6874 | 834 handle_map.erase (p); |
835 | |
836 if (h.value () < 0) | |
837 handle_free_list.insert (h); | |
838 } | |
839 else | |
840 error ("graphics_handle::free: invalid object %g", h.value ()); | |
6705 | 841 } |
842 else | |
6874 | 843 error ("graphics_handle::free: can't delete root figure"); |
6705 | 844 } |
845 } | |
6406 | 846 |
847 gh_manager *gh_manager::instance = 0; | |
848 | |
849 static void | |
7189 | 850 xset (const graphics_handle& h, const caseless_str& name, |
6406 | 851 const octave_value& val) |
852 { | |
853 graphics_object obj = gh_manager::get_object (h); | |
854 obj.set (name, val); | |
855 } | |
856 | |
857 static void | |
858 xset (const graphics_handle& h, const octave_value_list& args) | |
859 { | |
860 if (args.length () > 0) | |
861 { | |
862 graphics_object obj = gh_manager::get_object (h); | |
863 obj.set (args); | |
864 } | |
865 } | |
866 | |
867 | |
868 static octave_value | |
7189 | 869 xget (const graphics_handle& h, const caseless_str& name) |
6406 | 870 { |
871 graphics_object obj = gh_manager::get_object (h); | |
872 return obj.get (name); | |
873 } | |
874 | |
875 static graphics_handle | |
876 reparent (const octave_value& ov, const std::string& who, | |
877 const std::string& property, const graphics_handle& new_parent, | |
878 bool adopt = true) | |
879 { | |
880 graphics_handle h = octave_NaN; | |
881 | |
882 double val = ov.double_value (); | |
883 | |
884 if (! error_state) | |
885 { | |
886 h = gh_manager::lookup (val); | |
887 | |
7056 | 888 if (h.ok ()) |
6406 | 889 { |
890 graphics_object obj = gh_manager::get_object (h); | |
891 | |
892 graphics_handle parent_h = obj.get_parent (); | |
893 | |
894 graphics_object parent_obj = gh_manager::get_object (parent_h); | |
895 | |
896 parent_obj.remove_child (h); | |
897 | |
898 if (adopt) | |
6874 | 899 obj.set ("parent", new_parent.value ()); |
6406 | 900 else |
901 obj.reparent (new_parent); | |
902 } | |
903 else | |
904 error ("%s: invalid graphics handle (= %g) for %s", | |
905 who.c_str (), val, property.c_str ()); | |
906 } | |
907 else | |
908 error ("%s: expecting %s to be a graphics handle", | |
909 who.c_str (), property.c_str ()); | |
910 | |
911 return h; | |
912 } | |
913 | |
914 // This function is NOT equivalent to the scripting language function gcf. | |
915 graphics_handle | |
916 gcf (void) | |
917 { | |
918 octave_value val = xget (0, "currentfigure"); | |
919 | |
920 return val.is_empty () ? octave_NaN : val.double_value (); | |
921 } | |
922 | |
923 // This function is NOT equivalent to the scripting language function gca. | |
924 graphics_handle | |
925 gca (void) | |
926 { | |
927 octave_value val = xget (gcf (), "currentaxes"); | |
928 | |
929 return val.is_empty () ? octave_NaN : val.double_value (); | |
930 } | |
931 | |
932 static void | |
933 adopt (const graphics_handle& p, const graphics_handle& h) | |
934 { | |
935 graphics_object parent_obj = gh_manager::get_object (p); | |
936 | |
937 parent_obj.adopt (h); | |
938 } | |
939 | |
940 static bool | |
7056 | 941 is_handle (const graphics_handle& h) |
942 { | |
943 return h.ok (); | |
944 } | |
945 | |
946 static bool | |
6406 | 947 is_handle (double val) |
948 { | |
6874 | 949 graphics_handle h = gh_manager::lookup (val); |
950 | |
951 return h.ok (); | |
6406 | 952 } |
953 | |
954 static bool | |
955 is_handle (const octave_value& val) | |
956 { | |
7142 | 957 return val.is_real_scalar () && is_handle (val.double_value ()); |
6406 | 958 } |
959 | |
960 static bool | |
961 is_figure (double val) | |
962 { | |
963 graphics_object obj = gh_manager::get_object (val); | |
964 | |
965 return obj && obj.isa ("figure"); | |
966 } | |
967 | |
7370 | 968 static void |
969 xcreatefcn (const graphics_handle& h) | |
970 { | |
971 graphics_object obj = gh_manager::get_object (h); | |
972 obj.get_properties ().execute_createfcn (); | |
973 } | |
974 | |
6406 | 975 // --------------------------------------------------------------------- |
976 | |
977 static int | |
978 compare (const void *a_arg, const void *b_arg) | |
979 { | |
980 double a = *(static_cast<const double *> (a_arg)); | |
981 double b = *(static_cast<const double *> (b_arg)); | |
982 | |
983 return a > b ? 1 : (a < b) ? -1 : 0; | |
984 } | |
985 | |
986 static Matrix | |
987 maybe_set_children (const Matrix& kids, const octave_value& val) | |
988 { | |
989 const Matrix new_kids = val.matrix_value (); | |
990 | |
991 bool ok = true; | |
992 | |
993 if (! error_state) | |
994 { | |
995 if (kids.numel () == new_kids.numel ()) | |
996 { | |
997 Matrix t1 = kids; | |
998 Matrix t2 = new_kids; | |
999 | |
1000 t1.qsort (compare); | |
1001 t2.qsort (compare); | |
1002 | |
1003 if (t1 != t2) | |
1004 ok = false; | |
1005 } else | |
1006 ok = false; | |
1007 | |
1008 if (! ok) | |
1009 error ("set: new children must be a permutation of existing children"); | |
1010 } | |
1011 else | |
1012 { | |
1013 ok = false; | |
1014 error ("set: expecting children to be array of graphics handles"); | |
1015 } | |
1016 | |
1017 return ok ? new_kids : kids; | |
1018 } | |
1019 | |
6705 | 1020 void |
1021 base_properties::set_from_list (base_graphics_object& obj, | |
1022 property_list& defaults) | |
6406 | 1023 { |
6705 | 1024 std::string go_name = graphics_object_name (); |
6406 | 1025 |
6705 | 1026 property_list::plist_map_const_iterator p = defaults.find (go_name); |
6406 | 1027 |
6705 | 1028 if (p != defaults.end ()) |
1029 { | |
1030 const property_list::pval_map_type pval_map = p->second; | |
6406 | 1031 |
6705 | 1032 for (property_list::pval_map_const_iterator q = pval_map.begin (); |
1033 q != pval_map.end (); | |
1034 q++) | |
1035 { | |
1036 std::string pname = q->first; | |
6406 | 1037 |
6705 | 1038 obj.set (pname, q->second); |
6406 | 1039 |
6705 | 1040 if (error_state) |
1041 { | |
1042 error ("error setting default property %s", pname.c_str ()); | |
1043 break; | |
1044 } | |
1045 } | |
1046 } | |
1047 } | |
6406 | 1048 |
7363 | 1049 octave_value |
1050 base_properties::get (const caseless_str& name) const | |
1051 { | |
1052 octave_value retval; | |
1053 | |
1054 if (name.compare ("tag")) | |
1055 retval = get_tag (); | |
1056 else if (name.compare ("type")) | |
1057 retval = get_type (); | |
1058 else if (name.compare ("__modified__")) | |
1059 retval = is_modified (); | |
1060 else if (name.compare ("parent")) | |
1061 retval = get_parent ().as_octave_value (); | |
1062 else if (name.compare ("children")) | |
1063 retval = children; | |
7366 | 1064 else if (name.compare ("busyaction")) |
1065 retval = get_busyaction (); | |
1066 else if (name.compare ("buttondownfcn")) | |
1067 retval = get_buttondownfcn (); | |
1068 else if (name.compare ("clipping")) | |
1069 retval = get_clipping (); | |
1070 else if (name.compare ("createfcn")) | |
1071 retval = get_createfcn (); | |
1072 else if (name.compare ("deletefcn")) | |
1073 retval = get_deletefcn (); | |
1074 else if (name.compare ("handlevisibility")) | |
1075 retval = get_handlevisibility (); | |
1076 else if (name.compare ("hittest")) | |
1077 retval = get_hittest (); | |
1078 else if (name.compare ("interruptible")) | |
1079 retval = get_interruptible (); | |
1080 else if (name.compare ("selected")) | |
1081 retval = get_selected (); | |
1082 else if (name.compare ("selectionhighlight")) | |
1083 retval = get_selectionhighlight (); | |
1084 else if (name.compare ("uicontextmenu")) | |
1085 retval = get_uicontextmenu (); | |
1086 else if (name.compare ("userdata")) | |
1087 retval = get_userdata (); | |
1088 else if (name.compare ("visible")) | |
1089 retval = get_visible (); | |
7403 | 1090 else if (name.compare ("beingdeleted")) |
1091 retval = get_beingdeleted (); | |
7363 | 1092 else |
1093 { | |
1094 std::map<caseless_str, property>::const_iterator it = all_props.find (name); | |
1095 | |
1096 if (it != all_props.end ()) | |
1097 retval = it->second.get (); | |
1098 else | |
1099 error ("get: unknown property \"%s\"", name.c_str ()); | |
1100 } | |
1101 | |
1102 return retval; | |
1103 } | |
1104 | |
1105 octave_value | |
7379 | 1106 base_properties::get (bool all) const |
7363 | 1107 { |
1108 Octave_map m; | |
1109 | |
1110 for (std::map<caseless_str, property>::const_iterator it = all_props.begin (); | |
1111 it != all_props.end (); ++it) | |
7379 | 1112 if (all || ! it->second.is_hidden ()) |
1113 m.assign (it->second.get_name (), it->second.get ()); | |
7363 | 1114 |
1115 m.assign ("tag", get_tag ()); | |
1116 m.assign ("type", get_type ()); | |
7379 | 1117 if (all) |
1118 m.assign ("__modified__", is_modified ()); | |
7363 | 1119 m.assign ("parent", get_parent ().as_octave_value ()); |
1120 m.assign ("children", children); | |
7366 | 1121 m.assign ("busyaction", get_busyaction ()); |
1122 m.assign ("buttondownfcn", get_buttondownfcn ()); | |
1123 m.assign ("clipping", get_clipping ()); | |
1124 m.assign ("createfcn", get_createfcn ()); | |
1125 m.assign ("deletefcn", get_deletefcn ()); | |
1126 m.assign ("handlevisibility", get_handlevisibility ()); | |
1127 m.assign ("hittest", get_hittest ()); | |
1128 m.assign ("interruptible", get_interruptible ()); | |
1129 m.assign ("selected", get_selected ()); | |
1130 m.assign ("selectionhighlight", get_selectionhighlight ()); | |
1131 m.assign ("uicontextmenu", get_uicontextmenu ()); | |
1132 m.assign ("userdata", get_userdata ()); | |
1133 m.assign ("visible", get_visible ()); | |
7403 | 1134 m.assign ("beingdeleted", get_beingdeleted ()); |
7363 | 1135 |
1136 return m; | |
1137 } | |
1138 | |
1139 void | |
1140 base_properties::set (const caseless_str& name, const octave_value& val) | |
1141 { | |
1142 if (name.compare ("tag")) | |
1143 set_tag (val); | |
1144 else if (name.compare ("__modified__")) | |
1145 __modified__ = val; | |
1146 else if (name.compare ("parent")) | |
1147 set_parent (val); | |
1148 else if (name.compare ("children")) | |
1149 maybe_set_children (children, val); | |
7366 | 1150 else if (name.compare ("busyaction")) |
1151 set_busyaction (val); | |
1152 else if (name.compare ("buttondownfcn")) | |
1153 set_buttondownfcn (val); | |
1154 else if (name.compare ("clipping")) | |
1155 set_clipping (val); | |
1156 else if (name.compare ("createfcn")) | |
1157 set_createfcn (val); | |
1158 else if (name.compare ("deletefcn")) | |
1159 set_deletefcn (val); | |
1160 else if (name.compare ("handlevisibility")) | |
1161 set_handlevisibility (val); | |
1162 else if (name.compare ("hittest")) | |
1163 set_hittest (val); | |
1164 else if (name.compare ("interruptible")) | |
1165 set_interruptible (val); | |
1166 else if (name.compare ("selected")) | |
1167 set_selected (val); | |
1168 else if (name.compare ("selectionhighlight")) | |
1169 set_selectionhighlight (val); | |
1170 else if (name.compare ("uicontextmenu")) | |
1171 set_uicontextmenu (val); | |
1172 else if (name.compare ("userdata")) | |
1173 set_userdata (val); | |
1174 else if (name.compare ("visible")) | |
1175 set_visible (val); | |
7363 | 1176 else |
1177 { | |
1178 std::map<caseless_str, property>::iterator it = all_props.find (name); | |
1179 | |
1180 if (it != all_props.end ()) | |
1181 it->second.set (val); | |
1182 else | |
1183 error ("set: unknown property \"%s\"", name.c_str ()); | |
1184 } | |
1185 | |
1186 if (! error_state && ! name.compare ("__modified__")) | |
1187 mark_modified (); | |
1188 } | |
1189 | |
1190 property | |
1191 base_properties::get_property (const caseless_str& name) const | |
1192 { | |
1193 std::map<caseless_str, property>::const_iterator it = all_props.find (name); | |
1194 | |
1195 if (it == all_props.end ()) | |
1196 return property (); | |
1197 else | |
1198 return it->second; | |
1199 } | |
1200 | |
6705 | 1201 void |
1202 base_properties::remove_child (const graphics_handle& h) | |
6406 | 1203 { |
6705 | 1204 octave_idx_type k = -1; |
1205 octave_idx_type n = children.numel (); | |
1206 for (octave_idx_type i = 0; i < n; i++) | |
6406 | 1207 { |
6874 | 1208 if (h.value () == children(i)) |
6406 | 1209 { |
6705 | 1210 k = i; |
1211 break; | |
6406 | 1212 } |
1213 } | |
1214 | |
6705 | 1215 if (k >= 0) |
6406 | 1216 { |
6705 | 1217 Matrix new_kids (1, n-1); |
1218 octave_idx_type j = 0; | |
1219 for (octave_idx_type i = 0; i < n; i++) | |
1220 { | |
1221 if (i != k) | |
1222 new_kids(j++) = children(i); | |
1223 } | |
1224 children = new_kids; | |
7378 | 1225 mark_modified (); |
6406 | 1226 } |
6705 | 1227 } |
6406 | 1228 |
6836 | 1229 void |
1230 base_properties::set_parent (const octave_value& val) | |
6705 | 1231 { |
1232 double tmp = val.double_value (); | |
6406 | 1233 |
6705 | 1234 graphics_handle new_parent = octave_NaN; |
6406 | 1235 |
6705 | 1236 if (! error_state) |
1237 { | |
1238 new_parent = gh_manager::lookup (tmp); | |
6406 | 1239 |
7056 | 1240 if (new_parent.ok ()) |
6705 | 1241 { |
7363 | 1242 graphics_object parent_obj = gh_manager::get_object (get_parent ()); |
6406 | 1243 |
6705 | 1244 parent_obj.remove_child (__myhandle__); |
6406 | 1245 |
7363 | 1246 parent = new_parent.as_octave_value (); |
6406 | 1247 |
7363 | 1248 ::adopt (parent.handle_value (), __myhandle__); |
6705 | 1249 } |
1250 else | |
1251 error ("set: invalid graphics handle (= %g) for parent", tmp); | |
1252 } | |
1253 else | |
1254 error ("set: expecting parent to be a graphics handle"); | |
1255 } | |
6432 | 1256 |
6705 | 1257 void |
6836 | 1258 base_properties::mark_modified (void) |
1259 { | |
7363 | 1260 __modified__ = "on"; |
1261 graphics_object parent_obj = gh_manager::get_object (get_parent ()); | |
7379 | 1262 if (parent_obj) |
1263 parent_obj.mark_modified (); | |
6836 | 1264 } |
1265 | |
1266 void | |
1267 base_properties::override_defaults (base_graphics_object& obj) | |
1268 { | |
7363 | 1269 graphics_object parent_obj = gh_manager::get_object (get_parent ()); |
6836 | 1270 parent_obj.override_defaults (obj); |
1271 } | |
1272 | |
1273 void | |
7214 | 1274 base_properties::update_axis_limits (const std::string& axis_type) const |
1275 { | |
7363 | 1276 graphics_handle h = (get_type () == "axes") ? __myhandle__ : get_parent (); |
7214 | 1277 |
1278 graphics_object obj = gh_manager::get_object (h); | |
1279 | |
1280 if (obj.isa ("axes")) | |
1281 obj.update_axis_limits (axis_type); | |
1282 } | |
1283 | |
1284 void | |
6836 | 1285 base_properties::delete_children (void) |
1286 { | |
1287 octave_idx_type n = children.numel (); | |
1288 | |
1289 for (octave_idx_type i = 0; i < n; i++) | |
1290 gh_manager::free (children(i)); | |
1291 } | |
1292 | |
7419 | 1293 graphics_backend |
1294 base_properties::get_backend (void) const | |
1295 { | |
1296 graphics_object go = gh_manager::get_object (get_parent ()); | |
1297 | |
1298 if (go) | |
1299 return go.get_backend (); | |
1300 else | |
1301 return graphics_backend (); | |
1302 } | |
1303 | |
7363 | 1304 // --------------------------------------------------------------------- |
1305 | |
7408 | 1306 class gnuplot_backend : public base_graphics_backend |
1307 { | |
1308 public: | |
1309 gnuplot_backend (void) | |
1310 : base_graphics_backend ("gnuplot") { } | |
1311 | |
1312 ~gnuplot_backend (void) { } | |
1313 | |
1314 bool is_valid (void) const { return true; } | |
1315 | |
1316 void close_figure (const octave_value& pstream) const | |
1317 { | |
1318 if (! pstream.is_empty()) | |
1319 { | |
1320 octave_value_list args; | |
7680
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1321 Matrix fids = pstream.matrix_value (); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1322 |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1323 if (! error_state) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1324 { |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1325 args(1) = "\nquit;\n"; |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1326 args(0) = octave_value (fids (0)); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1327 feval ("fputs", args); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1328 args.resize (1); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1329 feval ("fflush", args); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1330 feval ("pclose", args); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1331 if (fids.numel () > 1) |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1332 { |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1333 args(0) = octave_value (fids (1)); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1334 feval ("pclose", args); |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1335 } |
a0ec02774303
Use popen2 for communication with gnuplot
David Bateman <dbateman@free.fr>
parents:
7527
diff
changeset
|
1336 } |
7408 | 1337 } |
1338 } | |
1339 | |
1340 void redraw_figure (const graphics_handle& fh) const | |
1341 { | |
1342 octave_value_list args; | |
1343 args(0) = fh.as_octave_value (); | |
1344 feval ("gnuplot_drawnow", args); | |
1345 } | |
1346 | |
1347 void print_figure (const graphics_handle& fh, const std::string& term, | |
1348 const std::string& file, bool mono, | |
1349 const std::string& debug_file) const | |
1350 { | |
1351 octave_value_list args; | |
1352 if (! debug_file.empty ()) | |
1353 args(4) = debug_file; | |
7409 | 1354 args(3) = mono; |
1355 args(2) = file; | |
1356 args(1) = term; | |
1357 args(0) = fh.as_octave_value (); | |
7408 | 1358 feval ("gnuplot_drawnow", args); |
1359 } | |
1360 | |
7409 | 1361 Matrix get_canvas_size (const graphics_handle&) const |
7427 | 1362 { |
1363 Matrix sz (1, 2, 0.0); | |
1364 return sz; | |
1365 } | |
1366 | |
1367 double get_screen_resolution (void) const | |
1368 { return 72.0; } | |
7445 | 1369 |
1370 Matrix get_screen_size (void) const | |
1371 { return Matrix (1, 2, 0.0); } | |
7408 | 1372 }; |
1373 | |
1374 graphics_backend | |
1375 graphics_backend::default_backend (void) | |
1376 { | |
1377 if (available_backends.size () == 0) | |
1378 register_backend (new gnuplot_backend ()); | |
1379 | |
1380 return available_backends["gnuplot"]; | |
1381 } | |
1382 | |
1383 std::map<std::string, graphics_backend> graphics_backend::available_backends; | |
1384 | |
1385 // --------------------------------------------------------------------- | |
1386 | |
7363 | 1387 #include "graphics-props.cc" |
1388 | |
1389 // --------------------------------------------------------------------- | |
1390 | |
6836 | 1391 void |
7363 | 1392 root_figure::properties::set_currentfigure (const octave_value& v) |
6874 | 1393 { |
7378 | 1394 graphics_handle val (v); |
7363 | 1395 |
6874 | 1396 if (error_state) |
1397 return; | |
1398 | |
7059 | 1399 if (xisnan (val.value ()) || is_handle (val)) |
6874 | 1400 { |
1401 currentfigure = val; | |
1402 | |
7363 | 1403 gh_manager::push_figure (val); |
6874 | 1404 } |
1405 else | |
1406 gripe_set_invalid ("currentfigure"); | |
1407 } | |
1408 | |
7822
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1409 void |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1410 root_figure::properties::set_callbackobject (const octave_value& v) |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1411 { |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1412 graphics_handle val (v); |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1413 |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1414 if (error_state) |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1415 return; |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1416 |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1417 if (xisnan (val.value ())) |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1418 { |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1419 if (! cbo_stack.empty ()) |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1420 { |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1421 val = cbo_stack.front (); |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1422 |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1423 cbo_stack.pop_front (); |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1424 } |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1425 |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1426 callbackobject = val; |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1427 } |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1428 else if (is_handle (val)) |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1429 { |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1430 if (get_callbackobject ().ok ()) |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1431 cbo_stack.push_front (get_callbackobject ()); |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1432 |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1433 callbackobject = val; |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1434 } |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1435 else |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1436 gripe_set_invalid ("callbackobject"); |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1437 } |
edbaa13397ee
Implement callbackobject property in root object.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7820
diff
changeset
|
1438 |
6406 | 1439 property_list |
1440 root_figure::factory_properties = root_figure::init_factory_properties (); | |
1441 | |
1442 // --------------------------------------------------------------------- | |
1443 | |
7363 | 1444 void |
1445 figure::properties::set_currentaxes (const octave_value& v) | |
1446 { | |
7378 | 1447 graphics_handle val (v); |
6705 | 1448 |
6874 | 1449 if (error_state) |
1450 return; | |
1451 | |
7070 | 1452 if (xisnan (val.value ()) || is_handle (val)) |
6874 | 1453 currentaxes = val; |
1454 else | |
1455 gripe_set_invalid ("currentaxes"); | |
1456 } | |
1457 | |
1458 void | |
1459 figure::properties::set_visible (const octave_value& val) | |
1460 { | |
1461 std::string s = val.string_value (); | |
1462 | |
1463 if (! error_state) | |
1464 { | |
1465 if (s == "on") | |
1466 xset (0, "currentfigure", __myhandle__.value ()); | |
1467 | |
1468 visible = val; | |
1469 } | |
1470 } | |
1471 | |
1472 void | |
7408 | 1473 figure::properties::close (bool pop) |
6705 | 1474 { |
7408 | 1475 if (backend) |
1476 backend.close_figure (get___plot_stream__ ()); | |
1477 | |
1478 if (pop) | |
6406 | 1479 { |
7408 | 1480 gh_manager::pop_figure (__myhandle__); |
1481 | |
1482 graphics_handle cf = gh_manager::current_figure (); | |
1483 | |
1484 xset (0, "currentfigure", cf.value ()); | |
6406 | 1485 } |
6705 | 1486 } |
6432 | 1487 |
7445 | 1488 Matrix |
7447 | 1489 figure::properties::get_boundingbox (bool) const |
7445 | 1490 { |
1491 graphics_backend b = get_backend (); | |
1492 // FIXME: screen size should be obtained from root object | |
1493 Matrix screen_size = b.get_screen_size (); | |
1494 Matrix pos; | |
1495 | |
1496 pos = convert_position (get_position ().matrix_value (), get_units (), | |
1497 "pixels", screen_size, b); | |
1498 | |
1499 pos(0)--; | |
1500 pos(1)--; | |
7447 | 1501 pos(1) = screen_size(1) - pos(1) - pos(3); |
7445 | 1502 |
1503 return pos; | |
1504 } | |
1505 | |
6836 | 1506 octave_value |
7189 | 1507 figure::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 | |
6406 | 1522 // --------------------------------------------------------------------- |
1523 | |
7363 | 1524 void |
1525 axes::properties::set_title (const octave_value& v) | |
6962 | 1526 { |
7363 | 1527 graphics_handle val = ::reparent (v, "set", "title", __myhandle__, false); |
6962 | 1528 |
6874 | 1529 if (! error_state) |
1530 { | |
7363 | 1531 gh_manager::free (title.handle_value ()); |
6874 | 1532 title = val; |
1533 } | |
1534 } | |
1535 | |
1536 void | |
7363 | 1537 axes::properties::set_xlabel (const octave_value& v) |
6874 | 1538 { |
7363 | 1539 graphics_handle val = ::reparent (v, "set", "xlabel", __myhandle__, false); |
6874 | 1540 |
1541 if (! error_state) | |
1542 { | |
7363 | 1543 gh_manager::free (xlabel.handle_value ()); |
6874 | 1544 xlabel = val; |
1545 } | |
1546 } | |
1547 | |
1548 void | |
7363 | 1549 axes::properties::set_ylabel (const octave_value& v) |
6874 | 1550 { |
7363 | 1551 graphics_handle val = ::reparent (v, "set", "ylabel", __myhandle__, false); |
6874 | 1552 |
1553 if (! error_state) | |
1554 { | |
7363 | 1555 gh_manager::free (ylabel.handle_value ()); |
6874 | 1556 ylabel = val; |
1557 } | |
1558 } | |
1559 | |
1560 void | |
7363 | 1561 axes::properties::set_zlabel (const octave_value& v) |
6874 | 1562 { |
7363 | 1563 graphics_handle val = ::reparent (v, "set", "zlabel", __myhandle__, false); |
6874 | 1564 |
1565 if (! error_state) | |
1566 { | |
7363 | 1567 gh_manager::free (zlabel.handle_value ()); |
6874 | 1568 zlabel = val; |
1569 } | |
1570 } | |
1571 | |
1572 void | |
6844 | 1573 axes::properties::set_defaults (base_graphics_object& obj, |
6890 | 1574 const std::string& mode) |
6705 | 1575 { |
7427 | 1576 position = default_axes_position (); |
7363 | 1577 title = graphics_handle (); |
6705 | 1578 box = "on"; |
1579 key = "off"; | |
1580 keybox = "off"; | |
7363 | 1581 keypos = 1.0; |
6962 | 1582 colororder = default_colororder (); |
6705 | 1583 dataaspectratio = Matrix (1, 3, 1.0); |
1584 dataaspectratiomode = "auto"; | |
7363 | 1585 layer = "bottom"; |
6705 | 1586 |
1587 Matrix tlim (1, 2, 0.0); | |
1588 tlim(1) = 1; | |
1589 xlim = tlim; | |
1590 ylim = tlim; | |
1591 zlim = tlim; | |
6807 | 1592 |
1593 Matrix cl (1, 2, 0); | |
1594 cl(1) = 1; | |
1595 clim = cl; | |
1596 | |
7363 | 1597 xlimmode = "auto"; |
1598 ylimmode = "auto"; | |
1599 zlimmode = "auto"; | |
1600 climmode = "auto"; | |
1601 xlabel = graphics_handle (); | |
1602 ylabel = graphics_handle (); | |
1603 zlabel = graphics_handle (); | |
6705 | 1604 xgrid = "off"; |
1605 ygrid = "off"; | |
1606 zgrid = "off"; | |
1607 xminorgrid = "off"; | |
1608 yminorgrid = "off"; | |
1609 zminorgrid = "off"; | |
1610 xtick = Matrix (); | |
1611 ytick = Matrix (); | |
1612 ztick = Matrix (); | |
1613 xtickmode = "auto"; | |
1614 ytickmode = "auto"; | |
1615 ztickmode = "auto"; | |
1616 xticklabel = ""; | |
1617 yticklabel = ""; | |
1618 zticklabel = ""; | |
1619 xticklabelmode = "auto"; | |
1620 yticklabelmode = "auto"; | |
1621 zticklabelmode = "auto"; | |
7453 | 1622 color = color_values (1, 1, 1); |
7364 | 1623 xcolor = color_values ("black"); |
1624 ycolor = color_values ("black"); | |
1625 zcolor = color_values ("black"); | |
7363 | 1626 xscale = "linear"; |
1627 yscale = "linear"; | |
1628 zscale = "linear"; | |
6705 | 1629 xdir = "normal"; |
1630 ydir = "normal"; | |
1631 zdir = "normal"; | |
7363 | 1632 yaxislocation = "left"; |
1633 xaxislocation = "bottom"; | |
6705 | 1634 |
7427 | 1635 // Note: camera properties will be set through update_transform |
1636 camerapositionmode = "auto"; | |
1637 cameratargetmode = "auto"; | |
1638 cameraupvectormode = "auto"; | |
1639 cameraviewanglemode = "auto"; | |
1640 plotboxaspectratio = Matrix (1, 3, 1.0); | |
1641 drawmode = "normal"; | |
1642 fontangle = "normal"; | |
1643 fontname = "Helvetica"; | |
1644 fontsize = 12; | |
1645 fontunits = "points"; | |
1646 fontweight = "normal"; | |
7820
cb4838d70ab2
Fix default value for axes gridlinestyle and minorgridlinestyle.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7680
diff
changeset
|
1647 gridlinestyle = ":"; |
7427 | 1648 linestyleorder = "-"; |
1649 linewidth = 0.5; | |
7820
cb4838d70ab2
Fix default value for axes gridlinestyle and minorgridlinestyle.
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7680
diff
changeset
|
1650 minorgridlinestyle = ":"; |
7427 | 1651 // Note: plotboxaspectratio will be set through update_aspectratiors |
1652 plotboxaspectratiomode = "auto"; | |
1653 projection = "orthographic"; | |
1654 tickdir = "in"; | |
1655 tickdirmode = "auto"; | |
1656 ticklength = Matrix (1, 2, 0.1); | |
1657 tightinset = Matrix (1, 4, 0.0); | |
1658 | |
1659 sx = "linear"; | |
1660 sy = "linear"; | |
1661 sz = "linear"; | |
1662 | |
6705 | 1663 Matrix tview (1, 2, 0.0); |
1664 tview(1) = 90; | |
1665 view = tview; | |
1666 | |
6765 | 1667 visible = "on"; |
6705 | 1668 nextplot = "replace"; |
1669 | |
1670 // FIXME -- this is not quite right; we should preserve | |
1671 // "position" and "units". | |
1672 | |
1673 if (mode != "replace") | |
1674 { | |
6406 | 1675 Matrix touterposition (1, 4, 0.0); |
1676 touterposition(2) = 1; | |
1677 touterposition(3) = 1; | |
1678 outerposition = touterposition; | |
1679 } | |
1680 | |
7363 | 1681 activepositionproperty = "outerposition"; |
1682 __colorbar__ = "none"; | |
7189 | 1683 |
6705 | 1684 delete_children (); |
6406 | 1685 |
6705 | 1686 children = Matrix (); |
6432 | 1687 |
7427 | 1688 update_transform (); |
1689 | |
6705 | 1690 override_defaults (obj); |
1691 } | |
1692 | |
6874 | 1693 graphics_handle |
1694 axes::properties::get_title (void) const | |
1695 { | |
7363 | 1696 if (! title.handle_value ().ok ()) |
6874 | 1697 title = gh_manager::make_graphics_handle ("text", __myhandle__); |
1698 | |
7363 | 1699 return title.handle_value (); |
6874 | 1700 } |
1701 | |
1702 graphics_handle | |
1703 axes::properties::get_xlabel (void) const | |
1704 { | |
7363 | 1705 if (! xlabel.handle_value ().ok ()) |
6874 | 1706 xlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
1707 | |
7363 | 1708 return xlabel.handle_value (); |
6874 | 1709 } |
1710 | |
1711 graphics_handle | |
1712 axes::properties::get_ylabel (void) const | |
1713 { | |
7363 | 1714 if (! ylabel.handle_value ().ok ()) |
6874 | 1715 ylabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
1716 | |
7363 | 1717 return ylabel.handle_value (); |
6874 | 1718 } |
1719 | |
1720 graphics_handle | |
1721 axes::properties::get_zlabel (void) const | |
1722 { | |
7363 | 1723 if (! zlabel.handle_value ().ok ()) |
6874 | 1724 zlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
1725 | |
7363 | 1726 return zlabel.handle_value (); |
6705 | 1727 } |
6406 | 1728 |
6705 | 1729 void |
6844 | 1730 axes::properties::remove_child (const graphics_handle& h) |
6705 | 1731 { |
7363 | 1732 if (title.handle_value ().ok () && h == title.handle_value ()) |
6705 | 1733 title = gh_manager::make_graphics_handle ("text", __myhandle__); |
7363 | 1734 else if (xlabel.handle_value ().ok () && h == xlabel.handle_value ()) |
6705 | 1735 xlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
7363 | 1736 else if (ylabel.handle_value ().ok () && h == ylabel.handle_value ()) |
6705 | 1737 ylabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
7363 | 1738 else if (zlabel.handle_value ().ok () && h == zlabel.handle_value ()) |
6705 | 1739 zlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
1740 else | |
1741 base_properties::remove_child (h); | |
1742 } | |
6406 | 1743 |
6705 | 1744 void |
6844 | 1745 axes::properties::delete_children (void) |
6705 | 1746 { |
1747 base_properties::delete_children (); | |
1748 | |
7363 | 1749 gh_manager::free (title.handle_value ()); |
1750 gh_manager::free (xlabel.handle_value ()); | |
1751 gh_manager::free (ylabel.handle_value ()); | |
1752 gh_manager::free (zlabel.handle_value ()); | |
6705 | 1753 } |
6406 | 1754 |
7427 | 1755 inline Matrix |
1756 xform_matrix (void) | |
1757 { | |
1758 Matrix m (4, 4, 0.0); | |
1759 for (int i = 0; i < 4; i++) | |
1760 m(i,i) = 1; | |
1761 return m; | |
1762 } | |
1763 | |
1764 inline ColumnVector | |
1765 xform_vector (void) | |
1766 { | |
1767 ColumnVector v (4, 0.0); | |
1768 v(3) = 1; | |
1769 return v; | |
1770 } | |
1771 | |
1772 inline ColumnVector | |
1773 xform_vector (double x, double y, double z) | |
1774 { | |
1775 ColumnVector v (4, 1.0); | |
1776 v(0) = x; v(1) = y; v(2) = z; | |
1777 return v; | |
1778 } | |
1779 | |
1780 inline ColumnVector | |
1781 transform (const Matrix& m, double x, double y, double z) | |
1782 { | |
1783 return (m * xform_vector (x, y, z)); | |
1784 } | |
1785 | |
1786 inline Matrix | |
1787 xform_scale (double x, double y, double z) | |
1788 { | |
1789 Matrix m (4, 4, 0.0); | |
1790 m(0,0) = x; m(1,1) = y; m(2,2) = z; m(3,3) = 1; | |
1791 return m; | |
1792 } | |
1793 | |
1794 inline Matrix | |
1795 xform_translate (double x, double y, double z) | |
1796 { | |
1797 Matrix m = xform_matrix (); | |
1798 m(0,3) = x; m(1,3) = y; m(2,3) = z; m(3,3) = 1; | |
1799 return m; | |
1800 } | |
1801 | |
1802 inline void | |
1803 scale (Matrix& m, double x, double y, double z) | |
1804 { | |
1805 m = m * xform_scale (x, y, z); | |
1806 } | |
1807 | |
1808 inline void | |
1809 translate (Matrix& m, double x, double y, double z) | |
1810 { | |
1811 m = m * xform_translate (x, y, z); | |
1812 } | |
1813 | |
1814 inline void | |
1815 xform (ColumnVector& v, const Matrix& m) | |
1816 { | |
1817 v = m*v; | |
1818 } | |
1819 | |
1820 inline void | |
1821 scale (ColumnVector& v, double x, double y, double z) | |
1822 { | |
1823 v(0) *= x; | |
1824 v(1) *= y; | |
1825 v(2) *= z; | |
1826 } | |
1827 | |
1828 inline void | |
1829 translate (ColumnVector& v, double x, double y, double z) | |
1830 { | |
1831 v(0) += x; | |
1832 v(1) += y; | |
1833 v(2) += z; | |
1834 } | |
1835 | |
1836 inline void | |
1837 normalize (ColumnVector& v) | |
1838 { | |
1839 double fact = 1.0/sqrt(v(0)*v(0)+v(1)*v(1)+v(2)*v(2)); | |
1840 scale (v, fact, fact, fact); | |
1841 } | |
1842 | |
1843 inline double | |
1844 dot (const ColumnVector& v1, const ColumnVector& v2) | |
1845 { | |
1846 return (v1(0)*v2(0)+v1(1)*v2(1)+v1(2)*v2(2)); | |
1847 } | |
1848 | |
1849 inline double | |
1850 norm (const ColumnVector& v) | |
1851 { | |
1852 return sqrt (dot (v, v)); | |
1853 } | |
1854 | |
1855 inline ColumnVector | |
1856 cross (const ColumnVector& v1, const ColumnVector& v2) | |
1857 { | |
1858 ColumnVector r = xform_vector (); | |
1859 r(0) = v1(1)*v2(2)-v1(2)*v2(1); | |
1860 r(1) = v1(2)*v2(0)-v1(0)*v2(2); | |
1861 r(2) = v1(0)*v2(1)-v1(1)*v2(0); | |
1862 return r; | |
1863 } | |
1864 | |
1865 inline Matrix | |
1866 unit_cube (void) | |
1867 { | |
1868 static double data[32] = { | |
1869 0,0,0,1, | |
1870 1,0,0,1, | |
1871 0,1,0,1, | |
1872 0,0,1,1, | |
1873 1,1,0,1, | |
1874 1,0,1,1, | |
1875 0,1,1,1, | |
1876 1,1,1,1}; | |
1877 Matrix m (4, 8); | |
1878 memcpy (m.fortran_vec (), data, sizeof(double)*32); | |
1879 return m; | |
1880 } | |
1881 | |
1882 inline ColumnVector | |
1883 cam2xform (const Array<double>& m) | |
1884 { | |
1885 ColumnVector retval (4, 1.0); | |
1886 memcpy (retval.fortran_vec (), m.fortran_vec (), sizeof(double)*3); | |
1887 return retval; | |
1888 } | |
1889 | |
1890 inline RowVector | |
1891 xform2cam (const ColumnVector& v) | |
1892 { | |
1893 return v.extract_n (0, 3).transpose (); | |
1894 } | |
1895 | |
1896 void | |
1897 axes::properties::update_camera (void) | |
1898 { | |
1899 double xd = (xdir_is ("normal") ? 1 : -1); | |
1900 double yd = (ydir_is ("normal") ? 1 : -1); | |
1901 double zd = (zdir_is ("normal") ? 1 : -1); | |
1902 | |
7526
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1903 Matrix xlimits = sx.scale (get_xlim ().matrix_value ()); |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1904 Matrix ylimits = sy.scale (get_ylim ().matrix_value ()); |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1905 Matrix zlimits = sz.scale (get_zlim ().matrix_value ()); |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1906 |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1907 double xo = xlimits(xd > 0 ? 0 : 1); |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1908 double yo = ylimits(yd > 0 ? 0 : 1); |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1909 double zo = zlimits(zd > 0 ? 0 : 1); |
7427 | 1910 |
1911 Matrix pb = get_plotboxaspectratio ().matrix_value (); | |
1912 | |
1913 bool autocam = (camerapositionmode_is ("auto") | |
1914 && cameratargetmode_is ("auto") | |
1915 && cameraupvectormode_is ("auto") | |
1916 && cameraviewanglemode_is ("auto")); | |
1917 bool dowarp = (autocam && dataaspectratiomode_is("auto") | |
1918 && plotboxaspectratiomode_is ("auto")); | |
1919 | |
1920 ColumnVector c_eye (xform_vector ()); | |
1921 ColumnVector c_center (xform_vector ()); | |
1922 ColumnVector c_upv (xform_vector ()); | |
1923 | |
1924 if (cameratargetmode_is ("auto")) | |
1925 { | |
7526
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1926 c_center(0) = (xlimits(0)+xlimits(1))/2; |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1927 c_center(1) = (ylimits(0)+ylimits(1))/2; |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1928 c_center(2) = (zlimits(0)+zlimits(1))/2; |
7427 | 1929 |
1930 cameratarget = xform2cam (c_center); | |
1931 } | |
1932 else | |
1933 c_center = cam2xform (get_cameratarget ().matrix_value ()); | |
1934 | |
1935 if (camerapositionmode_is ("auto")) | |
1936 { | |
7526
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1937 Matrix tview = get_view ().matrix_value (); |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1938 double az = tview(0), el = tview(1); |
7427 | 1939 double d = 5*sqrt(pb(0)*pb(0)+pb(1)*pb(1)+pb(2)*pb(2)); |
1940 | |
1941 if (el == 90 || el == -90) | |
1942 c_eye(2) = d*signum(el); | |
1943 else | |
1944 { | |
1945 az *= M_PI/180.0; | |
1946 el *= M_PI/180.0; | |
1947 c_eye(0) = d*cos(el)*sin(az); | |
1948 c_eye(1) = -d*cos(el)*cos(az); | |
1949 c_eye(2) = d*sin(el); | |
1950 } | |
7526
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1951 c_eye(0) = c_eye(0)*(xlimits(1)-xlimits(0))/(xd*pb(0))+c_center(0); |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1952 c_eye(1) = c_eye(1)*(ylimits(1)-ylimits(0))/(yd*pb(1))+c_center(1); |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1953 c_eye(2) = c_eye(2)*(zlimits(1)-zlimits(0))/(zd*pb(2))+c_center(2); |
7427 | 1954 |
1955 cameraposition = xform2cam (c_eye); | |
1956 } | |
1957 else | |
1958 c_eye = cam2xform (get_cameraposition ().matrix_value ()); | |
1959 | |
1960 if (cameraupvectormode_is ("auto")) | |
1961 { | |
7526
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1962 Matrix tview = get_view ().matrix_value (); |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1963 double az = tview(0), el = tview(1); |
7427 | 1964 |
1965 if (el == 90 || el == -90) | |
1966 { | |
7526
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1967 c_upv(0) = -sin(az*M_PI/180.0)*(xlimits(1)-xlimits(0))/pb(0); |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1968 c_upv(1) = cos(az*M_PI/180.0)*(ylimits(1)-ylimits(0))/pb(1); |
7427 | 1969 } |
1970 else | |
1971 c_upv(2) = 1; | |
1972 | |
1973 cameraupvector = xform2cam (c_upv); | |
1974 } | |
1975 else | |
1976 c_upv = cam2xform (get_cameraupvector ().matrix_value ()); | |
1977 | |
1978 Matrix x_view = xform_matrix (); | |
1979 Matrix x_projection = xform_matrix (); | |
1980 Matrix x_viewport = xform_matrix (); | |
1981 Matrix x_normrender = xform_matrix (); | |
1982 Matrix x_pre = xform_matrix (); | |
1983 | |
1984 x_render = xform_matrix (); | |
1985 x_render_inv = xform_matrix (); | |
1986 | |
1987 scale (x_pre, pb(0), pb(1), pb(2)); | |
1988 translate (x_pre, -0.5, -0.5, -0.5); | |
7526
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1989 scale (x_pre, xd/(xlimits(1)-xlimits(0)), yd/(ylimits(1)-ylimits(0)), |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1990 zd/(zlimits(1)-zlimits(0))); |
7427 | 1991 translate (x_pre, -xo, -yo, -zo); |
1992 | |
1993 xform (c_eye, x_pre); | |
1994 xform (c_center, x_pre); | |
7526
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1995 scale (c_upv, pb(0)/(xlimits(1)-xlimits(0)), pb(1)/(ylimits(1)-ylimits(0)), |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
1996 pb(2)/(zlimits(1)-zlimits(0))); |
7427 | 1997 translate (c_center, -c_eye(0), -c_eye(1), -c_eye(2)); |
1998 | |
1999 ColumnVector F (c_center), f (F), UP (c_upv); | |
2000 normalize (f); | |
2001 normalize (UP); | |
2002 | |
7440 | 2003 if (std::abs (dot (f, UP)) > 1e-15) |
7427 | 2004 { |
2005 double fa = 1/sqrt(1-f(2)*f(2)); | |
2006 scale (UP, fa, fa, fa); | |
2007 } | |
2008 | |
2009 ColumnVector s = cross (f, UP); | |
2010 ColumnVector u = cross (s, f); | |
2011 | |
2012 scale (x_view, 1, 1, -1); | |
2013 Matrix l = xform_matrix (); | |
2014 l(0,0) = s(0); l(0,1) = s(1); l(0,2) = s(2); | |
2015 l(1,0) = u(0); l(1,1) = u(1); l(1,2) = u(2); | |
2016 l(2,0) = -f(0); l(2,1) = -f(1); l(2,2) = -f(2); | |
2017 x_view = x_view * l; | |
2018 translate (x_view, -c_eye(0), -c_eye(1), -c_eye(2)); | |
2019 scale (x_view, pb(0), pb(1), pb(2)); | |
2020 translate (x_view, -0.5, -0.5, -0.5); | |
2021 | |
2022 Matrix x_cube = x_view * unit_cube (); | |
2023 ColumnVector cmin = x_cube.row_min (), cmax = x_cube.row_max (); | |
2024 double xM = cmax(0)-cmin(0); | |
2025 double yM = cmax(1)-cmin(1); | |
2026 | |
7447 | 2027 Matrix bb = get_boundingbox (true); |
7427 | 2028 |
2029 double v_angle; | |
2030 | |
2031 if (cameraviewanglemode_is ("auto")) | |
2032 { | |
2033 double af; | |
2034 | |
2035 // FIXME: Was this really needed? When compared to Matlab, it | |
2036 // does not seem to be required. Need investigation with concrete | |
2037 // backend to see results visually. | |
2038 if (false && dowarp) | |
2039 af = 1.0 / (xM > yM ? xM : yM); | |
2040 else | |
2041 { | |
2042 if ((bb(2)/bb(3)) > (xM/yM)) | |
2043 af = 1.0 / yM; | |
2044 else | |
2045 af = 1.0 / xM; | |
2046 } | |
2047 v_angle = 2 * (180.0 / M_PI) * atan (1 / (2 * af * norm (F))); | |
2048 | |
2049 cameraviewangle = v_angle; | |
2050 } | |
2051 else | |
2052 v_angle = get_cameraviewangle (); | |
2053 | |
2054 double pf = 1 / (2 * tan ((v_angle / 2) * M_PI / 180.0) * norm (F)); | |
2055 scale (x_projection, pf, pf, 1); | |
2056 | |
2057 if (dowarp) | |
2058 { | |
2059 xM *= pf; | |
2060 yM *= pf; | |
7447 | 2061 translate (x_viewport, bb(0)+bb(2)/2, bb(1)+bb(3)/2, 0); |
7427 | 2062 scale (x_viewport, bb(2)/xM, -bb(3)/yM, 1); |
2063 } | |
2064 else | |
2065 { | |
2066 double pix = 1; | |
2067 if (autocam) | |
2068 { | |
2069 if ((bb(2)/bb(3)) > (xM/yM)) | |
2070 pix = bb(3); | |
2071 else | |
2072 pix = bb(2); | |
2073 } | |
2074 else | |
2075 pix = (bb(2) < bb(3) ? bb(2) : bb(3)); | |
7447 | 2076 translate (x_viewport, bb(0)+bb(2)/2, bb(1)+bb(3)/2, 0); |
7427 | 2077 scale (x_viewport, pix, -pix, 1); |
2078 } | |
2079 | |
2080 x_normrender = x_viewport * x_projection * x_view; | |
2081 | |
2082 x_cube = x_normrender * unit_cube (); | |
2083 cmin = x_cube.row_min (); | |
2084 cmax = x_cube.row_max (); | |
2085 x_zlim.resize (1, 2); | |
2086 x_zlim(0) = cmin(2); | |
2087 x_zlim(1) = cmax(2); | |
2088 | |
2089 x_render = x_normrender; | |
7526
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
2090 scale (x_render, xd/(xlimits(1)-xlimits(0)), yd/(ylimits(1)-ylimits(0)), |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
2091 zd/(zlimits(1)-zlimits(0))); |
7427 | 2092 translate (x_render, -xo, -yo, -zo); |
2093 | |
2094 x_viewtransform = x_view; | |
2095 x_projectiontransform = x_projection; | |
2096 x_viewporttransform = x_viewport; | |
2097 x_normrendertransform = x_normrender; | |
2098 x_rendertransform = x_render; | |
2099 | |
2100 x_render_inv = x_render.inverse (); | |
2101 | |
2102 // Note: these matrices are a slight modified version of the regular | |
2103 // matrices, more suited for OpenGL rendering (x_gl_mat1 => light | |
2104 // => x_gl_mat2) | |
2105 x_gl_mat1 = x_view; | |
7526
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
2106 scale (x_gl_mat1, xd/(xlimits(1)-xlimits(0)), yd/(ylimits(1)-ylimits(0)), |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
2107 zd/(zlimits(1)-zlimits(0))); |
7427 | 2108 translate (x_gl_mat1, -xo, -yo, -zo); |
2109 x_gl_mat2 = x_viewport * x_projection; | |
2110 } | |
2111 | |
2112 void | |
2113 axes::properties::update_aspectratios (void) | |
2114 { | |
7526
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
2115 Matrix xlimits = get_xlim ().matrix_value (); |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
2116 Matrix ylimits = get_ylim ().matrix_value (); |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
2117 Matrix zlimits = get_zlim ().matrix_value (); |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
2118 |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
2119 double dx = (xlimits(1)-xlimits(0)); |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
2120 double dy = (ylimits(1)-ylimits(0)); |
52d58b0463ed
graphics.cc, graphics.h.in: avoid some GCC warnings
John W. Eaton <jwe@octave.org>
parents:
7523
diff
changeset
|
2121 double dz = (zlimits(1)-zlimits(0)); |
7427 | 2122 |
2123 if (dataaspectratiomode_is ("auto")) | |
2124 { | |
2125 double dmin = xmin (xmin (dx, dy), dz); | |
2126 Matrix da (1, 3, 0.0); | |
2127 | |
2128 da(0) = dx/dmin; | |
2129 da(1) = dy/dmin; | |
2130 da(2) = dz/dmin; | |
2131 | |
2132 dataaspectratio = da; | |
2133 } | |
2134 | |
2135 if (plotboxaspectratiomode_is ("auto")) | |
2136 { | |
2137 if (dataaspectratiomode_is ("auto")) | |
2138 plotboxaspectratio = Matrix (1, 3, 1.0); | |
2139 else | |
2140 { | |
2141 Matrix da = get_dataaspectratio ().matrix_value (); | |
2142 Matrix pba (1, 3, 0.0); | |
2143 | |
2144 pba(0) = dx/da(0); | |
2145 pba(1) = dy/da(1); | |
2146 pba(2) = dz/da(2); | |
2147 } | |
2148 } | |
2149 | |
2150 // FIXME: if plotboxaspectratiomode is "manual", limits | |
2151 // and/or dataaspectratio might be adapted | |
2152 } | |
2153 | |
7447 | 2154 // The INTERNAL flag defines whether position or outerposition is used. |
2155 | |
7427 | 2156 Matrix |
7447 | 2157 axes::properties::get_boundingbox (bool internal) const |
7427 | 2158 { |
7447 | 2159 graphics_object obj = gh_manager::get_object (get_parent ()); |
2160 Matrix parent_bb = obj.get_properties ().get_boundingbox (true); | |
2161 Matrix pos = (internal ? | |
2162 get_position ().matrix_value () | |
2163 : get_outerposition ().matrix_value ()); | |
2164 | |
2165 | |
2166 pos = convert_position (pos, get_units (), "pixels", | |
2167 parent_bb.extract_n (0, 2, 1, 2), get_backend ()); | |
7427 | 2168 pos(0)--; |
2169 pos(1)--; | |
7447 | 2170 pos(1) = parent_bb(3) - pos(1) - pos(3); |
7427 | 2171 |
2172 return pos; | |
2173 } | |
2174 | |
7435 | 2175 ColumnVector |
2176 graphics_xform::xform_vector (double x, double y, double z) | |
2177 { return ::xform_vector (x, y, z); } | |
2178 | |
2179 Matrix | |
2180 graphics_xform::xform_eye (void) | |
2181 { return ::xform_matrix (); } | |
2182 | |
2183 ColumnVector | |
2184 graphics_xform::transform (double x, double y, double z, | |
2185 bool use_scale) const | |
2186 { | |
2187 if (use_scale) | |
2188 { | |
2189 x = sx.scale (x); | |
2190 y = sy.scale (y); | |
2191 z = sz.scale (z); | |
2192 } | |
2193 | |
2194 return ::transform (xform, x, y, z); | |
2195 } | |
2196 | |
2197 ColumnVector | |
2198 graphics_xform::untransform (double x, double y, double z, | |
2199 bool use_scale) const | |
2200 { | |
2201 ColumnVector v = ::transform (xform_inv, x, y, z); | |
2202 | |
2203 if (use_scale) | |
2204 { | |
2205 v(0) = sx.unscale (v(0)); | |
2206 v(1) = sy.unscale (v(1)); | |
2207 v(2) = sz.unscale (v(2)); | |
2208 } | |
2209 | |
2210 return v; | |
2211 } | |
2212 | |
6836 | 2213 octave_value |
7189 | 2214 axes::get_default (const caseless_str& name) const |
6836 | 2215 { |
2216 octave_value retval = default_properties.lookup (name); | |
2217 | |
2218 if (retval.is_undefined ()) | |
2219 { | |
2220 graphics_handle parent = get_parent (); | |
2221 graphics_object parent_obj = gh_manager::get_object (parent); | |
2222 | |
2223 retval = parent_obj.get_default (name); | |
2224 } | |
2225 | |
2226 return retval; | |
2227 } | |
2228 | |
7222 | 2229 static void |
2230 check_limit_vals (double& min_val, double& max_val, double& min_pos, | |
2231 const data_property& data) | |
2232 { | |
7267 | 2233 double val = data.min_val (); |
2234 if (! (xisinf (val) || xisnan (val)) && val < min_val) | |
2235 min_val = val; | |
2236 val = data.max_val (); | |
2237 if (! (xisinf (val) || xisnan (val)) && val > max_val) | |
2238 max_val = val; | |
2239 val = data.min_pos (); | |
2240 if (! (xisinf (val) || xisnan (val)) && val > 0 && val < min_pos) | |
2241 min_pos = val; | |
7222 | 2242 } |
2243 | |
2244 // Attempt to make "nice" limits from the actual max and min of the | |
2245 // data. For log plots, we will also use the smallest strictly positive | |
2246 // value. | |
2247 | |
2248 static Matrix | |
2249 get_axis_limits (double xmin, double xmax, double min_pos, bool logscale) | |
2250 { | |
2251 Matrix retval; | |
2252 | |
2253 double min_val = xmin; | |
2254 double max_val = xmax; | |
2255 | |
2256 if (! (xisinf (min_val) || xisinf (max_val))) | |
2257 { | |
2258 if (logscale) | |
2259 { | |
2260 if (xisinf (min_pos)) | |
2261 { | |
2262 // warning ("axis: logscale with no positive values to plot"); | |
2263 return retval; | |
2264 } | |
2265 | |
2266 if (min_val <= 0) | |
2267 { | |
2268 warning ("axis: omitting nonpositive data in log plot"); | |
2269 min_val = min_pos; | |
2270 } | |
2271 // FIXME -- maybe this test should also be relative? | |
2272 if (std::abs (min_val - max_val) < sqrt (DBL_EPSILON)) | |
2273 { | |
2274 min_val *= 0.9; | |
2275 max_val *= 1.1; | |
2276 } | |
2277 min_val = pow (10, floor (log10 (min_val))); | |
2278 max_val = pow (10, ceil (log10 (max_val))); | |
2279 } | |
2280 else | |
2281 { | |
2282 if (min_val == 0 && max_val == 0) | |
2283 { | |
2284 min_val = -1; | |
2285 max_val = 1; | |
2286 } | |
2287 // FIXME -- maybe this test should also be relative? | |
2288 else if (std::abs (min_val - max_val) < sqrt (DBL_EPSILON)) | |
2289 { | |
2290 min_val -= 0.1 * std::abs (min_val); | |
2291 max_val += 0.1 * std::abs (max_val); | |
2292 } | |
2293 // FIXME -- to do a better job, we should consider the tic spacing. | |
2294 double scale = pow (10, floor (log10 (max_val - min_val) - 1)); | |
2295 min_val = scale * floor (min_val / scale); | |
2296 max_val = scale * ceil (max_val / scale); | |
2297 } | |
2298 } | |
2299 | |
2300 retval.resize (1, 2); | |
2301 | |
2302 retval(0) = min_val; | |
2303 retval(1) = max_val; | |
2304 | |
2305 return retval; | |
2306 } | |
2307 | |
7446 | 2308 // magform(x) Returns (a, b), where x = a * 10^b, a >= 1., and b is |
7447 | 2309 // integral. |
7446 | 2310 |
2311 void | |
7447 | 2312 axes::properties::magform (double x, double& a, int& b) |
7446 | 2313 { |
2314 if (x == 0) | |
2315 { | |
2316 a = 0; | |
2317 b = 0; | |
2318 } | |
2319 else | |
2320 { | |
2321 double l = std::log10 (std::abs (x)); | |
2322 double r = std::fmod (l, 1.); | |
2323 a = std::pow (10.0, r); | |
2324 b = static_cast<int> (l-r); | |
2325 if (a < 1) | |
2326 { | |
2327 a *= 10; | |
2328 b -= 1; | |
2329 } | |
2330 | |
2331 if (x < 0) | |
2332 a = -a; | |
2333 } | |
2334 } | |
2335 | |
2336 // A translation from Tom Holoryd's python code at | |
2337 // http://kurage.nimh.nih.gov/tomh/tics.py | |
2338 // FIXME -- add log ticks | |
7447 | 2339 |
7446 | 2340 void |
2341 axes::properties::calc_ticks (const array_property& lims, array_property& ticks) | |
2342 { | |
2343 int ticint = 5; | |
2344 | |
2345 if (lims.get ().is_empty ()) | |
2346 return; | |
2347 | |
2348 double lo = (lims.get ().matrix_value ()) (0); | |
2349 double hi = (lims.get ().matrix_value ()) (1); | |
2350 | |
2351 // Reference: Lewart, C. R., "Algorithms SCALE1, SCALE2, and | |
2352 // SCALE3 for Determination of Scales on Computer Generated | |
2353 // Plots", Communications of the ACM, 10 (1973), 639-640. | |
2354 // Also cited as ACM Algorithm 463. | |
2355 | |
2356 double a; | |
2357 int b, x; | |
7447 | 2358 |
2359 magform ((hi-lo)/ticint, a, b); | |
2360 | |
2361 static const double sqrt_2 = sqrt (2.0); | |
2362 static const double sqrt_10 = sqrt (10.0); | |
2363 static const double sqrt_50 = sqrt (50.0); | |
2364 | |
2365 if (a < sqrt_2) | |
7446 | 2366 x = 1; |
7447 | 2367 else if (a < sqrt_10) |
7446 | 2368 x = 2; |
7447 | 2369 else if (a < sqrt_50) |
7446 | 2370 x = 5; |
2371 else | |
2372 x = 10; | |
2373 | |
2374 double sep = x * std::pow (10., b); | |
2375 | |
2376 // FIXME x can now be used to set minor ticks | |
2377 if (x == 10) | |
2378 x = 1; | |
2379 | |
2380 // The following guarantees that if zero is in the range, it will be | |
2381 // included as a tic. | |
2382 | |
2383 int i1 = static_cast<int> (std::floor (lo / sep)); | |
2384 int i2 = static_cast<int> (std::ceil (hi / sep)); | |
2385 | |
2386 Matrix limits (1, i2-i1+1); | |
7447 | 2387 for (int i = 0; i < i2-i1+1; i++) |
7446 | 2388 limits (i) = sep*(i+i1); |
2389 | |
2390 ticks = limits; | |
2391 } | |
2392 | |
7222 | 2393 static bool updating_axis_limits = false; |
2394 | |
7214 | 2395 void |
7222 | 2396 axes::update_axis_limits (const std::string& axis_type) |
7214 | 2397 { |
7222 | 2398 if (updating_axis_limits) |
2399 return; | |
2400 | |
2401 Matrix kids = xproperties.get_children (); | |
2402 | |
2403 octave_idx_type n = kids.numel (); | |
2404 | |
2405 double min_val = octave_Inf; | |
2406 double max_val = -octave_Inf; | |
2407 double min_pos = octave_Inf; | |
2408 | |
2409 char update_type = 0; | |
2410 | |
2411 Matrix limits; | |
2412 | |
2413 if (axis_type == "xdata" || axis_type == "xscale" | |
2414 || axis_type == "xldata" || axis_type == "xudata" | |
2415 || axis_type == "xlimmode") | |
2416 { | |
7363 | 2417 if (xproperties.xlimmode_is ("auto")) |
7222 | 2418 { |
2419 for (octave_idx_type i = 0; i < n; i++) | |
2420 { | |
2421 graphics_object obj = gh_manager::get_object (kids(i)); | |
2422 | |
2423 if (obj.isa ("line") || obj.isa ("image") | |
2424 || obj.isa ("patch") || obj.isa ("surface")) | |
2425 { | |
7363 | 2426 data_property xdata = obj.get_xdata_property (); |
7222 | 2427 |
2428 check_limit_vals (min_val, max_val, min_pos, xdata); | |
2429 | |
2430 if (obj.isa ("line")) | |
2431 { | |
7363 | 2432 data_property xldata = obj.get_xldata_property (); |
2433 data_property xudata = obj.get_xudata_property (); | |
7222 | 2434 |
2435 check_limit_vals (min_val, max_val, min_pos, xldata); | |
2436 check_limit_vals (min_val, max_val, min_pos, xudata); | |
2437 } | |
2438 } | |
2439 } | |
2440 | |
2441 limits = get_axis_limits (min_val, max_val, min_pos, | |
7363 | 2442 xproperties.xscale_is ("log")); |
7222 | 2443 |
2444 update_type = 'x'; | |
2445 } | |
2446 } | |
2447 else if (axis_type == "ydata" || axis_type == "yscale" | |
2448 || axis_type == "ldata" || axis_type == "udata" | |
2449 || axis_type == "ylimmode") | |
2450 { | |
7363 | 2451 if (xproperties.ylimmode_is ("auto")) |
7222 | 2452 { |
2453 for (octave_idx_type i = 0; i < n; i++) | |
2454 { | |
2455 graphics_object obj = gh_manager::get_object (kids(i)); | |
2456 | |
2457 if (obj.isa ("line") || obj.isa ("image") | |
2458 || obj.isa ("patch") || obj.isa ("surface")) | |
2459 { | |
7363 | 2460 data_property ydata = obj.get_ydata_property (); |
7222 | 2461 |
2462 check_limit_vals (min_val, max_val, min_pos, ydata); | |
2463 | |
2464 if (obj.isa ("line")) | |
2465 { | |
7363 | 2466 data_property ldata = obj.get_ldata_property (); |
2467 data_property udata = obj.get_udata_property (); | |
7222 | 2468 |
2469 check_limit_vals (min_val, max_val, min_pos, ldata); | |
2470 check_limit_vals (min_val, max_val, min_pos, udata); | |
2471 } | |
2472 } | |
2473 } | |
2474 | |
2475 limits = get_axis_limits (min_val, max_val, min_pos, | |
7363 | 2476 xproperties.yscale_is ("log")); |
7222 | 2477 |
2478 update_type = 'y'; | |
2479 } | |
2480 } | |
2481 else if (axis_type == "zdata" || axis_type == "zscale" | |
2482 || axis_type == "zlimmode") | |
2483 { | |
7363 | 2484 if (xproperties.zlimmode_is ("auto")) |
7222 | 2485 { |
2486 for (octave_idx_type i = 0; i < n; i++) | |
2487 { | |
2488 graphics_object obj = gh_manager::get_object (kids(i)); | |
2489 | |
2490 if (obj.isa ("line") || obj.isa ("patch") || obj.isa ("surface")) | |
2491 { | |
7363 | 2492 data_property zdata = obj.get_zdata_property (); |
7222 | 2493 |
2494 check_limit_vals (min_val, max_val, min_pos, zdata); | |
2495 } | |
2496 } | |
2497 | |
2498 limits = get_axis_limits (min_val, max_val, min_pos, | |
7363 | 2499 xproperties.zscale_is ("log")); |
7222 | 2500 |
2501 update_type = 'z'; | |
2502 } | |
2503 } | |
2504 else if (axis_type == "cdata" || axis_type == "climmode") | |
2505 { | |
7363 | 2506 if (xproperties.climmode_is ("auto")) |
7222 | 2507 { |
2508 for (octave_idx_type i = 0; i < n; i++) | |
2509 { | |
2510 graphics_object obj = gh_manager::get_object (kids(i)); | |
2511 | |
2512 if (obj.isa ("image") || obj.isa ("patch") || obj.isa ("surface")) | |
2513 { | |
7363 | 2514 data_property cdata = obj.get_cdata_property (); |
7222 | 2515 |
2516 check_limit_vals (min_val, max_val, min_pos, cdata); | |
2517 } | |
2518 } | |
2519 | |
2520 if (min_val == max_val) | |
2521 max_val = min_val + 1; | |
2522 | |
2523 limits.resize (1, 2); | |
2524 | |
2525 limits(0) = min_val; | |
2526 limits(1) = max_val; | |
2527 | |
2528 update_type = 'c'; | |
2529 } | |
2530 | |
2531 } | |
2532 | |
2533 unwind_protect_bool (updating_axis_limits); | |
2534 updating_axis_limits = true; | |
2535 | |
2536 switch (update_type) | |
2537 { | |
2538 case 'x': | |
2539 xproperties.set_xlim (limits); | |
2540 xproperties.set_xlimmode ("auto"); | |
7446 | 2541 xproperties.update_xlim (); |
7222 | 2542 break; |
2543 | |
2544 case 'y': | |
2545 xproperties.set_ylim (limits); | |
2546 xproperties.set_ylimmode ("auto"); | |
7446 | 2547 xproperties.update_ylim (); |
7222 | 2548 break; |
2549 | |
2550 case 'z': | |
2551 xproperties.set_zlim (limits); | |
2552 xproperties.set_zlimmode ("auto"); | |
7446 | 2553 xproperties.update_zlim (); |
7222 | 2554 break; |
2555 | |
2556 case 'c': | |
2557 xproperties.set_clim (limits); | |
2558 xproperties.set_climmode ("auto"); | |
2559 break; | |
2560 | |
2561 default: | |
2562 break; | |
2563 } | |
2564 | |
7427 | 2565 xproperties.update_transform (); |
2566 | |
7222 | 2567 unwind_protect::run (); |
7214 | 2568 } |
2569 | |
7363 | 2570 // --------------------------------------------------------------------- |
2571 | |
2572 // Note: "line" code is entirely auto-generated | |
6406 | 2573 |
2574 // --------------------------------------------------------------------- | |
2575 | |
7363 | 2576 // Note: "text" code is entirely auto-generated |
6406 | 2577 |
2578 // --------------------------------------------------------------------- | |
2579 | |
7363 | 2580 // Note: "image" code is entirely auto-generated |
6406 | 2581 |
2582 // --------------------------------------------------------------------- | |
2583 | |
7363 | 2584 // Note: "patch" code is entirely auto-generated |
6807 | 2585 |
2586 // --------------------------------------------------------------------- | |
2587 | |
7363 | 2588 // Note: "surface" code is entirely auto-generated |
6406 | 2589 |
2590 // --------------------------------------------------------------------- | |
2591 | |
2592 octave_value | |
7189 | 2593 base_graphics_object::get_default (const caseless_str& name) const |
6406 | 2594 { |
2595 graphics_handle parent = get_parent (); | |
2596 graphics_object parent_obj = gh_manager::get_object (parent); | |
2597 | |
2598 return parent_obj.get_default (type () + name); | |
2599 } | |
2600 | |
2601 octave_value | |
7189 | 2602 base_graphics_object::get_factory_default (const caseless_str& name) const |
6406 | 2603 { |
2604 graphics_object parent_obj = gh_manager::get_object (0); | |
2605 | |
2606 return parent_obj.get_factory_default (type () + name); | |
2607 } | |
2608 | |
7286 | 2609 // We use a random value for the handle to avoid issues with plots and |
2610 // scalar values for the first argument. | |
6406 | 2611 gh_manager::gh_manager (void) |
7286 | 2612 : handle_map (), handle_free_list (), |
2613 next_handle (-1.0 - (rand () + 1.0) / (RAND_MAX + 2.0)) | |
6406 | 2614 { |
2615 handle_map[0] = graphics_object (new root_figure ()); | |
2616 } | |
2617 | |
2618 graphics_handle | |
2619 gh_manager::do_make_graphics_handle (const std::string& go_name, | |
7370 | 2620 const graphics_handle& p, bool do_createfcn) |
6406 | 2621 { |
2622 graphics_handle h = get_handle (go_name); | |
2623 | |
2624 base_graphics_object *go = 0; | |
2625 | |
2626 if (go_name == "figure") | |
2627 go = new figure (h, p); | |
2628 else if (go_name == "axes") | |
2629 go = new axes (h, p); | |
2630 else if (go_name == "line") | |
2631 go = new line (h, p); | |
2632 else if (go_name == "text") | |
2633 go = new text (h, p); | |
2634 else if (go_name == "image") | |
2635 go = new image (h, p); | |
6807 | 2636 else if (go_name == "patch") |
2637 go = new patch (h, p); | |
6406 | 2638 else if (go_name == "surface") |
2639 go = new surface (h, p); | |
2640 if (go) | |
7370 | 2641 { |
2642 handle_map[h] = graphics_object (go); | |
2643 if (do_createfcn) | |
2644 go->get_properties ().execute_createfcn (); | |
2645 } | |
6406 | 2646 else |
2647 error ("gh_manager::do_make_graphics_handle: invalid object type `%s'", | |
2648 go_name.c_str ()); | |
2649 | |
2650 return h; | |
2651 } | |
2652 | |
2653 graphics_handle | |
2654 gh_manager::do_make_figure_handle (double val) | |
2655 { | |
2656 graphics_handle h = val; | |
2657 | |
2658 handle_map[h] = graphics_object (new figure (h, 0)); | |
2659 | |
2660 return h; | |
2661 } | |
2662 | |
2663 void | |
2664 gh_manager::do_push_figure (const graphics_handle& h) | |
2665 { | |
2666 do_pop_figure (h); | |
2667 | |
2668 figure_list.push_front (h); | |
2669 } | |
2670 | |
2671 void | |
2672 gh_manager::do_pop_figure (const graphics_handle& h) | |
2673 { | |
2674 for (figure_list_iterator p = figure_list.begin (); | |
2675 p != figure_list.end (); | |
2676 p++) | |
2677 { | |
2678 if (*p == h) | |
2679 { | |
2680 figure_list.erase (p); | |
2681 break; | |
2682 } | |
2683 } | |
2684 } | |
2685 | |
2686 property_list::plist_map_type | |
2687 root_figure::init_factory_properties (void) | |
2688 { | |
2689 property_list::plist_map_type plist_map; | |
2690 | |
6844 | 2691 plist_map["figure"] = figure::properties::factory_defaults (); |
2692 plist_map["axes"] = axes::properties::factory_defaults (); | |
2693 plist_map["line"] = line::properties::factory_defaults (); | |
2694 plist_map["text"] = text::properties::factory_defaults (); | |
2695 plist_map["image"] = image::properties::factory_defaults (); | |
2696 plist_map["patch"] = patch::properties::factory_defaults (); | |
2697 plist_map["surface"] = surface::properties::factory_defaults (); | |
6406 | 2698 |
2699 return plist_map; | |
2700 } | |
2701 | |
2702 // --------------------------------------------------------------------- | |
2703 | |
2704 DEFUN (ishandle, args, , | |
2705 "-*- texinfo -*-\n\ | |
6678 | 2706 @deftypefn {Built-in Function} {} ishandle (@var{h})\n\ |
6406 | 2707 Return true if @var{h} is a graphics handle and false otherwise.\n\ |
2708 @end deftypefn") | |
2709 { | |
2710 octave_value retval; | |
2711 | |
2712 if (args.length () == 1) | |
2713 retval = is_handle (args(0)); | |
2714 else | |
2715 print_usage (); | |
2716 | |
2717 return retval; | |
2718 } | |
2719 | |
2720 DEFUN (set, args, , | |
2721 "-*- texinfo -*-\n\ | |
6678 | 2722 @deftypefn {Built-in Function} {} set (@var{h}, @var{p}, @var{v}, @dots{})\n\ |
6732 | 2723 Set the named property value or vector @var{p} to the value @var{v}\n\ |
6894 | 2724 for the graphics handle @var{h}.\n\ |
6406 | 2725 @end deftypefn") |
2726 { | |
2727 octave_value retval; | |
2728 | |
2729 int nargin = args.length (); | |
2730 | |
2731 if (nargin > 0) | |
2732 { | |
6732 | 2733 ColumnVector hcv (args(0).vector_value ()); |
6406 | 2734 |
2735 if (! error_state) | |
6732 | 2736 { |
6733 | 2737 bool request_drawnow = false; |
2738 | |
6732 | 2739 for (octave_idx_type n = 0; n < hcv.length (); n++) |
2740 { | |
2741 graphics_object obj = gh_manager::get_object (hcv(n)); | |
6406 | 2742 |
6732 | 2743 if (obj) |
2744 { | |
2745 obj.set (args.splice (0, 1)); | |
6406 | 2746 |
6733 | 2747 request_drawnow = true; |
6732 | 2748 } |
2749 else | |
6733 | 2750 { |
2751 error ("set: invalid handle (= %g)", hcv(n)); | |
2752 break; | |
2753 } | |
6732 | 2754 } |
6733 | 2755 |
2756 if (! error_state && request_drawnow) | |
7409 | 2757 Vdrawnow_requested = true; |
6732 | 2758 } |
6406 | 2759 else |
6732 | 2760 error ("set: expecting graphics handle as first argument"); |
6406 | 2761 } |
2762 else | |
2763 print_usage (); | |
2764 | |
2765 return retval; | |
2766 } | |
2767 | |
2768 DEFUN (get, args, , | |
2769 "-*- texinfo -*-\n\ | |
6678 | 2770 @deftypefn {Built-in Function} {} get (@var{h}, @var{p})\n\ |
6406 | 2771 Return the named property @var{p} from the graphics handle @var{h}.\n\ |
2772 If @var{p} is omitted, return the complete property list for @var{h}.\n\ | |
6732 | 2773 If @var{h} is a vector, return a cell array including the property\n\ |
2774 values or lists respectively.\n\ | |
6406 | 2775 @end deftypefn") |
2776 { | |
2777 octave_value retval; | |
6732 | 2778 octave_value_list vlist; |
6406 | 2779 |
2780 int nargin = args.length (); | |
2781 | |
2782 if (nargin == 1 || nargin == 2) | |
2783 { | |
6732 | 2784 ColumnVector hcv (args(0).vector_value ()); |
6406 | 2785 |
2786 if (! error_state) | |
6732 | 2787 { |
6733 | 2788 octave_idx_type len = hcv.length (); |
2789 | |
2790 vlist.resize (len); | |
2791 | |
2792 for (octave_idx_type n = 0; n < len; n++) | |
6732 | 2793 { |
2794 graphics_object obj = gh_manager::get_object (hcv(n)); | |
6406 | 2795 |
6732 | 2796 if (obj) |
2797 { | |
2798 if (nargin == 1) | |
2799 vlist(n) = obj.get (); | |
2800 else | |
2801 { | |
7189 | 2802 caseless_str property = args(1).string_value (); |
6406 | 2803 |
6732 | 2804 if (! error_state) |
2805 vlist(n) = obj.get (property); | |
2806 else | |
6733 | 2807 { |
2808 error ("get: expecting property name as second argument"); | |
2809 break; | |
2810 } | |
6732 | 2811 } |
2812 } | |
2813 else | |
6733 | 2814 { |
2815 error ("get: invalid handle (= %g)", hcv(n)); | |
2816 break; | |
2817 } | |
6732 | 2818 } |
2819 } | |
6406 | 2820 else |
6732 | 2821 error ("get: expecting graphics handle as first argument"); |
6406 | 2822 } |
2823 else | |
2824 print_usage (); | |
2825 | |
6733 | 2826 if (! error_state) |
6732 | 2827 { |
6733 | 2828 octave_idx_type len = vlist.length (); |
2829 | |
2830 if (len > 1) | |
2831 retval = Cell (vlist); | |
2832 else if (len == 1) | |
2833 retval = vlist(0); | |
6732 | 2834 } |
2835 | |
6406 | 2836 return retval; |
2837 } | |
2838 | |
7379 | 2839 DEFUN (__get__, args, , |
2840 "-*- texinfo -*-\n\ | |
2841 @deftypefn {Built-in Function} {} __get__ (@var{h})\n\ | |
2842 Return all properties from the graphics handle @var{h}.\n\ | |
2843 If @var{h} is a vector, return a cell array including the property\n\ | |
2844 values or lists respectively.\n\ | |
2845 @end deftypefn") | |
2846 { | |
2847 octave_value retval; | |
2848 octave_value_list vlist; | |
2849 | |
2850 int nargin = args.length (); | |
2851 | |
2852 if (nargin == 1) | |
2853 { | |
2854 ColumnVector hcv (args(0).vector_value ()); | |
2855 | |
2856 if (! error_state) | |
2857 { | |
2858 octave_idx_type len = hcv.length (); | |
2859 | |
2860 vlist.resize (len); | |
2861 | |
2862 for (octave_idx_type n = 0; n < len; n++) | |
2863 { | |
2864 graphics_object obj = gh_manager::get_object (hcv(n)); | |
2865 | |
2866 if (obj) | |
2867 vlist(n) = obj.get (true); | |
2868 else | |
2869 { | |
2870 error ("get: invalid handle (= %g)", hcv(n)); | |
2871 break; | |
2872 } | |
2873 } | |
2874 } | |
2875 else | |
2876 error ("get: expecting graphics handle as first argument"); | |
2877 } | |
2878 else | |
2879 print_usage (); | |
2880 | |
2881 if (! error_state) | |
2882 { | |
2883 octave_idx_type len = vlist.length (); | |
2884 | |
2885 if (len > 1) | |
2886 retval = Cell (vlist); | |
2887 else if (len == 1) | |
2888 retval = vlist(0); | |
2889 } | |
2890 | |
2891 return retval; | |
2892 } | |
2893 | |
6406 | 2894 static octave_value |
2895 make_graphics_object (const std::string& go_name, | |
6874 | 2896 const octave_value_list& args) |
6406 | 2897 { |
2898 octave_value retval; | |
2899 | |
2900 double val = args(0).double_value (); | |
2901 | |
2902 if (! error_state) | |
2903 { | |
2904 graphics_handle parent = gh_manager::lookup (val); | |
2905 | |
7056 | 2906 if (parent.ok ()) |
6406 | 2907 { |
2908 graphics_handle h | |
7370 | 2909 = gh_manager::make_graphics_handle (go_name, parent, false); |
6406 | 2910 |
2911 if (! error_state) | |
2912 { | |
2913 adopt (parent, h); | |
2914 | |
2915 xset (h, args.splice (0, 1)); | |
7370 | 2916 xcreatefcn (h); |
6406 | 2917 |
6874 | 2918 retval = h.value (); |
7296 | 2919 |
2920 if (! error_state) | |
7409 | 2921 Vdrawnow_requested = true; |
6406 | 2922 } |
2923 else | |
2924 error ("__go%s__: unable to create graphics handle", | |
2925 go_name.c_str ()); | |
2926 } | |
2927 else | |
2928 error ("__go_%s__: invalid parent", go_name.c_str ()); | |
2929 } | |
2930 else | |
2931 error ("__go_%s__: invalid parent", go_name.c_str ()); | |
2932 | |
2933 return retval; | |
2934 } | |
2935 | |
2936 DEFUN (__go_figure__, args, , | |
2937 "-*- texinfo -*-\n\ | |
2938 @deftypefn {Built-in Function} {} __go_figure__ (@var{fignum})\n\ | |
6945 | 2939 Undocumented internal function.\n\ |
6406 | 2940 @end deftypefn") |
2941 { | |
2942 octave_value retval; | |
2943 | |
2944 if (args.length () > 0) | |
2945 { | |
2946 double val = args(0).double_value (); | |
2947 | |
2948 if (! error_state) | |
2949 { | |
2950 if (is_figure (val)) | |
2951 { | |
2952 graphics_handle h = gh_manager::lookup (val); | |
2953 | |
2954 xset (h, args.splice (0, 1)); | |
2955 | |
6874 | 2956 retval = h.value (); |
6406 | 2957 } |
2958 else | |
2959 { | |
2960 graphics_handle h = octave_NaN; | |
2961 | |
2962 if (xisnan (val)) | |
7370 | 2963 h = gh_manager::make_graphics_handle ("figure", 0, false); |
6406 | 2964 else if (val > 0 && D_NINT (val) == val) |
2965 h = gh_manager::make_figure_handle (val); | |
2966 else | |
2967 error ("__go_figure__: invalid figure number"); | |
2968 | |
7056 | 2969 if (! error_state && h.ok ()) |
6406 | 2970 { |
2971 adopt (0, h); | |
2972 | |
2973 xset (h, args.splice (0, 1)); | |
7370 | 2974 xcreatefcn (h); |
6406 | 2975 |
6874 | 2976 retval = h.value (); |
6406 | 2977 } |
2978 else | |
2979 error ("__go_figure__: failed to create figure handle"); | |
2980 } | |
2981 } | |
2982 else | |
2983 error ("__go_figure__: expecting figure number to be double value"); | |
2984 } | |
2985 else | |
2986 print_usage (); | |
2987 | |
2988 return retval; | |
2989 } | |
2990 | |
2991 #define GO_BODY(TYPE) \ | |
2992 octave_value retval; \ | |
2993 \ | |
2994 if (args.length () > 0) \ | |
2995 retval = make_graphics_object (#TYPE, args); \ | |
2996 else \ | |
2997 print_usage (); \ | |
2998 \ | |
2999 return retval | |
3000 | |
3001 DEFUN (__go_axes__, args, , | |
3002 "-*- texinfo -*-\n\ | |
3003 @deftypefn {Built-in Function} {} __go_axes__ (@var{parent})\n\ | |
6945 | 3004 Undocumented internal function.\n\ |
6406 | 3005 @end deftypefn") |
3006 { | |
3007 GO_BODY (axes); | |
3008 } | |
3009 | |
3010 DEFUN (__go_line__, args, , | |
3011 "-*- texinfo -*-\n\ | |
3012 @deftypefn {Built-in Function} {} __go_line__ (@var{parent})\n\ | |
6945 | 3013 Undocumented internal function.\n\ |
6406 | 3014 @end deftypefn") |
3015 { | |
3016 GO_BODY (line); | |
3017 } | |
3018 | |
3019 DEFUN (__go_text__, args, , | |
3020 "-*- texinfo -*-\n\ | |
3021 @deftypefn {Built-in Function} {} __go_text__ (@var{parent})\n\ | |
6945 | 3022 Undocumented internal function.\n\ |
6406 | 3023 @end deftypefn") |
3024 { | |
3025 GO_BODY (text); | |
3026 } | |
3027 | |
3028 DEFUN (__go_image__, args, , | |
3029 "-*- texinfo -*-\n\ | |
3030 @deftypefn {Built-in Function} {} __go_image__ (@var{parent})\n\ | |
6945 | 3031 Undocumented internal function.\n\ |
6406 | 3032 @end deftypefn") |
3033 { | |
3034 GO_BODY (image); | |
3035 } | |
3036 | |
3037 DEFUN (__go_surface__, args, , | |
3038 "-*- texinfo -*-\n\ | |
3039 @deftypefn {Built-in Function} {} __go_surface__ (@var{parent})\n\ | |
6945 | 3040 Undocumented internal function.\n\ |
6406 | 3041 @end deftypefn") |
3042 { | |
3043 GO_BODY (surface); | |
3044 } | |
3045 | |
6807 | 3046 DEFUN (__go_patch__, args, , |
3047 "-*- texinfo -*-\n\ | |
3048 @deftypefn {Built-in Function} {} __go_patch__ (@var{parent})\n\ | |
6945 | 3049 Undocumented internal function.\n\ |
6807 | 3050 @end deftypefn") |
3051 { | |
3052 GO_BODY (patch); | |
3053 } | |
3054 | |
6406 | 3055 DEFUN (__go_delete__, args, , |
3056 "-*- texinfo -*-\n\ | |
3057 @deftypefn {Built-in Function} {} __go_delete__ (@var{h})\n\ | |
6945 | 3058 Undocumented internal function.\n\ |
6406 | 3059 @end deftypefn") |
3060 { | |
3061 octave_value_list retval; | |
3062 | |
3063 if (args.length () == 1) | |
3064 { | |
3065 graphics_handle h = octave_NaN; | |
3066 | |
3067 double val = args(0).double_value (); | |
3068 | |
3069 if (! error_state) | |
3070 { | |
3071 h = gh_manager::lookup (val); | |
3072 | |
7056 | 3073 if (h.ok ()) |
6406 | 3074 { |
3075 graphics_object obj = gh_manager::get_object (h); | |
3076 | |
3077 graphics_handle parent_h = obj.get_parent (); | |
3078 | |
3079 graphics_object parent_obj = gh_manager::get_object (parent_h); | |
3080 | |
7367 | 3081 // NOTE: free the handle before removing it from its parent's |
3082 // children, such that the object's state is correct when | |
3083 // the deletefcn callback is executed | |
6406 | 3084 |
3085 gh_manager::free (h); | |
7367 | 3086 |
3087 parent_obj.remove_child (h); | |
6406 | 3088 } |
3089 else | |
3090 error ("delete: invalid graphics object (= %g)", val); | |
3091 } | |
3092 else | |
3093 error ("delete: invalid graphics object"); | |
3094 } | |
3095 else | |
3096 print_usage (); | |
3097 | |
3098 return retval; | |
3099 } | |
3100 | |
3101 DEFUN (__go_axes_init__, args, , | |
3102 "-*- texinfo -*-\n\ | |
3103 @deftypefn {Built-in Function} {} __go_axes_init__ (@var{h}, @var{mode})\n\ | |
6945 | 3104 Undocumented internal function.\n\ |
6406 | 3105 @end deftypefn") |
3106 { | |
3107 octave_value retval; | |
3108 | |
3109 int nargin = args.length (); | |
3110 | |
3111 std::string mode = ""; | |
3112 | |
3113 if (nargin == 2) | |
3114 { | |
3115 mode = args(1).string_value (); | |
3116 | |
3117 if (error_state) | |
3118 return retval; | |
3119 } | |
3120 | |
3121 if (nargin == 1 || nargin == 2) | |
3122 { | |
3123 graphics_handle h = octave_NaN; | |
3124 | |
3125 double val = args(0).double_value (); | |
3126 | |
3127 if (! error_state) | |
3128 { | |
3129 h = gh_manager::lookup (val); | |
3130 | |
7056 | 3131 if (h.ok ()) |
6406 | 3132 { |
3133 graphics_object obj = gh_manager::get_object (h); | |
3134 | |
3135 obj.set_defaults (mode); | |
3136 } | |
3137 else | |
3138 error ("__go_axes_init__: invalid graphics object (= %g)", val); | |
3139 } | |
3140 else | |
3141 error ("__go_axes_init__: invalid graphics object"); | |
3142 } | |
3143 else | |
3144 print_usage (); | |
3145 | |
3146 return retval; | |
3147 } | |
3148 | |
3149 DEFUN (__go_handles__, , , | |
3150 "-*- texinfo -*-\n\ | |
3151 @deftypefn {Built-in Function} {} __go_handles__ ()\n\ | |
6945 | 3152 Undocumented internal function.\n\ |
6406 | 3153 @end deftypefn") |
3154 { | |
6425 | 3155 return octave_value (gh_manager::handle_list ()); |
3156 } | |
3157 | |
3158 DEFUN (__go_figure_handles__, , , | |
3159 "-*- texinfo -*-\n\ | |
3160 @deftypefn {Built-in Function} {} __go_figure_handles__ ()\n\ | |
6945 | 3161 Undocumented internal function.\n\ |
6425 | 3162 @end deftypefn") |
3163 { | |
3164 return octave_value (gh_manager::figure_handle_list ()); | |
6406 | 3165 } |
3166 | |
7409 | 3167 static void |
3168 clear_drawnow_request (void *) | |
3169 { | |
3170 Vdrawnow_requested = false; | |
3171 } | |
3172 | |
7408 | 3173 DEFUN (drawnow, args, , |
3174 "-*- texinfo -*-\n\ | |
3175 @deftypefn {Built-in Function} {} __go_drawnow__ ()\n\ | |
3176 @deftypefnx {Built-in Function} {} __go_drawnow__ (@var{term}, @var{file}, @var{mono}, @var{debug_file})\n\ | |
3177 Undocumented internal function.\n\ | |
3178 @end deftypefn") | |
3179 { | |
3180 static int drawnow_executing = 0; | |
3181 static bool __go_close_all_registered__ = false; | |
3182 | |
3183 octave_value retval; | |
3184 | |
7409 | 3185 unwind_protect::begin_frame ("Fdrawnow"); |
3186 unwind_protect::add (clear_drawnow_request); | |
3187 | |
3188 unwind_protect_int (drawnow_executing); | |
3189 | |
3190 if (++drawnow_executing <= 1) | |
7408 | 3191 { |
7409 | 3192 if (! __go_close_all_registered__) |
3193 { | |
3194 octave_add_atexit_function ("__go_close_all__"); | |
3195 | |
3196 __go_close_all_registered__ = true; | |
3197 } | |
3198 | |
3199 if (args.length () == 0) | |
7408 | 3200 { |
7409 | 3201 Matrix hlist = gh_manager::figure_handle_list (); |
3202 | |
3203 for (int i = 0; ! error_state && i < hlist.length (); i++) | |
7408 | 3204 { |
7409 | 3205 graphics_handle h = gh_manager::lookup (hlist(i)); |
3206 | |
3207 if (h.ok () && h != 0) | |
7408 | 3208 { |
7409 | 3209 graphics_object go = gh_manager::get_object (h); |
3210 figure::properties& fprops = dynamic_cast <figure::properties&> (go.get_properties ()); | |
3211 | |
3212 if (fprops.is_modified ()) | |
7408 | 3213 { |
7409 | 3214 if (fprops.is_visible ()) |
3215 fprops.get_backend ().redraw_figure (h); | |
3216 else if (! fprops.get___plot_stream__ ().is_empty ()) | |
3217 { | |
3218 fprops.close (false); | |
3219 fprops.set___plot_stream__ (Matrix ()); | |
3220 fprops.set___enhanced__ (false); | |
3221 } | |
3222 fprops.set_modified (false); | |
7408 | 3223 } |
3224 } | |
3225 } | |
3226 } | |
7409 | 3227 else if (args.length () >= 2 && args.length () <= 4) |
7408 | 3228 { |
7409 | 3229 std::string term, file, debug_file; |
3230 bool mono; | |
3231 | |
3232 term = args(0).string_value (); | |
7408 | 3233 |
3234 if (! error_state) | |
3235 { | |
7409 | 3236 file = args(1).string_value (); |
7408 | 3237 |
3238 if (! error_state) | |
3239 { | |
7409 | 3240 size_t pos = file.find_last_of (file_ops::dir_sep_chars); |
3241 | |
3242 if (pos != NPOS) | |
3243 { | |
3244 file_stat fs (file.substr (0, pos)); | |
3245 | |
3246 if (! (fs && fs.is_dir ())) | |
3247 error ("drawnow: nonexistent directory `%s'", | |
3248 file.substr (0, pos).c_str ()); | |
3249 } | |
3250 | |
3251 mono = (args.length () >= 3 ? args(2).bool_value () : false); | |
7408 | 3252 |
3253 if (! error_state) | |
3254 { | |
7409 | 3255 debug_file = (args.length () > 3 ? args(3).string_value () |
3256 : ""); | |
3257 | |
3258 if (! error_state) | |
7408 | 3259 { |
7409 | 3260 graphics_handle h = gcf (); |
3261 | |
3262 if (h.ok ()) | |
3263 { | |
3264 graphics_object go = gh_manager::get_object (h); | |
3265 | |
7419 | 3266 go.get_backend () |
7409 | 3267 .print_figure (h, term, file, mono, debug_file); |
3268 } | |
3269 else | |
3270 error ("drawnow: nothing to draw"); | |
7408 | 3271 } |
3272 else | |
7409 | 3273 error ("drawnow: invalid debug_file, expected a string value"); |
7408 | 3274 } |
3275 else | |
7409 | 3276 error ("drawnow: invalid colormode, expected a boolean value"); |
7408 | 3277 } |
3278 else | |
7409 | 3279 error ("drawnow: invalid file, expected a string value"); |
7408 | 3280 } |
3281 else | |
7409 | 3282 error ("drawnow: invalid terminal, expected a string value"); |
7408 | 3283 } |
3284 else | |
7409 | 3285 print_usage (); |
7408 | 3286 } |
7409 | 3287 |
3288 unwind_protect::run_frame ("Fdrawnow"); | |
7408 | 3289 |
3290 return retval; | |
3291 } | |
3292 | |
6595 | 3293 octave_value |
7447 | 3294 get_property_from_handle (double handle, const std::string& property, |
3295 const std::string& func) | |
6595 | 3296 { |
3297 graphics_object obj = gh_manager::get_object (handle); | |
3298 octave_value retval; | |
3299 | |
3300 if (obj) | |
3301 { | |
7189 | 3302 caseless_str p = std::string (property); |
6595 | 3303 retval = obj.get (p); |
3304 } | |
3305 else | |
3306 error ("%s: invalid handle (= %g)", func.c_str(), handle); | |
3307 | |
3308 return retval; | |
3309 } | |
3310 | |
3311 bool | |
7447 | 3312 set_property_in_handle (double handle, const std::string& property, |
3313 const octave_value& arg, const std::string& func) | |
6595 | 3314 { |
3315 graphics_object obj = gh_manager::get_object (handle); | |
3316 int ret = false; | |
3317 | |
3318 if (obj) | |
3319 { | |
7189 | 3320 caseless_str p = std::string (property); |
6595 | 3321 obj.set (p, arg); |
3322 if (!error_state) | |
3323 ret = true; | |
3324 } | |
3325 else | |
3326 error ("%s: invalid handle (= %g)", func.c_str(), handle); | |
3327 | |
3328 return ret; | |
3329 } | |
3330 | |
6406 | 3331 /* |
3332 ;;; Local Variables: *** | |
3333 ;;; mode: C++ *** | |
3334 ;;; End: *** | |
3335 */ |