Rearranged a bit. Use the new macro in valuenode.h introduced in the previous commit...
[synfig.git] / synfig-core / trunk / src / synfig / loadcanvas.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file loadcanvas.cpp
3 **      \brief writeme
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include <cstdlib>
34 #include <cstdio>
35 #include <cstring>
36 #include <ETL/stringf>
37 #include <libxml++/libxml++.h>
38 #include <vector>
39 #include <stdexcept>
40 #include <iostream>
41
42 #include "loadcanvas.h"
43 #include "valuenode.h"
44 #include "valuenode_subtract.h"
45 #include "valuenode_animated.h"
46 #include "valuenode_composite.h"
47 #include "valuenode_const.h"
48 #include "valuenode_linear.h"
49 #include "valuenode_dynamiclist.h"
50 #include "valuenode_reference.h"
51 #include "valuenode_scale.h"
52 #include "valuenode_timedswap.h"
53 #include "valuenode_twotone.h"
54 #include "valuenode_stripes.h"
55 #include "valuenode_segcalctangent.h"
56 #include "valuenode_segcalcvertex.h"
57 #include "valuenode_bline.h"
58
59 #include "layer.h"
60 #include "string.h"
61
62 #include "exception.h"
63
64 #include "gradient.h"
65
66 #include <map>
67 #include <sigc++/bind.h>
68
69 #endif
70
71 /* === U S I N G =========================================================== */
72
73 using namespace std;
74 using namespace synfig;
75 using namespace etl;
76
77 /*
78 class test_class {
79 static int bleh;
80 public:
81         test_class() { assert(!bleh); bleh++; synfig::info("test_class: initi: %d",bleh); }
82         ~test_class() { assert(bleh); synfig::info("test_class: uninit: %d",bleh); bleh--; }
83 };
84 int test_class::bleh(0);
85
86 test_class test_class_instance;
87 */
88
89 /* === M A C R O S ========================================================= */
90
91 inline bool is_whitespace(char x) { return ((x)=='\n' || (x)=='\t' || (x)==' '); }
92
93 /* === P R O C E D U R E S ================================================= */
94
95 static std::map<String, Canvas::LooseHandle>* open_canvas_map_(0);
96
97 std::map<synfig::String, etl::loose_handle<Canvas> >& synfig::get_open_canvas_map()
98 {
99         if(!open_canvas_map_)
100                 open_canvas_map_=new std::map<String, Canvas::LooseHandle>;
101         return *open_canvas_map_;
102 }
103
104 static void _remove_from_open_canvas_map(Canvas *x) { get_open_canvas_map().erase(etl::absolute_path(x->get_file_name())); }
105
106 static void _canvas_file_name_changed(Canvas *x)
107 {
108         std::map<synfig::String, etl::loose_handle<Canvas> >::iterator iter;
109
110         for(iter=get_open_canvas_map().begin();iter!=get_open_canvas_map().end();++iter)
111                 if(iter->second==x)
112                         break;
113         assert(iter!=get_open_canvas_map().end());
114         if(iter==get_open_canvas_map().end())
115                 return;
116         get_open_canvas_map().erase(iter->first);
117         get_open_canvas_map()[etl::absolute_path(x->get_file_name())]=x;
118
119 }
120
121 Canvas::Handle
122 synfig::open_canvas(const String &filename)
123 {
124         CanvasParser parser;
125
126         parser.set_allow_errors(true);
127
128         Canvas::Handle canvas=parser.parse_from_file(filename);
129
130         if(parser.error_count())
131                 return Canvas::Handle();
132
133         return canvas;
134 }
135
136 Canvas::Handle
137 synfig::open_canvas_as(const String &filename,const String &as)
138 {
139         CanvasParser parser;
140
141         parser.set_allow_errors(true);
142
143         Canvas::Handle canvas=parser.parse_from_file_as(filename,as);
144
145         if(parser.error_count())
146                 return Canvas::Handle();
147
148         return canvas;
149 }
150
151 Canvas::Handle
152 synfig::string_to_canvas(const String &data)
153 {
154         CanvasParser parser;
155
156         parser.set_allow_errors(true);
157
158         Canvas::Handle canvas=parser.parse_from_string(data);
159
160         if(parser.error_count())
161                 return Canvas::Handle();
162
163         return canvas;
164 }
165
166 /* === M E T H O D S ======================================================= */
167
168 void
169 CanvasParser::error_unexpected_element(xmlpp::Node *element,const String &got, const String &expected)
170 {
171         error(element,strprintf(_("Unexpected element <%s>, Expected <%s>"),got.c_str(),expected.c_str()));
172 }
173
174 void
175 CanvasParser::error_unexpected_element(xmlpp::Node *element,const String &got)
176 {
177         error(element,strprintf(_("Unexpected element <%s>"),got.c_str()));
178 }
179
180 void
181 CanvasParser::warning(xmlpp::Node *element, const String &text)
182 {
183         string str=strprintf("%s:<%s>:%d: warning: ",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
184         //synfig::warning(str);
185         cerr<<str<<endl;
186         total_warnings_++;
187         if(total_warnings_>=max_warnings_)
188                 fatal_error(element, _("Too many warnings"));
189 }
190
191 void
192 CanvasParser::error(xmlpp::Node *element, const String &text)
193 {
194         string str=strprintf("%s:<%s>:%d: error: ",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
195         total_errors_++;
196         if(!allow_errors_)
197                 throw runtime_error(str);
198         cerr<<str<<endl;
199         //      synfig::error(str);
200 }
201
202 void
203 CanvasParser::fatal_error(xmlpp::Node *element, const String &text)
204 {
205         string str=strprintf("%s:<%s>:%d:",filename.c_str(),element->get_name().c_str(),element->get_line())+text;
206         throw runtime_error(str);
207 }
208
209
210
211 Keyframe
212 CanvasParser::parse_keyframe(xmlpp::Element *element,Canvas::Handle canvas)
213 {
214         assert(element->get_name()=="keyframe");
215
216         if(!element->get_attribute("time"))
217         {
218                 error(element,strprintf(_("<%s> is missing \"%s\" attribute"),"real","time"));
219                 return Keyframe();
220         }
221
222         Keyframe ret(Time(element->get_attribute("time")->get_value(),canvas->rend_desc().get_frame_rate()));
223
224
225         if(element->get_children().empty())
226                 return ret;
227
228         if(element->get_child_text()->get_content().empty())
229                 return ret;
230
231         ret.set_description(element->get_child_text()->get_content());
232
233         return ret;
234 }
235
236
237 Real
238 CanvasParser::parse_real(xmlpp::Element *element)
239 {
240         assert(element->get_name()=="real");
241
242         if(!element->get_children().empty())
243                 warning(element, strprintf(_("<%s> should not contain anything"),"real"));
244
245         if(!element->get_attribute("value"))
246         {
247                 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"real"));
248                 return false;
249         }
250
251         string val=element->get_attribute("value")->get_value();
252
253         return atof(val.c_str());
254 }
255
256 Time
257 CanvasParser::parse_time(xmlpp::Element *element,Canvas::Handle canvas)
258 {
259         assert(element->get_name()=="time");
260
261         if(!element->get_children().empty())
262                 warning(element, strprintf(_("<%s> should not contain anything"),"time"));
263
264         if(!element->get_attribute("value"))
265         {
266                 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"time"));
267                 return false;
268         }
269
270         string val=element->get_attribute("value")->get_value();
271
272         return Time(val,canvas->rend_desc().get_frame_rate());
273 }
274
275 int
276 CanvasParser::parse_integer(xmlpp::Element *element)
277 {
278         assert(element->get_name()=="integer");
279
280         if(!element->get_children().empty())
281                 warning(element, strprintf(_("<%s> should not contain anything"),"integer"));
282
283         if(!element->get_attribute("value"))
284         {
285                 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"integer"));
286                 return false;
287         }
288
289         string val=element->get_attribute("value")->get_value();
290
291         return atoi(val.c_str());
292 }
293
294 // see 'minor hack' at the end of parse_vector() below
295 // making this 'static' to give it file local scope
296 // stops it working (where working means working around
297 // bug #1509627)
298 Vector &canvas_parser_vector_id(Vector &vector)
299 {
300         return vector;
301 }
302
303 Vector
304 CanvasParser::parse_vector(xmlpp::Element *element)
305 {
306         assert(element->get_name()=="vector");
307
308         if(element->get_children().empty())
309         {
310                 error(element, "Undefined value in <vector>");
311                 return Vector();
312         }
313
314         Vector vect;
315
316         xmlpp::Element::NodeList list = element->get_children();
317         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
318         {
319                 xmlpp::Element *child=dynamic_cast<xmlpp::Element*>((xmlpp::Node*)*iter);
320                 if(!child)
321                         continue;
322                 else
323                 if(child->get_name()=="x")
324                 {
325                         if(child->get_children().empty())
326                         {
327                                 error(element, "Undefined value in <x>");
328                                 return Vector();
329                         }
330                         vect[0]=atof(child->get_child_text()->get_content().c_str());
331                 }
332                 else
333                 if(child->get_name()=="y")
334                 {
335                         if(child->get_children().empty())
336                         {
337                                 error(element, "Undefined value in <y>");
338                                 return Vector();
339                         }
340                         vect[1]=atof(child->get_child_text()->get_content().c_str());
341                 }
342                 else
343                         error_unexpected_element(child,child->get_name());
344         }
345         // Minor hack - gcc 4.1.2 and earlier think that we're not using
346         // 'vect' and optimize it out at -O2 and higher.  This convinces
347         // them that we are really using it.
348         return canvas_parser_vector_id(vect);
349         // When the bug is fixed, we can just do this instead:
350         // return vect;
351 }
352
353 Color
354 CanvasParser::parse_color(xmlpp::Element *element)
355 {
356         assert(element->get_name()=="color");
357
358         if(element->get_children().empty())
359         {
360                 error(element, "Undefined value in <color>");
361                 return Color();
362         }
363
364         Color color(0);
365
366         xmlpp::Element::NodeList list = element->get_children();
367         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
368         {
369                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
370                 if(!child)
371                         continue;
372                 else
373                 if(child->get_name()=="r")
374                 {
375                         if(child->get_children().empty())
376                         {
377                                 error(element, "Undefined value in <r>");
378                                 return Color();
379                         }
380                         color.set_r(atof(child->get_child_text()->get_content().c_str()));
381                 }
382                 else
383                 if(child->get_name()=="g")
384                 {
385                         if(child->get_children().empty())
386                         {
387                                 error(element, "Undefined value in <g>");
388                                 return Color();
389                         }
390                         color.set_g(atof(child->get_child_text()->get_content().c_str()));
391                 }
392                 else
393                 if(child->get_name()=="b")
394                 {
395                         if(child->get_children().empty())
396                         {
397                                 error(element, "Undefined value in <b>");
398                                 return Color();
399                         }
400                         color.set_b(atof(child->get_child_text()->get_content().c_str()));
401                 }
402                 else
403                 if(child->get_name()=="a")
404                 {
405                         if(child->get_children().empty())
406                         {
407                                 error(element, "Undefined value in <a>");
408                                 return Color();
409                         }
410                         color.set_a(atof(child->get_child_text()->get_content().c_str()));
411                 }
412                 else
413                         error_unexpected_element(child,child->get_name());
414         }
415
416         return color;
417 }
418
419 synfig::String
420 CanvasParser::parse_string(xmlpp::Element *element)
421 {
422         assert(element->get_name()=="string");
423
424         if(element->get_children().empty())
425         {
426                 warning(element, "Undefined value in <string>");
427                 return synfig::String();
428         }
429
430         if(element->get_child_text()->get_content().empty())
431         {
432                 warning(element, "Content element of <string> appears to be empty");
433                 return synfig::String();
434         }
435
436         return element->get_child_text()->get_content();
437 }
438
439 bool
440 CanvasParser::parse_bool(xmlpp::Element *element)
441 {
442         assert(element->get_name()=="bool");
443
444         if(!element->get_children().empty())
445                 warning(element, strprintf(_("<%s> should not contain anything"),"bool"));
446
447         if(!element->get_attribute("value"))
448         {
449                 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"bool"));
450                 return false;
451         }
452
453         string val=element->get_attribute("value")->get_value();
454
455         if(val=="true" || val=="1")
456                 return true;
457         if(val=="false" || val=="0")
458                 return false;
459
460         error(element,strprintf(_("Bad value \"%s\" in <%s>"),val.c_str(),"bool"));
461
462         return false;
463 }
464
465 Gradient
466 CanvasParser::parse_gradient(xmlpp::Element *node)
467 {
468         assert(node->get_name()=="gradient");
469         Gradient ret;
470
471         xmlpp::Element::NodeList list = node->get_children();
472         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
473         {
474                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
475                 if(!child)
476                         continue;
477                 else
478                 {
479                         Gradient::CPoint cpoint;
480                         cpoint.color=parse_color(child);
481
482                         if(!child->get_attribute("pos"))
483                         {
484                                 error(child,strprintf(_("<%s> is missing \"pos\" attribute"),"gradient"));
485                                 return Gradient();
486                         }
487
488                         cpoint.pos=atof(child->get_attribute("pos")->get_value().c_str());
489
490                         ret.push_back(cpoint);
491                 }
492         }
493         ret.sort();
494         return ret;
495 }
496
497 ValueBase
498 CanvasParser::parse_list(xmlpp::Element *element,Canvas::Handle canvas)
499 {
500         vector<ValueBase> value_list;
501
502         xmlpp::Element::NodeList list = element->get_children();
503         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
504         {
505                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
506                 if(!child)
507                         continue;
508                 else
509                 {
510                         value_list.push_back(parse_value(child,canvas));
511                         if(!value_list.back().is_valid())
512                         {
513                                 value_list.pop_back();
514                                 error(child,"Bad ValueBase");
515                                 continue;
516                         }
517                 }
518         }
519         return value_list;
520 }
521
522 Segment
523 CanvasParser::parse_segment(xmlpp::Element *element)
524 {
525         assert(element->get_name()=="segment");
526
527         if(element->get_children().empty())
528         {
529                 error(element, "Undefined value in <segment>");
530                 return Segment();
531         }
532
533         Segment seg;
534
535         xmlpp::Element::NodeList list = element->get_children();
536         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
537         {
538                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
539                 if(!child)
540                         continue;
541                 else
542                 if(child->get_name()=="p1")
543                 {
544                         xmlpp::Element::NodeList list = child->get_children();
545                         xmlpp::Element::NodeList::iterator iter;
546
547                         // Search for the first non-text XML element
548                         for(iter = list.begin(); iter != list.end(); ++iter)
549                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
550
551                         if(iter==list.end())
552                         {
553                                 error(element, "Undefined value in <p1>");
554                                 continue;
555                         }
556
557                         if((*iter)->get_name()!="vector")
558                         {
559                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
560                                 continue;
561                         }
562
563                         seg.p1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
564                 }
565                 else
566                 if(child->get_name()=="t1")
567                 {
568                         xmlpp::Element::NodeList list = child->get_children();
569                         xmlpp::Element::NodeList::iterator iter;
570
571                         // Search for the first non-text XML element
572                         for(iter = list.begin(); iter != list.end(); ++iter)
573                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
574
575                         if(iter==list.end())
576                         {
577                                 error(element, "Undefined value in <t1>");
578                                 continue;
579                         }
580
581                         if((*iter)->get_name()!="vector")
582                         {
583                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
584                                 continue;
585                         }
586
587                         seg.t1=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
588                 }
589                 else
590                 if(child->get_name()=="p2")
591                 {
592                         xmlpp::Element::NodeList list = child->get_children();
593                         xmlpp::Element::NodeList::iterator iter;
594
595                         // Search for the first non-text XML element
596                         for(iter = list.begin(); iter != list.end(); ++iter)
597                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
598
599                         if(iter==list.end())
600                         {
601                                 error(element, "Undefined value in <p2>");
602                                 continue;
603                         }
604
605                         if((*iter)->get_name()!="vector")
606                         {
607                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
608                                 continue;
609                         }
610
611                         seg.p2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
612                 }
613                 else
614                 if(child->get_name()=="t2")
615                 {
616                         xmlpp::Element::NodeList list = child->get_children();
617                         xmlpp::Element::NodeList::iterator iter;
618
619                         // Search for the first non-text XML element
620                         for(iter = list.begin(); iter != list.end(); ++iter)
621                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
622
623                         if(iter==list.end())
624                         {
625                                 error(element, "Undefined value in <t2>");
626                                 continue;
627                         }
628
629                         if((*iter)->get_name()!="vector")
630                         {
631                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
632                                 continue;
633                         }
634
635                         seg.t2=parse_vector(dynamic_cast<xmlpp::Element*>(*iter));
636                 }
637                 else
638                         error_unexpected_element(child,child->get_name());
639         }
640         return seg;
641 }
642
643 BLinePoint
644 CanvasParser::parse_bline_point(xmlpp::Element *element)
645 {
646         assert(element->get_name()=="bline_point");
647         if(element->get_children().empty())
648         {
649                 error(element, "Undefined value in <bline_point>");
650                 return BLinePoint();
651         }
652
653         BLinePoint ret;
654         ret.set_split_tangent_flag(false);
655
656         xmlpp::Element::NodeList list = element->get_children();
657         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
658         {
659                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
660                 if(!child)
661                         continue;
662                 else
663                 // Vertex
664                 if(child->get_name()[0]=='v' || child->get_name()=="p1")
665                 {
666                         xmlpp::Element::NodeList list = child->get_children();
667                         xmlpp::Element::NodeList::iterator iter;
668
669                         // Search for the first non-text XML element
670                         for(iter = list.begin(); iter != list.end(); ++iter)
671                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
672
673                         if(iter==list.end())
674                         {
675                                 error(element, "Undefined value in <vertex>");
676                                 continue;
677                         }
678
679                         if((*iter)->get_name()!="vector")
680                         {
681                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
682                                 continue;
683                         }
684
685                         ret.set_vertex(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
686                 }
687                 else
688                 // Tangent 1
689                 if(child->get_name()=="t1" || child->get_name()=="tangent")
690                 {
691                         xmlpp::Element::NodeList list = child->get_children();
692                         xmlpp::Element::NodeList::iterator iter;
693
694                         // Search for the first non-text XML element
695                         for(iter = list.begin(); iter != list.end(); ++iter)
696                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
697
698                         if(iter==list.end())
699                         {
700                                 error(element, "Undefined value in <t1>");
701                                 continue;
702                         }
703
704                         if((*iter)->get_name()!="vector")
705                         {
706                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
707                                 continue;
708                         }
709
710                         ret.set_tangent1(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
711                 }
712                 else
713                 // Tangent 2
714                 if(child->get_name()=="t2")
715                 {
716                         xmlpp::Element::NodeList list = child->get_children();
717                         xmlpp::Element::NodeList::iterator iter;
718
719                         // Search for the first non-text XML element
720                         for(iter = list.begin(); iter != list.end(); ++iter)
721                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
722
723                         if(iter==list.end())
724                         {
725                                 error(element, "Undefined value in <t2>");
726                                 continue;
727                         }
728
729                         if((*iter)->get_name()!="vector")
730                         {
731                                 error_unexpected_element((*iter),(*iter)->get_name(),"vector");
732                                 continue;
733                         }
734
735                         ret.set_tangent2(parse_vector(dynamic_cast<xmlpp::Element*>(*iter)));
736                         ret.set_split_tangent_flag(true);
737                 }
738                 else
739                 // width
740                 if(child->get_name()=="width")
741                 {
742                         xmlpp::Element::NodeList list = child->get_children();
743                         xmlpp::Element::NodeList::iterator iter;
744
745                         // Search for the first non-text XML element
746                         for(iter = list.begin(); iter != list.end(); ++iter)
747                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
748
749                         if(iter==list.end())
750                         {
751                                 error(element, "Undefined value in <width>");
752                                 continue;
753                         }
754
755                         if((*iter)->get_name()!="real")
756                         {
757                                 error_unexpected_element((*iter),(*iter)->get_name(),"real");
758                                 continue;
759                         }
760
761                         ret.set_width(parse_real(dynamic_cast<xmlpp::Element*>(*iter)));
762                 }
763                 else
764                 // origin
765                 if(child->get_name()=="origin")
766                 {
767                         xmlpp::Element::NodeList list = child->get_children();
768                         xmlpp::Element::NodeList::iterator iter;
769
770                         // Search for the first non-text XML element
771                         for(iter = list.begin(); iter != list.end(); ++iter)
772                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
773
774                         if(iter==list.end())
775                         {
776                                 error(element, "Undefined value in <origin>");
777                                 continue;
778                         }
779
780                         if((*iter)->get_name()!="real")
781                         {
782                                 error_unexpected_element((*iter),(*iter)->get_name(),"real");
783                                 continue;
784                         }
785
786                         ret.set_origin(parse_real(dynamic_cast<xmlpp::Element*>(*iter)));
787                 }
788                 else
789                         error_unexpected_element(child,child->get_name());
790         }
791         return ret;
792 }
793
794 Angle
795 CanvasParser::parse_angle(xmlpp::Element *element)
796 {
797         assert(element->get_name()=="angle");
798
799         if(!element->get_children().empty())
800                 warning(element, strprintf(_("<%s> should not contain anything"),"angle"));
801
802         if(!element->get_attribute("value"))
803         {
804                 error(element,strprintf(_("<%s> is missing \"value\" attribute"),"angle"));
805                 return Angle();
806         }
807
808         string val=element->get_attribute("value")->get_value();
809
810         return Angle::deg(atof(val.c_str()));
811 }
812
813 ValueBase
814 CanvasParser::parse_value(xmlpp::Element *element,Canvas::Handle canvas)
815 {
816         if(element->get_name()=="real")
817                 return parse_real(element);
818         else
819         if(element->get_name()=="time")
820                 return parse_time(element,canvas);
821         else
822         if(element->get_name()=="integer")
823                 return parse_integer(element);
824         else
825         if(element->get_name()=="string")
826                 return parse_string(element);
827         else
828         if(element->get_name()=="vector")
829                 return parse_vector(element);
830         else
831         if(element->get_name()=="color")
832                 return parse_color(element);
833         else
834         if(element->get_name()=="segment")
835                 return parse_segment(element);
836         else
837         if(element->get_name()=="list")
838                 return parse_list(element,canvas);
839         else
840         if(element->get_name()=="gradient")
841                 return parse_gradient(element);
842         else
843         if(element->get_name()=="bool")
844                 return parse_bool(element);
845         else
846         //if(element->get_name()=="canvas")
847         //      return parse_canvas(element,canvas,true);       // inline canvas
848         //else
849         if(element->get_name()=="angle" || element->get_name()=="degrees" || element->get_name()=="radians" || element->get_name()=="rotations")
850                 return parse_angle(element);
851         else
852         if(element->get_name()=="bline_point")
853                 return parse_bline_point(element);
854         else
855         if(element->get_name()=="canvas")
856                 return ValueBase(parse_canvas(element,canvas,true));
857         else
858         {
859                 DEBUGPOINT();
860                 error_unexpected_element(element,element->get_name());
861         }
862
863         return ValueBase();
864 }
865
866
867
868
869 ValueNode_Animated::Handle
870 CanvasParser::parse_animated(xmlpp::Element *element,Canvas::Handle canvas)
871 {
872         assert(element->get_name()=="hermite" || element->get_name()=="animated");
873
874         if(!element->get_attribute("type"))
875         {
876                 error(element,"Missing attribute \"type\" in <animated>");
877                 return ValueNode_Animated::Handle();
878         }
879
880         ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
881
882         if(!type)
883         {
884                 error(element,"Bad type in <animated>");
885                 return ValueNode_Animated::Handle();
886         }
887
888         ValueNode_Animated::Handle value_node=ValueNode_Animated::create(type);
889
890         if(!value_node)
891         {
892                 error(element,strprintf(_("Unable to create <animated> with type \"%s\""),ValueBase::type_name(type).c_str()));
893                 return ValueNode_Animated::Handle();
894         }
895
896         value_node->set_root_canvas(canvas->get_root());
897
898         xmlpp::Element::NodeList list = element->get_children();
899         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
900         {
901                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
902                 if(!child)
903                         continue;
904                 else
905                 if(child->get_name()=="waypoint")
906                 {
907                         if(!child->get_attribute("time"))
908                         {
909                                 error(child,_("<waypoint> is missing attribute \"time\""));
910                                 continue;
911                         }
912
913                         Time time(child->get_attribute("time")->get_value(),canvas->rend_desc().get_frame_rate());
914
915
916                         ValueNode::Handle waypoint_value_node;
917                         xmlpp::Element::NodeList list = child->get_children();
918
919                         if(child->get_attribute("use"))
920                         {
921                                 if(!list.empty())
922                                         warning(child,_("Found \"use\" attribute for <waypoint>, but it wasn't empty. Ignoring contents..."));
923
924                                 // the waypoint might look like this, in which case we won't find "mycanvas" in the list of valuenodes, 'cos it's a canvas
925                                 //
926                                 //      <animated type="canvas">
927                                 //        <waypoint time="0s" use="mycanvas"/>
928                                 //      </animated>
929                                 if (type==ValueBase::TYPE_CANVAS)
930                                         waypoint_value_node=ValueNode_Const::create(canvas->surefind_canvas(child->get_attribute("use")->get_value()));
931                                 else
932                                         waypoint_value_node=canvas->surefind_value_node(child->get_attribute("use")->get_value());
933                         }
934                         else
935                         {
936                                 if(child->get_children().empty())
937                                 {
938                                         error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
939                                         continue;
940                                 }
941
942                                 xmlpp::Element::NodeList::iterator iter;
943
944                                 // Search for the first non-text XML element
945                                 for(iter = list.begin(); iter != list.end(); ++iter)
946                                         if(dynamic_cast<xmlpp::Element*>(*iter)) break;
947
948                                 if(iter==list.end())
949                                 {
950                                         error(child, strprintf(_("<%s> is missing its data"),"waypoint"));
951                                         continue;
952                                 }
953
954                                 waypoint_value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
955
956                                 /*
957                                 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
958
959                                 if(!data.is_valid())
960                                 {
961                                         error(child,_("Bad data for <waypoint>"));
962                                         continue;
963                                 }
964                                 */
965                                 if(!waypoint_value_node)
966                                 {
967                                         error(child,_("Bad data for <waypoint>"));
968                                         continue;
969                                 }
970
971                                 /*! HACK -- This is a temporary fix to help repair some
972                                 **      weirdness that is currently going on (10-21-2004).
973                                 **      This short circuits the linking of waypoints,
974                                 **      a feature which is so obscure that we can get
975                                 **      away with something like this pretty easily.
976                                 */
977                                 waypoint_value_node=waypoint_value_node->clone();
978
979                                 // Warn if there is trash after the param value
980                                 for(iter++; iter != list.end(); ++iter)
981                                         if(dynamic_cast<xmlpp::Element*>(*iter))
982                                                 warning((*iter),strprintf(_("Unexpected element <%s> after <waypoint> data, ignoring..."),(*iter)->get_name().c_str()));
983                         }
984
985
986                         try {
987                                 ValueNode_Animated::WaypointList::iterator waypoint=value_node->new_waypoint(time,waypoint_value_node);
988
989                         if(child->get_attribute("tension"))
990                         {
991                                 synfig::String str(child->get_attribute("tension")->get_value());
992                                 waypoint->set_tension(atof(str.c_str()));
993                         }
994                         if(child->get_attribute("temporal-tension"))
995                         {
996                                 synfig::String str(child->get_attribute("temporal-tension")->get_value());
997                                 waypoint->set_time_tension(atof(str.c_str()));
998                         }
999                         if(child->get_attribute("continuity"))
1000                         {
1001                                 synfig::String str(child->get_attribute("continuity")->get_value());
1002                                 waypoint->set_continuity(atof(str.c_str()));
1003                         }
1004                         if(child->get_attribute("bias"))
1005                         {
1006                                 synfig::String str(child->get_attribute("bias")->get_value());
1007                                 waypoint->set_bias(atof(str.c_str()));
1008                         }
1009
1010                         if(child->get_attribute("before"))
1011                         {
1012                                 string val=child->get_attribute("before")->get_value();
1013                                 if(val=="halt")
1014                                         waypoint->set_before(INTERPOLATION_HALT);
1015                                 else if(val=="constant")
1016                                         waypoint->set_before(INTERPOLATION_CONSTANT);
1017                                 else if(val=="linear")
1018                                         waypoint->set_before(INTERPOLATION_LINEAR);
1019                                 else if(val=="manual")
1020                                         waypoint->set_before(INTERPOLATION_MANUAL);
1021                                 else if(val=="auto")
1022                                         waypoint->set_before(INTERPOLATION_TCB);
1023                                 else
1024                                         error(child,strprintf(_("\"%s\" not a valid value for attribute \"%s\" in <%s>"),val.c_str(),"before","waypoint"));
1025                         }
1026
1027                         if(child->get_attribute("after"))
1028                         {
1029                                 string val=child->get_attribute("after")->get_value();
1030                                 if(val=="halt")
1031                                         waypoint->set_after(INTERPOLATION_HALT);
1032                                 else if(val=="constant")
1033                                         waypoint->set_after(INTERPOLATION_CONSTANT);
1034                                 else if(val=="linear")
1035                                         waypoint->set_after(INTERPOLATION_LINEAR);
1036                                 else if(val=="manual")
1037                                         waypoint->set_after(INTERPOLATION_MANUAL);
1038                                 else if(val=="auto")
1039                                         waypoint->set_after(INTERPOLATION_TCB);
1040                                 else
1041                                         error(child,strprintf(_("\"%s\" not a valid value for attribute \"%s\" in <%s>"),val.c_str(),"before","waypoint"));
1042                         }
1043                         }
1044                         catch(Exception::BadTime x)
1045                         {
1046                                 warning(child,x.what());
1047                         }
1048                         continue;
1049
1050                 }
1051                 else
1052                         error_unexpected_element(child,child->get_name());
1053         }
1054
1055         // in canvas version 0.1, angles used to wrap, so to get from -179
1056         // degrees to 180 degrees meant a 1 degree change
1057         // in canvas version 0.2 they don't, so that's a 359 degree change
1058
1059         // when loading a version 0.1 canvas, modify constant angle
1060         // waypoints to that they are within 180 degrees of the previous
1061         // waypoint's value
1062         if (type == ValueBase::TYPE_ANGLE) 
1063         {
1064                 Canvas::Handle parent = canvas;
1065                 while (!parent->is_root())
1066                         parent = parent->parent();
1067
1068                 if (parent->get_version() == "0.1")
1069                 {
1070                         bool first = true;
1071                         Real angle, prev = 0;
1072                         WaypointList &wl = value_node->waypoint_list();
1073                         for (WaypointList::iterator iter = wl.begin(); iter != wl.end(); iter++)
1074                         {
1075                                 angle = Angle::deg(iter->get_value(iter->get_time()).get(Angle())).get();
1076                                 if (first)
1077                                         first = false;
1078                                 else if (iter->get_value_node()->get_name() == "constant")
1079                                         if (angle - prev > 180)
1080                                         {
1081                                                 while (angle - prev > 180) angle -= 360;
1082                                                 iter->set_value(Angle::deg(angle));
1083                                         }
1084                                         else if (prev - angle > 180)
1085                                         {
1086                                                 while (prev - angle > 180) angle += 360;
1087                                                 iter->set_value(Angle::deg(angle));
1088                                         }
1089                                 prev = angle;
1090                         }
1091                 }
1092         }
1093
1094         value_node->changed();
1095         return value_node;
1096 }
1097
1098 etl::handle<LinkableValueNode>
1099 CanvasParser::parse_linkable_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1100 {
1101         handle<LinkableValueNode> value_node;
1102         ValueBase::Type type;
1103
1104         // Determine the type
1105         if(element->get_attribute("type"))
1106         {
1107                 type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1108
1109                 if(!type)
1110                 {
1111                         error(element,"Bad type in ValueNode");
1112                         return 0;
1113                 }
1114         }
1115         else
1116         {
1117                 error(element,"Missing type in ValueNode");
1118                 return 0;
1119         }
1120
1121         value_node=LinkableValueNode::create(element->get_name(),type);
1122
1123         if(!value_node)
1124         {
1125                 error(element,"Unknown ValueNode type "+element->get_name());
1126                 return 0;
1127         }
1128
1129         if(value_node->get_type()!=type)
1130         {
1131                 error(element,"ValueNode did not accept type");
1132                 return 0;
1133         }
1134
1135         value_node->set_root_canvas(canvas->get_root());
1136
1137         int i;
1138         for(i=0;i<value_node->link_count();i++)
1139         {
1140                 if(element->get_attribute(value_node->link_name(i)))
1141                 try {
1142                         String id(element->get_attribute(value_node->link_name(i))->get_value());
1143
1144                         if(!value_node->set_link(i,
1145                                         canvas->surefind_value_node(
1146                                                 id
1147                                         )
1148                                 )
1149                         ) error(element,strprintf(_("Unable to set link \"%s\" to ValueNode \"%s\" (link #%d in \"%s\")"),value_node->link_name(i).c_str(),id.c_str(),i,value_node->get_name().c_str()));
1150                 }
1151                 catch(Exception::IDNotFound)
1152                 {
1153                         error(element,"Unable to resolve "+element->get_attribute(value_node->link_name(i))->get_value());
1154                 }
1155                 catch(Exception::FileNotFound)
1156                 {
1157                         error(element,"Unable to open file referenced in "+element->get_attribute(value_node->link_name(i))->get_value());
1158                 }
1159                 catch(...)
1160                 {
1161                         error(element,strprintf(_("Unknown Exception thrown when referencing ValueNode \"%s\""),
1162                                 element->get_attribute(value_node->link_name(i))->get_value().c_str()));
1163                         throw;
1164                 }
1165         }
1166
1167
1168
1169         xmlpp::Element::NodeList list = element->get_children();
1170         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1171         {
1172                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1173                 try
1174                 {
1175                         if(!child)
1176                                 continue;
1177                         int index=value_node->get_link_index_from_name(child->get_name());
1178
1179                         xmlpp::Element::NodeList list = child->get_children();
1180                         xmlpp::Element::NodeList::iterator iter;
1181
1182                         // Search for the first non-text XML element
1183                         for(iter = list.begin(); iter != list.end(); ++iter)
1184                                 if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1185
1186                         if(iter==list.end())
1187                         {
1188                                 error(child,_("element is missing its contents"));
1189                                 continue;
1190                         }
1191
1192                         ValueNode::Handle link=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1193
1194                         if(!link)
1195                         {
1196                                 error((*iter),"Parse of ValueNode failed");
1197                         }
1198                         else
1199                         if(!value_node->set_link(index,link))
1200                         {
1201                                 //error(dynamic_cast<xmlpp::Element*>(*iter),strprintf("Unable to connect value node ('%s' of type '%s') to link %d",link->get_name().c_str(),ValueBase::type_name(link->get_type()).c_str(),index));
1202                                 error(element,strprintf("Unable to connect value node ('%s' of type '%s') to link %d",link->get_name().c_str(),ValueBase::type_name(link->get_type()).c_str(),index));
1203                         }
1204
1205                         // \todo do a search for more elements and warn if they are found
1206
1207                 }
1208                 catch(Exception::BadLinkName)
1209                 {
1210                         error_unexpected_element(child,child->get_name());
1211                 }
1212                 catch(...)
1213                 {
1214                         error(child,strprintf(_("Unknown Exception thrown when working on element \"%s\""),child->get_name().c_str()));
1215                         throw;
1216                 }
1217         }
1218
1219         return value_node;
1220 }
1221
1222 handle<ValueNode_Composite>
1223 CanvasParser::parse_composite(xmlpp::Element *element,Canvas::Handle canvas)
1224 {
1225         assert(element->get_name()=="composite");
1226
1227         if(!element->get_attribute("type"))
1228         {
1229                 error(element,"Missing attribute \"type\" in <composite>");
1230                 return handle<ValueNode_Composite>();
1231         }
1232
1233         ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1234
1235         if(!type)
1236         {
1237                 error(element,"Bad type in <composite>");
1238                 return handle<ValueNode_Composite>();
1239         }
1240
1241         handle<ValueNode_Composite> value_node=ValueNode_Composite::create(type);
1242         handle<ValueNode> c[6];
1243
1244         if(!value_node)
1245         {
1246                 error(element,strprintf(_("Unable to create <composite>")));
1247                 return handle<ValueNode_Composite>();
1248         }
1249
1250         int i;
1251
1252         for(i=0;i<value_node->link_count();i++)
1253         {
1254                 string name=strprintf("c%d",i+1);
1255                 if(c[i])
1256                 {
1257                         error(element,name+" was already defined in <composite>");
1258                         continue;
1259                 }
1260                 if(element->get_attribute(name))
1261                 {
1262                         c[i]=canvas->surefind_value_node(element->get_attribute(name)->get_value());
1263                         if(c[i])
1264                         {
1265                                 if(!value_node->set_link(i,c[i]))
1266                                 {
1267                                         error(element,'"'+name+"\" attribute in <composite> has bad type");
1268                                 }
1269                         }
1270                         else
1271                                 error(element,'"'+name+"\" attribute in <composite> references unknown ID");
1272                 }
1273         }
1274
1275         xmlpp::Element::NodeList list = element->get_children();
1276         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1277         {
1278                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1279                 if(!child)
1280                         continue;
1281                 else
1282                 for(i=0;i<value_node->link_count();i++)
1283                 {
1284                         string name=strprintf("c%d",i+1);
1285                         if(child->get_name()==name)
1286                         {
1287                                 if(c[i])
1288                                 {
1289                                         error(child,name+" was already defined in <composite>");
1290                                         break;
1291                                 }
1292
1293                                 xmlpp::Element::NodeList list = child->get_children();
1294                                 xmlpp::Element::NodeList::iterator iter;
1295
1296                                 // Search for the first non-text XML element
1297                                 for(iter = list.begin(); iter != list.end(); ++iter)
1298                                         if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1299
1300                                 if(iter==list.end())
1301                                 {
1302                                         error(child,strprintf(_("<%s> is missing its contents"),name.c_str()));
1303                                         break;
1304                                 }
1305
1306                                 c[i]=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1307
1308                                 if(!c[i])
1309                                 {
1310                                         error((*iter),"Parse of "+name+" ValueNode failed");
1311                                         break;
1312                                 }
1313
1314                                 if(!value_node->set_link(i,c[i]))
1315                                 {
1316                                         error(child,strprintf(_("<%s> has a bad value"),name.c_str()));
1317                                         break;
1318                                 }
1319
1320                                 // \todo do a search for more elements and warn if they are found
1321                                 break;
1322                         }
1323                 }
1324                 // somewhat of a hack, but it works
1325                 if(i==value_node->link_count()) error_unexpected_element(child,child->get_name());
1326         }
1327
1328         switch(value_node->link_count())
1329         {
1330         case 1:
1331                 if(!value_node->get_link(0))
1332                 {
1333                         error(element,"<composite> is missing parameters");
1334                         return handle<ValueNode_Composite>();
1335                 }
1336                 break;
1337         case 2:
1338                 if(!value_node->get_link(0) ||!value_node->get_link(1))
1339                 {
1340                         error(element,"<composite> is missing parameters");
1341                         return handle<ValueNode_Composite>();
1342                 }
1343                 break;
1344         case 3:
1345                 if(!value_node->get_link(0) ||!value_node->get_link(1) ||!value_node->get_link(2))
1346                 {
1347                         error(element,"<composite> is missing parameters");
1348                         return handle<ValueNode_Composite>();
1349                 }
1350                 break;
1351         case 4:
1352                 if(!value_node->get_link(0) ||!value_node->get_link(1) ||!value_node->get_link(2) ||!value_node->get_link(3))
1353                 {
1354                         error(element,"<composite> is missing parameters");
1355                         return handle<ValueNode_Composite>();
1356                 }
1357                 break;
1358     }
1359         return value_node;
1360 }
1361
1362 // This will also parse a bline
1363 handle<ValueNode_DynamicList>
1364 CanvasParser::parse_dynamic_list(xmlpp::Element *element,Canvas::Handle canvas)
1365 {
1366         assert(element->get_name()=="dynamic_list" || element->get_name()=="bline");
1367
1368         const float fps(canvas?canvas->rend_desc().get_frame_rate():0);
1369
1370         if(!element->get_attribute("type"))
1371         {
1372                 error(element,"Missing attribute \"type\" in <dynamic_list>");
1373                 return handle<ValueNode_DynamicList>();
1374         }
1375
1376         ValueBase::Type type=ValueBase::ident_type(element->get_attribute("type")->get_value());
1377
1378         if(!type)
1379         {
1380                 error(element,"Bad type in <dynamic_list>");
1381                 return handle<ValueNode_DynamicList>();
1382         }
1383
1384         handle<ValueNode_DynamicList> value_node;
1385         handle<ValueNode_BLine> bline_value_node;
1386
1387         if(element->get_name()=="bline")
1388         {
1389                 value_node=bline_value_node=ValueNode_BLine::create();
1390                 if(element->get_attribute("loop"))
1391                 {
1392                         String loop=element->get_attribute("loop")->get_value();
1393                         if(loop=="true" || loop=="1" || loop=="TRUE" || loop=="True")
1394                                 bline_value_node->set_loop(true);
1395                         else
1396                                 bline_value_node->set_loop(false);
1397                 }
1398
1399         }
1400         else
1401                 value_node=ValueNode_DynamicList::create(type);
1402
1403         if(!value_node)
1404         {
1405                 error(element,strprintf(_("Unable to create <dynamic_list>")));
1406                 return handle<ValueNode_DynamicList>();
1407         }
1408
1409         value_node->set_root_canvas(canvas->get_root());
1410
1411         xmlpp::Element::NodeList list = element->get_children();
1412         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1413         {
1414                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1415                 if(!child)
1416                         continue;
1417                 else
1418                 if(child->get_name()=="entry")
1419                 {
1420                         ValueNode_DynamicList::ListEntry list_entry;
1421
1422                         // Parse begin/end waypoints
1423                         {
1424                                 typedef synfig::ValueNode_DynamicList::ListEntry::Activepoint Activepoint;
1425                                 typedef synfig::ValueNode_DynamicList::ListEntry::ActivepointList ActivepointList;
1426
1427                                 String begin_sequence;
1428                                 String end_sequence;
1429
1430                                 ActivepointList &timing_info(list_entry.timing_info);
1431
1432                                 if(child->get_attribute("begin"))
1433                                         begin_sequence=child->get_attribute("begin")->get_value();
1434
1435                                 if(child->get_attribute("on"))
1436                                         begin_sequence=child->get_attribute("on")->get_value();
1437
1438                                 if(child->get_attribute("end"))
1439                                         end_sequence=child->get_attribute("end")->get_value();
1440
1441                                 if(child->get_attribute("off"))
1442                                         end_sequence=child->get_attribute("off")->get_value();
1443
1444                                 // clear out any auto-start
1445                                 if(!begin_sequence.empty())
1446                                         timing_info.clear();
1447
1448                                 //! \optimize
1449                                 while(!begin_sequence.empty())
1450                                 {
1451                                         String::iterator iter(find(begin_sequence.begin(),begin_sequence.end(),','));
1452                                         String timecode(begin_sequence.begin(), iter);
1453                                         int priority=0;
1454
1455                                         // skip whitespace before checking for a priority
1456                                         while (isspace(timecode[0]))
1457                                                 timecode=timecode.substr(1);
1458
1459                                         // If there is a priority, then grab it and remove
1460                                         // it from the timecode
1461                                         if(timecode[0]=='p')
1462                                         {
1463                                                 //priority=timecode[1]-'0';
1464                                                 //timecode=String(timecode.begin()+3,timecode.end());
1465                                                 int space=timecode.find_first_of(' ');
1466                                                 priority=atoi(String(timecode,1,space-1).c_str());
1467                                                 timecode=String(timecode.begin()+space+1,timecode.end());
1468                                                 //synfig::info("priority: %d      timecode: %s",priority,timecode.c_str());
1469                                         }
1470
1471                                         timing_info.push_back(
1472                                                 Activepoint(
1473                                                         Time(
1474                                                                 timecode,
1475                                                                 fps
1476                                                         ),
1477                                                         true,   // Mark as a "on" activepoint
1478                                                         priority
1479                                                 )
1480                                         );
1481
1482                                         if(iter==begin_sequence.end())
1483                                                 begin_sequence.clear();
1484                                         else
1485                                                 begin_sequence=String(iter+1,begin_sequence.end());
1486                                 }
1487
1488                                 //! \optimize
1489                                 while(!end_sequence.empty())
1490                                 {
1491                                         String::iterator iter(find(end_sequence.begin(),end_sequence.end(),','));
1492                                         String timecode(end_sequence.begin(),   iter);
1493                                         int priority=0;
1494
1495                                         // skip whitespace before checking for a priority
1496                                         while (isspace(timecode[0]))
1497                                                 timecode=timecode.substr(1);
1498
1499                                         // If there is a priority, then grab it and remove
1500                                         // it from the timecode
1501                                         if(timecode[0]=='p')
1502                                         {
1503                                                 //priority=timecode[1]-'0';
1504                                                 //timecode=String(timecode.begin()+3,timecode.end());
1505                                                 int space=timecode.find_first_of(' ');
1506                                                 priority=atoi(String(timecode,1,space-1).c_str());
1507                                                 timecode=String(timecode.begin()+space+1,timecode.end());
1508                                                 //synfig::info("priority: %d      timecode: %s",priority,timecode.c_str());
1509                                         }
1510
1511                                         timing_info.push_back(
1512                                                 Activepoint(
1513                                                         Time(
1514                                                                 timecode,
1515                                                                 fps
1516                                                         ),
1517                                                         false,  // Mark as a "off" activepoint
1518                                                         priority
1519                                                 )
1520                                         );
1521                                         if(iter==end_sequence.end())
1522                                                 end_sequence.clear();
1523                                         else
1524                                                 end_sequence=String(iter+1,end_sequence.end());
1525                                 }
1526
1527                                 timing_info.sort();
1528                         }
1529
1530                         if(child->get_attribute("use"))
1531                         {
1532                                 // \todo does this need to be able to read 'use="canvas"', like waypoints can now?  (see 'surefind_canvas' in this file)
1533                                 string id=child->get_attribute("use")->get_value();
1534                                 try
1535                                 {
1536                                         list_entry.value_node=canvas->surefind_value_node(id);
1537                                 }
1538                                 catch(Exception::IDNotFound)
1539                                 {
1540                                         error(child,"\"use\" attribute in <entry> references unknown ID -- "+id);
1541                                         continue;
1542                                 }
1543                         }
1544                         else
1545                         {
1546                                 xmlpp::Element::NodeList list = child->get_children();
1547                                 xmlpp::Element::NodeList::iterator iter;
1548
1549                                 // Search for the first non-text XML element
1550                                 for(iter = list.begin(); iter != list.end(); ++iter)
1551                                         if(dynamic_cast<xmlpp::Element*>(*iter)) break;
1552
1553                                 if(iter==list.end())
1554                                 {
1555                                         error(child,strprintf(_("<entry> is missing its contents or missing \"use\" element")));
1556                                         continue;
1557                                 }
1558
1559                                 list_entry.value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1560
1561                                 if(!list_entry.value_node)
1562                                         error((*iter),"Parse of ValueNode failed");
1563
1564                                 // \todo do a search for more elements and warn if they are found
1565
1566                         }
1567
1568                         value_node->add(list_entry);
1569                         value_node->set_link(value_node->link_count()-1,list_entry.value_node);
1570                 }
1571                 else
1572                         error_unexpected_element(child,child->get_name());
1573         }
1574         return value_node;
1575 }
1576
1577 handle<ValueNode>
1578 CanvasParser::parse_value_node(xmlpp::Element *element,Canvas::Handle canvas)
1579 {
1580         handle<ValueNode> value_node;
1581         assert(element);
1582
1583         GUID guid;
1584
1585         if(element->get_attribute("guid"))
1586         {
1587                 guid=GUID(element->get_attribute("guid")->get_value())^canvas->get_root()->get_guid();
1588                 value_node=guid_cast<ValueNode>(guid);
1589                 if(value_node)
1590                         return value_node;
1591         }
1592
1593         // If ValueBase::ident_type() recognizes the name, then we know it's a ValueBase
1594         if(element->get_name()!="canvas" && ValueBase::ident_type(element->get_name()))
1595         {
1596                 ValueBase data=parse_value(element,canvas);
1597
1598                 if(!data.is_valid())
1599                 {
1600                         error(element,strprintf(_("Bad data in <%s>"),element->get_name().c_str()));
1601                         return value_node;
1602                 }
1603
1604                 // We want to convert this ValueBase into a
1605                 // ValueNode_Const. That way, we can treat the
1606                 // ID like any other Datanode. Think of this
1607                 // as a shorthand for creating constant ValueNodes.
1608
1609                 value_node=ValueNode_Const::create(data);
1610         }
1611         else
1612         if(element->get_name()=="hermite" || element->get_name()=="animated")
1613                 value_node=parse_animated(element,canvas);
1614         else
1615         if(element->get_name()=="composite")
1616                 value_node=parse_composite(element,canvas);
1617         else
1618         if(element->get_name()=="dynamic_list")
1619                 value_node=parse_dynamic_list(element,canvas);
1620         else
1621         if(element->get_name()=="bline") // This is not a typo. The dynamic list parser will parse a bline.
1622                 value_node=parse_dynamic_list(element,canvas);
1623         else
1624         if(LinkableValueNode::book().count(element->get_name()))
1625                 value_node=parse_linkable_value_node(element,canvas);
1626         else
1627         if(element->get_name()=="canvas")
1628                 value_node=ValueNode_Const::create(parse_canvas(element,canvas,true));
1629         else
1630         {
1631                 error_unexpected_element(element,element->get_name());
1632                 error(element, "Expected a ValueNode");
1633                 value_node=PlaceholderValueNode::create();
1634         }
1635
1636
1637         value_node->set_root_canvas(canvas->get_root());
1638
1639
1640         // If we were successful, and our element has
1641         // an ID attribute, go ahead and add it to the
1642         // value_node list
1643         if(value_node && element->get_attribute("id"))
1644         {
1645                 string id=element->get_attribute("id")->get_value();
1646
1647                 //value_node->set_id(id);
1648
1649                 // If there is already a value_node in the list
1650                 // with the same ID, then that is an error
1651                 try { canvas->add_value_node(value_node,id); }
1652                 catch(Exception::BadLinkName)
1653                 {
1654                         warning(element,strprintf(_("Bad ID \"%s\""),id.c_str()));
1655                         return value_node;
1656                 }
1657                 catch(Exception::IDAlreadyExists)
1658                 {
1659                         error(element,strprintf(_("Duplicate ID \"%s\""),id.c_str()));
1660                         return value_node;
1661                 }
1662                 catch(...)
1663                 {
1664                         error(element,strprintf(_("Unknown Exception thrown when adding ValueNode \"%s\""),id.c_str()));
1665                         throw;
1666                 }
1667         }
1668         value_node->set_guid(guid);
1669         return value_node;
1670 }
1671
1672 void
1673 CanvasParser::parse_canvas_defs(xmlpp::Element *element,Canvas::Handle canvas)
1674 {
1675         assert(element->get_name()=="defs");
1676         xmlpp::Element::NodeList list = element->get_children();
1677         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1678         {
1679                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1680                 if(!child)
1681                         continue;
1682                 else
1683                 if(child->get_name()=="canvas")
1684                         parse_canvas(child, canvas);
1685                 else
1686                         parse_value_node(child,canvas);
1687         }
1688 }
1689
1690 Layer::Handle
1691 CanvasParser::parse_layer(xmlpp::Element *element,Canvas::Handle canvas)
1692 {
1693
1694         assert(element->get_name()=="layer");
1695         Layer::Handle layer;
1696
1697         if(!element->get_attribute("type"))
1698         {
1699                 error(element,_("Missing \"type\" attribute to \"layer\" element"));
1700                 return Layer::Handle();
1701         }
1702
1703         layer=Layer::create(element->get_attribute("type")->get_value());
1704         layer->set_canvas(canvas);
1705
1706         if(element->get_attribute("group"))
1707         {
1708                 layer->add_to_group(
1709                         element->get_attribute("group")->get_value()
1710                 );
1711         }
1712
1713         // Handle the version attribute
1714         if(element->get_attribute("version"))
1715         {
1716                 String version(element->get_attribute("version")->get_value());
1717                 if(version>layer->get_version())
1718                         warning(element,_("Installed layer version is smaller than layer version in file"));
1719                 if(version!=layer->get_version())
1720                         layer->set_version(version);
1721         }
1722
1723         // Handle the description
1724         if(element->get_attribute("desc"))
1725                 layer->set_description(element->get_attribute("desc")->get_value());
1726
1727         if(element->get_attribute("active"))
1728                 layer->set_active(element->get_attribute("active")->get_value()=="false"?false:true);
1729
1730         xmlpp::Element::NodeList list = element->get_children();
1731         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1732         {
1733                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1734                 if(!child)
1735                         continue;
1736                 else
1737                 if(child->get_name()=="name")
1738                         warning(child,_("<name> entry for <layer> is not yet supported. Ignoring..."));
1739                 else
1740                 if(child->get_name()=="desc")
1741                         warning(child,_("<desc> entry for <layer> is not yet supported. Ignoring..."));
1742                 else
1743                 if(child->get_name()=="param")
1744                 {
1745                         xmlpp::Element::NodeList list = child->get_children();
1746
1747                         if(!child->get_attribute("name"))
1748                         {
1749                                 error(child,_("Missing \"name\" attribute for <param>."));
1750                                 continue;
1751                         }
1752
1753                         String param_name=child->get_attribute("name")->get_value();
1754
1755                         if(child->get_attribute("use"))
1756                         {
1757                                 // If the "use" attribute is used, then the
1758                                 // element should be empty. Warn the user if
1759                                 // we find otherwise.
1760                                 if(!list.empty())
1761                                         warning(child,_("Found \"use\" attribute for <param>, but it wasn't empty. Ignoring contents..."));
1762
1763                                 String str=     child->get_attribute("use")->get_value();
1764
1765                                 if(layer->get_param(param_name).get_type()==ValueBase::TYPE_CANVAS)
1766                                 {
1767                                         if(!layer->set_param(param_name,canvas->surefind_canvas(str)))
1768                                                 error((*iter),_("Layer rejected canvas link"));
1769                                 }
1770                                 else
1771                                 try
1772                                 {
1773                                         handle<ValueNode> value_node=canvas->surefind_value_node(str);
1774
1775                                         // Assign the value_node to the dynamic parameter list
1776                                         layer->connect_dynamic_param(param_name,value_node);
1777                         }
1778                                 catch(Exception::IDNotFound)
1779                                 {
1780                                         error(child,strprintf(_("Unknown ID (%s) referenced in <param>"),str.c_str()));
1781                                 }
1782
1783                                 continue;
1784                         }
1785
1786                         xmlpp::Element::NodeList::iterator iter;
1787
1788                         // Search for the first non-text XML element
1789                         for(iter = list.begin(); iter != list.end(); ++iter)
1790                                 if(dynamic_cast<xmlpp::Element*>(*iter))
1791                                         break;
1792                                 //if(!(!dynamic_cast<xmlpp::Element*>(*iter) && (*iter)->get_name()=="text"||(*iter)->get_name()=="comment"   )) break;
1793
1794                         if(iter==list.end())
1795                         {
1796                                 error(child,_("<param> is either missing its contents, or missing a \"use\" attribute."));
1797                                 continue;
1798                         }
1799
1800                         // If we recognize the element name as a
1801                         // ValueBase, then treat is at one
1802                         if(/*(*iter)->get_name()!="canvas" && */ValueBase::ident_type((*iter)->get_name()) && !dynamic_cast<xmlpp::Element*>(*iter)->get_attribute("guid"))
1803                         {
1804                                 ValueBase data=parse_value(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1805
1806                                 if(!data.is_valid())
1807                                 {
1808                                         error((*iter),_("Bad data for <param>"));
1809                                         continue;
1810                                 }
1811
1812                                 // Set the layer's parameter, and make sure that
1813                                 // the layer liked it
1814                                 if(!layer->set_param(param_name,data))
1815                                 {
1816                                         warning((*iter),_("Layer rejected value for <param>"));
1817                                         continue;
1818                                 }
1819                         }
1820                         else    // ... otherwise, we assume that it is a ValueNode
1821                         {
1822                                 handle<ValueNode> value_node=parse_value_node(dynamic_cast<xmlpp::Element*>(*iter),canvas);
1823
1824                                 if(!value_node)
1825                                 {
1826                                         error((*iter),_("Bad data for <param>"));
1827                                         continue;
1828                                 }
1829
1830                                 // Assign the value_node to the dynamic parameter list
1831                                 layer->connect_dynamic_param(param_name,value_node);
1832                         }
1833
1834                         // Warn if there is trash after the param value
1835                         for(iter++; iter != list.end(); ++iter)
1836                                 if(dynamic_cast<xmlpp::Element*>(*iter))
1837                                         warning((*iter),strprintf(_("Unexpected element <%s> after <param> data, ignoring..."),(*iter)->get_name().c_str()));
1838                         continue;
1839                 }
1840                 else
1841                         error_unexpected_element(child,child->get_name());
1842         }
1843
1844         layer->reset_version();
1845         return layer;
1846 }
1847
1848 Canvas::Handle
1849 CanvasParser::parse_canvas(xmlpp::Element *element,Canvas::Handle parent,bool inline_, String filename)
1850 {
1851
1852         if(element->get_name()!="canvas")
1853         {
1854                 error_unexpected_element(element,element->get_name(),"canvas");
1855                 return Canvas::Handle();
1856         }
1857         Canvas::Handle canvas;
1858
1859
1860
1861         if(parent && (element->get_attribute("id") || inline_))
1862         {
1863                 if(inline_)
1864                 {
1865                         canvas=Canvas::create_inline(parent);
1866                 }
1867                 else
1868                 {
1869                         try
1870                         {
1871                                 canvas=parent->find_canvas(element->get_attribute("id")->get_value());
1872                         }
1873                         catch(...)
1874                         {
1875                                 canvas=parent->new_child_canvas(element->get_attribute("id")->get_value());
1876                         }
1877                 }
1878                 canvas->rend_desc().clear_flags();
1879         }
1880         else
1881         {
1882                 canvas=Canvas::create();
1883                 if(filename=="/dev/stdin")
1884                         canvas->set_file_name("./stdin.sif");
1885                 else
1886                         canvas->set_file_name(filename);
1887                 canvas->rend_desc().clear_flags();
1888         }
1889
1890         if(element->get_attribute("guid"))
1891         {
1892                 GUID guid(element->get_attribute("guid")->get_value());
1893                 if(guid_cast<Canvas>(guid))
1894                         return guid_cast<Canvas>(guid);
1895                 else
1896                         canvas->set_guid(guid);
1897         }
1898
1899         if(element->get_attribute("version"))
1900                 canvas->set_version(element->get_attribute("version")->get_value());
1901
1902         if(element->get_attribute("width"))
1903                 canvas->rend_desc().set_w(atoi(element->get_attribute("width")->get_value().c_str()));
1904
1905         if(element->get_attribute("height"))
1906                 canvas->rend_desc().set_h(atoi(element->get_attribute("height")->get_value().c_str()));
1907
1908         if(element->get_attribute("xres"))
1909                 canvas->rend_desc().set_x_res(atof(element->get_attribute("xres")->get_value().c_str()));
1910
1911         if(element->get_attribute("yres"))
1912                 canvas->rend_desc().set_y_res(atof(element->get_attribute("yres")->get_value().c_str()));
1913
1914
1915         if(element->get_attribute("fps"))
1916                 canvas->rend_desc().set_frame_rate(atof(element->get_attribute("fps")->get_value().c_str()));
1917
1918         if(element->get_attribute("start-time"))
1919                 canvas->rend_desc().set_time_start(Time(element->get_attribute("start-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1920
1921         if(element->get_attribute("begin-time"))
1922                 canvas->rend_desc().set_time_start(Time(element->get_attribute("begin-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1923
1924         if(element->get_attribute("end-time"))
1925                 canvas->rend_desc().set_time_end(Time(element->get_attribute("end-time")->get_value(),canvas->rend_desc().get_frame_rate()));
1926
1927         if(element->get_attribute("antialias"))
1928                 canvas->rend_desc().set_antialias(atoi(element->get_attribute("antialias")->get_value().c_str()));
1929
1930         if(element->get_attribute("view-box"))
1931         {
1932                 string values=element->get_attribute("view-box")->get_value();
1933                 Vector
1934                         tl,
1935                         br;
1936                 tl[0]=atof(string(values.data(),values.find(' ')).c_str());
1937                 values=string(values.begin()+values.find(' ')+1,values.end());
1938                 tl[1]=atof(string(values.data(),values.find(' ')).c_str());
1939                 values=string(values.begin()+values.find(' ')+1,values.end());
1940                 br[0]=atof(string(values.data(),values.find(' ')).c_str());
1941                 values=string(values.begin()+values.find(' ')+1,values.end());
1942                 br[1]=atof(values.c_str());
1943
1944                 canvas->rend_desc().set_tl(tl);
1945                 canvas->rend_desc().set_br(br);
1946         }
1947
1948         if(element->get_attribute("bgcolor"))
1949         {
1950                 string values=element->get_attribute("bgcolor")->get_value();
1951                 Color bg;
1952
1953                 bg.set_r(atof(string(values.data(),values.find(' ')).c_str()));
1954                 values=string(values.begin()+values.find(' ')+1,values.end());
1955
1956                 bg.set_g(atof(string(values.data(),values.find(' ')).c_str()));
1957                 values=string(values.begin()+values.find(' ')+1,values.end());
1958
1959                 bg.set_b(atof(string(values.data(),values.find(' ')).c_str()));
1960                 values=string(values.begin()+values.find(' ')+1,values.end());
1961
1962                 bg.set_a(atof(values.c_str()));
1963
1964                 canvas->rend_desc().set_bg_color(bg);
1965         }
1966
1967         if(element->get_attribute("focus"))
1968         {
1969                 string values=element->get_attribute("focus")->get_value();
1970                 Vector focus;
1971
1972                 focus[0]=atof(string(values.data(),values.find(' ')).c_str());
1973                 values=string(values.begin()+values.find(' ')+1,values.end());
1974                 focus[1]=atof(values.c_str());
1975
1976                 canvas->rend_desc().set_focus(focus);
1977         }
1978
1979         canvas->rend_desc().set_flags(RendDesc::PX_ASPECT|RendDesc::IM_SPAN);
1980
1981         xmlpp::Element::NodeList list = element->get_children();
1982         for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
1983         {
1984                 xmlpp::Element *child(dynamic_cast<xmlpp::Element*>(*iter));
1985                 if(child)
1986                 {
1987                         if(child->get_name()=="defs")
1988                         {
1989                                 if(canvas->is_inline())
1990                                         error(child,_("Inline canvas cannot have a <defs> section"));
1991                                 parse_canvas_defs(child, canvas);
1992                         }
1993                         else
1994                         if(child->get_name()=="keyframe")
1995                         {
1996                                 if(canvas->is_inline())
1997                                 {
1998                                         warning(child,_("Inline canvas cannot have keyframes"));
1999                                         continue;
2000                                 }
2001
2002                                 canvas->keyframe_list().add(parse_keyframe(child,canvas));
2003                                 canvas->keyframe_list().sync();
2004                         }
2005                         else
2006                         if(child->get_name()=="meta")
2007                         {
2008                                 if(canvas->is_inline())
2009                                 {
2010                                         warning(child,_("Inline canvases cannot have metadata"));
2011                                         continue;
2012                                 }
2013
2014                                 String name,content;
2015
2016                                 if(!child->get_attribute("name"))
2017                                 {
2018                                         warning(child,_("<meta> must have a name"));
2019                                         continue;
2020                                 }
2021
2022                                 if(!child->get_attribute("content"))
2023                                 {
2024                                         warning(child,_("<meta> must have content"));
2025                                         continue;
2026                                 }
2027
2028                                 canvas->set_meta_data(child->get_attribute("name")->get_value(),child->get_attribute("content")->get_value());
2029                         }
2030                         else if(child->get_name()=="name")
2031                         {
2032                                 xmlpp::Element::NodeList list = child->get_children();
2033
2034                                 // If we don't have any name, warn
2035                                 if(list.empty())
2036                                         warning(child,_("blank \"name\" entity"));
2037
2038                                 string tmp;
2039                                 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2040                                         if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2041                                 canvas->set_name(tmp);
2042                         }
2043                         else
2044                         if(child->get_name()=="desc")
2045                         {
2046
2047                                 xmlpp::Element::NodeList list = child->get_children();
2048
2049                                 // If we don't have any description, warn
2050                                 if(list.empty())
2051                                         warning(child,_("blank \"desc\" entity"));
2052
2053                                 string tmp;
2054                                 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2055                                         if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2056                                 canvas->set_description(tmp);
2057                         }
2058                         else
2059                         if(child->get_name()=="author")
2060                         {
2061
2062                                 xmlpp::Element::NodeList list = child->get_children();
2063
2064                                 // If we don't have any description, warn
2065                                 if(list.empty())
2066                                         warning(child,_("blank \"author\" entity"));
2067
2068                                 string tmp;
2069                                 for(xmlpp::Element::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
2070                                         if(dynamic_cast<xmlpp::TextNode*>(*iter))tmp+=dynamic_cast<xmlpp::TextNode*>(*iter)->get_content();
2071                                 canvas->set_author(tmp);
2072                         }
2073                         else
2074                         if(child->get_name()=="layer")
2075                         {
2076                                 //if(canvas->is_inline())
2077                                 //      canvas->push_front(parse_layer(child,canvas->parent()));
2078                                 //else
2079                                         canvas->push_front(parse_layer(child,canvas));
2080                         }
2081                         else
2082                                 error_unexpected_element(child,child->get_name());
2083                 }
2084 //              else
2085 //              if((child->get_name()=="text"||child->get_name()=="comment") && child->has_child_text())
2086 //                      continue;
2087         }
2088
2089         if(canvas->value_node_list().placeholder_count())
2090         {
2091                 String nodes;
2092                 for (ValueNodeList::const_iterator iter = canvas->value_node_list().begin(); iter != canvas->value_node_list().end(); iter++)
2093                         if(PlaceholderValueNode::Handle::cast_dynamic(*iter))
2094                         {
2095                                 if (nodes != "") nodes += ", ";
2096                                 nodes += "'" + (*iter)->get_id() + "'";
2097                         }
2098                 error(element,strprintf(_("Canvas '%s' has undefined %s: %s"),
2099                                                                 canvas->get_id().c_str(),
2100                                                                 canvas->value_node_list().placeholder_count() == 1 ? _("ValueNode") : _("ValueNodes"),
2101                                                                 nodes.c_str()));
2102         }
2103
2104         canvas->set_version(CURRENT_CANVAS_VERSION);
2105         return canvas;
2106 }
2107
2108 Canvas::Handle
2109 CanvasParser::parse_from_file(const String &file)
2110 {
2111         return parse_from_file_as(file,file);
2112 }
2113
2114 Canvas::Handle
2115 CanvasParser::parse_from_file_as(const String &file_,const String &as_)
2116 {
2117         try
2118         {
2119         ChangeLocale change_locale(LC_NUMERIC, "C");
2120                 String file(unix_to_local_path(file_));
2121                 String as(unix_to_local_path(as_));
2122
2123                 if(get_open_canvas_map().count(etl::absolute_path(as)))
2124                         return get_open_canvas_map()[etl::absolute_path(as)];
2125
2126                 filename=as;
2127                 total_warnings_=0;
2128                 xmlpp::DomParser parser(file);
2129                 if(parser)
2130                 {
2131                         Canvas::Handle canvas(parse_canvas(parser.get_document()->get_root_node(),0,false,as));
2132                         get_open_canvas_map()[etl::absolute_path(as)]=canvas;
2133                         canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2134                         canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2135
2136
2137
2138                         const ValueNodeList& value_node_list(canvas->value_node_list());
2139
2140                         again:
2141                         ValueNodeList::const_iterator iter;
2142                         for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2143                         {
2144                                 ValueNode::Handle value_node(*iter);
2145                                 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2146                                 {
2147                                         canvas->remove_value_node(value_node);
2148                                         goto again;
2149                                 }
2150                         }
2151
2152                         return canvas;
2153                 }
2154         }
2155         catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2156         catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2157         catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2158         catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2159         catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2160         catch(const std::exception& ex)
2161         {
2162                 synfig::error("Standard Exception: "+String(ex.what()));
2163                 return Canvas::Handle();
2164         }
2165         catch(const String& str)
2166         {
2167                 cerr<<str<<endl;
2168                 //      synfig::error(str);
2169                 return Canvas::Handle();
2170         }
2171         return Canvas::Handle();
2172 }
2173
2174 Canvas::Handle
2175 CanvasParser::parse_from_string(const String &data)
2176 {
2177         try
2178         {
2179         ChangeLocale change_locale(LC_NUMERIC, "C");
2180                 filename=_("<INTERNAL>");
2181                 total_warnings_=0;
2182                 xmlpp::DomParser parser;
2183                 parser.parse_memory(data);
2184                 xmlpp::Element *root=parser.get_document()->get_root_node();
2185                 if(parser)
2186                 {
2187                         Canvas::Handle canvas(parse_canvas(root));
2188                         canvas->signal_deleted().connect(sigc::bind(sigc::ptr_fun(_remove_from_open_canvas_map),canvas.get()));
2189                         canvas->signal_file_name_changed().connect(sigc::bind(sigc::ptr_fun(_canvas_file_name_changed),canvas.get()));
2190
2191                         const ValueNodeList& value_node_list(canvas->value_node_list());
2192                         again:
2193                         ValueNodeList::const_iterator iter;
2194                         for(iter=value_node_list.begin();iter!=value_node_list.end();++iter)
2195                         {
2196                                 ValueNode::Handle value_node(*iter);
2197                                 if(value_node->is_exported() && value_node->get_id().find("Unnamed")==0)
2198                                 {
2199                                         canvas->remove_value_node(value_node);
2200                                         goto again;
2201                                 }
2202                         }
2203
2204                         return canvas;
2205                 }
2206         }
2207         catch(Exception::BadLinkName) { synfig::error("BadLinkName Thrown"); }
2208         catch(Exception::BadType) { synfig::error("BadType Thrown"); }
2209         catch(Exception::FileNotFound) { synfig::error("FileNotFound Thrown"); }
2210         catch(Exception::IDNotFound) { synfig::error("IDNotFound Thrown"); }
2211         catch(Exception::IDAlreadyExists) { synfig::error("IDAlreadyExists Thrown"); }
2212         catch(const std::exception& ex)
2213         {
2214                 synfig::error("Standard Exception: "+String(ex.what()));
2215                 return Canvas::Handle();
2216         }
2217         catch(const String& str)
2218         {
2219                 cerr<<str<<endl;
2220                 //      synfig::error(str);
2221                 return Canvas::Handle();
2222         }
2223         return Canvas::Handle();
2224 }