]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
update copyright year
[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-2001 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         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               );
161
162         lyx::Assert(heightResize == DEFAULT_SIZE ||
163                heightResize == CM ||
164                heightResize == INCH ||
165                heightResize == PERCENT_PAGE
166               );
167
168         lyx::Assert(widthSize >= 0.0);
169         lyx::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         lyx::Assert(rotateAngle < 360);
176         lyx::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 bool operator!=(InsetGraphicsParams const & left,
202                 InsetGraphicsParams const & right)
203 {
204         return  !(left == right);
205 }
206
207
208 namespace {
209
210 void writeResize(ostream & os, string const & key,
211                         InsetGraphicsParams::Resize resize, double size)
212 {
213         os << ' ' << key << "Resize ";
214
215         os << resizeTranslator.find(resize);
216         os << ' ' << key << ' ' << size << '\n';
217 }
218
219 void writeOrigin(ostream & os,
220                         InsetGraphicsParams::Origin origin)
221 {
222         os << " rotateOrigin " << originTranslator.find(origin);
223         os << '\n';
224 }
225
226 } // namespace anon
227
228
229 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
230 {
231         // If there is no filename, write nothing for it.
232         if (! filename.empty()) {
233                 os << "filename "
234                 << MakeRelPath(filename, OnlyPath(buf->fileName()))
235                 << '\n';
236         }
237
238         // Save the display type
239         os << " display " << displayTranslator.find(display) << '\n';
240
241         // Save the inline status
242         if (inlineFigure)
243                 os << " inline";
244
245         // Save the subcaption status
246         if (subcaption)
247                 os << " subcaption";
248
249         if (! subcaptionText.empty())
250                 os << " subcaptionText \"" << subcaptionText << '\"' << '\n';
251
252         writeResize(os, "width", widthResize, widthSize);
253         writeResize(os, "height", heightResize, heightSize);
254
255         writeOrigin(os, rotateOrigin);
256         if (rotateAngle != 0)
257                 os << " rotateAngle " << rotateAngle << '\n';
258 }
259
260
261 namespace {
262
263 void readResize(InsetGraphicsParams * igp, bool height,
264                        string const & token)
265 {
266         InsetGraphicsParams::Resize resize = InsetGraphicsParams::DEFAULT_SIZE;
267
268         resize = resizeTranslator.find(token);
269
270         if (height)
271                 igp->heightResize = resize;
272         else
273                 igp->widthResize = resize;
274 }
275
276
277 void readOrigin(InsetGraphicsParams * igp, string const & token)
278 {
279         // TODO: complete this function.
280         igp->rotateOrigin = originTranslator.find(token);
281 }
282
283 } // namespace anon
284
285
286 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
287                                string const& token)
288 {
289         if (token == "filename") {
290                 lex.next();
291                 filename = lex.GetString();
292
293                 if (!filename.empty()) {
294                         // Make the filename with absolute directory.
295                         filename = MakeAbsPath(filename, OnlyPath(buf->fileName()));
296                 }
297         } else if (token == "display") {
298                 lex.next();
299                 string const type = lex.GetString();
300
301                 display = displayTranslator.find(type);
302         } else if (token == "inline") {
303                 inlineFigure = true;
304         } else if (token == "subcaption") {
305                 subcaption = true;
306         } else if (token == "subcaptionText") {
307                 lex.next();
308                 subcaptionText = lex.GetString();
309         } else if (token == "widthResize") {
310                 lex.next();
311                 string const token = lex.GetString();
312
313                 readResize(this, false, token);
314         } else if (token == "width") {
315                 lex.next();
316                 widthSize = lex.GetFloat();
317         } else if (token == "heightResize") {
318                 lex.next();
319                 string const token = lex.GetString();
320
321                 readResize(this, true, token);
322         } else if (token == "height") {
323                 lex.next();
324                 heightSize = lex.GetFloat();
325         } else if (token == "rotateOrigin") {
326                 lex.next();
327                 string const token = lex.GetString();
328
329                 readOrigin(this, token);
330         } else if (token == "rotateAngle") {
331                 lex.next();
332                 rotateAngle = lex.GetInteger();
333         } else {
334                 // If it's none of the above, its not ours.
335                 return false;
336         }
337
338         return true;
339 }