]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
quick fix for commented out figures
[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+"col%"));
57     else if (token == "percentOfPage")
58         return (LyXLength(value+"page%"));
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(ostream & os) const
186 {
187         // If there is no filename, write nothing for it.
188         if (!filename.empty()) {
189                 os << "\tfilename " << filename << '\n';
190         }
191         if (!bb.empty())                // bounding box
192                 os << "\tBoundingBox " << bb << '\n';
193         if (clip)                       // clip image
194                 os << "\tclip\n";
195         if (draft)                      // draft mode
196                 os << "\tdraft\n";
197         // Save the display type for the view inside lyx
198         os << "\tdisplay " << displayTranslator.find(display) << '\n';
199         // Save the subcaption status
200         if (subcaption)
201             os << "\tsubcaption\n";
202         if (!subcaptionText.empty())
203             os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
204         if (noUnzip)
205             os << "\tnoUnzip\n";
206     // we always need the size type
207     // 0: no special
208     // 1: width/height combination
209     // 2: scale
210         os << "\tsize_type " <<  size_type << '\n';
211         if (!width.zero())
212             os << "\twidth " << width.asString() << '\n';
213         if (!height.zero())
214             os << "\theight " << height.asString() << '\n';
215         if (scale != 0)
216             os << "\tscale " << scale << '\n';
217         if (keepAspectRatio)
218                 os << "\tkeepAspectRatio\n";
219         if (rotate)
220                 os << "\trotate\n";
221         if (!lyx::float_equal(rotateAngle, 0.0, 0.001))
222                 os << "\trotateAngle " << rotateAngle << '\n';
223         if (!rotateOrigin.empty())
224                 os << "\trotateOrigin " << rotateOrigin << '\n';
225         if (!special.empty())
226                 os << "\tspecial " << special << '\n';
227         // the values for the view in lyx
228         os << "\tlyxsize_type " <<  lyxsize_type << '\n';
229         if (!lyxwidth.zero())           // the lyx-viewsize
230             os << "\tlyxwidth " << lyxwidth.asString() << '\n';
231         if (!lyxheight.zero())
232             os << "\tlyxheight " << lyxheight.asString();
233         if (lyxscale != 0)
234             os << "\tlyxscale " << lyxscale << '\n';
235 }
236
237
238 bool InsetGraphicsParams::Read(LyXLex & lex, string const& token)
239 {
240         if (token == "filename") {
241                 lex.next();
242                 filename = lex.getString();
243         } else if (token == "BoundingBox") {
244                 for (int i=0; i<4 ;i++) {
245                     lex.next();
246                     bb += (lex.getString()+" ");
247                 }
248         } else if (token == "clip") {
249                 clip = true;
250         } else if (token == "draft") {
251                 draft = true;
252         } else if (token == "display") {
253                 lex.next();
254                 string const type = lex.getString();
255                 display = displayTranslator.find(type);
256         } else if (token == "subcaption") {
257                 subcaption = true;
258         } else if (token == "subcaptionText") {
259                 lex.next();
260                 subcaptionText = lex.getString();
261         } else if (token == "noUnzip") {
262                 noUnzip = true;
263         } else if (token == "size_type") {
264                 lex.next();
265                 switch (lex.getInteger()) {
266                     case 0 : size_type = DEFAULT_SIZE;
267                         break;
268                     case 1 : size_type = WH;
269                         break;
270                     case 2 : size_type = SCALE;
271                 }
272         } else if (token == "width") {
273                 lex.next();
274                 width = LyXLength(lex.getString());
275                 size_type = WH;
276         } else if (token == "height") {
277                 lex.next();
278                 height = LyXLength(lex.getString());
279                 size_type = WH;
280         } else if (token == "keepAspectRatio") {
281                 keepAspectRatio = true;
282         } else if (token == "scale") {
283                 lex.next();
284                 scale = lex.getInteger();
285         } else if (token == "rotate") {
286                 rotate = true;
287         } else if (token == "rotateAngle") {
288                 lex.next();
289                 rotateAngle = lex.getFloat();
290         } else if (token == "rotateOrigin") {
291                 lex.next();
292                 rotateOrigin=lex.getString();
293         } else if (token == "lyxsize_type") {
294                 lex.next();
295                 switch (lex.getInteger()) {
296                     case 0 : lyxsize_type = DEFAULT_SIZE;
297                         break;
298                     case 1 : lyxsize_type = WH;
299                         break;
300                     case 2 : lyxsize_type = SCALE;
301                 }
302         } else if (token == "lyxwidth") {
303                 lex.next();
304                 lyxwidth = LyXLength(lex.getString());
305         } else if (token == "lyxheight") {
306                 lex.next();
307                 lyxheight = LyXLength(lex.getString());
308         } else if (token == "lyxscale") {
309                 lex.next();
310                 lyxscale = lex.getInteger();
311         // now the compytibility stuff for "old" 1.2.0 files which uses
312         // the first try of the new graphic inset. Can be deleted, when
313         // 1.3 comes out
314         } else if (token == "widthResize") {
315                 if (lex.next()) {
316                     string const token = lex.getString();
317                     if (token == "scale") {
318                         lex.next();
319                         scale = lex.getInteger();
320                         size_type = SCALE;
321                     }
322                     else {
323                         width = convertResizeValue(token, lex);
324                         size_type = WH;
325                     }
326                 }
327         } else if (token == "heightResize") {
328                 if (lex.next())
329                         height = convertResizeValue(lex.getString(), lex);
330         // end compytibility stuff
331         } else {        // If it's none of the above, its not ours.
332                 return false;
333         }
334         return true;
335 }