]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
(Herbert): small read graphics inset bug fix.
[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 "support/LAssert.h"
26
27
28 using std::ostream;
29
30
31 namespace {
32
33 /// This variable keeps a tab on whether the translator was set with the
34 /// translations.
35 bool translatorsSet = false;
36
37 /// This is the translator between the Display enum and corresponding lyx
38 /// file strings.
39 Translator< InsetGraphicsParams::DisplayType, string >
40 displayTranslator(InsetGraphicsParams::DEFAULT, "default");
41
42 } // namespace anon
43
44
45 InsetGraphicsParams::InsetGraphicsParams()
46 {
47         init();
48         // Set translators
49         if (! translatorsSet) {
50                 translatorsSet = true;
51                 // Fill the display translator
52                 displayTranslator.addPair(DEFAULT, "default");
53                 displayTranslator.addPair(MONOCHROME, "monochrome");
54                 displayTranslator.addPair(GRAYSCALE, "grayscale");
55                 displayTranslator.addPair(COLOR, "color");
56                 displayTranslator.addPair(NONE, "none");
57         }
58 }
59
60
61 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
62 {
63         // I decided to skip the initialization since the copy will overwrite
64         // everything anyway.
65         //    init();
66         copy(igp);
67 }
68
69 InsetGraphicsParams &
70 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
71 {
72         // Are we assigning the object into itself?
73         if (this == &params)
74                 return * this;
75         copy(params);
76         return *this;
77 }
78
79 void InsetGraphicsParams::init()
80 {
81         subcaptionText = filename = string();
82         bb = string();                  // bounding box
83         draft = false;                  // draft mode
84         clip = false;                   // clip image
85         display = DEFAULT;              // see pref
86         subcaption = false;             // subfigure
87         noUnzip = false;                // unzip files
88         width = LyXLength();            // set to 0pt
89         height = LyXLength();
90         lyxwidth = LyXLength();         // for the view in lyx
91         lyxheight = LyXLength();        // also set to 0pt
92         scale = 0;                      // unit is %
93         lyxscale = 0;                   // same for lyxview
94         size_type = DEFAULT_SIZE;       // do nothing
95         lyxsize_type = DEFAULT_SIZE;    // do nothing
96         keepAspectRatio = false;        // only for latex
97         rotate = false;                 // Rotating
98         rotateOrigin = "center";        // Origin
99         rotateAngle = 0.0;              // in degrees
100         special = string();             // userdefined stuff
101 }
102
103 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
104 {
105         filename = igp.filename;
106         bb = igp.bb;
107         draft = igp.draft;
108         clip = igp.clip;
109         display = igp.display;
110         subcaption = igp.subcaption;
111         subcaptionText = igp.subcaptionText;
112         noUnzip = igp.noUnzip;
113         keepAspectRatio = igp.keepAspectRatio;
114         width = igp.width;
115         height = igp.height;
116         scale = igp.scale;
117         size_type = igp.size_type;
118         lyxsize_type = igp.lyxsize_type;
119         lyxwidth = igp.lyxwidth;
120         lyxheight = igp.lyxheight;
121         lyxscale = igp.lyxscale;
122         rotate = igp.rotate;
123         rotateOrigin = igp.rotateOrigin;
124         rotateAngle = igp.rotateAngle;
125         special = igp.special;
126 }
127
128 bool operator==(InsetGraphicsParams const & left,
129                 InsetGraphicsParams const & right)
130 {
131         if (left.filename == right.filename &&
132             left.bb == right.bb &&
133             left.draft == right.draft &&
134             left.clip == right.clip &&
135             left.display == right.display &&
136             left.subcaption == right.subcaption &&
137             left.noUnzip == right.noUnzip &&
138             left.subcaptionText == right.subcaptionText &&
139             left.keepAspectRatio == right.keepAspectRatio &&
140             left.width == right.width &&
141             left.height == right.height &&
142             left.scale == right.scale &&
143             left.size_type == right.size_type &&
144             left.lyxsize_type == right.lyxsize_type &&
145             left.lyxwidth == right.lyxwidth &&
146             left.lyxheight == right.lyxheight &&
147             left.lyxscale == right.lyxscale &&
148             left.rotate == right.rotate &&
149             left.rotateOrigin == right.rotateOrigin &&
150             lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
151                              left.special == right.special)
152                 )
153                 return true;
154
155         return false;
156 }
157
158 bool operator!=(InsetGraphicsParams const & left,
159                 InsetGraphicsParams const & right)
160 {
161         return  !(left == right);
162 }
163
164
165 void InsetGraphicsParams::Write(ostream & os) const
166 {
167         // If there is no filename, write nothing for it.
168         if (!filename.empty()) {
169                 os << "\tfilename " << filename << '\n';
170         }
171         if (!bb.empty())                // bounding box
172                 os << "\tBoundingBox " << bb << '\n';
173         if (clip)                       // clip image
174                 os << "\tclip\n";
175         if (draft)                      // draft mode
176                 os << "\tdraft\n";
177         // Save the display type for the view inside lyx
178         os << "\tdisplay " << displayTranslator.find(display) << '\n';
179         // Save the subcaption status
180         if (subcaption)
181                 os << "\tsubcaption\n";
182         if (!subcaptionText.empty())
183                 os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
184         if (noUnzip)
185                 os << "\tnoUnzip\n";
186         // we always need the size type
187         // 0: no special
188         // 1: width/height combination
189         // 2: scale
190         os << "\tsize_type " <<  size_type << '\n';
191         if (!width.zero())
192                 os << "\twidth " << width.asString() << '\n';
193         if (!height.zero())
194                 os << "\theight " << height.asString() << '\n';
195         if (scale != 0)
196                 os << "\tscale " << scale << '\n';
197         if (keepAspectRatio)
198                 os << "\tkeepAspectRatio\n";
199         if (rotate)
200                 os << "\trotate\n";
201         if (rotateAngle != 0.0)
202                 os << "\trotateAngle " << rotateAngle << '\n';
203         if (!rotateOrigin.empty())
204                 os << "\trotateOrigin " << rotateOrigin << '\n';
205         if (!special.empty())
206                 os << "\tspecial " << special << '\n';
207         // the values for the view in lyx
208         os << "\tlyxsize_type " <<  lyxsize_type << '\n';
209         if (!lyxwidth.zero())           // the lyx-viewsize
210                 os << "\tlyxwidth " << lyxwidth.asString() << '\n';
211         if (!lyxheight.zero())
212                 os << "\tlyxheight " << lyxheight.asString();
213         if (lyxscale != 0)
214                 os << "\tlyxscale " << lyxscale << '\n';
215 }
216
217
218 bool InsetGraphicsParams::Read(LyXLex & lex, string const& token)
219 {
220         if (token == "filename") {
221                 lex.next();
222                 filename = lex.getString();
223         } else if (token == "BoundingBox") {
224                 for (int i=0; i<4 ;i++) {
225                         lex.next();
226                         bb += (lex.getString()+" ");
227                 }
228         } else if (token == "clip") {
229                 clip = true;
230         } else if (token == "draft") {
231                 draft = true;
232         } else if (token == "display") {
233                 lex.next();
234                 string const type = lex.getString();
235                 display = displayTranslator.find(type);
236         } else if (token == "subcaption") {
237                 subcaption = true;
238         } else if (token == "subcaptionText") {
239                 lex.next();
240                 subcaptionText = lex.getString();
241         } else if (token == "noUnzip") {
242                 noUnzip = true;
243         } else if (token == "size_type") {
244                 lex.next();
245                 switch (lex.getInteger()) {
246                 case 0:
247                         size_type = DEFAULT_SIZE;
248                         break;
249                 case 1:
250                         size_type = WH;
251                         break;
252                 case 2:
253                         size_type = SCALE;
254                         break;
255                 }
256         } else if (token == "width") {
257                 lex.next();
258                 width = LyXLength(lex.getString());
259         } else if (token == "height") {
260                 lex.next();
261                 height = LyXLength(lex.getString());
262         } else if (token == "keepAspectRatio") {
263                 keepAspectRatio = true;
264         } else if (token == "scale") {
265                 lex.next();
266                 scale = lex.getInteger();
267         } else if (token == "rotate") {
268                 rotate = true;
269         } else if (token == "rotateAngle") {
270                 lex.next();
271                 rotateAngle = lex.getFloat();
272         } else if (token == "rotateOrigin") {
273                 lex.next();
274                 rotateOrigin=lex.getString();
275         } else if (token == "lyxsize_type") {
276                 lex.next();
277                 switch (lex.getInteger()) {
278                 case 0:
279                         lyxsize_type = DEFAULT_SIZE;
280                         break;
281                 case 1:
282                         lyxsize_type = WH;
283                         break;
284                 case 2:
285                         lyxsize_type = SCALE;
286                         break;
287                 }
288         } else if (token == "lyxwidth") {
289                 lex.next();
290                 lyxwidth = LyXLength(lex.getString());
291         } else if (token == "lyxheight") {
292                 lex.next();
293                 lyxheight = LyXLength(lex.getString());
294         } else if (token == "lyxscale") {
295                 lex.next();
296                 lyxscale = lex.getInteger();
297         } else if (token == "special") {
298                 lex.eatLine();
299                 special = lex.getString();
300         } else {        // If it's none of the above, its not ours.
301                 return false;
302         }
303         return true;
304 }