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