2 /* This file is part of
3 * =================================================
5 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich.
7 * Copyright 1995-2000 The LyX Team.
9 * This file Copyright 2000 Baruch Even
10 * ================================================= */
15 #pragma implementation
18 #include "insetgraphicsParams.h"
20 #include "support/translator.h"
21 #include "support/filetools.h"
23 #include "support/LAssert.h"
25 /// This variable keeps a tab on whether the translator was set with the
27 static bool translatorsSet = false;
29 /// This is the translator between the Resize enum and corresponding lyx
31 static Translator < InsetGraphicsParams::Resize, string >
32 resizeTranslator(InsetGraphicsParams::DEFAULT_SIZE, "default");
34 /// This is the translator between the Origin enum and corresponding lyx
36 static Translator < InsetGraphicsParams::Origin, string >
37 originTranslator(InsetGraphicsParams::DEFAULT, "default");
39 /// This is the translator between the Display enum and corresponding lyx
41 static Translator < InsetGraphicsParams::DisplayType, string >
42 displayTranslator(InsetGraphicsParams::MONOCHROME, "monochrome");
46 InsetGraphicsParams::InsetGraphicsParams()
51 if (! translatorsSet) {
52 translatorsSet = true;
54 // Fill the resize translator
55 resizeTranslator.addPair(DEFAULT_SIZE, "default");
56 resizeTranslator.addPair(CM, "cm");
57 resizeTranslator.addPair(INCH, "inch");
58 resizeTranslator.addPair(PERCENT_PAGE, "percentOfPage");
59 resizeTranslator.addPair(PERCENT_COLUMN, "percentOfColumn");
61 // Fill the origin translator
62 originTranslator.addPair(DEFAULT, "default");
63 originTranslator.addPair(LEFTTOP, "leftTop");
64 originTranslator.addPair(LEFTCENTER, "leftCenter");
65 originTranslator.addPair(LEFTBASELINE, "leftBaseLine");
66 originTranslator.addPair(LEFTBOTTOM, "leftBottom");
67 originTranslator.addPair(CENTERTOP, "centerTop");
68 originTranslator.addPair(CENTER, "center");
69 originTranslator.addPair(CENTERBASELINE, "centerBaseLine");
70 originTranslator.addPair(CENTERBOTTOM, "centerBottom");
71 originTranslator.addPair(RIGHTTOP, "rightTop");
72 originTranslator.addPair(RIGHTCENTER, "rightCenter");
73 originTranslator.addPair(RIGHTBASELINE, "rightBaseLine");
74 originTranslator.addPair(RIGHTBOTTOM, "rightBottom");
75 originTranslator.addPair(REFERENCE_POINT, "referencePoint");
77 // Fill the display translator
78 displayTranslator.addPair(MONOCHROME, "monochrome");
79 displayTranslator.addPair(GRAYSCALE, "grayscale");
80 displayTranslator.addPair(COLOR, "color");
81 displayTranslator.addPair(NONE, "none");
87 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
89 // I decided to skip the initialization since the copy will overwrite
96 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
98 // Are we assigning the object into itself?
106 void InsetGraphicsParams::init()
108 subcaptionText = filename = string();
109 display = MONOCHROME;
110 inlineFigure = false;
112 keepAspectRatio = true;
113 widthResize = DEFAULT_SIZE;
115 heightResize = DEFAULT_SIZE;
117 rotateOrigin = DEFAULT;
123 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
125 filename = igp.filename;
126 display = igp.display;
127 inlineFigure = igp.inlineFigure;
128 subcaption = igp.subcaption;
129 subcaptionText = igp.subcaptionText;
130 keepAspectRatio = igp.keepAspectRatio;
131 widthResize = igp.widthResize;
132 widthSize = igp.widthSize;
133 heightResize = igp.heightResize;
134 heightSize = igp.heightSize;
135 rotateOrigin = igp.rotateOrigin;
136 rotateAngle = igp.rotateAngle;
141 void InsetGraphicsParams::testInvariant() const
143 // Filename might be empty (when the dialog is first created).
144 // Assert(!filename.empty());
146 Assert(display == COLOR ||
147 display == MONOCHROME ||
148 display == GRAYSCALE ||
152 Assert(widthResize == DEFAULT_SIZE ||
154 widthResize == INCH ||
155 widthResize == PERCENT_PAGE ||
156 widthResize == PERCENT_COLUMN
159 Assert(heightResize == DEFAULT_SIZE ||
160 heightResize == CM ||
161 heightResize == INCH ||
162 heightResize == PERCENT_PAGE
165 Assert(widthSize >= 0.0);
166 Assert(heightSize >= 0.0);
168 // Angle is in degrees and ranges -360 < angle < 360
169 // The reason for this is that in latex there is a meaning for the
170 // different angles and they are not necessarliy interchangeable,
171 // it depends on the rotation origin.
172 Assert(rotateAngle < 360);
173 Assert(rotateAngle > -360);
177 bool operator==(InsetGraphicsParams const & left,
178 InsetGraphicsParams const & right)
180 if (left.filename == right.filename &&
181 left.display == right.display &&
182 left.inlineFigure == right.inlineFigure &&
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 left.rotateAngle == right.rotateAngle
200 void writeResize(ostream & os, string const & key,
201 InsetGraphicsParams::Resize resize, double size)
203 os << ' ' << key << "Resize ";
205 os << resizeTranslator.find(resize);
206 os << ' ' << key << ' ' << size << '\n';
209 static void writeOrigin(ostream & os,
210 InsetGraphicsParams::Origin origin)
212 os << " rotateOrigin " << originTranslator.find(origin);
216 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
218 // If there is no filename, write nothing for it.
219 if (! filename.empty()) {
221 << MakeRelPath(filename, OnlyPath(buf->fileName()))
225 // Save the display type
226 os << " display " << displayTranslator.find(display) << '\n';
228 // Save the inline status
232 // Save the subcaption status
236 if (! subcaptionText.empty())
237 os << " subcaptionText \"" << subcaptionText << '\"' << '\n';
239 writeResize(os, "width", widthResize, widthSize);
240 writeResize(os, "height", heightResize, heightSize);
242 writeOrigin(os, rotateOrigin);
243 if (rotateAngle != 0)
244 os << " rotateAngle " << rotateAngle << '\n';
249 void readResize(InsetGraphicsParams * igp, bool height,
250 string const & token)
252 InsetGraphicsParams::Resize resize = InsetGraphicsParams::DEFAULT_SIZE;
254 resize = resizeTranslator.find(token);
257 igp->heightResize = resize;
259 igp->widthResize = resize;
264 void readOrigin(InsetGraphicsParams * igp, string const & token)
265 { // TODO: complete this function.
266 igp->rotateOrigin = originTranslator.find(token);
269 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
272 if (token == "filename") {
274 filename = lex.GetString();
276 if (!filename.empty()) {
277 // Make the filename with absolute directory.
278 filename = MakeAbsPath(filename, OnlyPath(buf->fileName()));
280 } else if (token == "display") {
282 string const type = lex.GetString();
284 display = displayTranslator.find(type);
285 } else if (token == "inline") {
287 } else if (token == "subcaption") {
289 } else if (token == "subcaptionText") {
291 subcaptionText = lex.GetString();
292 } else if (token == "widthResize") {
294 string const token = lex.GetString();
296 readResize(this, false, token);
297 } else if (token == "width") {
299 widthSize = lex.GetFloat();
300 } else if (token == "heightResize") {
302 string const token = lex.GetString();
304 readResize(this, true, token);
305 } else if (token == "height") {
307 heightSize = lex.GetFloat();
308 } else if (token == "rotateOrigin") {
310 string const token = lex.GetString();
312 readOrigin(this, token);
313 } else if (token == "rotateAngle") {
315 rotateAngle = lex.GetInteger();
317 // If it's none of the above, its not ours.