]> git.lyx.org Git - lyx.git/blob - src/insets/InsetVSpace.cpp
Fix for bug 4135
[lyx.git] / src / insets / InsetVSpace.cpp
1 /**
2  * \file InsetVSpace.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author various
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetVSpace.h"
15
16 #include "Buffer.h"
17 #include "Cursor.h"
18 #include "DispatchResult.h"
19 #include "FuncRequest.h"
20 #include "gettext.h"
21 #include "Color.h"
22 #include "Lexer.h"
23 #include "Text.h"
24 #include "MetricsInfo.h"
25 #include "OutputParams.h"
26
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
29
30 #include <sstream>
31
32
33 namespace lyx {
34
35 using std::istringstream;
36 using std::ostream;
37 using std::ostringstream;
38 using std::string;
39 using std::max;
40
41
42 namespace {
43
44 int const ADD_TO_VSPACE_WIDTH = 5;
45
46 } // namespace anon
47
48
49 InsetVSpace::InsetVSpace(VSpace const & space)
50         : space_(space)
51 {}
52
53
54 InsetVSpace::~InsetVSpace()
55 {
56         InsetVSpaceMailer(*this).hideDialog();
57 }
58
59
60 std::auto_ptr<Inset> InsetVSpace::doClone() const
61 {
62         return std::auto_ptr<Inset>(new InsetVSpace(*this));
63 }
64
65
66 void InsetVSpace::doDispatch(Cursor & cur, FuncRequest & cmd)
67 {
68         switch (cmd.action) {
69
70         case LFUN_INSET_MODIFY: {
71                 InsetVSpaceMailer::string2params(to_utf8(cmd.argument()), space_);
72                 break;
73         }
74
75         case LFUN_MOUSE_RELEASE:
76                 if (!cur.selection())
77                         InsetVSpaceMailer(*this).showDialog(&cur.bv());
78                 break;
79
80         default:
81                 Inset::doDispatch(cur, cmd);
82                 break;
83         }
84 }
85
86
87 void InsetVSpace::read(Buffer const &, Lexer & lex)
88 {
89         BOOST_ASSERT(lex.isOK());
90         string vsp;
91         lex >> vsp;
92         if (lex)
93                 space_ = VSpace(vsp);
94
95         string end_token;
96         lex >> end_token;
97         if (end_token != "\\end_inset")
98                 lex.printError("Missing \\end_inset at this point. "
99                                "Read: `$$Token'");
100 }
101
102
103 void InsetVSpace::write(Buffer const &, ostream & os) const
104 {
105         os << "VSpace " << space_.asLyXCommand();
106 }
107
108
109 docstring const InsetVSpace::label() const
110 {
111         static docstring const label = _("Vertical Space");
112         return label + " (" + space_.asGUIName() + ')';
113 }
114
115
116 namespace {
117 int const arrow_size = 4;
118 }
119
120
121 bool InsetVSpace::metrics(MetricsInfo & mi, Dimension & dim) const
122 {
123         int height = 3 * arrow_size;
124         if (space_.length().len().value() >= 0.0)
125                 height = max(height, space_.inPixels(*mi.base.bv));
126
127         Font font;
128         font.decSize();
129         font.decSize();
130
131         int w = 0;
132         int a = 0;
133         int d = 0;
134         theFontMetrics(font).rectText(label(), w, a, d);
135
136         height = max(height, a + d);
137
138         dim.asc = height / 2 + (a - d) / 2; // align cursor with the
139         dim.des = height - dim.asc;         // label text
140         dim.wid = ADD_TO_VSPACE_WIDTH + 2 * arrow_size + 5 + w;
141         bool const changed = dim_ != dim;
142         dim_ = dim;
143         return changed;
144 }
145
146
147 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
148 {
149         setPosCache(pi, x, y);
150
151         x += ADD_TO_VSPACE_WIDTH;
152
153         int const start = y - dim_.asc;
154         int const end   = y + dim_.des;
155
156         // y-values for top arrow
157         int ty1, ty2;
158         // y-values for bottom arrow
159         int by1, by2;
160
161         if (space_.kind() == VSpace::VFILL) {
162                 ty1 = ty2 = start;
163                 by1 = by2 = end;
164         } else {
165                 // adding or removing space
166                 bool const added = space_.kind() != VSpace::LENGTH ||
167                                    space_.length().len().value() >= 0.0;
168                 ty1 = added ? (start + arrow_size) : start;
169                 ty2 = added ? start : (start + arrow_size);
170                 by1 = added ? (end - arrow_size) : end;
171                 by2 = added ? end : (end - arrow_size);
172         }
173
174         int const midx = x + arrow_size;
175         int const rightx = midx + arrow_size;
176
177         // first the string
178         int w = 0;
179         int a = 0;
180         int d = 0;
181
182         Font font;
183         font.setColor(Color::added_space);
184         font.decSize();
185         font.decSize();
186         docstring const lab = label();
187         theFontMetrics(font).rectText(lab, w, a, d);
188
189         pi.pain.rectText(x + 2 * arrow_size + 5,
190                          start + (end - start) / 2 + (a - d) / 2,
191                          lab, font, Color::none, Color::none);
192
193         // top arrow
194         pi.pain.line(x, ty1, midx, ty2, Color::added_space);
195         pi.pain.line(midx, ty2, rightx, ty1, Color::added_space);
196
197         // bottom arrow
198         pi.pain.line(x, by1, midx, by2, Color::added_space);
199         pi.pain.line(midx, by2, rightx, by1, Color::added_space);
200
201         // joining line
202         pi.pain.line(midx, ty2, midx, by2, Color::added_space);
203 }
204
205
206 int InsetVSpace::latex(Buffer const & buf, odocstream & os,
207                        OutputParams const &) const
208 {
209         os << from_ascii(space_.asLatexCommand(buf.params())) << '\n';
210         return 1;
211 }
212
213
214 int InsetVSpace::plaintext(Buffer const &, odocstream & os,
215                            OutputParams const &) const
216 {
217         os << "\n\n";
218         return PLAINTEXT_NEWLINE;
219 }
220
221
222 int InsetVSpace::docbook(Buffer const &, odocstream & os,
223                          OutputParams const &) const
224 {
225         os << '\n';
226         return 1;
227 }
228
229
230 string const InsetVSpaceMailer::name_ = "vspace";
231
232
233 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
234         : inset_(inset)
235 {}
236
237
238 string const InsetVSpaceMailer::inset2string(Buffer const &) const
239 {
240         return params2string(inset_.space());
241 }
242
243
244 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
245 {
246         vspace = VSpace();
247         if (in.empty())
248                 return;
249
250         istringstream data(in);
251         Lexer lex(0,0);
252         lex.setStream(data);
253
254         string name;
255         lex >> name;
256         if (!lex || name != name_)
257                 return print_mailer_error("InsetVSpaceMailer", in, 1, name_);
258
259         string vsp;
260         lex >> vsp;
261         if (lex)
262                 vspace = VSpace(vsp);
263 }
264
265
266 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
267 {
268         ostringstream data;
269         data << name_ << ' ' << vspace.asLyXCommand();
270         return data.str();
271 }
272
273
274 } // namespace lyx