]> git.lyx.org Git - lyx.git/blob - src/insets/InsetVSpace.cpp
InsetIndex is no longer an InsetCommand.
[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 "Lexer.h"
22 #include "Text.h"
23 #include "MetricsInfo.h"
24 #include "OutputParams.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 Inset * InsetVSpace::clone() const
60 {
61         return new InsetVSpace(*this);
62 }
63
64
65 void InsetVSpace::doDispatch(Cursor & 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                 if (!cur.selection())
76                         InsetVSpaceMailer(*this).showDialog(&cur.bv());
77                 break;
78
79         default:
80                 Inset::doDispatch(cur, cmd);
81                 break;
82         }
83 }
84
85
86 void InsetVSpace::read(Buffer const &, Lexer & lex)
87 {
88         BOOST_ASSERT(lex.isOK());
89         string vsp;
90         lex >> vsp;
91         if (lex)
92                 space_ = VSpace(vsp);
93
94         string end_token;
95         lex >> end_token;
96         if (end_token != "\\end_inset")
97                 lex.printError("Missing \\end_inset at this point. "
98                                "Read: `$$Token'");
99 }
100
101
102 void InsetVSpace::write(Buffer const &, ostream & os) const
103 {
104         os << "VSpace " << space_.asLyXCommand();
105 }
106
107
108 docstring const InsetVSpace::label() const
109 {
110         static docstring const label = _("Vertical Space");
111         return label + " (" + 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         Font 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         // Cache the inset dimension. 
141         setDimCache(mi, dim);
142 }
143
144
145 void InsetVSpace::draw(PainterInfo & pi, int x, int y) const
146 {
147         Dimension const dim = dimension(*pi.base.bv);
148         x += ADD_TO_VSPACE_WIDTH;
149         int const start = y - dim.asc;
150         int const end   = y + dim.des;
151
152         // y-values for top arrow
153         int ty1, ty2;
154         // y-values for bottom arrow
155         int by1, by2;
156
157         if (space_.kind() == VSpace::VFILL) {
158                 ty1 = ty2 = start;
159                 by1 = by2 = end;
160         } else {
161                 // adding or removing space
162                 bool const added = space_.kind() != VSpace::LENGTH ||
163                                    space_.length().len().value() >= 0.0;
164                 ty1 = added ? (start + arrow_size) : start;
165                 ty2 = added ? start : (start + arrow_size);
166                 by1 = added ? (end - arrow_size) : end;
167                 by2 = added ? end : (end - arrow_size);
168         }
169
170         int const midx = x + arrow_size;
171         int const rightx = midx + arrow_size;
172
173         // first the string
174         int w = 0;
175         int a = 0;
176         int d = 0;
177
178         Font font;
179         font.setColor(Color_added_space);
180         font.decSize();
181         font.decSize();
182         docstring const lab = label();
183         theFontMetrics(font).rectText(lab, w, a, d);
184
185         pi.pain.rectText(x + 2 * arrow_size + 5,
186                          start + (end - start) / 2 + (a - d) / 2,
187                          lab, font, Color_none, Color_none);
188
189         // top arrow
190         pi.pain.line(x, ty1, midx, ty2, Color_added_space);
191         pi.pain.line(midx, ty2, rightx, ty1, Color_added_space);
192
193         // bottom arrow
194         pi.pain.line(x, by1, midx, by2, Color_added_space);
195         pi.pain.line(midx, by2, rightx, by1, Color_added_space);
196
197         // joining line
198         pi.pain.line(midx, ty2, midx, by2, Color_added_space);
199 }
200
201
202 int InsetVSpace::latex(Buffer const & buf, odocstream & os,
203                        OutputParams const &) const
204 {
205         os << from_ascii(space_.asLatexCommand(buf.params())) << '\n';
206         return 1;
207 }
208
209
210 int InsetVSpace::plaintext(Buffer const &, odocstream & os,
211                            OutputParams const &) const
212 {
213         os << "\n\n";
214         return PLAINTEXT_NEWLINE;
215 }
216
217
218 int InsetVSpace::docbook(Buffer const &, odocstream & os,
219                          OutputParams const &) const
220 {
221         os << '\n';
222         return 1;
223 }
224
225
226 string const InsetVSpaceMailer::name_ = "vspace";
227
228
229 InsetVSpaceMailer::InsetVSpaceMailer(InsetVSpace & inset)
230         : inset_(inset)
231 {}
232
233
234 string const InsetVSpaceMailer::inset2string(Buffer const &) const
235 {
236         return params2string(inset_.space());
237 }
238
239
240 void InsetVSpaceMailer::string2params(string const & in, VSpace & vspace)
241 {
242         vspace = VSpace();
243         if (in.empty())
244                 return;
245
246         istringstream data(in);
247         Lexer lex(0,0);
248         lex.setStream(data);
249
250         string name;
251         lex >> name;
252         if (!lex || name != name_)
253                 return print_mailer_error("InsetVSpaceMailer", in, 1, name_);
254
255         string vsp;
256         lex >> vsp;
257         if (lex)
258                 vspace = VSpace(vsp);
259 }
260
261
262 string const InsetVSpaceMailer::params2string(VSpace const & vspace)
263 {
264         ostringstream data;
265         data << name_ << ' ' << vspace.asLyXCommand();
266         return data.str();
267 }
268
269
270 } // namespace lyx