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"
16 #include "FuncRequest.h"
17 #include "FuncStatus.h"
18 #include "Dimension.h"
20 #include "MetricsInfo.h"
21 #include "OutputParams.h"
23 #include "frontends/FontMetrics.h"
24 #include "frontends/Painter.h"
26 #include "support/debug.h"
27 #include "support/docstring.h"
28 #include "support/docstream.h"
34 InsetNewline::InsetNewline()
37 void InsetNewlineParams::write(ostream & os) const
41 case InsetNewlineParams::NEWLINE:
44 case InsetNewlineParams::LINEBREAK:
51 void InsetNewlineParams::read(Lexer & lex)
54 string const command = lex.getString();
56 if (command == "newline")
57 kind = InsetNewlineParams::NEWLINE;
58 else if (command == "linebreak")
59 kind = InsetNewlineParams::LINEBREAK;
61 lex.printError("InsetNewline: Unknown kind: `$$Token'");
67 if (token != "\\end_inset")
68 lex.printError("Missing \\end_inset at this point. "
73 void InsetNewline::write(ostream & os) const
80 void InsetNewline::read(Lexer & lex)
86 void InsetNewline::metrics(MetricsInfo & mi, Dimension & dim) const
88 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
89 dim.asc = fm.maxAscent();
90 dim.des = fm.maxDescent();
91 dim.wid = fm.width('n');
95 void InsetNewline::doDispatch(Cursor & cur, FuncRequest & cmd)
99 case LFUN_INSET_MODIFY: {
100 InsetNewlineParams params;
101 InsetNewlineMailer::string2params(to_utf8(cmd.argument()), params);
102 params_.kind = params.kind;
107 Inset::doDispatch(cur, cmd);
113 bool InsetNewline::getStatus(Cursor & cur, FuncRequest const & cmd,
114 FuncStatus & status) const
116 switch (cmd.action) {
118 case LFUN_INSET_MODIFY:
119 if (cmd.getArg(0) == "newline") {
120 InsetNewlineParams params;
121 InsetNewlineMailer::string2params(to_utf8(cmd.argument()), params);
122 status.setOnOff(params_.kind == params.kind);
124 status.enabled(true);
127 return Inset::getStatus(cur, cmd, status);
132 ColorCode InsetNewline::ColorName() const
134 switch (params_.kind) {
135 case InsetNewlineParams::NEWLINE:
136 return Color_eolmarker;
138 case InsetNewlineParams::LINEBREAK:
139 return Color_pagebreak;
142 return Color_eolmarker;
148 int InsetNewline::latex(odocstream & os, OutputParams const &) const
150 switch (params_.kind) {
151 case InsetNewlineParams::NEWLINE:
154 case InsetNewlineParams::LINEBREAK:
155 os << "\\linebreak{}\n";
165 int InsetNewline::plaintext(odocstream & os, OutputParams const &) const
168 return PLAINTEXT_NEWLINE;
172 int InsetNewline::docbook(odocstream & os, OutputParams const &) const
179 void InsetNewline::draw(PainterInfo & pi, int x, int y) const
182 font.setColor(ColorName());
184 frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
185 int const wid = fm.width('n');
186 int const asc = fm.maxAscent();
191 yp[0] = int(y - 0.875 * asc * 0.75);
192 yp[1] = int(y - 0.500 * asc * 0.75);
193 yp[2] = int(y - 0.125 * asc * 0.75);
196 xp[0] = int(x + wid * 0.375);
198 xp[2] = int(x + wid * 0.375);
200 xp[0] = int(x + wid * 0.625);
201 xp[1] = int(x + wid);
202 xp[2] = int(x + wid * 0.625);
205 pi.pain.lines(xp, yp, 3, ColorName());
207 yp[0] = int(y - 0.500 * asc * 0.75);
208 yp[1] = int(y - 0.500 * asc * 0.75);
209 yp[2] = int(y - asc * 0.75);
213 xp[1] = int(x + wid);
214 xp[2] = int(x + wid);
216 xp[0] = int(x + wid);
221 pi.pain.lines(xp, yp, 3, ColorName());
223 if (params_.kind == InsetNewlineParams::LINEBREAK) {
225 yp[2] = int(y - 0.500 * asc * 0.75);
228 xp[0] = int(x + 1.3 * wid);
229 xp[1] = int(x + 2 * wid);
230 xp[2] = int(x + 2 * wid);
232 xp[0] = int(x - 0.3 * wid);
233 xp[1] = int(x - wid);
234 xp[2] = int(x - wid);
236 pi.pain.lines(xp, yp, 3, ColorName());
238 yp[0] = int(y - 0.875 * asc * 0.75);
239 yp[1] = int(y - 0.500 * asc * 0.75);
240 yp[2] = int(y - 0.125 * asc * 0.75);
243 xp[0] = int(x + 2 * wid * 0.813);
244 xp[1] = int(x + 2 * wid);
245 xp[2] = int(x + 2 * wid * 0.813);
247 xp[0] = int(x - wid * 0.625);
248 xp[1] = int(x - wid);
249 xp[2] = int(x - wid * 0.625);
251 pi.pain.lines(xp, yp, 3, ColorName());
256 docstring InsetNewline::contextMenu(BufferView const &, int, int) const
258 return from_ascii("context-newline");
262 string const InsetNewlineMailer::name_ = "newline";
265 InsetNewlineMailer::InsetNewlineMailer(InsetNewline & inset)
270 string const InsetNewlineMailer::inset2string(Buffer const &) const
272 return params2string(inset_.params());
276 void InsetNewlineMailer::string2params(string const & in, InsetNewlineParams & params)
278 params = InsetNewlineParams();
282 istringstream data(in);
288 if (!lex || name != name_)
289 return print_mailer_error("InsetNewlineMailer", in, 1, name_);
295 string const InsetNewlineMailer::params2string(InsetNewlineParams const & params)
298 data << name_ << ' ';