]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
Fix language of error insets
[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  *          This file Copyright 2000 Baruch Even
9  * ================================================= */
10
11 #include <config.h> 
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif 
16
17 #include "insetgraphicsParams.h"
18
19 #include "support/translator.h"
20 #include "support/filetools.h"
21 #include "support/lyxlib.h"
22 #include "support/LOstream.h"
23
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 Resize enum and corresponding lyx
33 /// file strings.
34 Translator< InsetGraphicsParams::Resize, string >
35 resizeTranslator(InsetGraphicsParams::DEFAULT_SIZE, "default");
36
37 /// This is the translator between the Origin enum and corresponding lyx
38 /// file strings.
39 Translator< InsetGraphicsParams::Origin, string >
40 originTranslator(InsetGraphicsParams::DEFAULT, "default");
41
42 /// This is the translator between the Display enum and corresponding lyx
43 /// file strings.
44 Translator< InsetGraphicsParams::DisplayType, string >
45 displayTranslator(InsetGraphicsParams::MONOCHROME, "monochrome");
46
47 } // namespace anon
48
49
50 InsetGraphicsParams::InsetGraphicsParams()
51 {
52         init();
53
54         // Set translators
55         if (! translatorsSet) {
56                 translatorsSet = true;
57
58                 // Fill the resize translator
59                 resizeTranslator.addPair(DEFAULT_SIZE, "default");
60                 resizeTranslator.addPair(CM, "cm");
61                 resizeTranslator.addPair(INCH, "inch");
62                 resizeTranslator.addPair(PERCENT_PAGE, "percentOfPage");
63                 resizeTranslator.addPair(PERCENT_COLUMN, "percentOfColumn");
64                 resizeTranslator.addPair(SCALE, "scale");
65
66                 // Fill the origin translator
67                 originTranslator.addPair(DEFAULT, "default");
68                 originTranslator.addPair(LEFTTOP, "leftTop");
69                 originTranslator.addPair(LEFTCENTER, "leftCenter");
70                 originTranslator.addPair(LEFTBASELINE, "leftBaseLine");
71                 originTranslator.addPair(LEFTBOTTOM, "leftBottom");
72                 originTranslator.addPair(CENTERTOP, "centerTop");
73                 originTranslator.addPair(CENTER, "center");
74                 originTranslator.addPair(CENTERBASELINE, "centerBaseLine");
75                 originTranslator.addPair(CENTERBOTTOM, "centerBottom");
76                 originTranslator.addPair(RIGHTTOP, "rightTop");
77                 originTranslator.addPair(RIGHTCENTER, "rightCenter");
78                 originTranslator.addPair(RIGHTBASELINE, "rightBaseLine");
79                 originTranslator.addPair(RIGHTBOTTOM, "rightBottom");
80                 originTranslator.addPair(REFERENCE_POINT, "referencePoint");
81
82                 // Fill the display translator
83                 displayTranslator.addPair(MONOCHROME, "monochrome");
84                 displayTranslator.addPair(GRAYSCALE, "grayscale");
85                 displayTranslator.addPair(COLOR, "color");
86                 displayTranslator.addPair(NONE, "none");
87         }
88
89 }
90
91
92 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
93 {
94         // I decided to skip the initialization since the copy will overwrite
95         // everything anyway.
96         //    init();
97         copy(igp);
98 }
99
100 InsetGraphicsParams &
101 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
102 {
103         // Are we assigning the object into itself?
104         if (this == &params)
105                 return * this;
106
107         copy(params);
108         return *this;
109 }
110
111 void InsetGraphicsParams::init()
112 {
113         subcaptionText = filename = string();
114         display = MONOCHROME;
115         subcaption = false;
116         keepAspectRatio = true;
117         widthResize = DEFAULT_SIZE;
118         widthSize = 0.0;
119         heightResize = DEFAULT_SIZE;
120         heightSize = 0.0;
121         rotateOrigin = DEFAULT;
122         rotateAngle = 0.0;
123
124         testInvariant();
125 }
126
127 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
128 {
129         filename = igp.filename;
130         display = igp.display;
131         subcaption = igp.subcaption;
132         subcaptionText = igp.subcaptionText;
133         keepAspectRatio = igp.keepAspectRatio;
134         widthResize = igp.widthResize;
135         widthSize = igp.widthSize;
136         heightResize = igp.heightResize;
137         heightSize = igp.heightSize;
138         rotateOrigin = igp.rotateOrigin;
139         rotateAngle = igp.rotateAngle;
140
141         testInvariant();
142 }
143
144 void InsetGraphicsParams::testInvariant() const
145 {
146         // Filename might be empty (when the dialog is first created).
147         // Assert(!filename.empty());
148
149         lyx::Assert(display == COLOR ||
150                display == MONOCHROME ||
151                display == GRAYSCALE ||
152                display == NONE
153               );
154
155         lyx::Assert(widthResize == DEFAULT_SIZE ||
156                widthResize == CM ||
157                widthResize == INCH ||
158                widthResize == PERCENT_PAGE ||
159                widthResize == PERCENT_COLUMN ||
160                    widthResize == SCALE
161               );
162
163         lyx::Assert(heightResize == DEFAULT_SIZE ||
164                heightResize == CM ||
165                heightResize == INCH ||
166                heightResize == PERCENT_PAGE ||
167                    heightResize == SCALE
168               );
169
170         // For SCALE these can be negative.
171         //lyx::Assert(widthSize >= 0.0);
172         //lyx::Assert(heightSize >= 0.0);
173
174         // Angle is in degrees and ranges -360 < angle < 360
175         // The reason for this is that in latex there is a meaning for the
176         // different angles and they are not necessarliy interchangeable,
177         // it depends on the rotation origin.
178         lyx::Assert(rotateAngle < 360.0);
179         lyx::Assert(rotateAngle > -360.0);
180
181 }
182
183 bool operator==(InsetGraphicsParams const & left,
184                 InsetGraphicsParams const & right)
185 {
186         if (left.filename == right.filename &&
187                 left.display == right.display &&
188                 left.subcaption == right.subcaption &&
189                 left.subcaptionText == right.subcaptionText &&
190                 left.keepAspectRatio == right.keepAspectRatio &&
191                 left.widthResize == right.widthResize &&
192                 left.widthSize == right.widthSize &&
193                 left.heightResize == right.heightResize &&
194                 left.heightSize == right.heightSize &&
195                 left.rotateOrigin == right.rotateOrigin &&
196                 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001)
197            )
198                 return true;
199
200         return false;
201 }
202
203 bool operator!=(InsetGraphicsParams const & left,
204                 InsetGraphicsParams const & right)
205 {
206         return  !(left == right);
207 }
208
209
210 namespace {
211
212 void writeResize(ostream & os, string const & key,
213                         InsetGraphicsParams::Resize resize, double size)
214 {
215         os << ' ' << key << "Resize ";
216
217         os << resizeTranslator.find(resize);
218         os << ' ' << key << ' ' << size << '\n';
219 }
220
221 void writeOrigin(ostream & os,
222                         InsetGraphicsParams::Origin origin)
223 {
224         os << " rotateOrigin " << originTranslator.find(origin);
225         os << '\n';
226 }
227
228 } // namespace anon
229
230
231 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
232 {
233         // If there is no filename, write nothing for it.
234         if (! filename.empty()) {
235                 os << "filename "
236                 << MakeRelPath(filename, OnlyPath(buf->fileName()))
237                 << '\n';
238         }
239
240         // Save the display type
241         os << " display " << displayTranslator.find(display) << '\n';
242
243         // Save the subcaption status
244         if (subcaption)
245                 os << " subcaption";
246
247         if (! subcaptionText.empty())
248                 os << " subcaptionText \"" << subcaptionText << '\"' << '\n';
249
250         writeResize(os, "width", widthResize, widthSize);
251         writeResize(os, "height", heightResize, heightSize);
252
253         writeOrigin(os, rotateOrigin);
254         if (lyx::float_equal(rotateAngle, 0.0, 0.001))
255                 os << " rotateAngle " << rotateAngle << '\n';
256 }
257
258
259 namespace {
260
261 void readResize(InsetGraphicsParams * igp, bool height,
262                        string const & token)
263 {
264         InsetGraphicsParams::Resize resize = InsetGraphicsParams::DEFAULT_SIZE;
265
266         resize = resizeTranslator.find(token);
267
268         if (height)
269                 igp->heightResize = resize;
270         else
271                 igp->widthResize = resize;
272 }
273
274
275 void readOrigin(InsetGraphicsParams * igp, string const & token)
276 {
277         // TODO: complete this function.
278         igp->rotateOrigin = originTranslator.find(token);
279 }
280
281 } // namespace anon
282
283
284 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
285                                string const& token)
286 {
287         if (token == "filename") {
288                 lex.next();
289                 filename = lex.getString();
290
291                 if (!filename.empty()) {
292                         // Make the filename with absolute directory.
293                         filename = MakeAbsPath(filename, OnlyPath(buf->fileName()));
294                 }
295         } else if (token == "display") {
296                 lex.next();
297                 string const type = lex.getString();
298
299                 display = displayTranslator.find(type);
300         } else if (token == "subcaption") {
301                 subcaption = true;
302         } else if (token == "subcaptionText") {
303                 lex.next();
304                 subcaptionText = lex.getString();
305         } else if (token == "widthResize") {
306                 lex.next();
307                 string const token = lex.getString();
308
309                 readResize(this, false, token);
310         } else if (token == "width") {
311                 lex.next();
312                 widthSize = lex.getFloat();
313         } else if (token == "heightResize") {
314                 lex.next();
315                 string const token = lex.getString();
316
317                 readResize(this, true, token);
318         } else if (token == "height") {
319                 lex.next();
320                 heightSize = lex.getFloat();
321         } else if (token == "rotateOrigin") {
322                 lex.next();
323                 string const token = lex.getString();
324
325                 readOrigin(this, token);
326         } else if (token == "rotateAngle") {
327                 lex.next();
328                 rotateAngle = lex.getFloat();
329         } else {
330                 // If it's none of the above, its not ours.
331                 return false;
332         }
333
334         return true;
335 }