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