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