]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
Herbert's big graphics 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
21 #include "support/translator.h"
22 #include "support/filetools.h"
23 #include "support/lyxlib.h"
24 #include "support/LOstream.h"
25
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::MONOCHROME, "monochrome");
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(MONOCHROME, "monochrome");
70                 displayTranslator.addPair(GRAYSCALE, "grayscale");
71                 displayTranslator.addPair(COLOR, "color");
72                 displayTranslator.addPair(NONE, "none");
73         }
74 }
75
76
77 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
78 {
79         // I decided to skip the initialization since the copy will overwrite
80         // everything anyway.
81         //    init();
82         copy(igp);
83 }
84
85 InsetGraphicsParams &
86 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
87 {
88         // Are we assigning the object into itself?
89         if (this == &params)
90                 return * this;
91         copy(params);
92         return *this;
93 }
94
95 void InsetGraphicsParams::init()
96 {
97         subcaptionText = filename = string();
98         bb = string();                  // bounding box
99         draft = false;                  // draft mode
100         clip = false;                   // clip image
101         display = MONOCHROME;           // LyX-View
102         subcaption = false;             // subfigure
103         width = LyXLength();            // set to 0pt
104         height = LyXLength();
105         lyxwidth = LyXLength();         // for the view in lyx
106         lyxheight = LyXLength();
107         scale = 0;
108         size_type = DEFAULT_SIZE;
109         keepAspectRatio = false;
110         rotateOrigin = string();        // 
111         rotateAngle = 0.0;              // in degrees
112         special = string();             // userdefined stuff
113
114         testInvariant();
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         lyxwidth = igp.lyxwidth;
132         lyxheight = igp.lyxheight;
133         rotateOrigin = igp.rotateOrigin;
134         rotateAngle = igp.rotateAngle;
135         special = igp.special;
136
137         testInvariant();
138 }
139
140 void InsetGraphicsParams::testInvariant() const
141 {
142         // Filename might be empty (when the dialog is first created).
143         // Assert(!filename.empty());
144         lyx::Assert(display == COLOR ||
145                display == MONOCHROME ||
146                display == GRAYSCALE ||
147                display == NONE
148               );
149         // Angle is in degrees and ranges -360 < angle < 360
150         // The reason for this is that in latex there is a meaning for the
151         // different angles and they are not necessarliy interchangeable,
152         // it depends on the rotation origin.
153         lyx::Assert(rotateAngle < 360.0);
154         lyx::Assert(rotateAngle > -360.0);
155
156 }
157
158 bool operator==(InsetGraphicsParams const & left,
159                 InsetGraphicsParams const & right)
160 {
161         if (left.filename == right.filename &&
162                 left.bb == right.bb &&
163                 left.draft == right.draft &&
164                 left.clip == right.clip &&
165                 left.display == right.display &&
166                 left.subcaption == right.subcaption &&
167                 left.subcaptionText == right.subcaptionText &&
168                 left.keepAspectRatio == right.keepAspectRatio &&
169                 left.width == right.width &&
170                 left.height == right.height &&
171                 left.scale == right.scale &&
172                 left.size_type == right.size_type &&
173                 left.lyxwidth == right.lyxwidth &&
174                 left.lyxheight == right.lyxheight &&
175                 left.rotateOrigin == right.rotateOrigin &&
176                 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001 &&
177                 left.special == right.special) 
178            )
179                 return true;
180
181         return false;
182 }
183
184 bool operator!=(InsetGraphicsParams const & left,
185                 InsetGraphicsParams const & right)
186 {
187         return  !(left == right);
188 }
189
190
191 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
192 {
193         // If there is no filename, write nothing for it.
194         if (! filename.empty()) {
195                 os << "\tfilename "
196                 << MakeRelPath(filename, buf->filePath())
197                 << '\n';
198         }
199         if (!bb.empty())                // bounding box
200                 os << "\tBoundingBox " << bb << '\n';
201         if (clip)                       // clip image
202                 os << "\tclip\n";
203         if (draft)                      // draft mode
204                 os << "\tdraft\n";
205         // Save the display type
206         os << "\tdisplay " << displayTranslator.find(display) << '\n';
207         // Save the subcaption status
208         if (subcaption)
209             os << "\tsubcaption\n";
210         if (!subcaptionText.empty())
211             os << "\tsubcaptionText \"" << subcaptionText << '\"' << '\n';
212     // we always need the size type
213     // 0: no special
214     // 1: width/height combination
215     // 2: scale
216         os << "\tsize_type " <<  size_type << '\n';
217         if (!width.zero())
218             os << "\twidth " << width.asString() << '\n';
219         if (!height.zero())
220             os << "\theight " << height.asString() << '\n';
221         if (scale != 0)
222             os << "\tscale " << scale << '\n';
223         if (keepAspectRatio)
224                 os << "\tkeepAspectRatio\n";
225         if (!lyx::float_equal(rotateAngle, 0.0, 0.001))
226                 os << "\trotateAngle " << rotateAngle << '\n';
227         if (!rotateOrigin.empty())
228                 os << "\trotateOrigin " << rotateOrigin << '\n';
229         if (!special.empty())
230                 os << "\tspecial " << special << '\n';
231         if (!lyxwidth.zero())           // the lyx-viewsize
232             os << "\tlyxwidth " << lyxwidth.asString() << '\n';
233         if (!lyxheight.zero())
234             os << "\tlyxheight " << lyxheight.asString();
235 }
236
237
238 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
239                                string const& token)
240 {
241         if (token == "filename") {
242                 lex.next();
243                 filename = lex.getString();
244                 if (!filename.empty()) {
245                         // Make the filename with absolute directory.
246                         filename = MakeAbsPath(filename, buf->filePath());
247                 }
248         } else if (token == "BoundingBox") {
249                 for (int i=0; i<4 ;i++) {
250                     lex.next();
251                     bb += (lex.getString()+" ");
252                 }
253         } else if (token == "clip") {
254                 clip = true;
255         } else if (token == "draft") {
256                 draft = true;
257         } else if (token == "display") {
258                 lex.next();
259                 string const type = lex.getString();
260                 display = displayTranslator.find(type);
261         } else if (token == "subcaption") {
262                 subcaption = true;
263         } else if (token == "subcaptionText") {
264                 lex.next();
265                 subcaptionText = lex.getString();
266         } else if (token == "widthResize") {
267                 if (lex.next()) {
268                     string const token = lex.getString();
269                     if (token == "scale") {
270                         lex.next();
271                         scale = lex.getInteger();
272                         size_type = SCALE;
273                     }
274                     else {
275                         width = convertResizeValue(token, lex);
276                         size_type = WH;
277                     }
278                 }
279         } else if (token == "size_type") {
280                 lex.next();
281                 switch (lex.getInteger()) {
282                     case 0 : size_type = DEFAULT_SIZE;
283                         break;
284                     case 1 : size_type = WH;
285                         break;
286                     case 2 : size_type = SCALE;
287                 }
288         } else if (token == "width") {
289                 lex.next();
290                 width = LyXLength(lex.getString());
291                 size_type = WH;
292         } else if (token == "heightResize") {
293                 if (lex.next())
294                         height = convertResizeValue(lex.getString(), lex);
295         } else if (token == "height") {
296                 lex.next();
297                 height = LyXLength(lex.getString());
298                 size_type = WH;
299         } else if (token == "keepAspectRatio") {
300                 keepAspectRatio = true;
301         } else if (token == "rotateAngle") {
302                 lex.next();
303                 rotateAngle = lex.getFloat();
304         } else if (token == "rotateOrigin") {
305                 lex.next();
306                 rotateOrigin=lex.getString();
307         } else if (token == "lyxwidth") {
308                 lex.next();
309                 lyxwidth = LyXLength(lex.getString());
310         } else if (token == "lyxheight") {
311                 lex.next();
312                 lyxheight = LyXLength(lex.getString());
313         } else {
314                 // If it's none of the above, its not ours.
315                 return false;
316         }
317         return true;
318 }