]> git.lyx.org Git - lyx.git/blob - src/insets/InsetGraphicsParams.cpp
Fix bad cursor positioning when entering an inset
[lyx.git] / src / insets / InsetGraphicsParams.cpp
1 /**
2  * \file InsetGraphicsParams.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Baruch Even
7  * \author Herbert Voß
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetGraphicsParams.h"
15
16 #include "Buffer.h"
17 #include "LyX.h" // for use_gui
18 #include "Lexer.h"
19 #include "LyXRC.h"
20
21 #include "graphics/epstools.h"
22 #include "graphics/GraphicsTypes.h"
23
24 #include "support/convert.h"
25 #include "support/debug.h"
26 #include "support/lyxlib.h"
27 #include "support/lstrings.h"
28 #include "support/Translator.h"
29
30 #include <ostream>
31
32 using namespace std;
33 using namespace lyx::support;
34
35 namespace lyx {
36
37
38 InsetGraphicsParams::InsetGraphicsParams()
39 {
40         init();
41 }
42
43
44 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
45 {
46         // I decided to skip the initialization since the copy will overwrite
47         // everything anyway.
48         //    init();
49         copy(igp);
50 }
51
52
53 InsetGraphicsParams & InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
54 {
55         // Are we assigning the object into itself?
56         if (this == &params)
57                 return *this;
58         copy(params);
59         return *this;
60 }
61
62
63 void InsetGraphicsParams::init()
64 {
65         filename.erase();
66         lyxscale = 100;                 // lyx scaling in percentage
67         display = true;         // may be overriden by display mode in preferences
68         scale = string("100");                  // output scaling in percentage
69         width = Length();
70         height = Length();
71         keepAspectRatio = false;        // for LaTeX output
72         draft = false;                  // draft mode
73         scaleBeforeRotation = false;    // scale image before rotating
74
75         bbox = graphics::BoundingBox(); // bounding box
76         clip = false;                   // clip image
77
78         rotateAngle = "0";              // angle of rotation in degrees
79         rotateOrigin.erase();           // Origin of rotation
80         special.erase();                // additional userdefined stuff
81         groupId.clear();
82 }
83
84
85 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
86 {
87         filename = igp.filename;
88         lyxscale = igp.lyxscale;
89         display = igp.display;
90         scale = igp.scale;
91         width = igp.width;
92         height = igp.height;
93         keepAspectRatio = igp.keepAspectRatio;
94         draft = igp.draft;
95         scaleBeforeRotation = igp.scaleBeforeRotation;
96
97         bbox = igp.bbox;
98         clip = igp.clip;
99
100         rotateAngle = igp.rotateAngle;
101         rotateOrigin = igp.rotateOrigin;
102         special = igp.special;
103         groupId = igp.groupId;
104 }
105
106
107 bool operator==(InsetGraphicsParams const & left,
108                 InsetGraphicsParams const & right)
109 {
110         return left.filename == right.filename &&
111             left.lyxscale == right.lyxscale &&
112             left.display == right.display &&
113             left.scale == right.scale &&
114             left.width == right.width &&
115             left.height == right.height &&
116             left.keepAspectRatio == right.keepAspectRatio &&
117             left.draft == right.draft &&
118             left.scaleBeforeRotation == right.scaleBeforeRotation &&
119
120             left.bbox == right.bbox &&
121             left.clip == right.clip &&
122
123             left.rotateAngle == right.rotateAngle &&
124             left.rotateOrigin == right.rotateOrigin &&
125             left.special == right.special &&
126             left.groupId == right.groupId;
127 }
128
129
130 bool operator!=(InsetGraphicsParams const & left,
131                 InsetGraphicsParams const & right)
132 {
133         return  !(left == right);
134 }
135
136
137 void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const
138 {
139         // Do not write the default values
140         if (!filename.empty())
141                 os << "\tfilename " << filename.outputFileName(buffer.filePath()) << '\n';
142         if (lyxscale != 100)
143                 os << "\tlyxscale " << lyxscale << '\n';
144         if (!display)
145                 os << "\tdisplay false\n";
146         if (!scale.empty() && !float_equal(convert<double>(scale), 0.0, 0.05)) {
147                 if (!float_equal(convert<double>(scale), 100.0, 0.05))
148                         os << "\tscale " << scale << '\n';
149         } else {
150                 if (!width.zero())
151                         os << "\twidth " << width.asString() << '\n';
152                 if (!height.zero())
153                         os << "\theight " << height.asString() << '\n';
154         }
155
156         if (keepAspectRatio)
157                 os << "\tkeepAspectRatio\n";
158         if (draft)                      // draft mode
159                 os << "\tdraft\n";
160         if (scaleBeforeRotation)
161                 os << "\tscaleBeforeRotation\n";
162
163         if (!bbox.empty())              // bounding box
164                 os << "\tBoundingBox " << bbox << '\n';
165         if (clip)                       // clip image
166                 os << "\tclip\n";
167
168         if (!rotateAngle.empty()
169                 && !float_equal(convert<double>(rotateAngle), 0.0, 0.001))
170                 os << "\trotateAngle " << rotateAngle << '\n';
171         if (!rotateOrigin.empty())
172                 os << "\trotateOrigin " << rotateOrigin << '\n';
173         if (!special.empty())
174                 os << "\tspecial " << special << '\n';
175         if (!groupId.empty())
176                 os << "\tgroupId "<< groupId << '\n';
177 }
178
179
180 bool InsetGraphicsParams::Read(Lexer & lex, string const & token,
181                                Buffer const & buf, bool allowOrigin)
182 {
183         if (token == "filename") {
184                 lex.eatLine();
185                 if (allowOrigin)
186                         filename = buf.getReferencedFileName(lex.getString());
187                 else
188                         filename.set(lex.getString(), buf.filePath());
189         } else if (token == "lyxscale") {
190                 lex.next();
191                 lyxscale = lex.getInteger();
192         } else if (token == "display") {
193                 lex.next();
194                 string const type = lex.getString();
195                 display = lex.getString() != "false";
196         } else if (token == "scale") {
197                 lex.next();
198                 scale = lex.getString();
199         } else if (token == "width") {
200                 lex.next();
201                 width = Length(lex.getString());
202                 scale = string();
203         } else if (token == "height") {
204                 lex.next();
205                 height = Length(lex.getString());
206                 scale = string();
207         } else if (token == "keepAspectRatio") {
208                 keepAspectRatio = true;
209         } else if (token == "draft") {
210                 draft = true;
211         } else if (token == "scaleBeforeRotation") {
212                 scaleBeforeRotation = true;
213         } else if (token == "BoundingBox") {
214                 lex.next();
215                 bbox.xl = Length(lex.getString());
216                 lex.next();
217                 bbox.yb = Length(lex.getString());
218                 lex.next();
219                 bbox.xr = Length(lex.getString());
220                 lex.next();
221                 bbox.yt = Length(lex.getString());
222         } else if (token == "clip") {
223                 clip = true;
224         } else if (token == "rotateAngle") {
225                 lex.next();
226                 rotateAngle = lex.getString();
227         } else if (token == "rotateOrigin") {
228                 lex.next();
229                 rotateOrigin=lex.getString();
230         } else if (token == "special") {
231                 lex.eatLine();
232                 special = lex.getString();
233         } else if (token == "groupId") {
234                 lex.eatLine();
235                 groupId = lex.getString();
236
237         // catch and ignore following two old-format tokens and their arguments.
238         // e.g. "size_kind scale" clashes with the setting of the
239         // "scale <value>" keyword.
240         } else if (token == "size_kind" || token == "lyxsize_kind") {
241                 lex.next();
242                 lex.getString();
243
244         } else {
245                 // If it's none of the above, it's not ours.
246                 return false;
247         }
248         return true;
249 }
250
251
252 graphics::Params InsetGraphicsParams::as_grfxParams() const
253 {
254         graphics::Params pars;
255         pars.filename = filename;
256         pars.scale = lyxscale;
257         pars.angle = convert<double>(rotateAngle);
258
259         if (clip) {
260                 pars.bb = bbox;
261
262                 // Get the original Bounding Box from the file
263                 string const tmp = graphics::readBB_from_PSFile(filename);
264                 LYXERR(Debug::GRAPHICS, "BB_from_File: " << tmp);
265                 if (!tmp.empty()) {
266                         unsigned int const bb_orig_xl = convert<unsigned int>(token(tmp, ' ', 0));
267                         unsigned int const bb_orig_yb = convert<unsigned int>(token(tmp, ' ', 1));
268
269                         // new pars.bb values must be >= zero
270                         if (pars.bb.xl.inBP() > static_cast<int>(bb_orig_xl))
271                                 pars.bb.xl = Length(pars.bb.xl.inBP() - bb_orig_xl, Length::BP);
272                         else
273                                 pars.bb.xl = Length();
274
275                         if (pars.bb.xr.inBP() > static_cast<int>(bb_orig_xl))
276                                 pars.bb.xr = Length(pars.bb.xr.inBP() - bb_orig_xl, Length::BP);
277                         else
278                                 pars.bb.xr = Length();
279
280                         if (pars.bb.yb.inBP() > static_cast<int>(bb_orig_yb))
281                                 pars.bb.yb = Length(pars.bb.yb.inBP() - bb_orig_yb, Length::BP);
282                         else
283                                 pars.bb.yb = Length();
284
285                         if (pars.bb.yt.inBP() > static_cast<int>(bb_orig_yb))
286                                 pars.bb.yt = Length(pars.bb.yt.inBP() - bb_orig_yb, Length::BP);
287                         else
288                                 pars.bb.yt = Length();
289                 }
290
291                 // Paranoia check.
292                 int const width  = pars.bb.xr.inBP() - pars.bb.xl.inBP();
293                 int const height = pars.bb.yt.inBP() - pars.bb.yb.inBP();
294
295                 if (width  < 0 || height < 0) {
296                         pars.bb.xl = Length();
297                         pars.bb.xr = Length();
298                         pars.bb.yb = Length();
299                         pars.bb.yt = Length();
300                 }
301         }
302
303         pars.display = display;
304
305         // Override the above if we're not using a gui
306         if (!use_gui)
307                 pars.display = false;
308
309         return pars;
310 }
311
312
313 } // namespace lyx