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