]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
Herbert's latest "graphic5" patch (with the zippedFile stuff commented out),
[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         size_type = DEFAULT_SIZE;       // do nothing
108         keepAspectRatio = false;        //
109         rotateOrigin = "center";        // 
110         rotateAngle = 0.0;              // in degrees
111         special = string();             // userdefined stuff
112 }
113
114 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
115 {
116         filename = igp.filename;
117         bb = igp.bb;
118         draft = igp.draft;
119         clip = igp.clip;
120         display = igp.display;
121         subcaption = igp.subcaption;
122         subcaptionText = igp.subcaptionText;
123         keepAspectRatio = igp.keepAspectRatio;
124         width = igp.width;
125         height = igp.height;
126         scale = igp.scale;
127         size_type = igp.size_type;
128         lyxwidth = igp.lyxwidth;
129         lyxheight = igp.lyxheight;
130         rotateOrigin = igp.rotateOrigin;
131         rotateAngle = igp.rotateAngle;
132         special = igp.special;
133 }
134
135 bool operator==(InsetGraphicsParams const & left,
136                 InsetGraphicsParams const & right)
137 {
138         if (left.filename == right.filename &&
139                 left.bb == right.bb &&
140                 left.draft == right.draft &&
141                 left.clip == right.clip &&
142                 left.display == right.display &&
143                 left.subcaption == right.subcaption &&
144                 left.subcaptionText == right.subcaptionText &&
145                 left.keepAspectRatio == right.keepAspectRatio &&
146                 left.width == right.width &&
147                 left.height == right.height &&
148                 left.scale == right.scale &&
149                 left.size_type == right.size_type &&
150                 left.lyxwidth == right.lyxwidth &&
151                 left.lyxheight == right.lyxheight &&
152                 left.rotateOrigin == right.rotateOrigin &&
153                 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
154                 left.special == right.special) 
155            )
156                 return true;
157
158         return false;
159 }
160
161 bool operator!=(InsetGraphicsParams const & left,
162                 InsetGraphicsParams const & right)
163 {
164         return  !(left == right);
165 }
166
167
168 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
169 {
170         // If there is no filename, write nothing for it.
171         if (! filename.empty()) {
172                 os << "\tfilename "
173                 << MakeRelPath(filename, buf->filePath())
174                 << '\n';
175         }
176         if (!bb.empty())                // bounding box
177                 os << "\tBoundingBox " << bb << '\n';
178         if (clip)                       // clip image
179                 os << "\tclip\n";
180         if (draft)                      // draft mode
181                 os << "\tdraft\n";
182         // Save the display type
183         os << "\tdisplay " << displayTranslator.find(display) << '\n';
184         // Save the subcaption status
185         if (subcaption)
186             os << "\tsubcaption\n";
187         if (!subcaptionText.empty())
188             os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
189     // we always need the size type
190     // 0: no special
191     // 1: width/height combination
192     // 2: scale
193         os << "\tsize_type " <<  size_type << '\n';
194         if (!width.zero())
195             os << "\twidth " << width.asString() << '\n';
196         if (!height.zero())
197             os << "\theight " << height.asString() << '\n';
198         if (scale != 0)
199             os << "\tscale " << scale << '\n';
200         if (keepAspectRatio)
201                 os << "\tkeepAspectRatio\n";
202         if (!lyx::float_equal(rotateAngle, 0.0, 0.001))
203                 os << "\trotateAngle " << rotateAngle << '\n';
204         if (!rotateOrigin.empty())
205                 os << "\trotateOrigin " << rotateOrigin << '\n';
206         if (!special.empty())
207                 os << "\tspecial " << special << '\n';
208         if (!lyxwidth.zero())           // the lyx-viewsize
209             os << "\tlyxwidth " << lyxwidth.asString() << '\n';
210         if (!lyxheight.zero())
211             os << "\tlyxheight " << lyxheight.asString();
212 }
213
214
215 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
216                                string const& token)
217 {
218         if (token == "filename") {
219                 lex.next();
220                 filename = lex.getString();
221                 if (!filename.empty()) {
222                         // Make the filename with absolute directory.
223                         filename = MakeAbsPath(filename, buf->filePath());
224                 }
225         } else if (token == "BoundingBox") {
226                 for (int i=0; i<4 ;i++) {
227                     lex.next();
228                     bb += (lex.getString()+" ");
229                 }
230         } else if (token == "clip") {
231                 clip = true;
232         } else if (token == "draft") {
233                 draft = true;
234         } else if (token == "display") {
235                 lex.next();
236                 string const type = lex.getString();
237                 display = displayTranslator.find(type);
238         } else if (token == "subcaption") {
239                 subcaption = true;
240         } else if (token == "subcaptionText") {
241                 lex.next();
242                 subcaptionText = lex.getString();
243         } else if (token == "widthResize") {
244                 if (lex.next()) {
245                     string const token = lex.getString();
246                     if (token == "scale") {
247                         lex.next();
248                         scale = lex.getInteger();
249                         size_type = SCALE;
250                     }
251                     else {
252                         width = convertResizeValue(token, lex);
253                         size_type = WH;
254                     }
255                 }
256         } else if (token == "size_type") {
257                 lex.next();
258                 switch (lex.getInteger()) {
259                     case 0 : size_type = DEFAULT_SIZE;
260                         break;
261                     case 1 : size_type = WH;
262                         break;
263                     case 2 : size_type = SCALE;
264                 }
265         } else if (token == "width") {
266                 lex.next();
267                 width = LyXLength(lex.getString());
268                 size_type = WH;
269         } else if (token == "heightResize") {
270                 if (lex.next())
271                         height = convertResizeValue(lex.getString(), lex);
272         } else if (token == "height") {
273                 lex.next();
274                 height = LyXLength(lex.getString());
275                 size_type = WH;
276         } else if (token == "keepAspectRatio") {
277                 keepAspectRatio = true;
278         } else if (token == "scale") {
279                 lex.next();
280                 scale = lex.getInteger();
281         } else if (token == "rotateAngle") {
282                 lex.next();
283                 rotateAngle = lex.getFloat();
284         } else if (token == "rotateOrigin") {
285                 lex.next();
286                 rotateOrigin=lex.getString();
287         } else if (token == "lyxwidth") {
288                 lex.next();
289                 lyxwidth = LyXLength(lex.getString());
290         } else if (token == "lyxheight") {
291                 lex.next();
292                 lyxheight = LyXLength(lex.getString());
293         } else {
294                 // If it's none of the above, its not ours.
295                 return false;
296         }
297         return true;
298 }