]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
Enable the graphics inset to work correctly with relative file names.
[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
21 #include "support/translator.h"
22 #include "support/filetools.h"
23 #include "support/lyxlib.h"
24 #include "support/LOstream.h"
25 #include "support/LAssert.h"
26
27
28 using std::ostream;
29
30
31 namespace {
32
33 /// This variable keeps a tab on whether the translator was set with the
34 /// translations.
35 bool translatorsSet = false;
36
37 /// This is the translator between the Display enum and corresponding lyx
38 /// file strings.
39 Translator< InsetGraphicsParams::DisplayType, string >
40 displayTranslator(InsetGraphicsParams::DEFAULT, "default");
41
42 // this is only compatibility stuff for the first 1.2 version
43 // it is obselete until 1.3
44 LyXLength convertResizeValue(string const token, LyXLex & lex) {
45     lex.next();
46     string value = lex.getString();     // "width" or "height"
47     lex.next();                         // anyway not interesting
48     value = lex.getString();
49     if (token == "default")
50         return (LyXLength(value+"pt"));
51     else if (token == "cm")
52         return (LyXLength(value+"cm"));
53     else if (token == "inch")
54         return (LyXLength(value+"in"));
55     else if (token == "percentOfColumn")
56         return (LyXLength(value+"c%"));
57     else if (token == "percentOfPage")
58         return (LyXLength(value+"p%"));
59     else return LyXLength("0pt");       // nothing with figinset
60 }
61
62 } // namespace anon
63
64
65 InsetGraphicsParams::InsetGraphicsParams()
66 {
67         init();
68         // Set translators
69         if (! translatorsSet) {
70                 translatorsSet = true;
71                 // Fill the display translator
72                 displayTranslator.addPair(DEFAULT, "default");
73                 displayTranslator.addPair(MONOCHROME, "monochrome");
74                 displayTranslator.addPair(GRAYSCALE, "grayscale");
75                 displayTranslator.addPair(COLOR, "color");
76                 displayTranslator.addPair(NONE, "none");
77         }
78 }
79
80
81 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
82 {
83         // I decided to skip the initialization since the copy will overwrite
84         // everything anyway.
85         //    init();
86         copy(igp);
87 }
88
89 InsetGraphicsParams &
90 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
91 {
92         // Are we assigning the object into itself?
93         if (this == &params)
94                 return * this;
95         copy(params);
96         return *this;
97 }
98
99 void InsetGraphicsParams::init()
100 {
101         subcaptionText = filename = string();
102         bb = string();                  // bounding box
103         draft = false;                  // draft mode
104         clip = false;                   // clip image
105         display = DEFAULT;              // see pref
106         subcaption = false;             // subfigure
107         noUnzip = false;                // unzip files
108         width = LyXLength();            // set to 0pt
109         height = LyXLength();
110         lyxwidth = LyXLength();         // for the view in lyx
111         lyxheight = LyXLength();        // also set to 0pt
112         scale = 0;                      // unit is %
113         lyxscale = 0;                   // same for lyxview
114         size_type = DEFAULT_SIZE;       // do nothing
115         lyxsize_type = DEFAULT_SIZE;    // do nothing
116         keepAspectRatio = false;        // only for latex
117         rotate = false;                 // Rotating
118         rotateOrigin = "center";        // Origin
119         rotateAngle = 0.0;              // in degrees
120         special = string();             // userdefined stuff
121 }
122
123 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
124 {
125         filename = igp.filename;
126         bb = igp.bb;
127         draft = igp.draft;
128         clip = igp.clip;
129         display = igp.display;
130         subcaption = igp.subcaption;
131         subcaptionText = igp.subcaptionText;
132         noUnzip = igp.noUnzip;
133         keepAspectRatio = igp.keepAspectRatio;
134         width = igp.width;
135         height = igp.height;
136         scale = igp.scale;
137         size_type = igp.size_type;
138         lyxsize_type = igp.lyxsize_type;
139         lyxwidth = igp.lyxwidth;
140         lyxheight = igp.lyxheight;
141         lyxscale = igp.lyxscale;
142         rotate = igp.rotate;
143         rotateOrigin = igp.rotateOrigin;
144         rotateAngle = igp.rotateAngle;
145         special = igp.special;
146 }
147
148 bool operator==(InsetGraphicsParams const & left,
149                 InsetGraphicsParams const & right)
150 {
151         if (left.filename == right.filename &&
152                 left.bb == right.bb &&
153                 left.draft == right.draft &&
154                 left.clip == right.clip &&
155                 left.display == right.display &&
156                 left.subcaption == right.subcaption &&
157                 left.noUnzip == right.noUnzip &&
158                 left.subcaptionText == right.subcaptionText &&
159                 left.keepAspectRatio == right.keepAspectRatio &&
160                 left.width == right.width &&
161                 left.height == right.height &&
162                 left.scale == right.scale &&
163                 left.size_type == right.size_type &&
164                 left.lyxsize_type == right.lyxsize_type &&
165                 left.lyxwidth == right.lyxwidth &&
166                 left.lyxheight == right.lyxheight &&
167                 left.lyxscale == right.lyxscale &&
168                 left.rotate == right.rotate &&
169                 left.rotateOrigin == right.rotateOrigin &&
170                 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
171                 left.special == right.special)
172           )
173                 return true;
174
175         return false;
176 }
177
178 bool operator!=(InsetGraphicsParams const & left,
179                 InsetGraphicsParams const & right)
180 {
181         return  !(left == right);
182 }
183
184
185 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
186 {
187         // If there is no filename, write nothing for it.
188         if (! filename.empty()) {
189                 os << "\tfilename "
190                 << MakeRelPath(filename, buf->filePath())
191                 << '\n';
192         }
193         if (!bb.empty())                // bounding box
194                 os << "\tBoundingBox " << bb << '\n';
195         if (clip)                       // clip image
196                 os << "\tclip\n";
197         if (draft)                      // draft mode
198                 os << "\tdraft\n";
199         // Save the display type for the view inside lyx
200         os << "\tdisplay " << displayTranslator.find(display) << '\n';
201         // Save the subcaption status
202         if (subcaption)
203             os << "\tsubcaption\n";
204         if (!subcaptionText.empty())
205             os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
206         if (noUnzip)
207             os << "\tnoUnzip\n";
208     // we always need the size type
209     // 0: no special
210     // 1: width/height combination
211     // 2: scale
212         os << "\tsize_type " <<  size_type << '\n';
213         if (!width.zero())
214             os << "\twidth " << width.asString() << '\n';
215         if (!height.zero())
216             os << "\theight " << height.asString() << '\n';
217         if (scale != 0)
218             os << "\tscale " << scale << '\n';
219         if (keepAspectRatio)
220                 os << "\tkeepAspectRatio\n";
221         if (rotate)
222                 os << "\trotate\n";
223         if (!lyx::float_equal(rotateAngle, 0.0, 0.001))
224                 os << "\trotateAngle " << rotateAngle << '\n';
225         if (!rotateOrigin.empty())
226                 os << "\trotateOrigin " << rotateOrigin << '\n';
227         if (!special.empty())
228                 os << "\tspecial " << special << '\n';
229         // the values for the view in lyx
230         os << "\tlyxsize_type " <<  lyxsize_type << '\n';
231         if (!lyxwidth.zero())           // the lyx-viewsize
232             os << "\tlyxwidth " << lyxwidth.asString() << '\n';
233         if (!lyxheight.zero())
234             os << "\tlyxheight " << lyxheight.asString();
235         if (lyxscale != 0)
236             os << "\tlyxscale " << lyxscale << '\n';
237 }
238
239
240 bool InsetGraphicsParams::Read(LyXLex & lex, string const& token)
241 {
242         if (token == "filename") {
243                 lex.next();
244                 filename = lex.getString();
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 }