]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
prepare for 1.1.6pre2
[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 using std::endl;
26
27 /// This variable keeps a tab on whether the translator was set with the
28 /// translations.
29 static bool translatorsSet = false;
30
31 /// This is the translator between the Resize enum and corresponding lyx
32 /// file strings.
33 static Translator < InsetGraphicsParams::Resize, string >
34 resizeTranslator(InsetGraphicsParams::DEFAULT_SIZE, "default");
35
36 /// This is the translator between the Origin enum and corresponding lyx
37 /// file strings.
38 static Translator < InsetGraphicsParams::Origin, string >
39 originTranslator(InsetGraphicsParams::DEFAULT, "default");
40
41 /// This is the translator between the Display enum and corresponding lyx
42 /// file strings.
43 static Translator < InsetGraphicsParams::DisplayType, string >
44 displayTranslator(InsetGraphicsParams::MONOCHROME, "monochrome");
45
46
47
48 InsetGraphicsParams::InsetGraphicsParams()
49 {
50         init();
51
52         // Set translators
53         if (! translatorsSet) {
54                 translatorsSet = true;
55
56                 // Fill the resize translator
57                 resizeTranslator.addPair(DEFAULT_SIZE, "default");
58                 resizeTranslator.addPair(CM, "cm");
59                 resizeTranslator.addPair(INCH, "inch");
60                 resizeTranslator.addPair(PERCENT_PAGE, "percentOfPage");
61                 resizeTranslator.addPair(PERCENT_COLUMN, "percentOfColumn");
62
63                 // Fill the origin translator
64                 originTranslator.addPair(DEFAULT, "default");
65                 originTranslator.addPair(LEFTTOP, "leftTop");
66                 originTranslator.addPair(LEFTCENTER, "leftCenter");
67                 originTranslator.addPair(LEFTBASELINE, "leftBaseLine");
68                 originTranslator.addPair(LEFTBOTTOM, "leftBottom");
69                 originTranslator.addPair(CENTERTOP, "centerTop");
70                 originTranslator.addPair(CENTER, "center");
71                 originTranslator.addPair(CENTERBASELINE, "centerBaseLine");
72                 originTranslator.addPair(CENTERBOTTOM, "centerBottom");
73                 originTranslator.addPair(RIGHTTOP, "rightTop");
74                 originTranslator.addPair(RIGHTCENTER, "rightCenter");
75                 originTranslator.addPair(RIGHTBASELINE, "rightBaseLine");
76                 originTranslator.addPair(RIGHTBOTTOM, "rightBottom");
77                 originTranslator.addPair(REFERENCE_POINT, "referencePoint");
78
79                 // Fill the display translator
80                 displayTranslator.addPair(MONOCHROME, "monochrome");
81                 displayTranslator.addPair(GRAYSCALE, "grayscale");
82                 displayTranslator.addPair(COLOR, "color");
83                 displayTranslator.addPair(NONE, "none");
84         }
85
86 }
87
88
89 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
90 {
91         // I decided to skip the initialization since the copy will overwrite
92         // everything anyway.
93         //    init();
94         copy(igp);
95 }
96
97 InsetGraphicsParams &
98 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
99 {
100         // Are we assigning the object into itself?
101         if (this == &params)
102                 return * this;
103
104         copy(params);
105         return *this;
106 }
107
108 void InsetGraphicsParams::init()
109 {
110         subcaptionText = filename = string();
111         display = MONOCHROME;
112         inlineFigure = false;
113         subcaption = false;
114         keepAspectRatio = true;
115         widthResize = DEFAULT_SIZE;
116         widthSize = 0.0;
117         heightResize = DEFAULT_SIZE;
118         heightSize = 0.0;
119         rotateOrigin = DEFAULT;
120         rotateAngle = 0;
121
122         testInvariant();
123 }
124
125 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
126 {
127         filename = igp.filename;
128         display = igp.display;
129         inlineFigure = igp.inlineFigure;
130         subcaption = igp.subcaption;
131         subcaptionText = igp.subcaptionText;
132         keepAspectRatio = igp.keepAspectRatio;
133         widthResize = igp.widthResize;
134         widthSize = igp.widthSize;
135         heightResize = igp.heightResize;
136         heightSize = igp.heightSize;
137         rotateOrigin = igp.rotateOrigin;
138         rotateAngle = igp.rotateAngle;
139
140         testInvariant();
141 }
142
143 void InsetGraphicsParams::testInvariant() const
144 {
145         // Filename might be empty (when the dialog is first created).
146         // Assert(!filename.empty());
147
148         Assert(display == COLOR ||
149                display == MONOCHROME ||
150                display == GRAYSCALE ||
151                display == NONE
152               );
153
154         Assert(widthResize == DEFAULT_SIZE ||
155                widthResize == CM ||
156                widthResize == INCH ||
157                widthResize == PERCENT_PAGE ||
158                widthResize == PERCENT_COLUMN
159               );
160
161         Assert(heightResize == DEFAULT_SIZE ||
162                heightResize == CM ||
163                heightResize == INCH ||
164                heightResize == PERCENT_PAGE
165               );
166
167         Assert(widthSize >= 0.0);
168         Assert(heightSize >= 0.0);
169
170         // Angle is in degrees and ranges -360 < angle < 360
171         // The reason for this is that in latex there is a meaning for the
172         // different angles and they are not necessarliy interchangeable,
173         // it depends on the rotation origin.
174         Assert(rotateAngle < 360);
175         Assert(rotateAngle > -360);
176
177 }
178
179 bool operator==(InsetGraphicsParams const & left,
180                 InsetGraphicsParams const & right)
181 {
182         if (left.filename == right.filename &&
183                 left.display == right.display &&
184                 left.inlineFigure == right.inlineFigure &&
185                 left.subcaption == right.subcaption &&
186                 left.subcaptionText == right.subcaptionText &&
187                 left.keepAspectRatio == right.keepAspectRatio &&
188                 left.widthResize == right.widthResize &&
189                 left.widthSize == right.widthSize &&
190                 left.heightResize == right.heightResize &&
191                 left.heightSize == right.heightSize &&
192                 left.rotateOrigin == right.rotateOrigin &&
193                 left.rotateAngle == right.rotateAngle
194            )
195                 return true;
196
197         return false;
198 }
199
200
201 static
202 void writeResize(ostream & os, string const & key,
203                         InsetGraphicsParams::Resize resize, double size)
204 {
205         os << ' ' << key << "Resize ";
206
207         os << resizeTranslator.find(resize);
208 #if 0
209         // Old code, before using translators
210         switch (resize) {
211         case InsetGraphicsParams::DEFAULT_SIZE:
212                 os << "default";
213                 break;
214
215         case InsetGraphicsParams::CM:
216                 os << "cm";
217                 break;
218
219         case InsetGraphicsParams::INCH:
220                 os << "inch";
221                 break;
222
223         case InsetGraphicsParams::PERCENT_PAGE:
224                 os << "percentOfPage";
225                 break;
226
227         case InsetGraphicsParams::PERCENT_COLUMN:
228                 os << "percentOfColumnt";
229                 break;
230         }
231 #endif 
232         os << ' ' << key << ' ' << size << endl;
233 }
234
235 static void writeOrigin(ostream & os,
236                         InsetGraphicsParams::Origin origin)
237 {
238         os << " rotateOrigin " << originTranslator.find(origin);
239
240 #if 0
241         // Old method.
242         switch (origin) {
243         case InsetGraphicsParams:: DEFAULT:
244                 os << "default";
245                 break;
246         case InsetGraphicsParams::      LEFTTOP:
247                 os << "LeftTop";
248                 break;
249         case InsetGraphicsParams::      LEFTCENTER:
250                 os << "LeftCenter";
251                 break;
252         case InsetGraphicsParams::      LEFTBASELINE:
253                 os << "LeftBaseLine";
254                 break;
255         case InsetGraphicsParams::      LEFTBOTTOM:
256                 os << "LeftBottom";
257                 break;
258         case InsetGraphicsParams::      CENTERTOP:
259                 os << "CenterTop";
260                 break;
261         case InsetGraphicsParams::      CENTER:
262                 os << "Center";
263                 break;
264         case InsetGraphicsParams::      CENTERBASELINE:
265                 os << "CenterBaseLine";
266                 break;
267         case InsetGraphicsParams::      CENTERBOTTOM:
268                 os << "CenterBottom";
269                 break;
270         case InsetGraphicsParams::      RIGHTTOP:
271                 os << "RightTop";
272                 break;
273         case InsetGraphicsParams::      RIGHTCENTER:
274                 os << "RightCenter";
275                 break;
276         case InsetGraphicsParams::      RIGHTBASELINE:
277                 os << "RightBaseLine";
278                 break;
279         case InsetGraphicsParams::      RIGHTBOTTOM:
280                 os << "RightBottom";
281                 break;
282                 // Current REFERENCE_POINT is aliased to LEFTBASELINE
283                 //    case InsetGraphicsParams::        REFERENCE_POINT:
284         }
285
286 #endif 
287
288         os << endl;
289 }
290
291 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
292 {
293         // If there is no filename, write nothing for it.
294         if (! filename.empty()) {
295                 os << "filename "
296                 << MakeRelPath(filename, OnlyPath(buf->fileName()))
297                 << endl;
298         }
299
300         // Save the display type
301         os << " display " << displayTranslator.find(display) << endl;
302 #if 0
303         switch (display) {
304         case COLOR:
305                 os << "color";
306                 break;
307
308         case GRAYSCALE:
309                 os << "grayscale";
310                 break;
311
312         case MONOCHROME:
313                 os << "monochrome";
314                 break;
315
316         case NONE:
317                 os << "none";
318                 break;
319         }
320         os << endl;
321 #endif 
322
323         // Save the inline status
324         if (inlineFigure)
325                 os << " inline";
326
327         // Save the subcaption status
328         if (subcaption)
329                 os << " subcaption";
330
331         if (! subcaptionText.empty())
332                 os << " subcaptionText \"" << subcaptionText << '\"' << endl;
333
334         writeResize(os, "width", widthResize, widthSize);
335         writeResize(os, "height", heightResize, heightSize);
336
337         writeOrigin(os, rotateOrigin);
338         if (rotateAngle != 0)
339                 os << " rotateAngle " << rotateAngle << endl;
340 }
341
342
343 static
344 void readResize(InsetGraphicsParams * igp, bool height,
345                        string const & token)
346 {
347         InsetGraphicsParams::Resize resize = InsetGraphicsParams::DEFAULT_SIZE;
348
349         resize = resizeTranslator.find(token);
350 #if 0
351         // Old code, before translator.
352         if (token == "default")
353                 resize = InsetGraphicsParams::DEFAULT_SIZE;
354         else if (token == "cm")
355                 resize = InsetGraphicsParams::CM;
356         else if (token == "inch")
357                 resize = InsetGraphicsParams::INCH;
358         else if (token == "percentOfPage")
359                 resize = InsetGraphicsParams::PERCENT_PAGE;
360         else if (token == "percentOfColumn")
361                 resize = InsetGraphicsParams::PERCENT_COLUMN;
362         else {
363                 lyxerr << "BUG: When reading resize value of InsetGraphicsParam"
364                 " unknown token found '" << token << '\'' << endl;
365         }
366 #endif 
367
368         if (height)
369                 igp->heightResize = resize;
370         else
371                 igp->widthResize = resize;
372 }
373
374
375 static
376 void readOrigin(InsetGraphicsParams * igp, string const & token)
377 { // TODO: complete this function.
378         igp->rotateOrigin = originTranslator.find(token);
379 }
380
381 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
382                                string const& token)
383 {
384         if (token == "filename") {
385                 lex.next();
386                 filename = lex.GetString();
387
388                 if (!filename.empty()) {
389                         // Make the filename with absolute directory.
390                         filename = MakeAbsPath(filename, OnlyPath(buf->fileName()));
391                 }
392         } else if (token == "display") {
393                 lex.next();
394                 string const type = lex.GetString();
395
396                 display = displayTranslator.find(type);
397 #if 0
398                 if (type == "color")
399                         display = COLOR;
400                 else if (type == "grayscale")
401                         display = GRAYSCALE;
402                 else if (type == "monochrome")
403                         display = MONOCHROME;
404                 else if (type == "none")
405                         display = NONE;
406                 else {
407                         display = MONOCHROME;
408                         lyxerr << "BUG: When reading InsetGraphicsParams"
409                         " display has an unknown type " << type << endl;
410                 }
411 #endif 
412         } else if (token == "inline") {
413                 inlineFigure = true;
414         } else if (token == "subcaption") {
415                 subcaption = true;
416         } else if (token == "subcaptionText") {
417                 lex.next();
418                 subcaptionText = lex.GetString();
419         } else if (token == "widthResize") {
420                 lex.next();
421                 string const token = lex.GetString();
422
423                 readResize(this, false, token);
424         } else if (token == "width") {
425                 lex.next();
426                 widthSize = lex.GetFloat();
427         } else if (token == "heightResize") {
428                 lex.next();
429                 string const token = lex.GetString();
430
431                 readResize(this, true, token);
432         } else if (token == "height") {
433                 lex.next();
434                 heightSize = lex.GetFloat();
435         } else if (token == "rotateOrigin") {
436                 lex.next();
437                 string const token = lex.GetString();
438
439                 readOrigin(this, token);
440         } else if (token == "rotateAngle") {
441                 lex.next();
442                 rotateAngle = lex.GetInteger();
443         } else {
444                 // If it's none of the above, its not ours.
445                 return false;
446         }
447
448         return true;
449 }