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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cctype> |
|
29 |
|
30 #include <algorithm> |
|
31 #include <list> |
|
32 #include <map> |
|
33 #include <set> |
|
34 #include <string> |
|
35 |
|
36 #include <defun.h> |
|
37 #include <ov.h> |
|
38 #include <oct-obj.h> |
|
39 #include <oct-map.h> |
|
40 #include <ov-fcn-handle.h> |
|
41 #include <parse.h> |
|
42 |
|
43 static void |
|
44 gripe_set_invalid (const std::string& pname) |
|
45 { |
|
46 error ("set: invalid value for %s property", pname.c_str ()); |
|
47 } |
|
48 |
|
49 static octave_value |
|
50 nan_to_empty (double val) |
|
51 { |
|
52 return xisnan (val) ? octave_value (Matrix ()) : octave_value (val); |
|
53 } |
|
54 |
|
55 static octave_value |
|
56 empty_to_nan (const octave_value& val) |
|
57 { |
|
58 return val.is_empty () ? octave_value (octave_NaN) : val; |
|
59 } |
|
60 |
|
61 // --------------------------------------------------------------------- |
|
62 |
|
63 class color_property |
|
64 { |
|
65 public: |
|
66 color_property (double r = 0, double g = 0, double b = 1) |
|
67 : red (r), green (g), blue (b) |
|
68 { |
|
69 validate (); |
|
70 } |
|
71 |
|
72 color_property (const octave_value& val) |
|
73 : red (), green (), blue () |
|
74 { |
|
75 // FIXME -- need some error checking here. |
|
76 |
|
77 Matrix m = val.matrix_value (); |
|
78 |
|
79 if (! error_state && m.numel () == 3) |
|
80 { |
|
81 red = m(0); |
|
82 green = m(1); |
|
83 blue = m(2); |
|
84 |
|
85 validate (); |
|
86 } |
|
87 else |
|
88 error ("invalid RGB color specification"); |
|
89 } |
|
90 |
|
91 void validate (void) const |
|
92 { |
|
93 if (red < 0 || red > 1 || green < 0 || green > 1 || blue < 0 || blue > 1) |
|
94 error ("invalid RGB color specification"); |
|
95 } |
|
96 |
|
97 operator octave_value (void) const |
|
98 { |
|
99 Matrix retval (1, 3); |
|
100 |
|
101 retval(0) = red; |
|
102 retval(1) = green; |
|
103 retval(2) = blue; |
|
104 |
|
105 return retval; |
|
106 } |
|
107 |
|
108 private: |
|
109 double red; |
|
110 double green; |
|
111 double blue; |
|
112 }; |
|
113 |
|
114 class colormap_property |
|
115 { |
|
116 public: |
|
117 colormap_property (const Matrix& m = Matrix ()) |
|
118 : cmap (m) |
|
119 { |
|
120 if (cmap.is_empty ()) |
|
121 { |
|
122 cmap = Matrix (64, 3); |
|
123 |
|
124 for (octave_idx_type i = 0; i < 64; i++) |
|
125 cmap(i,0) = cmap(i,1) = cmap(i,2) = i / 64.0; |
|
126 } |
|
127 |
|
128 validate (); |
|
129 } |
|
130 |
|
131 colormap_property (const octave_value& val) |
|
132 { |
|
133 cmap = val.matrix_value (); |
|
134 |
|
135 validate (); |
|
136 } |
|
137 |
|
138 void validate (void) const |
|
139 { |
|
140 if (error_state || cmap.columns () != 3) |
|
141 error ("invalid colormap specification"); |
|
142 } |
|
143 |
|
144 operator octave_value (void) const { return cmap; } |
|
145 |
|
146 private: |
|
147 Matrix cmap; |
|
148 }; |
|
149 |
|
150 // --------------------------------------------------------------------- |
|
151 |
|
152 class property_name : public std::string |
|
153 { |
|
154 public: |
|
155 typedef std::string::iterator iterator; |
|
156 typedef std::string::const_iterator const_iterator; |
|
157 |
|
158 property_name (void) : std::string () { } |
|
159 property_name (const std::string& s) : std::string (s) { } |
|
160 property_name (const char *s) : std::string (s) { } |
|
161 |
|
162 property_name (const property_name& name) : std::string (name) { } |
|
163 |
|
164 property_name& operator = (const property_name& pname) |
|
165 { |
|
166 std::string::operator = (pname); |
|
167 return *this; |
|
168 } |
|
169 |
|
170 operator std::string (void) const { return *this; } |
|
171 |
|
172 // Case-insensitive comparison. |
|
173 bool compare (const std::string& s, size_t limit = NPOS) const |
|
174 { |
|
175 const_iterator p1 = begin (); |
|
176 const_iterator p2 = s.begin (); |
|
177 |
|
178 size_t k = 0; |
|
179 |
|
180 while (p1 != end () && p2 != s.end () && k++ < limit) |
|
181 { |
|
182 if (std::tolower (*p1) != std::tolower (*p2)) |
|
183 return false; |
|
184 |
|
185 *p1++; |
|
186 *p2++; |
|
187 } |
|
188 |
|
189 return (limit == NPOS) ? size () == s.size () : k == limit; |
|
190 } |
|
191 }; |
|
192 |
|
193 // --------------------------------------------------------------------- |
|
194 |
|
195 class property_list |
|
196 { |
|
197 public: |
|
198 typedef std::map<std::string, octave_value> pval_map_type; |
|
199 typedef std::map<std::string, pval_map_type> plist_map_type; |
|
200 |
|
201 typedef pval_map_type::iterator pval_map_iterator; |
|
202 typedef pval_map_type::const_iterator pval_map_const_iterator; |
|
203 |
|
204 typedef plist_map_type::iterator plist_map_iterator; |
|
205 typedef plist_map_type::const_iterator plist_map_const_iterator; |
|
206 |
|
207 property_list (const plist_map_type& m = plist_map_type ()) |
|
208 : plist_map (m) { } |
|
209 |
|
210 ~property_list (void) { } |
|
211 |
|
212 void set (const property_name& name, const octave_value& val) |
|
213 { |
|
214 size_t offset = 0; |
|
215 |
|
216 size_t len = name.length (); |
|
217 |
|
218 if (len > 4) |
|
219 { |
|
220 property_name pfx = name.substr (0, 4); |
|
221 |
|
222 if (pfx.compare ("axes") || pfx.compare ("line") |
|
223 || pfx.compare ("text")) |
|
224 offset = 4; |
|
225 else if (len > 5) |
|
226 { |
|
227 pfx = name.substr (0, 5); |
|
228 |
|
229 if (pfx.compare ("image")) |
|
230 offset = 5; |
|
231 else if (len > 6) |
|
232 { |
|
233 pfx = name.substr (0, 6); |
|
234 |
|
235 if (pfx.compare ("figure")) |
|
236 offset = 6; |
|
237 else if (len > 7) |
|
238 { |
|
239 pfx = name.substr (0, 7); |
|
240 |
|
241 if (pfx.compare ("surface")) |
|
242 offset = 7; |
|
243 } |
|
244 } |
|
245 } |
|
246 |
|
247 if (offset > 0) |
|
248 { |
|
249 // FIXME -- should we validate property names and values here? |
|
250 |
|
251 std::string pname = name.substr (offset); |
|
252 |
|
253 std::transform (pfx.begin (), pfx.end (), pfx.begin (), tolower); |
|
254 std::transform (pname.begin (), pname.end (), pname.begin (), tolower); |
|
255 |
|
256 bool remove = false; |
|
257 if (val.is_string ()) |
|
258 { |
|
259 property_name tval = val.string_value (); |
|
260 |
|
261 remove = tval.compare ("remove"); |
|
262 } |
|
263 |
|
264 pval_map_type& pval_map = plist_map[pfx]; |
|
265 |
|
266 if (remove) |
|
267 { |
|
268 pval_map_iterator p = pval_map.find (pname); |
|
269 |
|
270 if (p != pval_map.end ()) |
|
271 pval_map.erase (p); |
|
272 } |
|
273 else |
|
274 pval_map[pname] = val; |
|
275 } |
|
276 } |
|
277 |
|
278 if (offset == 0) |
|
279 error ("invalid default property specification"); |
|
280 } |
|
281 |
|
282 octave_value lookup (const property_name& name) const |
|
283 { |
|
284 octave_value retval; |
|
285 |
|
286 size_t offset = 0; |
|
287 |
|
288 size_t len = name.length (); |
|
289 |
|
290 if (len > 4) |
|
291 { |
|
292 property_name pfx = name.substr (0, 4); |
|
293 |
|
294 if (pfx.compare ("axes") || pfx.compare ("line") |
|
295 || pfx.compare ("text")) |
|
296 offset = 4; |
|
297 else if (len > 5) |
|
298 { |
|
299 pfx = name.substr (0, 5); |
|
300 |
|
301 if (pfx.compare ("image")) |
|
302 offset = 5; |
|
303 else if (len > 6) |
|
304 { |
|
305 pfx = name.substr (0, 6); |
|
306 |
|
307 if (pfx.compare ("figure")) |
|
308 offset = 6; |
|
309 else if (len > 7) |
|
310 { |
|
311 pfx = name.substr (0, 7); |
|
312 |
|
313 if (pfx.compare ("surface")) |
|
314 offset = 7; |
|
315 } |
|
316 } |
|
317 } |
|
318 |
|
319 if (offset > 0) |
|
320 { |
|
321 std::string pname = name.substr (offset); |
|
322 |
|
323 std::transform (pfx.begin (), pfx.end (), pfx.begin (), tolower); |
|
324 std::transform (pname.begin (), pname.end (), pname.begin (), tolower); |
|
325 |
|
326 plist_map_const_iterator p = find (pfx); |
|
327 |
|
328 if (p != end ()) |
|
329 { |
|
330 const pval_map_type& pval_map = p->second; |
|
331 |
|
332 pval_map_const_iterator q = pval_map.find (pname); |
|
333 |
|
334 if (q != pval_map.end ()) |
|
335 retval = q->second; |
|
336 } |
|
337 } |
|
338 } |
|
339 |
|
340 return retval; |
|
341 } |
|
342 |
|
343 plist_map_iterator begin (void) { return plist_map.begin (); } |
|
344 plist_map_const_iterator begin (void) const { return plist_map.begin (); } |
|
345 |
|
346 plist_map_iterator end (void) { return plist_map.end (); } |
|
347 plist_map_const_iterator end (void) const { return plist_map.end (); } |
|
348 |
|
349 plist_map_iterator find (const std::string& go_name) |
|
350 { |
|
351 return plist_map.find (go_name); |
|
352 } |
|
353 |
|
354 plist_map_const_iterator find (const std::string& go_name) const |
|
355 { |
|
356 return plist_map.find (go_name); |
|
357 } |
|
358 |
|
359 Octave_map as_struct (const std::string& prefix_arg) const |
|
360 { |
|
361 Octave_map m; |
|
362 |
|
363 for (plist_map_const_iterator p = begin (); p != end (); p++) |
|
364 { |
|
365 std::string prefix = prefix_arg + p->first; |
|
366 |
|
367 const pval_map_type pval_map = p->second; |
|
368 |
|
369 for (pval_map_const_iterator q = pval_map.begin (); |
|
370 q != pval_map.end (); |
|
371 q++) |
|
372 m.assign (prefix + q->first, q->second); |
|
373 } |
|
374 |
|
375 return m; |
|
376 } |
|
377 |
|
378 private: |
|
379 plist_map_type plist_map; |
|
380 }; |
|
381 |
|
382 // --------------------------------------------------------------------- |
|
383 |
|
384 typedef double graphics_handle; |
|
385 |
|
386 // --------------------------------------------------------------------- |
|
387 |
|
388 class base_graphics_object |
|
389 { |
|
390 public: |
|
391 friend class graphics_object; |
|
392 |
|
393 base_graphics_object (void) : count (1) { } |
|
394 |
|
395 base_graphics_object (const base_graphics_object&) { } |
|
396 |
|
397 virtual ~base_graphics_object (void) { } |
|
398 |
|
399 virtual void override_defaults (base_graphics_object&) |
|
400 { |
|
401 error ("base_graphics_object::override_defaults: invalid graphics object"); |
|
402 } |
|
403 |
|
404 virtual void set_from_list (property_list&) |
|
405 { |
|
406 error ("base_graphics_object::set_from_list: invalid graphics object"); |
|
407 } |
|
408 |
|
409 virtual void set (const property_name&, const octave_value&) |
|
410 { |
|
411 error ("base_graphics_object::set: invalid graphics object"); |
|
412 } |
|
413 |
|
414 virtual void set_defaults (const std::string&) |
|
415 { |
|
416 error ("base_graphics_object::set_defaults: invalid graphics object"); |
|
417 } |
|
418 |
|
419 virtual octave_value get (void) const |
|
420 { |
|
421 error ("base_graphics_object::get: invalid graphics object"); |
|
422 return octave_value (); |
|
423 } |
|
424 |
|
425 virtual octave_value get (const property_name&) const |
|
426 { |
|
427 error ("base_graphics_object::get: invalid graphics object"); |
|
428 return octave_value (); |
|
429 } |
|
430 |
|
431 virtual octave_value get_default (const property_name&) const; |
|
432 |
|
433 virtual octave_value get_factory_default (const property_name&) const; |
|
434 |
|
435 virtual octave_value get_defaults (void) const |
|
436 { |
|
437 error ("base_graphics_object::get_defaults: invalid graphics object"); |
|
438 return octave_value (); |
|
439 } |
|
440 |
|
441 virtual octave_value get_factory_defaults (void) const |
|
442 { |
|
443 error ("base_graphics_object::get_factory_defaults: invalid graphics object"); |
|
444 return octave_value (); |
|
445 } |
|
446 |
|
447 virtual graphics_handle get_parent (void) const |
|
448 { |
|
449 error ("base_graphics_object::get_parent: invalid graphics object"); |
|
450 return octave_NaN; |
|
451 } |
|
452 |
|
453 virtual void remove_child (const graphics_handle&) |
|
454 { |
|
455 error ("base_graphics_object::remove_child: invalid graphics object"); |
|
456 } |
|
457 |
|
458 virtual void adopt (const graphics_handle&) |
|
459 { |
|
460 error ("base_graphics_object::adopt: invalid graphics object"); |
|
461 } |
|
462 |
|
463 virtual void reparent (const graphics_handle&) |
|
464 { |
|
465 error ("base_graphics_object::reparent: invalid graphics object"); |
|
466 } |
|
467 |
|
468 virtual void defaults (void) const |
|
469 { |
|
470 error ("base_graphics_object::default: invalid graphics object"); |
|
471 } |
|
472 |
|
473 virtual bool valid_object (void) const { return false; } |
|
474 |
|
475 virtual std::string type (void) const { return "unknown"; } |
|
476 |
|
477 bool isa (const std::string& go_name) const |
|
478 { |
|
479 return type () == go_name; |
|
480 } |
|
481 |
|
482 protected: |
|
483 // A reference count. |
|
484 int count; |
|
485 }; |
|
486 |
|
487 class graphics_object |
|
488 { |
|
489 public: |
|
490 graphics_object (void) : rep (new base_graphics_object ()) { } |
|
491 |
|
492 graphics_object (base_graphics_object *new_rep) |
|
493 : rep (new_rep) { } |
|
494 |
|
495 graphics_object (const graphics_object& obj) |
|
496 { |
|
497 rep = obj.rep; |
|
498 rep->count++; |
|
499 } |
|
500 |
|
501 graphics_object& operator = (const graphics_object& obj) |
|
502 { |
|
503 if (rep != obj.rep) |
|
504 { |
|
505 if (--rep->count == 0) |
|
506 delete rep; |
|
507 |
|
508 rep = obj.rep; |
|
509 rep->count++; |
|
510 } |
|
511 |
|
512 return *this; |
|
513 } |
|
514 |
|
515 ~graphics_object (void) |
|
516 { |
|
517 if (--rep->count == 0) |
|
518 delete rep; |
|
519 } |
|
520 |
|
521 void override_defaults (base_graphics_object& obj) |
|
522 { |
|
523 rep->override_defaults (obj); |
|
524 } |
|
525 |
|
526 void set_from_list (property_list& plist) |
|
527 { |
|
528 rep->set_from_list (plist); |
|
529 } |
|
530 |
|
531 void set (const property_name& name, const octave_value& val) |
|
532 { |
|
533 rep->set (name, val); |
|
534 } |
|
535 |
|
536 void set (const octave_value_list& args) |
|
537 { |
|
538 int nargin = args.length (); |
|
539 |
|
540 if (nargin == 0) |
|
541 rep->defaults (); |
|
542 else if (nargin % 2 == 0) |
|
543 { |
|
544 for (int i = 0; i < nargin; i += 2) |
|
545 { |
|
546 property_name name = args(i).string_value (); |
|
547 |
|
548 if (! error_state) |
|
549 { |
|
550 octave_value val = args(i+1); |
|
551 |
|
552 if (val.is_string ()) |
|
553 { |
|
554 property_name tval = val.string_value (); |
|
555 |
|
556 if (tval.compare ("default")) |
|
557 val = get_default (name); |
|
558 else if (tval.compare ("factory")) |
|
559 val = get_factory_default (name); |
|
560 } |
|
561 |
|
562 if (error_state) |
|
563 break; |
|
564 |
|
565 rep->set (name, val); |
|
566 } |
|
567 else |
|
568 error ("set: expecting argument %d to be a property name", i); |
|
569 } |
|
570 } |
|
571 else |
|
572 error ("set: invalid number of arguments"); |
|
573 } |
|
574 |
|
575 void set_defaults (const std::string& mode) |
|
576 { |
|
577 rep->set_defaults (mode); |
|
578 } |
|
579 |
|
580 octave_value get (void) const |
|
581 { |
|
582 return rep->get (); |
|
583 } |
|
584 |
|
585 octave_value get (const property_name& name) const |
|
586 { |
|
587 return name.compare ("default") |
|
588 ? get_defaults () |
|
589 : (name.compare ("factory") |
|
590 ? get_factory_defaults () : rep->get (name)); |
|
591 } |
|
592 |
|
593 octave_value get_default (const property_name& name) const |
|
594 { |
|
595 return rep->get_default (name); |
|
596 } |
|
597 |
|
598 octave_value get_factory_default (const property_name& name) const |
|
599 { |
|
600 return rep->get_factory_default (name); |
|
601 } |
|
602 |
|
603 octave_value get_defaults (void) const { return rep->get_defaults (); } |
|
604 |
|
605 octave_value get_factory_defaults (void) const |
|
606 { |
|
607 return rep->get_factory_defaults (); |
|
608 } |
|
609 |
|
610 graphics_handle get_parent (void) const { return rep->get_parent (); } |
|
611 |
|
612 void remove_child (const graphics_handle& h) { return rep->remove_child (h); } |
|
613 |
|
614 void adopt (const graphics_handle& h) { return rep->adopt (h); } |
|
615 |
|
616 void reparent (const graphics_handle& h) { return rep->reparent (h); } |
|
617 |
|
618 void defaults (void) const { rep->defaults (); } |
|
619 |
|
620 bool isa (const std::string& go_name) const { return rep->isa (go_name); } |
|
621 |
|
622 bool valid_object (void) const { return rep->valid_object (); } |
|
623 |
|
624 operator bool (void) const { return rep->valid_object (); } |
|
625 |
|
626 private: |
|
627 base_graphics_object *rep; |
|
628 }; |
|
629 |
|
630 // --------------------------------------------------------------------- |
|
631 |
|
632 class gh_manager |
|
633 { |
|
634 protected: |
|
635 |
|
636 gh_manager (void); |
|
637 |
|
638 public: |
|
639 |
|
640 static bool instance_ok (void) |
|
641 { |
|
642 bool retval = true; |
|
643 |
|
644 if (! instance) |
|
645 instance = new gh_manager (); |
|
646 |
|
647 if (! instance) |
|
648 { |
|
649 ::error ("unable to create gh_manager!"); |
|
650 |
|
651 retval = false; |
|
652 } |
|
653 |
|
654 return retval; |
|
655 } |
|
656 |
|
657 static void free (const graphics_handle& h) |
|
658 { |
|
659 if (instance_ok ()) |
|
660 instance->do_free (h); |
|
661 } |
|
662 |
|
663 static graphics_handle lookup (double val) |
|
664 { |
|
665 return instance_ok () ? instance->do_lookup (val) : graphics_handle (); |
|
666 } |
|
667 |
|
668 static graphics_object get_object (const graphics_handle& h) |
|
669 { |
|
670 return instance_ok () ? instance->do_get_object (h) : graphics_object (); |
|
671 } |
|
672 |
|
673 static graphics_handle |
|
674 make_graphics_handle (const std::string& go_name, |
|
675 const graphics_handle& parent) |
|
676 { |
|
677 return instance_ok () |
|
678 ? instance->do_make_graphics_handle (go_name, parent) : octave_NaN; |
|
679 } |
|
680 |
|
681 static graphics_handle make_figure_handle (double val) |
|
682 { |
|
683 return instance_ok () |
|
684 ? instance->do_make_figure_handle (val) : octave_NaN; |
|
685 } |
|
686 |
|
687 static void push_figure (const graphics_handle& h) |
|
688 { |
|
689 if (instance_ok ()) |
|
690 instance->do_push_figure (h); |
|
691 } |
|
692 |
|
693 static void pop_figure (const graphics_handle& h) |
|
694 { |
|
695 if (instance_ok ()) |
|
696 instance->do_pop_figure (h); |
|
697 } |
|
698 |
|
699 static graphics_handle current_figure (void) |
|
700 { |
|
701 return instance_ok () ? instance->do_current_figure () : octave_NaN; |
|
702 } |
|
703 |
|
704 static Matrix list (void) |
|
705 { |
|
706 return instance_ok () ? instance->do_list () : Matrix (); |
|
707 } |
|
708 |
|
709 private: |
|
710 |
|
711 static gh_manager *instance; |
|
712 |
|
713 typedef std::map<graphics_handle, graphics_object>::iterator iterator; |
|
714 typedef std::map<graphics_handle, graphics_object>::const_iterator const_iterator; |
|
715 |
|
716 typedef std::set<graphics_handle>::iterator free_list_iterator; |
|
717 typedef std::set<graphics_handle>::const_iterator const_free_list_iterator; |
|
718 |
|
719 typedef std::list<graphics_handle>::iterator figure_list_iterator; |
|
720 typedef std::list<graphics_handle>::const_iterator const_figure_list_iterator; |
|
721 |
|
722 // A map of handles to graphics objects. |
|
723 std::map<graphics_handle, graphics_object> handle_map; |
|
724 |
|
725 // The available graphics handles. |
|
726 std::set<graphics_handle> handle_free_list; |
|
727 |
|
728 // The next handle available if handle_free_list is empty. |
|
729 graphics_handle next_handle; |
|
730 |
|
731 // The allocated figure handles. Top of the stack is most recently |
|
732 // created. |
|
733 std::list<graphics_handle> figure_list; |
|
734 |
|
735 graphics_handle get_handle (const std::string& go_name) |
|
736 { |
|
737 graphics_handle retval; |
|
738 |
|
739 if (go_name == "figure") |
|
740 { |
|
741 // We always want the lowest unused figure number. |
|
742 |
|
743 retval = 1; |
|
744 |
|
745 while (handle_map.find (retval) != handle_map.end ()) |
|
746 retval++; |
|
747 } |
|
748 else |
|
749 { |
|
750 free_list_iterator p = handle_free_list.begin (); |
|
751 |
|
752 if (p != handle_free_list.end ()) |
|
753 { |
|
754 retval = *p; |
|
755 handle_free_list.erase (p); |
|
756 } |
|
757 else |
|
758 retval = next_handle--; |
|
759 } |
|
760 |
|
761 return retval; |
|
762 } |
|
763 |
|
764 void do_free (const graphics_handle& h) |
|
765 { |
|
766 if (h != 0) |
|
767 { |
|
768 iterator p = handle_map.find (h); |
|
769 |
|
770 if (p != handle_map.end ()) |
|
771 { |
|
772 handle_map.erase (p); |
|
773 |
|
774 if (h < 0) |
|
775 handle_free_list.insert (h); |
|
776 } |
|
777 else |
|
778 error ("graphics_handle::free: invalid object %g", h); |
|
779 } |
|
780 else |
|
781 error ("graphics_handle::free: can't delete root figure"); |
|
782 } |
|
783 |
|
784 graphics_handle do_lookup (double val) |
|
785 { |
|
786 iterator p = handle_map.find (val); |
|
787 |
|
788 return (p != handle_map.end ()) ? p->first : octave_NaN; |
|
789 } |
|
790 |
|
791 graphics_object do_get_object (const graphics_handle& h) |
|
792 { |
|
793 iterator p = handle_map.find (h); |
|
794 |
|
795 return (p != handle_map.end ()) ? p->second : graphics_object (); |
|
796 } |
|
797 |
|
798 graphics_handle do_make_graphics_handle (const std::string& go_name, |
|
799 const graphics_handle& p); |
|
800 |
|
801 graphics_handle do_make_figure_handle (double val); |
|
802 |
|
803 Matrix do_list (void) |
|
804 { |
|
805 Matrix retval (1, handle_map.size ()); |
|
806 octave_idx_type i = 0; |
|
807 for (const_iterator p = handle_map.begin (); p != handle_map.end (); p++) |
|
808 retval(i++) = p->first; |
|
809 return retval; |
|
810 } |
|
811 |
|
812 void do_push_figure (const graphics_handle& h); |
|
813 |
|
814 void do_pop_figure (const graphics_handle& h); |
|
815 |
|
816 graphics_handle do_current_figure (void) const |
|
817 { |
|
818 return figure_list.empty () ? octave_NaN : figure_list.front (); |
|
819 } |
|
820 }; |
|
821 |
|
822 gh_manager *gh_manager::instance = 0; |
|
823 |
|
824 // --------------------------------------------------------------------- |
|
825 |
|
826 static void |
|
827 xset (const graphics_handle& h, const property_name& name, |
|
828 const octave_value& val) |
|
829 { |
|
830 graphics_object obj = gh_manager::get_object (h); |
|
831 obj.set (name, val); |
|
832 } |
|
833 |
|
834 static void |
|
835 xset (const graphics_handle& h, const octave_value_list& args) |
|
836 { |
|
837 if (args.length () > 0) |
|
838 { |
|
839 graphics_object obj = gh_manager::get_object (h); |
|
840 obj.set (args); |
|
841 } |
|
842 } |
|
843 |
|
844 |
|
845 static octave_value |
|
846 xget (const graphics_handle& h, const property_name& name) |
|
847 { |
|
848 graphics_object obj = gh_manager::get_object (h); |
|
849 return obj.get (name); |
|
850 } |
|
851 |
|
852 static graphics_handle |
|
853 reparent (const octave_value& ov, const std::string& who, |
|
854 const std::string& property, const graphics_handle& new_parent, |
|
855 bool adopt = true) |
|
856 { |
|
857 graphics_handle h = octave_NaN; |
|
858 |
|
859 double val = ov.double_value (); |
|
860 |
|
861 if (! error_state) |
|
862 { |
|
863 h = gh_manager::lookup (val); |
|
864 |
|
865 if (! xisnan (h)) |
|
866 { |
|
867 graphics_object obj = gh_manager::get_object (h); |
|
868 |
|
869 graphics_handle parent_h = obj.get_parent (); |
|
870 |
|
871 graphics_object parent_obj = gh_manager::get_object (parent_h); |
|
872 |
|
873 parent_obj.remove_child (h); |
|
874 |
|
875 if (adopt) |
|
876 obj.set ("parent", new_parent); |
|
877 else |
|
878 obj.reparent (new_parent); |
|
879 } |
|
880 else |
|
881 error ("%s: invalid graphics handle (= %g) for %s", |
|
882 who.c_str (), val, property.c_str ()); |
|
883 } |
|
884 else |
|
885 error ("%s: expecting %s to be a graphics handle", |
|
886 who.c_str (), property.c_str ()); |
|
887 |
|
888 return h; |
|
889 } |
|
890 |
|
891 // This function is NOT equivalent to the scripting language function gcf. |
|
892 graphics_handle |
|
893 gcf (void) |
|
894 { |
|
895 octave_value val = xget (0, "currentfigure"); |
|
896 |
|
897 return val.is_empty () ? octave_NaN : val.double_value (); |
|
898 } |
|
899 |
|
900 // This function is NOT equivalent to the scripting language function gca. |
|
901 graphics_handle |
|
902 gca (void) |
|
903 { |
|
904 octave_value val = xget (gcf (), "currentaxes"); |
|
905 |
|
906 return val.is_empty () ? octave_NaN : val.double_value (); |
|
907 } |
|
908 |
|
909 static void |
|
910 adopt (const graphics_handle& p, const graphics_handle& h) |
|
911 { |
|
912 graphics_object parent_obj = gh_manager::get_object (p); |
|
913 |
|
914 parent_obj.adopt (h); |
|
915 } |
|
916 |
|
917 static bool |
|
918 is_handle (double val) |
|
919 { |
|
920 return ! xisnan (gh_manager::lookup (val)); |
|
921 } |
|
922 |
|
923 static bool |
|
924 is_handle (const octave_value& val) |
|
925 { |
|
926 return val.is_real_type () && is_handle (val.double_value ()); |
|
927 } |
|
928 |
|
929 static bool |
|
930 is_figure (double val) |
|
931 { |
|
932 graphics_object obj = gh_manager::get_object (val); |
|
933 |
|
934 return obj && obj.isa ("figure"); |
|
935 } |
|
936 |
|
937 // --------------------------------------------------------------------- |
|
938 |
|
939 static int |
|
940 compare (const void *a_arg, const void *b_arg) |
|
941 { |
|
942 double a = *(static_cast<const double *> (a_arg)); |
|
943 double b = *(static_cast<const double *> (b_arg)); |
|
944 |
|
945 return a > b ? 1 : (a < b) ? -1 : 0; |
|
946 } |
|
947 |
|
948 static Matrix |
|
949 maybe_set_children (const Matrix& kids, const octave_value& val) |
|
950 { |
|
951 const Matrix new_kids = val.matrix_value (); |
|
952 |
|
953 bool ok = true; |
|
954 |
|
955 if (! error_state) |
|
956 { |
|
957 if (kids.numel () == new_kids.numel ()) |
|
958 { |
|
959 Matrix t1 = kids; |
|
960 Matrix t2 = new_kids; |
|
961 |
|
962 t1.qsort (compare); |
|
963 t2.qsort (compare); |
|
964 |
|
965 if (t1 != t2) |
|
966 ok = false; |
|
967 } else |
|
968 ok = false; |
|
969 |
|
970 if (! ok) |
|
971 error ("set: new children must be a permutation of existing children"); |
|
972 } |
|
973 else |
|
974 { |
|
975 ok = false; |
|
976 error ("set: expecting children to be array of graphics handles"); |
|
977 } |
|
978 |
|
979 return ok ? new_kids : kids; |
|
980 } |
|
981 |
|
982 class base_properties |
|
983 { |
|
984 public: |
|
985 base_properties (const std::string& t = "unknown", |
|
986 const graphics_handle& mh = octave_NaN, |
|
987 const graphics_handle& p = octave_NaN) |
|
988 : type (t), __myhandle__ (mh), parent (p), children () { } |
|
989 |
|
990 virtual ~base_properties (void) { } |
|
991 |
|
992 virtual std::string graphics_object_name (void) const = 0; |
|
993 |
|
994 void override_defaults (base_graphics_object& obj) |
|
995 { |
|
996 graphics_object parent_obj = gh_manager::get_object (parent); |
|
997 parent_obj.override_defaults (obj); |
|
998 } |
|
999 |
|
1000 // Look through DEFAULTS for properties with given CLASS_NAME, and |
|
1001 // apply them to the current object with set (virtual method). |
|
1002 |
|
1003 void set_from_list (base_graphics_object& obj, property_list& defaults) |
|
1004 { |
|
1005 std::string go_name = graphics_object_name (); |
|
1006 |
|
1007 property_list::plist_map_const_iterator p = defaults.find (go_name); |
|
1008 |
|
1009 if (p != defaults.end ()) |
|
1010 { |
|
1011 const property_list::pval_map_type pval_map = p->second; |
|
1012 |
|
1013 for (property_list::pval_map_const_iterator q = pval_map.begin (); |
|
1014 q != pval_map.end (); |
|
1015 q++) |
|
1016 { |
|
1017 std::string pname = q->first; |
|
1018 |
|
1019 obj.set (pname, q->second); |
|
1020 |
|
1021 if (error_state) |
|
1022 { |
|
1023 error ("error setting default property %s", pname.c_str ()); |
|
1024 break; |
|
1025 } |
|
1026 } |
|
1027 } |
|
1028 } |
|
1029 |
|
1030 virtual void set (const property_name& name, const octave_value& val) = 0; |
|
1031 |
|
1032 graphics_handle get_parent (void) const { return parent; } |
|
1033 |
|
1034 void remove_child (const graphics_handle& h) |
|
1035 { |
|
1036 octave_idx_type k = -1; |
|
1037 octave_idx_type n = children.numel (); |
|
1038 for (octave_idx_type i = 0; i < n; i++) |
|
1039 { |
|
1040 if (h == children(i)) |
|
1041 { |
|
1042 k = i; |
|
1043 break; |
|
1044 } |
|
1045 } |
|
1046 |
|
1047 if (k >= 0) |
|
1048 { |
|
1049 Matrix new_kids (1, n-1); |
|
1050 octave_idx_type j = 0; |
|
1051 for (octave_idx_type i = 0; i < n; i++) |
|
1052 { |
|
1053 if (i != k) |
|
1054 new_kids(j++) = children(i); |
|
1055 } |
|
1056 children = new_kids; |
|
1057 } |
|
1058 } |
|
1059 |
|
1060 void adopt (const graphics_handle& h) |
|
1061 { |
|
1062 octave_idx_type n = children.numel (); |
|
1063 children.resize (1, n+1); |
|
1064 children(n) = h; |
|
1065 } |
|
1066 |
|
1067 void set_parent (const octave_value& val) |
|
1068 { |
|
1069 double tmp = val.double_value (); |
|
1070 |
|
1071 graphics_handle new_parent = octave_NaN; |
|
1072 |
|
1073 if (! error_state) |
|
1074 { |
|
1075 new_parent = gh_manager::lookup (tmp); |
|
1076 |
|
1077 if (! xisnan (new_parent)) |
|
1078 { |
|
1079 graphics_object parent_obj = gh_manager::get_object (parent); |
|
1080 |
|
1081 parent_obj.remove_child (__myhandle__); |
|
1082 |
|
1083 parent = new_parent; |
|
1084 |
|
1085 ::adopt (parent, __myhandle__); |
|
1086 } |
|
1087 else |
|
1088 error ("set: invalid graphics handle (= %g) for parent", tmp); |
|
1089 } |
|
1090 else |
|
1091 error ("set: expecting parent to be a graphics handle"); |
|
1092 } |
|
1093 |
|
1094 void reparent (const graphics_handle& new_parent) { parent = new_parent; } |
|
1095 |
|
1096 virtual void delete_children (void) |
|
1097 { |
|
1098 octave_idx_type n = children.numel (); |
|
1099 |
|
1100 for (octave_idx_type i = 0; i < n; i++) |
|
1101 gh_manager::free (children(i)); |
|
1102 } |
|
1103 |
|
1104 protected: |
|
1105 std::string type; |
|
1106 graphics_handle __myhandle__; |
|
1107 graphics_handle parent; |
|
1108 Matrix children; |
|
1109 }; |
|
1110 |
|
1111 // --------------------------------------------------------------------- |
|
1112 |
|
1113 class root_figure : public base_graphics_object |
|
1114 { |
|
1115 public: |
|
1116 class root_figure_properties : public base_properties |
|
1117 { |
|
1118 public: |
|
1119 root_figure_properties (void) |
|
1120 : base_properties ("root figure", 0, octave_NaN), |
|
1121 currentfigure (octave_NaN), |
|
1122 visible ("on") |
|
1123 { } |
|
1124 |
|
1125 ~root_figure_properties (void) { } |
|
1126 |
|
1127 void set (const property_name& name, const octave_value& val) |
|
1128 { |
|
1129 if (name.compare ("currentfigure")) |
|
1130 { |
|
1131 octave_value tval = empty_to_nan (val); |
|
1132 |
|
1133 if (is_handle (tval)) |
|
1134 { |
|
1135 currentfigure = tval.double_value (); |
|
1136 |
|
1137 gh_manager::push_figure (currentfigure); |
|
1138 } |
|
1139 else |
|
1140 gripe_set_invalid ("currentfigure"); |
|
1141 } |
|
1142 else if (name.compare ("children")) |
|
1143 children = maybe_set_children (children, val); |
|
1144 else if (name.compare ("visible")) |
|
1145 visible = val; |
|
1146 else |
|
1147 warning ("set: invalid property `%s'", name.c_str ()); |
|
1148 } |
|
1149 |
|
1150 octave_value get (void) const |
|
1151 { |
|
1152 Octave_map m; |
|
1153 |
|
1154 m.assign ("type", type); |
|
1155 m.assign ("currentfigure", nan_to_empty (currentfigure)); |
|
1156 m.assign ("children", children); |
|
1157 m.assign ("visible", visible); |
|
1158 |
|
1159 return m; |
|
1160 } |
|
1161 |
|
1162 octave_value get (const property_name& name) const |
|
1163 { |
|
1164 octave_value retval; |
|
1165 |
|
1166 if (name.compare ("type")) |
|
1167 retval = type; |
|
1168 else if (name.compare ("currentfigure")) |
|
1169 retval = nan_to_empty (currentfigure); |
|
1170 else if (name.compare ("children")) |
|
1171 retval = children; |
|
1172 else if (name.compare ("visible")) |
|
1173 retval = visible; |
|
1174 else |
|
1175 warning ("get: invalid property `%s'", name.c_str ()); |
|
1176 |
|
1177 return retval; |
|
1178 } |
|
1179 |
|
1180 std::string graphics_object_name (void) const { return go_name; } |
|
1181 |
|
1182 private: |
|
1183 graphics_handle currentfigure; |
|
1184 octave_value visible; |
|
1185 |
|
1186 static std::string go_name; |
|
1187 }; |
|
1188 |
|
1189 root_figure_properties properties; |
|
1190 |
|
1191 public: |
|
1192 |
|
1193 root_figure (void) : properties (), default_properties () { } |
|
1194 |
|
1195 ~root_figure (void) { properties.delete_children (); } |
|
1196 |
|
1197 std::string type (void) const { return properties.graphics_object_name (); } |
|
1198 |
|
1199 void override_defaults (base_graphics_object& obj) |
|
1200 { |
|
1201 // Now override with our defaults. If the default_properties |
|
1202 // list includes the properties for all defaults (line, |
|
1203 // surface, etc.) then we don't have to know the type of OBJ |
|
1204 // here, we just call its set function and let it decide which |
|
1205 // properties from the list to use. |
|
1206 obj.set_from_list (default_properties); |
|
1207 } |
|
1208 |
|
1209 void set_from_list (property_list& plist) |
|
1210 { |
|
1211 properties.set_from_list (*this, plist); |
|
1212 } |
|
1213 |
|
1214 void set (const property_name& name, const octave_value& value) |
|
1215 { |
|
1216 if (name.compare ("default", 7)) |
|
1217 // strip "default", pass rest to function that will |
|
1218 // parse the remainder and add the element to the |
|
1219 // default_properties map. |
|
1220 default_properties.set (name.substr (7), value); |
|
1221 else |
|
1222 properties.set (name, value); |
|
1223 } |
|
1224 |
|
1225 octave_value get (void) const |
|
1226 { |
|
1227 return properties.get (); |
|
1228 } |
|
1229 |
|
1230 octave_value get (const property_name& name) const |
|
1231 { |
|
1232 octave_value retval; |
|
1233 |
|
1234 if (name.compare ("default", 7)) |
|
1235 return get_default (name.substr (7)); |
|
1236 else if (name.compare ("factory", 7)) |
|
1237 return get_factory_default (name.substr (7)); |
|
1238 else |
|
1239 retval = properties.get (name); |
|
1240 |
|
1241 return retval; |
|
1242 } |
|
1243 |
|
1244 octave_value get_default (const property_name& name) const |
|
1245 { |
|
1246 octave_value retval = default_properties.lookup (name); |
|
1247 |
|
1248 if (retval.is_undefined ()) |
|
1249 error ("get: invalid default property `%s'", name.c_str ()); |
|
1250 |
|
1251 return retval; |
|
1252 } |
|
1253 |
|
1254 octave_value get_factory_default (const property_name& name) const |
|
1255 { |
|
1256 octave_value retval = factory_properties.lookup (name); |
|
1257 |
|
1258 if (retval.is_undefined ()) |
|
1259 error ("get: invalid factory default property `%s'", name.c_str ()); |
|
1260 |
|
1261 return retval; |
|
1262 } |
|
1263 |
|
1264 octave_value get_defaults (void) const |
|
1265 { |
|
1266 return default_properties.as_struct ("default"); |
|
1267 } |
|
1268 |
|
1269 octave_value get_factory_defaults (void) const |
|
1270 { |
|
1271 return factory_properties.as_struct ("factory"); |
|
1272 } |
|
1273 |
|
1274 graphics_handle get_parent (void) const { return properties.get_parent (); } |
|
1275 |
|
1276 void remove_child (const graphics_handle& h) { properties.remove_child (h); } |
|
1277 |
|
1278 void adopt (const graphics_handle& h) { properties.adopt (h); } |
|
1279 |
|
1280 void reparent (const graphics_handle& np) { properties.reparent (np); } |
|
1281 |
|
1282 bool valid_object (void) const { return true; } |
|
1283 |
|
1284 private: |
|
1285 property_list default_properties; |
|
1286 |
|
1287 static property_list factory_properties; |
|
1288 |
|
1289 static property_list::plist_map_type init_factory_properties (void); |
|
1290 }; |
|
1291 |
|
1292 property_list |
|
1293 root_figure::factory_properties = root_figure::init_factory_properties (); |
|
1294 |
|
1295 std::string root_figure::root_figure_properties::go_name ("root figure"); |
|
1296 |
|
1297 // --------------------------------------------------------------------- |
|
1298 |
|
1299 class figure : public base_graphics_object |
|
1300 { |
|
1301 public: |
|
1302 class figure_properties : public base_properties |
|
1303 { |
|
1304 public: |
|
1305 figure_properties (const graphics_handle& mh, const graphics_handle& p) |
|
1306 : base_properties (go_name, mh, p), |
|
1307 __plot_stream__ (Matrix ()), |
|
1308 nextplot ("replace"), |
|
1309 closerequestfcn (make_fcn_handle ("closereq")), |
|
1310 currentaxes (octave_NaN), |
|
1311 colormap (), |
|
1312 visible ("on"), |
|
1313 paperorientation ("portrait") |
|
1314 { } |
|
1315 |
|
1316 ~figure_properties (void) { } |
|
1317 |
|
1318 void set (const property_name& name, const octave_value& val) |
|
1319 { |
|
1320 if (name.compare ("children")) |
|
1321 children = maybe_set_children (children, val); |
|
1322 else if (name.compare ("__plot_stream__")) |
|
1323 __plot_stream__ = val; |
|
1324 else if (name.compare ("nextplot")) |
|
1325 nextplot = val; |
|
1326 else if (name.compare ("closerequestfcn")) |
|
1327 closerequestfcn = val; |
|
1328 else if (name.compare ("currentaxes")) |
|
1329 { |
|
1330 octave_value tval = empty_to_nan (val); |
|
1331 |
|
1332 if (is_handle (tval)) |
|
1333 currentaxes = tval.double_value (); |
|
1334 else |
|
1335 gripe_set_invalid ("currentaxes"); |
|
1336 } |
|
1337 else if (name.compare ("colormap")) |
|
1338 colormap = colormap_property (val); |
|
1339 else if (name.compare ("visible")) |
|
1340 visible = val; |
|
1341 else if (name.compare ("paperorientation")) |
|
1342 paperorientation = val; |
|
1343 else |
|
1344 warning ("set: invalid property `%s'", name.c_str ()); |
|
1345 } |
|
1346 |
|
1347 octave_value get (void) const |
|
1348 { |
|
1349 Octave_map m; |
|
1350 |
|
1351 m.assign ("type", type); |
|
1352 m.assign ("parent", parent); |
|
1353 m.assign ("children", children); |
|
1354 m.assign ("__plot_stream__", __plot_stream__); |
|
1355 m.assign ("nextplot", nextplot); |
|
1356 m.assign ("closerequestfcn", closerequestfcn); |
|
1357 m.assign ("currentaxes", nan_to_empty (currentaxes)); |
|
1358 m.assign ("colormap", colormap); |
|
1359 m.assign ("visible", visible); |
|
1360 m.assign ("paperorientation", paperorientation); |
|
1361 |
|
1362 return m; |
|
1363 } |
|
1364 |
|
1365 octave_value get (const property_name& name) const |
|
1366 { |
|
1367 octave_value retval; |
|
1368 |
|
1369 if (name.compare ("type")) |
|
1370 retval = type; |
|
1371 else if (name.compare ("parent")) |
|
1372 retval = parent; |
|
1373 else if (name.compare ("children")) |
|
1374 retval = children; |
|
1375 else if (name.compare ("__plot_stream__")) |
|
1376 retval = __plot_stream__; |
|
1377 else if (name.compare ("nextplot")) |
|
1378 retval = nextplot; |
|
1379 else if (name.compare ("closerequestfcn")) |
|
1380 retval = closerequestfcn; |
|
1381 else if (name.compare ("currentaxes")) |
|
1382 retval = nan_to_empty (currentaxes); |
|
1383 else if (name.compare ("colormap")) |
|
1384 retval = colormap; |
|
1385 else if (name.compare ("visible")) |
|
1386 retval = visible; |
|
1387 else if (name.compare ("paperorientation")) |
|
1388 retval = paperorientation; |
|
1389 else |
|
1390 warning ("get: invalid property `%s'", name.c_str ()); |
|
1391 |
|
1392 return retval; |
|
1393 } |
|
1394 |
|
1395 void close (void) |
|
1396 { |
|
1397 if (! __plot_stream__.is_empty ()) |
|
1398 { |
|
1399 octave_value_list args; |
|
1400 args(1) = "\nquit;\n"; |
|
1401 args(0) = __plot_stream__; |
|
1402 feval ("fputs", args); |
|
1403 args.resize (1); |
|
1404 feval ("fflush", args); |
|
1405 feval ("pclose", args); |
|
1406 } |
|
1407 |
|
1408 gh_manager::pop_figure (__myhandle__); |
|
1409 |
|
1410 xset (0, "currentfigure", gh_manager::current_figure ()); |
|
1411 } |
|
1412 |
|
1413 std::string graphics_object_name (void) const { return go_name; } |
|
1414 |
|
1415 static property_list::pval_map_type factory_defaults (void) |
|
1416 { |
|
1417 property_list::pval_map_type m; |
|
1418 |
|
1419 m["nextplot"] = "replace"; |
|
1420 // m["closerequestfcn"] = make_fcn_handle ("closereq"); |
|
1421 m["colormap"] = colormap_property (); |
|
1422 m["visible"] = "on"; |
|
1423 m["paperorientation"] = "portrait"; |
|
1424 |
|
1425 return m; |
|
1426 } |
|
1427 |
|
1428 private: |
|
1429 octave_value __plot_stream__; |
|
1430 octave_value nextplot; |
|
1431 octave_value closerequestfcn; |
|
1432 graphics_handle currentaxes; |
|
1433 colormap_property colormap; |
|
1434 octave_value visible; |
|
1435 octave_value paperorientation; |
|
1436 |
|
1437 static std::string go_name; |
|
1438 }; |
|
1439 |
|
1440 figure_properties properties; |
|
1441 |
|
1442 public: |
|
1443 figure (const graphics_handle& mh, const graphics_handle& p) |
|
1444 : base_graphics_object (), properties (mh, p), default_properties () |
|
1445 { |
|
1446 properties.override_defaults (*this); |
|
1447 } |
|
1448 |
|
1449 ~figure (void) |
|
1450 { |
|
1451 properties.delete_children (); |
|
1452 properties.close (); |
|
1453 } |
|
1454 |
|
1455 std::string type (void) const { return properties.graphics_object_name (); } |
|
1456 |
|
1457 void override_defaults (base_graphics_object& obj) |
|
1458 { |
|
1459 // Allow parent (root figure) to override first (properties knows how |
|
1460 // to find the parent object). |
|
1461 properties.override_defaults (obj); |
|
1462 |
|
1463 // Now override with our defaults. If the default_properties |
|
1464 // list includes the properties for all defaults (line, |
|
1465 // surface, etc.) then we don't have to know the type of OBJ |
|
1466 // here, we just call its set function and let it decide which |
|
1467 // properties from the list to use. |
|
1468 obj.set_from_list (default_properties); |
|
1469 } |
|
1470 |
|
1471 void set_from_list (property_list& plist) |
|
1472 { |
|
1473 properties.set_from_list (*this, plist); |
|
1474 } |
|
1475 |
|
1476 void set (const property_name& name, const octave_value& value) |
|
1477 { |
|
1478 if (name.compare ("default", 7)) |
|
1479 // strip "default", pass rest to function that will |
|
1480 // parse the remainder and add the element to the |
|
1481 // default_properties map. |
|
1482 default_properties.set (name.substr (7), value); |
|
1483 else |
|
1484 properties.set (name, value); |
|
1485 } |
|
1486 |
|
1487 octave_value get (void) const |
|
1488 { |
|
1489 return properties.get (); |
|
1490 } |
|
1491 |
|
1492 octave_value get (const property_name& name) const |
|
1493 { |
|
1494 octave_value retval; |
|
1495 |
|
1496 if (name.compare ("default", 7)) |
|
1497 retval = get_default (name.substr (7)); |
|
1498 else |
|
1499 retval = properties.get (name); |
|
1500 |
|
1501 return retval; |
|
1502 } |
|
1503 |
|
1504 octave_value get_default (const property_name& name) const |
|
1505 { |
|
1506 octave_value retval = default_properties.lookup (name); |
|
1507 |
|
1508 if (retval.is_undefined ()) |
|
1509 { |
|
1510 graphics_handle parent = get_parent (); |
|
1511 graphics_object parent_obj = gh_manager::get_object (parent); |
|
1512 |
|
1513 retval = parent_obj.get_default (name); |
|
1514 } |
|
1515 |
|
1516 return retval; |
|
1517 } |
|
1518 |
|
1519 octave_value get_defaults (void) const |
|
1520 { |
|
1521 return default_properties.as_struct ("default"); |
|
1522 } |
|
1523 |
|
1524 graphics_handle get_parent (void) const { return properties.get_parent (); } |
|
1525 |
|
1526 void remove_child (const graphics_handle& h) { properties.remove_child (h); } |
|
1527 |
|
1528 void adopt (const graphics_handle& h) { properties.adopt (h); } |
|
1529 |
|
1530 void reparent (const graphics_handle& np) { properties.reparent (np); } |
|
1531 |
|
1532 bool valid_object (void) const { return true; } |
|
1533 |
|
1534 private: |
|
1535 property_list default_properties; |
|
1536 }; |
|
1537 |
|
1538 std::string figure::figure_properties::go_name ("figure"); |
|
1539 |
|
1540 // --------------------------------------------------------------------- |
|
1541 |
|
1542 class axes : public base_graphics_object |
|
1543 { |
|
1544 public: |
|
1545 class axes_properties : public base_properties |
|
1546 { |
|
1547 public: |
|
1548 axes_properties (const graphics_handle& mh, const graphics_handle& p) |
|
1549 : base_properties (go_name, mh, p), |
|
1550 position (Matrix ()), |
|
1551 title (octave_NaN), |
|
1552 box ("on"), |
|
1553 key ("off"), |
|
1554 keybox ("off"), |
|
1555 keypos (1), |
|
1556 dataaspectratio (Matrix (1, 3, 1.0)), |
|
1557 dataaspectratiomode ("auto"), |
|
1558 xlim (), |
|
1559 ylim (), |
|
1560 zlim (), |
|
1561 xlimmode ("auto"), |
|
1562 ylimmode ("auto"), |
|
1563 zlimmode ("auto"), |
|
1564 xlabel (octave_NaN), |
|
1565 ylabel (octave_NaN), |
|
1566 zlabel (octave_NaN), |
|
1567 xgrid ("off"), |
|
1568 ygrid ("off"), |
|
1569 zgrid ("off"), |
|
1570 xminorgrid ("off"), |
|
1571 yminorgrid ("off"), |
|
1572 zminorgrid ("off"), |
|
1573 xtick (Matrix ()), |
|
1574 ytick (Matrix ()), |
|
1575 ztick (Matrix ()), |
|
1576 xtickmode ("auto"), |
|
1577 ytickmode ("auto"), |
|
1578 ztickmode ("auto"), |
|
1579 xticklabel (""), |
|
1580 yticklabel (""), |
|
1581 zticklabel (""), |
|
1582 xticklabelmode ("auto"), |
|
1583 yticklabelmode ("auto"), |
|
1584 zticklabelmode ("auto"), |
|
1585 xscale ("linear"), |
|
1586 yscale ("linear"), |
|
1587 zscale ("linear"), |
|
1588 xdir ("normal"), |
|
1589 ydir ("normal"), |
|
1590 zdir ("normal"), |
|
1591 view (), |
|
1592 nextplot ("replace"), |
|
1593 outerposition () |
|
1594 { |
|
1595 Matrix tlim (1, 2, 0.0); |
|
1596 tlim(1) = 1; |
|
1597 xlim = tlim; |
|
1598 ylim = tlim; |
|
1599 zlim = tlim; |
|
1600 |
|
1601 Matrix tview (1, 2, 0.0); |
|
1602 tview(1) = 90; |
|
1603 view = tview; |
|
1604 |
|
1605 Matrix touterposition (1, 4, 0.0); |
|
1606 touterposition(2) = 1; |
|
1607 touterposition(3) = 1; |
|
1608 outerposition = touterposition; |
|
1609 } |
|
1610 |
|
1611 ~axes_properties (void) { } |
|
1612 |
|
1613 void set (const property_name& name, const octave_value& val) |
|
1614 { |
|
1615 if (name.compare ("parent")) |
|
1616 set_parent (val); |
|
1617 else if (name.compare ("children")) |
|
1618 children = maybe_set_children (children, val); |
|
1619 else if (name.compare ("position")) |
|
1620 position = val; |
|
1621 else if (name.compare ("title")) |
|
1622 { |
|
1623 graphics_handle h = ::reparent (val, "set", "title", |
|
1624 __myhandle__, false); |
|
1625 if (! error_state) |
|
1626 { |
|
1627 if (! xisnan (title)) |
|
1628 gh_manager::free (title); |
|
1629 |
|
1630 title = h; |
|
1631 } |
|
1632 } |
|
1633 else if (name.compare ("box")) |
|
1634 box = val; |
|
1635 else if (name.compare ("key")) |
|
1636 key = val; |
|
1637 else if (name.compare ("keybox")) |
|
1638 keybox = val; |
|
1639 else if (name.compare ("keypos")) |
|
1640 keypos = val; |
|
1641 else if (name.compare ("dataaspectratio")) |
|
1642 { |
|
1643 dataaspectratio = val; |
|
1644 dataaspectratiomode = "manual"; |
|
1645 } |
|
1646 else if (name.compare ("dataaspectratiomode")) |
|
1647 dataaspectratiomode = val; |
|
1648 else if (name.compare ("xlim")) |
|
1649 { |
|
1650 xlim = val; |
|
1651 xlimmode = "manual"; |
|
1652 } |
|
1653 else if (name.compare ("ylim")) |
|
1654 { |
|
1655 ylim = val; |
|
1656 ylimmode = "manual"; |
|
1657 } |
|
1658 else if (name.compare ("zlim")) |
|
1659 { |
|
1660 zlim = val; |
|
1661 zlimmode = "manual"; |
|
1662 } |
|
1663 else if (name.compare ("xlimmode")) |
|
1664 xlimmode = val; |
|
1665 else if (name.compare ("ylimmode")) |
|
1666 ylimmode = val; |
|
1667 else if (name.compare ("zlimmode")) |
|
1668 zlimmode = val; |
|
1669 else if (name.compare ("xlabel")) |
|
1670 { |
|
1671 graphics_handle h = ::reparent (val, "set", "xlabel", |
|
1672 __myhandle__, false); |
|
1673 if (! error_state) |
|
1674 { |
|
1675 if (! xisnan (xlabel)) |
|
1676 gh_manager::free (xlabel); |
|
1677 |
|
1678 xlabel = h; |
|
1679 } |
|
1680 } |
|
1681 else if (name.compare ("ylabel")) |
|
1682 { |
|
1683 graphics_handle h = ::reparent (val, "set", "ylabel", |
|
1684 __myhandle__, false); |
|
1685 if (! error_state) |
|
1686 { |
|
1687 if (! xisnan (ylabel)) |
|
1688 gh_manager::free (ylabel); |
|
1689 |
|
1690 ylabel = h; |
|
1691 } |
|
1692 } |
|
1693 else if (name.compare ("zlabel")) |
|
1694 { |
|
1695 graphics_handle h = ::reparent (val, "set", "zlabel", |
|
1696 __myhandle__, false); |
|
1697 if (! error_state) |
|
1698 { |
|
1699 if (! xisnan (zlabel)) |
|
1700 gh_manager::free (zlabel); |
|
1701 |
|
1702 zlabel = h; |
|
1703 } |
|
1704 } |
|
1705 else if (name.compare ("xgrid")) |
|
1706 xgrid = val; |
|
1707 else if (name.compare ("ygrid")) |
|
1708 ygrid = val; |
|
1709 else if (name.compare ("zgrid")) |
|
1710 zgrid = val; |
|
1711 else if (name.compare ("xminorgrid")) |
|
1712 xminorgrid = val; |
|
1713 else if (name.compare ("yminorgrid")) |
|
1714 yminorgrid = val; |
|
1715 else if (name.compare ("zminorgrid")) |
|
1716 zminorgrid = val; |
|
1717 else if (name.compare ("xtick")) |
|
1718 { |
|
1719 xtick = val; |
|
1720 xtickmode = "manual"; |
|
1721 } |
|
1722 else if (name.compare ("ytick")) |
|
1723 { |
|
1724 ytick = val; |
|
1725 ytickmode = "manual"; |
|
1726 } |
|
1727 else if (name.compare ("ztick")) |
|
1728 { |
|
1729 ztick = val; |
|
1730 ztickmode = "manual"; |
|
1731 } |
|
1732 else if (name.compare ("xtickmode")) |
|
1733 xtickmode = val; |
|
1734 else if (name.compare ("ytickmode")) |
|
1735 ytickmode = val; |
|
1736 else if (name.compare ("ztickmode")) |
|
1737 ztickmode = val; |
|
1738 else if (name.compare ("xticklabel")) |
|
1739 { |
|
1740 xticklabel = val; |
|
1741 xticklabelmode = "manual"; |
|
1742 } |
|
1743 else if (name.compare ("yticklabel")) |
|
1744 { |
|
1745 yticklabel = val; |
|
1746 yticklabelmode = "manual"; |
|
1747 } |
|
1748 else if (name.compare ("zticklabel")) |
|
1749 { |
|
1750 zticklabel = val; |
|
1751 zticklabelmode = "manual"; |
|
1752 } |
|
1753 else if (name.compare ("xticklabelmode")) |
|
1754 xticklabelmode = val; |
|
1755 else if (name.compare ("yticklabelmode")) |
|
1756 yticklabelmode = val; |
|
1757 else if (name.compare ("zticklabelmode")) |
|
1758 zticklabelmode = val; |
|
1759 else if (name.compare ("xscale")) |
|
1760 xscale = val; |
|
1761 else if (name.compare ("yscale")) |
|
1762 yscale = val; |
|
1763 else if (name.compare ("zscale")) |
|
1764 zscale = val; |
|
1765 else if (name.compare ("xdir")) |
|
1766 xdir = val; |
|
1767 else if (name.compare ("ydir")) |
|
1768 ydir = val; |
|
1769 else if (name.compare ("zdir")) |
|
1770 zdir = val; |
|
1771 else if (name.compare ("view")) |
|
1772 view = val; |
|
1773 else if (name.compare ("nextplot")) |
|
1774 nextplot = val; |
|
1775 else if (name.compare ("outerposition")) |
|
1776 outerposition = val; |
|
1777 else |
|
1778 warning ("set: invalid property `%s'", name.c_str ()); |
|
1779 } |
|
1780 |
|
1781 void set_defaults (base_graphics_object& obj, const std::string& mode) |
|
1782 { |
|
1783 position = Matrix (); |
|
1784 title = octave_NaN; |
|
1785 box = "on"; |
|
1786 key = "off"; |
|
1787 keybox = "off"; |
|
1788 keypos = 1; |
|
1789 dataaspectratio = Matrix (1, 3, 1.0); |
|
1790 dataaspectratiomode = "auto"; |
|
1791 |
|
1792 Matrix tlim (1, 2, 0.0); |
|
1793 tlim(1) = 1; |
|
1794 xlim = tlim; |
|
1795 ylim = tlim; |
|
1796 zlim = tlim; |
|
1797 |
|
1798 xlimmode = "auto"; |
|
1799 ylimmode = "auto"; |
|
1800 zlimmode = "auto"; |
|
1801 xlabel = octave_NaN; |
|
1802 ylabel = octave_NaN; |
|
1803 zlabel = octave_NaN; |
|
1804 xgrid = "off"; |
|
1805 ygrid = "off"; |
|
1806 zgrid = "off"; |
|
1807 xminorgrid = "off"; |
|
1808 yminorgrid = "off"; |
|
1809 zminorgrid = "off"; |
|
1810 xtick = Matrix (); |
|
1811 ytick = Matrix (); |
|
1812 ztick = Matrix (); |
|
1813 xtickmode = "auto"; |
|
1814 ytickmode = "auto"; |
|
1815 ztickmode = "auto"; |
|
1816 xticklabel = ""; |
|
1817 yticklabel = ""; |
|
1818 zticklabel = ""; |
|
1819 xticklabelmode = "auto"; |
|
1820 yticklabelmode = "auto"; |
|
1821 zticklabelmode = "auto"; |
|
1822 xscale = "linear"; |
|
1823 yscale = "linear"; |
|
1824 zscale = "linear"; |
|
1825 xdir = "normal"; |
|
1826 ydir = "normal"; |
|
1827 zdir = "normal"; |
|
1828 |
|
1829 Matrix tview (1, 2, 0.0); |
|
1830 tview(1) = 90; |
|
1831 view = tview; |
|
1832 |
|
1833 nextplot = "replace"; |
|
1834 |
|
1835 // FIXME -- this is not quite right; we should preserve |
|
1836 // "position" and "units". |
|
1837 |
|
1838 if (mode == "replace") |
|
1839 { |
|
1840 Matrix touterposition (1, 4, 0.0); |
|
1841 touterposition(2) = 1; |
|
1842 touterposition(3) = 1; |
|
1843 outerposition = touterposition; |
|
1844 } |
|
1845 |
|
1846 delete_children (); |
|
1847 |
|
1848 children = Matrix (); |
|
1849 |
|
1850 override_defaults (obj); |
|
1851 } |
|
1852 |
|
1853 octave_value get (void) const |
|
1854 { |
|
1855 Octave_map m; |
|
1856 |
|
1857 if (xisnan (title)) |
|
1858 title = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1859 |
|
1860 if (xisnan (xlabel)) |
|
1861 xlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1862 |
|
1863 if (xisnan (ylabel)) |
|
1864 ylabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1865 |
|
1866 if (xisnan (zlabel)) |
|
1867 zlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1868 |
|
1869 m.assign ("type", type); |
|
1870 m.assign ("parent", parent); |
|
1871 m.assign ("children", children); |
|
1872 m.assign ("position", position); |
|
1873 m.assign ("title", title); |
|
1874 m.assign ("box", box); |
|
1875 m.assign ("key", key); |
|
1876 m.assign ("keybox", keybox); |
|
1877 m.assign ("keypos", keypos); |
|
1878 m.assign ("dataaspectratio", dataaspectratio); |
|
1879 m.assign ("dataaspectratiomode", dataaspectratiomode); |
|
1880 m.assign ("xlim", xlim); |
|
1881 m.assign ("ylim", ylim); |
|
1882 m.assign ("zlim", zlim); |
|
1883 m.assign ("xlimmode", xlimmode); |
|
1884 m.assign ("ylimmode", ylimmode); |
|
1885 m.assign ("zlimmode", zlimmode); |
|
1886 m.assign ("xlabel", xlabel); |
|
1887 m.assign ("ylabel", ylabel); |
|
1888 m.assign ("zlabel", zlabel); |
|
1889 m.assign ("xgrid", xgrid); |
|
1890 m.assign ("ygrid", ygrid); |
|
1891 m.assign ("zgrid", zgrid); |
|
1892 m.assign ("xminorgrid", xminorgrid); |
|
1893 m.assign ("yminorgrid", yminorgrid); |
|
1894 m.assign ("zminorgrid", zminorgrid); |
|
1895 m.assign ("xtick", xtick); |
|
1896 m.assign ("ytick", ytick); |
|
1897 m.assign ("ztick", ztick); |
|
1898 m.assign ("xtickmode", xtickmode); |
|
1899 m.assign ("ytickmode", ytickmode); |
|
1900 m.assign ("ztickmode", ztickmode); |
|
1901 m.assign ("xticklabel", xticklabel); |
|
1902 m.assign ("yticklabel", yticklabel); |
|
1903 m.assign ("zticklabel", zticklabel); |
|
1904 m.assign ("xticklabelmode", xticklabelmode); |
|
1905 m.assign ("yticklabelmode", yticklabelmode); |
|
1906 m.assign ("zticklabelmode", zticklabelmode); |
|
1907 m.assign ("xscale", xscale); |
|
1908 m.assign ("yscale", yscale); |
|
1909 m.assign ("zscale", zscale); |
|
1910 m.assign ("xdir", xdir); |
|
1911 m.assign ("ydir", ydir); |
|
1912 m.assign ("zdir", zdir); |
|
1913 m.assign ("view", view); |
|
1914 m.assign ("nextplot", nextplot); |
|
1915 m.assign ("outerposition", outerposition); |
|
1916 |
|
1917 return m; |
|
1918 } |
|
1919 |
|
1920 octave_value get (const property_name& name) const |
|
1921 { |
|
1922 octave_value retval; |
|
1923 |
|
1924 if (name.compare ("type")) |
|
1925 retval = type; |
|
1926 else if (name.compare ("parent")) |
|
1927 retval = parent; |
|
1928 else if (name.compare ("children")) |
|
1929 retval = children; |
|
1930 else if (name.compare ("position")) |
|
1931 retval = position; |
|
1932 else if (name.compare ("title")) |
|
1933 { |
|
1934 if (xisnan (title)) |
|
1935 title = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1936 |
|
1937 retval = title; |
|
1938 } |
|
1939 else if (name.compare ("box")) |
|
1940 retval = box; |
|
1941 else if (name.compare ("key")) |
|
1942 retval = key; |
|
1943 else if (name.compare ("keybox")) |
|
1944 retval = keybox; |
|
1945 else if (name.compare ("keypos")) |
|
1946 retval = keypos; |
|
1947 else if (name.compare ("dataaspectratio")) |
|
1948 retval = dataaspectratio; |
|
1949 else if (name.compare ("dataaspectratiomode")) |
|
1950 retval = dataaspectratiomode; |
|
1951 else if (name.compare ("xlim")) |
|
1952 retval = xlim; |
|
1953 else if (name.compare ("ylim")) |
|
1954 retval = ylim; |
|
1955 else if (name.compare ("zlim")) |
|
1956 retval = zlim; |
|
1957 else if (name.compare ("xlimmode")) |
|
1958 retval = xlimmode; |
|
1959 else if (name.compare ("ylimmode")) |
|
1960 retval = ylimmode; |
|
1961 else if (name.compare ("zlimmode")) |
|
1962 retval = zlimmode; |
|
1963 else if (name.compare ("xlabel")) |
|
1964 { |
|
1965 if (xisnan (xlabel)) |
|
1966 xlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1967 |
|
1968 retval = xlabel; |
|
1969 } |
|
1970 else if (name.compare ("ylabel")) |
|
1971 { |
|
1972 if (xisnan (ylabel)) |
|
1973 ylabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1974 |
|
1975 retval = ylabel; |
|
1976 } |
|
1977 else if (name.compare ("zlabel")) |
|
1978 { |
|
1979 if (xisnan (zlabel)) |
|
1980 zlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
1981 |
|
1982 retval = zlabel; |
|
1983 } |
|
1984 else if (name.compare ("xgrid")) |
|
1985 retval = xgrid; |
|
1986 else if (name.compare ("ygrid")) |
|
1987 retval = ygrid; |
|
1988 else if (name.compare ("zgrid")) |
|
1989 retval = zgrid; |
|
1990 else if (name.compare ("xminorgrid")) |
|
1991 retval = xminorgrid; |
|
1992 else if (name.compare ("yminorgrid")) |
|
1993 retval = yminorgrid; |
|
1994 else if (name.compare ("zminorgrid")) |
|
1995 retval = zminorgrid; |
|
1996 else if (name.compare ("xtick")) |
|
1997 retval = xtick; |
|
1998 else if (name.compare ("ytick")) |
|
1999 retval = ytick; |
|
2000 else if (name.compare ("ztick")) |
|
2001 retval = ztick; |
|
2002 else if (name.compare ("xtickmode")) |
|
2003 retval = xtickmode; |
|
2004 else if (name.compare ("ytickmode")) |
|
2005 retval = ytickmode; |
|
2006 else if (name.compare ("ztickmode")) |
|
2007 retval = ztickmode; |
|
2008 else if (name.compare ("xticklabel")) |
|
2009 retval = xticklabel; |
|
2010 else if (name.compare ("yticklabel")) |
|
2011 retval = yticklabel; |
|
2012 else if (name.compare ("zticklabel")) |
|
2013 retval = zticklabel; |
|
2014 else if (name.compare ("xticklabelmode")) |
|
2015 retval = xticklabelmode; |
|
2016 else if (name.compare ("yticklabelmode")) |
|
2017 retval = yticklabelmode; |
|
2018 else if (name.compare ("zticklabelmode")) |
|
2019 retval = zticklabelmode; |
|
2020 else if (name.compare ("xscale")) |
|
2021 retval = xscale; |
|
2022 else if (name.compare ("yscale")) |
|
2023 retval = yscale; |
|
2024 else if (name.compare ("zscale")) |
|
2025 retval = zscale; |
|
2026 else if (name.compare ("xdir")) |
|
2027 retval = xdir; |
|
2028 else if (name.compare ("ydir")) |
|
2029 retval = ydir; |
|
2030 else if (name.compare ("zdir")) |
|
2031 retval = zdir; |
|
2032 else if (name.compare ("view")) |
|
2033 retval = view; |
|
2034 else if (name.compare ("nextplot")) |
|
2035 retval = nextplot; |
|
2036 else if (name.compare ("outerposition")) |
|
2037 retval = outerposition; |
|
2038 else |
|
2039 warning ("get: invalid property `%s'", name.c_str ()); |
|
2040 |
|
2041 return retval; |
|
2042 } |
|
2043 |
|
2044 void remove_child (const graphics_handle& h) |
|
2045 { |
|
2046 if (! xisnan (title) && h == title) |
|
2047 title = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
2048 else if (! xisnan (xlabel) && h == xlabel) |
|
2049 xlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
2050 else if (! xisnan (ylabel) && h == ylabel) |
|
2051 ylabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
2052 else if (! xisnan (zlabel) && h == zlabel) |
|
2053 zlabel = gh_manager::make_graphics_handle ("text", __myhandle__); |
|
2054 else |
|
2055 base_properties::remove_child (h); |
|
2056 } |
|
2057 |
|
2058 void delete_children (void) |
|
2059 { |
|
2060 base_properties::delete_children (); |
|
2061 |
|
2062 if (! xisnan (title)) |
|
2063 gh_manager::free (title); |
|
2064 |
|
2065 if (! xisnan (xlabel)) |
|
2066 gh_manager::free (xlabel); |
|
2067 |
|
2068 if (! xisnan (ylabel)) |
|
2069 gh_manager::free (ylabel); |
|
2070 |
|
2071 if (! xisnan (zlabel)) |
|
2072 gh_manager::free (zlabel); |
|
2073 } |
|
2074 |
|
2075 std::string graphics_object_name (void) const { return go_name; } |
|
2076 |
|
2077 static property_list::pval_map_type factory_defaults (void) |
|
2078 { |
|
2079 property_list::pval_map_type m; |
|
2080 |
|
2081 m["position"] = Matrix (); |
|
2082 m["title"] = octave_NaN; |
|
2083 m["box"] = "on"; |
|
2084 m["key"] = "off"; |
|
2085 m["keybox"] = "off"; |
|
2086 m["keypos"] = 1; |
|
2087 m["dataaspectratio"] = Matrix (1, 3, 1.0); |
|
2088 m["dataaspectratiomode"] = "auto"; |
|
2089 |
|
2090 Matrix tlim (1, 2, 0.0); |
|
2091 tlim(1) = 1; |
|
2092 |
|
2093 m["xlim"] = tlim; |
|
2094 m["ylim"] = tlim; |
|
2095 m["zlim"] = tlim; |
|
2096 |
|
2097 m["xlimmode"] = "auto"; |
|
2098 m["ylimmode"] = "auto"; |
|
2099 m["zlimmode"] = "auto"; |
|
2100 m["xlabel"] = octave_NaN; |
|
2101 m["ylabel"] = octave_NaN; |
|
2102 m["zlabel"] = octave_NaN; |
|
2103 m["xgrid"] = "off"; |
|
2104 m["ygrid"] = "off"; |
|
2105 m["zgrid"] = "off"; |
|
2106 m["xminorgrid"] = "off"; |
|
2107 m["yminorgrid"] = "off"; |
|
2108 m["zminorgrid"] = "off"; |
|
2109 m["xtick"] = Matrix (); |
|
2110 m["ytick"] = Matrix (); |
|
2111 m["ztick"] = Matrix (); |
|
2112 m["xtickmode"] = "auto"; |
|
2113 m["ytickmode"] = "auto"; |
|
2114 m["ztickmode"] = "auto"; |
|
2115 m["xticklabel"] = ""; |
|
2116 m["yticklabel"] = ""; |
|
2117 m["zticklabel"] = ""; |
|
2118 m["xticklabelmode"] = "auto"; |
|
2119 m["yticklabelmode"] = "auto"; |
|
2120 m["zticklabelmode"] = "auto"; |
|
2121 m["xscale"] = "linear"; |
|
2122 m["yscale"] = "linear"; |
|
2123 m["zscale"] = "linear"; |
|
2124 m["xdir"] = "normal"; |
|
2125 m["ydir"] = "normal"; |
|
2126 m["zdir"] = "normal"; |
|
2127 |
|
2128 Matrix tview (1, 2, 0.0); |
|
2129 tview(1) = 90; |
|
2130 |
|
2131 m["view"] = tview; |
|
2132 |
|
2133 m["nextplot"] = "replace"; |
|
2134 |
|
2135 Matrix touterposition (1, 4, 0.0); |
|
2136 touterposition(2) = 1; |
|
2137 touterposition(3) = 1; |
|
2138 |
|
2139 m["outerposition"] = touterposition; |
|
2140 |
|
2141 return m; |
|
2142 } |
|
2143 |
|
2144 private: |
|
2145 octave_value position; |
|
2146 mutable graphics_handle title; |
|
2147 octave_value box; |
|
2148 octave_value key; |
|
2149 octave_value keybox; |
|
2150 octave_value keypos; |
|
2151 octave_value dataaspectratio; |
|
2152 octave_value dataaspectratiomode; |
|
2153 octave_value xlim; |
|
2154 octave_value ylim; |
|
2155 octave_value zlim; |
|
2156 octave_value xlimmode; |
|
2157 octave_value ylimmode; |
|
2158 octave_value zlimmode; |
|
2159 mutable graphics_handle xlabel; |
|
2160 mutable graphics_handle ylabel; |
|
2161 mutable graphics_handle zlabel; |
|
2162 octave_value xgrid; |
|
2163 octave_value ygrid; |
|
2164 octave_value zgrid; |
|
2165 octave_value xminorgrid; |
|
2166 octave_value yminorgrid; |
|
2167 octave_value zminorgrid; |
|
2168 octave_value xtick; |
|
2169 octave_value ytick; |
|
2170 octave_value ztick; |
|
2171 octave_value xtickmode; |
|
2172 octave_value ytickmode; |
|
2173 octave_value ztickmode; |
|
2174 octave_value xticklabel; |
|
2175 octave_value yticklabel; |
|
2176 octave_value zticklabel; |
|
2177 octave_value xticklabelmode; |
|
2178 octave_value yticklabelmode; |
|
2179 octave_value zticklabelmode; |
|
2180 octave_value xscale; |
|
2181 octave_value yscale; |
|
2182 octave_value zscale; |
|
2183 octave_value xdir; |
|
2184 octave_value ydir; |
|
2185 octave_value zdir; |
|
2186 octave_value view; |
|
2187 octave_value nextplot; |
|
2188 octave_value outerposition; |
|
2189 |
|
2190 static std::string go_name; |
|
2191 }; |
|
2192 |
|
2193 axes_properties properties; |
|
2194 |
|
2195 public: |
|
2196 axes (const graphics_handle& mh, const graphics_handle& p) |
|
2197 : base_graphics_object (), properties (mh, p), default_properties () |
|
2198 { |
|
2199 properties.override_defaults (*this); |
|
2200 } |
|
2201 |
|
2202 ~axes (void) { properties.delete_children (); } |
|
2203 |
|
2204 std::string type (void) const { return properties.graphics_object_name (); } |
|
2205 |
|
2206 void override_defaults (base_graphics_object& obj) |
|
2207 { |
|
2208 // Allow parent (figure) to override first (properties knows how |
|
2209 // to find the parent object). |
|
2210 properties.override_defaults (obj); |
|
2211 |
|
2212 // Now override with our defaults. If the default_properties |
|
2213 // list includes the properties for all defaults (line, |
|
2214 // surface, etc.) then we don't have to know the type of OBJ |
|
2215 // here, we just call its set function and let it decide which |
|
2216 // properties from the list to use. |
|
2217 obj.set_from_list (default_properties); |
|
2218 } |
|
2219 |
|
2220 void set_from_list (property_list& plist) |
|
2221 { |
|
2222 properties.set_from_list (*this, plist); |
|
2223 } |
|
2224 |
|
2225 void set (const property_name& name, const octave_value& value) |
|
2226 { |
|
2227 if (name.compare ("default", 7)) |
|
2228 // strip "default", pass rest to function that will |
|
2229 // parse the remainder and add the element to the |
|
2230 // default_properties map. |
|
2231 default_properties.set (name.substr (7), value); |
|
2232 else |
|
2233 properties.set (name, value); |
|
2234 } |
|
2235 |
|
2236 void set_defaults (const std::string& mode) |
|
2237 { |
|
2238 properties.set_defaults (*this, mode); |
|
2239 } |
|
2240 |
|
2241 octave_value get (void) const |
|
2242 { |
|
2243 return properties.get (); |
|
2244 } |
|
2245 |
|
2246 octave_value get (const property_name& name) const |
|
2247 { |
|
2248 octave_value retval; |
|
2249 |
|
2250 // FIXME -- finish this. |
|
2251 if (name.compare ("default", 7)) |
|
2252 retval = get_default (name.substr (7)); |
|
2253 else |
|
2254 retval = properties.get (name); |
|
2255 |
|
2256 return retval; |
|
2257 } |
|
2258 |
|
2259 octave_value get_default (const property_name& name) const |
|
2260 { |
|
2261 octave_value retval = default_properties.lookup (name); |
|
2262 |
|
2263 if (retval.is_undefined ()) |
|
2264 { |
|
2265 graphics_handle parent = get_parent (); |
|
2266 graphics_object parent_obj = gh_manager::get_object (parent); |
|
2267 |
|
2268 retval = parent_obj.get_default (name); |
|
2269 } |
|
2270 |
|
2271 return retval; |
|
2272 } |
|
2273 |
|
2274 octave_value get_defaults (void) const |
|
2275 { |
|
2276 return default_properties.as_struct ("default"); |
|
2277 } |
|
2278 |
|
2279 graphics_handle get_parent (void) const { return properties.get_parent (); } |
|
2280 |
|
2281 void remove_child (const graphics_handle& h) { properties.remove_child (h); } |
|
2282 |
|
2283 void adopt (const graphics_handle& h) { properties.adopt (h); } |
|
2284 |
|
2285 void reparent (const graphics_handle& np) { properties.reparent (np); } |
|
2286 |
|
2287 bool valid_object (void) const { return true; } |
|
2288 |
|
2289 private: |
|
2290 property_list default_properties; |
|
2291 }; |
|
2292 |
|
2293 std::string axes::axes_properties::go_name ("axes"); |
|
2294 |
|
2295 // --------------------------------------------------------------------- |
|
2296 |
|
2297 static Matrix |
|
2298 default_data (void) |
|
2299 { |
|
2300 Matrix retval (1, 2); |
|
2301 |
|
2302 retval(0) = 0; |
|
2303 retval(1) = 1; |
|
2304 |
|
2305 return retval; |
|
2306 } |
|
2307 |
|
2308 class line : public base_graphics_object |
|
2309 { |
|
2310 public: |
|
2311 class line_properties : public base_properties |
|
2312 { |
|
2313 public: |
|
2314 line_properties (const graphics_handle& mh, const graphics_handle& p) |
|
2315 : base_properties (go_name, mh, p), |
|
2316 xdata (default_data ()), |
|
2317 ydata (default_data ()), |
|
2318 zdata (Matrix ()), |
|
2319 ldata (Matrix ()), |
|
2320 udata (Matrix ()), |
|
2321 xldata (Matrix ()), |
|
2322 xudata (Matrix ()), |
|
2323 color (), |
|
2324 linestyle ("-"), |
|
2325 linewidth (0.5), |
|
2326 marker ("none"), |
|
2327 markersize (1), |
|
2328 keylabel ("") { } |
|
2329 |
|
2330 ~line_properties (void) { } |
|
2331 |
|
2332 void set (const property_name& name, const octave_value& val) |
|
2333 { |
|
2334 if (name.compare ("parent")) |
|
2335 set_parent (val); |
|
2336 else if (name.compare ("children")) |
|
2337 children = maybe_set_children (children, val); |
|
2338 else if (name.compare ("xdata")) |
|
2339 xdata = val; |
|
2340 else if (name.compare ("ydata")) |
|
2341 ydata = val; |
|
2342 else if (name.compare ("zdata")) |
|
2343 zdata = val; |
|
2344 else if (name.compare ("ldata")) |
|
2345 ldata = val; |
|
2346 else if (name.compare ("udata")) |
|
2347 udata = val; |
|
2348 else if (name.compare ("xldata")) |
|
2349 xldata = val; |
|
2350 else if (name.compare ("xudata")) |
|
2351 xudata = val; |
|
2352 else if (name.compare ("color")) |
|
2353 color = color_property (val); |
|
2354 else if (name.compare ("linestyle")) |
|
2355 linestyle = val; |
|
2356 else if (name.compare ("linewidth")) |
|
2357 linewidth = val; |
|
2358 else if (name.compare ("marker")) |
|
2359 marker = val; |
|
2360 else if (name.compare ("markersize")) |
|
2361 markersize = val; |
|
2362 else if (name.compare ("keylabel")) |
|
2363 keylabel = val; |
|
2364 else |
|
2365 warning ("set: invalid property `%s'", name.c_str ()); |
|
2366 } |
|
2367 |
|
2368 octave_value get (void) const |
|
2369 { |
|
2370 Octave_map m; |
|
2371 |
|
2372 m.assign ("type", type); |
|
2373 m.assign ("parent", parent); |
|
2374 m.assign ("children", children); |
|
2375 m.assign ("xdata", xdata); |
|
2376 m.assign ("ydata", ydata); |
|
2377 m.assign ("zdata", zdata); |
|
2378 m.assign ("ldata", ldata); |
|
2379 m.assign ("udata", udata); |
|
2380 m.assign ("xldata", xldata); |
|
2381 m.assign ("xudata", xudata); |
|
2382 m.assign ("color", color); |
|
2383 m.assign ("linestyle", linestyle); |
|
2384 m.assign ("linewidth", linewidth); |
|
2385 m.assign ("marker", marker); |
|
2386 m.assign ("markersize", markersize); |
|
2387 m.assign ("keylabel", keylabel); |
|
2388 |
|
2389 return m; |
|
2390 } |
|
2391 |
|
2392 octave_value get (const property_name& name) const |
|
2393 { |
|
2394 octave_value retval; |
|
2395 |
|
2396 if (name.compare ("type")) |
|
2397 retval = type; |
|
2398 else if (name.compare ("parent")) |
|
2399 retval = parent; |
|
2400 else if (name.compare ("children")) |
|
2401 retval = children; |
|
2402 else if (name.compare ("xdata")) |
|
2403 retval = xdata; |
|
2404 else if (name.compare ("ydata")) |
|
2405 retval = ydata; |
|
2406 else if (name.compare ("zdata")) |
|
2407 retval = zdata; |
|
2408 else if (name.compare ("ldata")) |
|
2409 retval = ldata; |
|
2410 else if (name.compare ("udata")) |
|
2411 retval = udata; |
|
2412 else if (name.compare ("xldata")) |
|
2413 retval = xldata; |
|
2414 else if (name.compare ("xudata")) |
|
2415 retval = xudata; |
|
2416 else if (name.compare ("color")) |
|
2417 retval = color; |
|
2418 else if (name.compare ("linestyle")) |
|
2419 retval = linestyle; |
|
2420 else if (name.compare ("linewidth")) |
|
2421 retval = linewidth; |
|
2422 else if (name.compare ("marker")) |
|
2423 retval = marker; |
|
2424 else if (name.compare ("markersize")) |
|
2425 retval = markersize; |
|
2426 else if (name.compare ("keylabel")) |
|
2427 retval = keylabel; |
|
2428 else |
|
2429 warning ("get: invalid property `%s'", name.c_str ()); |
|
2430 |
|
2431 return retval; |
|
2432 } |
|
2433 |
|
2434 std::string graphics_object_name (void) const { return go_name; } |
|
2435 |
|
2436 static property_list::pval_map_type factory_defaults (void) |
|
2437 { |
|
2438 property_list::pval_map_type m; |
|
2439 |
|
2440 m["xdata"] = default_data (); |
|
2441 m["ydata"] = default_data (); |
|
2442 m["zdata"] = Matrix (); |
|
2443 m["ldata"] = Matrix (); |
|
2444 m["udata"] = Matrix (); |
|
2445 m["xldata"] = Matrix (); |
|
2446 m["xudata"] = Matrix (); |
|
2447 m["color"] = color_property (); |
|
2448 m["linestyle"] = "-"; |
|
2449 m["linewidth"] = 0.5; |
|
2450 m["marker"] = "none"; |
|
2451 m["markersize"] = 1; |
|
2452 m["keylabel"] = ""; |
|
2453 |
|
2454 return m; |
|
2455 } |
|
2456 |
|
2457 private: |
|
2458 octave_value xdata; |
|
2459 octave_value ydata; |
|
2460 octave_value zdata; |
|
2461 octave_value ldata; |
|
2462 octave_value udata; |
|
2463 octave_value xldata; |
|
2464 octave_value xudata; |
|
2465 color_property color; |
|
2466 octave_value linestyle; |
|
2467 octave_value linewidth; |
|
2468 octave_value marker; |
|
2469 octave_value markersize; |
|
2470 octave_value keylabel; |
|
2471 |
|
2472 static std::string go_name; |
|
2473 }; |
|
2474 |
|
2475 line_properties properties; |
|
2476 |
|
2477 public: |
|
2478 line (const graphics_handle& mh, const graphics_handle& p) |
|
2479 : base_graphics_object (), properties (mh, p) |
|
2480 { |
|
2481 properties.override_defaults (*this); |
|
2482 } |
|
2483 |
|
2484 ~line (void) { properties.delete_children (); } |
|
2485 |
|
2486 std::string type (void) const { return properties.graphics_object_name (); } |
|
2487 |
|
2488 void override_defaults (base_graphics_object& obj) |
|
2489 { |
|
2490 // Allow parent (figure) to override first (properties knows how |
|
2491 // to find the parent object). |
|
2492 properties.override_defaults (obj); |
|
2493 } |
|
2494 |
|
2495 void set_from_list (property_list& plist) |
|
2496 { |
|
2497 properties.set_from_list (*this, plist); |
|
2498 } |
|
2499 |
|
2500 void set (const property_name& name, const octave_value& val) |
|
2501 { |
|
2502 properties.set (name, val); |
|
2503 } |
|
2504 |
|
2505 octave_value get (void) const |
|
2506 { |
|
2507 return properties.get (); |
|
2508 } |
|
2509 |
|
2510 octave_value get (const property_name& name) const |
|
2511 { |
|
2512 return properties.get (name); |
|
2513 } |
|
2514 |
|
2515 graphics_handle get_parent (void) const { return properties.get_parent (); } |
|
2516 |
|
2517 void remove_child (const graphics_handle& h) { properties.remove_child (h); } |
|
2518 |
|
2519 void adopt (const graphics_handle& h) { properties.adopt (h); } |
|
2520 |
|
2521 void reparent (const graphics_handle& h) { properties.reparent (h); } |
|
2522 |
|
2523 bool valid_object (void) const { return true; } |
|
2524 }; |
|
2525 |
|
2526 std::string line::line_properties::go_name ("line"); |
|
2527 |
|
2528 // --------------------------------------------------------------------- |
|
2529 |
|
2530 class text : public base_graphics_object |
|
2531 { |
|
2532 public: |
|
2533 class text_properties : public base_properties |
|
2534 { |
|
2535 public: |
|
2536 text_properties (const graphics_handle& mh, const graphics_handle& p) |
|
2537 : base_properties (go_name, mh, p), |
|
2538 string (""), |
|
2539 units ("data"), |
|
2540 position (Matrix (1, 3, 0.0)), |
|
2541 horizontalalignment ("left") |
|
2542 { } |
|
2543 |
|
2544 ~text_properties (void) { } |
|
2545 |
|
2546 void set (const property_name& name, const octave_value& val) |
|
2547 { |
|
2548 if (name.compare ("parent")) |
|
2549 set_parent (val); |
|
2550 else if (name.compare ("children")) |
|
2551 children = maybe_set_children (children, val); |
|
2552 else if (name.compare ("string")) |
|
2553 string = val; |
|
2554 else if (name.compare ("units")) |
|
2555 units = val; |
|
2556 else if (name.compare ("position")) |
|
2557 position = val; |
|
2558 else if (name.compare ("horizontalalignment")) |
|
2559 horizontalalignment = val; |
|
2560 else |
|
2561 warning ("set: invalid property `%s'", name.c_str ()); |
|
2562 } |
|
2563 |
|
2564 octave_value get (void) const |
|
2565 { |
|
2566 Octave_map m; |
|
2567 |
|
2568 m.assign ("type", type); |
|
2569 m.assign ("parent", parent); |
|
2570 m.assign ("children", children); |
|
2571 m.assign ("string", string); |
|
2572 m.assign ("units", units); |
|
2573 m.assign ("position", position); |
|
2574 m.assign ("horizontalalignment", horizontalalignment); |
|
2575 |
|
2576 return m; |
|
2577 } |
|
2578 |
|
2579 octave_value get (const property_name& name) const |
|
2580 { |
|
2581 octave_value retval; |
|
2582 |
|
2583 if (name.compare ("type")) |
|
2584 retval = type; |
|
2585 else if (name.compare ("parent")) |
|
2586 retval = parent; |
|
2587 else if (name.compare ("children")) |
|
2588 retval = children; |
|
2589 else if (name.compare ("string")) |
|
2590 retval = string; |
|
2591 else if (name.compare ("units")) |
|
2592 retval = units; |
|
2593 else if (name.compare ("position")) |
|
2594 retval = position; |
|
2595 else if (name.compare ("horizontalalignment")) |
|
2596 retval = horizontalalignment; |
|
2597 else |
|
2598 warning ("get: invalid property `%s'", name.c_str ()); |
|
2599 |
|
2600 return retval; |
|
2601 } |
|
2602 |
|
2603 std::string graphics_object_name (void) const { return go_name; } |
|
2604 |
|
2605 static property_list::pval_map_type factory_defaults (void) |
|
2606 { |
|
2607 property_list::pval_map_type m; |
|
2608 |
|
2609 m["string"] = ""; |
|
2610 m["units"] = "data"; |
|
2611 m["position"] = Matrix (1, 3, 0.0); |
|
2612 m["horizontalalignment"] = "left"; |
|
2613 |
|
2614 return m; |
|
2615 } |
|
2616 |
|
2617 private: |
|
2618 octave_value string; |
|
2619 octave_value units; |
|
2620 octave_value position; |
|
2621 octave_value horizontalalignment; |
|
2622 |
|
2623 static std::string go_name; |
|
2624 }; |
|
2625 |
|
2626 text_properties properties; |
|
2627 |
|
2628 public: |
|
2629 text (const graphics_handle& mh, const graphics_handle& p) |
|
2630 : base_graphics_object (), properties (mh, p) |
|
2631 { |
|
2632 properties.override_defaults (*this); |
|
2633 } |
|
2634 |
|
2635 ~text (void) { properties.delete_children (); } |
|
2636 |
|
2637 std::string type (void) const { return properties.graphics_object_name (); } |
|
2638 |
|
2639 void override_defaults (base_graphics_object& obj) |
|
2640 { |
|
2641 // Allow parent (figure) to override first (properties knows how |
|
2642 // to find the parent object). |
|
2643 properties.override_defaults (obj); |
|
2644 } |
|
2645 |
|
2646 void set_from_list (property_list& plist) |
|
2647 { |
|
2648 properties.set_from_list (*this, plist); |
|
2649 } |
|
2650 |
|
2651 void set (const property_name& name, const octave_value& val) |
|
2652 { |
|
2653 properties.set (name, val); |
|
2654 } |
|
2655 |
|
2656 octave_value get (void) const |
|
2657 { |
|
2658 return properties.get (); |
|
2659 } |
|
2660 |
|
2661 octave_value get (const property_name& name) const |
|
2662 { |
|
2663 return properties.get (name); |
|
2664 } |
|
2665 |
|
2666 graphics_handle get_parent (void) const { return properties.get_parent (); } |
|
2667 |
|
2668 void remove_child (const graphics_handle& h) { properties.remove_child (h); } |
|
2669 |
|
2670 void adopt (const graphics_handle& h) { properties.adopt (h); } |
|
2671 |
|
2672 void reparent (const graphics_handle& h) { properties.reparent (h); } |
|
2673 |
|
2674 bool valid_object (void) const { return true; } |
|
2675 }; |
|
2676 |
|
2677 std::string text::text_properties::go_name ("text"); |
|
2678 |
|
2679 // --------------------------------------------------------------------- |
|
2680 |
|
2681 class image : public base_graphics_object |
|
2682 { |
|
2683 public: |
|
2684 class image_properties : public base_properties |
|
2685 { |
|
2686 public: |
|
2687 image_properties (const graphics_handle& mh, const graphics_handle& p) |
|
2688 : base_properties (go_name, mh, p), |
|
2689 cdata (Matrix ()), |
|
2690 xdata (Matrix ()), |
|
2691 ydata (Matrix ()) |
|
2692 { } |
|
2693 |
|
2694 ~image_properties (void) { } |
|
2695 |
|
2696 void set (const property_name& name, const octave_value& val) |
|
2697 { |
|
2698 if (name.compare ("parent")) |
|
2699 set_parent (val); |
|
2700 else if (name.compare ("children")) |
|
2701 children = maybe_set_children (children, val); |
|
2702 else if (name.compare ("cdata")) |
|
2703 cdata = val; |
|
2704 else if (name.compare ("xdata")) |
|
2705 xdata = val; |
|
2706 else if (name.compare ("ydata")) |
|
2707 ydata = val; |
|
2708 else |
|
2709 warning ("set: invalid property `%s'", name.c_str ()); |
|
2710 } |
|
2711 |
|
2712 octave_value get (void) const |
|
2713 { |
|
2714 Octave_map m; |
|
2715 |
|
2716 m.assign ("type", type); |
|
2717 m.assign ("parent", parent); |
|
2718 m.assign ("children", children); |
|
2719 m.assign ("cdata", cdata); |
|
2720 m.assign ("xdata", xdata); |
|
2721 m.assign ("ydata", ydata); |
|
2722 |
|
2723 return m; |
|
2724 } |
|
2725 |
|
2726 octave_value get (const property_name& name) const |
|
2727 { |
|
2728 octave_value retval; |
|
2729 |
|
2730 if (name.compare ("type")) |
|
2731 retval = type; |
|
2732 else if (name.compare ("parent")) |
|
2733 retval = parent; |
|
2734 else if (name.compare ("children")) |
|
2735 retval = children; |
|
2736 else if (name.compare ("cdata")) |
|
2737 retval = cdata; |
|
2738 else if (name.compare ("xdata")) |
|
2739 retval = xdata; |
|
2740 else if (name.compare ("ydata")) |
|
2741 retval = ydata; |
|
2742 else |
|
2743 warning ("get: invalid property `%s'", name.c_str ()); |
|
2744 |
|
2745 return retval; |
|
2746 } |
|
2747 |
|
2748 std::string graphics_object_name (void) const { return go_name; } |
|
2749 |
|
2750 static property_list::pval_map_type factory_defaults (void) |
|
2751 { |
|
2752 property_list::pval_map_type m; |
|
2753 |
|
2754 m["cdata"] = Matrix (); |
|
2755 m["xdata"] = Matrix (); |
|
2756 m["ydata"] = Matrix (); |
|
2757 |
|
2758 return m; |
|
2759 } |
|
2760 |
|
2761 private: |
|
2762 octave_value cdata; |
|
2763 octave_value xdata; |
|
2764 octave_value ydata; |
|
2765 |
|
2766 static std::string go_name; |
|
2767 }; |
|
2768 |
|
2769 image_properties properties; |
|
2770 |
|
2771 public: |
|
2772 image (const graphics_handle& mh, const graphics_handle& p) |
|
2773 : base_graphics_object (), properties (mh, p) |
|
2774 { |
|
2775 properties.override_defaults (*this); |
|
2776 } |
|
2777 |
|
2778 ~image (void) { properties.delete_children (); } |
|
2779 |
|
2780 std::string type (void) const { return properties.graphics_object_name (); } |
|
2781 |
|
2782 void override_defaults (base_graphics_object& obj) |
|
2783 { |
|
2784 // Allow parent (figure) to override first (properties knows how |
|
2785 // to find the parent object). |
|
2786 properties.override_defaults (obj); |
|
2787 } |
|
2788 |
|
2789 void set_from_list (property_list& plist) |
|
2790 { |
|
2791 properties.set_from_list (*this, plist); |
|
2792 } |
|
2793 |
|
2794 void set (const property_name& name, const octave_value& val) |
|
2795 { |
|
2796 properties.set (name, val); |
|
2797 } |
|
2798 |
|
2799 octave_value get (void) const |
|
2800 { |
|
2801 return properties.get (); |
|
2802 } |
|
2803 |
|
2804 octave_value get (const property_name& name) const |
|
2805 { |
|
2806 return properties.get (name); |
|
2807 } |
|
2808 |
|
2809 graphics_handle get_parent (void) const { return properties.get_parent (); } |
|
2810 |
|
2811 void remove_child (const graphics_handle& h) { properties.remove_child (h); } |
|
2812 |
|
2813 void adopt (const graphics_handle& h) { properties.adopt (h); } |
|
2814 |
|
2815 void reparent (const graphics_handle& h) { properties.reparent (h); } |
|
2816 |
|
2817 bool valid_object (void) const { return true; } |
|
2818 }; |
|
2819 |
|
2820 std::string image::image_properties::go_name ("image"); |
|
2821 |
|
2822 // --------------------------------------------------------------------- |
|
2823 |
|
2824 class surface : public base_graphics_object |
|
2825 { |
|
2826 public: |
|
2827 class surface_properties : public base_properties |
|
2828 { |
|
2829 public: |
|
2830 surface_properties (const graphics_handle& mh, const graphics_handle& p) |
|
2831 : base_properties (go_name, mh, p), |
|
2832 xdata (Matrix ()), |
|
2833 ydata (Matrix ()), |
|
2834 zdata (Matrix ()), |
|
2835 keylabel ("") |
|
2836 { } |
|
2837 |
|
2838 ~surface_properties (void) { } |
|
2839 |
|
2840 void set (const property_name& name, const octave_value& val) |
|
2841 { |
|
2842 if (name.compare ("parent")) |
|
2843 set_parent (val); |
|
2844 else if (name.compare ("children")) |
|
2845 children = maybe_set_children (children, val); |
|
2846 else if (name.compare ("xdata")) |
|
2847 xdata = val; |
|
2848 else if (name.compare ("ydata")) |
|
2849 ydata = val; |
|
2850 else if (name.compare ("zdata")) |
|
2851 zdata = val; |
|
2852 else if (name.compare ("keylabel")) |
|
2853 keylabel = val; |
|
2854 else |
|
2855 warning ("set: invalid property `%s'", name.c_str ()); |
|
2856 } |
|
2857 |
|
2858 octave_value get (void) const |
|
2859 { |
|
2860 Octave_map m; |
|
2861 |
|
2862 m.assign ("type", type); |
|
2863 m.assign ("parent", parent); |
|
2864 m.assign ("children", children); |
|
2865 m.assign ("xdata", xdata); |
|
2866 m.assign ("ydata", ydata); |
|
2867 m.assign ("zdata", zdata); |
|
2868 m.assign ("keylabel", keylabel); |
|
2869 |
|
2870 return m; |
|
2871 } |
|
2872 |
|
2873 octave_value get (const property_name& name) const |
|
2874 { |
|
2875 octave_value retval; |
|
2876 |
|
2877 if (name.compare ("type")) |
|
2878 retval = type; |
|
2879 else if (name.compare ("parent")) |
|
2880 retval = parent; |
|
2881 else if (name.compare ("children")) |
|
2882 retval = children; |
|
2883 else if (name.compare ("xdata")) |
|
2884 retval = xdata; |
|
2885 else if (name.compare ("ydata")) |
|
2886 retval = ydata; |
|
2887 else if (name.compare ("zdata")) |
|
2888 retval = zdata; |
|
2889 else if (name.compare ("keylabel")) |
|
2890 retval = keylabel; |
|
2891 else |
|
2892 warning ("get: invalid property `%s'", name.c_str ()); |
|
2893 |
|
2894 return retval; |
|
2895 } |
|
2896 |
|
2897 std::string graphics_object_name (void) const { return go_name; } |
|
2898 |
|
2899 static property_list::pval_map_type factory_defaults (void) |
|
2900 { |
|
2901 property_list::pval_map_type m; |
|
2902 |
|
2903 m["xdata"] = Matrix (); |
|
2904 m["ydata"] = Matrix (); |
|
2905 m["zdata"] = Matrix (); |
|
2906 m["keylabel"] = ""; |
|
2907 |
|
2908 return m; |
|
2909 } |
|
2910 |
|
2911 private: |
|
2912 octave_value xdata; |
|
2913 octave_value ydata; |
|
2914 octave_value zdata; |
|
2915 octave_value keylabel; |
|
2916 |
|
2917 static std::string go_name; |
|
2918 }; |
|
2919 |
|
2920 surface_properties properties; |
|
2921 |
|
2922 public: |
|
2923 surface (const graphics_handle& mh, const graphics_handle& p) |
|
2924 : base_graphics_object (), properties (mh, p) |
|
2925 { |
|
2926 properties.override_defaults (*this); |
|
2927 } |
|
2928 |
|
2929 ~surface (void) { properties.delete_children (); } |
|
2930 |
|
2931 std::string type (void) const { return properties.graphics_object_name (); } |
|
2932 |
|
2933 void override_defaults (base_graphics_object& obj) |
|
2934 { |
|
2935 // Allow parent (figure) to override first (properties knows how |
|
2936 // to find the parent object). |
|
2937 properties.override_defaults (obj); |
|
2938 } |
|
2939 |
|
2940 void set_from_list (property_list& plist) |
|
2941 { |
|
2942 properties.set_from_list (*this, plist); |
|
2943 } |
|
2944 |
|
2945 void set (const property_name& name, const octave_value& val) |
|
2946 { |
|
2947 properties.set (name, val); |
|
2948 } |
|
2949 |
|
2950 octave_value get (void) const |
|
2951 { |
|
2952 return properties.get (); |
|
2953 } |
|
2954 |
|
2955 octave_value get (const property_name& name) const |
|
2956 { |
|
2957 return properties.get (name); |
|
2958 } |
|
2959 |
|
2960 graphics_handle get_parent (void) const { return properties.get_parent (); } |
|
2961 |
|
2962 void remove_child (const graphics_handle& h) { properties.remove_child (h); } |
|
2963 |
|
2964 void adopt (const graphics_handle& h) { properties.adopt (h); } |
|
2965 |
|
2966 void reparent (const graphics_handle& h) { properties.reparent (h); } |
|
2967 |
|
2968 bool valid_object (void) const { return true; } |
|
2969 }; |
|
2970 |
|
2971 std::string surface::surface_properties::go_name ("surface"); |
|
2972 |
|
2973 // --------------------------------------------------------------------- |
|
2974 |
|
2975 octave_value |
|
2976 base_graphics_object::get_default (const property_name& name) const |
|
2977 { |
|
2978 graphics_handle parent = get_parent (); |
|
2979 graphics_object parent_obj = gh_manager::get_object (parent); |
|
2980 |
|
2981 return parent_obj.get_default (type () + name); |
|
2982 } |
|
2983 |
|
2984 octave_value |
|
2985 base_graphics_object::get_factory_default (const property_name& name) const |
|
2986 { |
|
2987 graphics_object parent_obj = gh_manager::get_object (0); |
|
2988 |
|
2989 return parent_obj.get_factory_default (type () + name); |
|
2990 } |
|
2991 |
|
2992 gh_manager::gh_manager (void) |
|
2993 : handle_map (), handle_free_list (), next_handle (-1) |
|
2994 { |
|
2995 handle_map[0] = graphics_object (new root_figure ()); |
|
2996 } |
|
2997 |
|
2998 graphics_handle |
|
2999 gh_manager::do_make_graphics_handle (const std::string& go_name, |
|
3000 const graphics_handle& p) |
|
3001 { |
|
3002 graphics_handle h = get_handle (go_name); |
|
3003 |
|
3004 base_graphics_object *go = 0; |
|
3005 |
|
3006 if (go_name == "figure") |
|
3007 go = new figure (h, p); |
|
3008 else if (go_name == "axes") |
|
3009 go = new axes (h, p); |
|
3010 else if (go_name == "line") |
|
3011 go = new line (h, p); |
|
3012 else if (go_name == "text") |
|
3013 go = new text (h, p); |
|
3014 else if (go_name == "image") |
|
3015 go = new image (h, p); |
|
3016 else if (go_name == "surface") |
|
3017 go = new surface (h, p); |
|
3018 |
|
3019 if (go) |
|
3020 handle_map[h] = graphics_object (go); |
|
3021 else |
|
3022 error ("gh_manager::do_make_graphics_handle: invalid object type `%s'", |
|
3023 go_name.c_str ()); |
|
3024 |
|
3025 return h; |
|
3026 } |
|
3027 |
|
3028 graphics_handle |
|
3029 gh_manager::do_make_figure_handle (double val) |
|
3030 { |
|
3031 graphics_handle h = val; |
|
3032 |
|
3033 handle_map[h] = graphics_object (new figure (h, 0)); |
|
3034 |
|
3035 return h; |
|
3036 } |
|
3037 |
|
3038 void |
|
3039 gh_manager::do_push_figure (const graphics_handle& h) |
|
3040 { |
|
3041 do_pop_figure (h); |
|
3042 |
|
3043 figure_list.push_front (h); |
|
3044 } |
|
3045 |
|
3046 void |
|
3047 gh_manager::do_pop_figure (const graphics_handle& h) |
|
3048 { |
|
3049 for (figure_list_iterator p = figure_list.begin (); |
|
3050 p != figure_list.end (); |
|
3051 p++) |
|
3052 { |
|
3053 if (*p == h) |
|
3054 { |
|
3055 figure_list.erase (p); |
|
3056 break; |
|
3057 } |
|
3058 } |
|
3059 } |
|
3060 |
|
3061 property_list::plist_map_type |
|
3062 root_figure::init_factory_properties (void) |
|
3063 { |
|
3064 property_list::plist_map_type plist_map; |
|
3065 |
|
3066 plist_map["figure"] = figure::figure_properties::factory_defaults (); |
|
3067 plist_map["axes"] = axes::axes_properties::factory_defaults (); |
|
3068 plist_map["line"] = line::line_properties::factory_defaults (); |
|
3069 plist_map["text"] = text::text_properties::factory_defaults (); |
|
3070 plist_map["image"] = image::image_properties::factory_defaults (); |
|
3071 plist_map["surface"] = surface::surface_properties::factory_defaults (); |
|
3072 |
|
3073 return plist_map; |
|
3074 } |
|
3075 |
|
3076 // --------------------------------------------------------------------- |
|
3077 |
|
3078 DEFUN (ishandle, args, , |
|
3079 "-*- texinfo -*-\n\ |
|
3080 @deftypefn {Function File} {} ishandle (@var{h})\n\ |
|
3081 Return true if @var{h} is a graphics handle and false otherwise.\n\ |
|
3082 @end deftypefn") |
|
3083 { |
|
3084 octave_value retval; |
|
3085 |
|
3086 if (args.length () == 1) |
|
3087 retval = is_handle (args(0)); |
|
3088 else |
|
3089 print_usage (); |
|
3090 |
|
3091 return retval; |
|
3092 } |
|
3093 |
|
3094 DEFUN (set, args, , |
|
3095 "-*- texinfo -*-\n\ |
|
3096 @deftypefn {Function File} {} set (@var{h}, @var{p}, @var{v}, @dots{})\n\ |
|
3097 Set the named property @var{p} to the value @var{v} in the graphics\n\ |
|
3098 handle @var{h}.\n\ |
|
3099 @end deftypefn") |
|
3100 { |
|
3101 octave_value retval; |
|
3102 |
|
3103 int nargin = args.length (); |
|
3104 |
|
3105 if (nargin > 0) |
|
3106 { |
|
3107 double handle = args(0).double_value (); |
|
3108 |
|
3109 if (! error_state) |
|
3110 { |
|
3111 graphics_object obj = gh_manager::get_object (handle); |
|
3112 |
|
3113 if (obj) |
|
3114 { |
|
3115 obj.set (args.splice (0, 1)); |
|
3116 |
|
3117 feval ("__request_drawnow__"); |
|
3118 } |
|
3119 else |
|
3120 error ("set: invalid handle (= %g)", handle); |
|
3121 } |
|
3122 else |
|
3123 error ("set: expecting graphics handle as first argument"); |
|
3124 } |
|
3125 else |
|
3126 print_usage (); |
|
3127 |
|
3128 return retval; |
|
3129 } |
|
3130 |
|
3131 DEFUN (get, args, , |
|
3132 "-*- texinfo -*-\n\ |
|
3133 @deftypefn {Function File} {} get (@var{h}, @var{p})\n\ |
|
3134 Return the named property @var{p} from the graphics handle @var{h}.\n\ |
|
3135 If @var{p} is omitted, return the complete property list for @var{h}.\n\ |
|
3136 @end deftypefn") |
|
3137 { |
|
3138 octave_value retval; |
|
3139 |
|
3140 int nargin = args.length (); |
|
3141 |
|
3142 if (nargin == 1 || nargin == 2) |
|
3143 { |
|
3144 double handle = args(0).double_value (); |
|
3145 |
|
3146 if (! error_state) |
|
3147 { |
|
3148 graphics_object obj = gh_manager::get_object (handle); |
|
3149 |
|
3150 if (obj) |
|
3151 { |
|
3152 if (nargin == 1) |
|
3153 retval = obj.get (); |
|
3154 else |
|
3155 { |
|
3156 property_name property = args(1).string_value (); |
|
3157 |
|
3158 if (! error_state) |
|
3159 retval = obj.get (property); |
|
3160 else |
|
3161 error ("get: expecting property name as second argument"); |
|
3162 } |
|
3163 } |
|
3164 else |
|
3165 error ("get: invalid handle (= %g)", handle); |
|
3166 } |
|
3167 else |
|
3168 error ("get: expecting graphics handle as first argument"); |
|
3169 } |
|
3170 else |
|
3171 print_usage (); |
|
3172 |
|
3173 return retval; |
|
3174 } |
|
3175 |
|
3176 static octave_value |
|
3177 make_graphics_object (const std::string& go_name, |
|
3178 const octave_value_list& args) |
|
3179 { |
|
3180 octave_value retval; |
|
3181 |
|
3182 double val = args(0).double_value (); |
|
3183 |
|
3184 if (! error_state) |
|
3185 { |
|
3186 graphics_handle parent = gh_manager::lookup (val); |
|
3187 |
|
3188 if (! xisnan (parent)) |
|
3189 { |
|
3190 graphics_handle h |
|
3191 = gh_manager::make_graphics_handle (go_name, parent); |
|
3192 |
|
3193 if (! error_state) |
|
3194 { |
|
3195 adopt (parent, h); |
|
3196 |
|
3197 xset (h, args.splice (0, 1)); |
|
3198 |
|
3199 retval = h; |
|
3200 } |
|
3201 else |
|
3202 error ("__go%s__: unable to create graphics handle", |
|
3203 go_name.c_str ()); |
|
3204 } |
|
3205 else |
|
3206 error ("__go_%s__: invalid parent", go_name.c_str ()); |
|
3207 } |
|
3208 else |
|
3209 error ("__go_%s__: invalid parent", go_name.c_str ()); |
|
3210 |
|
3211 return retval; |
|
3212 } |
|
3213 |
|
3214 DEFUN (__go_figure__, args, , |
|
3215 "-*- texinfo -*-\n\ |
|
3216 @deftypefn {Built-in Function} {} __go_figure__ (@var{fignum})\n\ |
|
3217 Create a figure graphics object.\n\ |
|
3218 @end deftypefn") |
|
3219 { |
|
3220 octave_value retval; |
|
3221 |
|
3222 if (args.length () > 0) |
|
3223 { |
|
3224 double val = args(0).double_value (); |
|
3225 |
|
3226 if (! error_state) |
|
3227 { |
|
3228 if (is_figure (val)) |
|
3229 { |
|
3230 graphics_handle h = gh_manager::lookup (val); |
|
3231 |
|
3232 xset (h, args.splice (0, 1)); |
|
3233 |
|
3234 retval = h; |
|
3235 } |
|
3236 else |
|
3237 { |
|
3238 graphics_handle h = octave_NaN; |
|
3239 |
|
3240 if (xisnan (val)) |
|
3241 h = gh_manager::make_graphics_handle ("figure", 0); |
|
3242 else if (val > 0 && D_NINT (val) == val) |
|
3243 h = gh_manager::make_figure_handle (val); |
|
3244 else |
|
3245 error ("__go_figure__: invalid figure number"); |
|
3246 |
|
3247 if (! (error_state || xisnan (h))) |
|
3248 { |
|
3249 adopt (0, h); |
|
3250 |
|
3251 xset (h, args.splice (0, 1)); |
|
3252 |
|
3253 retval = h; |
|
3254 } |
|
3255 else |
|
3256 error ("__go_figure__: failed to create figure handle"); |
|
3257 } |
|
3258 } |
|
3259 else |
|
3260 error ("__go_figure__: expecting figure number to be double value"); |
|
3261 } |
|
3262 else |
|
3263 print_usage (); |
|
3264 |
|
3265 return retval; |
|
3266 } |
|
3267 |
|
3268 #define GO_BODY(TYPE) \ |
|
3269 octave_value retval; \ |
|
3270 \ |
|
3271 if (args.length () > 0) \ |
|
3272 retval = make_graphics_object (#TYPE, args); \ |
|
3273 else \ |
|
3274 print_usage (); \ |
|
3275 \ |
|
3276 return retval |
|
3277 |
|
3278 DEFUN (__go_axes__, args, , |
|
3279 "-*- texinfo -*-\n\ |
|
3280 @deftypefn {Built-in Function} {} __go_axes__ (@var{parent})\n\ |
|
3281 Create an axes graphics object.\n\ |
|
3282 @end deftypefn") |
|
3283 { |
|
3284 GO_BODY (axes); |
|
3285 } |
|
3286 |
|
3287 DEFUN (__go_line__, args, , |
|
3288 "-*- texinfo -*-\n\ |
|
3289 @deftypefn {Built-in Function} {} __go_line__ (@var{parent})\n\ |
|
3290 Create a line graphics object.\n\ |
|
3291 @end deftypefn") |
|
3292 { |
|
3293 GO_BODY (line); |
|
3294 } |
|
3295 |
|
3296 DEFUN (__go_text__, args, , |
|
3297 "-*- texinfo -*-\n\ |
|
3298 @deftypefn {Built-in Function} {} __go_text__ (@var{parent})\n\ |
|
3299 Create a text graphics object.\n\ |
|
3300 @end deftypefn") |
|
3301 { |
|
3302 GO_BODY (text); |
|
3303 } |
|
3304 |
|
3305 DEFUN (__go_image__, args, , |
|
3306 "-*- texinfo -*-\n\ |
|
3307 @deftypefn {Built-in Function} {} __go_image__ (@var{parent})\n\ |
|
3308 Create an image graphics object.\n\ |
|
3309 @end deftypefn") |
|
3310 { |
|
3311 GO_BODY (image); |
|
3312 } |
|
3313 |
|
3314 DEFUN (__go_surface__, args, , |
|
3315 "-*- texinfo -*-\n\ |
|
3316 @deftypefn {Built-in Function} {} __go_surface__ (@var{parent})\n\ |
|
3317 Create a surface graphics object.\n\ |
|
3318 @end deftypefn") |
|
3319 { |
|
3320 GO_BODY (surface); |
|
3321 } |
|
3322 |
|
3323 DEFUN (__go_delete__, args, , |
|
3324 "-*- texinfo -*-\n\ |
|
3325 @deftypefn {Built-in Function} {} __go_delete__ (@var{h})\n\ |
|
3326 @end deftypefn") |
|
3327 { |
|
3328 octave_value_list retval; |
|
3329 |
|
3330 if (args.length () == 1) |
|
3331 { |
|
3332 graphics_handle h = octave_NaN; |
|
3333 |
|
3334 double val = args(0).double_value (); |
|
3335 |
|
3336 if (! error_state) |
|
3337 { |
|
3338 h = gh_manager::lookup (val); |
|
3339 |
|
3340 if (! xisnan (h)) |
|
3341 { |
|
3342 graphics_object obj = gh_manager::get_object (h); |
|
3343 |
|
3344 graphics_handle parent_h = obj.get_parent (); |
|
3345 |
|
3346 graphics_object parent_obj = gh_manager::get_object (parent_h); |
|
3347 |
|
3348 parent_obj.remove_child (h); |
|
3349 |
|
3350 gh_manager::free (h); |
|
3351 } |
|
3352 else |
|
3353 error ("delete: invalid graphics object (= %g)", val); |
|
3354 } |
|
3355 else |
|
3356 error ("delete: invalid graphics object"); |
|
3357 } |
|
3358 else |
|
3359 print_usage (); |
|
3360 |
|
3361 return retval; |
|
3362 } |
|
3363 |
|
3364 DEFUN (__go_axes_init__, args, , |
|
3365 "-*- texinfo -*-\n\ |
|
3366 @deftypefn {Built-in Function} {} __go_axes_init__ (@var{h}, @var{mode})\n\ |
|
3367 Initialize axes object.\n\ |
|
3368 @end deftypefn") |
|
3369 { |
|
3370 octave_value retval; |
|
3371 |
|
3372 int nargin = args.length (); |
|
3373 |
|
3374 std::string mode = ""; |
|
3375 |
|
3376 if (nargin == 2) |
|
3377 { |
|
3378 mode = args(1).string_value (); |
|
3379 |
|
3380 if (error_state) |
|
3381 return retval; |
|
3382 } |
|
3383 |
|
3384 if (nargin == 1 || nargin == 2) |
|
3385 { |
|
3386 graphics_handle h = octave_NaN; |
|
3387 |
|
3388 double val = args(0).double_value (); |
|
3389 |
|
3390 if (! error_state) |
|
3391 { |
|
3392 h = gh_manager::lookup (val); |
|
3393 |
|
3394 if (! xisnan (h)) |
|
3395 { |
|
3396 graphics_object obj = gh_manager::get_object (h); |
|
3397 |
|
3398 obj.set_defaults (mode); |
|
3399 } |
|
3400 else |
|
3401 error ("__go_axes_init__: invalid graphics object (= %g)", val); |
|
3402 } |
|
3403 else |
|
3404 error ("__go_axes_init__: invalid graphics object"); |
|
3405 } |
|
3406 else |
|
3407 print_usage (); |
|
3408 |
|
3409 return retval; |
|
3410 } |
|
3411 |
|
3412 DEFUN (__go_handles__, , , |
|
3413 "-*- texinfo -*-\n\ |
|
3414 @deftypefn {Built-in Function} {} __go_handles__ ()\n\ |
|
3415 Return current list of function handles.\n\ |
|
3416 @end deftypefn") |
|
3417 { |
|
3418 return octave_value (gh_manager::list ()); |
|
3419 } |
|
3420 |
|
3421 /* |
|
3422 ;;; Local Variables: *** |
|
3423 ;;; mode: C++ *** |
|
3424 ;;; End: *** |
|
3425 */ |