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