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 * ================================================= */
13 #pragma implementation
17 #include "insetgraphicsParams.h"
19 #include "support/translator.h"
20 #include "support/filetools.h"
22 #ifdef ENABLE_ASSERTIONS
23 #include "support/LAssert.h"
26 /// This variable keeps a tab on whether the translator was set with the
28 static bool translatorsSet = false;
30 /// This is the translator between the Resize enum and corresponding lyx
32 static Translator<InsetGraphicsParams::Resize, string>
33 resizeTranslator(InsetGraphicsParams::DEFAULT_SIZE, "default");
35 /// This is the translator between the Origin enum and corresponding lyx
37 static Translator<InsetGraphicsParams::Origin, string>
38 originTranslator(InsetGraphicsParams::DEFAULT, "default");
40 /// This is the translator between the Display enum and corresponding lyx
42 static Translator<InsetGraphicsParams::DisplayType, string>
43 displayTranslator(InsetGraphicsParams::MONOCHROME, "monochrome");
47 InsetGraphicsParams::InsetGraphicsParams()
52 if (! translatorsSet) {
53 translatorsSet = true;
55 // Fill the resize translator
56 resizeTranslator.addPair(DEFAULT_SIZE, "default");
57 resizeTranslator.addPair(CM, "cm");
58 resizeTranslator.addPair(INCH, "inch");
59 resizeTranslator.addPair(PERCENT_PAGE, "percentOfPage");
60 resizeTranslator.addPair(PERCENT_COLUMN, "percentOfColumn");
62 // Fill the origin translator
63 originTranslator.addPair(DEFAULT, "default");
64 originTranslator.addPair(LEFTTOP, "leftTop");
65 originTranslator.addPair(LEFTCENTER, "leftCenter");
66 originTranslator.addPair(LEFTBASELINE, "leftBaseLine");
67 originTranslator.addPair(LEFTBOTTOM, "leftBottom");
68 originTranslator.addPair(CENTERTOP, "centerTop");
69 originTranslator.addPair(CENTER, "center");
70 originTranslator.addPair(CENTERBASELINE, "centerBaseLine");
71 originTranslator.addPair(CENTERBOTTOM, "centerBottom");
72 originTranslator.addPair(RIGHTTOP, "rightTop");
73 originTranslator.addPair(RIGHTCENTER, "rightCenter");
74 originTranslator.addPair(RIGHTBASELINE, "rightBaseLine");
75 originTranslator.addPair(RIGHTBOTTOM, "rightBottom");
76 originTranslator.addPair(REFERENCE_POINT, "referencePoint");
78 // Fill the display translator
79 displayTranslator.addPair(MONOCHROME, "monochrome");
80 displayTranslator.addPair(GRAYSCALE, "grayscale");
81 displayTranslator.addPair(COLOR, "color");
82 displayTranslator.addPair(NONE, "none");
88 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
90 // I decided to skip the initialization since the copy will overwrite
96 InsetGraphicsParams const &
97 InsetGraphicsParams::operator=(InsetGraphicsParams const ¶ms)
99 // Are we assigning the object into itself?
107 void InsetGraphicsParams::init()
109 subcaptionText = filename = string();
110 display = MONOCHROME;
111 inlineFigure = false;
113 keepAspectRatio = true;
114 widthResize = DEFAULT_SIZE;
116 heightResize = DEFAULT_SIZE;
118 rotateOrigin = DEFAULT;
121 #ifdef ENABLE_ASSERTION
126 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
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;
141 #ifdef ENABLE_ASSERTIONS
146 void InsetGraphicsParams::testInvariant() const
148 #ifdef ENABLE_ASSERTIONS
149 // Filename might be empty (when the dialog is first created).
150 // Assert(!filename.empty());
152 Assert(display == COLOR ||
153 display == MONOCHROME ||
154 display == GRAYSCALE ||
158 Assert(widthResize == DEFAULT_SIZE ||
160 widthResize == INCH ||
161 widthResize == PERCENT_PAGE ||
162 widthResize == PERCENT_COLUMN
165 Assert(heightResize == DEFAULT_SIZE ||
166 heightResize == CM ||
167 heightResize == INCH ||
168 heightResize == PERCENT_PAGE
171 Assert(widthSize >= 0.0);
172 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 Assert(rotateAngle < 360);
179 Assert(rotateAngle > -360);
184 bool operator==(InsetGraphicsParams const & left,
185 InsetGraphicsParams const & right)
187 if (left.filename == right.filename &&
188 left.display == right.display &&
189 left.inlineFigure == right.inlineFigure &&
190 left.subcaption == right.subcaption &&
191 left.subcaptionText == right.subcaptionText &&
192 left.keepAspectRatio == right.keepAspectRatio &&
193 left.widthResize == right.widthResize &&
194 left.widthSize == right.widthSize &&
195 left.heightResize == right.heightResize &&
196 left.heightSize == right.heightSize &&
197 left.rotateOrigin == right.rotateOrigin &&
198 left.rotateAngle == right.rotateAngle
205 static void writeResize(ostream & os, char const * key,
206 InsetGraphicsParams::Resize resize, double size)
208 os << ' ' << key << "Resize ";
210 os << resizeTranslator.find(resize);
212 // Old code, before using translators
214 case InsetGraphicsParams::DEFAULT_SIZE:
218 case InsetGraphicsParams::CM:
222 case InsetGraphicsParams::INCH:
226 case InsetGraphicsParams::PERCENT_PAGE:
227 os << "percentOfPage";
230 case InsetGraphicsParams::PERCENT_COLUMN:
231 os << "percentOfColumnt";
235 os << ' ' << key << ' ' << size << endl;
238 static void writeOrigin(ostream & os,
239 InsetGraphicsParams::Origin origin)
241 os << " rotateOrigin " << originTranslator.find(origin);
246 case InsetGraphicsParams:: DEFAULT:
249 case InsetGraphicsParams:: LEFTTOP:
252 case InsetGraphicsParams:: LEFTCENTER:
255 case InsetGraphicsParams:: LEFTBASELINE:
256 os << "LeftBaseLine";
258 case InsetGraphicsParams:: LEFTBOTTOM:
261 case InsetGraphicsParams:: CENTERTOP:
264 case InsetGraphicsParams:: CENTER:
267 case InsetGraphicsParams:: CENTERBASELINE:
268 os << "CenterBaseLine";
270 case InsetGraphicsParams:: CENTERBOTTOM:
271 os << "CenterBottom";
273 case InsetGraphicsParams:: RIGHTTOP:
276 case InsetGraphicsParams:: RIGHTCENTER:
279 case InsetGraphicsParams:: RIGHTBASELINE:
280 os << "RightBaseLine";
282 case InsetGraphicsParams:: RIGHTBOTTOM:
285 // Current REFERENCE_POINT is aliased to LEFTBASELINE
286 // case InsetGraphicsParams:: REFERENCE_POINT:
293 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
295 // If there is no filename, write nothing for it.
296 if (! filename.empty()) {
298 << MakeRelPath(filename, OnlyPath(buf->fileName()))
302 // Save the display type
303 os << " display " << displayTranslator.find(display) << endl;
325 // Save the inline status
329 // Save the subcaption status
333 if (! subcaptionText.empty())
334 os << " subcaptionText \"" << subcaptionText << '\"' << endl;
336 writeResize(os, "width", widthResize, widthSize);
337 writeResize(os, "height", heightResize, heightSize);
339 writeOrigin(os, rotateOrigin);
340 if (rotateAngle != 0)
341 os << " rotateAngle " << rotateAngle << endl;
344 static readResize(InsetGraphicsParams * igp, bool height, string const & token)
346 InsetGraphicsParams::Resize resize = InsetGraphicsParams::DEFAULT_SIZE;
348 resize = resizeTranslator.find(token);
350 // Old code, before translator.
351 if (token == "default")
352 resize = InsetGraphicsParams::DEFAULT_SIZE;
353 else if (token == "cm")
354 resize = InsetGraphicsParams::CM;
355 else if (token == "inch")
356 resize = InsetGraphicsParams::INCH;
357 else if (token == "percentOfPage")
358 resize = InsetGraphicsParams::PERCENT_PAGE;
359 else if (token == "percentOfColumn")
360 resize = InsetGraphicsParams::PERCENT_COLUMN;
362 lyxerr << "BUG: When reading resize value of InsetGraphicsParam"
363 " unknown token found '" << token << '\'' << endl;
368 igp->heightResize = resize;
370 igp->widthResize = resize;
373 static readOrigin(InsetGraphicsParams * igp, string const & token)
374 { // TODO: complete this function.
375 igp->rotateOrigin = originTranslator.find(token);
378 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
381 if (token == "filename") {
383 filename = lex.GetString();
385 if (!filename.empty()) {
386 // Make the filename with absolute directory.
387 filename = MakeAbsPath(filename, OnlyPath(buf->fileName()));
389 } else if (token == "display") {
391 string const type = lex.GetString();
393 display = displayTranslator.find(type);
397 else if (type == "grayscale")
399 else if (type == "monochrome")
400 display = MONOCHROME;
401 else if (type == "none")
404 display = MONOCHROME;
405 lyxerr << "BUG: When reading InsetGraphicsParams"
406 " display has an unknown type " << type << endl;
409 } else if (token == "inline") {
411 } else if (token == "subcaption") {
413 } else if (token == "subcaptionText") {
415 subcaptionText = lex.GetString();
416 } else if (token == "widthResize") {
418 string const token = lex.GetString();
420 readResize(this, false, token);
421 } else if (token == "width") {
423 widthSize = lex.GetFloat();
424 } else if (token == "heightResize") {
426 string const token = lex.GetString();
428 readResize(this, true, token);
429 } else if (token == "height") {
431 heightSize = lex.GetFloat();
432 } else if (token == "rotateOrigin") {
434 string const token = lex.GetString();
436 readOrigin(this, token);
437 } else if (token == "rotateAngle") {
439 rotateAngle = lex.GetInteger();
441 // If it's none of the above, its not ours.