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