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 "Dimension.h"
17 #include "FuncRequest.h"
18 #include "FuncStatus.h"
20 #include "MetricsInfo.h"
21 #include "OutputParams.h"
23 #include "frontends/Application.h"
24 #include "frontends/FontMetrics.h"
25 #include "frontends/Painter.h"
27 #include "support/debug.h"
28 #include "support/docstream.h"
29 #include "support/docstring.h"
35 InsetNewline::InsetNewline()
39 void InsetNewlineParams::write(ostream & os) const
43 case InsetNewlineParams::NEWLINE:
46 case InsetNewlineParams::LINEBREAK:
53 void InsetNewlineParams::read(Lexer & lex)
56 string const command = lex.getString();
58 if (command == "newline")
59 kind = InsetNewlineParams::NEWLINE;
60 else if (command == "linebreak")
61 kind = InsetNewlineParams::LINEBREAK;
63 lex.printError("InsetNewline: Unknown kind: `$$Token'");
69 if (token != "\\end_inset")
70 lex.printError("Missing \\end_inset at this point. "
75 void InsetNewline::write(ostream & os) const
82 void InsetNewline::read(Lexer & lex)
88 void InsetNewline::metrics(MetricsInfo & mi, Dimension & dim) const
90 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
91 dim.asc = fm.maxAscent();
92 dim.des = fm.maxDescent();
93 dim.wid = fm.width('n');
97 void InsetNewline::doDispatch(Cursor & cur, FuncRequest & cmd)
101 case LFUN_INSET_MODIFY: {
102 InsetNewlineParams params;
103 string2params(to_utf8(cmd.argument()), params);
104 params_.kind = params.kind;
109 Inset::doDispatch(cur, cmd);
115 bool InsetNewline::getStatus(Cursor & cur, FuncRequest const & cmd,
116 FuncStatus & status) const
118 switch (cmd.action) {
120 case LFUN_INSET_MODIFY:
121 if (cmd.getArg(0) == "newline") {
122 InsetNewlineParams params;
123 string2params(to_utf8(cmd.argument()), params);
124 status.setOnOff(params_.kind == params.kind);
126 status.enabled(true);
130 return Inset::getStatus(cur, cmd, status);
135 ColorCode InsetNewline::ColorName() const
137 switch (params_.kind) {
138 case InsetNewlineParams::NEWLINE:
139 return Color_eolmarker;
141 case InsetNewlineParams::LINEBREAK:
142 return Color_pagebreak;
145 return Color_eolmarker;
151 int InsetNewline::latex(odocstream & os, OutputParams const &) const
153 switch (params_.kind) {
154 case InsetNewlineParams::NEWLINE:
157 case InsetNewlineParams::LINEBREAK:
158 os << "\\linebreak{}\n";
168 int InsetNewline::plaintext(odocstream & os, OutputParams const &) const
171 return PLAINTEXT_NEWLINE;
175 int InsetNewline::docbook(odocstream & os, OutputParams const &) const
182 void InsetNewline::draw(PainterInfo & pi, int x, int y) const
185 font.setColor(ColorName());
187 frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
188 int const wid = fm.width('n');
189 int const asc = fm.maxAscent();
194 yp[0] = int(y - 0.875 * asc * 0.75);
195 yp[1] = int(y - 0.500 * asc * 0.75);
196 yp[2] = int(y - 0.125 * asc * 0.75);
199 xp[0] = int(x + wid * 0.375);
201 xp[2] = int(x + wid * 0.375);
203 xp[0] = int(x + wid * 0.625);
204 xp[1] = int(x + wid);
205 xp[2] = int(x + wid * 0.625);
208 pi.pain.lines(xp, yp, 3, ColorName());
210 yp[0] = int(y - 0.500 * asc * 0.75);
211 yp[1] = int(y - 0.500 * asc * 0.75);
212 yp[2] = int(y - asc * 0.75);
216 xp[1] = int(x + wid);
217 xp[2] = int(x + wid);
219 xp[0] = int(x + wid);
224 pi.pain.lines(xp, yp, 3, ColorName());
226 if (params_.kind == InsetNewlineParams::LINEBREAK) {
228 yp[2] = int(y - 0.500 * asc * 0.75);
231 xp[0] = int(x + 1.3 * wid);
232 xp[1] = int(x + 2 * wid);
233 xp[2] = int(x + 2 * wid);
235 xp[0] = int(x - 0.3 * wid);
236 xp[1] = int(x - wid);
237 xp[2] = int(x - wid);
239 pi.pain.lines(xp, yp, 3, ColorName());
241 yp[0] = int(y - 0.875 * asc * 0.75);
242 yp[1] = int(y - 0.500 * asc * 0.75);
243 yp[2] = int(y - 0.125 * asc * 0.75);
246 xp[0] = int(x + 2 * wid * 0.813);
247 xp[1] = int(x + 2 * wid);
248 xp[2] = int(x + 2 * wid * 0.813);
250 xp[0] = int(x - wid * 0.625);
251 xp[1] = int(x - wid);
252 xp[2] = int(x - wid * 0.625);
254 pi.pain.lines(xp, yp, 3, ColorName());
259 docstring InsetNewline::contextMenu(BufferView const &, int, int) const
261 return from_ascii("context-newline");
265 void InsetNewline::string2params(string const & in, InsetNewlineParams & params)
267 params = InsetNewlineParams();
271 istringstream data(in);
277 if (!lex || name != "newline") {
278 LYXERR0("Expected arg 1 to be \"newlien\" in " << in);
286 string InsetNewline::params2string(InsetNewlineParams const & params)
289 data << "newline" << ' ';