]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
Fixed small drawing bug in InsetText.
[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  *          This file Copyright 2000 Baruch Even
9  * ================================================= */
10
11 #include <config.h> 
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif 
16
17 #include "insetgraphicsParams.h"
18
19 #include "support/translator.h"
20 #include "support/filetools.h"
21 #include "support/lyxlib.h"
22 #include "support/LOstream.h"
23
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 Resize enum and corresponding lyx
33 /// file strings.
34 Translator< InsetGraphicsParams::Resize, string >
35 resizeTranslator(InsetGraphicsParams::DEFAULT_SIZE, "default");
36
37 /// This is the translator between the Origin enum and corresponding lyx
38 /// file strings.
39 Translator< InsetGraphicsParams::Origin, string >
40 originTranslator(InsetGraphicsParams::DEFAULT, "default");
41
42 /// This is the translator between the Display enum and corresponding lyx
43 /// file strings.
44 Translator< InsetGraphicsParams::DisplayType, string >
45 displayTranslator(InsetGraphicsParams::MONOCHROME, "monochrome");
46
47 } // namespace anon
48
49
50 InsetGraphicsParams::InsetGraphicsParams()
51 {
52         init();
53
54         // Set translators
55         if (! translatorsSet) {
56                 translatorsSet = true;
57
58                 // Fill the resize translator
59                 resizeTranslator.addPair(DEFAULT_SIZE, "default");
60                 resizeTranslator.addPair(CM, "cm");
61                 resizeTranslator.addPair(INCH, "inch");
62                 resizeTranslator.addPair(PERCENT_PAGE, "percentOfPage");
63                 resizeTranslator.addPair(PERCENT_COLUMN, "percentOfColumn");
64                 resizeTranslator.addPair(SCALE, "scale");
65
66                 // Fill the origin translator
67                 originTranslator.addPair(DEFAULT, "default");
68                 originTranslator.addPair(LEFTTOP, "leftTop");
69                 originTranslator.addPair(LEFTCENTER, "leftCenter");
70                 originTranslator.addPair(LEFTBASELINE, "leftBaseLine");
71                 originTranslator.addPair(LEFTBOTTOM, "leftBottom");
72                 originTranslator.addPair(CENTERTOP, "centerTop");
73                 originTranslator.addPair(CENTER, "center");
74                 originTranslator.addPair(CENTERBASELINE, "centerBaseLine");
75                 originTranslator.addPair(CENTERBOTTOM, "centerBottom");
76                 originTranslator.addPair(RIGHTTOP, "rightTop");
77                 originTranslator.addPair(RIGHTCENTER, "rightCenter");
78                 originTranslator.addPair(RIGHTBASELINE, "rightBaseLine");
79                 originTranslator.addPair(RIGHTBOTTOM, "rightBottom");
80                 originTranslator.addPair(REFERENCE_POINT, "referencePoint");
81
82                 // Fill the display translator
83                 displayTranslator.addPair(MONOCHROME, "monochrome");
84                 displayTranslator.addPair(GRAYSCALE, "grayscale");
85                 displayTranslator.addPair(COLOR, "color");
86                 displayTranslator.addPair(NONE, "none");
87         }
88
89 }
90
91
92 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
93 {
94         // I decided to skip the initialization since the copy will overwrite
95         // everything anyway.
96         //    init();
97         copy(igp);
98 }
99
100 InsetGraphicsParams &
101 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
102 {
103         // Are we assigning the object into itself?
104         if (this == &params)
105                 return * this;
106
107         copy(params);
108         return *this;
109 }
110
111 void InsetGraphicsParams::init()
112 {
113         subcaptionText = filename = string();
114         display = MONOCHROME;
115         subcaption = false;
116         keepAspectRatio = true;
117         widthResize = DEFAULT_SIZE;
118         widthSize = 0.0;
119         heightResize = DEFAULT_SIZE;
120         heightSize = 0.0;
121         rotateOrigin = DEFAULT;
122         rotateAngle = 0.0;
123
124         testInvariant();
125 }
126
127 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
128 {
129         filename = igp.filename;
130         display = igp.display;
131         subcaption = igp.subcaption;
132         subcaptionText = igp.subcaptionText;
133         keepAspectRatio = igp.keepAspectRatio;
134         widthResize = igp.widthResize;
135         widthSize = igp.widthSize;
136         heightResize = igp.heightResize;
137         heightSize = igp.heightSize;
138         rotateOrigin = igp.rotateOrigin;
139         rotateAngle = igp.rotateAngle;
140
141         testInvariant();
142 }
143
144 void InsetGraphicsParams::testInvariant() const
145 {
146         // Filename might be empty (when the dialog is first created).
147         // Assert(!filename.empty());
148
149         lyx::Assert(display == COLOR ||
150                display == MONOCHROME ||
151                display == GRAYSCALE ||
152                display == NONE
153               );
154
155         lyx::Assert(widthResize == DEFAULT_SIZE ||
156                widthResize == CM ||
157                widthResize == INCH ||
158                widthResize == PERCENT_PAGE ||
159                widthResize == PERCENT_COLUMN ||
160                    widthResize == SCALE
161               );
162
163         lyx::Assert(heightResize == DEFAULT_SIZE ||
164                heightResize == CM ||
165                heightResize == INCH ||
166                heightResize == PERCENT_PAGE ||
167                    heightResize == SCALE
168               );
169
170         // For SCALE these can be negative.
171         //lyx::Assert(widthSize >= 0.0);
172         //lyx::Assert(heightSize >= 0.0);
173
174         // Angle is in degrees and ranges -360 < angle < 360
175         // The reason for this is that in latex there is a meaning for the
176         // different angles and they are not necessarliy interchangeable,
177         // it depends on the rotation origin.
178         lyx::Assert(rotateAngle < 360.0);
179         lyx::Assert(rotateAngle > -360.0);
180
181 }
182
183 bool operator==(InsetGraphicsParams const & left,
184                 InsetGraphicsParams const & right)
185 {
186         if (left.filename == right.filename &&
187                 left.display == right.display &&
188                 left.subcaption == right.subcaption &&
189                 left.subcaptionText == right.subcaptionText &&
190                 left.keepAspectRatio == right.keepAspectRatio &&
191                 left.widthResize == right.widthResize &&
192                 left.widthSize == right.widthSize &&
193                 left.heightResize == right.heightResize &&
194                 left.heightSize == right.heightSize &&
195                 left.rotateOrigin == right.rotateOrigin &&
196                 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001)
197            )
198                 return true;
199
200         return false;
201 }
202
203 bool operator!=(InsetGraphicsParams const & left,
204                 InsetGraphicsParams const & right)
205 {
206         return  !(left == right);
207 }
208
209
210 namespace {
211
212 void writeResize(ostream & os, string const & key,
213                         InsetGraphicsParams::Resize resize, double size)
214 {
215         os << ' ' << key << "Resize ";
216
217         os << resizeTranslator.find(resize);
218         os << ' ' << key << ' ' << size << '\n';
219 }
220
221 void writeOrigin(ostream & os,
222                         InsetGraphicsParams::Origin origin)
223 {
224         os << " rotateOrigin " << originTranslator.find(origin);
225         os << '\n';
226 }
227
228 } // namespace anon
229
230
231 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
232 {
233         // If there is no filename, write nothing for it.
234         if (! filename.empty()) {
235                 os << "filename "
236                 << MakeRelPath(filename, buf->filePath())
237                 << '\n';
238         }
239
240         // Save the display type
241         os << " display " << displayTranslator.find(display) << '\n';
242
243         // Save the subcaption status
244         if (subcaption)
245                 os << " subcaption";
246
247         if (! subcaptionText.empty())
248                 os << " subcaptionText \"" << subcaptionText << '\"' << '\n';
249
250         writeResize(os, "width", widthResize, widthSize);
251         writeResize(os, "height", heightResize, heightSize);
252
253         writeOrigin(os, rotateOrigin);
254         if (!lyx::float_equal(rotateAngle, 0.0, 0.001)) {
255                 os << " rotateAngle " << rotateAngle << '\n';
256         }
257 }
258
259
260 namespace {
261
262 void readResize(InsetGraphicsParams * igp, bool height,
263                        string const & token)
264 {
265         InsetGraphicsParams::Resize resize = InsetGraphicsParams::DEFAULT_SIZE;
266
267         resize = resizeTranslator.find(token);
268
269         if (height)
270                 igp->heightResize = resize;
271         else
272                 igp->widthResize = resize;
273 }
274
275
276 void readOrigin(InsetGraphicsParams * igp, string const & token)
277 {
278         // TODO: complete this function.
279         igp->rotateOrigin = originTranslator.find(token);
280 }
281
282 } // namespace anon
283
284
285 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
286                                string const& token)
287 {
288         if (token == "filename") {
289                 lex.next();
290                 filename = lex.getString();
291
292                 if (!filename.empty()) {
293                         // Make the filename with absolute directory.
294                         filename = MakeAbsPath(filename, buf->filePath());
295                 }
296         } else if (token == "display") {
297                 lex.next();
298                 string const type = lex.getString();
299
300                 display = displayTranslator.find(type);
301         } else if (token == "subcaption") {
302                 subcaption = true;
303         } else if (token == "subcaptionText") {
304                 lex.next();
305                 subcaptionText = lex.getString();
306         } else if (token == "widthResize") {
307                 lex.next();
308                 string const token = lex.getString();
309
310                 readResize(this, false, token);
311         } else if (token == "width") {
312                 lex.next();
313                 widthSize = lex.getFloat();
314         } else if (token == "heightResize") {
315                 lex.next();
316                 string const token = lex.getString();
317
318                 readResize(this, true, token);
319         } else if (token == "height") {
320                 lex.next();
321                 heightSize = lex.getFloat();
322         } else if (token == "rotateOrigin") {
323                 lex.next();
324                 string const token = lex.getString();
325
326                 readOrigin(this, token);
327         } else if (token == "rotateAngle") {
328                 lex.next();
329                 rotateAngle = lex.getFloat();
330         } else {
331                 // If it's none of the above, its not ours.
332                 return false;
333         }
334
335         return true;
336 }