2 * \file InsetNewline.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "InsetNewline.h"
17 #include "Dimension.h"
18 #include "FuncRequest.h"
19 #include "FuncStatus.h"
20 #include "MetricsInfo.h"
21 #include "output_docbook.h"
22 #include "output_xhtml.h"
23 #include "texstream.h"
25 #include "frontends/Application.h"
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/docstring.h"
32 #include "support/Lexer.h"
40 InsetNewline::InsetNewline() : Inset(nullptr)
44 int InsetNewline::rowFlags() const
46 if (params_.kind == InsetNewlineParams::LINEBREAK)
47 return AlwaysBreakAfter;
49 return AlwaysBreakAfter | Flush;
53 void InsetNewlineParams::write(ostream & os) const
56 case InsetNewlineParams::NEWLINE:
59 case InsetNewlineParams::LINEBREAK:
66 void InsetNewlineParams::read(Lexer & lex)
69 lex.setContext("InsetNewlineParams::read");
71 if (token == "newline")
72 kind = InsetNewlineParams::NEWLINE;
73 else if (token == "linebreak")
74 kind = InsetNewlineParams::LINEBREAK;
76 lex.printError("Unknown kind: `$$Token'");
80 void InsetNewline::write(ostream & os) const
87 void InsetNewline::read(Lexer & lex)
94 void InsetNewline::metrics(MetricsInfo & mi, Dimension & dim) const
96 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
97 dim.asc = fm.maxAscent();
98 dim.des = fm.maxDescent();
99 dim.wid = fm.width('n');
103 void InsetNewline::doDispatch(Cursor & cur, FuncRequest & cmd)
105 switch (cmd.action()) {
107 case LFUN_INSET_MODIFY: {
108 InsetNewlineParams params;
110 string2params(to_utf8(cmd.argument()), params);
111 params_.kind = params.kind;
116 Inset::doDispatch(cur, cmd);
122 bool InsetNewline::getStatus(Cursor & cur, FuncRequest const & cmd,
123 FuncStatus & status) const
125 switch (cmd.action()) {
127 case LFUN_INSET_MODIFY:
128 if (cmd.getArg(0) == "newline") {
129 InsetNewlineParams params;
130 string2params(to_utf8(cmd.argument()), params);
131 status.setOnOff(params_.kind == params.kind);
133 status.setEnabled(true);
136 return Inset::getStatus(cur, cmd, status);
141 ColorCode InsetNewline::ColorName() const
143 switch (params_.kind) {
144 case InsetNewlineParams::NEWLINE:
145 return Color_eolmarker;
147 case InsetNewlineParams::LINEBREAK:
148 return Color_pagebreak;
151 // not really useful, but to avoids gcc complaints
152 return Color_eolmarker;
156 void InsetNewline::latex(otexstream & os, OutputParams const & rp) const
158 switch (params_.kind) {
159 case InsetNewlineParams::NEWLINE:
160 if (!rp.newlinecmd.empty())
161 os << "\\" << rp.newlinecmd << "\n";
162 else if (rp.inTableCell == OutputParams::PLAIN)
167 case InsetNewlineParams::LINEBREAK:
168 os << "\\linebreak{}\n";
177 int InsetNewline::plaintext(odocstringstream & os,
178 OutputParams const &, size_t) const
181 return PLAINTEXT_NEWLINE;
185 void InsetNewline::docbook(XMLStream &, OutputParams const &) const
187 // New lines are handled by Paragraph::simpleDocBookOnePar.
191 docstring InsetNewline::xhtml(XMLStream & xs, OutputParams const &) const
193 xs << xml::CR() << xml::CompTag("br") << xml::CR();
198 void InsetNewline::draw(PainterInfo & pi, int x, int y) const
201 font.setColor(ColorName());
203 frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
204 int const wid = fm.width('n');
205 int const asc = fm.maxAscent();
210 yp[0] = int(y - 0.875 * asc * 0.75);
211 yp[1] = int(y - 0.500 * asc * 0.75);
212 yp[2] = int(y - 0.125 * asc * 0.75);
215 xp[0] = int(x + wid * 0.375);
217 xp[2] = int(x + wid * 0.375);
219 xp[0] = int(x + wid * 0.625);
220 xp[1] = int(x + wid);
221 xp[2] = int(x + wid * 0.625);
224 pi.pain.lines(xp, yp, 3, ColorName());
226 yp[0] = int(y - 0.500 * asc * 0.75);
227 yp[1] = int(y - 0.500 * asc * 0.75);
228 yp[2] = int(y - asc * 0.75);
232 xp[1] = int(x + wid);
233 xp[2] = int(x + wid);
235 xp[0] = int(x + wid);
240 pi.pain.lines(xp, yp, 3, ColorName());
242 if (params_.kind == InsetNewlineParams::LINEBREAK) {
244 yp[2] = int(y - 0.500 * asc * 0.75);
247 xp[0] = int(x + 1.3 * wid);
248 xp[1] = int(x + 2 * wid);
249 xp[2] = int(x + 2 * wid);
251 xp[0] = int(x - 0.3 * wid);
252 xp[1] = int(x - wid);
253 xp[2] = int(x - wid);
255 pi.pain.lines(xp, yp, 3, ColorName());
257 yp[0] = int(y - 0.875 * asc * 0.75);
258 yp[1] = int(y - 0.500 * asc * 0.75);
259 yp[2] = int(y - 0.125 * asc * 0.75);
262 xp[0] = int(x + 2 * wid * 0.813);
263 xp[1] = int(x + 2 * wid);
264 xp[2] = int(x + 2 * wid * 0.813);
266 xp[0] = int(x - wid * 0.625);
267 xp[1] = int(x - wid);
268 xp[2] = int(x - wid * 0.625);
270 pi.pain.lines(xp, yp, 3, ColorName());
275 string InsetNewline::contextMenuName() const
277 return "context-newline";
281 void InsetNewline::string2params(string const & in, InsetNewlineParams & params)
283 params = InsetNewlineParams();
286 istringstream data(in);
289 lex.setContext("InsetNewline::string2params");
295 string InsetNewline::params2string(InsetNewlineParams const & params)
298 data << "newline" << ' ';