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 "OutputParams.h"
23 #include "output_docbook.h"
24 #include "output_xhtml.h"
25 #include "texstream.h"
27 #include "frontends/Application.h"
28 #include "frontends/FontMetrics.h"
29 #include "frontends/Painter.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/docstring.h"
39 InsetNewline::InsetNewline() : Inset(0)
43 void InsetNewlineParams::write(ostream & os) const
46 case InsetNewlineParams::NEWLINE:
49 case InsetNewlineParams::LINEBREAK:
56 void InsetNewlineParams::read(Lexer & lex)
59 lex.setContext("InsetNewlineParams::read");
61 if (token == "newline")
62 kind = InsetNewlineParams::NEWLINE;
63 else if (token == "linebreak")
64 kind = InsetNewlineParams::LINEBREAK;
66 lex.printError("Unknown kind: `$$Token'");
70 void InsetNewline::write(ostream & os) const
77 void InsetNewline::read(Lexer & lex)
84 void InsetNewline::metrics(MetricsInfo & mi, Dimension & dim) const
86 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
87 dim.asc = fm.maxAscent();
88 dim.des = fm.maxDescent();
89 dim.wid = fm.width('n');
93 void InsetNewline::doDispatch(Cursor & cur, FuncRequest & cmd)
95 switch (cmd.action()) {
97 case LFUN_INSET_MODIFY: {
98 InsetNewlineParams params;
100 string2params(to_utf8(cmd.argument()), params);
101 params_.kind = params.kind;
106 Inset::doDispatch(cur, cmd);
112 bool InsetNewline::getStatus(Cursor & cur, FuncRequest const & cmd,
113 FuncStatus & status) const
115 switch (cmd.action()) {
117 case LFUN_INSET_MODIFY:
118 if (cmd.getArg(0) == "newline") {
119 InsetNewlineParams params;
120 string2params(to_utf8(cmd.argument()), params);
121 status.setOnOff(params_.kind == params.kind);
123 status.setEnabled(true);
126 return Inset::getStatus(cur, cmd, status);
131 ColorCode InsetNewline::ColorName() const
133 switch (params_.kind) {
134 case InsetNewlineParams::NEWLINE:
135 return Color_eolmarker;
137 case InsetNewlineParams::LINEBREAK:
138 return Color_pagebreak;
141 // not really useful, but to avoids gcc complaints
142 return Color_eolmarker;
146 void InsetNewline::latex(otexstream & os, OutputParams const & rp) const
148 switch (params_.kind) {
149 case InsetNewlineParams::NEWLINE:
150 if (!rp.newlinecmd.empty())
151 os << "\\" << rp.newlinecmd << "\n";
152 else if (rp.inTableCell == OutputParams::PLAIN)
157 case InsetNewlineParams::LINEBREAK:
158 os << "\\linebreak{}\n";
167 int InsetNewline::plaintext(odocstringstream & os,
168 OutputParams const &, size_t) const
171 return PLAINTEXT_NEWLINE;
175 void InsetNewline::docbook(XMLStream & xs, OutputParams const & runparams) const
177 if (runparams.docbook_in_par) {
179 if (!xs.pending_tags_empty()) {
180 xs << xml::EndTag("para");
181 xs << xml::StartTag("para");
184 xs << xml::CR() << xml::CompTag("br") << xml::CR();
188 xs << xml::CR() << xml::CompTag("br") << xml::CR();
193 docstring InsetNewline::xhtml(XMLStream & xs, OutputParams const &) const
195 xs << xml::CR() << xml::CompTag("br") << xml::CR();
200 void InsetNewline::draw(PainterInfo & pi, int x, int y) const
203 font.setColor(ColorName());
205 frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
206 int const wid = fm.width('n');
207 int const asc = fm.maxAscent();
212 yp[0] = int(y - 0.875 * asc * 0.75);
213 yp[1] = int(y - 0.500 * asc * 0.75);
214 yp[2] = int(y - 0.125 * asc * 0.75);
217 xp[0] = int(x + wid * 0.375);
219 xp[2] = int(x + wid * 0.375);
221 xp[0] = int(x + wid * 0.625);
222 xp[1] = int(x + wid);
223 xp[2] = int(x + wid * 0.625);
226 pi.pain.lines(xp, yp, 3, ColorName());
228 yp[0] = int(y - 0.500 * asc * 0.75);
229 yp[1] = int(y - 0.500 * asc * 0.75);
230 yp[2] = int(y - asc * 0.75);
234 xp[1] = int(x + wid);
235 xp[2] = int(x + wid);
237 xp[0] = int(x + wid);
242 pi.pain.lines(xp, yp, 3, ColorName());
244 if (params_.kind == InsetNewlineParams::LINEBREAK) {
246 yp[2] = int(y - 0.500 * asc * 0.75);
249 xp[0] = int(x + 1.3 * wid);
250 xp[1] = int(x + 2 * wid);
251 xp[2] = int(x + 2 * wid);
253 xp[0] = int(x - 0.3 * wid);
254 xp[1] = int(x - wid);
255 xp[2] = int(x - wid);
257 pi.pain.lines(xp, yp, 3, ColorName());
259 yp[0] = int(y - 0.875 * asc * 0.75);
260 yp[1] = int(y - 0.500 * asc * 0.75);
261 yp[2] = int(y - 0.125 * asc * 0.75);
264 xp[0] = int(x + 2 * wid * 0.813);
265 xp[1] = int(x + 2 * wid);
266 xp[2] = int(x + 2 * wid * 0.813);
268 xp[0] = int(x - wid * 0.625);
269 xp[1] = int(x - wid);
270 xp[2] = int(x - wid * 0.625);
272 pi.pain.lines(xp, yp, 3, ColorName());
277 string InsetNewline::contextMenuName() const
279 return "context-newline";
283 void InsetNewline::string2params(string const & in, InsetNewlineParams & params)
285 params = InsetNewlineParams();
288 istringstream data(in);
291 lex.setContext("InsetNewline::string2params");
297 string InsetNewline::params2string(InsetNewlineParams const & params)
300 data << "newline" << ' ';