2 * \file InsetSeparator.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Enrico Forestieri
8 * Full author contact details are available in file CREDITS.
13 #include "InsetSeparator.h"
16 #include "Dimension.h"
17 #include "FuncRequest.h"
18 #include "FuncStatus.h"
19 #include "MetricsInfo.h"
20 #include "output_docbook.h"
21 #include "output_xhtml.h"
22 #include "texstream.h"
24 #include "frontends/Application.h"
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
28 #include "support/debug.h"
29 #include "support/docstream.h"
30 #include "support/docstring.h"
31 #include "support/Lexer.h"
34 using namespace lyx::frontend;
40 InsetSeparator::InsetSeparator() : Inset(nullptr)
44 InsetSeparator::InsetSeparator(InsetSeparatorParams const & params)
45 : Inset(nullptr), params_(params)
49 void InsetSeparatorParams::write(ostream & os) const
52 case InsetSeparatorParams::PLAIN:
55 case InsetSeparatorParams::PARBREAK:
58 case InsetSeparatorParams::LATEXPAR:
65 void InsetSeparatorParams::read(Lexer & lex)
68 lex.setContext("InsetSeparatorParams::read");
71 kind = InsetSeparatorParams::PLAIN;
72 else if (token == "parbreak")
73 kind = InsetSeparatorParams::PARBREAK;
74 else if (token == "latexpar")
75 kind = InsetSeparatorParams::LATEXPAR;
77 lex.printError("Unknown kind: `$$Token'");
81 void InsetSeparator::write(ostream & os) const
88 void InsetSeparator::read(Lexer & lex)
95 void InsetSeparator::doDispatch(Cursor & cur, FuncRequest & cmd)
97 switch (cmd.action()) {
99 case LFUN_INSET_MODIFY: {
100 InsetSeparatorParams params;
102 string2params(to_utf8(cmd.argument()), params);
103 params_.kind = params.kind;
108 Inset::doDispatch(cur, cmd);
114 bool InsetSeparator::getStatus(Cursor & cur, FuncRequest const & cmd,
115 FuncStatus & status) const
117 switch (cmd.action()) {
119 case LFUN_INSET_MODIFY: {
120 if (cmd.getArg(0) != "separator")
122 InsetSeparatorParams params;
123 string2params(to_utf8(cmd.argument()), params);
124 status.setOnOff(params_.kind == params.kind);
125 status.setEnabled(true);
129 return Inset::getStatus(cur, cmd, status);
135 ColorCode InsetSeparator::ColorName() const
141 void InsetSeparator::latex(otexstream & os, OutputParams const & runparams) const
143 // Do nothing if a paragraph break was just output
144 if (!os.afterParbreak()) {
145 switch (params_.kind) {
146 case InsetSeparatorParams::PLAIN:
147 os << breakln << "%\n";
149 case InsetSeparatorParams::PARBREAK:
150 case InsetSeparatorParams::LATEXPAR:
151 if (runparams.inDeletedInset)
152 os << breakln << "}\n\n{";
154 os << breakln << "\n";
157 os << breakln << "%\n";
164 int InsetSeparator::plaintext(odocstringstream & os,
165 OutputParams const &, size_t) const
168 return PLAINTEXT_NEWLINE;
172 void InsetSeparator::docbook(XMLStream & xs, OutputParams const &) const
178 docstring InsetSeparator::xhtml(XMLStream & xs, OutputParams const &) const
180 xs << xml::CR() << xml::CompTag("br") << xml::CR();
185 void InsetSeparator::metrics(MetricsInfo & mi, Dimension & dim) const
187 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
188 dim.asc = fm.maxAscent();
189 dim.des = fm.maxDescent();
190 dim.wid = fm.width('n');
191 if (params_.kind != InsetSeparatorParams::LATEXPAR)
196 void InsetSeparator::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 if (params_.kind != InsetSeparatorParams::LATEXPAR) {
209 yp[0] = int(y - 0.500 * asc * 0.75);
213 xp[1] = int(x + wid * 8);
215 pi.pain.lines(xp, yp, 2, ColorName());
217 if (params_.kind == InsetSeparatorParams::PARBREAK) {
218 yp[0] += int(0.25 * asc * 0.75);
220 pi.pain.lines(xp, yp, 2, ColorName());
223 yp[0] = int(y - 0.500 * asc * 0.5);
224 yp[1] = int(y - 0.250 * asc * 0.5);
228 xp[0] = int(x + 1 + wid * 0.375);
231 xp[0] = int(x - 1 + wid * 0.625);
232 xp[1] = int(x - 1 + wid);
236 pi.pain.lines(xp, yp, 3, ColorName(), Painter::fill_oddeven);
239 yp[2] = int(y - 0.850 * asc * 0.5);
240 yp[3] = int(y - 1.250 * asc * 0.5);
247 xp[1] = int(x + 1 + wid * 0.50);
248 xp[2] = int(x + wid);
250 xp[4] = int(x + wid * 0.75);
252 xp[1] = int(x + wid * 0.50);
255 xp[4] = int(x + wid * 0.25);
265 for (int i = 1; i < 7; ++i) {
272 int d = pi.ltr_pos ? yp[4] - yp[5] : yp[5] - yp[4];
275 c2y[2] = int(y - 0.500 * asc * 0.5);
279 pi.pain.path(xp, yp, c1x, c1y, c2x, c2y, 7, ColorName());
284 string InsetSeparator::contextMenuName() const
286 if (params_.kind == InsetSeparatorParams::LATEXPAR)
289 return "context-separator";
293 void InsetSeparator::string2params(string const & in, InsetSeparatorParams & params)
295 params = InsetSeparatorParams();
298 istringstream data(in);
301 lex.setContext("InsetSeparator::string2params");
307 string InsetSeparator::params2string(InsetSeparatorParams const & params)
310 data << "separator" << ' ';