Merge branch 'genete_static_values'
[synfig.git] / synfig-core / src / synfig / loadcanvas.h
1 /* === S Y N F I G ========================================================= */
2 /*!     \file loadcanvas.h
3 **      \brief Implementation for the Synfig Canvas Loader (canvas file parser)
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **      Copyright (c) 2009 Carlos A. Sosa Navarro
11 **
12 **      This package is free software; you can redistribute it and/or
13 **      modify it under the terms of the GNU General Public License as
14 **      published by the Free Software Foundation; either version 2 of
15 **      the License, or (at your option) any later version.
16 **
17 **      This package is distributed in the hope that it will be useful,
18 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
19 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 **      General Public License for more details.
21 **      \endlegal
22 */
23 /* ========================================================================= */
24
25 /* === S T A R T =========================================================== */
26
27 #ifndef __SYNFIG_LOADCANVAS_H
28 #define __SYNFIG_LOADCANVAS_H
29
30 /* === H E A D E R S ======================================================= */
31
32 #include "string.h"
33 #include "canvas.h"
34 #include "valuenode.h"
35 #include "vector.h"
36 #include "value.h"
37 #include "valuenode_subtract.h"
38 #include "valuenode_animated.h"
39 #include "valuenode_composite.h"
40 #include "valuenode_dynamiclist.h"
41 #include "keyframe.h"
42 #include "guid.h"
43
44 /* === M A C R O S ========================================================= */
45
46 /* === T Y P E D E F S ===================================================== */
47
48 /* === C L A S S E S & S T R U C T S ======================================= */
49
50 namespace xmlpp { class Node; class Element; };
51
52 namespace synfig {
53
54 /*!     \class CanvasParser
55 **      \brief Class that handles xmlpp elements from a sif file and converts
56 * them into Synfig objects
57 */
58 class CanvasParser
59 {
60         /*
61  --     ** -- D A T A -------------------------------------------------------------
62         */
63
64 private:
65         //! Maximun number of allowed warnings before fatal error is thrown
66         int max_warnings_;
67         //! Total number of warning during canvas parsing
68     int total_warnings_;
69         //! Total number of errors during canvas parsing
70     int total_errors_;
71         //! True if errors doesn't stop canvas parsing
72         bool allow_errors_;
73         //! File name to parse
74         String filename;
75         //! Path of the file name to parse
76         String path;
77         //! Error text when errors found
78         String errors_text;
79         //! Warning text when warnings found
80         String warnings_text;
81         //! Seems not to be used
82         GUID guid_;
83
84         /*
85  --     ** -- C O N S T R U C T O R S ---------------------------------------------
86         */
87
88 public:
89
90         CanvasParser():
91                 max_warnings_   (1000),
92                 total_warnings_ (0),
93                 total_errors_   (0),
94                 allow_errors_   (false)
95         { }
96
97         /*
98  --     ** -- M E M B E R   F U N C T I O N S -------------------------------------
99         */
100
101 public:
102
103         //! Sets allow errors variable
104         CanvasParser &set_allow_errors(bool x) { allow_errors_=x; return *this; }
105
106         //! Sets the maximum number of warnings before a fatal error is thrown
107         CanvasParser &set_max_warnings(int i) { max_warnings_=i; return *this; }
108
109         //! Returns the maximum number of warnings before a fatal_error is thrown
110         int get_max_warnings() { return max_warnings_; }
111
112         //! Returns the number of errors in the last parse
113         int error_count()const { return total_errors_; }
114
115         //! Returns the number of warnings in the last parse
116         int warning_count()const { return total_warnings_; }
117
118         //! Sets the path of the file to parse
119         void set_path(const synfig::String& x) { path=x; }
120
121         //! Gets the path of the file to parse
122         const synfig::String& get_path()const { return path; }
123
124         //! Gets error text string
125         const synfig::String& get_errors_text()const { return errors_text; }
126         //! Gets warning text string
127         const synfig::String& get_warnings_text()const { return warnings_text; }
128
129         //! Register a canvas in the canvas map
130         /*! \param canvas The handle to the canvas to register
131          *  \param as The absolute path to the file that represents the canvas
132          * Apart of store the pair canvas and */
133         static void register_canvas_in_map(Canvas::Handle canvas, String as);
134
135 #ifdef _DEBUG
136         static void show_canvas_map(String file, int line, String text);
137 #endif  // _DEBUG
138
139         //! Parse a Cavnas form a file with absolute path.
140         Canvas::Handle parse_from_file_as(const String &filename,const String &as,String &errors);
141         //! Parse a Canvas from a xmlpp root node
142         Canvas::Handle parse_as(xmlpp::Element* node,String &errors);
143
144         //! Set of absolute file names of the canvases currently being parsed
145         static std::set<String> loading_;
146
147 private:
148
149         //! Error handling function
150         void error(xmlpp::Node *node,const String &text);
151         //! Fatal Error handling function
152         void fatal_error(xmlpp::Node *node,const String &text);
153         //! Warning handling function
154         void warning(xmlpp::Node *node,const String &text);
155         //! Unexpected element error handling function
156         void error_unexpected_element(xmlpp::Node *node,const String &got, const String &expected);
157         //! Unexpected element error handling function
158         void error_unexpected_element(xmlpp::Node *node,const String &got);
159
160         //! Canvas Parsing Function
161         Canvas::Handle parse_canvas(xmlpp::Element *node,Canvas::Handle parent=0,bool inline_=false, String path=".");
162         //! Canvas definitions Parsing Function (exported value nodes and exported canvases)
163         void parse_canvas_defs(xmlpp::Element *node,Canvas::Handle canvas);
164         //! Layer Parsing Function
165         etl::handle<Layer> parse_layer(xmlpp::Element *node,Canvas::Handle canvas);
166         //! Generic Value Base Parsing Function
167         ValueBase parse_value(xmlpp::Element *node,Canvas::Handle canvas);
168         //! Generic Value Node Parsing Function
169         etl::handle<ValueNode> parse_value_node(xmlpp::Element *node,Canvas::Handle canvas);
170
171         //! Real Value Base Parsing Function
172         Real parse_real(xmlpp::Element *node);
173         //! Time Value Base Parsing Function
174         Time parse_time(xmlpp::Element *node,Canvas::Handle canvas);
175         //! Integer Value Base Parsing Function
176         int parse_integer(xmlpp::Element *node);
177         //! Vector Value Base Parsing Function
178         Vector parse_vector(xmlpp::Element *node);
179         //! Color Value Base Parsing Function
180         Color parse_color(xmlpp::Element *node);
181         //! Angle Value Base Parsing Function
182         Angle parse_angle(xmlpp::Element *node);
183         //! String Value Base Parsing Function
184         String parse_string(xmlpp::Element *node);
185         //! Bool Value Base Parsing Function
186         bool parse_bool(xmlpp::Element *node);
187         //! Segment Value Base Parsing Function
188         Segment parse_segment(xmlpp::Element *node);
189         //! List Value Base Parsing Function
190         ValueBase parse_list(xmlpp::Element *node,Canvas::Handle canvas);
191         //! Gradient Value Base Parsing Function
192         Gradient parse_gradient(xmlpp::Element *node);
193         //! Bline Point Value Base Parsing Function
194         BLinePoint parse_bline_point(xmlpp::Element *node);
195
196         //! Keyframe Parsing Function
197         Keyframe parse_keyframe(xmlpp::Element *node,Canvas::Handle canvas);
198
199         //! ValueNode Animated Parsing Function
200         etl::handle<ValueNode_Animated> parse_animated(xmlpp::Element *node,Canvas::Handle canvas);
201         //! Linkable ValueNode Parsing Function
202         etl::handle<LinkableValueNode> parse_linkable_value_node(xmlpp::Element *node,Canvas::Handle canvas);
203         //! Dynamic List Parsnig Function
204         etl::handle<ValueNode_DynamicList> parse_dynamic_list(xmlpp::Element *node,Canvas::Handle canvas);
205
206         //! Static option for ValueBase parsing fucntion
207         bool parse_static(xmlpp::Element *node);
208
209 }; // END of CanvasParser
210
211 /* === E X T E R N S ======================================================= */
212
213 //!     Loads a canvas from current xmlpp Element
214 /*!     \return The Canvas's handle on success, an empty handle on failure */
215 extern Canvas::Handle open_canvas(xmlpp::Element* node,String &errors,String &warnings);
216 //!     Loads a canvas from \a filename
217 /*!     \return The Canvas's handle on success, an empty handle on failure */
218 extern Canvas::Handle open_canvas(const String &filename,String &errors,String &warnings);
219 //!     Loads a canvas from \a filename and its absolute path
220 /*!     \return The Canvas's handle on success, an empty handle on failure */
221 extern Canvas::Handle open_canvas_as(const String &filename,const String &as,String &errors,String &warnings);
222
223 //! Returns the Open Canvases Map.
224 //! \see open_canvas_map_
225 std::map<synfig::String, etl::loose_handle<Canvas> >& get_open_canvas_map();
226
227 }; // END of namespace synfig
228
229 /* === E N D =============================================================== */
230
231 #endif