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"
20 #include "MetricsInfo.h"
21 #include "output_docbook.h"
22 #include "output_xhtml.h"
23 #include "texstream.h"
25 #include "frontends/Application.h"
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/docstring.h"
34 using namespace lyx::frontend;
38 InsetSeparator::InsetSeparator() : Inset(nullptr)
42 InsetSeparator::InsetSeparator(InsetSeparatorParams const & params)
43 : Inset(nullptr), params_(params)
47 void InsetSeparatorParams::write(ostream & os) const
50 case InsetSeparatorParams::PLAIN:
53 case InsetSeparatorParams::PARBREAK:
56 case InsetSeparatorParams::LATEXPAR:
63 void InsetSeparatorParams::read(Lexer & lex)
66 lex.setContext("InsetSeparatorParams::read");
69 kind = InsetSeparatorParams::PLAIN;
70 else if (token == "parbreak")
71 kind = InsetSeparatorParams::PARBREAK;
72 else if (token == "latexpar")
73 kind = InsetSeparatorParams::LATEXPAR;
75 lex.printError("Unknown kind: `$$Token'");
79 void InsetSeparator::write(ostream & os) const
86 void InsetSeparator::read(Lexer & lex)
93 void InsetSeparator::doDispatch(Cursor & cur, FuncRequest & cmd)
95 switch (cmd.action()) {
97 case LFUN_INSET_MODIFY: {
98 InsetSeparatorParams params;
100 string2params(to_utf8(cmd.argument()), params);
101 params_.kind = params.kind;
106 Inset::doDispatch(cur, cmd);
112 bool InsetSeparator::getStatus(Cursor & cur, FuncRequest const & cmd,
113 FuncStatus & status) const
115 switch (cmd.action()) {
117 case LFUN_INSET_MODIFY: {
118 if (cmd.getArg(0) != "separator")
120 InsetSeparatorParams params;
121 string2params(to_utf8(cmd.argument()), params);
122 status.setOnOff(params_.kind == params.kind);
123 status.setEnabled(true);
127 return Inset::getStatus(cur, cmd, status);
133 ColorCode InsetSeparator::ColorName() const
139 void InsetSeparator::latex(otexstream & os, OutputParams const & runparams) const
141 // Do nothing if a paragraph break was just output
142 if (!os.afterParbreak()) {
143 switch (params_.kind) {
144 case InsetSeparatorParams::PLAIN:
145 os << breakln << "%\n";
147 case InsetSeparatorParams::PARBREAK:
148 case InsetSeparatorParams::LATEXPAR:
149 if (runparams.inDeletedInset)
150 os << breakln << "}\n\n{";
152 os << breakln << "\n";
155 os << breakln << "%\n";
162 int InsetSeparator::plaintext(odocstringstream & os,
163 OutputParams const &, size_t) const
166 return PLAINTEXT_NEWLINE;
170 void InsetSeparator::docbook(XMLStream & xs, OutputParams const &) const
176 docstring InsetSeparator::xhtml(XMLStream & xs, OutputParams const &) const
178 xs << xml::CR() << xml::CompTag("br") << xml::CR();
183 void InsetSeparator::metrics(MetricsInfo & mi, Dimension & dim) const
185 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
186 dim.asc = fm.maxAscent();
187 dim.des = fm.maxDescent();
188 dim.wid = fm.width('n');
189 if (params_.kind != InsetSeparatorParams::LATEXPAR)
194 void InsetSeparator::draw(PainterInfo & pi, int x, int y) const
197 font.setColor(ColorName());
199 frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
200 int const wid = fm.width('n');
201 int const asc = fm.maxAscent();
206 if (params_.kind != InsetSeparatorParams::LATEXPAR) {
207 yp[0] = int(y - 0.500 * asc * 0.75);
211 xp[1] = int(x + wid * 8);
213 pi.pain.lines(xp, yp, 2, ColorName());
215 if (params_.kind == InsetSeparatorParams::PARBREAK) {
216 yp[0] += int(0.25 * asc * 0.75);
218 pi.pain.lines(xp, yp, 2, ColorName());
221 yp[0] = int(y - 0.500 * asc * 0.5);
222 yp[1] = int(y - 0.250 * asc * 0.5);
226 xp[0] = int(x + 1 + wid * 0.375);
229 xp[0] = int(x - 1 + wid * 0.625);
230 xp[1] = int(x - 1 + wid);
234 pi.pain.lines(xp, yp, 3, ColorName(), Painter::fill_oddeven);
237 yp[2] = int(y - 0.850 * asc * 0.5);
238 yp[3] = int(y - 1.250 * asc * 0.5);
245 xp[1] = int(x + 1 + wid * 0.50);
246 xp[2] = int(x + wid);
248 xp[4] = int(x + wid * 0.75);
250 xp[1] = int(x + wid * 0.50);
253 xp[4] = int(x + wid * 0.25);
263 for (int i = 1; i < 7; ++i) {
270 int d = pi.ltr_pos ? yp[4] - yp[5] : yp[5] - yp[4];
273 c2y[2] = int(y - 0.500 * asc * 0.5);
277 pi.pain.path(xp, yp, c1x, c1y, c2x, c2y, 7, ColorName());
282 string InsetSeparator::contextMenuName() const
284 if (params_.kind == InsetSeparatorParams::LATEXPAR)
287 return "context-separator";
291 void InsetSeparator::string2params(string const & in, InsetSeparatorParams & params)
293 params = InsetSeparatorParams();
296 istringstream data(in);
299 lex.setContext("InsetSeparator::string2params");
305 string InsetSeparator::params2string(InsetSeparatorParams const & params)
308 data << "separator" << ' ';