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