]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
FormExternal patch from Rob,
[lyx.git] / src / insets / insetgraphicsParams.C
1 /* This file is part of
2  * =================================================
3  * 
4  *          LyX, The Document Processor
5  *          Copyright 1995 Matthias Ettrich.
6  *          Copyright 1995-2001 The LyX Team.
7  *
8  * \author Baruch Even
9  * \author Herbert Voss <voss@lyx.org>
10  *
11  * ================================================= */
12
13 #include <config.h> 
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif 
18
19 #include "insetgraphicsParams.h"
20 #include "support/translator.h"
21 #include "support/filetools.h"
22 #include "support/lyxlib.h"
23 #include "support/LOstream.h"
24 #include "support/LAssert.h"
25
26 namespace {
27
28 /// This variable keeps a tab on whether the translator was set with the
29 /// translations.
30 bool translatorsSet = false;
31
32 /// This is the translator between the Display enum and corresponding lyx
33 /// file strings.
34 Translator< InsetGraphicsParams::DisplayType, string >
35 displayTranslator(InsetGraphicsParams::DEFAULT, "default");
36
37 // this is only compatibility stuff for the first 1.2 version
38 // it is obselete until 1.3
39 LyXLength convertResizeValue(string const token, LyXLex & lex) {
40     lex.next();
41     string value = lex.getString();     // "width" or "height"  
42     lex.next();                         // anyway not interesting
43     value = lex.getString();
44     if (token == "default")
45         return (LyXLength(value+"pt"));
46     else if (token == "cm")
47         return (LyXLength(value+"cm"));
48     else if (token == "inch")
49         return (LyXLength(value+"in"));
50     else if (token == "percentOfColumn")
51         return (LyXLength(value+"c%"));
52     else if (token == "percentOfPage")
53         return (LyXLength(value+"p%"));
54     else return LyXLength("0pt");       // nothing with figinset
55 }
56
57 } // namespace anon
58
59
60 InsetGraphicsParams::InsetGraphicsParams()
61 {
62         init();
63         // Set translators
64         if (! translatorsSet) {
65                 translatorsSet = true;
66                 // Fill the display translator
67                 displayTranslator.addPair(DEFAULT, "default");
68                 displayTranslator.addPair(MONOCHROME, "monochrome");
69                 displayTranslator.addPair(GRAYSCALE, "grayscale");
70                 displayTranslator.addPair(COLOR, "color");
71                 displayTranslator.addPair(NONE, "none");
72         }
73 }
74
75
76 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
77 {
78         // I decided to skip the initialization since the copy will overwrite
79         // everything anyway.
80         //    init();
81         copy(igp);
82 }
83
84 InsetGraphicsParams &
85 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
86 {
87         // Are we assigning the object into itself?
88         if (this == &params)
89                 return * this;
90         copy(params);
91         return *this;
92 }
93
94 void InsetGraphicsParams::init()
95 {
96         subcaptionText = filename = string();
97         bb = string();                  // bounding box
98         draft = false;                  // draft mode
99         clip = false;                   // clip image
100         display = DEFAULT;              // see pref
101         subcaption = false;             // subfigure
102         noUnzip = false;                // unzip files
103         width = LyXLength();            // set to 0pt
104         height = LyXLength();
105         lyxwidth = LyXLength();         // for the view in lyx
106         lyxheight = LyXLength();        // also set to 0pt
107         scale = 0;                      // unit is %
108         lyxscale = 0;                   // same for lyxview
109         size_type = DEFAULT_SIZE;       // do nothing
110         lyxsize_type = DEFAULT_SIZE;    // do nothing
111         keepAspectRatio = false;        // only for latex
112         rotate = false;                 // Rotating 
113         rotateOrigin = "center";        // Origin
114         rotateAngle = 0.0;              // in degrees
115         special = string();             // userdefined stuff
116 }
117
118 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
119 {
120         filename = igp.filename;
121         bb = igp.bb;
122         draft = igp.draft;
123         clip = igp.clip;
124         display = igp.display;
125         subcaption = igp.subcaption;
126         subcaptionText = igp.subcaptionText;
127         noUnzip = igp.noUnzip;
128         keepAspectRatio = igp.keepAspectRatio;
129         width = igp.width;
130         height = igp.height;
131         scale = igp.scale;
132         size_type = igp.size_type;
133         lyxsize_type = igp.lyxsize_type;
134         lyxwidth = igp.lyxwidth;
135         lyxheight = igp.lyxheight;
136         lyxscale = igp.lyxscale;
137         rotate = igp.rotate;
138         rotateOrigin = igp.rotateOrigin;
139         rotateAngle = igp.rotateAngle;
140         special = igp.special;
141 }
142
143 bool operator==(InsetGraphicsParams const & left,
144                 InsetGraphicsParams const & right)
145 {
146         if (left.filename == right.filename &&
147                 left.bb == right.bb &&
148                 left.draft == right.draft &&
149                 left.clip == right.clip &&
150                 left.display == right.display &&
151                 left.subcaption == right.subcaption &&
152                 left.noUnzip == right.noUnzip &&
153                 left.subcaptionText == right.subcaptionText &&
154                 left.keepAspectRatio == right.keepAspectRatio &&
155                 left.width == right.width &&
156                 left.height == right.height &&
157                 left.scale == right.scale &&
158                 left.size_type == right.size_type &&
159                 left.lyxsize_type == right.lyxsize_type &&
160                 left.lyxwidth == right.lyxwidth &&
161                 left.lyxheight == right.lyxheight &&
162                 left.lyxscale == right.lyxscale &&
163                 left.rotate == right.rotate &&
164                 left.rotateOrigin == right.rotateOrigin &&
165                 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
166                 left.special == right.special) 
167            )
168                 return true;
169
170         return false;
171 }
172
173 bool operator!=(InsetGraphicsParams const & left,
174                 InsetGraphicsParams const & right)
175 {
176         return  !(left == right);
177 }
178
179
180 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
181 {
182         // If there is no filename, write nothing for it.
183         if (! filename.empty()) {
184                 os << "\tfilename "
185                 << MakeRelPath(filename, buf->filePath())
186                 << '\n';
187         }
188         if (!bb.empty())                // bounding box
189                 os << "\tBoundingBox " << bb << '\n';
190         if (clip)                       // clip image
191                 os << "\tclip\n";
192         if (draft)                      // draft mode
193                 os << "\tdraft\n";
194         // Save the display type for the view inside lyx
195         os << "\tdisplay " << displayTranslator.find(display) << '\n';
196         // Save the subcaption status
197         if (subcaption)
198             os << "\tsubcaption\n";
199         if (!subcaptionText.empty())
200             os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
201         if (noUnzip)
202             os << "\tnoUnzip\n";
203     // we always need the size type
204     // 0: no special
205     // 1: width/height combination
206     // 2: scale
207         os << "\tsize_type " <<  size_type << '\n';
208         if (!width.zero())
209             os << "\twidth " << width.asString() << '\n';
210         if (!height.zero())
211             os << "\theight " << height.asString() << '\n';
212         if (scale != 0)
213             os << "\tscale " << scale << '\n';
214         if (keepAspectRatio)
215                 os << "\tkeepAspectRatio\n";
216         if (rotate)
217                 os << "\trotate\n";
218         if (!lyx::float_equal(rotateAngle, 0.0, 0.001))
219                 os << "\trotateAngle " << rotateAngle << '\n';
220         if (!rotateOrigin.empty())
221                 os << "\trotateOrigin " << rotateOrigin << '\n';
222         if (!special.empty())
223                 os << "\tspecial " << special << '\n';
224         // the values for the view in lyx
225         os << "\tlyxsize_type " <<  lyxsize_type << '\n';
226         if (!lyxwidth.zero())           // the lyx-viewsize
227             os << "\tlyxwidth " << lyxwidth.asString() << '\n';
228         if (!lyxheight.zero())
229             os << "\tlyxheight " << lyxheight.asString();
230         if (lyxscale != 0)
231             os << "\tlyxscale " << lyxscale << '\n';
232 }
233
234
235 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
236                                string const& token)
237 {
238         if (token == "filename") {
239                 lex.next();
240                 filename = lex.getString();
241                 if (!filename.empty()) {
242                         // Make the filename with absolute directory.
243                         filename = MakeAbsPath(filename, buf->filePath());
244                 }
245         } else if (token == "BoundingBox") {
246                 for (int i=0; i<4 ;i++) {
247                     lex.next();
248                     bb += (lex.getString()+" ");
249                 }
250         } else if (token == "clip") {
251                 clip = true;
252         } else if (token == "draft") {
253                 draft = true;
254         } else if (token == "display") {
255                 lex.next();
256                 string const type = lex.getString();
257                 display = displayTranslator.find(type);
258         } else if (token == "subcaption") {
259                 subcaption = true;
260         } else if (token == "subcaptionText") {
261                 lex.next();
262                 subcaptionText = lex.getString();
263         } else if (token == "noUnzip") {
264                 noUnzip = true;
265         } else if (token == "size_type") {
266                 lex.next();
267                 switch (lex.getInteger()) {
268                     case 0 : size_type = DEFAULT_SIZE;
269                         break;
270                     case 1 : size_type = WH;
271                         break;
272                     case 2 : size_type = SCALE;
273                 }
274         } else if (token == "width") {
275                 lex.next();
276                 width = LyXLength(lex.getString());
277                 size_type = WH;
278         } else if (token == "height") {
279                 lex.next();
280                 height = LyXLength(lex.getString());
281                 size_type = WH;
282         } else if (token == "keepAspectRatio") {
283                 keepAspectRatio = true;
284         } else if (token == "scale") {
285                 lex.next();
286                 scale = lex.getInteger();
287         } else if (token == "rotate") {
288                 rotate = true;
289         } else if (token == "rotateAngle") {
290                 lex.next();
291                 rotateAngle = lex.getFloat();
292         } else if (token == "rotateOrigin") {
293                 lex.next();
294                 rotateOrigin=lex.getString();
295         } else if (token == "lyxsize_type") {
296                 lex.next();
297                 switch (lex.getInteger()) {
298                     case 0 : lyxsize_type = DEFAULT_SIZE;
299                         break;
300                     case 1 : lyxsize_type = WH;
301                         break;
302                     case 2 : lyxsize_type = SCALE;
303                 }
304         } else if (token == "lyxwidth") {
305                 lex.next();
306                 lyxwidth = LyXLength(lex.getString());
307         } else if (token == "lyxheight") {
308                 lex.next();
309                 lyxheight = LyXLength(lex.getString());
310         } else if (token == "lyxscale") {
311                 lex.next();
312                 lyxscale = lex.getInteger();
313         // now the compytibility stuff for "old" 1.2.0 files which uses
314         // the first try of the new graphic inset. Can be deleted, when
315         // 1.3 comes out
316         } else if (token == "widthResize") {
317                 if (lex.next()) {
318                     string const token = lex.getString();
319                     if (token == "scale") {
320                         lex.next();
321                         scale = lex.getInteger();
322                         size_type = SCALE;
323                     }
324                     else {
325                         width = convertResizeValue(token, lex);
326                         size_type = WH;
327                     }
328                 }
329         } else if (token == "heightResize") {
330                 if (lex.next())
331                         height = convertResizeValue(lex.getString(), lex);
332         // end compytibility stuff
333         } else {        // If it's none of the above, its not ours.
334                 return false;
335         }
336         return true;
337 }