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");
65 // Fill the origin translator
66 originTranslator.addPair(DEFAULT, "default");
67 originTranslator.addPair(LEFTTOP, "leftTop");
68 originTranslator.addPair(LEFTCENTER, "leftCenter");
69 originTranslator.addPair(LEFTBASELINE, "leftBaseLine");
70 originTranslator.addPair(LEFTBOTTOM, "leftBottom");
71 originTranslator.addPair(CENTERTOP, "centerTop");
72 originTranslator.addPair(CENTER, "center");
73 originTranslator.addPair(CENTERBASELINE, "centerBaseLine");
74 originTranslator.addPair(CENTERBOTTOM, "centerBottom");
75 originTranslator.addPair(RIGHTTOP, "rightTop");
76 originTranslator.addPair(RIGHTCENTER, "rightCenter");
77 originTranslator.addPair(RIGHTBASELINE, "rightBaseLine");
78 originTranslator.addPair(RIGHTBOTTOM, "rightBottom");
79 originTranslator.addPair(REFERENCE_POINT, "referencePoint");
81 // Fill the display translator
82 displayTranslator.addPair(MONOCHROME, "monochrome");
83 displayTranslator.addPair(GRAYSCALE, "grayscale");
84 displayTranslator.addPair(COLOR, "color");
85 displayTranslator.addPair(NONE, "none");
91 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
93 // I decided to skip the initialization since the copy will overwrite
100 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
102 // Are we assigning the object into itself?
110 void InsetGraphicsParams::init()
112 subcaptionText = filename = string();
113 display = MONOCHROME;
115 keepAspectRatio = true;
116 widthResize = DEFAULT_SIZE;
118 heightResize = DEFAULT_SIZE;
120 rotateOrigin = DEFAULT;
126 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
128 filename = igp.filename;
129 display = igp.display;
130 subcaption = igp.subcaption;
131 subcaptionText = igp.subcaptionText;
132 keepAspectRatio = igp.keepAspectRatio;
133 widthResize = igp.widthResize;
134 widthSize = igp.widthSize;
135 heightResize = igp.heightResize;
136 heightSize = igp.heightSize;
137 rotateOrigin = igp.rotateOrigin;
138 rotateAngle = igp.rotateAngle;
143 void InsetGraphicsParams::testInvariant() const
145 // Filename might be empty (when the dialog is first created).
146 // Assert(!filename.empty());
148 lyx::Assert(display == COLOR ||
149 display == MONOCHROME ||
150 display == GRAYSCALE ||
154 lyx::Assert(widthResize == DEFAULT_SIZE ||
156 widthResize == INCH ||
157 widthResize == PERCENT_PAGE ||
158 widthResize == PERCENT_COLUMN
161 lyx::Assert(heightResize == DEFAULT_SIZE ||
162 heightResize == CM ||
163 heightResize == INCH ||
164 heightResize == PERCENT_PAGE
167 lyx::Assert(widthSize >= 0.0);
168 lyx::Assert(heightSize >= 0.0);
170 // Angle is in degrees and ranges -360 < angle < 360
171 // The reason for this is that in latex there is a meaning for the
172 // different angles and they are not necessarliy interchangeable,
173 // it depends on the rotation origin.
174 lyx::Assert(rotateAngle < 360.0);
175 lyx::Assert(rotateAngle > -360.0);
179 bool operator==(InsetGraphicsParams const & left,
180 InsetGraphicsParams const & right)
182 if (left.filename == right.filename &&
183 left.display == right.display &&
184 left.subcaption == right.subcaption &&
185 left.subcaptionText == right.subcaptionText &&
186 left.keepAspectRatio == right.keepAspectRatio &&
187 left.widthResize == right.widthResize &&
188 left.widthSize == right.widthSize &&
189 left.heightResize == right.heightResize &&
190 left.heightSize == right.heightSize &&
191 left.rotateOrigin == right.rotateOrigin &&
192 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001)
199 bool operator!=(InsetGraphicsParams const & left,
200 InsetGraphicsParams const & right)
202 return !(left == right);
208 void writeResize(ostream & os, string const & key,
209 InsetGraphicsParams::Resize resize, double size)
211 os << ' ' << key << "Resize ";
213 os << resizeTranslator.find(resize);
214 os << ' ' << key << ' ' << size << '\n';
217 void writeOrigin(ostream & os,
218 InsetGraphicsParams::Origin origin)
220 os << " rotateOrigin " << originTranslator.find(origin);
227 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
229 // If there is no filename, write nothing for it.
230 if (! filename.empty()) {
232 << MakeRelPath(filename, OnlyPath(buf->fileName()))
236 // Save the display type
237 os << " display " << displayTranslator.find(display) << '\n';
239 // Save the subcaption status
243 if (! subcaptionText.empty())
244 os << " subcaptionText \"" << subcaptionText << '\"' << '\n';
246 writeResize(os, "width", widthResize, widthSize);
247 writeResize(os, "height", heightResize, heightSize);
249 writeOrigin(os, rotateOrigin);
250 if (lyx::float_equal(rotateAngle, 0.0, 0.001))
251 os << " rotateAngle " << rotateAngle << '\n';
257 void readResize(InsetGraphicsParams * igp, bool height,
258 string const & token)
260 InsetGraphicsParams::Resize resize = InsetGraphicsParams::DEFAULT_SIZE;
262 resize = resizeTranslator.find(token);
265 igp->heightResize = resize;
267 igp->widthResize = resize;
271 void readOrigin(InsetGraphicsParams * igp, string const & token)
273 // TODO: complete this function.
274 igp->rotateOrigin = originTranslator.find(token);
280 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
283 if (token == "filename") {
285 filename = lex.GetString();
287 if (!filename.empty()) {
288 // Make the filename with absolute directory.
289 filename = MakeAbsPath(filename, OnlyPath(buf->fileName()));
291 } else if (token == "display") {
293 string const type = lex.GetString();
295 display = displayTranslator.find(type);
296 } else if (token == "subcaption") {
298 } else if (token == "subcaptionText") {
300 subcaptionText = lex.GetString();
301 } else if (token == "widthResize") {
303 string const token = lex.GetString();
305 readResize(this, false, token);
306 } else if (token == "width") {
308 widthSize = lex.GetFloat();
309 } else if (token == "heightResize") {
311 string const token = lex.GetString();
313 readResize(this, true, token);
314 } else if (token == "height") {
316 heightSize = lex.GetFloat();
317 } else if (token == "rotateOrigin") {
319 string const token = lex.GetString();
321 readOrigin(this, token);
322 } else if (token == "rotateAngle") {
324 rotateAngle = lex.GetFloat();
326 // If it's none of the above, its not ours.