]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
870f8d18dd9c8ad2e5115ae77c908c144b85f837
[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 "lyxrc.h"
22 #include "support/translator.h"
23 #include "support/filetools.h"
24 #include "support/lyxlib.h"
25 #include "support/LOstream.h"
26 #include "support/LAssert.h"
27
28 namespace {
29
30 /// This variable keeps a tab on whether the translator was set with the
31 /// translations.
32 bool translatorsSet = false;
33
34 /// This is the translator between the Display enum and corresponding lyx
35 /// file strings.
36 Translator< InsetGraphicsParams::DisplayType, string >
37 displayTranslator(InsetGraphicsParams::DEFAULT, "default");
38
39 // this is only compatibility stuff for the first 1.2 version
40 // it is obselete until 1.3
41 LyXLength convertResizeValue(string const token, LyXLex & lex) {
42     lex.next();
43     string value = lex.getString();     // "width" or "height"  
44     lex.next();                         // anyway not interesting
45     value = lex.getString();
46     if (token == "default")
47         return (LyXLength(value+"pt"));
48     else if (token == "cm")
49         return (LyXLength(value+"cm"));
50     else if (token == "inch")
51         return (LyXLength(value+"in"));
52     else if (token == "percentOfColumn")
53         return (LyXLength(value+"c%"));
54     else if (token == "percentOfPage")
55         return (LyXLength(value+"p%"));
56     else return LyXLength("0pt");       // nothing with figinset
57 }
58
59 } // namespace anon
60
61
62 InsetGraphicsParams::InsetGraphicsParams()
63 {
64         init();
65         // Set translators
66         if (! translatorsSet) {
67                 translatorsSet = true;
68                 // Fill the display translator
69                 displayTranslator.addPair(DEFAULT, "default");
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 == DEFAULT ||
153                display == COLOR ||
154                display == MONOCHROME ||
155                display == GRAYSCALE ||
156                display == NONE
157               );
158         // Angle is in degrees and ranges -360 < angle < 360
159         // The reason for this is that in latex there is a meaning for the
160         // different angles and they are not necessarliy interchangeable,
161         // it depends on the rotation origin.
162         lyx::Assert(rotateAngle < 360.0);
163         lyx::Assert(rotateAngle > -360.0);
164
165 }
166
167 bool operator==(InsetGraphicsParams const & left,
168                 InsetGraphicsParams const & right)
169 {
170         if (left.filename == right.filename &&
171                 left.bb == right.bb &&
172                 left.draft == right.draft &&
173                 left.clip == right.clip &&
174                 left.display == right.display &&
175                 left.subcaption == right.subcaption &&
176                 left.subcaptionText == right.subcaptionText &&
177                 left.keepAspectRatio == right.keepAspectRatio &&
178                 left.width == right.width &&
179                 left.height == right.height &&
180                 left.scale == right.scale &&
181                 left.size_type == right.size_type &&
182                 left.lyxwidth == right.lyxwidth &&
183                 left.lyxheight == right.lyxheight &&
184                 left.rotateOrigin == right.rotateOrigin &&
185                 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
186                 left.special == right.special) 
187            )
188                 return true;
189
190         return false;
191 }
192
193 bool operator!=(InsetGraphicsParams const & left,
194                 InsetGraphicsParams const & right)
195 {
196         return  !(left == right);
197 }
198
199
200 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
201 {
202         // If there is no filename, write nothing for it.
203         if (! filename.empty()) {
204                 os << "\tfilename "
205                 << MakeRelPath(filename, buf->filePath())
206                 << '\n';
207         }
208         if (!bb.empty())                // bounding box
209                 os << "\tBoundingBox " << bb << '\n';
210         if (clip)                       // clip image
211                 os << "\tclip\n";
212         if (draft)                      // draft mode
213                 os << "\tdraft\n";
214         // Save the display type
215         os << "\tdisplay " << displayTranslator.find(display) << '\n';
216         // Save the subcaption status
217         if (subcaption)
218             os << "\tsubcaption\n";
219         if (!subcaptionText.empty())
220             os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
221     // we always need the size type
222     // 0: no special
223     // 1: width/height combination
224     // 2: scale
225         os << "\tsize_type " <<  size_type << '\n';
226         if (!width.zero())
227             os << "\twidth " << width.asString() << '\n';
228         if (!height.zero())
229             os << "\theight " << height.asString() << '\n';
230         if (scale != 0)
231             os << "\tscale " << scale << '\n';
232         if (keepAspectRatio)
233                 os << "\tkeepAspectRatio\n";
234         if (!lyx::float_equal(rotateAngle, 0.0, 0.001))
235                 os << "\trotateAngle " << rotateAngle << '\n';
236         if (!rotateOrigin.empty())
237                 os << "\trotateOrigin " << rotateOrigin << '\n';
238         if (!special.empty())
239                 os << "\tspecial " << special << '\n';
240         if (!lyxwidth.zero())           // the lyx-viewsize
241             os << "\tlyxwidth " << lyxwidth.asString() << '\n';
242         if (!lyxheight.zero())
243             os << "\tlyxheight " << lyxheight.asString();
244 }
245
246
247 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
248                                string const& token)
249 {
250         if (token == "filename") {
251                 lex.next();
252                 filename = lex.getString();
253                 if (!filename.empty()) {
254                         // Make the filename with absolute directory.
255                         filename = MakeAbsPath(filename, buf->filePath());
256                 }
257         } else if (token == "BoundingBox") {
258                 for (int i=0; i<4 ;i++) {
259                     lex.next();
260                     bb += (lex.getString()+" ");
261                 }
262         } else if (token == "clip") {
263                 clip = true;
264         } else if (token == "draft") {
265                 draft = true;
266         } else if (token == "display") {
267                 lex.next();
268                 string const type = lex.getString();
269                 display = displayTranslator.find(type);
270         } else if (token == "subcaption") {
271                 subcaption = true;
272         } else if (token == "subcaptionText") {
273                 lex.next();
274                 subcaptionText = lex.getString();
275         } else if (token == "widthResize") {
276                 if (lex.next()) {
277                     string const token = lex.getString();
278                     if (token == "scale") {
279                         lex.next();
280                         scale = lex.getInteger();
281                         size_type = SCALE;
282                     }
283                     else {
284                         width = convertResizeValue(token, lex);
285                         size_type = WH;
286                     }
287                 }
288         } else if (token == "size_type") {
289                 lex.next();
290                 switch (lex.getInteger()) {
291                     case 0 : size_type = DEFAULT_SIZE;
292                         break;
293                     case 1 : size_type = WH;
294                         break;
295                     case 2 : size_type = SCALE;
296                 }
297         } else if (token == "width") {
298                 lex.next();
299                 width = LyXLength(lex.getString());
300                 size_type = WH;
301         } else if (token == "heightResize") {
302                 if (lex.next())
303                         height = convertResizeValue(lex.getString(), lex);
304         } else if (token == "height") {
305                 lex.next();
306                 height = LyXLength(lex.getString());
307                 size_type = WH;
308         } else if (token == "keepAspectRatio") {
309                 keepAspectRatio = true;
310         } else if (token == "scale") {
311                 lex.next();
312                 scale = lex.getInteger();
313         } else if (token == "rotateAngle") {
314                 lex.next();
315                 rotateAngle = lex.getFloat();
316         } else if (token == "rotateOrigin") {
317                 lex.next();
318                 rotateOrigin=lex.getString();
319         } else if (token == "lyxwidth") {
320                 lex.next();
321                 lyxwidth = LyXLength(lex.getString());
322         } else if (token == "lyxheight") {
323                 lex.next();
324                 lyxheight = LyXLength(lex.getString());
325         } else {
326                 // If it's none of the above, its not ours.
327                 return false;
328         }
329         return true;
330 }