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 "OutputParams.h"
22 #include "output_docbook.h"
23 #include "output_xhtml.h"
24 #include "texstream.h"
26 #include "frontends/Application.h"
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/docstring.h"
35 using namespace lyx::frontend;
39 InsetSeparator::InsetSeparator() : Inset(0)
43 InsetSeparator::InsetSeparator(InsetSeparatorParams const & params)
44 : Inset(0), params_(params)
48 void InsetSeparatorParams::write(ostream & os) const
51 case InsetSeparatorParams::PLAIN:
54 case InsetSeparatorParams::PARBREAK:
57 case InsetSeparatorParams::LATEXPAR:
64 void InsetSeparatorParams::read(Lexer & lex)
67 lex.setContext("InsetSeparatorParams::read");
70 kind = InsetSeparatorParams::PLAIN;
71 else if (token == "parbreak")
72 kind = InsetSeparatorParams::PARBREAK;
73 else if (token == "latexpar")
74 kind = InsetSeparatorParams::LATEXPAR;
76 lex.printError("Unknown kind: `$$Token'");
80 void InsetSeparator::write(ostream & os) const
87 void InsetSeparator::read(Lexer & lex)
94 void InsetSeparator::doDispatch(Cursor & cur, FuncRequest & cmd)
96 switch (cmd.action()) {
98 case LFUN_INSET_MODIFY: {
99 InsetSeparatorParams params;
101 string2params(to_utf8(cmd.argument()), params);
102 params_.kind = params.kind;
107 Inset::doDispatch(cur, cmd);
113 bool InsetSeparator::getStatus(Cursor & cur, FuncRequest const & cmd,
114 FuncStatus & status) const
116 switch (cmd.action()) {
118 case LFUN_INSET_MODIFY: {
119 if (cmd.getArg(0) != "separator")
121 InsetSeparatorParams params;
122 string2params(to_utf8(cmd.argument()), params);
123 status.setOnOff(params_.kind == params.kind);
124 status.setEnabled(true);
128 return Inset::getStatus(cur, cmd, status);
134 ColorCode InsetSeparator::ColorName() const
140 void InsetSeparator::latex(otexstream & os, OutputParams const & runparams) const
142 // Do nothing if a paragraph break was just output
143 if (!os.afterParbreak()) {
144 switch (params_.kind) {
145 case InsetSeparatorParams::PLAIN:
146 os << breakln << "%\n";
148 case InsetSeparatorParams::PARBREAK:
149 case InsetSeparatorParams::LATEXPAR:
150 if (runparams.inDeletedInset)
151 os << breakln << "}\n\n{";
153 os << breakln << "\n";
156 os << breakln << "%\n";
163 int InsetSeparator::plaintext(odocstringstream & os,
164 OutputParams const &, size_t) const
167 return PLAINTEXT_NEWLINE;
171 void InsetSeparator::docbook(XMLStream & xs, OutputParams const &) const
177 docstring InsetSeparator::xhtml(XMLStream & xs, OutputParams const &) const
179 xs << xml::CR() << xml::CompTag("br") << xml::CR();
184 void InsetSeparator::metrics(MetricsInfo & mi, Dimension & dim) const
186 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
187 dim.asc = fm.maxAscent();
188 dim.des = fm.maxDescent();
189 dim.wid = fm.width('n');
190 if (params_.kind != InsetSeparatorParams::LATEXPAR)
195 void InsetSeparator::draw(PainterInfo & pi, int x, int y) const
198 font.setColor(ColorName());
200 frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
201 int const wid = fm.width('n');
202 int const asc = fm.maxAscent();
207 if (params_.kind != InsetSeparatorParams::LATEXPAR) {
208 yp[0] = int(y - 0.500 * asc * 0.75);
212 xp[1] = int(x + wid * 8);
214 pi.pain.lines(xp, yp, 2, ColorName());
216 if (params_.kind == InsetSeparatorParams::PARBREAK) {
217 yp[0] += int(0.25 * asc * 0.75);
219 pi.pain.lines(xp, yp, 2, ColorName());
222 yp[0] = int(y - 0.500 * asc * 0.5);
223 yp[1] = int(y - 0.250 * asc * 0.5);
227 xp[0] = int(x + 1 + wid * 0.375);
230 xp[0] = int(x - 1 + wid * 0.625);
231 xp[1] = int(x - 1 + wid);
235 pi.pain.lines(xp, yp, 3, ColorName(), Painter::fill_oddeven);
238 yp[2] = int(y - 0.850 * asc * 0.5);
239 yp[3] = int(y - 1.250 * asc * 0.5);
246 xp[1] = int(x + 1 + wid * 0.50);
247 xp[2] = int(x + wid);
249 xp[4] = int(x + wid * 0.75);
251 xp[1] = int(x + wid * 0.50);
254 xp[4] = int(x + wid * 0.25);
264 for (int i = 1; i < 7; ++i) {
271 int d = pi.ltr_pos ? yp[4] - yp[5] : yp[5] - yp[4];
274 c2y[2] = int(y - 0.500 * asc * 0.5);
278 pi.pain.path(xp, yp, c1x, c1y, c2x, c2y, 7, ColorName());
283 string InsetSeparator::contextMenuName() const
285 if (params_.kind == InsetSeparatorParams::LATEXPAR)
288 return "context-separator";
292 void InsetSeparator::string2params(string const & in, InsetSeparatorParams & params)
294 params = InsetSeparatorParams();
297 istringstream data(in);
300 lex.setContext("InsetSeparator::string2params");
306 string InsetSeparator::params2string(InsetSeparatorParams const & params)
309 data << "separator" << ' ';