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