1 /* This file is part of
2 * =================================================
4 * LyX, The Document Processor
5 * Copyright 1995 Matthias Ettrich.
6 * Copyright 1995-2001 The LyX Team.
8 * This file Copyright 2000 Baruch Even
9 * ================================================= */
14 #pragma implementation
17 #include "insetgraphicsParams.h"
19 #include "support/translator.h"
20 #include "support/filetools.h"
21 #include "support/lyxlib.h"
23 #include "support/LAssert.h"
27 /// This variable keeps a tab on whether the translator was set with the
29 bool translatorsSet = false;
31 /// This is the translator between the Resize enum and corresponding lyx
33 Translator< InsetGraphicsParams::Resize, string >
34 resizeTranslator(InsetGraphicsParams::DEFAULT_SIZE, "default");
36 /// This is the translator between the Origin enum and corresponding lyx
38 Translator< InsetGraphicsParams::Origin, string >
39 originTranslator(InsetGraphicsParams::DEFAULT, "default");
41 /// This is the translator between the Display enum and corresponding lyx
43 Translator< InsetGraphicsParams::DisplayType, string >
44 displayTranslator(InsetGraphicsParams::MONOCHROME, "monochrome");
49 InsetGraphicsParams::InsetGraphicsParams()
54 if (! translatorsSet) {
55 translatorsSet = true;
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");
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");
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");
90 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
92 // I decided to skip the initialization since the copy will overwrite
99 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
101 // Are we assigning the object into itself?
109 void InsetGraphicsParams::init()
111 subcaptionText = filename = string();
112 display = MONOCHROME;
114 keepAspectRatio = true;
115 widthResize = DEFAULT_SIZE;
117 heightResize = DEFAULT_SIZE;
119 rotateOrigin = DEFAULT;
125 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
127 filename = igp.filename;
128 display = igp.display;
129 subcaption = igp.subcaption;
130 subcaptionText = igp.subcaptionText;
131 keepAspectRatio = igp.keepAspectRatio;
132 widthResize = igp.widthResize;
133 widthSize = igp.widthSize;
134 heightResize = igp.heightResize;
135 heightSize = igp.heightSize;
136 rotateOrigin = igp.rotateOrigin;
137 rotateAngle = igp.rotateAngle;
142 void InsetGraphicsParams::testInvariant() const
144 // Filename might be empty (when the dialog is first created).
145 // Assert(!filename.empty());
147 lyx::Assert(display == COLOR ||
148 display == MONOCHROME ||
149 display == GRAYSCALE ||
153 lyx::Assert(widthResize == DEFAULT_SIZE ||
155 widthResize == INCH ||
156 widthResize == PERCENT_PAGE ||
157 widthResize == PERCENT_COLUMN
160 lyx::Assert(heightResize == DEFAULT_SIZE ||
161 heightResize == CM ||
162 heightResize == INCH ||
163 heightResize == PERCENT_PAGE
166 lyx::Assert(widthSize >= 0.0);
167 lyx::Assert(heightSize >= 0.0);
169 // Angle is in degrees and ranges -360 < angle < 360
170 // The reason for this is that in latex there is a meaning for the
171 // different angles and they are not necessarliy interchangeable,
172 // it depends on the rotation origin.
173 lyx::Assert(rotateAngle < 360.0);
174 lyx::Assert(rotateAngle > -360.0);
178 bool operator==(InsetGraphicsParams const & left,
179 InsetGraphicsParams const & right)
181 if (left.filename == right.filename &&
182 left.display == right.display &&
183 left.subcaption == right.subcaption &&
184 left.subcaptionText == right.subcaptionText &&
185 left.keepAspectRatio == right.keepAspectRatio &&
186 left.widthResize == right.widthResize &&
187 left.widthSize == right.widthSize &&
188 left.heightResize == right.heightResize &&
189 left.heightSize == right.heightSize &&
190 left.rotateOrigin == right.rotateOrigin &&
191 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001)
198 bool operator!=(InsetGraphicsParams const & left,
199 InsetGraphicsParams const & right)
201 return !(left == right);
207 void writeResize(ostream & os, string const & key,
208 InsetGraphicsParams::Resize resize, double size)
210 os << ' ' << key << "Resize ";
212 os << resizeTranslator.find(resize);
213 os << ' ' << key << ' ' << size << '\n';
216 void writeOrigin(ostream & os,
217 InsetGraphicsParams::Origin origin)
219 os << " rotateOrigin " << originTranslator.find(origin);
226 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
228 // If there is no filename, write nothing for it.
229 if (! filename.empty()) {
231 << MakeRelPath(filename, OnlyPath(buf->fileName()))
235 // Save the display type
236 os << " display " << displayTranslator.find(display) << '\n';
238 // Save the subcaption status
242 if (! subcaptionText.empty())
243 os << " subcaptionText \"" << subcaptionText << '\"' << '\n';
245 writeResize(os, "width", widthResize, widthSize);
246 writeResize(os, "height", heightResize, heightSize);
248 writeOrigin(os, rotateOrigin);
249 if (lyx::float_equal(rotateAngle, 0.0, 0.001))
250 os << " rotateAngle " << rotateAngle << '\n';
256 void readResize(InsetGraphicsParams * igp, bool height,
257 string const & token)
259 InsetGraphicsParams::Resize resize = InsetGraphicsParams::DEFAULT_SIZE;
261 resize = resizeTranslator.find(token);
264 igp->heightResize = resize;
266 igp->widthResize = resize;
270 void readOrigin(InsetGraphicsParams * igp, string const & token)
272 // TODO: complete this function.
273 igp->rotateOrigin = originTranslator.find(token);
279 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
282 if (token == "filename") {
284 filename = lex.GetString();
286 if (!filename.empty()) {
287 // Make the filename with absolute directory.
288 filename = MakeAbsPath(filename, OnlyPath(buf->fileName()));
290 } else if (token == "display") {
292 string const type = lex.GetString();
294 display = displayTranslator.find(type);
295 } else if (token == "subcaption") {
297 } else if (token == "subcaptionText") {
299 subcaptionText = lex.GetString();
300 } else if (token == "widthResize") {
302 string const token = lex.GetString();
304 readResize(this, false, token);
305 } else if (token == "width") {
307 widthSize = lex.GetFloat();
308 } else if (token == "heightResize") {
310 string const token = lex.GetString();
312 readResize(this, true, token);
313 } else if (token == "height") {
315 heightSize = lex.GetFloat();
316 } else if (token == "rotateOrigin") {
318 string const token = lex.GetString();
320 readOrigin(this, token);
321 } else if (token == "rotateAngle") {
323 rotateAngle = lex.GetFloat();
325 // If it's none of the above, its not ours.