]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
A couple of fixes from Herbert.
[lyx.git] / src / insets / insetgraphicsParams.C
1 /* This file is part of
2  * =================================================
3  * 
4  *          LyX, The Document Processor
5  *          Copyright 1995 Matthias Ettrich.
6  *          Copyright 1995-2001 The LyX Team.
7  *
8  * \author Baruch Even
9  * \author Herbert Voss <voss@lyx.org>
10  *
11  * ================================================= */
12
13 #include <config.h> 
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif 
18
19 #include "insetgraphicsParams.h"
20
21 #include "support/translator.h"
22 #include "support/filetools.h"
23 #include "support/lyxlib.h"
24 #include "support/LOstream.h"
25 #include "lyxrc.h"
26
27 #include "support/LAssert.h"
28
29 namespace {
30
31 /// This variable keeps a tab on whether the translator was set with the
32 /// translations.
33 bool translatorsSet = false;
34
35 /// This is the translator between the Display enum and corresponding lyx
36 /// file strings.
37 Translator< InsetGraphicsParams::DisplayType, string >
38 displayTranslator(InsetGraphicsParams::MONOCHROME, "monochrome");
39
40 // this is only compatibility stuff for the first 1.2 version
41 // it is obselete until 1.3
42 LyXLength convertResizeValue(string const token, LyXLex & lex) {
43     lex.next();
44     string value = lex.getString();     // "width" or "height"  
45     lex.next();                         // anyway not interesting
46     value = lex.getString();
47     if (token == "default")
48         return (LyXLength(value+"pt"));
49     else if (token == "cm")
50         return (LyXLength(value+"cm"));
51     else if (token == "inch")
52         return (LyXLength(value+"in"));
53     else if (token == "percentOfColumn")
54         return (LyXLength(value+"c%"));
55     else if (token == "percentOfPage")
56         return (LyXLength(value+"p%"));
57     else return LyXLength("0pt");       // nothing with figinset
58 }
59
60 } // namespace anon
61
62
63 InsetGraphicsParams::InsetGraphicsParams()
64 {
65         init();
66         // Set translators
67         if (! translatorsSet) {
68                 translatorsSet = true;
69                 // Fill the display translator
70                 displayTranslator.addPair(MONOCHROME, "monochrome");
71                 displayTranslator.addPair(GRAYSCALE, "grayscale");
72                 displayTranslator.addPair(COLOR, "color");
73                 displayTranslator.addPair(NONE, "none");
74         }
75 }
76
77
78 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
79 {
80         // I decided to skip the initialization since the copy will overwrite
81         // everything anyway.
82         //    init();
83         copy(igp);
84 }
85
86 InsetGraphicsParams &
87 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
88 {
89         // Are we assigning the object into itself?
90         if (this == &params)
91                 return * this;
92         copy(params);
93         return *this;
94 }
95
96 void InsetGraphicsParams::init()
97 {
98         subcaptionText = filename = string();
99         bb = string();                  // bounding box
100         draft = false;                  // draft mode
101         clip = false;                   // clip image
102         if (lyxrc.display_graphics == "mono") 
103             display = MONOCHROME;
104         else if (lyxrc.display_graphics == "gray") 
105             display = GRAYSCALE;
106         else if (lyxrc.display_graphics == "color") 
107             display = COLOR;
108         else
109             display = NONE;
110         subcaption = false;             // subfigure
111         width = LyXLength();            // set to 0pt
112         height = LyXLength();
113         lyxwidth = LyXLength();         // for the view in lyx
114         lyxheight = LyXLength();
115         scale = 0;
116         size_type = DEFAULT_SIZE;       // do nothing
117         keepAspectRatio = false;        //
118         rotateOrigin = "center";        // 
119         rotateAngle = 0.0;              // in degrees
120         special = string();             // userdefined stuff
121
122         testInvariant();
123 }
124
125 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
126 {
127         filename = igp.filename;
128         bb = igp.bb;
129         draft = igp.draft;
130         clip = igp.clip;
131         display = igp.display;
132         subcaption = igp.subcaption;
133         subcaptionText = igp.subcaptionText;
134         keepAspectRatio = igp.keepAspectRatio;
135         width = igp.width;
136         height = igp.height;
137         scale = igp.scale;
138         size_type = igp.size_type;
139         lyxwidth = igp.lyxwidth;
140         lyxheight = igp.lyxheight;
141         rotateOrigin = igp.rotateOrigin;
142         rotateAngle = igp.rotateAngle;
143         special = igp.special;
144
145         testInvariant();
146 }
147
148 void InsetGraphicsParams::testInvariant() const
149 {
150         // Filename might be empty (when the dialog is first created).
151         // Assert(!filename.empty());
152         lyx::Assert(display == COLOR ||
153                display == MONOCHROME ||
154                display == GRAYSCALE ||
155                display == NONE
156               );
157         // Angle is in degrees and ranges -360 < angle < 360
158         // The reason for this is that in latex there is a meaning for the
159         // different angles and they are not necessarliy interchangeable,
160         // it depends on the rotation origin.
161         lyx::Assert(rotateAngle < 360.0);
162         lyx::Assert(rotateAngle > -360.0);
163
164 }
165
166 bool operator==(InsetGraphicsParams const & left,
167                 InsetGraphicsParams const & right)
168 {
169         if (left.filename == right.filename &&
170                 left.bb == right.bb &&
171                 left.draft == right.draft &&
172                 left.clip == right.clip &&
173                 left.display == right.display &&
174                 left.subcaption == right.subcaption &&
175                 left.subcaptionText == right.subcaptionText &&
176                 left.keepAspectRatio == right.keepAspectRatio &&
177                 left.width == right.width &&
178                 left.height == right.height &&
179                 left.scale == right.scale &&
180                 left.size_type == right.size_type &&
181                 left.lyxwidth == right.lyxwidth &&
182                 left.lyxheight == right.lyxheight &&
183                 left.rotateOrigin == right.rotateOrigin &&
184                 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
185                 left.special == right.special) 
186            )
187                 return true;
188
189         return false;
190 }
191
192 bool operator!=(InsetGraphicsParams const & left,
193                 InsetGraphicsParams const & right)
194 {
195         return  !(left == right);
196 }
197
198
199 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
200 {
201         // If there is no filename, write nothing for it.
202         if (! filename.empty()) {
203                 os << "\tfilename "
204                 << MakeRelPath(filename, buf->filePath())
205                 << '\n';
206         }
207         if (!bb.empty())                // bounding box
208                 os << "\tBoundingBox " << bb << '\n';
209         if (clip)                       // clip image
210                 os << "\tclip\n";
211         if (draft)                      // draft mode
212                 os << "\tdraft\n";
213         // Save the display type
214         os << "\tdisplay " << displayTranslator.find(display) << '\n';
215         // Save the subcaption status
216         if (subcaption)
217             os << "\tsubcaption\n";
218         if (!subcaptionText.empty())
219             os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
220     // we always need the size type
221     // 0: no special
222     // 1: width/height combination
223     // 2: scale
224         os << "\tsize_type " <<  size_type << '\n';
225         if (!width.zero())
226             os << "\twidth " << width.asString() << '\n';
227         if (!height.zero())
228             os << "\theight " << height.asString() << '\n';
229         if (scale != 0)
230             os << "\tscale " << scale << '\n';
231         if (keepAspectRatio)
232                 os << "\tkeepAspectRatio\n";
233         if (!lyx::float_equal(rotateAngle, 0.0, 0.001))
234                 os << "\trotateAngle " << rotateAngle << '\n';
235         if (!rotateOrigin.empty())
236                 os << "\trotateOrigin " << rotateOrigin << '\n';
237         if (!special.empty())
238                 os << "\tspecial " << special << '\n';
239         if (!lyxwidth.zero())           // the lyx-viewsize
240             os << "\tlyxwidth " << lyxwidth.asString() << '\n';
241         if (!lyxheight.zero())
242             os << "\tlyxheight " << lyxheight.asString();
243 }
244
245
246 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
247                                string const& token)
248 {
249         if (token == "filename") {
250                 lex.next();
251                 filename = lex.getString();
252                 if (!filename.empty()) {
253                         // Make the filename with absolute directory.
254                         filename = MakeAbsPath(filename, buf->filePath());
255                 }
256         } else if (token == "BoundingBox") {
257                 for (int i=0; i<4 ;i++) {
258                     lex.next();
259                     bb += (lex.getString()+" ");
260                 }
261         } else if (token == "clip") {
262                 clip = true;
263         } else if (token == "draft") {
264                 draft = true;
265         } else if (token == "display") {
266                 lex.next();
267                 string const type = lex.getString();
268                 display = displayTranslator.find(type);
269         } else if (token == "subcaption") {
270                 subcaption = true;
271         } else if (token == "subcaptionText") {
272                 lex.next();
273                 subcaptionText = lex.getString();
274         } else if (token == "widthResize") {
275                 if (lex.next()) {
276                     string const token = lex.getString();
277                     if (token == "scale") {
278                         lex.next();
279                         scale = lex.getInteger();
280                         size_type = SCALE;
281                     }
282                     else {
283                         width = convertResizeValue(token, lex);
284                         size_type = WH;
285                     }
286                 }
287         } else if (token == "size_type") {
288                 lex.next();
289                 switch (lex.getInteger()) {
290                     case 0 : size_type = DEFAULT_SIZE;
291                         break;
292                     case 1 : size_type = WH;
293                         break;
294                     case 2 : size_type = SCALE;
295                 }
296         } else if (token == "width") {
297                 lex.next();
298                 width = LyXLength(lex.getString());
299                 size_type = WH;
300         } else if (token == "heightResize") {
301                 if (lex.next())
302                         height = convertResizeValue(lex.getString(), lex);
303         } else if (token == "height") {
304                 lex.next();
305                 height = LyXLength(lex.getString());
306                 size_type = WH;
307         } else if (token == "keepAspectRatio") {
308                 keepAspectRatio = true;
309         } else if (token == "scale") {
310                 lex.next();
311                 scale = lex.getInteger();
312         } else if (token == "rotateAngle") {
313                 lex.next();
314                 rotateAngle = lex.getFloat();
315         } else if (token == "rotateOrigin") {
316                 lex.next();
317                 rotateOrigin=lex.getString();
318         } else if (token == "lyxwidth") {
319                 lex.next();
320                 lyxwidth = LyXLength(lex.getString());
321         } else if (token == "lyxheight") {
322                 lex.next();
323                 lyxheight = LyXLength(lex.getString());
324         } else {
325                 // If it's none of the above, its not ours.
326                 return false;
327         }
328         return true;
329 }