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"
21 #include "MetricsInfo.h"
22 #include "output_docbook.h"
23 #include "output_xhtml.h"
24 #include "texstream.h"
26 #include "frontends/Application.h"
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/docstring.h"
38 InsetNewline::InsetNewline() : Inset(nullptr)
42 int InsetNewline::rowFlags() const
44 if (params_.kind == InsetNewlineParams::LINEBREAK)
45 return AlwaysBreakAfter;
47 return AlwaysBreakAfter | Flush;
51 void InsetNewlineParams::write(ostream & os) const
54 case InsetNewlineParams::NEWLINE:
57 case InsetNewlineParams::LINEBREAK:
64 void InsetNewlineParams::read(Lexer & lex)
67 lex.setContext("InsetNewlineParams::read");
69 if (token == "newline")
70 kind = InsetNewlineParams::NEWLINE;
71 else if (token == "linebreak")
72 kind = InsetNewlineParams::LINEBREAK;
74 lex.printError("Unknown kind: `$$Token'");
78 void InsetNewline::write(ostream & os) const
85 void InsetNewline::read(Lexer & lex)
92 void InsetNewline::metrics(MetricsInfo & mi, Dimension & dim) const
94 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
95 dim.asc = fm.maxAscent();
96 dim.des = fm.maxDescent();
97 dim.wid = fm.width('n');
101 void InsetNewline::doDispatch(Cursor & cur, FuncRequest & cmd)
103 switch (cmd.action()) {
105 case LFUN_INSET_MODIFY: {
106 InsetNewlineParams params;
108 string2params(to_utf8(cmd.argument()), params);
109 params_.kind = params.kind;
114 Inset::doDispatch(cur, cmd);
120 bool InsetNewline::getStatus(Cursor & cur, FuncRequest const & cmd,
121 FuncStatus & status) const
123 switch (cmd.action()) {
125 case LFUN_INSET_MODIFY:
126 if (cmd.getArg(0) == "newline") {
127 InsetNewlineParams params;
128 string2params(to_utf8(cmd.argument()), params);
129 status.setOnOff(params_.kind == params.kind);
131 status.setEnabled(true);
134 return Inset::getStatus(cur, cmd, status);
139 ColorCode InsetNewline::ColorName() const
141 switch (params_.kind) {
142 case InsetNewlineParams::NEWLINE:
143 return Color_eolmarker;
145 case InsetNewlineParams::LINEBREAK:
146 return Color_pagebreak;
149 // not really useful, but to avoids gcc complaints
150 return Color_eolmarker;
154 void InsetNewline::latex(otexstream & os, OutputParams const & rp) const
156 switch (params_.kind) {
157 case InsetNewlineParams::NEWLINE:
158 if (!rp.newlinecmd.empty())
159 os << "\\" << rp.newlinecmd << "\n";
160 else if (rp.inTableCell == OutputParams::PLAIN)
165 case InsetNewlineParams::LINEBREAK:
166 os << "\\linebreak{}\n";
175 int InsetNewline::plaintext(odocstringstream & os,
176 OutputParams const &, size_t) const
179 return PLAINTEXT_NEWLINE;
183 void InsetNewline::docbook(XMLStream &, OutputParams const &) const
185 // New lines are handled by Paragraph::simpleDocBookOnePar.
189 docstring InsetNewline::xhtml(XMLStream & xs, OutputParams const &) const
191 xs << xml::CR() << xml::CompTag("br") << xml::CR();
196 void InsetNewline::draw(PainterInfo & pi, int x, int y) const
199 font.setColor(ColorName());
201 frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
202 int const wid = fm.width('n');
203 int const asc = fm.maxAscent();
208 yp[0] = int(y - 0.875 * asc * 0.75);
209 yp[1] = int(y - 0.500 * asc * 0.75);
210 yp[2] = int(y - 0.125 * asc * 0.75);
213 xp[0] = int(x + wid * 0.375);
215 xp[2] = int(x + wid * 0.375);
217 xp[0] = int(x + wid * 0.625);
218 xp[1] = int(x + wid);
219 xp[2] = int(x + wid * 0.625);
222 pi.pain.lines(xp, yp, 3, ColorName());
224 yp[0] = int(y - 0.500 * asc * 0.75);
225 yp[1] = int(y - 0.500 * asc * 0.75);
226 yp[2] = int(y - asc * 0.75);
230 xp[1] = int(x + wid);
231 xp[2] = int(x + wid);
233 xp[0] = int(x + wid);
238 pi.pain.lines(xp, yp, 3, ColorName());
240 if (params_.kind == InsetNewlineParams::LINEBREAK) {
242 yp[2] = int(y - 0.500 * asc * 0.75);
245 xp[0] = int(x + 1.3 * wid);
246 xp[1] = int(x + 2 * wid);
247 xp[2] = int(x + 2 * wid);
249 xp[0] = int(x - 0.3 * wid);
250 xp[1] = int(x - wid);
251 xp[2] = int(x - wid);
253 pi.pain.lines(xp, yp, 3, ColorName());
255 yp[0] = int(y - 0.875 * asc * 0.75);
256 yp[1] = int(y - 0.500 * asc * 0.75);
257 yp[2] = int(y - 0.125 * asc * 0.75);
260 xp[0] = int(x + 2 * wid * 0.813);
261 xp[1] = int(x + 2 * wid);
262 xp[2] = int(x + 2 * wid * 0.813);
264 xp[0] = int(x - wid * 0.625);
265 xp[1] = int(x - wid);
266 xp[2] = int(x - wid * 0.625);
268 pi.pain.lines(xp, yp, 3, ColorName());
273 string InsetNewline::contextMenuName() const
275 return "context-newline";
279 void InsetNewline::string2params(string const & in, InsetNewlineParams & params)
281 params = InsetNewlineParams();
284 istringstream data(in);
287 lex.setContext("InsetNewline::string2params");
293 string InsetNewline::params2string(InsetNewlineParams const & params)
296 data << "newline" << ' ';