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"
22 #include "support/LOstream.h"
24 #include "support/LAssert.h"
28 /// This variable keeps a tab on whether the translator was set with the
30 bool translatorsSet = false;
32 /// This is the translator between the Resize enum and corresponding lyx
34 Translator< InsetGraphicsParams::Resize, string >
35 resizeTranslator(InsetGraphicsParams::DEFAULT_SIZE, "default");
37 /// This is the translator between the Origin enum and corresponding lyx
39 Translator< InsetGraphicsParams::Origin, string >
40 originTranslator(InsetGraphicsParams::DEFAULT, "default");
42 /// This is the translator between the Display enum and corresponding lyx
44 Translator< InsetGraphicsParams::DisplayType, string >
45 displayTranslator(InsetGraphicsParams::MONOCHROME, "monochrome");
50 InsetGraphicsParams::InsetGraphicsParams()
55 if (! translatorsSet) {
56 translatorsSet = true;
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");
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");
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");
92 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
94 // I decided to skip the initialization since the copy will overwrite
100 InsetGraphicsParams &
101 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
103 // Are we assigning the object into itself?
111 void InsetGraphicsParams::init()
113 subcaptionText = filename = string();
114 display = MONOCHROME;
116 keepAspectRatio = true;
117 widthResize = DEFAULT_SIZE;
119 heightResize = DEFAULT_SIZE;
121 rotateOrigin = DEFAULT;
127 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
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;
144 void InsetGraphicsParams::testInvariant() const
146 // Filename might be empty (when the dialog is first created).
147 // Assert(!filename.empty());
149 lyx::Assert(display == COLOR ||
150 display == MONOCHROME ||
151 display == GRAYSCALE ||
155 lyx::Assert(widthResize == DEFAULT_SIZE ||
157 widthResize == INCH ||
158 widthResize == PERCENT_PAGE ||
159 widthResize == PERCENT_COLUMN ||
163 lyx::Assert(heightResize == DEFAULT_SIZE ||
164 heightResize == CM ||
165 heightResize == INCH ||
166 heightResize == PERCENT_PAGE ||
167 heightResize == SCALE
170 // For SCALE these can be negative.
171 //lyx::Assert(widthSize >= 0.0);
172 //lyx::Assert(heightSize >= 0.0);
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);
183 bool operator==(InsetGraphicsParams const & left,
184 InsetGraphicsParams const & right)
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)
203 bool operator!=(InsetGraphicsParams const & left,
204 InsetGraphicsParams const & right)
206 return !(left == right);
212 void writeResize(ostream & os, string const & key,
213 InsetGraphicsParams::Resize resize, double size)
215 os << ' ' << key << "Resize ";
217 os << resizeTranslator.find(resize);
218 os << ' ' << key << ' ' << size << '\n';
221 void writeOrigin(ostream & os,
222 InsetGraphicsParams::Origin origin)
224 os << " rotateOrigin " << originTranslator.find(origin);
231 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
233 // If there is no filename, write nothing for it.
234 if (! filename.empty()) {
236 << MakeRelPath(filename, buf->filePath())
240 // Save the display type
241 os << " display " << displayTranslator.find(display) << '\n';
243 // Save the subcaption status
247 if (! subcaptionText.empty())
248 os << " subcaptionText \"" << subcaptionText << '\"' << '\n';
250 writeResize(os, "width", widthResize, widthSize);
251 writeResize(os, "height", heightResize, heightSize);
253 writeOrigin(os, rotateOrigin);
254 if (!lyx::float_equal(rotateAngle, 0.0, 0.001)) {
255 os << " rotateAngle " << rotateAngle << '\n';
262 void readResize(InsetGraphicsParams * igp, bool height,
263 string const & token)
265 InsetGraphicsParams::Resize resize = InsetGraphicsParams::DEFAULT_SIZE;
267 resize = resizeTranslator.find(token);
270 igp->heightResize = resize;
272 igp->widthResize = resize;
276 void readOrigin(InsetGraphicsParams * igp, string const & token)
278 // TODO: complete this function.
279 igp->rotateOrigin = originTranslator.find(token);
285 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
288 if (token == "filename") {
290 filename = lex.getString();
292 if (!filename.empty()) {
293 // Make the filename with absolute directory.
294 filename = MakeAbsPath(filename, buf->filePath());
296 } else if (token == "display") {
298 string const type = lex.getString();
300 display = displayTranslator.find(type);
301 } else if (token == "subcaption") {
303 } else if (token == "subcaptionText") {
305 subcaptionText = lex.getString();
306 } else if (token == "widthResize") {
308 string const token = lex.getString();
310 readResize(this, false, token);
311 } else if (token == "width") {
313 widthSize = lex.getFloat();
314 } else if (token == "heightResize") {
316 string const token = lex.getString();
318 readResize(this, true, token);
319 } else if (token == "height") {
321 heightSize = lex.getFloat();
322 } else if (token == "rotateOrigin") {
324 string const token = lex.getString();
326 readOrigin(this, token);
327 } else if (token == "rotateAngle") {
329 rotateAngle = lex.getFloat();
331 // If it's none of the above, its not ours.