Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_03 / synfig-core / src / modules / lyr_std / import.cpp
1 /*! ========================================================================
2 ** Synfig
3 ** Image Import Layer Implementation
4 ** $Id: import.cpp,v 1.2 2005/03/19 04:26:42 darco Exp $
5 **
6 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
7 **
8 **      This package is free software; you can redistribute it and/or
9 **      modify it under the terms of the GNU General Public License as
10 **      published by the Free Software Foundation; either version 2 of
11 **      the License, or (at your option) any later version.
12 **
13 **      This package is distributed in the hope that it will be useful,
14 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
15 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 **      General Public License for more details.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "import.h"
32 #include <synfig/string.h>
33 #include <synfig/time.h>
34 #include <synfig/context.h>
35 #include <synfig/paramdesc.h>
36 #include <synfig/renddesc.h>
37 #include <synfig/surface.h>
38 #include <synfig/value.h>
39 #include <synfig/valuenode.h>
40 #include <synfig/canvas.h>
41
42 #endif
43
44 using namespace synfig;
45 using namespace std;
46 using namespace etl;
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 SYNFIG_LAYER_INIT(Import);
53 SYNFIG_LAYER_SET_NAME(Import,"import");
54 SYNFIG_LAYER_SET_LOCAL_NAME(Import,_("Import"));
55 SYNFIG_LAYER_SET_CATEGORY(Import,_("Other"));
56 SYNFIG_LAYER_SET_VERSION(Import,"0.1");
57 SYNFIG_LAYER_SET_CVS_ID(Import,"$Id: import.cpp,v 1.2 2005/03/19 04:26:42 darco Exp $");
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62
63 Import::Import()
64 {
65         time_offset=0;
66 }
67
68 Import::~Import()
69 {
70 }
71
72 void
73 Import::on_canvas_set()
74 {
75         if(get_canvas())set_param("filename",filename);
76 }
77
78 bool
79 Import::set_param(const String & param, const ValueBase &value)
80 {
81         try{
82         IMPORT(time_offset);
83         if(param=="filename" && value.same_as(filename))
84         {
85                 if(!get_canvas())
86                 {
87                         filename=value.get(filename);
88                         importer=0;
89                         surface.clear();
90                         return true;
91                 }
92                 
93                 String newfilename=value.get(string());
94                 String filename_with_path;
95                 
96                 // Get rid of any %20 crap
97                 {
98                         unsigned int n;
99                         while((n=newfilename.find("%20"))!=String::npos)
100                                 newfilename.replace(n,3," ");
101                 }
102                 
103                 //if(get_canvas()->get_file_path()==dirname(newfilename))
104                 //{
105                 //      synfig::info("Image seems to be in local directory. Adjusting path...");
106                 //      newfilename=basename(newfilename);
107                 //}
108                 
109 #ifndef WIN32
110                 if(is_absolute_path(newfilename))
111                 {
112                         string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
113                         while(basename(curpath)==".")curpath=dirname(curpath);
114                                 
115                         newfilename=relative_path(curpath,newfilename);
116                         synfig::info("basename(curpath)=%s, Path adjusted to %s",basename(curpath).c_str(),newfilename.c_str());
117                 }
118 #endif
119
120                 if(filename.empty())
121                         filename=newfilename;
122
123                 if(newfilename.empty())
124                 {
125                         filename=newfilename;
126                         importer=0;
127                         surface.clear();
128                         return true;
129                 }
130                 
131                 // If we are already loaded, don't reload
132                 if(filename==newfilename && importer)
133                 {
134                         synfig::warning(strprintf(_("Filename seems to already be set to \"%s\" (%s)"),filename.c_str(),newfilename.c_str()));
135                         return true;
136                 }
137                 
138                 assert(get_canvas());
139                 
140                 if(is_absolute_path(newfilename))
141                         filename_with_path=newfilename;
142                 else
143                         filename_with_path=get_canvas()->get_file_path()+ETL_DIRECTORY_SEPERATOR+newfilename;
144                         
145                 handle<Importer> newimporter;
146                 
147                 newimporter=Importer::open(absolute_path(filename_with_path));
148
149                 if(!newimporter)
150                 {
151                         newimporter=Importer::open(get_canvas()->get_file_path()+ETL_DIRECTORY_SEPERATOR+basename(newfilename));
152                         if(!newimporter)
153                         {
154                                 synfig::error(strprintf("Unable to create an importer object with file \"%s\"",filename_with_path.c_str()));
155                                 surface.clear();
156                                 return false;
157                         }
158                 }
159
160                 surface.clear();
161                 if(!newimporter->get_frame(surface,Time(0)))
162                 {
163                         synfig::warning(strprintf("Unable to get frame from \"%s\"",filename_with_path.c_str()));
164                 }
165
166                 importer=newimporter;
167                 filename=newfilename;
168                 abs_filename=absolute_path(filename_with_path);
169                 
170                 return true;
171         }
172         } catch(...) { set_amount(0); return false; }
173         
174         return Layer_Bitmap::set_param(param,value);
175 }
176
177 ValueBase
178 Import::get_param(const String & param)const
179 {
180         EXPORT(time_offset);
181
182         if(get_canvas())
183         {
184                 if(param=="filename")
185                 {
186                         string curpath(cleanup_path(absolute_path(get_canvas()->get_file_path())));
187                         return relative_path(curpath,abs_filename);
188                 }
189         }
190         else
191                 EXPORT(filename);
192
193         EXPORT_NAME();
194         EXPORT_VERSION();
195                 
196         return Layer_Bitmap::get_param(param);  
197 }
198
199 Layer::Vocab
200 Import::get_param_vocab()const
201 {
202         Layer::Vocab ret(Layer_Bitmap::get_param_vocab());
203         
204         ret.push_back(ParamDesc("filename")
205                 .set_local_name(_("Filename"))
206                 .set_description(_("File to import"))
207                 .set_hint("filename")
208         );
209         ret.push_back(ParamDesc("time_offset")
210                 .set_local_name(_("Time Offset"))
211         );
212         
213         return ret;
214 }
215
216 void
217 Import::set_time(Context context, Time time)const
218 {
219         if(get_amount() && importer && importer->is_animated())importer->get_frame(surface,time+time_offset);
220         //else surface.clear();
221         context.set_time(time);
222 }
223
224 void
225 Import::set_time(Context context, Time time, const Point &pos)const
226 {
227         if(get_amount() && importer && importer->is_animated())importer->get_frame(surface,time+time_offset);
228         //else surface.clear();
229         context.set_time(time,pos);
230 }