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