]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
Angus insetindex patch + protect patch from Dekel
[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 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 #ifdef ENABLE_ASSERTION
124     testInvariant();
125 #endif
126 }
127
128 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
129 {
130     filename = igp.filename;
131     display = igp.display;
132     inlineFigure = igp.inlineFigure;
133     subcaption = igp.subcaption;
134     subcaptionText = igp.subcaptionText;
135     keepAspectRatio = igp.keepAspectRatio;
136     widthResize = igp.widthResize;
137     widthSize = igp.widthSize;
138     heightResize = igp.heightResize;
139     heightSize = igp.heightSize;
140     rotateOrigin = igp.rotateOrigin;
141     rotateAngle = igp.rotateAngle;
142
143 #ifdef ENABLE_ASSERTIONS
144     testInvariant();
145 #endif    
146 }
147
148 void InsetGraphicsParams::testInvariant() const
149 {
150 #ifdef ENABLE_ASSERTIONS
151     // Filename might be empty (when the dialog is first created).
152     // Assert(!filename.empty());
153     
154     Assert(display == COLOR ||
155             display == MONOCHROME ||
156             display == GRAYSCALE ||
157             display == NONE
158             );
159     
160     Assert(widthResize == DEFAULT_SIZE ||
161             widthResize == CM ||
162             widthResize == INCH ||
163             widthResize == PERCENT_PAGE ||
164             widthResize == PERCENT_COLUMN
165             );
166
167     Assert(heightResize == DEFAULT_SIZE ||
168             heightResize == CM ||
169             heightResize == INCH ||
170             heightResize == PERCENT_PAGE
171             );
172     
173     Assert(widthSize  >= 0.0);
174     Assert(heightSize >= 0.0);
175     
176     // Angle is in degrees and ranges -360 < angle < 360
177     // The reason for this is that in latex there is a meaning for the
178     // different angles and they are not necessarliy interchangeable,
179     // it depends on the rotation origin.
180     Assert(rotateAngle <  360);
181     Assert(rotateAngle > -360);
182
183 #endif
184 }
185
186 bool operator==(InsetGraphicsParams const & left, 
187         InsetGraphicsParams const & right)
188 {
189     if (left.filename == right.filename   &&
190             left.display == right.display &&
191             left.inlineFigure == right.inlineFigure &&
192             left.subcaption == right.subcaption &&
193             left.subcaptionText == right.subcaptionText &&
194             left.keepAspectRatio == right.keepAspectRatio &&
195             left.widthResize == right.widthResize &&
196             left.widthSize == right.widthSize &&
197             left.heightResize == right.heightResize &&
198             left.heightSize == right.heightSize &&
199             left.rotateOrigin == right.rotateOrigin &&
200             left.rotateAngle == right.rotateAngle
201             )
202         return true;
203
204     return false;
205 }
206
207 static void writeResize(ostream & os, char const * key, 
208         InsetGraphicsParams::Resize resize, double size)
209 {
210     os << ' ' << key << "Resize ";
211
212     os << resizeTranslator.find(resize);
213 #if 0
214     // Old code, before using translators
215     switch (resize) {
216     case InsetGraphicsParams::DEFAULT_SIZE:
217         os << "default";
218         break;
219         
220     case InsetGraphicsParams::CM:
221         os << "cm";
222         break;
223         
224     case InsetGraphicsParams::INCH:
225         os << "inch";
226         break;
227         
228     case InsetGraphicsParams::PERCENT_PAGE:
229         os << "percentOfPage";
230         break;
231         
232     case InsetGraphicsParams::PERCENT_COLUMN:
233         os << "percentOfColumnt";
234         break;
235     }
236 #endif
237     os << ' ' << key << ' ' << size << endl;
238 }
239
240 static void writeOrigin(ostream & os, 
241         InsetGraphicsParams::Origin origin)
242 {
243     os << " rotateOrigin " << originTranslator.find(origin);
244     
245 #if 0
246     // Old method.
247     switch (origin) {
248     case InsetGraphicsParams::  DEFAULT:
249         os << "default";
250         break;
251     case InsetGraphicsParams::  LEFTTOP:
252         os << "LeftTop";
253         break;
254     case InsetGraphicsParams::  LEFTCENTER:
255         os << "LeftCenter";
256         break;
257     case InsetGraphicsParams::  LEFTBASELINE:
258         os << "LeftBaseLine";
259         break;
260     case InsetGraphicsParams::  LEFTBOTTOM:
261         os << "LeftBottom";
262         break;
263     case InsetGraphicsParams::  CENTERTOP:
264         os << "CenterTop";
265         break;
266     case InsetGraphicsParams::  CENTER:
267         os << "Center";
268         break;
269     case InsetGraphicsParams::  CENTERBASELINE:
270         os << "CenterBaseLine";
271         break;
272     case InsetGraphicsParams::  CENTERBOTTOM:
273         os << "CenterBottom";
274         break;
275     case InsetGraphicsParams::  RIGHTTOP:
276         os << "RightTop";
277         break;
278     case InsetGraphicsParams::  RIGHTCENTER:
279         os << "RightCenter";
280         break;
281     case InsetGraphicsParams::  RIGHTBASELINE:
282         os << "RightBaseLine";
283         break;
284     case InsetGraphicsParams::  RIGHTBOTTOM:
285         os << "RightBottom";
286         break;
287         // Current REFERENCE_POINT is aliased to LEFTBASELINE
288 //    case InsetGraphicsParams::        REFERENCE_POINT:
289     }
290 #endif
291     
292     os << endl;
293 }
294
295 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
296 {
297     // If there is no filename, write nothing for it.
298     if (! filename.empty()) {
299         os << "filename " 
300            << MakeRelPath(filename, OnlyPath(buf->fileName())) 
301            << endl;
302     }
303
304     // Save the display type
305     os << " display " << displayTranslator.find(display) << endl;
306 #if 0
307     switch (display) {
308     case COLOR:
309         os << "color";
310         break;
311
312     case GRAYSCALE:
313         os << "grayscale";
314         break;
315
316     case MONOCHROME:
317         os << "monochrome";
318         break;
319
320     case NONE:
321         os << "none";
322         break;
323     }
324     os << endl;
325 #endif
326     
327     // Save the inline status
328     if (inlineFigure)
329         os << " inline";
330
331     // Save the subcaption status
332     if (subcaption)
333         os << " subcaption";
334
335     if (! subcaptionText.empty())
336         os << " subcaptionText \"" << subcaptionText << '\"' << endl;
337
338     writeResize(os, "width", widthResize, widthSize);
339     writeResize(os, "height", heightResize, heightSize);
340
341     writeOrigin(os, rotateOrigin);
342     if (rotateAngle != 0)
343         os << " rotateAngle " << rotateAngle << endl;
344 }
345    
346 static void readResize(InsetGraphicsParams * igp, bool height, 
347                        string const & token)
348 {
349     InsetGraphicsParams::Resize resize = InsetGraphicsParams::DEFAULT_SIZE;
350
351     resize = resizeTranslator.find(token);
352 #if 0
353     // Old code, before translator.
354     if (token == "default")
355         resize = InsetGraphicsParams::DEFAULT_SIZE;
356     else if (token == "cm")
357         resize = InsetGraphicsParams::CM;
358     else if (token == "inch")
359         resize = InsetGraphicsParams::INCH;
360     else if (token == "percentOfPage")
361         resize = InsetGraphicsParams::PERCENT_PAGE;
362     else if (token == "percentOfColumn")
363         resize = InsetGraphicsParams::PERCENT_COLUMN;
364     else {
365         lyxerr << "BUG: When reading resize value of InsetGraphicsParam"
366             " unknown token found '" << token << '\'' << endl;
367     }
368 #endif 
369     
370     if (height)
371         igp->heightResize = resize;
372     else 
373         igp->widthResize = resize;
374 }
375     
376 static 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 }