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